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();
61 /* begin transaction */
62 rc = txn_begin( bdb->bi_dbenv, NULL, <id,
66 Debug( LDAP_DEBUG_TRACE,
67 "bdb_delete: txn_begin failed: %s (%d)\n",
68 db_strerror(rc), rc, 0 );
70 text = "internal error";
74 lockid = TXN_ID( ltid );
78 opinfo.boi_txn = ltid;
80 op->o_private = &opinfo;
82 if ( !be_issuffix( be, ndn ) ) {
83 dnParent( ndn, &pdn );
86 if( pdn.bv_len != 0 ) {
90 obj.data = pdn.bv_val-1;
91 obj.size = pdn.bv_len+1;
92 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
93 DB_LOCK_WRITE, &lock);
97 rc = bdb_dn2entry_r( be, ltid, &pdn, &p, NULL, 0 );
103 case DB_LOCK_DEADLOCK:
104 case DB_LOCK_NOTGRANTED:
108 text = "internal error";
113 Debug( LDAP_DEBUG_TRACE,
114 "<=- bdb_delete: parent does not exist\n",
117 text = "could not locate parent of entry";
121 /* check parent for "children" acl */
122 rc = access_allowed( be, conn, op, p,
123 children, NULL, ACL_WRITE );
125 bdb_cache_return_entry_r(&bdb->bi_cache, p);
128 switch( opinfo.boi_err ) {
129 case DB_LOCK_DEADLOCK:
130 case DB_LOCK_NOTGRANTED:
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, (struct berval *)&slap_empty_bv )
146 || be_isupdate( be, &op->o_ndn ) ) {
147 p = (Entry *)&slap_entry_root;
149 /* check parent for "children" acl */
150 rc = access_allowed( be, conn, op, p,
151 children, NULL, ACL_WRITE );
154 switch( opinfo.boi_err ) {
155 case DB_LOCK_DEADLOCK:
156 case DB_LOCK_NOTGRANTED:
161 Debug( LDAP_DEBUG_TRACE,
162 "<=- bdb_delete: no access "
163 "to parent\n", 0, 0, 0 );
164 rc = LDAP_INSUFFICIENT_ACCESS;
169 Debug( LDAP_DEBUG_TRACE,
170 "<=- bdb_delete: no parent "
171 "and not root\n", 0, 0, 0);
172 rc = LDAP_INSUFFICIENT_ACCESS;
182 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
183 DB_LOCK_WRITE, &lock);
188 /* get entry for read/modify/write */
189 rc = bdb_dn2entry_w( be, ltid, ndn, &e, &matched, DB_RMW );
195 case DB_LOCK_DEADLOCK:
196 case DB_LOCK_NOTGRANTED:
200 text = "internal error";
205 char *matched_dn = NULL;
208 Debug( LDAP_DEBUG_ARGS,
209 "<=- bdb_delete: no such object %s\n",
212 if ( matched != NULL ) {
213 matched_dn = ch_strdup( matched->e_dn );
214 refs = is_entry_referral( matched )
215 ? get_entry_referrals( be, conn, op, matched )
217 bdb_cache_return_entry_r(&bdb->bi_cache, matched );
221 refs = referral_rewrite( default_referral,
222 NULL, dn, LDAP_SCOPE_DEFAULT );
225 send_ldap_result( conn, op, LDAP_REFERRAL,
226 matched_dn, NULL, refs, NULL );
228 ber_bvarray_free( refs );
235 if ( !manageDSAit && is_entry_referral( e ) ) {
236 /* entry is a referral, don't allow delete */
237 BerVarray refs = get_entry_referrals( be,
240 Debug( LDAP_DEBUG_TRACE,
241 "bdb_delete: entry is referral\n",
244 send_ldap_result( conn, op, LDAP_REFERRAL,
245 e->e_dn, NULL, refs, NULL );
247 ber_bvarray_free( refs );
253 rc = bdb_dn2id_children( be, ltid, &e->e_nname );
254 if( rc != DB_NOTFOUND ) {
256 case DB_LOCK_DEADLOCK:
257 case DB_LOCK_NOTGRANTED:
260 Debug(LDAP_DEBUG_ARGS,
261 "<=- bdb_delete: non-leaf %s\n",
263 rc = LDAP_NOT_ALLOWED_ON_NONLEAF;
264 text = "subtree delete not supported";
267 Debug(LDAP_DEBUG_ARGS,
268 "<=- bdb_delete: has_children failed: %s (%d)\n",
269 db_strerror(rc), rc, 0 );
271 text = "internal error";
276 /* delete from dn2id */
277 rc = bdb_dn2id_delete( be, ltid, pdn.bv_val, e );
280 case DB_LOCK_DEADLOCK:
281 case DB_LOCK_NOTGRANTED:
286 Debug(LDAP_DEBUG_ARGS,
287 "<=- bdb_delete: dn2id failed: %s (%d)\n",
288 db_strerror(rc), rc, 0 );
289 text = "DN index delete failed";
293 /* delete from id2entry */
294 rc = bdb_id2entry_delete( be, ltid, e );
297 case DB_LOCK_DEADLOCK:
298 case DB_LOCK_NOTGRANTED:
303 Debug(LDAP_DEBUG_ARGS,
304 "<=- bdb_delete: id2entry failed: %s (%d)\n",
305 db_strerror(rc), rc, 0 );
306 text = "entry delete failed";
310 /* delete indices for old attributes */
311 rc = bdb_index_entry_del( be, ltid, e, e->e_attrs );
312 if ( rc != LDAP_SUCCESS ) {
314 case DB_LOCK_DEADLOCK:
315 case DB_LOCK_NOTGRANTED:
320 Debug( LDAP_DEBUG_ANY, "entry index delete failed!\n",
322 text = "entry index delete failed";
326 #if 0 /* Do we want to reclaim deleted IDs? */
327 ldap_pvt_thread_mutex_lock( &bdb->bi_lastid_mutex );
328 if ( e->e_id == bdb->bi_lastid ) {
329 bdb_last_id( be, ltid );
331 ldap_pvt_thread_mutex_unlock( &bdb->bi_lastid_mutex );
335 rc = txn_abort( ltid );
337 rc = txn_commit( ltid, 0 );
340 op->o_private = NULL;
343 Debug( LDAP_DEBUG_TRACE,
344 "bdb_delete: txn_%s failed: %s (%d)\n",
345 op->o_noop ? "abort (no-op)" : "commit",
346 db_strerror(rc), rc );
348 text = "commit failed";
351 Debug( LDAP_DEBUG_TRACE,
352 "bdb_delete: deleted%s id=%08lx dn=\"%s\"\n",
353 op->o_noop ? " (no-op)" : "",
360 send_ldap_result( conn, op, rc, 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;