]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/id2entry.c
More for #5860 - if the cache blew past the maxsize, bring it all the way
[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-2009 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         ID id,
97         Entry **e )
98 {
99         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
100         DB *db = bdb->bi_id2entry->bdi_db;
101         DBT key, data;
102         DBC *cursor;
103         EntryHeader eh;
104         char buf[16];
105         int rc = 0, off;
106         ID nid;
107
108         *e = NULL;
109
110         DBTzero( &key );
111         key.data = &nid;
112         key.size = sizeof(ID);
113         BDB_ID2DISK( id, &nid );
114
115         DBTzero( &data );
116         data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
117
118         /* fetch it */
119         rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
120         if ( rc ) return rc;
121
122         /* Get the nattrs / nvals counts first */
123         data.ulen = data.dlen = sizeof(buf);
124         data.data = buf;
125         rc = cursor->c_get( cursor, &key, &data, DB_SET );
126         if ( rc ) goto finish;
127
128
129         eh.bv.bv_val = buf;
130         eh.bv.bv_len = data.size;
131         rc = entry_header( &eh );
132         if ( rc ) goto finish;
133
134         /* Get the size */
135         data.flags ^= DB_DBT_PARTIAL;
136         data.ulen = 0;
137         rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
138         if ( rc != DB_BUFFER_SMALL ) goto finish;
139
140         /* Allocate a block and retrieve the data */
141         off = eh.data - eh.bv.bv_val;
142         eh.bv.bv_len = eh.nvals * sizeof( struct berval ) + data.size;
143         eh.bv.bv_val = ch_malloc( eh.bv.bv_len );
144         eh.data = eh.bv.bv_val + eh.nvals * sizeof( struct berval );
145         data.data = eh.data;
146         data.ulen = data.size;
147
148         /* skip past already parsed nattr/nvals */
149         eh.data += off;
150
151         rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
152
153 finish:
154         cursor->c_close( cursor );
155
156         if( rc != 0 ) {
157                 return rc;
158         }
159
160 #ifdef SLAP_ZONE_ALLOC
161         rc = entry_decode(&eh, e, bdb->bi_cache.c_zctx);
162 #else
163         rc = entry_decode(&eh, e);
164 #endif
165
166         if( rc == 0 ) {
167                 (*e)->e_id = id;
168         } else {
169                 /* only free on error. On success, the entry was
170                  * decoded in place.
171                  */
172 #ifndef SLAP_ZONE_ALLOC
173                 ch_free(eh.bv.bv_val);
174 #endif
175         }
176 #ifdef SLAP_ZONE_ALLOC
177         ch_free(eh.bv.bv_val);
178 #endif
179
180         return rc;
181 }
182
183 int bdb_id2entry_delete(
184         BackendDB *be,
185         DB_TXN *tid,
186         Entry *e )
187 {
188         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
189         DB *db = bdb->bi_id2entry->bdi_db;
190         DBT key;
191         int rc;
192         ID nid;
193
194         DBTzero( &key );
195         key.data = &nid;
196         key.size = sizeof(ID);
197         BDB_ID2DISK( e->e_id, &nid );
198
199         /* delete from database */
200         rc = db->del( db, tid, &key, 0 );
201
202         return rc;
203 }
204
205 int bdb_entry_return(
206         Entry *e
207 )
208 {
209         /* Our entries are allocated in two blocks; the data comes from
210          * the db itself and the Entry structure and associated pointers
211          * are allocated in entry_decode. The db data pointer is saved
212          * in e_bv.
213          */
214         if ( e->e_bv.bv_val ) {
215                 /* See if the DNs were changed by modrdn */
216                 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
217                         e->e_bv.bv_val + e->e_bv.bv_len ) {
218                         ch_free(e->e_name.bv_val);
219                         ch_free(e->e_nname.bv_val);
220                 }
221                 e->e_name.bv_val = NULL;
222                 e->e_nname.bv_val = NULL;
223                 /* In tool mode the e_bv buffer is realloc'd, leave it alone */
224                 if( !(slapMode & SLAP_TOOL_MODE) ) {
225                         free( e->e_bv.bv_val );
226                 }
227                 BER_BVZERO( &e->e_bv );
228         }
229         entry_free( e );
230         return 0;
231 }
232
233 int bdb_entry_release(
234         Operation *op,
235         Entry *e,
236         int rw )
237 {
238         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
239         struct bdb_op_info *boi;
240         OpExtra *oex;
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                 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
256                         if ( oex->oe_key == bdb ) break;
257                 }
258                 boi = (struct bdb_op_info *)oex;
259
260                 /* lock is freed with txn */
261                 if ( !boi || boi->boi_txn ) {
262                         bdb_unlocked_cache_return_entry_rw( bdb, e, rw );
263                 } else {
264                         struct bdb_lock_info *bli, *prev;
265                         for ( prev=(struct bdb_lock_info *)&boi->boi_locks,
266                                 bli = boi->boi_locks; bli; prev=bli, bli=bli->bli_next ) {
267                                 if ( bli->bli_id == e->e_id ) {
268                                         bdb_cache_return_entry_rw( bdb, e, rw, &bli->bli_lock );
269                                         prev->bli_next = bli->bli_next;
270                                         /* Cleanup, or let caller know we unlocked */
271                                         if ( bli->bli_flag & BLI_DONTFREE )
272                                                 bli->bli_flag = 0;
273                                         else
274                                                 op->o_tmpfree( bli, op->o_tmpmemctx );
275                                         break;
276                                 }
277                         }
278                         if ( !boi->boi_locks ) {
279                                 LDAP_SLIST_REMOVE( &op->o_extra, &boi->boi_oe, OpExtra, oe_next );
280                                 if ( !(boi->boi_flag & BOI_DONTFREE))
281                                         op->o_tmpfree( boi, op->o_tmpmemctx );
282                         }
283                 }
284         } else {
285 #ifdef SLAP_ZONE_ALLOC
286                 int zseq = -1;
287                 if (e->e_private != NULL) {
288                         BEI(e)->bei_e = NULL;
289                         zseq = BEI(e)->bei_zseq;
290                 }
291 #else
292                 if (e->e_private != NULL)
293                         BEI(e)->bei_e = NULL;
294 #endif
295                 e->e_private = NULL;
296 #ifdef SLAP_ZONE_ALLOC
297                 bdb_entry_return ( bdb, e, zseq );
298 #else
299                 bdb_entry_return ( e );
300 #endif
301         }
302  
303         return 0;
304 }
305
306 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
307  */
308 int bdb_entry_get(
309         Operation *op,
310         struct berval *ndn,
311         ObjectClass *oc,
312         AttributeDescription *at,
313         int rw,
314         Entry **ent )
315 {
316         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
317         struct bdb_op_info *boi = NULL;
318         DB_TXN *txn = NULL;
319         Entry *e = NULL;
320         EntryInfo *ei;
321         int     rc;
322         const char *at_name = at ? at->ad_cname.bv_val : "(null)";
323
324         DB_LOCK         lock;
325
326         Debug( LDAP_DEBUG_ARGS,
327                 "=> bdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 ); 
328         Debug( LDAP_DEBUG_ARGS,
329                 "=> bdb_entry_get: oc: \"%s\", at: \"%s\"\n",
330                 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
331
332         if( op ) {
333                 OpExtra *oex;
334                 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
335                         if ( oex->oe_key == bdb ) break;
336                 }
337                 boi = (struct bdb_op_info *)oex;
338                 if ( boi )
339                         txn = boi->boi_txn;
340         }
341
342         if ( !txn ) {
343                 rc = bdb_reader_get( op, bdb->bi_dbenv, &txn );
344                 switch(rc) {
345                 case 0:
346                         break;
347                 default:
348                         return LDAP_OTHER;
349                 }
350         }
351
352 dn2entry_retry:
353         /* can we find entry */
354         rc = bdb_dn2entry( op, txn, ndn, &ei, 0, &lock );
355         switch( rc ) {
356         case DB_NOTFOUND:
357         case 0:
358                 break;
359         case DB_LOCK_DEADLOCK:
360         case DB_LOCK_NOTGRANTED:
361                 /* the txn must abort and retry */
362                 if ( txn ) {
363                         if ( boi ) boi->boi_err = rc;
364                         return LDAP_BUSY;
365                 }
366                 ldap_pvt_thread_yield();
367                 goto dn2entry_retry;
368         default:
369                 if ( boi ) boi->boi_err = rc;
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                 return LDAP_NO_SUCH_OBJECT; 
378         }
379         
380         Debug( LDAP_DEBUG_ACL,
381                 "=> bdb_entry_get: found entry: \"%s\"\n",
382                 ndn->bv_val, 0, 0 ); 
383
384         if ( oc && !is_entry_objectclass( e, oc, 0 )) {
385                 Debug( LDAP_DEBUG_ACL,
386                         "<= bdb_entry_get: failed to find objectClass %s\n",
387                         oc->soc_cname.bv_val, 0, 0 ); 
388                 rc = LDAP_NO_SUCH_ATTRIBUTE;
389                 goto return_results;
390         }
391
392         /* NOTE: attr_find() or attrs_find()? */
393         if ( at && attr_find( e->e_attrs, at ) == NULL ) {
394                 Debug( LDAP_DEBUG_ACL,
395                         "<= bdb_entry_get: failed to find attribute %s\n",
396                         at->ad_cname.bv_val, 0, 0 ); 
397                 rc = LDAP_NO_SUCH_ATTRIBUTE;
398                 goto return_results;
399         }
400
401 return_results:
402         if( rc != LDAP_SUCCESS ) {
403                 /* free entry */
404                 bdb_cache_return_entry_rw(bdb, e, rw, &lock);
405
406         } else {
407                 if ( slapMode == SLAP_SERVER_MODE ) {
408                         *ent = e;
409                         /* big drag. we need a place to store a read lock so we can
410                          * release it later?? If we're in a txn, nothing is needed
411                          * here because the locks will go away with the txn.
412                          */
413                         if ( op ) {
414                                 if ( !boi ) {
415                                         boi = op->o_tmpcalloc(1,sizeof(struct bdb_op_info),op->o_tmpmemctx);
416                                         boi->boi_oe.oe_key = bdb;
417                                         LDAP_SLIST_INSERT_HEAD( &op->o_extra, &boi->boi_oe, oe_next );
418                                 }
419                                 if ( !boi->boi_txn ) {
420                                         struct bdb_lock_info *bli;
421                                         bli = op->o_tmpalloc( sizeof(struct bdb_lock_info),
422                                                 op->o_tmpmemctx );
423                                         bli->bli_next = boi->boi_locks;
424                                         bli->bli_id = e->e_id;
425                                         bli->bli_flag = 0;
426                                         bli->bli_lock = lock;
427                                         boi->boi_locks = bli;
428                                 }
429                         }
430                 } else {
431                         *ent = entry_dup( e );
432                         bdb_cache_return_entry_rw(bdb, e, rw, &lock);
433                 }
434         }
435
436         Debug( LDAP_DEBUG_TRACE,
437                 "bdb_entry_get: rc=%d\n",
438                 rc, 0, 0 ); 
439         return(rc);
440 }