]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/id2entry.c
Restructured to avoid direct use of sl_malloc functions. Minimizes impact
[openldap] / servers / slapd / back-bdb / id2entry.c
1 /* id2entry.c - routines to deal with the id2entry database */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11 #include <ac/string.h>
12
13 #include "back-bdb.h"
14 #include "external.h"
15
16 static int bdb_id2entry_put(
17         BackendDB *be,
18         DB_TXN *tid,
19         Entry *e,
20         int flag )
21 {
22         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
23         DB *db = bdb->bi_id2entry->bdi_db;
24         DBT key, data;
25         struct berval bv;
26         int rc;
27 #ifdef BDB_HIER
28         struct berval odn, ondn;
29
30         /* We only store rdns, and they go in the dn2id database. */
31
32         odn = e->e_name; ondn = e->e_nname;
33
34         e->e_name = slap_empty_bv;
35         e->e_nname = slap_empty_bv;
36 #endif
37         DBTzero( &key );
38         key.data = (char *) &e->e_id;
39         key.size = sizeof(ID);
40
41         rc = entry_encode( e, &bv );
42 #ifdef BDB_HIER
43         e->e_name = odn; e->e_nname = ondn;
44 #endif
45         if( rc != LDAP_SUCCESS ) {
46                 return -1;
47         }
48
49         DBTzero( &data );
50         bv2DBT( &bv, &data );
51
52         rc = db->put( db, tid, &key, &data, flag );
53
54         free( bv.bv_val );
55         return rc;
56 }
57
58 /*
59  * This routine adds (or updates) an entry on disk.
60  * The cache should be already be updated.
61  */
62
63
64 int bdb_id2entry_add(
65         BackendDB *be,
66         DB_TXN *tid,
67         Entry *e )
68 {
69         return bdb_id2entry_put(be, tid, e, DB_NOOVERWRITE);
70 }
71
72 int bdb_id2entry_update(
73         BackendDB *be,
74         DB_TXN *tid,
75         Entry *e )
76 {
77         return bdb_id2entry_put(be, tid, e, 0);
78 }
79
80 int bdb_id2entry(
81         BackendDB *be,
82         DB_TXN *tid,
83         ID id,
84         Entry **e )
85 {
86         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
87         DB *db = bdb->bi_id2entry->bdi_db;
88         DBT key, data;
89         struct berval bv;
90         int rc = 0, ret = 0;
91
92         *e = NULL;
93
94         DBTzero( &key );
95         key.data = (char *) &id;
96         key.size = sizeof(ID);
97
98         DBTzero( &data );
99         data.flags = DB_DBT_MALLOC;
100
101         /* fetch it */
102         rc = db->get( db, tid, &key, &data, bdb->bi_db_opflags );
103
104         if( rc != 0 ) {
105                 return rc;
106         }
107
108         DBT2bv( &data, &bv );
109
110         rc = entry_decode( &bv, e );
111
112         if( rc == 0 ) {
113                 (*e)->e_id = id;
114         } else {
115                 /* only free on error. On success, the entry was
116                  * decoded in place.
117                  */
118                 ch_free( data.data );
119         }
120
121         return rc;
122 }
123
124 int bdb_id2entry_delete(
125         BackendDB *be,
126         DB_TXN *tid,
127         Entry *e )
128 {
129         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
130         DB *db = bdb->bi_id2entry->bdi_db;
131         DBT key;
132         int rc;
133
134         DBTzero( &key );
135         key.data = (char *) &e->e_id;
136         key.size = sizeof(ID);
137
138         /* delete from database */
139         rc = db->del( db, tid, &key, 0 );
140
141         return rc;
142 }
143
144 int bdb_entry_return(
145         Entry *e )
146 {
147         /* Our entries are allocated in two blocks; the data comes from
148          * the db itself and the Entry structure and associated pointers
149          * are allocated in entry_decode. The db data pointer is saved
150          * in e_bv. Since the Entry structure is allocated as a single
151          * block, e_attrs is always a fixed offset from e. The exception
152          * is when an entry has been modified, in which case we also need
153          * to free e_attrs.
154          */
155         if( !e->e_bv.bv_val ) { /* A regular entry, from do_add */
156                 entry_free( e );
157                 return 0;
158         }
159         if( (void *) e->e_attrs != (void *) (e+1)) {
160                 attrs_free( e->e_attrs );
161         }
162
163         /* See if the DNs were changed by modrdn */
164         if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
165                 e->e_bv.bv_val + e->e_bv.bv_len ) {
166                 ch_free(e->e_name.bv_val);
167                 ch_free(e->e_nname.bv_val);
168                 e->e_name.bv_val = NULL;
169                 e->e_nname.bv_val = NULL;
170         }
171 #ifndef BDB_HIER
172         /* In tool mode the e_bv buffer is realloc'd, leave it alone */
173         if( !(slapMode & SLAP_TOOL_MODE) ) {
174                 free( e->e_bv.bv_val );
175         }
176 #else
177         free( e->e_bv.bv_val );
178 #endif
179         free( e );
180
181         return 0;
182 }
183
184 int bdb_entry_release(
185         Operation *o,
186         Entry *e,
187         int rw )
188 {
189         struct bdb_info *bdb = (struct bdb_info *) o->o_bd->be_private;
190         struct bdb_op_info *boi = NULL;
191  
192         /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
193                         SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
194  
195         if ( slapMode == SLAP_SERVER_MODE ) {
196                 /* free entry and reader or writer lock */
197                 if ( o ) {
198                         boi = (struct bdb_op_info *)o->o_private;
199                 }
200                 /* lock is freed with txn */
201                 if ( !boi || boi->boi_txn ) {
202                         bdb_unlocked_cache_return_entry_rw( &bdb->bi_cache, e, rw );
203                 } else {
204                         bdb_cache_return_entry_rw( bdb->bi_dbenv, &bdb->bi_cache, e, rw, &boi->boi_lock );
205                         o->o_tmpfree( boi, o->o_tmpmemctx );
206                         o->o_private = NULL;
207                 }
208         } else {
209                 if (e->e_private != NULL)
210                         BEI(e)->bei_e = NULL;
211                 e->e_private = NULL;
212                 bdb_entry_return ( e );
213         }
214  
215         return 0;
216 }
217
218 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
219  */
220 int bdb_entry_get(
221         Operation *op,
222         struct berval *ndn,
223         ObjectClass *oc,
224         AttributeDescription *at,
225         int rw,
226         Entry **ent )
227 {
228         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
229         struct bdb_op_info *boi = NULL;
230         DB_TXN *txn = NULL;
231         Entry *e = NULL;
232         EntryInfo *ei;
233         int     rc;
234         const char *at_name = at->ad_cname.bv_val;
235
236         u_int32_t       locker = 0;
237         DB_LOCK         lock;
238         int             free_lock_id = 0;
239
240 #ifdef NEW_LOGGING
241         LDAP_LOG( BACK_BDB, ARGS, 
242                 "bdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 );
243         LDAP_LOG( BACK_BDB, ARGS, 
244                 "bdb_entry_get: oc: \"%s\", at: \"%s\"\n",
245                 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
246 #else
247         Debug( LDAP_DEBUG_ARGS,
248                 "=> bdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 ); 
249         Debug( LDAP_DEBUG_ARGS,
250                 "=> bdb_entry_get: oc: \"%s\", at: \"%s\"\n",
251                 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
252 #endif
253
254         if( op ) boi = (struct bdb_op_info *) op->o_private;
255         if( boi != NULL && op->o_bd == boi->boi_bdb ) {
256                 txn = boi->boi_txn;
257                 locker = boi->boi_locker;
258         }
259
260         if ( txn != NULL ) {
261                 locker = TXN_ID ( txn );
262         } else if ( !locker ) {
263                 rc = LOCK_ID ( bdb->bi_dbenv, &locker );
264                 free_lock_id = 1;
265                 switch(rc) {
266                 case 0:
267                         break;
268                 default:
269                         return LDAP_OTHER;
270                 }
271         }
272
273 dn2entry_retry:
274         /* can we find entry */
275         rc = bdb_dn2entry( op, txn, ndn, &ei, 0, locker, &lock );
276         switch( rc ) {
277         case DB_NOTFOUND:
278         case 0:
279                 break;
280         case DB_LOCK_DEADLOCK:
281         case DB_LOCK_NOTGRANTED:
282                 /* the txn must abort and retry */
283                 if ( txn ) {
284                         boi->boi_err = rc;
285                         return LDAP_BUSY;
286                 }
287                 ldap_pvt_thread_yield();
288                 goto dn2entry_retry;
289         default:
290                 if ( boi ) boi->boi_err = rc;
291                 if ( free_lock_id ) {
292                         LOCK_ID_FREE( bdb->bi_dbenv, locker );
293                 }
294                 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
295         }
296         if (ei) e = ei->bei_e;
297         if (e == NULL) {
298 #ifdef NEW_LOGGING
299                 LDAP_LOG( BACK_BDB, INFO, 
300                         "bdb_entry_get: cannot find entry (%s)\n", 
301                         ndn->bv_val, 0, 0 );
302 #else
303                 Debug( LDAP_DEBUG_ACL,
304                         "=> bdb_entry_get: cannot find entry: \"%s\"\n",
305                                 ndn->bv_val, 0, 0 ); 
306 #endif
307                 if ( free_lock_id ) {
308                         LOCK_ID_FREE( bdb->bi_dbenv, locker );
309                 }
310                 return LDAP_NO_SUCH_OBJECT; 
311         }
312         
313 #ifdef NEW_LOGGING
314         LDAP_LOG( BACK_BDB, DETAIL1, "bdb_entry_get: found entry (%s)\n",
315                 ndn->bv_val, 0, 0 );
316 #else
317         Debug( LDAP_DEBUG_ACL,
318                 "=> bdb_entry_get: found entry: \"%s\"\n",
319                 ndn->bv_val, 0, 0 ); 
320 #endif
321
322 #ifdef BDB_ALIASES
323         /* find attribute values */
324         if( is_entry_alias( e ) ) {
325 #ifdef NEW_LOGGING
326                 LDAP_LOG( BACK_BDB, INFO, 
327                         "bdb_entry_get: entry (%s) is an alias\n", e->e_name.bv_val, 0, 0 );
328 #else
329                 Debug( LDAP_DEBUG_ACL,
330                         "<= bdb_entry_get: entry is an alias\n", 0, 0, 0 );
331 #endif
332                 rc = LDAP_ALIAS_PROBLEM;
333                 goto return_results;
334         }
335 #endif
336
337         if( is_entry_referral( e ) ) {
338 #ifdef NEW_LOGGING
339                 LDAP_LOG( BACK_BDB, INFO, 
340                         "bdb_entry_get: entry (%s) is a referral.\n", e->e_name.bv_val, 0, 0);
341 #else
342                 Debug( LDAP_DEBUG_ACL,
343                         "<= bdb_entry_get: entry is a referral\n", 0, 0, 0 );
344 #endif
345                 rc = LDAP_REFERRAL;
346                 goto return_results;
347         }
348
349         if ( oc && !is_entry_objectclass( e, oc, 0 )) {
350 #ifdef NEW_LOGGING
351                 LDAP_LOG( BACK_BDB, INFO, 
352                         "bdb_entry_get: failed to find objectClass.\n", 0, 0, 0 );
353 #else
354                 Debug( LDAP_DEBUG_ACL,
355                         "<= bdb_entry_get: failed to find objectClass\n",
356                         0, 0, 0 ); 
357 #endif
358                 rc = LDAP_NO_SUCH_ATTRIBUTE;
359                 goto return_results;
360         }
361
362 return_results:
363         if( rc != LDAP_SUCCESS ) {
364                 /* free entry */
365                 bdb_cache_return_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, rw, &lock);
366         } else {
367                 *ent = e;
368                 /* big drag. we need a place to store a read lock so we can
369                  * release it later??
370                  */
371                 if ( op && !boi ) {
372                         boi = op->o_tmpcalloc(1,sizeof(struct bdb_op_info),op->o_tmpmemctx);
373                         boi->boi_lock = lock;
374                         op->o_private = boi;
375                 }
376         }
377
378         if ( free_lock_id ) {
379                 LOCK_ID_FREE( bdb->bi_dbenv, locker );
380         }
381
382 #ifdef NEW_LOGGING
383         LDAP_LOG( BACK_BDB, ENTRY, "bdb_entry_get: rc=%d\n", rc, 0, 0 );
384 #else
385         Debug( LDAP_DEBUG_TRACE,
386                 "bdb_entry_get: rc=%d\n",
387                 rc, 0, 0 ); 
388 #endif
389         return(rc);
390 }