]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/id2entry.c
Import ITS#4439 (slapd not responding) fix for BDB/HDB cache from HEAD
[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                 boi = (struct bdb_op_info *)op->o_private;
262
263                 /* lock is freed with txn */
264                 if ( !boi || boi->boi_txn ) {
265                         bdb_unlocked_cache_return_entry_rw( &bdb->bi_cache, e, rw );
266                 } else {
267                         struct bdb_lock_info *bli, *prev;
268                         for ( prev=(struct bdb_lock_info *)&boi->boi_locks,
269                                 bli = boi->boi_locks; bli; prev=bli, bli=bli->bli_next ) {
270                                 if ( bli->bli_id == e->e_id ) {
271                                         bdb_cache_return_entry_rw( bdb->bi_dbenv, &bdb->bi_cache,
272                                                 e, rw, &bli->bli_lock );
273                                         prev->bli_next = bli->bli_next;
274                                         op->o_tmpfree( bli, op->o_tmpmemctx );
275                                         break;
276                                 }
277                         }
278                         if ( !boi->boi_locks ) {
279                                 op->o_tmpfree( boi, op->o_tmpmemctx );
280                                 op->o_private = NULL;
281                         }
282                 }
283         } else {
284 #ifdef SLAP_ZONE_ALLOC
285                 int zseq = -1;
286                 if (e->e_private != NULL) {
287                         BEI(e)->bei_e = NULL;
288                         zseq = BEI(e)->bei_zseq;
289                 }
290 #else
291                 if (e->e_private != NULL)
292                         BEI(e)->bei_e = NULL;
293 #endif
294                 e->e_private = NULL;
295 #ifdef SLAP_ZONE_ALLOC
296                 bdb_entry_return ( bdb, e, zseq );
297 #else
298                 bdb_entry_return ( e );
299 #endif
300         }
301  
302         return 0;
303 }
304
305 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
306  */
307 int bdb_entry_get(
308         Operation *op,
309         struct berval *ndn,
310         ObjectClass *oc,
311         AttributeDescription *at,
312         int rw,
313         Entry **ent )
314 {
315         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
316         struct bdb_op_info *boi = NULL;
317         DB_TXN *txn = NULL;
318         Entry *e = NULL;
319         EntryInfo *ei;
320         int     rc;
321         const char *at_name = at ? at->ad_cname.bv_val : "(null)";
322
323         u_int32_t       locker = 0;
324         DB_LOCK         lock;
325         int             free_lock_id = 0;
326
327         Debug( LDAP_DEBUG_ARGS,
328                 "=> bdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 ); 
329         Debug( LDAP_DEBUG_ARGS,
330                 "=> bdb_entry_get: oc: \"%s\", at: \"%s\"\n",
331                 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
332
333         if( op ) boi = (struct bdb_op_info *) op->o_private;
334         if( boi != NULL && op->o_bd->be_private == boi->boi_bdb->be_private ) {
335                 txn = boi->boi_txn;
336                 locker = boi->boi_locker;
337         }
338
339         if ( txn != NULL ) {
340                 locker = TXN_ID ( txn );
341         } else if ( !locker ) {
342                 rc = LOCK_ID ( bdb->bi_dbenv, &locker );
343                 free_lock_id = 1;
344                 switch(rc) {
345                 case 0:
346                         break;
347                 default:
348                         return LDAP_OTHER;
349                 }
350         }
351
352 dn2entry_retry:
353         /* can we find entry */
354         rc = bdb_dn2entry( op, txn, ndn, &ei, 0, locker, &lock );
355         switch( rc ) {
356         case DB_NOTFOUND:
357         case 0:
358                 break;
359         case DB_LOCK_DEADLOCK:
360         case DB_LOCK_NOTGRANTED:
361                 /* the txn must abort and retry */
362                 if ( txn ) {
363                         boi->boi_err = rc;
364                         return LDAP_BUSY;
365                 }
366                 ldap_pvt_thread_yield();
367                 goto dn2entry_retry;
368         default:
369                 if ( boi ) boi->boi_err = rc;
370                 if ( free_lock_id ) {
371                         LOCK_ID_FREE( bdb->bi_dbenv, locker );
372                 }
373                 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
374         }
375         if (ei) e = ei->bei_e;
376         if (e == NULL) {
377                 Debug( LDAP_DEBUG_ACL,
378                         "=> bdb_entry_get: cannot find entry: \"%s\"\n",
379                                 ndn->bv_val, 0, 0 ); 
380                 if ( free_lock_id ) {
381                         LOCK_ID_FREE( bdb->bi_dbenv, locker );
382                 }
383                 return LDAP_NO_SUCH_OBJECT; 
384         }
385         
386         Debug( LDAP_DEBUG_ACL,
387                 "=> bdb_entry_get: found entry: \"%s\"\n",
388                 ndn->bv_val, 0, 0 ); 
389
390         /* find attribute values */
391         if( is_entry_alias( e ) ) {
392                 Debug( LDAP_DEBUG_ACL,
393                         "<= bdb_entry_get: entry is an alias\n", 0, 0, 0 );
394                 rc = LDAP_ALIAS_PROBLEM;
395                 goto return_results;
396         }
397
398         if( is_entry_referral( e ) ) {
399                 Debug( LDAP_DEBUG_ACL,
400                         "<= bdb_entry_get: entry is a referral\n", 0, 0, 0 );
401                 rc = LDAP_REFERRAL;
402                 goto return_results;
403         }
404
405         if ( oc && !is_entry_objectclass( e, oc, 0 )) {
406                 Debug( LDAP_DEBUG_ACL,
407                         "<= bdb_entry_get: failed to find objectClass %s\n",
408                         oc->soc_cname.bv_val, 0, 0 ); 
409                 rc = LDAP_NO_SUCH_ATTRIBUTE;
410                 goto return_results;
411         }
412
413 return_results:
414         if( rc != LDAP_SUCCESS ) {
415                 /* free entry */
416                 bdb_cache_return_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, rw, &lock);
417
418         } else {
419                 if ( slapMode == SLAP_SERVER_MODE ) {
420                         *ent = e;
421                         /* big drag. we need a place to store a read lock so we can
422                          * release it later?? If we're in a txn, nothing is needed
423                          * here because the locks will go away with the txn.
424                          */
425                         if ( op ) {
426                                 if ( !boi ) {
427                                         boi = op->o_tmpcalloc(1,sizeof(struct bdb_op_info),op->o_tmpmemctx);
428                                         boi->boi_bdb = op->o_bd;
429                                         op->o_private = boi;
430                                 }
431                                 if ( !boi->boi_txn ) {
432                                         struct bdb_lock_info *bli;
433                                         bli = op->o_tmpalloc( sizeof(struct bdb_lock_info),
434                                                 op->o_tmpmemctx );
435                                         bli->bli_next = boi->boi_locks;
436                                         bli->bli_id = e->e_id;
437                                         bli->bli_lock = lock;
438                                         boi->boi_locks = bli;
439                                 }
440                         }
441                 } else {
442                         *ent = entry_dup( e );
443                         bdb_cache_return_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, rw, &lock);
444                 }
445         }
446
447         if ( free_lock_id ) {
448                 LOCK_ID_FREE( bdb->bi_dbenv, locker );
449         }
450
451         Debug( LDAP_DEBUG_TRACE,
452                 "bdb_entry_get: rc=%d\n",
453                 rc, 0, 0 ); 
454         return(rc);
455 }