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