]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/id2entry.c
redesign of back-bdb locking :
[openldap] / servers / slapd / back-bdb / id2entry.c
1 /* id2entry.c - routines to deal with the id2entry database */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11 #include <ac/string.h>
12
13 #include "back-bdb.h"
14
15 int bdb_id2entry_put(
16         BackendDB *be,
17         DB_TXN *tid,
18         Entry *e,
19         int flag )
20 {
21         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
22         DB *db = bdb->bi_id2entry->bdi_db;
23         DBT key, data;
24         struct berval bv;
25         int rc;
26 #ifdef BDB_HIER
27         char *odn, *ondn;
28
29         /* We only store rdns, and they go in the id2parent database. */
30
31         odn = e->e_dn; ondn = e->e_ndn;
32
33         e->e_dn = ""; e->e_ndn = "";
34 #endif
35         DBTzero( &key );
36         key.data = (char *) &e->e_id;
37         key.size = sizeof(ID);
38
39         rc = entry_encode( e, &bv );
40 #ifdef BDB_HIER
41         e->e_dn = odn; e->e_ndn = ondn;
42 #endif
43         if( rc != LDAP_SUCCESS ) {
44                 return -1;
45         }
46
47         DBTzero( &data );
48         bv2DBT( &bv, &data );
49
50         rc = db->put( db, tid, &key, &data, flag );
51
52         free( bv.bv_val );
53         return rc;
54 }
55
56 /*
57  * This routine adds (or updates) an entry on disk.
58  * The cache should be already be updated.
59  */
60
61
62 int bdb_id2entry_add(
63         BackendDB *be,
64         DB_TXN *tid,
65         Entry *e )
66 {
67         return bdb_id2entry_put(be, tid, e, DB_NOOVERWRITE);
68 }
69
70 int bdb_id2entry_update(
71         BackendDB *be,
72         DB_TXN *tid,
73         Entry *e )
74 {
75         return bdb_id2entry_put(be, tid, e, 0);
76 }
77
78 int bdb_id2entry_rw(
79         BackendDB *be,
80         DB_TXN *tid,
81         ID id,
82         Entry **e,
83         int rw,
84         u_int32_t locker,
85         DB_LOCK *lock )
86 {
87         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
88         DB *db = bdb->bi_id2entry->bdi_db;
89         DBT key, data;
90         struct berval bv;
91         int rc = 0, ret = 0;
92
93         *e = NULL;
94
95         DBTzero( &key );
96         key.data = (char *) &id;
97         key.size = sizeof(ID);
98
99         DBTzero( &data );
100         data.flags = DB_DBT_MALLOC;
101
102         if ((*e = bdb_cache_find_entry_id(bdb->bi_dbenv, &bdb->bi_cache, id, rw, locker, lock)) != NULL) {
103                 return 0;
104         }
105
106         /* fetch it */
107         rc = db->get( db, tid, &key, &data, bdb->bi_db_opflags );
108
109         if( rc != 0 ) {
110                 return rc;
111         }
112
113         DBT2bv( &data, &bv );
114
115         rc = entry_decode( &bv, e );
116
117         if( rc == 0 ) {
118                 (*e)->e_id = id;
119         } else {
120                 /* only free on error. On success, the entry was
121                  * decoded in place.
122                  */
123                 ch_free( data.data );
124         }
125
126         if ( rc == 0 ) {
127                 ret = bdb_cache_add_entry_rw( bdb->bi_dbenv,
128                                 &bdb->bi_cache, *e, rw, locker, lock);
129                 while ( ret == 1 || ret == -1 ) {
130                         Entry *ee;
131                         int add_loop_cnt = 0;
132                         if ( (*e)->e_private != NULL ) {
133                                 free ((*e)->e_private);
134                         }
135                         (*e)->e_private = NULL;
136                         if ( (ee = bdb_cache_find_entry_id
137                                         (bdb->bi_dbenv, &bdb->bi_cache, id, rw, locker, lock) ) != NULL) {
138                                 bdb_entry_return ( *e );
139                                 *e = ee;
140                                 return 0;
141                         }
142                         if ( ++add_loop_cnt == BDB_MAX_ADD_LOOP ) {
143                                 bdb_entry_return ( *e );
144                                 *e = NULL;
145                                 return LDAP_BUSY;
146                         }
147                 }
148                 if ( ret != 0 ) {
149                         if ( (*e)->e_private != NULL )
150                                 free ( (*e)->e_private );
151                         bdb_entry_return( *e );
152                         *e = NULL;
153                         ch_free( data.data );
154                 }
155                 rc = ret;
156         }
157
158 #ifdef BDB_HIER
159         bdb_fix_dn(be, id, *e);
160 #endif
161
162         if (rc == 0) {
163                 bdb_cache_entry_commit(*e);
164         }
165
166         return rc;
167 }
168
169 int bdb_id2entry_delete(
170         BackendDB *be,
171         DB_TXN *tid,
172         Entry *e )
173 {
174         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
175         DB *db = bdb->bi_id2entry->bdi_db;
176         DBT key;
177         int rc;
178
179         bdb_cache_delete_entry(&bdb->bi_cache, e);
180
181         DBTzero( &key );
182         key.data = (char *) &e->e_id;
183         key.size = sizeof(ID);
184
185         /* delete from database */
186         rc = db->del( db, tid, &key, 0 );
187
188         return rc;
189 }
190
191 int bdb_entry_return(
192         Entry *e )
193 {
194         /* Our entries are allocated in two blocks; the data comes from
195          * the db itself and the Entry structure and associated pointers
196          * are allocated in entry_decode. The db data pointer is saved
197          * in e_bv. Since the Entry structure is allocated as a single
198          * block, e_attrs is always a fixed offset from e. The exception
199          * is when an entry has been modified, in which case we also need
200          * to free e_attrs.
201          */
202         if( !e->e_bv.bv_val ) { /* A regular entry, from do_add */
203                 entry_free( e );
204                 return 0;
205         }
206         if( (void *) e->e_attrs != (void *) (e+1)) {
207                 attrs_free( e->e_attrs );
208         }
209
210 #ifndef BDB_HIER
211         /* See if the DNs were changed by modrdn */
212         if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
213                 e->e_bv.bv_val + e->e_bv.bv_len ) {
214                 ch_free(e->e_name.bv_val);
215                 ch_free(e->e_nname.bv_val);
216                 e->e_name.bv_val = NULL;
217                 e->e_nname.bv_val = NULL;
218         }
219 #else
220         /* We had to construct the dn and ndn as well, in a single block */
221         if( e->e_name.bv_val ) {
222                 free( e->e_name.bv_val );
223         }
224 #endif
225         /* In tool mode the e_bv buffer is realloc'd, leave it alone */
226         if( !(slapMode & SLAP_TOOL_MODE) ) {
227                 free( e->e_bv.bv_val );
228         }
229
230         free( e );
231
232         return 0;
233 }
234
235 int bdb_entry_release(
236         BackendDB *be,
237         Connection *c,
238         Operation *o,
239         Entry *e,
240         int rw )
241 {
242         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
243  
244         /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
245                         SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
246  
247         if ( slapMode == SLAP_SERVER_MODE ) {
248                 /* free entry and reader or writer lock */
249                 bdb_unlocked_cache_return_entry_rw( &bdb->bi_cache, e, rw );
250         } else {
251                 if (e->e_private != NULL)
252                         free (e->e_private);
253                 e->e_private = NULL;
254                 bdb_entry_return ( e );
255         }
256  
257         return 0;
258 }