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>
24 static int bdb_id2entry_put(
30 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
31 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;
48 /* Store ID in BigEndian format */
50 key.size = sizeof(ID);
51 BDB_ID2DISK( e->e_id, &nid );
53 rc = entry_encode( e, &bv );
55 e->e_name = odn; e->e_nname = ondn;
57 if( rc != LDAP_SUCCESS ) {
64 rc = db->put( db, tid, &key, &data, flag );
71 * This routine adds (or updates) an entry on disk.
72 * The cache should be already be updated.
81 return bdb_id2entry_put(be, tid, e, DB_NOOVERWRITE);
84 int bdb_id2entry_update(
89 return bdb_id2entry_put(be, tid, e, 0);
98 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
99 DB *db = bdb->bi_id2entry->bdi_db;
109 key.size = sizeof(ID);
110 BDB_ID2DISK( id, &nid );
113 data.flags = DB_DBT_MALLOC;
116 rc = db->get( db, tid, &key, &data, bdb->bi_db_opflags );
122 DBT2bv( &data, &bv );
124 rc = entry_decode( &bv, e );
129 /* only free on error. On success, the entry was
132 ch_free( data.data );
138 int bdb_id2entry_delete(
143 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
144 DB *db = bdb->bi_id2entry->bdi_db;
151 key.size = sizeof(ID);
152 BDB_ID2DISK( e->e_id, &nid );
154 /* delete from database */
155 rc = db->del( db, tid, &key, 0 );
160 int bdb_entry_return(
163 /* Our entries are allocated in two blocks; the data comes from
164 * the db itself and the Entry structure and associated pointers
165 * are allocated in entry_decode. The db data pointer is saved
166 * in e_bv. Since the Entry structure is allocated as a single
167 * block, e_attrs is always a fixed offset from e. The exception
168 * is when an entry has been modified, in which case we also need
171 if( !e->e_bv.bv_val ) { /* A regular entry, from do_add */
175 if( (void *) e->e_attrs != (void *) (e+1)) {
176 attrs_free( e->e_attrs );
179 /* See if the DNs were changed by modrdn */
180 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
181 e->e_bv.bv_val + e->e_bv.bv_len ) {
182 ch_free(e->e_name.bv_val);
183 ch_free(e->e_nname.bv_val);
184 e->e_name.bv_val = NULL;
185 e->e_nname.bv_val = NULL;
188 /* In tool mode the e_bv buffer is realloc'd, leave it alone */
189 if( !(slapMode & SLAP_TOOL_MODE) ) {
190 free( e->e_bv.bv_val );
193 free( e->e_bv.bv_val );
200 int bdb_entry_release(
205 struct bdb_info *bdb = (struct bdb_info *) o->o_bd->be_private;
206 struct bdb_op_info *boi = NULL;
208 /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
209 SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
211 if ( slapMode == SLAP_SERVER_MODE ) {
212 /* If not in our cache, just free it */
213 if ( !e->e_private ) {
214 return bdb_entry_return( e );
216 /* free entry and reader or writer lock */
218 boi = (struct bdb_op_info *)o->o_private;
220 /* lock is freed with txn */
221 if ( !boi || boi->boi_txn ) {
222 bdb_unlocked_cache_return_entry_rw( &bdb->bi_cache, e, rw );
224 struct bdb_lock_info *bli, *prev;
225 for ( prev=(struct bdb_lock_info *)&boi->boi_locks,
226 bli = boi->boi_locks; bli; prev=bli, bli=bli->bli_next ) {
227 if ( bli->bli_id == e->e_id ) {
228 bdb_cache_return_entry_rw( bdb->bi_dbenv, &bdb->bi_cache,
229 e, rw, &bli->bli_lock );
230 prev->bli_next = bli->bli_next;
231 o->o_tmpfree( bli, o->o_tmpmemctx );
235 if ( !boi->boi_locks ) {
236 o->o_tmpfree( boi, o->o_tmpmemctx );
241 if (e->e_private != NULL)
242 BEI(e)->bei_e = NULL;
244 bdb_entry_return ( e );
250 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
256 AttributeDescription *at,
260 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
261 struct bdb_op_info *boi = NULL;
266 const char *at_name = at ? at->ad_cname.bv_val : "(null)";
268 u_int32_t locker = 0;
270 int free_lock_id = 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);
278 if( op ) boi = (struct bdb_op_info *) op->o_private;
279 if( boi != NULL && op->o_bd->be_private == boi->boi_bdb->be_private ) {
281 locker = boi->boi_locker;
285 locker = TXN_ID ( txn );
286 } else if ( !locker ) {
287 rc = LOCK_ID ( bdb->bi_dbenv, &locker );
298 /* can we find entry */
299 rc = bdb_dn2entry( op, txn, ndn, &ei, 0, locker, &lock );
304 case DB_LOCK_DEADLOCK:
305 case DB_LOCK_NOTGRANTED:
306 /* the txn must abort and retry */
311 ldap_pvt_thread_yield();
314 if ( boi ) boi->boi_err = rc;
315 if ( free_lock_id ) {
316 LOCK_ID_FREE( bdb->bi_dbenv, locker );
318 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
320 if (ei) e = ei->bei_e;
322 Debug( LDAP_DEBUG_ACL,
323 "=> bdb_entry_get: cannot find entry: \"%s\"\n",
325 if ( free_lock_id ) {
326 LOCK_ID_FREE( bdb->bi_dbenv, locker );
328 return LDAP_NO_SUCH_OBJECT;
331 Debug( LDAP_DEBUG_ACL,
332 "=> bdb_entry_get: found entry: \"%s\"\n",
335 /* find attribute values */
336 if( is_entry_alias( e ) ) {
337 Debug( LDAP_DEBUG_ACL,
338 "<= bdb_entry_get: entry is an alias\n", 0, 0, 0 );
339 rc = LDAP_ALIAS_PROBLEM;
343 if( is_entry_referral( e ) ) {
344 Debug( LDAP_DEBUG_ACL,
345 "<= bdb_entry_get: entry is a referral\n", 0, 0, 0 );
350 if ( oc && !is_entry_objectclass( e, oc, 0 )) {
351 Debug( LDAP_DEBUG_ACL,
352 "<= bdb_entry_get: failed to find objectClass\n",
354 rc = LDAP_NO_SUCH_ATTRIBUTE;
359 if( rc != LDAP_SUCCESS ) {
361 bdb_cache_return_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, rw, &lock);
364 if ( slapMode == SLAP_SERVER_MODE ) {
366 /* big drag. we need a place to store a read lock so we can
367 * release it later?? If we're in a txn, nothing is needed
368 * here because the locks will go away with the txn.
372 boi = op->o_tmpcalloc(1,sizeof(struct bdb_op_info),op->o_tmpmemctx);
373 boi->boi_bdb = op->o_bd;
376 if ( !boi->boi_txn ) {
377 struct bdb_lock_info *bli;
378 bli = op->o_tmpalloc( sizeof(struct bdb_lock_info),
380 bli->bli_next = boi->boi_locks;
381 bli->bli_id = e->e_id;
382 bli->bli_lock = lock;
383 boi->boi_locks = bli;
387 *ent = entry_dup( e );
388 bdb_cache_return_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, rw, &lock);
392 if ( free_lock_id ) {
393 LOCK_ID_FREE( bdb->bi_dbenv, locker );
396 Debug( LDAP_DEBUG_TRACE,
397 "bdb_entry_get: rc=%d\n",