]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-ldap/bind.c
queue implicit binds (ITS#4409)
[openldap] / servers / slapd / back-ldap / bind.c
index 5c5a40e70712a4549929b4a7ea43526979a50577..b8fc62aaafe67777b32b8c834a23b5d32dfde60a 100644 (file)
@@ -91,6 +91,8 @@ ldap_back_bind( Operation *op, SlapReply *rs )
                        }
                }
 
+               assert( lc->lc_binding == 1 );
+               lc->lc_binding = 0;
                LDAP_BACK_CONN_ISBOUND_SET( lc );
                ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
 
@@ -122,12 +124,12 @@ retry_lock:;
 
                assert( lc->lc_refcnt == 1 );
                lc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
-                               ldap_back_conn_cmp );
+                               ldap_back_conndn_cmp );
                assert( lc != NULL );
 
                ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
                lerr = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
-                       ldap_back_conn_cmp, ldap_back_conn_dup );
+                       ldap_back_conndn_cmp, ldap_back_conndn_dup );
                ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
                if ( lerr == -1 ) {
                        /* we can do this because lc_refcnt == 1 */
@@ -144,24 +146,43 @@ retry_lock:;
 }
 
 /*
- * ldap_back_conn_cmp
+ * ldap_back_conndn_cmp
  *
- * compares two ldapconn_t based on the value of the conn pointer;
- * used by avl stuff
+ * compares two ldapconn_t based on the value of the conn pointer
+ * and of the local DN; used by avl stuff for insert, lookup
+ * and direct delete
  */
 int
-ldap_back_conn_cmp( const void *c1, const void *c2 )
+ldap_back_conndn_cmp( const void *c1, const void *c2 )
 {
        const ldapconn_t        *lc1 = (const ldapconn_t *)c1;
        const ldapconn_t        *lc2 = (const ldapconn_t *)c2;
        int rc;
 
        /* If local DNs don't match, it is definitely not a match */
-       rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
-       if ( rc ) {
-               return rc;
+       /* For shared sessions, conn is NULL. Only explicitly
+        * bound sessions will have non-NULL conn.
+        */
+       rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
+       if ( rc == 0 ) {
+               rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
        }
 
+       return rc;
+}
+
+/*
+ * ldap_back_conn_cmp
+ *
+ * compares two ldapconn_t based on the value of the conn pointer;
+ * used by avl stuff for delete of all conns with the same connid
+ */
+int
+ldap_back_conn_cmp( const void *c1, const void *c2 )
+{
+       const ldapconn_t        *lc1 = (const ldapconn_t *)c1;
+       const ldapconn_t        *lc2 = (const ldapconn_t *)c2;
+
        /* For shared sessions, conn is NULL. Only explicitly
         * bound sessions will have non-NULL conn.
         */
@@ -169,20 +190,20 @@ ldap_back_conn_cmp( const void *c1, const void *c2 )
 }
 
 /*
- * ldap_back_conn_dup
+ * ldap_back_conndn_dup
  *
  * returns -1 in case a duplicate ldapconn_t has been inserted;
  * used by avl stuff
  */
 int
-ldap_back_conn_dup( void *c1, void *c2 )
+ldap_back_conndn_dup( void *c1, void *c2 )
 {
        ldapconn_t      *lc1 = (ldapconn_t *)c1;
        ldapconn_t      *lc2 = (ldapconn_t *)c2;
 
        /* Cannot have more than one shared session with same DN */
-       if ( dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) &&
-                               lc1->lc_conn == lc2->lc_conn )
+       if ( lc1->lc_conn == lc2->lc_conn &&
+               dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) )
        {
                return -1;
        }
@@ -204,12 +225,12 @@ ravl_print( Avlnode *root, int depth )
        ravl_print( root->avl_right, depth+1 );
        
        for ( i = 0; i < depth; i++ ) {
-               printf( "   " );
+               fprintf( stderr, "-" );
        }
 
        lc = root->avl_data;
-       printf( "lc(%lx) local(%s) conn(%lx) %s\n",
-               lc, lc->lc_local_ndn.bv_val, lc->lc_conn,
+       fprintf( stderr, "lc=%p local=\"%s\" conn=%p %s\n",
+               (void *)lc, lc->lc_local_ndn.bv_val, (void *)lc->lc_conn,
                avl_bf2str( root->avl_bf) );
        
        ravl_print( root->avl_left, depth+1 );
@@ -218,16 +239,16 @@ ravl_print( Avlnode *root, int depth )
 static void
 myprint( Avlnode *root )
 {
-       printf( "********\n" );
+       fprintf( stderr, "========>\n" );
        
        if ( root == 0 ) {
-               printf( "\tNULL\n" );
+               fprintf( stderr, "\tNULL\n" );
 
        } else {
                ravl_print( root, 0 );
        }
        
-       printf( "********\n" );
+       fprintf( stderr, "<========\n" );
 }
 #endif /* PRINT_CONNTREE */
 
@@ -243,7 +264,7 @@ ldap_back_freeconn( Operation *op, ldapconn_t *lc, int dolock )
        assert( lc->lc_refcnt > 0 );
        if ( --lc->lc_refcnt == 0 ) {
                lc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
-                               ldap_back_conn_cmp );
+                               ldap_back_conndn_cmp );
                assert( lc != NULL );
 
                ldap_back_conn_free( (void *)lc );
@@ -446,6 +467,7 @@ ldap_back_prepare_conn( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_bac
        }
        (*lcp)->lc_ld = ld;
        (*lcp)->lc_refcnt = 1;
+       (*lcp)->lc_binding = 1;
 #ifdef HAVE_TLS
        if ( is_tls ) {
                LDAP_BACK_CONN_ISTLS_SET( *lcp );
@@ -480,7 +502,7 @@ ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok )
        ldapinfo_t      *li = (ldapinfo_t *)op->o_bd->be_private;
        ldapconn_t      *lc = NULL,
                        lc_curr = { 0 };
-       int             refcnt = 1;
+       int             refcnt = 1, binding = 1;
 
        /* Internal searches are privileged and shared. So is root. */
        if ( op->o_do_not_cache || be_isroot( op ) ) {
@@ -506,7 +528,7 @@ retry_lock:
                ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
 
                lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree, 
-                               (caddr_t)&lc_curr, ldap_back_conn_cmp );
+                               (caddr_t)&lc_curr, ldap_back_conndn_cmp );
                if ( lc != NULL ) {
                        /* Don't reuse connections while they're still binding */
                        if ( LDAP_BACK_CONN_BINDING( lc ) ) {
@@ -515,6 +537,7 @@ retry_lock:
                                goto retry_lock;
                        }
                        refcnt = ++lc->lc_refcnt;
+                       binding = ++lc->lc_binding;
                }
                ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
        }
@@ -562,9 +585,10 @@ retry_lock:
                        lc_curr.lc_conn = LDAP_BACK_PCONN;
                        ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
                        tmplc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree, 
-                                       (caddr_t)&lc_curr, ldap_back_conn_cmp );
+                                       (caddr_t)&lc_curr, ldap_back_conndn_cmp );
                        if ( tmplc != NULL ) {
                                refcnt = ++tmplc->lc_refcnt;
+                               binding = ++tmplc->lc_binding;
                                ldap_back_conn_free( lc );
                                lc = tmplc;
                        }
@@ -582,8 +606,9 @@ retry_lock:
                ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
 
                assert( lc->lc_refcnt == 1 );
+               assert( lc->lc_binding == 1 );
                rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
-                       ldap_back_conn_cmp, ldap_back_conn_dup );
+                       ldap_back_conndn_cmp, ldap_back_conndn_dup );
 
 #if PRINT_CONNTREE > 0
                myprint( li->li_conninfo.lai_tree );
@@ -592,8 +617,8 @@ retry_lock:
                ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
 
                Debug( LDAP_DEBUG_TRACE,
-                       "=>ldap_back_getconn: conn %p inserted (refcnt=%u)\n",
-                       (void *)lc, refcnt, 0 );
+                       "=>ldap_back_getconn: conn %p inserted refcnt=%u binding=%u\n",
+                       (void *)lc, refcnt, binding );
        
                /* Err could be -1 in case a duplicate ldapconn is inserted */
                if ( rs->sr_err != 0 ) {
@@ -618,8 +643,8 @@ retry_lock:
 
                if ( lc ) {
                        Debug( LDAP_DEBUG_TRACE,
-                               "=>ldap_back_getconn: conn %p fetched (refcnt=%u)\n",
-                               (void *)lc, refcnt, 0 );
+                               "=>ldap_back_getconn: conn %p fetched refcnt=%u binding=%u\n",
+                               (void *)lc, refcnt, binding );
                }
        }
 
@@ -671,15 +696,57 @@ ldap_back_dobind_int(
 {      
        ldapinfo_t      *li = (ldapinfo_t *)op->o_bd->be_private;
 
-       int             rc = LDAP_BACK_CONN_ISBOUND( lc );
+       int             rc, binding = 0;
        ber_int_t       msgid;
 
        assert( retries >= 0 );
 
-       if ( rc ) {
-               return rc;
+retry_lock:;
+       if ( dolock ) {
+               ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
+       }
+
+       if ( binding == 0 ) {
+               /* check if already bound */
+               rc = LDAP_BACK_CONN_ISBOUND( lc );
+               if ( rc ) {
+                       lc->lc_binding--;
+                       if ( dolock ) {
+                               ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
+                       }
+                       return rc;
+               }
+
+               if ( LDAP_BACK_CONN_BINDING( lc ) ) {
+                       /* if someone else is about to bind it, give up and retry */
+                       if ( dolock ) {
+                               ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
+                       }
+                       ldap_pvt_thread_yield();
+                       goto retry_lock;
+
+               } else {
+                       /* otherwise this thread will bind it */
+                       LDAP_BACK_CONN_BINDING_SET( lc );
+                       binding = 1;
+               }
        }
 
+       /* wait for pending operations to finish */
+       /* FIXME: may become a bottleneck! */
+       if ( lc->lc_refcnt != lc->lc_binding ) {
+               if ( dolock ) {
+                       ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
+               }
+               ldap_pvt_thread_yield();
+               goto retry_lock;
+       }
+
+       if ( dolock ) {
+               ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
+       }
+
+#if 0
        while ( lc->lc_refcnt > 1 ) {
                ldap_pvt_thread_yield();
                rc = LDAP_BACK_CONN_ISBOUND( lc );
@@ -695,6 +762,7 @@ ldap_back_dobind_int(
        if ( dolock ) {
                ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
        }
+#endif
 
        /*
         * FIXME: we need to let clients use proxyAuthz
@@ -803,6 +871,7 @@ retry:;
                        }
                }
 
+               lc->lc_binding--;
                ldap_back_freeconn( op, lc, dolock );
                rs->sr_err = slap_map_api2result( rs );
 
@@ -815,6 +884,7 @@ retry:;
        }
 
 done:;
+       lc->lc_binding--;
        LDAP_BACK_CONN_BINDING_CLEAR( lc );
        rc = LDAP_BACK_CONN_ISBOUND( lc );
        if ( !rc ) {
@@ -930,16 +1000,6 @@ retry:;
                default:
                        rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
                                        &match, &text, NULL, NULL, 1 );
-#ifndef LDAP_NULL_IS_NULL
-                       if ( match != NULL && match[ 0 ] == '\0' ) {
-                               ldap_memfree( match );
-                               match = NULL;
-                       }
-                       if ( text != NULL && text[ 0 ] == '\0' ) {
-                               ldap_memfree( text );
-                               text = NULL;
-                       }
-#endif /* LDAP_NULL_IS_NULL */
                        rs->sr_text = text;
                        if ( rc != LDAP_SUCCESS ) {
                                rs->sr_err = rc;