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->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;
135 eh.bv.bv_len = data.size;
136 rc = entry_header( &eh );
137 if ( rc ) goto leave;
140 data.flags ^= DB_DBT_PARTIAL;
142 rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
143 if ( rc != DB_BUFFER_SMALL ) goto leave;
145 /* Allocate a block and retrieve the data */
146 off = eh.data - eh.bv.bv_val;
147 eh.bv.bv_len = eh.nvals * sizeof( struct berval ) + data.size;
148 eh.bv.bv_val = ch_malloc( eh.bv.bv_len );
149 eh.data = eh.bv.bv_val + eh.nvals * sizeof( struct berval );
151 data.ulen = data.size;
153 /* skip past already parsed nattr/nvals */
156 rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
159 cursor->c_close( cursor );
165 #ifdef SLAP_ZONE_ALLOC
166 rc = entry_decode(&eh, e, bdb->bi_cache.c_zctx);
168 rc = entry_decode(&eh, e);
174 /* only free on error. On success, the entry was
177 #ifndef SLAP_ZONE_ALLOC
178 ch_free(eh.bv.bv_val);
181 #ifdef SLAP_ZONE_ALLOC
182 ch_free(eh.bv.bv_val);
188 int bdb_id2entry_delete(
193 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
194 DB *db = bdb->bi_id2entry->bdi_db;
201 key.size = sizeof(ID);
202 BDB_ID2DISK( e->e_id, &nid );
204 /* delete from database */
205 rc = db->del( db, tid, &key, 0 );
210 int bdb_entry_return(
214 /* Our entries are allocated in two blocks; the data comes from
215 * the db itself and the Entry structure and associated pointers
216 * are allocated in entry_decode. The db data pointer is saved
219 if ( e->e_bv.bv_val ) {
220 /* See if the DNs were changed by modrdn */
221 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
222 e->e_bv.bv_val + e->e_bv.bv_len ) {
223 ch_free(e->e_name.bv_val);
224 ch_free(e->e_nname.bv_val);
226 e->e_name.bv_val = NULL;
227 e->e_nname.bv_val = NULL;
228 /* In tool mode the e_bv buffer is realloc'd, leave it alone */
229 if( !(slapMode & SLAP_TOOL_MODE) ) {
230 free( e->e_bv.bv_val );
232 BER_BVZERO( &e->e_bv );
238 int bdb_entry_release(
243 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
244 struct bdb_op_info *boi = NULL;
246 /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
247 SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
249 if ( slapMode == SLAP_SERVER_MODE ) {
250 /* If not in our cache, just free it */
251 if ( !e->e_private ) {
252 #ifdef SLAP_ZONE_ALLOC
253 return bdb_entry_return( bdb, e, -1 );
255 return bdb_entry_return( e );
258 /* free entry and reader or writer lock */
259 boi = (struct bdb_op_info *)op->o_private;
261 /* lock is freed with txn */
262 if ( !boi || boi->boi_txn ) {
263 bdb_unlocked_cache_return_entry_rw( bdb, e, rw );
265 struct bdb_lock_info *bli, *prev;
266 for ( prev=(struct bdb_lock_info *)&boi->boi_locks,
267 bli = boi->boi_locks; bli; prev=bli, bli=bli->bli_next ) {
268 if ( bli->bli_id == e->e_id ) {
269 bdb_cache_return_entry_rw( bdb, 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, 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, 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",