1 /* modify.c - bdb backend modify routine */
4 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
17 int bdb_modify_internal(
22 Modifications *modlist,
31 Attribute *save_attrs;
35 LDAP_LOG ( OPERATION, ENTRY, "bdb_modify_internal: 0x%08lx: %s\n",
36 e->e_id, e->e_dn, 0 );
38 Debug( LDAP_DEBUG_TRACE, "bdb_modify_internal: 0x%08lx: %s\n",
42 if ( !acl_check_modlist( be, conn, op, e, modlist )) {
43 return LDAP_INSUFFICIENT_ACCESS;
46 save_attrs = e->e_attrs;
47 e->e_attrs = attrs_dup( e->e_attrs );
49 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
52 switch ( mod->sm_op ) {
55 LDAP_LOG ( OPERATION, DETAIL1, "bdb_modify_internal: add\n", 0,0,0);
57 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: add\n", 0, 0, 0);
59 err = modify_add_values( e, mod, text, textbuf, textlen );
60 if( err != LDAP_SUCCESS ) {
62 LDAP_LOG ( OPERATION, ERR,
63 "bdb_modify_internal: %d %s\n", err, *text, 0 );
65 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
73 LDAP_LOG ( OPERATION, DETAIL1,
74 "bdb_modify_internal: delete\n", 0, 0, 0 );
76 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: delete\n", 0, 0, 0);
78 err = modify_delete_values( e, mod, text, textbuf, textlen );
79 assert( err != LDAP_TYPE_OR_VALUE_EXISTS );
80 if( err != LDAP_SUCCESS ) {
82 LDAP_LOG ( OPERATION, ERR,
83 "bdb_modify_internal: %d %s\n", err, *text, 0 );
85 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
91 case LDAP_MOD_REPLACE:
93 LDAP_LOG ( OPERATION, DETAIL1,
94 "bdb_modify_internal: replace\n", 0, 0, 0 );
96 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: replace\n", 0, 0, 0);
98 err = modify_replace_values( e, mod, text, textbuf, textlen );
99 if( err != LDAP_SUCCESS ) {
101 LDAP_LOG ( OPERATION, ERR,
102 "bdb_modify_internal: %d %s\n", err, *text, 0 );
104 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
110 case SLAP_MOD_SOFTADD:
112 LDAP_LOG ( OPERATION, DETAIL1,
113 "bdb_modify_internal: softadd\n",0,0,0 );
115 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: softadd\n", 0, 0, 0);
117 /* Avoid problems in index_add_mods()
118 * We need to add index if necessary.
120 mod->sm_op = LDAP_MOD_ADD;
122 err = modify_add_values( e, mod, text, textbuf, textlen );
123 if ( err == LDAP_TYPE_OR_VALUE_EXISTS ) {
127 if( err != LDAP_SUCCESS ) {
129 LDAP_LOG ( OPERATION, ERR,
130 "bdb_modify_internal: %d %s\n", err, *text, 0 );
132 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
140 LDAP_LOG ( OPERATION, ERR,
141 "bdb_modify_internal: invalid op %d\n", mod->sm_op, 0, 0 );
143 Debug(LDAP_DEBUG_ANY, "bdb_modify_internal: invalid op %d\n",
146 *text = "Invalid modify operation";
149 LDAP_LOG ( OPERATION, ERR,
150 "bdb_modify_internal: %d %s\n", err, *text, 0 );
152 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
157 if ( err != LDAP_SUCCESS ) {
158 attrs_free( e->e_attrs );
159 e->e_attrs = save_attrs;
160 /* unlock entry, delete from cache */
164 /* If objectClass was modified, reset the flags */
165 if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
169 /* check if modified attribute was indexed
170 * but not in case of NOOP... */
171 err = bdb_index_is_indexed( be, mod->sm_desc );
172 if ( err == LDAP_SUCCESS && !op->o_noop ) {
173 ap = attr_find( save_attrs, mod->sm_desc );
174 if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
176 ap = attr_find( e->e_attrs, mod->sm_desc );
177 if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
181 /* check that the entry still obeys the schema */
182 rc = entry_schema_check( be, e, save_attrs, text, textbuf, textlen );
183 if ( rc != LDAP_SUCCESS || op->o_noop ) {
184 attrs_free( e->e_attrs );
185 e->e_attrs = save_attrs;
187 if ( rc != LDAP_SUCCESS ) {
189 LDAP_LOG ( OPERATION, ERR, "bdb_modify_internal: "
190 "entry failed schema check %s\n",
193 Debug( LDAP_DEBUG_ANY,
194 "entry failed schema check: %s\n",
199 /* if NOOP then silently revert to saved attrs */
203 /* update the indices of the modified attributes */
205 /* start with deleting the old index entries */
206 for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
207 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
208 rc = bdb_index_values( be, tid, ap->a_desc, ap->a_vals,
209 e->e_id, SLAP_INDEX_DELETE_OP );
210 if ( rc != LDAP_SUCCESS ) {
211 attrs_free( e->e_attrs );
212 e->e_attrs = save_attrs;
214 LDAP_LOG ( OPERATION, ERR,
215 "bdb_modify_internal: attribute index delete failure\n",
218 Debug( LDAP_DEBUG_ANY,
219 "Attribute index delete failure",
224 ap->a_flags &= ~SLAP_ATTR_IXDEL;
228 /* add the new index entries */
229 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
230 if (ap->a_flags & SLAP_ATTR_IXADD) {
231 rc = bdb_index_values( be, tid, ap->a_desc, ap->a_vals,
232 e->e_id, SLAP_INDEX_ADD_OP );
233 if ( rc != LDAP_SUCCESS ) {
234 attrs_free( e->e_attrs );
235 e->e_attrs = save_attrs;
237 LDAP_LOG ( OPERATION, ERR,
238 "bdb_modify_internal: attribute index add failure\n",
241 Debug( LDAP_DEBUG_ANY,
242 "Attribute index add failure",
247 ap->a_flags &= ~SLAP_ATTR_IXADD;
262 Modifications *modlist )
264 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
266 Entry *matched = NULL;
268 int manageDSAit = get_manageDSAit( op );
269 const char *text = NULL;
270 char textbuf[SLAP_TEXT_BUFLEN];
271 size_t textlen = sizeof textbuf;
273 struct bdb_op_info opinfo;
281 LDAP_LOG ( OPERATION, ENTRY, "bdb_modify: %s\n", dn->bv_val, 0, 0 );
283 Debug( LDAP_DEBUG_ARGS, "bdb_modify: %s\n", dn->bv_val, 0, 0 );
287 retry: /* transaction retry */
289 bdb_cache_delete_entry(&bdb->bi_cache, e);
290 bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
293 LDAP_LOG ( OPERATION, DETAIL1, "bdb_modify: retrying...\n", 0, 0, 0 );
295 Debug(LDAP_DEBUG_TRACE,
296 "bdb_modify: retrying...\n", 0, 0, 0);
298 rc = TXN_ABORT( ltid );
300 op->o_private = NULL;
303 text = "internal error";
306 ldap_pvt_thread_yield();
309 /* begin transaction */
310 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
311 bdb->bi_db_opflags );
315 LDAP_LOG ( OPERATION, DETAIL1,
316 "bdb_modify: txn_begin failed: %s (%d)\n", db_strerror(rc), rc, 0 );
318 Debug( LDAP_DEBUG_TRACE,
319 "bdb_modify: txn_begin failed: %s (%d)\n",
320 db_strerror(rc), rc, 0 );
323 text = "internal error";
327 locker = TXN_ID ( ltid );
330 opinfo.boi_txn = ltid;
332 op->o_private = &opinfo;
335 rc = bdb_dn2entry_w( be, ltid, ndn, &e, &matched, 0, locker, &lock );
339 LDAP_LOG ( OPERATION, DETAIL1,
340 "bdb_modify: dn2entry failed: (%d)\n", rc, 0, 0 );
342 Debug( LDAP_DEBUG_TRACE,
343 "bdb_modify: dn2entry failed (%d)\n",
347 case DB_LOCK_DEADLOCK:
348 case DB_LOCK_NOTGRANTED:
353 text = "ldap server busy";
358 text = "internal error";
362 /* acquire and lock entry */
364 char* matched_dn = NULL;
367 if ( matched != NULL ) {
368 matched_dn = ch_strdup( matched->e_dn );
369 refs = is_entry_referral( matched )
370 ? get_entry_referrals( be, conn, op, matched )
372 bdb_unlocked_cache_return_entry_r (&bdb->bi_cache, matched);
376 refs = referral_rewrite( default_referral,
377 NULL, dn, LDAP_SCOPE_DEFAULT );
380 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
381 matched_dn, NULL, refs, NULL );
383 ber_bvarray_free( refs );
389 if ( !manageDSAit && is_entry_referral( e ) ) {
390 /* entry is a referral, don't allow modify */
391 BerVarray refs = get_entry_referrals( be,
395 LDAP_LOG ( OPERATION, DETAIL1, "bdb_modify: entry is referral\n", 0, 0, 0 );
397 Debug( LDAP_DEBUG_TRACE,
398 "bdb_modify: entry is referral\n",
402 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
403 e->e_dn, NULL, refs, NULL );
405 ber_bvarray_free( refs );
409 /* Modify the entry */
410 rc = bdb_modify_internal( be, conn, op, ltid, modlist, e,
411 &text, textbuf, textlen );
413 if( rc != LDAP_SUCCESS ) {
415 LDAP_LOG ( OPERATION, ERR,
416 "bdb_modify: modify failed (%d)\n", rc, 0, 0 );
418 Debug( LDAP_DEBUG_TRACE,
419 "bdb_modify: modify failed (%d)\n",
423 case DB_LOCK_DEADLOCK:
424 case DB_LOCK_NOTGRANTED:
430 /* change the entry itself */
431 rc = bdb_id2entry_update( be, ltid, e );
434 LDAP_LOG ( OPERATION, ERR,
435 "bdb_modify: id2entry update failed (%d)\n", rc, 0, 0 );
437 Debug( LDAP_DEBUG_TRACE,
438 "bdb_modify: id2entry update failed (%d)\n",
442 case DB_LOCK_DEADLOCK:
443 case DB_LOCK_NOTGRANTED:
446 text = "entry update failed";
451 if ( ( rc = TXN_ABORT( ltid ) ) != 0 ) {
452 text = "txn_abort (no-op) failed";
458 rc = TXN_COMMIT( ltid, 0 );
461 op->o_private = NULL;
465 LDAP_LOG ( OPERATION, ERR,
466 "bdb_modify: txn_%s failed %s (%d)\n",
467 op->o_noop ? "abort (no_op)" : "commit", db_strerror(rc), rc );
469 Debug( LDAP_DEBUG_TRACE,
470 "bdb_modify: txn_%s failed: %s (%d)\n",
471 op->o_noop ? "abort (no-op)" : "commit",
472 db_strerror(rc), rc );
475 text = "commit failed";
479 LDAP_LOG ( OPERATION, DETAIL1,
480 "bdb_modify: updated%s id=%08lx dn=\"%s\"\n",
481 op->o_noop ? " (no_op)" : "", e->e_id, e->e_dn );
483 Debug( LDAP_DEBUG_TRACE,
484 "bdb_modify: updated%s id=%08lx dn=\"%s\"\n",
485 op->o_noop ? " (no-op)" : "",
493 send_ldap_result( conn, op, rc,
494 NULL, text, NULL, NULL );
496 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
497 ldap_pvt_thread_yield();
498 TXN_CHECKPOINT( bdb->bi_dbenv,
499 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
505 op->o_private = NULL;
509 bdb_unlocked_cache_return_entry_w (&bdb->bi_cache, e);
511 return ( ( rc == LDAP_SUCCESS ) ? noop : rc );