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 err = bdb_index_is_indexed( be, mod->sm_desc );
171 if ( err == LDAP_SUCCESS ) {
172 ap = attr_find( save_attrs, mod->sm_desc );
173 if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
175 ap = attr_find( e->e_attrs, mod->sm_desc );
176 if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
180 /* check that the entry still obeys the schema */
181 rc = entry_schema_check( be, e, save_attrs, text, textbuf, textlen );
182 if ( rc != LDAP_SUCCESS ) {
183 attrs_free( e->e_attrs );
184 e->e_attrs = save_attrs;
186 LDAP_LOG ( OPERATION, ERR,
187 "bdb_modify_internal: entry failed schema check %s\n",
190 Debug( LDAP_DEBUG_ANY, "entry failed schema check: %s\n",
196 /* update the indices of the modified attributes */
198 /* start with deleting the old index entries */
199 for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
200 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
201 rc = bdb_index_values( be, tid, ap->a_desc, ap->a_vals,
202 e->e_id, SLAP_INDEX_DELETE_OP );
203 if ( rc != LDAP_SUCCESS ) {
204 attrs_free( e->e_attrs );
205 e->e_attrs = save_attrs;
207 LDAP_LOG ( OPERATION, ERR,
208 "bdb_modify_internal: attribute index delete failure\n",
211 Debug( LDAP_DEBUG_ANY,
212 "Attribute index delete failure",
217 ap->a_flags &= ~SLAP_ATTR_IXDEL;
221 /* add the new index entries */
222 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
223 if (ap->a_flags & SLAP_ATTR_IXADD) {
224 rc = bdb_index_values( be, tid, ap->a_desc, ap->a_vals,
225 e->e_id, SLAP_INDEX_ADD_OP );
226 if ( rc != LDAP_SUCCESS ) {
227 attrs_free( e->e_attrs );
228 e->e_attrs = save_attrs;
230 LDAP_LOG ( OPERATION, ERR,
231 "bdb_modify_internal: attribute index add failure\n",
234 Debug( LDAP_DEBUG_ANY,
235 "Attribute index add failure",
240 ap->a_flags &= ~SLAP_ATTR_IXADD;
255 Modifications *modlist )
257 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
259 Entry *matched = NULL;
261 int manageDSAit = get_manageDSAit( op );
262 const char *text = NULL;
263 char textbuf[SLAP_TEXT_BUFLEN];
264 size_t textlen = sizeof textbuf;
266 struct bdb_op_info opinfo;
274 LDAP_LOG ( OPERATION, ENTRY, "bdb_modify: %s\n", dn->bv_val, 0, 0 );
276 Debug( LDAP_DEBUG_ARGS, "bdb_modify: %s\n", dn->bv_val, 0, 0 );
280 retry: /* transaction retry */
282 bdb_cache_delete_entry(&bdb->bi_cache, e);
283 bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
286 LDAP_LOG ( OPERATION, DETAIL1, "bdb_modify: retrying...\n", 0, 0, 0 );
288 Debug(LDAP_DEBUG_TRACE,
289 "bdb_modify: retrying...\n", 0, 0, 0);
291 rc = TXN_ABORT( ltid );
293 op->o_private = NULL;
296 text = "internal error";
299 ldap_pvt_thread_yield();
302 /* begin transaction */
303 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
304 bdb->bi_db_opflags );
308 LDAP_LOG ( OPERATION, DETAIL1,
309 "bdb_modify: txn_begin failed: %s (%d)\n", db_strerror(rc), rc, 0 );
311 Debug( LDAP_DEBUG_TRACE,
312 "bdb_modify: txn_begin failed: %s (%d)\n",
313 db_strerror(rc), rc, 0 );
316 text = "internal error";
320 locker = TXN_ID ( ltid );
323 opinfo.boi_txn = ltid;
325 op->o_private = &opinfo;
328 rc = bdb_dn2entry_w( be, ltid, ndn, &e, &matched, 0, locker, &lock );
332 LDAP_LOG ( OPERATION, DETAIL1,
333 "bdb_modify: dn2entry failed: (%d)\n", rc, 0, 0 );
335 Debug( LDAP_DEBUG_TRACE,
336 "bdb_modify: dn2entry failed (%d)\n",
340 case DB_LOCK_DEADLOCK:
341 case DB_LOCK_NOTGRANTED:
346 text = "ldap server busy";
351 text = "internal error";
355 /* acquire and lock entry */
357 char* matched_dn = NULL;
360 if ( matched != NULL ) {
361 matched_dn = ch_strdup( matched->e_dn );
362 refs = is_entry_referral( matched )
363 ? get_entry_referrals( be, conn, op, matched )
365 bdb_unlocked_cache_return_entry_r (&bdb->bi_cache, matched);
369 refs = referral_rewrite( default_referral,
370 NULL, dn, LDAP_SCOPE_DEFAULT );
373 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
374 matched_dn, NULL, refs, NULL );
376 ber_bvarray_free( refs );
382 if ( !manageDSAit && is_entry_referral( e ) ) {
383 /* entry is a referral, don't allow modify */
384 BerVarray refs = get_entry_referrals( be,
388 LDAP_LOG ( OPERATION, DETAIL1, "bdb_modify: entry is referral\n", 0, 0, 0 );
390 Debug( LDAP_DEBUG_TRACE,
391 "bdb_modify: entry is referral\n",
395 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
396 e->e_dn, NULL, refs, NULL );
398 ber_bvarray_free( refs );
402 /* Modify the entry */
403 rc = bdb_modify_internal( be, conn, op, ltid, modlist, e,
404 &text, textbuf, textlen );
406 if( rc != LDAP_SUCCESS ) {
408 LDAP_LOG ( OPERATION, ERR,
409 "bdb_modify: modify failed (%d)\n", rc, 0, 0 );
411 Debug( LDAP_DEBUG_TRACE,
412 "bdb_modify: modify failed (%d)\n",
416 case DB_LOCK_DEADLOCK:
417 case DB_LOCK_NOTGRANTED:
423 /* change the entry itself */
424 rc = bdb_id2entry_update( be, ltid, e );
427 LDAP_LOG ( OPERATION, ERR,
428 "bdb_modify: id2entry update failed (%d)\n", rc, 0, 0 );
430 Debug( LDAP_DEBUG_TRACE,
431 "bdb_modify: id2entry update failed (%d)\n",
435 case DB_LOCK_DEADLOCK:
436 case DB_LOCK_NOTGRANTED:
439 text = "entry update failed";
444 if ( ( rc = TXN_ABORT( ltid ) ) != 0 ) {
445 text = "txn_abort (no-op) failed";
451 rc = TXN_COMMIT( ltid, 0 );
454 op->o_private = NULL;
458 LDAP_LOG ( OPERATION, ERR,
459 "bdb_modify: txn_%s failed %s (%d)\n",
460 op->o_noop ? "abort (no_op)" : "commit", db_strerror(rc), rc );
462 Debug( LDAP_DEBUG_TRACE,
463 "bdb_modify: txn_%s failed: %s (%d)\n",
464 op->o_noop ? "abort (no-op)" : "commit",
465 db_strerror(rc), rc );
468 text = "commit failed";
472 LDAP_LOG ( OPERATION, DETAIL1,
473 "bdb_modify: updated%s id=%08lx dn=\"%s\"\n",
474 op->o_noop ? " (no_op)" : "", e->e_id, e->e_dn );
476 Debug( LDAP_DEBUG_TRACE,
477 "bdb_modify: updated%s id=%08lx dn=\"%s\"\n",
478 op->o_noop ? " (no-op)" : "",
486 send_ldap_result( conn, op, rc,
487 NULL, text, NULL, NULL );
489 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
490 ldap_pvt_thread_yield();
491 TXN_CHECKPOINT( bdb->bi_dbenv,
492 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
498 op->o_private = NULL;
502 bdb_unlocked_cache_return_entry_w (&bdb->bi_cache, e);
504 return ( ( rc == LDAP_SUCCESS ) ? noop : rc );