]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-bdb/modify.c
ITS#5729 check for NULL bei_parent
[openldap] / servers / slapd / back-bdb / modify.c
index 9ef342c3066d1042f61db67258d6a2e357925478..cf44b7d9e501da6b2c977ba8f696b90ac9c0b252 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 2000-2006 The OpenLDAP Foundation.
+ * Copyright 2000-2008 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -42,6 +42,8 @@ int bdb_modify_internal(
        Attribute       *save_attrs;
        Attribute       *ap;
        int                     glue_attr_delete = 0;
+       int                     got_delete;
+       AttrInfo *ai;
 
        Debug( LDAP_DEBUG_TRACE, "bdb_modify_internal: 0x%08lx: %s\n",
                e->e_id, e->e_dn, 0);
@@ -87,7 +89,9 @@ int bdb_modify_internal(
        }
 
        for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
+               struct berval ix_at;
                mod = &ml->sml_mod;
+               got_delete = 0;
 
                switch ( mod->sm_op ) {
                case LDAP_MOD_ADD:
@@ -117,6 +121,8 @@ int bdb_modify_internal(
                        if( err != LDAP_SUCCESS ) {
                                Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
                                        err, *text, 0);
+                       } else {
+                               got_delete = 1;
                        }
                        break;
 
@@ -129,6 +135,8 @@ int bdb_modify_internal(
                        if( err != LDAP_SUCCESS ) {
                                Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
                                        err, *text, 0);
+                       } else {
+                               got_delete = 1;
                        }
                        break;
 
@@ -142,6 +150,8 @@ int bdb_modify_internal(
                                Debug(LDAP_DEBUG_ARGS,
                                        "bdb_modify_internal: %d %s\n",
                                        err, *text, 0);
+                       } else {
+                               got_delete = 1;
                        }
                        break;
 
@@ -194,13 +204,24 @@ int bdb_modify_internal(
 
                /* check if modified attribute was indexed
                 * but not in case of NOOP... */
-               err = bdb_index_is_indexed( op->o_bd, mod->sm_desc );
-               if ( err == LDAP_SUCCESS && !op->o_noop ) {
-                       ap = attr_find( save_attrs, mod->sm_desc );
-                       if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
-
-                       ap = attr_find( e->e_attrs, mod->sm_desc );
-                       if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
+               ai = bdb_index_mask( op->o_bd, mod->sm_desc, &ix_at );
+               if ( ai && !op->o_noop ) {
+                       if ( got_delete ) {
+                               struct berval ix2;
+
+                               ap = attr_find( save_attrs, mod->sm_desc );
+                               if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
+
+                               /* Find all other attrs that index to same slot */
+                               for ( ap = e->e_attrs; ap; ap=ap->a_next ) {
+                                       ai = bdb_index_mask( op->o_bd, ap->a_desc, &ix2 );
+                                       if ( ai && ix2.bv_val == ix_at.bv_val )
+                                               ap->a_flags |= SLAP_ATTR_IXADD;
+                               }
+                       } else {
+                               ap = attr_find( e->e_attrs, mod->sm_desc );
+                               if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
+                       }
                }
        }
 
@@ -211,7 +232,7 @@ int bdb_modify_internal(
                attrs_free( e->e_attrs );
                /* clear the indexing flags */
                for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
-                       ap->a_flags = 0;
+                       ap->a_flags &= ~(SLAP_ATTR_IXADD|SLAP_ATTR_IXDEL);
                }
                e->e_attrs = save_attrs;
 
@@ -230,24 +251,57 @@ int bdb_modify_internal(
        /* start with deleting the old index entries */
        for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
                if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
-                       rc = bdb_index_values( op, tid, ap->a_desc,
-                               ap->a_nvals,
-                               e->e_id, SLAP_INDEX_DELETE_OP );
-                       if ( rc != LDAP_SUCCESS ) {
-                               attrs_free( e->e_attrs );
-                               e->e_attrs = save_attrs;
-                               Debug( LDAP_DEBUG_ANY,
-                                      "Attribute index delete failure",
-                                      0, 0, 0 );
-                               return rc;
-                       }
+                       struct berval *vals;
+                       Attribute *a2;
                        ap->a_flags &= ~SLAP_ATTR_IXDEL;
+                       a2 = attr_find( e->e_attrs, ap->a_desc );
+                       if ( a2 ) {
+                               /* need to detect which values were deleted */
+                               int i, j;
+                               struct berval tmp;
+                               j = ap->a_numvals;
+                               for ( i=0; i<j; ) {
+                                       rc = attr_valfind( a2, SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
+                                               &ap->a_nvals[i], NULL, op->o_tmpmemctx );
+                                       /* Move deleted values to end of array */
+                                       if ( rc == LDAP_NO_SUCH_ATTRIBUTE ) {
+                                               j--;
+                                               if ( i != j ) {
+                                                       tmp = ap->a_nvals[j];
+                                                       ap->a_nvals[j] = ap->a_nvals[i];
+                                                       ap->a_nvals[i] = tmp;
+                                                       tmp = ap->a_vals[j];
+                                                       ap->a_vals[j] = ap->a_vals[i];
+                                                       ap->a_vals[i] = tmp;
+                                               }
+                                               continue;
+                                       }
+                                       i++;
+                               }
+                               vals = &ap->a_nvals[j];
+                       } else {
+                               /* attribute was completely deleted */
+                               vals = ap->a_nvals;
+                       }
+                       if ( !BER_BVISNULL( vals )) {
+                               rc = bdb_index_values( op, tid, ap->a_desc,
+                                       vals, e->e_id, SLAP_INDEX_DELETE_OP );
+                               if ( rc != LDAP_SUCCESS ) {
+                                       attrs_free( e->e_attrs );
+                                       e->e_attrs = save_attrs;
+                                       Debug( LDAP_DEBUG_ANY,
+                                                  "Attribute index delete failure",
+                                                  0, 0, 0 );
+                                       return rc;
+                               }
+                       }
                }
        }
 
        /* add the new index entries */
        for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
                if (ap->a_flags & SLAP_ATTR_IXADD) {
+                       ap->a_flags &= ~SLAP_ATTR_IXADD;
                        rc = bdb_index_values( op, tid, ap->a_desc,
                                ap->a_nvals,
                                e->e_id, SLAP_INDEX_ADD_OP );
@@ -259,7 +313,6 @@ int bdb_modify_internal(
                                       0, 0, 0 );
                                return rc;
                        }
-                       ap->a_flags &= ~SLAP_ATTR_IXADD;
                }
        }
 
@@ -281,7 +334,6 @@ bdb_modify( Operation *op, SlapReply *rs )
        Entry           dummy = {0};
        int                     fakeroot = 0;
 
-       u_int32_t       locker = 0;
        DB_LOCK         lock;
 
        int             num_retries = 0;
@@ -357,7 +409,8 @@ retry:      /* transaction retry */
 
                rs->sr_err = TXN_ABORT( ltid );
                ltid = NULL;
-               op->o_private = NULL;
+               LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
+               opinfo.boi_oe.oe_key = NULL;
                op->o_do_not_cache = opinfo.boi_acl_cache;
                if( rs->sr_err != 0 ) {
                        rs->sr_err = LDAP_OTHER;
@@ -384,18 +437,15 @@ retry:    /* transaction retry */
                goto return_results;
        }
 
-       locker = TXN_ID ( ltid );
-
-       opinfo.boi_bdb = op->o_bd;
+       opinfo.boi_oe.oe_key = bdb;
        opinfo.boi_txn = ltid;
-       opinfo.boi_locker = locker;
        opinfo.boi_err = 0;
        opinfo.boi_acl_cache = op->o_do_not_cache;
-       op->o_private = &opinfo;
+       LDAP_SLIST_INSERT_HEAD( &op->o_extra, &opinfo.boi_oe, oe_next );
 
        /* get entry or ancestor */
        rs->sr_err = bdb_dn2entry( op, ltid, &op->o_req_ndn, &ei, 1,
-               locker, &lock );
+               &lock );
 
        if ( rs->sr_err != 0 ) {
                Debug( LDAP_DEBUG_TRACE,
@@ -522,7 +572,7 @@ retry:      /* transaction retry */
        }
        /* Modify the entry */
        dummy = *e;
-       rs->sr_err = bdb_modify_internal( op, lt2, op->oq_modify.rs_modlist,
+       rs->sr_err = bdb_modify_internal( op, lt2, op->orm_modlist,
                &dummy, &rs->sr_text, textbuf, textlen );
 
        if( rs->sr_err != LDAP_SUCCESS ) {
@@ -588,6 +638,8 @@ retry:      /* transaction retry */
                } else {
                        rs->sr_err = LDAP_X_NO_OPERATION;
                        ltid = NULL;
+                       /* Only free attrs if they were dup'd.  */
+                       if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
                        goto return_results;
                }
        } else {
@@ -600,7 +652,7 @@ retry:      /* transaction retry */
                        attrs_free( dummy.e_attrs );
 
                } else {
-                       rc = bdb_cache_modify( bdb, e, dummy.e_attrs, locker, &lock );
+                       rc = bdb_cache_modify( bdb, e, dummy.e_attrs, ltid, &lock );
                        switch( rc ) {
                        case DB_LOCK_DEADLOCK:
                        case DB_LOCK_NOTGRANTED:
@@ -612,7 +664,8 @@ retry:      /* transaction retry */
                rs->sr_err = TXN_COMMIT( ltid, 0 );
        }
        ltid = NULL;
-       op->o_private = NULL;
+       LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
+       opinfo.boi_oe.oe_key = NULL;
 
        if( rs->sr_err != 0 ) {
                Debug( LDAP_DEBUG_TRACE,
@@ -651,7 +704,9 @@ done:
        if( ltid != NULL ) {
                TXN_ABORT( ltid );
        }
-       op->o_private = NULL;
+       if ( opinfo.boi_oe.oe_key ) {
+               LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
+       }
 
        if( e != NULL ) {
                bdb_unlocked_cache_return_entry_w (&bdb->bi_cache, e);
@@ -665,5 +720,8 @@ done:
                slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
                slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
        }
+
+       rs->sr_text = NULL;
+
        return rs->sr_err;
 }