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>
24 static int bdb_id2entry_put(
30 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
31 DB *db = bdb->bi_id2entry->bdi_db;
37 struct berval odn, ondn;
39 /* We only store rdns, and they go in the dn2id database. */
41 odn = e->e_name; ondn = e->e_nname;
43 e->e_name = slap_empty_bv;
44 e->e_nname = slap_empty_bv;
48 /* Store ID in BigEndian format */
50 key.size = sizeof(ID);
51 BDB_ID2DISK( e->e_id, &nid );
53 rc = entry_encode( e, &bv );
55 e->e_name = odn; e->e_nname = ondn;
57 if( rc != LDAP_SUCCESS ) {
64 rc = db->put( db, tid, &key, &data, flag );
71 * This routine adds (or updates) an entry on disk.
72 * The cache should be already be updated.
81 return bdb_id2entry_put(be, tid, e, DB_NOOVERWRITE);
84 int bdb_id2entry_update(
89 return bdb_id2entry_put(be, tid, e, 0);
99 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
100 DB *db = bdb->bi_id2entry->bdi_db;
111 key.size = sizeof(ID);
112 BDB_ID2DISK( id, &nid );
115 data.flags = DB_DBT_MALLOC;
118 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
121 /* Use our own locker if needed */
122 if ( !tid && locker )
123 cursor->locker = locker;
125 rc = cursor->c_get( cursor, &key, &data, DB_SET );
126 cursor->c_close( cursor );
132 DBT2bv( &data, &bv );
134 #ifdef SLAP_ZONE_ALLOC
135 rc = entry_decode(&bv, e, bdb->bi_cache.c_zctx);
137 rc = entry_decode(&bv, e);
143 /* only free on error. On success, the entry was
146 #ifndef SLAP_ZONE_ALLOC
150 #ifdef SLAP_ZONE_ALLOC
157 int bdb_id2entry_delete(
162 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
163 DB *db = bdb->bi_id2entry->bdi_db;
170 key.size = sizeof(ID);
171 BDB_ID2DISK( e->e_id, &nid );
173 /* delete from database */
174 rc = db->del( db, tid, &key, 0 );
179 int bdb_entry_return(
183 /* Our entries are allocated in two blocks; the data comes from
184 * the db itself and the Entry structure and associated pointers
185 * are allocated in entry_decode. The db data pointer is saved
188 if ( e->e_bv.bv_val ) {
189 /* See if the DNs were changed by modrdn */
190 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
191 e->e_bv.bv_val + e->e_bv.bv_len ) {
192 ch_free(e->e_name.bv_val);
193 ch_free(e->e_nname.bv_val);
195 e->e_name.bv_val = NULL;
196 e->e_nname.bv_val = NULL;
197 /* In tool mode the e_bv buffer is realloc'd, leave it alone */
198 if( !(slapMode & SLAP_TOOL_MODE) ) {
199 free( e->e_bv.bv_val );
201 BER_BVZERO( &e->e_bv );
207 int bdb_entry_release(
212 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
213 struct bdb_op_info *boi = NULL;
215 /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
216 SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
218 if ( slapMode == SLAP_SERVER_MODE ) {
219 /* If not in our cache, just free it */
220 if ( !e->e_private ) {
221 #ifdef SLAP_ZONE_ALLOC
222 return bdb_entry_return( bdb, e, -1 );
224 return bdb_entry_return( e );
227 /* free entry and reader or writer lock */
228 boi = (struct bdb_op_info *)op->o_private;
230 /* lock is freed with txn */
231 if ( !boi || boi->boi_txn ) {
232 bdb_unlocked_cache_return_entry_rw( &bdb->bi_cache, e, rw );
234 struct bdb_lock_info *bli, *prev;
235 for ( prev=(struct bdb_lock_info *)&boi->boi_locks,
236 bli = boi->boi_locks; bli; prev=bli, bli=bli->bli_next ) {
237 if ( bli->bli_id == e->e_id ) {
238 bdb_cache_return_entry_rw( bdb->bi_dbenv, &bdb->bi_cache,
239 e, rw, &bli->bli_lock );
240 prev->bli_next = bli->bli_next;
241 op->o_tmpfree( bli, op->o_tmpmemctx );
245 if ( !boi->boi_locks ) {
246 op->o_tmpfree( boi, op->o_tmpmemctx );
247 op->o_private = NULL;
251 #ifdef SLAP_ZONE_ALLOC
253 if (e->e_private != NULL) {
254 BEI(e)->bei_e = NULL;
255 zseq = BEI(e)->bei_zseq;
258 if (e->e_private != NULL)
259 BEI(e)->bei_e = NULL;
262 #ifdef SLAP_ZONE_ALLOC
263 bdb_entry_return ( bdb, e, zseq );
265 bdb_entry_return ( e );
272 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
278 AttributeDescription *at,
282 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
283 struct bdb_op_info *boi = NULL;
288 const char *at_name = at ? at->ad_cname.bv_val : "(null)";
290 u_int32_t locker = 0;
292 int free_lock_id = 0;
294 Debug( LDAP_DEBUG_ARGS,
295 "=> bdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 );
296 Debug( LDAP_DEBUG_ARGS,
297 "=> bdb_entry_get: oc: \"%s\", at: \"%s\"\n",
298 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
300 if( op ) boi = (struct bdb_op_info *) op->o_private;
301 if( boi != NULL && op->o_bd->be_private == boi->boi_bdb->be_private ) {
303 locker = boi->boi_locker;
307 locker = TXN_ID ( txn );
308 } else if ( !locker ) {
309 rc = LOCK_ID ( bdb->bi_dbenv, &locker );
320 /* can we find entry */
321 rc = bdb_dn2entry( op, txn, ndn, &ei, 0, locker, &lock );
326 case DB_LOCK_DEADLOCK:
327 case DB_LOCK_NOTGRANTED:
328 /* the txn must abort and retry */
333 ldap_pvt_thread_yield();
336 if ( boi ) boi->boi_err = rc;
337 if ( free_lock_id ) {
338 LOCK_ID_FREE( bdb->bi_dbenv, locker );
340 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
342 if (ei) e = ei->bei_e;
344 Debug( LDAP_DEBUG_ACL,
345 "=> bdb_entry_get: cannot find entry: \"%s\"\n",
347 if ( free_lock_id ) {
348 LOCK_ID_FREE( bdb->bi_dbenv, locker );
350 return LDAP_NO_SUCH_OBJECT;
353 Debug( LDAP_DEBUG_ACL,
354 "=> bdb_entry_get: found entry: \"%s\"\n",
357 /* find attribute values */
358 if( is_entry_alias( e ) ) {
359 Debug( LDAP_DEBUG_ACL,
360 "<= bdb_entry_get: entry is an alias\n", 0, 0, 0 );
361 rc = LDAP_ALIAS_PROBLEM;
365 if( is_entry_referral( e ) ) {
366 Debug( LDAP_DEBUG_ACL,
367 "<= bdb_entry_get: entry is a referral\n", 0, 0, 0 );
372 if ( oc && !is_entry_objectclass( e, oc, 0 )) {
373 Debug( LDAP_DEBUG_ACL,
374 "<= bdb_entry_get: failed to find objectClass %s\n",
375 oc->soc_cname.bv_val, 0, 0 );
376 rc = LDAP_NO_SUCH_ATTRIBUTE;
381 if( rc != LDAP_SUCCESS ) {
383 bdb_cache_return_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, rw, &lock);
386 if ( slapMode == SLAP_SERVER_MODE ) {
388 /* big drag. we need a place to store a read lock so we can
389 * release it later?? If we're in a txn, nothing is needed
390 * here because the locks will go away with the txn.
394 boi = op->o_tmpcalloc(1,sizeof(struct bdb_op_info),op->o_tmpmemctx);
395 boi->boi_bdb = op->o_bd;
398 if ( !boi->boi_txn ) {
399 struct bdb_lock_info *bli;
400 bli = op->o_tmpalloc( sizeof(struct bdb_lock_info),
402 bli->bli_next = boi->boi_locks;
403 bli->bli_id = e->e_id;
404 bli->bli_lock = lock;
405 boi->boi_locks = bli;
409 *ent = entry_dup( e );
410 bdb_cache_return_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, rw, &lock);
414 if ( free_lock_id ) {
415 LOCK_ID_FREE( bdb->bi_dbenv, locker );
418 Debug( LDAP_DEBUG_TRACE,
419 "bdb_entry_get: rc=%d\n",