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 );
163 /* See if the DNs were changed by modrdn */
164 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
165 e->e_bv.bv_val + e->e_bv.bv_len ) {
166 ch_free(e->e_name.bv_val);
167 ch_free(e->e_nname.bv_val);
168 e->e_name.bv_val = NULL;
169 e->e_nname.bv_val = NULL;
172 /* In tool mode the e_bv buffer is realloc'd, leave it alone */
173 if( !(slapMode & SLAP_TOOL_MODE) ) {
174 free( e->e_bv.bv_val );
177 free( e->e_bv.bv_val );
184 int bdb_entry_release(
189 struct bdb_info *bdb = (struct bdb_info *) o->o_bd->be_private;
190 struct bdb_op_info *boi = NULL;
192 /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
193 SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
195 if ( slapMode == SLAP_SERVER_MODE ) {
196 /* free entry and reader or writer lock */
198 boi = (struct bdb_op_info *)o->o_private;
200 /* lock is freed with txn */
201 if ( !boi || boi->boi_txn ) {
202 bdb_unlocked_cache_return_entry_rw( &bdb->bi_cache, e, rw );
204 bdb_cache_return_entry_rw( bdb->bi_dbenv, &bdb->bi_cache, e, rw, &boi->boi_lock );
205 o->o_tmpfree( boi, o->o_tmpmemctx );
209 if (e->e_private != NULL)
210 BEI(e)->bei_e = NULL;
212 bdb_entry_return ( e );
218 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
224 AttributeDescription *at,
228 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
229 struct bdb_op_info *boi = NULL;
234 const char *at_name = at ? at->ad_cname.bv_val : "(null)";
236 u_int32_t locker = 0;
238 int free_lock_id = 0;
241 LDAP_LOG( BACK_BDB, ARGS,
242 "bdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 );
243 LDAP_LOG( BACK_BDB, ARGS,
244 "bdb_entry_get: oc: \"%s\", at: \"%s\"\n",
245 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
247 Debug( LDAP_DEBUG_ARGS,
248 "=> bdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 );
249 Debug( LDAP_DEBUG_ARGS,
250 "=> bdb_entry_get: oc: \"%s\", at: \"%s\"\n",
251 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
254 if( op ) boi = (struct bdb_op_info *) op->o_private;
255 if( boi != NULL && op->o_bd == boi->boi_bdb ) {
257 locker = boi->boi_locker;
261 locker = TXN_ID ( txn );
262 } else if ( !locker ) {
263 rc = LOCK_ID ( bdb->bi_dbenv, &locker );
274 /* can we find entry */
275 rc = bdb_dn2entry( op, txn, ndn, &ei, 0, locker, &lock );
280 case DB_LOCK_DEADLOCK:
281 case DB_LOCK_NOTGRANTED:
282 /* the txn must abort and retry */
287 ldap_pvt_thread_yield();
290 if ( boi ) boi->boi_err = rc;
291 if ( free_lock_id ) {
292 LOCK_ID_FREE( bdb->bi_dbenv, locker );
294 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
296 if (ei) e = ei->bei_e;
299 LDAP_LOG( BACK_BDB, INFO,
300 "bdb_entry_get: cannot find entry (%s)\n",
303 Debug( LDAP_DEBUG_ACL,
304 "=> bdb_entry_get: cannot find entry: \"%s\"\n",
307 if ( free_lock_id ) {
308 LOCK_ID_FREE( bdb->bi_dbenv, locker );
310 return LDAP_NO_SUCH_OBJECT;
314 LDAP_LOG( BACK_BDB, DETAIL1, "bdb_entry_get: found entry (%s)\n",
317 Debug( LDAP_DEBUG_ACL,
318 "=> bdb_entry_get: found entry: \"%s\"\n",
323 /* find attribute values */
324 if( is_entry_alias( e ) ) {
326 LDAP_LOG( BACK_BDB, INFO,
327 "bdb_entry_get: entry (%s) is an alias\n", e->e_name.bv_val, 0, 0 );
329 Debug( LDAP_DEBUG_ACL,
330 "<= bdb_entry_get: entry is an alias\n", 0, 0, 0 );
332 rc = LDAP_ALIAS_PROBLEM;
337 if( is_entry_referral( e ) ) {
339 LDAP_LOG( BACK_BDB, INFO,
340 "bdb_entry_get: entry (%s) is a referral.\n", e->e_name.bv_val, 0, 0);
342 Debug( LDAP_DEBUG_ACL,
343 "<= bdb_entry_get: entry is a referral\n", 0, 0, 0 );
349 if ( oc && !is_entry_objectclass( e, oc, 0 )) {
351 LDAP_LOG( BACK_BDB, INFO,
352 "bdb_entry_get: failed to find objectClass.\n", 0, 0, 0 );
354 Debug( LDAP_DEBUG_ACL,
355 "<= bdb_entry_get: failed to find objectClass\n",
358 rc = LDAP_NO_SUCH_ATTRIBUTE;
363 if( rc != LDAP_SUCCESS ) {
365 bdb_cache_return_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, rw, &lock);
368 /* big drag. we need a place to store a read lock so we can
372 boi = op->o_tmpcalloc(1,sizeof(struct bdb_op_info),op->o_tmpmemctx);
373 boi->boi_lock = lock;
378 if ( free_lock_id ) {
379 LOCK_ID_FREE( bdb->bi_dbenv, locker );
383 LDAP_LOG( BACK_BDB, ENTRY, "bdb_entry_get: rc=%d\n", rc, 0, 0 );
385 Debug( LDAP_DEBUG_TRACE,
386 "bdb_entry_get: rc=%d\n",