]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/delete.c
Entry caching. Based on ITS#1545 from Jong Hyuk Choi, jongchoi@us.ibm.com
[openldap] / servers / slapd / back-bdb / delete.c
1 /* delete.c - bdb backend delete routine */
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 #include "external.h"
15
16 int
17 bdb_delete(
18         BackendDB       *be,
19         Connection      *conn,
20         Operation       *op,
21         struct berval   *dn,
22         struct berval   *ndn
23 )
24 {
25         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
26         Entry   *matched;
27         struct berval   pdn = {0, NULL};
28         Entry   *e, *p = NULL;
29         int     rc;
30         const char *text;
31         int             manageDSAit = get_manageDSAit( op );
32         AttributeDescription *children = slap_schema.si_ad_children;
33         DB_TXN          *ltid = NULL;
34         struct bdb_op_info opinfo;
35 #if 0
36         u_int32_t       lockid;
37         DB_LOCK         lock;
38 #endif
39
40         Debug( LDAP_DEBUG_ARGS, "==> bdb_delete: %s\n",
41                 dn->bv_val, 0, 0 );
42
43         if( 0 ) {
44 retry:  /* transaction retry */
45                 if( e != NULL ) {
46                         bdb_cache_return_entry_w(&bdb->bi_cache, e);
47                 }
48                 Debug( LDAP_DEBUG_TRACE, "==> bdb_delete: retrying...\n",
49                         0, 0, 0 );
50                 rc = txn_abort( ltid );
51                 ltid = NULL;
52                 op->o_private = NULL;
53                 if( rc != 0 ) {
54                         rc = LDAP_OTHER;
55                         text = "internal error";
56                         goto return_results;
57                 }
58                 ldap_pvt_thread_yield();
59         }
60
61         if( bdb->bi_txn ) {
62                 /* begin transaction */
63                 rc = txn_begin( bdb->bi_dbenv, NULL, &ltid, 
64                         bdb->bi_db_opflags );
65                 text = NULL;
66                 if( rc != 0 ) {
67                         Debug( LDAP_DEBUG_TRACE,
68                                 "bdb_delete: txn_begin failed: %s (%d)\n",
69                                 db_strerror(rc), rc, 0 );
70                         rc = LDAP_OTHER;
71                         text = "internal error";
72                         goto return_results;
73                 }
74 #if 0
75                 lockid = TXN_ID( ltid );
76 #endif
77         }
78
79         opinfo.boi_bdb = be;
80         opinfo.boi_txn = ltid;
81         opinfo.boi_err = 0;
82         op->o_private = &opinfo;
83
84         if ( !be_issuffix( be, ndn->bv_val ) ) {
85                 rc = dnParent( ndn->bv_val, &pdn.bv_val );
86                 if ( rc != LDAP_SUCCESS ) {
87                         text = "internal error";
88                         goto return_results;
89                 }
90                 if (pdn.bv_val && pdn.bv_val[0]) {
91                         pdn.bv_len = ndn->bv_len - (pdn.bv_val - ndn->bv_val);
92                 }
93         }
94
95         if( pdn.bv_len != 0 ) {
96 #if 0
97                 if ( ltid ) {
98                         DBT obj;
99                         obj.data = pdn.bv_val-1;
100                         obj.size = pdn.bv_len+1;
101                         rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
102                                 DB_LOCK_WRITE, &lock);
103                 }
104 #endif
105                 /* get parent */
106                 rc = bdb_dn2entry_r( be, ltid, &pdn, &p, NULL, 0 );
107
108                 switch( rc ) {
109                 case 0:
110                 case DB_NOTFOUND:
111                         break;
112                 case DB_LOCK_DEADLOCK:
113                 case DB_LOCK_NOTGRANTED:
114                         goto retry;
115                 default:
116                         rc = LDAP_OTHER;
117                         text = "internal error";
118                         goto return_results;
119                 }
120
121                 if( p == NULL) {
122                         Debug( LDAP_DEBUG_TRACE,
123                                 "<=- bdb_delete: parent does not exist\n",
124                                 0, 0, 0);
125                         rc = LDAP_OTHER;
126                         text = "could not locate parent of entry";
127                         goto return_results;
128                 }
129
130                 /* check parent for "children" acl */
131                 rc = access_allowed( be, conn, op, p,
132                         children, NULL, ACL_WRITE );
133
134                 bdb_cache_return_entry_r(&bdb->bi_cache, p);
135                 p = NULL;
136
137                 switch( opinfo.boi_err ) {
138                 case DB_LOCK_DEADLOCK:
139                 case DB_LOCK_NOTGRANTED:
140                         goto retry;
141                 }
142
143                 if ( !rc  ) {
144                         Debug( LDAP_DEBUG_TRACE,
145                                 "<=- bdb_delete: no access to parent\n",
146                                 0, 0, 0 );
147                         rc = LDAP_INSUFFICIENT_ACCESS;
148                         goto return_results;
149                 }
150
151         } else {
152                 /* no parent, must be root to delete */
153                 if( ! be_isroot( be, &op->o_ndn ) ) {
154                         if ( be_issuffix( be, "" ) || be_isupdate( be, &op->o_ndn ) ) {
155                                 p = (Entry *)&slap_entry_root;
156
157                                 /* check parent for "children" acl */
158                                 rc = access_allowed( be, conn, op, p,
159                                         children, NULL, ACL_WRITE );
160                                 p = NULL;
161
162                                 switch( opinfo.boi_err ) {
163                                 case DB_LOCK_DEADLOCK:
164                                 case DB_LOCK_NOTGRANTED:
165                                         goto retry;
166                                 }
167
168                                 if ( !rc  ) {
169                                         Debug( LDAP_DEBUG_TRACE,
170                                                 "<=- bdb_delete: no access "
171                                                 "to parent\n", 0, 0, 0 );
172                                         rc = LDAP_INSUFFICIENT_ACCESS;
173                                         goto return_results;
174                                 }
175
176                         } else {
177                                 Debug( LDAP_DEBUG_TRACE,
178                                         "<=- bdb_delete: no parent "
179                                         "and not root\n", 0, 0, 0);
180                                 rc = LDAP_INSUFFICIENT_ACCESS;
181                                 goto return_results;
182                         }
183                 }
184
185 #if 0
186                 if ( ltid ) {
187                         DBT obj;
188                         obj.data = ",";
189                         obj.size = 1;
190                         rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
191                                 DB_LOCK_WRITE, &lock);
192                 }
193 #endif
194         }
195
196         /* get entry for read/modify/write */
197         rc = bdb_dn2entry_w( be, ltid, ndn, &e, &matched, DB_RMW );
198
199         switch( rc ) {
200         case 0:
201         case DB_NOTFOUND:
202                 break;
203         case DB_LOCK_DEADLOCK:
204         case DB_LOCK_NOTGRANTED:
205                 goto retry;
206         default:
207                 rc = LDAP_OTHER;
208                 text = "internal error";
209                 goto return_results;
210         }
211
212         if ( e == NULL ) {
213                 char *matched_dn = NULL;
214                 BerVarray refs;
215
216                 Debug( LDAP_DEBUG_ARGS,
217                         "<=- bdb_delete: no such object %s\n",
218                         dn->bv_val, 0, 0);
219
220                 if ( matched != NULL ) {
221                         matched_dn = ch_strdup( matched->e_dn );
222                         refs = is_entry_referral( matched )
223                                 ? get_entry_referrals( be, conn, op, matched )
224                                 : NULL;
225                         bdb_cache_return_entry_r(&bdb->bi_cache, matched );
226                         matched = NULL;
227
228                 } else {
229                         refs = referral_rewrite( default_referral,
230                                 NULL, dn, LDAP_SCOPE_DEFAULT );
231                 }
232
233                 send_ldap_result( conn, op, LDAP_REFERRAL,
234                         matched_dn, NULL, refs, NULL );
235
236                 ber_bvarray_free( refs );
237                 free( matched_dn );
238
239                 rc = -1;
240                 goto done;
241         }
242
243         if ( !manageDSAit && is_entry_referral( e ) ) {
244                 /* parent is a referral, don't allow add */
245                 /* parent is an alias, don't allow add */
246                 BerVarray refs = get_entry_referrals( be,
247                         conn, op, e );
248
249                 Debug( LDAP_DEBUG_TRACE,
250                         "bdb_delete: entry is referral\n",
251                         0, 0, 0 );
252
253                 send_ldap_result( conn, op, LDAP_REFERRAL,
254                         e->e_dn, NULL, refs, NULL );
255
256                 ber_bvarray_free( refs );
257
258                 rc = 1;
259                 goto done;
260         }
261
262         rc = bdb_dn2id_children( be, ltid, &e->e_nname );
263         if( rc != DB_NOTFOUND ) {
264                 switch( rc ) {
265                 case DB_LOCK_DEADLOCK:
266                 case DB_LOCK_NOTGRANTED:
267                         goto retry;
268                 case 0:
269                         Debug(LDAP_DEBUG_ARGS,
270                                 "<=- bdb_delete: non-leaf %s\n",
271                                 dn->bv_val, 0, 0);
272                         rc = LDAP_NOT_ALLOWED_ON_NONLEAF;
273                         text = "subtree delete not supported";
274                         break;
275                 default:
276                         Debug(LDAP_DEBUG_ARGS,
277                                 "<=- bdb_delete: has_children failed: %s (%d)\n",
278                                 db_strerror(rc), rc, 0 );
279                         rc = LDAP_OTHER;
280                         text = "internal error";
281                 }
282                 goto return_results;
283         }
284
285         /* delete from dn2id */
286         rc = bdb_dn2id_delete( be, ltid, pdn.bv_val, e );
287         if ( rc != 0 ) {
288                 switch( rc ) {
289                 case DB_LOCK_DEADLOCK:
290                 case DB_LOCK_NOTGRANTED:
291                         goto retry;
292                 default:
293                         rc = LDAP_OTHER;
294                 }
295                 Debug(LDAP_DEBUG_ARGS,
296                         "<=- bdb_delete: dn2id failed: %s (%d)\n",
297                         db_strerror(rc), rc, 0 );
298                 text = "DN index delete failed";
299                 goto return_results;
300         }
301
302         /* delete from id2entry */
303         rc = bdb_id2entry_delete( be, ltid, e );
304         if ( rc != 0 ) {
305                 switch( rc ) {
306                 case DB_LOCK_DEADLOCK:
307                 case DB_LOCK_NOTGRANTED:
308                         goto retry;
309                 default:
310                         rc = LDAP_OTHER;
311                 }
312                 Debug(LDAP_DEBUG_ARGS,
313                         "<=- bdb_delete: id2entry failed: %s (%d)\n",
314                         db_strerror(rc), rc, 0 );
315                 text = "entry delete failed";
316                 goto return_results;
317         }
318
319         /* delete indices for old attributes */
320         rc = bdb_index_entry_del( be, ltid, e, e->e_attrs );
321         if ( rc != LDAP_SUCCESS ) {
322                 switch( rc ) {
323                 case DB_LOCK_DEADLOCK:
324                 case DB_LOCK_NOTGRANTED:
325                         goto retry;
326                 default:
327                         rc = LDAP_OTHER;
328                 }
329                 Debug( LDAP_DEBUG_ANY, "entry index delete failed!\n",
330                         0, 0, 0 );
331                 text = "entry index delete failed";
332                 goto return_results;
333         }
334
335 #if 0   /* Do we want to reclaim deleted IDs? */
336         ldap_pvt_thread_mutex_lock( &bdb->bi_lastid_mutex );
337         if ( e->e_id == bdb->bi_lastid ) {
338                 bdb_last_id( be, ltid );
339         }
340         ldap_pvt_thread_mutex_unlock( &bdb->bi_lastid_mutex );
341 #endif
342
343         if( bdb->bi_txn ) {
344                 rc = txn_commit( ltid, 0 );
345         }
346         ltid = NULL;
347         op->o_private = NULL;
348
349         if( rc != 0 ) {
350                 Debug( LDAP_DEBUG_TRACE,
351                         "bdb_delete: txn_commit failed: %s (%d)\n",
352                         db_strerror(rc), rc, 0 );
353                 rc = LDAP_OTHER;
354                 text = "commit failed";
355
356         } else {
357                 Debug( LDAP_DEBUG_TRACE,
358                         "bdb_delete: deleted id=%08lx dn=\"%s\"\n",
359                         e->e_id, e->e_dn, 0 );
360                 rc = LDAP_SUCCESS;
361                 text = NULL;
362         }
363
364 return_results:
365         send_ldap_result( conn, op, LDAP_SUCCESS,
366                 NULL, text, NULL, NULL );
367
368         if(rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
369                 ldap_pvt_thread_yield();
370                 TXN_CHECKPOINT( bdb->bi_dbenv,
371                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
372         }
373
374 done:
375         /* free entry */
376         if( e != NULL ) {
377                 bdb_cache_return_entry_w(&bdb->bi_cache, e);
378         }
379
380         if( ltid != NULL ) {
381                 txn_abort( ltid );
382                 op->o_private = NULL;
383         }
384
385         return rc;
386 }