]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/id2entry.c
Happy New Year (belated)
[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-2008 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         BDB_LOCKER 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_SETLOCKER( cursor, locker );
126         }
127
128         /* Get the nattrs / nvals counts first */
129         data.ulen = data.dlen = sizeof(buf);
130         data.data = buf;
131         rc = cursor->c_get( cursor, &key, &data, DB_SET );
132         if ( rc ) goto finish;
133
134
135         eh.bv.bv_val = buf;
136         eh.bv.bv_len = data.size;
137         rc = entry_header( &eh );
138         if ( rc ) goto finish;
139
140         /* Get the size */
141         data.flags ^= DB_DBT_PARTIAL;
142         data.ulen = 0;
143         rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
144         if ( rc != DB_BUFFER_SMALL ) goto finish;
145
146         /* Allocate a block and retrieve the data */
147         off = eh.data - eh.bv.bv_val;
148         eh.bv.bv_len = eh.nvals * sizeof( struct berval ) + data.size;
149         eh.bv.bv_val = ch_malloc( eh.bv.bv_len );
150         eh.data = eh.bv.bv_val + eh.nvals * sizeof( struct berval );
151         data.data = eh.data;
152         data.ulen = data.size;
153
154         /* skip past already parsed nattr/nvals */
155         eh.data += off;
156
157         rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
158
159 finish:
160         cursor->c_close( cursor );
161
162         if( rc != 0 ) {
163                 return rc;
164         }
165
166 #ifdef SLAP_ZONE_ALLOC
167         rc = entry_decode(&eh, e, bdb->bi_cache.c_zctx);
168 #else
169         rc = entry_decode(&eh, e);
170 #endif
171
172         if( rc == 0 ) {
173                 (*e)->e_id = id;
174         } else {
175                 /* only free on error. On success, the entry was
176                  * decoded in place.
177                  */
178 #ifndef SLAP_ZONE_ALLOC
179                 ch_free(eh.bv.bv_val);
180 #endif
181         }
182 #ifdef SLAP_ZONE_ALLOC
183         ch_free(eh.bv.bv_val);
184 #endif
185
186         return rc;
187 }
188
189 int bdb_id2entry_delete(
190         BackendDB *be,
191         DB_TXN *tid,
192         Entry *e )
193 {
194         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
195         DB *db = bdb->bi_id2entry->bdi_db;
196         DBT key;
197         int rc;
198         ID nid;
199
200         DBTzero( &key );
201         key.data = &nid;
202         key.size = sizeof(ID);
203         BDB_ID2DISK( e->e_id, &nid );
204
205         /* delete from database */
206         rc = db->del( db, tid, &key, 0 );
207
208         return rc;
209 }
210
211 int bdb_entry_return(
212         Entry *e
213 )
214 {
215         /* Our entries are allocated in two blocks; the data comes from
216          * the db itself and the Entry structure and associated pointers
217          * are allocated in entry_decode. The db data pointer is saved
218          * in e_bv.
219          */
220         if ( e->e_bv.bv_val ) {
221                 /* See if the DNs were changed by modrdn */
222                 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
223                         e->e_bv.bv_val + e->e_bv.bv_len ) {
224                         ch_free(e->e_name.bv_val);
225                         ch_free(e->e_nname.bv_val);
226                 }
227                 e->e_name.bv_val = NULL;
228                 e->e_nname.bv_val = NULL;
229                 /* In tool mode the e_bv buffer is realloc'd, leave it alone */
230                 if( !(slapMode & SLAP_TOOL_MODE) ) {
231                         free( e->e_bv.bv_val );
232                 }
233                 BER_BVZERO( &e->e_bv );
234         }
235         entry_free( e );
236         return 0;
237 }
238
239 int bdb_entry_release(
240         Operation *op,
241         Entry *e,
242         int rw )
243 {
244         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
245         struct bdb_op_info *boi = NULL;
246  
247         /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
248                         SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
249  
250         if ( slapMode == SLAP_SERVER_MODE ) {
251                 /* If not in our cache, just free it */
252                 if ( !e->e_private ) {
253 #ifdef SLAP_ZONE_ALLOC
254                         return bdb_entry_return( bdb, e, -1 );
255 #else
256                         return bdb_entry_return( e );
257 #endif
258                 }
259                 /* free entry and reader or writer lock */
260                 boi = (struct bdb_op_info *)op->o_private;
261
262                 /* lock is freed with txn */
263                 if ( !boi || boi->boi_txn ) {
264                         bdb_unlocked_cache_return_entry_rw( bdb, e, rw );
265                 } else {
266                         struct bdb_lock_info *bli, *prev;
267                         for ( prev=(struct bdb_lock_info *)&boi->boi_locks,
268                                 bli = boi->boi_locks; bli; prev=bli, bli=bli->bli_next ) {
269                                 if ( bli->bli_id == e->e_id ) {
270                                         bdb_cache_return_entry_rw( bdb, e, rw, &bli->bli_lock );
271                                         prev->bli_next = bli->bli_next;
272                                         op->o_tmpfree( bli, op->o_tmpmemctx );
273                                         break;
274                                 }
275                         }
276                         if ( !boi->boi_locks ) {
277                                 op->o_tmpfree( boi, op->o_tmpmemctx );
278                                 op->o_private = NULL;
279                         }
280                 }
281         } else {
282 #ifdef SLAP_ZONE_ALLOC
283                 int zseq = -1;
284                 if (e->e_private != NULL) {
285                         BEI(e)->bei_e = NULL;
286                         zseq = BEI(e)->bei_zseq;
287                 }
288 #else
289                 if (e->e_private != NULL)
290                         BEI(e)->bei_e = NULL;
291 #endif
292                 e->e_private = NULL;
293 #ifdef SLAP_ZONE_ALLOC
294                 bdb_entry_return ( bdb, e, zseq );
295 #else
296                 bdb_entry_return ( e );
297 #endif
298         }
299  
300         return 0;
301 }
302
303 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
304  */
305 int bdb_entry_get(
306         Operation *op,
307         struct berval *ndn,
308         ObjectClass *oc,
309         AttributeDescription *at,
310         int rw,
311         Entry **ent )
312 {
313         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
314         struct bdb_op_info *boi = NULL;
315         DB_TXN *txn = NULL;
316         Entry *e = NULL;
317         EntryInfo *ei;
318         int     rc;
319         const char *at_name = at ? at->ad_cname.bv_val : "(null)";
320
321         BDB_LOCKER      locker = 0;
322         DB_LOCK         lock;
323         int             free_lock_id = 0;
324
325         Debug( LDAP_DEBUG_ARGS,
326                 "=> bdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 ); 
327         Debug( LDAP_DEBUG_ARGS,
328                 "=> bdb_entry_get: oc: \"%s\", at: \"%s\"\n",
329                 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
330
331         if( op ) boi = (struct bdb_op_info *) op->o_private;
332         if( boi != NULL && op->o_bd->be_private == boi->boi_bdb->be_private ) {
333                 txn = boi->boi_txn;
334         }
335
336         if ( txn != NULL ) {
337                 locker = TXN_ID ( txn );
338         } else {
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         if ( oc && !is_entry_objectclass( e, oc, 0 )) {
388                 Debug( LDAP_DEBUG_ACL,
389                         "<= bdb_entry_get: failed to find objectClass %s\n",
390                         oc->soc_cname.bv_val, 0, 0 ); 
391                 rc = LDAP_NO_SUCH_ATTRIBUTE;
392                 goto return_results;
393         }
394
395 return_results:
396         if( rc != LDAP_SUCCESS ) {
397                 /* free entry */
398                 bdb_cache_return_entry_rw(bdb, e, rw, &lock);
399
400         } else {
401                 if ( slapMode == SLAP_SERVER_MODE ) {
402                         *ent = e;
403                         /* big drag. we need a place to store a read lock so we can
404                          * release it later?? If we're in a txn, nothing is needed
405                          * here because the locks will go away with the txn.
406                          */
407                         if ( op ) {
408                                 if ( !boi ) {
409                                         boi = op->o_tmpcalloc(1,sizeof(struct bdb_op_info),op->o_tmpmemctx);
410                                         boi->boi_bdb = op->o_bd;
411                                         op->o_private = boi;
412                                 }
413                                 if ( !boi->boi_txn ) {
414                                         struct bdb_lock_info *bli;
415                                         bli = op->o_tmpalloc( sizeof(struct bdb_lock_info),
416                                                 op->o_tmpmemctx );
417                                         bli->bli_next = boi->boi_locks;
418                                         bli->bli_id = e->e_id;
419                                         bli->bli_lock = lock;
420                                         boi->boi_locks = bli;
421                                 }
422                         }
423                 } else {
424                         *ent = entry_dup( e );
425                         bdb_cache_return_entry_rw(bdb, e, rw, &lock);
426                 }
427         }
428
429         if ( free_lock_id ) {
430                 LOCK_ID_FREE( bdb->bi_dbenv, locker );
431         }
432
433         Debug( LDAP_DEBUG_TRACE,
434                 "bdb_entry_get: rc=%d\n",
435                 rc, 0, 0 ); 
436         return(rc);
437 }