Whamcloud - gitweb
LU-13437 mdt: rename misses remote LOOKUP lock revoke
[fs/lustre-release.git] / lustre / mdt / mdt_reint.c
index fe19a93..5d37de3 100644 (file)
 #include <lustre_lmv.h>
 
 static inline void mdt_reint_init_ma(struct mdt_thread_info *info,
-                                     struct md_attr *ma)
+                                    struct md_attr *ma)
 {
        ma->ma_need = MA_INODE;
-        ma->ma_valid = 0;
+       ma->ma_valid = 0;
 }
 
 /**
@@ -59,7 +59,7 @@ static inline void mdt_reint_init_ma(struct mdt_thread_info *info,
  * Return real version or ENOENT_VERSION if object doesn't exist
  */
 static void mdt_obj_version_get(struct mdt_thread_info *info,
-                                struct mdt_object *o, __u64 *version)
+                               struct mdt_object *o, __u64 *version)
 {
        LASSERT(o);
 
@@ -78,20 +78,20 @@ static void mdt_obj_version_get(struct mdt_thread_info *info,
  * Should be called only during replay.
  */
 static int mdt_version_check(struct ptlrpc_request *req,
-                             __u64 version, int idx)
+                            __u64 version, int idx)
 {
-        __u64 *pre_ver = lustre_msg_get_versions(req->rq_reqmsg);
-        ENTRY;
-
-        if (!exp_connect_vbr(req->rq_export))
-                RETURN(0);
-
-        LASSERT(req_is_replay(req));
-        /** VBR: version is checked always because costs nothing */
-        LASSERT(idx < PTLRPC_NUM_VERSIONS);
-        /** Sanity check for malformed buffers */
-        if (pre_ver == NULL) {
-                CERROR("No versions in request buffer\n");
+       __u64 *pre_ver = lustre_msg_get_versions(req->rq_reqmsg);
+
+       ENTRY;
+       if (!exp_connect_vbr(req->rq_export))
+               RETURN(0);
+
+       LASSERT(req_is_replay(req));
+       /** VBR: version is checked always because costs nothing */
+       LASSERT(idx < PTLRPC_NUM_VERSIONS);
+       /** Sanity check for malformed buffers */
+       if (pre_ver == NULL) {
+               CERROR("No versions in request buffer\n");
                spin_lock(&req->rq_export->exp_lock);
                req->rq_export->exp_vbr_failed = 1;
                spin_unlock(&req->rq_export->exp_lock);
@@ -111,18 +111,18 @@ static int mdt_version_check(struct ptlrpc_request *req,
  * Save pre-versions in reply.
  */
 static void mdt_version_save(struct ptlrpc_request *req, __u64 version,
-                             int idx)
+                            int idx)
 {
-        __u64 *reply_ver;
+       __u64 *reply_ver;
 
-        if (!exp_connect_vbr(req->rq_export))
-                return;
+       if (!exp_connect_vbr(req->rq_export))
+               return;
 
-        LASSERT(!req_is_replay(req));
-        LASSERT(req->rq_repmsg != NULL);
-        reply_ver = lustre_msg_get_versions(req->rq_repmsg);
-        if (reply_ver)
-                reply_ver[idx] = version;
+       LASSERT(!req_is_replay(req));
+       LASSERT(req->rq_repmsg != NULL);
+       reply_ver = lustre_msg_get_versions(req->rq_repmsg);
+       if (reply_ver)
+               reply_ver[idx] = version;
 }
 
 /**
@@ -131,11 +131,11 @@ static void mdt_version_save(struct ptlrpc_request *req, __u64 version,
  */
 static void mdt_enoent_version_save(struct mdt_thread_info *info, int idx)
 {
-        /* save version of file name for replay, it must be ENOENT here */
-        if (!req_is_replay(mdt_info_req(info))) {
-                info->mti_ver[idx] = ENOENT_VERSION;
-                mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
-        }
+       /* save version of file name for replay, it must be ENOENT here */
+       if (!req_is_replay(mdt_info_req(info))) {
+               info->mti_ver[idx] = ENOENT_VERSION;
+               mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
+       }
 }
 
 /**
@@ -144,44 +144,44 @@ static void mdt_enoent_version_save(struct mdt_thread_info *info, int idx)
  * Versions are saved in reply only during normal operations not replays.
  */
 void mdt_version_get_save(struct mdt_thread_info *info,
-                          struct mdt_object *mto, int idx)
+                         struct mdt_object *mto, int idx)
 {
-        /* don't save versions during replay */
-        if (!req_is_replay(mdt_info_req(info))) {
-                mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
-                mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
-        }
+       /* don't save versions during replay */
+       if (!req_is_replay(mdt_info_req(info))) {
+               mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
+               mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
+       }
 }
 
 /**
  * Get version from disk and check it, no save in reply.
  */
 int mdt_version_get_check(struct mdt_thread_info *info,
-                          struct mdt_object *mto, int idx)
+                         struct mdt_object *mto, int idx)
 {
-        /* only check versions during replay */
-        if (!req_is_replay(mdt_info_req(info)))
-                return 0;
+       /* only check versions during replay */
+       if (!req_is_replay(mdt_info_req(info)))
+               return 0;
 
-        mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
-        return mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
+       mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
+       return mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
 }
 
 /**
  * Get version from disk and check if recovery or just save.
  */
 int mdt_version_get_check_save(struct mdt_thread_info *info,
-                               struct mdt_object *mto, int idx)
+                              struct mdt_object *mto, int idx)
 {
-        int rc = 0;
-
-        mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
-        if (req_is_replay(mdt_info_req(info)))
-                rc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx],
-                                       idx);
-        else
-                mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
-        return rc;
+       int rc = 0;
+
+       mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
+       if (req_is_replay(mdt_info_req(info)))
+               rc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx],
+                                      idx);
+       else
+               mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
+       return rc;
 }
 
 /**
@@ -190,30 +190,31 @@ int mdt_version_get_check_save(struct mdt_thread_info *info,
  * This checks version of 'name'. Many reint functions uses 'name' for child not
  * FID, therefore we need to get object by name and check its version.
  */
-static int mdt_lookup_version_check(struct mdt_thread_info *info,
-                                   struct mdt_object *p,
-                                   const struct lu_name *lname,
-                                   struct lu_fid *fid, int idx)
+int mdt_lookup_version_check(struct mdt_thread_info *info,
+                            struct mdt_object *p,
+                            const struct lu_name *lname,
+                            struct lu_fid *fid, int idx)
 {
-        int rc, vbrc;
-
-        rc = mdo_lookup(info->mti_env, mdt_object_child(p), lname, fid,
-                        &info->mti_spec);
-        /* Check version only during replay */
-        if (!req_is_replay(mdt_info_req(info)))
-                return rc;
-
-        info->mti_ver[idx] = ENOENT_VERSION;
-        if (rc == 0) {
-                struct mdt_object *child;
-                child = mdt_object_find(info->mti_env, info->mti_mdt, fid);
-                if (likely(!IS_ERR(child))) {
-                        mdt_obj_version_get(info, child, &info->mti_ver[idx]);
-                        mdt_object_put(info->mti_env, child);
-                }
-        }
-        vbrc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
-        return vbrc ? vbrc : rc;
+       int rc, vbrc;
+
+       rc = mdo_lookup(info->mti_env, mdt_object_child(p), lname, fid,
+                       &info->mti_spec);
+       /* Check version only during replay */
+       if (!req_is_replay(mdt_info_req(info)))
+               return rc;
+
+       info->mti_ver[idx] = ENOENT_VERSION;
+       if (rc == 0) {
+               struct mdt_object *child;
+
+               child = mdt_object_find(info->mti_env, info->mti_mdt, fid);
+               if (likely(!IS_ERR(child))) {
+                       mdt_obj_version_get(info, child, &info->mti_ver[idx]);
+                       mdt_object_put(info->mti_env, child);
+               }
+       }
+       vbrc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
+       return vbrc ? vbrc : rc;
 
 }
 
@@ -250,17 +251,25 @@ static int mdt_unlock_slaves(struct mdt_thread_info *mti,
 static inline int mdt_object_striped(struct mdt_thread_info *mti,
                                     struct mdt_object *obj)
 {
+       struct lu_device *bottom_dev;
+       struct lu_object *bottom_obj;
        int rc;
 
        if (!S_ISDIR(obj->mot_header.loh_attr))
                return 0;
 
-       rc = mo_xattr_get(mti->mti_env, mdt_object_child(obj), &LU_BUF_NULL,
+       /* 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);
+
+       rc = dt_xattr_get(mti->mti_env, lu2dt(bottom_obj), &LU_BUF_NULL,
                          XATTR_NAME_LMV);
-       if (rc <= 0)
-               return rc == -ENODATA ? 0 : rc;
+       lu_object_put(mti->mti_env, bottom_obj);
 
-       return 1;
+       return (rc > 0) ? 1 : (rc == -ENODATA) ? 0 : rc;
 }
 
 /**
@@ -334,6 +343,133 @@ void mdt_reint_striped_unlock(struct mdt_thread_info *info,
        mdt_object_unlock(info, o, lh, decref);
 }
 
+static int mdt_restripe(struct mdt_thread_info *info,
+                       struct mdt_object *parent,
+                       const struct lu_name *lname,
+                       const struct lu_fid *tfid,
+                       struct md_op_spec *spec,
+                       struct md_attr *ma)
+{
+       struct mdt_device *mdt = info->mti_mdt;
+       struct lu_fid *fid = &info->mti_tmp_fid2;
+       struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
+       struct lmv_mds_md_v1 *lmv;
+       struct mdt_object *child;
+       struct mdt_lock_handle *lhp;
+       struct mdt_lock_handle *lhc;
+       struct mdt_body *repbody;
+       int rc;
+
+       ENTRY;
+       if (!mdt->mdt_enable_dir_restripe)
+               RETURN(-EPERM);
+
+       rc = mdt_version_get_check_save(info, parent, 0);
+       if (rc)
+               RETURN(rc);
+
+       lhp = &info->mti_lh[MDT_LH_PARENT];
+       mdt_lock_pdo_init(lhp, LCK_PW, lname);
+       rc = mdt_reint_object_lock(info, parent, lhp, MDS_INODELOCK_UPDATE,
+                                  true);
+       if (rc)
+               RETURN(rc);
+
+       rc = mdt_stripe_get(info, parent, ma, XATTR_NAME_LMV);
+       if (rc)
+               GOTO(unlock_parent, rc);
+
+       if (ma->ma_valid & MA_LMV) {
+               /* don't allow restripe if parent dir layout is changing */
+               lmv = &ma->ma_lmv->lmv_md_v1;
+               if (!lmv_is_sane(lmv))
+                       GOTO(unlock_parent, rc = -EBADF);
+
+               if (lmv_is_layout_changing(lmv))
+                       GOTO(unlock_parent, rc = -EBUSY);
+       }
+
+       fid_zero(fid);
+       rc = mdt_lookup_version_check(info, parent, lname, fid, 1);
+       if (rc)
+               GOTO(unlock_parent, rc);
+
+       child = mdt_object_find(info->mti_env, mdt, fid);
+       if (IS_ERR(child))
+               GOTO(unlock_parent, rc = PTR_ERR(child));
+
+       if (!mdt_object_exists(child))
+               GOTO(out_child, rc = -ENOENT);
+
+       if (mdt_object_remote(child)) {
+               struct mdt_body *repbody;
+
+               repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
+               if (!repbody)
+                       GOTO(out_child, rc = -EPROTO);
+
+               repbody->mbo_fid1 = *fid;
+               repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
+               GOTO(out_child, rc = -EREMOTE);
+       }
+
+       /* lock object */
+       lhc = &info->mti_lh[MDT_LH_CHILD];
+       mdt_lock_reg_init(lhc, LCK_EX);
+
+       /* enqueue object remote LOOKUP lock */
+       if (mdt_object_remote(parent)) {
+               rc = mdt_remote_object_lock(info, parent, fid,
+                                           &lhc->mlh_rreg_lh,
+                                           lhc->mlh_rreg_mode,
+                                           MDS_INODELOCK_LOOKUP, false);
+               if (rc != ELDLM_OK)
+                       GOTO(out_child, rc);
+       }
+
+       rc = mdt_reint_striped_lock(info, child, lhc, MDS_INODELOCK_FULL, einfo,
+                                   true);
+       if (rc)
+               GOTO(unlock_child, rc);
+
+       tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(child));
+       rc = mdt_version_get_check_save(info, child, 1);
+       if (rc)
+               GOTO(unlock_child, rc);
+
+       spin_lock(&mdt->mdt_restriper.mdr_lock);
+       if (child->mot_restriping) {
+               /* race? */
+               spin_unlock(&mdt->mdt_restriper.mdr_lock);
+               GOTO(unlock_child, rc = -EBUSY);
+       }
+       child->mot_restriping = 1;
+       spin_unlock(&mdt->mdt_restriper.mdr_lock);
+
+       *fid = *tfid;
+       rc = mdt_restripe_internal(info, parent, child, lname, fid, spec, ma);
+       if (rc)
+               GOTO(restriping_clear, rc);
+
+       repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
+       if (!repbody)
+               GOTO(restriping_clear, rc = -EPROTO);
+
+       mdt_pack_attr2body(info, repbody, &ma->ma_attr, fid);
+       EXIT;
+
+restriping_clear:
+       child->mot_restriping = 0;
+unlock_child:
+       mdt_reint_striped_unlock(info, child, lhc, einfo, rc);
+out_child:
+       mdt_object_put(info->mti_env, child);
+unlock_parent:
+       mdt_object_unlock(info, parent, lhp, rc);
+
+       return rc;
+}
+
 /*
  * VBR: we save three versions in reply:
  * 0 - parent. Check that parent version is the same during replay.
@@ -344,19 +480,20 @@ void mdt_reint_striped_unlock(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;
-       struct mdt_object       *child;
-       struct mdt_lock_handle  *lh;
-       struct mdt_body         *repbody;
-       struct md_attr          *ma = &info->mti_attr;
+       struct mdt_device *mdt = info->mti_mdt;
+       struct mdt_object *parent;
+       struct mdt_object *child;
+       struct mdt_lock_handle *lh;
+       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;
+       struct md_op_spec *spec = &info->mti_spec;
+       bool restripe = false;
        int rc;
-       ENTRY;
 
-       DEBUG_REQ(D_INODE, mdt_info_req(info), "Create  ("DNAME"->"DFID") "
-                 "in "DFID,
+       ENTRY;
+       DEBUG_REQ(D_INODE, mdt_info_req(info),
+                 "Create ("DNAME"->"DFID") in "DFID,
                  PNAME(&rr->rr_name), PFID(rr->rr_fid2), PFID(rr->rr_fid1));
 
        if (!fid_is_md_operative(rr->rr_fid1))
@@ -369,7 +506,8 @@ static int mdt_create(struct mdt_thread_info *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 */
+                * striped dir(>= 2.6), old client will return -ENOTSUPP
+                */
                if (!mdt_is_dne_client(exp))
                        RETURN(-ENOTSUPP);
 
@@ -383,10 +521,19 @@ static int mdt_create(struct mdt_thread_info *info)
                        RETURN(-EPERM);
                }
 
+               if ((!(exp_connect_flags2(exp) & OBD_CONNECT2_CRUSH)) &&
+                   (le32_to_cpu(lum->lum_hash_type) & LMV_HASH_TYPE_MASK) ==
+                   LMV_HASH_TYPE_CRUSH)
+                       RETURN(-EPROTO);
+
                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);
+
+               /* restripe if later found dir exists */
+               if (le32_to_cpu(lum->lum_stripe_offset) == LMV_OFFSET_DEFAULT)
+                       restripe = true;
        }
 
        repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
@@ -398,6 +545,29 @@ static int mdt_create(struct mdt_thread_info *info)
        if (!mdt_object_exists(parent))
                GOTO(put_parent, rc = -ENOENT);
 
+       /*
+        * LU-10235: check if name exists locklessly first to avoid massive
+        * lock recalls on existing directories.
+        */
+       rc = mdt_lookup_version_check(info, parent, &rr->rr_name,
+                                     &info->mti_tmp_fid1, 1);
+       if (rc == 0) {
+               if (!restripe)
+                       GOTO(put_parent, rc = -EEXIST);
+
+               rc = mdt_restripe(info, parent, &rr->rr_name, rr->rr_fid2, spec,
+                                 ma);
+       }
+
+       /* -ENOENT is expected here */
+       if (rc != -ENOENT)
+               GOTO(put_parent, rc);
+
+       /* save version of file name for replay, it must be ENOENT here */
+       mdt_enoent_version_save(info, 1);
+
+       OBD_RACE(OBD_FAIL_MDS_CREATE_RACE);
+
        lh = &info->mti_lh[MDT_LH_PARENT];
        mdt_lock_pdo_init(lh, LCK_PW, &rr->rr_name);
        rc = mdt_object_lock(info, parent, lh, MDS_INODELOCK_UPDATE);
@@ -410,22 +580,6 @@ static int mdt_create(struct mdt_thread_info *info)
                        GOTO(unlock_parent, rc);
        }
 
-       /*
-        * Check child name version during replay.
-        * During create replay a file may exist with same name.
-        */
-       rc = mdt_lookup_version_check(info, parent, &rr->rr_name,
-                                     &info->mti_tmp_fid1, 1);
-       if (rc == 0)
-               GOTO(unlock_parent, rc = -EEXIST);
-
-       /* -ENOENT is expected here */
-       if (rc != -ENOENT)
-               GOTO(unlock_parent, rc);
-
-       /* save version of file name for replay, it must be ENOENT here */
-       mdt_enoent_version_save(info, 1);
-
        child = mdt_object_new(info->mti_env, mdt, rr->rr_fid2);
        if (unlikely(IS_ERR(child)))
                GOTO(unlock_parent, rc = PTR_ERR(child));
@@ -507,13 +661,14 @@ static int mdt_create(struct mdt_thread_info *info)
        if (ma->ma_valid & MA_INODE)
                mdt_pack_attr2body(info, repbody, &ma->ma_attr,
                                   mdt_object_fid(child));
+       EXIT;
 put_child:
        mdt_object_put(info->mti_env, child);
 unlock_parent:
        mdt_object_unlock(info, parent, lh, rc);
 put_parent:
        mdt_object_put(info->mti_env, parent);
-       RETURN(rc);
+       return rc;
 }
 
 static int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo,
@@ -526,8 +681,8 @@ static int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo,
        struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
        bool cos_incompat;
        int rc;
-       ENTRY;
 
+       ENTRY;
        rc = mdt_object_striped(info, mo);
        if (rc < 0)
                RETURN(rc);
@@ -540,7 +695,8 @@ static int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo,
        /* Even though the new MDT will grant PERM lock to the old
         * client, but the old client will almost ignore that during
         * So it needs to revoke both LOOKUP and PERM lock here, so
-        * both new and old client can cancel the dcache */
+        * both new and old client can cancel the dcache
+        */
        if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
                lockpart |= MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
 
@@ -593,8 +749,8 @@ int mdt_add_dirty_flag(struct mdt_thread_info *info, struct mdt_object *mo,
        struct lu_ucred *uc = mdt_ucred(info);
        cfs_cap_t cap_saved;
        int rc;
-       ENTRY;
 
+       ENTRY;
        /* If the file was modified, add the dirty flag */
        ma->ma_need = MA_HSM;
        rc = mdt_attr_get_complex(info, mo, ma);
@@ -610,7 +766,8 @@ int mdt_add_dirty_flag(struct mdt_thread_info *info, struct mdt_object *mo,
                ma->ma_hsm.mh_flags |= HS_DIRTY;
 
                /* Bump cap so that closes from non-owner writers can
-                * set the HSM state to dirty. */
+                * 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);
@@ -632,15 +789,18 @@ static int mdt_reint_setattr(struct mdt_thread_info *info,
        struct ptlrpc_request *req = mdt_info_req(info);
        struct mdt_object *mo;
        struct mdt_body *repbody;
+       ktime_t kstart = ktime_get();
        int rc, rc2;
-       ENTRY;
 
+       ENTRY;
        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);
 
+       OBD_RACE(OBD_FAIL_PTLRPC_RESEND_RACE);
+
        repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
        mo = mdt_object_find(info->mti_env, mdt, rr->rr_fid1);
        if (IS_ERR(mo))
@@ -652,6 +812,7 @@ static int mdt_reint_setattr(struct mdt_thread_info *info,
        if (mdt_object_remote(mo))
                GOTO(out_put, rc = -EREMOTE);
 
+       ma->ma_enable_chprojid_gid = mdt->mdt_enable_chprojid_gid;
        /* 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) &&
@@ -680,14 +841,25 @@ static int mdt_reint_setattr(struct mdt_thread_info *info,
                        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)) {
+                * 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 (rc > 0 && mdt_lmm_is_flr(info->mti_big_lmm))
-                               GOTO(out_put, rc = -EOPNOTSUPP);
+                       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
@@ -718,6 +890,8 @@ static int mdt_reint_setattr(struct mdt_thread_info *info,
                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) {
@@ -731,27 +905,35 @@ static int mdt_reint_setattr(struct mdt_thread_info *info,
                                GOTO(out_put, rc = -EPERM);
                }
 
+               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);
 
-               lh = &info->mti_lh[MDT_LH_PARENT];
-               mdt_lock_reg_init(lh, LCK_PW);
-
-               rc = mdt_object_lock(info, mo, lh, MDS_INODELOCK_XATTR);
-               if (rc != 0)
-                       GOTO(out_put, rc);
-
                if (ma->ma_valid & MA_LOV) {
                        buf->lb_buf = ma->ma_lmm;
                        buf->lb_len = ma->ma_lmm_size;
+                       name = XATTR_NAME_LOV;
                } else {
-                       buf->lb_buf = ma->ma_lmv;
+                       struct lmv_user_md *lmu = &ma->ma_lmv->lmv_user_md;
+
+                       buf->lb_buf = lmu;
                        buf->lb_len = ma->ma_lmv_size;
+                       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, lockpart);
+               if (rc != 0)
+                       GOTO(out_put, rc);
+
                rc = mo_xattr_set(info->mti_env, mdt_object_child(mo), buf,
-                                 (ma->ma_valid & MA_LOV) ?
-                                       XATTR_NAME_LOV : XATTR_NAME_DEFAULT_LMV,
-                                 0);
+                                 name, 0);
 
                mdt_object_unlock(info, mo, lh, rc);
                if (rc)
@@ -764,8 +946,8 @@ static int mdt_reint_setattr(struct mdt_thread_info *info,
        if (ma->ma_attr_flags & MDS_DATA_MODIFIED)
                rc = mdt_add_dirty_flag(info, mo, ma);
 
-        ma->ma_need = MA_INODE;
-        ma->ma_valid = 0;
+       ma->ma_need = MA_INODE;
+       ma->ma_valid = 0;
        rc = mdt_attr_get_complex(info, mo, ma);
        if (rc != 0)
                GOTO(out_put, rc);
@@ -777,24 +959,26 @@ out_put:
        mdt_object_put(info->mti_env, mo);
 out:
        if (rc == 0)
-               mdt_counter_incr(req, LPROC_MDT_SETATTR);
+               mdt_counter_incr(req, LPROC_MDT_SETATTR,
+                                ktime_us_delta(ktime_get(), kstart));
 
-        mdt_client_compatibility(info);
-        rc2 = mdt_fix_reply(info);
-        if (rc == 0)
-                rc = rc2;
-        return rc;
+       mdt_client_compatibility(info);
+       rc2 = mdt_fix_reply(info);
+       if (rc == 0)
+               rc = rc2;
+       return rc;
 }
 
 static int mdt_reint_create(struct mdt_thread_info *info,
-                            struct mdt_lock_handle *lhc)
+                           struct mdt_lock_handle *lhc)
 {
-        struct ptlrpc_request   *req = mdt_info_req(info);
-        int                     rc;
-        ENTRY;
+       struct ptlrpc_request   *req = mdt_info_req(info);
+       ktime_t                 kstart = ktime_get();
+       int                     rc;
 
-        if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
-                RETURN(err_serious(-ESTALE));
+       ENTRY;
+       if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
+               RETURN(err_serious(-ESTALE));
 
        if (info->mti_dlm_req)
                ldlm_request_cancel(mdt_info_req(info),
@@ -805,16 +989,12 @@ static int mdt_reint_create(struct mdt_thread_info *info,
 
        switch (info->mti_attr.ma_attr.la_mode & S_IFMT) {
        case S_IFDIR:
-               mdt_counter_incr(req, LPROC_MDT_MKDIR);
-               break;
-        case S_IFREG:
-        case S_IFLNK:
-        case S_IFCHR:
-        case S_IFBLK:
-        case S_IFIFO:
+       case S_IFREG:
+       case S_IFLNK:
+       case S_IFCHR:
+       case S_IFBLK:
+       case S_IFIFO:
        case S_IFSOCK:
-               /* Special file should stay on the same node as parent. */
-               mdt_counter_incr(req, LPROC_MDT_MKNOD);
                break;
        default:
                CERROR("%s: Unsupported mode %o\n",
@@ -824,6 +1004,16 @@ static int mdt_reint_create(struct mdt_thread_info *info,
        }
 
        rc = mdt_create(info);
+       if (rc == 0) {
+               if ((info->mti_attr.ma_attr.la_mode & S_IFMT) == S_IFDIR)
+                       mdt_counter_incr(req, LPROC_MDT_MKDIR,
+                                        ktime_us_delta(ktime_get(), kstart));
+               else
+                       /* Special file should stay on the same node as parent*/
+                       mdt_counter_incr(req, LPROC_MDT_MKNOD,
+                                        ktime_us_delta(ktime_get(), kstart));
+       }
+
        RETURN(rc);
 }
 
@@ -844,12 +1034,12 @@ static int mdt_reint_unlink(struct mdt_thread_info *info,
        struct mdt_lock_handle *child_lh;
        struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
        __u64 lock_ibits;
-       bool cos_incompat = false, discard = false;
+       bool cos_incompat = false;
        int no_name = 0;
+       ktime_t kstart = ktime_get();
        int rc;
 
        ENTRY;
-
        DEBUG_REQ(D_INODE, req, "unlink "DFID"/"DNAME"", PFID(rr->rr_fid1),
                  PNAME(&rr->rr_name));
 
@@ -899,15 +1089,15 @@ relock:
                 *    find the name entry on MDT0 anymore.
                 * In this case, MDT1 only needs to destory the local
                 * directory.
-                * */
+                */
                if (mdt_object_remote(mp) && rc == -ENOENT &&
                    !fid_is_zero(rr->rr_fid2) &&
                    lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
                        no_name = 1;
                        *child_fid = *rr->rr_fid2;
-                } else {
+               } else {
                        GOTO(unlock_parent, rc);
-                }
+               }
        }
 
        if (!fid_is_md_operative(child_fid))
@@ -921,7 +1111,7 @@ relock:
        if (!cos_incompat) {
                rc = mdt_object_striped(info, mc);
                if (rc < 0)
-                       GOTO(unlock_parent, rc);
+                       GOTO(put_child, rc);
 
                cos_incompat = rc;
                if (cos_incompat) {
@@ -971,7 +1161,8 @@ relock:
                 * this MDT. Since the unlink will happen on another MDT,
                 * it will release the LOOKUP lock right away. Then What
                 * would happen if another client try to grab the LOOKUP
-                * lock at the same time with unlink XXX */
+                * lock at the same time with unlink XXX
+                */
                mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_LOOKUP);
                repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
                LASSERT(repbody != NULL);
@@ -981,7 +1172,8 @@ relock:
        }
        /* We used to acquire MDS_INODELOCK_FULL here but we can't do
         * this now because a running HSM restore on the child (unlink
-        * victim) will hold the layout lock. See LU-4002. */
+        * victim) will hold the layout lock. See LU-4002.
+        */
        lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
        if (mdt_object_remote(mp)) {
                /* Enqueue lookup lock from parent MDT */
@@ -1025,8 +1217,8 @@ relock:
                rc = mdt_attr_get_complex(info, mc, ma);
                if (rc)
                        GOTO(out_stat, rc);
-       } else {
-               discard = mdt_dom_check_for_discard(info, mc);
+       } else if (mdt_dom_check_for_discard(info, mc)) {
+               mdt_dom_discard_data(info, mc);
        }
        mdt_handle_last_unlink(info, mc, ma);
 
@@ -1034,7 +1226,8 @@ 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);
+                       mdt_counter_incr(req, LPROC_MDT_RMDIR,
+                                        ktime_us_delta(ktime_get(), kstart));
                        break;
                case S_IFREG:
                case S_IFLNK:
@@ -1042,7 +1235,8 @@ out_stat:
                case S_IFBLK:
                case S_IFIFO:
                case S_IFSOCK:
-                       mdt_counter_incr(req, LPROC_MDT_UNLINK);
+                       mdt_counter_incr(req, LPROC_MDT_UNLINK,
+                                        ktime_us_delta(ktime_get(), kstart));
                        break;
                default:
                        LASSERTF(0, "bad file type %o unlinking\n",
@@ -1060,14 +1254,8 @@ unlock_parent:
        mdt_object_unlock(info, mp, parent_lh, rc);
 put_parent:
        mdt_object_put(info->mti_env, mp);
-
-       /* discard is just a PW DOM lock to drop the data on a client
-        * no need to keep objects being get and locked, do that after all.
-        */
-       if (discard)
-               mdt_dom_discard_data(info, child_fid);
-
-        return rc;
+       CFS_RACE_WAKEUP(OBD_FAIL_OBD_ZERO_NLINK_RACE);
+       return rc;
 }
 
 /*
@@ -1084,21 +1272,28 @@ static int mdt_reint_link(struct mdt_thread_info *info,
        struct mdt_object       *mp;
        struct mdt_lock_handle  *lhs;
        struct mdt_lock_handle  *lhp;
+       ktime_t kstart = ktime_get();
        bool cos_incompat;
        int rc;
-       ENTRY;
 
+       ENTRY;
        DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/"DNAME,
                  PFID(rr->rr_fid1), PFID(rr->rr_fid2), PNAME(&rr->rr_name));
 
        if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
                RETURN(err_serious(-ENOENT));
 
+       if (OBD_FAIL_PRECHECK(OBD_FAIL_PTLRPC_RESEND_RACE)) {
+               req->rq_no_reply = 1;
+               RETURN(err_serious(-ENOENT));
+       }
+
        if (info->mti_dlm_req)
                ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
 
        /* Invalid case so return error immediately instead of
-        * processing it */
+        * processing it
+        */
        if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
                RETURN(-EPERM);
 
@@ -1174,7 +1369,8 @@ static int mdt_reint_link(struct mdt_thread_info *info,
                      mdt_object_child(ms), &rr->rr_name, ma);
 
        if (rc == 0)
-               mdt_counter_incr(req, LPROC_MDT_LINK);
+               mdt_counter_incr(req, LPROC_MDT_LINK,
+                                ktime_us_delta(ktime_get(), kstart));
 
        EXIT;
 unlock_source:
@@ -1231,15 +1427,16 @@ static int mdt_rename_lock(struct mdt_thread_info *info,
                           struct lustre_handle *lh)
 {
        int     rc;
-       ENTRY;
 
+       ENTRY;
        if (mdt_seq_site(info->mti_mdt)->ss_node_id != 0) {
                struct lu_fid *fid = &info->mti_tmp_fid1;
                struct mdt_object *obj;
 
                /* XXX, right now, it has to use object API to
                 * enqueue lock cross MDT, so it will enqueue
-                * rename lock(with LUSTRE_BFL_FID) by root object */
+                * rename lock(with LUSTRE_BFL_FID) by root object
+                */
                lu_root_fid(fid);
                obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
                if (IS_ERR(obj))
@@ -1257,7 +1454,7 @@ static int mdt_rename_lock(struct mdt_thread_info *info,
                __u64 flags = 0;
 
                fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
-               memset(policy, 0, sizeof *policy);
+               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(info->mti_env, ns, res_id,
@@ -1289,7 +1486,6 @@ static struct mdt_object *mdt_parent_find_check(struct mdt_thread_info *info,
        int rc;
 
        ENTRY;
-
        dir = mdt_object_find(info->mti_env, info->mti_mdt, fid);
        if (IS_ERR(dir))
                RETURN(dir);
@@ -1364,9 +1560,9 @@ int mdt_revoke_remote_lookup_lock(struct mdt_thread_info *info,
  * different list.
  */
 struct mdt_sub_lock {
-       struct mdt_object      *msl_obj;
-       struct mdt_lock_handle  msl_lh;
-       struct list_head        msl_linkage;
+       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,
@@ -1423,7 +1619,6 @@ static int mdt_link_parents_lock(struct mdt_thread_info *info,
        int rc;
 
        ENTRY;
-
        if (S_ISDIR(lu_object_attr(&obj->mot_obj)))
                RETURN(0);
 
@@ -1625,20 +1820,19 @@ static int mdt_lock_remote_slaves(struct mdt_thread_info *info,
        int rc;
 
        ENTRY;
-
        LASSERT(mdt_object_remote(obj));
        LASSERT(ma->ma_valid & MA_LMV);
        LASSERT(lmv);
 
-       if (le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_V1)
+       if (!lmv_is_sane(lmv))
                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));
@@ -1736,9 +1930,6 @@ static int mdt_migrate_object_lock(struct mdt_thread_info *info,
                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);
@@ -1788,25 +1979,15 @@ static int mdt_migrate_lookup(struct mdt_thread_info *info,
        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);
-               }
 
-               rc = lmv_name_to_stripe_index(hash_type, stripe_count,
-                                             lname->ln_name,
-                                             lname->ln_namelen);
+               if (!lmv_is_sane(lmv))
+                       return -EBADF;
+
+               rc = lmv_name_to_stripe_index_old(lmv, lname->ln_name,
+                                                 lname->ln_namelen);
                if (rc < 0)
                        return rc;
 
-               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);
@@ -1816,20 +1997,17 @@ static int mdt_migrate_lookup(struct mdt_thread_info *info,
                fid_zero(fid);
                rc = mdo_lookup(env, mdt_object_child(stripe), lname, fid,
                                &info->mti_spec);
-               if (rc == -ENOENT && is_migrating) {
+               if (rc == -ENOENT && lmv_is_layout_changing(lmv)) {
                        /*
-                        * 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.
+                        * if parent layout is changeing, 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,
+                       rc = lmv_name_to_stripe_index(lmv, lname->ln_name,
                                                      lname->ln_namelen);
                        if (rc < 0)
                                return rc;
@@ -1915,7 +2093,7 @@ static int mdd_migrate_close(struct mdt_thread_info *info,
         * 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_reprocess_all(lease->l_resource, lease);
        LDLM_LOCK_PUT(lease);
 
 close:
@@ -1939,10 +2117,12 @@ close:
  *  9. unlock above locks
  * 10. sync device if source has links
  */
-static int mdt_reint_migrate_internal(struct mdt_thread_info *info)
+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;
@@ -1952,6 +2132,7 @@ static int mdt_reint_migrate_internal(struct mdt_thread_info *info)
        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;
@@ -1962,11 +2143,18 @@ static int mdt_reint_migrate_internal(struct mdt_thread_info *info)
        bool open_sem_locked = false;
        bool do_sync = false;
        int rc;
-       ENTRY;
 
+       ENTRY;
        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);
@@ -1974,26 +2162,45 @@ static int mdt_reint_migrate_internal(struct mdt_thread_info *info)
        if (!mdt->mdt_enable_remote_dir || !mdt->mdt_enable_dir_migration)
                RETURN(-EPERM);
 
-       if (!md_capable(uc, CFS_CAP_SYS_ADMIN) &&
+       if (uc && !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.
+        *
+        * req is NULL if this is called by directory auto-split.
+        */
+       if (req && !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);
+               }
+       }
+
        /* pobj is master object of parent */
-       pobj = mdt_parent_find_check(info, rr->rr_fid1, 0);
+       pobj = mdt_object_find(env, mdt, rr->rr_fid1);
        if (IS_ERR(pobj))
-               RETURN(PTR_ERR(pobj));
+               GOTO(unlock_rename, rc = PTR_ERR(pobj));
 
-       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);
+       if (req) {
+               rc = mdt_version_get_check(info, pobj, 0);
+               if (rc)
+                       GOTO(put_parent, rc);
        }
 
-       ma->ma_lmv = info->mti_big_lmm;
-       ma->ma_lmv_size = info->mti_big_lmmsize;
-       ma->ma_valid = 0;
+       if (!mdt_object_exists(pobj))
+               GOTO(put_parent, rc = -ENOENT);
+
+       if (!S_ISDIR(lu_object_attr(&pobj->mot_obj)))
+               GOTO(put_parent, rc = -ENOTDIR);
+
        rc = mdt_stripe_get(info, pobj, ma, XATTR_NAME_LMV);
        if (rc)
                GOTO(put_parent, rc);
@@ -2035,18 +2242,14 @@ lock_parent:
         */
        do_sync = rc;
 
-       /* TODO: DoM migration is not supported yet */
+       /* TODO: DoM migration is not supported, migrate dirent only */
        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);
+                       GOTO(unlock_links, rc);
 
-               if (ma->ma_valid & MA_LOV &&
-                   mdt_lmm_dom_entry(ma->ma_lmm) != LMM_NO_DOM)
-                       GOTO(put_source, rc = -EOPNOTSUPP);
+               if (ma->ma_valid & MA_LOV && mdt_lmm_dom_stripesize(ma->ma_lmm))
+                       info->mti_spec.sp_migrate_nsonly = 1;
        }
 
        /* if migration HSM is allowed */
@@ -2101,7 +2304,11 @@ lock_parent:
 
        rc = mdo_migrate(env, mdt_object_child(pobj),
                         mdt_object_child(sobj), &rr->rr_name,
-                        mdt_object_child(tobj), &info->mti_spec, ma);
+                        mdt_object_child(tobj),
+                        &info->mti_spec, ma);
+       if (!rc)
+               lprocfs_counter_incr(mdt->mdt_lu_dev.ld_obd->obd_md_stats,
+                                    LPROC_MDT_MIGRATE + LPROC_MD_LAST_OPC);
        EXIT;
 
        mdt_object_unlock(info, tobj, lht, rc);
@@ -2114,7 +2321,12 @@ unlock_open_sem:
        if (open_sem_locked)
                up_write(&sobj->mot_open_sem);
 unlock_links:
-       mdt_unlock_list(info, &link_locks, do_sync ?: rc);
+       /* if we've got too many locks to save into RPC,
+        * then just commit before the locks are released
+        */
+       if (!rc && do_sync)
+               mdt_device_sync(env, mdt);
+       mdt_unlock_list(info, &link_locks, do_sync ? 1 : rc);
 put_source:
        mdt_object_put(env, sobj);
        mdt_object_put(env, spobj);
@@ -2123,9 +2335,9 @@ unlock_parent:
                                  &parent_slave_locks, rc);
 put_parent:
        mdt_object_put(env, pobj);
-
-       if (!rc && do_sync)
-               mdt_device_sync(env, mdt);
+unlock_rename:
+       if (lustre_handle_is_used(&rename_lh))
+               mdt_rename_unlock(&rename_lh);
 
        return rc;
 }
@@ -2204,10 +2416,6 @@ static int mdt_rename_determine_lock_order(struct mdt_thread_info *info,
                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;
@@ -2241,24 +2449,79 @@ static int mdt_rename_determine_lock_order(struct mdt_thread_info *info,
 }
 
 /*
+ * lock rename source object.
+ *
+ * Both source and source parent may be remote, and source may be a remote
+ * object on source parent, to avoid overriding lock handle, store remote
+ * LOOKUP lock separately in @lhr.
+ *
+ * \retval     0 on success
+ * \retval     -ev negative errno upon error
+ */
+static int mdt_rename_source_lock(struct mdt_thread_info *info,
+                                 struct mdt_object *parent,
+                                 struct mdt_object *child,
+                                 struct mdt_lock_handle *lhc,
+                                 struct mdt_lock_handle *lhr,
+                                 __u64 ibits,
+                                 bool cos_incompat)
+{
+       int rc;
+
+       rc = mdt_is_remote_object(info, parent, child);
+       if (rc < 0)
+               return rc;
+
+       if (rc) {
+               /* enqueue remote LOOKUP lock from the parent MDT */
+               __u64 rmt_ibits = MDS_INODELOCK_LOOKUP;
+
+               if (mdt_object_remote(parent)) {
+                       rc = mdt_remote_object_lock(info, parent,
+                                                   mdt_object_fid(child),
+                                                   &lhr->mlh_rreg_lh,
+                                                   lhr->mlh_rreg_mode,
+                                                   rmt_ibits, false);
+                       if (rc != ELDLM_OK)
+                               return rc;
+               } else {
+                       LASSERT(mdt_object_remote(child));
+                       rc = mdt_object_local_lock(info, child, lhr,
+                                                  &rmt_ibits, 0, true);
+                       if (rc < 0)
+                               return rc;
+               }
+
+               ibits &= ~MDS_INODELOCK_LOOKUP;
+       }
+
+       if (mdt_object_remote(child)) {
+               rc = mdt_remote_object_lock(info, child, mdt_object_fid(child),
+                                           &lhc->mlh_rreg_lh,
+                                           lhc->mlh_rreg_mode,
+                                           ibits, false);
+               if (rc == ELDLM_OK)
+                       rc = 0;
+       } else {
+               rc = mdt_reint_object_lock(info, child, lhc, ibits,
+                                          cos_incompat);
+       }
+
+       if (!rc)
+               mdt_object_unlock(info, child, lhr, rc);
+
+       return rc;
+}
+
+/*
  * 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);
@@ -2266,22 +2529,32 @@ 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;
+       struct mdt_lock_handle *lh_rmt = NULL;
        struct mdt_lock_handle *lh_newp = NULL;
        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 cos_incompat, discard = false;
+       bool reverse = false, discard = false;
+       bool cos_incompat;
+       ktime_t kstart = ktime_get();
        int rc;
-       ENTRY;
 
+       ENTRY;
        DEBUG_REQ(D_INODE, req, "rename "DFID"/"DNAME" to "DFID"/"DNAME,
                  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_parent_find_check(info, rr->rr_fid1, 0);
        if (IS_ERR(msrcdir))
@@ -2298,16 +2571,44 @@ static int mdt_reint_rename_internal(struct mdt_thread_info *info,
                        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_put_tgtdir, rc);
+               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
-        * remote, relock parents. */
+        * remote, relock parents.
+        */
        cos_incompat = (mdt_object_remote(msrcdir) ||
                        mdt_object_remote(mtgtdir));
 
@@ -2325,7 +2626,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);
 
@@ -2333,20 +2634,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,
@@ -2355,7 +2657,7 @@ relock:
                }
                if (rc != 0) {
                        mdt_object_unlock(info, msrcdir, lh_srcdirp, rc);
-                       GOTO(out_put_tgtdir, rc);
+                       GOTO(out_unlock_rename, rc);
                }
        }
 
@@ -2385,8 +2687,12 @@ relock:
                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. */
+        * to avoid reverse locking.
+        */
        if (mtgtdir != msrcdir) {
                rc = mdo_is_subdir(info->mti_env, mdt_object_child(mtgtdir),
                                   old_fid);
@@ -2411,7 +2717,8 @@ relock:
 
        /* find mnew object:
         * mnew target object may not exist now
-        * lookup with version checking */
+        * lookup with version checking
+        */
        fid_zero(new_fid);
        rc = mdt_lookup_version_check(info, mtgtdir, &rr->rr_tgt_name, new_fid,
                                      3);
@@ -2452,35 +2759,25 @@ relock:
                /* Before locking the target dir, check we do not replace
                 * a dir with a non-dir, otherwise it may deadlock with
                 * link op which tries to create a link in this dir
-                * back to this non-dir. */
+                * back to this non-dir.
+                */
                if (S_ISDIR(lu_object_attr(&mnew->mot_obj)) &&
                    !S_ISDIR(lu_object_attr(&mold->mot_obj)))
                        GOTO(out_put_new, rc = -EISDIR);
 
                lh_oldp = &info->mti_lh[MDT_LH_OLD];
+               lh_rmt = &info->mti_lh[MDT_LH_RMT];
                mdt_lock_reg_init(lh_oldp, LCK_EX);
+               mdt_lock_reg_init(lh_rmt, LCK_EX);
                lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_XATTR;
-               if (mdt_object_remote(msrcdir)) {
-                       /* Enqueue lookup lock from the parent MDT */
-                       rc = mdt_remote_object_lock(info, msrcdir,
-                                                   mdt_object_fid(mold),
-                                                   &lh_oldp->mlh_rreg_lh,
-                                                   lh_oldp->mlh_rreg_mode,
-                                                   MDS_INODELOCK_LOOKUP,
-                                                   false);
-                       if (rc != ELDLM_OK)
-                               GOTO(out_put_new, rc);
-
-                       lock_ibits &= ~MDS_INODELOCK_LOOKUP;
-               }
-
-               rc = mdt_reint_object_lock(info, mold, lh_oldp, lock_ibits,
-                                          cos_incompat);
-               if (rc != 0)
-                       GOTO(out_unlock_old, rc);
+               rc = mdt_rename_source_lock(info, msrcdir, mold, lh_oldp,
+                                           lh_rmt, lock_ibits, cos_incompat);
+               if (rc < 0)
+                       GOTO(out_put_new, rc);
 
                /* Check if @msrcdir is subdir of @mnew, before locking child
-                * to avoid reverse locking. */
+                * to avoid reverse locking.
+                */
                if (mtgtdir != msrcdir) {
                        rc = mdo_is_subdir(info->mti_env,
                                           mdt_object_child(msrcdir), new_fid);
@@ -2494,43 +2791,43 @@ relock:
                /* We used to acquire MDS_INODELOCK_FULL here but we
                 * can't do this now because a running HSM restore on
                 * the rename onto victim will hold the layout
-                * lock. See LU-4002. */
+                * lock. See LU-4002.
+                */
 
                lh_newp = &info->mti_lh[MDT_LH_NEW];
                mdt_lock_reg_init(lh_newp, LCK_EX);
-               rc = mdt_reint_object_lock(info, mnew, lh_newp,
-                                          MDS_INODELOCK_LOOKUP |
-                                          MDS_INODELOCK_UPDATE,
+               lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
+               if (mdt_object_remote(mtgtdir)) {
+                       rc = mdt_remote_object_lock(info, mtgtdir,
+                                                   mdt_object_fid(mnew),
+                                                   &lh_newp->mlh_rreg_lh,
+                                                   lh_newp->mlh_rreg_mode,
+                                                   MDS_INODELOCK_LOOKUP,
+                                                   false);
+                       if (rc != ELDLM_OK)
+                               GOTO(out_unlock_old, rc);
+
+                       lock_ibits &= ~MDS_INODELOCK_LOOKUP;
+               }
+               rc = mdt_reint_object_lock(info, mnew, lh_newp, lock_ibits,
                                           cos_incompat);
                if (rc != 0)
-                       GOTO(out_unlock_old, rc);
+                       GOTO(out_unlock_new, rc);
 
                /* get and save version after locking */
                mdt_version_get_save(info, mnew, 3);
-       } else if (rc != -EREMOTE && rc != -ENOENT) {
+       } else if (rc != -ENOENT) {
                GOTO(out_put_old, rc);
        } else {
                lh_oldp = &info->mti_lh[MDT_LH_OLD];
+               lh_rmt = &info->mti_lh[MDT_LH_RMT];
                mdt_lock_reg_init(lh_oldp, LCK_EX);
+               mdt_lock_reg_init(lh_rmt, LCK_EX);
                lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_XATTR;
-               if (mdt_object_remote(msrcdir)) {
-                       /* Enqueue lookup lock from the parent MDT */
-                       rc = mdt_remote_object_lock(info, msrcdir,
-                                                   mdt_object_fid(mold),
-                                                   &lh_oldp->mlh_rreg_lh,
-                                                   lh_oldp->mlh_rreg_mode,
-                                                   MDS_INODELOCK_LOOKUP,
-                                                   false);
-                       if (rc != ELDLM_OK)
-                               GOTO(out_put_old, rc);
-
-                       lock_ibits &= ~MDS_INODELOCK_LOOKUP;
-               }
-
-               rc = mdt_reint_object_lock(info, mold, lh_oldp, lock_ibits,
-                                          cos_incompat);
+               rc = mdt_rename_source_lock(info, msrcdir, mold, lh_oldp,
+                                           lh_rmt, lock_ibits, cos_incompat);
                if (rc != 0)
-                       GOTO(out_unlock_old, rc);
+                       GOTO(out_put_old, rc);
 
                mdt_enoent_version_save(info, 3);
        }
@@ -2554,111 +2851,67 @@ relock:
 
        /* handle last link of tgt object */
        if (rc == 0) {
-               mdt_counter_incr(req, LPROC_MDT_RENAME);
+               mdt_counter_incr(req, LPROC_MDT_RENAME,
+                                ktime_us_delta(ktime_get(), kstart));
                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);
+                                        msrcdir, mtgtdir,
+                                        ktime_us_delta(ktime_get(), kstart));
        }
 
        EXIT;
+out_unlock_new:
        if (mnew != NULL)
                mdt_object_unlock(info, mnew, lh_newp, rc);
 out_unlock_old:
+       mdt_object_unlock(info, NULL, lh_rmt, rc);
        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);
 
-       /* If 'discard' is set then new_fid must exits.
-        * DOM data discard need neither object nor lock,
-        * so do this at the end.
+       /* 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, new_fid);
-
-       return rc;
-}
-
-static int mdt_reint_rename_or_migrate(struct mdt_thread_info *info,
-                                      struct mdt_lock_handle *lhc, bool rename)
-{
-       struct mdt_reint_record *rr = &info->mti_rr;
-       struct ptlrpc_request   *req = mdt_info_req(info);
-       struct lustre_handle    rename_lh = { 0 };
-       int                     rc;
-       ENTRY;
-
-       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);
-
-       /* 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 (discard) {
+               mdt_dom_discard_data(info, mnew);
+               mdt_object_put(info->mti_env, mnew);
        }
-
-       if (rename)
-               rc = mdt_reint_rename_internal(info, lhc);
-       else
-               rc = mdt_reint_migrate_internal(info);
-
-       if (lustre_handle_is_used(&rename_lh))
-               mdt_rename_unlock(&rename_lh);
-
-       RETURN(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);
-}
-
-static int mdt_reint_migrate(struct mdt_thread_info *info,
-                           struct mdt_lock_handle *lhc)
-{
-       return mdt_reint_rename_or_migrate(info, lhc, false);
+       return rc;
 }
 
 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 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;
+       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;
 
-       DEBUG_REQ(D_INODE, req, DFID": FLR file resync\n", PFID(rr->rr_fid1));
+       ENTRY;
+       DEBUG_REQ(D_INODE, req, DFID", FLR file resync", PFID(rr->rr_fid1));
 
        if (info->mti_dlm_req)
                ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
@@ -2683,7 +2936,8 @@ static int mdt_reint_resync(struct mdt_thread_info *info,
        /* 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. */
+        * 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);
 
@@ -2695,10 +2949,13 @@ static int mdt_reint_resync(struct mdt_thread_info *info,
 
        /* 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);
+       lhc = &info->mti_lh[MDT_LH_LOCAL];
+       rc = mdt_layout_change(info, mo, lhc, &layout);
        if (rc)
                GOTO(out_unlock, rc);
 
+       mdt_object_unlock(info, mo, lhc, 0);
+
        ma->ma_need = MA_INODE;
        ma->ma_valid = 0;
        rc = mdt_attr_get_complex(info, mo, ma);
@@ -2776,8 +3033,8 @@ int mdt_reint_rec(struct mdt_thread_info *info,
 {
        const struct mdt_reinter *mr;
        int rc;
-       ENTRY;
 
+       ENTRY;
        if (!(info->mti_rr.rr_opcode < ARRAY_SIZE(mdt_reinters)))
                RETURN(-EPROTO);