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