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