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-2005 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 struct bdb_lock_info *bli, *prev;
218 for ( prev=(struct bdb_lock_info *)&boi->boi_locks,
219 bli = boi->boi_locks; bli; prev=bli, bli=bli->bli_next ) {
220 if ( bli->bli_id == e->e_id ) {
221 bdb_cache_return_entry_rw( bdb->bi_dbenv, &bdb->bi_cache,
222 e, rw, &bli->bli_lock );
223 prev->bli_next = bli->bli_next;
224 o->o_tmpfree( bli, o->o_tmpmemctx );
228 if ( !boi->boi_locks ) {
229 o->o_tmpfree( boi, o->o_tmpmemctx );
234 if (e->e_private != NULL)
235 BEI(e)->bei_e = NULL;
237 bdb_entry_return ( e );
243 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
249 AttributeDescription *at,
253 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
254 struct bdb_op_info *boi = NULL;
259 const char *at_name = at ? at->ad_cname.bv_val : "(null)";
261 u_int32_t locker = 0;
263 int free_lock_id = 0;
266 LDAP_LOG( BACK_BDB, ARGS,
267 "bdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 );
268 LDAP_LOG( BACK_BDB, ARGS,
269 "bdb_entry_get: oc: \"%s\", at: \"%s\"\n",
270 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
272 Debug( LDAP_DEBUG_ARGS,
273 "=> bdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 );
274 Debug( LDAP_DEBUG_ARGS,
275 "=> bdb_entry_get: oc: \"%s\", at: \"%s\"\n",
276 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
279 if( op ) boi = (struct bdb_op_info *) op->o_private;
280 if( boi != NULL && op->o_bd->be_private == boi->boi_bdb->be_private ) {
282 locker = boi->boi_locker;
286 locker = TXN_ID ( txn );
287 } else if ( !locker ) {
288 rc = LOCK_ID ( bdb->bi_dbenv, &locker );
299 /* can we find entry */
300 rc = bdb_dn2entry( op, txn, ndn, &ei, 0, locker, &lock );
305 case DB_LOCK_DEADLOCK:
306 case DB_LOCK_NOTGRANTED:
307 /* the txn must abort and retry */
312 ldap_pvt_thread_yield();
315 if ( boi ) boi->boi_err = rc;
316 if ( free_lock_id ) {
317 LOCK_ID_FREE( bdb->bi_dbenv, locker );
319 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
321 if (ei) e = ei->bei_e;
324 LDAP_LOG( BACK_BDB, INFO,
325 "bdb_entry_get: cannot find entry (%s)\n",
328 Debug( LDAP_DEBUG_ACL,
329 "=> bdb_entry_get: cannot find entry: \"%s\"\n",
332 if ( free_lock_id ) {
333 LOCK_ID_FREE( bdb->bi_dbenv, locker );
335 return LDAP_NO_SUCH_OBJECT;
339 LDAP_LOG( BACK_BDB, DETAIL1, "bdb_entry_get: found entry (%s)\n",
342 Debug( LDAP_DEBUG_ACL,
343 "=> bdb_entry_get: found entry: \"%s\"\n",
347 /* find attribute values */
348 if( is_entry_alias( e ) ) {
350 LDAP_LOG( BACK_BDB, INFO,
351 "bdb_entry_get: entry (%s) is an alias\n", e->e_name.bv_val, 0, 0 );
353 Debug( LDAP_DEBUG_ACL,
354 "<= bdb_entry_get: entry is an alias\n", 0, 0, 0 );
356 rc = LDAP_ALIAS_PROBLEM;
360 if( is_entry_referral( e ) ) {
362 LDAP_LOG( BACK_BDB, INFO,
363 "bdb_entry_get: entry (%s) is a referral.\n", e->e_name.bv_val, 0, 0);
365 Debug( LDAP_DEBUG_ACL,
366 "<= bdb_entry_get: entry is a referral\n", 0, 0, 0 );
372 if ( oc && !is_entry_objectclass( e, oc, 0 )) {
374 LDAP_LOG( BACK_BDB, INFO,
375 "bdb_entry_get: failed to find objectClass.\n", 0, 0, 0 );
377 Debug( LDAP_DEBUG_ACL,
378 "<= bdb_entry_get: failed to find objectClass\n",
381 rc = LDAP_NO_SUCH_ATTRIBUTE;
386 if( rc != LDAP_SUCCESS ) {
388 bdb_cache_return_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, rw, &lock);
391 if ( slapMode == SLAP_SERVER_MODE ) {
393 /* big drag. we need a place to store a read lock so we can
394 * release it later?? If we're in a txn, nothing is needed
395 * here because the locks will go away with the txn.
399 boi = op->o_tmpcalloc(1,sizeof(struct bdb_op_info),op->o_tmpmemctx);
400 boi->boi_bdb = op->o_bd;
403 if ( !boi->boi_txn ) {
404 struct bdb_lock_info *bli;
405 bli = op->o_tmpalloc( sizeof(struct bdb_lock_info),
407 bli->bli_next = boi->boi_locks;
408 bli->bli_id = e->e_id;
409 bli->bli_lock = lock;
410 boi->boi_locks = bli;
414 *ent = entry_dup( e );
415 bdb_cache_return_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, rw, &lock);
419 if ( free_lock_id ) {
420 LOCK_ID_FREE( bdb->bi_dbenv, locker );
424 LDAP_LOG( BACK_BDB, ENTRY, "bdb_entry_get: rc=%d\n", rc, 0, 0 );
426 Debug( LDAP_DEBUG_TRACE,
427 "bdb_entry_get: rc=%d\n",