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