]> git.sur5r.net Git - openldap/blob - servers/slapd/back-mdb/id2entry.c
Checkpoint
[openldap] / servers / slapd / back-mdb / id2entry.c
1 /* id2entry.c - routines to deal with the id2entry database */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2011 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21 #include <ac/errno.h>
22
23 #include "back-mdb.h"
24
25 static int mdb_id2entry_put(
26         Operation *op,
27         MDB_txn *tid,
28         Entry *e,
29         int flag )
30 {
31         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
32         MDB_dbi dbi = mdb->mi_id2entry->mdi_dbi;
33         MDB_val key, data;
34         struct berval bv;
35         int rc;
36         struct berval odn, ondn;
37
38         /* We only store rdns, and they go in the dn2id database. */
39
40         odn = e->e_name; ondn = e->e_nname;
41
42         e->e_name = slap_empty_bv;
43         e->e_nname = slap_empty_bv;
44
45         key.mv_data = &e->e_id;
46         key.mv_size = sizeof(ID);
47
48         rc = mdb_entry_encode( op, e, &bv );
49         e->e_name = odn; e->e_nname = ondn;
50         if( rc != LDAP_SUCCESS ) {
51                 return -1;
52         }
53
54         data.mv_size = bv.bv_len;
55         data.mv_data = bv.bv_val;
56
57         rc = mdb_put( tid, dbi, &key, &data, flag );
58
59         op->o_tmpfree( op->o_tmpmemctx, bv.bv_val );
60         return rc;
61 }
62
63 /*
64  * This routine adds (or updates) an entry on disk.
65  * The cache should be already be updated.
66  */
67
68
69 int mdb_id2entry_add(
70         Operation *op,
71         MDB_txn *tid,
72         Entry *e )
73 {
74         return mdb_id2entry_put(op, tid, e, MDB_NOOVERWRITE);
75 }
76
77 int mdb_id2entry_update(
78         Operation *op,
79         MDB_txn *tid,
80         Entry *e )
81 {
82         return mdb_id2entry_put(op, tid, e, 0);
83 }
84
85 int mdb_id2entry(
86         Operation *op,
87         MDB_txn *tid,
88         ID id,
89         Entry **e )
90 {
91         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
92         MDB_dbi dbi = mdb->mi_id2entry->mdi_dbi;
93         MDB_val key, data;
94         EntryHeader eh;
95         char buf[16];
96         int rc = 0, off;
97
98         *e = NULL;
99
100         key.mv_data = &id;
101         key.mv_size = sizeof(ID);
102
103         /* fetch it */
104         rc = mdb_get( tid, dbi, &key, &data );
105         if ( rc ) return rc;
106
107         rc = mdb_entry_decode(&eh, e);
108
109         if( rc == 0 ) {
110                 (*e)->e_id = id;
111         }
112
113         return rc;
114 }
115
116 int mdb_id2entry_delete(
117         BackendDB *be,
118         MDB_txn *tid,
119         Entry *e )
120 {
121         struct mdb_info *mdb = (struct mdb_info *) be->be_private;
122         MDB_dbi dbi = mdb->mi_id2entry->mdi_dbi;
123         MDB_val key;
124         int rc;
125
126         key.mv_data = &e->e_id;
127         key.mv_size = sizeof(ID);
128
129         /* delete from database */
130         rc = mdb_del( tid, dbi, &key, NULL, 0 );
131
132         return rc;
133 }
134
135 int mdb_entry_return(
136         Entry *e
137 )
138 {
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
142          * in e_bv.
143          */
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);
150                 }
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 );
156                 }
157                 BER_BVZERO( &e->e_bv );
158         }
159         entry_free( e );
160         return 0;
161 }
162
163 int mdb_entry_release(
164         Operation *op,
165         Entry *e,
166         int rw )
167 {
168         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
169         struct mdb_op_info *moi;
170         OpExtra *oex;
171  
172         /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
173                         SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
174  
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 );
179                 }
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;
183                 }
184                 moi = (struct mdb_op_info *)oex;
185
186                 /* lock is freed with txn */
187                 if ( !moi || moi->moi_txn ) {
188                         mdb_unlocked_cache_return_entry_rw( mdb, e, rw );
189                 } else {
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 )
198                                                 bli->bli_flag = 0;
199                                         else
200                                                 op->o_tmpfree( bli, op->o_tmpmemctx );
201                                         break;
202                                 }
203                         }
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 );
208                         }
209                 }
210         } else {
211                 if (e->e_private != NULL)
212                         BEI(e)->bei_e = NULL;
213                 e->e_private = NULL;
214                 mdb_entry_return ( e );
215         }
216  
217         return 0;
218 }
219
220 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
221  */
222 int mdb_entry_get(
223         Operation *op,
224         struct berval *ndn,
225         ObjectClass *oc,
226         AttributeDescription *at,
227         int rw,
228         Entry **ent )
229 {
230         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
231         struct mdb_op_info *boi = NULL;
232         DB_TXN *txn = NULL;
233         Entry *e = NULL;
234         EntryInfo *ei;
235         int     rc;
236         const char *at_name = at ? at->ad_cname.bv_val : "(null)";
237
238         DB_LOCK         lock;
239
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);
245
246         if( op ) {
247                 OpExtra *oex;
248                 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
249                         if ( oex->oe_key == mdb ) break;
250                 }
251                 boi = (struct mdb_op_info *)oex;
252                 if ( boi )
253                         txn = boi->boi_txn;
254         }
255
256         if ( !txn ) {
257                 rc = mdb_reader_get( op, mdb->bi_dbenv, &txn );
258                 switch(rc) {
259                 case 0:
260                         break;
261                 default:
262                         return LDAP_OTHER;
263                 }
264         }
265
266 dn2entry_retry:
267         /* can we find entry */
268         rc = mdb_dn2entry( op, txn, ndn, &ei, 0, &lock );
269         switch( rc ) {
270         case MDB_NOTFOUND:
271         case 0:
272                 break;
273         default:
274                 if ( boi ) boi->boi_err = rc;
275                 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
276         }
277         if (ei) e = ei->bei_e;
278         if (e == NULL) {
279                 Debug( LDAP_DEBUG_ACL,
280                         "=> mdb_entry_get: cannot find entry: \"%s\"\n",
281                                 ndn->bv_val, 0, 0 ); 
282                 return LDAP_NO_SUCH_OBJECT; 
283         }
284         
285         Debug( LDAP_DEBUG_ACL,
286                 "=> mdb_entry_get: found entry: \"%s\"\n",
287                 ndn->bv_val, 0, 0 ); 
288
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;
294                 goto return_results;
295         }
296
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;
303                 goto return_results;
304         }
305
306 return_results:
307         if( rc != LDAP_SUCCESS ) {
308                 /* free entry */
309                 mdb_cache_return_entry_rw(mdb, e, rw, &lock);
310
311         } else {
312                 if ( slapMode == SLAP_SERVER_MODE ) {
313                         *ent = e;
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.
317                          */
318                         if ( op ) {
319                                 if ( !boi ) {
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 );
323                                 }
324                                 if ( !boi->boi_txn ) {
325                                         struct mdb_lock_info *bli;
326                                         bli = op->o_tmpalloc( sizeof(struct mdb_lock_info),
327                                                 op->o_tmpmemctx );
328                                         bli->bli_next = boi->boi_locks;
329                                         bli->bli_id = e->e_id;
330                                         bli->bli_flag = 0;
331                                         bli->bli_lock = lock;
332                                         boi->boi_locks = bli;
333                                 }
334                         }
335                 } else {
336                         *ent = entry_dup( e );
337                         mdb_cache_return_entry_rw(mdb, e, rw, &lock);
338                 }
339         }
340
341         Debug( LDAP_DEBUG_TRACE,
342                 "mdb_entry_get: rc=%d\n",
343                 rc, 0, 0 ); 
344         return(rc);
345 }