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>
25 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
27 struct berval pdn = {0, NULL};
32 int manageDSAit = get_manageDSAit( op );
33 AttributeDescription *children = slap_schema.si_ad_children;
34 AttributeDescription *entry = slap_schema.si_ad_entry;
36 struct bdb_op_info opinfo;
47 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
52 LDAP_LOG ( OPERATION, ARGS, "==> bdb_delete: %s\n", dn->bv_val, 0, 0 );
54 Debug( LDAP_DEBUG_ARGS, "==> bdb_delete: %s\n",
59 retry: /* transaction retry */
61 bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
64 LDAP_LOG ( OPERATION, DETAIL1,
65 "==> bdb_delete: retrying...\n", 0, 0, 0 );
67 Debug( LDAP_DEBUG_TRACE, "==> bdb_delete: retrying...\n",
70 rc = TXN_ABORT( ltid );
73 op->o_do_not_cache = opinfo.boi_acl_cache;
76 text = "internal error";
79 ldap_pvt_thread_yield();
82 /* begin transaction */
83 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
88 LDAP_LOG ( OPERATION, ERR,
89 "==> bdb_delete: txn_begin failed: %s (%d)\n",
90 db_strerror(rc), rc, 0 );
92 Debug( LDAP_DEBUG_TRACE,
93 "bdb_delete: txn_begin failed: %s (%d)\n",
94 db_strerror(rc), rc, 0 );
97 text = "internal error";
101 locker = TXN_ID ( ltid );
103 lockid = TXN_ID( ltid );
107 opinfo.boi_txn = ltid;
108 opinfo.boi_locker = locker;
110 opinfo.boi_acl_cache = op->o_do_not_cache;
111 op->o_private = &opinfo;
113 if ( !be_issuffix( be, ndn ) ) {
114 dnParent( ndn, &pdn );
117 if( pdn.bv_len != 0 ) {
121 obj.data = pdn.bv_val-1;
122 obj.size = pdn.bv_len+1;
123 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
124 DB_LOCK_WRITE, &lock);
128 rc = bdb_dn2entry_r( be, ltid, &pdn, &p, NULL, 0, locker, &lock );
134 case DB_LOCK_DEADLOCK:
135 case DB_LOCK_NOTGRANTED:
138 text = "ldap server busy";
142 text = "internal error";
148 LDAP_LOG ( OPERATION, DETAIL1,
149 "<=- bdb_delete: parent does not exist\n", 0, 0, 0 );
151 Debug( LDAP_DEBUG_TRACE,
152 "<=- bdb_delete: parent does not exist\n",
156 text = "could not locate parent of entry";
160 /* check parent for "children" acl */
161 rc = access_allowed( be, conn, op, p,
162 children, NULL, ACL_WRITE, NULL );
164 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p);
168 switch( opinfo.boi_err ) {
169 case DB_LOCK_DEADLOCK:
170 case DB_LOCK_NOTGRANTED:
175 LDAP_LOG ( OPERATION, DETAIL1,
176 "<=- bdb_delete: no write access to parent\n", 0, 0, 0 );
178 Debug( LDAP_DEBUG_TRACE,
179 "<=- bdb_delete: no write access to parent\n",
182 rc = LDAP_INSUFFICIENT_ACCESS;
183 text = "no write access to parent";
188 /* no parent, must be root to delete */
189 if( ! be_isroot( be, &op->o_ndn ) ) {
190 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
191 || be_isupdate( be, &op->o_ndn ) ) {
192 p = (Entry *)&slap_entry_root;
194 /* check parent for "children" acl */
195 rc = access_allowed( be, conn, op, p,
196 children, NULL, ACL_WRITE, NULL );
201 switch( opinfo.boi_err ) {
202 case DB_LOCK_DEADLOCK:
203 case DB_LOCK_NOTGRANTED:
208 LDAP_LOG ( OPERATION, DETAIL1,
209 "<=- bdb_delete: no access to parent\n", 0, 0, 0 );
211 Debug( LDAP_DEBUG_TRACE,
212 "<=- bdb_delete: no access "
213 "to parent\n", 0, 0, 0 );
215 rc = LDAP_INSUFFICIENT_ACCESS;
216 text = "no write access to parent";
222 LDAP_LOG ( OPERATION, DETAIL1,
223 "<=- bdb_delete: no parent and not root\n", 0, 0, 0 );
225 Debug( LDAP_DEBUG_TRACE,
226 "<=- bdb_delete: no parent "
227 "and not root\n", 0, 0, 0);
229 rc = LDAP_INSUFFICIENT_ACCESS;
239 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
240 DB_LOCK_WRITE, &lock);
245 /* get entry for read/modify/write */
246 rc = bdb_dn2entry_w( be, ltid, ndn, &e, &matched, DB_RMW, locker, &lock );
252 case DB_LOCK_DEADLOCK:
253 case DB_LOCK_NOTGRANTED:
256 text = "ldap server busy";
260 text = "internal error";
265 char *matched_dn = NULL;
269 LDAP_LOG ( OPERATION, ARGS,
270 "<=- bdb_delete: no such object %s\n", dn->bv_val, 0, 0);
272 Debug( LDAP_DEBUG_ARGS,
273 "<=- bdb_delete: no such object %s\n",
277 if ( matched != NULL ) {
278 matched_dn = ch_strdup( matched->e_dn );
279 refs = is_entry_referral( matched )
280 ? get_entry_referrals( be, conn, op, matched )
282 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, matched);
286 refs = referral_rewrite( default_referral,
287 NULL, dn, LDAP_SCOPE_DEFAULT );
290 send_ldap_result( conn, op, LDAP_REFERRAL,
291 matched_dn, NULL, refs, NULL );
293 ber_bvarray_free( refs );
300 rc = access_allowed( be, conn, op, e,
301 entry, NULL, ACL_WRITE, NULL );
304 switch( opinfo.boi_err ) {
305 case DB_LOCK_DEADLOCK:
306 case DB_LOCK_NOTGRANTED:
311 LDAP_LOG ( OPERATION, DETAIL1,
312 "<=- bdb_delete: no write access to entry\n", 0, 0, 0 );
314 Debug( LDAP_DEBUG_TRACE,
315 "<=- bdb_delete: no write access to entry\n",
318 rc = LDAP_INSUFFICIENT_ACCESS;
319 text = "no write access to entry";
323 if ( !manageDSAit && is_entry_referral( e ) ) {
324 /* entry is a referral, don't allow delete */
325 BerVarray refs = get_entry_referrals( be,
329 LDAP_LOG ( OPERATION, DETAIL1,
330 "<=- bdb_delete: entry is referral\n", 0, 0, 0 );
332 Debug( LDAP_DEBUG_TRACE,
333 "bdb_delete: entry is referral\n",
337 send_ldap_result( conn, op, LDAP_REFERRAL,
338 e->e_dn, NULL, refs, NULL );
340 ber_bvarray_free( refs );
346 rc = bdb_dn2id_children( be, ltid, &e->e_nname, 0 );
347 if( rc != DB_NOTFOUND ) {
349 case DB_LOCK_DEADLOCK:
350 case DB_LOCK_NOTGRANTED:
354 LDAP_LOG ( OPERATION, DETAIL1,
355 "<=- bdb_delete: non-leaf %s\n", dn->bv_val, 0, 0 );
357 Debug(LDAP_DEBUG_ARGS,
358 "<=- bdb_delete: non-leaf %s\n",
361 rc = LDAP_NOT_ALLOWED_ON_NONLEAF;
362 text = "subtree delete not supported";
366 LDAP_LOG ( OPERATION, ERR,
367 "<=- bdb_delete: has_children failed %s (%d)\n",
368 db_strerror(rc), rc, 0 );
370 Debug(LDAP_DEBUG_ARGS,
371 "<=- bdb_delete: has_children failed: %s (%d)\n",
372 db_strerror(rc), rc, 0 );
375 text = "internal error";
380 /* delete from dn2id */
381 rc = bdb_dn2id_delete( be, ltid, pdn.bv_val, e );
384 case DB_LOCK_DEADLOCK:
385 case DB_LOCK_NOTGRANTED:
389 LDAP_LOG ( OPERATION, ERR,
390 "<=- bdb_delete: dn2id failed %s (%d)\n", db_strerror(rc), rc, 0 );
392 Debug(LDAP_DEBUG_ARGS,
393 "<=- bdb_delete: dn2id failed: %s (%d)\n",
394 db_strerror(rc), rc, 0 );
396 text = "DN index delete failed";
401 /* delete from id2entry */
402 rc = bdb_id2entry_delete( be, ltid, e );
405 case DB_LOCK_DEADLOCK:
406 case DB_LOCK_NOTGRANTED:
410 LDAP_LOG ( OPERATION, ERR,
411 "<=- bdb_delete: id2entry failed: %s (%d)\n",
412 db_strerror(rc), rc, 0 );
414 Debug(LDAP_DEBUG_ARGS,
415 "<=- bdb_delete: id2entry failed: %s (%d)\n",
416 db_strerror(rc), rc, 0 );
418 text = "entry delete failed";
423 /* delete indices for old attributes */
424 rc = bdb_index_entry_del( be, ltid, e, e->e_attrs );
425 if ( rc != LDAP_SUCCESS ) {
427 case DB_LOCK_DEADLOCK:
428 case DB_LOCK_NOTGRANTED:
432 LDAP_LOG ( OPERATION, ERR,
433 "<=- bdb_delete: entry index delete failed!\n", 0, 0, 0 );
435 Debug( LDAP_DEBUG_ANY, "entry index delete failed!\n",
438 text = "entry index delete failed";
443 #if 0 /* Do we want to reclaim deleted IDs? */
444 ldap_pvt_thread_mutex_lock( &bdb->bi_lastid_mutex );
445 if ( e->e_id == bdb->bi_lastid ) {
446 bdb_last_id( be, ltid );
448 ldap_pvt_thread_mutex_unlock( &bdb->bi_lastid_mutex );
452 if ( ( rc = TXN_ABORT( ltid ) ) != 0 ) {
453 text = "txn_abort (no-op) failed";
459 rc = TXN_COMMIT( ltid, 0 );
462 op->o_private = NULL;
466 LDAP_LOG ( OPERATION, ERR,
467 "bdb_delete: txn_%s failed: %s (%d)\n",
468 op->o_noop ? "abort (no-op)" : "commit", db_strerror(rc), rc );
470 Debug( LDAP_DEBUG_TRACE,
471 "bdb_delete: txn_%s failed: %s (%d)\n",
472 op->o_noop ? "abort (no-op)" : "commit",
473 db_strerror(rc), rc );
476 text = "commit failed";
480 LDAP_LOG ( OPERATION, RESULTS,
481 "bdb_delete: deleted%s id=%08lx db=\"%s\"\n",
482 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
484 Debug( LDAP_DEBUG_TRACE,
485 "bdb_delete: deleted%s id=%08lx dn=\"%s\"\n",
486 op->o_noop ? " (no-op)" : "",
494 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
496 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
497 if ( rc == LDAP_SUCCESS && !noop ) {
498 LDAP_LIST_FOREACH( ps_list, &bdb->psearch_list, link ) {
499 bdb_psearch( be, conn, op, ps_list, e, LDAP_PSEARCH_BY_DELETE );
504 if(rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
505 ldap_pvt_thread_yield();
506 TXN_CHECKPOINT( bdb->bi_dbenv,
507 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
513 bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
518 op->o_private = NULL;
521 return ( ( rc == LDAP_SUCCESS ) ? noop : rc );