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;
272 LDAP_LOG ( OPERATION, ENTRY, "bdb_modify: %s\n", dn->bv_val, 0, 0 );
274 Debug( LDAP_DEBUG_ARGS, "bdb_modify: %s\n", dn->bv_val, 0, 0 );
278 retry: /* transaction retry */
280 bdb_cache_delete_entry(&bdb->bi_cache, e);
281 bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
284 LDAP_LOG ( OPERATION, DETAIL1, "bdb_modify: retrying...\n", 0, 0, 0 );
286 Debug(LDAP_DEBUG_TRACE,
287 "bdb_modify: retrying...\n", 0, 0, 0);
289 rc = TXN_ABORT( ltid );
291 op->o_private = NULL;
294 text = "internal error";
297 ldap_pvt_thread_yield();
300 /* begin transaction */
301 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
302 bdb->bi_db_opflags );
306 LDAP_LOG ( OPERATION, DETAIL1,
307 "bdb_modify: txn_begin failed: %s (%d)\n", db_strerror(rc), rc, 0 );
309 Debug( LDAP_DEBUG_TRACE,
310 "bdb_modify: txn_begin failed: %s (%d)\n",
311 db_strerror(rc), rc, 0 );
314 text = "internal error";
318 locker = TXN_ID ( ltid );
321 opinfo.boi_txn = ltid;
323 op->o_private = &opinfo;
326 rc = bdb_dn2entry_w( be, ltid, ndn, &e, &matched, 0, locker, &lock );
330 LDAP_LOG ( OPERATION, DETAIL1,
331 "bdb_modify: dn2entry failed: (%d)\n", rc, 0, 0 );
333 Debug( LDAP_DEBUG_TRACE,
334 "bdb_modify: dn2entry failed (%d)\n",
338 case DB_LOCK_DEADLOCK:
339 case DB_LOCK_NOTGRANTED:
344 text = "ldap server busy";
349 text = "internal error";
353 /* acquire and lock entry */
355 char* matched_dn = NULL;
358 if ( matched != NULL ) {
359 matched_dn = ch_strdup( matched->e_dn );
360 refs = is_entry_referral( matched )
361 ? get_entry_referrals( be, conn, op, matched )
363 bdb_unlocked_cache_return_entry_r (&bdb->bi_cache, matched);
367 refs = referral_rewrite( default_referral,
368 NULL, dn, LDAP_SCOPE_DEFAULT );
371 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
372 matched_dn, NULL, refs, NULL );
374 ber_bvarray_free( refs );
380 if ( !manageDSAit && is_entry_referral( e ) ) {
381 /* entry is a referral, don't allow modify */
382 BerVarray refs = get_entry_referrals( be,
386 LDAP_LOG ( OPERATION, DETAIL1, "bdb_modify: entry is referral\n", 0, 0, 0 );
388 Debug( LDAP_DEBUG_TRACE,
389 "bdb_modify: entry is referral\n",
393 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
394 e->e_dn, NULL, refs, NULL );
396 ber_bvarray_free( refs );
400 /* Modify the entry */
401 rc = bdb_modify_internal( be, conn, op, ltid, modlist, e,
402 &text, textbuf, textlen );
404 if( rc != LDAP_SUCCESS ) {
406 LDAP_LOG ( OPERATION, ERR,
407 "bdb_modify: modify failed (%d)\n", rc, 0, 0 );
409 Debug( LDAP_DEBUG_TRACE,
410 "bdb_modify: modify failed (%d)\n",
414 case DB_LOCK_DEADLOCK:
415 case DB_LOCK_NOTGRANTED:
421 /* change the entry itself */
422 rc = bdb_id2entry_update( be, ltid, e );
425 LDAP_LOG ( OPERATION, ERR,
426 "bdb_modify: id2entry update failed (%d)\n", rc, 0, 0 );
428 Debug( LDAP_DEBUG_TRACE,
429 "bdb_modify: id2entry update failed (%d)\n",
433 case DB_LOCK_DEADLOCK:
434 case DB_LOCK_NOTGRANTED:
437 text = "entry update failed";
442 rc = TXN_ABORT( ltid );
444 rc = TXN_COMMIT( ltid, 0 );
447 op->o_private = NULL;
451 LDAP_LOG ( OPERATION, ERR,
452 "bdb_modify: txn_%s failed %s (%d)\n",
453 op->o_noop ? "abort (no_op)" : "commit", db_strerror(rc), rc );
455 Debug( LDAP_DEBUG_TRACE,
456 "bdb_modify: txn_%s failed: %s (%d)\n",
457 op->o_noop ? "abort (no-op)" : "commit",
458 db_strerror(rc), rc );
461 text = "commit failed";
465 LDAP_LOG ( OPERATION, DETAIL1,
466 "bdb_modify: updated%s id=%08lx dn=\"%s\"\n",
467 op->o_noop ? " (no_op)" : "", e->e_id, e->e_dn );
469 Debug( LDAP_DEBUG_TRACE,
470 "bdb_modify: updated%s id=%08lx dn=\"%s\"\n",
471 op->o_noop ? " (no-op)" : "",
479 send_ldap_result( conn, op, rc,
480 NULL, text, NULL, NULL );
482 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
483 ldap_pvt_thread_yield();
484 TXN_CHECKPOINT( bdb->bi_dbenv,
485 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
491 op->o_private = NULL;
495 bdb_unlocked_cache_return_entry_w (&bdb->bi_cache, e);