]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/delete.c
Do deadlock detection on any conflict, instead of in a separate thread
[openldap] / servers / slapd / back-bdb / delete.c
1 /* delete.c - bdb backend delete routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11 #include <ac/string.h>
12
13 #include "back-bdb.h"
14 #include "external.h"
15
16 int
17 bdb_delete(
18         BackendDB       *be,
19         Connection      *conn,
20         Operation       *op,
21         struct berval   *dn,
22         struct berval   *ndn
23 )
24 {
25         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
26         Entry   *matched;
27         char    *pdn = NULL;
28         Entry   *e, *p = NULL;
29         int     rc;
30         const char *text;
31         int             manageDSAit = get_manageDSAit( op );
32         AttributeDescription *children = slap_schema.si_ad_children;
33         DB_TXN          *ltid = NULL;
34         struct bdb_op_info opinfo;
35
36         Debug( LDAP_DEBUG_ARGS, "==> bdb_delete: %s\n",
37                 dn->bv_val, 0, 0 );
38
39         if( 0 ) {
40 retry:  /* transaction retry */
41                 Debug( LDAP_DEBUG_TRACE, "==> bdb_delete: retrying...\n",
42                         0, 0, 0 );
43                 rc = txn_abort( ltid );
44                 ltid = NULL;
45                 op->o_private = NULL;
46                 if( rc != 0 ) {
47                         rc = LDAP_OTHER;
48                         text = "internal error";
49                         goto return_results;
50                 }
51         }
52
53         if( bdb->bi_txn ) {
54                 /* begin transaction */
55                 rc = txn_begin( bdb->bi_dbenv, NULL, &ltid, 
56                         bdb->bi_db_opflags );
57                 text = NULL;
58                 if( rc != 0 ) {
59                         Debug( LDAP_DEBUG_TRACE,
60                                 "bdb_delete: txn_begin failed: %s (%d)\n",
61                                 db_strerror(rc), rc, 0 );
62                         rc = LDAP_OTHER;
63                         text = "internal error";
64                         goto return_results;
65                 }
66         }
67
68         opinfo.boi_bdb = be;
69         opinfo.boi_txn = ltid;
70         opinfo.boi_err = 0;
71         op->o_private = &opinfo;
72
73         /* get entry for read/modify/write */
74         rc = bdb_dn2entry( be, ltid, ndn, &e, &matched, DB_RMW );
75
76         switch( rc ) {
77         case 0:
78         case DB_NOTFOUND:
79                 break;
80         case DB_LOCK_DEADLOCK:
81         case DB_LOCK_NOTGRANTED:
82                 goto retry;
83         default:
84                 rc = LDAP_OTHER;
85                 text = "internal error";
86                 goto return_results;
87         }
88
89         if ( e == NULL ) {
90                 char *matched_dn = NULL;
91                 BerVarray refs;
92
93                 Debug( LDAP_DEBUG_ARGS,
94                         "<=- bdb_delete: no such object %s\n",
95                         dn->bv_val, 0, 0);
96
97                 if ( matched != NULL ) {
98                         matched_dn = ch_strdup( matched->e_dn );
99                         refs = is_entry_referral( matched )
100                                 ? get_entry_referrals( be, conn, op, matched )
101                                 : NULL;
102                         bdb_entry_return( be, matched );
103                         matched = NULL;
104
105                 } else {
106                         refs = referral_rewrite( default_referral,
107                                 NULL, dn, LDAP_SCOPE_DEFAULT );
108                 }
109
110                 send_ldap_result( conn, op, LDAP_REFERRAL,
111                         matched_dn, NULL, refs, NULL );
112
113                 ber_bvarray_free( refs );
114                 free( matched_dn );
115
116                 rc = -1;
117                 goto done;
118         }
119
120         if ( be_issuffix( be, ndn->bv_val ) ) {
121                 pdn = NULL;
122         } else {
123                 rc = dnParent( ndn->bv_val, &pdn );
124                 if ( rc != LDAP_SUCCESS ) {
125                         text = "internal error";
126                         goto return_results;
127                 }
128         }
129
130         if( pdn != NULL && *pdn != '\0' ) {
131                 struct berval pbv;
132
133                 pbv.bv_len = ndn->bv_len - (pdn - ndn->bv_val);
134                 pbv.bv_val = pdn;
135                 /* get parent */
136                 rc = bdb_dn2entry( be, ltid, &pbv, &p, NULL, 0 );
137
138                 switch( rc ) {
139                 case 0:
140                 case DB_NOTFOUND:
141                         break;
142                 case DB_LOCK_DEADLOCK:
143                 case DB_LOCK_NOTGRANTED:
144                         goto retry;
145                 default:
146                         rc = LDAP_OTHER;
147                         text = "internal error";
148                         goto return_results;
149                 }
150
151                 if( p == NULL) {
152                         Debug( LDAP_DEBUG_TRACE,
153                                 "<=- bdb_delete: parent does not exist\n",
154                                 0, 0, 0);
155                         rc = LDAP_OTHER;
156                         text = "could not locate parent of entry";
157                         goto return_results;
158                 }
159
160                 /* check parent for "children" acl */
161                 rc = access_allowed( be, conn, op, p,
162                         children, NULL, ACL_WRITE );
163
164                 bdb_entry_return( be, p );
165                 p = NULL;
166
167                 if ( !rc  ) {
168                         Debug( LDAP_DEBUG_TRACE,
169                                 "<=- bdb_delete: no access to parent\n",
170                                 0, 0, 0 );
171                         rc = LDAP_INSUFFICIENT_ACCESS;
172                         goto return_results;
173                 }
174
175         } else {
176                 /* no parent, must be root to delete */
177                 if( ! be_isroot( be, &op->o_ndn ) ) {
178                         if ( be_issuffix( be, "" ) || be_isupdate( be, &op->o_ndn ) ) {
179                                 p = (Entry *)&slap_entry_root;
180
181                                 /* check parent for "children" acl */
182                                 rc = access_allowed( be, conn, op, p,
183                                         children, NULL, ACL_WRITE );
184                                 p = NULL;
185
186                                 if ( !rc  ) {
187                                         Debug( LDAP_DEBUG_TRACE,
188                                                 "<=- bdb_delete: no access "
189                                                 "to parent\n", 0, 0, 0 );
190                                         rc = LDAP_INSUFFICIENT_ACCESS;
191                                         goto return_results;
192                                 }
193
194                         } else {
195                                 Debug( LDAP_DEBUG_TRACE,
196                                         "<=- bdb_delete: no parent "
197                                         "and not root\n", 0, 0, 0);
198                                 rc = LDAP_INSUFFICIENT_ACCESS;
199                                 goto return_results;
200                         }
201                 }
202         }
203
204         if ( !manageDSAit && is_entry_referral( e ) ) {
205                 /* parent is a referral, don't allow add */
206                 /* parent is an alias, don't allow add */
207                 BerVarray refs = get_entry_referrals( be,
208                         conn, op, e );
209
210                 Debug( LDAP_DEBUG_TRACE,
211                         "bdb_delete: entry is referral\n",
212                         0, 0, 0 );
213
214                 send_ldap_result( conn, op, LDAP_REFERRAL,
215                         e->e_dn, NULL, refs, NULL );
216
217                 ber_bvarray_free( refs );
218
219                 rc = 1;
220                 goto done;
221         }
222
223         rc = bdb_dn2id_children( be, ltid, &e->e_nname );
224         if( rc != DB_NOTFOUND ) {
225                 switch( rc ) {
226                 case DB_LOCK_DEADLOCK:
227                 case DB_LOCK_NOTGRANTED:
228                         goto retry;
229                 case 0:
230                         Debug(LDAP_DEBUG_ARGS,
231                                 "<=- bdb_delete: non-leaf %s\n",
232                                 dn->bv_val, 0, 0);
233                         rc = LDAP_NOT_ALLOWED_ON_NONLEAF;
234                         text = "subtree delete not supported";
235                         break;
236                 default:
237                         Debug(LDAP_DEBUG_ARGS,
238                                 "<=- bdb_delete: has_children failed: %s (%d)\n",
239                                 db_strerror(rc), rc, 0 );
240                         rc = LDAP_OTHER;
241                         text = "internal error";
242                 }
243                 goto return_results;
244         }
245
246         /* delete from dn2id */
247         rc = bdb_dn2id_delete( be, ltid, pdn, e );
248         if ( rc != 0 ) {
249                 switch( rc ) {
250                 case DB_LOCK_DEADLOCK:
251                 case DB_LOCK_NOTGRANTED:
252                         goto retry;
253                 default:
254                         rc = LDAP_OTHER;
255                 }
256                 Debug(LDAP_DEBUG_ARGS,
257                         "<=- bdb_delete: dn2id failed: %s (%d)\n",
258                         db_strerror(rc), rc, 0 );
259                 text = "DN index delete failed";
260                 goto return_results;
261         }
262
263         /* delete indices for old attributes */
264         rc = bdb_index_entry_del( be, ltid, e, e->e_attrs );
265         if ( rc != LDAP_SUCCESS ) {
266                 switch( rc ) {
267                 case DB_LOCK_DEADLOCK:
268                 case DB_LOCK_NOTGRANTED:
269                         goto retry;
270                 default:
271                         rc = LDAP_OTHER;
272                 }
273                 Debug( LDAP_DEBUG_ANY, "entry index delete failed!\n",
274                         0, 0, 0 );
275                 text = "entry index delete failed";
276                 goto return_results;
277         }
278
279         /* delete from id2entry */
280         rc = bdb_id2entry_delete( be, ltid, e->e_id );
281         if ( rc != 0 ) {
282                 switch( rc ) {
283                 case DB_LOCK_DEADLOCK:
284                 case DB_LOCK_NOTGRANTED:
285                         goto retry;
286                 default:
287                         rc = LDAP_OTHER;
288                 }
289                 Debug(LDAP_DEBUG_ARGS,
290                         "<=- bdb_delete: id2entry failed: %s (%d)\n",
291                         db_strerror(rc), rc, 0 );
292                 text = "entry delete failed";
293                 goto return_results;
294         }
295
296
297 #if 0   /* Do we want to reclaim deleted IDs? */
298         ldap_pvt_thread_mutex_lock( &bdb->bi_lastid_mutex );
299         if ( e->e_id == bdb->bi_lastid ) {
300                 bdb_last_id( be, ltid );
301         }
302         ldap_pvt_thread_mutex_unlock( &bdb->bi_lastid_mutex );
303 #endif
304
305         if( bdb->bi_txn ) {
306                 rc = txn_commit( ltid, 0 );
307         }
308         ltid = NULL;
309         op->o_private = NULL;
310
311         if( rc != 0 ) {
312                 Debug( LDAP_DEBUG_TRACE,
313                         "bdb_delete: txn_commit failed: %s (%d)\n",
314                         db_strerror(rc), rc, 0 );
315                 rc = LDAP_OTHER;
316                 text = "commit failed";
317
318         } else {
319                 Debug( LDAP_DEBUG_TRACE,
320                         "bdb_delete: deleted id=%08lx dn=\"%s\"\n",
321                         e->e_id, e->e_dn, 0 );
322                 rc = LDAP_SUCCESS;
323                 text = NULL;
324         }
325
326 return_results:
327         send_ldap_result( conn, op, LDAP_SUCCESS,
328                 NULL, text, NULL, NULL );
329
330         if(rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
331                 ldap_pvt_thread_yield();
332                 TXN_CHECKPOINT( bdb->bi_dbenv,
333                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
334         }
335
336 done:
337         /* free entry */
338         if( e != NULL ) {
339                 bdb_entry_return( be, e );
340         }
341
342         if( ltid != NULL ) {
343                 txn_abort( ltid );
344                 op->o_private = NULL;
345         }
346
347         return rc;
348 }