1 /* delete.c - bdb backend delete routine */
4 * Copyright 1998-2002 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 #ifdef LDAP_CLIENT_UPDATE
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 );
75 text = "internal error";
78 ldap_pvt_thread_yield();
81 /* begin transaction */
82 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
87 LDAP_LOG ( OPERATION, ERR,
88 "==> bdb_delete: txn_begin failed: %s (%d)\n",
89 db_strerror(rc), rc, 0 );
91 Debug( LDAP_DEBUG_TRACE,
92 "bdb_delete: txn_begin failed: %s (%d)\n",
93 db_strerror(rc), rc, 0 );
96 text = "internal error";
100 locker = TXN_ID ( ltid );
102 lockid = TXN_ID( ltid );
106 opinfo.boi_txn = ltid;
108 op->o_private = &opinfo;
110 if ( !be_issuffix( be, ndn ) ) {
111 dnParent( ndn, &pdn );
114 if( pdn.bv_len != 0 ) {
118 obj.data = pdn.bv_val-1;
119 obj.size = pdn.bv_len+1;
120 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
121 DB_LOCK_WRITE, &lock);
125 rc = bdb_dn2entry_r( be, ltid, &pdn, &p, NULL, 0, locker, &lock );
131 case DB_LOCK_DEADLOCK:
132 case DB_LOCK_NOTGRANTED:
135 text = "ldap server busy";
139 text = "internal error";
145 LDAP_LOG ( OPERATION, DETAIL1,
146 "<=- bdb_delete: parent does not exist\n", 0, 0, 0 );
148 Debug( LDAP_DEBUG_TRACE,
149 "<=- bdb_delete: parent does not exist\n",
153 text = "could not locate parent of entry";
157 /* check parent for "children" acl */
158 rc = access_allowed( be, conn, op, p,
159 children, NULL, ACL_WRITE, NULL );
161 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p);
164 switch( opinfo.boi_err ) {
165 case DB_LOCK_DEADLOCK:
166 case DB_LOCK_NOTGRANTED:
172 LDAP_LOG ( OPERATION, DETAIL1,
173 "<=- bdb_delete: no write access to parent\n", 0, 0, 0 );
175 Debug( LDAP_DEBUG_TRACE,
176 "<=- bdb_delete: no write access to parent\n",
179 rc = LDAP_INSUFFICIENT_ACCESS;
180 text = "no write access to parent";
185 /* no parent, must be root to delete */
186 if( ! be_isroot( be, &op->o_ndn ) ) {
187 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
188 || be_isupdate( be, &op->o_ndn ) ) {
189 p = (Entry *)&slap_entry_root;
191 /* check parent for "children" acl */
192 rc = access_allowed( be, conn, op, p,
193 children, NULL, ACL_WRITE, NULL );
197 switch( opinfo.boi_err ) {
198 case DB_LOCK_DEADLOCK:
199 case DB_LOCK_NOTGRANTED:
205 LDAP_LOG ( OPERATION, DETAIL1,
206 "<=- bdb_delete: no access to parent\n", 0, 0, 0 );
208 Debug( LDAP_DEBUG_TRACE,
209 "<=- bdb_delete: no access "
210 "to parent\n", 0, 0, 0 );
212 rc = LDAP_INSUFFICIENT_ACCESS;
213 text = "no write access to parent";
219 LDAP_LOG ( OPERATION, DETAIL1,
220 "<=- bdb_delete: no parent and not root\n", 0, 0, 0 );
222 Debug( LDAP_DEBUG_TRACE,
223 "<=- bdb_delete: no parent "
224 "and not root\n", 0, 0, 0);
226 rc = LDAP_INSUFFICIENT_ACCESS;
236 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
237 DB_LOCK_WRITE, &lock);
242 /* get entry for read/modify/write */
243 rc = bdb_dn2entry_w( be, ltid, ndn, &e, &matched, DB_RMW, locker, &lock );
249 case DB_LOCK_DEADLOCK:
250 case DB_LOCK_NOTGRANTED:
253 text = "ldap server busy";
257 text = "internal error";
262 char *matched_dn = NULL;
266 LDAP_LOG ( OPERATION, ARGS,
267 "<=- bdb_delete: no such object %s\n", dn->bv_val, 0, 0);
269 Debug( LDAP_DEBUG_ARGS,
270 "<=- bdb_delete: no such object %s\n",
274 if ( matched != NULL ) {
275 matched_dn = ch_strdup( matched->e_dn );
276 refs = is_entry_referral( matched )
277 ? get_entry_referrals( be, conn, op, matched )
279 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, matched);
283 refs = referral_rewrite( default_referral,
284 NULL, dn, LDAP_SCOPE_DEFAULT );
287 send_ldap_result( conn, op, LDAP_REFERRAL,
288 matched_dn, NULL, refs, NULL );
290 ber_bvarray_free( refs );
297 rc = access_allowed( be, conn, op, e,
298 entry, NULL, ACL_WRITE, NULL );
300 switch( opinfo.boi_err ) {
301 case DB_LOCK_DEADLOCK:
302 case DB_LOCK_NOTGRANTED:
308 LDAP_LOG ( OPERATION, DETAIL1,
309 "<=- bdb_delete: no write access to entry\n", 0, 0, 0 );
311 Debug( LDAP_DEBUG_TRACE,
312 "<=- bdb_delete: no write access to entry\n",
315 rc = LDAP_INSUFFICIENT_ACCESS;
316 text = "no write access to entry";
320 if ( !manageDSAit && is_entry_referral( e ) ) {
321 /* entry is a referral, don't allow delete */
322 BerVarray refs = get_entry_referrals( be,
326 LDAP_LOG ( OPERATION, DETAIL1,
327 "<=- bdb_delete: entry is referral\n", 0, 0, 0 );
329 Debug( LDAP_DEBUG_TRACE,
330 "bdb_delete: entry is referral\n",
334 send_ldap_result( conn, op, LDAP_REFERRAL,
335 e->e_dn, NULL, refs, NULL );
337 ber_bvarray_free( refs );
343 rc = bdb_dn2id_children( be, ltid, &e->e_nname, 0 );
344 if( rc != DB_NOTFOUND ) {
346 case DB_LOCK_DEADLOCK:
347 case DB_LOCK_NOTGRANTED:
351 LDAP_LOG ( OPERATION, DETAIL1,
352 "<=- bdb_delete: non-leaf %s\n", dn->bv_val, 0, 0 );
354 Debug(LDAP_DEBUG_ARGS,
355 "<=- bdb_delete: non-leaf %s\n",
358 rc = LDAP_NOT_ALLOWED_ON_NONLEAF;
359 text = "subtree delete not supported";
363 LDAP_LOG ( OPERATION, ERR,
364 "<=- bdb_delete: has_children failed %s (%d)\n",
365 db_strerror(rc), rc, 0 );
367 Debug(LDAP_DEBUG_ARGS,
368 "<=- bdb_delete: has_children failed: %s (%d)\n",
369 db_strerror(rc), rc, 0 );
372 text = "internal error";
377 /* delete from dn2id */
378 rc = bdb_dn2id_delete( be, ltid, pdn.bv_val, e );
381 case DB_LOCK_DEADLOCK:
382 case DB_LOCK_NOTGRANTED:
388 LDAP_LOG ( OPERATION, ERR,
389 "<=- bdb_delete: dn2id failed %s (%d)\n", db_strerror(rc), rc, 0 );
391 Debug(LDAP_DEBUG_ARGS,
392 "<=- bdb_delete: dn2id failed: %s (%d)\n",
393 db_strerror(rc), rc, 0 );
395 text = "DN index delete failed";
399 /* delete from id2entry */
400 rc = bdb_id2entry_delete( be, ltid, e );
403 case DB_LOCK_DEADLOCK:
404 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";
422 /* delete indices for old attributes */
423 rc = bdb_index_entry_del( be, ltid, e, e->e_attrs );
424 if ( rc != LDAP_SUCCESS ) {
426 case DB_LOCK_DEADLOCK:
427 case DB_LOCK_NOTGRANTED:
433 LDAP_LOG ( OPERATION, ERR,
434 "<=- bdb_delete: entry index delete failed!\n", 0, 0, 0 );
436 Debug( LDAP_DEBUG_ANY, "entry index delete failed!\n",
439 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 #ifdef LDAP_CLIENT_UPDATE
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, LCUP_PSEARCH_BY_DELETE );
502 #endif /* LDAP_CLIENT_UPDATE */
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 );