]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/delete.c
Added ldap_pvt_thread_yield() after txn_abort
[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                 if ( !rc  ) {
135                         Debug( LDAP_DEBUG_TRACE,
136                                 "<=- bdb_delete: no access to parent\n",
137                                 0, 0, 0 );
138                         rc = LDAP_INSUFFICIENT_ACCESS;
139                         goto return_results;
140                 }
141
142         } else {
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;
147
148                                 /* check parent for "children" acl */
149                                 rc = access_allowed( be, conn, op, p,
150                                         children, NULL, ACL_WRITE );
151                                 p = NULL;
152
153                                 if ( !rc  ) {
154                                         Debug( LDAP_DEBUG_TRACE,
155                                                 "<=- bdb_delete: no access "
156                                                 "to parent\n", 0, 0, 0 );
157                                         rc = LDAP_INSUFFICIENT_ACCESS;
158                                         goto return_results;
159                                 }
160
161                         } else {
162                                 Debug( LDAP_DEBUG_TRACE,
163                                         "<=- bdb_delete: no parent "
164                                         "and not root\n", 0, 0, 0);
165                                 rc = LDAP_INSUFFICIENT_ACCESS;
166                                 goto return_results;
167                         }
168                 }
169 #if 0
170                 if ( ltid ) {
171                         DBT obj;
172                         obj.data = ",";
173                         obj.size = 1;
174                         rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
175                                 DB_LOCK_WRITE, &lock);
176                 }
177 #endif
178         }
179
180         /* get entry for read/modify/write */
181         rc = bdb_dn2entry( be, ltid, ndn, &e, &matched, DB_RMW );
182
183         switch( rc ) {
184         case 0:
185         case DB_NOTFOUND:
186                 break;
187         case DB_LOCK_DEADLOCK:
188         case DB_LOCK_NOTGRANTED:
189                 goto retry;
190         default:
191                 rc = LDAP_OTHER;
192                 text = "internal error";
193                 goto return_results;
194         }
195
196         if ( e == NULL ) {
197                 char *matched_dn = NULL;
198                 BerVarray refs;
199
200                 Debug( LDAP_DEBUG_ARGS,
201                         "<=- bdb_delete: no such object %s\n",
202                         dn->bv_val, 0, 0);
203
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 )
208                                 : NULL;
209                         bdb_entry_return( be, matched );
210                         matched = NULL;
211
212                 } else {
213                         refs = referral_rewrite( default_referral,
214                                 NULL, dn, LDAP_SCOPE_DEFAULT );
215                 }
216
217                 send_ldap_result( conn, op, LDAP_REFERRAL,
218                         matched_dn, NULL, refs, NULL );
219
220                 ber_bvarray_free( refs );
221                 free( matched_dn );
222
223                 rc = -1;
224                 goto done;
225         }
226
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,
231                         conn, op, e );
232
233                 Debug( LDAP_DEBUG_TRACE,
234                         "bdb_delete: entry is referral\n",
235                         0, 0, 0 );
236
237                 send_ldap_result( conn, op, LDAP_REFERRAL,
238                         e->e_dn, NULL, refs, NULL );
239
240                 ber_bvarray_free( refs );
241
242                 rc = 1;
243                 goto done;
244         }
245
246         rc = bdb_dn2id_children( be, ltid, &e->e_nname );
247         if( rc != DB_NOTFOUND ) {
248                 switch( rc ) {
249                 case DB_LOCK_DEADLOCK:
250                 case DB_LOCK_NOTGRANTED:
251                         goto retry;
252                 case 0:
253                         Debug(LDAP_DEBUG_ARGS,
254                                 "<=- bdb_delete: non-leaf %s\n",
255                                 dn->bv_val, 0, 0);
256                         rc = LDAP_NOT_ALLOWED_ON_NONLEAF;
257                         text = "subtree delete not supported";
258                         break;
259                 default:
260                         Debug(LDAP_DEBUG_ARGS,
261                                 "<=- bdb_delete: has_children failed: %s (%d)\n",
262                                 db_strerror(rc), rc, 0 );
263                         rc = LDAP_OTHER;
264                         text = "internal error";
265                 }
266                 goto return_results;
267         }
268
269         /* delete from dn2id */
270         rc = bdb_dn2id_delete( be, ltid, pdn.bv_val, e );
271         if ( rc != 0 ) {
272                 switch( rc ) {
273                 case DB_LOCK_DEADLOCK:
274                 case DB_LOCK_NOTGRANTED:
275                         goto retry;
276                 default:
277                         rc = LDAP_OTHER;
278                 }
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";
283                 goto return_results;
284         }
285
286         /* delete from id2entry */
287         rc = bdb_id2entry_delete( be, ltid, e->e_id );
288         if ( rc != 0 ) {
289                 switch( rc ) {
290                 case DB_LOCK_DEADLOCK:
291                 case DB_LOCK_NOTGRANTED:
292                         goto retry;
293                 default:
294                         rc = LDAP_OTHER;
295                 }
296                 Debug(LDAP_DEBUG_ARGS,
297                         "<=- bdb_delete: id2entry failed: %s (%d)\n",
298                         db_strerror(rc), rc, 0 );
299                 text = "entry delete failed";
300                 goto return_results;
301         }
302
303         /* delete indices for old attributes */
304         rc = bdb_index_entry_del( be, ltid, e, e->e_attrs );
305         if ( rc != LDAP_SUCCESS ) {
306                 switch( rc ) {
307                 case DB_LOCK_DEADLOCK:
308                 case DB_LOCK_NOTGRANTED:
309                         goto retry;
310                 default:
311                         rc = LDAP_OTHER;
312                 }
313                 Debug( LDAP_DEBUG_ANY, "entry index delete failed!\n",
314                         0, 0, 0 );
315                 text = "entry index delete failed";
316                 goto return_results;
317         }
318
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 );
323         }
324         ldap_pvt_thread_mutex_unlock( &bdb->bi_lastid_mutex );
325 #endif
326
327         if( bdb->bi_txn ) {
328                 rc = txn_commit( ltid, 0 );
329         }
330         ltid = NULL;
331         op->o_private = NULL;
332
333         if( rc != 0 ) {
334                 Debug( LDAP_DEBUG_TRACE,
335                         "bdb_delete: txn_commit failed: %s (%d)\n",
336                         db_strerror(rc), rc, 0 );
337                 rc = LDAP_OTHER;
338                 text = "commit failed";
339
340         } else {
341                 Debug( LDAP_DEBUG_TRACE,
342                         "bdb_delete: deleted id=%08lx dn=\"%s\"\n",
343                         e->e_id, e->e_dn, 0 );
344                 rc = LDAP_SUCCESS;
345                 text = NULL;
346         }
347
348 return_results:
349         send_ldap_result( conn, op, LDAP_SUCCESS,
350                 NULL, text, NULL, NULL );
351
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 );
356         }
357
358 done:
359         /* free entry */
360         if( e != NULL ) {
361                 bdb_entry_return( be, e );
362         }
363
364         if( ltid != NULL ) {
365                 txn_abort( ltid );
366                 op->o_private = NULL;
367         }
368
369         return rc;
370 }