]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-bdb/search.c
Fix IRIX sc_mask conflict
[openldap] / servers / slapd / back-bdb / search.c
index 0c1d283c56c6eb8c4f67f03ec04e15756cb4622f..3bea37794c7c29de6cfcd5e97767fc7a143e7632 100644 (file)
@@ -44,7 +44,6 @@ bdb_search(
        int             attrsonly )
 {
        struct bdb_info *bdb = (struct bdb_info *) be->be_private;
-       int              abandon;
        int             rc;
        const char *text = NULL;
        time_t          stoptime;
@@ -60,11 +59,20 @@ bdb_search(
        struct slap_limits_set *limit = NULL;
        int isroot = 0;
 
+       u_int32_t       locker;
+       DB_LOCK         lock;
+
+#ifdef NEW_LOGGING
+       LDAP_LOG ( OPERATION, ENTRY, "bdb_back_search\n", 0, 0, 0 );
+#else
        Debug( LDAP_DEBUG_TRACE, "=> bdb_back_search\n",
                0, 0, 0);
+#endif
 
        manageDSAit = get_manageDSAit( op );
 
+       LOCK_ID (bdb->bi_dbenv, &locker );
+
        if ( nbase->bv_len == 0 ) {
                /* DIT root special case */
                e = (Entry *) &slap_entry_root;
@@ -78,22 +86,38 @@ bdb_search(
        } else
 #endif
        {
-               rc = bdb_dn2entry_r( be, NULL, nbase, &e, &matched, 0 );
+dn2entry_retry:
+               rc = bdb_dn2entry_r( be, NULL, nbase, &e, &matched, 0, locker, &lock );
        }
 
        switch(rc) {
        case DB_NOTFOUND:
        case 0:
                break;
+       case LDAP_BUSY:
+               if (e != NULL) {
+                       bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
+               }
+               if (matched != NULL) {
+                       bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
+               }
+               send_ldap_result( conn, op, LDAP_BUSY,
+                       NULL, "ldap server busy", NULL, NULL );
+               LOCK_ID_FREE (bdb->bi_dbenv, locker );
+               return LDAP_BUSY;
+       case DB_LOCK_DEADLOCK:
+       case DB_LOCK_NOTGRANTED:
+               goto dn2entry_retry;
        default:
                if (e != NULL) {
-                       bdb_cache_return_entry_w(&bdb->bi_cache, e);
+                       bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
                }
                if (matched != NULL) {
-                       bdb_cache_return_entry_r(&bdb->bi_cache, matched);
+                       bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
                }
                send_ldap_result( conn, op, rc=LDAP_OTHER,
                        NULL, "internal error", NULL, NULL );
+               LOCK_ID_FREE (bdb->bi_dbenv, locker );
                return rc;
        }
 
@@ -103,14 +127,13 @@ bdb_search(
 
                if ( matched != NULL ) {
                        BerVarray erefs;
-
                        ber_dupbv( &matched_dn, &matched->e_name );
 
                        erefs = is_entry_referral( matched )
                                ? get_entry_referrals( be, conn, op, matched )
                                : NULL;
 
-                       bdb_cache_return_entry_r (&bdb->bi_cache, matched);
+                       bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
                        matched = NULL;
 
                        if( erefs ) {
@@ -127,6 +150,7 @@ bdb_search(
                send_ldap_result( conn, op,     rc=LDAP_REFERRAL ,
                        matched_dn.bv_val, text, refs, NULL );
 
+               LOCK_ID_FREE (bdb->bi_dbenv, locker );
                if ( refs ) ber_bvarray_free( refs );
                if ( matched_dn.bv_val ) ber_memfree( matched_dn.bv_val );
                return rc;
@@ -141,7 +165,7 @@ bdb_search(
                erefs = get_entry_referrals( be, conn, op, e );
                refs = NULL;
 
-               bdb_cache_return_entry_r( &bdb->bi_cache, e );
+               bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
                e = NULL;
 
                if( erefs ) {
@@ -150,14 +174,20 @@ bdb_search(
                        ber_bvarray_free( erefs );
                }
 
+#ifdef NEW_LOGGING
+               LDAP_LOG ( OPERATION, RESULTS, 
+                       "bdb_search: entry is referral\n", 0, 0, 0 );
+#else
                Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
                        0, 0, 0 );
+#endif
 
                send_ldap_result( conn, op, LDAP_REFERRAL,
                        matched_dn.bv_val,
                        refs ? NULL : "bad referral object",
                        refs, NULL );
 
+               LOCK_ID_FREE (bdb->bi_dbenv, locker );
                ber_bvarray_free( refs );
                ber_memfree( matched_dn.bv_val );
                return 1;
@@ -192,7 +222,7 @@ bdb_search(
                /* if requested limit higher than hard limit, abort */
                } else if ( tlimit > limit->lms_t_hard ) {
                        /* no hard limit means use soft instead */
-                       if ( limit->lms_t_hard == 0 ) {
+                       if ( limit->lms_t_hard == 0 && tlimit > limit->lms_t_soft ) {
                                tlimit = limit->lms_t_soft;
 
                        /* positive hard limit means abort */
@@ -214,7 +244,7 @@ bdb_search(
                /* if requested limit higher than hard limit, abort */
                } else if ( slimit > limit->lms_s_hard ) {
                        /* no hard limit means use soft instead */
-                       if ( limit->lms_s_hard == 0 ) {
+                       if ( limit->lms_s_hard == 0 && slimit > limit->lms_s_soft ) {
                                slimit = limit->lms_s_soft;
 
                        /* positive hard limit means abort */
@@ -253,13 +283,17 @@ bdb_search(
        cursor = e->e_id == NOID ? 1 : e->e_id;
 
        if ( e != &slap_entry_root ) {
-               bdb_cache_return_entry_r(&bdb->bi_cache, e);
+               bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
        }
        e = NULL;
 
        if ( candidates[0] == 0 ) {
+#ifdef NEW_LOGGING
+       LDAP_LOG ( OPERATION, RESULTS, "bdb_search: no candidates\n", 0, 0, 0 );
+#else
                Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
                        0, 0, 0 );
+#endif
 
                send_search_result( conn, op,
                        LDAP_SUCCESS,
@@ -273,7 +307,7 @@ bdb_search(
        if ( !isroot && limit->lms_s_unchecked != -1 ) {
                if ( BDB_IDL_N(candidates) > (unsigned) limit->lms_s_unchecked ) {
                        send_search_result( conn, op, 
-                                       LDAP_UNWILLING_TO_PERFORM,
+                                       LDAP_ADMINLIMIT_EXCEEDED,
                                        NULL, NULL, NULL, NULL, 0 );
                        rc = 1;
                        goto done;
@@ -287,11 +321,7 @@ bdb_search(
                int             scopeok = 0;
 
                /* check for abandon */
-               ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
-               abandon = op->o_abandon;
-               ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
-
-               if ( abandon ) {
+               if ( op->o_abandon ) {
                        rc = 0;
                        goto done;
                }
@@ -303,15 +333,30 @@ bdb_search(
                        goto done;
                }
 
+id2entry_retry:
                /* get the entry with reader lock */
-               rc = bdb_id2entry_r( be, NULL, id, &e );
+               rc = bdb_id2entry_r( be, NULL, id, &e, locker, &lock );
+
+               if (rc == LDAP_BUSY) {
+                       send_ldap_result( conn, op, rc=LDAP_BUSY,
+                               NULL, "ldap server busy", NULL, NULL );
+                       goto done;
+
+               } else if ( rc == DB_LOCK_DEADLOCK || rc == DB_LOCK_NOTGRANTED ) {
+                       goto id2entry_retry;    
+               }
 
                if ( e == NULL ) {
                        if( !BDB_IDL_IS_RANGE(candidates) ) {
                                /* only complain for non-range IDLs */
+#ifdef NEW_LOGGING
+                               LDAP_LOG ( OPERATION, RESULTS,
+                                       "bdb_search: candidate %ld not found\n", (long) id, 0, 0);
+#else
                                Debug( LDAP_DEBUG_TRACE,
                                        "bdb_search: candidate %ld not found\n",
                                        (long) id, 0, 0 );
+#endif
                        }
 
                        goto loop_continue;
@@ -367,9 +412,14 @@ bdb_search(
 
                        } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
                                /* alias is within scope */
+#ifdef NEW_LOGGING
+                               LDAP_LOG ( OPERATION, RESULTS,
+                                       "bdb_search: \"%s\" in subtree\n", e->edn, 0, 0);
+#else
                                Debug( LDAP_DEBUG_TRACE,
                                        "bdb_search: \"%s\" in subtree\n",
                                        e->e_dn, 0, 0 );
+#endif
                                goto loop_continue;
                        }
 
@@ -385,18 +435,49 @@ bdb_search(
                if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
                        is_entry_referral( e ) )
                {
-                       BerVarray erefs = get_entry_referrals(
-                               be, conn, op, e );
-                       BerVarray refs = referral_rewrite( erefs,
-                               &e->e_name, NULL,
-                               scope == LDAP_SCOPE_SUBTREE 
-                                       ? LDAP_SCOPE_SUBTREE
-                                       : LDAP_SCOPE_BASE );
+                       struct berval   dn;
 
-                       send_search_reference( be, conn, op,
-                               e, refs, NULL, &v2refs );
+                       /* check scope */
+                       if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
+                               if ( !be_issuffix( be, &e->e_nname ) ) {
+                                       dnParent( &e->e_nname, &dn );
+                                       scopeok = dn_match( &dn, &realbase );
+                               } else {
+                                       scopeok = (realbase.bv_len == 0);
+                               }
 
-                       ber_bvarray_free( refs );
+                       } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
+                               scopeok = dnIsSuffix( &e->e_nname, &realbase );
+
+                       } else {
+                               scopeok = 1;
+                       }
+
+                       if( scopeok ) {
+                               BerVarray erefs = get_entry_referrals(
+                                       be, conn, op, e );
+                               BerVarray refs = referral_rewrite( erefs,
+                                       &e->e_name, NULL,
+                                       scope == LDAP_SCOPE_SUBTREE
+                                               ? LDAP_SCOPE_SUBTREE
+                                               : LDAP_SCOPE_BASE );
+
+                               send_search_reference( be, conn, op,
+                                       e, refs, NULL, &v2refs );
+
+                               ber_bvarray_free( refs );
+
+                       } else {
+#ifdef NEW_LOGGING
+                               LDAP_LOG(OPERATION, DETAIL2, 
+                                       "bdb_search: candidate referral %ld scope not okay\n",
+                                       id, 0, 0 );
+#else
+                               Debug( LDAP_DEBUG_TRACE,
+                                       "bdb_search: candidate referral %ld scope not okay\n",
+                                       id, 0, 0 );
+#endif
+                       }
 
                        goto loop_continue;
                }
@@ -425,7 +506,7 @@ bdb_search(
                        if ( scopeok ) {
                                /* check size limit */
                                if ( --slimit == -1 ) {
-                                       bdb_cache_return_entry_r (&bdb->bi_cache, e);
+                                       bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
                                        e = NULL;
                                        send_search_result( conn, op,
                                                rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
@@ -450,27 +531,37 @@ bdb_search(
                                        case 1:         /* entry not sent */
                                                break;
                                        case -1:        /* connection closed */
-                                               bdb_cache_return_entry_r(&bdb->bi_cache, e);
+                                               bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
                                                e = NULL;
                                                rc = LDAP_OTHER;
                                                goto done;
                                        }
                                }
                        } else {
+#ifdef NEW_LOGGING
+                               LDAP_LOG ( OPERATION, RESULTS,
+                                       "bdb_search: %ld scope not okay\n", (long) id, 0, 0);
+#else
                                Debug( LDAP_DEBUG_TRACE,
                                        "bdb_search: %ld scope not okay\n",
                                        (long) id, 0, 0 );
+#endif
                        }
                } else {
+#ifdef NEW_LOGGING
+                               LDAP_LOG ( OPERATION, RESULTS,
+                                       "bdb_search: %ld does match filter\n", (long) id, 0, 0);
+#else
                        Debug( LDAP_DEBUG_TRACE,
                                "bdb_search: %ld does match filter\n",
                                (long) id, 0, 0 );
+#endif
                }
 
 loop_continue:
                if( e != NULL ) {
                        /* free reader lock */
-                        bdb_cache_return_entry_r ( &bdb->bi_cache, e );
+                        bdb_cache_return_entry_r ( bdb->bi_dbenv, &bdb->bi_cache, e , &lock);
                         e = NULL;
                }
 
@@ -485,9 +576,11 @@ loop_continue:
 done:
        if( e != NULL ) {
                /* free reader lock */
-               bdb_cache_return_entry_r ( &bdb->bi_cache, e );
+               bdb_cache_return_entry_r ( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
        }
 
+       LOCK_ID_FREE (bdb->bi_dbenv, locker );
+
        if( v2refs ) ber_bvarray_free( v2refs );
        if( realbase.bv_val ) ch_free( realbase.bv_val );
 
@@ -500,8 +593,13 @@ static int base_candidate(
        Entry   *e,
        ID              *ids )
 {
+#ifdef NEW_LOGGING
+       LDAP_LOG ( OPERATION, ENTRY,
+               "base_candidate: base: \"%s\" (0x%08lx)\n", e->e_dn, (long) e->e_id, 0);
+#else
        Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
                e->e_dn, (long) e->e_id, 0);
+#endif
 
        ids[0] = 1;
        ids[1] = e->e_id;
@@ -509,14 +607,19 @@ static int base_candidate(
 }
 
 /* Is "objectClass=xx" mentioned anywhere in this filter? Presence
- * doesn't count, we're looking for explicit values.
+ * doesn't count, we're looking for explicit values. Also count depth
+ * of filter tree while we're at it.
  */
 static int oc_filter(
-       Filter *f
+       Filter *f,
+       int cur,
+       int *max
 )
 {
        int rc = 0;
 
+       if( cur > *max ) *max = cur;
+
        switch(f->f_choice) {
        case LDAP_FILTER_EQUALITY:
        case LDAP_FILTER_APPROX:
@@ -531,8 +634,9 @@ static int oc_filter(
 
        case LDAP_FILTER_AND:
        case LDAP_FILTER_OR:
+               cur++;
                for (f=f->f_and; f; f=f->f_next)
-                       if ((rc = oc_filter(f)))
+                       if ((rc = oc_filter(f, cur, max)))
                                break;
                break;
        default:
@@ -550,9 +654,9 @@ static int search_candidates(
        int deref,
        ID      *ids )
 {
-       int rc;
+       int rc, depth = 1;
        Filter          f, scopef, rf, xf;
-       ID              tmp[BDB_IDL_UM_SIZE];
+       ID              *stack;
        AttributeAssertion aa_ref;
 #ifdef BDB_SUBENTRIES
        Filter  sf;
@@ -562,7 +666,6 @@ static int search_candidates(
        Filter  af;
        AttributeAssertion aa_alias;
 #endif
-       struct bdb_info *bdb = (struct bdb_info *) be->be_private;
 
        /*
         * This routine takes as input a filter (user-filter)
@@ -571,9 +674,15 @@ static int search_candidates(
         *              (|[(objectClass=referral)(objectClass=alias)](user-filter))
         */
 
+#ifdef NEW_LOGGING
+       LDAP_LOG ( OPERATION, ENTRY,
+               "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n", 
+               e->e_dn, (long) e->e_id, scope);
+#else
        Debug(LDAP_DEBUG_TRACE,
                "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
                e->e_dn, (long) e->e_id, scope );
+#endif
 
        xf.f_or = filter;
        xf.f_choice = LDAP_FILTER_OR;
@@ -582,7 +691,7 @@ static int search_candidates(
        /* If the user's filter doesn't mention objectClass, or if
         * it just uses objectClass=*, these clauses are redundant.
         */
-       if (oc_filter(filter) && !get_subentries_visibility(op) ) {
+       if (oc_filter(filter, 1, &depth) && !get_subentries_visibility(op) ) {
                if( !get_manageDSAit(op) ) { /* match referrals */
                        struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
                        rf.f_choice = LDAP_FILTER_EQUALITY;
@@ -604,6 +713,8 @@ static int search_candidates(
                        xf.f_or = ⁡
                }
 #endif
+               /* We added one of these clauses, filter depth increased */
+               if( xf.f_or != filter ) depth++;
        }
 
        f.f_next = NULL;
@@ -614,6 +725,8 @@ static int search_candidates(
                : SLAPD_FILTER_DN_ONE;
        scopef.f_dn = &e->e_nname;
        scopef.f_next = xf.f_or == filter ? filter : &xf ;
+       /* Filter depth increased again, adding scope clause */
+       depth++;
 
 #ifdef BDB_SUBENTRIES
        if( get_subentries_visibility( op ) ) {
@@ -627,19 +740,36 @@ static int search_candidates(
        }
 #endif
 
-       rc = bdb_filter_candidates( be, &f, ids, tmp );
+       /* Allocate IDL stack, plus 1 more for former tmp */
+       stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
+
+       rc = bdb_filter_candidates( be, &f, ids, stack, stack+BDB_IDL_UM_SIZE );
+
+       ch_free( stack );
 
        if( rc ) {
+#ifdef NEW_LOGGING
+               LDAP_LOG ( OPERATION, DETAIL1,
+                       "bdb_search_candidates: failed (rc=%d)\n", rc, 0, 0  );
+#else
                Debug(LDAP_DEBUG_TRACE,
                        "bdb_search_candidates: failed (rc=%d)\n",
                        rc, NULL, NULL );
+#endif
 
        } else {
+#ifdef NEW_LOGGING
+               LDAP_LOG ( OPERATION, DETAIL1,
+                       "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
+                       (long) ids[0], (long) BDB_IDL_FIRST(ids), 
+                       (long) BDB_IDL_LAST(ids));
+#else
                Debug(LDAP_DEBUG_TRACE,
                        "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
                        (long) ids[0],
                        (long) BDB_IDL_FIRST(ids),
                        (long) BDB_IDL_LAST(ids) );
+#endif
        }
 
        return rc;