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;
45 LDAP_LOG (( "delete", LDAP_LEVEL_ARGS, "==> bdb_delete: %s\n",
48 Debug( LDAP_DEBUG_ARGS, "==> bdb_delete: %s\n",
53 retry: /* transaction retry */
55 bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
58 LDAP_LOG (( "delete", LDAP_LEVEL_DETAIL1,
59 "==> bdb_delete: retrying...\n" ));
61 Debug( LDAP_DEBUG_TRACE, "==> bdb_delete: retrying...\n",
64 rc = TXN_ABORT( ltid );
69 text = "internal error";
72 ldap_pvt_thread_yield();
75 /* begin transaction */
76 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
81 LDAP_LOG (( "delete", LDAP_LEVEL_ERR,
82 "==> bdb_delete: txn_begin failed: %s (%d)\n",
83 db_strerror(rc), rc ));
85 Debug( LDAP_DEBUG_TRACE,
86 "bdb_delete: txn_begin failed: %s (%d)\n",
87 db_strerror(rc), rc, 0 );
90 text = "internal error";
94 locker = TXN_ID ( ltid );
96 lockid = TXN_ID( ltid );
100 opinfo.boi_txn = ltid;
102 op->o_private = &opinfo;
104 if ( !be_issuffix( be, ndn ) ) {
105 dnParent( ndn, &pdn );
108 if( pdn.bv_len != 0 ) {
112 obj.data = pdn.bv_val-1;
113 obj.size = pdn.bv_len+1;
114 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
115 DB_LOCK_WRITE, &lock);
119 rc = bdb_dn2entry_r( be, ltid, &pdn, &p, NULL, 0, locker, &lock );
125 case DB_LOCK_DEADLOCK:
126 case DB_LOCK_NOTGRANTED:
129 text = "ldap server busy";
133 text = "internal error";
139 LDAP_LOG (( "delete", LDAP_LEVEL_DETAIL1,
140 "<=- bdb_delete: parent does not exist\n" ));
142 Debug( LDAP_DEBUG_TRACE,
143 "<=- bdb_delete: parent does not exist\n",
147 text = "could not locate parent of entry";
151 /* check parent for "children" acl */
152 rc = access_allowed( be, conn, op, p,
153 children, NULL, ACL_WRITE, NULL );
155 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p);
158 switch( opinfo.boi_err ) {
159 case DB_LOCK_DEADLOCK:
160 case DB_LOCK_NOTGRANTED:
166 LDAP_LOG (( "delete", LDAP_LEVEL_DETAIL1,
167 "<=- bdb_delete: no access to parent\n" ));
169 Debug( LDAP_DEBUG_TRACE,
170 "<=- bdb_delete: no access to parent\n",
173 rc = LDAP_INSUFFICIENT_ACCESS;
178 /* no parent, must be root to delete */
179 if( ! be_isroot( be, &op->o_ndn ) ) {
180 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
181 || be_isupdate( be, &op->o_ndn ) ) {
182 p = (Entry *)&slap_entry_root;
184 /* check parent for "children" acl */
185 rc = access_allowed( be, conn, op, p,
186 children, NULL, ACL_WRITE, NULL );
189 switch( opinfo.boi_err ) {
190 case DB_LOCK_DEADLOCK:
191 case DB_LOCK_NOTGRANTED:
197 LDAP_LOG (( "delete", LDAP_LEVEL_DETAIL1,
198 "<=- bdb_delete: no access to parent\n" ));
200 Debug( LDAP_DEBUG_TRACE,
201 "<=- bdb_delete: no access "
202 "to parent\n", 0, 0, 0 );
204 rc = LDAP_INSUFFICIENT_ACCESS;
210 LDAP_LOG (( "delete", LDAP_LEVEL_DETAIL1,
211 "<=- bdb_delete: no parent and not root\n" ));
213 Debug( LDAP_DEBUG_TRACE,
214 "<=- bdb_delete: no parent "
215 "and not root\n", 0, 0, 0);
217 rc = LDAP_INSUFFICIENT_ACCESS;
227 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
228 DB_LOCK_WRITE, &lock);
233 /* get entry for read/modify/write */
234 rc = bdb_dn2entry_w( be, ltid, ndn, &e, &matched, DB_RMW, locker, &lock );
240 case DB_LOCK_DEADLOCK:
241 case DB_LOCK_NOTGRANTED:
244 text = "ldap server busy";
248 text = "internal error";
253 char *matched_dn = NULL;
257 LDAP_LOG (( "delete", LDAP_LEVEL_ARGS,
258 "<=- bdb_delete: no such object %s\n",
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 (( "delete", LDAP_LEVEL_DETAIL1,
296 "<=- bdb_delete: entry is referral\n" ));
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 );
313 if( rc != DB_NOTFOUND ) {
315 case DB_LOCK_DEADLOCK:
316 case DB_LOCK_NOTGRANTED:
320 LDAP_LOG (( "delete", LDAP_LEVEL_DETAIL1,
321 "<=- bdb_delete: non-leaf %s\n",
324 Debug(LDAP_DEBUG_ARGS,
325 "<=- bdb_delete: non-leaf %s\n",
328 rc = LDAP_NOT_ALLOWED_ON_NONLEAF;
329 text = "subtree delete not supported";
333 LDAP_LOG (( "delete", LDAP_LEVEL_ERR,
334 "<=- bdb_delete: has_children failed %s (%d)\n",
335 db_strerror(rc), rc ));
337 Debug(LDAP_DEBUG_ARGS,
338 "<=- bdb_delete: has_children failed: %s (%d)\n",
339 db_strerror(rc), rc, 0 );
342 text = "internal error";
347 /* delete from dn2id */
348 rc = bdb_dn2id_delete( be, ltid, pdn.bv_val, e );
351 case DB_LOCK_DEADLOCK:
352 case DB_LOCK_NOTGRANTED:
358 LDAP_LOG (( "delete", LDAP_LEVEL_ERR,
359 "<=- bdb_delete: dn2id failed %s (%d)\n",
360 db_strerror(rc), rc ));
362 Debug(LDAP_DEBUG_ARGS,
363 "<=- bdb_delete: dn2id failed: %s (%d)\n",
364 db_strerror(rc), rc, 0 );
366 text = "DN index delete failed";
370 /* delete from id2entry */
371 rc = bdb_id2entry_delete( be, ltid, e );
374 case DB_LOCK_DEADLOCK:
375 case DB_LOCK_NOTGRANTED:
381 LDAP_LOG (( "delete", LDAP_LEVEL_ERR,
382 "<=- bdb_delete: id2entry failed: %s (%d)\n",
383 db_strerror(rc), rc ));
385 Debug(LDAP_DEBUG_ARGS,
386 "<=- bdb_delete: id2entry failed: %s (%d)\n",
387 db_strerror(rc), rc, 0 );
389 text = "entry delete failed";
393 /* delete indices for old attributes */
394 rc = bdb_index_entry_del( be, ltid, e, e->e_attrs );
395 if ( rc != LDAP_SUCCESS ) {
397 case DB_LOCK_DEADLOCK:
398 case DB_LOCK_NOTGRANTED:
404 LDAP_LOG (( "delete", LDAP_LEVEL_ERR,
405 "<=- bdb_delete: entry index delete failed!\n" ));
407 Debug( LDAP_DEBUG_ANY, "entry index delete failed!\n",
410 text = "entry index delete failed";
414 #if 0 /* Do we want to reclaim deleted IDs? */
415 ldap_pvt_thread_mutex_lock( &bdb->bi_lastid_mutex );
416 if ( e->e_id == bdb->bi_lastid ) {
417 bdb_last_id( be, ltid );
419 ldap_pvt_thread_mutex_unlock( &bdb->bi_lastid_mutex );
423 rc = TXN_ABORT( ltid );
425 rc = TXN_COMMIT( ltid, 0 );
428 op->o_private = NULL;
432 LDAP_LOG (( "delete", LDAP_LEVEL_ERR,
433 "bdb_delete: txn_%s failed: %s (%d)\n",
434 op->o_noop ? "abort (no-op)" : "commit",
435 db_strerror(rc), rc ));
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(rc), rc );
443 text = "commit failed";
447 LDAP_LOG (( "delete", LDAP_LEVEL_RESULTS,
448 "bdb_delete: deleted%s id=%08lx db=\"%s\"\n",
449 op->o_noop ? " (no-op)" : "",
452 Debug( LDAP_DEBUG_TRACE,
453 "bdb_delete: deleted%s id=%08lx dn=\"%s\"\n",
454 op->o_noop ? " (no-op)" : "",
462 send_ldap_result( conn, op, rc, NULL, text, NULL, NULL );
464 if(rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
465 ldap_pvt_thread_yield();
466 TXN_CHECKPOINT( bdb->bi_dbenv,
467 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
473 bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
478 op->o_private = NULL;