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_dn2id_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, pdn.bv_val, e );
362 if ( rs->sr_err != 0 ) {
363 switch( rs->sr_err ) {
364 case DB_LOCK_DEADLOCK:
365 case DB_LOCK_NOTGRANTED:
369 LDAP_LOG ( OPERATION, ERR,
370 "<=- bdb_delete: dn2id failed %s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
372 Debug(LDAP_DEBUG_ARGS,
373 "<=- bdb_delete: dn2id failed: %s (%d)\n",
374 db_strerror(rs->sr_err), rs->sr_err, 0 );
376 rs->sr_text = "DN index delete failed";
377 rs->sr_err = LDAP_OTHER;
381 /* delete from id2entry */
382 rs->sr_err = bdb_id2entry_delete( op->o_bd, lt2, e );
383 if ( rs->sr_err != 0 ) {
384 switch( rs->sr_err ) {
385 case DB_LOCK_DEADLOCK:
386 case DB_LOCK_NOTGRANTED:
390 LDAP_LOG ( OPERATION, ERR,
391 "<=- bdb_delete: id2entry failed: %s (%d)\n",
392 db_strerror(rs->sr_err), rs->sr_err, 0 );
394 Debug(LDAP_DEBUG_ARGS,
395 "<=- bdb_delete: id2entry failed: %s (%d)\n",
396 db_strerror(rs->sr_err), rs->sr_err, 0 );
398 rs->sr_text = "entry delete failed";
399 rs->sr_err = LDAP_OTHER;
403 /* delete indices for old attributes */
404 rs->sr_err = bdb_index_entry_del( op, lt2, e );
405 if ( rs->sr_err != LDAP_SUCCESS ) {
406 switch( rs->sr_err ) {
407 case DB_LOCK_DEADLOCK:
408 case DB_LOCK_NOTGRANTED:
412 LDAP_LOG ( OPERATION, ERR,
413 "<=- bdb_delete: entry index delete failed!\n", 0, 0, 0 );
415 Debug( LDAP_DEBUG_ANY, "entry index delete failed!\n",
418 rs->sr_text = "entry index delete failed";
419 rs->sr_err = LDAP_OTHER;
422 if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
423 rs->sr_err = LDAP_OTHER;
424 rs->sr_text = "txn_commit(2) failed";
428 #if 0 /* Do we want to reclaim deleted IDs? */
429 ldap_pvt_thread_mutex_lock( &bdb->bi_lastid_mutex );
430 if ( e->e_id == bdb->bi_lastid ) {
431 bdb_last_id( op->o_bd, ltid );
433 ldap_pvt_thread_mutex_unlock( &bdb->bi_lastid_mutex );
437 if ( ( rs->sr_err = TXN_ABORT( ltid ) ) != 0 ) {
438 rs->sr_text = "txn_abort (no-op) failed";
441 rs->sr_err = LDAP_SUCCESS;
444 bdb_cache_delete_entry( &bdb->bi_cache, e, bdb->bi_dbenv,
446 rs->sr_err = TXN_COMMIT( ltid, 0 );
449 op->o_private = NULL;
451 if( rs->sr_err != 0 ) {
453 LDAP_LOG ( OPERATION, ERR,
454 "bdb_delete: txn_%s failed: %s (%d)\n",
455 op->o_noop ? "abort (no-op)" : "commit", db_strerror(rs->sr_err), rs->sr_err );
457 Debug( LDAP_DEBUG_TRACE,
458 "bdb_delete: txn_%s failed: %s (%d)\n",
459 op->o_noop ? "abort (no-op)" : "commit",
460 db_strerror(rs->sr_err), rs->sr_err );
462 rs->sr_err = LDAP_OTHER;
463 rs->sr_text = "commit failed";
467 LDAP_LOG ( OPERATION, RESULTS,
468 "bdb_delete: deleted%s id=%08lx db=\"%s\"\n",
469 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
471 Debug( LDAP_DEBUG_TRACE,
472 "bdb_delete: deleted%s id=%08lx dn=\"%s\"\n",
473 op->o_noop ? " (no-op)" : "",
476 rs->sr_err = LDAP_SUCCESS;
481 send_ldap_result( op, rs );
483 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
484 if ( rs->sr_err == LDAP_SUCCESS && !noop ) {
485 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
486 bdb_psearch( op, rs, ps_list, e, LDAP_PSEARCH_BY_DELETE );
491 if(rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp ) {
492 ldap_pvt_thread_yield();
493 TXN_CHECKPOINT( bdb->bi_dbenv,
494 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
500 if ( rs->sr_err == LDAP_SUCCESS ) {
501 bdb_entry_return( e );
503 bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
509 op->o_private = NULL;
512 return ( ( rs->sr_err == LDAP_SUCCESS ) ? noop : rs->sr_err );