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