Whamcloud - gitweb
LU-5396 lod: (and mdt, mgs) make some symbols static
[fs/lustre-release.git] / lustre / mdt / mdt_handler.c
index f882b4d..5118c44 100644 (file)
@@ -27,7 +27,7 @@
  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2010, 2013, Intel Corporation.
+ * Copyright (c) 2010, 2014, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -53,6 +53,7 @@
  * struct OBD_{ALLOC,FREE}*()
  */
 #include <obd_support.h>
+#include <lustre_ioctl.h>
 /* struct ptlrpc_request */
 #include <lustre_net.h>
 /* struct obd_export */
@@ -67,8 +68,8 @@
 #include <lustre_acl.h>
 #include <lustre_param.h>
 #include <lustre_quota.h>
-#include <lustre_linkea.h>
 #include <lustre_lfsck.h>
+#include <lustre_nodemap.h>
 
 mdl_mode_t mdt_mdl_lock_modes[] = {
         [LCK_MINMODE] = MDL_MINMODE,
@@ -263,7 +264,7 @@ static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o
         EXIT;
 }
 
-int mdt_getstatus(struct tgt_session_info *tsi)
+static int mdt_getstatus(struct tgt_session_info *tsi)
 {
        struct mdt_thread_info  *info = tsi2mdt_info(tsi);
        struct mdt_device       *mdt = info->mti_mdt;
@@ -280,15 +281,15 @@ int mdt_getstatus(struct tgt_session_info *tsi)
                GOTO(out, rc = err_serious(-ENOMEM));
 
        repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
-       repbody->fid1 = mdt->mdt_md_root_fid;
-       repbody->valid |= OBD_MD_FLID;
+       repbody->mbo_fid1 = mdt->mdt_md_root_fid;
+       repbody->mbo_valid |= OBD_MD_FLID;
 
        if (tsi->tsi_tgt->lut_mds_capa &&
            exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
                struct mdt_object       *root;
                struct lustre_capa      *capa;
 
-               root = mdt_object_find(info->mti_env, mdt, &repbody->fid1);
+               root = mdt_object_find(info->mti_env, mdt, &repbody->mbo_fid1);
                if (IS_ERR(root))
                        GOTO(out, rc = PTR_ERR(root));
 
@@ -299,7 +300,7 @@ int mdt_getstatus(struct tgt_session_info *tsi)
                                  0);
                 mdt_object_put(info->mti_env, root);
                 if (rc == 0)
-                        repbody->valid |= OBD_MD_FLMDSCAPA;
+                       repbody->mbo_valid |= OBD_MD_FLMDSCAPA;
         }
        EXIT;
 out:
@@ -307,7 +308,7 @@ out:
        return rc;
 }
 
-int mdt_statfs(struct tgt_session_info *tsi)
+static int mdt_statfs(struct tgt_session_info *tsi)
 {
        struct ptlrpc_request           *req = tgt_ses_req(tsi);
        struct mdt_thread_info          *info = tsi2mdt_info(tsi);
@@ -383,78 +384,142 @@ static void mdt_pack_size2body(struct mdt_thread_info *info,
             !(ma->ma_valid & MA_SOM))
                 return;
 
-        b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
-        b->size = ma->ma_som->msd_size;
-        b->blocks = ma->ma_som->msd_blocks;
+       b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
+       b->mbo_size = ma->ma_som->msd_size;
+       b->mbo_blocks = ma->ma_som->msd_blocks;
 }
 
+#ifdef CONFIG_FS_POSIX_ACL
+/*
+ * Pack ACL data into the reply. UIDs/GIDs are mapped and filtered by nodemap.
+ *
+ * \param      info    thread info object
+ * \param      repbody reply to pack ACLs into
+ * \param      o       mdt object of file to examine
+ * \param      nodemap nodemap of client to reply to
+ * \retval     0       success
+ * \retval     -errno  error getting or parsing ACL from disk
+ */
+int mdt_pack_acl2body(struct mdt_thread_info *info, struct mdt_body *repbody,
+                     struct mdt_object *o, struct lu_nodemap *nodemap)
+{
+       const struct lu_env     *env = info->mti_env;
+       struct md_object        *next = mdt_object_child(o);
+       struct lu_buf           *buf = &info->mti_buf;
+       int rc;
+
+       buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
+       buf->lb_len = req_capsule_get_size(info->mti_pill, &RMF_ACL,
+                                          RCL_SERVER);
+       if (buf->lb_len == 0)
+               return 0;
+
+       rc = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_ACCESS);
+       if (rc < 0) {
+               if (rc == -ENODATA) {
+                       repbody->mbo_aclsize = 0;
+                       repbody->mbo_valid |= OBD_MD_FLACL;
+                       rc = 0;
+               } else if (rc == -EOPNOTSUPP) {
+                       rc = 0;
+               } else {
+                       CERROR("%s: unable to read "DFID" ACL: rc = %d\n",
+                              mdt_obd_name(info->mti_mdt),
+                              PFID(mdt_object_fid(o)), rc);
+               }
+       } else {
+               rc = nodemap_map_acl(nodemap, buf->lb_buf,
+                                    rc, NODEMAP_FS_TO_CLIENT);
+               /* if all ACLs mapped out, rc is still >= 0 */
+               if (rc < 0) {
+                       CERROR("%s: nodemap_map_acl unable to parse "DFID
+                              " ACL: rc = %d\n", mdt_obd_name(info->mti_mdt),
+                              PFID(mdt_object_fid(o)), rc);
+               } else {
+                       repbody->mbo_aclsize = rc;
+                       repbody->mbo_valid |= OBD_MD_FLACL;
+                       rc = 0;
+               }
+       }
+       return rc;
+}
+#endif
+
 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
                         const struct lu_attr *attr, const struct lu_fid *fid)
 {
-        struct md_attr *ma = &info->mti_attr;
-
-        LASSERT(ma->ma_valid & MA_INODE);
-
-        b->atime      = attr->la_atime;
-        b->mtime      = attr->la_mtime;
-        b->ctime      = attr->la_ctime;
-        b->mode       = attr->la_mode;
-        b->size       = attr->la_size;
-        b->blocks     = attr->la_blocks;
-        b->uid        = attr->la_uid;
-        b->gid        = attr->la_gid;
-        b->flags      = attr->la_flags;
-        b->nlink      = attr->la_nlink;
-        b->rdev       = attr->la_rdev;
-
-        /*XXX should pack the reply body according to lu_valid*/
-        b->valid |= OBD_MD_FLCTIME | OBD_MD_FLUID   |
-                    OBD_MD_FLGID   | OBD_MD_FLTYPE  |
-                    OBD_MD_FLMODE  | OBD_MD_FLNLINK | OBD_MD_FLFLAGS |
-                    OBD_MD_FLATIME | OBD_MD_FLMTIME ;
-
-        if (!S_ISREG(attr->la_mode)) {
-                b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
+       struct md_attr          *ma = &info->mti_attr;
+       struct obd_export       *exp = info->mti_exp;
+       struct lu_nodemap       *nodemap = exp->exp_target_data.ted_nodemap;
+
+       LASSERT(ma->ma_valid & MA_INODE);
+
+       b->mbo_atime      = attr->la_atime;
+       b->mbo_mtime      = attr->la_mtime;
+       b->mbo_ctime      = attr->la_ctime;
+       b->mbo_mode       = attr->la_mode;
+       b->mbo_size       = attr->la_size;
+       b->mbo_blocks     = attr->la_blocks;
+       b->mbo_uid        = nodemap_map_id(nodemap, NODEMAP_UID,
+                                          NODEMAP_FS_TO_CLIENT,
+                                          attr->la_uid);
+       b->mbo_gid        = nodemap_map_id(nodemap, NODEMAP_GID,
+                                          NODEMAP_FS_TO_CLIENT,
+                                          attr->la_gid);
+       b->mbo_flags      = attr->la_flags;
+       b->mbo_nlink      = attr->la_nlink;
+       b->mbo_rdev       = attr->la_rdev;
+
+       /* XXX should pack the reply body according to lu_valid */
+       b->mbo_valid |= OBD_MD_FLCTIME | OBD_MD_FLUID   |
+                       OBD_MD_FLGID   | OBD_MD_FLTYPE  |
+                       OBD_MD_FLMODE  | OBD_MD_FLNLINK | OBD_MD_FLFLAGS |
+                       OBD_MD_FLATIME | OBD_MD_FLMTIME ;
+
+       if (!S_ISREG(attr->la_mode)) {
+               b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
        } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
-                /* means no objects are allocated on osts. */
-                LASSERT(!(ma->ma_valid & MA_LOV));
-                /* just ignore blocks occupied by extend attributes on MDS */
-                b->blocks = 0;
-                /* if no object is allocated on osts, the size on mds is valid. b=22272 */
-                b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
+               /* means no objects are allocated on osts. */
+               LASSERT(!(ma->ma_valid & MA_LOV));
+               /* just ignore blocks occupied by extend attributes on MDS */
+               b->mbo_blocks = 0;
+               /* if no object is allocated on osts, the size on mds is valid.
+                * b=22272 */
+               b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
        } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL &&
                   ma->ma_lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) {
                /* A released file stores its size on MDS. */
                /* But return 1 block for released file, unless tools like tar
                 * will consider it fully sparse. (LU-3864)
                 */
-               if (unlikely(b->size == 0))
-                       b->blocks = 0;
+               if (unlikely(b->mbo_size == 0))
+                       b->mbo_blocks = 0;
                else
-                       b->blocks = 1;
-               b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
+                       b->mbo_blocks = 1;
+               b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
        }
 
-        if (fid) {
-                b->fid1 = *fid;
-                b->valid |= OBD_MD_FLID;
-                CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, size="LPU64"\n",
-                                PFID(fid), b->nlink, b->mode, b->size);
-        }
+       if (fid) {
+               b->mbo_fid1 = *fid;
+               b->mbo_valid |= OBD_MD_FLID;
+               CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, size="LPU64"\n",
+                               PFID(fid), b->mbo_nlink,
+                      b->mbo_mode, b->mbo_size);
+       }
 
-        if (info)
-                mdt_body_reverse_idmap(info, b);
+       if (info)
+               mdt_body_reverse_idmap(info, b);
 
-       if (fid != NULL && (b->valid & OBD_MD_FLSIZE))
-                CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
-                       PFID(fid), (unsigned long long)b->size);
+       if (fid != NULL && (b->mbo_valid & OBD_MD_FLSIZE))
+               CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
+                      PFID(fid), (unsigned long long)b->mbo_size);
 }
 
 static inline int mdt_body_has_lov(const struct lu_attr *la,
-                                   const struct mdt_body *body)
+                                  const struct mdt_body *body)
 {
-        return ((S_ISREG(la->la_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
-                (S_ISDIR(la->la_mode) && (body->valid & OBD_MD_FLDIREA )) );
+       return (S_ISREG(la->la_mode) && (body->mbo_valid & OBD_MD_FLEASIZE)) ||
+              (S_ISDIR(la->la_mode) && (body->mbo_valid & OBD_MD_FLDIREA));
 }
 
 void mdt_client_compatibility(struct mdt_thread_info *info)
@@ -482,8 +547,41 @@ void mdt_client_compatibility(struct mdt_thread_info *info)
         EXIT;
 }
 
-static int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
-                            char *name)
+static int mdt_attr_get_eabuf_size(struct mdt_thread_info *info,
+                                  struct mdt_object *o)
+{
+       const struct lu_env *env = info->mti_env;
+       int rc, rc2;
+
+       rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
+                         XATTR_NAME_LOV);
+
+       if (rc == -ENODATA)
+               rc = 0;
+
+       if (rc < 0)
+               goto out;
+
+       /* Is it a directory? Let's check for the LMV as well */
+       if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
+               rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
+                                  XATTR_NAME_LMV);
+
+               if (rc2 == -ENODATA)
+                       rc2 = mo_xattr_get(env, mdt_object_child(o),
+                                          &LU_BUF_NULL,
+                                          XATTR_NAME_DEFAULT_LMV);
+
+               if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
+                       rc = rc2;
+       }
+
+out:
+       return rc;
+}
+
+int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
+                     const char *name)
 {
        const struct lu_env *env = info->mti_env;
        int rc;
@@ -521,43 +619,83 @@ static int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
        RETURN(rc);
 }
 
-int mdt_attr_get_lov(struct mdt_thread_info *info,
-                    struct mdt_object *o, struct md_attr *ma)
+int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
+                  struct md_attr *ma, const char *name)
 {
        struct md_object *next = mdt_object_child(o);
        struct lu_buf    *buf = &info->mti_buf;
        int rc;
 
-       buf->lb_buf = ma->ma_lmm;
-       buf->lb_len = ma->ma_lmm_size;
-       rc = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_LOV);
+       if (strcmp(name, XATTR_NAME_LOV) == 0) {
+               buf->lb_buf = ma->ma_lmm;
+               buf->lb_len = ma->ma_lmm_size;
+               LASSERT(!(ma->ma_valid & MA_LOV));
+       } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
+               buf->lb_buf = ma->ma_lmv;
+               buf->lb_len = ma->ma_lmv_size;
+               LASSERT(!(ma->ma_valid & MA_LMV));
+       } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
+               buf->lb_buf = ma->ma_lmv;
+               buf->lb_len = ma->ma_lmv_size;
+               LASSERT(!(ma->ma_valid & MA_LMV_DEF));
+       } else {
+               return -EINVAL;
+       }
+
+       rc = mo_xattr_get(info->mti_env, next, buf, name);
        if (rc > 0) {
-               ma->ma_lmm_size = rc;
-               ma->ma_valid |= MA_LOV;
+
+got:
+               if (strcmp(name, XATTR_NAME_LOV) == 0) {
+                       if (info->mti_big_lmm_used)
+                               ma->ma_lmm = info->mti_big_lmm;
+
+                       /* NOT return LOV EA with hole to old client. */
+                       if (unlikely(le32_to_cpu(ma->ma_lmm->lmm_pattern) &
+                                    LOV_PATTERN_F_HOLE) &&
+                           !(exp_connect_flags(info->mti_exp) &
+                             OBD_CONNECT_LFSCK)) {
+                               return -EIO;
+                       } else {
+                               ma->ma_lmm_size = rc;
+                               ma->ma_valid |= MA_LOV;
+                       }
+               } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
+                       if (info->mti_big_lmm_used)
+                               ma->ma_lmv = info->mti_big_lmm;
+
+                       ma->ma_lmv_size = rc;
+                       ma->ma_valid |= MA_LMV;
+               } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
+                       ma->ma_lmv_size = rc;
+                       ma->ma_valid |= MA_LMV_DEF;
+               }
+
+               /* Update mdt_max_mdsize so all clients will be aware that */
+               if (info->mti_mdt->mdt_max_mdsize < rc)
+                       info->mti_mdt->mdt_max_mdsize = rc;
+
                rc = 0;
        } else if (rc == -ENODATA) {
                /* no LOV EA */
                rc = 0;
        } else if (rc == -ERANGE) {
-               rc = mdt_big_xattr_get(info, o, XATTR_NAME_LOV);
+               /* Default LMV has fixed size, so it must be able to fit
+                * in the original buffer */
+               if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
+                       return rc;
+               rc = mdt_big_xattr_get(info, o, name);
                if (rc > 0) {
                        info->mti_big_lmm_used = 1;
-                       ma->ma_valid |= MA_LOV;
-                       ma->ma_lmm = info->mti_big_lmm;
-                       ma->ma_lmm_size = rc;
-                       /* update mdt_max_mdsize so all clients
-                        * will be aware about that */
-                       if (info->mti_mdt->mdt_max_mdsize < rc)
-                               info->mti_mdt->mdt_max_mdsize = rc;
-                       rc = 0;
+                       goto got;
                }
        }
 
        return rc;
 }
 
-int mdt_attr_get_pfid(struct mdt_thread_info *info,
-                     struct mdt_object *o, struct lu_fid *pfid)
+static int mdt_attr_get_pfid(struct mdt_thread_info *info,
+                            struct mdt_object *o, struct lu_fid *pfid)
 {
        struct lu_buf           *buf = &info->mti_buf;
        struct link_ea_header   *leh;
@@ -609,13 +747,17 @@ int mdt_attr_get_complex(struct mdt_thread_info *info,
        const struct lu_env *env = info->mti_env;
        struct md_object    *next = mdt_object_child(o);
        struct lu_buf       *buf = &info->mti_buf;
-       u32                  mode = lu_object_attr(&next->mo_lu);
        int                  need = ma->ma_need;
        int                  rc = 0, rc2;
+       u32                  mode;
        ENTRY;
 
        ma->ma_valid = 0;
 
+       if (mdt_object_exists(o) == 0)
+               GOTO(out, rc = -ENOENT);
+       mode = lu_object_attr(&next->mo_lu);
+
        if (need & MA_INODE) {
                ma->ma_need = MA_INODE;
                rc = mo_attr_get(env, next, ma);
@@ -633,23 +775,21 @@ int mdt_attr_get_complex(struct mdt_thread_info *info,
        }
 
        if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
-               rc = mdt_attr_get_lov(info, o, ma);
+               rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
                if (rc)
                        GOTO(out, rc);
        }
 
        if (need & MA_LMV && S_ISDIR(mode)) {
-               buf->lb_buf = ma->ma_lmv;
-               buf->lb_len = ma->ma_lmv_size;
-               rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LMV);
-               if (rc2 > 0) {
-                       ma->ma_lmv_size = rc2;
-                       ma->ma_valid |= MA_LMV;
-               } else if (rc2 == -ENODATA) {
-                       /* no LMV EA */
-                       ma->ma_lmv_size = 0;
-               } else
-                       GOTO(out, rc = rc2);
+               rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
+               if (rc != 0)
+                       GOTO(out, rc);
+       }
+
+       if (need & MA_LMV_DEF && S_ISDIR(mode)) {
+               rc = mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
+               if (rc != 0)
+                       GOTO(out, rc);
        }
 
        if (need & MA_SOM && S_ISREG(mode)) {
@@ -703,44 +843,48 @@ out:
 static int mdt_getattr_internal(struct mdt_thread_info *info,
                                 struct mdt_object *o, int ma_need)
 {
-        struct md_object        *next = mdt_object_child(o);
-        const struct mdt_body   *reqbody = info->mti_body;
-        struct ptlrpc_request   *req = mdt_info_req(info);
-        struct md_attr          *ma = &info->mti_attr;
-        struct lu_attr          *la = &ma->ma_attr;
-        struct req_capsule      *pill = info->mti_pill;
-        const struct lu_env     *env = info->mti_env;
-        struct mdt_body         *repbody;
-        struct lu_buf           *buffer = &info->mti_buf;
-        int                     rc;
-       int                     is_root;
-        ENTRY;
+       struct md_object        *next = mdt_object_child(o);
+       const struct mdt_body   *reqbody = info->mti_body;
+       struct ptlrpc_request   *req = mdt_info_req(info);
+       struct md_attr          *ma = &info->mti_attr;
+       struct lu_attr          *la = &ma->ma_attr;
+       struct req_capsule      *pill = info->mti_pill;
+       const struct lu_env     *env = info->mti_env;
+       struct mdt_body         *repbody;
+       struct lu_buf           *buffer = &info->mti_buf;
+       struct obd_export       *exp = info->mti_exp;
+       struct lu_nodemap       *nodemap = exp->exp_target_data.ted_nodemap;
+       int                      rc;
+       int                      is_root;
+       ENTRY;
 
-        if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
-                RETURN(err_serious(-ENOMEM));
+       if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
+               RETURN(err_serious(-ENOMEM));
 
-        repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
+       repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
 
-        ma->ma_valid = 0;
+       ma->ma_valid = 0;
 
        if (mdt_object_remote(o)) {
                /* This object is located on remote node.*/
-               /* Return -EIO for old client */
+               /* Return -ENOTSUPP for old client */
                if (!mdt_is_dne_client(req->rq_export))
-                       GOTO(out, rc = -EIO);
+                       GOTO(out, rc = -ENOTSUPP);
 
-               repbody->fid1 = *mdt_object_fid(o);
-               repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
+               repbody->mbo_fid1 = *mdt_object_fid(o);
+               repbody->mbo_valid = OBD_MD_FLID | OBD_MD_MDS;
                GOTO(out, rc = 0);
        }
 
-       buffer->lb_len = reqbody->eadatasize;
-       if (buffer->lb_len > 0) {
+       if (reqbody->mbo_eadatasize > 0) {
                buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
                if (buffer->lb_buf == NULL)
                        GOTO(out, rc = -EPROTO);
+               buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
+                                                     RCL_SERVER);
        } else {
                buffer->lb_buf = NULL;
+               buffer->lb_len = 0;
                ma_need &= ~(MA_LOV | MA_LMV);
                CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
                       mdt_obd_name(info->mti_mdt),
@@ -749,13 +893,17 @@ static int mdt_getattr_internal(struct mdt_thread_info *info,
 
        /* If it is dir object and client require MEA, then we got MEA */
        if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
-           reqbody->valid & OBD_MD_MEA) {
+           (reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
                /* Assumption: MDT_MD size is enough for lmv size. */
                ma->ma_lmv = buffer->lb_buf;
                ma->ma_lmv_size = buffer->lb_len;
                ma->ma_need = MA_INODE;
-               if (ma->ma_lmm_size > 0)
-                       ma->ma_need |= MA_LMV;
+               if (ma->ma_lmv_size > 0) {
+                       if (reqbody->mbo_valid & OBD_MD_MEA)
+                               ma->ma_need |= MA_LMV;
+                       else if (reqbody->mbo_valid & OBD_MD_DEFAULT_MEA)
+                               ma->ma_need |= MA_LMV_DEF;
+               }
        } else {
                ma->ma_lmm = buffer->lb_buf;
                ma->ma_lmm_size = buffer->lb_len;
@@ -765,7 +913,7 @@ static int mdt_getattr_internal(struct mdt_thread_info *info,
        }
 
         if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
-            reqbody->valid & OBD_MD_FLDIREA  &&
+           reqbody->mbo_valid & OBD_MD_FLDIREA  &&
             lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
                 /* get default stripe info for this dir. */
                 ma->ma_need |= MA_LOV_DEF;
@@ -785,8 +933,8 @@ static int mdt_getattr_internal(struct mdt_thread_info *info,
        /* if file is released, check if a restore is running */
        if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_RELEASED) &&
            mdt_hsm_restore_is_running(info, mdt_object_fid(o))) {
-               repbody->t_state = MS_RESTORE;
-               repbody->valid |= OBD_MD_TSTATE;
+               repbody->mbo_t_state = MS_RESTORE;
+               repbody->mbo_valid |= OBD_MD_TSTATE;
        }
 
        is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
@@ -805,7 +953,7 @@ static int mdt_getattr_internal(struct mdt_thread_info *info,
                root = mdt_object_find(env, mdt, &rootfid);
                if (IS_ERR(root))
                        RETURN(PTR_ERR(root));
-               rc = mdt_attr_get_lov(info, root, ma);
+               rc = mdt_stripe_get(info, root, ma, XATTR_NAME_LOV);
                mdt_object_put(info->mti_env, root);
                if (unlikely(rc)) {
                        CERROR("%s: getattr error for "DFID": rc = %d\n",
@@ -823,24 +971,38 @@ static int mdt_getattr_internal(struct mdt_thread_info *info,
         if (mdt_body_has_lov(la, reqbody)) {
                 if (ma->ma_valid & MA_LOV) {
                         LASSERT(ma->ma_lmm_size);
-                        mdt_dump_lmm(D_INFO, ma->ma_lmm);
-                        repbody->eadatasize = ma->ma_lmm_size;
-                        if (S_ISDIR(la->la_mode))
-                                repbody->valid |= OBD_MD_FLDIREA;
-                        else
-                                repbody->valid |= OBD_MD_FLEASIZE;
-                }
-                if (ma->ma_valid & MA_LMV) {
-                        LASSERT(S_ISDIR(la->la_mode));
-                        repbody->eadatasize = ma->ma_lmv_size;
-                        repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
+                       repbody->mbo_eadatasize = ma->ma_lmm_size;
+                       if (S_ISDIR(la->la_mode))
+                               repbody->mbo_valid |= OBD_MD_FLDIREA;
+                       else
+                               repbody->mbo_valid |= OBD_MD_FLEASIZE;
+                       mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->mbo_valid);
                 }
+               if (ma->ma_valid & MA_LMV) {
+                       /* Return -ENOTSUPP for old client */
+                       if (!mdt_is_striped_client(req->rq_export))
+                               RETURN(-ENOTSUPP);
+
+                       LASSERT(S_ISDIR(la->la_mode));
+                       mdt_dump_lmv(D_INFO, ma->ma_lmv);
+                       repbody->mbo_eadatasize = ma->ma_lmv_size;
+                       repbody->mbo_valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
+               }
+               if (ma->ma_valid & MA_LMV_DEF) {
+                       /* Return -ENOTSUPP for old client */
+                       if (!mdt_is_striped_client(req->rq_export))
+                               RETURN(-ENOTSUPP);
+                       LASSERT(S_ISDIR(la->la_mode));
+                       repbody->mbo_eadatasize = ma->ma_lmv_size;
+                       repbody->mbo_valid |= (OBD_MD_FLDIREA |
+                                              OBD_MD_DEFAULT_MEA);
+               }
        } else if (S_ISLNK(la->la_mode) &&
-                  reqbody->valid & OBD_MD_LINKNAME) {
+                  reqbody->mbo_valid & OBD_MD_LINKNAME) {
                buffer->lb_buf = ma->ma_lmm;
                /* eadatasize from client includes NULL-terminator, so
                 * there is no need to read it */
-               buffer->lb_len = reqbody->eadatasize - 1;
+               buffer->lb_len = reqbody->mbo_eadatasize - 1;
                rc = mo_readlink(env, next, buffer);
                if (unlikely(rc <= 0)) {
                        CERROR("%s: readlink failed for "DFID": rc = %d\n",
@@ -852,16 +1014,16 @@ static int mdt_getattr_internal(struct mdt_thread_info *info,
 
                        if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
                                rc -= 2;
-                       repbody->valid |= OBD_MD_LINKNAME;
+                       repbody->mbo_valid |= OBD_MD_LINKNAME;
                        /* we need to report back size with NULL-terminator
                         * because client expects that */
-                       repbody->eadatasize = rc + 1;
-                       if (repbody->eadatasize != reqbody->eadatasize)
+                       repbody->mbo_eadatasize = rc + 1;
+                       if (repbody->mbo_eadatasize != reqbody->mbo_eadatasize)
                                CDEBUG(D_INODE, "%s: Read shorter symlink %d "
                                       "on "DFID ", expected %d\n",
                                       mdt_obd_name(info->mti_mdt),
                                       rc, PFID(mdt_object_fid(o)),
-                                      reqbody->eadatasize - 1);
+                                      reqbody->mbo_eadatasize - 1);
                        /* NULL terminate */
                        ((char *)ma->ma_lmm)[rc] = 0;
 
@@ -875,62 +1037,37 @@ static int mdt_getattr_internal(struct mdt_thread_info *info,
                 }
         }
 
-        if (reqbody->valid & OBD_MD_FLMODEASIZE) {
-               repbody->max_cookiesize = 0;
-                repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
-                repbody->valid |= OBD_MD_FLMODEASIZE;
-                CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
-                       "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
-                       repbody->max_cookiesize);
-        }
+       if (reqbody->mbo_valid & OBD_MD_FLMODEASIZE) {
+               repbody->mbo_max_cookiesize = 0;
+               repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
+               repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
+               CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
+                      "MAX_COOKIE to : %d:%d\n", repbody->mbo_max_mdsize,
+                      repbody->mbo_max_cookiesize);
+       }
 
-        if (exp_connect_rmtclient(info->mti_exp) &&
-            reqbody->valid & OBD_MD_FLRMTPERM) {
-                void *buf = req_capsule_server_get(pill, &RMF_ACL);
+       if (exp_connect_rmtclient(info->mti_exp) &&
+           reqbody->mbo_valid & OBD_MD_FLRMTPERM) {
+               void *buf = req_capsule_server_get(pill, &RMF_ACL);
 
-                /* mdt_getattr_lock only */
-                rc = mdt_pack_remote_perm(info, o, buf);
-                if (rc) {
-                        repbody->valid &= ~OBD_MD_FLRMTPERM;
-                        repbody->aclsize = 0;
-                        RETURN(rc);
-                } else {
-                        repbody->valid |= OBD_MD_FLRMTPERM;
-                        repbody->aclsize = sizeof(struct mdt_remote_perm);
-                }
-        }
+               /* mdt_getattr_lock only */
+               rc = mdt_pack_remote_perm(info, o, buf);
+               if (rc) {
+                       repbody->mbo_valid &= ~OBD_MD_FLRMTPERM;
+                       repbody->mbo_aclsize = 0;
+                       RETURN(rc);
+               } else {
+                       repbody->mbo_valid |= OBD_MD_FLRMTPERM;
+                       repbody->mbo_aclsize = sizeof(struct mdt_remote_perm);
+               }
+       }
 #ifdef CONFIG_FS_POSIX_ACL
        else if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
-                (reqbody->valid & OBD_MD_FLACL)) {
-                buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
-                buffer->lb_len = req_capsule_get_size(pill,
-                                                      &RMF_ACL, RCL_SERVER);
-                if (buffer->lb_len > 0) {
-                        rc = mo_xattr_get(env, next, buffer,
-                                          XATTR_NAME_ACL_ACCESS);
-                        if (rc < 0) {
-                                if (rc == -ENODATA) {
-                                        repbody->aclsize = 0;
-                                        repbody->valid |= OBD_MD_FLACL;
-                                        rc = 0;
-                                } else if (rc == -EOPNOTSUPP) {
-                                        rc = 0;
-                               } else {
-                                       CERROR("%s: unable to read "DFID
-                                              " ACL: rc = %d\n",
-                                              mdt_obd_name(info->mti_mdt),
-                                              PFID(mdt_object_fid(o)), rc);
-                               }
-                        } else {
-                                repbody->aclsize = rc;
-                                repbody->valid |= OBD_MD_FLACL;
-                                rc = 0;
-                        }
-                }
-        }
+                (reqbody->mbo_valid & OBD_MD_FLACL))
+               rc = mdt_pack_acl2body(info, repbody, o, nodemap);
 #endif
 
-       if (reqbody->valid & OBD_MD_FLMDSCAPA &&
+       if (reqbody->mbo_valid & OBD_MD_FLMDSCAPA &&
            info->mti_mdt->mdt_lut.lut_mds_capa &&
            exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
                 struct lustre_capa *capa;
@@ -941,7 +1078,7 @@ static int mdt_getattr_internal(struct mdt_thread_info *info,
                 rc = mo_capa_get(env, next, capa, 0);
                 if (rc)
                         RETURN(rc);
-                repbody->valid |= OBD_MD_FLMDSCAPA;
+               repbody->mbo_valid |= OBD_MD_FLMDSCAPA;
         }
 
 out:
@@ -979,25 +1116,24 @@ static int mdt_renew_capa(struct mdt_thread_info *info)
         *capa = *c;
         rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
         if (rc == 0)
-                body->valid |= OBD_MD_FLOSSCAPA;
+               body->mbo_valid |= OBD_MD_FLOSSCAPA;
         RETURN(rc);
 }
 
-int mdt_getattr(struct tgt_session_info *tsi)
+static int mdt_getattr(struct tgt_session_info *tsi)
 {
        struct mdt_thread_info  *info = tsi2mdt_info(tsi);
         struct mdt_object       *obj = info->mti_object;
         struct req_capsule      *pill = info->mti_pill;
         struct mdt_body         *reqbody;
         struct mdt_body         *repbody;
-        mode_t                   mode;
         int rc, rc2;
         ENTRY;
 
         reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
         LASSERT(reqbody);
 
-        if (reqbody->valid & OBD_MD_FLOSSCAPA) {
+       if (reqbody->mbo_valid & OBD_MD_FLOSSCAPA) {
                 rc = req_capsule_server_pack(pill);
                 if (unlikely(rc))
                         RETURN(err_serious(rc));
@@ -1008,13 +1144,36 @@ int mdt_getattr(struct tgt_session_info *tsi)
         LASSERT(obj != NULL);
        LASSERT(lu_object_assert_exists(&obj->mot_obj));
 
-       mode = lu_object_attr(&obj->mot_obj);
+       /* Unlike intent case where we need to pre-fill out buffers early on
+        * in intent policy for ldlm reasons, here we can have a much better
+        * guess at EA size by just reading it from disk.
+        * Exceptions are readdir and (missing) directory striping */
+       /* Readlink */
+       if (reqbody->mbo_valid & OBD_MD_LINKNAME) {
+               /* No easy way to know how long is the symlink, but it cannot
+                * be more than PATH_MAX, so we allocate +1 */
+               rc = PATH_MAX + 1;
+
+       /* A special case for fs ROOT: getattr there might fetch
+        * default EA for entire fs, not just for this dir!
+        */
+       } else if (lu_fid_eq(mdt_object_fid(obj),
+                            &info->mti_mdt->mdt_md_root_fid) &&
+                  (reqbody->mbo_valid & OBD_MD_FLDIREA) &&
+                  (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
+                                                                MDS_GETATTR)) {
+               /* Should the default strping be bigger, mdt_fix_reply
+                * will reallocate */
+               rc = DEF_REP_MD_SIZE;
+       } else {
+               /* Read the actual EA size from disk */
+               rc = mdt_attr_get_eabuf_size(info, obj);
+       }
 
-       /* old clients may not report needed easize, use max value then */
-       req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
-                            reqbody->eadatasize == 0 ?
-                            info->mti_mdt->mdt_max_mdsize :
-                            reqbody->eadatasize);
+       if (rc < 0)
+               GOTO(out, rc = err_serious(rc));
+
+       req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
 
        rc = req_capsule_server_pack(pill);
        if (unlikely(rc != 0))
@@ -1022,25 +1181,25 @@ int mdt_getattr(struct tgt_session_info *tsi)
 
         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
         LASSERT(repbody != NULL);
-        repbody->eadatasize = 0;
-        repbody->aclsize = 0;
+       repbody->mbo_eadatasize = 0;
+       repbody->mbo_aclsize = 0;
 
-        if (reqbody->valid & OBD_MD_FLRMTPERM)
-                rc = mdt_init_ucred(info, reqbody);
-        else
-                rc = mdt_check_ucred(info);
-        if (unlikely(rc))
-                GOTO(out_shrink, rc);
+       if (reqbody->mbo_valid & OBD_MD_FLRMTPERM)
+               rc = mdt_init_ucred(info, reqbody);
+       else
+               rc = mdt_check_ucred(info);
+       if (unlikely(rc))
+               GOTO(out_shrink, rc);
 
-        info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
+       info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
 
-        /*
-         * Don't check capability at all, because rename might getattr for
-         * remote obj, and at that time no capability is available.
-         */
-        mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
-        rc = mdt_getattr_internal(info, obj, 0);
-        if (reqbody->valid & OBD_MD_FLRMTPERM)
+       /*
+        * Don't check capability at all, because rename might getattr for
+        * remote obj, and at that time no capability is available.
+        */
+       mdt_set_capainfo(info, 1, &reqbody->mbo_fid1, BYPASS_CAPA);
+       rc = mdt_getattr_internal(info, obj, 0);
+       if (reqbody->mbo_valid & OBD_MD_FLRMTPERM)
                 mdt_exit_ucred(info);
         EXIT;
 out_shrink:
@@ -1053,36 +1212,7 @@ out:
        return rc;
 }
 
-int mdt_is_subdir(struct tgt_session_info *tsi)
-{
-       struct mdt_thread_info  *info = tsi2mdt_info(tsi);
-        struct mdt_object     *o = info->mti_object;
-        struct req_capsule    *pill = info->mti_pill;
-        const struct mdt_body *body = info->mti_body;
-        struct mdt_body       *repbody;
-        int                    rc;
-        ENTRY;
-
-        LASSERT(o != NULL);
-
-        repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
-
-       /*
-        * We save last checked parent fid to @repbody->fid1 for remote
-        * directory case.
-        */
-       LASSERT(fid_is_sane(&body->fid2));
-       LASSERT(mdt_object_exists(o) && !mdt_object_remote(o));
-       rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
-                          &body->fid2, &repbody->fid1);
-       if (rc == 0 || rc == -EREMOTE)
-               repbody->valid |= OBD_MD_FLID;
-
-       mdt_thread_info_fini(info);
-       RETURN(rc);
-}
-
-int mdt_swap_layouts(struct tgt_session_info *tsi)
+static int mdt_swap_layouts(struct tgt_session_info *tsi)
 {
        struct mdt_thread_info  *info;
        struct ptlrpc_request   *req = tgt_ses_req(tsi);
@@ -1104,26 +1234,30 @@ int mdt_swap_layouts(struct tgt_session_info *tsi)
                RETURN(-EOPNOTSUPP);
 
        info = tsi2mdt_info(tsi);
+
+       if (info->mti_dlm_req != NULL)
+               ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
+
        if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
-               mdt_set_capainfo(info, 0, &info->mti_body->fid1,
+               mdt_set_capainfo(info, 0, &info->mti_body->mbo_fid1,
                                 req_capsule_client_get(info->mti_pill,
                                                        &RMF_CAPA1));
 
        if (req_capsule_get_size(info->mti_pill, &RMF_CAPA2, RCL_CLIENT))
-               mdt_set_capainfo(info, 1, &info->mti_body->fid2,
+               mdt_set_capainfo(info, 1, &info->mti_body->mbo_fid2,
                                 req_capsule_client_get(info->mti_pill,
                                                        &RMF_CAPA2));
 
        o1 = info->mti_object;
        o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
-                               &info->mti_body->fid2);
+                               &info->mti_body->mbo_fid2);
        if (IS_ERR(o))
                GOTO(out, rc = PTR_ERR(o));
 
        if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
                GOTO(put, rc = -ENOENT);
 
-       rc = lu_fid_cmp(&info->mti_body->fid1, &info->mti_body->fid2);
+       rc = lu_fid_cmp(&info->mti_body->mbo_fid1, &info->mti_body->mbo_fid2);
        if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
                GOTO(put, rc);
 
@@ -1176,43 +1310,32 @@ out:
 }
 
 static int mdt_raw_lookup(struct mdt_thread_info *info,
-                          struct mdt_object *parent,
-                          const struct lu_name *lname,
-                          struct ldlm_reply *ldlm_rep)
+                         struct mdt_object *parent,
+                         const struct lu_name *lname,
+                         struct ldlm_reply *ldlm_rep)
 {
-        struct md_object *next = mdt_object_child(info->mti_object);
-        const struct mdt_body *reqbody = info->mti_body;
-        struct lu_fid *child_fid = &info->mti_tmp_fid1;
-        struct mdt_body *repbody;
-        int rc;
-        ENTRY;
-
-        if (reqbody->valid != OBD_MD_FLID)
-                RETURN(0);
-
-        LASSERT(!info->mti_cross_ref);
+       struct lu_fid   *child_fid = &info->mti_tmp_fid1;
+       int              rc;
+       ENTRY;
 
-        /* Only got the fid of this obj by name */
-        fid_zero(child_fid);
-        rc = mdo_lookup(info->mti_env, next, lname, child_fid,
-                        &info->mti_spec);
-#if 0
-        /* XXX is raw_lookup possible as intent operation? */
-        if (rc != 0) {
-                if (rc == -ENOENT)
-                        mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
-                RETURN(rc);
-        } else
-                mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
+       LASSERT(!info->mti_cross_ref);
+
+       /* Only got the fid of this obj by name */
+       fid_zero(child_fid);
+       rc = mdo_lookup(info->mti_env, mdt_object_child(info->mti_object),
+                       lname, child_fid, &info->mti_spec);
+       if (rc == 0) {
+               struct mdt_body *repbody;
+
+               repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
+               repbody->mbo_fid1 = *child_fid;
+               repbody->mbo_valid = OBD_MD_FLID;
+               mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
+       } else if (rc == -ENOENT) {
+               mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
+       }
 
-        repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
-#endif
-        if (rc == 0) {
-                repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
-                repbody->fid1 = *child_fid;
-                repbody->valid = OBD_MD_FLID;
-        }
-        RETURN(1);
+       RETURN(rc);
 }
 
 /*
@@ -1230,93 +1353,36 @@ static int mdt_getattr_name_lock(struct mdt_thread_info *info,
         struct mdt_body        *reqbody   = NULL;
         struct mdt_object      *parent    = info->mti_object;
         struct mdt_object      *child;
-        struct md_object       *next      = mdt_object_child(parent);
         struct lu_fid          *child_fid = &info->mti_tmp_fid1;
         struct lu_name         *lname     = NULL;
         struct mdt_lock_handle *lhp       = NULL;
         struct ldlm_lock       *lock;
-        struct ldlm_res_id     *res_id;
-        int                     is_resent;
-        int                     ma_need = 0;
-        int                     rc;
-
-        ENTRY;
-
-        is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
-        LASSERT(ergo(is_resent,
-                     lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
-
-        LASSERT(parent != NULL);
-
-       lname = &info->mti_name;
-       mdt_name_unpack(info->mti_pill, &RMF_NAME, lname, MNF_FIX_ANON);
-
-       if (!info->mti_cross_ref) {
-               /*
-                * XXX: Check for anonymous name is for getattr by fid
-                * (OBD_CONNECT_ATTRFID), otherwise do not allow empty name,
-                * that is the name must contain at least one character and
-                * the terminating '\0'.
-                */
-               if (!lu_name_is_valid(lname)) {
-                       reqbody = req_capsule_client_get(info->mti_pill,
-                                                        &RMF_MDT_BODY);
-                       if (unlikely(reqbody == NULL))
-                               RETURN(err_serious(-EFAULT));
-
-                       if (unlikely(!fid_is_sane(&reqbody->fid2)))
-                               RETURN(err_serious(-EINVAL));
-
-                       CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
-                              "ldlm_rep = %p\n",
-                              PFID(mdt_object_fid(parent)),
-                              PFID(&reqbody->fid2), ldlm_rep);
-               } else {
-                       CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
-                              "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
-                              PNAME(lname), ldlm_rep);
-               }
-       }
+       bool                    is_resent;
+       bool                    try_layout;
+       int                     ma_need = 0;
+       int                     rc;
+       ENTRY;
 
-        mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
+       is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
+       LASSERT(ergo(is_resent,
+                    lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
 
-       if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
-               LU_OBJECT_DEBUG(D_INODE, info->mti_env,
-                               &parent->mot_obj,
-                               "Parent doesn't exist!\n");
-               RETURN(-ESTALE);
-       } else if (!info->mti_cross_ref) {
-               LASSERTF(!mdt_object_remote(parent),
-                        "Parent "DFID" is on remote server\n",
-                        PFID(mdt_object_fid(parent)));
-       }
+       if (parent == NULL)
+               RETURN(-ENOENT);
 
-       if (lu_name_is_valid(lname)) {
-                rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
-                if (rc != 0) {
-                        if (rc > 0)
-                                rc = 0;
-                        RETURN(rc);
-                }
-        }
+       if (info->mti_cross_ref) {
+               /* Only getattr on the child. Parent is on another node. */
+               mdt_set_disposition(info, ldlm_rep,
+                                   DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
+               child = parent;
+               CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
+                      "ldlm_rep = %p\n",
+                      PFID(mdt_object_fid(child)), ldlm_rep);
 
-        if (info->mti_cross_ref) {
-                /* Only getattr on the child. Parent is on another node. */
-                mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
-                child = parent;
-                CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
-                       "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
-
-                if (is_resent) {
-                        /* Do not take lock for resent case. */
-                        lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
-                        LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
-                                 lhc->mlh_reg_lh.cookie);
-                        LASSERT(fid_res_name_eq(mdt_object_fid(child),
-                                                &lock->l_resource->lr_name));
-                        LDLM_LOCK_PUT(lock);
-                        rc = 0;
-                } else {
+               rc = mdt_check_resent_lock(info, child, lhc);
+               if (rc < 0) {
+                       RETURN(rc);
+               } else if (rc > 0) {
                        mdt_lock_handle_init(lhc);
                        mdt_lock_reg_init(lhc, LCK_PR);
 
@@ -1330,27 +1396,79 @@ static int mdt_getattr_name_lock(struct mdt_thread_info *info,
 
                        rc = mdt_object_lock(info, child, lhc, child_bits,
                                             MDT_LOCAL_LOCK);
+                       if (rc < 0)
+                               RETURN(rc);
                }
-                if (rc == 0) {
-                        /* Finally, we can get attr for child. */
-                       if (!mdt_object_exists(child)) {
-                               LU_OBJECT_DEBUG(D_INFO, info->mti_env,
-                                               &child->mot_obj,
-                                            "remote object doesn't exist.\n");
-                                mdt_object_unlock(info, child, lhc, 1);
-                               RETURN(-ENOENT);
-                       }
 
-                        mdt_set_capainfo(info, 0, mdt_object_fid(child),
-                                         BYPASS_CAPA);
-                        rc = mdt_getattr_internal(info, child, 0);
-                        if (unlikely(rc != 0))
-                                mdt_object_unlock(info, child, lhc, 1);
-                }
+               /* Finally, we can get attr for child. */
+               if (!mdt_object_exists(child)) {
+                       LU_OBJECT_DEBUG(D_INFO, info->mti_env,
+                                       &child->mot_obj,
+                                       "remote object doesn't exist.\n");
+                       mdt_object_unlock(info, child, lhc, 1);
+                       RETURN(-ENOENT);
+               }
+
+               mdt_set_capainfo(info, 0, mdt_object_fid(child), BYPASS_CAPA);
+               rc = mdt_getattr_internal(info, child, 0);
+               if (unlikely(rc != 0))
+                       mdt_object_unlock(info, child, lhc, 1);
+
                 RETURN(rc);
         }
 
+       lname = &info->mti_name;
+       mdt_name_unpack(info->mti_pill, &RMF_NAME, lname, MNF_FIX_ANON);
+
+       if (lu_name_is_valid(lname)) {
+               CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
+                      "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
+                      PNAME(lname), ldlm_rep);
+       } else {
+               reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
+               if (unlikely(reqbody == NULL))
+                       RETURN(err_serious(-EPROTO));
+
+               *child_fid = reqbody->mbo_fid2;
+
+               if (unlikely(!fid_is_sane(child_fid)))
+                       RETURN(err_serious(-EINVAL));
+
+               CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
+                      "ldlm_rep = %p\n",
+                      PFID(mdt_object_fid(parent)),
+                      PFID(&reqbody->mbo_fid2), ldlm_rep);
+       }
+
+       mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
+
+       if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
+               LU_OBJECT_DEBUG(D_INODE, info->mti_env,
+                               &parent->mot_obj,
+                               "Parent doesn't exist!\n");
+               RETURN(-ESTALE);
+       }
+
+       if (mdt_object_remote(parent)) {
+               CERROR("%s: parent "DFID" is on remote target\n",
+                      mdt_obd_name(info->mti_mdt),
+                      PFID(mdt_object_fid(parent)));
+               RETURN(-EIO);
+       }
+
        if (lu_name_is_valid(lname)) {
+               /* Always allow to lookup ".." */
+               if (unlikely(lname->ln_namelen == 2 &&
+                            lname->ln_name[0] == '.' &&
+                            lname->ln_name[1] == '.'))
+                       info->mti_spec.sp_permitted = 1;
+
+               if (info->mti_body->mbo_valid == OBD_MD_FLID) {
+                       rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
+
+                       RETURN(rc);
+               }
+
                /* step 1: lock parent only if parent is a directory */
                if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
                        lhp = &info->mti_lh[MDT_LH_PARENT];
@@ -1364,20 +1482,16 @@ static int mdt_getattr_name_lock(struct mdt_thread_info *info,
 
                 /* step 2: lookup child's fid by name */
                 fid_zero(child_fid);
-                rc = mdo_lookup(info->mti_env, next, lname, child_fid,
-                                &info->mti_spec);
+               rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
+                               child_fid, &info->mti_spec);
+               if (rc == -ENOENT)
+                       mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
 
-                if (rc != 0) {
-                        if (rc == -ENOENT)
-                                mdt_set_disposition(info, ldlm_rep,
-                                                    DISP_LOOKUP_NEG);
-                        GOTO(out_parent, rc);
-                } else
-                        mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
-        } else {
-                *child_fid = reqbody->fid2;
-                mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
-        }
+               if (rc != 0)
+                       GOTO(out_parent, rc);
+       }
+
+       mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
 
        /*
         *step 3: find the child object by fid & lock it.
@@ -1398,37 +1512,15 @@ static int mdt_getattr_name_lock(struct mdt_thread_info *info,
 
        if (unlikely(IS_ERR(child)))
                GOTO(out_parent, rc = PTR_ERR(child));
-       if (is_resent) {
-               /* Do not take lock for resent case. */
-               lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
-               LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
-                        lhc->mlh_reg_lh.cookie);
 
-               res_id = &lock->l_resource->lr_name;
-               if (!fid_res_name_eq(mdt_object_fid(child),
-                                    &lock->l_resource->lr_name)) {
-                       LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
-                                                &lock->l_resource->lr_name),
-                                "Lock res_id: "DLDLMRES", fid: "DFID"\n",
-                                PLDLMRES(lock->l_resource),
-                                PFID(mdt_object_fid(parent)));
-                       CWARN("Although resent, but still not get child lock"
-                             "parent:"DFID" child:"DFID"\n",
-                             PFID(mdt_object_fid(parent)),
-                             PFID(mdt_object_fid(child)));
-                       lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
-                       LDLM_LOCK_PUT(lock);
-                       GOTO(relock, 0);
-               }
-               LDLM_LOCK_PUT(lock);
-               rc = 0;
-       } else {
-               bool try_layout = false;
-
-relock:
+       rc = mdt_check_resent_lock(info, child, lhc);
+       if (rc < 0) {
+               GOTO(out_child, rc);
+       } else if (rc > 0) {
                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
                 mdt_lock_handle_init(lhc);
                mdt_lock_reg_init(lhc, LCK_PR);
+               try_layout = false;
 
                if (!mdt_object_exists(child)) {
                        LU_OBJECT_DEBUG(D_INODE, info->mti_env,
@@ -1485,6 +1577,10 @@ relock:
                                ma_need |= MA_LOV;
                        }
                } else {
+                       /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
+                        * client will enqueue the lock to the remote MDT */
+                       if (mdt_object_remote(child))
+                               child_bits &= ~MDS_INODELOCK_UPDATE;
                        rc = mdt_object_lock(info, child, lhc, child_bits,
                                                MDT_CROSS_LOCK);
                }
@@ -1506,7 +1602,6 @@ relock:
                 mdt_object_unlock(info, child, lhc, 1);
        } else if (lock) {
                /* Debugging code. */
-               res_id = &lock->l_resource->lr_name;
                LDLM_DEBUG(lock, "Returning lock to client");
                LASSERTF(fid_res_name_eq(mdt_object_fid(child),
                                         &lock->l_resource->lr_name),
@@ -1529,7 +1624,7 @@ out_parent:
 }
 
 /* normal handler: should release the child lock */
-int mdt_getattr_name(struct tgt_session_info *tsi)
+static int mdt_getattr_name(struct tgt_session_info *tsi)
 {
        struct mdt_thread_info  *info = tsi2mdt_info(tsi);
         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
@@ -1543,9 +1638,9 @@ int mdt_getattr_name(struct tgt_session_info *tsi)
         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
         LASSERT(repbody != NULL);
 
-        info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
-        repbody->eadatasize = 0;
-        repbody->aclsize = 0;
+       info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
+       repbody->mbo_eadatasize = 0;
+       repbody->mbo_aclsize = 0;
 
         rc = mdt_init_ucred(info, reqbody);
         if (unlikely(rc))
@@ -1570,7 +1665,7 @@ out_shrink:
 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
                          void *karg, void *uarg);
 
-int mdt_set_info(struct tgt_session_info *tsi)
+static int mdt_set_info(struct tgt_session_info *tsi)
 {
        struct ptlrpc_request   *req = tgt_ses_req(tsi);
        char                    *key;
@@ -1628,7 +1723,7 @@ int mdt_set_info(struct tgt_session_info *tsi)
        RETURN(rc);
 }
 
-int mdt_readpage(struct tgt_session_info *tsi)
+static int mdt_readpage(struct tgt_session_info *tsi)
 {
        struct mdt_thread_info  *info = mdt_th_info(tsi->tsi_env);
        struct mdt_object       *object = mdt_obj(tsi->tsi_corpus);
@@ -1652,17 +1747,17 @@ int mdt_readpage(struct tgt_session_info *tsi)
          * reqbody->size contains offset of where to start to read and
          * reqbody->nlink contains number bytes to read.
          */
-        rdpg->rp_hash = reqbody->size;
-        if (rdpg->rp_hash != reqbody->size) {
-                CERROR("Invalid hash: "LPX64" != "LPX64"\n",
-                       rdpg->rp_hash, reqbody->size);
-                RETURN(-EFAULT);
-        }
+       rdpg->rp_hash = reqbody->mbo_size;
+       if (rdpg->rp_hash != reqbody->mbo_size) {
+               CERROR("Invalid hash: "LPX64" != "LPX64"\n",
+                      rdpg->rp_hash, reqbody->mbo_size);
+               RETURN(-EFAULT);
+       }
 
-        rdpg->rp_attrs = reqbody->mode;
+       rdpg->rp_attrs = reqbody->mbo_mode;
        if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
                rdpg->rp_attrs |= LUDA_64BITHASH;
-       rdpg->rp_count  = min_t(unsigned int, reqbody->nlink,
+       rdpg->rp_count  = min_t(unsigned int, reqbody->mbo_nlink,
                                exp_max_brw_size(tsi->tsi_exp));
        rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
                          PAGE_CACHE_SHIFT;
@@ -1717,7 +1812,7 @@ static int mdt_reint_internal(struct mdt_thread_info *info,
         /* for replay (no_create) lmm is not needed, client has it already */
         if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
                 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
-                                     info->mti_rr.rr_eadatalen);
+                                    DEF_REP_MD_SIZE);
 
        /* llog cookies are always 0, the field is kept for compatibility */
         if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
@@ -1732,8 +1827,8 @@ static int mdt_reint_internal(struct mdt_thread_info *info,
         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
                 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
                 LASSERT(repbody);
-                repbody->eadatasize = 0;
-                repbody->aclsize = 0;
+               repbody->mbo_eadatasize = 0;
+               repbody->mbo_aclsize = 0;
         }
 
         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
@@ -1752,7 +1847,8 @@ static int mdt_reint_internal(struct mdt_thread_info *info,
                 GOTO(out_ucred, rc = err_serious(rc));
 
         if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
-                rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
+               DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
+               rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
                 GOTO(out_ucred, rc);
         }
         rc = mdt_reint_rec(info, lhc);
@@ -1793,7 +1889,7 @@ static long mdt_reint_opcode(struct ptlrpc_request *req,
        return opc;
 }
 
-int mdt_reint(struct tgt_session_info *tsi)
+static int mdt_reint(struct tgt_session_info *tsi)
 {
        long opc;
        int  rc;
@@ -1805,7 +1901,8 @@ int mdt_reint(struct tgt_session_info *tsi)
                [REINT_RENAME]   = &RQF_MDS_REINT_RENAME,
                [REINT_OPEN]     = &RQF_MDS_REINT_OPEN,
                [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
-               [REINT_RMENTRY]  = &RQF_MDS_REINT_UNLINK
+               [REINT_RMENTRY]  = &RQF_MDS_REINT_UNLINK,
+               [REINT_MIGRATE]  = &RQF_MDS_REINT_RENAME
        };
 
        ENTRY;
@@ -1841,22 +1938,23 @@ static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
 /* this should sync this object */
 static int mdt_object_sync(struct mdt_thread_info *info)
 {
-        struct md_object *next;
-        int rc;
-        ENTRY;
+       struct md_object *next;
+       int rc;
+       ENTRY;
 
-        if (!mdt_object_exists(info->mti_object)) {
-                CWARN("Non existing object  "DFID"!\n",
-                      PFID(mdt_object_fid(info->mti_object)));
-                RETURN(-ESTALE);
-        }
-        next = mdt_object_child(info->mti_object);
-        rc = mo_object_sync(info->mti_env, next);
+       if (!mdt_object_exists(info->mti_object)) {
+               CWARN("%s: non existing object "DFID": rc = %d\n",
+                     mdt_obd_name(info->mti_mdt),
+                     PFID(mdt_object_fid(info->mti_object)), -ESTALE);
+               RETURN(-ESTALE);
+       }
+       next = mdt_object_child(info->mti_object);
+       rc = mo_object_sync(info->mti_env, next);
 
-        RETURN(rc);
+       RETURN(rc);
 }
 
-int mdt_sync(struct tgt_session_info *tsi)
+static int mdt_sync(struct tgt_session_info *tsi)
 {
        struct ptlrpc_request   *req = tgt_ses_req(tsi);
        struct req_capsule      *pill = tsi->tsi_pill;
@@ -1868,7 +1966,7 @@ int mdt_sync(struct tgt_session_info *tsi)
        if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
                RETURN(err_serious(-ENOMEM));
 
-       if (fid_seq(&tsi->tsi_mdt_body->fid1) == 0) {
+       if (fid_seq(&tsi->tsi_mdt_body->mbo_fid1) == 0) {
                rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
        } else {
                struct mdt_thread_info *info = tsi2mdt_info(tsi);
@@ -1902,7 +2000,7 @@ int mdt_sync(struct tgt_session_info *tsi)
  * Handle quota control requests to consult current usage/limit, but also
  * to configure quota enforcement
  */
-int mdt_quotactl(struct tgt_session_info *tsi)
+static int mdt_quotactl(struct tgt_session_info *tsi)
 {
        struct obd_export       *exp  = tsi->tsi_exp;
        struct req_capsule      *pill = tsi->tsi_pill;
@@ -1910,6 +2008,7 @@ int mdt_quotactl(struct tgt_session_info *tsi)
        int                      id, rc;
        struct mdt_device       *mdt = mdt_exp2dev(exp);
        struct lu_device        *qmt = mdt->mdt_qmt_dev;
+       struct lu_nodemap       *nodemap = exp->exp_target_data.ted_nodemap;
        ENTRY;
 
        oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
@@ -1930,9 +2029,11 @@ int mdt_quotactl(struct tgt_session_info *tsi)
                /* deprecated, not used any more */
                RETURN(-EOPNOTSUPP);
                /* master quotactl */
-       case Q_GETINFO:
        case Q_SETINFO:
        case Q_SETQUOTA:
+               if (!nodemap_can_setquota(nodemap))
+                       RETURN(-EPERM);
+       case Q_GETINFO:
        case Q_GETQUOTA:
                if (qmt == NULL)
                        RETURN(-EOPNOTSUPP);
@@ -1971,6 +2072,13 @@ int mdt_quotactl(struct tgt_session_info *tsi)
                }
        }
 
+       if (oqctl->qc_type == USRQUOTA)
+               id = nodemap_map_id(nodemap, NODEMAP_UID,
+                                   NODEMAP_CLIENT_TO_FS, id);
+       else if (oqctl->qc_type == GRPQUOTA)
+               id = nodemap_map_id(nodemap, NODEMAP_UID,
+                                   NODEMAP_CLIENT_TO_FS, id);
+
        repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
        if (repoqc == NULL)
                RETURN(err_serious(-EFAULT));
@@ -2054,7 +2162,7 @@ static int mdt_llog_ctxt_unclone(const struct lu_env *env,
 /*
  * sec context handlers
  */
-int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
+static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
 {
        int rc;
 
@@ -2076,7 +2184,7 @@ int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
 /*
  * quota request handlers
  */
-int mdt_quota_dqacq(struct tgt_session_info *tsi)
+static int mdt_quota_dqacq(struct tgt_session_info *tsi)
 {
        struct mdt_device       *mdt = mdt_exp2dev(tsi->tsi_exp);
        struct lu_device        *qmt = mdt->mdt_qmt_dev;
@@ -2137,12 +2245,13 @@ struct mdt_object *mdt_object_find(const struct lu_env *env,
 static void mdt_device_commit_async(const struct lu_env *env,
                                     struct mdt_device *mdt)
 {
-        struct dt_device *dt = mdt->mdt_bottom;
-        int rc;
+       struct dt_device *dt = mdt->mdt_bottom;
+       int rc;
 
-        rc = dt->dd_ops->dt_commit_async(env, dt);
-        if (unlikely(rc != 0))
-                CWARN("async commit start failed with rc = %d", rc);
+       rc = dt->dd_ops->dt_commit_async(env, dt);
+       if (unlikely(rc != 0))
+               CWARN("%s: async commit start failed: rc = %d\n",
+                     mdt_obd_name(mdt), rc);
 }
 
 /**
@@ -2217,10 +2326,11 @@ int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
         if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
                 struct lu_env env;
 
-                rc = lu_env_init(&env, LCT_LOCAL);
-                if (unlikely(rc != 0))
-                        CWARN("lu_env initialization failed with rc = %d,"
-                              "cannot start asynchronous commit\n", rc);
+               rc = lu_env_init(&env, LCT_LOCAL);
+               if (unlikely(rc != 0))
+                       CWARN("%s: lu_env initialization failed, cannot "
+                             "start asynchronous commit: rc = %d\n",
+                             obd->obd_name, rc);
                 else
                         mdt_device_commit_async(&env, mdt);
                 lu_env_fini(&env);
@@ -2228,8 +2338,9 @@ int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
         RETURN(rc);
 }
 
-int mdt_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
-                       void *data, int flag)
+/* Used for cross-MDT lock */
+int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
+                           void *data, int flag)
 {
        struct lustre_handle lockh;
        int               rc;
@@ -2252,24 +2363,65 @@ int mdt_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
        RETURN(0);
 }
 
+int mdt_check_resent_lock(struct mdt_thread_info *info,
+                         struct mdt_object *mo,
+                         struct mdt_lock_handle *lhc)
+{
+       /* the lock might already be gotten in ldlm_handle_enqueue() */
+       if (unlikely(lustre_handle_is_used(&lhc->mlh_reg_lh))) {
+               struct ptlrpc_request *req = mdt_info_req(info);
+               struct ldlm_lock      *lock;
+
+               lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
+               LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
+               if (lock == NULL) {
+                       /* Lock is pinned by ldlm_handle_enqueue0() as it is
+                        * a resend case, however, it could be already destroyed
+                        * due to client eviction or a raced cancel RPC. */
+                       LDLM_DEBUG_NOLOCK("Invalid lock handle "LPX64"\n",
+                                         lhc->mlh_reg_lh.cookie);
+                       RETURN(-ESTALE);
+               }
+
+               if (!fid_res_name_eq(mdt_object_fid(mo),
+                                    &lock->l_resource->lr_name)) {
+                       CWARN("%s: Although resent, but still not "
+                             "get child lock:"DFID"\n",
+                             info->mti_exp->exp_obd->obd_name,
+                             PFID(mdt_object_fid(mo)));
+                       LDLM_LOCK_PUT(lock);
+                       RETURN(-EPROTO);
+               }
+               LDLM_LOCK_PUT(lock);
+               return 0;
+       }
+       return 1;
+}
+
 int mdt_remote_object_lock(struct mdt_thread_info *mti,
-                          struct mdt_object *o, struct lustre_handle *lh,
-                          ldlm_mode_t mode, __u64 ibits)
+                          struct mdt_object *o, const struct lu_fid *fid,
+                          struct lustre_handle *lh, ldlm_mode_t mode,
+                          __u64 ibits)
 {
        struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
        ldlm_policy_data_t *policy = &mti->mti_policy;
+       struct ldlm_res_id *res_id = &mti->mti_res_id;
        int rc = 0;
        ENTRY;
 
        LASSERT(mdt_object_remote(o));
 
-       LASSERT(ibits & MDS_INODELOCK_UPDATE);
+       LASSERT(ibits == MDS_INODELOCK_UPDATE);
+
+       fid_build_reg_res_name(fid, res_id);
 
        memset(einfo, 0, sizeof(*einfo));
        einfo->ei_type = LDLM_IBITS;
        einfo->ei_mode = mode;
-       einfo->ei_cb_bl = mdt_md_blocking_ast;
+       einfo->ei_cb_bl = mdt_remote_blocking_ast;
        einfo->ei_cb_cp = ldlm_completion_ast;
+       einfo->ei_enq_slave = 0;
+       einfo->ei_res_id = res_id;
 
        memset(policy, 0, sizeof(*policy));
        policy->l_inodebits.bits = ibits;
@@ -2279,9 +2431,10 @@ int mdt_remote_object_lock(struct mdt_thread_info *mti,
        RETURN(rc);
 }
 
-static int mdt_object_lock0(struct mdt_thread_info *info, struct mdt_object *o,
-                           struct mdt_lock_handle *lh, __u64 ibits,
-                           bool nonblock, int locality)
+static int mdt_object_local_lock(struct mdt_thread_info *info,
+                                struct mdt_object *o,
+                                struct mdt_lock_handle *lh, __u64 ibits,
+                                bool nonblock, int locality)
 {
         struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
         ldlm_policy_data_t *policy = &info->mti_policy;
@@ -2295,23 +2448,9 @@ static int mdt_object_lock0(struct mdt_thread_info *info, struct mdt_object *o,
         LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
         LASSERT(lh->mlh_type != MDT_NUL_LOCK);
 
-       if (mdt_object_remote(o)) {
-                if (locality == MDT_CROSS_LOCK) {
-                       ibits &= ~(MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM |
-                                  MDS_INODELOCK_LAYOUT);
-                        ibits |= MDS_INODELOCK_LOOKUP;
-                } else {
-                       LASSERTF(!(ibits &
-                                (MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM |
-                                 MDS_INODELOCK_LAYOUT)),
-                               "%s: wrong bit "LPX64" for remote obj "DFID"\n",
-                               mdt_obd_name(info->mti_mdt), ibits,
-                               PFID(mdt_object_fid(o)));
-                        LASSERT(ibits & MDS_INODELOCK_LOOKUP);
-                }
-                /* No PDO lock on remote object */
-                LASSERT(lh->mlh_type != MDT_PDO_LOCK);
-        }
+       /* Only enqueue LOOKUP lock for remote object */
+       if (mdt_object_remote(o))
+               LASSERT(ibits == MDS_INODELOCK_LOOKUP);
 
        if (lh->mlh_type == MDT_PDO_LOCK) {
                 /* check for exists after object is locked */
@@ -2346,11 +2485,12 @@ static int mdt_object_lock0(struct mdt_thread_info *info, struct mdt_object *o,
                          * want it slowed down due to possible cancels.
                          */
                         policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
-                        rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
-                                          policy, res_id, dlmflags,
-                                          &info->mti_exp->exp_handle.h_cookie);
-                        if (unlikely(rc))
-                                RETURN(rc);
+                       rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
+                                         policy, res_id, dlmflags,
+                                         info->mti_exp == NULL ? NULL :
+                                         &info->mti_exp->exp_handle.h_cookie);
+                       if (unlikely(rc != 0))
+                               GOTO(out_unlock, rc);
                 }
 
                 /*
@@ -2367,24 +2507,81 @@ static int mdt_object_lock0(struct mdt_thread_info *info, struct mdt_object *o,
          * 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_id, LDLM_FL_LOCAL_ONLY | dlmflags,
-                          &info->mti_exp->exp_handle.h_cookie);
-        if (rc)
-                mdt_object_unlock(info, o, lh, 1);
-        else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
-                 lh->mlh_pdo_hash != 0 &&
-                 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
-                OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
-        }
+       rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
+                         res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
+                         info->mti_exp == NULL ? NULL :
+                         &info->mti_exp->exp_handle.h_cookie);
+out_unlock:
+       if (rc != 0)
+               mdt_object_unlock(info, o, lh, 1);
+       else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
+                  lh->mlh_pdo_hash != 0 &&
+                  (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
+               OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
 
-        RETURN(rc);
+       RETURN(rc);
+}
+
+static int
+mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
+                        struct mdt_lock_handle *lh, __u64 ibits,
+                        bool nonblock, int locality)
+{
+       int rc;
+       ENTRY;
+
+       if (!mdt_object_remote(o))
+               return mdt_object_local_lock(info, o, lh, ibits, nonblock,
+                                            locality);
+
+       if (locality == MDT_LOCAL_LOCK) {
+               CERROR("%s: try to get local lock for remote object"
+                      DFID".\n", mdt_obd_name(info->mti_mdt),
+                      PFID(mdt_object_fid(o)));
+               RETURN(-EPROTO);
+       }
+
+       /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
+       ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
+                  MDS_INODELOCK_XATTR);
+       if (ibits & MDS_INODELOCK_UPDATE) {
+               /* Sigh, PDO needs to enqueue 2 locks right now, but
+                * enqueue RPC can only request 1 lock, to avoid extra
+                * RPC, so it will instead enqueue EX lock for remote
+                * object anyway XXX*/
+               if (lh->mlh_type == MDT_PDO_LOCK &&
+                   lh->mlh_pdo_hash != 0) {
+                       CDEBUG(D_INFO, "%s: "DFID" convert PDO lock to"
+                              "EX lock.\n", mdt_obd_name(info->mti_mdt),
+                              PFID(mdt_object_fid(o)));
+                       lh->mlh_pdo_hash = 0;
+                       lh->mlh_rreg_mode = LCK_EX;
+                       lh->mlh_type = MDT_REG_LOCK;
+               }
+               rc = mdt_remote_object_lock(info, o, mdt_object_fid(o),
+                                           &lh->mlh_rreg_lh,
+                                           lh->mlh_rreg_mode,
+                                           MDS_INODELOCK_UPDATE);
+               if (rc != ELDLM_OK)
+                       RETURN(rc);
+       }
+
+       /* Only enqueue LOOKUP lock for remote object */
+       if (ibits & MDS_INODELOCK_LOOKUP) {
+               rc = mdt_object_local_lock(info, o, lh,
+                                          MDS_INODELOCK_LOOKUP,
+                                          nonblock, locality);
+               if (rc != ELDLM_OK)
+                       RETURN(rc);
+       }
+
+       RETURN(0);
 }
 
 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
                    struct mdt_lock_handle *lh, __u64 ibits, int locality)
 {
-       return mdt_object_lock0(info, o, lh, ibits, false, locality);
+       return mdt_object_lock_internal(info, o, lh, ibits, false, locality);
 }
 
 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
@@ -2393,7 +2590,7 @@ int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
        struct mdt_lock_handle tmp = *lh;
        int rc;
 
-       rc = mdt_object_lock0(info, o, &tmp, ibits, true, locality);
+       rc = mdt_object_lock_internal(info, o, &tmp, ibits, true, locality);
        if (rc == 0)
                *lh = tmp;
 
@@ -2544,11 +2741,11 @@ static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
         if (body == NULL)
                 RETURN(-EFAULT);
 
-        if (!(body->valid & OBD_MD_FLID))
-                RETURN(0);
+       if (!(body->mbo_valid & OBD_MD_FLID))
+               RETURN(0);
 
-        if (!fid_is_sane(&body->fid1)) {
-                CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
+       if (!fid_is_sane(&body->mbo_fid1)) {
+               CERROR("Invalid fid: "DFID"\n", PFID(&body->mbo_fid1));
                 RETURN(-EINVAL);
         }
 
@@ -2559,17 +2756,17 @@ static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
          */
         if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
             req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
-                mdt_set_capainfo(info, 0, &body->fid1,
-                                 req_capsule_client_get(pill, &RMF_CAPA1));
-
-        obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
-        if (!IS_ERR(obj)) {
-                if ((flags & HABEO_CORPUS) &&
-                    !mdt_object_exists(obj)) {
-                        mdt_object_put(env, obj);
-                        /* for capability renew ENOENT will be handled in
-                         * mdt_renew_capa */
-                        if (body->valid & OBD_MD_FLOSSCAPA)
+               mdt_set_capainfo(info, 0, &body->mbo_fid1,
+                                req_capsule_client_get(pill, &RMF_CAPA1));
+
+       obj = mdt_object_find(env, info->mti_mdt, &body->mbo_fid1);
+       if (!IS_ERR(obj)) {
+               if ((flags & HABEO_CORPUS) &&
+                   !mdt_object_exists(obj)) {
+                       mdt_object_put(env, obj);
+                       /* for capability renew ENOENT will be handled in
+                        * mdt_renew_capa */
+                       if (body->mbo_valid & OBD_MD_FLOSSCAPA)
                                 rc = 0;
                         else
                                 rc = -ENOENT;
@@ -2598,7 +2795,7 @@ static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
                 /* Pack reply. */
                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
                         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
-                                             info->mti_body->eadatasize);
+                                            DEF_REP_MD_SIZE);
                 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
                        req_capsule_set_size(pill, &RMF_LOGCOOKIES,
                                             RCL_SERVER, 0);
@@ -2671,9 +2868,12 @@ void mdt_thread_info_init(struct ptlrpc_request *req,
         info->mti_opdata = 0;
        info->mti_big_lmm_used = 0;
 
-        /* To not check for split by default. */
         info->mti_spec.no_create = 0;
        info->mti_spec.sp_rm_entry = 0;
+       info->mti_spec.sp_permitted = 0;
+
+       info->mti_spec.u.sp_ea.eadata = NULL;
+       info->mti_spec.u.sp_ea.eadatalen = 0;
 }
 
 void mdt_thread_info_fini(struct mdt_thread_info *info)
@@ -2717,7 +2917,8 @@ struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
                if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
                    req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT) > 0) {
                        lc = req_capsule_client_get(pill, &RMF_CAPA1);
-                       mdt_set_capainfo(mti, 0, &tsi->tsi_mdt_body->fid1, lc);
+                       mdt_set_capainfo(mti, 0, &tsi->tsi_mdt_body->mbo_fid1,
+                                        lc);
                }
        }
        mti->mti_body = tsi->tsi_mdt_body;
@@ -2726,13 +2927,19 @@ struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
        return mti;
 }
 
-int mdt_tgt_connect(struct tgt_session_info *tsi)
+static int mdt_tgt_connect(struct tgt_session_info *tsi)
 {
        struct ptlrpc_request   *req = tgt_ses_req(tsi);
        int                      rc;
 
        ENTRY;
 
+       if (OBD_FAIL_CHECK(OBD_FAIL_TGT_DELAY_CONDITIONAL) &&
+           cfs_fail_val ==
+           tsi2mdt_info(tsi)->mti_mdt->mdt_seq_site.ss_node_id)
+               schedule_timeout_and_set_state(TASK_UNINTERRUPTIBLE,
+                       msecs_to_jiffies(3 * MSEC_PER_SEC));
+
        rc = tgt_connect(tsi);
        if (rc != 0)
                RETURN(rc);
@@ -2853,21 +3060,18 @@ static struct mdt_it_flavor {
        }
 };
 
-int mdt_intent_lock_replace(struct mdt_thread_info *info,
-                            struct ldlm_lock **lockp,
-                            struct ldlm_lock *new_lock,
-                            struct mdt_lock_handle *lh,
-                           __u64 flags)
+static int
+mdt_intent_lock_replace(struct mdt_thread_info *info,
+                       struct ldlm_lock **lockp,
+                       struct mdt_lock_handle *lh,
+                       __u64 flags)
 {
         struct ptlrpc_request  *req = mdt_info_req(info);
         struct ldlm_lock       *lock = *lockp;
+       struct ldlm_lock       *new_lock;
 
-        /*
-         * Get new lock only for cases when possible resent did not find any
-         * lock.
-         */
-        if (new_lock == NULL)
-                new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
+       /* If possible resent found a lock, @lh is set to its handle */
+       new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
 
         if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
                 lh->mlh_reg_lh.cookie = 0;
@@ -2899,6 +3103,8 @@ int mdt_intent_lock_replace(struct mdt_thread_info *info,
                  */
                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
                         MSG_RESENT);
+
+               LDLM_LOCK_RELEASE(new_lock);
                 lh->mlh_reg_lh.cookie = 0;
                 RETURN(ELDLM_LOCK_REPLACED);
         }
@@ -2940,52 +3146,30 @@ int mdt_intent_lock_replace(struct mdt_thread_info *info,
 
 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
                                    struct ldlm_lock *new_lock,
-                                   struct ldlm_lock **old_lock,
                                    struct mdt_lock_handle *lh,
-                                   enum mdt_it_code opcode)
+                                   __u64 flags)
 {
         struct ptlrpc_request  *req = mdt_info_req(info);
-        struct obd_export      *exp = req->rq_export;
-        struct lustre_handle    remote_hdl;
         struct ldlm_request    *dlmreq;
-        struct ldlm_lock       *lock;
 
         if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
                 return;
 
         dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
-        remote_hdl = dlmreq->lock_handle[0];
-       /* If the client does not require open lock, it does not need to
-        * search lock in exp_lock_hash, since the server thread will
-        * make sure the lock will be released, and the resend request
-        * can always re-enqueue the lock */
-       if ((opcode != MDT_IT_OPEN) || (opcode == MDT_IT_OPEN &&
-           info->mti_spec.sp_cr_flags & MDS_OPEN_LOCK)) {
-               /* In the function below, .hs_keycmp resolves to
-                * ldlm_export_lock_keycmp() */
-               /* coverity[overrun-buffer-val] */
-               lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
-               if (lock) {
-                       lock_res_and_lock(lock);
-                       if (lock != new_lock) {
-                               lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
-                               lh->mlh_reg_mode = lock->l_granted_mode;
-
-                               LDLM_DEBUG(lock, "Restoring lock cookie");
-                               DEBUG_REQ(D_DLMTRACE, req,
-                                         "restoring lock cookie "LPX64,
-                                         lh->mlh_reg_lh.cookie);
-                               if (old_lock)
-                                       *old_lock = LDLM_LOCK_GET(lock);
-                               cfs_hash_put(exp->exp_lock_hash,
-                                            &lock->l_exp_hash);
-                               unlock_res_and_lock(lock);
-                               return;
-                       }
-                       cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
-                       unlock_res_and_lock(lock);
-               }
+
+       /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
+        * lock was found by ldlm_handle_enqueue(); if so @lh must be
+        * initialized. */
+       if (flags & LDLM_FL_RESENT) {
+               lh->mlh_reg_lh.cookie = new_lock->l_handle.h_cookie;
+               lh->mlh_reg_mode = new_lock->l_granted_mode;
+
+               LDLM_DEBUG(new_lock, "Restoring lock cookie");
+               DEBUG_REQ(D_DLMTRACE, req, "restoring lock cookie "LPX64,
+                         lh->mlh_reg_lh.cookie);
+               return;
        }
+
         /*
          * If the xid matches, then we know this is a resent request, and allow
          * it. (It's probably an OPEN, for which we don't send a lock.
@@ -3000,8 +3184,8 @@ static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
          */
         lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
 
-        DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
-                  remote_hdl.cookie);
+       DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
+                 dlmreq->lock_handle[0].cookie);
 }
 
 static int mdt_intent_getxattr(enum mdt_it_code opcode,
@@ -3018,7 +3202,7 @@ static int mdt_intent_getxattr(enum mdt_it_code opcode,
         * (for the resend case) or a new lock. Below we will use it to
         * replace the original lock.
         */
-       mdt_intent_fixup_resent(info, *lockp, NULL, lhc, opcode);
+       mdt_intent_fixup_resent(info, *lockp, lhc, flags);
        if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
                mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
                rc = mdt_object_lock(info, info->mti_object, lhc,
@@ -3030,7 +3214,7 @@ static int mdt_intent_getxattr(enum mdt_it_code opcode,
 
        grc = mdt_getxattr(info);
 
-       rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
+       rc = mdt_intent_lock_replace(info, lockp, lhc, flags);
 
        if (mdt_info_req(info)->rq_repmsg != NULL)
                ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
@@ -3048,10 +3232,8 @@ static int mdt_intent_getattr(enum mdt_it_code opcode,
                              __u64 flags)
 {
         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
-        struct ldlm_lock       *new_lock = NULL;
         __u64                   child_bits;
         struct ldlm_reply      *ldlm_rep;
-        struct ptlrpc_request  *req;
         struct mdt_body        *reqbody;
         struct mdt_body        *repbody;
         int                     rc, rc2;
@@ -3063,9 +3245,9 @@ static int mdt_intent_getattr(enum mdt_it_code opcode,
         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
         LASSERT(repbody);
 
-        info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
-        repbody->eadatasize = 0;
-        repbody->aclsize = 0;
+       info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
+       repbody->mbo_eadatasize = 0;
+       repbody->mbo_aclsize = 0;
 
         switch (opcode) {
         case MDT_IT_LOOKUP:
@@ -3084,12 +3266,11 @@ static int mdt_intent_getattr(enum mdt_it_code opcode,
         if (rc)
                 GOTO(out_shrink, rc);
 
-        req = info->mti_pill->rc_req;
         ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
         mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
 
        /* Get lock from request for possible resent case. */
-       mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc, opcode);
+       mdt_intent_fixup_resent(info, *lockp, lhc, flags);
 
        rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
        ldlm_rep->lock_policy_res2 = clear_serious(rc);
@@ -3102,7 +3283,7 @@ static int mdt_intent_getattr(enum mdt_it_code opcode,
                 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
         }
 
-        rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
+       rc = mdt_intent_lock_replace(info, lockp, lhc, flags);
         EXIT;
 out_ucred:
         mdt_exit_ucred(info);
@@ -3119,11 +3300,11 @@ static int mdt_intent_layout(enum mdt_it_code opcode,
                             struct ldlm_lock **lockp,
                             __u64 flags)
 {
+       struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_LAYOUT];
        struct layout_intent *layout;
        struct lu_fid *fid;
        struct mdt_object *obj = NULL;
-       struct md_object *child = NULL;
-       int rc;
+       int rc = 0;
        ENTRY;
 
        if (opcode != MDT_IT_LAYOUT) {
@@ -3135,16 +3316,20 @@ static int mdt_intent_layout(enum mdt_it_code opcode,
        fid = &info->mti_tmp_fid2;
        fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
 
+       /* Get lock from request for possible resent case. */
+       mdt_intent_fixup_resent(info, *lockp, lhc, flags);
+
        obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
        if (IS_ERR(obj))
                RETURN(PTR_ERR(obj));
 
        if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
-               child = mdt_object_child(obj);
-
                /* get the length of lsm */
-               rc = mo_xattr_get(info->mti_env, child, &LU_BUF_NULL,
-                                 XATTR_NAME_LOV);
+               rc = mdt_attr_get_eabuf_size(info, obj);
+               if (rc < 0) {
+                       mdt_object_put(info->mti_env, obj);
+                       RETURN(rc);
+               }
 
                if (rc > info->mti_mdt->mdt_max_mdsize)
                        info->mti_mdt->mdt_max_mdsize = rc;
@@ -3153,17 +3338,19 @@ static int mdt_intent_layout(enum mdt_it_code opcode,
        mdt_object_put(info->mti_env, obj);
 
        (*lockp)->l_lvb_type = LVB_T_LAYOUT;
-       req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
-                       ldlm_lvbo_size(*lockp));
+       req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER, rc);
        rc = req_capsule_server_pack(info->mti_pill);
        if (rc != 0)
                RETURN(-EINVAL);
 
+       if (lustre_handle_is_used(&lhc->mlh_reg_lh))
+               rc = mdt_intent_lock_replace(info, lockp, lhc, flags);
+
        layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
        LASSERT(layout != NULL);
        if (layout->li_opc == LAYOUT_INTENT_ACCESS)
-               /* return to normal ldlm handling */
-               RETURN(0);
+               /* return to normal/resent ldlm handling */
+               RETURN(rc);
 
        CERROR("%s: Unsupported layout intent (%d)\n",
                mdt_obd_name(info->mti_mdt), layout->li_opc);
@@ -3198,7 +3385,7 @@ static int mdt_intent_reint(enum mdt_it_code opcode,
         }
 
        /* Get lock from request for possible resent case. */
-       mdt_intent_fixup_resent(info, *lockp, NULL, lhc, opcode);
+       mdt_intent_fixup_resent(info, *lockp, lhc, flags);
 
         rc = mdt_reint_internal(info, lhc, opc);
 
@@ -3214,10 +3401,10 @@ static int mdt_intent_reint(enum mdt_it_code opcode,
 
        /* the open lock or the lock for cross-ref object should be
         * returned to the client */
-       if (rc == -EREMOTE || mdt_get_disposition(rep, DISP_OPEN_LOCK)) {
-               LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
+       if (lustre_handle_is_used(&lhc->mlh_reg_lh) &&
+           (rc == 0 || rc == -MDT_EREMOTE_OPEN)) {
                rep->lock_policy_res2 = 0;
-               rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
+               rc = mdt_intent_lock_replace(info, lockp, lhc, flags);
                RETURN(rc);
        }
 
@@ -3227,6 +3414,7 @@ static int mdt_intent_reint(enum mdt_it_code opcode,
             mdt_get_disposition(rep, DISP_LOOKUP_NEG))
                 rep->lock_policy_res2 = 0;
 
+       lhc->mlh_reg_lh.cookie = 0ull;
         if (rc == -ENOTCONN || rc == -ENODEV ||
             rc == -EOVERFLOW) { /**< if VBR failure then return error */
                 /*
@@ -3235,28 +3423,13 @@ static int mdt_intent_reint(enum mdt_it_code opcode,
                  * will detect this, then disconnect, reconnect the import
                  * immediately, instead of impacting the following the rpc.
                  */
-                lhc->mlh_reg_lh.cookie = 0ull;
                 RETURN(rc);
-        } else {
-                /*
-                 * For other cases, the error will be returned by intent.
-                 * and client will retrieve the result from intent.
-                 */
-                 /*
-                  * FIXME: when open lock is finished, that should be
-                  * checked here.
-                  */
-                if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
-                        LASSERTF(rc == 0, "Error occurred but lock handle "
-                                 "is still in use, rc = %d\n", rc);
-                        rep->lock_policy_res2 = 0;
-                        rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
-                        RETURN(rc);
-                } else {
-                        lhc->mlh_reg_lh.cookie = 0ull;
-                        RETURN(ELDLM_LOCK_ABORTED);
-                }
         }
+       /*
+        * For other cases, the error will be returned by intent, and client
+        * will retrieve the result from intent.
+        */
+       RETURN(ELDLM_LOCK_ABORTED);
 }
 
 static int mdt_intent_code(long itcode)
@@ -3306,20 +3479,19 @@ static int mdt_intent_code(long itcode)
         return rc;
 }
 
-static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
-                         struct ldlm_lock **lockp, __u64 flags)
-{
-        struct req_capsule   *pill;
-        struct mdt_it_flavor *flv;
-        int opc;
-        int rc;
-        ENTRY;
-
-        opc = mdt_intent_code(itopc);
-        if (opc < 0)
-                RETURN(-EINVAL);
-
-        pill = info->mti_pill;
+static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
+                         struct ldlm_lock **lockp, __u64 flags)
+{
+       struct req_capsule      *pill = info->mti_pill;
+       struct ptlrpc_request   *req = mdt_info_req(info);
+       struct mdt_it_flavor    *flv;
+       int opc;
+       int rc;
+       ENTRY;
+
+       opc = mdt_intent_code(itopc);
+       if (opc < 0)
+               RETURN(-EINVAL);
 
        if (opc == MDT_IT_QUOTA) {
                struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
@@ -3335,33 +3507,33 @@ static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
                RETURN(rc);
        }
 
-       flv  = &mdt_it_flavor[opc];
-        if (flv->it_fmt != NULL)
-                req_capsule_extend(pill, flv->it_fmt);
+       flv = &mdt_it_flavor[opc];
+       if (flv->it_fmt != NULL)
+               req_capsule_extend(pill, flv->it_fmt);
 
-        rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
-        if (rc == 0) {
-                struct ptlrpc_request *req = mdt_info_req(info);
-               if (flv->it_flags & MUTABOR &&
-                   exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
-                       RETURN(-EROFS);
-        }
-        if (rc == 0 && flv->it_act != NULL) {
+       rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
+       if (rc < 0)
+               RETURN(rc);
+
+       if (flv->it_flags & MUTABOR &&
+           exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
+               RETURN(-EROFS);
+
+       if (flv->it_act != NULL) {
                struct ldlm_reply *rep;
 
                /* execute policy */
                rc = flv->it_act(opc, info, lockp, flags);
 
                /* Check whether the reply has been packed successfully. */
-               if (mdt_info_req(info)->rq_repmsg != NULL) {
+               if (req->rq_repmsg != NULL) {
                        rep = req_capsule_server_get(info->mti_pill,
                                                     &RMF_DLM_REP);
                        rep->lock_policy_res2 =
                                ptlrpc_status_hton(rep->lock_policy_res2);
                }
-       } else {
-               rc = -EPROTO;
        }
+
        RETURN(rc);
 }
 
@@ -3443,10 +3615,8 @@ static void mdt_seq_fini_cli(struct mdt_device *mdt)
        if (ss == NULL)
                return;
 
-       if (ss->ss_server_seq == NULL)
+       if (ss->ss_server_seq != NULL)
                seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
-
-       return;
 }
 
 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
@@ -3505,7 +3675,7 @@ static int mdt_register_seq_exp(struct mdt_device *mdt)
        if (lwp_name == NULL)
                GOTO(out_free, rc = -ENOMEM);
 
-       rc = tgt_name2lwpname(mdt_obd_name(mdt), lwp_name);
+       rc = tgt_name2lwp_name(mdt_obd_name(mdt), lwp_name, MAX_OBD_NAME, 0);
        if (rc != 0)
                GOTO(out_free, rc);
 
@@ -3663,7 +3833,6 @@ static int mdt_fld_init(const struct lu_env *env,
 static void mdt_stack_pre_fini(const struct lu_env *env,
                           struct mdt_device *m, struct lu_device *top)
 {
-       struct obd_device       *obd;
        struct lustre_cfg_bufs  *bufs;
        struct lustre_cfg       *lcfg;
        struct mdt_thread_info  *info;
@@ -3678,7 +3847,6 @@ static void mdt_stack_pre_fini(const struct lu_env *env,
 
        LASSERT(m->mdt_child_exp);
        LASSERT(m->mdt_child_exp->exp_obd);
-       obd = m->mdt_child_exp->exp_obd;
 
        /* process cleanup, pass mdt obd name to get obd umount flags */
        /* XXX: this is needed because all layers are referenced by
@@ -3688,10 +3856,9 @@ static void mdt_stack_pre_fini(const struct lu_env *env,
        lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
        lustre_cfg_bufs_set_string(bufs, 1, NULL);
        lcfg = lustre_cfg_new(LCFG_PRE_CLEANUP, bufs);
-       if (!lcfg) {
-               CERROR("%s:Cannot alloc lcfg!\n", mdt_obd_name(m));
-               return;
-       }
+       if (lcfg == NULL)
+               RETURN_EXIT;
+
        top->ld_ops->ldo_process_config(env, top, lcfg);
        lustre_cfg_free(lcfg);
        EXIT;
@@ -3724,10 +3891,9 @@ static void mdt_stack_fini(const struct lu_env *env,
                strcat(flags, "A");
        lustre_cfg_bufs_set_string(bufs, 1, flags);
        lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
-       if (!lcfg) {
-               CERROR("Cannot alloc lcfg!\n");
-               return;
-       }
+       if (lcfg == NULL)
+               RETURN_EXIT;
+
        LASSERT(top);
        top->ld_ops->ldo_process_config(env, top, lcfg);
        lustre_cfg_free(lcfg);
@@ -3851,7 +4017,7 @@ static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
        lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
 
        lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
-       if (!lcfg)
+       if (lcfg == NULL)
                GOTO(free_bufs, rc = -ENOMEM);
 
        rc = class_attach(lcfg);
@@ -3862,7 +4028,7 @@ static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
        if (!obd) {
                CERROR("Can not find obd %s (%s in config)\n",
                       MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
-               GOTO(class_detach, rc = -EINVAL);
+               GOTO(lcfg_cleanup, rc = -EINVAL);
        }
 
        lustre_cfg_free(lcfg);
@@ -3873,6 +4039,8 @@ static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
        lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
 
        lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
+       if (lcfg == NULL)
+               GOTO(class_detach, rc = -ENOMEM);
 
        rc = class_setup(obd, lcfg);
        if (rc)
@@ -3890,7 +4058,6 @@ static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
        site->ls_top_dev = &mdt->mdt_lu_dev;
        mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
 
-
        /* now connect to bottom OSD */
        snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
        rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
@@ -3899,7 +4066,6 @@ static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
        mdt->mdt_bottom =
                lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
 
-
        rc = lu_env_refill((struct lu_env *)env);
        if (rc != 0)
                CERROR("Failure to refill session: '%d'\n", rc);
@@ -3981,7 +4147,7 @@ static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
        lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
 
        lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
-       if (!lcfg)
+       if (lcfg == NULL)
                GOTO(cleanup_mem, rc = -ENOMEM);
 
        rc = class_attach(lcfg);
@@ -3992,7 +4158,7 @@ static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
        if (!obd) {
                CERROR("Can not find obd %s (%s in config)\n", qmtname,
                       lustre_cfg_string(cfg, 0));
-               GOTO(class_detach, rc = -EINVAL);
+               GOTO(lcfg_cleanup, rc = -EINVAL);
        }
 
        lustre_cfg_free(lcfg);
@@ -4006,6 +4172,8 @@ static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
                                   mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
 
        lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
+       if (lcfg == NULL)
+               GOTO(class_detach, rc = -ENOMEM);
 
        rc = class_setup(obd, lcfg);
        if (rc)
@@ -4073,7 +4241,7 @@ static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
  * for now. This will be removed along with converting rest of MDT code
  * to use tgt_session_info */
-int mdt_tgt_getxattr(struct tgt_session_info *tsi)
+static int mdt_tgt_getxattr(struct tgt_session_info *tsi)
 {
        struct mdt_thread_info  *info = tsi2mdt_info(tsi);
        int                      rc;
@@ -4107,7 +4275,6 @@ TGT_MDT_HDL(HABEO_CORPUS,         MDS_DONE_WRITING,
                                                        mdt_done_writing),
 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO,        MDS_READPAGE,   mdt_readpage),
 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO,        MDS_SYNC,       mdt_sync),
-TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO,        MDS_IS_SUBDIR,  mdt_is_subdir),
 TGT_MDT_HDL(0,                         MDS_QUOTACTL,   mdt_quotactl),
 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_PROGRESS,
                                                        mdt_hsm_progress),
@@ -4122,8 +4289,9 @@ TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_STATE_SET,
 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_ACTION,        mdt_hsm_action),
 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_REQUEST,
                                                        mdt_hsm_request),
-TGT_MDT_HDL(HABEO_CORPUS|HABEO_REFERO | MUTABOR, MDS_SWAP_LAYOUTS,
-                                                       mdt_swap_layouts)
+TGT_MDT_HDL(HABEO_CLAVIS | HABEO_CORPUS | HABEO_REFERO | MUTABOR,
+           MDS_SWAP_LAYOUTS,
+           mdt_swap_layouts),
 };
 
 static struct tgt_handler mdt_sec_ctx_ops[] = {
@@ -4158,8 +4326,8 @@ static struct tgt_opc_slice mdt_common_slice[] = {
                .tos_hs         = mdt_sec_ctx_ops
        },
        {
-               .tos_opc_start  = UPDATE_OBJ,
-               .tos_opc_end    = UPDATE_LAST_OPC,
+               .tos_opc_start  = OUT_UPDATE_FIRST_OPC,
+               .tos_opc_end    = OUT_UPDATE_LAST_OPC,
                .tos_hs         = tgt_out_handlers
        },
        {
@@ -4182,6 +4350,11 @@ static struct tgt_opc_slice mdt_common_slice[] = {
                .tos_opc_end    = LLOG_LAST_OPC,
                .tos_hs         = tgt_llog_handlers
        },
+       {
+               .tos_opc_start  = LFSCK_FIRST_OPC,
+               .tos_opc_end    = LFSCK_LAST_OPC,
+               .tos_hs         = tgt_lfsck_handlers
+       },
 
        {
                .tos_hs         = NULL
@@ -4190,15 +4363,18 @@ static struct tgt_opc_slice mdt_common_slice[] = {
 
 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
 {
-       struct md_device  *next = m->mdt_child;
-       struct lu_device  *d    = &m->mdt_lu_dev;
-        struct obd_device *obd = mdt2obd_dev(m);
-        ENTRY;
-
-        target_recovery_fini(obd);
+       struct md_device        *next = m->mdt_child;
+       struct lu_device        *d    = &m->mdt_lu_dev;
+       struct obd_device       *obd  = mdt2obd_dev(m);
+       struct lfsck_stop        stop;
+       ENTRY;
 
-        ping_evictor_stop();
+       stop.ls_status = LS_PAUSED;
+       stop.ls_flags = 0;
+       next->md_ops->mdo_iocontrol(env, next, OBD_IOC_STOP_LFSCK, 0, &stop);
 
+       target_recovery_fini(obd);
+       ping_evictor_stop();
        mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
 
        if (m->mdt_opts.mo_coordinator)
@@ -4226,14 +4402,7 @@ static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
 
        mdt_quota_fini(env, m);
 
-        cfs_free_nidlist(&m->mdt_nosquash_nids);
-        if (m->mdt_nosquash_str) {
-                OBD_FREE(m->mdt_nosquash_str, m->mdt_nosquash_strlen);
-                m->mdt_nosquash_str = NULL;
-                m->mdt_nosquash_strlen = 0;
-        }
-
-       next->md_ops->mdo_iocontrol(env, next, OBD_IOC_PAUSE_LFSCK, 0, NULL);
+       cfs_free_nidlist(&m->mdt_squash.rsi_nosquash_nids);
 
         mdt_seq_fini(env, m);
         mdt_fld_fini(env, m);
@@ -4249,12 +4418,12 @@ static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
 
        LASSERT(atomic_read(&d->ld_ref) == 0);
 
-       server_put_mount(mdt_obd_name(m));
+       server_put_mount(mdt_obd_name(m), true);
 
        EXIT;
 }
 
-int mdt_postrecov(const struct lu_env *, struct mdt_device *);
+static int mdt_postrecov(const struct lu_env *, struct mdt_device *);
 
 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
@@ -4319,12 +4488,10 @@ static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
         m->mdt_capa_timeout = CAPA_TIMEOUT;
         m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
         m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
-        m->mdt_squash_uid = 0;
-        m->mdt_squash_gid = 0;
-        CFS_INIT_LIST_HEAD(&m->mdt_nosquash_nids);
-        m->mdt_nosquash_str = NULL;
-        m->mdt_nosquash_strlen = 0;
-       init_rwsem(&m->mdt_squash_sem);
+       m->mdt_squash.rsi_uid = 0;
+       m->mdt_squash.rsi_gid = 0;
+       INIT_LIST_HEAD(&m->mdt_squash.rsi_nosquash_nids);
+       init_rwsem(&m->mdt_squash.rsi_sem);
        spin_lock_init(&m->mdt_osfs_lock);
        m->mdt_osfs_age = cfs_time_shift_64(-1000);
        m->mdt_enable_remote_dir = 0;
@@ -4505,7 +4672,7 @@ err_fini_stack:
        mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
 err_lmi:
        if (lmi)
-               server_put_mount(dev);
+               server_put_mount(dev, true);
        return(rc);
 }
 
@@ -4533,7 +4700,6 @@ static int mdt_process_config(const struct lu_env *env,
 
        switch (cfg->lcfg_command) {
        case LCFG_PARAM: {
-               struct lprocfs_static_vars  lvars;
                struct obd_device          *obd = d->ld_obd;
 
                /* For interoperability */
@@ -4568,8 +4734,7 @@ static int mdt_process_config(const struct lu_env *env,
                        }
                }
 
-               lprocfs_mdt_init_vars(&lvars);
-               rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars,
+               rc = class_process_proc_param(PARAM_MDT, obd->obd_vars,
                                              cfg, obd);
                if (rc > 0 || rc == -ENOSYS) {
                        /* is it an HSM var ? */
@@ -4596,24 +4761,24 @@ static int mdt_process_config(const struct lu_env *env,
 }
 
 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
-                                          const struct lu_object_header *hdr,
-                                          struct lu_device *d)
+                                         const struct lu_object_header *hdr,
+                                         struct lu_device *d)
 {
-        struct mdt_object *mo;
+       struct mdt_object *mo;
 
-        ENTRY;
+       ENTRY;
 
-       OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, __GFP_IO);
-        if (mo != NULL) {
-                struct lu_object *o;
-                struct lu_object_header *h;
+       OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, GFP_NOFS);
+       if (mo != NULL) {
+               struct lu_object *o;
+               struct lu_object_header *h;
 
                o = &mo->mot_obj;
-                h = &mo->mot_header;
-                lu_object_header_init(h);
-                lu_object_init(o, h, d);
-                lu_object_add_top(h, o);
-                o->lo_ops = &mdt_obj_ops;
+               h = &mo->mot_header;
+               lu_object_header_init(h);
+               lu_object_init(o, h, d);
+               lu_object_add_top(h, o);
+               o->lo_ops = &mdt_obj_ops;
                mutex_init(&mo->mot_ioepoch_mutex);
                mutex_init(&mo->mot_lov_mutex);
                init_rwsem(&mo->mot_open_sem);
@@ -4681,7 +4846,6 @@ static int mdt_prepare(const struct lu_env *env,
        struct mdt_device *mdt = mdt_dev(cdev);
        struct lu_device *next = &mdt->mdt_child->md_lu_dev;
        struct obd_device *obd = cdev->ld_obd;
-       struct lfsck_start_param lsp;
        int rc;
 
        ENTRY;
@@ -4700,16 +4864,10 @@ static int mdt_prepare(const struct lu_env *env,
        if (rc)
                RETURN(rc);
 
-       lsp.lsp_start = NULL;
-       lsp.lsp_namespace = mdt->mdt_namespace;
-       rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
-                                                  OBD_IOC_START_LFSCK,
-                                                  0, &lsp);
-       if (rc != 0) {
-               CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
-                     mdt_obd_name(mdt), rc);
-               rc = 0;
-       }
+       rc = lfsck_register_namespace(env, mdt->mdt_bottom, mdt->mdt_namespace);
+       /* The LFSCK instance is registered just now, so it must be there when
+        * register the namespace to such instance. */
+       LASSERTF(rc == 0, "register namespace failed: rc = %d\n", rc);
 
        if (mdt->mdt_seq_site.ss_node_id == 0) {
                rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
@@ -4792,12 +4950,14 @@ static int mdt_connect_internal(struct obd_export *exp,
        data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
        data->ocd_ibits_known &= MDS_INODELOCK_FULL;
 
-       /* If no known bits (which should not happen, probably,
-          as everybody should support LOOKUP and UPDATE bits at least)
-          revert to compat mode with plain locks. */
-       if (!data->ocd_ibits_known &&
-           data->ocd_connect_flags & OBD_CONNECT_IBITS)
-               data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
+       if (!(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
+           !(data->ocd_connect_flags & OBD_CONNECT_IBITS)) {
+               CWARN("%s: client %s does not support ibits lock, either "
+                     "very old or an invalid client: flags "LPX64"\n",
+                     mdt_obd_name(mdt), exp->exp_client_uuid.uuid,
+                     data->ocd_connect_flags);
+               return -EBADE;
+       }
 
        if (!mdt->mdt_opts.mo_acl)
                data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
@@ -4869,87 +5029,6 @@ static int mdt_connect_internal(struct obd_export *exp,
        return 0;
 }
 
-/* mds_connect copy */
-static int mdt_obd_connect(const struct lu_env *env,
-                           struct obd_export **exp, struct obd_device *obd,
-                           struct obd_uuid *cluuid,
-                           struct obd_connect_data *data,
-                           void *localdata)
-{
-        struct obd_export      *lexp;
-        struct lustre_handle    conn = { 0 };
-        struct mdt_device      *mdt;
-        int                     rc;
-        ENTRY;
-
-        LASSERT(env != NULL);
-        if (!exp || !obd || !cluuid)
-                RETURN(-EINVAL);
-
-       mdt = mdt_dev(obd->obd_lu_dev);
-
-       /*
-        * first, check whether the stack is ready to handle requests
-        * XXX: probably not very appropriate method is used now
-        *      at some point we should find a better one
-        */
-       if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) && data != NULL &&
-           !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
-               rc = obd_get_info(env, mdt->mdt_child_exp,
-                                 sizeof(KEY_OSP_CONNECTED),
-                                 KEY_OSP_CONNECTED, NULL, NULL, NULL);
-               if (rc)
-                       RETURN(-EAGAIN);
-               set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
-       }
-
-        rc = class_connect(&conn, obd, cluuid);
-        if (rc)
-                RETURN(rc);
-
-        lexp = class_conn2export(&conn);
-        LASSERT(lexp != NULL);
-
-        rc = mdt_connect_internal(lexp, mdt, data);
-        if (rc == 0) {
-                struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
-
-                LASSERT(lcd);
-               memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
-               rc = tgt_client_new(env, lexp);
-                if (rc == 0)
-                        mdt_export_stats_init(obd, lexp, localdata);
-        }
-
-        if (rc != 0) {
-                class_disconnect(lexp);
-                *exp = NULL;
-        } else {
-                *exp = lexp;
-        }
-
-        RETURN(rc);
-}
-
-static int mdt_obd_reconnect(const struct lu_env *env,
-                             struct obd_export *exp, struct obd_device *obd,
-                             struct obd_uuid *cluuid,
-                             struct obd_connect_data *data,
-                             void *localdata)
-{
-        int                     rc;
-        ENTRY;
-
-        if (exp == NULL || obd == NULL || cluuid == NULL)
-                RETURN(-EINVAL);
-
-        rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
-        if (rc == 0)
-                mdt_export_stats_init(obd, exp, localdata);
-
-        RETURN(rc);
-}
-
 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
                                   struct mdt_thread_info *info,
                                   struct mdt_file_data *mfd)
@@ -4977,25 +5056,26 @@ static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
 
 static int mdt_export_cleanup(struct obd_export *exp)
 {
-        struct mdt_export_data *med = &exp->exp_mdt_data;
-        struct obd_device      *obd = exp->exp_obd;
-        struct mdt_device      *mdt;
-        struct mdt_thread_info *info;
-        struct lu_env           env;
-        CFS_LIST_HEAD(closing_list);
-        struct mdt_file_data *mfd, *n;
-        int rc = 0;
-        ENTRY;
+       struct list_head         closing_list;
+       struct mdt_export_data  *med = &exp->exp_mdt_data;
+       struct obd_device       *obd = exp->exp_obd;
+       struct mdt_device       *mdt;
+       struct mdt_thread_info  *info;
+       struct lu_env            env;
+       struct mdt_file_data    *mfd, *n;
+       int rc = 0;
+       ENTRY;
 
+       INIT_LIST_HEAD(&closing_list);
        spin_lock(&med->med_open_lock);
-       while (!cfs_list_empty(&med->med_open_head)) {
-               cfs_list_t *tmp = med->med_open_head.next;
-               mfd = cfs_list_entry(tmp, struct mdt_file_data, mfd_list);
+       while (!list_empty(&med->med_open_head)) {
+               struct list_head *tmp = med->med_open_head.next;
+               mfd = list_entry(tmp, struct mdt_file_data, mfd_list);
 
                /* Remove mfd handle so it can't be found again.
                 * We are consuming the mfd_list reference here. */
                class_handle_unhash(&mfd->mfd_handle);
-               cfs_list_move_tail(&mfd->mfd_list, &closing_list);
+               list_move_tail(&mfd->mfd_list, &closing_list);
        }
        spin_unlock(&med->med_open_lock);
         mdt = mdt_dev(obd->obd_lu_dev);
@@ -5012,12 +5092,13 @@ static int mdt_export_cleanup(struct obd_export *exp)
         info->mti_mdt = mdt;
         info->mti_exp = exp;
 
-        if (!cfs_list_empty(&closing_list)) {
-                struct md_attr *ma = &info->mti_attr;
+       if (!list_empty(&closing_list)) {
+               struct md_attr *ma = &info->mti_attr;
 
-                /* Close any open files (which may also cause orphan unlinking). */
-                cfs_list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
-                        cfs_list_del_init(&mfd->mfd_list);
+               /* Close any open files (which may also cause orphan
+                * unlinking). */
+               list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
+                       list_del_init(&mfd->mfd_list);
                        ma->ma_need = ma->ma_valid = 0;
 
                        /* This file is being closed due to an eviction, it
@@ -5063,23 +5144,120 @@ static int mdt_obd_disconnect(struct obd_export *exp)
         LASSERT(exp);
         class_export_get(exp);
 
-        rc = server_disconnect_export(exp);
-        if (rc != 0)
-                CDEBUG(D_IOCTL, "server disconnect error: %d\n", rc);
+       nodemap_del_member(exp);
+       rc = server_disconnect_export(exp);
+       if (rc != 0)
+               CDEBUG(D_IOCTL, "server disconnect error: rc = %d\n", rc);
 
-        rc = mdt_export_cleanup(exp);
-        class_export_put(exp);
-        RETURN(rc);
+       rc = mdt_export_cleanup(exp);
+       class_export_put(exp);
+       RETURN(rc);
+}
+
+/* mds_connect copy */
+static int mdt_obd_connect(const struct lu_env *env,
+                          struct obd_export **exp, struct obd_device *obd,
+                          struct obd_uuid *cluuid,
+                          struct obd_connect_data *data,
+                          void *localdata)
+{
+       struct obd_export       *lexp;
+       struct lustre_handle    conn = { 0 };
+       struct mdt_device       *mdt;
+       int                      rc;
+       lnet_nid_t              *client_nid = localdata;
+       ENTRY;
+
+       LASSERT(env != NULL);
+       if (!exp || !obd || !cluuid)
+               RETURN(-EINVAL);
+
+       mdt = mdt_dev(obd->obd_lu_dev);
+
+       /*
+        * first, check whether the stack is ready to handle requests
+        * XXX: probably not very appropriate method is used now
+        *      at some point we should find a better one
+        */
+       if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) && data != NULL &&
+           !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
+               rc = obd_get_info(env, mdt->mdt_child_exp,
+                                 sizeof(KEY_OSP_CONNECTED),
+                                 KEY_OSP_CONNECTED, NULL, NULL, NULL);
+               if (rc)
+                       RETURN(-EAGAIN);
+               set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
+       }
+
+       rc = class_connect(&conn, obd, cluuid);
+       if (rc)
+               RETURN(rc);
+
+       lexp = class_conn2export(&conn);
+       LASSERT(lexp != NULL);
+
+       rc = mdt_connect_internal(lexp, mdt, data);
+       if (rc == 0) {
+               struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
+
+               LASSERT(lcd);
+               memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
+               rc = tgt_client_new(env, lexp);
+               if (rc == 0) {
+                       rc = nodemap_add_member(*client_nid, lexp);
+                       if (rc != 0 && rc != -EEXIST)
+                               goto out;
+
+                       mdt_export_stats_init(obd, lexp, localdata);
+               }
+
+               /* For phase I, sync for cross-ref operation. */
+               spin_lock(&lexp->exp_lock);
+               lexp->exp_keep_sync = 1;
+               spin_unlock(&lexp->exp_lock);
+       }
+out:
+       if (rc != 0) {
+               class_disconnect(lexp);
+               *exp = NULL;
+       } else {
+               *exp = lexp;
+       }
+
+       RETURN(rc);
+}
+
+static int mdt_obd_reconnect(const struct lu_env *env,
+                            struct obd_export *exp, struct obd_device *obd,
+                            struct obd_uuid *cluuid,
+                            struct obd_connect_data *data,
+                            void *localdata)
+{
+       lnet_nid_t             *client_nid = localdata;
+       int                     rc;
+       ENTRY;
+
+       if (exp == NULL || obd == NULL || cluuid == NULL)
+               RETURN(-EINVAL);
+
+       rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
+       if (rc == 0) {
+               rc = nodemap_add_member(*client_nid, exp);
+               if (rc == 0 || rc == -EEXIST)
+                       mdt_export_stats_init(obd, exp, localdata);
+       }
+
+       RETURN(rc);
 }
 
 /* FIXME: Can we avoid using these two interfaces? */
 static int mdt_init_export(struct obd_export *exp)
 {
-        struct mdt_export_data *med = &exp->exp_mdt_data;
-        int                     rc;
-        ENTRY;
+       struct mdt_export_data *med = &exp->exp_mdt_data;
+       int                     rc;
+       ENTRY;
 
-        CFS_INIT_LIST_HEAD(&med->med_open_head);
+       INIT_LIST_HEAD(&med->med_open_head);
        spin_lock_init(&med->med_open_lock);
        mutex_init(&med->med_idmap_mutex);
        med->med_idmap = NULL;
@@ -5124,36 +5302,17 @@ static int mdt_destroy_export(struct obd_export *exp)
                                      &exp->exp_client_uuid)))
                 RETURN(0);
 
-        ldlm_destroy_export(exp);
-        tgt_client_free(exp);
+       ldlm_destroy_export(exp);
+       tgt_client_free(exp);
 
-        LASSERT(cfs_list_empty(&exp->exp_outstanding_replies));
-        LASSERT(cfs_list_empty(&exp->exp_mdt_data.med_open_head));
+       LASSERT(list_empty(&exp->exp_outstanding_replies));
+       LASSERT(list_empty(&exp->exp_mdt_data.med_open_head));
 
-        RETURN(0);
+       RETURN(0);
 }
 
-/** The maximum depth that fid2path() will search.
- * This is limited only because we want to store the fids for
- * historical path lookup purposes.
- */
-#define MAX_PATH_DEPTH 100
-
-/** mdt_path() lookup structure. */
-struct path_lookup_info {
-       __u64                   pli_recno;      /**< history point */
-       __u64                   pli_currec;     /**< current record */
-       struct lu_fid           pli_fid;
-       struct lu_fid           pli_fids[MAX_PATH_DEPTH]; /**< path, in fids */
-       struct mdt_object       *pli_mdt_obj;
-       char                    *pli_path;      /**< full path */
-       int                     pli_pathlen;
-       int                     pli_linkno;     /**< which hardlink to follow */
-       int                     pli_fidcount;   /**< number of \a pli_fids */
-};
-
-static int mdt_links_read(struct mdt_thread_info *info,
-                         struct mdt_object *mdt_obj, struct linkea_data *ldata)
+int mdt_links_read(struct mdt_thread_info *info, struct mdt_object *mdt_obj,
+                  struct linkea_data *ldata)
 {
        int rc;
 
@@ -5183,8 +5342,21 @@ static int mdt_links_read(struct mdt_thread_info *info,
        return linkea_init(ldata);
 }
 
+/**
+ * Given an MDT object, try to look up the full path to the object.
+ * Part of the MDT layer implementation of lfs fid2path.
+ *
+ * \param[in]     info  Per-thread common data shared by MDT level handlers.
+ * \param[in]     obj   Object to do path lookup of
+ * \param[in,out] fp    User-provided struct to store path information
+ *
+ * \retval 0 Lookup successful, path information stored in fp
+ * \retval -EAGAIN Lookup failed, usually because object is being moved
+ * \retval negative errno if there was a problem
+ */
 static int mdt_path_current(struct mdt_thread_info *info,
-                           struct path_lookup_info *pli)
+                           struct mdt_object *obj,
+                           struct getinfo_fid2path *fp)
 {
        struct mdt_device       *mdt = info->mti_mdt;
        struct mdt_object       *mdt_obj;
@@ -5197,6 +5369,7 @@ static int mdt_path_current(struct mdt_thread_info *info,
        int                     reclen;
        struct linkea_data      ldata = { 0 };
        int                     rc = 0;
+       bool                    first = true;
        ENTRY;
 
        /* temp buffer for path element, the buffer will be finally freed
@@ -5206,121 +5379,151 @@ static int mdt_path_current(struct mdt_thread_info *info,
                RETURN(-ENOMEM);
 
        ldata.ld_buf = buf;
-       ptr = pli->pli_path + pli->pli_pathlen - 1;
+       ptr = fp->gf_path + fp->gf_pathlen - 1;
        *ptr = 0;
        --ptr;
-       pli->pli_fidcount = 0;
-       pli->pli_fids[0] = *(struct lu_fid *)mdt_object_fid(pli->pli_mdt_obj);
+       *tmpfid = fp->gf_fid = *mdt_object_fid(obj);
 
        /* root FID only exists on MDT0, and fid2path should also ends at MDT0,
         * so checking root_fid can only happen on MDT0. */
-       while (!lu_fid_eq(&mdt->mdt_md_root_fid,
-                         &pli->pli_fids[pli->pli_fidcount])) {
-               mdt_obj = mdt_object_find(info->mti_env, mdt,
-                                         &pli->pli_fids[pli->pli_fidcount]);
+       while (!lu_fid_eq(&mdt->mdt_md_root_fid, &fp->gf_fid)) {
+               struct lu_buf           lmv_buf;
+
+               mdt_obj = mdt_object_find(info->mti_env, mdt, tmpfid);
                if (IS_ERR(mdt_obj))
                        GOTO(out, rc = PTR_ERR(mdt_obj));
-               if (mdt_object_remote(mdt_obj)) {
-                       mdt_object_put(info->mti_env, mdt_obj);
-                       GOTO(remote_out, rc = -EREMOTE);
-               }
+
                if (!mdt_object_exists(mdt_obj)) {
                        mdt_object_put(info->mti_env, mdt_obj);
                        GOTO(out, rc = -ENOENT);
                }
 
+               if (mdt_object_remote(mdt_obj)) {
+                       mdt_object_put(info->mti_env, mdt_obj);
+                       GOTO(remote_out, rc = -EREMOTE);
+               }
+
                rc = mdt_links_read(info, mdt_obj, &ldata);
-               mdt_object_put(info->mti_env, mdt_obj);
-               if (rc != 0)
+               if (rc != 0) {
+                       mdt_object_put(info->mti_env, mdt_obj);
                        GOTO(out, rc);
+               }
 
                leh = buf->lb_buf;
                lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
                linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
                /* If set, use link #linkno for path lookup, otherwise use
                   link #0.  Only do this for the final path element. */
-               if (pli->pli_fidcount == 0 &&
-                   pli->pli_linkno < leh->leh_reccount) {
+               if (first && fp->gf_linkno < leh->leh_reccount) {
                        int count;
-                       for (count = 0; count < pli->pli_linkno; count++) {
+                       for (count = 0; count < fp->gf_linkno; count++) {
                                lee = (struct link_ea_entry *)
                                     ((char *)lee + reclen);
                                linkea_entry_unpack(lee, &reclen, tmpname,
                                                    tmpfid);
                        }
-                       if (pli->pli_linkno < leh->leh_reccount - 1)
+                       if (fp->gf_linkno < leh->leh_reccount - 1)
                                /* indicate to user there are more links */
-                               pli->pli_linkno++;
+                               fp->gf_linkno++;
+               }
+
+               lmv_buf.lb_buf = info->mti_xattr_buf;
+               lmv_buf.lb_len = sizeof(info->mti_xattr_buf);
+               /* Check if it is slave stripes */
+               rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
+                                 &lmv_buf, XATTR_NAME_LMV);
+               mdt_object_put(info->mti_env, mdt_obj);
+               if (rc > 0) {
+                       union lmv_mds_md *lmm = lmv_buf.lb_buf;
+
+                       /* For slave stripes, get its master */
+                       if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_STRIPE) {
+                               fp->gf_fid = *tmpfid;
+                               continue;
+                       }
+               } else if (rc < 0 && rc != -ENODATA) {
+                       GOTO(out, rc);
                }
 
+               rc = 0;
+
                /* Pack the name in the end of the buffer */
                ptr -= tmpname->ln_namelen;
-               if (ptr - 1 <= pli->pli_path)
+               if (ptr - 1 <= fp->gf_path)
                        GOTO(out, rc = -EOVERFLOW);
                strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
                *(--ptr) = '/';
 
-               /* Store the parent fid for historic lookup */
-               if (++pli->pli_fidcount >= MAX_PATH_DEPTH)
-                       GOTO(out, rc = -EOVERFLOW);
-               pli->pli_fids[pli->pli_fidcount] = *tmpfid;
+               /* keep the last resolved fid to the client, so the
+                * client will build the left path on another MDT for
+                * remote object */
+               fp->gf_fid = *tmpfid;
+
+               first = false;
        }
 
 remote_out:
        ptr++; /* skip leading / */
-       memmove(pli->pli_path, ptr, pli->pli_path + pli->pli_pathlen - ptr);
+       memmove(fp->gf_path, ptr, fp->gf_path + fp->gf_pathlen - ptr);
 
-       EXIT;
 out:
-       return rc;
+       RETURN(rc);
 }
 
-/* Returns the full path to this fid, as of changelog record recno. */
+/**
+ * Given an MDT object, use mdt_path_current to get the path.
+ * Essentially a wrapper to retry mdt_path_current a set number of times
+ * if -EAGAIN is returned (usually because an object is being moved).
+ *
+ * Part of the MDT layer implementation of lfs fid2path.
+ *
+ * \param[in]     info  Per-thread common data shared by mdt level handlers.
+ * \param[in]     obj   Object to do path lookup of
+ * \param[in,out] fp    User-provided struct for arguments and to store path
+ *                     information
+ *
+ * \retval 0 Lookup successful, path information stored in fp
+ * \retval negative errno if there was a problem
+ */
 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
-                   char *path, int pathlen, __u64 *recno, int *linkno,
-                   struct lu_fid *fid)
+                   struct getinfo_fid2path *fp)
 {
        struct mdt_device       *mdt = info->mti_mdt;
-       struct path_lookup_info *pli;
        int                     tries = 3;
        int                     rc = -EAGAIN;
        ENTRY;
 
-       if (pathlen < 3)
+       if (fp->gf_pathlen < 3)
                RETURN(-EOVERFLOW);
 
        if (lu_fid_eq(&mdt->mdt_md_root_fid, mdt_object_fid(obj))) {
-               path[0] = '\0';
+               fp->gf_path[0] = '\0';
                RETURN(0);
        }
 
-       OBD_ALLOC_PTR(pli);
-       if (pli == NULL)
-               RETURN(-ENOMEM);
-
-       pli->pli_mdt_obj = obj;
-       pli->pli_recno = *recno;
-       pli->pli_path = path;
-       pli->pli_pathlen = pathlen;
-       pli->pli_linkno = *linkno;
-
        /* Retry multiple times in case file is being moved */
        while (tries-- && rc == -EAGAIN)
-               rc = mdt_path_current(info, pli);
-
-       /* return the last resolved fids to the client, so the client will
-        * build the left path on another MDT for remote object */
-       *fid = pli->pli_fids[pli->pli_fidcount];
-
-       *recno = pli->pli_currec;
-       /* Return next link index to caller */
-       *linkno = pli->pli_linkno;
-
-       OBD_FREE_PTR(pli);
+               rc = mdt_path_current(info, obj, fp);
 
        RETURN(rc);
 }
 
+/**
+ * Get the full path of the provided FID, as of changelog record recno.
+ *
+ * This checks sanity and looks up object for user provided FID
+ * before calling the actual path lookup code.
+ *
+ * Part of the MDT layer implementation of lfs fid2path.
+ *
+ * \param[in]     info  Per-thread common data shared by mdt level handlers.
+ * \param[in,out] fp    User-provided struct for arguments and to store path
+ *                     information
+ *
+ * \retval 0 Lookup successful, path information and recno stored in fp
+ * \retval -ENOENT, object does not exist
+ * \retval negative errno if there was a problem
+ */
 static int mdt_fid2path(struct mdt_thread_info *info,
                        struct getinfo_fid2path *fp)
 {
@@ -5358,13 +5561,12 @@ static int mdt_fid2path(struct mdt_thread_info *info,
 
        if (rc < 0) {
                mdt_object_put(info->mti_env, obj);
-               CDEBUG(D_IOCTL, "nonlocal object "DFID": %d\n",
+               CDEBUG(D_IOCTL, "nonlocal object "DFID": rc = %d\n",
                       PFID(&fp->gf_fid), rc);
                RETURN(rc);
        }
 
-       rc = mdt_path(info, obj, fp->gf_path, fp->gf_pathlen, &fp->gf_recno,
-                     &fp->gf_linkno, &fp->gf_fid);
+       rc = mdt_path(info, obj, fp);
 
        CDEBUG(D_INFO, "fid "DFID", path %s recno "LPX64" linkno %u\n",
               PFID(&fp->gf_fid), fp->gf_path, fp->gf_recno, fp->gf_linkno);
@@ -5406,7 +5608,7 @@ int mdt_get_info(struct tgt_session_info *tsi)
 
        key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
        if (key == NULL) {
-               CDEBUG(D_IOCTL, "No GETINFO key");
+               CDEBUG(D_IOCTL, "No GETINFO key\n");
                RETURN(err_serious(-EFAULT));
        }
        keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
@@ -5414,7 +5616,8 @@ int mdt_get_info(struct tgt_session_info *tsi)
 
        vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
        if (vallen == NULL) {
-               CDEBUG(D_IOCTL, "Unable to get RMF_GETINFO_VALLEN buffer");
+               CDEBUG(D_IOCTL, "%s: cannot get RMF_GETINFO_VALLEN buffer\n",
+                               tgt_name(tsi->tsi_tgt));
                RETURN(err_serious(-EFAULT));
        }
 
@@ -5426,7 +5629,8 @@ int mdt_get_info(struct tgt_session_info *tsi)
 
        valout = req_capsule_server_get(tsi->tsi_pill, &RMF_GETINFO_VAL);
        if (valout == NULL) {
-               CDEBUG(D_IOCTL, "Unable to get get-info RPC out buffer");
+               CDEBUG(D_IOCTL, "%s: cannot get get-info RPC out buffer\n",
+                               tgt_name(tsi->tsi_tgt));
                RETURN(err_serious(-EFAULT));
        }
 
@@ -5556,14 +5760,24 @@ static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
                }
 
                lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
-               lsp.lsp_namespace = mdt->mdt_namespace;
+               lsp.lsp_index_valid = 0;
                rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
                break;
        }
        case OBD_IOC_STOP_LFSCK: {
-               struct md_device *next = mdt->mdt_child;
+               struct md_device        *next = mdt->mdt_child;
+               struct obd_ioctl_data   *data = karg;
+               struct lfsck_stop        stop;
+
+               stop.ls_status = LS_STOPPED;
+               /* Old lfsck utils may pass NULL @stop. */
+               if (data->ioc_inlbuf1 == NULL)
+                       stop.ls_flags = 0;
+               else
+                       stop.ls_flags =
+                       ((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
 
-               rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, NULL);
+               rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &stop);
                break;
        }
         case OBD_IOC_GET_OBJ_VERSION: {
@@ -5577,9 +5791,15 @@ static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
                 rc = mdt_ioc_version_get(mti, karg);
                 break;
         }
-       case OBD_IOC_CATLOGLIST:
-               rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg);
+       case OBD_IOC_CATLOGLIST: {
+               struct mdt_thread_info *mti;
+
+               mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
+               lu_local_obj_fid(&mti->mti_tmp_fid1, LLOG_CATALOGS_OID);
+               rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg,
+                                      &mti->mti_tmp_fid1);
                break;
+        }
        default:
                rc = -EOPNOTSUPP;
                CERROR("%s: Not supported cmd = %d, rc = %d\n",
@@ -5590,17 +5810,27 @@ static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
         RETURN(rc);
 }
 
-int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
+static int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
 {
-        struct lu_device *ld = md2lu_dev(mdt->mdt_child);
-        int rc;
-        ENTRY;
+       struct lu_device *ld = md2lu_dev(mdt->mdt_child);
+       struct lfsck_start_param lsp;
+       int rc;
+       ENTRY;
 
-        rc = ld->ld_ops->ldo_recovery_complete(env, ld);
-        RETURN(rc);
+       lsp.lsp_start = NULL;
+       lsp.lsp_index_valid = 0;
+       rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
+                                                  OBD_IOC_START_LFSCK,
+                                                  0, &lsp);
+       if (rc != 0 && rc != -EALREADY)
+               CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
+                     mdt_obd_name(mdt), rc);
+
+       rc = ld->ld_ops->ldo_recovery_complete(env, ld);
+       RETURN(rc);
 }
 
-int mdt_obd_postrecov(struct obd_device *obd)
+static int mdt_obd_postrecov(struct obd_device *obd)
 {
         struct lu_env env;
         int rc;
@@ -5714,13 +5944,13 @@ void mdt_enable_cos(struct mdt_device *mdt, int val)
 
         mdt->mdt_opts.mo_cos = !!val;
         rc = lu_env_init(&env, LCT_LOCAL);
-        if (unlikely(rc != 0)) {
-                CWARN("lu_env initialization failed with rc = %d,"
-                      "cannot sync\n", rc);
-                return;
-        }
-        mdt_device_sync(&env, mdt);
-        lu_env_fini(&env);
+       if (unlikely(rc != 0)) {
+               CWARN("%s: lu_env initialization failed, cannot "
+                     "sync: rc = %d\n", mdt_obd_name(mdt), rc);
+               return;
+       }
+       mdt_device_sync(&env, mdt);
+       lu_env_fini(&env);
 }
 
 /**
@@ -5750,7 +5980,6 @@ static struct lu_device_type mdt_device_type = {
 
 static int __init mdt_mod_init(void)
 {
-       struct lprocfs_static_vars lvars;
        int rc;
 
        CLASSERT(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") ==
@@ -5765,12 +5994,8 @@ static int __init mdt_mod_init(void)
        if (rc)
                GOTO(lu_fini, rc);
 
-       lprocfs_mdt_init_vars(&lvars);
-       rc = class_register_type(&mdt_obd_device_ops, NULL, NULL,
-#ifndef HAVE_ONLY_PROCFS_SEQ
-                               lvars.module_vars,
-#endif
-                               LUSTRE_MDT_NAME, &mdt_device_type);
+       rc = class_register_type(&mdt_obd_device_ops, NULL, true, NULL,
+                                LUSTRE_MDT_NAME, &mdt_device_type);
        if (rc)
                GOTO(mds_fini, rc);
 lu_fini: