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-2004 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>
25 static int bdb_id2entry_put(
31 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
32 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;
47 key.data = (char *) &e->e_id;
48 key.size = sizeof(ID);
50 rc = entry_encode( e, &bv );
52 e->e_name = odn; e->e_nname = ondn;
54 if( rc != LDAP_SUCCESS ) {
61 rc = db->put( db, tid, &key, &data, flag );
68 * This routine adds (or updates) an entry on disk.
69 * The cache should be already be updated.
78 return bdb_id2entry_put(be, tid, e, DB_NOOVERWRITE);
81 int bdb_id2entry_update(
86 return bdb_id2entry_put(be, tid, e, 0);
95 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
96 DB *db = bdb->bi_id2entry->bdi_db;
104 key.data = (char *) &id;
105 key.size = sizeof(ID);
108 data.flags = DB_DBT_MALLOC;
111 rc = db->get( db, tid, &key, &data, bdb->bi_db_opflags );
117 DBT2bv( &data, &bv );
119 rc = entry_decode( &bv, e );
124 /* only free on error. On success, the entry was
127 ch_free( data.data );
133 int bdb_id2entry_delete(
138 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
139 DB *db = bdb->bi_id2entry->bdi_db;
144 key.data = (char *) &e->e_id;
145 key.size = sizeof(ID);
147 /* delete from database */
148 rc = db->del( db, tid, &key, 0 );
153 int bdb_entry_return(
156 /* Our entries are allocated in two blocks; the data comes from
157 * the db itself and the Entry structure and associated pointers
158 * are allocated in entry_decode. The db data pointer is saved
159 * in e_bv. Since the Entry structure is allocated as a single
160 * block, e_attrs is always a fixed offset from e. The exception
161 * is when an entry has been modified, in which case we also need
164 if( !e->e_bv.bv_val ) { /* A regular entry, from do_add */
168 if( (void *) e->e_attrs != (void *) (e+1)) {
169 attrs_free( e->e_attrs );
172 /* See if the DNs were changed by modrdn */
173 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
174 e->e_bv.bv_val + e->e_bv.bv_len ) {
175 ch_free(e->e_name.bv_val);
176 ch_free(e->e_nname.bv_val);
177 e->e_name.bv_val = NULL;
178 e->e_nname.bv_val = NULL;
181 /* In tool mode the e_bv buffer is realloc'd, leave it alone */
182 if( !(slapMode & SLAP_TOOL_MODE) ) {
183 free( e->e_bv.bv_val );
186 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 /* If not in our cache, just free it */
206 if ( !e->e_private ) {
207 return bdb_entry_return( e );
209 /* free entry and reader or writer lock */
211 boi = (struct bdb_op_info *)o->o_private;
213 /* lock is freed with txn */
214 if ( !boi || boi->boi_txn ) {
215 bdb_unlocked_cache_return_entry_rw( &bdb->bi_cache, e, rw );
217 bdb_cache_return_entry_rw( bdb->bi_dbenv, &bdb->bi_cache, e, rw, &boi->boi_lock );
218 o->o_tmpfree( boi, o->o_tmpmemctx );
222 if (e->e_private != NULL)
223 BEI(e)->bei_e = NULL;
225 bdb_entry_return ( e );
231 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
237 AttributeDescription *at,
241 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
242 struct bdb_op_info *boi = NULL;
247 const char *at_name = at ? at->ad_cname.bv_val : "(null)";
249 u_int32_t locker = 0;
251 int free_lock_id = 0;
254 LDAP_LOG( BACK_BDB, ARGS,
255 "bdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 );
256 LDAP_LOG( BACK_BDB, ARGS,
257 "bdb_entry_get: oc: \"%s\", at: \"%s\"\n",
258 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
260 Debug( LDAP_DEBUG_ARGS,
261 "=> bdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 );
262 Debug( LDAP_DEBUG_ARGS,
263 "=> bdb_entry_get: oc: \"%s\", at: \"%s\"\n",
264 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
267 if( op ) boi = (struct bdb_op_info *) op->o_private;
268 if( boi != NULL && op->o_bd == boi->boi_bdb ) {
270 locker = boi->boi_locker;
274 locker = TXN_ID ( txn );
275 } else if ( !locker ) {
276 rc = LOCK_ID ( bdb->bi_dbenv, &locker );
287 /* can we find entry */
288 rc = bdb_dn2entry( op, txn, ndn, &ei, 0, locker, &lock );
293 case DB_LOCK_DEADLOCK:
294 case DB_LOCK_NOTGRANTED:
295 /* the txn must abort and retry */
300 ldap_pvt_thread_yield();
303 if ( boi ) boi->boi_err = rc;
304 if ( free_lock_id ) {
305 LOCK_ID_FREE( bdb->bi_dbenv, locker );
307 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
309 if (ei) e = ei->bei_e;
312 LDAP_LOG( BACK_BDB, INFO,
313 "bdb_entry_get: cannot find entry (%s)\n",
316 Debug( LDAP_DEBUG_ACL,
317 "=> bdb_entry_get: cannot find entry: \"%s\"\n",
320 if ( free_lock_id ) {
321 LOCK_ID_FREE( bdb->bi_dbenv, locker );
323 return LDAP_NO_SUCH_OBJECT;
327 LDAP_LOG( BACK_BDB, DETAIL1, "bdb_entry_get: found entry (%s)\n",
330 Debug( LDAP_DEBUG_ACL,
331 "=> bdb_entry_get: found entry: \"%s\"\n",
336 /* find attribute values */
337 if( is_entry_alias( e ) ) {
339 LDAP_LOG( BACK_BDB, INFO,
340 "bdb_entry_get: entry (%s) is an alias\n", e->e_name.bv_val, 0, 0 );
342 Debug( LDAP_DEBUG_ACL,
343 "<= bdb_entry_get: entry is an alias\n", 0, 0, 0 );
345 rc = LDAP_ALIAS_PROBLEM;
350 if( is_entry_referral( e ) ) {
352 LDAP_LOG( BACK_BDB, INFO,
353 "bdb_entry_get: entry (%s) is a referral.\n", e->e_name.bv_val, 0, 0);
355 Debug( LDAP_DEBUG_ACL,
356 "<= bdb_entry_get: entry is a referral\n", 0, 0, 0 );
362 if ( oc && !is_entry_objectclass( e, oc, 0 )) {
364 LDAP_LOG( BACK_BDB, INFO,
365 "bdb_entry_get: failed to find objectClass.\n", 0, 0, 0 );
367 Debug( LDAP_DEBUG_ACL,
368 "<= bdb_entry_get: failed to find objectClass\n",
371 rc = LDAP_NO_SUCH_ATTRIBUTE;
376 if( rc != LDAP_SUCCESS ) {
378 bdb_cache_return_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, rw, &lock);
381 if ( slapMode == SLAP_SERVER_MODE ) {
383 /* big drag. we need a place to store a read lock so we can
387 boi = op->o_tmpcalloc(1,sizeof(struct bdb_op_info),op->o_tmpmemctx);
388 boi->boi_lock = lock;
393 *ent = entry_dup( e );
394 bdb_cache_return_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, rw, &lock);
398 if ( free_lock_id ) {
399 LOCK_ID_FREE( bdb->bi_dbenv, locker );
403 LDAP_LOG( BACK_BDB, ENTRY, "bdb_entry_get: rc=%d\n", rc, 0, 0 );
405 Debug( LDAP_DEBUG_TRACE,
406 "bdb_entry_get: rc=%d\n",