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-2011 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 mdb_id2entry_put(
31 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
32 MDB_dbi dbi = mdb->mi_id2entry->mdi_dbi;
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;
45 key.mv_data = &e->e_id;
46 key.mv_size = sizeof(ID);
48 rc = entry_encode( e, &bv );
49 e->e_name = odn; e->e_nname = ondn;
50 if( rc != LDAP_SUCCESS ) {
54 data.mv_size = bv.bv_len;
55 data.mv_data = bv.bv_val;
57 rc = mdb_put( tid, dbi, &key, &data, flag );
59 op->o_tmpfree( op->o_tmpmemctx, bv.bv_val );
64 * This routine adds (or updates) an entry on disk.
65 * The cache should be already be updated.
74 return mdb_id2entry_put(op, tid, e, MDB_NOOVERWRITE);
77 int mdb_id2entry_update(
82 return mdb_id2entry_put(op, tid, e, 0);
91 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
92 MDB_dbi dbi = mdb->mi_id2entry->mdi_dbi;
101 key.mv_size = sizeof(ID);
104 rc = mdb_get( tid, dbi, &key, &data );
107 eh.bv.bv_val = data.mv_data;
108 eh.bv.bv_len = data.mv_size;
109 rc = entry_header( &eh );
112 rc = entry_decode(&eh, e);
121 int mdb_id2entry_delete(
126 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
127 MDB_dbi dbi = mdb->mi_id2entry->mdi_dbi;
131 key.mv_data = &e->e_id;
132 key.mv_size = sizeof(ID);
134 /* delete from database */
135 rc = mdb_del( tid, dbi, &key, NULL, 0 );
140 int mdb_entry_return(
144 /* Our entries are allocated in two blocks; the data comes from
145 * the db itself and the Entry structure and associated pointers
146 * are allocated in entry_decode. The db data pointer is saved
149 if ( e->e_bv.bv_val ) {
150 /* See if the DNs were changed by modrdn */
151 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
152 e->e_bv.bv_val + e->e_bv.bv_len ) {
153 ch_free(e->e_name.bv_val);
154 ch_free(e->e_nname.bv_val);
156 e->e_name.bv_val = NULL;
157 e->e_nname.bv_val = NULL;
158 /* In tool mode the e_bv buffer is realloc'd, leave it alone */
159 if( !(slapMode & SLAP_TOOL_MODE) ) {
160 free( e->e_bv.bv_val );
162 BER_BVZERO( &e->e_bv );
168 int mdb_entry_release(
174 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
175 struct mdb_op_info *moi;
178 /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
179 SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
181 if ( slapMode == SLAP_SERVER_MODE ) {
182 /* If not in our cache, just free it */
183 if ( !e->e_private ) {
184 return mdb_entry_return( e );
186 /* free entry and reader or writer lock */
187 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
188 if ( oex->oe_key == mdb ) break;
190 moi = (struct mdb_op_info *)oex;
192 /* lock is freed with txn */
193 if ( !moi || moi->moi_txn ) {
194 mdb_unlocked_cache_return_entry_rw( mdb, e, rw );
196 struct mdb_lock_info *bli, *prev;
197 for ( prev=(struct mdb_lock_info *)&moi->boi_locks,
198 bli = boi->boi_locks; bli; prev=bli, bli=bli->bli_next ) {
199 if ( bli->bli_id == e->e_id ) {
200 mdb_cache_return_entry_rw( mdb, e, rw, &bli->bli_lock );
201 prev->bli_next = bli->bli_next;
202 /* Cleanup, or let caller know we unlocked */
203 if ( bli->bli_flag & BLI_DONTFREE )
206 op->o_tmpfree( bli, op->o_tmpmemctx );
210 if ( !boi->boi_locks ) {
211 LDAP_SLIST_REMOVE( &op->o_extra, &boi->boi_oe, OpExtra, oe_next );
212 if ( !(boi->boi_flag & BOI_DONTFREE))
213 op->o_tmpfree( boi, op->o_tmpmemctx );
217 if (e->e_private != NULL)
218 BEI(e)->bei_e = NULL;
220 mdb_entry_return ( e );
225 return mdb_entry_return( e );
229 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
235 AttributeDescription *at,
240 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
241 struct mdb_op_info *boi = NULL;
246 const char *at_name = at ? at->ad_cname.bv_val : "(null)";
248 Debug( LDAP_DEBUG_ARGS,
249 "=> mdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 );
250 Debug( LDAP_DEBUG_ARGS,
251 "=> mdb_entry_get: oc: \"%s\", at: \"%s\"\n",
252 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
256 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
257 if ( oex->oe_key == mdb ) break;
259 boi = (struct mdb_op_info *)oex;
265 rc = mdb_reader_get( op, mdb->bi_dbenv, &txn );
275 /* can we find entry */
276 rc = mdb_dn2entry( op, txn, ndn, &ei, 0, &lock );
282 if ( boi ) boi->boi_err = rc;
283 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
285 if (ei) e = ei->bei_e;
287 Debug( LDAP_DEBUG_ACL,
288 "=> mdb_entry_get: cannot find entry: \"%s\"\n",
290 return LDAP_NO_SUCH_OBJECT;
293 Debug( LDAP_DEBUG_ACL,
294 "=> mdb_entry_get: found entry: \"%s\"\n",
297 if ( oc && !is_entry_objectclass( e, oc, 0 )) {
298 Debug( LDAP_DEBUG_ACL,
299 "<= mdb_entry_get: failed to find objectClass %s\n",
300 oc->soc_cname.bv_val, 0, 0 );
301 rc = LDAP_NO_SUCH_ATTRIBUTE;
305 /* NOTE: attr_find() or attrs_find()? */
306 if ( at && attr_find( e->e_attrs, at ) == NULL ) {
307 Debug( LDAP_DEBUG_ACL,
308 "<= mdb_entry_get: failed to find attribute %s\n",
309 at->ad_cname.bv_val, 0, 0 );
310 rc = LDAP_NO_SUCH_ATTRIBUTE;
315 if( rc != LDAP_SUCCESS ) {
317 mdb_cache_return_entry_rw(mdb, e, rw, &lock);
320 if ( slapMode == SLAP_SERVER_MODE ) {
322 /* big drag. we need a place to store a read lock so we can
323 * release it later?? If we're in a txn, nothing is needed
324 * here because the locks will go away with the txn.
328 boi = op->o_tmpcalloc(1,sizeof(struct mdb_op_info),op->o_tmpmemctx);
329 boi->boi_oe.oe_key = mdb;
330 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &boi->boi_oe, oe_next );
332 if ( !boi->boi_txn ) {
333 struct mdb_lock_info *bli;
334 bli = op->o_tmpalloc( sizeof(struct mdb_lock_info),
336 bli->bli_next = boi->boi_locks;
337 bli->bli_id = e->e_id;
339 bli->bli_lock = lock;
340 boi->boi_locks = bli;
344 *ent = entry_dup( e );
345 mdb_cache_return_entry_rw(mdb, e, rw, &lock);
349 Debug( LDAP_DEBUG_TRACE,
350 "mdb_entry_get: rc=%d\n",