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;
34 Debug( LDAP_DEBUG_TRACE, "bdb_modify_internal: 0x%08lx: %s\n",
37 if ( !acl_check_modlist( be, conn, op, e, modlist )) {
38 return LDAP_INSUFFICIENT_ACCESS;
41 save_attrs = e->e_attrs;
42 e->e_attrs = attrs_dup( e->e_attrs );
44 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
47 switch ( mod->sm_op ) {
49 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: add\n", 0, 0, 0);
50 err = modify_add_values( e, mod, text, textbuf, textlen );
51 if( err != LDAP_SUCCESS ) {
52 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
58 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: delete\n", 0, 0, 0);
59 err = modify_delete_values( e, mod, text, textbuf, textlen );
60 assert( err != LDAP_TYPE_OR_VALUE_EXISTS );
61 if( err != LDAP_SUCCESS ) {
62 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
67 case LDAP_MOD_REPLACE:
68 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: replace\n", 0, 0, 0);
69 err = modify_replace_values( e, mod, text, textbuf, textlen );
70 assert( err != LDAP_TYPE_OR_VALUE_EXISTS );
71 if( err != LDAP_SUCCESS ) {
72 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
77 case SLAP_MOD_SOFTADD:
78 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: softadd\n", 0, 0, 0);
79 /* Avoid problems in index_add_mods()
80 * We need to add index if necessary.
82 mod->sm_op = LDAP_MOD_ADD;
84 err = modify_add_values( e, mod, text, textbuf, textlen );
85 if ( err == LDAP_TYPE_OR_VALUE_EXISTS ) {
89 if( err != LDAP_SUCCESS ) {
90 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
96 Debug(LDAP_DEBUG_ANY, "bdb_modify_internal: invalid op %d\n",
98 *text = "Invalid modify operation";
100 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
104 if ( err != LDAP_SUCCESS ) {
105 attrs_free( e->e_attrs );
106 e->e_attrs = save_attrs;
107 /* unlock entry, delete from cache */
111 /* If objectClass was modified, reset the flags */
112 if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
116 /* check if modified attribute was indexed */
117 err = bdb_index_is_indexed( be, mod->sm_desc );
118 if ( err == LDAP_SUCCESS ) {
119 ap = attr_find( save_attrs, mod->sm_desc );
120 if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
122 ap = attr_find( e->e_attrs, mod->sm_desc );
123 if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
127 /* check that the entry still obeys the schema */
128 rc = entry_schema_check( be, e, save_attrs, text, textbuf, textlen );
129 if ( rc != LDAP_SUCCESS ) {
130 attrs_free( e->e_attrs );
131 e->e_attrs = save_attrs;
132 Debug( LDAP_DEBUG_ANY, "entry failed schema check: %s\n",
137 /* update the indices of the modified attributes */
139 /* start with deleting the old index entries */
140 for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
141 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
142 rc = bdb_index_values( be, tid, ap->a_desc, ap->a_vals,
143 e->e_id, SLAP_INDEX_DELETE_OP );
144 if ( rc != LDAP_SUCCESS ) {
145 attrs_free( e->e_attrs );
146 e->e_attrs = save_attrs;
147 Debug( LDAP_DEBUG_ANY,
148 "Attribute index delete failure",
152 ap->a_flags &= ~SLAP_ATTR_IXDEL;
156 /* add the new index entries */
157 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
158 if (ap->a_flags & SLAP_ATTR_IXADD) {
159 rc = bdb_index_values( be, tid, ap->a_desc, ap->a_vals,
160 e->e_id, SLAP_INDEX_ADD_OP );
161 if ( rc != LDAP_SUCCESS ) {
162 attrs_free( e->e_attrs );
163 e->e_attrs = save_attrs;
164 Debug( LDAP_DEBUG_ANY,
165 "Attribute index add failure",
169 ap->a_flags &= ~SLAP_ATTR_IXADD;
184 Modifications *modlist )
186 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
190 int manageDSAit = get_manageDSAit( op );
191 const char *text = NULL;
192 char textbuf[SLAP_TEXT_BUFLEN];
193 size_t textlen = sizeof textbuf;
195 struct bdb_op_info opinfo;
197 Debug( LDAP_DEBUG_ARGS, "bdb_modify: %s\n", dn->bv_val, 0, 0 );
200 retry: /* transaction retry */
202 bdb_cache_delete_entry(&bdb->bi_cache, e);
203 bdb_cache_return_entry_w(&bdb->bi_cache, e);
205 Debug(LDAP_DEBUG_TRACE,
206 "bdb_modify: retrying...\n", 0, 0, 0);
207 rc = TXN_ABORT( ltid );
209 op->o_private = NULL;
212 text = "internal error";
215 ldap_pvt_thread_yield();
218 /* begin transaction */
219 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
220 bdb->bi_db_opflags );
223 Debug( LDAP_DEBUG_TRACE,
224 "bdb_modify: txn_begin failed: %s (%d)\n",
225 db_strerror(rc), rc, 0 );
227 text = "internal error";
232 opinfo.boi_txn = ltid;
234 op->o_private = &opinfo;
237 rc = bdb_dn2entry_w( be, ltid, ndn, &e, &matched, 0 );
240 Debug( LDAP_DEBUG_TRACE,
241 "bdb_modify: dn2entry failed (%d)\n",
244 case DB_LOCK_DEADLOCK:
245 case DB_LOCK_NOTGRANTED:
252 text = "internal error";
256 /* acquire and lock entry */
258 char* matched_dn = NULL;
261 if ( matched != NULL ) {
262 matched_dn = ch_strdup( matched->e_dn );
263 refs = is_entry_referral( matched )
264 ? get_entry_referrals( be, conn, op, matched )
266 bdb_cache_return_entry_r (&bdb->bi_cache, matched);
270 refs = referral_rewrite( default_referral,
271 NULL, dn, LDAP_SCOPE_DEFAULT );
274 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
275 matched_dn, NULL, refs, NULL );
277 ber_bvarray_free( refs );
283 if ( !manageDSAit && is_entry_referral( e ) ) {
284 /* entry is a referral, don't allow modify */
285 BerVarray refs = get_entry_referrals( be,
288 Debug( LDAP_DEBUG_TRACE,
289 "bdb_modify: entry is referral\n",
292 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
293 e->e_dn, NULL, refs, NULL );
295 ber_bvarray_free( refs );
299 /* Modify the entry */
300 rc = bdb_modify_internal( be, conn, op, ltid, modlist, e,
301 &text, textbuf, textlen );
303 if( rc != LDAP_SUCCESS ) {
304 Debug( LDAP_DEBUG_TRACE,
305 "bdb_modify: modify failed (%d)\n",
308 case DB_LOCK_DEADLOCK:
309 case DB_LOCK_NOTGRANTED:
315 /* change the entry itself */
316 rc = bdb_id2entry_update( be, ltid, e );
318 Debug( LDAP_DEBUG_TRACE,
319 "bdb_modify: id2entry update failed (%d)\n",
322 case DB_LOCK_DEADLOCK:
323 case DB_LOCK_NOTGRANTED:
326 text = "entry update failed";
331 rc = TXN_ABORT( ltid );
333 rc = TXN_COMMIT( ltid, 0 );
336 op->o_private = NULL;
339 Debug( LDAP_DEBUG_TRACE,
340 "bdb_modify: txn_%s failed: %s (%d)\n",
341 op->o_noop ? "abort (no-op)" : "commit",
342 db_strerror(rc), rc );
344 text = "commit failed";
347 Debug( LDAP_DEBUG_TRACE,
348 "bdb_modify: updated%s id=%08lx dn=\"%s\"\n",
349 op->o_noop ? " (no-op)" : "",
356 send_ldap_result( conn, op, rc,
357 NULL, text, NULL, NULL );
359 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
360 ldap_pvt_thread_yield();
361 TXN_CHECKPOINT( bdb->bi_dbenv,
362 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
368 op->o_private = NULL;
372 bdb_cache_return_entry_w (&bdb->bi_cache, e);