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>
16 static int bdb_id2entry_put(
22 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
23 DB *db = bdb->bi_id2entry->bdi_db;
28 struct berval odn, ondn;
30 /* We only store rdns, and they go in the dn2id database. */
32 odn = e->e_name; ondn = e->e_nname;
34 e->e_name = slap_empty_bv;
35 e->e_nname = slap_empty_bv;
38 key.data = (char *) &e->e_id;
39 key.size = sizeof(ID);
41 rc = entry_encode( e, &bv );
43 e->e_name = odn; e->e_nname = ondn;
45 if( rc != LDAP_SUCCESS ) {
52 rc = db->put( db, tid, &key, &data, flag );
59 * This routine adds (or updates) an entry on disk.
60 * The cache should be already be updated.
69 return bdb_id2entry_put(be, tid, e, DB_NOOVERWRITE);
72 int bdb_id2entry_update(
77 return bdb_id2entry_put(be, tid, e, 0);
86 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
87 DB *db = bdb->bi_id2entry->bdi_db;
95 key.data = (char *) &id;
96 key.size = sizeof(ID);
99 data.flags = DB_DBT_MALLOC;
102 rc = db->get( db, tid, &key, &data, bdb->bi_db_opflags );
108 DBT2bv( &data, &bv );
110 rc = entry_decode( &bv, e );
115 /* only free on error. On success, the entry was
118 ch_free( data.data );
124 int bdb_id2entry_delete(
129 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
130 DB *db = bdb->bi_id2entry->bdi_db;
135 key.data = (char *) &e->e_id;
136 key.size = sizeof(ID);
138 /* delete from database */
139 rc = db->del( db, tid, &key, 0 );
144 int bdb_entry_return(
147 /* Our entries are allocated in two blocks; the data comes from
148 * the db itself and the Entry structure and associated pointers
149 * are allocated in entry_decode. The db data pointer is saved
150 * in e_bv. Since the Entry structure is allocated as a single
151 * block, e_attrs is always a fixed offset from e. The exception
152 * is when an entry has been modified, in which case we also need
155 if( !e->e_bv.bv_val ) { /* A regular entry, from do_add */
159 if( (void *) e->e_attrs != (void *) (e+1)) {
160 attrs_free( e->e_attrs );
164 /* See if the DNs were changed by modrdn */
165 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
166 e->e_bv.bv_val + e->e_bv.bv_len ) {
167 ch_free(e->e_name.bv_val);
168 ch_free(e->e_nname.bv_val);
169 e->e_name.bv_val = NULL;
170 e->e_nname.bv_val = NULL;
173 /* We had to construct the dn and ndn as well, in a single block */
174 if( e->e_name.bv_val ) {
175 free( e->e_name.bv_val );
178 /* In tool mode the e_bv buffer is realloc'd, leave it alone */
179 if( !(slapMode & SLAP_TOOL_MODE) ) {
180 free( e->e_bv.bv_val );
188 int bdb_entry_release(
193 struct bdb_info *bdb = (struct bdb_info *) o->o_bd->be_private;
194 struct bdb_op_info *boi = NULL;
196 /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
197 SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
199 if ( slapMode == SLAP_SERVER_MODE ) {
200 /* free entry and reader or writer lock */
202 boi = (struct bdb_op_info *)o->o_private;
204 /* lock is freed with txn */
205 if ( !boi || boi->boi_txn ) {
206 bdb_unlocked_cache_return_entry_rw( &bdb->bi_cache, e, rw );
208 bdb_cache_return_entry_rw( bdb->bi_dbenv, &bdb->bi_cache, e, rw, &boi->boi_lock );
209 sl_free( boi, o->o_tmpmemctx );
213 if (e->e_private != NULL)
214 BEI(e)->bei_e = NULL;
216 bdb_entry_return ( e );
222 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
228 AttributeDescription *at,
232 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
233 struct bdb_op_info *boi = NULL;
238 const char *at_name = at->ad_cname.bv_val;
240 u_int32_t locker = 0;
242 int free_lock_id = 0;
245 LDAP_LOG( BACK_BDB, ARGS,
246 "bdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 );
247 LDAP_LOG( BACK_BDB, ARGS,
248 "bdb_entry_get: oc: \"%s\", at: \"%s\"\n",
249 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
251 Debug( LDAP_DEBUG_ARGS,
252 "=> bdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 );
253 Debug( LDAP_DEBUG_ARGS,
254 "=> bdb_entry_get: oc: \"%s\", at: \"%s\"\n",
255 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
258 if( op ) boi = (struct bdb_op_info *) op->o_private;
259 if( boi != NULL && op->o_bd == boi->boi_bdb ) {
261 locker = boi->boi_locker;
265 locker = TXN_ID ( txn );
266 } else if ( !locker ) {
267 rc = LOCK_ID ( bdb->bi_dbenv, &locker );
278 /* can we find entry */
279 rc = bdb_dn2entry( op->o_bd, txn, ndn, &ei, 0, locker, &lock, op->o_tmpmemctx );
284 case DB_LOCK_DEADLOCK:
285 case DB_LOCK_NOTGRANTED:
286 /* the txn must abort and retry */
291 ldap_pvt_thread_yield();
295 if ( free_lock_id ) {
296 LOCK_ID_FREE( bdb->bi_dbenv, locker );
298 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
300 if (ei) e = ei->bei_e;
303 LDAP_LOG( BACK_BDB, INFO,
304 "bdb_entry_get: cannot find entry (%s)\n",
307 Debug( LDAP_DEBUG_ACL,
308 "=> bdb_entry_get: cannot find entry: \"%s\"\n",
311 if ( free_lock_id ) {
312 LOCK_ID_FREE( bdb->bi_dbenv, locker );
314 return LDAP_NO_SUCH_OBJECT;
318 LDAP_LOG( BACK_BDB, DETAIL1, "bdb_entry_get: found entry (%s)\n",
321 Debug( LDAP_DEBUG_ACL,
322 "=> bdb_entry_get: found entry: \"%s\"\n",
327 /* find attribute values */
328 if( is_entry_alias( e ) ) {
330 LDAP_LOG( BACK_BDB, INFO,
331 "bdb_entry_get: entry (%s) is an alias\n", e->e_name.bv_val, 0, 0 );
333 Debug( LDAP_DEBUG_ACL,
334 "<= bdb_entry_get: entry is an alias\n", 0, 0, 0 );
336 rc = LDAP_ALIAS_PROBLEM;
341 if( is_entry_referral( e ) ) {
343 LDAP_LOG( BACK_BDB, INFO,
344 "bdb_entry_get: entry (%s) is a referral.\n", e->e_name.bv_val, 0, 0);
346 Debug( LDAP_DEBUG_ACL,
347 "<= bdb_entry_get: entry is a referral\n", 0, 0, 0 );
353 if ( oc && !is_entry_objectclass( e, oc, 0 )) {
355 LDAP_LOG( BACK_BDB, INFO,
356 "bdb_entry_get: failed to find objectClass.\n", 0, 0, 0 );
358 Debug( LDAP_DEBUG_ACL,
359 "<= bdb_entry_get: failed to find objectClass\n",
362 rc = LDAP_NO_SUCH_ATTRIBUTE;
367 if( rc != LDAP_SUCCESS ) {
369 bdb_cache_return_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, rw, &lock);
372 /* big drag. we need a place to store a read lock so we can
376 boi = sl_calloc(1,sizeof(struct bdb_op_info),op->o_tmpmemctx);
377 boi->boi_lock = lock;
382 if ( free_lock_id ) {
383 LOCK_ID_FREE( bdb->bi_dbenv, locker );
387 LDAP_LOG( BACK_BDB, ENTRY, "bdb_entry_get: rc=%d\n", rc, 0, 0 );
389 Debug( LDAP_DEBUG_TRACE,
390 "bdb_entry_get: rc=%d\n",