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};
31 int manageDSAit = get_manageDSAit( op );
32 AttributeDescription *children = slap_schema.si_ad_children;
34 struct bdb_op_info opinfo;
40 Debug( LDAP_DEBUG_ARGS, "==> bdb_delete: %s\n",
44 retry: /* transaction retry */
46 bdb_cache_return_entry_w(&bdb->bi_cache, e);
48 Debug( LDAP_DEBUG_TRACE, "==> bdb_delete: retrying...\n",
50 rc = txn_abort( ltid );
55 text = "internal error";
58 ldap_pvt_thread_yield();
62 /* begin transaction */
63 rc = txn_begin( bdb->bi_dbenv, NULL, <id,
67 Debug( LDAP_DEBUG_TRACE,
68 "bdb_delete: txn_begin failed: %s (%d)\n",
69 db_strerror(rc), rc, 0 );
71 text = "internal error";
75 lockid = TXN_ID( ltid );
80 opinfo.boi_txn = ltid;
82 op->o_private = &opinfo;
84 if ( !be_issuffix( be, ndn ) ) {
85 rc = dnParent( ndn, &pdn );
86 if ( rc != LDAP_SUCCESS ) {
87 text = "internal error";
92 if( pdn.bv_len != 0 ) {
96 obj.data = pdn.bv_val-1;
97 obj.size = pdn.bv_len+1;
98 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
99 DB_LOCK_WRITE, &lock);
103 rc = bdb_dn2entry_r( be, ltid, &pdn, &p, NULL, 0 );
109 case DB_LOCK_DEADLOCK:
110 case DB_LOCK_NOTGRANTED:
114 text = "internal error";
119 Debug( LDAP_DEBUG_TRACE,
120 "<=- bdb_delete: parent does not exist\n",
123 text = "could not locate parent of entry";
127 /* check parent for "children" acl */
128 rc = access_allowed( be, conn, op, p,
129 children, NULL, ACL_WRITE );
131 bdb_cache_return_entry_r(&bdb->bi_cache, p);
134 switch( opinfo.boi_err ) {
135 case DB_LOCK_DEADLOCK:
136 case DB_LOCK_NOTGRANTED:
141 Debug( LDAP_DEBUG_TRACE,
142 "<=- bdb_delete: no access to parent\n",
144 rc = LDAP_INSUFFICIENT_ACCESS;
149 /* no parent, must be root to delete */
150 if( ! be_isroot( be, &op->o_ndn ) ) {
151 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
152 || be_isupdate( be, &op->o_ndn ) ) {
153 p = (Entry *)&slap_entry_root;
155 /* check parent for "children" acl */
156 rc = access_allowed( be, conn, op, p,
157 children, NULL, ACL_WRITE );
160 switch( opinfo.boi_err ) {
161 case DB_LOCK_DEADLOCK:
162 case DB_LOCK_NOTGRANTED:
167 Debug( LDAP_DEBUG_TRACE,
168 "<=- bdb_delete: no access "
169 "to parent\n", 0, 0, 0 );
170 rc = LDAP_INSUFFICIENT_ACCESS;
175 Debug( LDAP_DEBUG_TRACE,
176 "<=- bdb_delete: no parent "
177 "and not root\n", 0, 0, 0);
178 rc = LDAP_INSUFFICIENT_ACCESS;
188 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
189 DB_LOCK_WRITE, &lock);
194 /* get entry for read/modify/write */
195 rc = bdb_dn2entry_w( be, ltid, ndn, &e, &matched, DB_RMW );
201 case DB_LOCK_DEADLOCK:
202 case DB_LOCK_NOTGRANTED:
206 text = "internal error";
211 char *matched_dn = NULL;
214 Debug( LDAP_DEBUG_ARGS,
215 "<=- bdb_delete: no such object %s\n",
218 if ( matched != NULL ) {
219 matched_dn = ch_strdup( matched->e_dn );
220 refs = is_entry_referral( matched )
221 ? get_entry_referrals( be, conn, op, matched )
223 bdb_cache_return_entry_r(&bdb->bi_cache, matched );
227 refs = referral_rewrite( default_referral,
228 NULL, dn, LDAP_SCOPE_DEFAULT );
231 send_ldap_result( conn, op, LDAP_REFERRAL,
232 matched_dn, NULL, refs, NULL );
234 ber_bvarray_free( refs );
241 if ( !manageDSAit && is_entry_referral( e ) ) {
242 /* parent is a referral, don't allow add */
243 /* parent is an alias, don't allow add */
244 BerVarray refs = get_entry_referrals( be,
247 Debug( LDAP_DEBUG_TRACE,
248 "bdb_delete: entry is referral\n",
251 send_ldap_result( conn, op, LDAP_REFERRAL,
252 e->e_dn, NULL, refs, NULL );
254 ber_bvarray_free( refs );
260 rc = bdb_dn2id_children( be, ltid, &e->e_nname );
261 if( rc != DB_NOTFOUND ) {
263 case DB_LOCK_DEADLOCK:
264 case DB_LOCK_NOTGRANTED:
267 Debug(LDAP_DEBUG_ARGS,
268 "<=- bdb_delete: non-leaf %s\n",
270 rc = LDAP_NOT_ALLOWED_ON_NONLEAF;
271 text = "subtree delete not supported";
274 Debug(LDAP_DEBUG_ARGS,
275 "<=- bdb_delete: has_children failed: %s (%d)\n",
276 db_strerror(rc), rc, 0 );
278 text = "internal error";
283 /* delete from dn2id */
284 rc = bdb_dn2id_delete( be, ltid, pdn.bv_val, e );
287 case DB_LOCK_DEADLOCK:
288 case DB_LOCK_NOTGRANTED:
293 Debug(LDAP_DEBUG_ARGS,
294 "<=- bdb_delete: dn2id failed: %s (%d)\n",
295 db_strerror(rc), rc, 0 );
296 text = "DN index delete failed";
300 /* delete from id2entry */
301 rc = bdb_id2entry_delete( be, ltid, e );
304 case DB_LOCK_DEADLOCK:
305 case DB_LOCK_NOTGRANTED:
310 Debug(LDAP_DEBUG_ARGS,
311 "<=- bdb_delete: id2entry failed: %s (%d)\n",
312 db_strerror(rc), rc, 0 );
313 text = "entry delete failed";
317 /* delete indices for old attributes */
318 rc = bdb_index_entry_del( be, ltid, e, e->e_attrs );
319 if ( rc != LDAP_SUCCESS ) {
321 case DB_LOCK_DEADLOCK:
322 case DB_LOCK_NOTGRANTED:
327 Debug( LDAP_DEBUG_ANY, "entry index delete failed!\n",
329 text = "entry index delete failed";
333 #if 0 /* Do we want to reclaim deleted IDs? */
334 ldap_pvt_thread_mutex_lock( &bdb->bi_lastid_mutex );
335 if ( e->e_id == bdb->bi_lastid ) {
336 bdb_last_id( be, ltid );
338 ldap_pvt_thread_mutex_unlock( &bdb->bi_lastid_mutex );
342 rc = txn_commit( ltid, 0 );
345 op->o_private = NULL;
348 Debug( LDAP_DEBUG_TRACE,
349 "bdb_delete: txn_commit failed: %s (%d)\n",
350 db_strerror(rc), rc, 0 );
352 text = "commit failed";
355 Debug( LDAP_DEBUG_TRACE,
356 "bdb_delete: deleted id=%08lx dn=\"%s\"\n",
357 e->e_id, e->e_dn, 0 );
363 send_ldap_result( conn, op, LDAP_SUCCESS,
364 NULL, text, NULL, NULL );
366 if(rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
367 ldap_pvt_thread_yield();
368 TXN_CHECKPOINT( bdb->bi_dbenv,
369 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
375 bdb_cache_return_entry_w(&bdb->bi_cache, e);
380 op->o_private = NULL;