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;
35 struct bdb_op_info opinfo;
47 LDAP_LOG ( OPERATION, ARGS, "==> bdb_delete: %s\n", dn->bv_val, 0, 0 );
49 Debug( LDAP_DEBUG_ARGS, "==> bdb_delete: %s\n",
54 retry: /* transaction retry */
56 bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
59 LDAP_LOG ( OPERATION, DETAIL1,
60 "==> bdb_delete: retrying...\n", 0, 0, 0 );
62 Debug( LDAP_DEBUG_TRACE, "==> bdb_delete: retrying...\n",
65 rc = TXN_ABORT( ltid );
70 text = "internal error";
73 ldap_pvt_thread_yield();
76 /* begin transaction */
77 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
82 LDAP_LOG ( OPERATION, ERR,
83 "==> bdb_delete: txn_begin failed: %s (%d)\n",
84 db_strerror(rc), rc, 0 );
86 Debug( LDAP_DEBUG_TRACE,
87 "bdb_delete: txn_begin failed: %s (%d)\n",
88 db_strerror(rc), rc, 0 );
91 text = "internal error";
95 locker = TXN_ID ( ltid );
97 lockid = TXN_ID( ltid );
101 opinfo.boi_txn = ltid;
103 op->o_private = &opinfo;
105 if ( !be_issuffix( be, ndn ) ) {
106 dnParent( ndn, &pdn );
109 if( pdn.bv_len != 0 ) {
113 obj.data = pdn.bv_val-1;
114 obj.size = pdn.bv_len+1;
115 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
116 DB_LOCK_WRITE, &lock);
120 rc = bdb_dn2entry_r( be, ltid, &pdn, &p, NULL, 0, locker, &lock );
126 case DB_LOCK_DEADLOCK:
127 case DB_LOCK_NOTGRANTED:
130 text = "ldap server busy";
134 text = "internal error";
140 LDAP_LOG ( OPERATION, DETAIL1,
141 "<=- bdb_delete: parent does not exist\n", 0, 0, 0 );
143 Debug( LDAP_DEBUG_TRACE,
144 "<=- bdb_delete: parent does not exist\n",
148 text = "could not locate parent of entry";
152 /* check parent for "children" acl */
153 rc = access_allowed( be, conn, op, p,
154 children, NULL, ACL_WRITE, NULL );
156 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p);
159 switch( opinfo.boi_err ) {
160 case DB_LOCK_DEADLOCK:
161 case DB_LOCK_NOTGRANTED:
167 LDAP_LOG ( OPERATION, DETAIL1,
168 "<=- bdb_delete: no access to parent\n", 0, 0, 0 );
170 Debug( LDAP_DEBUG_TRACE,
171 "<=- bdb_delete: no access to parent\n",
174 rc = LDAP_INSUFFICIENT_ACCESS;
179 /* no parent, must be root to delete */
180 if( ! be_isroot( be, &op->o_ndn ) ) {
181 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
182 || be_isupdate( be, &op->o_ndn ) ) {
183 p = (Entry *)&slap_entry_root;
185 /* check parent for "children" acl */
186 rc = access_allowed( be, conn, op, p,
187 children, NULL, ACL_WRITE, NULL );
190 switch( opinfo.boi_err ) {
191 case DB_LOCK_DEADLOCK:
192 case DB_LOCK_NOTGRANTED:
198 LDAP_LOG ( OPERATION, DETAIL1,
199 "<=- bdb_delete: no access to parent\n", 0, 0, 0 );
201 Debug( LDAP_DEBUG_TRACE,
202 "<=- bdb_delete: no access "
203 "to parent\n", 0, 0, 0 );
205 rc = LDAP_INSUFFICIENT_ACCESS;
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 rc = LDAP_INSUFFICIENT_ACCESS;
228 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
229 DB_LOCK_WRITE, &lock);
234 /* get entry for read/modify/write */
235 rc = bdb_dn2entry_w( be, ltid, ndn, &e, &matched, DB_RMW, locker, &lock );
241 case DB_LOCK_DEADLOCK:
242 case DB_LOCK_NOTGRANTED:
245 text = "ldap server busy";
249 text = "internal error";
254 char *matched_dn = NULL;
258 LDAP_LOG ( OPERATION, ARGS,
259 "<=- bdb_delete: no such object %s\n", dn->bv_val, 0, 0);
261 Debug( LDAP_DEBUG_ARGS,
262 "<=- bdb_delete: no such object %s\n",
266 if ( matched != NULL ) {
267 matched_dn = ch_strdup( matched->e_dn );
268 refs = is_entry_referral( matched )
269 ? get_entry_referrals( be, conn, op, matched )
271 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, matched);
275 refs = referral_rewrite( default_referral,
276 NULL, dn, LDAP_SCOPE_DEFAULT );
279 send_ldap_result( conn, op, LDAP_REFERRAL,
280 matched_dn, NULL, refs, NULL );
282 ber_bvarray_free( refs );
289 if ( !manageDSAit && is_entry_referral( e ) ) {
290 /* entry is a referral, don't allow delete */
291 BerVarray refs = get_entry_referrals( be,
295 LDAP_LOG ( OPERATION, DETAIL1,
296 "<=- bdb_delete: entry is referral\n", 0, 0, 0 );
298 Debug( LDAP_DEBUG_TRACE,
299 "bdb_delete: entry is referral\n",
303 send_ldap_result( conn, op, LDAP_REFERRAL,
304 e->e_dn, NULL, refs, NULL );
306 ber_bvarray_free( refs );
312 rc = bdb_dn2id_children( be, ltid, &e->e_nname, 0 );
313 if( rc != DB_NOTFOUND ) {
315 case DB_LOCK_DEADLOCK:
316 case DB_LOCK_NOTGRANTED:
320 LDAP_LOG ( OPERATION, DETAIL1,
321 "<=- bdb_delete: non-leaf %s\n", dn->bv_val, 0, 0 );
323 Debug(LDAP_DEBUG_ARGS,
324 "<=- bdb_delete: non-leaf %s\n",
327 rc = LDAP_NOT_ALLOWED_ON_NONLEAF;
328 text = "subtree delete not supported";
332 LDAP_LOG ( OPERATION, ERR,
333 "<=- bdb_delete: has_children failed %s (%d)\n",
334 db_strerror(rc), rc, 0 );
336 Debug(LDAP_DEBUG_ARGS,
337 "<=- bdb_delete: has_children failed: %s (%d)\n",
338 db_strerror(rc), rc, 0 );
341 text = "internal error";
346 /* delete from dn2id */
347 rc = bdb_dn2id_delete( be, ltid, pdn.bv_val, e );
350 case DB_LOCK_DEADLOCK:
351 case DB_LOCK_NOTGRANTED:
357 LDAP_LOG ( OPERATION, ERR,
358 "<=- bdb_delete: dn2id failed %s (%d)\n", db_strerror(rc), rc, 0 );
360 Debug(LDAP_DEBUG_ARGS,
361 "<=- bdb_delete: dn2id failed: %s (%d)\n",
362 db_strerror(rc), rc, 0 );
364 text = "DN index delete failed";
368 /* delete from id2entry */
369 rc = bdb_id2entry_delete( be, ltid, e );
372 case DB_LOCK_DEADLOCK:
373 case DB_LOCK_NOTGRANTED:
379 LDAP_LOG ( OPERATION, ERR,
380 "<=- bdb_delete: id2entry failed: %s (%d)\n",
381 db_strerror(rc), rc, 0 );
383 Debug(LDAP_DEBUG_ARGS,
384 "<=- bdb_delete: id2entry failed: %s (%d)\n",
385 db_strerror(rc), rc, 0 );
387 text = "entry delete failed";
391 /* delete indices for old attributes */
392 rc = bdb_index_entry_del( be, ltid, e, e->e_attrs );
393 if ( rc != LDAP_SUCCESS ) {
395 case DB_LOCK_DEADLOCK:
396 case DB_LOCK_NOTGRANTED:
402 LDAP_LOG ( OPERATION, ERR,
403 "<=- bdb_delete: entry index delete failed!\n", 0, 0, 0 );
405 Debug( LDAP_DEBUG_ANY, "entry index delete failed!\n",
408 text = "entry index delete failed";
412 #if 0 /* Do we want to reclaim deleted IDs? */
413 ldap_pvt_thread_mutex_lock( &bdb->bi_lastid_mutex );
414 if ( e->e_id == bdb->bi_lastid ) {
415 bdb_last_id( be, ltid );
417 ldap_pvt_thread_mutex_unlock( &bdb->bi_lastid_mutex );
421 if ( ( rc = TXN_ABORT( ltid ) ) != 0 ) {
422 text = "txn_abort (no-op) failed";
428 rc = TXN_COMMIT( ltid, 0 );
431 op->o_private = NULL;
435 LDAP_LOG ( OPERATION, ERR,
436 "bdb_delete: txn_%s failed: %s (%d)\n",
437 op->o_noop ? "abort (no-op)" : "commit", db_strerror(rc), rc );
439 Debug( LDAP_DEBUG_TRACE,
440 "bdb_delete: txn_%s failed: %s (%d)\n",
441 op->o_noop ? "abort (no-op)" : "commit",
442 db_strerror(rc), rc );
445 text = "commit failed";
449 LDAP_LOG ( OPERATION, RESULTS,
450 "bdb_delete: deleted%s id=%08lx db=\"%s\"\n",
451 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
453 Debug( LDAP_DEBUG_TRACE,
454 "bdb_delete: deleted%s id=%08lx dn=\"%s\"\n",
455 op->o_noop ? " (no-op)" : "",
463 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
465 if(rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
466 ldap_pvt_thread_yield();
467 TXN_CHECKPOINT( bdb->bi_dbenv,
468 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
474 bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
479 op->o_private = NULL;
482 return ( ( rc == LDAP_SUCCESS ) ? noop : rc );