]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/modify.c
ITS#5972 Added newCookie sync info messages.
[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-2009 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         int                     fakeroot = 0;
367
368         DB_LOCK         lock;
369
370         int             num_retries = 0;
371
372         LDAPControl **preread_ctrl = NULL;
373         LDAPControl **postread_ctrl = NULL;
374         LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
375         int num_ctrls = 0;
376
377         int rc;
378
379 #ifdef LDAP_X_TXN
380         int settle = 0;
381 #endif
382
383         Debug( LDAP_DEBUG_ARGS, LDAP_XSTRING(bdb_modify) ": %s\n",
384                 op->o_req_dn.bv_val, 0, 0 );
385
386 #ifdef LDAP_X_TXN
387         if( op->o_txnSpec ) {
388                 /* acquire connection lock */
389                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
390                 if( op->o_conn->c_txn == CONN_TXN_INACTIVE ) {
391                         rs->sr_text = "invalid transaction identifier";
392                         rs->sr_err = LDAP_X_TXN_ID_INVALID;
393                         goto txnReturn;
394                 } else if( op->o_conn->c_txn == CONN_TXN_SETTLE ) {
395                         settle=1;
396                         goto txnReturn;
397                 }
398
399                 if( op->o_conn->c_txn_backend == NULL ) {
400                         op->o_conn->c_txn_backend = op->o_bd;
401
402                 } else if( op->o_conn->c_txn_backend != op->o_bd ) {
403                         rs->sr_text = "transaction cannot span multiple database contexts";
404                         rs->sr_err = LDAP_AFFECTS_MULTIPLE_DSAS;
405                         goto txnReturn;
406                 }
407
408                 /* insert operation into transaction */
409
410                 rs->sr_text = "transaction specified";
411                 rs->sr_err = LDAP_X_TXN_SPECIFY_OKAY;
412
413 txnReturn:
414                 /* release connection lock */
415                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
416
417                 if( !settle ) {
418                         send_ldap_result( op, rs );
419                         return rs->sr_err;
420                 }
421         }
422 #endif
423
424         ctrls[num_ctrls] = NULL;
425
426         slap_mods_opattrs( op, &op->orm_modlist, 1 );
427
428         if( 0 ) {
429 retry:  /* transaction retry */
430                 if ( dummy.e_attrs ) {
431                         attrs_free( dummy.e_attrs );
432                         dummy.e_attrs = NULL;
433                 }
434                 if( e != NULL ) {
435                         bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
436                         e = NULL;
437                 }
438                 Debug(LDAP_DEBUG_TRACE,
439                         LDAP_XSTRING(bdb_modify) ": retrying...\n", 0, 0, 0);
440
441                 rs->sr_err = TXN_ABORT( ltid );
442                 ltid = NULL;
443                 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
444                 opinfo.boi_oe.oe_key = NULL;
445                 op->o_do_not_cache = opinfo.boi_acl_cache;
446                 if( rs->sr_err != 0 ) {
447                         rs->sr_err = LDAP_OTHER;
448                         rs->sr_text = "internal error";
449                         goto return_results;
450                 }
451                 if ( op->o_abandon ) {
452                         rs->sr_err = SLAPD_ABANDON;
453                         goto return_results;
454                 }
455                 bdb_trans_backoff( ++num_retries );
456         }
457
458         /* begin transaction */
459         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, &ltid, 
460                 bdb->bi_db_opflags );
461         rs->sr_text = NULL;
462         if( rs->sr_err != 0 ) {
463                 Debug( LDAP_DEBUG_TRACE,
464                         LDAP_XSTRING(bdb_modify) ": txn_begin failed: "
465                         "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
466                 rs->sr_err = LDAP_OTHER;
467                 rs->sr_text = "internal error";
468                 goto return_results;
469         }
470
471         opinfo.boi_oe.oe_key = bdb;
472         opinfo.boi_txn = ltid;
473         opinfo.boi_err = 0;
474         opinfo.boi_acl_cache = op->o_do_not_cache;
475         LDAP_SLIST_INSERT_HEAD( &op->o_extra, &opinfo.boi_oe, oe_next );
476
477         /* get entry or ancestor */
478         rs->sr_err = bdb_dn2entry( op, ltid, &op->o_req_ndn, &ei, 1,
479                 &lock );
480
481         if ( rs->sr_err != 0 ) {
482                 Debug( LDAP_DEBUG_TRACE,
483                         LDAP_XSTRING(bdb_modify) ": dn2entry failed (%d)\n",
484                         rs->sr_err, 0, 0 );
485                 switch( rs->sr_err ) {
486                 case DB_LOCK_DEADLOCK:
487                 case DB_LOCK_NOTGRANTED:
488                         goto retry;
489                 case DB_NOTFOUND:
490                         if ( BER_BVISEMPTY( &op->o_req_ndn )) {
491                                 struct berval gluebv = BER_BVC("glue");
492                                 e = ch_calloc( 1, sizeof(Entry));
493                                 e->e_name.bv_val = ch_strdup( "" );
494                                 ber_dupbv( &e->e_nname, &e->e_name );
495                                 attr_merge_one( e, slap_schema.si_ad_objectClass,
496                                         &gluebv, NULL );
497                                 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
498                                         &gluebv, NULL );
499                                 e->e_private = ei;
500                                 fakeroot = 1;
501                                 rs->sr_err = 0;
502                         }
503                         break;
504                 case LDAP_BUSY:
505                         rs->sr_text = "ldap server busy";
506                         goto return_results;
507                 default:
508                         rs->sr_err = LDAP_OTHER;
509                         rs->sr_text = "internal error";
510                         goto return_results;
511                 }
512         }
513
514         if ( !fakeroot ) {
515                 e = ei->bei_e;
516         }
517
518         /* acquire and lock entry */
519         /* FIXME: dn2entry() should return non-glue entry */
520         if (( rs->sr_err == DB_NOTFOUND ) ||
521                 ( !manageDSAit && e && is_entry_glue( e )))
522         {
523                 if ( e != NULL ) {
524                         rs->sr_matched = ch_strdup( e->e_dn );
525                         rs->sr_ref = is_entry_referral( e )
526                                 ? get_entry_referrals( op, e )
527                                 : NULL;
528                         bdb_unlocked_cache_return_entry_r (&bdb->bi_cache, e);
529                         e = NULL;
530
531                 } else {
532                         rs->sr_ref = referral_rewrite( default_referral, NULL,
533                                 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
534                 }
535
536                 rs->sr_err = LDAP_REFERRAL;
537                 send_ldap_result( op, rs );
538
539                 if ( rs->sr_ref != default_referral ) {
540                         ber_bvarray_free( rs->sr_ref );
541                 }
542                 free( (char *)rs->sr_matched );
543                 rs->sr_ref = NULL;
544                 rs->sr_matched = NULL;
545
546                 goto done;
547         }
548
549         if ( !manageDSAit && is_entry_referral( e ) ) {
550                 /* entry is a referral, don't allow modify */
551                 rs->sr_ref = get_entry_referrals( op, e );
552
553                 Debug( LDAP_DEBUG_TRACE,
554                         LDAP_XSTRING(bdb_modify) ": entry is referral\n",
555                         0, 0, 0 );
556
557                 rs->sr_err = LDAP_REFERRAL;
558                 rs->sr_matched = e->e_name.bv_val;
559                 send_ldap_result( op, rs );
560
561                 ber_bvarray_free( rs->sr_ref );
562                 rs->sr_ref = NULL;
563                 rs->sr_matched = NULL;
564                 goto done;
565         }
566
567         if ( get_assert( op ) &&
568                 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
569         {
570                 rs->sr_err = LDAP_ASSERTION_FAILED;
571                 goto return_results;
572         }
573
574         if( op->o_preread ) {
575                 if( preread_ctrl == NULL ) {
576                         preread_ctrl = &ctrls[num_ctrls++];
577                         ctrls[num_ctrls] = NULL;
578                 }
579                 if ( slap_read_controls( op, rs, e,
580                         &slap_pre_read_bv, preread_ctrl ) )
581                 {
582                         Debug( LDAP_DEBUG_TRACE,
583                                 "<=- " LDAP_XSTRING(bdb_modify) ": pre-read "
584                                 "failed!\n", 0, 0, 0 );
585                         if ( op->o_preread & SLAP_CONTROL_CRITICAL ) {
586                                 /* FIXME: is it correct to abort
587                                  * operation if control fails? */
588                                 goto return_results;
589                         }
590                 }
591         }
592
593         /* nested transaction */
594         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, &lt2, bdb->bi_db_opflags );
595         rs->sr_text = NULL;
596         if( rs->sr_err != 0 ) {
597                 Debug( LDAP_DEBUG_TRACE,
598                         LDAP_XSTRING(bdb_modify) ": txn_begin(2) failed: " "%s (%d)\n",
599                         db_strerror(rs->sr_err), rs->sr_err, 0 );
600                 rs->sr_err = LDAP_OTHER;
601                 rs->sr_text = "internal error";
602                 goto return_results;
603         }
604         /* Modify the entry */
605         dummy = *e;
606         rs->sr_err = bdb_modify_internal( op, lt2, op->orm_modlist,
607                 &dummy, &rs->sr_text, textbuf, textlen );
608
609         if( rs->sr_err != LDAP_SUCCESS ) {
610                 Debug( LDAP_DEBUG_TRACE,
611                         LDAP_XSTRING(bdb_modify) ": modify failed (%d)\n",
612                         rs->sr_err, 0, 0 );
613                 if ( (rs->sr_err == LDAP_INSUFFICIENT_ACCESS) && opinfo.boi_err ) {
614                         rs->sr_err = opinfo.boi_err;
615                 }
616                 /* Only free attrs if they were dup'd.  */
617                 if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
618                 switch( rs->sr_err ) {
619                 case DB_LOCK_DEADLOCK:
620                 case DB_LOCK_NOTGRANTED:
621                         goto retry;
622                 }
623                 goto return_results;
624         }
625
626         /* change the entry itself */
627         rs->sr_err = bdb_id2entry_update( op->o_bd, lt2, &dummy );
628         if ( rs->sr_err != 0 ) {
629                 Debug( LDAP_DEBUG_TRACE,
630                         LDAP_XSTRING(bdb_modify) ": id2entry update failed " "(%d)\n",
631                         rs->sr_err, 0, 0 );
632                 switch( rs->sr_err ) {
633                 case DB_LOCK_DEADLOCK:
634                 case DB_LOCK_NOTGRANTED:
635                         goto retry;
636                 }
637                 rs->sr_text = "entry update failed";
638                 goto return_results;
639         }
640
641         if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
642                 rs->sr_err = LDAP_OTHER;
643                 rs->sr_text = "txn_commit(2) failed";
644                 goto return_results;
645         }
646
647         if( op->o_postread ) {
648                 if( postread_ctrl == NULL ) {
649                         postread_ctrl = &ctrls[num_ctrls++];
650                         ctrls[num_ctrls] = NULL;
651                 }
652                 if( slap_read_controls( op, rs, &dummy,
653                         &slap_post_read_bv, postread_ctrl ) )
654                 {
655                         Debug( LDAP_DEBUG_TRACE,
656                                 "<=- " LDAP_XSTRING(bdb_modify)
657                                 ": post-read failed!\n", 0, 0, 0 );
658                         if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
659                                 /* FIXME: is it correct to abort
660                                  * operation if control fails? */
661                                 goto return_results;
662                         }
663                 }
664         }
665
666         if( op->o_noop ) {
667                 if ( ( rs->sr_err = TXN_ABORT( ltid ) ) != 0 ) {
668                         rs->sr_text = "txn_abort (no-op) failed";
669                 } else {
670                         rs->sr_err = LDAP_X_NO_OPERATION;
671                         ltid = NULL;
672                         /* Only free attrs if they were dup'd.  */
673                         if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
674                         goto return_results;
675                 }
676         } else {
677                 /* may have changed in bdb_modify_internal() */
678                 e->e_ocflags = dummy.e_ocflags;
679                 if ( fakeroot ) {
680                         e->e_private = NULL;
681                         entry_free( e );
682                         e = NULL;
683                         attrs_free( dummy.e_attrs );
684
685                 } else {
686                         rc = bdb_cache_modify( bdb, e, dummy.e_attrs, ltid, &lock );
687                         switch( rc ) {
688                         case DB_LOCK_DEADLOCK:
689                         case DB_LOCK_NOTGRANTED:
690                                 goto retry;
691                         }
692                 }
693                 dummy.e_attrs = NULL;
694
695                 rs->sr_err = TXN_COMMIT( ltid, 0 );
696         }
697         ltid = NULL;
698         LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
699         opinfo.boi_oe.oe_key = NULL;
700
701         if( rs->sr_err != 0 ) {
702                 Debug( LDAP_DEBUG_TRACE,
703                         LDAP_XSTRING(bdb_modify) ": txn_%s failed: %s (%d)\n",
704                         op->o_noop ? "abort (no-op)" : "commit",
705                         db_strerror(rs->sr_err), rs->sr_err );
706                 rs->sr_err = LDAP_OTHER;
707                 rs->sr_text = "commit failed";
708
709                 goto return_results;
710         }
711
712         Debug( LDAP_DEBUG_TRACE,
713                 LDAP_XSTRING(bdb_modify) ": updated%s id=%08lx dn=\"%s\"\n",
714                 op->o_noop ? " (no-op)" : "",
715                 dummy.e_id, op->o_req_dn.bv_val );
716
717         rs->sr_err = LDAP_SUCCESS;
718         rs->sr_text = NULL;
719         if( num_ctrls ) rs->sr_ctrls = ctrls;
720
721 return_results:
722         if( dummy.e_attrs ) {
723                 attrs_free( dummy.e_attrs );
724         }
725         send_ldap_result( op, rs );
726
727         if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp_kbyte ) {
728                 TXN_CHECKPOINT( bdb->bi_dbenv,
729                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
730         }
731
732 done:
733         slap_graduate_commit_csn( op );
734
735         if( ltid != NULL ) {
736                 TXN_ABORT( ltid );
737         }
738         if ( opinfo.boi_oe.oe_key ) {
739                 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
740         }
741
742         if( e != NULL ) {
743                 bdb_unlocked_cache_return_entry_w (&bdb->bi_cache, e);
744         }
745
746         if( preread_ctrl != NULL && (*preread_ctrl) != NULL ) {
747                 slap_sl_free( (*preread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
748                 slap_sl_free( *preread_ctrl, op->o_tmpmemctx );
749         }
750         if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
751                 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
752                 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
753         }
754
755         rs->sr_text = NULL;
756
757         return rs->sr_err;
758 }