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;
48 LDAP_LOG ( OPERATION, ARGS, "==> bdb_delete: %s\n", dn->bv_val, 0, 0 );
50 Debug( LDAP_DEBUG_ARGS, "==> bdb_delete: %s\n",
55 retry: /* transaction retry */
57 bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
60 LDAP_LOG ( OPERATION, DETAIL1,
61 "==> bdb_delete: retrying...\n", 0, 0, 0 );
63 Debug( LDAP_DEBUG_TRACE, "==> bdb_delete: retrying...\n",
66 rc = TXN_ABORT( ltid );
71 text = "internal error";
74 ldap_pvt_thread_yield();
77 /* begin transaction */
78 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
83 LDAP_LOG ( OPERATION, ERR,
84 "==> bdb_delete: txn_begin failed: %s (%d)\n",
85 db_strerror(rc), rc, 0 );
87 Debug( LDAP_DEBUG_TRACE,
88 "bdb_delete: txn_begin failed: %s (%d)\n",
89 db_strerror(rc), rc, 0 );
92 text = "internal error";
96 locker = TXN_ID ( ltid );
98 lockid = TXN_ID( ltid );
102 opinfo.boi_txn = ltid;
104 op->o_private = &opinfo;
106 if ( !be_issuffix( be, ndn ) ) {
107 dnParent( ndn, &pdn );
110 if( pdn.bv_len != 0 ) {
114 obj.data = pdn.bv_val-1;
115 obj.size = pdn.bv_len+1;
116 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
117 DB_LOCK_WRITE, &lock);
121 rc = bdb_dn2entry_r( be, ltid, &pdn, &p, NULL, 0, locker, &lock );
127 case DB_LOCK_DEADLOCK:
128 case DB_LOCK_NOTGRANTED:
131 text = "ldap server busy";
135 text = "internal error";
141 LDAP_LOG ( OPERATION, DETAIL1,
142 "<=- bdb_delete: parent does not exist\n", 0, 0, 0 );
144 Debug( LDAP_DEBUG_TRACE,
145 "<=- bdb_delete: parent does not exist\n",
149 text = "could not locate parent of entry";
153 /* check parent for "children" acl */
154 rc = access_allowed( be, conn, op, p,
155 children, NULL, ACL_WRITE, NULL );
157 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p);
160 switch( opinfo.boi_err ) {
161 case DB_LOCK_DEADLOCK:
162 case DB_LOCK_NOTGRANTED:
168 LDAP_LOG ( OPERATION, DETAIL1,
169 "<=- bdb_delete: no write access to parent\n", 0, 0, 0 );
171 Debug( LDAP_DEBUG_TRACE,
172 "<=- bdb_delete: no write access to parent\n",
175 rc = LDAP_INSUFFICIENT_ACCESS;
176 text = "no write access to parent";
181 /* no parent, must be root to delete */
182 if( ! be_isroot( be, &op->o_ndn ) ) {
183 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
184 || be_isupdate( be, &op->o_ndn ) ) {
185 p = (Entry *)&slap_entry_root;
187 /* check parent for "children" acl */
188 rc = access_allowed( be, conn, op, p,
189 children, NULL, ACL_WRITE, NULL );
193 switch( opinfo.boi_err ) {
194 case DB_LOCK_DEADLOCK:
195 case DB_LOCK_NOTGRANTED:
201 LDAP_LOG ( OPERATION, DETAIL1,
202 "<=- bdb_delete: no access to parent\n", 0, 0, 0 );
204 Debug( LDAP_DEBUG_TRACE,
205 "<=- bdb_delete: no access "
206 "to parent\n", 0, 0, 0 );
208 rc = LDAP_INSUFFICIENT_ACCESS;
209 text = "no write access to parent";
215 LDAP_LOG ( OPERATION, DETAIL1,
216 "<=- bdb_delete: no parent and not root\n", 0, 0, 0 );
218 Debug( LDAP_DEBUG_TRACE,
219 "<=- bdb_delete: no parent "
220 "and not root\n", 0, 0, 0);
222 rc = LDAP_INSUFFICIENT_ACCESS;
232 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
233 DB_LOCK_WRITE, &lock);
238 /* get entry for read/modify/write */
239 rc = bdb_dn2entry_w( be, ltid, ndn, &e, &matched, DB_RMW, locker, &lock );
245 case DB_LOCK_DEADLOCK:
246 case DB_LOCK_NOTGRANTED:
249 text = "ldap server busy";
253 text = "internal error";
258 char *matched_dn = NULL;
262 LDAP_LOG ( OPERATION, ARGS,
263 "<=- bdb_delete: no such object %s\n", dn->bv_val, 0, 0);
265 Debug( LDAP_DEBUG_ARGS,
266 "<=- bdb_delete: no such object %s\n",
270 if ( matched != NULL ) {
271 matched_dn = ch_strdup( matched->e_dn );
272 refs = is_entry_referral( matched )
273 ? get_entry_referrals( be, conn, op, matched )
275 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, matched);
279 refs = referral_rewrite( default_referral,
280 NULL, dn, LDAP_SCOPE_DEFAULT );
283 send_ldap_result( conn, op, LDAP_REFERRAL,
284 matched_dn, NULL, refs, NULL );
286 ber_bvarray_free( refs );
293 rc = access_allowed( be, conn, op, e,
294 entry, NULL, ACL_WRITE, NULL );
296 switch( opinfo.boi_err ) {
297 case DB_LOCK_DEADLOCK:
298 case DB_LOCK_NOTGRANTED:
304 LDAP_LOG ( OPERATION, DETAIL1,
305 "<=- bdb_delete: no write access to entry\n", 0, 0, 0 );
307 Debug( LDAP_DEBUG_TRACE,
308 "<=- bdb_delete: no write access to entry\n",
311 rc = LDAP_INSUFFICIENT_ACCESS;
312 text = "no write access to entry";
316 if ( !manageDSAit && is_entry_referral( e ) ) {
317 /* entry is a referral, don't allow delete */
318 BerVarray refs = get_entry_referrals( be,
322 LDAP_LOG ( OPERATION, DETAIL1,
323 "<=- bdb_delete: entry is referral\n", 0, 0, 0 );
325 Debug( LDAP_DEBUG_TRACE,
326 "bdb_delete: entry is referral\n",
330 send_ldap_result( conn, op, LDAP_REFERRAL,
331 e->e_dn, NULL, refs, NULL );
333 ber_bvarray_free( refs );
339 rc = bdb_dn2id_children( be, ltid, &e->e_nname, 0 );
340 if( rc != DB_NOTFOUND ) {
342 case DB_LOCK_DEADLOCK:
343 case DB_LOCK_NOTGRANTED:
347 LDAP_LOG ( OPERATION, DETAIL1,
348 "<=- bdb_delete: non-leaf %s\n", dn->bv_val, 0, 0 );
350 Debug(LDAP_DEBUG_ARGS,
351 "<=- bdb_delete: non-leaf %s\n",
354 rc = LDAP_NOT_ALLOWED_ON_NONLEAF;
355 text = "subtree delete not supported";
359 LDAP_LOG ( OPERATION, ERR,
360 "<=- bdb_delete: has_children failed %s (%d)\n",
361 db_strerror(rc), rc, 0 );
363 Debug(LDAP_DEBUG_ARGS,
364 "<=- bdb_delete: has_children failed: %s (%d)\n",
365 db_strerror(rc), rc, 0 );
368 text = "internal error";
373 /* delete from dn2id */
374 rc = bdb_dn2id_delete( be, ltid, pdn.bv_val, e );
377 case DB_LOCK_DEADLOCK:
378 case DB_LOCK_NOTGRANTED:
384 LDAP_LOG ( OPERATION, ERR,
385 "<=- bdb_delete: dn2id failed %s (%d)\n", db_strerror(rc), rc, 0 );
387 Debug(LDAP_DEBUG_ARGS,
388 "<=- bdb_delete: dn2id failed: %s (%d)\n",
389 db_strerror(rc), rc, 0 );
391 text = "DN index delete failed";
395 /* delete from id2entry */
396 rc = bdb_id2entry_delete( be, ltid, e );
399 case DB_LOCK_DEADLOCK:
400 case DB_LOCK_NOTGRANTED:
406 LDAP_LOG ( OPERATION, ERR,
407 "<=- bdb_delete: id2entry failed: %s (%d)\n",
408 db_strerror(rc), rc, 0 );
410 Debug(LDAP_DEBUG_ARGS,
411 "<=- bdb_delete: id2entry failed: %s (%d)\n",
412 db_strerror(rc), rc, 0 );
414 text = "entry delete failed";
418 /* delete indices for old attributes */
419 rc = bdb_index_entry_del( be, ltid, e, e->e_attrs );
420 if ( rc != LDAP_SUCCESS ) {
422 case DB_LOCK_DEADLOCK:
423 case DB_LOCK_NOTGRANTED:
429 LDAP_LOG ( OPERATION, ERR,
430 "<=- bdb_delete: entry index delete failed!\n", 0, 0, 0 );
432 Debug( LDAP_DEBUG_ANY, "entry index delete failed!\n",
435 text = "entry index delete failed";
439 #if 0 /* Do we want to reclaim deleted IDs? */
440 ldap_pvt_thread_mutex_lock( &bdb->bi_lastid_mutex );
441 if ( e->e_id == bdb->bi_lastid ) {
442 bdb_last_id( be, ltid );
444 ldap_pvt_thread_mutex_unlock( &bdb->bi_lastid_mutex );
448 if ( ( rc = TXN_ABORT( ltid ) ) != 0 ) {
449 text = "txn_abort (no-op) failed";
455 rc = TXN_COMMIT( ltid, 0 );
458 op->o_private = NULL;
462 LDAP_LOG ( OPERATION, ERR,
463 "bdb_delete: txn_%s failed: %s (%d)\n",
464 op->o_noop ? "abort (no-op)" : "commit", db_strerror(rc), rc );
466 Debug( LDAP_DEBUG_TRACE,
467 "bdb_delete: txn_%s failed: %s (%d)\n",
468 op->o_noop ? "abort (no-op)" : "commit",
469 db_strerror(rc), rc );
472 text = "commit failed";
476 LDAP_LOG ( OPERATION, RESULTS,
477 "bdb_delete: deleted%s id=%08lx db=\"%s\"\n",
478 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
480 Debug( LDAP_DEBUG_TRACE,
481 "bdb_delete: deleted%s id=%08lx dn=\"%s\"\n",
482 op->o_noop ? " (no-op)" : "",
490 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
492 if(rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
493 ldap_pvt_thread_yield();
494 TXN_CHECKPOINT( bdb->bi_dbenv,
495 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
501 bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
506 op->o_private = NULL;
509 return ( ( rc == LDAP_SUCCESS ) ? noop : rc );