1 /* id2entry.c - routines to deal with the id2entry database */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
21 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
22 DB *db = bdb->bi_id2entry->bdi_db;
27 struct berval odn, ondn;
29 /* We only store rdns, and they go in the id2parent database. */
31 odn = e->e_name; ondn = e->e_nname;
33 e->e_name = slap_empty_bv;
34 e->e_nname = slap_empty_bv;
37 key.data = (char *) &e->e_id;
38 key.size = sizeof(ID);
40 rc = entry_encode( e, &bv );
42 e->e_name = odn; e->e_nname = ondn;
44 if( rc != LDAP_SUCCESS ) {
51 rc = db->put( db, tid, &key, &data, flag );
58 * This routine adds (or updates) an entry on disk.
59 * The cache should be already be updated.
68 return bdb_id2entry_put(be, tid, e, DB_NOOVERWRITE);
71 int bdb_id2entry_update(
76 return bdb_id2entry_put(be, tid, e, 0);
88 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
89 DB *db = bdb->bi_id2entry->bdi_db;
97 key.data = (char *) &id;
98 key.size = sizeof(ID);
101 data.flags = DB_DBT_MALLOC;
103 if ((*e = bdb_cache_find_entry_id(bdb->bi_dbenv, &bdb->bi_cache, id, rw, locker, lock)) != NULL) {
108 rc = db->get( db, tid, &key, &data, bdb->bi_db_opflags | ( rw ? DB_RMW : 0 ));
114 DBT2bv( &data, &bv );
116 rc = entry_decode( &bv, e );
121 /* only free on error. On success, the entry was
124 ch_free( data.data );
129 bdb_fix_dn(be, id, *e);
131 ret = bdb_cache_add_entry_rw( bdb->bi_dbenv,
132 &bdb->bi_cache, *e, rw, locker, lock);
133 while ( ret == 1 || ret == -1 ) {
135 int add_loop_cnt = 0;
136 if ( (*e)->e_private != NULL ) {
137 free ((*e)->e_private);
139 (*e)->e_private = NULL;
140 if ( (ee = bdb_cache_find_entry_id
141 (bdb->bi_dbenv, &bdb->bi_cache, id, rw, locker, lock) ) != NULL) {
142 bdb_entry_return ( *e );
146 if ( ++add_loop_cnt == BDB_MAX_ADD_LOOP ) {
147 bdb_entry_return ( *e );
153 if ( (*e)->e_private != NULL )
154 free ( (*e)->e_private );
155 bdb_entry_return( *e );
162 bdb_cache_entry_commit(*e);
168 int bdb_id2entry_delete(
173 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
174 DB *db = bdb->bi_id2entry->bdi_db;
178 bdb_cache_delete_entry(&bdb->bi_cache, e);
181 key.data = (char *) &e->e_id;
182 key.size = sizeof(ID);
184 /* delete from database */
185 rc = db->del( db, tid, &key, 0 );
190 int bdb_entry_return(
193 /* Our entries are allocated in two blocks; the data comes from
194 * the db itself and the Entry structure and associated pointers
195 * are allocated in entry_decode. The db data pointer is saved
196 * in e_bv. Since the Entry structure is allocated as a single
197 * block, e_attrs is always a fixed offset from e. The exception
198 * is when an entry has been modified, in which case we also need
201 if( !e->e_bv.bv_val ) { /* A regular entry, from do_add */
205 if( (void *) e->e_attrs != (void *) (e+1)) {
206 attrs_free( e->e_attrs );
208 #if defined(SLAP_NVALUES) && !defined(SLAP_NVALUES_ON_DISK)
210 /* nvals are not contiguous with the rest. oh well. */
212 for (a = e->e_attrs; a; a=a->a_next) {
213 if (a->a_nvals != a->a_vals) {
214 ber_bvarray_free( a->a_nvals );
222 /* See if the DNs were changed by modrdn */
223 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
224 e->e_bv.bv_val + e->e_bv.bv_len ) {
225 ch_free(e->e_name.bv_val);
226 ch_free(e->e_nname.bv_val);
227 e->e_name.bv_val = NULL;
228 e->e_nname.bv_val = NULL;
231 /* We had to construct the dn and ndn as well, in a single block */
232 if( e->e_name.bv_val ) {
233 free( e->e_name.bv_val );
236 /* In tool mode the e_bv buffer is realloc'd, leave it alone */
237 if( !(slapMode & SLAP_TOOL_MODE) ) {
238 free( e->e_bv.bv_val );
246 int bdb_entry_release(
251 struct bdb_info *bdb = (struct bdb_info *) o->o_bd->be_private;
252 struct bdb_op_info *boi = NULL;
254 /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
255 SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
257 if ( slapMode == SLAP_SERVER_MODE ) {
258 /* free entry and reader or writer lock */
260 boi = (struct bdb_op_info *)o->o_private;
262 /* lock is freed with txn */
263 if ( !boi || boi->boi_txn ) {
264 bdb_unlocked_cache_return_entry_rw( &bdb->bi_cache, e, rw );
266 bdb_cache_return_entry_rw( bdb->bi_dbenv, &bdb->bi_cache, e, rw, &boi->boi_lock );
271 if (e->e_private != NULL)
274 bdb_entry_return ( e );
280 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
286 AttributeDescription *at,
290 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
291 struct bdb_op_info *boi = NULL;
295 const char *at_name = at->ad_cname.bv_val;
297 u_int32_t locker = 0;
299 int free_lock_id = 0;
302 LDAP_LOG( BACK_BDB, ARGS,
303 "bdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 );
304 LDAP_LOG( BACK_BDB, ARGS,
305 "bdb_entry_get: oc: \"%s\", at: \"%s\"\n",
306 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
308 Debug( LDAP_DEBUG_ARGS,
309 "=> bdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 );
310 Debug( LDAP_DEBUG_ARGS,
311 "=> bdb_entry_get: oc: \"%s\", at: \"%s\"\n",
312 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
315 if( op ) boi = (struct bdb_op_info *) op->o_private;
316 if( boi != NULL && op->o_bd == boi->boi_bdb ) {
318 locker = boi->boi_locker;
322 locker = TXN_ID ( txn );
323 } else if ( !locker ) {
324 rc = LOCK_ID ( bdb->bi_dbenv, &locker );
335 /* can we find entry */
336 rc = bdb_dn2entry_rw( op->o_bd, txn, ndn, &e, NULL, 0, rw, locker, &lock );
341 case DB_LOCK_DEADLOCK:
342 case DB_LOCK_NOTGRANTED:
343 /* the txn must abort and retry */
348 ldap_pvt_thread_yield();
352 if ( free_lock_id ) {
353 LOCK_ID_FREE( bdb->bi_dbenv, locker );
355 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
359 LDAP_LOG( BACK_BDB, INFO,
360 "bdb_entry_get: cannot find entry (%s)\n",
363 Debug( LDAP_DEBUG_ACL,
364 "=> bdb_entry_get: cannot find entry: \"%s\"\n",
367 if ( free_lock_id ) {
368 LOCK_ID_FREE( bdb->bi_dbenv, locker );
370 return LDAP_NO_SUCH_OBJECT;
374 LDAP_LOG( BACK_BDB, DETAIL1, "bdb_entry_get: found entry (%s)\n",
377 Debug( LDAP_DEBUG_ACL,
378 "=> bdb_entry_get: found entry: \"%s\"\n",
383 /* find attribute values */
384 if( is_entry_alias( e ) ) {
386 LDAP_LOG( BACK_BDB, INFO,
387 "bdb_entry_get: entry (%s) is an alias\n", e->e_name.bv_val, 0, 0 );
389 Debug( LDAP_DEBUG_ACL,
390 "<= bdb_entry_get: entry is an alias\n", 0, 0, 0 );
392 rc = LDAP_ALIAS_PROBLEM;
397 if( is_entry_referral( e ) ) {
399 LDAP_LOG( BACK_BDB, INFO,
400 "bdb_entry_get: entry (%s) is a referral.\n", e->e_name.bv_val, 0, 0);
402 Debug( LDAP_DEBUG_ACL,
403 "<= bdb_entry_get: entry is a referral\n", 0, 0, 0 );
409 if ( oc && !is_entry_objectclass( e, oc, 0 )) {
411 LDAP_LOG( BACK_BDB, INFO,
412 "bdb_entry_get: failed to find objectClass.\n", 0, 0, 0 );
414 Debug( LDAP_DEBUG_ACL,
415 "<= bdb_entry_get: failed to find objectClass\n",
418 rc = LDAP_NO_SUCH_ATTRIBUTE;
423 if( rc != LDAP_SUCCESS ) {
425 bdb_cache_return_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, rw, &lock);
428 /* big drag. we need a place to store a read lock so we can
432 boi = ch_calloc(1,sizeof(struct bdb_op_info));
433 boi->boi_lock = lock;
438 if ( free_lock_id ) {
439 LOCK_ID_FREE( bdb->bi_dbenv, locker );
443 LDAP_LOG( BACK_BDB, ENTRY, "bdb_entry_get: rc=%d\n", rc, 0, 0 );
445 Debug( LDAP_DEBUG_TRACE,
446 "bdb_entry_get: rc=%d\n",