]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/delete.c
Set lock detector to DEFAULT, not NORUN.
[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         struct berval   pdn = {0, 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 #if 0
36         u_int32_t       lockid;
37         DB_LOCK         lock;
38 #endif
39
40         Debug( LDAP_DEBUG_ARGS, "==> bdb_delete: %s\n",
41                 dn->bv_val, 0, 0 );
42
43         if( 0 ) {
44 retry:  /* transaction retry */
45                 Debug( LDAP_DEBUG_TRACE, "==> bdb_delete: retrying...\n",
46                         0, 0, 0 );
47                 rc = txn_abort( ltid );
48                 ltid = NULL;
49                 op->o_private = NULL;
50                 if( rc != 0 ) {
51                         rc = LDAP_OTHER;
52                         text = "internal error";
53                         goto return_results;
54                 }
55                 ldap_pvt_thread_yield();
56         }
57
58         if( bdb->bi_txn ) {
59                 /* begin transaction */
60                 rc = txn_begin( bdb->bi_dbenv, NULL, &ltid, 
61                         bdb->bi_db_opflags );
62                 text = NULL;
63                 if( rc != 0 ) {
64                         Debug( LDAP_DEBUG_TRACE,
65                                 "bdb_delete: txn_begin failed: %s (%d)\n",
66                                 db_strerror(rc), rc, 0 );
67                         rc = LDAP_OTHER;
68                         text = "internal error";
69                         goto return_results;
70                 }
71 #if 0
72                 lockid = TXN_ID( ltid );
73 #endif
74         }
75
76         opinfo.boi_bdb = be;
77         opinfo.boi_txn = ltid;
78         opinfo.boi_err = 0;
79         op->o_private = &opinfo;
80
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";
85                         goto return_results;
86                 }
87                 if (pdn.bv_val && pdn.bv_val[0]) {
88                         pdn.bv_len = ndn->bv_len - (pdn.bv_val - ndn->bv_val);
89                 }
90         }
91
92         if( pdn.bv_len != 0 ) {
93 #if 0
94                 if ( ltid ) {
95                         DBT obj;
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);
100                 }
101 #endif
102                 /* get parent */
103                 rc = bdb_dn2entry( be, ltid, &pdn, &p, NULL, 0 );
104
105                 switch( rc ) {
106                 case 0:
107                 case DB_NOTFOUND:
108                         break;
109                 case DB_LOCK_DEADLOCK:
110                 case DB_LOCK_NOTGRANTED:
111                         goto retry;
112                 default:
113                         rc = LDAP_OTHER;
114                         text = "internal error";
115                         goto return_results;
116                 }
117
118                 if( p == NULL) {
119                         Debug( LDAP_DEBUG_TRACE,
120                                 "<=- bdb_delete: parent does not exist\n",
121                                 0, 0, 0);
122                         rc = LDAP_OTHER;
123                         text = "could not locate parent of entry";
124                         goto return_results;
125                 }
126
127                 /* check parent for "children" acl */
128                 rc = access_allowed( be, conn, op, p,
129                         children, NULL, ACL_WRITE );
130
131                 bdb_entry_return( be, p );
132                 p = NULL;
133
134                 switch( opinfo.boi_err ) {
135                 case DB_LOCK_DEADLOCK:
136                 case DB_LOCK_NOTGRANTED:
137                         goto retry;
138                 }
139
140                 if ( !rc  ) {
141                         Debug( LDAP_DEBUG_TRACE,
142                                 "<=- bdb_delete: no access to parent\n",
143                                 0, 0, 0 );
144                         rc = LDAP_INSUFFICIENT_ACCESS;
145                         goto return_results;
146                 }
147
148         } else {
149                 /* no parent, must be root to delete */
150                 if( ! be_isroot( be, &op->o_ndn ) ) {
151                         if ( be_issuffix( be, "" ) || be_isupdate( be, &op->o_ndn ) ) {
152                                 p = (Entry *)&slap_entry_root;
153
154                                 /* check parent for "children" acl */
155                                 rc = access_allowed( be, conn, op, p,
156                                         children, NULL, ACL_WRITE );
157                                 p = NULL;
158
159                                 switch( opinfo.boi_err ) {
160                                 case DB_LOCK_DEADLOCK:
161                                 case DB_LOCK_NOTGRANTED:
162                                         goto retry;
163                                 }
164
165                                 if ( !rc  ) {
166                                         Debug( LDAP_DEBUG_TRACE,
167                                                 "<=- bdb_delete: no access "
168                                                 "to parent\n", 0, 0, 0 );
169                                         rc = LDAP_INSUFFICIENT_ACCESS;
170                                         goto return_results;
171                                 }
172
173                         } else {
174                                 Debug( LDAP_DEBUG_TRACE,
175                                         "<=- bdb_delete: no parent "
176                                         "and not root\n", 0, 0, 0);
177                                 rc = LDAP_INSUFFICIENT_ACCESS;
178                                 goto return_results;
179                         }
180                 }
181
182 #if 0
183                 if ( ltid ) {
184                         DBT obj;
185                         obj.data = ",";
186                         obj.size = 1;
187                         rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
188                                 DB_LOCK_WRITE, &lock);
189                 }
190 #endif
191         }
192
193         /* get entry for read/modify/write */
194         rc = bdb_dn2entry( be, ltid, ndn, &e, &matched, DB_RMW );
195
196         switch( rc ) {
197         case 0:
198         case DB_NOTFOUND:
199                 break;
200         case DB_LOCK_DEADLOCK:
201         case DB_LOCK_NOTGRANTED:
202                 goto retry;
203         default:
204                 rc = LDAP_OTHER;
205                 text = "internal error";
206                 goto return_results;
207         }
208
209         if ( e == NULL ) {
210                 char *matched_dn = NULL;
211                 BerVarray refs;
212
213                 Debug( LDAP_DEBUG_ARGS,
214                         "<=- bdb_delete: no such object %s\n",
215                         dn->bv_val, 0, 0);
216
217                 if ( matched != NULL ) {
218                         matched_dn = ch_strdup( matched->e_dn );
219                         refs = is_entry_referral( matched )
220                                 ? get_entry_referrals( be, conn, op, matched )
221                                 : NULL;
222                         bdb_entry_return( be, matched );
223                         matched = NULL;
224
225                 } else {
226                         refs = referral_rewrite( default_referral,
227                                 NULL, dn, LDAP_SCOPE_DEFAULT );
228                 }
229
230                 send_ldap_result( conn, op, LDAP_REFERRAL,
231                         matched_dn, NULL, refs, NULL );
232
233                 ber_bvarray_free( refs );
234                 free( matched_dn );
235
236                 rc = -1;
237                 goto done;
238         }
239
240         if ( !manageDSAit && is_entry_referral( e ) ) {
241                 /* parent is a referral, don't allow add */
242                 /* parent is an alias, don't allow add */
243                 BerVarray refs = get_entry_referrals( be,
244                         conn, op, e );
245
246                 Debug( LDAP_DEBUG_TRACE,
247                         "bdb_delete: entry is referral\n",
248                         0, 0, 0 );
249
250                 send_ldap_result( conn, op, LDAP_REFERRAL,
251                         e->e_dn, NULL, refs, NULL );
252
253                 ber_bvarray_free( refs );
254
255                 rc = 1;
256                 goto done;
257         }
258
259         rc = bdb_dn2id_children( be, ltid, &e->e_nname );
260         if( rc != DB_NOTFOUND ) {
261                 switch( rc ) {
262                 case DB_LOCK_DEADLOCK:
263                 case DB_LOCK_NOTGRANTED:
264                         goto retry;
265                 case 0:
266                         Debug(LDAP_DEBUG_ARGS,
267                                 "<=- bdb_delete: non-leaf %s\n",
268                                 dn->bv_val, 0, 0);
269                         rc = LDAP_NOT_ALLOWED_ON_NONLEAF;
270                         text = "subtree delete not supported";
271                         break;
272                 default:
273                         Debug(LDAP_DEBUG_ARGS,
274                                 "<=- bdb_delete: has_children failed: %s (%d)\n",
275                                 db_strerror(rc), rc, 0 );
276                         rc = LDAP_OTHER;
277                         text = "internal error";
278                 }
279                 goto return_results;
280         }
281
282         /* delete from dn2id */
283         rc = bdb_dn2id_delete( be, ltid, pdn.bv_val, e );
284         if ( rc != 0 ) {
285                 switch( rc ) {
286                 case DB_LOCK_DEADLOCK:
287                 case DB_LOCK_NOTGRANTED:
288                         goto retry;
289                 default:
290                         rc = LDAP_OTHER;
291                 }
292                 Debug(LDAP_DEBUG_ARGS,
293                         "<=- bdb_delete: dn2id failed: %s (%d)\n",
294                         db_strerror(rc), rc, 0 );
295                 text = "DN index delete failed";
296                 goto return_results;
297         }
298
299         /* delete from id2entry */
300         rc = bdb_id2entry_delete( be, ltid, e->e_id );
301         if ( rc != 0 ) {
302                 switch( rc ) {
303                 case DB_LOCK_DEADLOCK:
304                 case DB_LOCK_NOTGRANTED:
305                         goto retry;
306                 default:
307                         rc = LDAP_OTHER;
308                 }
309                 Debug(LDAP_DEBUG_ARGS,
310                         "<=- bdb_delete: id2entry failed: %s (%d)\n",
311                         db_strerror(rc), rc, 0 );
312                 text = "entry delete failed";
313                 goto return_results;
314         }
315
316         /* delete indices for old attributes */
317         rc = bdb_index_entry_del( be, ltid, e, e->e_attrs );
318         if ( rc != LDAP_SUCCESS ) {
319                 switch( rc ) {
320                 case DB_LOCK_DEADLOCK:
321                 case DB_LOCK_NOTGRANTED:
322                         goto retry;
323                 default:
324                         rc = LDAP_OTHER;
325                 }
326                 Debug( LDAP_DEBUG_ANY, "entry index delete failed!\n",
327                         0, 0, 0 );
328                 text = "entry index delete failed";
329                 goto return_results;
330         }
331
332 #if 0   /* Do we want to reclaim deleted IDs? */
333         ldap_pvt_thread_mutex_lock( &bdb->bi_lastid_mutex );
334         if ( e->e_id == bdb->bi_lastid ) {
335                 bdb_last_id( be, ltid );
336         }
337         ldap_pvt_thread_mutex_unlock( &bdb->bi_lastid_mutex );
338 #endif
339
340         if( bdb->bi_txn ) {
341                 rc = txn_commit( ltid, 0 );
342         }
343         ltid = NULL;
344         op->o_private = NULL;
345
346         if( rc != 0 ) {
347                 Debug( LDAP_DEBUG_TRACE,
348                         "bdb_delete: txn_commit failed: %s (%d)\n",
349                         db_strerror(rc), rc, 0 );
350                 rc = LDAP_OTHER;
351                 text = "commit failed";
352
353         } else {
354                 Debug( LDAP_DEBUG_TRACE,
355                         "bdb_delete: deleted id=%08lx dn=\"%s\"\n",
356                         e->e_id, e->e_dn, 0 );
357                 rc = LDAP_SUCCESS;
358                 text = NULL;
359         }
360
361 return_results:
362         send_ldap_result( conn, op, LDAP_SUCCESS,
363                 NULL, text, NULL, NULL );
364
365         if(rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
366                 ldap_pvt_thread_yield();
367                 TXN_CHECKPOINT( bdb->bi_dbenv,
368                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
369         }
370
371 done:
372         /* free entry */
373         if( e != NULL ) {
374                 bdb_entry_return( be, e );
375         }
376
377         if( ltid != NULL ) {
378                 txn_abort( ltid );
379                 op->o_private = NULL;
380         }
381
382         return rc;
383 }