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 int manageDSAit = get_manageDSAit( op );
25 AttributeDescription *children = slap_schema.si_ad_children;
26 AttributeDescription *entry = slap_schema.si_ad_entry;
28 struct bdb_op_info opinfo;
35 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
40 LDAP_LOG ( OPERATION, ARGS, "==> bdb_delete: %s\n", op->o_req_dn.bv_val, 0, 0 );
42 Debug( LDAP_DEBUG_ARGS, "==> bdb_delete: %s\n",
43 op->o_req_dn.bv_val, 0, 0 );
47 retry: /* transaction retry */
49 bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
52 LDAP_LOG ( OPERATION, DETAIL1,
53 "==> bdb_delete: retrying...\n", 0, 0, 0 );
55 Debug( LDAP_DEBUG_TRACE, "==> bdb_delete: retrying...\n",
58 rs->sr_err = TXN_ABORT( ltid );
61 op->o_do_not_cache = opinfo.boi_acl_cache;
62 if( rs->sr_err != 0 ) {
63 rs->sr_err = LDAP_OTHER;
64 rs->sr_text = "internal error";
67 ldap_pvt_thread_yield();
70 /* begin transaction */
71 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
74 if( rs->sr_err != 0 ) {
76 LDAP_LOG ( OPERATION, ERR,
77 "==> bdb_delete: txn_begin failed: %s (%d)\n",
78 db_strerror(rs->sr_err), rs->sr_err, 0 );
80 Debug( LDAP_DEBUG_TRACE,
81 "bdb_delete: txn_begin failed: %s (%d)\n",
82 db_strerror(rs->sr_err), rs->sr_err, 0 );
84 rs->sr_err = LDAP_OTHER;
85 rs->sr_text = "internal error";
89 locker = TXN_ID ( ltid );
91 opinfo.boi_bdb = op->o_bd;
92 opinfo.boi_txn = ltid;
93 opinfo.boi_locker = locker;
95 opinfo.boi_acl_cache = op->o_do_not_cache;
96 op->o_private = &opinfo;
98 if ( !be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
99 dnParent( &op->o_req_ndn, &pdn );
102 if( pdn.bv_len != 0 ) {
104 rs->sr_err = bdb_dn2entry_r( op->o_bd, ltid, &pdn, &p, NULL, 0, locker, &lock );
106 switch( rs->sr_err ) {
110 case DB_LOCK_DEADLOCK:
111 case DB_LOCK_NOTGRANTED:
114 rs->sr_text = "ldap server busy";
117 rs->sr_err = LDAP_OTHER;
118 rs->sr_text = "internal error";
124 LDAP_LOG ( OPERATION, DETAIL1,
125 "<=- bdb_delete: parent does not exist\n", 0, 0, 0 );
127 Debug( LDAP_DEBUG_TRACE,
128 "<=- bdb_delete: parent does not exist\n",
131 rs->sr_err = LDAP_OTHER;
132 rs->sr_text = "could not locate parent of entry";
136 /* check parent for "children" acl */
137 rs->sr_err = access_allowed( op, p,
138 children, NULL, ACL_WRITE, NULL );
140 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p);
144 switch( opinfo.boi_err ) {
145 case DB_LOCK_DEADLOCK:
146 case DB_LOCK_NOTGRANTED:
151 LDAP_LOG ( OPERATION, DETAIL1,
152 "<=- bdb_delete: no write access to parent\n", 0, 0, 0 );
154 Debug( LDAP_DEBUG_TRACE,
155 "<=- bdb_delete: no write access to parent\n",
158 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
159 rs->sr_text = "no write access to parent";
164 /* no parent, must be root to delete */
165 if( ! be_isroot( op->o_bd, &op->o_ndn ) ) {
166 if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv )
167 || be_isupdate( op->o_bd, &op->o_ndn ) ) {
168 p = (Entry *)&slap_entry_root;
170 /* check parent for "children" acl */
171 rs->sr_err = access_allowed( op, p,
172 children, NULL, ACL_WRITE, NULL );
177 switch( opinfo.boi_err ) {
178 case DB_LOCK_DEADLOCK:
179 case DB_LOCK_NOTGRANTED:
184 LDAP_LOG ( OPERATION, DETAIL1,
185 "<=- bdb_delete: no access to parent\n", 0, 0, 0 );
187 Debug( LDAP_DEBUG_TRACE,
188 "<=- bdb_delete: no access "
189 "to parent\n", 0, 0, 0 );
191 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
192 rs->sr_text = "no write access to parent";
198 LDAP_LOG ( OPERATION, DETAIL1,
199 "<=- bdb_delete: no parent and not root\n", 0, 0, 0 );
201 Debug( LDAP_DEBUG_TRACE,
202 "<=- bdb_delete: no parent "
203 "and not root\n", 0, 0, 0);
205 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
211 /* get entry for read/modify/write */
212 rs->sr_err = bdb_dn2entry_w( op->o_bd, ltid, &op->o_req_ndn, &e, &matched, DB_RMW, locker, &lock );
214 switch( rs->sr_err ) {
218 case DB_LOCK_DEADLOCK:
219 case DB_LOCK_NOTGRANTED:
222 rs->sr_text = "ldap server busy";
225 rs->sr_err = LDAP_OTHER;
226 rs->sr_text = "internal error";
232 LDAP_LOG ( OPERATION, ARGS,
233 "<=- bdb_delete: no such object %s\n", op->o_req_dn.bv_val, 0, 0);
235 Debug( LDAP_DEBUG_ARGS,
236 "<=- bdb_delete: no such object %s\n",
237 op->o_req_dn.bv_val, 0, 0);
240 if ( matched != NULL ) {
241 rs->sr_matched = ch_strdup( matched->e_dn );
242 rs->sr_ref = is_entry_referral( matched )
243 ? get_entry_referrals( op, matched )
245 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, matched);
249 rs->sr_ref = referral_rewrite( default_referral,
250 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
253 rs->sr_err = LDAP_REFERRAL;
254 send_ldap_result( op, rs );
256 ber_bvarray_free( rs->sr_ref );
257 free( (char *)rs->sr_matched );
259 rs->sr_matched = NULL;
265 rs->sr_err = access_allowed( op, e,
266 entry, NULL, ACL_WRITE, NULL );
269 switch( opinfo.boi_err ) {
270 case DB_LOCK_DEADLOCK:
271 case DB_LOCK_NOTGRANTED:
276 LDAP_LOG ( OPERATION, DETAIL1,
277 "<=- bdb_delete: no write access to entry\n", 0, 0, 0 );
279 Debug( LDAP_DEBUG_TRACE,
280 "<=- bdb_delete: no write access to entry\n",
283 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
284 rs->sr_text = "no write access to entry";
288 if ( !manageDSAit && is_entry_referral( e ) ) {
289 /* entry is a referral, don't allow delete */
290 rs->sr_ref = get_entry_referrals( op, e );
293 LDAP_LOG ( OPERATION, DETAIL1,
294 "<=- bdb_delete: entry is referral\n", 0, 0, 0 );
296 Debug( LDAP_DEBUG_TRACE,
297 "bdb_delete: entry is referral\n",
301 rs->sr_err = LDAP_REFERRAL;
302 rs->sr_matched = e->e_name.bv_val;
303 send_ldap_result( op, rs );
305 ber_bvarray_free( rs->sr_ref );
307 rs->sr_matched = NULL;
313 rs->sr_err = bdb_dn2id_children( op->o_bd, ltid, &e->e_nname, 0 );
314 if( rs->sr_err != DB_NOTFOUND ) {
315 switch( rs->sr_err ) {
316 case DB_LOCK_DEADLOCK:
317 case DB_LOCK_NOTGRANTED:
321 LDAP_LOG ( OPERATION, DETAIL1,
322 "<=- bdb_delete: non-leaf %s\n", op->o_req_dn.bv_val, 0, 0 );
324 Debug(LDAP_DEBUG_ARGS,
325 "<=- bdb_delete: non-leaf %s\n",
326 op->o_req_dn.bv_val, 0, 0);
328 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
329 rs->sr_text = "subtree delete not supported";
333 LDAP_LOG ( OPERATION, ERR,
334 "<=- bdb_delete: has_children failed %s (%d)\n",
335 db_strerror(rs->sr_err), rs->sr_err, 0 );
337 Debug(LDAP_DEBUG_ARGS,
338 "<=- bdb_delete: has_children failed: %s (%d)\n",
339 db_strerror(rs->sr_err), rs->sr_err, 0 );
341 rs->sr_err = LDAP_OTHER;
342 rs->sr_text = "internal error";
347 /* delete from dn2id */
348 rs->sr_err = bdb_dn2id_delete( op->o_bd, ltid, pdn.bv_val, e );
349 if ( rs->sr_err != 0 ) {
350 switch( rs->sr_err ) {
351 case DB_LOCK_DEADLOCK:
352 case DB_LOCK_NOTGRANTED:
356 LDAP_LOG ( OPERATION, ERR,
357 "<=- bdb_delete: dn2id failed %s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
359 Debug(LDAP_DEBUG_ARGS,
360 "<=- bdb_delete: dn2id failed: %s (%d)\n",
361 db_strerror(rs->sr_err), rs->sr_err, 0 );
363 rs->sr_text = "DN index delete failed";
364 rs->sr_err = LDAP_OTHER;
368 /* delete from id2entry */
369 rs->sr_err = bdb_id2entry_delete( op->o_bd, ltid, e );
370 if ( rs->sr_err != 0 ) {
371 switch( rs->sr_err ) {
372 case DB_LOCK_DEADLOCK:
373 case DB_LOCK_NOTGRANTED:
377 LDAP_LOG ( OPERATION, ERR,
378 "<=- bdb_delete: id2entry failed: %s (%d)\n",
379 db_strerror(rs->sr_err), rs->sr_err, 0 );
381 Debug(LDAP_DEBUG_ARGS,
382 "<=- bdb_delete: id2entry failed: %s (%d)\n",
383 db_strerror(rs->sr_err), rs->sr_err, 0 );
385 rs->sr_text = "entry delete failed";
386 rs->sr_err = LDAP_OTHER;
390 /* delete indices for old attributes */
391 rs->sr_err = bdb_index_entry_del( op->o_bd, ltid, e );
392 if ( rs->sr_err != LDAP_SUCCESS ) {
393 switch( rs->sr_err ) {
394 case DB_LOCK_DEADLOCK:
395 case DB_LOCK_NOTGRANTED:
399 LDAP_LOG ( OPERATION, ERR,
400 "<=- bdb_delete: entry index delete failed!\n", 0, 0, 0 );
402 Debug( LDAP_DEBUG_ANY, "entry index delete failed!\n",
405 rs->sr_text = "entry index delete failed";
406 rs->sr_err = LDAP_OTHER;
410 #if 0 /* Do we want to reclaim deleted IDs? */
411 ldap_pvt_thread_mutex_lock( &bdb->bi_lastid_mutex );
412 if ( e->e_id == bdb->bi_lastid ) {
413 bdb_last_id( op->o_bd, ltid );
415 ldap_pvt_thread_mutex_unlock( &bdb->bi_lastid_mutex );
419 if ( ( rs->sr_err = TXN_ABORT( ltid ) ) != 0 ) {
420 rs->sr_text = "txn_abort (no-op) failed";
423 rs->sr_err = LDAP_SUCCESS;
426 rs->sr_err = TXN_COMMIT( ltid, 0 );
429 op->o_private = NULL;
431 if( rs->sr_err != 0 ) {
433 LDAP_LOG ( OPERATION, ERR,
434 "bdb_delete: txn_%s failed: %s (%d)\n",
435 op->o_noop ? "abort (no-op)" : "commit", db_strerror(rs->sr_err), rs->sr_err );
437 Debug( LDAP_DEBUG_TRACE,
438 "bdb_delete: txn_%s failed: %s (%d)\n",
439 op->o_noop ? "abort (no-op)" : "commit",
440 db_strerror(rs->sr_err), rs->sr_err );
442 rs->sr_err = LDAP_OTHER;
443 rs->sr_text = "commit failed";
447 LDAP_LOG ( OPERATION, RESULTS,
448 "bdb_delete: deleted%s id=%08lx db=\"%s\"\n",
449 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
451 Debug( LDAP_DEBUG_TRACE,
452 "bdb_delete: deleted%s id=%08lx dn=\"%s\"\n",
453 op->o_noop ? " (no-op)" : "",
456 rs->sr_err = LDAP_SUCCESS;
461 send_ldap_result( op, rs );
463 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
464 if ( rs->sr_err == LDAP_SUCCESS && !noop ) {
465 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
466 bdb_psearch( op, rs, ps_list, e, LDAP_PSEARCH_BY_DELETE );
471 if(rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp ) {
472 ldap_pvt_thread_yield();
473 TXN_CHECKPOINT( bdb->bi_dbenv,
474 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
480 bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
485 op->o_private = NULL;
488 return ( ( rs->sr_err == LDAP_SUCCESS ) ? noop : rs->sr_err );