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