]> git.sur5r.net Git - openldap/blob - servers/slapd/back-mdb/id2entry.c
Fix search referral on base
[openldap] / servers / slapd / back-mdb / 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-2011 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-mdb.h"
24
25 static int mdb_id2entry_put(
26         Operation *op,
27         MDB_txn *tid,
28         Entry *e,
29         int flag )
30 {
31         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
32         MDB_dbi dbi = mdb->mi_id2entry;
33         MDB_val key, data;
34         struct berval bv;
35         int rc;
36         struct berval odn, ondn;
37
38         /* We only store rdns, and they go in the dn2id database. */
39
40         odn = e->e_name; ondn = e->e_nname;
41
42         e->e_name = slap_empty_bv;
43         e->e_nname = slap_empty_bv;
44
45         key.mv_data = &e->e_id;
46         key.mv_size = sizeof(ID);
47
48         rc = entry_encode( e, &bv );
49         e->e_name = odn; e->e_nname = ondn;
50         if( rc != LDAP_SUCCESS ) {
51                 return -1;
52         }
53
54         data.mv_size = bv.bv_len;
55         data.mv_data = bv.bv_val;
56
57         rc = mdb_put( tid, dbi, &key, &data, flag );
58
59         op->o_tmpfree( bv.bv_val, op->o_tmpmemctx );
60         return rc;
61 }
62
63 /*
64  * This routine adds (or updates) an entry on disk.
65  * The cache should be already be updated.
66  */
67
68
69 int mdb_id2entry_add(
70         Operation *op,
71         MDB_txn *tid,
72         Entry *e )
73 {
74         return mdb_id2entry_put(op, tid, e, MDB_NOOVERWRITE);
75 }
76
77 int mdb_id2entry_update(
78         Operation *op,
79         MDB_txn *tid,
80         Entry *e )
81 {
82         return mdb_id2entry_put(op, tid, e, 0);
83 }
84
85 int mdb_id2entry(
86         Operation *op,
87         MDB_txn *tid,
88         ID id,
89         Entry **e )
90 {
91         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
92         MDB_dbi dbi = mdb->mi_id2entry;
93         MDB_val key, data;
94         EntryHeader eh;
95         int rc = 0;
96
97         *e = NULL;
98
99         key.mv_data = &id;
100         key.mv_size = sizeof(ID);
101
102         /* fetch it */
103         rc = mdb_get( tid, dbi, &key, &data );
104         if ( rc ) return rc;
105
106         eh.bv.bv_val = data.mv_data;
107         eh.bv.bv_len = data.mv_size;
108         rc = entry_header( &eh );
109         if ( rc ) return rc;
110
111         eh.bv.bv_len = eh.nvals * sizeof( struct berval );
112         eh.bv.bv_val = ch_malloc( eh.bv.bv_len );
113         rc = entry_decode(&eh, e);
114
115         if( rc == 0 ) {
116                 (*e)->e_id = id;
117                 (*e)->e_bv = eh.bv;
118                 (*e)->e_name.bv_val = NULL;
119                 (*e)->e_nname.bv_val = NULL;
120         } else {
121                 ch_free( eh.bv.bv_val );
122         }
123
124         return rc;
125 }
126
127 int mdb_id2entry_delete(
128         BackendDB *be,
129         MDB_txn *tid,
130         Entry *e )
131 {
132         struct mdb_info *mdb = (struct mdb_info *) be->be_private;
133         MDB_dbi dbi = mdb->mi_id2entry;
134         MDB_val key;
135         int rc;
136
137         key.mv_data = &e->e_id;
138         key.mv_size = sizeof(ID);
139
140         /* delete from database */
141         rc = mdb_del( tid, dbi, &key, NULL, 0 );
142
143         return rc;
144 }
145
146 int mdb_entry_return(
147         Entry *e
148 )
149 {
150         entry_free( e );
151         return 0;
152 }
153
154 int mdb_entry_release(
155         Operation *op,
156         Entry *e,
157         int rw )
158 {
159         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
160         struct mdb_op_info *moi = NULL;
161         int rc;
162  
163         /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
164                         SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
165  
166         mdb_entry_return ( e );
167         if ( slapMode == SLAP_SERVER_MODE ) {
168                 OpExtra *oex;
169                 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
170                         if ( oex->oe_key == mdb ) {
171                                 moi = (mdb_op_info *)oex;
172                                 /* If it was setup by entry_get we should probably free it */
173                                 if ( moi->moi_flag & MOI_FREEIT ) {
174                                         moi->moi_ref--;
175                                         if ( moi->moi_ref < 1 ) {
176                                                 mdb_txn_reset( moi->moi_txn );
177                                                 moi->moi_ref = 0;
178                                                 LDAP_SLIST_REMOVE( &op->o_extra, &moi->moi_oe, OpExtra, oe_next );
179                                                 op->o_tmpfree( moi, op->o_tmpmemctx );
180                                         }
181                                 }
182                                 break;
183                         }
184                 }
185         }
186  
187         return 0;
188 }
189
190 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
191  */
192 int mdb_entry_get(
193         Operation *op,
194         struct berval *ndn,
195         ObjectClass *oc,
196         AttributeDescription *at,
197         int rw,
198         Entry **ent )
199 {
200         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
201         struct mdb_op_info *moi = NULL;
202         MDB_txn *txn = NULL;
203         Entry *e = NULL;
204         int     rc;
205         const char *at_name = at ? at->ad_cname.bv_val : "(null)";
206
207         Debug( LDAP_DEBUG_ARGS,
208                 "=> mdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 ); 
209         Debug( LDAP_DEBUG_ARGS,
210                 "=> mdb_entry_get: oc: \"%s\", at: \"%s\"\n",
211                 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
212
213         rc = mdb_opinfo_get( op, mdb, rw == 0, &moi );
214         if ( rc )
215                 return LDAP_OTHER;
216         txn = moi->moi_txn;
217
218         /* can we find entry */
219         rc = mdb_dn2entry( op, txn, ndn, &e, 0 );
220         switch( rc ) {
221         case MDB_NOTFOUND:
222         case 0:
223                 break;
224         default:
225                 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
226         }
227         if (e == NULL) {
228                 Debug( LDAP_DEBUG_ACL,
229                         "=> mdb_entry_get: cannot find entry: \"%s\"\n",
230                                 ndn->bv_val, 0, 0 ); 
231                 return LDAP_NO_SUCH_OBJECT; 
232         }
233         
234         Debug( LDAP_DEBUG_ACL,
235                 "=> mdb_entry_get: found entry: \"%s\"\n",
236                 ndn->bv_val, 0, 0 ); 
237
238         if ( oc && !is_entry_objectclass( e, oc, 0 )) {
239                 Debug( LDAP_DEBUG_ACL,
240                         "<= mdb_entry_get: failed to find objectClass %s\n",
241                         oc->soc_cname.bv_val, 0, 0 ); 
242                 rc = LDAP_NO_SUCH_ATTRIBUTE;
243                 goto return_results;
244         }
245
246         /* NOTE: attr_find() or attrs_find()? */
247         if ( at && attr_find( e->e_attrs, at ) == NULL ) {
248                 Debug( LDAP_DEBUG_ACL,
249                         "<= mdb_entry_get: failed to find attribute %s\n",
250                         at->ad_cname.bv_val, 0, 0 ); 
251                 rc = LDAP_NO_SUCH_ATTRIBUTE;
252                 goto return_results;
253         }
254
255 return_results:
256         if( rc != LDAP_SUCCESS ) {
257                 /* free entry */
258                 mdb_entry_return( e );
259
260         } else {
261                 *ent = entry_dup( e );
262         }
263
264         Debug( LDAP_DEBUG_TRACE,
265                 "mdb_entry_get: rc=%d\n",
266                 rc, 0, 0 ); 
267         return(rc);
268 }
269
270 static void
271 mdb_reader_free( void *key, void *data )
272 {
273         MDB_txn *txn = data;
274
275         if ( txn ) mdb_txn_abort( txn );
276 }
277
278 /* free up any keys used by the main thread */
279 void
280 mdb_reader_flush( MDB_env *env )
281 {
282         void *data;
283         void *ctx = ldap_pvt_thread_pool_context();
284
285         if ( !ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
286                 ldap_pvt_thread_pool_setkey( ctx, env, NULL, 0, NULL, NULL );
287                 mdb_reader_free( env, data );
288         }
289 }
290
291 int
292 mdb_opinfo_get( Operation *op, struct mdb_info *mdb, int rdonly, mdb_op_info **moip )
293 {
294         int rc;
295         void *data;
296         void *ctx;
297         mdb_op_info *moi = NULL;
298         OpExtra *oex;
299
300         assert( op != NULL );
301
302         if ( !mdb || !moip ) return -1;
303
304         /* If no op was provided, try to find the ctx anyway... */
305         if ( op ) {
306                 ctx = op->o_threadctx;
307         } else {
308                 ctx = ldap_pvt_thread_pool_context();
309         }
310
311         if ( op ) {
312                 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
313                         if ( oex->oe_key == mdb ) break;
314                 }
315                 moi = (mdb_op_info *)oex;
316         }
317
318         if ( !moi ) {
319                 moi = *moip;
320
321                 if ( !moi ) {
322                         if ( op ) {
323                                 moi = op->o_tmpalloc(sizeof(struct mdb_op_info),op->o_tmpmemctx);
324                         } else {
325                                 moi = ch_malloc(sizeof(mdb_op_info));
326                         }
327                         moi->moi_flag = MOI_FREEIT;
328                         *moip = moi;
329                 }
330                 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &moi->moi_oe, oe_next );
331                 moi->moi_oe.oe_key = mdb;
332                 moi->moi_ref = 0;
333                 moi->moi_txn = NULL;
334         }
335
336         if ( !rdonly ) {
337                 /* This op started as a reader, but now wants to write. */
338                 if ( moi->moi_flag & MOI_READER ) {
339                         moi = *moip;
340                         LDAP_SLIST_INSERT_HEAD( &op->o_extra, &moi->moi_oe, oe_next );
341                 } else {
342                 /* This op is continuing an existing write txn */
343                         *moip = moi;
344                 }
345                 moi->moi_ref++;
346                 if ( !moi->moi_txn ) {
347                         rc = mdb_txn_begin( mdb->mi_dbenv, 0, &moi->moi_txn );
348                         if (rc) {
349                                 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
350                                         mdb_strerror(rc), rc, 0 );
351                         }
352                         return rc;
353                 }
354                 return 0;
355         }
356
357         /* OK, this is a reader */
358         if ( !moi->moi_txn ) {
359                 if ( !ctx ) {
360                         /* Shouldn't happen unless we're single-threaded */
361                         rc = mdb_txn_begin( mdb->mi_dbenv, 1, &moi->moi_txn );
362                         if (rc) {
363                                 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
364                                         mdb_strerror(rc), rc, 0 );
365                         }
366                         return rc;
367                 }
368                 if ( ldap_pvt_thread_pool_getkey( ctx, mdb->mi_dbenv, &data, NULL ) ) {
369                         rc = mdb_txn_begin( mdb->mi_dbenv, 1, &moi->moi_txn );
370                         if (rc) {
371                                 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
372                                         mdb_strerror(rc), rc, 0 );
373                                 return rc;
374                         }
375                         data = moi->moi_txn;
376                         if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, mdb->mi_dbenv,
377                                 data, mdb_reader_free, NULL, NULL ) ) ) {
378                                 mdb_txn_abort( moi->moi_txn );
379                                 moi->moi_txn = NULL;
380                                 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: thread_pool_setkey failed err (%d)\n",
381                                         rc, 0, 0 );
382                                 return rc;
383                         }
384                 } else {
385                         moi->moi_txn = data;
386                 }
387                 moi->moi_flag |= MOI_READER;
388         }
389         if ( moi->moi_ref < 1 ) {
390                 moi->moi_ref = 0;
391                 mdb_txn_renew( moi->moi_txn );
392         }
393         moi->moi_ref++;
394         if ( !*moip != moi )
395                 *moip = moi;
396
397         return 0;
398 }