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>
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;
39 struct berval odn, ondn;
41 /* We only store rdns, and they go in the dn2id database. */
43 odn = e->e_name; ondn = e->e_nname;
45 e->e_name = slap_empty_bv;
46 e->e_nname = slap_empty_bv;
50 key.size = sizeof(ID);
52 /* Set key in BigEndian order */
54 for ( i=sizeof(ID)-1; i>=0; i-- ) {
59 rc = entry_encode( e, &bv );
61 e->e_name = odn; e->e_nname = ondn;
63 if( rc != LDAP_SUCCESS ) {
70 rc = db->put( db, tid, &key, &data, flag );
77 * This routine adds (or updates) an entry on disk.
78 * The cache should be already be updated.
87 return bdb_id2entry_put(be, tid, e, DB_NOOVERWRITE);
90 int bdb_id2entry_update(
95 return bdb_id2entry_put(be, tid, e, 0);
104 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
105 DB *db = bdb->bi_id2entry->bdi_db;
108 char buf[sizeof(ID)];
110 int i, rc = 0, ret = 0;
116 key.size = sizeof(ID);
118 for ( i=sizeof(ID)-1; i>=0; i-- ) {
124 data.flags = DB_DBT_MALLOC;
127 rc = db->get( db, tid, &key, &data, bdb->bi_db_opflags );
133 DBT2bv( &data, &bv );
135 rc = entry_decode( &bv, e );
140 /* only free on error. On success, the entry was
143 ch_free( data.data );
149 int bdb_id2entry_delete(
154 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
155 DB *db = bdb->bi_id2entry->bdi_db;
157 char buf[sizeof(ID)];
163 key.size = sizeof(ID);
165 for ( i=sizeof(ID)-1; i>=0; i-- ) {
170 /* delete from database */
171 rc = db->del( db, tid, &key, 0 );
176 int bdb_entry_return(
179 /* Our entries are allocated in two blocks; the data comes from
180 * the db itself and the Entry structure and associated pointers
181 * are allocated in entry_decode. The db data pointer is saved
182 * in e_bv. Since the Entry structure is allocated as a single
183 * block, e_attrs is always a fixed offset from e. The exception
184 * is when an entry has been modified, in which case we also need
187 if( !e->e_bv.bv_val ) { /* A regular entry, from do_add */
191 if( (void *) e->e_attrs != (void *) (e+1)) {
192 attrs_free( e->e_attrs );
195 /* See if the DNs were changed by modrdn */
196 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
197 e->e_bv.bv_val + e->e_bv.bv_len ) {
198 ch_free(e->e_name.bv_val);
199 ch_free(e->e_nname.bv_val);
200 e->e_name.bv_val = NULL;
201 e->e_nname.bv_val = NULL;
204 /* In tool mode the e_bv buffer is realloc'd, leave it alone */
205 if( !(slapMode & SLAP_TOOL_MODE) ) {
206 free( e->e_bv.bv_val );
209 free( e->e_bv.bv_val );
216 int bdb_entry_release(
221 struct bdb_info *bdb = (struct bdb_info *) o->o_bd->be_private;
222 struct bdb_op_info *boi = NULL;
224 /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
225 SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
227 if ( slapMode == SLAP_SERVER_MODE ) {
228 /* free entry and reader or writer lock */
230 boi = (struct bdb_op_info *)o->o_private;
232 /* lock is freed with txn */
233 if ( !boi || boi->boi_txn ) {
234 bdb_unlocked_cache_return_entry_rw( &bdb->bi_cache, e, rw );
236 bdb_cache_return_entry_rw( bdb->bi_dbenv, &bdb->bi_cache, e, rw, &boi->boi_lock );
237 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;
273 LDAP_LOG( BACK_BDB, ARGS,
274 "bdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 );
275 LDAP_LOG( BACK_BDB, ARGS,
276 "bdb_entry_get: oc: \"%s\", at: \"%s\"\n",
277 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
279 Debug( LDAP_DEBUG_ARGS,
280 "=> bdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 );
281 Debug( LDAP_DEBUG_ARGS,
282 "=> bdb_entry_get: oc: \"%s\", at: \"%s\"\n",
283 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
286 if( op ) boi = (struct bdb_op_info *) op->o_private;
287 if( boi != NULL && op->o_bd == boi->boi_bdb ) {
289 locker = boi->boi_locker;
293 locker = TXN_ID ( txn );
294 } else if ( !locker ) {
295 rc = LOCK_ID ( bdb->bi_dbenv, &locker );
306 /* can we find entry */
307 rc = bdb_dn2entry( op, txn, ndn, &ei, 0, locker, &lock );
312 case DB_LOCK_DEADLOCK:
313 case DB_LOCK_NOTGRANTED:
314 /* the txn must abort and retry */
319 ldap_pvt_thread_yield();
322 if ( boi ) boi->boi_err = rc;
323 if ( free_lock_id ) {
324 LOCK_ID_FREE( bdb->bi_dbenv, locker );
326 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
328 if (ei) e = ei->bei_e;
331 LDAP_LOG( BACK_BDB, INFO,
332 "bdb_entry_get: cannot find entry (%s)\n",
335 Debug( LDAP_DEBUG_ACL,
336 "=> bdb_entry_get: cannot find entry: \"%s\"\n",
339 if ( free_lock_id ) {
340 LOCK_ID_FREE( bdb->bi_dbenv, locker );
342 return LDAP_NO_SUCH_OBJECT;
346 LDAP_LOG( BACK_BDB, DETAIL1, "bdb_entry_get: found entry (%s)\n",
349 Debug( LDAP_DEBUG_ACL,
350 "=> bdb_entry_get: found entry: \"%s\"\n",
355 /* find attribute values */
356 if( is_entry_alias( e ) ) {
358 LDAP_LOG( BACK_BDB, INFO,
359 "bdb_entry_get: entry (%s) is an alias\n", e->e_name.bv_val, 0, 0 );
361 Debug( LDAP_DEBUG_ACL,
362 "<= bdb_entry_get: entry is an alias\n", 0, 0, 0 );
364 rc = LDAP_ALIAS_PROBLEM;
369 if( is_entry_referral( e ) ) {
371 LDAP_LOG( BACK_BDB, INFO,
372 "bdb_entry_get: entry (%s) is a referral.\n", e->e_name.bv_val, 0, 0);
374 Debug( LDAP_DEBUG_ACL,
375 "<= bdb_entry_get: entry is a referral\n", 0, 0, 0 );
381 if ( oc && !is_entry_objectclass( e, oc, 0 )) {
383 LDAP_LOG( BACK_BDB, INFO,
384 "bdb_entry_get: failed to find objectClass.\n", 0, 0, 0 );
386 Debug( LDAP_DEBUG_ACL,
387 "<= bdb_entry_get: failed to find objectClass\n",
390 rc = LDAP_NO_SUCH_ATTRIBUTE;
395 if( rc != LDAP_SUCCESS ) {
397 bdb_cache_return_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, rw, &lock);
400 /* big drag. we need a place to store a read lock so we can
404 boi = op->o_tmpcalloc(1,sizeof(struct bdb_op_info),op->o_tmpmemctx);
405 boi->boi_lock = lock;
410 if ( free_lock_id ) {
411 LOCK_ID_FREE( bdb->bi_dbenv, locker );
415 LDAP_LOG( BACK_BDB, ENTRY, "bdb_entry_get: rc=%d\n", rc, 0, 0 );
417 Debug( LDAP_DEBUG_TRACE,
418 "bdb_entry_get: rc=%d\n",