]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/delete.c
Mem context tweaks for bdb_dn2idl
[openldap] / servers / slapd / back-bdb / delete.c
1 /* delete.c - bdb backend delete routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 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( Operation *op, SlapReply *rs )
18 {
19         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
20         Entry   *matched;
21         struct berval   pdn = {0, NULL};
22         Entry   *e = NULL;
23         Entry   *p = NULL;
24         EntryInfo       *ei = NULL, *eip = NULL;
25         int             manageDSAit = get_manageDSAit( op );
26         AttributeDescription *children = slap_schema.si_ad_children;
27         AttributeDescription *entry = slap_schema.si_ad_entry;
28         DB_TXN          *ltid = NULL, *lt2;
29         struct bdb_op_info opinfo;
30
31         u_int32_t       locker = 0;
32         DB_LOCK         lock, plock;
33
34         int             noop = 0;
35
36 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
37         Operation* ps_list;
38 #endif
39
40 #ifdef NEW_LOGGING
41         LDAP_LOG ( OPERATION, ARGS,  "==> bdb_delete: %s\n", op->o_req_dn.bv_val, 0, 0 );
42 #else
43         Debug( LDAP_DEBUG_ARGS, "==> bdb_delete: %s\n",
44                 op->o_req_dn.bv_val, 0, 0 );
45 #endif
46
47         if( 0 ) {
48 retry:  /* transaction retry */
49                 if( e != NULL ) {
50                         bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
51                         e = NULL;
52                 }
53 #ifdef NEW_LOGGING
54                 LDAP_LOG ( OPERATION, DETAIL1, 
55                         "==> bdb_delete: retrying...\n", 0, 0, 0 );
56 #else
57                 Debug( LDAP_DEBUG_TRACE, "==> bdb_delete: retrying...\n",
58                         0, 0, 0 );
59 #endif
60                 rs->sr_err = TXN_ABORT( ltid );
61                 ltid = NULL;
62                 op->o_private = NULL;
63                 op->o_do_not_cache = opinfo.boi_acl_cache;
64                 if( rs->sr_err != 0 ) {
65                         rs->sr_err = LDAP_OTHER;
66                         rs->sr_text = "internal error";
67                         goto return_results;
68                 }
69                 ldap_pvt_thread_yield();
70         }
71
72         /* begin transaction */
73         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, &ltid, 
74                 bdb->bi_db_opflags );
75         rs->sr_text = NULL;
76         if( rs->sr_err != 0 ) {
77 #ifdef NEW_LOGGING
78                 LDAP_LOG ( OPERATION, ERR, 
79                         "==> bdb_delete: txn_begin failed: %s (%d)\n",
80                         db_strerror(rs->sr_err), rs->sr_err, 0 );
81 #else
82                 Debug( LDAP_DEBUG_TRACE,
83                         "bdb_delete: txn_begin failed: %s (%d)\n",
84                         db_strerror(rs->sr_err), rs->sr_err, 0 );
85 #endif
86                 rs->sr_err = LDAP_OTHER;
87                 rs->sr_text = "internal error";
88                 goto return_results;
89         }
90
91         locker = TXN_ID ( ltid );
92
93         opinfo.boi_bdb = op->o_bd;
94         opinfo.boi_txn = ltid;
95         opinfo.boi_locker = locker;
96         opinfo.boi_err = 0;
97         opinfo.boi_acl_cache = op->o_do_not_cache;
98         op->o_private = &opinfo;
99
100         if ( !be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
101                 dnParent( &op->o_req_ndn, &pdn );
102         }
103
104         /* get entry */
105         rs->sr_err = bdb_dn2entry( op->o_bd, ltid, &op->o_req_ndn, &ei, 1,
106                 locker, &lock, op->o_tmpmemctx );
107
108         switch( rs->sr_err ) {
109         case 0:
110         case DB_NOTFOUND:
111                 break;
112         case DB_LOCK_DEADLOCK:
113         case DB_LOCK_NOTGRANTED:
114                 goto retry;
115         case LDAP_BUSY:
116                 rs->sr_text = "ldap server busy";
117                 goto return_results;
118         default:
119                 rs->sr_err = LDAP_OTHER;
120                 rs->sr_text = "internal error";
121                 goto return_results;
122         }
123
124         if ( rs->sr_err == 0 ) {
125                 e = ei->bei_e;
126                 eip = ei->bei_parent;
127                 bdb_cache_find_entry_id( op->o_bd, ltid, eip->bei_id, &eip,
128                         0, locker, &plock, op->o_tmpmemctx );
129         }
130         if ( eip ) {
131                 p = eip->bei_e;
132         }
133
134         if ( pdn.bv_len != 0 ) {
135                 if( p == NULL || !bvmatch( &pdn, &p->e_nname )) {
136 #ifdef NEW_LOGGING
137                         LDAP_LOG ( OPERATION, DETAIL1, 
138                                 "<=- bdb_delete: parent does not exist\n", 0, 0, 0 );
139 #else
140                         Debug( LDAP_DEBUG_TRACE,
141                                 "<=- bdb_delete: parent does not exist\n",
142                                 0, 0, 0);
143 #endif
144                         rs->sr_err = LDAP_OTHER;
145                         rs->sr_text = "could not locate parent of entry";
146                         goto return_results;
147                 }
148
149                 /* check parent for "children" acl */
150                 rs->sr_err = access_allowed( op, p,
151                         children, NULL, ACL_WRITE, NULL );
152
153                 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p);
154                 p = NULL;
155
156                 if ( !rs->sr_err  ) {
157                         switch( opinfo.boi_err ) {
158                         case DB_LOCK_DEADLOCK:
159                         case DB_LOCK_NOTGRANTED:
160                                 goto retry;
161                         }
162
163 #ifdef NEW_LOGGING
164                         LDAP_LOG ( OPERATION, DETAIL1, 
165                                 "<=- bdb_delete: no write access to parent\n", 0, 0, 0 );
166 #else
167                         Debug( LDAP_DEBUG_TRACE,
168                                 "<=- bdb_delete: no write access to parent\n",
169                                 0, 0, 0 );
170 #endif
171                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
172                         rs->sr_text = "no write access to parent";
173                         goto return_results;
174                 }
175
176         } else {
177                 /* no parent, must be root to delete */
178                 if( ! be_isroot( op->o_bd, &op->o_ndn ) ) {
179                         if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv )
180                                 || be_isupdate( op->o_bd, &op->o_ndn ) ) {
181                                 p = (Entry *)&slap_entry_root;
182
183                                 /* check parent for "children" acl */
184                                 rs->sr_err = access_allowed( op, p,
185                                         children, NULL, ACL_WRITE, NULL );
186
187                                 p = NULL;
188
189                                 if ( !rs->sr_err  ) {
190                                         switch( opinfo.boi_err ) {
191                                         case DB_LOCK_DEADLOCK:
192                                         case DB_LOCK_NOTGRANTED:
193                                                 goto retry;
194                                         }
195
196 #ifdef NEW_LOGGING
197                                         LDAP_LOG ( OPERATION, DETAIL1, 
198                                                 "<=- bdb_delete: no access to parent\n", 0, 0, 0 );
199 #else
200                                         Debug( LDAP_DEBUG_TRACE,
201                                                 "<=- bdb_delete: no access "
202                                                 "to parent\n", 0, 0, 0 );
203 #endif
204                                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
205                                         rs->sr_text = "no write access to parent";
206                                         goto return_results;
207                                 }
208
209                         } else {
210 #ifdef NEW_LOGGING
211                                 LDAP_LOG ( OPERATION, DETAIL1, 
212                                         "<=- bdb_delete: no parent and not root\n", 0, 0, 0 );
213 #else
214                                 Debug( LDAP_DEBUG_TRACE,
215                                         "<=- bdb_delete: no parent "
216                                         "and not root\n", 0, 0, 0);
217 #endif
218                                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
219                                 goto return_results;
220                         }
221                 }
222         }
223
224         if ( e == NULL ) {
225 #ifdef NEW_LOGGING
226                 LDAP_LOG ( OPERATION, ARGS, 
227                         "<=- bdb_delete: no such object %s\n", op->o_req_dn.bv_val, 0, 0);
228 #else
229                 Debug( LDAP_DEBUG_ARGS,
230                         "<=- bdb_delete: no such object %s\n",
231                         op->o_req_dn.bv_val, 0, 0);
232 #endif
233
234                 if ( matched != NULL ) {
235                         rs->sr_matched = ch_strdup( matched->e_dn );
236                         rs->sr_ref = is_entry_referral( matched )
237                                 ? get_entry_referrals( op, matched )
238                                 : NULL;
239                         bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, matched);
240                         matched = NULL;
241
242                 } else {
243                         rs->sr_ref = referral_rewrite( default_referral,
244                                 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
245                 }
246
247                 rs->sr_err = LDAP_REFERRAL;
248                 send_ldap_result( op, rs );
249
250                 ber_bvarray_free( rs->sr_ref );
251                 free( (char *)rs->sr_matched );
252                 rs->sr_ref = NULL;
253                 rs->sr_matched = NULL;
254
255                 rs->sr_err = -1;
256                 goto done;
257         }
258
259         rs->sr_err = access_allowed( op, e,
260                 entry, NULL, ACL_WRITE, NULL );
261
262         if ( !rs->sr_err  ) {
263                 switch( opinfo.boi_err ) {
264                 case DB_LOCK_DEADLOCK:
265                 case DB_LOCK_NOTGRANTED:
266                         goto retry;
267                 }
268
269 #ifdef NEW_LOGGING
270                 LDAP_LOG ( OPERATION, DETAIL1, 
271                         "<=- bdb_delete: no write access to entry\n", 0, 0, 0 );
272 #else
273                 Debug( LDAP_DEBUG_TRACE,
274                         "<=- bdb_delete: no write access to entry\n",
275                         0, 0, 0 );
276 #endif
277                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
278                 rs->sr_text = "no write access to entry";
279                 goto return_results;
280         }
281
282         if ( !manageDSAit && is_entry_referral( e ) ) {
283                 /* entry is a referral, don't allow delete */
284                 rs->sr_ref = get_entry_referrals( op, e );
285
286 #ifdef NEW_LOGGING
287                 LDAP_LOG ( OPERATION, DETAIL1, 
288                         "<=- bdb_delete: entry is referral\n", 0, 0, 0 );
289 #else
290                 Debug( LDAP_DEBUG_TRACE,
291                         "bdb_delete: entry is referral\n",
292                         0, 0, 0 );
293 #endif
294
295                 rs->sr_err = LDAP_REFERRAL;
296                 rs->sr_matched = e->e_name.bv_val;
297                 send_ldap_result( op, rs );
298
299                 ber_bvarray_free( rs->sr_ref );
300                 rs->sr_ref = NULL;
301                 rs->sr_matched = NULL;
302
303                 rs->sr_err = 1;
304                 goto done;
305         }
306
307         /* nested transaction */
308         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, &lt2, 
309                 bdb->bi_db_opflags );
310         rs->sr_text = NULL;
311         if( rs->sr_err != 0 ) {
312 #ifdef NEW_LOGGING
313                 LDAP_LOG ( OPERATION, ERR, 
314                         "bdb_delete: txn_begin(2) failed: %s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
315 #else
316                 Debug( LDAP_DEBUG_TRACE,
317                         "bdb_delete: txn_begin(2) failed: %s (%d)\n",
318                         db_strerror(rs->sr_err), rs->sr_err, 0 );
319 #endif
320                 rs->sr_err = LDAP_OTHER;
321                 rs->sr_text = "internal error";
322                 goto return_results;
323         }
324
325         /* Can't do it if we have kids */
326         rs->sr_err = bdb_cache_children( op, lt2, e );
327         if( rs->sr_err != DB_NOTFOUND ) {
328                 switch( rs->sr_err ) {
329                 case DB_LOCK_DEADLOCK:
330                 case DB_LOCK_NOTGRANTED:
331                         goto retry;
332                 case 0:
333 #ifdef NEW_LOGGING
334                         LDAP_LOG ( OPERATION, DETAIL1, 
335                                 "<=- bdb_delete: non-leaf %s\n", op->o_req_dn.bv_val, 0, 0 );
336 #else
337                         Debug(LDAP_DEBUG_ARGS,
338                                 "<=- bdb_delete: non-leaf %s\n",
339                                 op->o_req_dn.bv_val, 0, 0);
340 #endif
341                         rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
342                         rs->sr_text = "subtree delete not supported";
343                         break;
344                 default:
345 #ifdef NEW_LOGGING
346                         LDAP_LOG ( OPERATION, ERR, 
347                                 "<=- bdb_delete: has_children failed %s (%d)\n",
348                                 db_strerror(rs->sr_err), rs->sr_err, 0 );
349 #else
350                         Debug(LDAP_DEBUG_ARGS,
351                                 "<=- bdb_delete: has_children failed: %s (%d)\n",
352                                 db_strerror(rs->sr_err), rs->sr_err, 0 );
353 #endif
354                         rs->sr_err = LDAP_OTHER;
355                         rs->sr_text = "internal error";
356                 }
357                 goto return_results;
358         }
359
360         /* delete from dn2id */
361         rs->sr_err = bdb_dn2id_delete( op->o_bd, lt2, eip, e,
362                 op->o_tmpmemctx );
363         if ( rs->sr_err != 0 ) {
364                 switch( rs->sr_err ) {
365                 case DB_LOCK_DEADLOCK:
366                 case DB_LOCK_NOTGRANTED:
367                         goto retry;
368                 }
369 #ifdef NEW_LOGGING
370                 LDAP_LOG ( OPERATION, ERR, 
371                         "<=- bdb_delete: dn2id failed %s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
372 #else
373                 Debug(LDAP_DEBUG_ARGS,
374                         "<=- bdb_delete: dn2id failed: %s (%d)\n",
375                         db_strerror(rs->sr_err), rs->sr_err, 0 );
376 #endif
377                 rs->sr_text = "DN index delete failed";
378                 rs->sr_err = LDAP_OTHER;
379                 goto return_results;
380         }
381
382         /* delete from id2entry */
383         rs->sr_err = bdb_id2entry_delete( op->o_bd, lt2, e );
384         if ( rs->sr_err != 0 ) {
385                 switch( rs->sr_err ) {
386                 case DB_LOCK_DEADLOCK:
387                 case DB_LOCK_NOTGRANTED:
388                         goto retry;
389                 }
390 #ifdef NEW_LOGGING
391                 LDAP_LOG ( OPERATION, ERR, 
392                         "<=- bdb_delete: id2entry failed: %s (%d)\n", 
393                         db_strerror(rs->sr_err), rs->sr_err, 0 );
394 #else
395                 Debug(LDAP_DEBUG_ARGS,
396                         "<=- bdb_delete: id2entry failed: %s (%d)\n",
397                         db_strerror(rs->sr_err), rs->sr_err, 0 );
398 #endif
399                 rs->sr_text = "entry delete failed";
400                 rs->sr_err = LDAP_OTHER;
401                 goto return_results;
402         }
403
404         /* delete indices for old attributes */
405         rs->sr_err = bdb_index_entry_del( op, lt2, e );
406         if ( rs->sr_err != LDAP_SUCCESS ) {
407                 switch( rs->sr_err ) {
408                 case DB_LOCK_DEADLOCK:
409                 case DB_LOCK_NOTGRANTED:
410                         goto retry;
411                 }
412 #ifdef NEW_LOGGING
413                 LDAP_LOG ( OPERATION, ERR, 
414                         "<=- bdb_delete: entry index delete failed!\n", 0, 0, 0 );
415 #else
416                 Debug( LDAP_DEBUG_ANY, "entry index delete failed!\n",
417                         0, 0, 0 );
418 #endif
419                 rs->sr_text = "entry index delete failed";
420                 rs->sr_err = LDAP_OTHER;
421                 goto return_results;
422         }
423         if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
424                 rs->sr_err = LDAP_OTHER;
425                 rs->sr_text = "txn_commit(2) failed";
426                 goto return_results;
427         }
428
429 #if 0   /* Do we want to reclaim deleted IDs? */
430         ldap_pvt_thread_mutex_lock( &bdb->bi_lastid_mutex );
431         if ( e->e_id == bdb->bi_lastid ) {
432                 bdb_last_id( op->o_bd, ltid );
433         }
434         ldap_pvt_thread_mutex_unlock( &bdb->bi_lastid_mutex );
435 #endif
436
437         if( op->o_noop ) {
438                 if ( ( rs->sr_err = TXN_ABORT( ltid ) ) != 0 ) {
439                         rs->sr_text = "txn_abort (no-op) failed";
440                 } else {
441                         noop = 1;
442                         rs->sr_err = LDAP_SUCCESS;
443                 }
444         } else {
445                 bdb_cache_delete_entry( &bdb->bi_cache, e, bdb->bi_dbenv,
446                         locker, &lock );
447                 rs->sr_err = TXN_COMMIT( ltid, 0 );
448         }
449         ltid = NULL;
450         op->o_private = NULL;
451
452         if( rs->sr_err != 0 ) {
453 #ifdef NEW_LOGGING
454                 LDAP_LOG ( OPERATION, ERR, 
455                         "bdb_delete: txn_%s failed: %s (%d)\n",
456                         op->o_noop ? "abort (no-op)" : "commit", db_strerror(rs->sr_err), rs->sr_err );
457 #else
458                 Debug( LDAP_DEBUG_TRACE,
459                         "bdb_delete: txn_%s failed: %s (%d)\n",
460                         op->o_noop ? "abort (no-op)" : "commit",
461                         db_strerror(rs->sr_err), rs->sr_err );
462 #endif
463                 rs->sr_err = LDAP_OTHER;
464                 rs->sr_text = "commit failed";
465
466         } else {
467 #ifdef NEW_LOGGING
468                 LDAP_LOG ( OPERATION, RESULTS, 
469                         "bdb_delete: deleted%s id=%08lx db=\"%s\"\n",
470                         op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
471 #else
472                 Debug( LDAP_DEBUG_TRACE,
473                         "bdb_delete: deleted%s id=%08lx dn=\"%s\"\n",
474                         op->o_noop ? " (no-op)" : "",
475                         e->e_id, e->e_dn );
476 #endif
477                 rs->sr_err = LDAP_SUCCESS;
478                 rs->sr_text = NULL;
479         }
480
481 return_results:
482         send_ldap_result( op, rs );
483
484 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
485         if ( rs->sr_err == LDAP_SUCCESS && !noop ) {
486                 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
487                         bdb_psearch( op, rs, ps_list, e, LDAP_PSEARCH_BY_DELETE );
488                 }
489         }
490 #endif
491
492         if(rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp ) {
493                 ldap_pvt_thread_yield();
494                 TXN_CHECKPOINT( bdb->bi_dbenv,
495                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
496         }
497
498 done:
499         /* free entry */
500         if( e != NULL ) {
501                 if ( rs->sr_err == LDAP_SUCCESS ) {
502                         bdb_entry_return( e );
503                 } else {
504                         bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
505                 }
506         }
507
508         if( ltid != NULL ) {
509                 TXN_ABORT( ltid );
510                 op->o_private = NULL;
511         }
512
513         return ( ( rs->sr_err == LDAP_SUCCESS ) ? noop : rs->sr_err );
514 }