1 /* delete.c - bdb backend delete routine */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
17 bdb_delete( Operation *op, SlapReply *rs )
19 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
21 struct berval pdn = {0, 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;
36 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
41 LDAP_LOG ( OPERATION, ARGS, "==> bdb_delete: %s\n", op->o_req_dn.bv_val, 0, 0 );
43 Debug( LDAP_DEBUG_ARGS, "==> bdb_delete: %s\n",
44 op->o_req_dn.bv_val, 0, 0 );
48 retry: /* transaction retry */
50 bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
54 LDAP_LOG ( OPERATION, DETAIL1,
55 "==> bdb_delete: retrying...\n", 0, 0, 0 );
57 Debug( LDAP_DEBUG_TRACE, "==> bdb_delete: retrying...\n",
60 rs->sr_err = TXN_ABORT( ltid );
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";
69 ldap_pvt_thread_yield();
72 /* begin transaction */
73 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
76 if( rs->sr_err != 0 ) {
78 LDAP_LOG ( OPERATION, ERR,
79 "==> bdb_delete: txn_begin failed: %s (%d)\n",
80 db_strerror(rs->sr_err), rs->sr_err, 0 );
82 Debug( LDAP_DEBUG_TRACE,
83 "bdb_delete: txn_begin failed: %s (%d)\n",
84 db_strerror(rs->sr_err), rs->sr_err, 0 );
86 rs->sr_err = LDAP_OTHER;
87 rs->sr_text = "internal error";
91 locker = TXN_ID ( ltid );
93 opinfo.boi_bdb = op->o_bd;
94 opinfo.boi_txn = ltid;
95 opinfo.boi_locker = locker;
97 opinfo.boi_acl_cache = op->o_do_not_cache;
98 op->o_private = &opinfo;
100 if ( !be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
101 dnParent( &op->o_req_ndn, &pdn );
105 rs->sr_err = bdb_dn2entry( op->o_bd, ltid, &op->o_req_ndn, &ei, 1,
106 locker, &lock, op->o_tmpmemctx );
108 switch( rs->sr_err ) {
112 case DB_LOCK_DEADLOCK:
113 case DB_LOCK_NOTGRANTED:
116 rs->sr_text = "ldap server busy";
119 rs->sr_err = LDAP_OTHER;
120 rs->sr_text = "internal error";
124 if ( rs->sr_err == 0 ) {
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 );
134 if ( pdn.bv_len != 0 ) {
135 if( p == NULL || !bvmatch( &pdn, &p->e_nname )) {
137 LDAP_LOG ( OPERATION, DETAIL1,
138 "<=- bdb_delete: parent does not exist\n", 0, 0, 0 );
140 Debug( LDAP_DEBUG_TRACE,
141 "<=- bdb_delete: parent does not exist\n",
144 rs->sr_err = LDAP_OTHER;
145 rs->sr_text = "could not locate parent of entry";
149 /* check parent for "children" acl */
150 rs->sr_err = access_allowed( op, p,
151 children, NULL, ACL_WRITE, NULL );
153 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p);
157 switch( opinfo.boi_err ) {
158 case DB_LOCK_DEADLOCK:
159 case DB_LOCK_NOTGRANTED:
164 LDAP_LOG ( OPERATION, DETAIL1,
165 "<=- bdb_delete: no write access to parent\n", 0, 0, 0 );
167 Debug( LDAP_DEBUG_TRACE,
168 "<=- bdb_delete: no write access to parent\n",
171 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
172 rs->sr_text = "no write access to parent";
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;
183 /* check parent for "children" acl */
184 rs->sr_err = access_allowed( op, p,
185 children, NULL, ACL_WRITE, NULL );
190 switch( opinfo.boi_err ) {
191 case DB_LOCK_DEADLOCK:
192 case DB_LOCK_NOTGRANTED:
197 LDAP_LOG ( OPERATION, DETAIL1,
198 "<=- bdb_delete: no access to parent\n", 0, 0, 0 );
200 Debug( LDAP_DEBUG_TRACE,
201 "<=- bdb_delete: no access "
202 "to parent\n", 0, 0, 0 );
204 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
205 rs->sr_text = "no write access to parent";
211 LDAP_LOG ( OPERATION, DETAIL1,
212 "<=- bdb_delete: no parent and not root\n", 0, 0, 0 );
214 Debug( LDAP_DEBUG_TRACE,
215 "<=- bdb_delete: no parent "
216 "and not root\n", 0, 0, 0);
218 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
226 LDAP_LOG ( OPERATION, ARGS,
227 "<=- bdb_delete: no such object %s\n", op->o_req_dn.bv_val, 0, 0);
229 Debug( LDAP_DEBUG_ARGS,
230 "<=- bdb_delete: no such object %s\n",
231 op->o_req_dn.bv_val, 0, 0);
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 )
239 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, matched);
243 rs->sr_ref = referral_rewrite( default_referral,
244 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
247 rs->sr_err = LDAP_REFERRAL;
248 send_ldap_result( op, rs );
250 ber_bvarray_free( rs->sr_ref );
251 free( (char *)rs->sr_matched );
253 rs->sr_matched = NULL;
259 rs->sr_err = access_allowed( op, e,
260 entry, NULL, ACL_WRITE, NULL );
263 switch( opinfo.boi_err ) {
264 case DB_LOCK_DEADLOCK:
265 case DB_LOCK_NOTGRANTED:
270 LDAP_LOG ( OPERATION, DETAIL1,
271 "<=- bdb_delete: no write access to entry\n", 0, 0, 0 );
273 Debug( LDAP_DEBUG_TRACE,
274 "<=- bdb_delete: no write access to entry\n",
277 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
278 rs->sr_text = "no write access to entry";
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 );
287 LDAP_LOG ( OPERATION, DETAIL1,
288 "<=- bdb_delete: entry is referral\n", 0, 0, 0 );
290 Debug( LDAP_DEBUG_TRACE,
291 "bdb_delete: entry is referral\n",
295 rs->sr_err = LDAP_REFERRAL;
296 rs->sr_matched = e->e_name.bv_val;
297 send_ldap_result( op, rs );
299 ber_bvarray_free( rs->sr_ref );
301 rs->sr_matched = NULL;
307 /* nested transaction */
308 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, <2,
309 bdb->bi_db_opflags );
311 if( rs->sr_err != 0 ) {
313 LDAP_LOG ( OPERATION, ERR,
314 "bdb_delete: txn_begin(2) failed: %s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
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 );
320 rs->sr_err = LDAP_OTHER;
321 rs->sr_text = "internal error";
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:
334 LDAP_LOG ( OPERATION, DETAIL1,
335 "<=- bdb_delete: non-leaf %s\n", op->o_req_dn.bv_val, 0, 0 );
337 Debug(LDAP_DEBUG_ARGS,
338 "<=- bdb_delete: non-leaf %s\n",
339 op->o_req_dn.bv_val, 0, 0);
341 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
342 rs->sr_text = "subtree delete not supported";
346 LDAP_LOG ( OPERATION, ERR,
347 "<=- bdb_delete: has_children failed %s (%d)\n",
348 db_strerror(rs->sr_err), rs->sr_err, 0 );
350 Debug(LDAP_DEBUG_ARGS,
351 "<=- bdb_delete: has_children failed: %s (%d)\n",
352 db_strerror(rs->sr_err), rs->sr_err, 0 );
354 rs->sr_err = LDAP_OTHER;
355 rs->sr_text = "internal error";
360 /* delete from dn2id */
361 rs->sr_err = bdb_dn2id_delete( op->o_bd, lt2, eip, e,
363 if ( rs->sr_err != 0 ) {
364 switch( rs->sr_err ) {
365 case DB_LOCK_DEADLOCK:
366 case DB_LOCK_NOTGRANTED:
370 LDAP_LOG ( OPERATION, ERR,
371 "<=- bdb_delete: dn2id failed %s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
373 Debug(LDAP_DEBUG_ARGS,
374 "<=- bdb_delete: dn2id failed: %s (%d)\n",
375 db_strerror(rs->sr_err), rs->sr_err, 0 );
377 rs->sr_text = "DN index delete failed";
378 rs->sr_err = LDAP_OTHER;
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:
391 LDAP_LOG ( OPERATION, ERR,
392 "<=- bdb_delete: id2entry failed: %s (%d)\n",
393 db_strerror(rs->sr_err), rs->sr_err, 0 );
395 Debug(LDAP_DEBUG_ARGS,
396 "<=- bdb_delete: id2entry failed: %s (%d)\n",
397 db_strerror(rs->sr_err), rs->sr_err, 0 );
399 rs->sr_text = "entry delete failed";
400 rs->sr_err = LDAP_OTHER;
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:
413 LDAP_LOG ( OPERATION, ERR,
414 "<=- bdb_delete: entry index delete failed!\n", 0, 0, 0 );
416 Debug( LDAP_DEBUG_ANY, "entry index delete failed!\n",
419 rs->sr_text = "entry index delete failed";
420 rs->sr_err = LDAP_OTHER;
423 if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
424 rs->sr_err = LDAP_OTHER;
425 rs->sr_text = "txn_commit(2) failed";
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 );
434 ldap_pvt_thread_mutex_unlock( &bdb->bi_lastid_mutex );
438 if ( ( rs->sr_err = TXN_ABORT( ltid ) ) != 0 ) {
439 rs->sr_text = "txn_abort (no-op) failed";
442 rs->sr_err = LDAP_SUCCESS;
445 bdb_cache_delete_entry( &bdb->bi_cache, e, bdb->bi_dbenv,
447 rs->sr_err = TXN_COMMIT( ltid, 0 );
450 op->o_private = NULL;
452 if( rs->sr_err != 0 ) {
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 );
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 );
463 rs->sr_err = LDAP_OTHER;
464 rs->sr_text = "commit failed";
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 );
472 Debug( LDAP_DEBUG_TRACE,
473 "bdb_delete: deleted%s id=%08lx dn=\"%s\"\n",
474 op->o_noop ? " (no-op)" : "",
477 rs->sr_err = LDAP_SUCCESS;
482 send_ldap_result( op, rs );
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 );
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 );
501 if ( rs->sr_err == LDAP_SUCCESS ) {
502 bdb_entry_return( e );
504 bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
510 op->o_private = NULL;
513 return ( ( rs->sr_err == LDAP_SUCCESS ) ? noop : rs->sr_err );