]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/modify.c
Change recover logic
[openldap] / servers / slapd / back-bdb / modify.c
1 /* modify.c - bdb backend modify 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 #include <ac/time.h>
13
14 #include "back-bdb.h"
15 #include "external.h"
16
17 int bdb_modify_internal(
18         BackendDB *be,
19         Connection *conn,
20         Operation *op,
21         DB_TXN *tid,
22         Modifications *modlist,
23         Entry *e,
24         const char **text,
25         char *textbuf,
26         size_t textlen )
27 {
28         int rc, err;
29         Modification    *mod;
30         Modifications   *ml;
31         Attribute       *save_attrs;
32         Attribute       *ap;
33
34 #ifdef NEW_LOGGING
35         LDAP_LOG ( OPERATION, ENTRY, "bdb_modify_internal: 0x%08lx: %s\n", 
36                 e->e_id, e->e_dn, 0 );
37 #else
38         Debug( LDAP_DEBUG_TRACE, "bdb_modify_internal: 0x%08lx: %s\n",
39                 e->e_id, e->e_dn, 0);
40 #endif
41
42         if ( !acl_check_modlist( be, conn, op, e, modlist )) {
43                 return LDAP_INSUFFICIENT_ACCESS;
44         }
45
46         save_attrs = e->e_attrs;
47         e->e_attrs = attrs_dup( e->e_attrs );
48
49         for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
50                 mod = &ml->sml_mod;
51
52                 switch ( mod->sm_op ) {
53                 case LDAP_MOD_ADD:
54 #ifdef NEW_LOGGING
55                         LDAP_LOG ( OPERATION, DETAIL1, "bdb_modify_internal: add\n", 0,0,0);
56 #else
57                         Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: add\n", 0, 0, 0);
58 #endif
59                         err = modify_add_values( e, mod, text, textbuf, textlen );
60                         if( err != LDAP_SUCCESS ) {
61 #ifdef NEW_LOGGING
62                                 LDAP_LOG ( OPERATION, ERR, 
63                                         "bdb_modify_internal: %d %s\n", err, *text, 0 );
64 #else
65                                 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
66                                         err, *text, 0);
67 #endif
68                         }
69                         break;
70
71                 case LDAP_MOD_DELETE:
72 #ifdef NEW_LOGGING
73                         LDAP_LOG ( OPERATION, DETAIL1, 
74                                 "bdb_modify_internal: delete\n", 0, 0, 0 );
75 #else
76                         Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: delete\n", 0, 0, 0);
77 #endif
78                         err = modify_delete_values( e, mod, text, textbuf, textlen );
79                         assert( err != LDAP_TYPE_OR_VALUE_EXISTS );
80                         if( err != LDAP_SUCCESS ) {
81 #ifdef NEW_LOGGING
82                                 LDAP_LOG ( OPERATION, ERR, 
83                                         "bdb_modify_internal: %d %s\n", err, *text, 0 );
84 #else
85                                 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
86                                         err, *text, 0);
87 #endif
88                         }
89                         break;
90
91                 case LDAP_MOD_REPLACE:
92 #ifdef NEW_LOGGING
93                         LDAP_LOG ( OPERATION, DETAIL1, 
94                                 "bdb_modify_internal: replace\n", 0, 0, 0 );
95 #else
96                         Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: replace\n", 0, 0, 0);
97 #endif
98                         err = modify_replace_values( e, mod, text, textbuf, textlen );
99                         if( err != LDAP_SUCCESS ) {
100 #ifdef NEW_LOGGING
101                                 LDAP_LOG ( OPERATION, ERR, 
102                                         "bdb_modify_internal: %d %s\n", err, *text, 0 );
103 #else
104                                 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
105                                         err, *text, 0);
106 #endif
107                         }
108                         break;
109
110                 case SLAP_MOD_SOFTADD:
111 #ifdef NEW_LOGGING
112                         LDAP_LOG ( OPERATION, DETAIL1, 
113                                 "bdb_modify_internal: softadd\n",0,0,0 );
114 #else
115                         Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: softadd\n", 0, 0, 0);
116 #endif
117                         /* Avoid problems in index_add_mods()
118                          * We need to add index if necessary.
119                          */
120                         mod->sm_op = LDAP_MOD_ADD;
121
122                         err = modify_add_values( e, mod, text, textbuf, textlen );
123                         if ( err == LDAP_TYPE_OR_VALUE_EXISTS ) {
124                                 err = LDAP_SUCCESS;
125                         }
126
127                         if( err != LDAP_SUCCESS ) {
128 #ifdef NEW_LOGGING
129                                 LDAP_LOG ( OPERATION, ERR, 
130                                         "bdb_modify_internal: %d %s\n", err, *text, 0 );
131 #else
132                                 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
133                                         err, *text, 0);
134 #endif
135                         }
136                         break;
137
138                 default:
139 #ifdef NEW_LOGGING
140                                 LDAP_LOG ( OPERATION, ERR, 
141                                         "bdb_modify_internal: invalid op %d\n", mod->sm_op, 0, 0 );
142 #else
143                         Debug(LDAP_DEBUG_ANY, "bdb_modify_internal: invalid op %d\n",
144                                 mod->sm_op, 0, 0);
145 #endif
146                         *text = "Invalid modify operation";
147                         err = LDAP_OTHER;
148 #ifdef NEW_LOGGING
149                                 LDAP_LOG ( OPERATION, ERR, 
150                                         "bdb_modify_internal: %d %s\n", err, *text, 0 );
151 #else
152                         Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
153                                 err, *text, 0);
154 #endif
155                 }
156
157                 if ( err != LDAP_SUCCESS ) {
158                         attrs_free( e->e_attrs );
159                         e->e_attrs = save_attrs;
160                         /* unlock entry, delete from cache */
161                         return err; 
162                 }
163
164                 /* If objectClass was modified, reset the flags */
165                 if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
166                         e->e_ocflags = 0;
167                 }
168
169                 /* check if modified attribute was indexed */
170                 err = bdb_index_is_indexed( be, mod->sm_desc );
171                 if ( err == LDAP_SUCCESS ) {
172                         ap = attr_find( save_attrs, mod->sm_desc );
173                         if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
174
175                         ap = attr_find( e->e_attrs, mod->sm_desc );
176                         if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
177                 }
178         }
179
180         /* check that the entry still obeys the schema */
181         rc = entry_schema_check( be, e, save_attrs, text, textbuf, textlen );
182         if ( rc != LDAP_SUCCESS ) {
183                 attrs_free( e->e_attrs );
184                 e->e_attrs = save_attrs;
185 #ifdef NEW_LOGGING
186                                 LDAP_LOG ( OPERATION, ERR, 
187                                         "bdb_modify_internal: entry failed schema check %s\n", 
188                                         *text, 0, 0 );
189 #else
190                 Debug( LDAP_DEBUG_ANY, "entry failed schema check: %s\n",
191                         *text, 0, 0 );
192 #endif
193                 return rc;
194         }
195
196         /* update the indices of the modified attributes */
197
198         /* start with deleting the old index entries */
199         for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
200                 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
201                         rc = bdb_index_values( be, tid, ap->a_desc, ap->a_vals,
202                                                e->e_id, SLAP_INDEX_DELETE_OP );
203                         if ( rc != LDAP_SUCCESS ) {
204                                 attrs_free( e->e_attrs );
205                                 e->e_attrs = save_attrs;
206 #ifdef NEW_LOGGING
207                                 LDAP_LOG ( OPERATION, ERR, 
208                                         "bdb_modify_internal: attribute index delete failure\n",
209                                         0, 0, 0 );
210 #else
211                                 Debug( LDAP_DEBUG_ANY,
212                                        "Attribute index delete failure",
213                                        0, 0, 0 );
214 #endif
215                                 return rc;
216                         }
217                         ap->a_flags &= ~SLAP_ATTR_IXDEL;
218                 }
219         }
220
221         /* add the new index entries */
222         for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
223                 if (ap->a_flags & SLAP_ATTR_IXADD) {
224                         rc = bdb_index_values( be, tid, ap->a_desc, ap->a_vals,
225                                                e->e_id, SLAP_INDEX_ADD_OP );
226                         if ( rc != LDAP_SUCCESS ) {
227                                 attrs_free( e->e_attrs );
228                                 e->e_attrs = save_attrs;
229 #ifdef NEW_LOGGING
230                                 LDAP_LOG ( OPERATION, ERR, 
231                                         "bdb_modify_internal: attribute index add failure\n", 
232                                         0, 0, 0 );
233 #else
234                                 Debug( LDAP_DEBUG_ANY,
235                                        "Attribute index add failure",
236                                        0, 0, 0 );
237 #endif
238                                 return rc;
239                         }
240                         ap->a_flags &= ~SLAP_ATTR_IXADD;
241                 }
242         }
243
244         return rc;
245 }
246
247
248 int
249 bdb_modify(
250         BackendDB       *be,
251         Connection      *conn,
252         Operation       *op,
253         struct berval   *dn,
254         struct berval   *ndn,
255         Modifications   *modlist )
256 {
257         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
258         int rc;
259         Entry           *matched = NULL;
260         Entry           *e = NULL;
261         int             manageDSAit = get_manageDSAit( op );
262         const char *text = NULL;
263         char textbuf[SLAP_TEXT_BUFLEN];
264         size_t textlen = sizeof textbuf;
265         DB_TXN  *ltid = NULL;
266         struct bdb_op_info opinfo;
267
268         u_int32_t       locker;
269         DB_LOCK         lock;
270
271 #ifdef NEW_LOGGING
272         LDAP_LOG ( OPERATION, ENTRY, "bdb_modify: %s\n", dn->bv_val, 0, 0 );
273 #else
274         Debug( LDAP_DEBUG_ARGS, "bdb_modify: %s\n", dn->bv_val, 0, 0 );
275 #endif
276
277         if( 0 ) {
278 retry:  /* transaction retry */
279                 if( e != NULL ) {
280                         bdb_cache_delete_entry(&bdb->bi_cache, e);
281                         bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
282                 }
283 #ifdef NEW_LOGGING
284                 LDAP_LOG ( OPERATION, DETAIL1, "bdb_modify: retrying...\n", 0, 0, 0 );
285 #else
286                 Debug(LDAP_DEBUG_TRACE,
287                         "bdb_modify: retrying...\n", 0, 0, 0);
288 #endif
289                 rc = TXN_ABORT( ltid );
290                 ltid = NULL;
291                 op->o_private = NULL;
292                 if( rc != 0 ) {
293                         rc = LDAP_OTHER;
294                         text = "internal error";
295                         goto return_results;
296                 }
297                 ldap_pvt_thread_yield();
298         }
299
300         /* begin transaction */
301         rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &ltid, 
302                 bdb->bi_db_opflags );
303         text = NULL;
304         if( rc != 0 ) {
305 #ifdef NEW_LOGGING
306                 LDAP_LOG ( OPERATION, DETAIL1, 
307                         "bdb_modify: txn_begin failed: %s (%d)\n", db_strerror(rc), rc, 0 );
308 #else
309                 Debug( LDAP_DEBUG_TRACE,
310                         "bdb_modify: txn_begin failed: %s (%d)\n",
311                         db_strerror(rc), rc, 0 );
312 #endif
313                 rc = LDAP_OTHER;
314                 text = "internal error";
315                 goto return_results;
316         }
317
318         locker = TXN_ID ( ltid );
319
320         opinfo.boi_bdb = be;
321         opinfo.boi_txn = ltid;
322         opinfo.boi_err = 0;
323         op->o_private = &opinfo;
324
325         /* get entry */
326         rc = bdb_dn2entry_w( be, ltid, ndn, &e, &matched, 0, locker, &lock );
327
328         if ( rc != 0 ) {
329 #ifdef NEW_LOGGING
330                 LDAP_LOG ( OPERATION, DETAIL1, 
331                         "bdb_modify: dn2entry failed: (%d)\n", rc, 0, 0 );
332 #else
333                 Debug( LDAP_DEBUG_TRACE,
334                         "bdb_modify: dn2entry failed (%d)\n",
335                         rc, 0, 0 );
336 #endif
337                 switch( rc ) {
338                 case DB_LOCK_DEADLOCK:
339                 case DB_LOCK_NOTGRANTED:
340                         goto retry;
341                 case DB_NOTFOUND:
342                         break;
343                 case LDAP_BUSY:
344                         text = "ldap server busy";
345                         goto return_results;
346                 default:
347                         rc = LDAP_OTHER;
348                 }
349                 text = "internal error";
350                 goto return_results;
351         }
352
353         /* acquire and lock entry */
354         if ( e == NULL ) {
355                 char* matched_dn = NULL;
356                 BerVarray refs;
357
358                 if ( matched != NULL ) {
359                         matched_dn = ch_strdup( matched->e_dn );
360                         refs = is_entry_referral( matched )
361                                 ? get_entry_referrals( be, conn, op, matched )
362                                 : NULL;
363                         bdb_unlocked_cache_return_entry_r (&bdb->bi_cache, matched);
364                         matched = NULL;
365
366                 } else {
367                         refs = referral_rewrite( default_referral,
368                                 NULL, dn, LDAP_SCOPE_DEFAULT );
369                 }
370
371                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
372                         matched_dn, NULL, refs, NULL );
373
374                 ber_bvarray_free( refs );
375                 free( matched_dn );
376
377                 goto done;
378         }
379
380         if ( !manageDSAit && is_entry_referral( e ) ) {
381                 /* entry is a referral, don't allow modify */
382                 BerVarray refs = get_entry_referrals( be,
383                         conn, op, e );
384
385 #ifdef NEW_LOGGING
386                 LDAP_LOG ( OPERATION, DETAIL1, "bdb_modify: entry is referral\n", 0, 0, 0 );
387 #else
388                 Debug( LDAP_DEBUG_TRACE,
389                         "bdb_modify: entry is referral\n",
390                         0, 0, 0 );
391 #endif
392
393                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
394                         e->e_dn, NULL, refs, NULL );
395
396                 ber_bvarray_free( refs );
397                 goto done;
398         }
399         
400         /* Modify the entry */
401         rc = bdb_modify_internal( be, conn, op, ltid, modlist, e,
402                 &text, textbuf, textlen );
403
404         if( rc != LDAP_SUCCESS ) {
405 #ifdef NEW_LOGGING
406                 LDAP_LOG ( OPERATION, ERR, 
407                         "bdb_modify: modify failed (%d)\n", rc, 0, 0 );
408 #else
409                 Debug( LDAP_DEBUG_TRACE,
410                         "bdb_modify: modify failed (%d)\n",
411                         rc, 0, 0 );
412 #endif
413                 switch( rc ) {
414                 case DB_LOCK_DEADLOCK:
415                 case DB_LOCK_NOTGRANTED:
416                         goto retry;
417                 }
418                 goto return_results;
419         }
420
421         /* change the entry itself */
422         rc = bdb_id2entry_update( be, ltid, e );
423         if ( rc != 0 ) {
424 #ifdef NEW_LOGGING
425                 LDAP_LOG ( OPERATION, ERR, 
426                         "bdb_modify: id2entry update failed (%d)\n", rc, 0, 0 );
427 #else
428                 Debug( LDAP_DEBUG_TRACE,
429                         "bdb_modify: id2entry update failed (%d)\n",
430                         rc, 0, 0 );
431 #endif
432                 switch( rc ) {
433                 case DB_LOCK_DEADLOCK:
434                 case DB_LOCK_NOTGRANTED:
435                         goto retry;
436                 }
437                 text = "entry update failed";
438                 goto return_results;
439         }
440
441         if( op->o_noop ) {
442                 rc = TXN_ABORT( ltid );
443         } else {
444                 rc = TXN_COMMIT( ltid, 0 );
445         }
446         ltid = NULL;
447         op->o_private = NULL;
448
449         if( rc != 0 ) {
450 #ifdef NEW_LOGGING
451                 LDAP_LOG ( OPERATION, ERR, 
452                         "bdb_modify: txn_%s failed %s (%d)\n", 
453                         op->o_noop ? "abort (no_op)" : "commit", db_strerror(rc), rc );
454 #else
455                 Debug( LDAP_DEBUG_TRACE,
456                         "bdb_modify: txn_%s failed: %s (%d)\n",
457                         op->o_noop ? "abort (no-op)" : "commit",
458                         db_strerror(rc), rc );
459 #endif
460                 rc = LDAP_OTHER;
461                 text = "commit failed";
462
463         } else {
464 #ifdef NEW_LOGGING
465                 LDAP_LOG ( OPERATION, DETAIL1, 
466                         "bdb_modify: updated%s id=%08lx dn=\"%s\"\n", 
467                         op->o_noop ? " (no_op)" : "", e->e_id, e->e_dn );
468 #else
469                 Debug( LDAP_DEBUG_TRACE,
470                         "bdb_modify: updated%s id=%08lx dn=\"%s\"\n",
471                         op->o_noop ? " (no-op)" : "",
472                         e->e_id, e->e_dn );
473 #endif
474                 rc = LDAP_SUCCESS;
475                 text = NULL;
476         }
477
478 return_results:
479         send_ldap_result( conn, op, rc,
480                 NULL, text, NULL, NULL );
481
482         if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
483                 ldap_pvt_thread_yield();
484                 TXN_CHECKPOINT( bdb->bi_dbenv,
485                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
486         }
487
488 done:
489         if( ltid != NULL ) {
490                 TXN_ABORT( ltid );
491                 op->o_private = NULL;
492         }
493
494         if( e != NULL ) {
495                 bdb_unlocked_cache_return_entry_w (&bdb->bi_cache, e);
496         }
497         return rc;
498 }