]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/modify.c
5db7aa984770e7676323c9b22fa9ca4be44f0a95
[openldap] / servers / slapd / back-bdb / modify.c
1 /* modify.c - bdb backend modify routine */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2010 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21 #include <ac/time.h>
22
23 #include "back-bdb.h"
24
25 static struct berval scbva[] = {
26         BER_BVC("glue"),
27         BER_BVNULL
28 };
29
30 static void
31 bdb_modify_idxflags(
32         Operation *op,
33         AttributeDescription *desc,
34         int got_delete,
35         Attribute *newattrs,
36         Attribute *oldattrs )
37 {
38         struct berval   ix_at;
39         AttrInfo        *ai;
40
41         /* check if modified attribute was indexed
42          * but not in case of NOOP... */
43         ai = bdb_index_mask( op->o_bd, desc, &ix_at );
44         if ( ai ) {
45                 if ( got_delete ) {
46                         Attribute       *ap;
47                         struct berval   ix2;
48
49                         ap = attr_find( oldattrs, desc );
50                         if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
51
52                         /* Find all other attrs that index to same slot */
53                         for ( ap = newattrs; ap; ap = ap->a_next ) {
54                                 ai = bdb_index_mask( op->o_bd, ap->a_desc, &ix2 );
55                                 if ( ai && ix2.bv_val == ix_at.bv_val )
56                                         ap->a_flags |= SLAP_ATTR_IXADD;
57                         }
58
59                 } else {
60                         Attribute       *ap;
61
62                         ap = attr_find( newattrs, desc );
63                         if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
64                 }
65         }
66 }
67
68 int bdb_modify_internal(
69         Operation *op,
70         DB_TXN *tid,
71         Modifications *modlist,
72         Entry *e,
73         const char **text,
74         char *textbuf,
75         size_t textlen )
76 {
77         int rc, err;
78         Modification    *mod;
79         Modifications   *ml;
80         Attribute       *save_attrs;
81         Attribute       *ap;
82         int                     glue_attr_delete = 0;
83         int                     got_delete;
84
85         Debug( LDAP_DEBUG_TRACE, "bdb_modify_internal: 0x%08lx: %s\n",
86                 e->e_id, e->e_dn, 0);
87
88         if ( !acl_check_modlist( op, e, modlist )) {
89                 return LDAP_INSUFFICIENT_ACCESS;
90         }
91
92         /* save_attrs will be disposed of by bdb_cache_modify */
93         save_attrs = e->e_attrs;
94         e->e_attrs = attrs_dup( e->e_attrs );
95
96         for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
97                 int match;
98                 mod = &ml->sml_mod;
99                 switch( mod->sm_op ) {
100                 case LDAP_MOD_ADD:
101                 case LDAP_MOD_REPLACE:
102                         if ( mod->sm_desc == slap_schema.si_ad_structuralObjectClass ) {
103                                 value_match( &match, slap_schema.si_ad_structuralObjectClass,
104                                         slap_schema.si_ad_structuralObjectClass->
105                                                 ad_type->sat_equality,
106                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
107                                         &mod->sm_values[0], &scbva[0], text );
108                                 if ( !match ) glue_attr_delete = 1;
109                         }
110                 }
111                 if ( glue_attr_delete )
112                         break;
113         }
114
115         if ( glue_attr_delete ) {
116                 Attribute       **app = &e->e_attrs;
117                 while ( *app != NULL ) {
118                         if ( !is_at_operational( (*app)->a_desc->ad_type )) {
119                                 Attribute *save = *app;
120                                 *app = (*app)->a_next;
121                                 attr_free( save );
122                                 continue;
123                         }
124                         app = &(*app)->a_next;
125                 }
126         }
127
128         for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
129                 mod = &ml->sml_mod;
130                 got_delete = 0;
131
132                 switch ( mod->sm_op ) {
133                 case LDAP_MOD_ADD:
134                         Debug(LDAP_DEBUG_ARGS,
135                                 "bdb_modify_internal: add %s\n",
136                                 mod->sm_desc->ad_cname.bv_val, 0, 0);
137                         err = modify_add_values( e, mod, get_permissiveModify(op),
138                                 text, textbuf, textlen );
139                         if( err != LDAP_SUCCESS ) {
140                                 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
141                                         err, *text, 0);
142                         }
143                         break;
144
145                 case LDAP_MOD_DELETE:
146                         if ( glue_attr_delete ) {
147                                 err = LDAP_SUCCESS;
148                                 break;
149                         }
150
151                         Debug(LDAP_DEBUG_ARGS,
152                                 "bdb_modify_internal: delete %s\n",
153                                 mod->sm_desc->ad_cname.bv_val, 0, 0);
154                         err = modify_delete_values( e, mod, get_permissiveModify(op),
155                                 text, textbuf, textlen );
156                         assert( err != LDAP_TYPE_OR_VALUE_EXISTS );
157                         if( err != LDAP_SUCCESS ) {
158                                 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
159                                         err, *text, 0);
160                         } else {
161                                 got_delete = 1;
162                         }
163                         break;
164
165                 case LDAP_MOD_REPLACE:
166                         Debug(LDAP_DEBUG_ARGS,
167                                 "bdb_modify_internal: replace %s\n",
168                                 mod->sm_desc->ad_cname.bv_val, 0, 0);
169                         err = modify_replace_values( e, mod, get_permissiveModify(op),
170                                 text, textbuf, textlen );
171                         if( err != LDAP_SUCCESS ) {
172                                 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
173                                         err, *text, 0);
174                         } else {
175                                 got_delete = 1;
176                         }
177                         break;
178
179                 case LDAP_MOD_INCREMENT:
180                         Debug(LDAP_DEBUG_ARGS,
181                                 "bdb_modify_internal: increment %s\n",
182                                 mod->sm_desc->ad_cname.bv_val, 0, 0);
183                         err = modify_increment_values( e, mod, get_permissiveModify(op),
184                                 text, textbuf, textlen );
185                         if( err != LDAP_SUCCESS ) {
186                                 Debug(LDAP_DEBUG_ARGS,
187                                         "bdb_modify_internal: %d %s\n",
188                                         err, *text, 0);
189                         } else {
190                                 got_delete = 1;
191                         }
192                         break;
193
194                 case SLAP_MOD_SOFTADD:
195                         Debug(LDAP_DEBUG_ARGS,
196                                 "bdb_modify_internal: softadd %s\n",
197                                 mod->sm_desc->ad_cname.bv_val, 0, 0);
198                         /* Avoid problems in index_add_mods()
199                          * We need to add index if necessary.
200                          */
201                         mod->sm_op = LDAP_MOD_ADD;
202
203                         err = modify_add_values( e, mod, get_permissiveModify(op),
204                                 text, textbuf, textlen );
205
206                         mod->sm_op = SLAP_MOD_SOFTADD;
207
208                         if ( err == LDAP_TYPE_OR_VALUE_EXISTS ) {
209                                 err = LDAP_SUCCESS;
210                         }
211
212                         if( err != LDAP_SUCCESS ) {
213                                 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
214                                         err, *text, 0);
215                         }
216                         break;
217
218                 default:
219                         Debug(LDAP_DEBUG_ANY, "bdb_modify_internal: invalid op %d\n",
220                                 mod->sm_op, 0, 0);
221                         *text = "Invalid modify operation";
222                         err = LDAP_OTHER;
223                         Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
224                                 err, *text, 0);
225                 }
226
227                 if ( err != LDAP_SUCCESS ) {
228                         attrs_free( e->e_attrs );
229                         e->e_attrs = save_attrs;
230                         /* unlock entry, delete from cache */
231                         return err; 
232                 }
233
234                 /* If objectClass was modified, reset the flags */
235                 if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
236                         e->e_ocflags = 0;
237                 }
238
239                 if ( glue_attr_delete ) e->e_ocflags = 0;
240
241
242                 /* check if modified attribute was indexed
243                  * but not in case of NOOP... */
244                 if ( !op->o_noop ) {
245                         bdb_modify_idxflags( op, mod->sm_desc, got_delete, e->e_attrs, save_attrs );
246                 }
247         }
248
249         /* check that the entry still obeys the schema */
250         ap = NULL;
251         rc = entry_schema_check( op, e, save_attrs, get_relax(op), 0, &ap,
252                 text, textbuf, textlen );
253         if ( rc != LDAP_SUCCESS || op->o_noop ) {
254                 attrs_free( e->e_attrs );
255                 /* clear the indexing flags */
256                 for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
257                         ap->a_flags &= ~(SLAP_ATTR_IXADD|SLAP_ATTR_IXDEL);
258                 }
259                 e->e_attrs = save_attrs;
260
261                 if ( rc != LDAP_SUCCESS ) {
262                         Debug( LDAP_DEBUG_ANY,
263                                 "entry failed schema check: %s\n",
264                                 *text, 0, 0 );
265                 }
266
267                 /* if NOOP then silently revert to saved attrs */
268                 return rc;
269         }
270
271         /* structuralObjectClass modified! */
272         if ( ap ) {
273                 assert( ap->a_desc == slap_schema.si_ad_structuralObjectClass );
274                 if ( !op->o_noop ) {
275                         bdb_modify_idxflags( op, slap_schema.si_ad_structuralObjectClass,
276                                 1, e->e_attrs, save_attrs );
277                 }
278         }
279
280         /* update the indices of the modified attributes */
281
282         /* start with deleting the old index entries */
283         for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
284                 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
285                         struct berval *vals;
286                         Attribute *a2;
287                         ap->a_flags &= ~SLAP_ATTR_IXDEL;
288                         a2 = attr_find( e->e_attrs, ap->a_desc );
289                         if ( a2 ) {
290                                 /* need to detect which values were deleted */
291                                 int i, j;
292                                 struct berval tmp;
293                                 j = ap->a_numvals;
294                                 for ( i=0; i<j; ) {
295                                         rc = attr_valfind( a2, SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
296                                                 &ap->a_nvals[i], NULL, op->o_tmpmemctx );
297                                         /* Move deleted values to end of array */
298                                         if ( rc == LDAP_NO_SUCH_ATTRIBUTE ) {
299                                                 j--;
300                                                 if ( i != j ) {
301                                                         tmp = ap->a_nvals[j];
302                                                         ap->a_nvals[j] = ap->a_nvals[i];
303                                                         ap->a_nvals[i] = tmp;
304                                                         tmp = ap->a_vals[j];
305                                                         ap->a_vals[j] = ap->a_vals[i];
306                                                         ap->a_vals[i] = tmp;
307                                                 }
308                                                 continue;
309                                         }
310                                         i++;
311                                 }
312                                 vals = &ap->a_nvals[j];
313                         } else {
314                                 /* attribute was completely deleted */
315                                 vals = ap->a_nvals;
316                         }
317                         if ( !BER_BVISNULL( vals )) {
318                                 rc = bdb_index_values( op, tid, ap->a_desc,
319                                         vals, e->e_id, SLAP_INDEX_DELETE_OP );
320                                 if ( rc != LDAP_SUCCESS ) {
321                                         Debug( LDAP_DEBUG_ANY,
322                                                 "%s: attribute \"%s\" index delete failure\n",
323                                                 op->o_log_prefix, ap->a_desc->ad_cname.bv_val, 0 );
324                                         attrs_free( e->e_attrs );
325                                         e->e_attrs = save_attrs;
326                                         return rc;
327                                 }
328                         }
329                 }
330         }
331
332         /* add the new index entries */
333         for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
334                 if (ap->a_flags & SLAP_ATTR_IXADD) {
335                         ap->a_flags &= ~SLAP_ATTR_IXADD;
336                         rc = bdb_index_values( op, tid, ap->a_desc,
337                                 ap->a_nvals,
338                                 e->e_id, SLAP_INDEX_ADD_OP );
339                         if ( rc != LDAP_SUCCESS ) {
340                                 Debug( LDAP_DEBUG_ANY,
341                                        "%s: attribute \"%s\" index add failure\n",
342                                         op->o_log_prefix, ap->a_desc->ad_cname.bv_val, 0 );
343                                 attrs_free( e->e_attrs );
344                                 e->e_attrs = save_attrs;
345                                 return rc;
346                         }
347                 }
348         }
349
350         return rc;
351 }
352
353
354 int
355 bdb_modify( Operation *op, SlapReply *rs )
356 {
357         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
358         Entry           *e = NULL;
359         EntryInfo       *ei = NULL;
360         int             manageDSAit = get_manageDSAit( op );
361         char textbuf[SLAP_TEXT_BUFLEN];
362         size_t textlen = sizeof textbuf;
363         DB_TXN  *ltid = NULL, *lt2;
364         struct bdb_op_info opinfo = {{{ 0 }}};
365         Entry           dummy = {0};
366
367         DB_LOCK         lock;
368
369         int             num_retries = 0;
370
371         LDAPControl **preread_ctrl = NULL;
372         LDAPControl **postread_ctrl = NULL;
373         LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
374         int num_ctrls = 0;
375
376         int rc;
377
378 #ifdef LDAP_X_TXN
379         int settle = 0;
380 #endif
381
382         Debug( LDAP_DEBUG_ARGS, LDAP_XSTRING(bdb_modify) ": %s\n",
383                 op->o_req_dn.bv_val, 0, 0 );
384
385 #ifdef LDAP_X_TXN
386         if( op->o_txnSpec ) {
387                 /* acquire connection lock */
388                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
389                 if( op->o_conn->c_txn == CONN_TXN_INACTIVE ) {
390                         rs->sr_text = "invalid transaction identifier";
391                         rs->sr_err = LDAP_X_TXN_ID_INVALID;
392                         goto txnReturn;
393                 } else if( op->o_conn->c_txn == CONN_TXN_SETTLE ) {
394                         settle=1;
395                         goto txnReturn;
396                 }
397
398                 if( op->o_conn->c_txn_backend == NULL ) {
399                         op->o_conn->c_txn_backend = op->o_bd;
400
401                 } else if( op->o_conn->c_txn_backend != op->o_bd ) {
402                         rs->sr_text = "transaction cannot span multiple database contexts";
403                         rs->sr_err = LDAP_AFFECTS_MULTIPLE_DSAS;
404                         goto txnReturn;
405                 }
406
407                 /* insert operation into transaction */
408
409                 rs->sr_text = "transaction specified";
410                 rs->sr_err = LDAP_X_TXN_SPECIFY_OKAY;
411
412 txnReturn:
413                 /* release connection lock */
414                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
415
416                 if( !settle ) {
417                         send_ldap_result( op, rs );
418                         return rs->sr_err;
419                 }
420         }
421 #endif
422
423         ctrls[num_ctrls] = NULL;
424
425         /* Don't touch the opattrs, if this is a contextCSN update
426          * initiated from updatedn */
427         if ( !be_isupdate(op) || !op->orm_modlist || op->orm_modlist->sml_next ||
428                  op->orm_modlist->sml_desc != slap_schema.si_ad_contextCSN ) {
429
430                 slap_mods_opattrs( op, &op->orm_modlist, 1 );
431         }
432
433         if( 0 ) {
434 retry:  /* transaction retry */
435                 if ( dummy.e_attrs ) {
436                         attrs_free( dummy.e_attrs );
437                         dummy.e_attrs = NULL;
438                 }
439                 if( e != NULL ) {
440                         bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
441                         e = NULL;
442                 }
443                 Debug(LDAP_DEBUG_TRACE,
444                         LDAP_XSTRING(bdb_modify) ": retrying...\n", 0, 0, 0);
445
446                 rs->sr_err = TXN_ABORT( ltid );
447                 ltid = NULL;
448                 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
449                 opinfo.boi_oe.oe_key = NULL;
450                 op->o_do_not_cache = opinfo.boi_acl_cache;
451                 if( rs->sr_err != 0 ) {
452                         rs->sr_err = LDAP_OTHER;
453                         rs->sr_text = "internal error";
454                         goto return_results;
455                 }
456                 if ( op->o_abandon ) {
457                         rs->sr_err = SLAPD_ABANDON;
458                         goto return_results;
459                 }
460                 bdb_trans_backoff( ++num_retries );
461         }
462
463         /* begin transaction */
464         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, &ltid, 
465                 bdb->bi_db_opflags );
466         rs->sr_text = NULL;
467         if( rs->sr_err != 0 ) {
468                 Debug( LDAP_DEBUG_TRACE,
469                         LDAP_XSTRING(bdb_modify) ": txn_begin failed: "
470                         "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
471                 rs->sr_err = LDAP_OTHER;
472                 rs->sr_text = "internal error";
473                 goto return_results;
474         }
475
476         opinfo.boi_oe.oe_key = bdb;
477         opinfo.boi_txn = ltid;
478         opinfo.boi_err = 0;
479         opinfo.boi_acl_cache = op->o_do_not_cache;
480         LDAP_SLIST_INSERT_HEAD( &op->o_extra, &opinfo.boi_oe, oe_next );
481
482         /* get entry or ancestor */
483         rs->sr_err = bdb_dn2entry( op, ltid, &op->o_req_ndn, &ei, 1,
484                 &lock );
485
486         if ( rs->sr_err != 0 ) {
487                 Debug( LDAP_DEBUG_TRACE,
488                         LDAP_XSTRING(bdb_modify) ": dn2entry failed (%d)\n",
489                         rs->sr_err, 0, 0 );
490                 switch( rs->sr_err ) {
491                 case DB_LOCK_DEADLOCK:
492                 case DB_LOCK_NOTGRANTED:
493                         goto retry;
494                 case DB_NOTFOUND:
495                         break;
496                 case LDAP_BUSY:
497                         rs->sr_text = "ldap server busy";
498                         goto return_results;
499                 default:
500                         rs->sr_err = LDAP_OTHER;
501                         rs->sr_text = "internal error";
502                         goto return_results;
503                 }
504         }
505
506         e = ei->bei_e;
507
508         /* acquire and lock entry */
509         /* FIXME: dn2entry() should return non-glue entry */
510         if (( rs->sr_err == DB_NOTFOUND ) ||
511                 ( !manageDSAit && e && is_entry_glue( e )))
512         {
513                 if ( e != NULL ) {
514                         rs->sr_matched = ch_strdup( e->e_dn );
515                         rs->sr_ref = is_entry_referral( e )
516                                 ? get_entry_referrals( op, e )
517                                 : NULL;
518                         bdb_unlocked_cache_return_entry_r (&bdb->bi_cache, e);
519                         e = NULL;
520
521                 } else {
522                         rs->sr_ref = referral_rewrite( default_referral, NULL,
523                                 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
524                 }
525
526                 rs->sr_err = LDAP_REFERRAL;
527                 send_ldap_result( op, rs );
528
529                 if ( rs->sr_ref != default_referral ) {
530                         ber_bvarray_free( rs->sr_ref );
531                 }
532                 free( (char *)rs->sr_matched );
533                 rs->sr_ref = NULL;
534                 rs->sr_matched = NULL;
535
536                 goto done;
537         }
538
539         if ( !manageDSAit && is_entry_referral( e ) ) {
540                 /* entry is a referral, don't allow modify */
541                 rs->sr_ref = get_entry_referrals( op, e );
542
543                 Debug( LDAP_DEBUG_TRACE,
544                         LDAP_XSTRING(bdb_modify) ": entry is referral\n",
545                         0, 0, 0 );
546
547                 rs->sr_err = LDAP_REFERRAL;
548                 rs->sr_matched = e->e_name.bv_val;
549                 send_ldap_result( op, rs );
550
551                 ber_bvarray_free( rs->sr_ref );
552                 rs->sr_ref = NULL;
553                 rs->sr_matched = NULL;
554                 goto done;
555         }
556
557         if ( get_assert( op ) &&
558                 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
559         {
560                 rs->sr_err = LDAP_ASSERTION_FAILED;
561                 goto return_results;
562         }
563
564         if( op->o_preread ) {
565                 if( preread_ctrl == NULL ) {
566                         preread_ctrl = &ctrls[num_ctrls++];
567                         ctrls[num_ctrls] = NULL;
568                 }
569                 if ( slap_read_controls( op, rs, e,
570                         &slap_pre_read_bv, preread_ctrl ) )
571                 {
572                         Debug( LDAP_DEBUG_TRACE,
573                                 "<=- " LDAP_XSTRING(bdb_modify) ": pre-read "
574                                 "failed!\n", 0, 0, 0 );
575                         if ( op->o_preread & SLAP_CONTROL_CRITICAL ) {
576                                 /* FIXME: is it correct to abort
577                                  * operation if control fails? */
578                                 goto return_results;
579                         }
580                 }
581         }
582
583         /* nested transaction */
584         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, &lt2, bdb->bi_db_opflags );
585         rs->sr_text = NULL;
586         if( rs->sr_err != 0 ) {
587                 Debug( LDAP_DEBUG_TRACE,
588                         LDAP_XSTRING(bdb_modify) ": txn_begin(2) failed: " "%s (%d)\n",
589                         db_strerror(rs->sr_err), rs->sr_err, 0 );
590                 rs->sr_err = LDAP_OTHER;
591                 rs->sr_text = "internal error";
592                 goto return_results;
593         }
594         /* Modify the entry */
595         dummy = *e;
596         rs->sr_err = bdb_modify_internal( op, lt2, op->orm_modlist,
597                 &dummy, &rs->sr_text, textbuf, textlen );
598
599         if( rs->sr_err != LDAP_SUCCESS ) {
600                 Debug( LDAP_DEBUG_TRACE,
601                         LDAP_XSTRING(bdb_modify) ": modify failed (%d)\n",
602                         rs->sr_err, 0, 0 );
603                 if ( (rs->sr_err == LDAP_INSUFFICIENT_ACCESS) && opinfo.boi_err ) {
604                         rs->sr_err = opinfo.boi_err;
605                 }
606                 /* Only free attrs if they were dup'd.  */
607                 if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
608                 switch( rs->sr_err ) {
609                 case DB_LOCK_DEADLOCK:
610                 case DB_LOCK_NOTGRANTED:
611                         goto retry;
612                 }
613                 goto return_results;
614         }
615
616         /* change the entry itself */
617         rs->sr_err = bdb_id2entry_update( op->o_bd, lt2, &dummy );
618         if ( rs->sr_err != 0 ) {
619                 Debug( LDAP_DEBUG_TRACE,
620                         LDAP_XSTRING(bdb_modify) ": id2entry update failed " "(%d)\n",
621                         rs->sr_err, 0, 0 );
622                 switch( rs->sr_err ) {
623                 case DB_LOCK_DEADLOCK:
624                 case DB_LOCK_NOTGRANTED:
625                         goto retry;
626                 }
627                 rs->sr_text = "entry update failed";
628                 goto return_results;
629         }
630
631         if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
632                 rs->sr_err = LDAP_OTHER;
633                 rs->sr_text = "txn_commit(2) failed";
634                 goto return_results;
635         }
636
637         if( op->o_postread ) {
638                 if( postread_ctrl == NULL ) {
639                         postread_ctrl = &ctrls[num_ctrls++];
640                         ctrls[num_ctrls] = NULL;
641                 }
642                 if( slap_read_controls( op, rs, &dummy,
643                         &slap_post_read_bv, postread_ctrl ) )
644                 {
645                         Debug( LDAP_DEBUG_TRACE,
646                                 "<=- " LDAP_XSTRING(bdb_modify)
647                                 ": post-read failed!\n", 0, 0, 0 );
648                         if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
649                                 /* FIXME: is it correct to abort
650                                  * operation if control fails? */
651                                 goto return_results;
652                         }
653                 }
654         }
655
656         if( op->o_noop ) {
657                 if ( ( rs->sr_err = TXN_ABORT( ltid ) ) != 0 ) {
658                         rs->sr_text = "txn_abort (no-op) failed";
659                 } else {
660                         rs->sr_err = LDAP_X_NO_OPERATION;
661                         ltid = NULL;
662                         /* Only free attrs if they were dup'd.  */
663                         if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
664                         goto return_results;
665                 }
666         } else {
667                 /* may have changed in bdb_modify_internal() */
668                 e->e_ocflags = dummy.e_ocflags;
669                 rc = bdb_cache_modify( bdb, e, dummy.e_attrs, ltid, &lock );
670                 switch( rc ) {
671                 case DB_LOCK_DEADLOCK:
672                 case DB_LOCK_NOTGRANTED:
673                         goto retry;
674                 }
675                 dummy.e_attrs = NULL;
676
677                 rs->sr_err = TXN_COMMIT( ltid, 0 );
678         }
679         ltid = NULL;
680         LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
681         opinfo.boi_oe.oe_key = NULL;
682
683         if( rs->sr_err != 0 ) {
684                 Debug( LDAP_DEBUG_TRACE,
685                         LDAP_XSTRING(bdb_modify) ": txn_%s failed: %s (%d)\n",
686                         op->o_noop ? "abort (no-op)" : "commit",
687                         db_strerror(rs->sr_err), rs->sr_err );
688                 rs->sr_err = LDAP_OTHER;
689                 rs->sr_text = "commit failed";
690
691                 goto return_results;
692         }
693
694         Debug( LDAP_DEBUG_TRACE,
695                 LDAP_XSTRING(bdb_modify) ": updated%s id=%08lx dn=\"%s\"\n",
696                 op->o_noop ? " (no-op)" : "",
697                 dummy.e_id, op->o_req_dn.bv_val );
698
699         rs->sr_err = LDAP_SUCCESS;
700         rs->sr_text = NULL;
701         if( num_ctrls ) rs->sr_ctrls = ctrls;
702
703 return_results:
704         if( dummy.e_attrs ) {
705                 attrs_free( dummy.e_attrs );
706         }
707         send_ldap_result( op, rs );
708
709         if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp_kbyte ) {
710                 TXN_CHECKPOINT( bdb->bi_dbenv,
711                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
712         }
713
714 done:
715         slap_graduate_commit_csn( op );
716
717         if( ltid != NULL ) {
718                 TXN_ABORT( ltid );
719         }
720         if ( opinfo.boi_oe.oe_key ) {
721                 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
722         }
723
724         if( e != NULL ) {
725                 bdb_unlocked_cache_return_entry_w (&bdb->bi_cache, e);
726         }
727
728         if( preread_ctrl != NULL && (*preread_ctrl) != NULL ) {
729                 slap_sl_free( (*preread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
730                 slap_sl_free( *preread_ctrl, op->o_tmpmemctx );
731         }
732         if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
733                 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
734                 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
735         }
736
737         rs->sr_text = NULL;
738
739         return rs->sr_err;
740 }