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-2006 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->locker = locker;
127 /* Get the nattrs / nvals counts first */
128 data.ulen = data.dlen = sizeof(buf);
130 rc = cursor->c_get( cursor, &key, &data, DB_SET );
131 if ( rc ) goto leave;
134 eh.bv.bv_len = data.size;
135 rc = entry_header( &eh );
136 if ( rc ) goto leave;
139 data.flags ^= DB_DBT_PARTIAL;
141 rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
142 if ( rc != ENOMEM ) goto leave;
144 /* Allocate a block and retrieve the data */
145 off = eh.data - eh.bv.bv_val;
146 eh.bv.bv_len = eh.nvals * sizeof( struct berval ) + data.size;
147 eh.bv.bv_val = ch_malloc( eh.bv.bv_len );
148 eh.data = eh.bv.bv_val + eh.nvals * sizeof( struct berval );
150 data.ulen = data.size;
152 /* skip past already parsed nattr/nvals */
155 rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
158 cursor->c_close( cursor );
164 #ifdef SLAP_ZONE_ALLOC
165 rc = entry_decode(&eh, e, bdb->bi_cache.c_zctx);
167 rc = entry_decode(&eh, e);
173 /* only free on error. On success, the entry was
176 #ifndef SLAP_ZONE_ALLOC
177 ch_free(eh.bv.bv_val);
180 #ifdef SLAP_ZONE_ALLOC
181 ch_free(eh.bv.bv_val);
187 int bdb_id2entry_delete(
192 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
193 DB *db = bdb->bi_id2entry->bdi_db;
200 key.size = sizeof(ID);
201 BDB_ID2DISK( e->e_id, &nid );
203 /* delete from database */
204 rc = db->del( db, tid, &key, 0 );
209 int bdb_entry_return(
213 /* Our entries are allocated in two blocks; the data comes from
214 * the db itself and the Entry structure and associated pointers
215 * are allocated in entry_decode. The db data pointer is saved
218 if ( e->e_bv.bv_val ) {
219 /* See if the DNs were changed by modrdn */
220 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
221 e->e_bv.bv_val + e->e_bv.bv_len ) {
222 ch_free(e->e_name.bv_val);
223 ch_free(e->e_nname.bv_val);
225 e->e_name.bv_val = NULL;
226 e->e_nname.bv_val = NULL;
227 /* In tool mode the e_bv buffer is realloc'd, leave it alone */
228 if( !(slapMode & SLAP_TOOL_MODE) ) {
229 free( e->e_bv.bv_val );
231 BER_BVZERO( &e->e_bv );
237 int bdb_entry_release(
242 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
243 struct bdb_op_info *boi = NULL;
245 /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
246 SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
248 if ( slapMode == SLAP_SERVER_MODE ) {
249 /* If not in our cache, just free it */
250 if ( !e->e_private ) {
251 #ifdef SLAP_ZONE_ALLOC
252 return bdb_entry_return( bdb, e, -1 );
254 return bdb_entry_return( e );
257 /* free entry and reader or writer lock */
258 boi = (struct bdb_op_info *)op->o_private;
260 /* lock is freed with txn */
261 if ( !boi || boi->boi_txn ) {
262 bdb_unlocked_cache_return_entry_rw( &bdb->bi_cache, e, rw );
264 struct bdb_lock_info *bli, *prev;
265 for ( prev=(struct bdb_lock_info *)&boi->boi_locks,
266 bli = boi->boi_locks; bli; prev=bli, bli=bli->bli_next ) {
267 if ( bli->bli_id == e->e_id ) {
268 bdb_cache_return_entry_rw( bdb->bi_dbenv, &bdb->bi_cache,
269 e, rw, &bli->bli_lock );
270 prev->bli_next = bli->bli_next;
271 op->o_tmpfree( bli, op->o_tmpmemctx );
275 if ( !boi->boi_locks ) {
276 op->o_tmpfree( boi, op->o_tmpmemctx );
277 op->o_private = NULL;
281 #ifdef SLAP_ZONE_ALLOC
283 if (e->e_private != NULL) {
284 BEI(e)->bei_e = NULL;
285 zseq = BEI(e)->bei_zseq;
288 if (e->e_private != NULL)
289 BEI(e)->bei_e = NULL;
292 #ifdef SLAP_ZONE_ALLOC
293 bdb_entry_return ( bdb, e, zseq );
295 bdb_entry_return ( e );
302 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
308 AttributeDescription *at,
312 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
313 struct bdb_op_info *boi = NULL;
318 const char *at_name = at ? at->ad_cname.bv_val : "(null)";
320 u_int32_t locker = 0;
322 int free_lock_id = 0;
324 Debug( LDAP_DEBUG_ARGS,
325 "=> bdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 );
326 Debug( LDAP_DEBUG_ARGS,
327 "=> bdb_entry_get: oc: \"%s\", at: \"%s\"\n",
328 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
330 if( op ) boi = (struct bdb_op_info *) op->o_private;
331 if( boi != NULL && op->o_bd->be_private == boi->boi_bdb->be_private ) {
333 locker = boi->boi_locker;
337 locker = TXN_ID ( txn );
338 } else if ( !locker ) {
339 rc = LOCK_ID ( bdb->bi_dbenv, &locker );
350 /* can we find entry */
351 rc = bdb_dn2entry( op, txn, ndn, &ei, 0, locker, &lock );
356 case DB_LOCK_DEADLOCK:
357 case DB_LOCK_NOTGRANTED:
358 /* the txn must abort and retry */
363 ldap_pvt_thread_yield();
366 if ( boi ) boi->boi_err = rc;
367 if ( free_lock_id ) {
368 LOCK_ID_FREE( bdb->bi_dbenv, locker );
370 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
372 if (ei) e = ei->bei_e;
374 Debug( LDAP_DEBUG_ACL,
375 "=> bdb_entry_get: cannot find entry: \"%s\"\n",
377 if ( free_lock_id ) {
378 LOCK_ID_FREE( bdb->bi_dbenv, locker );
380 return LDAP_NO_SUCH_OBJECT;
383 Debug( LDAP_DEBUG_ACL,
384 "=> bdb_entry_get: found entry: \"%s\"\n",
387 /* find attribute values */
388 if( is_entry_alias( e ) ) {
389 Debug( LDAP_DEBUG_ACL,
390 "<= bdb_entry_get: entry is an alias\n", 0, 0, 0 );
391 rc = LDAP_ALIAS_PROBLEM;
395 if( is_entry_referral( e ) ) {
396 Debug( LDAP_DEBUG_ACL,
397 "<= bdb_entry_get: entry is a referral\n", 0, 0, 0 );
402 if ( oc && !is_entry_objectclass( e, oc, 0 )) {
403 Debug( LDAP_DEBUG_ACL,
404 "<= bdb_entry_get: failed to find objectClass %s\n",
405 oc->soc_cname.bv_val, 0, 0 );
406 rc = LDAP_NO_SUCH_ATTRIBUTE;
411 if( rc != LDAP_SUCCESS ) {
413 bdb_cache_return_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, rw, &lock);
416 if ( slapMode == SLAP_SERVER_MODE ) {
418 /* big drag. we need a place to store a read lock so we can
419 * release it later?? If we're in a txn, nothing is needed
420 * here because the locks will go away with the txn.
424 boi = op->o_tmpcalloc(1,sizeof(struct bdb_op_info),op->o_tmpmemctx);
425 boi->boi_bdb = op->o_bd;
428 if ( !boi->boi_txn ) {
429 struct bdb_lock_info *bli;
430 bli = op->o_tmpalloc( sizeof(struct bdb_lock_info),
432 bli->bli_next = boi->boi_locks;
433 bli->bli_id = e->e_id;
434 bli->bli_lock = lock;
435 boi->boi_locks = bli;
439 *ent = entry_dup( e );
440 bdb_cache_return_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, rw, &lock);
444 if ( free_lock_id ) {
445 LOCK_ID_FREE( bdb->bi_dbenv, locker );
448 Debug( LDAP_DEBUG_TRACE,
449 "bdb_entry_get: rc=%d\n",