]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-bdb/id2entry.c
Relax entry_header, zero-length entries are valid.
[openldap] / servers / slapd / back-bdb / id2entry.c
index 1ef56e71d330422e24e013f95f3a50f2a606046c..51238a151a68512db174c0a46fc82a4d9f6ee0ff 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 2000-2004 The OpenLDAP Foundation.
+ * Copyright 2000-2011 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -18,6 +18,7 @@
 
 #include <stdio.h>
 #include <ac/string.h>
+#include <ac/errno.h>
 
 #include "back-bdb.h"
 
@@ -98,8 +99,10 @@ int bdb_id2entry(
        struct bdb_info *bdb = (struct bdb_info *) be->be_private;
        DB *db = bdb->bi_id2entry->bdi_db;
        DBT key, data;
-       struct berval bv;
-       int rc = 0, ret = 0;
+       DBC *cursor;
+       EntryHeader eh;
+       char buf[16];
+       int rc = 0, off;
        ID nid;
 
        *e = NULL;
@@ -110,18 +113,61 @@ int bdb_id2entry(
        BDB_ID2DISK( id, &nid );
 
        DBTzero( &data );
-       data.flags = DB_DBT_MALLOC;
+       data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
 
        /* fetch it */
-       rc = db->get( db, tid, &key, &data, bdb->bi_db_opflags );
+       rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
+       if ( rc ) return rc;
+
+       /* Get the nattrs / nvals counts first */
+       data.ulen = data.dlen = sizeof(buf);
+       data.data = buf;
+       rc = cursor->c_get( cursor, &key, &data, DB_SET );
+       if ( rc ) goto finish;
+
+
+       eh.bv.bv_val = buf;
+       eh.bv.bv_len = data.size;
+       rc = entry_header( &eh );
+       if ( rc ) goto finish;
+
+       if ( eh.nvals ) {
+               /* Get the size */
+               data.flags ^= DB_DBT_PARTIAL;
+               data.ulen = 0;
+               rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
+               if ( rc != DB_BUFFER_SMALL ) goto finish;
+
+               /* Allocate a block and retrieve the data */
+               off = eh.data - eh.bv.bv_val;
+               eh.bv.bv_len = eh.nvals * sizeof( struct berval ) + data.size;
+               eh.bv.bv_val = ch_malloc( eh.bv.bv_len );
+               eh.data = eh.bv.bv_val + eh.nvals * sizeof( struct berval );
+               data.data = eh.data;
+               data.ulen = data.size;
+
+               /* skip past already parsed nattr/nvals */
+               eh.data += off;
+
+               rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
+       }
+
+finish:
+       cursor->c_close( cursor );
 
        if( rc != 0 ) {
                return rc;
        }
 
-       DBT2bv( &data, &bv );
-
-       rc = entry_decode( &bv, e );
+       if ( eh.nvals ) {
+#ifdef SLAP_ZONE_ALLOC
+               rc = entry_decode(&eh, e, bdb->bi_cache.c_zctx);
+#else
+               rc = entry_decode(&eh, e);
+#endif
+       } else {
+               *e = entry_alloc();
+       }
 
        if( rc == 0 ) {
                (*e)->e_id = id;
@@ -129,8 +175,13 @@ int bdb_id2entry(
                /* only free on error. On success, the entry was
                 * decoded in place.
                 */
-               ch_free( data.data );
+#ifndef SLAP_ZONE_ALLOC
+               ch_free(eh.bv.bv_val);
+#endif
        }
+#ifdef SLAP_ZONE_ALLOC
+       ch_free(eh.bv.bv_val);
+#endif
 
        return rc;
 }
@@ -158,55 +209,41 @@ int bdb_id2entry_delete(
 }
 
 int bdb_entry_return(
-       Entry *e )
+       Entry *e
+)
 {
        /* Our entries are allocated in two blocks; the data comes from
         * the db itself and the Entry structure and associated pointers
         * are allocated in entry_decode. The db data pointer is saved
-        * in e_bv. Since the Entry structure is allocated as a single
-        * block, e_attrs is always a fixed offset from e. The exception
-        * is when an entry has been modified, in which case we also need
-        * to free e_attrs.
+        * in e_bv.
         */
-#ifdef LDAP_COMP_MATCH
-       comp_tree_free( e->e_attrs );
-#endif
-       if( !e->e_bv.bv_val ) { /* A regular entry, from do_add */
-               entry_free( e );
-               return 0;
-       }
-       if( (void *) e->e_attrs != (void *) (e+1)) {
-               attrs_free( e->e_attrs );
-       }
-
-       /* See if the DNs were changed by modrdn */
-       if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
-               e->e_bv.bv_val + e->e_bv.bv_len ) {
-               ch_free(e->e_name.bv_val);
-               ch_free(e->e_nname.bv_val);
+       if ( e->e_bv.bv_val ) {
+               /* See if the DNs were changed by modrdn */
+               if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
+                       e->e_bv.bv_val + e->e_bv.bv_len ) {
+                       ch_free(e->e_name.bv_val);
+                       ch_free(e->e_nname.bv_val);
+               }
                e->e_name.bv_val = NULL;
                e->e_nname.bv_val = NULL;
+               /* In tool mode the e_bv buffer is realloc'd, leave it alone */
+               if( !(slapMode & SLAP_TOOL_MODE) ) {
+                       free( e->e_bv.bv_val );
+               }
+               BER_BVZERO( &e->e_bv );
        }
-#ifndef BDB_HIER
-       /* In tool mode the e_bv buffer is realloc'd, leave it alone */
-       if( !(slapMode & SLAP_TOOL_MODE) ) {
-               free( e->e_bv.bv_val );
-       }
-#else
-       free( e->e_bv.bv_val );
-#endif
-       free( e );
-
+       entry_free( e );
        return 0;
 }
 
 int bdb_entry_release(
-       Operation *o,
+       Operation *op,
        Entry *e,
        int rw )
 {
-       struct bdb_info *bdb = (struct bdb_info *) o->o_bd->be_private;
-       struct bdb_op_info *boi = NULL;
+       struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
+       struct bdb_op_info *boi;
+       OpExtra *oex;
  
        /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
                        SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
@@ -214,37 +251,59 @@ int bdb_entry_release(
        if ( slapMode == SLAP_SERVER_MODE ) {
                /* If not in our cache, just free it */
                if ( !e->e_private ) {
+#ifdef SLAP_ZONE_ALLOC
+                       return bdb_entry_return( bdb, e, -1 );
+#else
                        return bdb_entry_return( e );
+#endif
                }
                /* free entry and reader or writer lock */
-               if ( o ) {
-                       boi = (struct bdb_op_info *)o->o_private;
+               LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
+                       if ( oex->oe_key == bdb ) break;
                }
+               boi = (struct bdb_op_info *)oex;
+
                /* lock is freed with txn */
                if ( !boi || boi->boi_txn ) {
-                       bdb_unlocked_cache_return_entry_rw( &bdb->bi_cache, e, rw );
+                       bdb_unlocked_cache_return_entry_rw( bdb, e, rw );
                } else {
                        struct bdb_lock_info *bli, *prev;
                        for ( prev=(struct bdb_lock_info *)&boi->boi_locks,
                                bli = boi->boi_locks; bli; prev=bli, bli=bli->bli_next ) {
                                if ( bli->bli_id == e->e_id ) {
-                                       bdb_cache_return_entry_rw( bdb->bi_dbenv, &bdb->bi_cache,
-                                               e, rw, &bli->bli_lock );
+                                       bdb_cache_return_entry_rw( bdb, e, rw, &bli->bli_lock );
                                        prev->bli_next = bli->bli_next;
-                                       o->o_tmpfree( bli, o->o_tmpmemctx );
+                                       /* Cleanup, or let caller know we unlocked */
+                                       if ( bli->bli_flag & BLI_DONTFREE )
+                                               bli->bli_flag = 0;
+                                       else
+                                               op->o_tmpfree( bli, op->o_tmpmemctx );
                                        break;
                                }
                        }
                        if ( !boi->boi_locks ) {
-                               o->o_tmpfree( boi, o->o_tmpmemctx );
-                               o->o_private = NULL;
+                               LDAP_SLIST_REMOVE( &op->o_extra, &boi->boi_oe, OpExtra, oe_next );
+                               if ( !(boi->boi_flag & BOI_DONTFREE))
+                                       op->o_tmpfree( boi, op->o_tmpmemctx );
                        }
                }
        } else {
+#ifdef SLAP_ZONE_ALLOC
+               int zseq = -1;
+               if (e->e_private != NULL) {
+                       BEI(e)->bei_e = NULL;
+                       zseq = BEI(e)->bei_zseq;
+               }
+#else
                if (e->e_private != NULL)
                        BEI(e)->bei_e = NULL;
+#endif
                e->e_private = NULL;
+#ifdef SLAP_ZONE_ALLOC
+               bdb_entry_return ( bdb, e, zseq );
+#else
                bdb_entry_return ( e );
+#endif
        }
  
        return 0;
@@ -268,9 +327,7 @@ int bdb_entry_get(
        int     rc;
        const char *at_name = at ? at->ad_cname.bv_val : "(null)";
 
-       u_int32_t       locker = 0;
        DB_LOCK         lock;
-       int             free_lock_id = 0;
 
        Debug( LDAP_DEBUG_ARGS,
                "=> bdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 ); 
@@ -278,17 +335,18 @@ int bdb_entry_get(
                "=> bdb_entry_get: oc: \"%s\", at: \"%s\"\n",
                oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
 
-       if( op ) boi = (struct bdb_op_info *) op->o_private;
-       if( boi != NULL && op->o_bd->be_private == boi->boi_bdb->be_private ) {
-               txn = boi->boi_txn;
-               locker = boi->boi_locker;
+       if( op ) {
+               OpExtra *oex;
+               LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
+                       if ( oex->oe_key == bdb ) break;
+               }
+               boi = (struct bdb_op_info *)oex;
+               if ( boi )
+                       txn = boi->boi_txn;
        }
 
-       if ( txn != NULL ) {
-               locker = TXN_ID ( txn );
-       } else if ( !locker ) {
-               rc = LOCK_ID ( bdb->bi_dbenv, &locker );
-               free_lock_id = 1;
+       if ( !txn ) {
+               rc = bdb_reader_get( op, bdb->bi_dbenv, &txn );
                switch(rc) {
                case 0:
                        break;
@@ -299,7 +357,7 @@ int bdb_entry_get(
 
 dn2entry_retry:
        /* can we find entry */
-       rc = bdb_dn2entry( op, txn, ndn, &ei, 0, locker, &lock );
+       rc = bdb_dn2entry( op, txn, ndn, &ei, 0, &lock );
        switch( rc ) {
        case DB_NOTFOUND:
        case 0:
@@ -308,16 +366,13 @@ dn2entry_retry:
        case DB_LOCK_NOTGRANTED:
                /* the txn must abort and retry */
                if ( txn ) {
-                       boi->boi_err = rc;
+                       if ( boi ) boi->boi_err = rc;
                        return LDAP_BUSY;
                }
                ldap_pvt_thread_yield();
                goto dn2entry_retry;
        default:
                if ( boi ) boi->boi_err = rc;
-               if ( free_lock_id ) {
-                       LOCK_ID_FREE( bdb->bi_dbenv, locker );
-               }
                return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
        }
        if (ei) e = ei->bei_e;
@@ -325,9 +380,6 @@ dn2entry_retry:
                Debug( LDAP_DEBUG_ACL,
                        "=> bdb_entry_get: cannot find entry: \"%s\"\n",
                                ndn->bv_val, 0, 0 ); 
-               if ( free_lock_id ) {
-                       LOCK_ID_FREE( bdb->bi_dbenv, locker );
-               }
                return LDAP_NO_SUCH_OBJECT; 
        }
        
@@ -335,25 +387,19 @@ dn2entry_retry:
                "=> bdb_entry_get: found entry: \"%s\"\n",
                ndn->bv_val, 0, 0 ); 
 
-       /* find attribute values */
-       if( is_entry_alias( e ) ) {
-               Debug( LDAP_DEBUG_ACL,
-                       "<= bdb_entry_get: entry is an alias\n", 0, 0, 0 );
-               rc = LDAP_ALIAS_PROBLEM;
-               goto return_results;
-       }
-
-       if( is_entry_referral( e ) ) {
+       if ( oc && !is_entry_objectclass( e, oc, 0 )) {
                Debug( LDAP_DEBUG_ACL,
-                       "<= bdb_entry_get: entry is a referral\n", 0, 0, 0 );
-               rc = LDAP_REFERRAL;
+                       "<= bdb_entry_get: failed to find objectClass %s\n",
+                       oc->soc_cname.bv_val, 0, 0 ); 
+               rc = LDAP_NO_SUCH_ATTRIBUTE;
                goto return_results;
        }
 
-       if ( oc && !is_entry_objectclass( e, oc, 0 )) {
+       /* NOTE: attr_find() or attrs_find()? */
+       if ( at && attr_find( e->e_attrs, at ) == NULL ) {
                Debug( LDAP_DEBUG_ACL,
-                       "<= bdb_entry_get: failed to find objectClass\n",
-                       0, 0, 0 ); 
+                       "<= bdb_entry_get: failed to find attribute %s\n",
+                       at->ad_cname.bv_val, 0, 0 ); 
                rc = LDAP_NO_SUCH_ATTRIBUTE;
                goto return_results;
        }
@@ -361,7 +407,7 @@ dn2entry_retry:
 return_results:
        if( rc != LDAP_SUCCESS ) {
                /* free entry */
-               bdb_cache_return_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, rw, &lock);
+               bdb_cache_return_entry_rw(bdb, e, rw, &lock);
 
        } else {
                if ( slapMode == SLAP_SERVER_MODE ) {
@@ -373,8 +419,8 @@ return_results:
                        if ( op ) {
                                if ( !boi ) {
                                        boi = op->o_tmpcalloc(1,sizeof(struct bdb_op_info),op->o_tmpmemctx);
-                                       boi->boi_bdb = op->o_bd;
-                                       op->o_private = boi;
+                                       boi->boi_oe.oe_key = bdb;
+                                       LDAP_SLIST_INSERT_HEAD( &op->o_extra, &boi->boi_oe, oe_next );
                                }
                                if ( !boi->boi_txn ) {
                                        struct bdb_lock_info *bli;
@@ -382,20 +428,17 @@ return_results:
                                                op->o_tmpmemctx );
                                        bli->bli_next = boi->boi_locks;
                                        bli->bli_id = e->e_id;
+                                       bli->bli_flag = 0;
                                        bli->bli_lock = lock;
                                        boi->boi_locks = bli;
                                }
                        }
                } else {
                        *ent = entry_dup( e );
-                       bdb_cache_return_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, rw, &lock);
+                       bdb_cache_return_entry_rw(bdb, e, rw, &lock);
                }
        }
 
-       if ( free_lock_id ) {
-               LOCK_ID_FREE( bdb->bi_dbenv, locker );
-       }
-
        Debug( LDAP_DEBUG_TRACE,
                "bdb_entry_get: rc=%d\n",
                rc, 0, 0 );