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);
85 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
86 DB *db = bdb->bi_id2entry->bdi_db;
94 key.data = (char *) &id;
95 key.size = sizeof(ID);
98 data.flags = DB_DBT_MALLOC;
101 rc = db->get( db, tid, &key, &data, bdb->bi_db_opflags );
107 DBT2bv( &data, &bv );
109 rc = entry_decode( &bv, e );
114 /* only free on error. On success, the entry was
117 ch_free( data.data );
122 bdb_fix_dn(be, id, *e);
129 int bdb_id2entry_delete(
134 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
135 DB *db = bdb->bi_id2entry->bdi_db;
140 key.data = (char *) &e->e_id;
141 key.size = sizeof(ID);
143 /* delete from database */
144 rc = db->del( db, tid, &key, 0 );
149 int bdb_entry_return(
152 /* Our entries are allocated in two blocks; the data comes from
153 * the db itself and the Entry structure and associated pointers
154 * are allocated in entry_decode. The db data pointer is saved
155 * in e_bv. Since the Entry structure is allocated as a single
156 * block, e_attrs is always a fixed offset from e. The exception
157 * is when an entry has been modified, in which case we also need
160 if( !e->e_bv.bv_val ) { /* A regular entry, from do_add */
164 if( (void *) e->e_attrs != (void *) (e+1)) {
165 attrs_free( e->e_attrs );
169 /* See if the DNs were changed by modrdn */
170 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
171 e->e_bv.bv_val + e->e_bv.bv_len ) {
172 ch_free(e->e_name.bv_val);
173 ch_free(e->e_nname.bv_val);
174 e->e_name.bv_val = NULL;
175 e->e_nname.bv_val = NULL;
178 /* We had to construct the dn and ndn as well, in a single block */
179 if( e->e_name.bv_val ) {
180 free( e->e_name.bv_val );
183 /* In tool mode the e_bv buffer is realloc'd, leave it alone */
184 if( !(slapMode & SLAP_TOOL_MODE) ) {
185 free( e->e_bv.bv_val );
193 int bdb_entry_release(
198 struct bdb_info *bdb = (struct bdb_info *) o->o_bd->be_private;
199 struct bdb_op_info *boi = NULL;
201 /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
202 SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
204 if ( slapMode == SLAP_SERVER_MODE ) {
205 /* free entry and reader or writer lock */
207 boi = (struct bdb_op_info *)o->o_private;
209 /* lock is freed with txn */
210 if ( !boi || boi->boi_txn ) {
211 bdb_unlocked_cache_return_entry_rw( &bdb->bi_cache, e, rw );
213 bdb_cache_return_entry_rw( bdb->bi_dbenv, &bdb->bi_cache, e, rw, &boi->boi_lock );
214 sl_free( boi, o->o_tmpmemctx );
218 if (e->e_private != NULL)
221 bdb_entry_return ( e );
227 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
233 AttributeDescription *at,
237 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
238 struct bdb_op_info *boi = NULL;
243 const char *at_name = at->ad_cname.bv_val;
245 u_int32_t locker = 0;
247 int free_lock_id = 0;
250 LDAP_LOG( BACK_BDB, ARGS,
251 "bdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 );
252 LDAP_LOG( BACK_BDB, ARGS,
253 "bdb_entry_get: oc: \"%s\", at: \"%s\"\n",
254 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
256 Debug( LDAP_DEBUG_ARGS,
257 "=> bdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 );
258 Debug( LDAP_DEBUG_ARGS,
259 "=> bdb_entry_get: oc: \"%s\", at: \"%s\"\n",
260 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
263 if( op ) boi = (struct bdb_op_info *) op->o_private;
264 if( boi != NULL && op->o_bd == boi->boi_bdb ) {
266 locker = boi->boi_locker;
270 locker = TXN_ID ( txn );
271 } else if ( !locker ) {
272 rc = LOCK_ID ( bdb->bi_dbenv, &locker );
283 /* can we find entry */
284 rc = bdb_dn2entry( op->o_bd, txn, ndn, &ei, 0, locker, &lock, op->o_tmpmemctx );
289 case DB_LOCK_DEADLOCK:
290 case DB_LOCK_NOTGRANTED:
291 /* the txn must abort and retry */
296 ldap_pvt_thread_yield();
300 if ( free_lock_id ) {
301 LOCK_ID_FREE( bdb->bi_dbenv, locker );
303 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
305 if (ei) e = ei->bei_e;
308 LDAP_LOG( BACK_BDB, INFO,
309 "bdb_entry_get: cannot find entry (%s)\n",
312 Debug( LDAP_DEBUG_ACL,
313 "=> bdb_entry_get: cannot find entry: \"%s\"\n",
316 if ( free_lock_id ) {
317 LOCK_ID_FREE( bdb->bi_dbenv, locker );
319 return LDAP_NO_SUCH_OBJECT;
323 LDAP_LOG( BACK_BDB, DETAIL1, "bdb_entry_get: found entry (%s)\n",
326 Debug( LDAP_DEBUG_ACL,
327 "=> bdb_entry_get: found entry: \"%s\"\n",
332 /* find attribute values */
333 if( is_entry_alias( e ) ) {
335 LDAP_LOG( BACK_BDB, INFO,
336 "bdb_entry_get: entry (%s) is an alias\n", e->e_name.bv_val, 0, 0 );
338 Debug( LDAP_DEBUG_ACL,
339 "<= bdb_entry_get: entry is an alias\n", 0, 0, 0 );
341 rc = LDAP_ALIAS_PROBLEM;
346 if( is_entry_referral( e ) ) {
348 LDAP_LOG( BACK_BDB, INFO,
349 "bdb_entry_get: entry (%s) is a referral.\n", e->e_name.bv_val, 0, 0);
351 Debug( LDAP_DEBUG_ACL,
352 "<= bdb_entry_get: entry is a referral\n", 0, 0, 0 );
358 if ( oc && !is_entry_objectclass( e, oc, 0 )) {
360 LDAP_LOG( BACK_BDB, INFO,
361 "bdb_entry_get: failed to find objectClass.\n", 0, 0, 0 );
363 Debug( LDAP_DEBUG_ACL,
364 "<= bdb_entry_get: failed to find objectClass\n",
367 rc = LDAP_NO_SUCH_ATTRIBUTE;
372 if( rc != LDAP_SUCCESS ) {
374 bdb_cache_return_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, rw, &lock);
377 /* big drag. we need a place to store a read lock so we can
381 boi = sl_calloc(1,sizeof(struct bdb_op_info),op->o_tmpmemctx);
382 boi->boi_lock = lock;
387 if ( free_lock_id ) {
388 LOCK_ID_FREE( bdb->bi_dbenv, locker );
392 LDAP_LOG( BACK_BDB, ENTRY, "bdb_entry_get: rc=%d\n", rc, 0, 0 );
394 Debug( LDAP_DEBUG_TRACE,
395 "bdb_entry_get: rc=%d\n",