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-2008 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;
38 struct berval odn, ondn;
40 /* We only store rdns, and they go in the dn2id database. */
42 odn = e->e_name; ondn = e->e_nname;
44 e->e_name = slap_empty_bv;
45 e->e_nname = slap_empty_bv;
49 /* Store ID in BigEndian format */
51 key.size = sizeof(ID);
52 BDB_ID2DISK( e->e_id, &nid );
54 rc = entry_encode( e, &bv );
56 e->e_name = odn; e->e_nname = ondn;
58 if( rc != LDAP_SUCCESS ) {
65 rc = db->put( db, tid, &key, &data, flag );
72 * This routine adds (or updates) an entry on disk.
73 * The cache should be already be updated.
82 return bdb_id2entry_put(be, tid, e, DB_NOOVERWRITE);
85 int bdb_id2entry_update(
90 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;
112 key.size = sizeof(ID);
113 BDB_ID2DISK( id, &nid );
116 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
119 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
122 /* Get the nattrs / nvals counts first */
123 data.ulen = data.dlen = sizeof(buf);
125 rc = cursor->c_get( cursor, &key, &data, DB_SET );
126 if ( rc ) goto finish;
130 eh.bv.bv_len = data.size;
131 rc = entry_header( &eh );
132 if ( rc ) goto finish;
135 data.flags ^= DB_DBT_PARTIAL;
137 rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
138 if ( rc != DB_BUFFER_SMALL ) goto finish;
140 /* Allocate a block and retrieve the data */
141 off = eh.data - eh.bv.bv_val;
142 eh.bv.bv_len = eh.nvals * sizeof( struct berval ) + data.size;
143 eh.bv.bv_val = ch_malloc( eh.bv.bv_len );
144 eh.data = eh.bv.bv_val + eh.nvals * sizeof( struct berval );
146 data.ulen = data.size;
148 /* skip past already parsed nattr/nvals */
151 rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
154 cursor->c_close( cursor );
160 #ifdef SLAP_ZONE_ALLOC
161 rc = entry_decode(&eh, e, bdb->bi_cache.c_zctx);
163 rc = entry_decode(&eh, e);
169 /* only free on error. On success, the entry was
172 #ifndef SLAP_ZONE_ALLOC
173 ch_free(eh.bv.bv_val);
176 #ifdef SLAP_ZONE_ALLOC
177 ch_free(eh.bv.bv_val);
183 int bdb_id2entry_delete(
188 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
189 DB *db = bdb->bi_id2entry->bdi_db;
196 key.size = sizeof(ID);
197 BDB_ID2DISK( e->e_id, &nid );
199 /* delete from database */
200 rc = db->del( db, tid, &key, 0 );
205 int bdb_entry_return(
209 /* Our entries are allocated in two blocks; the data comes from
210 * the db itself and the Entry structure and associated pointers
211 * are allocated in entry_decode. The db data pointer is saved
214 if ( e->e_bv.bv_val ) {
215 /* See if the DNs were changed by modrdn */
216 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
217 e->e_bv.bv_val + e->e_bv.bv_len ) {
218 ch_free(e->e_name.bv_val);
219 ch_free(e->e_nname.bv_val);
221 e->e_name.bv_val = NULL;
222 e->e_nname.bv_val = NULL;
223 /* In tool mode the e_bv buffer is realloc'd, leave it alone */
224 if( !(slapMode & SLAP_TOOL_MODE) ) {
225 free( e->e_bv.bv_val );
227 BER_BVZERO( &e->e_bv );
233 int bdb_entry_release(
238 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
239 struct bdb_op_info *boi;
242 /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
243 SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
245 if ( slapMode == SLAP_SERVER_MODE ) {
246 /* If not in our cache, just free it */
247 if ( !e->e_private ) {
248 #ifdef SLAP_ZONE_ALLOC
249 return bdb_entry_return( bdb, e, -1 );
251 return bdb_entry_return( e );
254 /* free entry and reader or writer lock */
255 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
256 if ( oex->oe_key == bdb ) break;
258 boi = (struct bdb_op_info *)oex;
260 /* lock is freed with txn */
261 if ( !boi || boi->boi_txn ) {
262 bdb_unlocked_cache_return_entry_rw( bdb, e, rw );
264 struct bdb_lock_info *bli, *prev;
265 for ( prev=(struct bdb_lock_info *)&boi->boi_locks,
266 bli = boi->boi_locks; bli; prev=bli, bli=bli->bli_next ) {
267 if ( bli->bli_id == e->e_id ) {
268 bdb_cache_return_entry_rw( bdb, e, rw, &bli->bli_lock );
269 prev->bli_next = bli->bli_next;
270 op->o_tmpfree( bli, op->o_tmpmemctx );
274 if ( !boi->boi_locks ) {
275 LDAP_SLIST_REMOVE( &op->o_extra, &boi->boi_oe, OpExtra, oe_next );
276 op->o_tmpfree( boi, op->o_tmpmemctx );
280 #ifdef SLAP_ZONE_ALLOC
282 if (e->e_private != NULL) {
283 BEI(e)->bei_e = NULL;
284 zseq = BEI(e)->bei_zseq;
287 if (e->e_private != NULL)
288 BEI(e)->bei_e = NULL;
291 #ifdef SLAP_ZONE_ALLOC
292 bdb_entry_return ( bdb, e, zseq );
294 bdb_entry_return ( e );
301 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
307 AttributeDescription *at,
311 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
312 struct bdb_op_info *boi = NULL;
317 const char *at_name = at ? at->ad_cname.bv_val : "(null)";
321 Debug( LDAP_DEBUG_ARGS,
322 "=> bdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 );
323 Debug( LDAP_DEBUG_ARGS,
324 "=> bdb_entry_get: oc: \"%s\", at: \"%s\"\n",
325 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
329 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
330 if ( oex->oe_key == bdb ) break;
332 boi = (struct bdb_op_info *)oex;
338 rc = bdb_reader_get( op, bdb->bi_dbenv, &txn );
348 /* can we find entry */
349 rc = bdb_dn2entry( op, txn, ndn, &ei, 0, &lock );
354 case DB_LOCK_DEADLOCK:
355 case DB_LOCK_NOTGRANTED:
356 /* the txn must abort and retry */
361 ldap_pvt_thread_yield();
364 if ( boi ) boi->boi_err = rc;
365 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
367 if (ei) e = ei->bei_e;
369 Debug( LDAP_DEBUG_ACL,
370 "=> bdb_entry_get: cannot find entry: \"%s\"\n",
372 return LDAP_NO_SUCH_OBJECT;
375 Debug( LDAP_DEBUG_ACL,
376 "=> bdb_entry_get: found entry: \"%s\"\n",
379 if ( oc && !is_entry_objectclass( e, oc, 0 )) {
380 Debug( LDAP_DEBUG_ACL,
381 "<= bdb_entry_get: failed to find objectClass %s\n",
382 oc->soc_cname.bv_val, 0, 0 );
383 rc = LDAP_NO_SUCH_ATTRIBUTE;
387 /* NOTE: attr_find() or attrs_find()? */
388 if ( at && attr_find( e->e_attrs, at ) == NULL ) {
389 Debug( LDAP_DEBUG_ACL,
390 "<= bdb_entry_get: failed to find attribute %s\n",
391 at->ad_cname.bv_val, 0, 0 );
392 rc = LDAP_NO_SUCH_ATTRIBUTE;
397 if( rc != LDAP_SUCCESS ) {
399 bdb_cache_return_entry_rw(bdb, e, rw, &lock);
402 if ( slapMode == SLAP_SERVER_MODE ) {
404 /* big drag. we need a place to store a read lock so we can
405 * release it later?? If we're in a txn, nothing is needed
406 * here because the locks will go away with the txn.
410 boi = op->o_tmpcalloc(1,sizeof(struct bdb_op_info),op->o_tmpmemctx);
411 boi->boi_oe.oe_key = bdb;
412 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &boi->boi_oe, oe_next );
414 if ( !boi->boi_txn ) {
415 struct bdb_lock_info *bli;
416 bli = op->o_tmpalloc( sizeof(struct bdb_lock_info),
418 bli->bli_next = boi->boi_locks;
419 bli->bli_id = e->e_id;
420 bli->bli_lock = lock;
421 boi->boi_locks = bli;
425 *ent = entry_dup( e );
426 bdb_cache_return_entry_rw(bdb, e, rw, &lock);
430 Debug( LDAP_DEBUG_TRACE,
431 "bdb_entry_get: rc=%d\n",