Whamcloud - gitweb
LU-12675 mdt: release object reference upon error
[fs/lustre-release.git] / lustre / mdt / mdt_reint.c
index cfe0dd1..9970f98 100644 (file)
  *
  * You should have received a copy of the GNU General Public License
  * version 2 along with this program; If not, see
- * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
- *
- * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
- * CA 95054 USA or visit www.sun.com if you need additional information or
- * have any questions.
+ * http://www.gnu.org/licenses/gpl-2.0.html
  *
  * GPL HEADER END
  */
@@ -27,7 +23,7 @@
  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2011, 2015, Intel Corporation.
+ * Copyright (c) 2011, 2017, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -65,14 +61,15 @@ static inline void mdt_reint_init_ma(struct mdt_thread_info *info,
 static void mdt_obj_version_get(struct mdt_thread_info *info,
                                 struct mdt_object *o, __u64 *version)
 {
-        LASSERT(o);
+       LASSERT(o);
+
        if (mdt_object_exists(o) && !mdt_object_remote(o) &&
            !fid_is_obf(mdt_object_fid(o)))
-                *version = dt_version_get(info->mti_env, mdt_obj2dt(o));
-        else
-                *version = ENOENT_VERSION;
-        CDEBUG(D_INODE, "FID "DFID" version is "LPX64"\n",
-               PFID(mdt_object_fid(o)), *version);
+               *version = dt_version_get(info->mti_env, mdt_obj2dt(o));
+       else
+               *version = ENOENT_VERSION;
+       CDEBUG(D_INODE, "FID "DFID" version is %#llx\n",
+              PFID(mdt_object_fid(o)), *version);
 }
 
 /**
@@ -100,7 +97,7 @@ static int mdt_version_check(struct ptlrpc_request *req,
                spin_unlock(&req->rq_export->exp_lock);
                RETURN(-EOVERFLOW);
        } else if (pre_ver[idx] != version) {
-               CDEBUG(D_INODE, "Version mismatch "LPX64" != "LPX64"\n",
+               CDEBUG(D_INODE, "Version mismatch %#llx != %#llx\n",
                       pre_ver[idx], version);
                spin_lock(&req->rq_export->exp_lock);
                req->rq_export->exp_vbr_failed = 1;
@@ -220,155 +217,58 @@ static int mdt_lookup_version_check(struct mdt_thread_info *info,
 
 }
 
-static inline int mdt_remote_permission_check(struct mdt_thread_info *info)
-{
-       struct lu_ucred *uc  = mdt_ucred(info);
-       struct mdt_device *mdt = info->mti_mdt;
-
-       if (!md_capable(uc, CFS_CAP_SYS_ADMIN)) {
-               if (uc->uc_gid != mdt->mdt_enable_remote_dir_gid &&
-                   mdt->mdt_enable_remote_dir_gid != -1)
-                       return -EPERM;
-       }
-
-       return 0;
-}
-
-/**
- * mdt_remote_permission: Check whether the remote operation is permitted,
- *
- * Only sysadmin can create remote directory / striped directory,
- * migrate directory and set default stripedEA on directory, unless
- *
- * lctl set_param mdt.*.enable_remote_dir_gid=allow_gid.
- *
- * param[in] info: mdt_thread_info.
- *
- * retval      = 0 remote operation is allowed.
- *              < 0 remote operation is denied.
- */
-static int mdt_remote_permission(struct mdt_thread_info *info)
-{
-       struct md_op_spec *spec = &info->mti_spec;
-       struct lu_attr *attr = &info->mti_attr.ma_attr;
-       struct obd_export *exp = mdt_info_req(info)->rq_export;
-       int rc;
-
-       if (info->mti_rr.rr_opcode == REINT_MIGRATE) {
-               rc = mdt_remote_permission_check(info);
-               if (rc != 0)
-                       return rc;
-       }
-
-       if (info->mti_rr.rr_opcode == REINT_CREATE &&
-           (S_ISDIR(attr->la_mode) && spec->u.sp_ea.eadata != NULL &&
-            spec->u.sp_ea.eadatalen != 0)) {
-               const struct lmv_user_md *lum = spec->u.sp_ea.eadata;
-
-               /* Only new clients can create remote dir( >= 2.4) and
-                * striped dir(>= 2.6), old client will return -ENOTSUPP */
-               if (!mdt_is_dne_client(exp))
-                       return -ENOTSUPP;
-
-               if (le32_to_cpu(lum->lum_stripe_count) > 1 &&
-                   !mdt_is_striped_client(exp))
-                       return -ENOTSUPP;
-
-               rc = mdt_remote_permission_check(info);
-               if (rc != 0)
-                       return rc;
-       }
-
-       if (info->mti_rr.rr_opcode == REINT_SETATTR) {
-               struct md_attr *ma = &info->mti_attr;
-
-               if ((ma->ma_valid & MA_LMV)) {
-                       rc = mdt_remote_permission_check(info);
-                       if (rc != 0)
-                               return rc;
-               }
-       }
-
-       return 0;
-}
-
 static int mdt_unlock_slaves(struct mdt_thread_info *mti,
-                            struct mdt_object *obj, __u64 ibits,
-                            struct mdt_lock_handle *s0_lh,
-                            struct mdt_object *s0_obj,
+                            struct mdt_object *obj,
                             struct ldlm_enqueue_info *einfo,
                             int decref)
 {
        union ldlm_policy_data *policy = &mti->mti_policy;
+       struct mdt_lock_handle *lh = &mti->mti_lh[MDT_LH_LOCAL];
        struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
        int i;
-       int rc;
-       ENTRY;
-
-       if (!S_ISDIR(obj->mot_header.loh_attr))
-               RETURN(0);
 
-       /* Unlock stripe 0 */
-       if (s0_lh != NULL && lustre_handle_is_used(&s0_lh->mlh_reg_lh)) {
-               LASSERT(s0_obj != NULL);
-               mdt_object_unlock_put(mti, s0_obj, s0_lh, decref);
-       }
+       LASSERT(S_ISDIR(obj->mot_header.loh_attr));
+       LASSERT(slave_locks);
 
        memset(policy, 0, sizeof(*policy));
-       policy->l_inodebits.bits = ibits;
-
-       if (slave_locks != NULL) {
-               LASSERT(s0_lh != NULL);
-               for (i = 1; i < slave_locks->count; i++) {
-                       /* borrow s0_lh temporarily to do mdt unlock */
-                       mdt_lock_reg_init(s0_lh, einfo->ei_mode);
-                       s0_lh->mlh_rreg_lh = slave_locks->handles[i];
-                       mdt_object_unlock(mti, obj, s0_lh, decref);
-                       slave_locks->handles[i].cookie = 0ull;
-               }
+       policy->l_inodebits.bits = einfo->ei_inodebits;
+       mdt_lock_handle_init(lh);
+       mdt_lock_reg_init(lh, einfo->ei_mode);
+       for (i = 0; i < slave_locks->ha_count; i++) {
+               if (test_bit(i, (void *)slave_locks->ha_map))
+                       lh->mlh_rreg_lh = slave_locks->ha_handles[i];
+               else
+                       lh->mlh_reg_lh = slave_locks->ha_handles[i];
+               mdt_object_unlock(mti, NULL, lh, decref);
+               slave_locks->ha_handles[i].cookie = 0ull;
        }
 
-       rc = mo_object_unlock(mti->mti_env, mdt_object_child(obj), einfo,
-                             policy);
-       RETURN(rc);
+       return mo_object_unlock(mti->mti_env, mdt_object_child(obj), einfo,
+                               policy);
 }
 
-static int mdt_init_slaves(struct mdt_thread_info *mti, struct mdt_object *obj,
-                          struct lu_fid *fid)
+static inline int mdt_object_striped(struct mdt_thread_info *mti,
+                                    struct mdt_object *obj)
 {
-       struct lu_buf *buf = &mti->mti_buf;
-       struct lmv_mds_md_v1 *lmv;
+       struct lu_device *bottom_dev;
+       struct lu_object *bottom_obj;
        int rc;
-       ENTRY;
 
        if (!S_ISDIR(obj->mot_header.loh_attr))
-               RETURN(0);
-
-       buf->lb_buf = mti->mti_xattr_buf;
-       buf->lb_len = sizeof(mti->mti_xattr_buf);
-       rc = mo_xattr_get(mti->mti_env, mdt_object_child(obj), buf,
-                         XATTR_NAME_LMV);
-       if (rc == -ERANGE) {
-               rc = mdt_big_xattr_get(mti, obj, XATTR_NAME_LMV);
-               if (rc > 0) {
-                       buf->lb_buf = mti->mti_big_lmm;
-                       buf->lb_len = mti->mti_big_lmmsize;
-               }
-       }
-
-       if (rc == -ENODATA || rc == -ENOENT)
-               RETURN(0);
-
-       if (rc <= 0)
-               RETURN(rc);
+               return 0;
 
-       lmv = buf->lb_buf;
-       if (le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_V1)
-               RETURN(-EINVAL);
+       /* getxattr from bottom obj to avoid reading in shard FIDs */
+       bottom_dev = dt2lu_dev(mti->mti_mdt->mdt_bottom);
+       bottom_obj = lu_object_find_slice(mti->mti_env, bottom_dev,
+                                         mdt_object_fid(obj), NULL);
+       if (IS_ERR(bottom_obj))
+               return PTR_ERR(bottom_obj);
 
-       fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[0]);
+       rc = dt_xattr_get(mti->mti_env, lu2dt(bottom_obj), &LU_BUF_NULL,
+                         XATTR_NAME_LMV);
+       lu_object_put(mti->mti_env, bottom_obj);
 
-       RETURN(rc);
+       return (rc > 0) ? 1 : (rc == -ENODATA) ? 0 : rc;
 }
 
 /**
@@ -377,39 +277,12 @@ static int mdt_init_slaves(struct mdt_thread_info *mti, struct mdt_object *obj,
  **/
 static int mdt_lock_slaves(struct mdt_thread_info *mti, struct mdt_object *obj,
                           enum ldlm_mode mode, __u64 ibits,
-                          struct lu_fid *s0_fid,
-                          struct mdt_lock_handle *s0_lh,
-                          struct mdt_object **s0_objp,
                           struct ldlm_enqueue_info *einfo)
 {
        union ldlm_policy_data *policy = &mti->mti_policy;
-       int rc;
-       ENTRY;
-
-       rc = mdt_init_slaves(mti, obj, s0_fid);
-       if (rc <= 0)
-               RETURN(rc);
 
        LASSERT(S_ISDIR(obj->mot_header.loh_attr));
 
-       if (!lu_fid_eq(s0_fid, mdt_object_fid(obj))) {
-               /* Except migrating object, whose 0_stripe and master
-                * object are the same object, 0_stripe and master
-                * object are different, though they are in the same
-                * MDT, to avoid adding osd_object_lock here, so we
-                * will enqueue the stripe0 lock in MDT0 for now */
-               *s0_objp = mdt_object_find(mti->mti_env, mti->mti_mdt, s0_fid);
-               if (IS_ERR(*s0_objp))
-                       RETURN(PTR_ERR(*s0_objp));
-
-               rc = mdt_reint_object_lock(mti, *s0_objp, s0_lh, ibits, true);
-               if (rc < 0) {
-                       mdt_object_put(mti->mti_env, *s0_objp);
-                       RETURN(rc);
-               }
-       }
-
-       memset(einfo, 0, sizeof(*einfo));
        einfo->ei_type = LDLM_IBITS;
        einfo->ei_mode = mode;
        einfo->ei_cb_bl = mdt_remote_blocking_ast;
@@ -417,12 +290,56 @@ static int mdt_lock_slaves(struct mdt_thread_info *mti, struct mdt_object *obj,
        einfo->ei_cb_cp = ldlm_completion_ast;
        einfo->ei_enq_slave = 1;
        einfo->ei_namespace = mti->mti_mdt->mdt_namespace;
+       einfo->ei_inodebits = ibits;
        memset(policy, 0, sizeof(*policy));
        policy->l_inodebits.bits = ibits;
 
-       rc = mo_object_lock(mti->mti_env, mdt_object_child(obj), NULL, einfo,
-                           policy);
-       RETURN(rc);
+       return mo_object_lock(mti->mti_env, mdt_object_child(obj), NULL, einfo,
+                             policy);
+}
+
+int mdt_reint_striped_lock(struct mdt_thread_info *info,
+                          struct mdt_object *o,
+                          struct mdt_lock_handle *lh,
+                          __u64 ibits,
+                          struct ldlm_enqueue_info *einfo,
+                          bool cos_incompat)
+{
+       int rc;
+
+       LASSERT(!mdt_object_remote(o));
+
+       memset(einfo, 0, sizeof(*einfo));
+
+       rc = mdt_reint_object_lock(info, o, lh, ibits, cos_incompat);
+       if (rc)
+               return rc;
+
+       rc = mdt_object_striped(info, o);
+       if (rc != 1) {
+               if (rc < 0)
+                       mdt_object_unlock(info, o, lh, rc);
+               return rc;
+       }
+
+       rc = mdt_lock_slaves(info, o, lh->mlh_reg_mode, ibits, einfo);
+       if (rc) {
+               mdt_object_unlock(info, o, lh, rc);
+               if (rc == -EIO && OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME))
+                       rc = 0;
+       }
+
+       return rc;
+}
+
+void mdt_reint_striped_unlock(struct mdt_thread_info *info,
+                             struct mdt_object *o,
+                             struct mdt_lock_handle *lh,
+                             struct ldlm_enqueue_info *einfo, int decref)
+{
+       if (einfo->ei_cbdata)
+               mdt_unlock_slaves(info, o, einfo, decref);
+       mdt_object_unlock(info, o, lh, decref);
 }
 
 /*
@@ -433,7 +350,7 @@ static int mdt_lock_slaves(struct mdt_thread_info *mti, struct mdt_object *obj,
  * 2 - child. Version of child by FID. Must be ENOENT. It is mostly sanity
  * check.
  */
-static int mdt_md_create(struct mdt_thread_info *info)
+static int mdt_create(struct mdt_thread_info *info)
 {
        struct mdt_device       *mdt = info->mti_mdt;
        struct mdt_object       *parent;
@@ -442,6 +359,7 @@ static int mdt_md_create(struct mdt_thread_info *info)
        struct mdt_body         *repbody;
        struct md_attr          *ma = &info->mti_attr;
        struct mdt_reint_record *rr = &info->mti_rr;
+       struct md_op_spec       *spec = &info->mti_spec;
        int rc;
        ENTRY;
 
@@ -452,6 +370,33 @@ static int mdt_md_create(struct mdt_thread_info *info)
        if (!fid_is_md_operative(rr->rr_fid1))
                RETURN(-EPERM);
 
+       if (S_ISDIR(ma->ma_attr.la_mode) &&
+           spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen != 0) {
+               const struct lmv_user_md *lum = spec->u.sp_ea.eadata;
+               struct lu_ucred *uc = mdt_ucred(info);
+               struct obd_export *exp = mdt_info_req(info)->rq_export;
+
+               /* Only new clients can create remote dir( >= 2.4) and
+                * striped dir(>= 2.6), old client will return -ENOTSUPP */
+               if (!mdt_is_dne_client(exp))
+                       RETURN(-ENOTSUPP);
+
+               if (le32_to_cpu(lum->lum_stripe_count) > 1) {
+                       if (!mdt_is_striped_client(exp))
+                               RETURN(-ENOTSUPP);
+
+                       if (!mdt->mdt_enable_striped_dir)
+                               RETURN(-EPERM);
+               } else if (!mdt->mdt_enable_remote_dir) {
+                       RETURN(-EPERM);
+               }
+
+               if (!md_capable(uc, CFS_CAP_SYS_ADMIN) &&
+                   uc->uc_gid != mdt->mdt_enable_remote_dir_gid &&
+                   mdt->mdt_enable_remote_dir_gid != -1)
+                       RETURN(-EPERM);
+       }
+
        repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
 
        parent = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
@@ -493,10 +438,6 @@ static int mdt_md_create(struct mdt_thread_info *info)
        if (unlikely(IS_ERR(child)))
                GOTO(unlock_parent, rc = PTR_ERR(child));
 
-       rc = mdt_remote_permission(info);
-       if (rc != 0)
-               GOTO(put_child, rc);
-
        ma->ma_need = MA_INODE;
        ma->ma_valid = 0;
 
@@ -538,15 +479,15 @@ static int mdt_md_create(struct mdt_thread_info *info)
         */
        if (mdt_slc_is_enabled(mdt) && S_ISDIR(ma->ma_attr.la_mode)) {
                struct mdt_lock_handle *lhc;
-               struct mdt_lock_handle *s0_lh;
-               struct mdt_object *s0_obj = NULL;
-               struct ldlm_enqueue_info *einfo;
-               struct lu_fid *s0_fid = &info->mti_tmp_fid1;
-               bool cos_incompat = false;
-
-               rc = mdt_init_slaves(info, child, s0_fid);
-               if (rc > 0) {
-                       cos_incompat = true;
+               struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
+               bool cos_incompat;
+
+               rc = mdt_object_striped(info, child);
+               if (rc < 0)
+                       GOTO(put_child, rc);
+
+               cos_incompat = rc;
+               if (cos_incompat) {
                        if (!mdt_object_remote(parent)) {
                                mdt_object_unlock(info, parent, lh, 1);
                                mdt_lock_pdo_init(lh, LCK_PW, &rr->rr_name);
@@ -558,26 +499,16 @@ static int mdt_md_create(struct mdt_thread_info *info)
                        }
                }
 
-               einfo = &info->mti_einfo;
                lhc = &info->mti_lh[MDT_LH_CHILD];
                mdt_lock_handle_init(lhc);
                mdt_lock_reg_init(lhc, LCK_PW);
-               rc = mdt_reint_object_lock(info, child, lhc,
-                                          MDS_INODELOCK_UPDATE,
-                                          cos_incompat);
+               rc = mdt_reint_striped_lock(info, child, lhc,
+                                           MDS_INODELOCK_UPDATE, einfo,
+                                           cos_incompat);
                if (rc)
                        GOTO(put_child, rc);
-               mdt_object_unlock(info, child, lhc, rc);
-
-               s0_lh = &info->mti_lh[MDT_LH_LOCAL];
-               mdt_lock_handle_init(s0_lh);
-               mdt_lock_reg_init(s0_lh, LCK_PW);
-               rc = mdt_lock_slaves(info, child, LCK_PW, MDS_INODELOCK_UPDATE,
-                                    s0_fid, s0_lh, &s0_obj, einfo);
-               mdt_unlock_slaves(info, child, MDS_INODELOCK_UPDATE, s0_lh,
-                                 s0_obj, einfo, rc);
-               if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) && rc == -EIO)
-                       rc = 0;
+
+               mdt_reint_striped_unlock(info, child, lhc, einfo, rc);
        }
 
        /* Return fid & attr to client. */
@@ -597,22 +528,22 @@ static int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo,
                        struct md_attr *ma)
 {
        struct mdt_lock_handle  *lh;
-       int do_vbr = ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID|LA_FLAGS);
+       int do_vbr = ma->ma_attr.la_valid &
+                       (LA_MODE | LA_UID | LA_GID | LA_PROJID | LA_FLAGS);
        __u64 lockpart = MDS_INODELOCK_UPDATE;
-       struct ldlm_enqueue_info *einfo = &info->mti_einfo;
-       struct lu_fid *s0_fid = &info->mti_tmp_fid1;
-       struct mdt_lock_handle *s0_lh = NULL;
-       struct mdt_object *s0_obj = NULL;
-       bool cos_incompat = false;
+       struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
+       bool cos_incompat;
        int rc;
        ENTRY;
 
-       rc = mdt_init_slaves(info, mo, s0_fid);
-       if (rc > 0)
-               cos_incompat = true;
+       rc = mdt_object_striped(info, mo);
+       if (rc < 0)
+               RETURN(rc);
 
-        lh = &info->mti_lh[MDT_LH_PARENT];
-        mdt_lock_reg_init(lh, LCK_PW);
+       cos_incompat = rc;
+
+       lh = &info->mti_lh[MDT_LH_PARENT];
+       mdt_lock_reg_init(lh, LCK_PW);
 
        /* Even though the new MDT will grant PERM lock to the old
         * client, but the old client will almost ignore that during
@@ -621,52 +552,41 @@ static int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo,
        if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
                lockpart |= MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
 
-       rc = mdt_reint_object_lock(info, mo, lh, lockpart, cos_incompat);
+       rc = mdt_reint_striped_lock(info, mo, lh, lockpart, einfo,
+                                   cos_incompat);
        if (rc != 0)
                RETURN(rc);
 
-       s0_lh = &info->mti_lh[MDT_LH_LOCAL];
-       mdt_lock_reg_init(s0_lh, LCK_PW);
-       rc = mdt_lock_slaves(info, mo, LCK_PW, lockpart, s0_fid, s0_lh, &s0_obj,
-                            einfo);
-       if (rc != 0)
-               GOTO(out_unlock, rc);
-
-        /* all attrs are packed into mti_attr in unpack_setattr */
-        mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
-                       OBD_FAIL_MDS_REINT_SETATTR_WRITE);
-
-        /* This is only for set ctime when rename's source is on remote MDS. */
-        if (unlikely(ma->ma_attr.la_valid == LA_CTIME))
-                ma->ma_attr_flags |= MDS_VTX_BYPASS;
+       /* all attrs are packed into mti_attr in unpack_setattr */
+       mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
+                      OBD_FAIL_MDS_REINT_SETATTR_WRITE);
 
-        /* VBR: update version if attr changed are important for recovery */
-        if (do_vbr) {
-                /* update on-disk version of changed object */
+       /* VBR: update version if attr changed are important for recovery */
+       if (do_vbr) {
+               /* update on-disk version of changed object */
                tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mo));
-                rc = mdt_version_get_check_save(info, mo, 0);
-                if (rc)
-                        GOTO(out_unlock, rc);
-        }
+               rc = mdt_version_get_check_save(info, mo, 0);
+               if (rc)
+                       GOTO(out_unlock, rc);
+       }
 
        /* Ensure constant striping during chown(). See LU-2789. */
-       if (ma->ma_attr.la_valid & (LA_UID|LA_GID))
+       if (ma->ma_attr.la_valid & (LA_UID|LA_GID|LA_PROJID))
                mutex_lock(&mo->mot_lov_mutex);
 
-        /* all attrs are packed into mti_attr in unpack_setattr */
-        rc = mo_attr_set(info->mti_env, mdt_object_child(mo), ma);
+       /* all attrs are packed into mti_attr in unpack_setattr */
+       rc = mo_attr_set(info->mti_env, mdt_object_child(mo), ma);
 
-       if (ma->ma_attr.la_valid & (LA_UID|LA_GID))
+       if (ma->ma_attr.la_valid & (LA_UID|LA_GID|LA_PROJID))
                mutex_unlock(&mo->mot_lov_mutex);
 
-        if (rc != 0)
-                GOTO(out_unlock, rc);
-
-        EXIT;
+       if (rc != 0)
+               GOTO(out_unlock, rc);
+       mdt_dom_obj_lvb_update(info->mti_env, mo, false);
+       EXIT;
 out_unlock:
-       mdt_unlock_slaves(info, mo, lockpart, s0_lh, s0_obj, einfo, rc);
-        mdt_object_unlock(info, mo, lh, rc);
-        return rc;
+       mdt_reint_striped_unlock(info, mo, lh, einfo, rc);
+       return rc;
 }
 
 /**
@@ -678,6 +598,8 @@ out_unlock:
 int mdt_add_dirty_flag(struct mdt_thread_info *info, struct mdt_object *mo,
                        struct md_attr *ma)
 {
+       struct lu_ucred *uc = mdt_ucred(info);
+       cfs_cap_t cap_saved;
        int rc;
        ENTRY;
 
@@ -693,46 +615,44 @@ int mdt_add_dirty_flag(struct mdt_thread_info *info, struct mdt_object *mo,
        /* If an up2date copy exists in the backend, add dirty flag */
        if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_EXISTS)
            && !(ma->ma_hsm.mh_flags & (HS_DIRTY|HS_RELEASED))) {
-               struct mdt_lock_handle  *lh = &info->mti_lh[MDT_LH_CHILD];
-
                ma->ma_hsm.mh_flags |= HS_DIRTY;
 
-               mdt_lock_reg_init(lh, LCK_PW);
-               rc = mdt_object_lock(info, mo, lh, MDS_INODELOCK_XATTR);
-               if (rc != 0)
-                       RETURN(rc);
-
+               /* Bump cap so that closes from non-owner writers can
+                * set the HSM state to dirty. */
+               cap_saved = uc->uc_cap;
+               uc->uc_cap |= MD_CAP_TO_MASK(CFS_CAP_FOWNER);
                rc = mdt_hsm_attr_set(info, mo, &ma->ma_hsm);
+               uc->uc_cap = cap_saved;
                if (rc)
                        CERROR("file attribute change error for "DFID": %d\n",
                                PFID(mdt_object_fid(mo)), rc);
-               mdt_object_unlock(info, mo, lh, rc);
        }
 
        RETURN(rc);
 }
 
 static int mdt_reint_setattr(struct mdt_thread_info *info,
-                             struct mdt_lock_handle *lhc)
+                            struct mdt_lock_handle *lhc)
 {
-        struct md_attr          *ma = &info->mti_attr;
-        struct mdt_reint_record *rr = &info->mti_rr;
-        struct ptlrpc_request   *req = mdt_info_req(info);
-        struct mdt_object       *mo;
-        struct mdt_body         *repbody;
-       int                      rc, rc2;
-        ENTRY;
+       struct mdt_device *mdt = info->mti_mdt;
+       struct md_attr *ma = &info->mti_attr;
+       struct mdt_reint_record *rr = &info->mti_rr;
+       struct ptlrpc_request *req = mdt_info_req(info);
+       struct mdt_object *mo;
+       struct mdt_body *repbody;
+       int rc, rc2;
+       ENTRY;
 
-        DEBUG_REQ(D_INODE, req, "setattr "DFID" %x", PFID(rr->rr_fid1),
-                  (unsigned int)ma->ma_attr.la_valid);
+       DEBUG_REQ(D_INODE, req, "setattr "DFID" %x", PFID(rr->rr_fid1),
+                 (unsigned int)ma->ma_attr.la_valid);
 
        if (info->mti_dlm_req)
                ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
 
        repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
-        mo = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
-        if (IS_ERR(mo))
-                GOTO(out, rc = PTR_ERR(mo));
+       mo = mdt_object_find(info->mti_env, mdt, rr->rr_fid1);
+       if (IS_ERR(mo))
+               GOTO(out, rc = PTR_ERR(mo));
 
        if (!mdt_object_exists(mo))
                GOTO(out_put, rc = -ENOENT);
@@ -740,55 +660,139 @@ static int mdt_reint_setattr(struct mdt_thread_info *info,
        if (mdt_object_remote(mo))
                GOTO(out_put, rc = -EREMOTE);
 
-       if ((ma->ma_attr.la_valid & LA_SIZE) ||
-           (rr->rr_flags & MRF_OPEN_TRUNC)) {
+       /* revoke lease lock if size is going to be changed */
+       if (unlikely(ma->ma_attr.la_valid & LA_SIZE &&
+                    !(ma->ma_attr_flags & MDS_TRUNC_KEEP_LEASE) &&
+                    atomic_read(&mo->mot_lease_count) > 0)) {
+               down_read(&mo->mot_open_sem);
+
+               if (atomic_read(&mo->mot_lease_count) > 0) { /* lease exists */
+                       lhc = &info->mti_lh[MDT_LH_LOCAL];
+                       mdt_lock_reg_init(lhc, LCK_CW);
+
+                       rc = mdt_object_lock(info, mo, lhc, MDS_INODELOCK_OPEN);
+                       if (rc != 0) {
+                               up_read(&mo->mot_open_sem);
+                               GOTO(out_put, rc);
+                       }
+
+                       /* revoke lease lock */
+                       mdt_object_unlock(info, mo, lhc, 1);
+               }
+               up_read(&mo->mot_open_sem);
+       }
+
+       if (ma->ma_attr.la_valid & LA_SIZE || rr->rr_flags & MRF_OPEN_TRUNC) {
                /* Check write access for the O_TRUNC case */
                if (mdt_write_read(mo) < 0)
                        GOTO(out_put, rc = -ETXTBSY);
+
+               /* LU-10286: compatibility check for FLR.
+                * Please check the comment in mdt_finish_open() for details */
+               if (!exp_connect_flr(info->mti_exp) ||
+                   !exp_connect_overstriping(info->mti_exp)) {
+                       rc = mdt_big_xattr_get(info, mo, XATTR_NAME_LOV);
+                       if (rc < 0 && rc != -ENODATA)
+                               GOTO(out_put, rc);
+
+                       if (!exp_connect_flr(info->mti_exp)) {
+                               if (rc > 0 &&
+                                   mdt_lmm_is_flr(info->mti_big_lmm))
+                                       GOTO(out_put, rc = -EOPNOTSUPP);
+                       }
+
+                       if (!exp_connect_overstriping(info->mti_exp)) {
+                               if (rc > 0 &&
+                                   mdt_lmm_is_overstriping(info->mti_big_lmm))
+                                       GOTO(out_put, rc = -EOPNOTSUPP);
+                       }
+               }
+
+               /* For truncate, the file size sent from client
+                * is believable, but the blocks are incorrect,
+                * which makes the block size in LSOM attribute
+                * inconsisent with the real block size.
+                */
+               rc = mdt_lsom_update(info, mo, true);
+               if (rc)
+                       GOTO(out_put, rc);
        }
 
        if ((ma->ma_valid & MA_INODE) && ma->ma_attr.la_valid) {
                if (ma->ma_valid & MA_LOV)
                        GOTO(out_put, rc = -EPROTO);
 
-               rc = mdt_attr_set(info, mo, ma);
-                if (rc)
-                        GOTO(out_put, rc);
-       } else if ((ma->ma_valid & MA_LOV) && (ma->ma_valid & MA_INODE)) {
-               struct lu_buf *buf  = &info->mti_buf;
-
-               if (ma->ma_attr.la_valid != 0)
-                       GOTO(out_put, rc = -EPROTO);
+               /* MDT supports FMD for regular files due to Data-on-MDT */
+               if (S_ISREG(lu_object_attr(&mo->mot_obj)) &&
+                   ma->ma_attr.la_valid & (LA_ATIME | LA_MTIME | LA_CTIME))
+                       tgt_fmd_update(info->mti_exp, mdt_object_fid(mo),
+                                      req->rq_xid);
 
-               buf->lb_buf = ma->ma_lmm;
-               buf->lb_len = ma->ma_lmm_size;
-               rc = mo_xattr_set(info->mti_env, mdt_object_child(mo),
-                                 buf, XATTR_NAME_LOV, 0);
+               rc = mdt_attr_set(info, mo, ma);
                if (rc)
                        GOTO(out_put, rc);
-       } else if ((ma->ma_valid & MA_LMV) && (ma->ma_valid & MA_INODE)) {
-               struct lu_buf *buf  = &info->mti_buf;
-               struct mdt_lock_handle  *lh;
+       } else if ((ma->ma_valid & (MA_LOV | MA_LMV)) &&
+                  (ma->ma_valid & MA_INODE)) {
+               struct lu_buf *buf = &info->mti_buf;
+               struct lu_ucred *uc = mdt_ucred(info);
+               struct mdt_lock_handle *lh;
+               const char *name;
+               __u64 lockpart = MDS_INODELOCK_XATTR;
+
+               /* reject if either remote or striped dir is disabled */
+               if (ma->ma_valid & MA_LMV) {
+                       if (!mdt->mdt_enable_remote_dir ||
+                           !mdt->mdt_enable_striped_dir)
+                               GOTO(out_put, rc = -EPERM);
+
+                       if (!md_capable(uc, CFS_CAP_SYS_ADMIN) &&
+                           uc->uc_gid != mdt->mdt_enable_remote_dir_gid &&
+                           mdt->mdt_enable_remote_dir_gid != -1)
+                               GOTO(out_put, rc = -EPERM);
+               }
 
-               rc = mdt_remote_permission(info);
-               if (rc < 0)
-                       GOTO(out_put, rc);
+               if (!S_ISDIR(lu_object_attr(&mo->mot_obj)))
+                       GOTO(out_put, rc = -ENOTDIR);
 
                if (ma->ma_attr.la_valid != 0)
                        GOTO(out_put, rc = -EPROTO);
 
+               if (ma->ma_valid & MA_LOV) {
+                       buf->lb_buf = ma->ma_lmm;
+                       buf->lb_len = ma->ma_lmm_size;
+                       name = XATTR_NAME_LOV;
+               } else {
+                       struct lmv_user_md *lmu = &ma->ma_lmv->lmv_user_md;
+
+                       buf->lb_buf = lmu;
+                       buf->lb_len = ma->ma_lmv_size;
+
+                       if (le32_to_cpu(lmu->lum_hash_type) &
+                           LMV_HASH_FLAG_SPACE) {
+                               /*
+                                * only allow setting "space" hash flag on
+                                * plain directory.
+                                */
+                               rc = mdt_object_striped(info, mo);
+                               if (rc)
+                                       GOTO(out_put,
+                                            rc = (rc == 1) ? -EPERM : rc);
+                       }
+
+                       name = XATTR_NAME_DEFAULT_LMV;
+                       /* force client to update dir default layout */
+                       lockpart |= MDS_INODELOCK_LOOKUP;
+               }
+
                lh = &info->mti_lh[MDT_LH_PARENT];
                mdt_lock_reg_init(lh, LCK_PW);
 
-               rc = mdt_object_lock(info, mo, lh,
-                                    MDS_INODELOCK_XATTR);
+               rc = mdt_object_lock(info, mo, lh, lockpart);
                if (rc != 0)
                        GOTO(out_put, rc);
 
-               buf->lb_buf = ma->ma_lmv;
-               buf->lb_len = ma->ma_lmv_size;
-               rc = mo_xattr_set(info->mti_env, mdt_object_child(mo),
-                                 buf, XATTR_NAME_DEFAULT_LMV, 0);
+               rc = mo_xattr_set(info->mti_env, mdt_object_child(mo), buf,
+                                 name, 0);
 
                mdt_object_unlock(info, mo, lh, rc);
                if (rc)
@@ -809,11 +813,11 @@ static int mdt_reint_setattr(struct mdt_thread_info *info,
 
        mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
 
-        EXIT;
+       EXIT;
 out_put:
-        mdt_object_put(info->mti_env, mo);
+       mdt_object_put(info->mti_env, mo);
 out:
-        if (rc == 0)
+       if (rc == 0)
                mdt_counter_incr(req, LPROC_MDT_SETATTR);
 
         mdt_client_compatibility(info);
@@ -860,7 +864,7 @@ static int mdt_reint_create(struct mdt_thread_info *info,
                RETURN(err_serious(-EOPNOTSUPP));
        }
 
-       rc = mdt_md_create(info);
+       rc = mdt_create(info);
        RETURN(rc);
 }
 
@@ -879,14 +883,12 @@ static int mdt_reint_unlink(struct mdt_thread_info *info,
        struct mdt_object *mc;
        struct mdt_lock_handle *parent_lh;
        struct mdt_lock_handle *child_lh;
-       struct ldlm_enqueue_info *einfo = &info->mti_einfo;
-       struct lu_fid *s0_fid = &info->mti_tmp_fid2;
-       struct mdt_lock_handle *s0_lh = NULL;
-       struct mdt_object *s0_obj = NULL;
+       struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
        __u64 lock_ibits;
        bool cos_incompat = false;
        int no_name = 0;
        int rc;
+
        ENTRY;
 
        DEBUG_REQ(D_INODE, req, "unlink "DFID"/"DNAME"", PFID(rr->rr_fid1),
@@ -957,11 +959,17 @@ relock:
        if (IS_ERR(mc))
                GOTO(unlock_parent, rc = PTR_ERR(mc));
 
-       if (!cos_incompat && mdt_init_slaves(info, mc, s0_fid) > 0) {
-               cos_incompat = true;
-               mdt_object_put(info->mti_env, mc);
-               mdt_object_unlock(info, mp, parent_lh, -EAGAIN);
-               goto relock;
+       if (!cos_incompat) {
+               rc = mdt_object_striped(info, mc);
+               if (rc < 0)
+                       GOTO(put_child, rc);
+
+               cos_incompat = rc;
+               if (cos_incompat) {
+                       mdt_object_put(info->mti_env, mc);
+                       mdt_object_unlock(info, mp, parent_lh, -EAGAIN);
+                       goto relock;
+               }
        }
 
        child_lh = &info->mti_lh[MDT_LH_CHILD];
@@ -1028,10 +1036,10 @@ relock:
                lock_ibits &= ~MDS_INODELOCK_LOOKUP;
        }
 
-       rc = mdt_reint_object_lock(info, mc, child_lh, lock_ibits,
-                                  cos_incompat);
+       rc = mdt_reint_striped_lock(info, mc, child_lh, lock_ibits, einfo,
+                                   cos_incompat);
        if (rc != 0)
-               GOTO(unlock_child, rc);
+               GOTO(put_child, rc);
 
        /*
         * Now we can only make sure we need MA_INODE, in mdd layer, will check
@@ -1040,13 +1048,6 @@ relock:
        ma->ma_need = MA_INODE;
        ma->ma_valid = 0;
 
-       s0_lh = &info->mti_lh[MDT_LH_LOCAL];
-       mdt_lock_reg_init(s0_lh, LCK_EX);
-       rc = mdt_lock_slaves(info, mc, LCK_EX, MDS_INODELOCK_UPDATE, s0_fid,
-                            s0_lh, &s0_obj, einfo);
-       if (rc != 0)
-               GOTO(unlock_child, rc);
-
        mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
                       OBD_FAIL_MDS_REINT_UNLINK_WRITE);
        /* save version when object is locked */
@@ -1058,43 +1059,49 @@ relock:
                        mdt_object_child(mc), &rr->rr_name, ma, no_name);
 
        mutex_unlock(&mc->mot_lov_mutex);
+       if (rc != 0)
+               GOTO(unlock_child, rc);
 
-       if (rc == 0 && !lu_object_is_dying(&mc->mot_header))
+       if (!lu_object_is_dying(&mc->mot_header)) {
                rc = mdt_attr_get_complex(info, mc, ma);
-       if (rc == 0)
-               mdt_handle_last_unlink(info, mc, ma);
+               if (rc)
+                       GOTO(out_stat, rc);
+       } else if (mdt_dom_check_for_discard(info, mc)) {
+               mdt_dom_discard_data(info, mc);
+       }
+       mdt_handle_last_unlink(info, mc, ma);
 
-        if (ma->ma_valid & MA_INODE) {
-                switch (ma->ma_attr.la_mode & S_IFMT) {
-                case S_IFDIR:
+out_stat:
+       if (ma->ma_valid & MA_INODE) {
+               switch (ma->ma_attr.la_mode & S_IFMT) {
+               case S_IFDIR:
                        mdt_counter_incr(req, LPROC_MDT_RMDIR);
-                        break;
-                case S_IFREG:
-                case S_IFLNK:
-                case S_IFCHR:
-                case S_IFBLK:
-                case S_IFIFO:
-                case S_IFSOCK:
+                       break;
+               case S_IFREG:
+               case S_IFLNK:
+               case S_IFCHR:
+               case S_IFBLK:
+               case S_IFIFO:
+               case S_IFSOCK:
                        mdt_counter_incr(req, LPROC_MDT_UNLINK);
-                        break;
-                default:
-                        LASSERTF(0, "bad file type %o unlinking\n",
-                                 ma->ma_attr.la_mode);
-                }
-        }
+                       break;
+               default:
+                       LASSERTF(0, "bad file type %o unlinking\n",
+                               ma->ma_attr.la_mode);
+               }
+       }
 
-        EXIT;
+       EXIT;
 
 unlock_child:
-       mdt_unlock_slaves(info, mc, MDS_INODELOCK_UPDATE, s0_lh, s0_obj, einfo,
-                         rc);
-       mdt_object_unlock(info, mc, child_lh, rc);
+       mdt_reint_striped_unlock(info, mc, child_lh, einfo, rc);
 put_child:
        mdt_object_put(info->mti_env, mc);
 unlock_parent:
        mdt_object_unlock(info, mp, parent_lh, rc);
 put_parent:
        mdt_object_put(info->mti_env, mp);
+       CFS_RACE_WAKEUP(OBD_FAIL_OBD_ZERO_NLINK_RACE);
         return rc;
 }
 
@@ -1246,8 +1253,9 @@ static int mdt_pdir_hash_lock(struct mdt_thread_info *info,
         * going to be sent to client. If it is - mdt_intent_policy() path will
         * fix it up and turn FL_LOCAL flag off.
         */
-       rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
-                         res, dlmflags, &info->mti_exp->exp_handle.h_cookie);
+       rc = mdt_fid_lock(info->mti_env, ns, &lh->mlh_reg_lh, lh->mlh_reg_mode,
+                         policy, res, dlmflags,
+                         &info->mti_exp->exp_handle.h_cookie);
        return rc;
 }
 
@@ -1287,12 +1295,13 @@ static int mdt_rename_lock(struct mdt_thread_info *info,
                memset(policy, 0, sizeof *policy);
                policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
                flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
-               rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS, policy,
-                                          LCK_EX, &flags, ldlm_blocking_ast,
-                                          ldlm_completion_ast, NULL, NULL, 0,
-                                          LVB_T_NONE,
-                                          &info->mti_exp->exp_handle.h_cookie,
-                                          lh);
+               rc = ldlm_cli_enqueue_local(info->mti_env, ns, res_id,
+                                           LDLM_IBITS, policy, LCK_EX, &flags,
+                                           ldlm_blocking_ast,
+                                           ldlm_completion_ast, NULL, NULL, 0,
+                                           LVB_T_NONE,
+                                           &info->mti_exp->exp_handle.h_cookie,
+                                           lh);
                RETURN(rc);
        }
        RETURN(rc);
@@ -1307,500 +1316,884 @@ static void mdt_rename_unlock(struct lustre_handle *lh)
        EXIT;
 }
 
-/*
- * This is is_subdir() variant, it is CMD if cmm forwards it to correct
- * target. Source should not be ancestor of target dir. May be other rename
- * checks can be moved here later.
- */
-static int mdt_is_subdir(struct mdt_thread_info *info,
-                        struct mdt_object *dir,
-                        const struct lu_fid *fid)
+static struct mdt_object *mdt_parent_find_check(struct mdt_thread_info *info,
+                                               const struct lu_fid *fid,
+                                               int idx)
 {
-       struct lu_fid dir_fid = dir->mot_header.loh_fid;
-        int rc = 0;
-        ENTRY;
+       struct mdt_object *dir;
+       int rc;
 
-       /* If the source and target are in the same directory, they can not
-        * be parent/child relationship, so subdir check is not needed */
-       if (lu_fid_eq(&dir_fid, fid))
-               return 0;
+       ENTRY;
 
-       if (!mdt_object_exists(dir))
-               RETURN(-ENOENT);
-
-       rc = mdo_is_subdir(info->mti_env, mdt_object_child(dir),
-                          fid, &dir_fid);
-       if (rc < 0) {
-               CERROR("%s: failed subdir check in "DFID" for "DFID
-                      ": rc = %d\n", mdt_obd_name(info->mti_mdt),
-                      PFID(&dir_fid), PFID(fid), rc);
-               /* Return EINVAL only if a parent is the @fid */
-               if (rc == -EINVAL)
-                       rc = -EIO;
-       } else {
-               /* check the found fid */
-               if (lu_fid_eq(&dir_fid, fid))
-                       rc = -EINVAL;
-       }
+       dir = mdt_object_find(info->mti_env, info->mti_mdt, fid);
+       if (IS_ERR(dir))
+               RETURN(dir);
 
-        RETURN(rc);
-}
+       /* check early, the real version will be saved after locking */
+       rc = mdt_version_get_check(info, dir, idx);
+       if (rc)
+               GOTO(out_put, rc);
 
-/* Update object linkEA */
-struct mdt_lock_list {
-       struct mdt_object       *mll_obj;
-       struct mdt_lock_handle  mll_lh;
-       struct list_head        mll_list;
-};
+       if (!mdt_object_exists(dir))
+               GOTO(out_put, rc = -ENOENT);
 
-static void mdt_unlock_list(struct mdt_thread_info *info,
-                           struct list_head *list, int rc)
-{
-       struct mdt_lock_list *mll;
-       struct mdt_lock_list *mll2;
+       if (!S_ISDIR(lu_object_attr(&dir->mot_obj)))
+               GOTO(out_put, rc = -ENOTDIR);
 
-       list_for_each_entry_safe(mll, mll2, list, mll_list) {
-               mdt_object_unlock_put(info, mll->mll_obj, &mll->mll_lh, rc);
-               list_del(&mll->mll_list);
-               OBD_FREE_PTR(mll);
-       }
+       RETURN(dir);
+out_put:
+       mdt_object_put(info->mti_env, dir);
+       return ERR_PTR(rc);
 }
 
-static int mdt_lock_objects_in_linkea(struct mdt_thread_info *info,
-                                     struct mdt_object *obj,
-                                     struct mdt_object *pobj,
-                                     struct list_head *lock_list)
+/*
+ * in case obj is remote obj on its parent, revoke LOOKUP lock,
+ * herein we don't really check it, just do revoke.
+ */
+int mdt_revoke_remote_lookup_lock(struct mdt_thread_info *info,
+                                 struct mdt_object *pobj,
+                                 struct mdt_object *obj)
 {
-       struct lu_buf           *buf = &info->mti_big_buf;
-       struct linkea_data      ldata = { NULL };
-       int                     count;
-       int                     retry_count;
-       int                     rc;
-       ENTRY;
+       struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_LOCAL];
+       int rc;
+
+       mdt_lock_handle_init(lh);
+       mdt_lock_reg_init(lh, LCK_EX);
+
+       if (mdt_object_remote(pobj)) {
+               /* don't bother to check if pobj and obj are on the same MDT. */
+               rc = mdt_remote_object_lock(info, pobj, mdt_object_fid(obj),
+                                           &lh->mlh_rreg_lh, LCK_EX,
+                                           MDS_INODELOCK_LOOKUP, false);
+       } else if (mdt_object_remote(obj)) {
+               struct ldlm_res_id *res = &info->mti_res_id;
+               union ldlm_policy_data *policy = &info->mti_policy;
+               __u64 dlmflags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB |
+                                LDLM_FL_COS_INCOMPAT;
+
+               fid_build_reg_res_name(mdt_object_fid(obj), res);
+               memset(policy, 0, sizeof(*policy));
+               policy->l_inodebits.bits = MDS_INODELOCK_LOOKUP;
+               rc = mdt_fid_lock(info->mti_env, info->mti_mdt->mdt_namespace,
+                                 &lh->mlh_reg_lh, LCK_EX, policy, res,
+                                 dlmflags, NULL);
+       } else {
+               /* do nothing if both are local */
+               return 0;
+       }
+
+       if (rc != ELDLM_OK)
+               return rc;
+
+       /*
+        * TODO, currently we don't save this lock because there is no place to
+        * hold this lock handle, but to avoid race we need to save this lock.
+        */
+       mdt_object_unlock(info, NULL, lh, 1);
+
+       return 0;
+}
+
+/*
+ * operation may takes locks of linkea, or directory stripes, group them in
+ * different list.
+ */
+struct mdt_sub_lock {
+       struct mdt_object      *msl_obj;
+       struct mdt_lock_handle  msl_lh;
+       struct list_head        msl_linkage;
+};
+
+static void mdt_unlock_list(struct mdt_thread_info *info,
+                           struct list_head *list, int decref)
+{
+       struct mdt_sub_lock *msl;
+       struct mdt_sub_lock *tmp;
+
+       list_for_each_entry_safe(msl, tmp, list, msl_linkage) {
+               mdt_object_unlock_put(info, msl->msl_obj, &msl->msl_lh, decref);
+               list_del(&msl->msl_linkage);
+               OBD_FREE_PTR(msl);
+       }
+}
+
+static inline void mdt_migrate_object_unlock(struct mdt_thread_info *info,
+                                            struct mdt_object *obj,
+                                            struct mdt_lock_handle *lh,
+                                            struct ldlm_enqueue_info *einfo,
+                                            struct list_head *slave_locks,
+                                            int decref)
+{
+       if (mdt_object_remote(obj)) {
+               mdt_unlock_list(info, slave_locks, decref);
+               mdt_object_unlock(info, obj, lh, decref);
+       } else {
+               mdt_reint_striped_unlock(info, obj, lh, einfo, decref);
+       }
+}
+
+/*
+ * lock parents of links, and also check whether total locks don't exceed
+ * RS_MAX_LOCKS.
+ *
+ * \retval     0 on success, and locks can be saved in ptlrpc_reply_stat
+ * \retval     1 on success, but total lock count may exceed RS_MAX_LOCKS
+ * \retval     -ev negative errno upon error
+ */
+static int mdt_link_parents_lock(struct mdt_thread_info *info,
+                                struct mdt_object *pobj,
+                                const struct md_attr *ma,
+                                struct mdt_object *obj,
+                                struct mdt_lock_handle *lhp,
+                                struct ldlm_enqueue_info *peinfo,
+                                struct list_head *parent_slave_locks,
+                                struct list_head *link_locks)
+{
+       struct mdt_device *mdt = info->mti_mdt;
+       struct lu_buf *buf = &info->mti_big_buf;
+       struct lu_name *lname = &info->mti_name;
+       struct linkea_data ldata = { NULL };
+       bool blocked = false;
+       int local_lnkp_cnt = 0;
+       int rc;
+
+       ENTRY;
 
        if (S_ISDIR(lu_object_attr(&obj->mot_obj)))
                RETURN(0);
 
-       buf = lu_buf_check_and_alloc(buf, PATH_MAX);
+       buf = lu_buf_check_and_alloc(buf, MAX_LINKEA_SIZE);
        if (buf->lb_buf == NULL)
                RETURN(-ENOMEM);
 
        ldata.ld_buf = buf;
        rc = mdt_links_read(info, obj, &ldata);
-       if (rc != 0) {
+       if (rc) {
                if (rc == -ENOENT || rc == -ENODATA)
                        rc = 0;
                RETURN(rc);
        }
 
-       /* ignore the migrating parent(@pobj) */
-       retry_count = ldata.ld_leh->leh_reccount - 1;
-
-again:
-       LASSERT(ldata.ld_leh != NULL);
-       ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
-       for (count = 0; count < ldata.ld_leh->leh_reccount; count++) {
-               struct mdt_device *mdt = info->mti_mdt;
-               struct mdt_object *mdt_pobj;
-               struct mdt_lock_list *mll;
-               struct lu_name name;
-               struct lu_fid  fid;
-
-               linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
-                                   &name, &fid);
-               mdt_pobj = mdt_object_find(info->mti_env, mdt, &fid);
-               if (IS_ERR(mdt_pobj)) {
-                       CWARN("%s: cannot find obj "DFID": rc = %ld\n",
-                             mdt_obd_name(mdt), PFID(&fid), PTR_ERR(mdt_pobj));
-                       goto next;
+       for (linkea_first_entry(&ldata); ldata.ld_lee && !rc;
+            linkea_next_entry(&ldata)) {
+               struct mdt_object *lnkp;
+               struct mdt_sub_lock *msl;
+               struct lu_fid fid;
+               __u64 ibits;
+
+               linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, lname,
+                                   &fid);
+
+               /* check if it's also linked to parent */
+               if (lu_fid_eq(mdt_object_fid(pobj), &fid)) {
+                       CDEBUG(D_INFO, "skip parent "DFID", reovke "DNAME"\n",
+                              PFID(&fid), PNAME(lname));
+                       /* in case link is remote object, revoke LOOKUP lock */
+                       rc = mdt_revoke_remote_lookup_lock(info, pobj, obj);
+                       continue;
                }
 
-               if (!mdt_object_exists(mdt_pobj)) {
-                       CDEBUG(D_INFO, "%s: obj "DFID" does not exist\n",
-                             mdt_obd_name(mdt), PFID(&fid));
-                       mdt_object_put(info->mti_env, mdt_pobj);
-                       goto next;
+               lnkp = NULL;
+
+               /* check if it's linked to a stripe of parent */
+               if (ma->ma_valid & MA_LMV) {
+                       struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
+                       struct lu_fid *stripe_fid = &info->mti_tmp_fid1;
+                       int j = 0;
+
+                       for (; j < le32_to_cpu(lmv->lmv_stripe_count); j++) {
+                               fid_le_to_cpu(stripe_fid,
+                                             &lmv->lmv_stripe_fids[j]);
+                               if (lu_fid_eq(stripe_fid, &fid)) {
+                                       CDEBUG(D_INFO, "skip stripe "DFID
+                                              ", reovke "DNAME"\n",
+                                              PFID(&fid), PNAME(lname));
+                                       lnkp = mdt_object_find(info->mti_env,
+                                                              mdt, &fid);
+                                       if (IS_ERR(lnkp))
+                                               GOTO(out, rc = PTR_ERR(lnkp));
+                                       break;
+                               }
+                       }
+
+                       if (lnkp) {
+                               rc = mdt_revoke_remote_lookup_lock(info, lnkp,
+                                                                  obj);
+                               mdt_object_put(info->mti_env, lnkp);
+                               continue;
+                       }
                }
 
-               /* Check if the object already exists in the list */
-               list_for_each_entry(mll, lock_list, mll_list) {
-                       if (mll->mll_obj == mdt_pobj) {
-                               mdt_object_put(info->mti_env, mdt_pobj);
-                               goto next;
+               /* Check if it's already locked */
+               list_for_each_entry(msl, link_locks, msl_linkage) {
+                       if (lu_fid_eq(mdt_object_fid(msl->msl_obj), &fid)) {
+                               CDEBUG(D_INFO,
+                                      DFID" was locked, revoke "DNAME"\n",
+                                      PFID(&fid), PNAME(lname));
+                               lnkp = msl->msl_obj;
+                               break;
                        }
                }
 
-               if (mdt_pobj == pobj) {
-                       CDEBUG(D_INFO, "%s: skipping parent obj "DFID"\n",
-                              mdt_obd_name(mdt), PFID(&fid));
-                       mdt_object_put(info->mti_env, mdt_pobj);
-                       goto next;
+               if (lnkp) {
+                       rc = mdt_revoke_remote_lookup_lock(info, lnkp, obj);
+                       continue;
                }
 
-               OBD_ALLOC_PTR(mll);
-               if (mll == NULL) {
-                       mdt_object_put(info->mti_env, mdt_pobj);
-                       GOTO(out, rc = -ENOMEM);
+               CDEBUG(D_INFO, "lock "DFID":"DNAME"\n",
+                      PFID(&fid), PNAME(lname));
+
+               lnkp = mdt_object_find(info->mti_env, mdt, &fid);
+               if (IS_ERR(lnkp)) {
+                       CWARN("%s: cannot find obj "DFID": %ld\n",
+                             mdt_obd_name(mdt), PFID(&fid), PTR_ERR(lnkp));
+                       continue;
                }
 
-               /* Since this needs to lock all of objects in linkea, to avoid
-                * deadlocks, because it does not follow parent-child order as
-                * other MDT operation, let's use try_lock here and if the lock
-                * cannot be gotten because of conflicting locks, then drop all
-                * current locks, send an AST to the client, and start again. */
-               mdt_lock_pdo_init(&mll->mll_lh, LCK_PW, &name);
-               rc = mdt_reint_object_lock_try(info, mdt_pobj, &mll->mll_lh,
-                                               MDS_INODELOCK_UPDATE, true);
-               if (rc == 0) {
-                       mdt_unlock_list(info, lock_list, rc);
-
-                       CDEBUG(D_INFO, "%s: busy lock on "DFID" %s retry %d\n",
-                              mdt_obd_name(mdt), PFID(&fid), name.ln_name,
-                              retry_count);
-
-                       if (retry_count == 0) {
-                               mdt_object_put(info->mti_env, mdt_pobj);
-                               OBD_FREE_PTR(mll);
-                               GOTO(out, rc = -EBUSY);
-                       }
+               if (!mdt_object_exists(lnkp)) {
+                       CDEBUG(D_INFO, DFID" doesn't exist, skip "DNAME"\n",
+                             PFID(&fid), PNAME(lname));
+                       mdt_object_put(info->mti_env, lnkp);
+                       continue;
+               }
+
+               if (!mdt_object_remote(lnkp))
+                       local_lnkp_cnt++;
 
-                       rc = mdt_object_lock(info, mdt_pobj, &mll->mll_lh,
+               OBD_ALLOC_PTR(msl);
+               if (msl == NULL)
+                       GOTO(out, rc = -ENOMEM);
+
+               /*
+                * we can't follow parent-child lock order like other MD
+                * operations, use lock_try here to avoid deadlock, if the lock
+                * cannot be taken, drop all locks taken, revoke the blocked
+                * one, and continue processing the remaining entries, and in
+                * the end of the loop restart from beginning.
+                */
+               mdt_lock_pdo_init(&msl->msl_lh, LCK_PW, lname);
+               ibits = 0;
+               rc = mdt_object_lock_try(info, lnkp, &msl->msl_lh, &ibits,
+                                        MDS_INODELOCK_UPDATE, true);
+               if (!(ibits & MDS_INODELOCK_UPDATE)) {
+
+                       CDEBUG(D_INFO, "busy lock on "DFID" "DNAME"\n",
+                              PFID(&fid), PNAME(lname));
+
+                       mdt_unlock_list(info, link_locks, 1);
+                       /* also unlock parent locks to avoid deadlock */
+                       if (!blocked)
+                               mdt_migrate_object_unlock(info, pobj, lhp,
+                                                         peinfo,
+                                                         parent_slave_locks,
+                                                         1);
+
+                       blocked = true;
+
+                       mdt_lock_pdo_init(&msl->msl_lh, LCK_PW, lname);
+                       rc = mdt_object_lock(info, lnkp, &msl->msl_lh,
                                             MDS_INODELOCK_UPDATE);
-                       if (rc != 0) {
-                               mdt_object_put(info->mti_env, mdt_pobj);
-                               OBD_FREE_PTR(mll);
+                       if (rc) {
+                               mdt_object_put(info->mti_env, lnkp);
+                               OBD_FREE_PTR(msl);
                                GOTO(out, rc);
                        }
 
-                       if (mdt_object_remote(mdt_pobj)) {
+                       if (mdt_object_remote(lnkp)) {
                                struct ldlm_lock *lock;
 
-                               /* For remote object, Set lock to cb_atomic,
+                               /*
+                                * for remote object, set lock cb_atomic,
                                 * so lock can be released in blocking_ast()
-                                * immediately, then the next try_lock will
-                                * have better chance to succeds */
-                               lock =
-                               ldlm_handle2lock(&mll->mll_lh.mlh_rreg_lh);
+                                * immediately, then the next lock_try will
+                                * have better chance of success.
+                                */
+                               lock = ldlm_handle2lock(
+                                               &msl->msl_lh.mlh_rreg_lh);
                                LASSERT(lock != NULL);
                                lock_res_and_lock(lock);
                                ldlm_set_atomic_cb(lock);
                                unlock_res_and_lock(lock);
                                LDLM_LOCK_PUT(lock);
                        }
-                       mdt_object_unlock_put(info, mdt_pobj, &mll->mll_lh, rc);
-                       OBD_FREE_PTR(mll);
-                       retry_count--;
-                       goto again;
+
+                       mdt_object_unlock_put(info, lnkp, &msl->msl_lh, 1);
+                       OBD_FREE_PTR(msl);
+                       continue;
                }
-               rc = 0;
-               INIT_LIST_HEAD(&mll->mll_list);
-               mll->mll_obj = mdt_pobj;
-               list_add_tail(&mll->mll_list, lock_list);
-next:
-               ldata.ld_lee = (struct link_ea_entry *)((char *)ldata.ld_lee +
-                                                        ldata.ld_reclen);
+
+               INIT_LIST_HEAD(&msl->msl_linkage);
+               msl->msl_obj = lnkp;
+               list_add_tail(&msl->msl_linkage, link_locks);
+
+               rc = mdt_revoke_remote_lookup_lock(info, lnkp, obj);
        }
+
+       if (blocked)
+               GOTO(out, rc = -EBUSY);
+
+       EXIT;
 out:
-       if (rc != 0)
-               mdt_unlock_list(info, lock_list, rc);
-       RETURN(rc);
+       if (rc)
+               mdt_unlock_list(info, link_locks, rc);
+       else if (local_lnkp_cnt > RS_MAX_LOCKS - 6)
+               /*
+                * parent may have 3 local objects: master object and 2 stripes
+                * (if it's being migrated too); source may have 2 local
+                * objects: master and 1 stripe; target has 1 local object.
+                */
+               rc = 1;
+       return rc;
 }
 
-/* migrate files from one MDT to another MDT */
-static int mdt_reint_migrate_internal(struct mdt_thread_info *info,
-                                     struct mdt_lock_handle *lhc)
+static int mdt_lock_remote_slaves(struct mdt_thread_info *info,
+                                 struct mdt_object *obj,
+                                 const struct md_attr *ma,
+                                 struct list_head *slave_locks)
 {
-       struct mdt_reint_record *rr = &info->mti_rr;
-       struct md_attr          *ma = &info->mti_attr;
-       struct mdt_object       *msrcdir;
-       struct mdt_object       *mold;
-       struct mdt_object       *mnew = NULL;
-       struct mdt_lock_handle  *lh_dirp;
-       struct mdt_lock_handle  *lh_childp;
-       struct mdt_lock_handle  *lh_tgtp = NULL;
-       struct lu_fid           *old_fid = &info->mti_tmp_fid1;
-       struct list_head        lock_list;
-       __u64                   lock_ibits;
-       struct ldlm_lock        *lease = NULL;
-       bool                    lock_open_sem = false;
-       int                     rc;
+       struct mdt_device *mdt = info->mti_mdt;
+       const struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
+       struct lu_fid *fid = &info->mti_tmp_fid1;
+       struct mdt_object *slave;
+       struct mdt_sub_lock *msl;
+       int i;
+       int rc;
+
        ENTRY;
 
-       CDEBUG(D_INODE, "migrate "DFID"/"DNAME" to "DFID"\n", PFID(rr->rr_fid1),
-              PNAME(&rr->rr_name), PFID(rr->rr_fid2));
+       LASSERT(mdt_object_remote(obj));
+       LASSERT(ma->ma_valid & MA_LMV);
+       LASSERT(lmv);
 
-       /* 1: lock the source dir. */
-       msrcdir = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
-       if (IS_ERR(msrcdir)) {
-               CERROR("%s: cannot find source dir "DFID" : rc = %d\n",
-                       mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1),
-                       (int)PTR_ERR(msrcdir));
-               RETURN(PTR_ERR(msrcdir));
+       if (le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_V1)
+               RETURN(-EINVAL);
+
+       if (le32_to_cpu(lmv->lmv_stripe_count) < 1)
+               RETURN(0);
+
+       for (i = 0; i < le32_to_cpu(lmv->lmv_stripe_count); i++) {
+               fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[i]);
+
+               if (!fid_is_sane(fid))
+                       continue;
+
+               slave = mdt_object_find(info->mti_env, mdt, fid);
+               if (IS_ERR(slave))
+                       GOTO(out, rc = PTR_ERR(slave));
+
+               OBD_ALLOC_PTR(msl);
+               if (!msl) {
+                       mdt_object_put(info->mti_env, slave);
+                       GOTO(out, rc = -ENOMEM);
+               }
+
+               mdt_lock_reg_init(&msl->msl_lh, LCK_EX);
+               rc = mdt_reint_object_lock(info, slave, &msl->msl_lh,
+                                          MDS_INODELOCK_UPDATE, true);
+               if (rc) {
+                       OBD_FREE_PTR(msl);
+                       mdt_object_put(info->mti_env, slave);
+                       GOTO(out, rc);
+               }
+
+               INIT_LIST_HEAD(&msl->msl_linkage);
+               msl->msl_obj = slave;
+               list_add_tail(&msl->msl_linkage, slave_locks);
        }
+       EXIT;
 
-       lh_dirp = &info->mti_lh[MDT_LH_PARENT];
-       mdt_lock_pdo_init(lh_dirp, LCK_PW, &rr->rr_name);
-       rc = mdt_reint_object_lock(info, msrcdir, lh_dirp, MDS_INODELOCK_UPDATE,
-                                  true);
+out:
        if (rc)
-               GOTO(out_put_parent, rc);
+               mdt_unlock_list(info, slave_locks, rc);
+       return rc;
+}
 
-       if (!mdt_object_remote(msrcdir)) {
-               rc = mdt_version_get_check_save(info, msrcdir, 0);
-               if (rc)
-                       GOTO(out_unlock_parent, rc);
+/* lock parent and its stripes */
+static int mdt_migrate_parent_lock(struct mdt_thread_info *info,
+                                  struct mdt_object *obj,
+                                  const struct md_attr *ma,
+                                  struct mdt_lock_handle *lh,
+                                  struct ldlm_enqueue_info *einfo,
+                                  struct list_head *slave_locks)
+{
+       int rc;
+
+       if (mdt_object_remote(obj)) {
+               rc = mdt_remote_object_lock(info, obj, mdt_object_fid(obj),
+                                           &lh->mlh_rreg_lh, LCK_PW,
+                                           MDS_INODELOCK_UPDATE, false);
+               if (rc != ELDLM_OK)
+                       return rc;
+
+               /*
+                * if obj is remote and striped, lock its stripes explicitly
+                * because it's not striped in LOD layer on this MDT.
+                */
+               if (ma->ma_valid & MA_LMV) {
+                       rc = mdt_lock_remote_slaves(info, obj, ma, slave_locks);
+                       if (rc)
+                               mdt_object_unlock(info, obj, lh, rc);
+               }
+       } else {
+               rc = mdt_reint_striped_lock(info, obj, lh, MDS_INODELOCK_UPDATE,
+                                           einfo, true);
        }
 
-       /* 2: sanity check and find the object to be migrated. */
-       fid_zero(old_fid);
-       rc = mdt_lookup_version_check(info, msrcdir, &rr->rr_name, old_fid, 2);
-       if (rc != 0)
-               GOTO(out_unlock_parent, rc);
+       return rc;
+}
 
-       if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
-               GOTO(out_unlock_parent, rc = -EINVAL);
+/*
+ * in migration, object may be remote, and we need take full lock of it and its
+ * stripes if it's directory, besides, object may be a remote object on its
+ * parent, revoke its LOOKUP lock on where its parent is located.
+ */
+static int mdt_migrate_object_lock(struct mdt_thread_info *info,
+                                  struct mdt_object *pobj,
+                                  struct mdt_object *obj,
+                                  struct mdt_lock_handle *lh,
+                                  struct ldlm_enqueue_info *einfo,
+                                  struct list_head *slave_locks)
+{
+       int rc;
 
-       if (!fid_is_md_operative(old_fid))
-               GOTO(out_unlock_parent, rc = -EPERM);
+       if (mdt_object_remote(obj)) {
+               rc = mdt_revoke_remote_lookup_lock(info, pobj, obj);
+               if (rc)
+                       return rc;
 
-       if (lu_fid_eq(old_fid, &info->mti_mdt->mdt_md_root_fid))
-               GOTO(out_unlock_parent, rc = -EPERM);
+               rc = mdt_remote_object_lock(info, obj, mdt_object_fid(obj),
+                                           &lh->mlh_rreg_lh, LCK_EX,
+                                           MDS_INODELOCK_FULL, false);
+               if (rc != ELDLM_OK)
+                       return rc;
 
-       mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
-       if (IS_ERR(mold))
-               GOTO(out_unlock_parent, rc = PTR_ERR(mold));
+               /*
+                * if obj is remote and striped, lock its stripes explicitly
+                * because it's not striped in LOD layer on this MDT.
+                */
+               if (S_ISDIR(lu_object_attr(&obj->mot_obj))) {
+                       struct md_attr *ma = &info->mti_attr;
+
+                       ma->ma_lmv = info->mti_big_lmm;
+                       ma->ma_lmv_size = info->mti_big_lmmsize;
+                       ma->ma_valid = 0;
+                       rc = mdt_stripe_get(info, obj, ma, XATTR_NAME_LMV);
+                       if (rc) {
+                               mdt_object_unlock(info, obj, lh, rc);
+                               return rc;
+                       }
 
-       if (mdt_object_remote(mold)) {
-               CERROR("%s: source "DFID" is on the remote MDT\n",
-                      mdt_obd_name(info->mti_mdt), PFID(old_fid));
-               GOTO(out_put_child, rc = -EREMOTE);
-       }
+                       if (ma->ma_valid & MA_LMV) {
+                               rc = mdt_lock_remote_slaves(info, obj, ma,
+                                                           slave_locks);
+                               if (rc)
+                                       mdt_object_unlock(info, obj, lh, rc);
+                       }
+               }
+       } else {
+               if (mdt_object_remote(pobj)) {
+                       rc = mdt_revoke_remote_lookup_lock(info, pobj, obj);
+                       if (rc)
+                               return rc;
+               }
 
-       if (S_ISREG(lu_object_attr(&mold->mot_obj)) &&
-           !mdt_object_remote(msrcdir)) {
-               CERROR("%s: parent "DFID" is still on the same"
-                      " MDT, which should be migrated first:"
-                      " rc = %d\n", mdt_obd_name(info->mti_mdt),
-                      PFID(mdt_object_fid(msrcdir)), -EPERM);
-               GOTO(out_put_child, rc = -EPERM);
+               rc = mdt_reint_striped_lock(info, obj, lh, MDS_INODELOCK_FULL,
+                                           einfo, true);
        }
 
-       rc = mdt_remote_permission(info);
-       if (rc != 0)
-               GOTO(out_put_child, rc);
+       return rc;
+}
 
-       /* 3: iterate the linkea of the object and lock all of the objects */
-       INIT_LIST_HEAD(&lock_list);
-       rc = mdt_lock_objects_in_linkea(info, mold, msrcdir, &lock_list);
-       if (rc != 0)
-               GOTO(out_put_child, rc);
+/*
+ * lookup source by name, if parent is striped directory, we need to find the
+ * corresponding stripe where source is located, and then lookup there.
+ *
+ * besides, if parent is migrating too, and file is already in target stripe,
+ * this should be a redo of 'lfs migrate' on client side.
+ */
+static int mdt_migrate_lookup(struct mdt_thread_info *info,
+                             struct mdt_object *pobj,
+                             const struct md_attr *ma,
+                             const struct lu_name *lname,
+                             struct mdt_object **spobj,
+                             struct mdt_object **sobj)
+{
+       const struct lu_env *env = info->mti_env;
+       struct lu_fid *fid = &info->mti_tmp_fid1;
+       struct mdt_object *stripe;
+       int rc;
 
-       if (info->mti_spec.sp_migrate_close) {
-               struct close_data *data;
-               struct mdt_body  *repbody;
-               bool lease_broken = false;
+       if (ma->ma_valid & MA_LMV) {
+               /* if parent is striped, lookup on corresponding stripe */
+               struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
+               __u32 hash_type = le32_to_cpu(lmv->lmv_hash_type);
+               __u32 stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
+               bool is_migrating = le32_to_cpu(lmv->lmv_hash_type) &
+                                   LMV_HASH_FLAG_MIGRATION;
+
+               if (is_migrating) {
+                       hash_type = le32_to_cpu(lmv->lmv_migrate_hash);
+                       stripe_count -= le32_to_cpu(lmv->lmv_migrate_offset);
+               }
 
-               if (!req_capsule_field_present(info->mti_pill, &RMF_MDT_EPOCH,
-                                     RCL_CLIENT) ||
-                   !req_capsule_field_present(info->mti_pill, &RMF_CLOSE_DATA,
-                                     RCL_CLIENT))
-                       GOTO(out_lease, rc = -EPROTO);
+               rc = lmv_name_to_stripe_index(hash_type, stripe_count,
+                                             lname->ln_name,
+                                             lname->ln_namelen);
+               if (rc < 0)
+                       return rc;
 
-               data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
-               if (data == NULL)
-                       GOTO(out_lease, rc = -EPROTO);
+               if (le32_to_cpu(lmv->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
+                       rc += le32_to_cpu(lmv->lmv_migrate_offset);
+
+               fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[rc]);
+
+               stripe = mdt_object_find(env, info->mti_mdt, fid);
+               if (IS_ERR(stripe))
+                       return PTR_ERR(stripe);
+
+               fid_zero(fid);
+               rc = mdo_lookup(env, mdt_object_child(stripe), lname, fid,
+                               &info->mti_spec);
+               if (rc == -ENOENT && is_migrating) {
+                       /*
+                        * if parent is migrating, and lookup child failed on
+                        * source stripe, lookup again on target stripe, if it
+                        * exists, it means previous migration was interrupted,
+                        * and current file was migrated already.
+                        */
+                       mdt_object_put(env, stripe);
+
+                       hash_type = le32_to_cpu(lmv->lmv_hash_type);
+                       stripe_count = le32_to_cpu(lmv->lmv_migrate_offset);
+
+                       rc = lmv_name_to_stripe_index(hash_type, stripe_count,
+                                                     lname->ln_name,
+                                                     lname->ln_namelen);
+                       if (rc < 0)
+                               return rc;
 
-               lease = ldlm_handle2lock(&data->cd_handle);
-               if (lease == NULL)
-                       GOTO(out_lease, rc = -ESTALE);
+                       fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[rc]);
 
-               /* try to hold open_sem so that nobody else can open the file */
-               if (!down_write_trylock(&mold->mot_open_sem)) {
-                       ldlm_lock_cancel(lease);
-                       GOTO(out_lease, rc = -EBUSY);
-               }
+                       stripe = mdt_object_find(env, info->mti_mdt, fid);
+                       if (IS_ERR(stripe))
+                               return PTR_ERR(stripe);
 
-               lock_open_sem = true;
-               /* Check if the lease open lease has already canceled */
-               lock_res_and_lock(lease);
-               lease_broken = ldlm_is_cancel(lease);
-               unlock_res_and_lock(lease);
+                       fid_zero(fid);
+                       rc = mdo_lookup(env, mdt_object_child(stripe), lname,
+                                       fid, &info->mti_spec);
+                       mdt_object_put(env, stripe);
+                       return rc ?: -EALREADY;
+               } else if (rc) {
+                       mdt_object_put(env, stripe);
+                       return rc;
+               }
+       } else {
+               fid_zero(fid);
+               rc = mdo_lookup(env, mdt_object_child(pobj), lname, fid,
+                               &info->mti_spec);
+               if (rc)
+                       return rc;
 
-               LDLM_DEBUG(lease, DFID " lease broken? %d",
-                          PFID(mdt_object_fid(mold)), lease_broken);
+               stripe = pobj;
+               mdt_object_get(env, stripe);
+       }
 
-               /* Cancel server side lease. Client side counterpart should
-                * have been cancelled. It's okay to cancel it now as we've
-                * held mot_open_sem. */
-               ldlm_lock_cancel(lease);
+       *spobj = stripe;
 
-               if (lease_broken)
-                       GOTO(out_lease, rc = -EAGAIN);
-out_lease:
-               rc = mdt_close_internal(info, mdt_info_req(info), NULL);
-               repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
-               repbody->mbo_valid |= OBD_MD_CLOSE_INTENT_EXECED;
-               if (rc != 0)
-                       GOTO(out_unlock_list, rc);
+       *sobj = mdt_object_find(env, info->mti_mdt, fid);
+       if (IS_ERR(*sobj)) {
+               mdt_object_put(env, stripe);
+               rc = PTR_ERR(*sobj);
+               *spobj = NULL;
+               *sobj = NULL;
        }
 
-       /* 4: lock of the object migrated object */
-       lh_childp = &info->mti_lh[MDT_LH_OLD];
-       mdt_lock_reg_init(lh_childp, LCK_EX);
-       lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
-                    MDS_INODELOCK_LAYOUT;
-       if (mdt_object_remote(msrcdir)) {
-               /* Enqueue lookup lock from the parent MDT */
-               rc = mdt_remote_object_lock(info, msrcdir, mdt_object_fid(mold),
-                                           &lh_childp->mlh_rreg_lh,
-                                           lh_childp->mlh_rreg_mode,
-                                           MDS_INODELOCK_LOOKUP, false);
-               if (rc != ELDLM_OK)
-                       GOTO(out_unlock_list, rc);
+       return rc;
+}
 
-               lock_ibits &= ~MDS_INODELOCK_LOOKUP;
-       }
+/* end lease and close file for regular file */
+static int mdd_migrate_close(struct mdt_thread_info *info,
+                            struct mdt_object *obj)
+{
+       struct close_data *data;
+       struct mdt_body *repbody;
+       struct ldlm_lock *lease;
+       int rc;
+       int rc2;
 
-       rc = mdt_reint_object_lock(info, mold, lh_childp, lock_ibits, true);
-       if (rc != 0)
-               GOTO(out_unlock_child, rc);
+       rc = -EPROTO;
+       if (!req_capsule_field_present(info->mti_pill, &RMF_MDT_EPOCH,
+                                     RCL_CLIENT) ||
+           !req_capsule_field_present(info->mti_pill, &RMF_CLOSE_DATA,
+                                     RCL_CLIENT))
+               goto close;
 
-       /* Migration is incompatible with HSM. */
-       ma->ma_need = MA_HSM;
-       ma->ma_valid = 0;
-       rc = mdt_attr_get_complex(info, mold, ma);
-       if (rc != 0)
-               GOTO(out_unlock_child, rc);
+       data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
+       if (!data)
+               goto close;
+
+       rc = -ESTALE;
+       lease = ldlm_handle2lock(&data->cd_handle);
+       if (!lease)
+               goto close;
 
-       if ((ma->ma_valid & MA_HSM) && ma->ma_hsm.mh_flags != 0) {
-               rc = -ENOSYS;
-               CERROR("%s: cannot migrate HSM archived file "DFID": rc = %d\n",
-                      mdt_obd_name(info->mti_mdt), PFID(old_fid), rc);
-               GOTO(out_unlock_child, rc);
+       /* check if the lease was already canceled */
+       lock_res_and_lock(lease);
+       rc = ldlm_is_cancel(lease);
+       unlock_res_and_lock(lease);
+
+       if (rc) {
+               rc = -EAGAIN;
+               LDLM_DEBUG(lease, DFID" lease broken",
+                          PFID(mdt_object_fid(obj)));
        }
 
-       ma->ma_need = MA_LMV;
-       ma->ma_valid = 0;
-       ma->ma_lmv = (union lmv_mds_md *)info->mti_xattr_buf;
-       ma->ma_lmv_size = sizeof(info->mti_xattr_buf);
-       rc = mdt_stripe_get(info, mold, ma, XATTR_NAME_LMV);
-       if (rc != 0)
-               GOTO(out_unlock_child, rc);
-
-       if ((ma->ma_valid & MA_LMV)) {
-               struct lmv_mds_md_v1 *lmm1;
-
-               lmv_le_to_cpu(ma->ma_lmv, ma->ma_lmv);
-               lmm1 = &ma->ma_lmv->lmv_md_v1;
-               if (!(lmm1->lmv_hash_type & LMV_HASH_FLAG_MIGRATION)) {
-                       CERROR("%s: can not migrate striped dir "DFID
-                              ": rc = %d\n", mdt_obd_name(info->mti_mdt),
-                              PFID(mdt_object_fid(mold)), -EPERM);
-                       GOTO(out_unlock_child, rc = -EPERM);
-               }
+       /*
+        * cancel server side lease, client side counterpart should have been
+        * cancelled, it's okay to cancel it now as we've held mot_open_sem.
+        */
+       ldlm_lock_cancel(lease);
+       ldlm_reprocess_all(lease->l_resource);
+       LDLM_LOCK_PUT(lease);
 
-               if (!fid_is_sane(&lmm1->lmv_stripe_fids[1]))
-                       GOTO(out_unlock_child, rc = -EINVAL);
+close:
+       rc2 = mdt_close_internal(info, mdt_info_req(info), NULL);
+       repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
+       repbody->mbo_valid |= OBD_MD_CLOSE_INTENT_EXECED;
 
-               mnew = mdt_object_find(info->mti_env, info->mti_mdt,
-                                      &lmm1->lmv_stripe_fids[1]);
-               if (IS_ERR(mnew))
-                       GOTO(out_unlock_child, rc = PTR_ERR(mnew));
+       return rc ?: rc2;
+}
 
-               if (!mdt_object_remote(mnew)) {
-                       CERROR("%s: "DFID" being migrated is on this MDT:"
-                              " rc  = %d\n", mdt_obd_name(info->mti_mdt),
-                              PFID(rr->rr_fid2), -EPERM);
-                       GOTO(out_put_new, rc = -EPERM);
-               }
+/*
+ * migrate file in below steps:
+ *  1. lock parent and its stripes
+ *  2. lookup source by name
+ *  3. lock parents of source links if source is not directory
+ *  4. reject if source is in HSM
+ *  5. take source open_sem and close file if source is regular file
+ *  6. lock source and its stripes if it's directory
+ *  7. lock target so subsequent change to it can trigger COS
+ *  8. migrate file
+ *  9. unlock above locks
+ * 10. sync device if source has links
+ */
+static int mdt_reint_migrate(struct mdt_thread_info *info,
+                            struct mdt_lock_handle *unused)
+{
+       const struct lu_env *env = info->mti_env;
+       struct mdt_device *mdt = info->mti_mdt;
+       struct ptlrpc_request *req = mdt_info_req(info);
+       struct mdt_reint_record *rr = &info->mti_rr;
+       struct lu_ucred *uc = mdt_ucred(info);
+       struct md_attr *ma = &info->mti_attr;
+       struct ldlm_enqueue_info *peinfo = &info->mti_einfo[0];
+       struct ldlm_enqueue_info *seinfo = &info->mti_einfo[1];
+       struct mdt_object *pobj;
+       struct mdt_object *spobj = NULL;
+       struct mdt_object *sobj = NULL;
+       struct mdt_object *tobj;
+       struct lustre_handle rename_lh = { 0 };
+       struct mdt_lock_handle *lhp;
+       struct mdt_lock_handle *lhs;
+       struct mdt_lock_handle *lht;
+       LIST_HEAD(parent_slave_locks);
+       LIST_HEAD(child_slave_locks);
+       LIST_HEAD(link_locks);
+       int lock_retries = 5;
+       bool open_sem_locked = false;
+       bool do_sync = false;
+       int rc;
+       ENTRY;
 
-               lh_tgtp = &info->mti_lh[MDT_LH_CHILD];
-               mdt_lock_reg_init(lh_tgtp, LCK_EX);
-               rc = mdt_remote_object_lock(info, mnew,
-                                           mdt_object_fid(mnew),
-                                           &lh_tgtp->mlh_rreg_lh,
-                                           lh_tgtp->mlh_rreg_mode,
-                                           MDS_INODELOCK_UPDATE, false);
+       CDEBUG(D_INODE, "migrate "DFID"/"DNAME" to "DFID"\n", PFID(rr->rr_fid1),
+              PNAME(&rr->rr_name), PFID(rr->rr_fid2));
+
+       if (info->mti_dlm_req)
+               ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
+
+       if (!fid_is_md_operative(rr->rr_fid1) ||
+           !fid_is_md_operative(rr->rr_fid2))
+               RETURN(-EPERM);
+
+       /* don't allow migrate . or .. */
+       if (lu_name_is_dot_or_dotdot(&rr->rr_name))
+               RETURN(-EBUSY);
+
+       if (!mdt->mdt_enable_remote_dir || !mdt->mdt_enable_dir_migration)
+               RETURN(-EPERM);
+
+       if (!md_capable(uc, CFS_CAP_SYS_ADMIN) &&
+           uc->uc_gid != mdt->mdt_enable_remote_dir_gid &&
+           mdt->mdt_enable_remote_dir_gid != -1)
+               RETURN(-EPERM);
+
+       /*
+        * Note: do not enqueue rename lock for replay request, because
+        * if other MDT holds rename lock, but being blocked to wait for
+        * this MDT to finish its recovery, and the failover MDT can not
+        * get rename lock, which will cause deadlock.
+        */
+       if (!req_is_replay(req)) {
+               rc = mdt_rename_lock(info, &rename_lh);
                if (rc != 0) {
-                       lh_tgtp = NULL;
-                       GOTO(out_put_new, rc);
-               }
-       } else {
-               mnew = mdt_object_find(info->mti_env, info->mti_mdt,
-                                      rr->rr_fid2);
-               if (IS_ERR(mnew))
-                       GOTO(out_unlock_child, rc = PTR_ERR(mnew));
-               if (!mdt_object_remote(mnew)) {
-                       CERROR("%s: Migration "DFID" is on this MDT:"
-                              " rc = %d\n", mdt_obd_name(info->mti_mdt),
-                              PFID(rr->rr_fid2), -EXDEV);
-                       GOTO(out_put_new, rc = -EXDEV);
+                       CERROR("%s: can't lock FS for rename: rc = %d\n",
+                              mdt_obd_name(info->mti_mdt), rc);
+                       RETURN(rc);
                }
        }
 
-       /* 5: migrate it */
-       mdt_reint_init_ma(info, ma);
+       /* pobj is master object of parent */
+       pobj = mdt_parent_find_check(info, rr->rr_fid1, 0);
+       if (IS_ERR(pobj))
+               GOTO(unlock_rename, rc = PTR_ERR(pobj));
 
-       mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
-                      OBD_FAIL_MDS_REINT_RENAME_WRITE);
+       if (unlikely(!info->mti_big_lmm)) {
+               info->mti_big_lmmsize = lmv_mds_md_size(64, LMV_MAGIC);
+               OBD_ALLOC(info->mti_big_lmm, info->mti_big_lmmsize);
+               if (!info->mti_big_lmm)
+                       GOTO(put_parent, rc = -ENOMEM);
+       }
 
-       rc = mdo_migrate(info->mti_env, mdt_object_child(msrcdir),
-                        mdt_object_child(mold), &rr->rr_name,
-                        mdt_object_child(mnew), ma);
-       if (rc != 0)
-               GOTO(out_unlock_new, rc);
+       ma->ma_lmv = info->mti_big_lmm;
+       ma->ma_lmv_size = info->mti_big_lmmsize;
+       ma->ma_valid = 0;
+       rc = mdt_stripe_get(info, pobj, ma, XATTR_NAME_LMV);
+       if (rc)
+               GOTO(put_parent, rc);
 
-out_unlock_new:
-       if (lh_tgtp != NULL)
-               mdt_object_unlock(info, mnew, lh_tgtp, rc);
-out_put_new:
-       if (mnew)
-               mdt_object_put(info->mti_env, mnew);
-out_unlock_child:
-       mdt_object_unlock(info, mold, lh_childp, rc);
-out_unlock_list:
-       /* we don't really modify linkea objects, so we can safely decref these
-        * locks, and this can avoid saving them as COS locks, which may prevent
-        * subsequent migrate. */
-       mdt_unlock_list(info, &lock_list, 1);
-       if (lease != NULL) {
-               ldlm_reprocess_all(lease->l_resource);
-               LDLM_LOCK_PUT(lease);
+lock_parent:
+       /* lock parent object */
+       lhp = &info->mti_lh[MDT_LH_PARENT];
+       mdt_lock_reg_init(lhp, LCK_PW);
+       rc = mdt_migrate_parent_lock(info, pobj, ma, lhp, peinfo,
+                                    &parent_slave_locks);
+       if (rc)
+               GOTO(put_parent, rc);
+
+       /*
+        * spobj is the corresponding stripe against name if pobj is striped
+        * directory, which is the real parent, and no need to lock, because
+        * we've taken full lock of pobj.
+        */
+       rc = mdt_migrate_lookup(info, pobj, ma, &rr->rr_name, &spobj, &sobj);
+       if (rc)
+               GOTO(unlock_parent, rc);
+
+       /* lock parents of source links, and revoke LOOKUP lock of links */
+       rc = mdt_link_parents_lock(info, pobj, ma, sobj, lhp, peinfo,
+                                  &parent_slave_locks, &link_locks);
+       if (rc == -EBUSY && lock_retries-- > 0) {
+               mdt_object_put(env, sobj);
+               mdt_object_put(env, spobj);
+               goto lock_parent;
        }
 
-       if (lock_open_sem)
-               up_write(&mold->mot_open_sem);
-out_put_child:
-       mdt_object_put(info->mti_env, mold);
-out_unlock_parent:
-       mdt_object_unlock(info, msrcdir, lh_dirp, rc);
-out_put_parent:
-       mdt_object_put(info->mti_env, msrcdir);
+       if (rc < 0)
+               GOTO(put_source, rc);
 
-       RETURN(rc);
-}
+       /*
+        * RS_MAX_LOCKS is the limit of number of locks that can be saved along
+        * with one request, if total lock count exceeds this limit, we will
+        * drop all locks after migration, and synchronous device in the end.
+        */
+       do_sync = rc;
 
-static struct mdt_object *mdt_object_find_check(struct mdt_thread_info *info,
-                                               const struct lu_fid *fid,
-                                               int idx)
-{
-       struct mdt_object *dir;
-       int rc;
-       ENTRY;
+       /* TODO: DoM migration is not supported yet */
+       if (S_ISREG(lu_object_attr(&sobj->mot_obj))) {
+               ma->ma_lmm = info->mti_big_lmm;
+               ma->ma_lmm_size = info->mti_big_lmmsize;
+               ma->ma_valid = 0;
+               rc = mdt_stripe_get(info, sobj, ma, XATTR_NAME_LOV);
+               if (rc)
+                       GOTO(put_source, rc);
 
-       dir = mdt_object_find(info->mti_env, info->mti_mdt, fid);
-       if (IS_ERR(dir))
-               RETURN(dir);
+               if (ma->ma_valid & MA_LOV &&
+                   mdt_lmm_dom_entry(ma->ma_lmm) != LMM_NO_DOM)
+                       GOTO(put_source, rc = -EOPNOTSUPP);
+       }
 
-       /* check early, the real version will be saved after locking */
-       rc = mdt_version_get_check(info, dir, idx);
+       /* if migration HSM is allowed */
+       if (!mdt->mdt_opts.mo_migrate_hsm_allowed) {
+               ma->ma_need = MA_HSM;
+               ma->ma_valid = 0;
+               rc = mdt_attr_get_complex(info, sobj, ma);
+               if (rc)
+                       GOTO(unlock_links, rc);
+
+               if ((ma->ma_valid & MA_HSM) && ma->ma_hsm.mh_flags != 0)
+                       GOTO(unlock_links, rc = -EOPNOTSUPP);
+       }
+
+       /* end lease and close file for regular file */
+       if (info->mti_spec.sp_migrate_close) {
+               /* try to hold open_sem so that nobody else can open the file */
+               if (!down_write_trylock(&sobj->mot_open_sem)) {
+                       /* close anyway */
+                       mdd_migrate_close(info, sobj);
+                       GOTO(unlock_links, rc = -EBUSY);
+               } else {
+                       open_sem_locked = true;
+                       rc = mdd_migrate_close(info, sobj);
+                       if (rc)
+                               GOTO(unlock_open_sem, rc);
+               }
+       }
+
+       /* lock source */
+       lhs = &info->mti_lh[MDT_LH_OLD];
+       mdt_lock_reg_init(lhs, LCK_EX);
+       rc = mdt_migrate_object_lock(info, spobj, sobj, lhs, seinfo,
+                                    &child_slave_locks);
        if (rc)
-               GOTO(out_put, rc);
+               GOTO(unlock_open_sem, rc);
 
-       RETURN(dir);
-out_put:
-       mdt_object_put(info->mti_env, dir);
-       return ERR_PTR(rc);
+       /* lock target */
+       tobj = mdt_object_find(env, mdt, rr->rr_fid2);
+       if (IS_ERR(tobj))
+               GOTO(unlock_source, rc = PTR_ERR(tobj));
+
+       lht = &info->mti_lh[MDT_LH_NEW];
+       mdt_lock_reg_init(lht, LCK_EX);
+       rc = mdt_reint_object_lock(info, tobj, lht, MDS_INODELOCK_FULL, true);
+       if (rc)
+               GOTO(put_target, rc);
+
+       /* Don't do lookup sanity check. We know name doesn't exist. */
+       info->mti_spec.sp_cr_lookup = 0;
+       info->mti_spec.sp_feat = &dt_directory_features;
+
+       rc = mdo_migrate(env, mdt_object_child(pobj),
+                        mdt_object_child(sobj), &rr->rr_name,
+                        mdt_object_child(tobj), &info->mti_spec, ma);
+       EXIT;
+
+       mdt_object_unlock(info, tobj, lht, rc);
+put_target:
+       mdt_object_put(env, tobj);
+unlock_source:
+       mdt_migrate_object_unlock(info, sobj, lhs, seinfo,
+                                 &child_slave_locks, rc);
+unlock_open_sem:
+       if (open_sem_locked)
+               up_write(&sobj->mot_open_sem);
+unlock_links:
+       mdt_unlock_list(info, &link_locks, do_sync ? 1 : rc);
+put_source:
+       mdt_object_put(env, sobj);
+       mdt_object_put(env, spobj);
+unlock_parent:
+       mdt_migrate_object_unlock(info, pobj, lhp, peinfo,
+                                 &parent_slave_locks, rc);
+put_parent:
+       mdt_object_put(env, pobj);
+unlock_rename:
+       if (lustre_handle_is_used(&rename_lh))
+               mdt_rename_unlock(&rename_lh);
+
+       if (!rc && do_sync)
+               mdt_device_sync(env, mdt);
+
+       return rc;
 }
 
 static int mdt_object_lock_save(struct mdt_thread_info *info,
@@ -1822,24 +2215,106 @@ static int mdt_object_lock_save(struct mdt_thread_info *info,
 }
 
 /*
+ * determine lock order of sobj and tobj
+ *
+ * there are two situations we need to lock tobj before sobj:
+ * 1. sobj is child of tobj
+ * 2. sobj and tobj are stripes of a directory, and stripe index of sobj is
+ *    larger than that of tobj
+ *
+ * \retval     1 lock tobj before sobj
+ * \retval     0 lock sobj before tobj
+ * \retval     -ev negative errno upon error
+ */
+static int mdt_rename_determine_lock_order(struct mdt_thread_info *info,
+                                          struct mdt_object *sobj,
+                                          struct mdt_object *tobj)
+{
+       struct md_attr *ma = &info->mti_attr;
+       struct lu_fid *spfid = &info->mti_tmp_fid1;
+       struct lu_fid *tpfid = &info->mti_tmp_fid2;
+       struct lmv_mds_md_v1 *lmv;
+       __u32 sindex;
+       __u32 tindex;
+       int rc;
+
+       /* sobj and tobj are the same */
+       if (sobj == tobj)
+               return 0;
+
+       if (fid_is_root(mdt_object_fid(sobj)))
+               return 0;
+
+       if (fid_is_root(mdt_object_fid(tobj)))
+               return 1;
+
+       /* check whether sobj is child of tobj */
+       rc = mdo_is_subdir(info->mti_env, mdt_object_child(sobj),
+                          mdt_object_fid(tobj));
+       if (rc < 0)
+               return rc;
+
+       if (rc == 1)
+               return 1;
+
+       /* check whether sobj and tobj are children of the same parent */
+       rc = mdt_attr_get_pfid(info, sobj, spfid);
+       if (rc)
+               return rc;
+
+       rc = mdt_attr_get_pfid(info, tobj, tpfid);
+       if (rc)
+               return rc;
+
+       if (!lu_fid_eq(spfid, tpfid))
+               return 0;
+
+       /* check whether sobj and tobj are sibling stripes */
+       ma->ma_need = MA_LMV;
+       ma->ma_valid = 0;
+       ma->ma_lmv = (union lmv_mds_md *)info->mti_xattr_buf;
+       ma->ma_lmv_size = sizeof(info->mti_xattr_buf);
+       rc = mdt_stripe_get(info, sobj, ma, XATTR_NAME_LMV);
+       if (rc)
+               return rc;
+
+       if (!(ma->ma_valid & MA_LMV))
+               return 0;
+
+       lmv = &ma->ma_lmv->lmv_md_v1;
+       if (!(le32_to_cpu(lmv->lmv_magic) & LMV_MAGIC_STRIPE))
+               return 0;
+       sindex = le32_to_cpu(lmv->lmv_master_mdt_index);
+
+       ma->ma_valid = 0;
+       rc = mdt_stripe_get(info, tobj, ma, XATTR_NAME_LMV);
+       if (rc)
+               return rc;
+
+       if (!(ma->ma_valid & MA_LMV))
+               return -ENODATA;
+
+       lmv = &ma->ma_lmv->lmv_md_v1;
+       if (!(le32_to_cpu(lmv->lmv_magic) & LMV_MAGIC_STRIPE))
+               return -EINVAL;
+       tindex = le32_to_cpu(lmv->lmv_master_mdt_index);
+
+       /* check stripe index of sobj and tobj */
+       if (sindex == tindex)
+               return -EINVAL;
+
+       return sindex < tindex ? 0 : 1;
+}
+
+/*
  * VBR: rename versions in reply: 0 - srcdir parent; 1 - tgtdir parent;
  * 2 - srcdir child; 3 - tgtdir child.
  * Update on disk version of srcdir child.
  */
-/**
- * For DNE phase I, only these renames are allowed
- *     mv src_p/src_c tgt_p/tgt_c
- * 1. src_p/src_c/tgt_p/tgt_c are in the same MDT.
- * 2. src_p and tgt_p are same directory, and tgt_c does not
- *    exists. In this case, all of modification will happen
- *    in the MDT where ithesource parent is, only one remote
- *    update is needed, i.e. set c_time/m_time on the child.
- *    And tgt_c will be still in the same MDT as the original
- *    src_c.
- */
-static int mdt_reint_rename_internal(struct mdt_thread_info *info,
-                                    struct mdt_lock_handle *lhc)
+static int mdt_reint_rename(struct mdt_thread_info *info,
+                           struct mdt_lock_handle *unused)
 {
+       struct mdt_device *mdt = info->mti_mdt;
        struct mdt_reint_record *rr = &info->mti_rr;
        struct md_attr *ma = &info->mti_attr;
        struct ptlrpc_request *req = mdt_info_req(info);
@@ -1847,6 +2322,7 @@ static int mdt_reint_rename_internal(struct mdt_thread_info *info,
        struct mdt_object *mtgtdir = NULL;
        struct mdt_object *mold;
        struct mdt_object *mnew = NULL;
+       struct lustre_handle rename_lh = { 0 };
        struct mdt_lock_handle *lh_srcdirp;
        struct mdt_lock_handle *lh_tgtdirp;
        struct mdt_lock_handle *lh_oldp = NULL;
@@ -1854,7 +2330,7 @@ static int mdt_reint_rename_internal(struct mdt_thread_info *info,
        struct lu_fid *old_fid = &info->mti_tmp_fid1;
        struct lu_fid *new_fid = &info->mti_tmp_fid2;
        __u64 lock_ibits;
-       bool reverse = false;
+       bool reverse = false, discard = false;
        bool cos_incompat;
        int rc;
        ENTRY;
@@ -1863,8 +2339,15 @@ static int mdt_reint_rename_internal(struct mdt_thread_info *info,
                  PFID(rr->rr_fid1), PNAME(&rr->rr_name),
                  PFID(rr->rr_fid2), PNAME(&rr->rr_tgt_name));
 
+       if (info->mti_dlm_req)
+               ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
+
+       if (!fid_is_md_operative(rr->rr_fid1) ||
+           !fid_is_md_operative(rr->rr_fid2))
+               RETURN(-EPERM);
+
        /* find both parents. */
-       msrcdir = mdt_object_find_check(info, rr->rr_fid1, 0);
+       msrcdir = mdt_parent_find_check(info, rr->rr_fid1, 0);
        if (IS_ERR(msrcdir))
                RETURN(PTR_ERR(msrcdir));
 
@@ -1874,19 +2357,44 @@ static int mdt_reint_rename_internal(struct mdt_thread_info *info,
                mtgtdir = msrcdir;
                mdt_object_get(info->mti_env, mtgtdir);
        } else {
-               /* Check if the @msrcdir is not a child of the @mtgtdir,
-                * otherwise a reverse locking must take place. */
-               rc = mdt_is_subdir(info, msrcdir, rr->rr_fid2);
-               if (rc == -EINVAL)
-                       reverse = true;
-               else if (rc)
-                       GOTO(out_put_srcdir, rc);
-
-               mtgtdir = mdt_object_find_check(info, rr->rr_fid2, 1);
+               mtgtdir = mdt_parent_find_check(info, rr->rr_fid2, 1);
                if (IS_ERR(mtgtdir))
                        GOTO(out_put_srcdir, rc = PTR_ERR(mtgtdir));
        }
 
+       /*
+        * Note: do not enqueue rename lock for replay request, because
+        * if other MDT holds rename lock, but being blocked to wait for
+        * this MDT to finish its recovery, and the failover MDT can not
+        * get rename lock, which will cause deadlock.
+        */
+       if (!req_is_replay(req)) {
+               /*
+                * Normally rename RPC is handled on the MDT with the target
+                * directory (if target exists, it's on the MDT with the
+                * target), if the source directory is remote, it's a hint that
+                * source is remote too (this may not be true, but it won't
+                * cause any issue), return -EXDEV early to avoid taking
+                * rename_lock.
+                */
+               if (!mdt->mdt_enable_remote_rename &&
+                   mdt_object_remote(msrcdir))
+                       GOTO(out_put_tgtdir, rc = -EXDEV);
+
+               rc = mdt_rename_lock(info, &rename_lh);
+               if (rc != 0) {
+                       CERROR("%s: can't lock FS for rename: rc = %d\n",
+                              mdt_obd_name(mdt), rc);
+                       GOTO(out_put_tgtdir, rc);
+               }
+       }
+
+       rc = mdt_rename_determine_lock_order(info, msrcdir, mtgtdir);
+       if (rc < 0)
+               GOTO(out_unlock_rename, rc);
+
+       reverse = rc;
+
        /* source needs to be looked up after locking source parent, otherwise
         * this rename may race with unlink source, and cause rename hang, see
         * sanityn.sh 55b, so check parents first, if later we found source is
@@ -1908,7 +2416,7 @@ relock:
                rc = mdt_object_lock_save(info, mtgtdir, lh_tgtdirp, 1,
                                          cos_incompat);
                if (rc)
-                       GOTO(out_put_tgtdir, rc);
+                       GOTO(out_unlock_rename, rc);
 
                OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME, 5);
 
@@ -1916,20 +2424,21 @@ relock:
                                          cos_incompat);
                if (rc != 0) {
                        mdt_object_unlock(info, mtgtdir, lh_tgtdirp, rc);
-                       GOTO(out_put_tgtdir, rc);
+                       GOTO(out_unlock_rename, rc);
                }
        } else {
                rc = mdt_object_lock_save(info, msrcdir, lh_srcdirp, 0,
                                          cos_incompat);
                if (rc)
-                       GOTO(out_put_tgtdir, rc);
+                       GOTO(out_unlock_rename, rc);
 
                OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME, 5);
 
                if (mtgtdir != msrcdir) {
                        rc = mdt_object_lock_save(info, mtgtdir, lh_tgtdirp, 1,
                                                  cos_incompat);
-               } else if (lh_srcdirp->mlh_pdo_hash !=
+               } else if (!mdt_object_remote(mtgtdir) &&
+                          lh_srcdirp->mlh_pdo_hash !=
                           lh_tgtdirp->mlh_pdo_hash) {
                        rc = mdt_pdir_hash_lock(info, lh_tgtdirp, mtgtdir,
                                                MDS_INODELOCK_UPDATE,
@@ -1938,7 +2447,7 @@ relock:
                }
                if (rc != 0) {
                        mdt_object_unlock(info, msrcdir, lh_srcdirp, rc);
-                       GOTO(out_put_tgtdir, rc);
+                       GOTO(out_unlock_rename, rc);
                }
        }
 
@@ -1961,12 +2470,26 @@ relock:
        if (IS_ERR(mold))
                GOTO(out_unlock_parents, rc = PTR_ERR(mold));
 
+       if (!mdt_object_exists(mold)) {
+               LU_OBJECT_DEBUG(D_INODE, info->mti_env,
+                               &mold->mot_obj,
+                               "object does not exist");
+               GOTO(out_put_old, rc = -ENOENT);
+       }
+
+       if (mdt_object_remote(mold) && !mdt->mdt_enable_remote_rename)
+               GOTO(out_put_old, rc = -EXDEV);
+
        /* Check if @mtgtdir is subdir of @mold, before locking child
         * to avoid reverse locking. */
        if (mtgtdir != msrcdir) {
-               rc = mdt_is_subdir(info, mtgtdir, old_fid);
-               if (rc)
+               rc = mdo_is_subdir(info->mti_env, mdt_object_child(mtgtdir),
+                                  old_fid);
+               if (rc) {
+                       if (rc == 1)
+                               rc = -EINVAL;
                        GOTO(out_put_old, rc);
+               }
        }
 
        tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mold));
@@ -2003,6 +2526,13 @@ relock:
                if (IS_ERR(mnew))
                        GOTO(out_put_old, rc = PTR_ERR(mnew));
 
+               if (!mdt_object_exists(mnew)) {
+                       LU_OBJECT_DEBUG(D_INODE, info->mti_env,
+                                       &mnew->mot_obj,
+                                       "object does not exist");
+                       GOTO(out_put_new, rc = -ENOENT);
+               }
+
                if (mdt_object_remote(mnew)) {
                        struct mdt_body  *repbody;
 
@@ -2047,9 +2577,13 @@ relock:
                /* Check if @msrcdir is subdir of @mnew, before locking child
                 * to avoid reverse locking. */
                if (mtgtdir != msrcdir) {
-                       rc = mdt_is_subdir(info, msrcdir, new_fid);
-                       if (rc)
+                       rc = mdo_is_subdir(info->mti_env,
+                                          mdt_object_child(msrcdir), new_fid);
+                       if (rc) {
+                               if (rc == 1)
+                                       rc = -EINVAL;
                                GOTO(out_unlock_old, rc);
+                       }
                }
 
                /* We used to acquire MDS_INODELOCK_FULL here but we
@@ -2116,9 +2650,10 @@ relock:
        /* handle last link of tgt object */
        if (rc == 0) {
                mdt_counter_incr(req, LPROC_MDT_RENAME);
-               if (mnew)
+               if (mnew) {
                        mdt_handle_last_unlink(info, mnew, ma);
-
+                       discard = mdt_dom_check_for_discard(info, mnew);
+               }
                mdt_rename_counter_tally(info, info->mti_mdt, req,
                                         msrcdir, mtgtdir);
        }
@@ -2129,70 +2664,110 @@ relock:
 out_unlock_old:
        mdt_object_unlock(info, mold, lh_oldp, rc);
 out_put_new:
-       if (mnew != NULL)
+       if (mnew && !discard)
                mdt_object_put(info->mti_env, mnew);
 out_put_old:
        mdt_object_put(info->mti_env, mold);
 out_unlock_parents:
        mdt_object_unlock(info, mtgtdir, lh_tgtdirp, rc);
        mdt_object_unlock(info, msrcdir, lh_srcdirp, rc);
+out_unlock_rename:
+       if (lustre_handle_is_used(&rename_lh))
+               mdt_rename_unlock(&rename_lh);
 out_put_tgtdir:
        mdt_object_put(info->mti_env, mtgtdir);
 out_put_srcdir:
        mdt_object_put(info->mti_env, msrcdir);
+
+       /* The DoM discard can be done right in the place above where it is
+        * assigned, meanwhile it is done here after rename unlock due to
+        * compatibility with old clients, for them the discard blocks
+        * the main thread until completion. Check LU-11359 for details.
+        */
+       if (discard) {
+               mdt_dom_discard_data(info, mnew);
+               mdt_object_put(info->mti_env, mnew);
+       }
        return rc;
 }
 
-static int mdt_reint_rename_or_migrate(struct mdt_thread_info *info,
-                                      struct mdt_lock_handle *lhc, bool rename)
+static int mdt_reint_resync(struct mdt_thread_info *info,
+                           struct mdt_lock_handle *lhc)
 {
-       struct mdt_reint_record *rr = &info->mti_rr;
-       struct ptlrpc_request   *req = mdt_info_req(info);
-       struct lustre_handle    rename_lh = { 0 };
-       int                     rc;
+       struct mdt_reint_record *rr = &info->mti_rr;
+       struct ptlrpc_request   *req = mdt_info_req(info);
+       struct md_attr          *ma = &info->mti_attr;
+       struct mdt_object       *mo;
+       struct ldlm_lock        *lease;
+       struct mdt_body         *repbody;
+       struct md_layout_change  layout = { .mlc_mirror_id = rr->rr_mirror_id };
+       bool                     lease_broken;
+       int                      rc, rc2;
        ENTRY;
 
+       DEBUG_REQ(D_INODE, req, DFID": FLR file resync\n", PFID(rr->rr_fid1));
+
        if (info->mti_dlm_req)
                ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
 
-       if (!fid_is_md_operative(rr->rr_fid1) ||
-           !fid_is_md_operative(rr->rr_fid2))
-               RETURN(-EPERM);
+       mo = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
+       if (IS_ERR(mo))
+               GOTO(out, rc = PTR_ERR(mo));
 
-       /* Note: do not enqueue rename lock for replay request, because
-        * if other MDT holds rename lock, but being blocked to wait for
-        * this MDT to finish its recovery, and the failover MDT can not
-        * get rename lock, which will cause deadlock. */
-       if (!req_is_replay(req)) {
-               rc = mdt_rename_lock(info, &rename_lh);
-               if (rc != 0) {
-                       CERROR("%s: can't lock FS for rename: rc  = %d\n",
-                              mdt_obd_name(info->mti_mdt), rc);
-                       RETURN(rc);
-               }
-       }
+       if (!mdt_object_exists(mo))
+               GOTO(out_obj, rc = -ENOENT);
 
-       if (rename)
-               rc = mdt_reint_rename_internal(info, lhc);
-       else
-               rc = mdt_reint_migrate_internal(info, lhc);
+       if (!S_ISREG(lu_object_attr(&mo->mot_obj)))
+               GOTO(out_obj, rc = -EINVAL);
 
-       if (lustre_handle_is_used(&rename_lh))
-               mdt_rename_unlock(&rename_lh);
+       if (mdt_object_remote(mo))
+               GOTO(out_obj, rc = -EREMOTE);
+
+       lease = ldlm_handle2lock(rr->rr_lease_handle);
+       if (lease == NULL)
+               GOTO(out_obj, rc = -ESTALE);
+
+       /* It's really necessary to grab open_sem and check if the lease lock
+        * has been lost. There would exist a concurrent writer coming in and
+        * generating some dirty data in memory cache, the writeback would fail
+        * after the layout version is increased by MDS_REINT_RESYNC RPC. */
+       if (!down_write_trylock(&mo->mot_open_sem))
+               GOTO(out_put_lease, rc = -EBUSY);
+
+       lock_res_and_lock(lease);
+       lease_broken = ldlm_is_cancel(lease);
+       unlock_res_and_lock(lease);
+       if (lease_broken)
+               GOTO(out_unlock, rc = -EBUSY);
+
+       /* the file has yet opened by anyone else after we took the lease. */
+       layout.mlc_opc = MD_LAYOUT_RESYNC;
+       rc = mdt_layout_change(info, mo, &layout);
+       if (rc)
+               GOTO(out_unlock, rc);
 
-       RETURN(rc);
-}
+       ma->ma_need = MA_INODE;
+       ma->ma_valid = 0;
+       rc = mdt_attr_get_complex(info, mo, ma);
+       if (rc != 0)
+               GOTO(out_unlock, rc);
 
-static int mdt_reint_rename(struct mdt_thread_info *info,
-                           struct mdt_lock_handle *lhc)
-{
-       return mdt_reint_rename_or_migrate(info, lhc, true);
-}
+       repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
+       mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
 
-static int mdt_reint_migrate(struct mdt_thread_info *info,
-                           struct mdt_lock_handle *lhc)
-{
-       return mdt_reint_rename_or_migrate(info, lhc, false);
+       EXIT;
+out_unlock:
+       up_write(&mo->mot_open_sem);
+out_put_lease:
+       LDLM_LOCK_PUT(lease);
+out_obj:
+       mdt_object_put(info->mti_env, mo);
+out:
+       mdt_client_compatibility(info);
+       rc2 = mdt_fix_reply(info);
+       if (rc == 0)
+               rc = rc2;
+       return rc;
 }
 
 struct mdt_reinter {
@@ -2237,6 +2812,10 @@ static const struct mdt_reinter mdt_reinters[] = {
                .mr_handler = &mdt_reint_migrate,
                .mr_extra_opc = MDS_REINT_RENAME,
        },
+       [REINT_RESYNC] = {
+               .mr_handler = &mdt_reint_resync,
+               .mr_extra_opc = MDS_REINT_RESYNC,
+       },
 };
 
 int mdt_reint_rec(struct mdt_thread_info *info,