]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/modify.c
Cleanup
[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
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         slap_mods_opattrs( op, &op->orm_modlist, 1 );
426
427         if( 0 ) {
428 retry:  /* transaction retry */
429                 if ( dummy.e_attrs ) {
430                         attrs_free( dummy.e_attrs );
431                         dummy.e_attrs = NULL;
432                 }
433                 if( e != NULL ) {
434                         bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
435                         e = NULL;
436                 }
437                 Debug(LDAP_DEBUG_TRACE,
438                         LDAP_XSTRING(bdb_modify) ": retrying...\n", 0, 0, 0);
439
440                 rs->sr_err = TXN_ABORT( ltid );
441                 ltid = NULL;
442                 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
443                 opinfo.boi_oe.oe_key = NULL;
444                 op->o_do_not_cache = opinfo.boi_acl_cache;
445                 if( rs->sr_err != 0 ) {
446                         rs->sr_err = LDAP_OTHER;
447                         rs->sr_text = "internal error";
448                         goto return_results;
449                 }
450                 if ( op->o_abandon ) {
451                         rs->sr_err = SLAPD_ABANDON;
452                         goto return_results;
453                 }
454                 bdb_trans_backoff( ++num_retries );
455         }
456
457         /* begin transaction */
458         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, &ltid, 
459                 bdb->bi_db_opflags );
460         rs->sr_text = NULL;
461         if( rs->sr_err != 0 ) {
462                 Debug( LDAP_DEBUG_TRACE,
463                         LDAP_XSTRING(bdb_modify) ": txn_begin failed: "
464                         "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
465                 rs->sr_err = LDAP_OTHER;
466                 rs->sr_text = "internal error";
467                 goto return_results;
468         }
469
470         opinfo.boi_oe.oe_key = bdb;
471         opinfo.boi_txn = ltid;
472         opinfo.boi_err = 0;
473         opinfo.boi_acl_cache = op->o_do_not_cache;
474         LDAP_SLIST_INSERT_HEAD( &op->o_extra, &opinfo.boi_oe, oe_next );
475
476         /* get entry or ancestor */
477         rs->sr_err = bdb_dn2entry( op, ltid, &op->o_req_ndn, &ei, 1,
478                 &lock );
479
480         if ( rs->sr_err != 0 ) {
481                 Debug( LDAP_DEBUG_TRACE,
482                         LDAP_XSTRING(bdb_modify) ": dn2entry failed (%d)\n",
483                         rs->sr_err, 0, 0 );
484                 switch( rs->sr_err ) {
485                 case DB_LOCK_DEADLOCK:
486                 case DB_LOCK_NOTGRANTED:
487                         goto retry;
488                 case DB_NOTFOUND:
489                         break;
490                 case LDAP_BUSY:
491                         rs->sr_text = "ldap server busy";
492                         goto return_results;
493                 default:
494                         rs->sr_err = LDAP_OTHER;
495                         rs->sr_text = "internal error";
496                         goto return_results;
497                 }
498         }
499
500         e = ei->bei_e;
501
502         /* acquire and lock entry */
503         /* FIXME: dn2entry() should return non-glue entry */
504         if (( rs->sr_err == DB_NOTFOUND ) ||
505                 ( !manageDSAit && e && is_entry_glue( e )))
506         {
507                 if ( e != NULL ) {
508                         rs->sr_matched = ch_strdup( e->e_dn );
509                         rs->sr_ref = is_entry_referral( e )
510                                 ? get_entry_referrals( op, e )
511                                 : NULL;
512                         bdb_unlocked_cache_return_entry_r (&bdb->bi_cache, e);
513                         e = NULL;
514
515                 } else {
516                         rs->sr_ref = referral_rewrite( default_referral, NULL,
517                                 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
518                 }
519
520                 rs->sr_err = LDAP_REFERRAL;
521                 send_ldap_result( op, rs );
522
523                 if ( rs->sr_ref != default_referral ) {
524                         ber_bvarray_free( rs->sr_ref );
525                 }
526                 free( (char *)rs->sr_matched );
527                 rs->sr_ref = NULL;
528                 rs->sr_matched = NULL;
529
530                 goto done;
531         }
532
533         if ( !manageDSAit && is_entry_referral( e ) ) {
534                 /* entry is a referral, don't allow modify */
535                 rs->sr_ref = get_entry_referrals( op, e );
536
537                 Debug( LDAP_DEBUG_TRACE,
538                         LDAP_XSTRING(bdb_modify) ": entry is referral\n",
539                         0, 0, 0 );
540
541                 rs->sr_err = LDAP_REFERRAL;
542                 rs->sr_matched = e->e_name.bv_val;
543                 send_ldap_result( op, rs );
544
545                 ber_bvarray_free( rs->sr_ref );
546                 rs->sr_ref = NULL;
547                 rs->sr_matched = NULL;
548                 goto done;
549         }
550
551         if ( get_assert( op ) &&
552                 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
553         {
554                 rs->sr_err = LDAP_ASSERTION_FAILED;
555                 goto return_results;
556         }
557
558         if( op->o_preread ) {
559                 if( preread_ctrl == NULL ) {
560                         preread_ctrl = &ctrls[num_ctrls++];
561                         ctrls[num_ctrls] = NULL;
562                 }
563                 if ( slap_read_controls( op, rs, e,
564                         &slap_pre_read_bv, preread_ctrl ) )
565                 {
566                         Debug( LDAP_DEBUG_TRACE,
567                                 "<=- " LDAP_XSTRING(bdb_modify) ": pre-read "
568                                 "failed!\n", 0, 0, 0 );
569                         if ( op->o_preread & SLAP_CONTROL_CRITICAL ) {
570                                 /* FIXME: is it correct to abort
571                                  * operation if control fails? */
572                                 goto return_results;
573                         }
574                 }
575         }
576
577         /* nested transaction */
578         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, &lt2, bdb->bi_db_opflags );
579         rs->sr_text = NULL;
580         if( rs->sr_err != 0 ) {
581                 Debug( LDAP_DEBUG_TRACE,
582                         LDAP_XSTRING(bdb_modify) ": txn_begin(2) failed: " "%s (%d)\n",
583                         db_strerror(rs->sr_err), rs->sr_err, 0 );
584                 rs->sr_err = LDAP_OTHER;
585                 rs->sr_text = "internal error";
586                 goto return_results;
587         }
588         /* Modify the entry */
589         dummy = *e;
590         rs->sr_err = bdb_modify_internal( op, lt2, op->orm_modlist,
591                 &dummy, &rs->sr_text, textbuf, textlen );
592
593         if( rs->sr_err != LDAP_SUCCESS ) {
594                 Debug( LDAP_DEBUG_TRACE,
595                         LDAP_XSTRING(bdb_modify) ": modify failed (%d)\n",
596                         rs->sr_err, 0, 0 );
597                 if ( (rs->sr_err == LDAP_INSUFFICIENT_ACCESS) && opinfo.boi_err ) {
598                         rs->sr_err = opinfo.boi_err;
599                 }
600                 /* Only free attrs if they were dup'd.  */
601                 if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
602                 switch( rs->sr_err ) {
603                 case DB_LOCK_DEADLOCK:
604                 case DB_LOCK_NOTGRANTED:
605                         goto retry;
606                 }
607                 goto return_results;
608         }
609
610         /* change the entry itself */
611         rs->sr_err = bdb_id2entry_update( op->o_bd, lt2, &dummy );
612         if ( rs->sr_err != 0 ) {
613                 Debug( LDAP_DEBUG_TRACE,
614                         LDAP_XSTRING(bdb_modify) ": id2entry update failed " "(%d)\n",
615                         rs->sr_err, 0, 0 );
616                 switch( rs->sr_err ) {
617                 case DB_LOCK_DEADLOCK:
618                 case DB_LOCK_NOTGRANTED:
619                         goto retry;
620                 }
621                 rs->sr_text = "entry update failed";
622                 goto return_results;
623         }
624
625         if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
626                 rs->sr_err = LDAP_OTHER;
627                 rs->sr_text = "txn_commit(2) failed";
628                 goto return_results;
629         }
630
631         if( op->o_postread ) {
632                 if( postread_ctrl == NULL ) {
633                         postread_ctrl = &ctrls[num_ctrls++];
634                         ctrls[num_ctrls] = NULL;
635                 }
636                 if( slap_read_controls( op, rs, &dummy,
637                         &slap_post_read_bv, postread_ctrl ) )
638                 {
639                         Debug( LDAP_DEBUG_TRACE,
640                                 "<=- " LDAP_XSTRING(bdb_modify)
641                                 ": post-read failed!\n", 0, 0, 0 );
642                         if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
643                                 /* FIXME: is it correct to abort
644                                  * operation if control fails? */
645                                 goto return_results;
646                         }
647                 }
648         }
649
650         if( op->o_noop ) {
651                 if ( ( rs->sr_err = TXN_ABORT( ltid ) ) != 0 ) {
652                         rs->sr_text = "txn_abort (no-op) failed";
653                 } else {
654                         rs->sr_err = LDAP_X_NO_OPERATION;
655                         ltid = NULL;
656                         /* Only free attrs if they were dup'd.  */
657                         if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
658                         goto return_results;
659                 }
660         } else {
661                 /* may have changed in bdb_modify_internal() */
662                 e->e_ocflags = dummy.e_ocflags;
663                 rc = bdb_cache_modify( bdb, e, dummy.e_attrs, ltid, &lock );
664                 switch( rc ) {
665                 case DB_LOCK_DEADLOCK:
666                 case DB_LOCK_NOTGRANTED:
667                         goto retry;
668                 }
669                 dummy.e_attrs = NULL;
670
671                 rs->sr_err = TXN_COMMIT( ltid, 0 );
672         }
673         ltid = NULL;
674         LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
675         opinfo.boi_oe.oe_key = NULL;
676
677         if( rs->sr_err != 0 ) {
678                 Debug( LDAP_DEBUG_TRACE,
679                         LDAP_XSTRING(bdb_modify) ": txn_%s failed: %s (%d)\n",
680                         op->o_noop ? "abort (no-op)" : "commit",
681                         db_strerror(rs->sr_err), rs->sr_err );
682                 rs->sr_err = LDAP_OTHER;
683                 rs->sr_text = "commit failed";
684
685                 goto return_results;
686         }
687
688         Debug( LDAP_DEBUG_TRACE,
689                 LDAP_XSTRING(bdb_modify) ": updated%s id=%08lx dn=\"%s\"\n",
690                 op->o_noop ? " (no-op)" : "",
691                 dummy.e_id, op->o_req_dn.bv_val );
692
693         rs->sr_err = LDAP_SUCCESS;
694         rs->sr_text = NULL;
695         if( num_ctrls ) rs->sr_ctrls = ctrls;
696
697 return_results:
698         if( dummy.e_attrs ) {
699                 attrs_free( dummy.e_attrs );
700         }
701         send_ldap_result( op, rs );
702
703         if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp_kbyte ) {
704                 TXN_CHECKPOINT( bdb->bi_dbenv,
705                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
706         }
707
708 done:
709         slap_graduate_commit_csn( op );
710
711         if( ltid != NULL ) {
712                 TXN_ABORT( ltid );
713         }
714         if ( opinfo.boi_oe.oe_key ) {
715                 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
716         }
717
718         if( e != NULL ) {
719                 bdb_unlocked_cache_return_entry_w (&bdb->bi_cache, e);
720         }
721
722         if( preread_ctrl != NULL && (*preread_ctrl) != NULL ) {
723                 slap_sl_free( (*preread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
724                 slap_sl_free( *preread_ctrl, op->o_tmpmemctx );
725         }
726         if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
727                 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
728                 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
729         }
730
731         rs->sr_text = NULL;
732
733         return rs->sr_err;
734 }