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-2006 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);
99 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
100 DB *db = bdb->bi_id2entry->bdi_db;
111 key.size = sizeof(ID);
112 BDB_ID2DISK( id, &nid );
115 data.flags = DB_DBT_MALLOC;
118 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
121 /* Use our own locker if needed */
122 if ( !tid && locker )
123 cursor->locker = locker;
125 rc = cursor->c_get( cursor, &key, &data, DB_SET );
126 cursor->c_close( cursor );
132 DBT2bv( &data, &bv );
134 #ifdef SLAP_ZONE_ALLOC
135 rc = entry_decode(&bv, e, bdb->bi_cache.c_zctx);
137 rc = entry_decode(&bv, e);
143 /* only free on error. On success, the entry was
146 #ifndef SLAP_ZONE_ALLOC
150 #ifdef SLAP_ZONE_ALLOC
157 int bdb_id2entry_delete(
162 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
163 DB *db = bdb->bi_id2entry->bdi_db;
170 key.size = sizeof(ID);
171 BDB_ID2DISK( e->e_id, &nid );
173 /* delete from database */
174 rc = db->del( db, tid, &key, 0 );
179 #ifdef SLAP_ZONE_ALLOC
180 int bdb_entry_return(
181 struct bdb_info *bdb,
186 int bdb_entry_return(
191 #ifdef SLAP_ZONE_ALLOC
192 if (!slap_zn_validate(bdb->bi_cache.c_zctx, e, zseq)) {
196 /* Our entries are allocated in two blocks; the data comes from
197 * the db itself and the Entry structure and associated pointers
198 * are allocated in entry_decode. The db data pointer is saved
199 * in e_bv. Since the Entry structure is allocated as a single
200 * block, e_attrs is always a fixed offset from e. The exception
201 * is when an entry has been modified, in which case we also need
205 #ifdef LDAP_COMP_MATCH
206 comp_tree_free( e->e_attrs );
208 if( !e->e_bv.bv_val ) { /* Entry added by do_add */
212 if( (void *) e->e_attrs != (void *) (e+1)) {
213 attrs_free( e->e_attrs );
216 /* See if the DNs were changed by modrdn */
217 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
218 e->e_bv.bv_val + e->e_bv.bv_len ) {
219 ch_free(e->e_name.bv_val);
220 ch_free(e->e_nname.bv_val);
221 e->e_name.bv_val = NULL;
222 e->e_nname.bv_val = NULL;
224 #ifndef SLAP_ZONE_ALLOC
225 /* In tool mode the e_bv buffer is realloc'd, leave it alone */
226 if( !(slapMode & SLAP_TOOL_MODE) ) {
227 free( e->e_bv.bv_val );
229 #endif /* !SLAP_ZONE_ALLOC */
231 #ifdef SLAP_ZONE_ALLOC
232 slap_zn_free( e, bdb->bi_cache.c_zctx );
240 int bdb_entry_release(
245 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
246 struct bdb_op_info *boi = NULL;
248 /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
249 SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
251 if ( slapMode == SLAP_SERVER_MODE ) {
252 /* If not in our cache, just free it */
253 if ( !e->e_private ) {
254 #ifdef SLAP_ZONE_ALLOC
255 return bdb_entry_return( bdb, e, -1 );
257 return bdb_entry_return( e );
260 /* free entry and reader or writer lock */
261 boi = (struct bdb_op_info *)op->o_private;
263 /* lock is freed with txn */
264 if ( !boi || boi->boi_txn ) {
265 bdb_unlocked_cache_return_entry_rw( &bdb->bi_cache, e, rw );
267 struct bdb_lock_info *bli, *prev;
268 for ( prev=(struct bdb_lock_info *)&boi->boi_locks,
269 bli = boi->boi_locks; bli; prev=bli, bli=bli->bli_next ) {
270 if ( bli->bli_id == e->e_id ) {
271 bdb_cache_return_entry_rw( bdb->bi_dbenv, &bdb->bi_cache,
272 e, rw, &bli->bli_lock );
273 prev->bli_next = bli->bli_next;
274 op->o_tmpfree( bli, op->o_tmpmemctx );
278 if ( !boi->boi_locks ) {
279 op->o_tmpfree( boi, op->o_tmpmemctx );
280 op->o_private = NULL;
284 #ifdef SLAP_ZONE_ALLOC
286 if (e->e_private != NULL) {
287 BEI(e)->bei_e = NULL;
288 zseq = BEI(e)->bei_zseq;
291 if (e->e_private != NULL)
292 BEI(e)->bei_e = NULL;
295 #ifdef SLAP_ZONE_ALLOC
296 bdb_entry_return ( bdb, e, zseq );
298 bdb_entry_return ( e );
305 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
311 AttributeDescription *at,
315 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
316 struct bdb_op_info *boi = NULL;
321 const char *at_name = at ? at->ad_cname.bv_val : "(null)";
323 u_int32_t locker = 0;
325 int free_lock_id = 0;
327 Debug( LDAP_DEBUG_ARGS,
328 "=> bdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 );
329 Debug( LDAP_DEBUG_ARGS,
330 "=> bdb_entry_get: oc: \"%s\", at: \"%s\"\n",
331 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
333 if( op ) boi = (struct bdb_op_info *) op->o_private;
334 if( boi != NULL && op->o_bd->be_private == boi->boi_bdb->be_private ) {
336 locker = boi->boi_locker;
340 locker = TXN_ID ( txn );
341 } else if ( !locker ) {
342 rc = LOCK_ID ( bdb->bi_dbenv, &locker );
353 /* can we find entry */
354 rc = bdb_dn2entry( op, txn, ndn, &ei, 0, locker, &lock );
359 case DB_LOCK_DEADLOCK:
360 case DB_LOCK_NOTGRANTED:
361 /* the txn must abort and retry */
366 ldap_pvt_thread_yield();
369 if ( boi ) boi->boi_err = rc;
370 if ( free_lock_id ) {
371 LOCK_ID_FREE( bdb->bi_dbenv, locker );
373 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
375 if (ei) e = ei->bei_e;
377 Debug( LDAP_DEBUG_ACL,
378 "=> bdb_entry_get: cannot find entry: \"%s\"\n",
380 if ( free_lock_id ) {
381 LOCK_ID_FREE( bdb->bi_dbenv, locker );
383 return LDAP_NO_SUCH_OBJECT;
386 Debug( LDAP_DEBUG_ACL,
387 "=> bdb_entry_get: found entry: \"%s\"\n",
390 /* find attribute values */
391 if( is_entry_alias( e ) ) {
392 Debug( LDAP_DEBUG_ACL,
393 "<= bdb_entry_get: entry is an alias\n", 0, 0, 0 );
394 rc = LDAP_ALIAS_PROBLEM;
398 if( is_entry_referral( e ) ) {
399 Debug( LDAP_DEBUG_ACL,
400 "<= bdb_entry_get: entry is a referral\n", 0, 0, 0 );
405 if ( oc && !is_entry_objectclass( e, oc, 0 )) {
406 Debug( LDAP_DEBUG_ACL,
407 "<= bdb_entry_get: failed to find objectClass %s\n",
408 oc->soc_cname.bv_val, 0, 0 );
409 rc = LDAP_NO_SUCH_ATTRIBUTE;
414 if( rc != LDAP_SUCCESS ) {
416 bdb_cache_return_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, rw, &lock);
419 if ( slapMode == SLAP_SERVER_MODE ) {
421 /* big drag. we need a place to store a read lock so we can
422 * release it later?? If we're in a txn, nothing is needed
423 * here because the locks will go away with the txn.
427 boi = op->o_tmpcalloc(1,sizeof(struct bdb_op_info),op->o_tmpmemctx);
428 boi->boi_bdb = op->o_bd;
431 if ( !boi->boi_txn ) {
432 struct bdb_lock_info *bli;
433 bli = op->o_tmpalloc( sizeof(struct bdb_lock_info),
435 bli->bli_next = boi->boi_locks;
436 bli->bli_id = e->e_id;
437 bli->bli_lock = lock;
438 boi->boi_locks = bli;
442 *ent = entry_dup( e );
443 bdb_cache_return_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, rw, &lock);
447 if ( free_lock_id ) {
448 LOCK_ID_FREE( bdb->bi_dbenv, locker );
451 Debug( LDAP_DEBUG_TRACE,
452 "bdb_entry_get: rc=%d\n",