1 /* id2entry.c - routines to deal with the id2entry database */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2007 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
20 #include <ac/string.h>
25 static int bdb_id2entry_put(
31 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
32 DB *db = bdb->bi_id2entry->bdi_db;
38 struct berval odn, ondn;
40 /* We only store rdns, and they go in the dn2id database. */
42 odn = e->e_name; ondn = e->e_nname;
44 e->e_name = slap_empty_bv;
45 e->e_nname = slap_empty_bv;
49 /* Store ID in BigEndian format */
51 key.size = sizeof(ID);
52 BDB_ID2DISK( e->e_id, &nid );
54 rc = entry_encode( e, &bv );
56 e->e_name = odn; e->e_nname = ondn;
58 if( rc != LDAP_SUCCESS ) {
65 rc = db->put( db, tid, &key, &data, flag );
72 * This routine adds (or updates) an entry on disk.
73 * The cache should be already be updated.
82 return bdb_id2entry_put(be, tid, e, DB_NOOVERWRITE);
85 int bdb_id2entry_update(
90 return bdb_id2entry_put(be, tid, e, 0);
100 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
101 DB *db = bdb->bi_id2entry->bdi_db;
113 key.size = sizeof(ID);
114 BDB_ID2DISK( id, &nid );
117 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
120 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
123 /* Use our own locker if needed */
124 if ( !tid && locker ) {
125 CURSOR_SETLOCKER( cursor, locker );
128 /* Get the nattrs / nvals counts first */
129 data.ulen = data.dlen = sizeof(buf);
131 rc = cursor->c_get( cursor, &key, &data, DB_SET );
132 if ( rc ) goto finish;
136 eh.bv.bv_len = data.size;
137 rc = entry_header( &eh );
138 if ( rc ) goto finish;
141 data.flags ^= DB_DBT_PARTIAL;
143 rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
144 if ( rc != DB_BUFFER_SMALL ) goto finish;
146 /* Allocate a block and retrieve the data */
147 off = eh.data - eh.bv.bv_val;
148 eh.bv.bv_len = eh.nvals * sizeof( struct berval ) + data.size;
149 eh.bv.bv_val = ch_malloc( eh.bv.bv_len );
150 eh.data = eh.bv.bv_val + eh.nvals * sizeof( struct berval );
152 data.ulen = data.size;
154 /* skip past already parsed nattr/nvals */
157 rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
160 cursor->c_close( cursor );
166 #ifdef SLAP_ZONE_ALLOC
167 rc = entry_decode(&eh, e, bdb->bi_cache.c_zctx);
169 rc = entry_decode(&eh, e);
175 /* only free on error. On success, the entry was
178 #ifndef SLAP_ZONE_ALLOC
179 ch_free(eh.bv.bv_val);
182 #ifdef SLAP_ZONE_ALLOC
183 ch_free(eh.bv.bv_val);
189 int bdb_id2entry_delete(
194 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
195 DB *db = bdb->bi_id2entry->bdi_db;
202 key.size = sizeof(ID);
203 BDB_ID2DISK( e->e_id, &nid );
205 /* delete from database */
206 rc = db->del( db, tid, &key, 0 );
211 int bdb_entry_return(
215 /* Our entries are allocated in two blocks; the data comes from
216 * the db itself and the Entry structure and associated pointers
217 * are allocated in entry_decode. The db data pointer is saved
220 if ( e->e_bv.bv_val ) {
221 /* See if the DNs were changed by modrdn */
222 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
223 e->e_bv.bv_val + e->e_bv.bv_len ) {
224 ch_free(e->e_name.bv_val);
225 ch_free(e->e_nname.bv_val);
227 e->e_name.bv_val = NULL;
228 e->e_nname.bv_val = NULL;
229 /* In tool mode the e_bv buffer is realloc'd, leave it alone */
230 if( !(slapMode & SLAP_TOOL_MODE) ) {
231 free( e->e_bv.bv_val );
233 BER_BVZERO( &e->e_bv );
239 int bdb_entry_release(
244 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
245 struct bdb_op_info *boi = NULL;
247 /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
248 SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
250 if ( slapMode == SLAP_SERVER_MODE ) {
251 /* If not in our cache, just free it */
252 if ( !e->e_private ) {
253 #ifdef SLAP_ZONE_ALLOC
254 return bdb_entry_return( bdb, e, -1 );
256 return bdb_entry_return( e );
259 /* free entry and reader or writer lock */
260 boi = (struct bdb_op_info *)op->o_private;
262 /* lock is freed with txn */
263 if ( !boi || boi->boi_txn ) {
264 bdb_unlocked_cache_return_entry_rw( bdb, e, rw );
266 struct bdb_lock_info *bli, *prev;
267 for ( prev=(struct bdb_lock_info *)&boi->boi_locks,
268 bli = boi->boi_locks; bli; prev=bli, bli=bli->bli_next ) {
269 if ( bli->bli_id == e->e_id ) {
270 bdb_cache_return_entry_rw( bdb, e, rw, &bli->bli_lock );
271 prev->bli_next = bli->bli_next;
272 op->o_tmpfree( bli, op->o_tmpmemctx );
276 if ( !boi->boi_locks ) {
277 op->o_tmpfree( boi, op->o_tmpmemctx );
278 op->o_private = NULL;
282 #ifdef SLAP_ZONE_ALLOC
284 if (e->e_private != NULL) {
285 BEI(e)->bei_e = NULL;
286 zseq = BEI(e)->bei_zseq;
289 if (e->e_private != NULL)
290 BEI(e)->bei_e = NULL;
293 #ifdef SLAP_ZONE_ALLOC
294 bdb_entry_return ( bdb, e, zseq );
296 bdb_entry_return ( e );
303 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
309 AttributeDescription *at,
313 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
314 struct bdb_op_info *boi = NULL;
319 const char *at_name = at ? at->ad_cname.bv_val : "(null)";
321 BDB_LOCKER locker = 0;
323 int free_lock_id = 0;
325 Debug( LDAP_DEBUG_ARGS,
326 "=> bdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 );
327 Debug( LDAP_DEBUG_ARGS,
328 "=> bdb_entry_get: oc: \"%s\", at: \"%s\"\n",
329 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
331 if( op ) boi = (struct bdb_op_info *) op->o_private;
332 if( boi != NULL && op->o_bd->be_private == boi->boi_bdb->be_private ) {
334 locker = boi->boi_locker;
338 locker = TXN_ID ( txn );
339 } else if ( !locker ) {
340 rc = LOCK_ID ( bdb->bi_dbenv, &locker );
351 /* can we find entry */
352 rc = bdb_dn2entry( op, txn, ndn, &ei, 0, locker, &lock );
357 case DB_LOCK_DEADLOCK:
358 case DB_LOCK_NOTGRANTED:
359 /* the txn must abort and retry */
364 ldap_pvt_thread_yield();
367 if ( boi ) boi->boi_err = rc;
368 if ( free_lock_id ) {
369 LOCK_ID_FREE( bdb->bi_dbenv, locker );
371 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
373 if (ei) e = ei->bei_e;
375 Debug( LDAP_DEBUG_ACL,
376 "=> bdb_entry_get: cannot find entry: \"%s\"\n",
378 if ( free_lock_id ) {
379 LOCK_ID_FREE( bdb->bi_dbenv, locker );
381 return LDAP_NO_SUCH_OBJECT;
384 Debug( LDAP_DEBUG_ACL,
385 "=> bdb_entry_get: found entry: \"%s\"\n",
388 if ( oc && !is_entry_objectclass( e, oc, 0 )) {
389 Debug( LDAP_DEBUG_ACL,
390 "<= bdb_entry_get: failed to find objectClass %s\n",
391 oc->soc_cname.bv_val, 0, 0 );
392 rc = LDAP_NO_SUCH_ATTRIBUTE;
397 if( rc != LDAP_SUCCESS ) {
399 bdb_cache_return_entry_rw(bdb, e, rw, &lock);
402 if ( slapMode == SLAP_SERVER_MODE ) {
404 /* big drag. we need a place to store a read lock so we can
405 * release it later?? If we're in a txn, nothing is needed
406 * here because the locks will go away with the txn.
410 boi = op->o_tmpcalloc(1,sizeof(struct bdb_op_info),op->o_tmpmemctx);
411 boi->boi_bdb = op->o_bd;
414 if ( !boi->boi_txn ) {
415 struct bdb_lock_info *bli;
416 bli = op->o_tmpalloc( sizeof(struct bdb_lock_info),
418 bli->bli_next = boi->boi_locks;
419 bli->bli_id = e->e_id;
420 bli->bli_lock = lock;
421 boi->boi_locks = bli;
425 *ent = entry_dup( e );
426 bdb_cache_return_entry_rw(bdb, e, rw, &lock);
430 if ( free_lock_id ) {
431 LOCK_ID_FREE( bdb->bi_dbenv, locker );
434 Debug( LDAP_DEBUG_TRACE,
435 "bdb_entry_get: rc=%d\n",