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 dnParent( ndn, &pdn );
88 if( pdn.bv_len != 0 ) {
92 obj.data = pdn.bv_val-1;
93 obj.size = pdn.bv_len+1;
94 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
95 DB_LOCK_WRITE, &lock);
99 rc = bdb_dn2entry_r( be, ltid, &pdn, &p, NULL, 0 );
105 case DB_LOCK_DEADLOCK:
106 case DB_LOCK_NOTGRANTED:
110 text = "internal error";
115 Debug( LDAP_DEBUG_TRACE,
116 "<=- bdb_delete: parent does not exist\n",
119 text = "could not locate parent of entry";
123 /* check parent for "children" acl */
124 rc = access_allowed( be, conn, op, p,
125 children, NULL, ACL_WRITE );
127 bdb_cache_return_entry_r(&bdb->bi_cache, p);
130 switch( opinfo.boi_err ) {
131 case DB_LOCK_DEADLOCK:
132 case DB_LOCK_NOTGRANTED:
137 Debug( LDAP_DEBUG_TRACE,
138 "<=- bdb_delete: no access to parent\n",
140 rc = LDAP_INSUFFICIENT_ACCESS;
145 /* no parent, must be root to delete */
146 if( ! be_isroot( be, &op->o_ndn ) ) {
147 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
148 || be_isupdate( be, &op->o_ndn ) ) {
149 p = (Entry *)&slap_entry_root;
151 /* check parent for "children" acl */
152 rc = access_allowed( be, conn, op, p,
153 children, NULL, ACL_WRITE );
156 switch( opinfo.boi_err ) {
157 case DB_LOCK_DEADLOCK:
158 case DB_LOCK_NOTGRANTED:
163 Debug( LDAP_DEBUG_TRACE,
164 "<=- bdb_delete: no access "
165 "to parent\n", 0, 0, 0 );
166 rc = LDAP_INSUFFICIENT_ACCESS;
171 Debug( LDAP_DEBUG_TRACE,
172 "<=- bdb_delete: no parent "
173 "and not root\n", 0, 0, 0);
174 rc = LDAP_INSUFFICIENT_ACCESS;
184 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
185 DB_LOCK_WRITE, &lock);
190 /* get entry for read/modify/write */
191 rc = bdb_dn2entry_w( be, ltid, ndn, &e, &matched, DB_RMW );
197 case DB_LOCK_DEADLOCK:
198 case DB_LOCK_NOTGRANTED:
202 text = "internal error";
207 char *matched_dn = NULL;
210 Debug( LDAP_DEBUG_ARGS,
211 "<=- bdb_delete: no such object %s\n",
214 if ( matched != NULL ) {
215 matched_dn = ch_strdup( matched->e_dn );
216 refs = is_entry_referral( matched )
217 ? get_entry_referrals( be, conn, op, matched )
219 bdb_cache_return_entry_r(&bdb->bi_cache, matched );
223 refs = referral_rewrite( default_referral,
224 NULL, dn, LDAP_SCOPE_DEFAULT );
227 send_ldap_result( conn, op, LDAP_REFERRAL,
228 matched_dn, NULL, refs, NULL );
230 ber_bvarray_free( refs );
237 if ( !manageDSAit && is_entry_referral( e ) ) {
238 /* parent is a referral, don't allow add */
239 /* parent is an alias, don't allow add */
240 BerVarray refs = get_entry_referrals( be,
243 Debug( LDAP_DEBUG_TRACE,
244 "bdb_delete: entry is referral\n",
247 send_ldap_result( conn, op, LDAP_REFERRAL,
248 e->e_dn, NULL, refs, NULL );
250 ber_bvarray_free( refs );
256 rc = bdb_dn2id_children( be, ltid, &e->e_nname );
257 if( rc != DB_NOTFOUND ) {
259 case DB_LOCK_DEADLOCK:
260 case DB_LOCK_NOTGRANTED:
263 Debug(LDAP_DEBUG_ARGS,
264 "<=- bdb_delete: non-leaf %s\n",
266 rc = LDAP_NOT_ALLOWED_ON_NONLEAF;
267 text = "subtree delete not supported";
270 Debug(LDAP_DEBUG_ARGS,
271 "<=- bdb_delete: has_children failed: %s (%d)\n",
272 db_strerror(rc), rc, 0 );
274 text = "internal error";
279 /* delete from dn2id */
280 rc = bdb_dn2id_delete( be, ltid, pdn.bv_val, e );
283 case DB_LOCK_DEADLOCK:
284 case DB_LOCK_NOTGRANTED:
289 Debug(LDAP_DEBUG_ARGS,
290 "<=- bdb_delete: dn2id failed: %s (%d)\n",
291 db_strerror(rc), rc, 0 );
292 text = "DN index delete failed";
296 /* delete from id2entry */
297 rc = bdb_id2entry_delete( be, ltid, e );
300 case DB_LOCK_DEADLOCK:
301 case DB_LOCK_NOTGRANTED:
306 Debug(LDAP_DEBUG_ARGS,
307 "<=- bdb_delete: id2entry failed: %s (%d)\n",
308 db_strerror(rc), rc, 0 );
309 text = "entry delete failed";
313 /* delete indices for old attributes */
314 rc = bdb_index_entry_del( be, ltid, e, e->e_attrs );
315 if ( rc != LDAP_SUCCESS ) {
317 case DB_LOCK_DEADLOCK:
318 case DB_LOCK_NOTGRANTED:
323 Debug( LDAP_DEBUG_ANY, "entry index delete failed!\n",
325 text = "entry index delete failed";
329 #if 0 /* Do we want to reclaim deleted IDs? */
330 ldap_pvt_thread_mutex_lock( &bdb->bi_lastid_mutex );
331 if ( e->e_id == bdb->bi_lastid ) {
332 bdb_last_id( be, ltid );
334 ldap_pvt_thread_mutex_unlock( &bdb->bi_lastid_mutex );
338 rc = txn_commit( ltid, 0 );
341 op->o_private = NULL;
344 Debug( LDAP_DEBUG_TRACE,
345 "bdb_delete: txn_commit failed: %s (%d)\n",
346 db_strerror(rc), rc, 0 );
348 text = "commit failed";
351 Debug( LDAP_DEBUG_TRACE,
352 "bdb_delete: deleted id=%08lx dn=\"%s\"\n",
353 e->e_id, e->e_dn, 0 );
359 send_ldap_result( conn, op, LDAP_SUCCESS,
360 NULL, text, NULL, NULL );
362 if(rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
363 ldap_pvt_thread_yield();
364 TXN_CHECKPOINT( bdb->bi_dbenv,
365 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
371 bdb_cache_return_entry_w(&bdb->bi_cache, e);
376 op->o_private = NULL;