Whamcloud - gitweb
LU-14854 mdd: proper handle error in mdd_swap_layouts()
[fs/lustre-release.git] / lustre / mdd / mdd_object.c
index 0d83356..e431fdf 100644 (file)
@@ -27,7 +27,6 @@
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
- * Lustre is a trademark of Sun Microsystems, Inc.
  *
  * lustre/mdd/mdd_object.c
  *
@@ -72,6 +71,7 @@ static int mdd_changelog_data_store_by_fid(const struct lu_env *env,
                                           enum changelog_rec_type type,
                                           enum changelog_rec_flags clf_flags,
                                           const struct lu_fid *fid,
+                                          const struct lu_fid *pfid,
                                           const char *xattr_name,
                                           struct thandle *handle);
 
@@ -243,19 +243,14 @@ int mdd_la_get(const struct lu_env *env, struct mdd_object *obj,
 
 struct mdd_thread_info *mdd_env_info(const struct lu_env *env)
 {
-        struct mdd_thread_info *info;
-
-       lu_env_refill((struct lu_env *)env);
-        info = lu_context_key_get(&env->le_ctx, &mdd_thread_key);
-        LASSERT(info != NULL);
-        return info;
+       return lu_env_info(env, &mdd_thread_key);
 }
 
 struct lu_buf *mdd_buf_get(const struct lu_env *env, void *area, ssize_t len)
 {
        struct lu_buf *buf;
 
-       buf = &mdd_env_info(env)->mti_buf[0];
+       buf = &mdd_env_info(env)->mdi_buf[0];
        buf->lb_buf = area;
        buf->lb_len = len;
        return buf;
@@ -266,7 +261,7 @@ const struct lu_buf *mdd_buf_get_const(const struct lu_env *env,
 {
        struct lu_buf *buf;
 
-       buf = &mdd_env_info(env)->mti_buf[0];
+       buf = &mdd_env_info(env)->mdi_buf[0];
        buf->lb_buf = (void *)area;
        buf->lb_len = len;
        return buf;
@@ -340,6 +335,7 @@ static void mdd_object_free(const struct lu_env *env, struct lu_object *o)
        }
 
        lu_object_fini(o);
+       /* mdd doesn't contain an lu_object_header, so don't need call_rcu */
        OBD_SLAB_FREE_PTR(mdd, mdd_object_kmem);
 }
 
@@ -436,7 +432,7 @@ static int mdd_xattr_get(const struct lu_env *env,
                struct thandle *handle;
                int rc2;
 
-               LASSERT(mdo2fid(mdd_obj) != NULL);
+               LASSERT(mdd_object_fid(mdd_obj) != NULL);
 
                handle = mdd_trans_create(env, mdd);
                if (IS_ERR(handle))
@@ -452,8 +448,8 @@ static int mdd_xattr_get(const struct lu_env *env,
                        GOTO(stop, rc2);
 
                rc2 = mdd_changelog_data_store_by_fid(env, mdd, CL_GETXATTR, 0,
-                                                     mdo2fid(mdd_obj), name,
-                                                     handle);
+                                                     mdd_object_fid(mdd_obj),
+                                                     NULL, name, handle);
 
 stop:
                rc2 = mdd_trans_stop(env, mdd, rc2, handle);
@@ -554,7 +550,7 @@ int mdd_declare_create_object_internal(const struct lu_env *env,
                                       const struct md_op_spec *spec,
                                       struct dt_allocation_hint *hint)
 {
-       struct dt_object_format *dof = &mdd_env_info(env)->mti_dof;
+       struct dt_object_format *dof = &mdd_env_info(env)->mdi_dof;
        const struct dt_index_features *feat = spec->sp_feat;
        int rc;
        ENTRY;
@@ -586,7 +582,7 @@ int mdd_create_object_internal(const struct lu_env *env, struct mdd_object *p,
                               const struct md_op_spec *spec,
                               struct dt_allocation_hint *hint)
 {
-       struct dt_object_format *dof = &mdd_env_info(env)->mti_dof;
+       struct dt_object_format *dof = &mdd_env_info(env)->mdi_dof;
        int rc;
        ENTRY;
 
@@ -637,6 +633,22 @@ int mdd_update_time(const struct lu_env *env, struct mdd_object *obj,
        RETURN(rc);
 }
 
+
+static bool is_project_state_change(const struct lu_attr *oattr,
+                                   struct lu_attr *la)
+{
+       if (la->la_valid & LA_PROJID &&
+           oattr->la_projid != la->la_projid)
+               return true;
+
+       if ((la->la_valid & LA_FLAGS) &&
+           (la->la_flags & LUSTRE_PROJINHERIT_FL) !=
+           (oattr->la_flags & LUSTRE_PROJINHERIT_FL))
+               return true;
+
+       return false;
+}
+
 /*
  * This gives the same functionality as the code between
  * sys_chmod and inode_setattr
@@ -646,10 +658,12 @@ int mdd_update_time(const struct lu_env *env, struct mdd_object *obj,
  */
 static int mdd_fix_attr(const struct lu_env *env, struct mdd_object *obj,
                        const struct lu_attr *oattr, struct lu_attr *la,
-                       const unsigned long flags)
+                       const struct md_attr *ma)
 {
        struct lu_ucred  *uc;
        int               rc = 0;
+       const unsigned long flags = ma->ma_attr_flags;
+
        ENTRY;
 
        if (!la->la_valid)
@@ -669,6 +683,14 @@ static int mdd_fix_attr(const struct lu_env *env, struct mdd_object *obj,
        if (uc == NULL)
                RETURN(0);
 
+       if (is_project_state_change(oattr, la)) {
+               if (!md_capable(uc, CAP_SYS_RESOURCE) &&
+                   !lustre_in_group_p(uc, ma->ma_enable_chprojid_gid) &&
+                   !(ma->ma_enable_chprojid_gid == -1 &&
+                     mdd_permission_internal(env, obj, oattr, MAY_WRITE)))
+                       RETURN(-EPERM);
+       }
+
        if (la->la_valid == LA_CTIME) {
                if (!(flags & MDS_PERM_BYPASS))
                        /* This is only for set ctime when rename's source is
@@ -704,17 +726,26 @@ static int mdd_fix_attr(const struct lu_env *env, struct mdd_object *obj,
                                (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL);
 
                if ((uc->uc_fsuid != oattr->la_uid) &&
-                   !md_capable(uc, CFS_CAP_FOWNER))
+                   !md_capable(uc, CAP_FOWNER))
                        RETURN(-EPERM);
 
                /* The IMMUTABLE and APPEND_ONLY flags can
                 * only be changed by the relevant capability. */
                if ((oldflags ^ newflags) &&
-                   !md_capable(uc, CFS_CAP_LINUX_IMMUTABLE))
+                   !md_capable(uc, CAP_LINUX_IMMUTABLE))
                        RETURN(-EPERM);
 
-               if (!S_ISDIR(oattr->la_mode))
+               if (!S_ISDIR(oattr->la_mode)) {
                        la->la_flags &= ~(LUSTRE_DIRSYNC_FL | LUSTRE_TOPDIR_FL);
+               } else if (la->la_flags & LUSTRE_ENCRYPT_FL) {
+                       /* when trying to add encryption flag on dir,
+                        * make sure it is empty
+                        */
+                       rc = mdd_dir_is_empty(env, obj);
+                       if (rc)
+                               RETURN(rc);
+                       rc = 0;
+               }
        }
 
        if (oattr->la_flags & (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL) &&
@@ -726,7 +757,7 @@ static int mdd_fix_attr(const struct lu_env *env, struct mdd_object *obj,
        if ((la->la_valid & (LA_MTIME | LA_ATIME | LA_CTIME)) &&
            !(la->la_valid & ~(LA_MTIME | LA_ATIME | LA_CTIME))) {
                if ((uc->uc_fsuid != oattr->la_uid) &&
-                   !md_capable(uc, CFS_CAP_FOWNER)) {
+                   !md_capable(uc, CAP_FOWNER)) {
                        rc = mdd_permission_internal(env, obj, oattr,
                                                     MAY_WRITE);
                        if (rc)
@@ -759,7 +790,7 @@ static int mdd_fix_attr(const struct lu_env *env, struct mdd_object *obj,
        if (la->la_valid & LA_MODE) {
                if (!(flags & MDS_PERM_BYPASS) &&
                    (uc->uc_fsuid != oattr->la_uid) &&
-                   !md_capable(uc, CFS_CAP_FOWNER))
+                   !md_capable(uc, CAP_FOWNER))
                        RETURN(-EPERM);
 
                if (la->la_mode == (umode_t) -1)
@@ -771,7 +802,7 @@ static int mdd_fix_attr(const struct lu_env *env, struct mdd_object *obj,
                /* Also check the setgid bit! */
                if (!lustre_in_group_p(uc, (la->la_valid & LA_GID) ?
                                       la->la_gid : oattr->la_gid) &&
-                   !md_capable(uc, CFS_CAP_FSETID))
+                   !md_capable(uc, CAP_FSETID))
                        la->la_mode &= ~S_ISGID;
        } else {
               la->la_mode = oattr->la_mode;
@@ -783,7 +814,7 @@ static int mdd_fix_attr(const struct lu_env *env, struct mdd_object *obj,
                        la->la_uid = oattr->la_uid;
                if (((uc->uc_fsuid != oattr->la_uid) ||
                     (la->la_uid != oattr->la_uid)) &&
-                   !md_capable(uc, CFS_CAP_CHOWN))
+                   !md_capable(uc, CAP_CHOWN))
                        RETURN(-EPERM);
 
                /* If the user or group of a non-directory has been
@@ -809,7 +840,7 @@ static int mdd_fix_attr(const struct lu_env *env, struct mdd_object *obj,
                if (((uc->uc_fsuid != oattr->la_uid) ||
                     ((la->la_gid != oattr->la_gid) &&
                      !lustre_in_group_p(uc, la->la_gid))) &&
-                   !md_capable(uc, CFS_CAP_CHOWN))
+                   !md_capable(uc, CAP_CHOWN))
                        RETURN(-EPERM);
 
                /* Likewise, if the user or group of a non-directory
@@ -839,8 +870,10 @@ static int mdd_fix_attr(const struct lu_env *env, struct mdd_object *obj,
        }
 
        if (la->la_valid & LA_CTIME) {
-               /* The pure setattr, it has the priority over what is
-                * already set, do not drop it if ctime is equal. */
+               /**
+                * The pure setattr, it has the priority over what is
+                * already set, do not drop it if ctime is equal.
+                */
                if (la->la_ctime < oattr->la_ctime)
                        la->la_valid &= ~(LA_ATIME | LA_MTIME | LA_CTIME);
        }
@@ -853,6 +886,7 @@ static int mdd_changelog_data_store_by_fid(const struct lu_env *env,
                                           enum changelog_rec_type type,
                                           enum changelog_rec_flags clf_flags,
                                           const struct lu_fid *fid,
+                                          const struct lu_fid *pfid,
                                           const char *xattr_name,
                                           struct thandle *handle)
 {
@@ -879,7 +913,7 @@ static int mdd_changelog_data_store_by_fid(const struct lu_env *env,
        reclen = llog_data_len(LLOG_CHANGELOG_HDR_SZ +
                               changelog_rec_offset(clf_flags & CLF_SUPPORTED,
                                                    xflags & CLFE_SUPPORTED));
-       buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
+       buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mdi_chlg_buf, reclen);
        if (buf->lb_buf == NULL)
                RETURN(-ENOMEM);
        rec = buf->lb_buf;
@@ -888,6 +922,8 @@ static int mdd_changelog_data_store_by_fid(const struct lu_env *env,
        rec->cr.cr_flags = clf_flags;
        rec->cr.cr_type = (__u32)type;
        rec->cr.cr_tfid = *fid;
+       if (pfid)
+               rec->cr.cr_pfid = *pfid;
        rec->cr.cr_namelen = 0;
 
        if (clf_flags & CLF_JOBID)
@@ -919,11 +955,13 @@ static int mdd_changelog_data_store_by_fid(const struct lu_env *env,
  * want the change to commit without the log entry.
  * \param mdd_obj - mdd_object of change
  * \param handle - transaction handle
+ * \param pfid - parent FID for CL_MTIME changelogs
  */
 int mdd_changelog_data_store(const struct lu_env *env, struct mdd_device *mdd,
                             enum changelog_rec_type type,
                             enum changelog_rec_flags clf_flags,
-                            struct mdd_object *mdd_obj, struct thandle *handle)
+                            struct mdd_object *mdd_obj, struct thandle *handle,
+                            const struct lu_fid *pfid)
 {
        int                              rc;
 
@@ -945,7 +983,8 @@ int mdd_changelog_data_store(const struct lu_env *env, struct mdd_device *mdd,
        }
 
        rc = mdd_changelog_data_store_by_fid(env, mdd, type, clf_flags,
-                                            mdo2fid(mdd_obj), NULL, handle);
+                                            mdd_object_fid(mdd_obj), pfid,
+                                            NULL, handle);
        if (rc == 0)
                mdd_obj->mod_cltime = ktime_get();
 
@@ -981,8 +1020,8 @@ int mdd_changelog_data_store_xattr(const struct lu_env *env,
        }
 
        rc = mdd_changelog_data_store_by_fid(env, mdd, type, clf_flags,
-                                            mdo2fid(mdd_obj), xattr_name,
-                                            handle);
+                                            mdd_object_fid(mdd_obj), NULL,
+                                            xattr_name, handle);
        if (rc == 0)
                mdd_obj->mod_cltime = ktime_get();
 
@@ -1020,7 +1059,7 @@ static int mdd_changelog(const struct lu_env *env, enum changelog_rec_type type,
                GOTO(stop, rc);
 
        rc = mdd_changelog_data_store_by_fid(env, mdd, type, clf_flags,
-                                            fid, NULL, handle);
+                                            fid, NULL, NULL, handle);
 
 stop:
        rc = mdd_trans_stop(env, mdd, rc, handle);
@@ -1041,19 +1080,19 @@ stop:
  * atime and ctime are independent.) */
 static int mdd_attr_set_changelog(const struct lu_env *env,
                                   struct md_object *obj, struct thandle *handle,
-                                  __u64 valid)
+                                 const struct lu_fid *pfid, __u64 valid)
 {
        struct mdd_device *mdd = mdo2mdd(obj);
        int bits, type = 0;
 
-       bits =  (valid & LA_SIZE)  ? 1 << CL_TRUNC : 0;
-       bits |= (valid & ~(LA_CTIME|LA_MTIME|LA_ATIME)) ? 1 << CL_SETATTR : 0;
-       bits |= (valid & LA_MTIME) ? 1 << CL_MTIME : 0;
-       bits |= (valid & LA_CTIME) ? 1 << CL_CTIME : 0;
-       bits |= (valid & LA_ATIME) ? 1 << CL_ATIME : 0;
-       bits = bits & mdd->mdd_cl.mc_mask;
+       bits =  (valid & LA_SIZE)  ? BIT(CL_TRUNC) : 0;
+       bits |= (valid & ~(LA_CTIME|LA_MTIME|LA_ATIME)) ? BIT(CL_SETATTR) : 0;
+       bits |= (valid & LA_MTIME) ? BIT(CL_MTIME) : 0;
+       bits |= (valid & LA_CTIME) ? BIT(CL_CTIME) : 0;
+       bits |= (valid & LA_ATIME) ? BIT(CL_ATIME) : 0;
+       bits = bits & mdd->mdd_cl.mc_current_mask;
        /* This is an implementation limit rather than a protocol limit */
-       CLASSERT(CL_LAST <= sizeof(int) * 8);
+       BUILD_BUG_ON(CL_LAST > sizeof(int) * 8);
        if (bits == 0)
                return 0;
 
@@ -1061,8 +1100,8 @@ static int mdd_attr_set_changelog(const struct lu_env *env,
        type = __ffs(bits);
 
        /* XXX: we only store the low CLF_FLAGMASK bits of la_valid */
-       return mdd_changelog_data_store(env, mdd, type, valid,
-                                       md2mdd_obj(obj), handle);
+       return mdd_changelog_data_store(env, mdd, type, valid, md2mdd_obj(obj),
+                                       handle, pfid);
 }
 
 static int mdd_declare_attr_set(const struct lu_env *env,
@@ -1174,11 +1213,13 @@ int mdd_attr_set(const struct lu_env *env, struct md_object *obj,
        struct mdd_object *mdd_obj = md2mdd_obj(obj);
        struct mdd_device *mdd = mdo2mdd(obj);
        struct thandle *handle = NULL;
-       struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
+       struct lu_attr *la_copy = &mdd_env_info(env)->mdi_la_for_fix;
        struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
        const struct lu_attr *la = &ma->ma_attr;
        struct lu_ucred  *uc;
+       bool quota_reserved = false;
        bool chrgrp_by_unprivileged_user = false;
+       __s64 quota_size = 0;
        int rc;
        ENTRY;
 
@@ -1191,15 +1232,16 @@ int mdd_attr_set(const struct lu_env *env, struct md_object *obj,
                RETURN(rc);
 
        *la_copy = ma->ma_attr;
-       rc = mdd_fix_attr(env, mdd_obj, attr, la_copy, ma->ma_attr_flags);
+       rc = mdd_fix_attr(env, mdd_obj, attr, la_copy, ma);
        if (rc)
                RETURN(rc);
 
        /* no need to setattr anymore */
        if (la_copy->la_valid == 0) {
-               CDEBUG(D_INODE, "%s: no valid attribute on "DFID", previous"
-                      "valid is %#llx\n", mdd2obd_dev(mdd)->obd_name,
-                      PFID(mdo2fid(mdd_obj)), la->la_valid);
+               CDEBUG(D_INODE,
+                      "%s: no valid attribute on "DFID", previous was %#llx\n",
+                      mdd_obj_dev_name(mdd_obj),
+                      PFID(mdd_object_fid(mdd_obj)), la->la_valid);
 
                RETURN(0);
        }
@@ -1210,11 +1252,37 @@ int mdd_attr_set(const struct lu_env *env, struct md_object *obj,
        uc = lu_ucred_check(env);
        if (S_ISREG(attr->la_mode) && la->la_valid & LA_GID &&
            la->la_gid != attr->la_gid && uc != NULL && uc->uc_fsuid != 0) {
-               /* LU-10048: disable synchronous chgrp operation for it will
-                * cause deadlock between MDT and OST.
-               la_copy->la_valid |= LA_FLAGS;
-               la_copy->la_flags |= LUSTRE_SET_SYNC_FL;
-                */
+               CDEBUG(D_QUOTA, "%s: reserve quota for changing group: gid=%u size=%llu\n",
+                      mdd2obd_dev(mdd)->obd_name, la->la_gid, la->la_size);
+
+               if (la->la_valid & LA_BLOCKS)
+                       quota_size = la->la_blocks << 9;
+               else if (la->la_valid & LA_SIZE)
+                       quota_size = la->la_size;
+               /* use local attr gotten above */
+               else if (attr->la_valid & LA_BLOCKS)
+                       quota_size = attr->la_blocks << 9;
+               else if (attr->la_valid & LA_SIZE)
+                       quota_size = attr->la_size;
+
+               if (quota_size > 0) {
+                       rc = dt_reserve_or_free_quota(env, mdd->mdd_bottom,
+                                                     GRPQUOTA, attr->la_uid,
+                                                     la->la_gid, quota_size,
+                                                     false);
+
+                       if (rc) {
+                               CDEBUG(D_QUOTA, "%s: failed to reserve quota for gid %d size %llu\n",
+                                      mdd2obd_dev(mdd)->obd_name,
+                                      la->la_gid, quota_size);
+
+                               GOTO(out, rc);
+                       }
+
+                       quota_reserved = true;
+                       la_copy->la_valid |= LA_FLAGS;
+               }
+
                chrgrp_by_unprivileged_user = true;
 
                /* Flush the possible existing client setattr requests to OSTs
@@ -1287,9 +1355,24 @@ int mdd_attr_set(const struct lu_env *env, struct md_object *obj,
 
 out:
        if (rc == 0)
-               rc = mdd_attr_set_changelog(env, obj, handle,
+               rc = mdd_attr_set_changelog(env, obj, handle, &ma->ma_pfid,
                                            la_copy->la_valid);
 
+       if (rc == 0 && quota_reserved) {
+               struct thandle *sub_th;
+
+               sub_th = thandle_get_sub_by_dt(env, handle, mdd->mdd_bottom);
+               if (unlikely(IS_ERR(sub_th))) {
+                       dt_reserve_or_free_quota(env, mdd->mdd_bottom, GRPQUOTA,
+                                                attr->la_uid, la->la_gid,
+                                                -quota_size, false);
+               } else {
+                       sub_th->th_reserved_quota.qrr_type = GRPQUOTA;
+                       sub_th->th_reserved_quota.qrr_id.qid_gid = la->la_gid;
+                       sub_th->th_reserved_quota.qrr_count = quota_size;
+               }
+       }
+
        if (handle != NULL)
                rc = mdd_trans_stop(env, mdd, rc, handle);
 
@@ -1313,11 +1396,11 @@ static int mdd_xattr_sanity_check(const struct lu_env *env,
                 * can write attributes. */
                if (S_ISDIR(attr->la_mode) && (attr->la_mode & S_ISVTX) &&
                    (uc->uc_fsuid != attr->la_uid) &&
-                   !md_capable(uc, CFS_CAP_FOWNER))
+                   !md_capable(uc, CAP_FOWNER))
                        RETURN(-EPERM);
        } else if (strcmp(name, XATTR_NAME_SOM) != 0 &&
                   (uc->uc_fsuid != attr->la_uid) &&
-                  !md_capable(uc, CFS_CAP_FOWNER)) {
+                  !md_capable(uc, CAP_FOWNER)) {
                RETURN(-EPERM);
        }
 
@@ -1341,7 +1424,7 @@ static inline bool has_prefix(const char *str, const char *prefix)
  *
  * \param[in]  xattr_name  Full extended attribute name.
  *
- * \return The type of changelog to use, or -1 if no changelog is to be emitted.
+ * \return type of changelog to use, or CL_NONE if no changelog is to be emitted
  */
 static enum changelog_rec_type
 mdd_xattr_changelog_type(const struct lu_env *env, struct mdd_device *mdd,
@@ -1349,8 +1432,8 @@ mdd_xattr_changelog_type(const struct lu_env *env, struct mdd_device *mdd,
 {
        /* Layout changes systematically recorded */
        if (strcmp(XATTR_NAME_LOV, xattr_name) == 0 ||
-           strncmp(XATTR_LUSTRE_LOV, xattr_name,
-                   strlen(XATTR_LUSTRE_LOV)) == 0)
+           strcmp(XATTR_LUSTRE_LOV, xattr_name) == 0 ||
+           allowed_lustre_lov(xattr_name))
                return CL_LAYOUT;
 
        /* HSM information changes systematically recorded */
@@ -1420,8 +1503,9 @@ static int mdd_hsm_update_locked(const struct lu_env *env,
                RETURN(-ENOMEM);
 
        /* Read HSM attrs from disk */
-       current_buf = lu_buf_check_and_alloc(&info->mti_xattr_buf,
-                       MIN(mdd_obj2mdd_dev(mdd_obj)->mdd_dt_conf.ddp_max_ea_size,
+       current_buf = lu_buf_check_and_alloc(&info->mdi_xattr_buf,
+                       min_t(unsigned int,
+                             mdd_obj2mdd_dev(mdd_obj)->mdd_dt_conf.ddp_max_ea_size,
                            XATTR_SIZE_MAX));
        rc = mdo_xattr_get(env, mdd_obj, current_buf, XATTR_NAME_HSM);
        rc = lustre_buf2hsm(current_buf->lb_buf, rc, current_mh);
@@ -1501,64 +1585,64 @@ static int mdd_xattr_merge(const struct lu_env *env, struct md_object *md_obj,
        struct mdd_device *mdd = mdo2mdd(md_obj);
        struct mdd_object *obj = md2mdd_obj(md_obj);
        struct mdd_object *vic = md2mdd_obj(md_vic);
-       struct lu_buf *buf = &mdd_env_info(env)->mti_buf[0];
-       struct lu_buf *buf_vic = &mdd_env_info(env)->mti_buf[1];
+       struct lu_buf *buf = &mdd_env_info(env)->mdi_buf[0];
+       struct lu_buf *buf_vic = &mdd_env_info(env)->mdi_buf[1];
        struct lov_mds_md *lmm;
        struct thandle *handle;
-       int rc;
+       int rc, lock_order;
        ENTRY;
 
-       rc = lu_fid_cmp(mdo2fid(obj), mdo2fid(vic));
-       if (rc == 0) /* same fid */
+       lock_order = lu_fid_cmp(mdd_object_fid(obj), mdd_object_fid(vic));
+       if (lock_order == 0) /* same fid */
                RETURN(-EPERM);
 
        handle = mdd_trans_create(env, mdd);
        if (IS_ERR(handle))
                RETURN(PTR_ERR(handle));
 
-       if (rc > 0) {
-               mdd_write_lock(env, obj, DT_TGT_CHILD);
-               mdd_write_lock(env, vic, DT_TGT_CHILD);
-       } else {
-               mdd_write_lock(env, vic, DT_TGT_CHILD);
-               mdd_write_lock(env, obj, DT_TGT_CHILD);
-       }
-
        /* get EA of victim file */
        memset(buf_vic, 0, sizeof(*buf_vic));
        rc = mdd_stripe_get(env, vic, buf_vic, XATTR_NAME_LOV);
        if (rc < 0) {
                if (rc == -ENODATA)
                        rc = 0;
-               GOTO(out, rc);
+               GOTO(stop, rc);
        }
 
        /* parse the layout of victim file */
        lmm = buf_vic->lb_buf;
        if (le32_to_cpu(lmm->lmm_magic) != LOV_MAGIC_COMP_V1)
-               GOTO(out, rc = -EINVAL);
+               GOTO(stop, rc = -EINVAL);
 
        /* save EA of target file for restore */
        memset(buf, 0, sizeof(*buf));
        rc = mdd_stripe_get(env, obj, buf, XATTR_NAME_LOV);
        if (rc < 0)
-               GOTO(out, rc);
+               GOTO(stop, rc);
 
        /* Get rid of the layout from victim object */
        rc = mdd_declare_xattr_del(env, mdd, vic, XATTR_NAME_LOV, handle);
        if (rc)
-               GOTO(out, rc);
+               GOTO(stop, rc);
 
-       rc = mdd_declare_xattr_set(env, mdd, obj, buf_vic, XATTR_LUSTRE_LOV,
+       rc = mdd_declare_xattr_set(env, mdd, obj, buf_vic, XATTR_NAME_LOV,
                                   LU_XATTR_MERGE, handle);
        if (rc)
-               GOTO(out, rc);
+               GOTO(stop, rc);
 
        rc = mdd_trans_start(env, mdd, handle);
        if (rc != 0)
-               GOTO(out, rc);
+               GOTO(stop, rc);
 
-       rc = mdo_xattr_set(env, obj, buf_vic, XATTR_LUSTRE_LOV, LU_XATTR_MERGE,
+       if (lock_order > 0) {
+               mdd_write_lock(env, obj, DT_TGT_CHILD);
+               mdd_write_lock(env, vic, DT_TGT_CHILD);
+       } else {
+               mdd_write_lock(env, vic, DT_TGT_CHILD);
+               mdd_write_lock(env, obj, DT_TGT_CHILD);
+       }
+
+       rc = mdo_xattr_set(env, obj, buf_vic, XATTR_NAME_LOV, LU_XATTR_MERGE,
                           handle);
        if (rc)
                GOTO(out, rc);
@@ -1567,8 +1651,10 @@ static int mdd_xattr_merge(const struct lu_env *env, struct md_object *md_obj,
        if (rc) /* wtf? */
                GOTO(out_restore, rc);
 
-       (void)mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, obj, handle);
-       (void)mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, vic, handle);
+       (void)mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, obj, handle,
+                                      NULL);
+       (void)mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, vic, handle,
+                                      NULL);
        EXIT;
 
 out_restore:
@@ -1576,15 +1662,16 @@ out_restore:
                int rc2 = mdo_xattr_set(env, obj, buf, XATTR_NAME_LOV,
                                        LU_XATTR_REPLACE, handle);
                if (rc2)
-                       CERROR("%s: failed to rollback of layout of: "DFID
-                              ": %d, file state unknown\n",
-                              mdd_obj_dev_name(obj), PFID(mdo2fid(obj)), rc2);
+                       CERROR("%s: failed rollback of "DFID" layout: file state unknown: rc = %d\n",
+                              mdd_obj_dev_name(obj),
+                              PFID(mdd_object_fid(obj)), rc2);
        }
 
 out:
-       mdd_trans_stop(env, mdd, rc, handle);
        mdd_write_unlock(env, obj);
        mdd_write_unlock(env, vic);
+stop:
+       mdd_trans_stop(env, mdd, rc, handle);
        lu_buf_free(buf);
        lu_buf_free(buf_vic);
 
@@ -1728,10 +1815,10 @@ static int mdd_xattr_split(const struct lu_env *env, struct md_object *md_obj,
 {
        struct mdd_device *mdd = mdo2mdd(md_obj);
        struct mdd_object *obj = md2mdd_obj(md_obj);
-       struct mdd_object *vic = md2mdd_obj(mrd->mrd_obj);
-       struct lu_buf *buf = &mdd_env_info(env)->mti_buf[0];
-       struct lu_buf *buf_save = &mdd_env_info(env)->mti_buf[1];
-       struct lu_buf *buf_vic = &mdd_env_info(env)->mti_buf[2];
+       struct mdd_object *vic = NULL;
+       struct lu_buf *buf = &mdd_env_info(env)->mdi_buf[0];
+       struct lu_buf *buf_save = &mdd_env_info(env)->mdi_buf[1];
+       struct lu_buf *buf_vic = &mdd_env_info(env)->mdi_buf[2];
        struct lov_comp_md_v1 *lcm;
        struct thandle *handle;
        int rc;
@@ -1739,95 +1826,156 @@ static int mdd_xattr_split(const struct lu_env *env, struct md_object *md_obj,
 
        ENTRY;
 
-       rc = lu_fid_cmp(mdo2fid(obj), mdo2fid(vic));
-       if (rc == 0) /* same fid */
-               RETURN(-EPERM);
+       /**
+        * NULL @mrd_obj means mirror deleting, and use NULL vic to indicate
+        * mirror deleting
+        */
+       if (mrd->mrd_obj)
+               vic = md2mdd_obj(mrd->mrd_obj);
 
        handle = mdd_trans_create(env, mdd);
        if (IS_ERR(handle))
                RETURN(PTR_ERR(handle));
 
-       if (rc > 0) {
-               mdd_write_lock(env, obj, DT_TGT_CHILD);
-               mdd_write_lock(env, vic, DT_TGT_CHILD);
-       } else {
-               mdd_write_lock(env, vic, DT_TGT_CHILD);
-               mdd_write_lock(env, obj, DT_TGT_CHILD);
-       }
-
        /* get EA of mirrored file */
        memset(buf_save, 0, sizeof(*buf));
        rc = mdd_stripe_get(env, obj, buf_save, XATTR_NAME_LOV);
        if (rc < 0)
-               GOTO(out, rc);
+               GOTO(stop, rc);
 
        lcm = buf_save->lb_buf;
        if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
-               GOTO(out, rc = -EINVAL);
+               GOTO(stop, rc = -EINVAL);
 
        /**
         * Extract the mirror with specified mirror id, and store the splitted
-        * mirror layout to the victim file.
+        * mirror layout to the victim buffer.
         */
        memset(buf, 0, sizeof(*buf));
        memset(buf_vic, 0, sizeof(*buf_vic));
        rc = mdd_split_ea(lcm, mrd->mrd_mirror_id, buf, buf_vic);
        if (rc < 0)
-               GOTO(out, rc);
+               GOTO(stop, rc);
+       /**
+        * @buf stores layout w/o the specified mirror, @buf_vic stores the
+        * splitted mirror
+        */
 
        dom_stripe = mdd_lmm_dom_size(buf_vic->lb_buf) > 0;
 
-       rc = mdd_declare_xattr_set(env, mdd, obj, buf, XATTR_NAME_LOV,
-                                  LU_XATTR_SPLIT, handle);
-       if (rc)
-               GOTO(out, rc);
-       rc = mdd_declare_xattr_set(env, mdd, vic, buf_vic, XATTR_NAME_LOV,
-                                  LU_XATTR_SPLIT, handle);
-       if (rc)
-               GOTO(out, rc);
+       if (vic) {
+               /**
+                * non delete mirror split
+                *
+                * declare obj set remaining layout in @buf, will set obj's
+                * in-memory layout
+                */
+               rc = mdd_declare_xattr_set(env, mdd, obj, buf, XATTR_NAME_LOV,
+                                          LU_XATTR_SPLIT, handle);
+               if (rc)
+                       GOTO(stop, rc);
+
+               /* declare vic set splitted layout in @buf_vic */
+               rc = mdd_declare_xattr_set(env, mdd, vic, buf_vic,
+                                          XATTR_NAME_LOV, LU_XATTR_SPLIT,
+                                          handle);
+               if (rc)
+                       GOTO(stop, rc);
+       } else {
+               /**
+                * declare delete mirror objects in @buf_vic, will change obj's
+                * in-memory layout
+                */
+               rc = mdd_declare_xattr_set(env, mdd, obj, buf_vic,
+                                          XATTR_NAME_LOV, LU_XATTR_PURGE,
+                                          handle);
+               if (rc)
+                       GOTO(stop, rc);
+
+               /* declare obj set remaining layout in @buf */
+               rc = mdd_declare_xattr_set(env, mdd, obj, buf,
+                                          XATTR_NAME_LOV, LU_XATTR_SPLIT,
+                                          handle);
+               if (rc)
+                       GOTO(stop, rc);
+       }
 
        rc = mdd_trans_start(env, mdd, handle);
        if (rc)
-               GOTO(out, rc);
+               GOTO(stop, rc);
 
-       rc = mdo_xattr_set(env, obj, buf, XATTR_NAME_LOV, LU_XATTR_REPLACE,
-                          handle);
-       if (rc)
-               GOTO(out, rc);
+       if (vic) {
+               /* don't use the same file to save the splitted mirror */
+               rc = lu_fid_cmp(mdd_object_fid(obj), mdd_object_fid(vic));
+               if (rc == 0)
+                       GOTO(stop, rc = -EPERM);
 
-       rc = mdo_xattr_set(env, vic, buf_vic, XATTR_NAME_LOV, LU_XATTR_CREATE,
+               if (rc > 0) {
+                       mdd_write_lock(env, obj, DT_TGT_CHILD);
+                       mdd_write_lock(env, vic, DT_TGT_CHILD);
+               } else {
+                       mdd_write_lock(env, vic, DT_TGT_CHILD);
+                       mdd_write_lock(env, obj, DT_TGT_CHILD);
+               }
+       } else {
+               mdd_write_lock(env, obj, DT_TGT_CHILD);
+       }
+
+       /* set obj's layout in @buf */
+       rc = mdo_xattr_set(env, obj, buf, XATTR_NAME_LOV, LU_XATTR_SPLIT,
                           handle);
        if (rc)
-               GOTO(out_restore, rc);
+               GOTO(unlock, rc);
 
-       rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, obj, handle);
-       if (rc)
-               GOTO(out, rc);
+       if (vic) {
+               /* set vic's layout in @buf_vic */
+               rc = mdo_xattr_set(env, vic, buf_vic, XATTR_NAME_LOV,
+                                  LU_XATTR_CREATE, handle);
+               if (rc)
+                       GOTO(out_restore, rc);
+       } else {
+               /* delete mirror objects */
+               rc = mdo_xattr_set(env, obj, buf_vic, XATTR_NAME_LOV,
+                                  LU_XATTR_PURGE, handle);
+               if (rc)
+                       GOTO(out_restore, rc);
+       }
 
-       rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, vic, handle);
+       rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, obj, handle,
+                                     NULL);
        if (rc)
-               GOTO(out, rc);
-       EXIT;
+               GOTO(out_restore, rc);
+
+       if (vic) {
+               rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, vic,
+                                             handle, NULL);
+               if (rc)
+                       GOTO(out_restore, rc);
+       }
 
 out_restore:
        if (rc) {
-               /* restore obj's layout */
+               /* restore obj's in-memory and on-disk layout */
                int rc2 = mdo_xattr_set(env, obj, buf_save, XATTR_NAME_LOV,
                                        LU_XATTR_REPLACE, handle);
                if (rc2)
-                       CERROR("%s: failed to rollback of layout of: "DFID
-                              ": %d, file state unkonwn.\n",
-                              mdd_obj_dev_name(obj), PFID(mdo2fid(obj)), rc2);
+                       CERROR("%s: failed rollback "DFID
+                              " layout: file state unknown: rc = %d\n",
+                              mdd_obj_dev_name(obj),
+                              PFID(mdd_object_fid(obj)), rc);
        }
-out:
+
+unlock:
+       mdd_write_unlock(env, obj);
+       if (vic)
+               mdd_write_unlock(env, vic);
+stop:
        rc = mdd_trans_stop(env, mdd, rc, handle);
 
        /* Truncate local DOM data if all went well */
        if (!rc && dom_stripe)
                mdd_dom_data_truncate(env, mdd, obj);
 
-       mdd_write_unlock(env, obj);
-       mdd_write_unlock(env, vic);
        lu_buf_free(buf_save);
        lu_buf_free(buf);
        lu_buf_free(buf_vic);
@@ -1887,20 +2035,16 @@ static int mdd_xattr_set(const struct lu_env *env, struct md_object *obj,
                if (buf->lb_len != sizeof(*mrd))
                        RETURN(-EINVAL);
 
-               rc = mdd_layout_merge_allowed(env, obj, victim);
-               if (rc)
-                       RETURN(rc);
 
-               if (fl == LU_XATTR_MERGE)
+               if (fl == LU_XATTR_MERGE) {
+                       rc = mdd_layout_merge_allowed(env, obj, victim);
+                       if (rc)
+                               RETURN(rc);
                        /* merge layout of victim as a mirror of obj's. */
                        rc = mdd_xattr_merge(env, obj, victim);
-               else
+               } else {
                        rc = mdd_xattr_split(env, obj, mrd);
-               RETURN(rc);
-       }
-
-       if (strcmp(name, XATTR_NAME_LMV) == 0) {
-               rc = mdd_dir_layout_shrink(env, obj, buf);
+               }
                RETURN(rc);
        }
 
@@ -2043,7 +2187,7 @@ stop:
 int mdd_stripe_get(const struct lu_env *env, struct mdd_object *obj,
                   struct lu_buf *lmm_buf, const char *name)
 {
-       struct lu_buf *buf = &mdd_env_info(env)->mti_big_buf;
+       struct lu_buf *buf = &mdd_env_info(env)->mdi_big_buf;
        int rc;
 
        ENTRY;
@@ -2057,9 +2201,9 @@ int mdd_stripe_get(const struct lu_env *env, struct mdd_object *obj,
 repeat:
        rc = mdo_xattr_get(env, obj, buf, name);
        if (rc == -ERANGE) {
-               /* mti_big_buf is allocated but is too small
+               /* mdi_big_buf is allocated but is too small
                 * we need to increase it */
-               buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf,
+               buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mdi_big_buf,
                                             buf->lb_len * 2);
                if (buf->lb_buf == NULL)
                        RETURN(-ENOMEM);
@@ -2076,7 +2220,7 @@ repeat:
 
        /*
         * we don't use lmm_buf directly, because we don't know xattr size, so
-        * by using mti_big_buf we can avoid calling mdo_xattr_get() twice.
+        * by using mdi_big_buf we can avoid calling mdo_xattr_get() twice.
         */
        memcpy(lmm_buf->lb_buf, buf->lb_buf, rc);
 
@@ -2106,7 +2250,7 @@ static int mdd_xattr_hsm_replace(const struct lu_env *env,
        /* Add a changelog record for release. */
        hsm_set_cl_event(&clf_flags, HE_RELEASE);
        rc = mdd_changelog_data_store(env, mdo2mdd(&o->mod_obj), CL_HSM,
-                                     clf_flags, o, handle);
+                                     clf_flags, o, handle, NULL);
        RETURN(rc);
 }
 
@@ -2125,11 +2269,11 @@ static int mdd_layout_swap_allowed(const struct lu_env *env,
                                   const struct lu_attr *attr2,
                                   __u64 flags)
 {
-       const struct lu_fid     *fid1, *fid2;
+       const struct lu_fid *fid1, *fid2;
        ENTRY;
 
-       fid1 = mdo2fid(o1);
-       fid2 = mdo2fid(o2);
+       fid1 = mdd_object_fid(o1);
+       fid2 = mdd_object_fid(o2);
 
        if (!fid_is_norm(fid1) &&
            (!fid_is_igif(fid1) || IS_ERR(mdd_links_get(env, o1))))
@@ -2270,8 +2414,8 @@ stop:
 out:
        /* Ignore failure but report the error */
        if (rc)
-               CERROR("%s: "DFID" can't truncate DOM inode data, rc = %d\n",
-                      mdd_obj_dev_name(mo), PFID(mdo2fid(mo)), rc);
+               CERROR("%s: can't truncate DOM inode "DFID" data: rc = %d\n",
+                      mdd_obj_dev_name(mo), PFID(mdd_object_fid(mo)), rc);
        return rc;
 }
 
@@ -2288,10 +2432,10 @@ static int mdd_swap_layouts(const struct lu_env *env, struct md_object *obj1,
        struct lu_attr *snd_la = MDD_ENV_VAR(env, tattr);
        struct mdd_device *mdd = mdo2mdd(obj1);
        struct lov_mds_md *fst_lmm, *snd_lmm;
-       struct lu_buf *fst_buf = &info->mti_buf[0];
-       struct lu_buf *snd_buf = &info->mti_buf[1];
-       struct lu_buf *fst_hsm_buf = &info->mti_buf[2];
-       struct lu_buf *snd_hsm_buf = &info->mti_buf[3];
+       struct lu_buf *fst_buf = &info->mdi_buf[0];
+       struct lu_buf *snd_buf = &info->mdi_buf[1];
+       struct lu_buf *fst_hsm_buf = &info->mdi_buf[2];
+       struct lu_buf *snd_hsm_buf = &info->mdi_buf[3];
        struct ost_id *saved_oi = NULL;
        struct thandle *handle;
        struct mdd_object *dom_o = NULL;
@@ -2302,12 +2446,12 @@ static int mdd_swap_layouts(const struct lu_env *env, struct md_object *obj1,
 
        ENTRY;
 
-       CLASSERT(ARRAY_SIZE(info->mti_buf) >= 4);
-       memset(info->mti_buf, 0, sizeof(info->mti_buf));
+       BUILD_BUG_ON(ARRAY_SIZE(info->mdi_buf) < 4);
+       memset(info->mdi_buf, 0, sizeof(info->mdi_buf));
 
        /* we have to sort the 2 obj, so locking will always
         * be in the same order, even in case of 2 concurrent swaps */
-       rc = lu_fid_cmp(mdo2fid(fst_o), mdo2fid(snd_o));
+       rc = lu_fid_cmp(mdd_object_fid(fst_o), mdd_object_fid(snd_o));
        if (rc == 0) /* same fid ? */
                RETURN(-EPERM);
 
@@ -2372,15 +2516,17 @@ static int mdd_swap_layouts(const struct lu_env *env, struct md_object *obj1,
 
        if (domsize_vlt > 0 && domsize_dom == 0) {
                rc = -EOPNOTSUPP;
-               CDEBUG(D_LAYOUT, "cannot swap layout for "DFID": OST to DOM "
-                      "migration is not supported: rc = %d\n",
-                      PFID(mdo2fid(snd_o)), rc);
+               CDEBUG(D_LAYOUT,
+                      "%s: cannot swap "DFID" layout: OST to DOM migration not supported: rc = %d\n",
+                      mdd_obj_dev_name(snd_o),
+                      PFID(mdd_object_fid(snd_o)), rc);
                GOTO(stop, rc);
        } else if (domsize_vlt > 0 && domsize_dom != domsize_vlt) {
                rc = -EOPNOTSUPP;
-               CDEBUG(D_LAYOUT, "cannot swap layout for "DFID": new layout "
-                      "must have the same DoM component size: rc = %d\n",
-                      PFID(mdo2fid(fst_o)), rc);
+               CDEBUG(D_LAYOUT,
+                      "%s: cannot swap "DFID" layout: new layout must have same DoM component size: rc = %d\n",
+                      mdd_obj_dev_name(fst_o),
+                      PFID(mdd_object_fid(fst_o)), rc);
                GOTO(stop, rc);
        } else if (domsize_vlt > 0) {
                /* Migration with the same DOM component size, no need to
@@ -2438,10 +2584,13 @@ static int mdd_swap_layouts(const struct lu_env *env, struct md_object *obj1,
 
        /* set the file specific informations in lmm */
        if (fst_lmm != NULL) {
-               saved_oi = &info->mti_oa.o_oi;
+               struct ost_id temp_oi;
+
+               saved_oi = &info->mdi_oa.o_oi;
                mdd_get_lmm_oi(fst_lmm, saved_oi);
+               mdd_get_lmm_oi(snd_lmm, &temp_oi);
                mdd_set_lmm_gen(fst_lmm, &snd_gen);
-               mdd_set_lmm_oi(fst_lmm, &snd_lmm->lmm_oi);
+               mdd_set_lmm_oi(fst_lmm, &temp_oi);
                mdd_set_lmm_oi(snd_lmm, saved_oi);
        } else {
                if ((snd_lmm->lmm_magic & cpu_to_le32(LOV_MAGIC_MASK)) ==
@@ -2513,9 +2662,9 @@ static int mdd_swap_layouts(const struct lu_env *env, struct md_object *obj1,
                        rc2 = mdd_xattr_hsm_replace(env, fst_o, fst_hsm_buf,
                                                    handle);
                        if (rc2 < 0)
-                               CERROR("%s: restore "DFID" HSM error: %d/%d\n",
+                               CERROR("%s: HSM error restoring "DFID": rc = %d/%d\n",
                                       mdd_obj_dev_name(fst_o),
-                                      PFID(mdo2fid(fst_o)), rc, rc2);
+                                      PFID(mdd_object_fid(fst_o)), rc, rc2);
                        GOTO(stop, rc);
                }
        }
@@ -2537,11 +2686,13 @@ static int mdd_swap_layouts(const struct lu_env *env, struct md_object *obj1,
                GOTO(out_restore, rc);
 
        /* Issue one changelog record per file */
-       rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, fst_o, handle);
+       rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, fst_o, handle,
+                                     NULL);
        if (rc)
                GOTO(stop, rc);
 
-       rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, snd_o, handle);
+       rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, snd_o, handle,
+                                     NULL);
        if (rc)
                GOTO(stop, rc);
        EXIT;
@@ -2563,21 +2714,25 @@ out_restore:
                if (rc2 < 0)
                        goto do_lbug;
 
-               ++steps;
-               rc2 = mdd_xattr_hsm_replace(env, fst_o, fst_hsm_buf, handle);
-               if (rc2 < 0)
-                       goto do_lbug;
+               if (flags & SWAP_LAYOUTS_MDS_HSM) {
+                       ++steps;
+                       rc2 = mdd_xattr_hsm_replace(env, fst_o, fst_hsm_buf,
+                                                   handle);
+                       if (rc2 < 0)
+                               goto do_lbug;
 
-               ++steps;
-               rc2 = mdd_xattr_hsm_replace(env, snd_o, snd_hsm_buf, handle);
+                       ++steps;
+                       rc2 = mdd_xattr_hsm_replace(env, snd_o, snd_hsm_buf,
+                                                   handle);
+               }
 
        do_lbug:
                if (rc2 < 0) {
                        /* very bad day */
-                       CERROR("%s: unable to roll back layout swap. FIDs: "
-                              DFID" and "DFID "error: %d/%d, steps: %d\n",
+                       CERROR("%s: unable to roll back layout swap of "DFID" and "DFID", steps: %d: rc = %d/%d\n",
                               mdd_obj_dev_name(fst_o),
-                              PFID(mdo2fid(snd_o)), PFID(mdo2fid(fst_o)),
+                              PFID(mdd_object_fid(snd_o)),
+                              PFID(mdd_object_fid(fst_o)),
                               rc, rc2, steps);
                        /* a solution to avoid journal commit is to panic,
                         * but it has strong consequences so we use LBUG to
@@ -2657,7 +2812,8 @@ mdd_layout_instantiate_component(const struct lu_env *env,
        if (rc)
                RETURN(rc);
 
-       rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, obj, handle);
+       rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, obj, handle,
+                                     NULL);
        RETURN(rc);
 }
 
@@ -2675,7 +2831,7 @@ mdd_layout_update_rdonly(const struct lu_env *env, struct mdd_object *obj,
                         struct md_layout_change *mlc, struct thandle *handle)
 {
        struct mdd_device *mdd = mdd_obj2mdd_dev(obj);
-       struct lu_buf *som_buf = &mdd_env_info(env)->mti_buf[1];
+       struct lu_buf *som_buf = &mdd_env_info(env)->mdi_buf[1];
        struct lustre_som_attrs *som = &mlc->mlc_som;
        int fl = 0;
        int rc;
@@ -2703,6 +2859,12 @@ mdd_layout_update_rdonly(const struct lu_env *env, struct mdd_object *obj,
                lustre_som_swab(som);
                if (som->lsa_valid & SOM_FL_STRICT)
                        fl = LU_XATTR_REPLACE;
+
+               if (mlc->mlc_opc == MD_LAYOUT_WRITE &&
+                   mlc->mlc_intent->li_extent.e_end > som->lsa_size) {
+                       som->lsa_size = mlc->mlc_intent->li_extent.e_end + 1;
+                       fl = LU_XATTR_REPLACE;
+               }
        }
 
        rc = mdd_declare_layout_change(env, mdd, obj, mlc, handle);
@@ -2741,7 +2903,7 @@ mdd_layout_update_rdonly(const struct lu_env *env, struct mdd_object *obj,
        if (rc)
                GOTO(out, rc);
 
-       rc = mdd_changelog_data_store(env, mdd, CL_FLRW, 0, obj, handle);
+       rc = mdd_changelog_data_store(env, mdd, CL_FLRW, 0, obj, handle, NULL);
        if (rc)
                GOTO(out, rc);
 
@@ -2765,6 +2927,9 @@ mdd_layout_update_write_pending(const struct lu_env *env,
                struct thandle *handle)
 {
        struct mdd_device *mdd = mdd_obj2mdd_dev(obj);
+       struct lu_buf *som_buf = &mdd_env_info(env)->mdi_buf[1];
+       struct lustre_som_attrs *som = &mlc->mlc_som;
+       int fl = 0;
        int rc;
        ENTRY;
 
@@ -2777,8 +2942,25 @@ mdd_layout_update_write_pending(const struct lu_env *env,
                 * resync state. */
                break;
        case MD_LAYOUT_WRITE:
-               /* legal race for concurrent write, the file state has been
-                * changed by another client. */
+               /**
+                * legal race for concurrent write, the file state has been
+                * changed by another client. Or a jump over file size and
+                * write.
+                */
+               som_buf->lb_buf = som;
+               som_buf->lb_len = sizeof(*som);
+               rc = mdo_xattr_get(env, obj, som_buf, XATTR_NAME_SOM);
+               if (rc < 0 && rc != -ENODATA)
+                       RETURN(rc);
+
+               if (rc > 0) {
+                       lustre_som_swab(som);
+                       if (mlc->mlc_intent->li_extent.e_end > som->lsa_size) {
+                               som->lsa_size =
+                                       mlc->mlc_intent->li_extent.e_end + 1;
+                               fl = LU_XATTR_REPLACE;
+                       }
+               }
                break;
        default:
                RETURN(-EBUSY);
@@ -2788,6 +2970,13 @@ mdd_layout_update_write_pending(const struct lu_env *env,
        if (rc)
                GOTO(out, rc);
 
+       if (fl) {
+               rc = mdd_declare_xattr_set(env, mdd, obj, som_buf,
+                                          XATTR_NAME_SOM, fl, handle);
+               if (rc)
+                       GOTO(out, rc);
+       }
+
        rc = mdd_trans_start(env, mdd, handle);
        if (rc)
                GOTO(out, rc);
@@ -2797,6 +2986,12 @@ mdd_layout_update_write_pending(const struct lu_env *env,
 
        mdd_write_lock(env, obj, DT_TGT_CHILD);
        rc = mdo_layout_change(env, obj, mlc, handle);
+       if (!rc && fl) {
+               som->lsa_valid = SOM_FL_STALE;
+               lustre_som_swab(som);
+               rc = mdo_xattr_set(env, obj, som_buf, XATTR_NAME_SOM,
+                                  fl, handle);
+       }
        mdd_write_unlock(env, obj);
        if (rc)
                GOTO(out, rc);
@@ -2824,7 +3019,7 @@ mdd_object_update_sync_pending(const struct lu_env *env, struct mdd_object *obj,
                struct md_layout_change *mlc, struct thandle *handle)
 {
        struct mdd_device *mdd = mdd_obj2mdd_dev(obj);
-       struct lu_buf *som_buf = &mdd_env_info(env)->mti_buf[1];
+       struct lu_buf *som_buf = &mdd_env_info(env)->mdi_buf[1];
        int fl = 0;
        int rc;
        ENTRY;
@@ -2891,7 +3086,8 @@ mdd_object_update_sync_pending(const struct lu_env *env, struct mdd_object *obj,
                        GOTO(out, rc);
        }
 
-       rc = mdd_changelog_data_store(env, mdd, CL_RESYNC, 0, obj, handle);
+       rc = mdd_changelog_data_store(env, mdd, CL_RESYNC, 0, obj, handle,
+                                     NULL);
        if (rc)
                GOTO(out, rc);
        EXIT;
@@ -2916,8 +3112,24 @@ mdd_layout_change(const struct lu_env *env, struct md_object *o,
        struct thandle          *handle;
        int flr_state;
        int rc;
+
        ENTRY;
 
+       if (S_ISDIR(mdd_object_type(obj))) {
+               switch (mlc->mlc_opc) {
+               case MD_LAYOUT_SHRINK:
+                       rc = mdd_dir_layout_shrink(env, o, mlc);
+                       break;
+               case MD_LAYOUT_SPLIT:
+                       rc = mdd_dir_layout_split(env, o, mlc);
+                       break;
+               default:
+                       LBUG();
+               }
+
+               RETURN(rc);
+       }
+
        /* Verify acceptable operations */
        switch (mlc->mlc_opc) {
        case MD_LAYOUT_WRITE:
@@ -3013,8 +3225,8 @@ void mdd_object_make_hint(const struct lu_env *env, struct mdd_object *parent,
        nc->do_ops->do_ah_init(env, hint, np, nc, attr->la_mode & S_IFMT);
 }
 
-static int accmode(const struct lu_env *env, const struct lu_attr *la,
-                  u64 open_flags)
+static int mdd_accmode(const struct lu_env *env, const struct lu_attr *la,
+                      u64 open_flags)
 {
        /* Sadly, NFSD reopens a file repeatedly during operation, so the
         * "acc_mode = 0" allowance for newly-created files isn't honoured.
@@ -3036,7 +3248,8 @@ static int mdd_open_sanity_check(const struct lu_env *env,
                                 const struct lu_attr *attr, u64 open_flags,
                                 int is_replay)
 {
-       int mode, rc;
+       unsigned int may_mask;
+       int rc;
        ENTRY;
 
        /* EEXIST check, also opening of *open* orphans is allowed so we can
@@ -3049,13 +3262,13 @@ static int mdd_open_sanity_check(const struct lu_env *env,
        if (S_ISLNK(attr->la_mode))
                RETURN(-ELOOP);
 
-       mode = accmode(env, attr, open_flags);
+       may_mask = mdd_accmode(env, attr, open_flags);
 
-       if (S_ISDIR(attr->la_mode) && (mode & MAY_WRITE))
+       if (S_ISDIR(attr->la_mode) && (may_mask & MAY_WRITE))
                RETURN(-EISDIR);
 
        if (!(open_flags & MDS_OPEN_CREATED)) {
-               rc = mdd_permission_internal(env, obj, attr, mode);
+               rc = mdd_permission_internal(env, obj, attr, may_mask);
                if (rc)
                        RETURN(rc);
        }
@@ -3097,7 +3310,7 @@ static int mdd_open(const struct lu_env *env, struct md_object *obj,
 
        rc = mdd_open_sanity_check(env, mdd_obj, attr, open_flags,
                                   spec->no_create);
-       if ((rc == -EACCES) && (mdd->mdd_cl.mc_mask & (1 << CL_DN_OPEN)))
+       if ((rc == -EACCES) && (mdd->mdd_cl.mc_current_mask & BIT(CL_DN_OPEN)))
                type = CL_DN_OPEN;
        else if (rc != 0)
                GOTO(out, rc);
@@ -3152,7 +3365,7 @@ find:
        }
 
        /* FYI, only the bottom 32 bits of open_flags are recorded */
-       mdd_changelog(env, type, open_flags, md_dev, mdo2fid(mdd_obj));
+       mdd_changelog(env, type, open_flags, md_dev, mdd_object_fid(mdd_obj));
 
        EXIT;
 out:
@@ -3335,10 +3548,11 @@ out:
         * this is not a big deal if we have a CL_CLOSE entry with no matching
         * CL_OPEN. Plus Changelogs mask may not change often.
         */
-       if (((!(mdd->mdd_cl.mc_mask & (1 << CL_OPEN)) &&
+       if (((!(mdd->mdd_cl.mc_current_mask & BIT(CL_OPEN)) &&
              (open_flags & (MDS_FMODE_WRITE | MDS_OPEN_APPEND |
                             MDS_OPEN_TRUNC))) ||
-            ((mdd->mdd_cl.mc_mask & (1 << CL_OPEN)) && last_close_by_uid)) &&
+            ((mdd->mdd_cl.mc_current_mask & BIT(CL_OPEN)) &&
+             last_close_by_uid)) &&
            !(ma->ma_valid & MA_FLAGS && ma->ma_attr_flags & MDS_RECOV_OPEN)) {
                if (handle == NULL) {
                        handle = mdd_trans_create(env, mdo2mdd(obj));
@@ -3357,7 +3571,7 @@ out:
 
                /* FYI, only the bottom 32 bits of open_flags are recorded */
                mdd_changelog_data_store(env, mdd, CL_CLOSE, open_flags,
-                                        mdd_obj, handle);
+                                        mdd_obj, handle, NULL);
        }
 
 stop: