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 */
45 Debug( LDAP_DEBUG_TRACE, "==> bdb_delete: retrying...\n",
47 rc = txn_abort( ltid );
52 text = "internal error";
55 ldap_pvt_thread_yield();
59 /* begin transaction */
60 rc = txn_begin( bdb->bi_dbenv, NULL, <id,
64 Debug( LDAP_DEBUG_TRACE,
65 "bdb_delete: txn_begin failed: %s (%d)\n",
66 db_strerror(rc), rc, 0 );
68 text = "internal error";
72 lockid = TXN_ID( ltid );
77 opinfo.boi_txn = ltid;
79 op->o_private = &opinfo;
81 if ( !be_issuffix( be, ndn->bv_val ) ) {
82 rc = dnParent( ndn->bv_val, &pdn.bv_val );
83 if ( rc != LDAP_SUCCESS ) {
84 text = "internal error";
87 if (pdn.bv_val && pdn.bv_val[0]) {
88 pdn.bv_len = ndn->bv_len - (pdn.bv_val - ndn->bv_val);
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( 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_entry_return( be, p );
135 Debug( LDAP_DEBUG_TRACE,
136 "<=- bdb_delete: no access to parent\n",
138 rc = LDAP_INSUFFICIENT_ACCESS;
143 /* no parent, must be root to delete */
144 if( ! be_isroot( be, &op->o_ndn ) ) {
145 if ( be_issuffix( be, "" ) || be_isupdate( be, &op->o_ndn ) ) {
146 p = (Entry *)&slap_entry_root;
148 /* check parent for "children" acl */
149 rc = access_allowed( be, conn, op, p,
150 children, NULL, ACL_WRITE );
154 Debug( LDAP_DEBUG_TRACE,
155 "<=- bdb_delete: no access "
156 "to parent\n", 0, 0, 0 );
157 rc = LDAP_INSUFFICIENT_ACCESS;
162 Debug( LDAP_DEBUG_TRACE,
163 "<=- bdb_delete: no parent "
164 "and not root\n", 0, 0, 0);
165 rc = LDAP_INSUFFICIENT_ACCESS;
174 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
175 DB_LOCK_WRITE, &lock);
180 /* get entry for read/modify/write */
181 rc = bdb_dn2entry( be, ltid, ndn, &e, &matched, DB_RMW );
187 case DB_LOCK_DEADLOCK:
188 case DB_LOCK_NOTGRANTED:
192 text = "internal error";
197 char *matched_dn = NULL;
200 Debug( LDAP_DEBUG_ARGS,
201 "<=- bdb_delete: no such object %s\n",
204 if ( matched != NULL ) {
205 matched_dn = ch_strdup( matched->e_dn );
206 refs = is_entry_referral( matched )
207 ? get_entry_referrals( be, conn, op, matched )
209 bdb_entry_return( be, matched );
213 refs = referral_rewrite( default_referral,
214 NULL, dn, LDAP_SCOPE_DEFAULT );
217 send_ldap_result( conn, op, LDAP_REFERRAL,
218 matched_dn, NULL, refs, NULL );
220 ber_bvarray_free( refs );
227 if ( !manageDSAit && is_entry_referral( e ) ) {
228 /* parent is a referral, don't allow add */
229 /* parent is an alias, don't allow add */
230 BerVarray refs = get_entry_referrals( be,
233 Debug( LDAP_DEBUG_TRACE,
234 "bdb_delete: entry is referral\n",
237 send_ldap_result( conn, op, LDAP_REFERRAL,
238 e->e_dn, NULL, refs, NULL );
240 ber_bvarray_free( refs );
246 rc = bdb_dn2id_children( be, ltid, &e->e_nname );
247 if( rc != DB_NOTFOUND ) {
249 case DB_LOCK_DEADLOCK:
250 case DB_LOCK_NOTGRANTED:
253 Debug(LDAP_DEBUG_ARGS,
254 "<=- bdb_delete: non-leaf %s\n",
256 rc = LDAP_NOT_ALLOWED_ON_NONLEAF;
257 text = "subtree delete not supported";
260 Debug(LDAP_DEBUG_ARGS,
261 "<=- bdb_delete: has_children failed: %s (%d)\n",
262 db_strerror(rc), rc, 0 );
264 text = "internal error";
269 /* delete from dn2id */
270 rc = bdb_dn2id_delete( be, ltid, pdn.bv_val, e );
273 case DB_LOCK_DEADLOCK:
274 case DB_LOCK_NOTGRANTED:
279 Debug(LDAP_DEBUG_ARGS,
280 "<=- bdb_delete: dn2id failed: %s (%d)\n",
281 db_strerror(rc), rc, 0 );
282 text = "DN index delete failed";
286 /* delete from id2entry */
287 rc = bdb_id2entry_delete( be, ltid, e->e_id );
290 case DB_LOCK_DEADLOCK:
291 case DB_LOCK_NOTGRANTED:
296 Debug(LDAP_DEBUG_ARGS,
297 "<=- bdb_delete: id2entry failed: %s (%d)\n",
298 db_strerror(rc), rc, 0 );
299 text = "entry delete failed";
303 /* delete indices for old attributes */
304 rc = bdb_index_entry_del( be, ltid, e, e->e_attrs );
305 if ( rc != LDAP_SUCCESS ) {
307 case DB_LOCK_DEADLOCK:
308 case DB_LOCK_NOTGRANTED:
313 Debug( LDAP_DEBUG_ANY, "entry index delete failed!\n",
315 text = "entry index delete failed";
319 #if 0 /* Do we want to reclaim deleted IDs? */
320 ldap_pvt_thread_mutex_lock( &bdb->bi_lastid_mutex );
321 if ( e->e_id == bdb->bi_lastid ) {
322 bdb_last_id( be, ltid );
324 ldap_pvt_thread_mutex_unlock( &bdb->bi_lastid_mutex );
328 rc = txn_commit( ltid, 0 );
331 op->o_private = NULL;
334 Debug( LDAP_DEBUG_TRACE,
335 "bdb_delete: txn_commit failed: %s (%d)\n",
336 db_strerror(rc), rc, 0 );
338 text = "commit failed";
341 Debug( LDAP_DEBUG_TRACE,
342 "bdb_delete: deleted id=%08lx dn=\"%s\"\n",
343 e->e_id, e->e_dn, 0 );
349 send_ldap_result( conn, op, LDAP_SUCCESS,
350 NULL, text, NULL, NULL );
352 if(rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
353 ldap_pvt_thread_yield();
354 TXN_CHECKPOINT( bdb->bi_dbenv,
355 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
361 bdb_entry_return( be, e );
366 op->o_private = NULL;