]> git.sur5r.net Git - openldap/blob - servers/slapd/back-mdb/id2entry.c
Getting basic functions compiling
[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->mdi_dbi;
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( op->o_tmpmemctx, bv.bv_val );
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->mdi_dbi;
93         MDB_val key, data;
94         EntryHeader eh;
95         char buf[16];
96         int rc = 0, off;
97
98         *e = NULL;
99
100         key.mv_data = &id;
101         key.mv_size = sizeof(ID);
102
103         /* fetch it */
104         rc = mdb_get( tid, dbi, &key, &data );
105         if ( rc ) return rc;
106
107         eh.bv.bv_val = data.mv_data;
108         eh.bv.bv_len = data.mv_size;
109         rc = entry_header( &eh );
110         if ( rc ) return rc;
111
112         rc = entry_decode(&eh, e);
113
114         if( rc == 0 ) {
115                 (*e)->e_id = id;
116         }
117
118         return rc;
119 }
120
121 int mdb_id2entry_delete(
122         BackendDB *be,
123         MDB_txn *tid,
124         Entry *e )
125 {
126         struct mdb_info *mdb = (struct mdb_info *) be->be_private;
127         MDB_dbi dbi = mdb->mi_id2entry->mdi_dbi;
128         MDB_val key;
129         int rc;
130
131         key.mv_data = &e->e_id;
132         key.mv_size = sizeof(ID);
133
134         /* delete from database */
135         rc = mdb_del( tid, dbi, &key, NULL, 0 );
136
137         return rc;
138 }
139
140 int mdb_entry_return(
141         Entry *e
142 )
143 {
144         /* Our entries are allocated in two blocks; the data comes from
145          * the db itself and the Entry structure and associated pointers
146          * are allocated in entry_decode. The db data pointer is saved
147          * in e_bv.
148          */
149         if ( e->e_bv.bv_val ) {
150                 /* See if the DNs were changed by modrdn */
151                 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
152                         e->e_bv.bv_val + e->e_bv.bv_len ) {
153                         ch_free(e->e_name.bv_val);
154                         ch_free(e->e_nname.bv_val);
155                 }
156                 e->e_name.bv_val = NULL;
157                 e->e_nname.bv_val = NULL;
158                 /* In tool mode the e_bv buffer is realloc'd, leave it alone */
159                 if( !(slapMode & SLAP_TOOL_MODE) ) {
160                         free( e->e_bv.bv_val );
161                 }
162                 BER_BVZERO( &e->e_bv );
163         }
164         entry_free( e );
165         return 0;
166 }
167
168 int mdb_entry_release(
169         Operation *op,
170         Entry *e,
171         int rw )
172 {
173 #if 0
174         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
175         struct mdb_op_info *moi;
176         OpExtra *oex;
177  
178         /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
179                         SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
180  
181         if ( slapMode == SLAP_SERVER_MODE ) {
182                 /* If not in our cache, just free it */
183                 if ( !e->e_private ) {
184                         return mdb_entry_return( e );
185                 }
186                 /* free entry and reader or writer lock */
187                 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
188                         if ( oex->oe_key == mdb ) break;
189                 }
190                 moi = (struct mdb_op_info *)oex;
191
192                 /* lock is freed with txn */
193                 if ( !moi || moi->moi_txn ) {
194                         mdb_unlocked_cache_return_entry_rw( mdb, e, rw );
195                 } else {
196                         struct mdb_lock_info *bli, *prev;
197                         for ( prev=(struct mdb_lock_info *)&moi->boi_locks,
198                                 bli = boi->boi_locks; bli; prev=bli, bli=bli->bli_next ) {
199                                 if ( bli->bli_id == e->e_id ) {
200                                         mdb_cache_return_entry_rw( mdb, e, rw, &bli->bli_lock );
201                                         prev->bli_next = bli->bli_next;
202                                         /* Cleanup, or let caller know we unlocked */
203                                         if ( bli->bli_flag & BLI_DONTFREE )
204                                                 bli->bli_flag = 0;
205                                         else
206                                                 op->o_tmpfree( bli, op->o_tmpmemctx );
207                                         break;
208                                 }
209                         }
210                         if ( !boi->boi_locks ) {
211                                 LDAP_SLIST_REMOVE( &op->o_extra, &boi->boi_oe, OpExtra, oe_next );
212                                 if ( !(boi->boi_flag & BOI_DONTFREE))
213                                         op->o_tmpfree( boi, op->o_tmpmemctx );
214                         }
215                 }
216         } else {
217                 if (e->e_private != NULL)
218                         BEI(e)->bei_e = NULL;
219                 e->e_private = NULL;
220                 mdb_entry_return ( e );
221         }
222  
223         return 0;
224 #else
225         return mdb_entry_return( e );
226 #endif
227 }
228
229 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
230  */
231 int mdb_entry_get(
232         Operation *op,
233         struct berval *ndn,
234         ObjectClass *oc,
235         AttributeDescription *at,
236         int rw,
237         Entry **ent )
238 {
239 #if 0
240         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
241         struct mdb_op_info *boi = NULL;
242         MDB_txn *txn = NULL;
243         Entry *e = NULL;
244         EntryInfo *ei;
245         int     rc;
246         const char *at_name = at ? at->ad_cname.bv_val : "(null)";
247
248         Debug( LDAP_DEBUG_ARGS,
249                 "=> mdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 ); 
250         Debug( LDAP_DEBUG_ARGS,
251                 "=> mdb_entry_get: oc: \"%s\", at: \"%s\"\n",
252                 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
253
254         if( op ) {
255                 OpExtra *oex;
256                 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
257                         if ( oex->oe_key == mdb ) break;
258                 }
259                 boi = (struct mdb_op_info *)oex;
260                 if ( boi )
261                         txn = boi->boi_txn;
262         }
263
264         if ( !txn ) {
265                 rc = mdb_reader_get( op, mdb->bi_dbenv, &txn );
266                 switch(rc) {
267                 case 0:
268                         break;
269                 default:
270                         return LDAP_OTHER;
271                 }
272         }
273
274 dn2entry_retry:
275         /* can we find entry */
276         rc = mdb_dn2entry( op, txn, ndn, &ei, 0, &lock );
277         switch( rc ) {
278         case MDB_NOTFOUND:
279         case 0:
280                 break;
281         default:
282                 if ( boi ) boi->boi_err = rc;
283                 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
284         }
285         if (ei) e = ei->bei_e;
286         if (e == NULL) {
287                 Debug( LDAP_DEBUG_ACL,
288                         "=> mdb_entry_get: cannot find entry: \"%s\"\n",
289                                 ndn->bv_val, 0, 0 ); 
290                 return LDAP_NO_SUCH_OBJECT; 
291         }
292         
293         Debug( LDAP_DEBUG_ACL,
294                 "=> mdb_entry_get: found entry: \"%s\"\n",
295                 ndn->bv_val, 0, 0 ); 
296
297         if ( oc && !is_entry_objectclass( e, oc, 0 )) {
298                 Debug( LDAP_DEBUG_ACL,
299                         "<= mdb_entry_get: failed to find objectClass %s\n",
300                         oc->soc_cname.bv_val, 0, 0 ); 
301                 rc = LDAP_NO_SUCH_ATTRIBUTE;
302                 goto return_results;
303         }
304
305         /* NOTE: attr_find() or attrs_find()? */
306         if ( at && attr_find( e->e_attrs, at ) == NULL ) {
307                 Debug( LDAP_DEBUG_ACL,
308                         "<= mdb_entry_get: failed to find attribute %s\n",
309                         at->ad_cname.bv_val, 0, 0 ); 
310                 rc = LDAP_NO_SUCH_ATTRIBUTE;
311                 goto return_results;
312         }
313
314 return_results:
315         if( rc != LDAP_SUCCESS ) {
316                 /* free entry */
317                 mdb_cache_return_entry_rw(mdb, e, rw, &lock);
318
319         } else {
320                 if ( slapMode == SLAP_SERVER_MODE ) {
321                         *ent = e;
322                         /* big drag. we need a place to store a read lock so we can
323                          * release it later?? If we're in a txn, nothing is needed
324                          * here because the locks will go away with the txn.
325                          */
326                         if ( op ) {
327                                 if ( !boi ) {
328                                         boi = op->o_tmpcalloc(1,sizeof(struct mdb_op_info),op->o_tmpmemctx);
329                                         boi->boi_oe.oe_key = mdb;
330                                         LDAP_SLIST_INSERT_HEAD( &op->o_extra, &boi->boi_oe, oe_next );
331                                 }
332                                 if ( !boi->boi_txn ) {
333                                         struct mdb_lock_info *bli;
334                                         bli = op->o_tmpalloc( sizeof(struct mdb_lock_info),
335                                                 op->o_tmpmemctx );
336                                         bli->bli_next = boi->boi_locks;
337                                         bli->bli_id = e->e_id;
338                                         bli->bli_flag = 0;
339                                         bli->bli_lock = lock;
340                                         boi->boi_locks = bli;
341                                 }
342                         }
343                 } else {
344                         *ent = entry_dup( e );
345                         mdb_cache_return_entry_rw(mdb, e, rw, &lock);
346                 }
347         }
348
349         Debug( LDAP_DEBUG_TRACE,
350                 "mdb_entry_get: rc=%d\n",
351                 rc, 0, 0 ); 
352         return(rc);
353 #endif
354 }