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 = mdb_entry_encode( op, 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 rc = mdb_entry_decode(&eh, e);
116 int mdb_id2entry_delete(
121 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
122 MDB_dbi dbi = mdb->mi_id2entry->mdi_dbi;
126 key.mv_data = &e->e_id;
127 key.mv_size = sizeof(ID);
129 /* delete from database */
130 rc = mdb_del( tid, dbi, &key, NULL, 0 );
135 int mdb_entry_return(
139 /* Our entries are allocated in two blocks; the data comes from
140 * the db itself and the Entry structure and associated pointers
141 * are allocated in entry_decode. The db data pointer is saved
144 if ( e->e_bv.bv_val ) {
145 /* See if the DNs were changed by modrdn */
146 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
147 e->e_bv.bv_val + e->e_bv.bv_len ) {
148 ch_free(e->e_name.bv_val);
149 ch_free(e->e_nname.bv_val);
151 e->e_name.bv_val = NULL;
152 e->e_nname.bv_val = NULL;
153 /* In tool mode the e_bv buffer is realloc'd, leave it alone */
154 if( !(slapMode & SLAP_TOOL_MODE) ) {
155 free( e->e_bv.bv_val );
157 BER_BVZERO( &e->e_bv );
163 int mdb_entry_release(
168 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
169 struct mdb_op_info *moi;
172 /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
173 SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
175 if ( slapMode == SLAP_SERVER_MODE ) {
176 /* If not in our cache, just free it */
177 if ( !e->e_private ) {
178 return mdb_entry_return( e );
180 /* free entry and reader or writer lock */
181 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
182 if ( oex->oe_key == mdb ) break;
184 moi = (struct mdb_op_info *)oex;
186 /* lock is freed with txn */
187 if ( !moi || moi->moi_txn ) {
188 mdb_unlocked_cache_return_entry_rw( mdb, e, rw );
190 struct mdb_lock_info *bli, *prev;
191 for ( prev=(struct mdb_lock_info *)&moi->boi_locks,
192 bli = boi->boi_locks; bli; prev=bli, bli=bli->bli_next ) {
193 if ( bli->bli_id == e->e_id ) {
194 mdb_cache_return_entry_rw( mdb, e, rw, &bli->bli_lock );
195 prev->bli_next = bli->bli_next;
196 /* Cleanup, or let caller know we unlocked */
197 if ( bli->bli_flag & BLI_DONTFREE )
200 op->o_tmpfree( bli, op->o_tmpmemctx );
204 if ( !boi->boi_locks ) {
205 LDAP_SLIST_REMOVE( &op->o_extra, &boi->boi_oe, OpExtra, oe_next );
206 if ( !(boi->boi_flag & BOI_DONTFREE))
207 op->o_tmpfree( boi, op->o_tmpmemctx );
211 if (e->e_private != NULL)
212 BEI(e)->bei_e = NULL;
214 mdb_entry_return ( e );
220 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
226 AttributeDescription *at,
230 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
231 struct mdb_op_info *boi = NULL;
236 const char *at_name = at ? at->ad_cname.bv_val : "(null)";
240 Debug( LDAP_DEBUG_ARGS,
241 "=> mdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 );
242 Debug( LDAP_DEBUG_ARGS,
243 "=> mdb_entry_get: oc: \"%s\", at: \"%s\"\n",
244 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
248 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
249 if ( oex->oe_key == mdb ) break;
251 boi = (struct mdb_op_info *)oex;
257 rc = mdb_reader_get( op, mdb->bi_dbenv, &txn );
267 /* can we find entry */
268 rc = mdb_dn2entry( op, txn, ndn, &ei, 0, &lock );
274 if ( boi ) boi->boi_err = rc;
275 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
277 if (ei) e = ei->bei_e;
279 Debug( LDAP_DEBUG_ACL,
280 "=> mdb_entry_get: cannot find entry: \"%s\"\n",
282 return LDAP_NO_SUCH_OBJECT;
285 Debug( LDAP_DEBUG_ACL,
286 "=> mdb_entry_get: found entry: \"%s\"\n",
289 if ( oc && !is_entry_objectclass( e, oc, 0 )) {
290 Debug( LDAP_DEBUG_ACL,
291 "<= mdb_entry_get: failed to find objectClass %s\n",
292 oc->soc_cname.bv_val, 0, 0 );
293 rc = LDAP_NO_SUCH_ATTRIBUTE;
297 /* NOTE: attr_find() or attrs_find()? */
298 if ( at && attr_find( e->e_attrs, at ) == NULL ) {
299 Debug( LDAP_DEBUG_ACL,
300 "<= mdb_entry_get: failed to find attribute %s\n",
301 at->ad_cname.bv_val, 0, 0 );
302 rc = LDAP_NO_SUCH_ATTRIBUTE;
307 if( rc != LDAP_SUCCESS ) {
309 mdb_cache_return_entry_rw(mdb, e, rw, &lock);
312 if ( slapMode == SLAP_SERVER_MODE ) {
314 /* big drag. we need a place to store a read lock so we can
315 * release it later?? If we're in a txn, nothing is needed
316 * here because the locks will go away with the txn.
320 boi = op->o_tmpcalloc(1,sizeof(struct mdb_op_info),op->o_tmpmemctx);
321 boi->boi_oe.oe_key = mdb;
322 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &boi->boi_oe, oe_next );
324 if ( !boi->boi_txn ) {
325 struct mdb_lock_info *bli;
326 bli = op->o_tmpalloc( sizeof(struct mdb_lock_info),
328 bli->bli_next = boi->boi_locks;
329 bli->bli_id = e->e_id;
331 bli->bli_lock = lock;
332 boi->boi_locks = bli;
336 *ent = entry_dup( e );
337 mdb_cache_return_entry_rw(mdb, e, rw, &lock);
341 Debug( LDAP_DEBUG_TRACE,
342 "mdb_entry_get: rc=%d\n",