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