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>
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 #ifdef SLAP_ZONE_ALLOC
125 rc = entry_decode(&bv, e, bdb->bi_cache.c_zctx);
127 rc = entry_decode(&bv, e);
133 /* only free on error. On success, the entry was
136 #ifndef SLAP_ZONE_ALLOC
140 #ifdef SLAP_ZONE_ALLOC
147 int bdb_id2entry_delete(
152 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
153 DB *db = bdb->bi_id2entry->bdi_db;
160 key.size = sizeof(ID);
161 BDB_ID2DISK( e->e_id, &nid );
163 /* delete from database */
164 rc = db->del( db, tid, &key, 0 );
169 #ifdef SLAP_ZONE_ALLOC
170 int bdb_entry_return(
171 struct bdb_info *bdb,
176 int bdb_entry_return(
181 #ifdef SLAP_ZONE_ALLOC
182 if (!slap_zn_validate(bdb->bi_cache.c_zctx, e, zseq)) {
186 /* Our entries are allocated in two blocks; the data comes from
187 * the db itself and the Entry structure and associated pointers
188 * are allocated in entry_decode. The db data pointer is saved
189 * in e_bv. Since the Entry structure is allocated as a single
190 * block, e_attrs is always a fixed offset from e. The exception
191 * is when an entry has been modified, in which case we also need
195 #ifdef LDAP_COMP_MATCH
196 comp_tree_free( e->e_attrs );
198 if( !e->e_bv.bv_val ) { /* Entry added by do_add */
202 if( (void *) e->e_attrs != (void *) (e+1)) {
203 attrs_free( e->e_attrs );
206 /* See if the DNs were changed by modrdn */
207 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
208 e->e_bv.bv_val + e->e_bv.bv_len ) {
209 ch_free(e->e_name.bv_val);
210 ch_free(e->e_nname.bv_val);
211 e->e_name.bv_val = NULL;
212 e->e_nname.bv_val = NULL;
214 #ifndef SLAP_ZONE_ALLOC
215 /* In tool mode the e_bv buffer is realloc'd, leave it alone */
216 if( !(slapMode & SLAP_TOOL_MODE) ) {
217 free( e->e_bv.bv_val );
219 #endif /* !SLAP_ZONE_ALLOC */
221 #ifdef SLAP_ZONE_ALLOC
222 slap_zn_free( e, bdb->bi_cache.c_zctx );
230 int bdb_entry_release(
235 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
236 struct bdb_op_info *boi = NULL;
238 /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
239 SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
241 if ( slapMode == SLAP_SERVER_MODE ) {
242 /* If not in our cache, just free it */
243 if ( !e->e_private ) {
244 #ifdef SLAP_ZONE_ALLOC
245 return bdb_entry_return( bdb, e, -1 );
247 return bdb_entry_return( e );
250 /* free entry and reader or writer lock */
252 boi = (struct bdb_op_info *)op->o_private;
254 /* lock is freed with txn */
255 if ( !boi || boi->boi_txn ) {
256 bdb_unlocked_cache_return_entry_rw( &bdb->bi_cache, e, rw );
258 struct bdb_lock_info *bli, *prev;
259 for ( prev=(struct bdb_lock_info *)&boi->boi_locks,
260 bli = boi->boi_locks; bli; prev=bli, bli=bli->bli_next ) {
261 if ( bli->bli_id == e->e_id ) {
262 bdb_cache_return_entry_rw( bdb->bi_dbenv, &bdb->bi_cache,
263 e, rw, &bli->bli_lock );
264 prev->bli_next = bli->bli_next;
265 op->o_tmpfree( bli, op->o_tmpmemctx );
269 if ( !boi->boi_locks ) {
270 op->o_tmpfree( boi, op->o_tmpmemctx );
271 op->o_private = NULL;
275 #ifdef SLAP_ZONE_ALLOC
277 if (e->e_private != NULL) {
278 BEI(e)->bei_e = NULL;
279 zseq = BEI(e)->bei_zseq;
282 if (e->e_private != NULL)
283 BEI(e)->bei_e = NULL;
286 #ifdef SLAP_ZONE_ALLOC
287 bdb_entry_return ( bdb, e, zseq );
289 bdb_entry_return ( e );
296 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
302 AttributeDescription *at,
306 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
307 struct bdb_op_info *boi = NULL;
312 const char *at_name = at ? at->ad_cname.bv_val : "(null)";
314 u_int32_t locker = 0;
316 int free_lock_id = 0;
318 Debug( LDAP_DEBUG_ARGS,
319 "=> bdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 );
320 Debug( LDAP_DEBUG_ARGS,
321 "=> bdb_entry_get: oc: \"%s\", at: \"%s\"\n",
322 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
324 if( op ) boi = (struct bdb_op_info *) op->o_private;
325 if( boi != NULL && op->o_bd->be_private == boi->boi_bdb->be_private ) {
327 locker = boi->boi_locker;
331 locker = TXN_ID ( txn );
332 } else if ( !locker ) {
333 rc = LOCK_ID ( bdb->bi_dbenv, &locker );
344 /* can we find entry */
345 rc = bdb_dn2entry( op, txn, ndn, &ei, 0, locker, &lock );
350 case DB_LOCK_DEADLOCK:
351 case DB_LOCK_NOTGRANTED:
352 /* the txn must abort and retry */
357 ldap_pvt_thread_yield();
360 if ( boi ) boi->boi_err = rc;
361 if ( free_lock_id ) {
362 LOCK_ID_FREE( bdb->bi_dbenv, locker );
364 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
366 if (ei) e = ei->bei_e;
368 Debug( LDAP_DEBUG_ACL,
369 "=> bdb_entry_get: cannot find entry: \"%s\"\n",
371 if ( free_lock_id ) {
372 LOCK_ID_FREE( bdb->bi_dbenv, locker );
374 return LDAP_NO_SUCH_OBJECT;
377 Debug( LDAP_DEBUG_ACL,
378 "=> bdb_entry_get: found entry: \"%s\"\n",
381 /* find attribute values */
382 if( is_entry_alias( e ) ) {
383 Debug( LDAP_DEBUG_ACL,
384 "<= bdb_entry_get: entry is an alias\n", 0, 0, 0 );
385 rc = LDAP_ALIAS_PROBLEM;
389 if( is_entry_referral( e ) ) {
390 Debug( LDAP_DEBUG_ACL,
391 "<= bdb_entry_get: entry is a referral\n", 0, 0, 0 );
396 if ( oc && !is_entry_objectclass( e, oc, 0 )) {
397 Debug( LDAP_DEBUG_ACL,
398 "<= bdb_entry_get: failed to find objectClass %s\n",
399 oc->soc_cname.bv_val, 0, 0 );
400 rc = LDAP_NO_SUCH_ATTRIBUTE;
405 if( rc != LDAP_SUCCESS ) {
407 bdb_cache_return_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, rw, &lock);
410 if ( slapMode == SLAP_SERVER_MODE ) {
412 /* big drag. we need a place to store a read lock so we can
413 * release it later?? If we're in a txn, nothing is needed
414 * here because the locks will go away with the txn.
418 boi = op->o_tmpcalloc(1,sizeof(struct bdb_op_info),op->o_tmpmemctx);
419 boi->boi_bdb = op->o_bd;
422 if ( !boi->boi_txn ) {
423 struct bdb_lock_info *bli;
424 bli = op->o_tmpalloc( sizeof(struct bdb_lock_info),
426 bli->bli_next = boi->boi_locks;
427 bli->bli_id = e->e_id;
428 bli->bli_lock = lock;
429 boi->boi_locks = bli;
433 *ent = entry_dup( e );
434 bdb_cache_return_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, rw, &lock);
438 if ( free_lock_id ) {
439 LOCK_ID_FREE( bdb->bi_dbenv, locker );
442 Debug( LDAP_DEBUG_TRACE,
443 "bdb_entry_get: rc=%d\n",