Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / openldap / dist / servers / slapd / back-bdb / delete.c
blob88f73e1f18f773ba067c7d37b55a08d812587716
1 /* delete.c - bdb backend delete routine */
2 /* $OpenLDAP: pkg/ldap/servers/slapd/back-bdb/delete.c,v 1.155.2.8 2008/05/01 21:39:35 quanah Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2008 The OpenLDAP Foundation.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
10 * Public License.
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 #include "portable.h"
19 #include <stdio.h>
20 #include <ac/string.h>
22 #include "lutil.h"
23 #include "back-bdb.h"
25 int
26 bdb_delete( Operation *op, SlapReply *rs )
28 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
29 Entry *matched = NULL;
30 struct berval pdn = {0, NULL};
31 Entry *e = NULL;
32 Entry *p = NULL;
33 EntryInfo *ei = NULL, *eip = NULL;
34 int manageDSAit = get_manageDSAit( op );
35 AttributeDescription *children = slap_schema.si_ad_children;
36 AttributeDescription *entry = slap_schema.si_ad_entry;
37 DB_TXN *ltid = NULL, *lt2;
38 struct bdb_op_info opinfo = {0};
39 ID eid;
41 BDB_LOCKER locker = 0;
42 DB_LOCK lock, plock;
44 int num_retries = 0;
46 int rc;
48 LDAPControl **preread_ctrl = NULL;
49 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
50 int num_ctrls = 0;
52 int parent_is_glue = 0;
53 int parent_is_leaf = 0;
55 #ifdef LDAP_X_TXN
56 int settle = 0;
57 #endif
59 Debug( LDAP_DEBUG_ARGS, "==> " LDAP_XSTRING(bdb_delete) ": %s\n",
60 op->o_req_dn.bv_val, 0, 0 );
62 #ifdef LDAP_X_TXN
63 if( op->o_txnSpec ) {
64 /* acquire connection lock */
65 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
66 if( op->o_conn->c_txn == CONN_TXN_INACTIVE ) {
67 rs->sr_text = "invalid transaction identifier";
68 rs->sr_err = LDAP_X_TXN_ID_INVALID;
69 goto txnReturn;
70 } else if( op->o_conn->c_txn == CONN_TXN_SETTLE ) {
71 settle=1;
72 goto txnReturn;
75 if( op->o_conn->c_txn_backend == NULL ) {
76 op->o_conn->c_txn_backend = op->o_bd;
78 } else if( op->o_conn->c_txn_backend != op->o_bd ) {
79 rs->sr_text = "transaction cannot span multiple database contexts";
80 rs->sr_err = LDAP_AFFECTS_MULTIPLE_DSAS;
81 goto txnReturn;
84 /* insert operation into transaction */
86 rs->sr_text = "transaction specified";
87 rs->sr_err = LDAP_X_TXN_SPECIFY_OKAY;
89 txnReturn:
90 /* release connection lock */
91 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
93 if( !settle ) {
94 send_ldap_result( op, rs );
95 return rs->sr_err;
98 #endif
100 ctrls[num_ctrls] = 0;
102 /* allocate CSN */
103 if ( BER_BVISNULL( &op->o_csn ) ) {
104 struct berval csn;
105 char csnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
107 csn.bv_val = csnbuf;
108 csn.bv_len = sizeof(csnbuf);
109 slap_get_csn( op, &csn, 1 );
112 if( 0 ) {
113 retry: /* transaction retry */
114 if( e != NULL ) {
115 bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
116 e = NULL;
118 if( p != NULL ) {
119 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p);
120 p = NULL;
122 Debug( LDAP_DEBUG_TRACE,
123 "==> " LDAP_XSTRING(bdb_delete) ": retrying...\n",
124 0, 0, 0 );
125 rs->sr_err = TXN_ABORT( ltid );
126 ltid = NULL;
127 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
128 opinfo.boi_oe.oe_key = NULL;
129 op->o_do_not_cache = opinfo.boi_acl_cache;
130 if( rs->sr_err != 0 ) {
131 rs->sr_err = LDAP_OTHER;
132 rs->sr_text = "internal error";
133 goto return_results;
135 if ( op->o_abandon ) {
136 rs->sr_err = SLAPD_ABANDON;
137 goto return_results;
139 parent_is_glue = 0;
140 parent_is_leaf = 0;
141 bdb_trans_backoff( ++num_retries );
144 /* begin transaction */
145 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, &ltid,
146 bdb->bi_db_opflags );
147 rs->sr_text = NULL;
148 if( rs->sr_err != 0 ) {
149 Debug( LDAP_DEBUG_TRACE,
150 LDAP_XSTRING(bdb_delete) ": txn_begin failed: "
151 "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
152 rs->sr_err = LDAP_OTHER;
153 rs->sr_text = "internal error";
154 goto return_results;
157 locker = TXN_ID ( ltid );
159 opinfo.boi_oe.oe_key = bdb;
160 opinfo.boi_txn = ltid;
161 opinfo.boi_err = 0;
162 opinfo.boi_acl_cache = op->o_do_not_cache;
163 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &opinfo.boi_oe, oe_next );
165 if ( !be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
166 dnParent( &op->o_req_ndn, &pdn );
169 /* get entry */
170 rs->sr_err = bdb_dn2entry( op, ltid, &op->o_req_ndn, &ei, 1,
171 locker, &lock );
173 switch( rs->sr_err ) {
174 case 0:
175 case DB_NOTFOUND:
176 break;
177 case DB_LOCK_DEADLOCK:
178 case DB_LOCK_NOTGRANTED:
179 goto retry;
180 case LDAP_BUSY:
181 rs->sr_text = "ldap server busy";
182 goto return_results;
183 default:
184 rs->sr_err = LDAP_OTHER;
185 rs->sr_text = "internal error";
186 goto return_results;
189 if ( rs->sr_err == 0 ) {
190 e = ei->bei_e;
191 eip = ei->bei_parent;
192 } else {
193 matched = ei->bei_e;
196 /* FIXME : dn2entry() should return non-glue entry */
197 if ( e == NULL || ( !manageDSAit && is_entry_glue( e ))) {
198 Debug( LDAP_DEBUG_ARGS,
199 "<=- " LDAP_XSTRING(bdb_delete) ": no such object %s\n",
200 op->o_req_dn.bv_val, 0, 0);
202 if ( matched != NULL ) {
203 rs->sr_matched = ch_strdup( matched->e_dn );
204 rs->sr_ref = is_entry_referral( matched )
205 ? get_entry_referrals( op, matched )
206 : NULL;
207 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, matched);
208 matched = NULL;
210 } else {
211 rs->sr_ref = referral_rewrite( default_referral, NULL,
212 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
215 rs->sr_err = LDAP_REFERRAL;
216 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
217 goto return_results;
220 rc = bdb_cache_find_id( op, ltid, eip->bei_id, &eip, 0, locker, &plock );
221 switch( rc ) {
222 case DB_LOCK_DEADLOCK:
223 case DB_LOCK_NOTGRANTED:
224 goto retry;
225 case 0:
226 case DB_NOTFOUND:
227 break;
228 default:
229 rs->sr_err = LDAP_OTHER;
230 rs->sr_text = "internal error";
231 goto return_results;
233 if ( eip ) p = eip->bei_e;
235 if ( pdn.bv_len != 0 ) {
236 if( p == NULL || !bvmatch( &pdn, &p->e_nname )) {
237 Debug( LDAP_DEBUG_TRACE,
238 "<=- " LDAP_XSTRING(bdb_delete) ": parent "
239 "does not exist\n", 0, 0, 0 );
240 rs->sr_err = LDAP_OTHER;
241 rs->sr_text = "could not locate parent of entry";
242 goto return_results;
245 /* check parent for "children" acl */
246 rs->sr_err = access_allowed( op, p,
247 children, NULL, ACL_WDEL, NULL );
249 if ( !rs->sr_err ) {
250 switch( opinfo.boi_err ) {
251 case DB_LOCK_DEADLOCK:
252 case DB_LOCK_NOTGRANTED:
253 goto retry;
256 Debug( LDAP_DEBUG_TRACE,
257 "<=- " LDAP_XSTRING(bdb_delete) ": no write "
258 "access to parent\n", 0, 0, 0 );
259 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
260 rs->sr_text = "no write access to parent";
261 goto return_results;
264 } else {
265 /* no parent, must be root to delete */
266 if( ! be_isroot( op ) ) {
267 if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv )
268 || be_shadow_update( op ) ) {
269 p = (Entry *)&slap_entry_root;
271 /* check parent for "children" acl */
272 rs->sr_err = access_allowed( op, p,
273 children, NULL, ACL_WDEL, NULL );
275 p = NULL;
277 if ( !rs->sr_err ) {
278 switch( opinfo.boi_err ) {
279 case DB_LOCK_DEADLOCK:
280 case DB_LOCK_NOTGRANTED:
281 goto retry;
284 Debug( LDAP_DEBUG_TRACE,
285 "<=- " LDAP_XSTRING(bdb_delete)
286 ": no access to parent\n",
287 0, 0, 0 );
288 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
289 rs->sr_text = "no write access to parent";
290 goto return_results;
293 } else {
294 Debug( LDAP_DEBUG_TRACE,
295 "<=- " LDAP_XSTRING(bdb_delete)
296 ": no parent and not root\n", 0, 0, 0 );
297 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
298 goto return_results;
303 if ( get_assert( op ) &&
304 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
306 rs->sr_err = LDAP_ASSERTION_FAILED;
307 goto return_results;
310 rs->sr_err = access_allowed( op, e,
311 entry, NULL, ACL_WDEL, NULL );
313 if ( !rs->sr_err ) {
314 switch( opinfo.boi_err ) {
315 case DB_LOCK_DEADLOCK:
316 case DB_LOCK_NOTGRANTED:
317 goto retry;
320 Debug( LDAP_DEBUG_TRACE,
321 "<=- " LDAP_XSTRING(bdb_delete) ": no write access "
322 "to entry\n", 0, 0, 0 );
323 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
324 rs->sr_text = "no write access to entry";
325 goto return_results;
328 if ( !manageDSAit && is_entry_referral( e ) ) {
329 /* entry is a referral, don't allow delete */
330 rs->sr_ref = get_entry_referrals( op, e );
332 Debug( LDAP_DEBUG_TRACE,
333 LDAP_XSTRING(bdb_delete) ": entry is referral\n",
334 0, 0, 0 );
336 rs->sr_err = LDAP_REFERRAL;
337 rs->sr_matched = ch_strdup( e->e_name.bv_val );
338 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
339 goto return_results;
342 /* pre-read */
343 if( op->o_preread ) {
344 if( preread_ctrl == NULL ) {
345 preread_ctrl = &ctrls[num_ctrls++];
346 ctrls[num_ctrls] = NULL;
348 if( slap_read_controls( op, rs, e,
349 &slap_pre_read_bv, preread_ctrl ) )
351 Debug( LDAP_DEBUG_TRACE,
352 "<=- " LDAP_XSTRING(bdb_delete) ": pre-read "
353 "failed!\n", 0, 0, 0 );
354 if ( op->o_preread & SLAP_CONTROL_CRITICAL ) {
355 /* FIXME: is it correct to abort
356 * operation if control fails? */
357 goto return_results;
362 /* nested transaction */
363 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, &lt2,
364 bdb->bi_db_opflags );
365 rs->sr_text = NULL;
366 if( rs->sr_err != 0 ) {
367 Debug( LDAP_DEBUG_TRACE,
368 LDAP_XSTRING(bdb_delete) ": txn_begin(2) failed: "
369 "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
370 rs->sr_err = LDAP_OTHER;
371 rs->sr_text = "internal error";
372 goto return_results;
375 BDB_LOG_PRINTF( bdb->bi_dbenv, lt2, "slapd Starting delete %s(%d)",
376 e->e_nname.bv_val, e->e_id );
378 /* Can't do it if we have kids */
379 rs->sr_err = bdb_cache_children( op, lt2, e );
380 if( rs->sr_err != DB_NOTFOUND ) {
381 switch( rs->sr_err ) {
382 case DB_LOCK_DEADLOCK:
383 case DB_LOCK_NOTGRANTED:
384 goto retry;
385 case 0:
386 Debug(LDAP_DEBUG_ARGS,
387 "<=- " LDAP_XSTRING(bdb_delete)
388 ": non-leaf %s\n",
389 op->o_req_dn.bv_val, 0, 0);
390 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
391 rs->sr_text = "subordinate objects must be deleted first";
392 break;
393 default:
394 Debug(LDAP_DEBUG_ARGS,
395 "<=- " LDAP_XSTRING(bdb_delete)
396 ": has_children failed: %s (%d)\n",
397 db_strerror(rs->sr_err), rs->sr_err, 0 );
398 rs->sr_err = LDAP_OTHER;
399 rs->sr_text = "internal error";
401 goto return_results;
404 /* delete from dn2id */
405 rs->sr_err = bdb_dn2id_delete( op, lt2, eip, e );
406 if ( rs->sr_err != 0 ) {
407 Debug(LDAP_DEBUG_TRACE,
408 "<=- " LDAP_XSTRING(bdb_delete) ": dn2id failed: "
409 "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
410 switch( rs->sr_err ) {
411 case DB_LOCK_DEADLOCK:
412 case DB_LOCK_NOTGRANTED:
413 goto retry;
415 rs->sr_text = "DN index delete failed";
416 rs->sr_err = LDAP_OTHER;
417 goto return_results;
420 /* delete indices for old attributes */
421 rs->sr_err = bdb_index_entry_del( op, lt2, e );
422 if ( rs->sr_err != LDAP_SUCCESS ) {
423 Debug(LDAP_DEBUG_TRACE,
424 "<=- " LDAP_XSTRING(bdb_delete) ": index failed: "
425 "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
426 switch( rs->sr_err ) {
427 case DB_LOCK_DEADLOCK:
428 case DB_LOCK_NOTGRANTED:
429 goto retry;
431 rs->sr_text = "entry index delete failed";
432 rs->sr_err = LDAP_OTHER;
433 goto return_results;
436 /* fixup delete CSN */
437 if ( !SLAP_SHADOW( op->o_bd )) {
438 struct berval vals[2];
440 assert( !BER_BVISNULL( &op->o_csn ) );
441 vals[0] = op->o_csn;
442 BER_BVZERO( &vals[1] );
443 rs->sr_err = bdb_index_values( op, lt2, slap_schema.si_ad_entryCSN,
444 vals, 0, SLAP_INDEX_ADD_OP );
445 if ( rs->sr_err != LDAP_SUCCESS ) {
446 switch( rs->sr_err ) {
447 case DB_LOCK_DEADLOCK:
448 case DB_LOCK_NOTGRANTED:
449 goto retry;
451 rs->sr_text = "entryCSN index update failed";
452 rs->sr_err = LDAP_OTHER;
453 goto return_results;
457 /* delete from id2entry */
458 rs->sr_err = bdb_id2entry_delete( op->o_bd, lt2, e );
459 if ( rs->sr_err != 0 ) {
460 Debug( LDAP_DEBUG_TRACE,
461 "<=- " LDAP_XSTRING(bdb_delete) ": id2entry failed: "
462 "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
463 switch( rs->sr_err ) {
464 case DB_LOCK_DEADLOCK:
465 case DB_LOCK_NOTGRANTED:
466 goto retry;
468 rs->sr_text = "entry delete failed";
469 rs->sr_err = LDAP_OTHER;
470 goto return_results;
473 if ( pdn.bv_len != 0 ) {
474 parent_is_glue = is_entry_glue(p);
475 rs->sr_err = bdb_cache_children( op, lt2, p );
476 if ( rs->sr_err != DB_NOTFOUND ) {
477 switch( rs->sr_err ) {
478 case DB_LOCK_DEADLOCK:
479 case DB_LOCK_NOTGRANTED:
480 goto retry;
481 case 0:
482 break;
483 default:
484 Debug(LDAP_DEBUG_ARGS,
485 "<=- " LDAP_XSTRING(bdb_delete)
486 ": has_children failed: %s (%d)\n",
487 db_strerror(rs->sr_err), rs->sr_err, 0 );
488 rs->sr_err = LDAP_OTHER;
489 rs->sr_text = "internal error";
490 goto return_results;
492 parent_is_leaf = 1;
494 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p);
495 p = NULL;
498 BDB_LOG_PRINTF( bdb->bi_dbenv, lt2, "slapd Commit1 delete %s(%d)",
499 e->e_nname.bv_val, e->e_id );
501 if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
502 rs->sr_err = LDAP_OTHER;
503 rs->sr_text = "txn_commit(2) failed";
504 goto return_results;
507 eid = e->e_id;
509 #if 0 /* Do we want to reclaim deleted IDs? */
510 ldap_pvt_thread_mutex_lock( &bdb->bi_lastid_mutex );
511 if ( e->e_id == bdb->bi_lastid ) {
512 bdb_last_id( op->o_bd, ltid );
514 ldap_pvt_thread_mutex_unlock( &bdb->bi_lastid_mutex );
515 #endif
517 if( op->o_noop ) {
518 if ( ( rs->sr_err = TXN_ABORT( ltid ) ) != 0 ) {
519 rs->sr_text = "txn_abort (no-op) failed";
520 } else {
521 rs->sr_err = LDAP_X_NO_OPERATION;
522 ltid = NULL;
523 goto return_results;
525 } else {
527 BDB_LOG_PRINTF( bdb->bi_dbenv, ltid, "slapd Cache delete %s(%d)",
528 e->e_nname.bv_val, e->e_id );
530 rc = bdb_cache_delete( bdb, e, locker, &lock );
531 switch( rc ) {
532 case DB_LOCK_DEADLOCK:
533 case DB_LOCK_NOTGRANTED:
534 goto retry;
537 rs->sr_err = TXN_COMMIT( ltid, 0 );
539 ltid = NULL;
540 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
541 opinfo.boi_oe.oe_key = NULL;
543 BDB_LOG_PRINTF( bdb->bi_dbenv, NULL, "slapd Committed delete %s(%d)",
544 e->e_nname.bv_val, e->e_id );
546 if( rs->sr_err != 0 ) {
547 Debug( LDAP_DEBUG_TRACE,
548 LDAP_XSTRING(bdb_delete) ": txn_%s failed: %s (%d)\n",
549 op->o_noop ? "abort (no-op)" : "commit",
550 db_strerror(rs->sr_err), rs->sr_err );
551 rs->sr_err = LDAP_OTHER;
552 rs->sr_text = "commit failed";
554 goto return_results;
557 Debug( LDAP_DEBUG_TRACE,
558 LDAP_XSTRING(bdb_delete) ": deleted%s id=%08lx dn=\"%s\"\n",
559 op->o_noop ? " (no-op)" : "",
560 eid, op->o_req_dn.bv_val );
561 rs->sr_err = LDAP_SUCCESS;
562 rs->sr_text = NULL;
563 if( num_ctrls ) rs->sr_ctrls = ctrls;
565 return_results:
566 if ( rs->sr_err == LDAP_SUCCESS && parent_is_glue && parent_is_leaf ) {
567 op->o_delete_glue_parent = 1;
570 if ( p )
571 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p);
573 /* free entry */
574 if( e != NULL ) {
575 if ( rs->sr_err == LDAP_SUCCESS ) {
576 /* Free the EntryInfo and the Entry */
577 bdb_cache_entryinfo_lock( BEI(e) );
578 bdb_cache_delete_cleanup( &bdb->bi_cache, BEI(e) );
579 } else {
580 bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
584 if( ltid != NULL ) {
585 TXN_ABORT( ltid );
587 if ( opinfo.boi_oe.oe_key ) {
588 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
591 send_ldap_result( op, rs );
592 slap_graduate_commit_csn( op );
594 if( preread_ctrl != NULL && (*preread_ctrl) != NULL ) {
595 slap_sl_free( (*preread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
596 slap_sl_free( *preread_ctrl, op->o_tmpmemctx );
599 if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp_kbyte ) {
600 TXN_CHECKPOINT( bdb->bi_dbenv,
601 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
603 return rs->sr_err;