Whamcloud - gitweb
LU-3557 mdt: fix open existing file with O_CREAT for ro mount
[fs/lustre-release.git] / lustre / mdt / mdt_handler.c
index ccf86c1..0de83de 100644 (file)
 /* lu2dt_dev() */
 #include <dt_object.h>
 #include <lustre_mds.h>
-#include <lustre_mdt.h>
 #include <lustre_log.h>
 #include "mdt_internal.h"
 #include <lustre_acl.h>
 #include <lustre_param.h>
 #include <lustre_quota.h>
 #include <lustre_linkea.h>
+#include <lustre_lfsck.h>
 
 mdl_mode_t mdt_mdl_lock_modes[] = {
         [LCK_MINMODE] = MDL_MINMODE,
@@ -98,7 +98,13 @@ static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
 static const struct lu_object_operations mdt_obj_ops;
 
 /* Slab for MDT object allocation */
-static cfs_mem_cache_t *mdt_object_kmem;
+static struct kmem_cache *mdt_object_kmem;
+
+/* For HSM restore handles */
+struct kmem_cache *mdt_hsm_cdt_kmem;
+
+/* For HSM request handles */
+struct kmem_cache *mdt_hsm_car_kmem;
 
 static struct lu_kmem_descr mdt_caches[] = {
        {
@@ -107,6 +113,16 @@ static struct lu_kmem_descr mdt_caches[] = {
                .ckd_size  = sizeof(struct mdt_object)
        },
        {
+               .ckd_cache      = &mdt_hsm_cdt_kmem,
+               .ckd_name       = "mdt_cdt_restore_handle",
+               .ckd_size       = sizeof(struct cdt_restore_handle)
+       },
+       {
+               .ckd_cache      = &mdt_hsm_car_kmem,
+               .ckd_name       = "mdt_cdt_agent_req",
+               .ckd_size       = sizeof(struct cdt_agent_req)
+       },
+       {
                .ckd_cache = NULL
        }
 };
@@ -154,6 +170,13 @@ void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lm,
         if (name != NULL && (name[0] != '\0')) {
                 LASSERT(namelen > 0);
                 lh->mlh_pdo_hash = full_name_hash(name, namelen);
+               /* XXX Workaround for LU-2856
+                * Zero is a valid return value of full_name_hash, but several
+                * users of mlh_pdo_hash assume a non-zero hash value. We
+                * therefore map zero onto an arbitrary, but consistent
+                * value (1) to avoid problems further down the road. */
+               if (unlikely(!lh->mlh_pdo_hash))
+                       lh->mlh_pdo_hash = 1;
         } else {
                 LASSERT(namelen == 0);
                 lh->mlh_pdo_hash = 0ull;
@@ -388,7 +411,12 @@ void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
                 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;
-        }
+       } 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. */
+               b->blocks = 0;
+               b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
+       }
 
         if (fid) {
                 b->fid1 = *fid;
@@ -719,18 +747,26 @@ static int mdt_getattr_internal(struct mdt_thread_info *info,
                 ma->ma_som = &info->mti_u.som.data;
 
        rc = mdt_attr_get_complex(info, o, ma);
-        if (unlikely(rc)) {
-                CERROR("getattr error for "DFID": %d\n",
-                        PFID(mdt_object_fid(o)), rc);
-                RETURN(rc);
-        }
+       if (unlikely(rc)) {
+               CERROR("%s: getattr error for "DFID": rc = %d\n",
+                      mdt_obd_name(info->mti_mdt),
+                      PFID(mdt_object_fid(o)), rc);
+               RETURN(rc);
+       }
+
+       /* 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;
+       }
 
        is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
 
        /* the Lustre protocol supposes to return default striping
         * on the user-visible root if explicitly requested */
        if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
-           (ma->ma_need & MA_LOV_DEF && is_root) && (ma->ma_need & MA_LOV)) {
+           (ma->ma_need & MA_LOV_DEF && is_root) && ma->ma_need & MA_LOV) {
                struct lu_fid      rootfid;
                struct mdt_object *root;
                struct mdt_device *mdt = info->mti_mdt;
@@ -744,8 +780,9 @@ static int mdt_getattr_internal(struct mdt_thread_info *info,
                rc = mdt_attr_get_lov(info, root, ma);
                mdt_object_put(info->mti_env, root);
                if (unlikely(rc)) {
-                       CERROR("getattr error for "DFID": %d\n",
-                                       PFID(mdt_object_fid(o)), rc);
+                       CERROR("%s: getattr error for "DFID": rc = %d\n",
+                              mdt_obd_name(info->mti_mdt),
+                              PFID(mdt_object_fid(o)), rc);
                        RETURN(rc);
                }
        }
@@ -770,18 +807,20 @@ static int mdt_getattr_internal(struct mdt_thread_info *info,
                         repbody->eadatasize = ma->ma_lmv_size;
                         repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
                 }
-        } else if (S_ISLNK(la->la_mode) &&
-                   reqbody->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;
-                rc = mo_readlink(env, next, buffer);
-                if (unlikely(rc <= 0)) {
-                        CERROR("readlink failed: %d\n", rc);
-                        rc = -EFAULT;
-                } else {
-                       int print_limit = min_t(int, CFS_PAGE_SIZE - 128, rc);
+       } else if (S_ISLNK(la->la_mode) &&
+                  reqbody->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;
+               rc = mo_readlink(env, next, buffer);
+               if (unlikely(rc <= 0)) {
+                       CERROR("%s: readlink failed for "DFID": rc = %d\n",
+                              mdt_obd_name(info->mti_mdt),
+                              PFID(mdt_object_fid(o)), rc);
+                       rc = -EFAULT;
+               } else {
+                       int print_limit = min_t(int, PAGE_CACHE_SIZE - 128, rc);
 
                        if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
                                rc -= 2;
@@ -790,8 +829,11 @@ static int mdt_getattr_internal(struct mdt_thread_info *info,
                         * because client expects that */
                        repbody->eadatasize = rc + 1;
                        if (repbody->eadatasize != reqbody->eadatasize)
-                               CERROR("Read shorter symlink %d, expected %d\n",
-                                      rc, reqbody->eadatasize - 1);
+                               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);
                        /* NULL terminate */
                        ((char *)ma->ma_lmm)[rc] = 0;
 
@@ -845,9 +887,12 @@ static int mdt_getattr_internal(struct mdt_thread_info *info,
                                         rc = 0;
                                 } else if (rc == -EOPNOTSUPP) {
                                         rc = 0;
-                                } else {
-                                        CERROR("got acl size: %d\n", rc);
-                                }
+                               } 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;
@@ -932,9 +977,9 @@ int mdt_getattr(struct mdt_thread_info *info)
         }
 
         LASSERT(obj != NULL);
-        LASSERT(lu_object_assert_exists(&obj->mot_obj.mo_lu));
+       LASSERT(lu_object_assert_exists(&obj->mot_obj));
 
-        mode = lu_object_attr(&obj->mot_obj.mo_lu);
+       mode = lu_object_attr(&obj->mot_obj);
 
         /* old clients may not report needed easize, use max value then */
         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
@@ -1206,7 +1251,7 @@ static int mdt_getattr_name_lock(struct mdt_thread_info *info,
 
        if (unlikely(!mdt_object_exists(parent))) {
                LU_OBJECT_DEBUG(D_INODE, info->mti_env,
-                               &parent->mot_obj.mo_lu,
+                               &parent->mot_obj,
                                "Parent doesn't exist!\n");
                RETURN(-ESTALE);
        } else if (!info->mti_cross_ref) {
@@ -1266,7 +1311,7 @@ static int mdt_getattr_name_lock(struct mdt_thread_info *info,
 
         if (lname) {
                 /* step 1: lock parent only if parent is a directory */
-                if (S_ISDIR(lu_object_attr(&parent->mot_obj.mo_lu))) {
+               if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
                         lhp = &info->mti_lh[MDT_LH_PARENT];
                         mdt_lock_pdo_init(lhp, LCK_PR, name, namelen);
                         rc = mdt_object_lock(info, parent, lhp,
@@ -1299,35 +1344,33 @@ static int mdt_getattr_name_lock(struct mdt_thread_info *info,
          */
         child = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
 
-        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: %lu/%lu/%lu, Fid: "DFID".\n",
-                                 (unsigned long)res_id->name[0],
-                                 (unsigned long)res_id->name[1],
-                                 (unsigned long)res_id->name[2],
-                                 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 {
+       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:
@@ -1337,7 +1380,7 @@ relock:
 
                if (!mdt_object_exists(child)) {
                        LU_OBJECT_DEBUG(D_INODE, info->mti_env,
-                                       &child->mot_obj.mo_lu,
+                                       &child->mot_obj,
                                        "Object doesn't exist!\n");
                        GOTO(out_child, rc = -ENOENT);
                }
@@ -1369,7 +1412,7 @@ relock:
                LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
                if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
                    exp_connect_layout(info->mti_exp) &&
-                   S_ISREG(lu_object_attr(&child->mot_obj.mo_lu)) &&
+                   S_ISREG(lu_object_attr(&child->mot_obj)) &&
                    ldlm_rep != NULL) {
                        /* try to grant layout lock for regular file. */
                        try_layout = true;
@@ -1409,17 +1452,15 @@ relock:
         rc = mdt_getattr_internal(info, child, ma_need);
         if (unlikely(rc != 0)) {
                 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),
-                         "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
-                         (unsigned long)res_id->name[0],
-                         (unsigned long)res_id->name[1],
-                         (unsigned long)res_id->name[2],
-                         PFID(mdt_object_fid(child)));
+       } 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),
+                        "Lock res_id: "DLDLMRES", fid: "DFID"\n",
+                        PLDLMRES(lock->l_resource),
+                        PFID(mdt_object_fid(child)));
                if (mdt_object_exists(child) && !mdt_object_remote(child))
                        mdt_pack_size2body(info, child);
         }
@@ -1541,6 +1582,9 @@ int mdt_set_info(struct mdt_thread_info *info)
         RETURN(0);
 }
 
+int mdt_connect_check_sptlrpc(struct mdt_device *mdt, struct obd_export *exp,
+                             struct ptlrpc_request *req);
+
 /**
  * Top-level handler for MDT connection requests.
  */
@@ -1551,32 +1595,39 @@ int mdt_connect(struct mdt_thread_info *info)
        struct obd_export *exp;
        struct ptlrpc_request *req = mdt_info_req(info);
 
+       ENTRY;
+
        rc = target_handle_connect(req);
        if (rc != 0)
-               return err_serious(rc);
+               RETURN(err_serious(rc));
 
        LASSERT(req->rq_export != NULL);
-       info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
+       exp = req->rq_export;
+       info->mti_exp = exp;
+       info->mti_mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
        rc = mdt_init_sec_level(info);
-       if (rc != 0) {
-               obd_disconnect(class_export_get(req->rq_export));
-               return rc;
-       }
+       if (rc != 0)
+               GOTO(err, rc);
+
+       rc = mdt_connect_check_sptlrpc(info->mti_mdt, exp, req);
+       if (rc)
+               GOTO(err, rc);
 
        /* To avoid exposing partially initialized connection flags, changes up
         * to this point have been staged in reply->ocd_connect_flags. Now that
         * connection handling has completed successfully, atomically update
         * the connect flags in the shared export data structure. LU-1623 */
        reply = req_capsule_server_get(info->mti_pill, &RMF_CONNECT_DATA);
-       exp = req->rq_export;
        spin_lock(&exp->exp_lock);
        *exp_connect_flags_ptr(exp) = reply->ocd_connect_flags;
        spin_unlock(&exp->exp_lock);
 
        rc = mdt_init_idmap(info);
        if (rc != 0)
-               obd_disconnect(class_export_get(req->rq_export));
-
+               GOTO(err, rc);
+       RETURN(0);
+err:
+       obd_disconnect(class_export_get(req->rq_export));
        return rc;
 }
 
@@ -1616,7 +1667,7 @@ static int mdt_sendpage(struct mdt_thread_info *info,
 
        for (i = 0, tmpcount = nob; i < rdpg->rp_npages && tmpcount > 0;
             i++, tmpcount -= tmpsize) {
-                tmpsize = min_t(int, tmpcount, CFS_PAGE_SIZE);
+               tmpsize = min_t(int, tmpcount, PAGE_CACHE_SIZE);
                ptlrpc_prep_bulk_page_pin(desc, rdpg->rp_pages[i], 0, tmpsize);
         }
 
@@ -1661,14 +1712,14 @@ int mdt_readpage(struct mdt_thread_info *info)
                rdpg->rp_attrs |= LUDA_64BITHASH;
        rdpg->rp_count  = min_t(unsigned int, reqbody->nlink,
                                exp_max_brw_size(info->mti_exp));
-        rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE - 1) >>
-                          CFS_PAGE_SHIFT;
+       rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
+                         PAGE_CACHE_SHIFT;
         OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
         if (rdpg->rp_pages == NULL)
                 RETURN(-ENOMEM);
 
         for (i = 0; i < rdpg->rp_npages; ++i) {
-                rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
+               rdpg->rp_pages[i] = alloc_page(GFP_IOFS);
                 if (rdpg->rp_pages[i] == NULL)
                         GOTO(free_rdpg, rc = -ENOMEM);
         }
@@ -1686,7 +1737,7 @@ free_rdpg:
 
         for (i = 0; i < rdpg->rp_npages; i++)
                 if (rdpg->rp_pages[i] != NULL)
-                        cfs_free_page(rdpg->rp_pages[i]);
+                       __free_page(rdpg->rp_pages[i]);
         OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
 
         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
@@ -2099,14 +2150,15 @@ int mdt_obd_idx_read(struct mdt_thread_info *info)
                GOTO(out, rc = -EFAULT);
        rdpg->rp_count = min_t(unsigned int, req_ii->ii_count << LU_PAGE_SHIFT,
                               exp_max_brw_size(info->mti_exp));
-       rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE -1) >> CFS_PAGE_SHIFT;
+       rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
+                               PAGE_CACHE_SHIFT;
 
        /* allocate pages to store the containers */
        OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
        if (rdpg->rp_pages == NULL)
                GOTO(out, rc = -ENOMEM);
        for (i = 0; i < rdpg->rp_npages; i++) {
-               rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
+               rdpg->rp_pages[i] = alloc_page(GFP_IOFS);
                if (rdpg->rp_pages[i] == NULL)
                        GOTO(out, rc = -ENOMEM);
        }
@@ -2127,7 +2179,7 @@ out:
        if (rdpg->rp_pages) {
                for (i = 0; i < rdpg->rp_npages; i++)
                        if (rdpg->rp_pages[i])
-                               cfs_free_page(rdpg->rp_pages[i]);
+                               __free_page(rdpg->rp_pages[i]);
                OBD_FREE(rdpg->rp_pages,
                         rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
        }
@@ -2332,8 +2384,8 @@ int mdt_quota_dqacq(struct mdt_thread_info *info)
 
 static struct mdt_object *mdt_obj(struct lu_object *o)
 {
-        LASSERT(lu_device_is_mdt(o->lo_dev));
-        return container_of0(o, struct mdt_object, mot_obj.mo_lu);
+       LASSERT(lu_device_is_mdt(o->lo_dev));
+       return container_of0(o, struct mdt_object, mot_obj);
 }
 
 struct mdt_object *mdt_object_new(const struct lu_env *env,
@@ -2346,7 +2398,7 @@ struct mdt_object *mdt_object_new(const struct lu_env *env,
        ENTRY;
 
        CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
-       o = lu_object_find(env, &d->mdt_md_dev.md_lu_dev, f, &conf);
+       o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
        if (unlikely(IS_ERR(o)))
                m = (struct mdt_object *)o;
        else
@@ -2355,20 +2407,21 @@ struct mdt_object *mdt_object_new(const struct lu_env *env,
 }
 
 struct mdt_object *mdt_object_find(const struct lu_env *env,
-                                   struct mdt_device *d,
-                                   const struct lu_fid *f)
+                                  struct mdt_device *d,
+                                  const struct lu_fid *f)
 {
-        struct lu_object *o;
-        struct mdt_object *m;
-        ENTRY;
+       struct lu_object *o;
+       struct mdt_object *m;
+       ENTRY;
 
-        CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
-        o = lu_object_find(env, &d->mdt_md_dev.md_lu_dev, f, NULL);
-        if (unlikely(IS_ERR(o)))
-                m = (struct mdt_object *)o;
-        else
-                m = mdt_obj(o);
-        RETURN(m);
+       CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
+       o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
+       if (unlikely(IS_ERR(o)))
+               m = (struct mdt_object *)o;
+       else
+               m = mdt_obj(o);
+
+       RETURN(m);
 }
 
 /**
@@ -2508,7 +2561,7 @@ int mdt_remote_object_lock(struct mdt_thread_info *mti,
 
        LASSERT(mdt_object_remote(o));
 
-       LASSERT((ibits & MDS_INODELOCK_UPDATE));
+       LASSERT(ibits & MDS_INODELOCK_UPDATE);
 
        memset(einfo, 0, sizeof(*einfo));
        einfo->ei_type = LDLM_IBITS;
@@ -2563,8 +2616,8 @@ static int mdt_object_lock0(struct mdt_thread_info *info, struct mdt_object *o,
                         RETURN(-ESTALE);
                 } else {
                         /* Non-dir object shouldn't have PDO lock */
-                        if (!S_ISDIR(lu_object_attr(&o->mot_obj.mo_lu)))
-                                RETURN(-ENOTDIR);
+                       if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
+                               RETURN(-ENOTDIR);
                 }
         }
 
@@ -3049,12 +3102,11 @@ void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
  * uninitialized state, because it's too expensive to zero out whole
  * mdt_thread_info (> 1K) on each request arrival.
  */
-static void mdt_thread_info_init(struct ptlrpc_request *req,
-                                 struct mdt_thread_info *info)
+void mdt_thread_info_init(struct ptlrpc_request *req,
+                         struct mdt_thread_info *info)
 {
         int i;
 
-        req_capsule_init(&req->rq_pill, req, RCL_SERVER);
         info->mti_pill = &req->rq_pill;
 
         /* lock handle */
@@ -3087,11 +3139,10 @@ static void mdt_thread_info_init(struct ptlrpc_request *req,
        info->mti_spec.sp_rm_entry = 0;
 }
 
-static void mdt_thread_info_fini(struct mdt_thread_info *info)
+void mdt_thread_info_fini(struct mdt_thread_info *info)
 {
        int i;
 
-       req_capsule_fini(info->mti_pill);
        if (info->mti_object != NULL) {
                mdt_object_put(info->mti_env, info->mti_object);
                info->mti_object = NULL;
@@ -3100,11 +3151,41 @@ static void mdt_thread_info_fini(struct mdt_thread_info *info)
        for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
                mdt_lock_handle_fini(&info->mti_lh[i]);
        info->mti_env = NULL;
+       info->mti_pill = NULL;
+       info->mti_exp = NULL;
 
        if (unlikely(info->mti_big_buf.lb_buf != NULL))
                lu_buf_free(&info->mti_big_buf);
 }
 
+int mdt_tgt_connect(struct tgt_session_info *tsi)
+{
+       struct ptlrpc_request   *req = tgt_ses_req(tsi);
+       struct mdt_thread_info  *mti;
+       int                      rc;
+
+       ENTRY;
+
+       rc = tgt_connect(tsi);
+       if (rc != 0)
+               RETURN(rc);
+
+       /* XXX: switch mdt_init_idmap() to use tgt_session_info */
+       lu_env_refill((void *)tsi->tsi_env);
+       mti = lu_context_key_get(&tsi->tsi_env->le_ctx, &mdt_thread_key);
+       LASSERT(mti != NULL);
+
+       mdt_thread_info_init(req, mti);
+       rc = mdt_init_idmap(mti);
+       mdt_thread_info_fini(mti);
+       if (rc != 0)
+               GOTO(err, rc);
+       RETURN(0);
+err:
+       obd_disconnect(class_export_get(req->rq_export));
+       return rc;
+}
+
 static int mdt_filter_recovery_request(struct ptlrpc_request *req,
                                        struct obd_device *obd, int *process)
 {
@@ -3336,16 +3417,21 @@ static int mdt_handle0(struct ptlrpc_request *req,
                         if (likely(h != NULL)) {
                                 rc = mdt_req_handle(info, h, req);
                         } else {
-                                CERROR("The unsupported opc: 0x%x\n",
-                                       lustre_msg_get_opc(msg) );
+                               CERROR("%s: opc unsupported: 0x%x\n",
+                                       mdt_obd_name(info->mti_mdt),
+                                       lustre_msg_get_opc(msg));
                                 req->rq_status = -ENOTSUPP;
                                 rc = ptlrpc_error(req);
                                 RETURN(rc);
                         }
                 }
-        } else
-                CERROR(LUSTRE_MDT_NAME" drops mal-formed request\n");
-        RETURN(rc);
+       } else {
+               CDEBUG(D_INFO, "%s: drops mal-formed request: rc = %d\n",
+                       mdt_obd_name(info->mti_mdt), rc);
+               req->rq_status = rc;
+               rc = ptlrpc_error(req);
+       }
+       RETURN(rc);
 }
 
 /*
@@ -3374,11 +3460,13 @@ int mdt_handle_common(struct ptlrpc_request *req,
         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
         LASSERT(info != NULL);
 
+       req_capsule_init(&req->rq_pill, req, RCL_SERVER);
         mdt_thread_info_init(req, info);
 
         rc = mdt_handle0(req, info, supported);
 
         mdt_thread_info_fini(info);
+       req_capsule_fini(&req->rq_pill);
         RETURN(rc);
 }
 
@@ -3388,22 +3476,13 @@ int mdt_handle_common(struct ptlrpc_request *req,
  */
 int mdt_recovery_handle(struct ptlrpc_request *req)
 {
-        int rc;
-        ENTRY;
+       int rc;
 
-        switch (lustre_msg_get_opc(req->rq_reqmsg)) {
-        case FLD_QUERY:
-                rc = mdt_handle_common(req, mdt_fld_handlers);
-                break;
-        case SEQ_QUERY:
-                rc = mdt_handle_common(req, mdt_seq_handlers);
-                break;
-        default:
-                rc = mdt_handle_common(req, mdt_regular_handlers);
-                break;
-        }
+       ENTRY;
 
-        RETURN(rc);
+       rc = mdt_handle_common(req, mdt_regular_handlers);
+
+       RETURN(rc);
 }
 
 enum mdt_it_code {
@@ -3425,6 +3504,12 @@ static int mdt_intent_getattr(enum mdt_it_code opcode,
                               struct mdt_thread_info *info,
                               struct ldlm_lock **,
                              __u64);
+
+static int mdt_intent_getxattr(enum mdt_it_code opcode,
+                               struct mdt_thread_info *info,
+                               struct ldlm_lock **lockp,
+                               __u64 flags);
+
 static int mdt_intent_layout(enum mdt_it_code opcode,
                             struct mdt_thread_info *info,
                             struct ldlm_lock **,
@@ -3452,7 +3537,14 @@ static struct mdt_it_flavor {
         },
         [MDT_IT_OCREAT]   = {
                 .it_fmt   = &RQF_LDLM_INTENT,
-                .it_flags = MUTABOR,
+               /*
+                * OCREAT is not a MUTABOR request as if the file
+                * already exists.
+                * We do the extra check of OBD_CONNECT_RDONLY in
+                * mdt_reint_open() when we really need to create
+                * the object.
+                */
+               .it_flags = 0,
                 .it_act   = mdt_intent_reint,
                 .it_reint = REINT_OPEN
         },
@@ -3489,9 +3581,9 @@ static struct mdt_it_flavor {
                 .it_act   = NULL
         },
         [MDT_IT_GETXATTR] = {
-                .it_fmt   = NULL,
-                .it_flags = 0,
-                .it_act   = NULL
+               .it_fmt   = &RQF_LDLM_INTENT_GETXATTR,
+               .it_flags = HABEO_CORPUS,
+               .it_act   = mdt_intent_getxattr
         },
        [MDT_IT_LAYOUT] = {
                .it_fmt   = &RQF_LDLM_INTENT_LAYOUT,
@@ -3642,6 +3734,44 @@ static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
                   remote_hdl.cookie);
 }
 
+static int mdt_intent_getxattr(enum mdt_it_code opcode,
+                               struct mdt_thread_info *info,
+                               struct ldlm_lock **lockp,
+                               __u64 flags)
+{
+       struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
+       struct ldlm_reply      *ldlm_rep = NULL;
+       int rc, grc;
+
+       /*
+        * Initialize lhc->mlh_reg_lh either from a previously granted lock
+        * (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);
+       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,
+                                       MDS_INODELOCK_XATTR,
+                                       MDT_LOCAL_LOCK);
+               if (rc)
+                       return rc;
+       }
+
+       grc = mdt_getxattr(info);
+
+       rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
+
+       if (mdt_info_req(info)->rq_repmsg != NULL)
+               ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
+       if (ldlm_rep == NULL)
+               RETURN(err_serious(-EFAULT));
+
+       ldlm_rep->lock_policy_res2 = grc;
+
+       return rc;
+}
+
 static int mdt_intent_getattr(enum mdt_it_code opcode,
                               struct mdt_thread_info *info,
                               struct ldlm_lock **lockp,
@@ -3691,8 +3821,8 @@ static int mdt_intent_getattr(enum mdt_it_code opcode,
         /* Get lock from request for possible resent case. */
         mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
 
-        ldlm_rep->lock_policy_res2 =
-                mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
+       rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
+       ldlm_rep->lock_policy_res2 = clear_serious(rc);
 
         if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
                 ldlm_rep->lock_policy_res2 = 0;
@@ -3720,15 +3850,38 @@ static int mdt_intent_layout(enum mdt_it_code opcode,
                             __u64 flags)
 {
        struct layout_intent *layout;
+       struct lu_fid *fid;
+       struct mdt_object *obj = NULL;
+       struct md_object *child = NULL;
        int rc;
        ENTRY;
 
        if (opcode != MDT_IT_LAYOUT) {
-               CERROR("%s: Unknown intent (%d)\n",
-                       info->mti_exp->exp_obd->obd_name, opcode);
+               CERROR("%s: Unknown intent (%d)\n", mdt_obd_name(info->mti_mdt),
+                       opcode);
                RETURN(-EINVAL);
        }
 
+       fid = &info->mti_tmp_fid2;
+       fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
+
+       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);
+
+               if (rc > info->mti_mdt->mdt_max_mdsize)
+                       info->mti_mdt->mdt_max_mdsize = rc;
+       }
+
+       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));
@@ -3743,7 +3896,7 @@ static int mdt_intent_layout(enum mdt_it_code opcode,
                RETURN(0);
 
        CERROR("%s: Unsupported layout intent (%d)\n",
-               info->mti_exp->exp_obd->obd_name, layout->li_opc);
+               mdt_obd_name(info->mti_mdt), layout->li_opc);
        RETURN(-EINVAL);
 }
 
@@ -3924,8 +4077,18 @@ static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
                        RETURN(-EROFS);
         }
         if (rc == 0 && flv->it_act != NULL) {
-                /* execute policy */
-                rc = flv->it_act(opc, info, lockp, flags);
+               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) {
+                       rep = req_capsule_server_get(info->mti_pill,
+                                                    &RMF_DLM_REP);
+                       rep->lock_policy_res2 =
+                               ptlrpc_status_hton(rep->lock_policy_res2);
+               }
         } else {
                 rc = -EOPNOTSUPP;
         }
@@ -4123,7 +4286,7 @@ 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 = mdt2obd_dev(m);
+       struct obd_device       *obd;
        struct lustre_cfg_bufs  *bufs;
        struct lustre_cfg       *lcfg;
        struct mdt_thread_info  *info;
@@ -4145,7 +4308,7 @@ static void mdt_stack_pre_fini(const struct lu_env *env,
         * objects (some of them are pinned by osd, for example *
         * the proper solution should be a model where object used
         * by osd only doesn't have mdt/mdd slices -bzzz */
-       lustre_cfg_bufs_reset(bufs, obd->obd_name);
+       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) {
@@ -4158,44 +4321,44 @@ static void mdt_stack_pre_fini(const struct lu_env *env,
 }
 
 static void mdt_stack_fini(const struct lu_env *env,
-                           struct mdt_device *m, struct lu_device *top)
+                          struct mdt_device *m, struct lu_device *top)
 {
-        struct obd_device       *obd = mdt2obd_dev(m);
-        struct lustre_cfg_bufs  *bufs;
-        struct lustre_cfg       *lcfg;
-        struct mdt_thread_info  *info;
-        char flags[3]="";
-        ENTRY;
+       struct obd_device       *obd = mdt2obd_dev(m);
+       struct lustre_cfg_bufs  *bufs;
+       struct lustre_cfg       *lcfg;
+       struct mdt_thread_info  *info;
+       char                     flags[3] = "";
+       ENTRY;
 
-        info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
-        LASSERT(info != NULL);
+       info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
+       LASSERT(info != NULL);
 
        lu_dev_del_linkage(top->ld_site, top);
 
        lu_site_purge(env, top->ld_site, -1);
 
-        bufs = &info->mti_u.bufs;
-        /* process cleanup, pass mdt obd name to get obd umount flags */
+       bufs = &info->mti_u.bufs;
+       /* process cleanup, pass mdt obd name to get obd umount flags */
        /* another purpose is to let all layers to release their objects */
-        lustre_cfg_bufs_reset(bufs, obd->obd_name);
-        if (obd->obd_force)
-                strcat(flags, "F");
-        if (obd->obd_fail)
-                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;
-        }
-        LASSERT(top);
-        top->ld_ops->ldo_process_config(env, top, lcfg);
-        lustre_cfg_free(lcfg);
+       lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
+       if (obd->obd_force)
+               strcat(flags, "F");
+       if (obd->obd_fail)
+               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;
+       }
+       LASSERT(top);
+       top->ld_ops->ldo_process_config(env, top, lcfg);
+       lustre_cfg_free(lcfg);
 
        lu_site_purge(env, top->ld_site, -1);
 
-        m->mdt_child = NULL;
-        m->mdt_bottom = NULL;
+       m->mdt_child = NULL;
+       m->mdt_bottom = NULL;
 
        obd_disconnect(m->mdt_child_exp);
        m->mdt_child_exp = NULL;
@@ -4219,7 +4382,7 @@ static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
        obd = class_name2obd(next);
        if (obd == NULL) {
                CERROR("%s: can't locate next device: %s\n",
-                      m->mdt_md_dev.md_lu_dev.ld_obd->obd_name, next);
+                      mdt_obd_name(m), next);
                GOTO(out, rc = -ENOTCONN);
        }
 
@@ -4229,7 +4392,7 @@ static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
        rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
        if (rc) {
                CERROR("%s: cannot connect to next dev %s (%d)\n",
-                      m->mdt_md_dev.md_lu_dev.ld_obd->obd_name, next, rc);
+                      mdt_obd_name(m), next, rc);
                GOTO(out, rc);
        }
 
@@ -4345,9 +4508,9 @@ static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
 
        site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
        LASSERT(site);
-       LASSERT(mdt->mdt_md_dev.md_lu_dev.ld_site == NULL);
-       mdt->mdt_md_dev.md_lu_dev.ld_site = site;
-       site->ls_top_dev = &mdt->mdt_md_dev.md_lu_dev;
+       LASSERT(mdt_lu_site(mdt) == NULL);
+       mdt->mdt_lu_dev.ld_site = site;
+       site->ls_top_dev = &mdt->mdt_lu_dev;
        mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
 
 
@@ -4364,7 +4527,7 @@ static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
        if (rc != 0)
                CERROR("Failure to refill session: '%d'\n", rc);
 
-       lu_dev_add_linkage(site, &mdt->mdt_md_dev.md_lu_dev);
+       lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
 
        EXIT;
 class_detach:
@@ -4475,7 +4638,7 @@ static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
 
        /* configure local quota objects */
        rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
-                                                  &mdt->mdt_md_dev.md_lu_dev,
+                                                  &mdt->mdt_lu_dev,
                                                   mdt->mdt_qmt_dev);
        if (rc)
                GOTO(class_cleanup, rc);
@@ -4530,10 +4693,60 @@ static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
        EXIT;
 }
 
+static struct tgt_handler mdt_tgt_handlers[] = {
+TGT_RPC_HANDLER(MDS_FIRST_OPC,
+               0,                      MDS_CONNECT,    mdt_tgt_connect,
+               &RQF_CONNECT, LUSTRE_OBD_VERSION),
+TGT_RPC_HANDLER(MDS_FIRST_OPC,
+               0,                      MDS_DISCONNECT, tgt_disconnect,
+               &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
+};
+
+static struct tgt_opc_slice mdt_common_slice[] = {
+       {
+               .tos_opc_start  = MDS_FIRST_OPC,
+               .tos_opc_end    = MDS_LAST_OPC,
+               .tos_hs         = mdt_tgt_handlers
+       },
+       {
+               .tos_opc_start  = OBD_FIRST_OPC,
+               .tos_opc_end    = OBD_LAST_OPC,
+               .tos_hs         = tgt_obd_handlers
+       },
+       {
+               .tos_opc_start  = LDLM_FIRST_OPC,
+               .tos_opc_end    = LDLM_LAST_OPC,
+               .tos_hs         = tgt_dlm_handlers
+       },
+       {
+               .tos_opc_start  = SEC_FIRST_OPC,
+               .tos_opc_end    = SEC_LAST_OPC,
+               .tos_hs         = tgt_sec_ctx_handlers
+       },
+       {
+               .tos_opc_start  = UPDATE_OBJ,
+               .tos_opc_end    = UPDATE_LAST_OPC,
+               .tos_hs         = tgt_out_handlers
+       },
+       {
+               .tos_opc_start  = FLD_FIRST_OPC,
+               .tos_opc_end    = FLD_LAST_OPC,
+               .tos_hs         = fld_handlers
+       },
+       {
+               .tos_opc_start  = SEQ_FIRST_OPC,
+               .tos_opc_end    = SEQ_LAST_OPC,
+               .tos_hs         = seq_handlers
+       },
+       {
+               .tos_hs         = NULL
+       }
+};
+
 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_md_dev.md_lu_dev;
+       struct md_device  *next = m->mdt_child;
+       struct lu_device  *d    = &m->mdt_lu_dev;
         struct obd_device *obd = mdt2obd_dev(m);
         ENTRY;
 
@@ -4541,8 +4754,14 @@ static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
 
         ping_evictor_stop();
 
-
        mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
+
+       if (m->mdt_opts.mo_coordinator)
+               mdt_hsm_cdt_stop(m);
+
+       mdt_hsm_cdt_fini(m);
+
+       mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
         mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
         obd_exports_barrier(obd);
         obd_zombie_barrier();
@@ -4569,8 +4788,8 @@ static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
                 m->mdt_nosquash_strlen = 0;
         }
 
-       next->md_ops->mdo_iocontrol(env, next, OBD_IOC_PAUSE_LFSCK,
-                                   0, NULL);
+       next->md_ops->mdo_iocontrol(env, next, OBD_IOC_PAUSE_LFSCK, 0, NULL);
+
         mdt_seq_fini(env, m);
         mdt_fld_fini(env, m);
         sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
@@ -4579,40 +4798,40 @@ static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
         cfs_timer_disarm(&m->mdt_ck_timer);
         mdt_ck_thread_stop(m);
 
-        /*
-         * Finish the stack
-         */
-        mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
+       /*
+        * Finish the stack
+        */
+       mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
 
-        LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
+       LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
 
-       server_put_mount(mdt2obd_dev(m)->obd_name, NULL);
+       server_put_mount(mdt_obd_name(m), NULL);
 
-        EXIT;
+       EXIT;
 }
 
 static int mdt_adapt_sptlrpc_conf(struct obd_device *obd, int initial)
 {
-        struct mdt_device       *m = mdt_dev(obd->obd_lu_dev);
-        struct sptlrpc_rule_set  tmp_rset;
-        int                      rc;
+       struct mdt_device       *m = mdt_dev(obd->obd_lu_dev);
+       struct sptlrpc_rule_set  tmp_rset;
+       int                      rc;
 
-        sptlrpc_rule_set_init(&tmp_rset);
-        rc = sptlrpc_conf_target_get_rules(obd, &tmp_rset, initial);
-        if (rc) {
-                CERROR("mdt %s: failed get sptlrpc rules: %d\n",
-                       obd->obd_name, rc);
-                return rc;
-        }
+       sptlrpc_rule_set_init(&tmp_rset);
+       rc = sptlrpc_conf_target_get_rules(obd, &tmp_rset, initial);
+       if (rc) {
+               CERROR("mdt %s: failed get sptlrpc rules: %d\n",
+                      mdt_obd_name(m), rc);
+               return rc;
+       }
 
-        sptlrpc_target_update_exp_flavor(obd, &tmp_rset);
+       sptlrpc_target_update_exp_flavor(obd, &tmp_rset);
 
        write_lock(&m->mdt_sptlrpc_lock);
-        sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
-        m->mdt_sptlrpc_rset = tmp_rset;
+       sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
+       m->mdt_sptlrpc_rset = tmp_rset;
        write_unlock(&m->mdt_sptlrpc_lock);
 
-        return 0;
+       return 0;
 }
 
 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
@@ -4631,11 +4850,11 @@ static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
         const char                *identity_upcall = "NONE";
         struct md_device          *next;
         int                        rc;
-        int                        node_id;
+       long                       node_id;
         mntopt_t                   mntopts;
         ENTRY;
 
-        md_device_init(&m->mdt_md_dev, ldt);
+       lu_device_init(&m->mdt_lu_dev, ldt);
         /*
          * Environment (env) might be missing mdt_thread_key values at that
          * point, if device is allocated when mdt_thread_key is in QUIESCENT
@@ -4659,6 +4878,11 @@ static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
         m->mdt_som_conf = 0;
 
         m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
+
+       /* default is coordinator off, it is started through conf_param
+        * or /proc */
+       m->mdt_opts.mo_coordinator = 0;
+
        lmi = server_get_mount(dev);
         if (lmi == NULL) {
                 CERROR("Cannot get mount info for %s!\n", dev);
@@ -4672,7 +4896,7 @@ static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
         }
 
        rwlock_init(&m->mdt_sptlrpc_lock);
-        sptlrpc_rule_set_init(&m->mdt_sptlrpc_rset);
+       sptlrpc_rule_set_init(&m->mdt_sptlrpc_rset);
 
        spin_lock_init(&m->mdt_ioepoch_lock);
         m->mdt_opts.mo_compat_resname = 0;
@@ -4692,20 +4916,21 @@ static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
        m->mdt_enable_remote_dir = 0;
        m->mdt_enable_remote_dir_gid = 0;
 
-        m->mdt_md_dev.md_lu_dev.ld_ops = &mdt_lu_ops;
-        m->mdt_md_dev.md_lu_dev.ld_obd = obd;
-        /* set this lu_device to obd, because error handling need it */
-        obd->obd_lu_dev = &m->mdt_md_dev.md_lu_dev;
+       m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
+       m->mdt_lu_dev.ld_obd = obd;
+       /* Set this lu_device to obd for error handling purposes. */
+       obd->obd_lu_dev = &m->mdt_lu_dev;
 
        /* init the stack */
        rc = mdt_stack_init((struct lu_env *)env, m, cfg);
        if (rc) {
-               CERROR("Can't init device stack, rc %d\n", rc);
+               CERROR("%s: Can't init device stack, rc %d\n",
+                      mdt_obd_name(m), rc);
                GOTO(err_lmi, rc);
        }
 
-       s = m->mdt_md_dev.md_lu_dev.ld_site;
-       ss_site = &m->mdt_seq_site;
+       s = mdt_lu_site(m);
+       ss_site = mdt_seq_site(m);
        s->ld_seq_site = ss_site;
        ss_site->ss_lu = s;
 
@@ -4720,28 +4945,24 @@ static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
         /* No connection accepted until configurations will finish */
         obd->obd_no_conn = 1;
 
-        if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
-                char *str = lustre_cfg_string(cfg, 4);
-                if (strchr(str, 'n')) {
-                        CWARN("%s: recovery disabled\n", obd->obd_name);
-                        obd->obd_replayable = 0;
-                }
-        }
-
-        rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom);
-        if (rc)
-                GOTO(err_fini_stack, rc);
+       if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
+               char *str = lustre_cfg_string(cfg, 4);
+               if (strchr(str, 'n')) {
+                       CWARN("%s: recovery disabled\n", mdt_obd_name(m));
+                       obd->obd_replayable = 0;
+               }
+       }
 
-       rc = mdt_fld_init(env, obd->obd_name, m);
+       rc = mdt_fld_init(env, mdt_obd_name(m), m);
        if (rc)
-               GOTO(err_lut, rc);
+               GOTO(err_fini_stack, rc);
 
-       rc = mdt_seq_init(env, obd->obd_name, m);
+       rc = mdt_seq_init(env, mdt_obd_name(m), m);
        if (rc)
                GOTO(err_fini_fld, rc);
 
-        snprintf(info->mti_u.ns_name, sizeof info->mti_u.ns_name,
-                 LUSTRE_MDT_NAME"-%p", m);
+       snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
+                LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
         m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
                                               LDLM_NAMESPACE_SERVER,
                                               LDLM_NAMESPACE_GREEDY,
@@ -4758,13 +4979,26 @@ static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
 
         cfs_timer_init(&m->mdt_ck_timer, mdt_ck_timer_callback, m);
 
-        rc = mdt_ck_thread_start(m);
-        if (rc)
+       rc = mdt_hsm_cdt_init(m);
+       if (rc != 0) {
+               CERROR("%s: error initializing coordinator, rc %d\n",
+                      mdt_obd_name(m), rc);
                 GOTO(err_free_ns, rc);
+       }
 
-        rc = mdt_fs_setup(env, m, obd, lsi);
+        rc = mdt_ck_thread_start(m);
         if (rc)
-                GOTO(err_capa, rc);
+                GOTO(err_free_hsm, rc);
+
+       rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
+                     OBD_FAIL_MDS_ALL_REQUEST_NET,
+                     OBD_FAIL_MDS_ALL_REPLY_NET);
+       if (rc)
+               GOTO(err_capa, rc);
+
+       rc = mdt_fs_setup(env, m, obd, lsi);
+       if (rc)
+               GOTO(err_tgt, rc);
 
         mdt_adapt_sptlrpc_conf(obd, 1);
 
@@ -4772,30 +5006,35 @@ static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
         rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
                                          &mntopts);
         if (rc)
-               GOTO(err_llog_cleanup, rc);
+               GOTO(err_fs_cleanup, rc);
 
         if (mntopts & MNTOPT_USERXATTR)
                 m->mdt_opts.mo_user_xattr = 1;
         else
                 m->mdt_opts.mo_user_xattr = 0;
 
+       rc = next->md_ops->mdo_maxeasize_get(env, next, &m->mdt_max_ea_size);
+       if (rc)
+               GOTO(err_fs_cleanup, rc);
+
         if (mntopts & MNTOPT_ACL)
                 m->mdt_opts.mo_acl = 1;
         else
                 m->mdt_opts.mo_acl = 0;
 
-        /* XXX: to support suppgid for ACL, we enable identity_upcall
-         * by default, otherwise, maybe got unexpected -EACCESS. */
-        if (m->mdt_opts.mo_acl)
-                identity_upcall = MDT_IDENTITY_UPCALL_PATH;
-
-        m->mdt_identity_cache = upcall_cache_init(obd->obd_name,identity_upcall,
-                                                &mdt_identity_upcall_cache_ops);
-        if (IS_ERR(m->mdt_identity_cache)) {
-                rc = PTR_ERR(m->mdt_identity_cache);
-                m->mdt_identity_cache = NULL;
-               GOTO(err_llog_cleanup, rc);
-        }
+       /* XXX: to support suppgid for ACL, we enable identity_upcall
+        * by default, otherwise, maybe got unexpected -EACCESS. */
+       if (m->mdt_opts.mo_acl)
+               identity_upcall = MDT_IDENTITY_UPCALL_PATH;
+
+       m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
+                                               identity_upcall,
+                                               &mdt_identity_upcall_cache_ops);
+       if (IS_ERR(m->mdt_identity_cache)) {
+               rc = PTR_ERR(m->mdt_identity_cache);
+               m->mdt_identity_cache = NULL;
+               GOTO(err_fs_cleanup, rc);
+       }
 
         rc = mdt_procfs_init(m, dev);
         if (rc) {
@@ -4811,7 +5050,7 @@ static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
        ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
                           "mdt_ldlm_client", m->mdt_ldlm_client);
 
-        ping_evictor_start();
+       ping_evictor_start();
 
        /* recovery will be started upon mdt_prepare()
         * when the whole stack is complete and ready
@@ -4826,34 +5065,34 @@ static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
                 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
 
         RETURN(0);
-
 err_procfs:
-        mdt_procfs_fini(m);
+       mdt_procfs_fini(m);
 err_recovery:
-        target_recovery_fini(obd);
-        upcall_cache_cleanup(m->mdt_identity_cache);
-        m->mdt_identity_cache = NULL;
-err_llog_cleanup:
-        mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
-        mdt_fs_cleanup(env, m);
+       target_recovery_fini(obd);
+       upcall_cache_cleanup(m->mdt_identity_cache);
+       m->mdt_identity_cache = NULL;
+err_fs_cleanup:
+       mdt_fs_cleanup(env, m);
+err_tgt:
+       tgt_fini(env, &m->mdt_lut);
 err_capa:
-        cfs_timer_disarm(&m->mdt_ck_timer);
-        mdt_ck_thread_stop(m);
+       cfs_timer_disarm(&m->mdt_ck_timer);
+       mdt_ck_thread_stop(m);
+err_free_hsm:
+       mdt_hsm_cdt_fini(m);
 err_free_ns:
-        ldlm_namespace_free(m->mdt_namespace, NULL, 0);
-        obd->obd_namespace = m->mdt_namespace = NULL;
+       ldlm_namespace_free(m->mdt_namespace, NULL, 0);
+       obd->obd_namespace = m->mdt_namespace = NULL;
 err_fini_seq:
        mdt_seq_fini(env, m);
 err_fini_fld:
        mdt_fld_fini(env, m);
-err_lut:
-       tgt_fini(env, &m->mdt_lut);
 err_fini_stack:
-        mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
+       mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
 err_lmi:
        if (lmi)
                server_put_mount(dev, lmi->lmi_mnt);
-        return (rc);
+       return(rc);
 }
 
 /* For interoperability, the left element is old parameter, the right one
@@ -4943,12 +5182,12 @@ static struct lu_object *mdt_object_alloc(const struct lu_env *env,
 
         ENTRY;
 
-       OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, CFS_ALLOC_IO);
+       OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, __GFP_IO);
         if (mo != NULL) {
                 struct lu_object *o;
                 struct lu_object_header *h;
 
-                o = &mo->mot_obj.mo_lu;
+               o = &mo->mot_obj;
                 h = &mo->mot_header;
                 lu_object_header_init(h);
                 lu_object_init(o, h, d);
@@ -4956,9 +5195,11 @@ static struct lu_object *mdt_object_alloc(const struct lu_env *env,
                 o->lo_ops = &mdt_obj_ops;
                mutex_init(&mo->mot_ioepoch_mutex);
                mutex_init(&mo->mot_lov_mutex);
-                RETURN(o);
-        } else
-                RETURN(NULL);
+               init_rwsem(&mo->mot_open_sem);
+               init_rwsem(&mo->mot_xattr_sem);
+               RETURN(o);
+       }
+       RETURN(NULL);
 }
 
 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
@@ -4993,11 +5234,14 @@ static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
         CDEBUG(D_INFO, "object free, fid = "DFID"\n",
                PFID(lu_object_fid(o)));
 
-        lu_object_fini(o);
-        lu_object_header_fini(h);
+       LASSERT(atomic_read(&mo->mot_open_count) == 0);
+       LASSERT(atomic_read(&mo->mot_lease_count) == 0);
+
+       lu_object_fini(o);
+       lu_object_header_fini(h);
        OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
 
-        EXIT;
+       EXIT;
 }
 
 static int mdt_object_print(const struct lu_env *env, void *cookie,
@@ -5017,6 +5261,7 @@ 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;
@@ -5031,11 +5276,20 @@ static int mdt_prepare(const struct lu_env *env,
        if (rc)
                RETURN(rc);
 
+       rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
+       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, NULL);
-       if (rc != 0)
-               CWARN("Fail to auto trigger paused LFSCK.\n");
+                                                  0, &lsp);
+       if (rc != 0) {
+               CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
+                     mdt_obd_name(mdt), rc);
+               rc = 0;
+       }
 
        if (mdt->mdt_seq_site.ss_node_id == 0) {
                rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
@@ -5043,6 +5297,7 @@ static int mdt_prepare(const struct lu_env *env,
                if (rc)
                        RETURN(rc);
        }
+
        LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
        target_recovery_init(&mdt->mdt_lut, mdt_recovery_handle);
        set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
@@ -5145,7 +5400,7 @@ static int mdt_connect_internal(struct obd_export *exp,
                               "unexpectedly zero, network data "
                               "corruption? Refusing connection of this"
                               " client\n",
-                              exp->exp_obd->obd_name,
+                              mdt_obd_name(mdt),
                               exp->exp_client_uuid.uuid,
                               exp, data->ocd_connect_flags, data->ocd_version,
                               data->ocd_grant, data->ocd_index);
@@ -5170,7 +5425,7 @@ static int mdt_connect_internal(struct obd_export *exp,
 
        if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
                CWARN("%s: MDS requires FID support, but client not\n",
-                     mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
+                     mdt_obd_name(mdt));
                return -EBADE;
        }
 
@@ -5179,11 +5434,11 @@ static int mdt_connect_internal(struct obd_export *exp,
                                         OBD_CONNECT_MDS_MDS |
                                         OBD_CONNECT_SOM))) {
                CWARN("%s: MDS has SOM enabled, but client does not support "
-                     "it\n", mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
+                     "it\n", mdt_obd_name(mdt));
                return -EBADE;
        }
 
-       if (data->ocd_connect_flags & OBD_CONNECT_PINGLESS) {
+       if (OCD_HAS_FLAG(data, PINGLESS)) {
                if (ptlrpc_pinger_suppress_pings()) {
                        spin_lock(&exp->exp_obd->obd_dev_lock);
                        list_del_init(&exp->exp_obd_chain_timed);
@@ -5193,12 +5448,13 @@ static int mdt_connect_internal(struct obd_export *exp,
                }
        }
 
+       data->ocd_max_easize = mdt->mdt_max_ea_size;
+
        return 0;
 }
 
-static int mdt_connect_check_sptlrpc(struct mdt_device *mdt,
-                                    struct obd_export *exp,
-                                    struct ptlrpc_request *req)
+int mdt_connect_check_sptlrpc(struct mdt_device *mdt, struct obd_export *exp,
+                             struct ptlrpc_request *req)
 {
        struct sptlrpc_flavor   flvr;
        int                     rc = 0;
@@ -5247,11 +5503,9 @@ static int mdt_obd_connect(const struct lu_env *env,
                            struct obd_connect_data *data,
                            void *localdata)
 {
-        struct mdt_thread_info *info;
         struct obd_export      *lexp;
         struct lustre_handle    conn = { 0 };
         struct mdt_device      *mdt;
-        struct ptlrpc_request  *req;
         int                     rc;
         ENTRY;
 
@@ -5259,9 +5513,7 @@ static int mdt_obd_connect(const struct lu_env *env,
         if (!exp || !obd || !cluuid)
                 RETURN(-EINVAL);
 
-        info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
-        req = info->mti_pill->rc_req;
-        mdt = mdt_dev(obd->obd_lu_dev);
+       mdt = mdt_dev(obd->obd_lu_dev);
 
        /*
         * first, check whether the stack is ready to handle requests
@@ -5283,26 +5535,17 @@ static int mdt_obd_connect(const struct lu_env *env,
         lexp = class_conn2export(&conn);
         LASSERT(lexp != NULL);
 
-        rc = mdt_connect_check_sptlrpc(mdt, lexp, req);
-        if (rc)
-                GOTO(out, rc);
-
-        if (OBD_FAIL_CHECK(OBD_FAIL_TGT_RCVG_FLAG))
-                lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
-
         rc = mdt_connect_internal(lexp, mdt, data);
         if (rc == 0) {
                 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
 
                 LASSERT(lcd);
-               info->mti_exp = lexp;
                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);
         }
 
-out:
         if (rc != 0) {
                 class_disconnect(lexp);
                 *exp = NULL;
@@ -5319,23 +5562,12 @@ static int mdt_obd_reconnect(const struct lu_env *env,
                              struct obd_connect_data *data,
                              void *localdata)
 {
-        struct mdt_thread_info *info;
-        struct mdt_device      *mdt;
-        struct ptlrpc_request  *req;
         int                     rc;
         ENTRY;
 
         if (exp == NULL || obd == NULL || cluuid == NULL)
                 RETURN(-EINVAL);
 
-        info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
-        req = info->mti_pill->rc_req;
-        mdt = mdt_dev(obd->obd_lu_dev);
-
-        rc = mdt_connect_check_sptlrpc(mdt, exp, req);
-        if (rc)
-                RETURN(rc);
-
         rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
         if (rc == 0)
                 mdt_export_stats_init(obd, exp, localdata);
@@ -5343,6 +5575,31 @@ static int mdt_obd_reconnect(const struct lu_env *env,
         RETURN(rc);
 }
 
+static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
+                                  struct mdt_thread_info *info,
+                                  struct mdt_file_data *mfd)
+{
+       struct lu_context ses;
+       int rc;
+       ENTRY;
+
+       rc = lu_context_init(&ses, LCT_SESSION);
+       if (rc)
+               RETURN(rc);
+
+       env->le_ses = &ses;
+       lu_context_enter(&ses);
+
+       mdt_ucred(info)->uc_valid = UCRED_OLD;
+       rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
+
+       lu_context_exit(&ses);
+       lu_context_fini(&ses);
+       env->le_ses = NULL;
+
+       RETURN(rc);
+}
+
 static int mdt_export_cleanup(struct obd_export *exp)
 {
         struct mdt_export_data *med = &exp->exp_mdt_data;
@@ -5387,6 +5644,24 @@ static int mdt_export_cleanup(struct obd_export *exp)
                 cfs_list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
                         cfs_list_del_init(&mfd->mfd_list);
                        ma->ma_need = ma->ma_valid = 0;
+
+                       /* This file is being closed due to an eviction, it
+                        * could have been modified and now dirty regarding to
+                        * HSM archive, check this!
+                        * The logic here is to mark a file dirty if there's a
+                        * chance it was dirtied before the client was evicted,
+                        * so that we don't have to wait for a release attempt
+                        * before finding out the file was actually dirty and
+                        * fail the release. Aggressively marking it dirty here
+                        * will cause the policy engine to attempt to
+                        * re-archive it; when rearchiving, we can compare the
+                        * current version to the HSM data_version and make the
+                        * archive request into a noop if it's not actually
+                        * dirty.
+                        */
+                       if (mfd->mfd_mode & (FMODE_WRITE|MDS_FMODE_TRUNC))
+                               rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
+
                        /* Don't unlink orphan on failover umount, LU-184 */
                        if (exp->exp_flags & OBD_OPT_FAILOVER) {
                                ma->ma_valid = MA_FLAGS;
@@ -5530,9 +5805,7 @@ static int mdt_links_read(struct mdt_thread_info *info,
        if (rc < 0)
                return rc;
 
-       linkea_init(ldata);
-
-       return 0;
+       return linkea_init(ldata);
 }
 
 static int mdt_path_current(struct mdt_thread_info *info,
@@ -5548,7 +5821,7 @@ static int mdt_path_current(struct mdt_thread_info *info,
        char                    *ptr;
        int                     reclen;
        struct linkea_data      ldata = { 0 };
-       int                     rc;
+       int                     rc = 0;
        ENTRY;
 
        /* temp buffer for path element, the buffer will be finally freed
@@ -5584,15 +5857,15 @@ static int mdt_path_current(struct mdt_thread_info *info,
                rc = mdt_links_read(info, mdt_obj, &ldata);
                mdt_object_put(info->mti_env, mdt_obj);
                if (rc != 0)
-                       GOTO(out, rc = PTR_ERR(buf));
+                       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 (pli->pli_fidcount == 0 &&
+                   pli->pli_linkno < leh->leh_reccount) {
                        int count;
                        for (count = 0; count < pli->pli_linkno; count++) {
                                lee = (struct link_ea_entry *)
@@ -5677,7 +5950,6 @@ static int mdt_fid2path(struct mdt_thread_info *info,
                        struct getinfo_fid2path *fp)
 {
        struct mdt_device *mdt = info->mti_mdt;
-       struct obd_device *obd = mdt2obd_dev(mdt);
        struct mdt_object *obj;
        int    rc;
        ENTRY;
@@ -5688,17 +5960,17 @@ static int mdt_fid2path(struct mdt_thread_info *info,
        if (!fid_is_sane(&fp->gf_fid))
                RETURN(-EINVAL);
 
-       if (!fid_is_client_mdt_visible(&fp->gf_fid)) {
+       if (!fid_is_namespace_visible(&fp->gf_fid)) {
                CWARN("%s: "DFID" is invalid, sequence should be "
-                       ">= "LPX64"\n", obd->obd_name,
-                       PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
+                     ">= "LPX64"\n", mdt_obd_name(mdt),
+                     PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
                RETURN(-EINVAL);
        }
 
        obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
        if (obj == NULL || IS_ERR(obj)) {
                CDEBUG(D_IOCTL, "no object "DFID": %ld\n", PFID(&fp->gf_fid),
-                       PTR_ERR(obj));
+                      PTR_ERR(obj));
                RETURN(-EINVAL);
        }
 
@@ -5712,7 +5984,7 @@ 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",
-                       PFID(&fp->gf_fid), rc);
+                      PFID(&fp->gf_fid), rc);
                RETURN(rc);
        }
 
@@ -5884,29 +6156,35 @@ static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
         case OBD_IOC_SET_READONLY:
                 rc = dt->dd_ops->dt_ro(&env, dt);
                 break;
-        case OBD_IOC_ABORT_RECOVERY:
-                CERROR("Aborting recovery for device %s\n", obd->obd_name);
-                target_stop_recovery_thread(obd);
-                rc = 0;
-                break;
+       case OBD_IOC_ABORT_RECOVERY:
+               CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
+               target_stop_recovery_thread(obd);
+               rc = 0;
+               break;
         case OBD_IOC_CHANGELOG_REG:
         case OBD_IOC_CHANGELOG_DEREG:
         case OBD_IOC_CHANGELOG_CLEAR:
                 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
                 break;
-       case OBD_IOC_START_LFSCK:
-       case OBD_IOC_STOP_LFSCK: {
+       case OBD_IOC_START_LFSCK: {
                struct md_device *next = mdt->mdt_child;
                struct obd_ioctl_data *data = karg;
+               struct lfsck_start_param lsp;
 
                if (unlikely(data == NULL)) {
                        rc = -EINVAL;
                        break;
                }
 
-               rc = next->md_ops->mdo_iocontrol(&env, next, cmd,
-                                                data->ioc_inllen1,
-                                                data->ioc_inlbuf1);
+               lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
+               lsp.lsp_namespace = mdt->mdt_namespace;
+               rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
+               break;
+       }
+       case OBD_IOC_STOP_LFSCK: {
+               struct md_device *next = mdt->mdt_child;
+
+               rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, NULL);
                break;
        }
         case OBD_IOC_GET_OBJ_VERSION: {
@@ -5920,11 +6198,11 @@ static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
                 rc = mdt_ioc_version_get(mti, karg);
                 break;
         }
-        default:
-                CERROR("Not supported cmd = %d for device %s\n",
-                       cmd, obd->obd_name);
-                rc = -EOPNOTSUPP;
-        }
+       default:
+               rc = -EOPNOTSUPP;
+               CERROR("%s: Not supported cmd = %d, rc = %d\n",
+                       mdt_obd_name(mdt), cmd, rc);
+       }
 
         lu_env_fini(&env);
         RETURN(rc);
@@ -5978,12 +6256,13 @@ static struct lu_device* mdt_device_fini(const struct lu_env *env,
 static struct lu_device *mdt_device_free(const struct lu_env *env,
                                          struct lu_device *d)
 {
-        struct mdt_device *m = mdt_dev(d);
-        ENTRY;
+       struct mdt_device *m = mdt_dev(d);
+       ENTRY;
 
-        md_device_fini(&m->mdt_md_dev);
-        OBD_FREE_PTR(m);
-        RETURN(NULL);
+       lu_device_fini(&m->mdt_lu_dev);
+       OBD_FREE_PTR(m);
+
+       RETURN(NULL);
 }
 
 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
@@ -5997,7 +6276,7 @@ static struct lu_device *mdt_device_alloc(const struct lu_env *env,
         if (m != NULL) {
                 int rc;
 
-                l = &m->mdt_md_dev.md_lu_dev;
+               l = &m->mdt_lu_dev;
                 rc = mdt_init0(env, m, t, cfg);
                 if (rc != 0) {
                         mdt_device_free(env, l);
@@ -6092,6 +6371,10 @@ static int __init mdt_mod_init(void)
        struct lprocfs_static_vars lvars;
        int rc;
 
+       CLASSERT(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") ==
+                FID_NOBRACE_LEN + 1);
+       CLASSERT(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") ==
+                FID_LEN + 1);
        rc = lu_kmem_init(mdt_caches);
        if (rc)
                return rc;