]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/id2entry.c
return structuralObjectClass errors
[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-2006 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
22 #include "back-bdb.h"
23
24 static int bdb_id2entry_put(
25         BackendDB *be,
26         DB_TXN *tid,
27         Entry *e,
28         int flag )
29 {
30         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
31         DB *db = bdb->bi_id2entry->bdi_db;
32         DBT key, data;
33         struct berval bv;
34         int rc;
35         ID nid;
36 #ifdef BDB_HIER
37         struct berval odn, ondn;
38
39         /* We only store rdns, and they go in the dn2id database. */
40
41         odn = e->e_name; ondn = e->e_nname;
42
43         e->e_name = slap_empty_bv;
44         e->e_nname = slap_empty_bv;
45 #endif
46         DBTzero( &key );
47
48         /* Store ID in BigEndian format */
49         key.data = &nid;
50         key.size = sizeof(ID);
51         BDB_ID2DISK( e->e_id, &nid );
52
53         rc = entry_encode( e, &bv );
54 #ifdef BDB_HIER
55         e->e_name = odn; e->e_nname = ondn;
56 #endif
57         if( rc != LDAP_SUCCESS ) {
58                 return -1;
59         }
60
61         DBTzero( &data );
62         bv2DBT( &bv, &data );
63
64         rc = db->put( db, tid, &key, &data, flag );
65
66         free( bv.bv_val );
67         return rc;
68 }
69
70 /*
71  * This routine adds (or updates) an entry on disk.
72  * The cache should be already be updated.
73  */
74
75
76 int bdb_id2entry_add(
77         BackendDB *be,
78         DB_TXN *tid,
79         Entry *e )
80 {
81         return bdb_id2entry_put(be, tid, e, DB_NOOVERWRITE);
82 }
83
84 int bdb_id2entry_update(
85         BackendDB *be,
86         DB_TXN *tid,
87         Entry *e )
88 {
89         return bdb_id2entry_put(be, tid, e, 0);
90 }
91
92 int bdb_id2entry(
93         BackendDB *be,
94         DB_TXN *tid,
95         u_int32_t locker,
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         struct berval bv;
104         int rc = 0;
105         ID nid;
106
107         *e = NULL;
108
109         DBTzero( &key );
110         key.data = &nid;
111         key.size = sizeof(ID);
112         BDB_ID2DISK( id, &nid );
113
114         DBTzero( &data );
115         data.flags = DB_DBT_MALLOC;
116
117         /* fetch it */
118         rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
119         if ( rc ) return rc;
120
121         /* Use our own locker if needed */
122         if ( !tid && locker )
123                 cursor->locker = locker;
124
125         rc = cursor->c_get( cursor, &key, &data, DB_SET );
126         cursor->c_close( cursor );
127
128         if( rc != 0 ) {
129                 return rc;
130         }
131
132         DBT2bv( &data, &bv );
133
134 #ifdef SLAP_ZONE_ALLOC
135         rc = entry_decode(&bv, e, bdb->bi_cache.c_zctx);
136 #else
137         rc = entry_decode(&bv, e);
138 #endif
139
140         if( rc == 0 ) {
141                 (*e)->e_id = id;
142         } else {
143                 /* only free on error. On success, the entry was
144                  * decoded in place.
145                  */
146 #ifndef SLAP_ZONE_ALLOC
147                 ch_free(data.data);
148 #endif
149         }
150 #ifdef SLAP_ZONE_ALLOC
151         ch_free(data.data);
152 #endif
153
154         return rc;
155 }
156
157 int bdb_id2entry_delete(
158         BackendDB *be,
159         DB_TXN *tid,
160         Entry *e )
161 {
162         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
163         DB *db = bdb->bi_id2entry->bdi_db;
164         DBT key;
165         int rc;
166         ID nid;
167
168         DBTzero( &key );
169         key.data = &nid;
170         key.size = sizeof(ID);
171         BDB_ID2DISK( e->e_id, &nid );
172
173         /* delete from database */
174         rc = db->del( db, tid, &key, 0 );
175
176         return rc;
177 }
178
179 int bdb_entry_return(
180         Entry *e
181 )
182 {
183         /* Our entries are allocated in two blocks; the data comes from
184          * the db itself and the Entry structure and associated pointers
185          * are allocated in entry_decode. The db data pointer is saved
186          * in e_bv.
187          */
188         if ( e->e_bv.bv_val ) {
189                 /* See if the DNs were changed by modrdn */
190                 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
191                         e->e_bv.bv_val + e->e_bv.bv_len ) {
192                         ch_free(e->e_name.bv_val);
193                         ch_free(e->e_nname.bv_val);
194                 }
195                 e->e_name.bv_val = NULL;
196                 e->e_nname.bv_val = NULL;
197                 /* In tool mode the e_bv buffer is realloc'd, leave it alone */
198                 if( !(slapMode & SLAP_TOOL_MODE) ) {
199                         free( e->e_bv.bv_val );
200                 }
201                 BER_BVZERO( &e->e_bv );
202         }
203         entry_free( e );
204         return 0;
205 }
206
207 int bdb_entry_release(
208         Operation *op,
209         Entry *e,
210         int rw )
211 {
212         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
213         struct bdb_op_info *boi = NULL;
214  
215         /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
216                         SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
217  
218         if ( slapMode == SLAP_SERVER_MODE ) {
219                 /* If not in our cache, just free it */
220                 if ( !e->e_private ) {
221 #ifdef SLAP_ZONE_ALLOC
222                         return bdb_entry_return( bdb, e, -1 );
223 #else
224                         return bdb_entry_return( e );
225 #endif
226                 }
227                 /* free entry and reader or writer lock */
228                 boi = (struct bdb_op_info *)op->o_private;
229
230                 /* lock is freed with txn */
231                 if ( !boi || boi->boi_txn ) {
232                         bdb_unlocked_cache_return_entry_rw( &bdb->bi_cache, e, rw );
233                 } else {
234                         struct bdb_lock_info *bli, *prev;
235                         for ( prev=(struct bdb_lock_info *)&boi->boi_locks,
236                                 bli = boi->boi_locks; bli; prev=bli, bli=bli->bli_next ) {
237                                 if ( bli->bli_id == e->e_id ) {
238                                         bdb_cache_return_entry_rw( bdb->bi_dbenv, &bdb->bi_cache,
239                                                 e, rw, &bli->bli_lock );
240                                         prev->bli_next = bli->bli_next;
241                                         op->o_tmpfree( bli, op->o_tmpmemctx );
242                                         break;
243                                 }
244                         }
245                         if ( !boi->boi_locks ) {
246                                 op->o_tmpfree( boi, op->o_tmpmemctx );
247                                 op->o_private = NULL;
248                         }
249                 }
250         } else {
251 #ifdef SLAP_ZONE_ALLOC
252                 int zseq = -1;
253                 if (e->e_private != NULL) {
254                         BEI(e)->bei_e = NULL;
255                         zseq = BEI(e)->bei_zseq;
256                 }
257 #else
258                 if (e->e_private != NULL)
259                         BEI(e)->bei_e = NULL;
260 #endif
261                 e->e_private = NULL;
262 #ifdef SLAP_ZONE_ALLOC
263                 bdb_entry_return ( bdb, e, zseq );
264 #else
265                 bdb_entry_return ( e );
266 #endif
267         }
268  
269         return 0;
270 }
271
272 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
273  */
274 int bdb_entry_get(
275         Operation *op,
276         struct berval *ndn,
277         ObjectClass *oc,
278         AttributeDescription *at,
279         int rw,
280         Entry **ent )
281 {
282         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
283         struct bdb_op_info *boi = NULL;
284         DB_TXN *txn = NULL;
285         Entry *e = NULL;
286         EntryInfo *ei;
287         int     rc;
288         const char *at_name = at ? at->ad_cname.bv_val : "(null)";
289
290         u_int32_t       locker = 0;
291         DB_LOCK         lock;
292         int             free_lock_id = 0;
293
294         Debug( LDAP_DEBUG_ARGS,
295                 "=> bdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 ); 
296         Debug( LDAP_DEBUG_ARGS,
297                 "=> bdb_entry_get: oc: \"%s\", at: \"%s\"\n",
298                 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
299
300         if( op ) boi = (struct bdb_op_info *) op->o_private;
301         if( boi != NULL && op->o_bd->be_private == boi->boi_bdb->be_private ) {
302                 txn = boi->boi_txn;
303                 locker = boi->boi_locker;
304         }
305
306         if ( txn != NULL ) {
307                 locker = TXN_ID ( txn );
308         } else if ( !locker ) {
309                 rc = LOCK_ID ( bdb->bi_dbenv, &locker );
310                 free_lock_id = 1;
311                 switch(rc) {
312                 case 0:
313                         break;
314                 default:
315                         return LDAP_OTHER;
316                 }
317         }
318
319 dn2entry_retry:
320         /* can we find entry */
321         rc = bdb_dn2entry( op, txn, ndn, &ei, 0, locker, &lock );
322         switch( rc ) {
323         case DB_NOTFOUND:
324         case 0:
325                 break;
326         case DB_LOCK_DEADLOCK:
327         case DB_LOCK_NOTGRANTED:
328                 /* the txn must abort and retry */
329                 if ( txn ) {
330                         boi->boi_err = rc;
331                         return LDAP_BUSY;
332                 }
333                 ldap_pvt_thread_yield();
334                 goto dn2entry_retry;
335         default:
336                 if ( boi ) boi->boi_err = rc;
337                 if ( free_lock_id ) {
338                         LOCK_ID_FREE( bdb->bi_dbenv, locker );
339                 }
340                 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
341         }
342         if (ei) e = ei->bei_e;
343         if (e == NULL) {
344                 Debug( LDAP_DEBUG_ACL,
345                         "=> bdb_entry_get: cannot find entry: \"%s\"\n",
346                                 ndn->bv_val, 0, 0 ); 
347                 if ( free_lock_id ) {
348                         LOCK_ID_FREE( bdb->bi_dbenv, locker );
349                 }
350                 return LDAP_NO_SUCH_OBJECT; 
351         }
352         
353         Debug( LDAP_DEBUG_ACL,
354                 "=> bdb_entry_get: found entry: \"%s\"\n",
355                 ndn->bv_val, 0, 0 ); 
356
357         /* find attribute values */
358         if( is_entry_alias( e ) ) {
359                 Debug( LDAP_DEBUG_ACL,
360                         "<= bdb_entry_get: entry is an alias\n", 0, 0, 0 );
361                 rc = LDAP_ALIAS_PROBLEM;
362                 goto return_results;
363         }
364
365         if( is_entry_referral( e ) ) {
366                 Debug( LDAP_DEBUG_ACL,
367                         "<= bdb_entry_get: entry is a referral\n", 0, 0, 0 );
368                 rc = LDAP_REFERRAL;
369                 goto return_results;
370         }
371
372         if ( oc && !is_entry_objectclass( e, oc, 0 )) {
373                 Debug( LDAP_DEBUG_ACL,
374                         "<= bdb_entry_get: failed to find objectClass %s\n",
375                         oc->soc_cname.bv_val, 0, 0 ); 
376                 rc = LDAP_NO_SUCH_ATTRIBUTE;
377                 goto return_results;
378         }
379
380 return_results:
381         if( rc != LDAP_SUCCESS ) {
382                 /* free entry */
383                 bdb_cache_return_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, rw, &lock);
384
385         } else {
386                 if ( slapMode == SLAP_SERVER_MODE ) {
387                         *ent = e;
388                         /* big drag. we need a place to store a read lock so we can
389                          * release it later?? If we're in a txn, nothing is needed
390                          * here because the locks will go away with the txn.
391                          */
392                         if ( op ) {
393                                 if ( !boi ) {
394                                         boi = op->o_tmpcalloc(1,sizeof(struct bdb_op_info),op->o_tmpmemctx);
395                                         boi->boi_bdb = op->o_bd;
396                                         op->o_private = boi;
397                                 }
398                                 if ( !boi->boi_txn ) {
399                                         struct bdb_lock_info *bli;
400                                         bli = op->o_tmpalloc( sizeof(struct bdb_lock_info),
401                                                 op->o_tmpmemctx );
402                                         bli->bli_next = boi->boi_locks;
403                                         bli->bli_id = e->e_id;
404                                         bli->bli_lock = lock;
405                                         boi->boi_locks = bli;
406                                 }
407                         }
408                 } else {
409                         *ent = entry_dup( e );
410                         bdb_cache_return_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, rw, &lock);
411                 }
412         }
413
414         if ( free_lock_id ) {
415                 LOCK_ID_FREE( bdb->bi_dbenv, locker );
416         }
417
418         Debug( LDAP_DEBUG_TRACE,
419                 "bdb_entry_get: rc=%d\n",
420                 rc, 0, 0 ); 
421         return(rc);
422 }