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;
36 struct berval odn, ondn;
38 /* We only store rdns, and they go in the dn2id database. */
40 odn = e->e_name; ondn = e->e_nname;
42 e->e_name = slap_empty_bv;
43 e->e_nname = slap_empty_bv;
46 key.data = (char *) &e->e_id;
47 key.size = sizeof(ID);
49 rc = entry_encode( e, &bv );
51 e->e_name = odn; e->e_nname = ondn;
53 if( rc != LDAP_SUCCESS ) {
60 rc = db->put( db, tid, &key, &data, flag );
67 * This routine adds (or updates) an entry on disk.
68 * The cache should be already be updated.
77 return bdb_id2entry_put(be, tid, e, DB_NOOVERWRITE);
80 int bdb_id2entry_update(
85 return bdb_id2entry_put(be, tid, e, 0);
94 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
95 DB *db = bdb->bi_id2entry->bdi_db;
103 key.data = (char *) &id;
104 key.size = sizeof(ID);
107 data.flags = DB_DBT_MALLOC;
110 rc = db->get( db, tid, &key, &data, bdb->bi_db_opflags );
116 DBT2bv( &data, &bv );
118 rc = entry_decode( &bv, e );
123 /* only free on error. On success, the entry was
126 ch_free( data.data );
132 int bdb_id2entry_delete(
137 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
138 DB *db = bdb->bi_id2entry->bdi_db;
143 key.data = (char *) &e->e_id;
144 key.size = sizeof(ID);
146 /* delete from database */
147 rc = db->del( db, tid, &key, 0 );
152 int bdb_entry_return(
155 /* Our entries are allocated in two blocks; the data comes from
156 * the db itself and the Entry structure and associated pointers
157 * are allocated in entry_decode. The db data pointer is saved
158 * in e_bv. Since the Entry structure is allocated as a single
159 * block, e_attrs is always a fixed offset from e. The exception
160 * is when an entry has been modified, in which case we also need
163 if( !e->e_bv.bv_val ) { /* A regular entry, from do_add */
167 if( (void *) e->e_attrs != (void *) (e+1)) {
168 attrs_free( e->e_attrs );
171 /* See if the DNs were changed by modrdn */
172 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
173 e->e_bv.bv_val + e->e_bv.bv_len ) {
174 ch_free(e->e_name.bv_val);
175 ch_free(e->e_nname.bv_val);
176 e->e_name.bv_val = NULL;
177 e->e_nname.bv_val = NULL;
180 /* In tool mode the e_bv buffer is realloc'd, leave it alone */
181 if( !(slapMode & SLAP_TOOL_MODE) ) {
182 free( e->e_bv.bv_val );
185 free( e->e_bv.bv_val );
192 int bdb_entry_release(
197 struct bdb_info *bdb = (struct bdb_info *) o->o_bd->be_private;
198 struct bdb_op_info *boi = NULL;
200 /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
201 SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
203 if ( slapMode == SLAP_SERVER_MODE ) {
204 /* If not in our cache, just free it */
205 if ( !e->e_private ) {
206 return bdb_entry_return( e );
208 /* free entry and reader or writer lock */
210 boi = (struct bdb_op_info *)o->o_private;
212 /* lock is freed with txn */
213 if ( !boi || boi->boi_txn ) {
214 bdb_unlocked_cache_return_entry_rw( &bdb->bi_cache, e, rw );
216 bdb_cache_return_entry_rw( bdb->bi_dbenv, &bdb->bi_cache, e, rw, &boi->boi_lock );
217 o->o_tmpfree( boi, o->o_tmpmemctx );
221 if (e->e_private != NULL)
222 BEI(e)->bei_e = NULL;
224 bdb_entry_return ( e );
230 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
236 AttributeDescription *at,
240 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
241 struct bdb_op_info *boi = NULL;
246 const char *at_name = at ? at->ad_cname.bv_val : "(null)";
248 u_int32_t locker = 0;
250 int free_lock_id = 0;
252 Debug( LDAP_DEBUG_ARGS,
253 "=> bdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 );
254 Debug( LDAP_DEBUG_ARGS,
255 "=> bdb_entry_get: oc: \"%s\", at: \"%s\"\n",
256 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
258 if( op ) boi = (struct bdb_op_info *) op->o_private;
259 if( boi != NULL && op->o_bd->be_private == boi->boi_bdb->be_private ) {
261 locker = boi->boi_locker;
265 locker = TXN_ID ( txn );
266 } else if ( !locker ) {
267 rc = LOCK_ID ( bdb->bi_dbenv, &locker );
278 /* can we find entry */
279 rc = bdb_dn2entry( op, txn, ndn, &ei, 0, locker, &lock );
284 case DB_LOCK_DEADLOCK:
285 case DB_LOCK_NOTGRANTED:
286 /* the txn must abort and retry */
291 ldap_pvt_thread_yield();
294 if ( boi ) boi->boi_err = rc;
295 if ( free_lock_id ) {
296 LOCK_ID_FREE( bdb->bi_dbenv, locker );
298 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
300 if (ei) e = ei->bei_e;
302 Debug( LDAP_DEBUG_ACL,
303 "=> bdb_entry_get: cannot find entry: \"%s\"\n",
305 if ( free_lock_id ) {
306 LOCK_ID_FREE( bdb->bi_dbenv, locker );
308 return LDAP_NO_SUCH_OBJECT;
311 Debug( LDAP_DEBUG_ACL,
312 "=> bdb_entry_get: found entry: \"%s\"\n",
315 /* find attribute values */
316 if( is_entry_alias( e ) ) {
317 Debug( LDAP_DEBUG_ACL,
318 "<= bdb_entry_get: entry is an alias\n", 0, 0, 0 );
319 rc = LDAP_ALIAS_PROBLEM;
323 if( is_entry_referral( e ) ) {
324 Debug( LDAP_DEBUG_ACL,
325 "<= bdb_entry_get: entry is a referral\n", 0, 0, 0 );
330 if ( oc && !is_entry_objectclass( e, oc, 0 )) {
331 Debug( LDAP_DEBUG_ACL,
332 "<= bdb_entry_get: failed to find objectClass\n",
334 rc = LDAP_NO_SUCH_ATTRIBUTE;
339 if( rc != LDAP_SUCCESS ) {
341 bdb_cache_return_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, rw, &lock);
344 if ( slapMode == SLAP_SERVER_MODE ) {
346 /* big drag. we need a place to store a read lock so we can
350 boi = op->o_tmpcalloc(1,sizeof(struct bdb_op_info),op->o_tmpmemctx);
351 boi->boi_lock = lock;
352 boi->boi_bdb = op->o_bd;
357 *ent = entry_dup( e );
358 bdb_cache_return_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, rw, &lock);
362 if ( free_lock_id ) {
363 LOCK_ID_FREE( bdb->bi_dbenv, locker );
366 Debug( LDAP_DEBUG_TRACE,
367 "bdb_entry_get: rc=%d\n",