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->bv_val ) ) {
85 rc = dnParent( ndn->bv_val, &pdn.bv_val );
86 if ( rc != LDAP_SUCCESS ) {
87 text = "internal error";
90 if (pdn.bv_val && pdn.bv_val[0]) {
91 pdn.bv_len = ndn->bv_len - (pdn.bv_val - ndn->bv_val);
95 if( pdn.bv_len != 0 ) {
99 obj.data = pdn.bv_val-1;
100 obj.size = pdn.bv_len+1;
101 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
102 DB_LOCK_WRITE, &lock);
106 rc = bdb_dn2entry_r( be, ltid, &pdn, &p, NULL, 0 );
112 case DB_LOCK_DEADLOCK:
113 case DB_LOCK_NOTGRANTED:
117 text = "internal error";
122 Debug( LDAP_DEBUG_TRACE,
123 "<=- bdb_delete: parent does not exist\n",
126 text = "could not locate parent of entry";
130 /* check parent for "children" acl */
131 rc = access_allowed( be, conn, op, p,
132 children, NULL, ACL_WRITE );
134 bdb_cache_return_entry_r(&bdb->bi_cache, p);
137 switch( opinfo.boi_err ) {
138 case DB_LOCK_DEADLOCK:
139 case DB_LOCK_NOTGRANTED:
144 Debug( LDAP_DEBUG_TRACE,
145 "<=- bdb_delete: no access to parent\n",
147 rc = LDAP_INSUFFICIENT_ACCESS;
152 /* no parent, must be root to delete */
153 if( ! be_isroot( be, &op->o_ndn ) ) {
154 if ( be_issuffix( be, "" ) || be_isupdate( be, &op->o_ndn ) ) {
155 p = (Entry *)&slap_entry_root;
157 /* check parent for "children" acl */
158 rc = access_allowed( be, conn, op, p,
159 children, NULL, ACL_WRITE );
162 switch( opinfo.boi_err ) {
163 case DB_LOCK_DEADLOCK:
164 case DB_LOCK_NOTGRANTED:
169 Debug( LDAP_DEBUG_TRACE,
170 "<=- bdb_delete: no access "
171 "to parent\n", 0, 0, 0 );
172 rc = LDAP_INSUFFICIENT_ACCESS;
177 Debug( LDAP_DEBUG_TRACE,
178 "<=- bdb_delete: no parent "
179 "and not root\n", 0, 0, 0);
180 rc = LDAP_INSUFFICIENT_ACCESS;
190 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
191 DB_LOCK_WRITE, &lock);
196 /* get entry for read/modify/write */
197 rc = bdb_dn2entry_w( be, ltid, ndn, &e, &matched, DB_RMW );
203 case DB_LOCK_DEADLOCK:
204 case DB_LOCK_NOTGRANTED:
208 text = "internal error";
213 char *matched_dn = NULL;
216 Debug( LDAP_DEBUG_ARGS,
217 "<=- bdb_delete: no such object %s\n",
220 if ( matched != NULL ) {
221 matched_dn = ch_strdup( matched->e_dn );
222 refs = is_entry_referral( matched )
223 ? get_entry_referrals( be, conn, op, matched )
225 bdb_cache_return_entry_r(&bdb->bi_cache, matched );
229 refs = referral_rewrite( default_referral,
230 NULL, dn, LDAP_SCOPE_DEFAULT );
233 send_ldap_result( conn, op, LDAP_REFERRAL,
234 matched_dn, NULL, refs, NULL );
236 ber_bvarray_free( refs );
243 if ( !manageDSAit && is_entry_referral( e ) ) {
244 /* parent is a referral, don't allow add */
245 /* parent is an alias, don't allow add */
246 BerVarray refs = get_entry_referrals( be,
249 Debug( LDAP_DEBUG_TRACE,
250 "bdb_delete: entry is referral\n",
253 send_ldap_result( conn, op, LDAP_REFERRAL,
254 e->e_dn, NULL, refs, NULL );
256 ber_bvarray_free( refs );
262 rc = bdb_dn2id_children( be, ltid, &e->e_nname );
263 if( rc != DB_NOTFOUND ) {
265 case DB_LOCK_DEADLOCK:
266 case DB_LOCK_NOTGRANTED:
269 Debug(LDAP_DEBUG_ARGS,
270 "<=- bdb_delete: non-leaf %s\n",
272 rc = LDAP_NOT_ALLOWED_ON_NONLEAF;
273 text = "subtree delete not supported";
276 Debug(LDAP_DEBUG_ARGS,
277 "<=- bdb_delete: has_children failed: %s (%d)\n",
278 db_strerror(rc), rc, 0 );
280 text = "internal error";
285 /* delete from dn2id */
286 rc = bdb_dn2id_delete( be, ltid, pdn.bv_val, e );
289 case DB_LOCK_DEADLOCK:
290 case DB_LOCK_NOTGRANTED:
295 Debug(LDAP_DEBUG_ARGS,
296 "<=- bdb_delete: dn2id failed: %s (%d)\n",
297 db_strerror(rc), rc, 0 );
298 text = "DN index delete failed";
302 /* delete from id2entry */
303 rc = bdb_id2entry_delete( be, ltid, e );
306 case DB_LOCK_DEADLOCK:
307 case DB_LOCK_NOTGRANTED:
312 Debug(LDAP_DEBUG_ARGS,
313 "<=- bdb_delete: id2entry failed: %s (%d)\n",
314 db_strerror(rc), rc, 0 );
315 text = "entry delete failed";
319 /* delete indices for old attributes */
320 rc = bdb_index_entry_del( be, ltid, e, e->e_attrs );
321 if ( rc != LDAP_SUCCESS ) {
323 case DB_LOCK_DEADLOCK:
324 case DB_LOCK_NOTGRANTED:
329 Debug( LDAP_DEBUG_ANY, "entry index delete failed!\n",
331 text = "entry index delete failed";
335 #if 0 /* Do we want to reclaim deleted IDs? */
336 ldap_pvt_thread_mutex_lock( &bdb->bi_lastid_mutex );
337 if ( e->e_id == bdb->bi_lastid ) {
338 bdb_last_id( be, ltid );
340 ldap_pvt_thread_mutex_unlock( &bdb->bi_lastid_mutex );
344 rc = txn_commit( ltid, 0 );
347 op->o_private = NULL;
350 Debug( LDAP_DEBUG_TRACE,
351 "bdb_delete: txn_commit failed: %s (%d)\n",
352 db_strerror(rc), rc, 0 );
354 text = "commit failed";
357 Debug( LDAP_DEBUG_TRACE,
358 "bdb_delete: deleted id=%08lx dn=\"%s\"\n",
359 e->e_id, e->e_dn, 0 );
365 send_ldap_result( conn, op, LDAP_SUCCESS,
366 NULL, text, NULL, NULL );
368 if(rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
369 ldap_pvt_thread_yield();
370 TXN_CHECKPOINT( bdb->bi_dbenv,
371 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
377 bdb_cache_return_entry_w(&bdb->bi_cache, e);
382 op->o_private = NULL;