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