]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/id2entry.c
f362739185eac099f3f190bc56105f847879357f
[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-2006 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         u_int32_t locker,
96         ID id,
97         Entry **e )
98 {
99         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
100         DB *db = bdb->bi_id2entry->bdi_db;
101         DBT key, data;
102         DBC *cursor;
103         struct berval bv;
104         int rc = 0;
105         ID nid;
106
107         *e = NULL;
108
109         DBTzero( &key );
110         key.data = &nid;
111         key.size = sizeof(ID);
112         BDB_ID2DISK( id, &nid );
113
114         DBTzero( &data );
115         data.flags = DB_DBT_MALLOC;
116
117         /* fetch it */
118         rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
119         if ( rc ) return rc;
120
121         /* Use our own locker if needed */
122         if ( !tid && locker )
123                 cursor->locker = locker;
124
125         rc = cursor->c_get( cursor, &key, &data, DB_SET );
126         cursor->c_close( cursor );
127
128         if( rc != 0 ) {
129                 return rc;
130         }
131
132         DBT2bv( &data, &bv );
133
134 #ifdef SLAP_ZONE_ALLOC
135         rc = entry_decode(&bv, e, bdb->bi_cache.c_zctx);
136 #else
137         rc = entry_decode(&bv, e);
138 #endif
139
140         if( rc == 0 ) {
141                 (*e)->e_id = id;
142         } else {
143                 /* only free on error. On success, the entry was
144                  * decoded in place.
145                  */
146 #ifndef SLAP_ZONE_ALLOC
147                 ch_free(data.data);
148 #endif
149         }
150 #ifdef SLAP_ZONE_ALLOC
151         ch_free(data.data);
152 #endif
153
154         return rc;
155 }
156
157 int bdb_id2entry_delete(
158         BackendDB *be,
159         DB_TXN *tid,
160         Entry *e )
161 {
162         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
163         DB *db = bdb->bi_id2entry->bdi_db;
164         DBT key;
165         int rc;
166         ID nid;
167
168         DBTzero( &key );
169         key.data = &nid;
170         key.size = sizeof(ID);
171         BDB_ID2DISK( e->e_id, &nid );
172
173         /* delete from database */
174         rc = db->del( db, tid, &key, 0 );
175
176         return rc;
177 }
178
179 #ifdef SLAP_ZONE_ALLOC
180 int bdb_entry_return(
181         struct bdb_info *bdb,
182         Entry *e,
183         int zseq
184 )
185 #else
186 int bdb_entry_return(
187         Entry *e
188 )
189 #endif
190 {
191 #ifdef SLAP_ZONE_ALLOC
192         if (!slap_zn_validate(bdb->bi_cache.c_zctx, e, zseq)) {
193                 return 0;
194         }
195 #endif
196         /* Our entries are allocated in two blocks; the data comes from
197          * the db itself and the Entry structure and associated pointers
198          * are allocated in entry_decode. The db data pointer is saved
199          * in e_bv. Since the Entry structure is allocated as a single
200          * block, e_attrs is always a fixed offset from e. The exception
201          * is when an entry has been modified, in which case we also need
202          * to free e_attrs.
203          */
204
205 #ifdef LDAP_COMP_MATCH
206         comp_tree_free( e->e_attrs );
207 #endif
208         if( !e->e_bv.bv_val ) { /* Entry added by do_add */
209                 entry_free( e );
210                 return 0;
211         }
212         if( (void *) e->e_attrs != (void *) (e+1)) {
213                 attrs_free( e->e_attrs );
214         }
215
216         /* See if the DNs were changed by modrdn */
217         if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
218                 e->e_bv.bv_val + e->e_bv.bv_len ) {
219                 ch_free(e->e_name.bv_val);
220                 ch_free(e->e_nname.bv_val);
221                 e->e_name.bv_val = NULL;
222                 e->e_nname.bv_val = NULL;
223         }
224 #ifndef SLAP_ZONE_ALLOC
225         /* In tool mode the e_bv buffer is realloc'd, leave it alone */
226         if( !(slapMode & SLAP_TOOL_MODE) ) {
227                 free( e->e_bv.bv_val );
228         }
229 #endif /* !SLAP_ZONE_ALLOC */
230
231 #ifdef SLAP_ZONE_ALLOC
232         slap_zn_free( e, bdb->bi_cache.c_zctx );
233 #else
234         free( e );
235 #endif
236
237         return 0;
238 }
239
240 int bdb_entry_release(
241         Operation *op,
242         Entry *e,
243         int rw )
244 {
245         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
246         struct bdb_op_info *boi = NULL;
247  
248         /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
249                         SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
250  
251         if ( slapMode == SLAP_SERVER_MODE ) {
252                 /* If not in our cache, just free it */
253                 if ( !e->e_private ) {
254 #ifdef SLAP_ZONE_ALLOC
255                         return bdb_entry_return( bdb, e, -1 );
256 #else
257                         return bdb_entry_return( e );
258 #endif
259                 }
260                 /* free entry and reader or writer lock */
261                 if ( op ) {
262                         boi = (struct bdb_op_info *)op->o_private;
263                 }
264                 /* lock is freed with txn */
265                 if ( !boi || boi->boi_txn ) {
266                         bdb_unlocked_cache_return_entry_rw( &bdb->bi_cache, e, rw );
267                 } else {
268                         struct bdb_lock_info *bli, *prev;
269                         for ( prev=(struct bdb_lock_info *)&boi->boi_locks,
270                                 bli = boi->boi_locks; bli; prev=bli, bli=bli->bli_next ) {
271                                 if ( bli->bli_id == e->e_id ) {
272                                         bdb_cache_return_entry_rw( bdb->bi_dbenv, &bdb->bi_cache,
273                                                 e, rw, &bli->bli_lock );
274                                         prev->bli_next = bli->bli_next;
275                                         op->o_tmpfree( bli, op->o_tmpmemctx );
276                                         break;
277                                 }
278                         }
279                         if ( !boi->boi_locks ) {
280                                 op->o_tmpfree( boi, op->o_tmpmemctx );
281                                 op->o_private = NULL;
282                         }
283                 }
284         } else {
285 #ifdef SLAP_ZONE_ALLOC
286                 int zseq = -1;
287                 if (e->e_private != NULL) {
288                         BEI(e)->bei_e = NULL;
289                         zseq = BEI(e)->bei_zseq;
290                 }
291 #else
292                 if (e->e_private != NULL)
293                         BEI(e)->bei_e = NULL;
294 #endif
295                 e->e_private = NULL;
296 #ifdef SLAP_ZONE_ALLOC
297                 bdb_entry_return ( bdb, e, zseq );
298 #else
299                 bdb_entry_return ( e );
300 #endif
301         }
302  
303         return 0;
304 }
305
306 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
307  */
308 int bdb_entry_get(
309         Operation *op,
310         struct berval *ndn,
311         ObjectClass *oc,
312         AttributeDescription *at,
313         int rw,
314         Entry **ent )
315 {
316         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
317         struct bdb_op_info *boi = NULL;
318         DB_TXN *txn = NULL;
319         Entry *e = NULL;
320         EntryInfo *ei;
321         int     rc;
322         const char *at_name = at ? at->ad_cname.bv_val : "(null)";
323
324         u_int32_t       locker = 0;
325         DB_LOCK         lock;
326         int             free_lock_id = 0;
327
328         Debug( LDAP_DEBUG_ARGS,
329                 "=> bdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 ); 
330         Debug( LDAP_DEBUG_ARGS,
331                 "=> bdb_entry_get: oc: \"%s\", at: \"%s\"\n",
332                 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
333
334         if( op ) boi = (struct bdb_op_info *) op->o_private;
335         if( boi != NULL && op->o_bd->be_private == boi->boi_bdb->be_private ) {
336                 txn = boi->boi_txn;
337                 locker = boi->boi_locker;
338         }
339
340         if ( txn != NULL ) {
341                 locker = TXN_ID ( txn );
342         } else if ( !locker ) {
343                 rc = LOCK_ID ( bdb->bi_dbenv, &locker );
344                 free_lock_id = 1;
345                 switch(rc) {
346                 case 0:
347                         break;
348                 default:
349                         return LDAP_OTHER;
350                 }
351         }
352
353 dn2entry_retry:
354         /* can we find entry */
355         rc = bdb_dn2entry( op, txn, ndn, &ei, 0, locker, &lock );
356         switch( rc ) {
357         case DB_NOTFOUND:
358         case 0:
359                 break;
360         case DB_LOCK_DEADLOCK:
361         case DB_LOCK_NOTGRANTED:
362                 /* the txn must abort and retry */
363                 if ( txn ) {
364                         boi->boi_err = rc;
365                         return LDAP_BUSY;
366                 }
367                 ldap_pvt_thread_yield();
368                 goto dn2entry_retry;
369         default:
370                 if ( boi ) boi->boi_err = rc;
371                 if ( free_lock_id ) {
372                         LOCK_ID_FREE( bdb->bi_dbenv, locker );
373                 }
374                 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
375         }
376         if (ei) e = ei->bei_e;
377         if (e == NULL) {
378                 Debug( LDAP_DEBUG_ACL,
379                         "=> bdb_entry_get: cannot find entry: \"%s\"\n",
380                                 ndn->bv_val, 0, 0 ); 
381                 if ( free_lock_id ) {
382                         LOCK_ID_FREE( bdb->bi_dbenv, locker );
383                 }
384                 return LDAP_NO_SUCH_OBJECT; 
385         }
386         
387         Debug( LDAP_DEBUG_ACL,
388                 "=> bdb_entry_get: found entry: \"%s\"\n",
389                 ndn->bv_val, 0, 0 ); 
390
391         /* find attribute values */
392         if( is_entry_alias( e ) ) {
393                 Debug( LDAP_DEBUG_ACL,
394                         "<= bdb_entry_get: entry is an alias\n", 0, 0, 0 );
395                 rc = LDAP_ALIAS_PROBLEM;
396                 goto return_results;
397         }
398
399         if( is_entry_referral( e ) ) {
400                 Debug( LDAP_DEBUG_ACL,
401                         "<= bdb_entry_get: entry is a referral\n", 0, 0, 0 );
402                 rc = LDAP_REFERRAL;
403                 goto return_results;
404         }
405
406         if ( oc && !is_entry_objectclass( e, oc, 0 )) {
407                 Debug( LDAP_DEBUG_ACL,
408                         "<= bdb_entry_get: failed to find objectClass %s\n",
409                         oc->soc_cname.bv_val, 0, 0 ); 
410                 rc = LDAP_NO_SUCH_ATTRIBUTE;
411                 goto return_results;
412         }
413
414 return_results:
415         if( rc != LDAP_SUCCESS ) {
416                 /* free entry */
417                 bdb_cache_return_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, rw, &lock);
418
419         } else {
420                 if ( slapMode == SLAP_SERVER_MODE ) {
421                         *ent = e;
422                         /* big drag. we need a place to store a read lock so we can
423                          * release it later?? If we're in a txn, nothing is needed
424                          * here because the locks will go away with the txn.
425                          */
426                         if ( op ) {
427                                 if ( !boi ) {
428                                         boi = op->o_tmpcalloc(1,sizeof(struct bdb_op_info),op->o_tmpmemctx);
429                                         boi->boi_bdb = op->o_bd;
430                                         op->o_private = boi;
431                                 }
432                                 if ( !boi->boi_txn ) {
433                                         struct bdb_lock_info *bli;
434                                         bli = op->o_tmpalloc( sizeof(struct bdb_lock_info),
435                                                 op->o_tmpmemctx );
436                                         bli->bli_next = boi->boi_locks;
437                                         bli->bli_id = e->e_id;
438                                         bli->bli_lock = lock;
439                                         boi->boi_locks = bli;
440                                 }
441                         }
442                 } else {
443                         *ent = entry_dup( e );
444                         bdb_cache_return_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, rw, &lock);
445                 }
446         }
447
448         if ( free_lock_id ) {
449                 LOCK_ID_FREE( bdb->bi_dbenv, locker );
450         }
451
452         Debug( LDAP_DEBUG_TRACE,
453                 "bdb_entry_get: rc=%d\n",
454                 rc, 0, 0 ); 
455         return(rc);
456 }