Whamcloud - gitweb
LU-3467 mdt: call MDT handlers via unified request handler
[fs/lustre-release.git] / lustre / mdt / mdt_handler.c
index da410c2..23a2136 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) 2011, 2012, Whamcloud, Inc.
+ * Copyright (c) 2010, 2013, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
 /* 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,
@@ -91,110 +92,19 @@ ldlm_mode_t mdt_dlm_lock_modes[] = {
         [MDL_GROUP]   = LCK_GROUP
 };
 
-/*
- * Initialized in mdt_mod_init().
- */
-static unsigned long mdt_num_threads;
-CFS_MODULE_PARM(mdt_num_threads, "ul", ulong, 0444,
-               "number of MDS service threads to start "
-               "(deprecated in favor of mds_num_threads)");
-
-static unsigned long mds_num_threads;
-CFS_MODULE_PARM(mds_num_threads, "ul", ulong, 0444,
-               "number of MDS service threads to start");
-
-static char *mds_num_cpts;
-CFS_MODULE_PARM(mds_num_cpts, "c", charp, 0444,
-               "CPU partitions MDS threads should run on");
-
-static unsigned long mds_rdpg_num_threads;
-CFS_MODULE_PARM(mds_rdpg_num_threads, "ul", ulong, 0444,
-               "number of MDS readpage service threads to start");
-
-static char *mds_rdpg_num_cpts;
-CFS_MODULE_PARM(mds_rdpg_num_cpts, "c", charp, 0444,
-               "CPU partitions MDS readpage threads should run on");
-
-/* NB: these two should be removed along with setattr service in the future */
-static unsigned long mds_attr_num_threads;
-CFS_MODULE_PARM(mds_attr_num_threads, "ul", ulong, 0444,
-               "number of MDS setattr service threads to start");
-
-static char *mds_attr_num_cpts;
-CFS_MODULE_PARM(mds_attr_num_cpts, "c", charp, 0444,
-               "CPU partitions MDS setattr threads should run on");
-
-/* ptlrpc request handler for MDT. All handlers are
- * grouped into several slices - struct mdt_opc_slice,
- * and stored in an array - mdt_handlers[].
- */
-struct mdt_handler {
-        /* The name of this handler. */
-        const char *mh_name;
-        /* Fail id for this handler, checked at the beginning of this handler*/
-        int         mh_fail_id;
-        /* Operation code for this handler */
-        __u32       mh_opc;
-        /* flags are listed in enum mdt_handler_flags below. */
-        __u32       mh_flags;
-        /* The actual handler function to execute. */
-        int (*mh_act)(struct mdt_thread_info *info);
-        /* Request format for this request. */
-        const struct req_format *mh_fmt;
-};
-
-enum mdt_handler_flags {
-        /*
-         * struct mdt_body is passed in the incoming message, and object
-         * identified by this fid exists on disk.
-         *
-         * "habeo corpus" == "I have a body"
-         */
-        HABEO_CORPUS = (1 << 0),
-        /*
-         * struct ldlm_request is passed in the incoming message.
-         *
-         * "habeo clavis" == "I have a key"
-         */
-        HABEO_CLAVIS = (1 << 1),
-        /*
-         * this request has fixed reply format, so that reply message can be
-         * packed by generic code.
-         *
-         * "habeo refero" == "I have a reply"
-         */
-        HABEO_REFERO = (1 << 2),
-        /*
-         * this request will modify something, so check whether the filesystem
-         * is readonly or not, then return -EROFS to client asap if necessary.
-         *
-         * "mutabor" == "I shall modify"
-         */
-        MUTABOR      = (1 << 3)
-};
-
-struct mdt_opc_slice {
-        __u32               mos_opc_start;
-        int                 mos_opc_end;
-        struct mdt_handler *mos_hs;
-};
-
-static struct mdt_opc_slice mdt_regular_handlers[];
-static struct mdt_opc_slice mdt_readpage_handlers[];
-static struct mdt_opc_slice mdt_xmds_handlers[];
-static struct mdt_opc_slice mdt_seq_handlers[];
-static struct mdt_opc_slice mdt_fld_handlers[];
-
 static struct mdt_device *mdt_dev(struct lu_device *d);
-static int mdt_regular_handle(struct ptlrpc_request *req);
 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
-static int mdt_fid2path(const struct lu_env *env, struct mdt_device *mdt,
-                        struct getinfo_fid2path *fp);
 
 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[] = {
        {
@@ -203,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
        }
 };
@@ -236,6 +156,7 @@ void mdt_lock_reg_init(struct mdt_lock_handle *lh, ldlm_mode_t lm)
 {
         lh->mlh_pdo_hash = 0;
         lh->mlh_reg_mode = lm;
+       lh->mlh_rreg_mode = lm;
         lh->mlh_type = MDT_REG_LOCK;
 }
 
@@ -243,11 +164,19 @@ void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lm,
                        const char *name, int namelen)
 {
         lh->mlh_reg_mode = lm;
+       lh->mlh_rreg_mode = lm;
         lh->mlh_type = MDT_PDO_LOCK;
 
         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;
@@ -329,37 +258,34 @@ static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o
         EXIT;
 }
 
-static int mdt_getstatus(struct mdt_thread_info *info)
+int mdt_getstatus(struct tgt_session_info *tsi)
 {
-        struct mdt_device *mdt  = info->mti_mdt;
-        struct md_device  *next = mdt->mdt_child;
-        struct mdt_body   *repbody;
-        int                rc;
-
-        ENTRY;
+       struct mdt_thread_info  *info = tsi2mdt_info(tsi);
+       struct mdt_device       *mdt = info->mti_mdt;
+       struct mdt_body         *repbody;
+       int                      rc;
 
-        rc = mdt_check_ucred(info);
-        if (rc)
-                RETURN(err_serious(rc));
+       ENTRY;
 
-        if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
-                RETURN(err_serious(-ENOMEM));
+       rc = mdt_check_ucred(info);
+       if (rc)
+               GOTO(out, rc = err_serious(rc));
 
-        repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
-        rc = next->md_ops->mdo_root_get(info->mti_env, next, &repbody->fid1);
-        if (rc != 0)
-                RETURN(rc);
+       if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
+               GOTO(out, rc = err_serious(-ENOMEM));
 
-        repbody->valid |= OBD_MD_FLID;
+       repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
+       repbody->fid1 = mdt->mdt_md_root_fid;
+       repbody->valid |= OBD_MD_FLID;
 
-        if (mdt->mdt_opts.mo_mds_capa &&
-            info->mti_exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
-                struct mdt_object  *root;
-                struct lustre_capa *capa;
+       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);
-                if (IS_ERR(root))
-                        RETURN(PTR_ERR(root));
+               root = mdt_object_find(info->mti_env, mdt, &repbody->fid1);
+               if (IS_ERR(root))
+                       GOTO(out, rc = PTR_ERR(root));
 
                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
                 LASSERT(capa);
@@ -370,13 +296,16 @@ static int mdt_getstatus(struct mdt_thread_info *info)
                 if (rc == 0)
                         repbody->valid |= OBD_MD_FLMDSCAPA;
         }
-
-        RETURN(rc);
+       EXIT;
+out:
+       mdt_thread_info_fini(info);
+       return rc;
 }
 
-static int mdt_statfs(struct mdt_thread_info *info)
+int mdt_statfs(struct tgt_session_info *tsi)
 {
-       struct ptlrpc_request           *req = mdt_info_req(info);
+       struct ptlrpc_request           *req = tgt_ses_req(tsi);
+       struct mdt_thread_info          *info = tsi2mdt_info(tsi);
        struct md_device                *next = info->mti_mdt->mdt_child;
        struct ptlrpc_service_part      *svcpt;
        struct obd_statfs               *osfs;
@@ -384,23 +313,23 @@ static int mdt_statfs(struct mdt_thread_info *info)
 
        ENTRY;
 
-       svcpt = info->mti_pill->rc_req->rq_rqbd->rqbd_svcpt;
+       svcpt = req->rq_rqbd->rqbd_svcpt;
 
        /* This will trigger a watchdog timeout */
        OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
                         (MDT_SERVICE_WATCHDOG_FACTOR *
                          at_get(&svcpt->scp_at_estimate)) + 1);
 
-        rc = mdt_check_ucred(info);
-        if (rc)
-                RETURN(err_serious(rc));
+       rc = mdt_check_ucred(info);
+       if (rc)
+               GOTO(out, rc = err_serious(rc));
 
        if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
-               RETURN(err_serious(-ENOMEM));
+               GOTO(out, rc = err_serious(-ENOMEM));
 
        osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
        if (!osfs)
-               RETURN(-EPROTO);
+               GOTO(out, rc = -EPROTO);
 
        /** statfs information are cached in the mdt_device */
        if (cfs_time_before_64(info->mti_mdt->mdt_osfs_age,
@@ -408,7 +337,7 @@ static int mdt_statfs(struct mdt_thread_info *info)
                /** statfs data is too old, get up-to-date one */
                rc = next->md_ops->mdo_statfs(info->mti_env, next, osfs);
                if (rc)
-                       RETURN(rc);
+                       GOTO(out, rc);
                spin_lock(&info->mti_mdt->mdt_osfs_lock);
                info->mti_mdt->mdt_osfs = *osfs;
                info->mti_mdt->mdt_osfs_age = cfs_time_current_64();
@@ -422,7 +351,8 @@ static int mdt_statfs(struct mdt_thread_info *info)
 
        if (rc == 0)
                mdt_counter_incr(req, LPROC_MDT_STATFS);
-
+out:
+       mdt_thread_info_fini(info);
        RETURN(rc);
 }
 
@@ -487,17 +417,22 @@ 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. */
+               /* 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;
+               else
+                       b->blocks = 1;
+               b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
+       }
 
         if (fid) {
                 b->fid1 = *fid;
                 b->valid |= OBD_MD_FLID;
-
-                /* FIXME: these should be fixed when new igif ready.*/
-                b->ino  =  fid_oid(fid);       /* 1.6 compatibility */
-                b->generation = fid_ver(fid);  /* 1.6 compatibility */
-                b->valid |= OBD_MD_FLGENER;    /* 1.6 compatibility */
-
                 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, size="LPU64"\n",
                                 PFID(fid), b->nlink, b->mode, b->size);
         }
@@ -505,7 +440,7 @@ void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
         if (info)
                 mdt_body_reverse_idmap(info, b);
 
-        if (b->valid & OBD_MD_FLSIZE)
+       if (fid != NULL && (b->valid & OBD_MD_FLSIZE))
                 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
                        PFID(fid), (unsigned long long)b->size);
 }
@@ -526,9 +461,9 @@ void mdt_client_compatibility(struct mdt_thread_info *info)
         struct lu_attr        *la = &ma->ma_attr;
         ENTRY;
 
-        if (exp->exp_connect_flags & OBD_CONNECT_LAYOUTLOCK)
-                /* the client can deal with 16-bit lmm_stripe_count */
-                RETURN_EXIT;
+       if (exp_connect_layout(exp))
+               /* the client can deal with 16-bit lmm_stripe_count */
+               RETURN_EXIT;
 
         body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
 
@@ -783,13 +718,16 @@ static int mdt_getattr_internal(struct mdt_thread_info *info,
 
         ma->ma_valid = 0;
 
-        rc = mdt_object_exists(o);
-        if (rc < 0) {
-                /* This object is located on remote node.*/
-                repbody->fid1 = *mdt_object_fid(o);
-                repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
-                GOTO(out, rc = 0);
-        }
+       if (mdt_object_remote(o)) {
+               /* This object is located on remote node.*/
+               /* Return -EIO for old client */
+               if (!mdt_is_dne_client(req->rq_export))
+                       GOTO(out, rc = -EIO);
+
+               repbody->fid1 = *mdt_object_fid(o);
+               repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
+               GOTO(out, rc = 0);
+       }
 
        buffer->lb_len = reqbody->eadatasize;
        if (buffer->lb_len > 0)
@@ -807,7 +745,7 @@ static int mdt_getattr_internal(struct mdt_thread_info *info,
         } else {
                 ma->ma_lmm = buffer->lb_buf;
                 ma->ma_lmm_size = buffer->lb_len;
-                ma->ma_need = MA_LOV | MA_INODE;
+               ma->ma_need = MA_LOV | MA_INODE | MA_HSM;
         }
 
         if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
@@ -821,18 +759,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;
@@ -846,8 +792,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);
                }
        }
@@ -872,18 +819,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;
@@ -892,8 +841,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;
 
@@ -932,8 +884,8 @@ static int mdt_getattr_internal(struct mdt_thread_info *info,
                 }
         }
 #ifdef CONFIG_FS_POSIX_ACL
-        else if ((req->rq_export->exp_connect_flags & OBD_CONNECT_ACL) &&
-                 (reqbody->valid & OBD_MD_FLACL)) {
+       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);
@@ -947,9 +899,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;
@@ -959,9 +914,9 @@ static int mdt_getattr_internal(struct mdt_thread_info *info,
         }
 #endif
 
-        if (reqbody->valid & OBD_MD_FLMDSCAPA &&
-            info->mti_mdt->mdt_opts.mo_mds_capa &&
-            info->mti_exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
+       if (reqbody->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;
 
                 capa = req_capsule_server_get(pill, &RMF_CAPA1);
@@ -992,9 +947,9 @@ static int mdt_renew_capa(struct mdt_thread_info *info)
          * return directly, client will find body->valid OBD_MD_FLOSSCAPA
          * flag not set.
          */
-        if (!obj || !info->mti_mdt->mdt_opts.mo_oss_capa ||
-            !(info->mti_exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA))
-                RETURN(0);
+       if (!obj || !info->mti_mdt->mdt_lut.lut_oss_capa ||
+           !(exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA))
+               RETURN(0);
 
         body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
         LASSERT(body != NULL);
@@ -1012,8 +967,9 @@ static int mdt_renew_capa(struct mdt_thread_info *info)
         RETURN(rc);
 }
 
-static int mdt_getattr(struct mdt_thread_info *info)
+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;
@@ -1034,19 +990,19 @@ static 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,
-                             reqbody->eadatasize == 0 ?
-                             info->mti_mdt->mdt_max_mdsize :
-                             reqbody->eadatasize);
+       /* 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);
 
-        rc = req_capsule_server_pack(pill);
-        if (unlikely(rc != 0))
-                RETURN(err_serious(rc));
+       rc = req_capsule_server_pack(pill);
+       if (unlikely(rc != 0))
+               GOTO(out, rc = err_serious(rc));
 
         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
         LASSERT(repbody != NULL);
@@ -1076,11 +1032,14 @@ out_shrink:
         rc2 = mdt_fix_reply(info);
         if (rc == 0)
                 rc = rc2;
-        return rc;
+out:
+       mdt_thread_info_fini(info);
+       return rc;
 }
 
-static int mdt_is_subdir(struct mdt_thread_info *info)
+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;
@@ -1092,18 +1051,112 @@ static int mdt_is_subdir(struct mdt_thread_info *info)
 
         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) > 0);
-        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;
+       /*
+        * 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);
+}
 
-        RETURN(rc);
+int mdt_swap_layouts(struct tgt_session_info *tsi)
+{
+       struct mdt_thread_info  *info;
+       struct ptlrpc_request   *req = tgt_ses_req(tsi);
+       struct obd_export       *exp = req->rq_export;
+       struct mdt_object       *o1, *o2, *o;
+       struct mdt_lock_handle  *lh1, *lh2;
+       struct mdc_swap_layouts *msl;
+       int                      rc;
+       ENTRY;
+
+       /* client does not support layout lock, so layout swaping
+        * is disabled.
+        * FIXME: there is a problem for old clients which don't support
+        * layout lock yet. If those clients have already opened the file
+        * they won't be notified at all so that old layout may still be
+        * used to do IO. This can be fixed after file release is landed by
+        * doing exclusive open and taking full EX ibits lock. - Jinshan */
+       if (!exp_connect_layout(exp))
+               RETURN(-EOPNOTSUPP);
+
+       info = tsi2mdt_info(tsi);
+       if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
+               mdt_set_capainfo(info, 0, &info->mti_body->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,
+                                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);
+       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);
+       if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
+               GOTO(put, rc);
+
+       if (rc < 0)
+               swap(o1, o2);
+
+       /* permission check. Make sure the calling process having permission
+        * to write both files. */
+       rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
+                               MAY_WRITE);
+       if (rc < 0)
+               GOTO(put, rc);
+
+       rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
+                               MAY_WRITE);
+       if (rc < 0)
+               GOTO(put, rc);
+
+       msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
+       if (msl == NULL)
+               GOTO(put, rc = -EPROTO);
+
+       lh1 = &info->mti_lh[MDT_LH_NEW];
+       mdt_lock_reg_init(lh1, LCK_EX);
+       lh2 = &info->mti_lh[MDT_LH_OLD];
+       mdt_lock_reg_init(lh2, LCK_EX);
+
+       rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
+                            MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
+       if (rc < 0)
+               GOTO(put, rc);
+
+       rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
+                            MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
+       if (rc < 0)
+               GOTO(unlock1, rc);
+
+       rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
+                            mdt_object_child(o2), msl->msl_flags);
+       GOTO(unlock2, rc);
+unlock2:
+       mdt_object_unlock(info, o2, lh2, rc);
+unlock1:
+       mdt_object_unlock(info, o1, lh1, rc);
+put:
+       mdt_object_put(info->mti_env, o);
+out:
+       mdt_thread_info_fini(info);
+       RETURN(rc);
 }
 
 static int mdt_raw_lookup(struct mdt_thread_info *info,
@@ -1216,14 +1269,14 @@ static int mdt_getattr_name_lock(struct mdt_thread_info *info,
         }
         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
 
-       rc = mdt_object_exists(parent);
-       if (unlikely(rc == 0)) {
+       if (unlikely(!mdt_object_exists(parent)) && lname) {
                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) {
-               LASSERTF(rc > 0, "Parent "DFID" is on remote server\n",
+               LASSERTF(!mdt_object_remote(parent),
+                        "Parent "DFID" is on remote server\n",
                         PFID(mdt_object_fid(parent)));
        }
         if (lname) {
@@ -1260,9 +1313,9 @@ static int mdt_getattr_name_lock(struct mdt_thread_info *info,
                          * needed here but update is.
                          */
                         child_bits &= ~MDS_INODELOCK_LOOKUP;
-                        child_bits |= MDS_INODELOCK_UPDATE;
+                       child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
 
-                        rc = mdt_object_lock(info, child, lhc, child_bits,
+                       rc = mdt_object_lock(info, child, lhc, child_bits,
                                              MDT_LOCAL_LOCK);
                 }
                 if (rc == 0) {
@@ -1278,7 +1331,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,
@@ -1311,51 +1364,49 @@ 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:
                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
                 mdt_lock_handle_init(lhc);
-                if (child_bits == MDS_INODELOCK_LAYOUT)
-                        mdt_lock_reg_init(lhc, LCK_CR);
-                else
-                        mdt_lock_reg_init(lhc, LCK_PR);
+               mdt_lock_reg_init(lhc, LCK_PR);
 
-                if (mdt_object_exists(child) == 0) {
-                        LU_OBJECT_DEBUG(D_INODE, info->mti_env,
-                                        &child->mot_obj.mo_lu,
-                                        "Object doesn't exist!\n");
-                        GOTO(out_child, rc = -ENOENT);
-                }
+               if (!mdt_object_exists(child)) {
+                       LU_OBJECT_DEBUG(D_INODE, info->mti_env,
+                                       &child->mot_obj,
+                                       "Object doesn't exist!\n");
+                       GOTO(out_child, rc = -ENOENT);
+               }
 
-                if (!(child_bits & MDS_INODELOCK_UPDATE)) {
+               if (!(child_bits & MDS_INODELOCK_UPDATE) &&
+                     mdt_object_exists(child) && !mdt_object_remote(child)) {
                         struct md_attr *ma = &info->mti_attr;
 
                         ma->ma_valid = 0;
@@ -1364,17 +1415,11 @@ relock:
                         if (unlikely(rc != 0))
                                 GOTO(out_child, rc);
 
-                        /* layout lock is used only on regular files */
-                        if ((ma->ma_valid & MA_INODE) &&
-                            (ma->ma_attr.la_valid & LA_MODE) &&
-                            !S_ISREG(ma->ma_attr.la_mode))
-                                child_bits &= ~MDS_INODELOCK_LAYOUT;
-
-                        /* If the file has not been changed for some time, we
-                         * return not only a LOOKUP lock, but also an UPDATE
-                         * lock and this might save us RPC on later STAT. For
-                         * directories, it also let negative dentry starts
-                         * working for this dir. */
+                       /* If the file has not been changed for some time, we
+                        * return not only a LOOKUP lock, but also an UPDATE
+                        * lock and this might save us RPC on later STAT. For
+                        * directories, it also let negative dentry cache start
+                        * working for this dir. */
                         if (ma->ma_valid & MA_INODE &&
                             ma->ma_attr.la_valid & LA_CTIME &&
                             info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
@@ -1382,9 +1427,35 @@ relock:
                                 child_bits |= MDS_INODELOCK_UPDATE;
                 }
 
-                rc = mdt_object_lock(info, child, lhc, child_bits,
-                                     MDT_CROSS_LOCK);
+               /* layout lock must be granted in a best-effort way
+                * for IT operations */
+               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)) &&
+                   ldlm_rep != NULL) {
+                       /* try to grant layout lock for regular file. */
+                       try_layout = true;
+               }
 
+               rc = 0;
+               if (try_layout) {
+                       child_bits |= MDS_INODELOCK_LAYOUT;
+                       /* try layout lock, it may fail to be granted due to
+                        * contention at LOOKUP or UPDATE */
+                       if (!mdt_object_lock_try(info, child, lhc, child_bits,
+                                                MDT_CROSS_LOCK)) {
+                               child_bits &= ~MDS_INODELOCK_LAYOUT;
+                               LASSERT(child_bits != 0);
+                               rc = mdt_object_lock(info, child, lhc,
+                                               child_bits, MDT_CROSS_LOCK);
+                       } else {
+                               ma_need |= MA_LOV;
+                       }
+               } else {
+                       rc = mdt_object_lock(info, child, lhc, child_bits,
+                                               MDT_CROSS_LOCK);
+               }
                 if (unlikely(rc != 0))
                         GOTO(out_child, rc);
         }
@@ -1394,25 +1465,24 @@ relock:
         if (lock &&
             lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
             S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
-                ma_need = MA_SOM;
+                ma_need |= MA_SOM;
 
         /* finally, we can get attr for child. */
         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
         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)));
-                mdt_pack_size2body(info, 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);
         }
         if (lock)
                 LDLM_LOCK_PUT(lock);
@@ -1427,8 +1497,9 @@ out_parent:
 }
 
 /* normal handler: should release the child lock */
-static int mdt_getattr_name(struct mdt_thread_info *info)
+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];
         struct mdt_body        *reqbody;
         struct mdt_body        *repbody;
@@ -1460,184 +1531,88 @@ out_shrink:
         rc2 = mdt_fix_reply(info);
         if (rc == 0)
                 rc = rc2;
-        return rc;
-}
-
-static const struct lu_device_operations mdt_lu_ops;
-
-static int lu_device_is_mdt(struct lu_device *d)
-{
-        return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &mdt_lu_ops);
+       mdt_thread_info_fini(info);
+       return rc;
 }
 
 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
                          void *karg, void *uarg);
 
-static int mdt_set_info(struct mdt_thread_info *info)
+int mdt_set_info(struct tgt_session_info *tsi)
 {
-        struct ptlrpc_request *req = mdt_info_req(info);
-        char *key;
-        void *val;
-        int keylen, vallen, rc = 0;
-        ENTRY;
-
-        rc = req_capsule_server_pack(info->mti_pill);
-        if (rc)
-                RETURN(rc);
+       struct ptlrpc_request   *req = tgt_ses_req(tsi);
+       char                    *key;
+       void                    *val;
+       int                      keylen, vallen, rc = 0;
 
-        key = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_KEY);
-        if (key == NULL) {
-                DEBUG_REQ(D_HA, req, "no set_info key");
-                RETURN(-EFAULT);
-        }
+       ENTRY;
 
-        keylen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_KEY,
-                                      RCL_CLIENT);
+       key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
+       if (key == NULL) {
+               DEBUG_REQ(D_HA, req, "no set_info key");
+               RETURN(err_serious(-EFAULT));
+       }
 
-        val = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_VAL);
-        if (val == NULL) {
-                DEBUG_REQ(D_HA, req, "no set_info val");
-                RETURN(-EFAULT);
-        }
+       keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
+                                     RCL_CLIENT);
 
-        vallen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_VAL,
-                                      RCL_CLIENT);
+       val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
+       if (val == NULL) {
+               DEBUG_REQ(D_HA, req, "no set_info val");
+               RETURN(err_serious(-EFAULT));
+       }
 
-        /* Swab any part of val you need to here */
-        if (KEY_IS(KEY_READ_ONLY)) {
-                req->rq_status = 0;
-                lustre_msg_set_status(req->rq_repmsg, 0);
+       vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
+                                     RCL_CLIENT);
 
+       /* Swab any part of val you need to here */
+       if (KEY_IS(KEY_READ_ONLY)) {
                spin_lock(&req->rq_export->exp_lock);
                if (*(__u32 *)val)
-                       req->rq_export->exp_connect_flags |= OBD_CONNECT_RDONLY;
+                       *exp_connect_flags_ptr(req->rq_export) |=
+                               OBD_CONNECT_RDONLY;
                else
-                       req->rq_export->exp_connect_flags &=~OBD_CONNECT_RDONLY;
+                       *exp_connect_flags_ptr(req->rq_export) &=
+                               ~OBD_CONNECT_RDONLY;
                spin_unlock(&req->rq_export->exp_lock);
+       } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
+               struct changelog_setinfo *cs = val;
 
-        } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
-                struct changelog_setinfo *cs =
-                        (struct changelog_setinfo *)val;
-                if (vallen != sizeof(*cs)) {
-                        CERROR("Bad changelog_clear setinfo size %d\n", vallen);
-                        RETURN(-EINVAL);
-                }
-                if (ptlrpc_req_need_swab(req)) {
-                        __swab64s(&cs->cs_recno);
-                        __swab32s(&cs->cs_id);
-                }
-
-                rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, info->mti_exp,
-                                   vallen, val, NULL);
-                lustre_msg_set_status(req->rq_repmsg, rc);
-
-        } else {
-                RETURN(-EINVAL);
-        }
-        RETURN(0);
-}
-
-/**
- * Top-level handler for MDT connection requests.
- */
-static int mdt_connect(struct mdt_thread_info *info)
-{
-       int rc;
-       struct obd_connect_data *reply;
-       struct obd_export *exp;
-       struct ptlrpc_request *req = mdt_info_req(info);
-
-       rc = target_handle_connect(req);
-       if (rc != 0)
-               return err_serious(rc);
+               if (vallen != sizeof(*cs)) {
+                       CERROR("%s: bad changelog_clear setinfo size %d\n",
+                              tgt_name(tsi->tsi_tgt), vallen);
+                       RETURN(-EINVAL);
+               }
+               if (ptlrpc_req_need_swab(req)) {
+                       __swab64s(&cs->cs_recno);
+                       __swab32s(&cs->cs_id);
+               }
 
-       LASSERT(req->rq_export != NULL);
-       info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
-       rc = mdt_init_sec_level(info);
-       if (rc != 0) {
-               obd_disconnect(class_export_get(req->rq_export));
-               return rc;
+               rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
+                                  vallen, val, NULL);
+       } else {
+               RETURN(-EINVAL);
        }
-
-       /* 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->exp_connect_flags = 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));
-
-       return rc;
-}
-
-static int mdt_disconnect(struct mdt_thread_info *info)
-{
-        int rc;
-        ENTRY;
-
-        rc = target_handle_disconnect(mdt_info_req(info));
-        if (rc)
-                rc = err_serious(rc);
-        RETURN(rc);
+       RETURN(rc);
 }
 
-static int mdt_sendpage(struct mdt_thread_info *info,
-                        struct lu_rdpg *rdpg, int nob)
+int mdt_readpage(struct tgt_session_info *tsi)
 {
-        struct ptlrpc_request   *req = mdt_info_req(info);
-        struct obd_export       *exp = req->rq_export;
-        struct ptlrpc_bulk_desc *desc;
-        struct l_wait_info      *lwi = &info->mti_u.rdpg.mti_wait_info;
-        int                      tmpcount;
-        int                      tmpsize;
-        int                      i;
-        int                      rc;
-        ENTRY;
-
-        desc = ptlrpc_prep_bulk_exp(req, rdpg->rp_npages, BULK_PUT_SOURCE,
-                                    MDS_BULK_PORTAL);
-        if (desc == NULL)
-                RETURN(-ENOMEM);
-
-       if (!(exp->exp_connect_flags & OBD_CONNECT_BRW_SIZE))
-               /* old client requires reply size in it's PAGE_SIZE,
-                * which is rdpg->rp_count */
-               nob = rdpg->rp_count;
-
-       for (i = 0, tmpcount = nob; i < rdpg->rp_npages && tmpcount > 0;
-            i++, tmpcount -= tmpsize) {
-                tmpsize = min_t(int, tmpcount, CFS_PAGE_SIZE);
-               ptlrpc_prep_bulk_page_pin(desc, rdpg->rp_pages[i], 0, tmpsize);
-        }
-
-        LASSERT(desc->bd_nob == nob);
-        rc = target_bulk_io(exp, desc, lwi);
-       ptlrpc_free_bulk_pin(desc);
-        RETURN(rc);
-}
+       struct mdt_thread_info  *info = mdt_th_info(tsi->tsi_env);
+       struct mdt_object       *object = mdt_obj(tsi->tsi_corpus);
+       struct lu_rdpg          *rdpg = &info->mti_u.rdpg.mti_rdpg;
+       const struct mdt_body   *reqbody = tsi->tsi_mdt_body;
+       struct mdt_body         *repbody;
+       int                      rc;
+       int                      i;
 
-static int mdt_readpage(struct mdt_thread_info *info)
-{
-        struct mdt_object *object = info->mti_object;
-        struct lu_rdpg    *rdpg = &info->mti_u.rdpg.mti_rdpg;
-        struct mdt_body   *reqbody;
-        struct mdt_body   *repbody;
-        int                rc;
-        int                i;
-        ENTRY;
+       ENTRY;
 
-        if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
-                RETURN(err_serious(-ENOMEM));
+       if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
+               RETURN(err_serious(-ENOMEM));
 
-        reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
-        repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
-        if (reqbody == NULL || repbody == NULL)
+       repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
+       if (repbody == NULL || reqbody == NULL)
                 RETURN(err_serious(-EFAULT));
 
         /*
@@ -1653,53 +1628,53 @@ static int mdt_readpage(struct mdt_thread_info *info)
         }
 
         rdpg->rp_attrs = reqbody->mode;
-        if (info->mti_exp->exp_connect_flags & OBD_CONNECT_64BITHASH)
-                rdpg->rp_attrs |= LUDA_64BITHASH;
-        rdpg->rp_count  = min_t(unsigned int, reqbody->nlink,
-                                PTLRPC_MAX_BRW_SIZE);
-        rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE - 1) >>
-                          CFS_PAGE_SHIFT;
+       if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
+               rdpg->rp_attrs |= LUDA_64BITHASH;
+       rdpg->rp_count  = min_t(unsigned int, reqbody->nlink,
+                               exp_max_brw_size(tsi->tsi_exp));
+       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);
         }
 
         /* call lower layers to fill allocated pages with directory data */
-        rc = mo_readpage(info->mti_env, mdt_object_child(object), rdpg);
-        if (rc < 0)
-                GOTO(free_rdpg, rc);
+       rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
+       if (rc < 0)
+               GOTO(free_rdpg, rc);
 
-        /* send pages to client */
-        rc = mdt_sendpage(info, rdpg, rc);
+       /* send pages to client */
+       rc = tgt_sendpage(tsi, rdpg, rc);
 
-        EXIT;
+       EXIT;
 free_rdpg:
 
-        for (i = 0; i < rdpg->rp_npages; i++)
-                if (rdpg->rp_pages[i] != NULL)
-                        cfs_free_page(rdpg->rp_pages[i]);
-        OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
+       for (i = 0; i < rdpg->rp_npages; i++)
+               if (rdpg->rp_pages[i] != NULL)
+                       __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))
-                RETURN(0);
+       if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
+               RETURN(0);
 
-        return rc;
+       return rc;
 }
 
 static int mdt_reint_internal(struct mdt_thread_info *info,
                               struct mdt_lock_handle *lhc,
                               __u32 op)
 {
-        struct req_capsule      *pill = info->mti_pill;
-        struct mdt_body         *repbody;
-        int                      rc = 0, rc2;
-        ENTRY;
+       struct req_capsule      *pill = info->mti_pill;
+       struct mdt_body         *repbody;
+       int                      rc = 0, rc2;
 
+       ENTRY;
 
         rc = mdt_reint_unpack(info, op);
         if (rc != 0) {
@@ -1760,57 +1735,64 @@ out_shrink:
         return rc;
 }
 
-static long mdt_reint_opcode(struct mdt_thread_info *info,
-                             const struct req_format **fmt)
-{
-        struct mdt_rec_reint *rec;
-        long opc;
-
-        opc = err_serious(-EFAULT);
-        rec = req_capsule_client_get(info->mti_pill, &RMF_REC_REINT);
-        if (rec != NULL) {
-                opc = rec->rr_opcode;
-                DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
-                if (opc < REINT_MAX && fmt[opc] != NULL)
-                        req_capsule_extend(info->mti_pill, fmt[opc]);
-                else {
-                        CERROR("Unsupported opc: %ld\n", opc);
-                        opc = err_serious(opc);
-                }
-        }
-        return opc;
-}
-
-static int mdt_reint(struct mdt_thread_info *info)
-{
-        long opc;
-        int  rc;
-
-        static const struct req_format *reint_fmts[REINT_MAX] = {
-                [REINT_SETATTR]  = &RQF_MDS_REINT_SETATTR,
-                [REINT_CREATE]   = &RQF_MDS_REINT_CREATE,
-                [REINT_LINK]     = &RQF_MDS_REINT_LINK,
-                [REINT_UNLINK]   = &RQF_MDS_REINT_UNLINK,
-                [REINT_RENAME]   = &RQF_MDS_REINT_RENAME,
-                [REINT_OPEN]     = &RQF_MDS_REINT_OPEN,
-                [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR
-        };
+static long mdt_reint_opcode(struct ptlrpc_request *req,
+                            const struct req_format **fmt)
+{
+       struct mdt_device       *mdt;
+       struct mdt_rec_reint    *rec;
+       long                     opc;
+
+       rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
+       if (rec != NULL) {
+               opc = rec->rr_opcode;
+               DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
+               if (opc < REINT_MAX && fmt[opc] != NULL)
+                       req_capsule_extend(&req->rq_pill, fmt[opc]);
+               else {
+                       mdt = mdt_exp2dev(req->rq_export);
+                       CERROR("%s: Unsupported opcode '%ld' from client '%s':"
+                              " rc = %d\n", req->rq_export->exp_obd->obd_name,
+                              opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
+                       opc = err_serious(-EFAULT);
+               }
+       } else {
+               opc = err_serious(-EFAULT);
+       }
+       return opc;
+}
+
+int mdt_reint(struct tgt_session_info *tsi)
+{
+       long opc;
+       int  rc;
+       static const struct req_format *reint_fmts[REINT_MAX] = {
+               [REINT_SETATTR]  = &RQF_MDS_REINT_SETATTR,
+               [REINT_CREATE]   = &RQF_MDS_REINT_CREATE,
+               [REINT_LINK]     = &RQF_MDS_REINT_LINK,
+               [REINT_UNLINK]   = &RQF_MDS_REINT_UNLINK,
+               [REINT_RENAME]   = &RQF_MDS_REINT_RENAME,
+               [REINT_OPEN]     = &RQF_MDS_REINT_OPEN,
+               [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
+               [REINT_RMENTRY]  = &RQF_MDS_REINT_UNLINK
+       };
 
-        ENTRY;
+       ENTRY;
 
-        opc = mdt_reint_opcode(info, reint_fmts);
-        if (opc >= 0) {
-                /*
-                 * No lock possible here from client to pass it to reint code
-                 * path.
-                 */
-                rc = mdt_reint_internal(info, NULL, opc);
-        } else {
-                rc = opc;
-        }
+       opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
+       if (opc >= 0) {
+               struct mdt_thread_info *info = tsi2mdt_info(tsi);
+               /*
+                * No lock possible here from client to pass it to reint code
+                * path.
+                */
+               rc = mdt_reint_internal(info, NULL, opc);
+               mdt_thread_info_fini(info);
+       } else {
+               rc = opc;
+       }
 
-        info->mti_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
-        RETURN(rc);
+       tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
+       RETURN(rc);
 }
 
 /* this should sync the whole device */
@@ -1842,93 +1824,60 @@ static int mdt_object_sync(struct mdt_thread_info *info)
         RETURN(rc);
 }
 
-static int mdt_sync(struct mdt_thread_info *info)
+int mdt_sync(struct tgt_session_info *tsi)
 {
-        struct ptlrpc_request *req = mdt_info_req(info);
-        struct req_capsule *pill = info->mti_pill;
-        struct mdt_body *body;
-        int rc;
-        ENTRY;
-
-        /* The fid may be zero, so we req_capsule_set manually */
-        req_capsule_set(pill, &RQF_MDS_SYNC);
+       struct ptlrpc_request   *req = tgt_ses_req(tsi);
+       struct req_capsule      *pill = tsi->tsi_pill;
+       struct mdt_body         *body;
+       int                      rc;
 
-        body = req_capsule_client_get(pill, &RMF_MDT_BODY);
-        if (body == NULL)
-                RETURN(err_serious(-EINVAL));
+       ENTRY;
 
-        if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
-                RETURN(err_serious(-ENOMEM));
+       if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
+               RETURN(err_serious(-ENOMEM));
 
-        if (fid_seq(&body->fid1) == 0) {
-                /* sync the whole device */
-                rc = req_capsule_server_pack(pill);
-                if (rc == 0)
-                        rc = mdt_device_sync(info->mti_env, info->mti_mdt);
-                else
-                        rc = err_serious(rc);
-        } else {
-                /* sync an object */
-                rc = mdt_unpack_req_pack_rep(info, HABEO_CORPUS|HABEO_REFERO);
-                if (rc == 0) {
-                        rc = mdt_object_sync(info);
-                        if (rc == 0) {
-                                const struct lu_fid *fid;
-                                struct lu_attr *la = &info->mti_attr.ma_attr;
-
-                                info->mti_attr.ma_need = MA_INODE;
-                                info->mti_attr.ma_valid = 0;
-                               rc = mdt_attr_get_complex(info, info->mti_object,
-                                                         &info->mti_attr);
-                                if (rc == 0) {
-                                        body = req_capsule_server_get(pill,
-                                                                &RMF_MDT_BODY);
-                                        fid = mdt_object_fid(info->mti_object);
-                                        mdt_pack_attr2body(info, body, la, fid);
-                                }
-                        }
-                } else
-                        rc = err_serious(rc);
-        }
-        if (rc == 0)
+       if (fid_seq(&tsi->tsi_mdt_body->fid1) == 0) {
+               rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
+       } else {
+               struct mdt_thread_info *info = tsi2mdt_info(tsi);
+
+               /* sync an object */
+               rc = mdt_object_sync(info);
+               if (rc == 0) {
+                       const struct lu_fid *fid;
+                       struct lu_attr *la = &info->mti_attr.ma_attr;
+
+                       info->mti_attr.ma_need = MA_INODE;
+                       info->mti_attr.ma_valid = 0;
+                       rc = mdt_attr_get_complex(info, info->mti_object,
+                                                 &info->mti_attr);
+                       if (rc == 0) {
+                               body = req_capsule_server_get(pill,
+                                                             &RMF_MDT_BODY);
+                               fid = mdt_object_fid(info->mti_object);
+                               mdt_pack_attr2body(info, body, la, fid);
+                       }
+               }
+               mdt_thread_info_fini(info);
+       }
+       if (rc == 0)
                mdt_counter_incr(req, LPROC_MDT_SYNC);
 
-        RETURN(rc);
-}
-
-/*
- * Quotacheck handler.
- * in-kernel quotacheck isn't supported any more.
- */
-static int mdt_quotacheck(struct mdt_thread_info *info)
-{
-       struct obd_quotactl     *oqctl;
-       int                      rc;
-       ENTRY;
-
-       oqctl = req_capsule_client_get(info->mti_pill, &RMF_OBD_QUOTACTL);
-       if (oqctl == NULL)
-               RETURN(err_serious(-EPROTO));
-
-       rc = req_capsule_server_pack(info->mti_pill);
-       if (rc)
-               RETURN(err_serious(rc));
-
-       /* deprecated, not used any more */
-       RETURN(-EOPNOTSUPP);
+       RETURN(rc);
 }
 
 /*
  * Handle quota control requests to consult current usage/limit, but also
  * to configure quota enforcement
  */
-static int mdt_quotactl(struct mdt_thread_info *info)
+int mdt_quotactl(struct tgt_session_info *tsi)
 {
-       struct obd_export       *exp  = info->mti_exp;
-       struct req_capsule      *pill = info->mti_pill;
+       struct obd_export       *exp  = tsi->tsi_exp;
+       struct req_capsule      *pill = tsi->tsi_pill;
        struct obd_quotactl     *oqctl, *repoqc;
        int                      id, rc;
-       struct lu_device        *qmt = info->mti_mdt->mdt_qmt_dev;
+       struct mdt_device       *mdt = mdt_exp2dev(exp);
+       struct lu_device        *qmt = mdt->mdt_qmt_dev;
        ENTRY;
 
        oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
@@ -1969,7 +1918,7 @@ static int mdt_quotactl(struct mdt_thread_info *info)
        if (exp_connect_rmtclient(exp)) {
                struct lustre_idmap_table *idmap;
 
-               idmap = mdt_req2med(mdt_info_req(info))->med_idmap;
+               idmap = exp->exp_mdt_data.med_idmap;
 
                if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
                             oqctl->qc_cmd != Q_GETINFO))
@@ -2004,13 +1953,13 @@ static int mdt_quotactl(struct mdt_thread_info *info)
        case Q_SETQUOTA:
        case Q_GETQUOTA:
                /* forward quotactl request to QMT */
-               rc = qmt_hdls.qmth_quotactl(info->mti_env, qmt, oqctl);
+               rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
                break;
 
        case Q_GETOINFO:
        case Q_GETOQUOTA:
                /* slave quotactl */
-               rc = lquotactl_slv(info->mti_env, info->mti_mdt->mdt_bottom,
+               rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
                                   oqctl);
                break;
 
@@ -2026,120 +1975,6 @@ static int mdt_quotactl(struct mdt_thread_info *info)
        RETURN(rc);
 }
 
-/*
- * OBD PING and other handlers.
- */
-static int mdt_obd_ping(struct mdt_thread_info *info)
-{
-        int rc;
-        ENTRY;
-
-        req_capsule_set(info->mti_pill, &RQF_OBD_PING);
-
-        rc = target_handle_ping(mdt_info_req(info));
-        if (rc < 0)
-                rc = err_serious(rc);
-        RETURN(rc);
-}
-
-/*
- * OBD_IDX_READ handler
- */
-static int mdt_obd_idx_read(struct mdt_thread_info *info)
-{
-       struct mdt_device       *mdt = info->mti_mdt;
-       struct lu_rdpg          *rdpg = &info->mti_u.rdpg.mti_rdpg;
-       struct idx_info         *req_ii, *rep_ii;
-       int                      rc, i;
-       ENTRY;
-
-       memset(rdpg, 0, sizeof(*rdpg));
-       req_capsule_set(info->mti_pill, &RQF_OBD_IDX_READ);
-
-       /* extract idx_info buffer from request & reply */
-       req_ii = req_capsule_client_get(info->mti_pill, &RMF_IDX_INFO);
-       if (req_ii == NULL || req_ii->ii_magic != IDX_INFO_MAGIC)
-               RETURN(err_serious(-EPROTO));
-
-       rc = req_capsule_server_pack(info->mti_pill);
-       if (rc)
-               RETURN(err_serious(rc));
-
-       rep_ii = req_capsule_server_get(info->mti_pill, &RMF_IDX_INFO);
-       if (rep_ii == NULL)
-               RETURN(err_serious(-EFAULT));
-       rep_ii->ii_magic = IDX_INFO_MAGIC;
-
-       /* extract hash to start with */
-       rdpg->rp_hash = req_ii->ii_hash_start;
-
-       /* extract requested attributes */
-       rdpg->rp_attrs = req_ii->ii_attrs;
-
-       /* check that fid packed in request is valid and supported */
-       if (!fid_is_sane(&req_ii->ii_fid))
-               RETURN(-EINVAL);
-       rep_ii->ii_fid = req_ii->ii_fid;
-
-       /* copy flags */
-       rep_ii->ii_flags = req_ii->ii_flags;
-
-       /* compute number of pages to allocate, ii_count is the number of 4KB
-        * containers */
-       if (req_ii->ii_count <= 0)
-               GOTO(out, rc = -EFAULT);
-       rdpg->rp_count = min_t(unsigned int, req_ii->ii_count << LU_PAGE_SHIFT,
-                              PTLRPC_MAX_BRW_SIZE);
-       rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE -1) >> CFS_PAGE_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);
-               if (rdpg->rp_pages[i] == NULL)
-                       GOTO(out, rc = -ENOMEM);
-       }
-
-       /* populate pages with key/record pairs */
-       rc = dt_index_read(info->mti_env, mdt->mdt_bottom, rep_ii, rdpg);
-       if (rc < 0)
-               GOTO(out, rc);
-
-       LASSERTF(rc <= rdpg->rp_count, "dt_index_read() returned more than "
-                "asked %d > %d\n", rc, rdpg->rp_count);
-
-       /* send pages to client */
-       rc = mdt_sendpage(info, rdpg, rc);
-
-       GOTO(out, rc);
-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]);
-               OBD_FREE(rdpg->rp_pages,
-                        rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
-       }
-       return rc;
-}
-
-static int mdt_obd_log_cancel(struct mdt_thread_info *info)
-{
-        return err_serious(-EOPNOTSUPP);
-}
-
-static int mdt_obd_qc_callback(struct mdt_thread_info *info)
-{
-        return err_serious(-EOPNOTSUPP);
-}
-
-
-/*
- * LLOG handlers.
- */
-
 /** clone llog ctxt from child (mdd)
  * This allows remote llog (replicator) access.
  * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
@@ -2184,149 +2019,45 @@ static int mdt_llog_ctxt_unclone(const struct lu_env *env,
         return 0;
 }
 
-static int mdt_llog_create(struct mdt_thread_info *info)
-{
-       int rc;
-
-       req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
-       rc = llog_origin_handle_open(mdt_info_req(info));
-       return (rc < 0 ? err_serious(rc) : rc);
-}
-
-static int mdt_llog_destroy(struct mdt_thread_info *info)
-{
-        int rc;
-
-        req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_DESTROY);
-        rc = llog_origin_handle_destroy(mdt_info_req(info));
-        return (rc < 0 ? err_serious(rc) : rc);
-}
-
-static int mdt_llog_read_header(struct mdt_thread_info *info)
-{
-        int rc;
-
-        req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
-        rc = llog_origin_handle_read_header(mdt_info_req(info));
-        return (rc < 0 ? err_serious(rc) : rc);
-}
-
-static int mdt_llog_next_block(struct mdt_thread_info *info)
-{
-        int rc;
-
-        req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
-        rc = llog_origin_handle_next_block(mdt_info_req(info));
-        return (rc < 0 ? err_serious(rc) : rc);
-}
-
-static int mdt_llog_prev_block(struct mdt_thread_info *info)
-{
-        int rc;
-
-        req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK);
-        rc = llog_origin_handle_prev_block(mdt_info_req(info));
-        return (rc < 0 ? err_serious(rc) : rc);
-}
-
-
-/*
- * DLM handlers.
- */
-static struct ldlm_callback_suite cbs = {
-       .lcs_completion = ldlm_server_completion_ast,
-       .lcs_blocking   = ldlm_server_blocking_ast,
-       .lcs_glimpse    = ldlm_server_glimpse_ast
-};
-
-static int mdt_enqueue(struct mdt_thread_info *info)
-{
-        struct ptlrpc_request *req;
-        int rc;
-
-        /*
-         * info->mti_dlm_req already contains swapped and (if necessary)
-         * converted dlm request.
-         */
-        LASSERT(info->mti_dlm_req != NULL);
-
-        req = mdt_info_req(info);
-        rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
-                                  req, info->mti_dlm_req, &cbs);
-        info->mti_fail_id = OBD_FAIL_LDLM_REPLY;
-        return rc ? err_serious(rc) : req->rq_status;
-}
-
-static int mdt_convert(struct mdt_thread_info *info)
-{
-        int rc;
-        struct ptlrpc_request *req;
-
-        LASSERT(info->mti_dlm_req);
-        req = mdt_info_req(info);
-        rc = ldlm_handle_convert0(req, info->mti_dlm_req);
-        return rc ? err_serious(rc) : req->rq_status;
-}
-
-static int mdt_bl_callback(struct mdt_thread_info *info)
-{
-        CERROR("bl callbacks should not happen on MDS\n");
-        LBUG();
-        return err_serious(-EOPNOTSUPP);
-}
-
-static int mdt_cp_callback(struct mdt_thread_info *info)
-{
-        CERROR("cp callbacks should not happen on MDS\n");
-        LBUG();
-        return err_serious(-EOPNOTSUPP);
-}
-
 /*
  * sec context handlers
  */
-static int mdt_sec_ctx_handle(struct mdt_thread_info *info)
+int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
 {
-        int rc;
-
-        rc = mdt_handle_idmap(info);
+       int rc;
 
-        if (unlikely(rc)) {
-                struct ptlrpc_request *req = mdt_info_req(info);
-                __u32                  opc;
+       rc = mdt_handle_idmap(tsi);
+       if (unlikely(rc)) {
+               struct ptlrpc_request   *req = tgt_ses_req(tsi);
+               __u32                    opc;
 
-                opc = lustre_msg_get_opc(req->rq_reqmsg);
-                if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
-                        sptlrpc_svc_ctx_invalidate(req);
-        }
+               opc = lustre_msg_get_opc(req->rq_reqmsg);
+               if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
+                       sptlrpc_svc_ctx_invalidate(req);
+       }
 
-        CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
+       CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
 
-        return rc;
+       return rc;
 }
 
 /*
  * quota request handlers
  */
-static int mdt_quota_dqacq(struct mdt_thread_info *info)
+int mdt_quota_dqacq(struct tgt_session_info *tsi)
 {
-       struct lu_device        *qmt = info->mti_mdt->mdt_qmt_dev;
+       struct mdt_device       *mdt = mdt_exp2dev(tsi->tsi_exp);
+       struct lu_device        *qmt = mdt->mdt_qmt_dev;
        int                      rc;
        ENTRY;
 
        if (qmt == NULL)
                RETURN(err_serious(-EOPNOTSUPP));
 
-       rc = qmt_hdls.qmth_dqacq(info->mti_env, qmt, mdt_info_req(info));
+       rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
        RETURN(rc);
 }
 
-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);
-}
-
 struct mdt_object *mdt_object_new(const struct lu_env *env,
                                  struct mdt_device *d,
                                  const struct lu_fid *f)
@@ -2337,7 +2068,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
@@ -2346,20 +2077,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);
 }
 
 /**
@@ -2464,12 +2196,65 @@ int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
         RETURN(rc);
 }
 
-int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
-                    struct mdt_lock_handle *lh, __u64 ibits, int locality)
+int mdt_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
+                       void *data, int flag)
+{
+       struct lustre_handle lockh;
+       int               rc;
+
+       switch (flag) {
+       case LDLM_CB_BLOCKING:
+               ldlm_lock2handle(lock, &lockh);
+               rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
+               if (rc < 0) {
+                       CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
+                       RETURN(rc);
+               }
+               break;
+       case LDLM_CB_CANCELING:
+               LDLM_DEBUG(lock, "Revoke remote lock\n");
+               break;
+       default:
+               LBUG();
+       }
+       RETURN(0);
+}
+
+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 ldlm_enqueue_info *einfo = &mti->mti_einfo;
+       ldlm_policy_data_t *policy = &mti->mti_policy;
+       int rc = 0;
+       ENTRY;
+
+       LASSERT(mdt_object_remote(o));
+
+       LASSERT(ibits & MDS_INODELOCK_UPDATE);
+
+       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_cp = ldlm_completion_ast;
+
+       memset(policy, 0, sizeof(*policy));
+       policy->l_inodebits.bits = ibits;
+
+       rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
+                           policy);
+       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)
 {
         struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
         ldlm_policy_data_t *policy = &info->mti_policy;
         struct ldlm_res_id *res_id = &info->mti_res_id;
+       __u64 dlmflags;
         int rc;
         ENTRY;
 
@@ -2478,34 +2263,41 @@ int mdt_object_lock(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_exists(o) < 0) {
+       if (mdt_object_remote(o)) {
                 if (locality == MDT_CROSS_LOCK) {
-                        /* cross-ref object fix */
-                        ibits &= ~MDS_INODELOCK_UPDATE;
+                       ibits &= ~(MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM);
                         ibits |= MDS_INODELOCK_LOOKUP;
                 } else {
-                        LASSERT(!(ibits & MDS_INODELOCK_UPDATE));
+                       LASSERTF(!(ibits &
+                                 (MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM)),
+                               "%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);
         }
 
-        if (lh->mlh_type == MDT_PDO_LOCK) {
+       if (lh->mlh_type == MDT_PDO_LOCK) {
                 /* check for exists after object is locked */
                 if (mdt_object_exists(o) == 0) {
                         /* Non-existent object shouldn't have PDO lock */
                         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);
                 }
         }
 
         memset(policy, 0, sizeof(*policy));
         fid_build_reg_res_name(mdt_object_fid(o), res_id);
 
+       dlmflags = LDLM_FL_ATOMIC_CB;
+       if (nonblock)
+               dlmflags |= LDLM_FL_BLOCK_NOWAIT;
+
         /*
          * Take PDO lock on whole directory and build correct @res_id for lock
          * on part of directory.
@@ -2521,7 +2313,7 @@ int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
                          */
                         policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
                         rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
-                                          policy, res_id, LDLM_FL_ATOMIC_CB,
+                                          policy, res_id, dlmflags,
                                           &info->mti_exp->exp_handle.h_cookie);
                         if (unlikely(rc))
                                 RETURN(rc);
@@ -2542,7 +2334,7 @@ int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
          * 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 | LDLM_FL_ATOMIC_CB,
+                          res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
                           &info->mti_exp->exp_handle.h_cookie);
         if (rc)
                 mdt_object_unlock(info, o, lh, 1);
@@ -2555,6 +2347,25 @@ int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
         RETURN(rc);
 }
 
+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);
+}
+
+int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
+                       struct mdt_lock_handle *lh, __u64 ibits, int locality)
+{
+       struct mdt_lock_handle tmp = *lh;
+       int rc;
+
+       rc = mdt_object_lock0(info, o, &tmp, ibits, true, locality);
+       if (rc == 0)
+               *lh = tmp;
+
+       return rc == 0;
+}
+
 /**
  * Save a lock within request object.
  *
@@ -2629,6 +2440,9 @@ void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
         mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
         mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
 
+       if (lustre_handle_is_used(&lh->mlh_rreg_lh))
+               ldlm_lock_decref(&lh->mlh_rreg_lh, lh->mlh_rreg_mode);
+
         EXIT;
 }
 
@@ -2662,41 +2476,6 @@ void mdt_object_unlock_put(struct mdt_thread_info * info,
         mdt_object_put(info->mti_env, o);
 }
 
-static struct mdt_handler *mdt_handler_find(__u32 opc,
-                                            struct mdt_opc_slice *supported)
-{
-        struct mdt_opc_slice *s;
-        struct mdt_handler   *h;
-
-        h = NULL;
-        for (s = supported; s->mos_hs != NULL; s++) {
-                if (s->mos_opc_start <= opc && opc < s->mos_opc_end) {
-                        h = s->mos_hs + (opc - s->mos_opc_start);
-                        if (likely(h->mh_opc != 0))
-                                LASSERTF(h->mh_opc == opc,
-                                         "opcode mismatch %d != %d\n",
-                                         h->mh_opc, opc);
-                        else
-                                h = NULL; /* unsupported opc */
-                        break;
-                }
-        }
-        return h;
-}
-
-static int mdt_lock_resname_compat(struct mdt_device *m,
-                                   struct ldlm_request *req)
-{
-        /* XXX something... later. */
-        return 0;
-}
-
-static int mdt_lock_reply_compat(struct mdt_device *m, struct ldlm_reply *rep)
-{
-        /* XXX something... later. */
-        return 0;
-}
-
 /*
  * Generic code handling requests that have struct mdt_body passed in:
  *
@@ -2791,150 +2570,13 @@ static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
 
 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
 {
-        struct md_device *next = m->mdt_child;
-
-        return next->md_ops->mdo_init_capa_ctxt(env, next,
-                                                m->mdt_opts.mo_mds_capa,
-                                                m->mdt_capa_timeout,
-                                                m->mdt_capa_alg,
-                                                m->mdt_capa_keys);
-}
-
-/*
- * Invoke handler for this request opc. Also do necessary preprocessing
- * (according to handler ->mh_flags), and post-processing (setting of
- * ->last_{xid,committed}).
- */
-static int mdt_req_handle(struct mdt_thread_info *info,
-                          struct mdt_handler *h, struct ptlrpc_request *req)
-{
-        int   rc, serious = 0;
-        __u32 flags;
-
-        ENTRY;
-
-        LASSERT(h->mh_act != NULL);
-        LASSERT(h->mh_opc == lustre_msg_get_opc(req->rq_reqmsg));
-        LASSERT(current->journal_info == NULL);
-
-        /*
-         * Checking for various OBD_FAIL_$PREF_$OPC_NET codes. _Do_ not try
-         * to put same checks into handlers like mdt_close(), mdt_reint(),
-         * etc., without talking to mdt authors first. Checking same thing
-         * there again is useless and returning 0 error without packing reply
-         * is buggy! Handlers either pack reply or return error.
-         *
-         * We return 0 here and do not send any reply in order to emulate
-         * network failure. Do not send any reply in case any of NET related
-         * fail_id has occured.
-         */
-        if (OBD_FAIL_CHECK_ORSET(h->mh_fail_id, OBD_FAIL_ONCE))
-                RETURN(0);
-
-        rc = 0;
-        flags = h->mh_flags;
-        LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
-
-        if (h->mh_fmt != NULL) {
-                req_capsule_set(info->mti_pill, h->mh_fmt);
-                rc = mdt_unpack_req_pack_rep(info, flags);
-        }
-
-        if (rc == 0 && flags & MUTABOR &&
-            req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
-                /* should it be rq_status? */
-                rc = -EROFS;
-
-        if (rc == 0 && flags & HABEO_CLAVIS) {
-                struct ldlm_request *dlm_req;
-
-                LASSERT(h->mh_fmt != NULL);
-
-                dlm_req = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
-                if (dlm_req != NULL) {
-                        if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
-                                        LDLM_IBITS &&
-                                     dlm_req->lock_desc.l_policy_data.\
-                                        l_inodebits.bits == 0)) {
-                                /*
-                                 * Lock without inodebits makes no sense and
-                                 * will oops later in ldlm. If client miss to
-                                 * set such bits, do not trigger ASSERTION.
-                                 *
-                                 * For liblustre flock case, it maybe zero.
-                                 */
-                                rc = -EPROTO;
-                        } else {
-                                if (info->mti_mdt->mdt_opts.mo_compat_resname)
-                                        rc = mdt_lock_resname_compat(
-                                                                info->mti_mdt,
-                                                                dlm_req);
-                                info->mti_dlm_req = dlm_req;
-                        }
-                } else {
-                        rc = -EFAULT;
-                }
-        }
-
-        /* capability setting changed via /proc, needs reinitialize ctxt */
-        if (info->mti_mdt && info->mti_mdt->mdt_capa_conf) {
-                mdt_init_capa_ctxt(info->mti_env, info->mti_mdt);
-                info->mti_mdt->mdt_capa_conf = 0;
-        }
-
-        if (likely(rc == 0)) {
-                /*
-                 * Process request, there can be two types of rc:
-                 * 1) errors with msg unpack/pack, other failures outside the
-                 * operation itself. This is counted as serious errors;
-                 * 2) errors during fs operation, should be placed in rq_status
-                 * only
-                 */
-                rc = h->mh_act(info);
-                if (rc == 0 &&
-                    !req->rq_no_reply && req->rq_reply_state == NULL) {
-                        DEBUG_REQ(D_ERROR, req, "MDT \"handler\" %s did not "
-                                  "pack reply and returned 0 error\n",
-                                  h->mh_name);
-                        LBUG();
-                }
-                serious = is_serious(rc);
-                rc = clear_serious(rc);
-        } else
-                serious = 1;
-
-        req->rq_status = rc;
+       struct md_device *next = m->mdt_child;
 
-        /*
-         * ELDLM_* codes which > 0 should be in rq_status only as well as
-         * all non-serious errors.
-         */
-        if (rc > 0 || !serious)
-                rc = 0;
-
-        LASSERT(current->journal_info == NULL);
-
-        if (rc == 0 && (flags & HABEO_CLAVIS) &&
-            info->mti_mdt->mdt_opts.mo_compat_resname) {
-                struct ldlm_reply *dlmrep;
-
-                dlmrep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
-                if (dlmrep != NULL)
-                        rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
-        }
-
-        /* If we're DISCONNECTing, the mdt_export_data is already freed */
-        if (likely(rc == 0 && req->rq_export && h->mh_opc != MDS_DISCONNECT))
-                target_committed_to_req(req);
-
-        if (unlikely(req_is_replay(req) &&
-                     lustre_msg_get_transno(req->rq_reqmsg) == 0)) {
-                DEBUG_REQ(D_ERROR, req, "transno is 0 during REPLAY");
-                LBUG();
-        }
-
-        target_send_reply(req, rc, info->mti_fail_id);
-        RETURN(0);
+       return next->md_ops->mdo_init_capa_ctxt(env, next,
+                                               m->mdt_lut.lut_mds_capa,
+                                               m->mdt_capa_timeout,
+                                               m->mdt_capa_alg,
+                                               m->mdt_capa_keys);
 }
 
 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
@@ -2944,6 +2586,8 @@ void mdt_lock_handle_init(struct mdt_lock_handle *lh)
         lh->mlh_reg_mode = LCK_MINMODE;
         lh->mlh_pdo_lh.cookie = 0ull;
         lh->mlh_pdo_mode = LCK_MINMODE;
+       lh->mlh_rreg_lh.cookie = 0ull;
+       lh->mlh_rreg_mode = LCK_MINMODE;
 }
 
 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
@@ -2957,13 +2601,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;
-        struct md_capainfo *ci;
 
-        req_capsule_init(&req->rq_pill, req, RCL_SERVER);
         info->mti_pill = &req->rq_pill;
 
         /* lock handle */
@@ -2976,25 +2618,13 @@ static void mdt_thread_info_init(struct ptlrpc_request *req,
                 info->mti_exp = req->rq_export;
         } else
                 info->mti_mdt = NULL;
-        info->mti_env = req->rq_svc_thread->t_env;
-        ci = md_capainfo(info->mti_env);
-        memset(ci, 0, sizeof *ci);
-        if (req->rq_export) {
-                if (exp_connect_rmtclient(req->rq_export))
-                        ci->mc_auth = LC_ID_CONVERT;
-                else if (req->rq_export->exp_connect_flags &
-                         OBD_CONNECT_MDS_CAPA)
-                        ci->mc_auth = LC_ID_PLAIN;
-                else
-                        ci->mc_auth = LC_ID_NONE;
-        }
-
-        info->mti_fail_id = OBD_FAIL_MDS_ALL_REPLY_NET;
-        info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
-        info->mti_mos = NULL;
+       info->mti_env = req->rq_svc_thread->t_env;
+       info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
+       info->mti_mos = NULL;
 
         memset(&info->mti_attr, 0, sizeof(info->mti_attr));
-        info->mti_body = NULL;
+       info->mti_big_buf = LU_BUF_NULL;
+       info->mti_body = NULL;
         info->mti_object = NULL;
         info->mti_dlm_req = NULL;
         info->mti_has_trans = 0;
@@ -3004,341 +2634,77 @@ static void mdt_thread_info_init(struct ptlrpc_request *req,
 
         /* To not check for split by default. */
         info->mti_spec.no_create = 0;
+       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;
-        }
-        for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
-                mdt_lock_handle_fini(&info->mti_lh[i]);
-        info->mti_env = NULL;
-}
-
-static int mdt_filter_recovery_request(struct ptlrpc_request *req,
-                                       struct obd_device *obd, int *process)
-{
-        switch (lustre_msg_get_opc(req->rq_reqmsg)) {
-        case MDS_CONNECT: /* This will never get here, but for completeness. */
-        case OST_CONNECT: /* This will never get here, but for completeness. */
-        case MDS_DISCONNECT:
-        case OST_DISCONNECT:
-       case OBD_IDX_READ:
-               *process = 1;
-               RETURN(0);
-
-        case MDS_CLOSE:
-        case MDS_DONE_WRITING:
-        case MDS_SYNC: /* used in unmounting */
-        case OBD_PING:
-        case MDS_REINT:
-        case SEQ_QUERY:
-        case FLD_QUERY:
-        case LDLM_ENQUEUE:
-                *process = target_queue_recovery_request(req, obd);
-                RETURN(0);
-
-        default:
-                DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
-                *process = -EAGAIN;
-                RETURN(0);
-        }
-}
-
-/*
- * Handle recovery. Return:
- *        +1: continue request processing;
- *       -ve: abort immediately with the given error code;
- *         0: send reply with error code in req->rq_status;
- */
-static int mdt_recovery(struct mdt_thread_info *info)
-{
-        struct ptlrpc_request *req = mdt_info_req(info);
-        struct obd_device *obd;
-
-        ENTRY;
-
-        switch (lustre_msg_get_opc(req->rq_reqmsg)) {
-        case MDS_CONNECT:
-        case SEC_CTX_INIT:
-        case SEC_CTX_INIT_CONT:
-        case SEC_CTX_FINI:
-                {
-#if 0
-                        int rc;
-
-                        rc = mdt_handle_idmap(info);
-                        if (rc)
-                                RETURN(rc);
-                        else
-#endif
-                                RETURN(+1);
-                }
-        }
-
-        if (unlikely(!class_connected_export(req->rq_export))) {
-                CERROR("operation %d on unconnected MDS from %s\n",
-                       lustre_msg_get_opc(req->rq_reqmsg),
-                       libcfs_id2str(req->rq_peer));
-                /* FIXME: For CMD cleanup, when mds_B stop, the req from
-                 * mds_A will get -ENOTCONN(especially for ping req),
-                 * which will cause that mds_A deactive timeout, then when
-                 * mds_A cleanup, the cleanup process will be suspended since
-                 * deactive timeout is not zero.
-                 */
-                req->rq_status = -ENOTCONN;
-                target_send_reply(req, -ENOTCONN, info->mti_fail_id);
-                RETURN(0);
-        }
-
-        /* sanity check: if the xid matches, the request must be marked as a
-         * resent or replayed */
-        if (req_xid_is_last(req)) {
-                if (!(lustre_msg_get_flags(req->rq_reqmsg) &
-                      (MSG_RESENT | MSG_REPLAY))) {
-                        DEBUG_REQ(D_WARNING, req, "rq_xid "LPU64" matches last_xid, "
-                                  "expected REPLAY or RESENT flag (%x)", req->rq_xid,
-                                  lustre_msg_get_flags(req->rq_reqmsg));
-                        LBUG();
-                        req->rq_status = -ENOTCONN;
-                        RETURN(-ENOTCONN);
-                }
-        }
-
-        /* else: note the opposite is not always true; a RESENT req after a
-         * failover will usually not match the last_xid, since it was likely
-         * never committed. A REPLAYed request will almost never match the
-         * last xid, however it could for a committed, but still retained,
-         * open. */
-
-        obd = req->rq_export->exp_obd;
-
-        /* Check for aborted recovery... */
-        if (unlikely(obd->obd_recovering)) {
-                int rc;
-                int should_process;
-                DEBUG_REQ(D_INFO, req, "Got new replay");
-                rc = mdt_filter_recovery_request(req, obd, &should_process);
-                if (rc != 0 || !should_process)
-                        RETURN(rc);
-                else if (should_process < 0) {
-                        req->rq_status = should_process;
-                        rc = ptlrpc_error(req);
-                        RETURN(rc);
-                }
-        }
-        RETURN(+1);
-}
-
-static int mdt_msg_check_version(struct lustre_msg *msg)
-{
-        int rc;
-
-        switch (lustre_msg_get_opc(msg)) {
-        case MDS_CONNECT:
-        case MDS_DISCONNECT:
-        case OBD_PING:
-        case SEC_CTX_INIT:
-        case SEC_CTX_INIT_CONT:
-        case SEC_CTX_FINI:
-       case OBD_IDX_READ:
-                rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
-                if (rc)
-                        CERROR("bad opc %u version %08x, expecting %08x\n",
-                               lustre_msg_get_opc(msg),
-                               lustre_msg_get_version(msg),
-                               LUSTRE_OBD_VERSION);
-                break;
-        case MDS_GETSTATUS:
-        case MDS_GETATTR:
-        case MDS_GETATTR_NAME:
-        case MDS_STATFS:
-        case MDS_READPAGE:
-        case MDS_WRITEPAGE:
-        case MDS_IS_SUBDIR:
-        case MDS_REINT:
-        case MDS_CLOSE:
-        case MDS_DONE_WRITING:
-        case MDS_PIN:
-        case MDS_SYNC:
-        case MDS_GETXATTR:
-        case MDS_SETXATTR:
-        case MDS_SET_INFO:
-        case MDS_GET_INFO:
-        case MDS_QUOTACHECK:
-        case MDS_QUOTACTL:
-        case QUOTA_DQACQ:
-        case QUOTA_DQREL:
-        case SEQ_QUERY:
-        case FLD_QUERY:
-                rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
-                if (rc)
-                        CERROR("bad opc %u version %08x, expecting %08x\n",
-                               lustre_msg_get_opc(msg),
-                               lustre_msg_get_version(msg),
-                               LUSTRE_MDS_VERSION);
-                break;
-        case LDLM_ENQUEUE:
-        case LDLM_CONVERT:
-        case LDLM_BL_CALLBACK:
-        case LDLM_CP_CALLBACK:
-                rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
-                if (rc)
-                        CERROR("bad opc %u version %08x, expecting %08x\n",
-                               lustre_msg_get_opc(msg),
-                               lustre_msg_get_version(msg),
-                               LUSTRE_DLM_VERSION);
-                break;
-        case OBD_LOG_CANCEL:
-        case LLOG_ORIGIN_HANDLE_CREATE:
-        case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
-        case LLOG_ORIGIN_HANDLE_READ_HEADER:
-        case LLOG_ORIGIN_HANDLE_CLOSE:
-        case LLOG_ORIGIN_HANDLE_DESTROY:
-        case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
-        case LLOG_CATINFO:
-                rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
-                if (rc)
-                        CERROR("bad opc %u version %08x, expecting %08x\n",
-                               lustre_msg_get_opc(msg),
-                               lustre_msg_get_version(msg),
-                               LUSTRE_LOG_VERSION);
-                break;
-        default:
-                CERROR("MDS unknown opcode %d\n", lustre_msg_get_opc(msg));
-                rc = -ENOTSUPP;
-        }
-        return rc;
-}
-
-static int mdt_handle0(struct ptlrpc_request *req,
-                       struct mdt_thread_info *info,
-                       struct mdt_opc_slice *supported)
-{
-        struct mdt_handler *h;
-        struct lustre_msg  *msg;
-        int                 rc;
-
-        ENTRY;
-
-        if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_MDS_ALL_REQUEST_NET, OBD_FAIL_ONCE))
-                RETURN(0);
-
-        LASSERT(current->journal_info == NULL);
-
-        msg = req->rq_reqmsg;
-        rc = mdt_msg_check_version(msg);
-        if (likely(rc == 0)) {
-                rc = mdt_recovery(info);
-                if (likely(rc == +1)) {
-                        h = mdt_handler_find(lustre_msg_get_opc(msg),
-                                             supported);
-                        if (likely(h != NULL)) {
-                                rc = mdt_req_handle(info, h, req);
-                        } else {
-                                CERROR("The unsupported opc: 0x%x\n",
-                                       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);
-}
-
-/*
- * MDT handler function called by ptlrpc service thread when request comes.
- *
- * XXX common "target" functionality should be factored into separate module
- * shared by mdt, ost and stand-alone services like fld.
- */
-static int mdt_handle_common(struct ptlrpc_request *req,
-                             struct mdt_opc_slice *supported)
-{
-        struct lu_env          *env;
-        struct mdt_thread_info *info;
-        int                     rc;
-        ENTRY;
-
-        env = req->rq_svc_thread->t_env;
-        LASSERT(env != NULL);
-        LASSERT(env->le_ses != NULL);
-        LASSERT(env->le_ctx.lc_thread == req->rq_svc_thread);
-        info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
-        LASSERT(info != NULL);
+       int i;
 
-        mdt_thread_info_init(req, info);
+       if (info->mti_object != NULL) {
+               mdt_object_put(info->mti_env, info->mti_object);
+               info->mti_object = NULL;
+       }
 
-        rc = mdt_handle0(req, info, supported);
+       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;
 
-        mdt_thread_info_fini(info);
-        RETURN(rc);
+       if (unlikely(info->mti_big_buf.lb_buf != NULL))
+               lu_buf_free(&info->mti_big_buf);
 }
 
-/*
- * This is called from recovery code as handler of _all_ RPC types, FLD and SEQ
- * as well.
- */
-int mdt_recovery_handle(struct ptlrpc_request *req)
+struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
 {
-        int rc;
-        ENTRY;
+       struct mdt_thread_info  *mti;
+       struct lustre_capa      *lc;
 
-        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;
-        }
+       mti = mdt_th_info(tsi->tsi_env);
+       LASSERT(mti != NULL);
 
-        RETURN(rc);
-}
+       mdt_thread_info_init(tgt_ses_req(tsi), mti);
+       if (tsi->tsi_corpus != NULL) {
+               struct req_capsule *pill = tsi->tsi_pill;
 
-static int mdt_regular_handle(struct ptlrpc_request *req)
-{
-        return mdt_handle_common(req, mdt_regular_handlers);
-}
+               mti->mti_object = mdt_obj(tsi->tsi_corpus);
+               lu_object_get(tsi->tsi_corpus);
 
-static int mdt_readpage_handle(struct ptlrpc_request *req)
-{
-        return mdt_handle_common(req, mdt_readpage_handlers);
-}
+               /*
+                * XXX: must be part of tgt_mdt_body_unpack but moved here
+                * due to mdt_set_capainfo().
+                */
+               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);
+               }
+       }
+       mti->mti_body = tsi->tsi_mdt_body;
+       mti->mti_dlm_req = tsi->tsi_dlm_req;
 
-static int mdt_xmds_handle(struct ptlrpc_request *req)
-{
-        return mdt_handle_common(req, mdt_xmds_handlers);
+       return mti;
 }
 
-static int mdt_mdsc_handle(struct ptlrpc_request *req)
+int mdt_tgt_connect(struct tgt_session_info *tsi)
 {
-        return mdt_handle_common(req, mdt_seq_handlers);
-}
+       struct ptlrpc_request   *req = tgt_ses_req(tsi);
+       int                      rc;
 
-static int mdt_mdss_handle(struct ptlrpc_request *req)
-{
-        return mdt_handle_common(req, mdt_seq_handlers);
-}
+       ENTRY;
 
-static int mdt_dtss_handle(struct ptlrpc_request *req)
-{
-        return mdt_handle_common(req, mdt_seq_handlers);
-}
+       rc = tgt_connect(tsi);
+       if (rc != 0)
+               RETURN(rc);
 
-static int mdt_fld_handle(struct ptlrpc_request *req)
-{
-        return mdt_handle_common(req, mdt_fld_handlers);
+       rc = mdt_init_idmap(tsi);
+       if (rc != 0)
+               GOTO(err, rc);
+       RETURN(0);
+err:
+       obd_disconnect(class_export_get(req->rq_export));
+       return rc;
 }
 
 enum mdt_it_code {
@@ -3360,6 +2726,16 @@ 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 **,
+                            __u64);
 static int mdt_intent_reint(enum mdt_it_code opcode,
                             struct mdt_thread_info *info,
                             struct ldlm_lock **,
@@ -3383,7 +2759,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
         },
@@ -3420,15 +2803,15 @@ 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_GETATTR,
-                .it_flags = HABEO_REFERO,
-                .it_act   = mdt_intent_getattr
-        }
+       [MDT_IT_LAYOUT] = {
+               .it_fmt   = &RQF_LDLM_INTENT_LAYOUT,
+               .it_flags = 0,
+               .it_act   = mdt_intent_layout
+       }
 };
 
 int mdt_intent_lock_replace(struct mdt_thread_info *info,
@@ -3573,6 +2956,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,
@@ -3600,21 +3021,12 @@ static int mdt_intent_getattr(enum mdt_it_code opcode,
 
         switch (opcode) {
         case MDT_IT_LOOKUP:
-                child_bits = MDS_INODELOCK_LOOKUP;
+               child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
                 break;
         case MDT_IT_GETATTR:
-                child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
+               child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
+                            MDS_INODELOCK_PERM;
                 break;
-        case MDT_IT_LAYOUT: {
-                static int printed = 0;
-
-                if (!printed) {
-                        CERROR("layout lock not supported by this version\n");
-                        printed = 1;
-                }
-                GOTO(out_shrink, rc = -EINVAL);
-                break;
-        }
         default:
                 CERROR("Unsupported intent (%d)\n", opcode);
                 GOTO(out_shrink, rc = -EINVAL);
@@ -3631,8 +3043,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;
@@ -3654,6 +3066,62 @@ out_shrink:
         return rc;
 }
 
+static int mdt_intent_layout(enum mdt_it_code opcode,
+                            struct mdt_thread_info *info,
+                            struct ldlm_lock **lockp,
+                            __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", 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));
+       rc = req_capsule_server_pack(info->mti_pill);
+       if (rc != 0)
+               RETURN(-EINVAL);
+
+       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);
+
+       CERROR("%s: Unsupported layout intent (%d)\n",
+               mdt_obd_name(info->mti_mdt), layout->li_opc);
+       RETURN(-EINVAL);
+}
+
 static int mdt_intent_reint(enum mdt_it_code opcode,
                             struct mdt_thread_info *info,
                             struct ldlm_lock **lockp,
@@ -3671,7 +3139,7 @@ static int mdt_intent_reint(enum mdt_it_code opcode,
 
         ENTRY;
 
-        opc = mdt_reint_opcode(info, intent_fmts);
+       opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
         if (opc < 0)
                 RETURN(opc);
 
@@ -3696,14 +3164,16 @@ static int mdt_intent_reint(enum mdt_it_code opcode,
         if (rc != 0)
                 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
 
-        /* Cross-ref case, the lock should be returned to the client */
-        if (rc == -EREMOTE) {
-                LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
-                rep->lock_policy_res2 = 0;
-                rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
-                RETURN(rc);
-        }
-        rep->lock_policy_res2 = clear_serious(rc);
+       /* 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));
+               rep->lock_policy_res2 = 0;
+               rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
+               RETURN(rc);
+       }
+
+       rep->lock_policy_res2 = clear_serious(rc);
 
         if (rep->lock_policy_res2 == -ENOENT &&
             mdt_get_disposition(rep, DISP_LOOKUP_NEG))
@@ -3730,7 +3200,7 @@ static int mdt_intent_reint(enum mdt_it_code opcode,
                   */
                 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
                         LASSERTF(rc == 0, "Error occurred but lock handle "
-                                 "is still in use\n");
+                                 "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);
@@ -3817,14 +3287,6 @@ static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
                RETURN(rc);
        }
 
-       if (opc == MDT_IT_LAYOUT) {
-               (*lockp)->l_lvb_type = LVB_T_LAYOUT;
-               /* XXX: set replay RMF_DLM_LVB as the real EA size when LAYOUT
-                *      lock enabled. */
-       } else if (opc == MDT_IT_READDIR) {
-               req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
-       }
-
        flv  = &mdt_it_flavor[opc];
         if (flv->it_fmt != NULL)
                 req_capsule_extend(pill, flv->it_fmt);
@@ -3832,35 +3294,47 @@ static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
         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 &&
-                    req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
-                        RETURN(-EROFS);
+               if (flv->it_flags & MUTABOR &&
+                   exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
+                       RETURN(-EROFS);
         }
         if (rc == 0 && flv->it_act != NULL) {
-                /* execute policy */
-                rc = flv->it_act(opc, info, lockp, flags);
-        } else {
-                rc = -EOPNOTSUPP;
-        }
-        RETURN(rc);
+               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 = -EPROTO;
+       }
+       RETURN(rc);
 }
 
 static int mdt_intent_policy(struct ldlm_namespace *ns,
                              struct ldlm_lock **lockp, void *req_cookie,
                             ldlm_mode_t mode, __u64 flags, void *data)
 {
-        struct mdt_thread_info *info;
-        struct ptlrpc_request  *req  =  req_cookie;
-        struct ldlm_intent     *it;
-        struct req_capsule     *pill;
-        int rc;
+       struct tgt_session_info *tsi;
+       struct mdt_thread_info  *info;
+       struct ptlrpc_request   *req  =  req_cookie;
+       struct ldlm_intent      *it;
+       struct req_capsule      *pill;
+       int rc;
 
-        ENTRY;
+       ENTRY;
 
-        LASSERT(req != NULL);
+       LASSERT(req != NULL);
 
-        info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
-                                  &mdt_thread_key);
+       tsi = tgt_ses_info(req->rq_svc_thread->t_env);
+
+       info = tsi2mdt_info(tsi);
         LASSERT(info != NULL);
         pill = info->mti_pill;
         LASSERT(pill->rc_req == req);
@@ -3892,679 +3366,224 @@ static int mdt_intent_policy(struct ldlm_namespace *ns,
                 if (rc)
                         rc = err_serious(rc);
         }
-        RETURN(rc);
+       mdt_thread_info_fini(info);
+       RETURN(rc);
 }
 
 static int mdt_seq_fini(const struct lu_env *env,
                         struct mdt_device *m)
 {
-        struct md_site *ms = mdt_md_site(m);
-        ENTRY;
-
-        if (ms != NULL) {
-                if (ms->ms_server_seq) {
-                        seq_server_fini(ms->ms_server_seq, env);
-                        OBD_FREE_PTR(ms->ms_server_seq);
-                        ms->ms_server_seq = NULL;
-        }
-
-                if (ms->ms_control_seq) {
-                        seq_server_fini(ms->ms_control_seq, env);
-                        OBD_FREE_PTR(ms->ms_control_seq);
-                        ms->ms_control_seq = NULL;
-        }
-
-                if (ms->ms_client_seq) {
-                        seq_client_fini(ms->ms_client_seq);
-                        OBD_FREE_PTR(ms->ms_client_seq);
-                        ms->ms_client_seq = NULL;
-                }
-        }
-
-        RETURN(0);
+       return seq_site_fini(env, mdt_seq_site(m));
 }
 
 static int mdt_seq_init(const struct lu_env *env,
                         const char *uuid,
                         struct mdt_device *m)
 {
-        struct md_site *ms;
-        char *prefix;
-        int rc;
-        ENTRY;
+       struct seq_server_site *ss;
+       char *prefix;
+       int rc;
+       ENTRY;
 
-        ms = mdt_md_site(m);
+       ss = mdt_seq_site(m);
 
-        /*
-         * This is sequence-controller node. Init seq-controller server on local
-         * MDT.
-         */
-        if (ms->ms_node_id == 0) {
-                LASSERT(ms->ms_control_seq == NULL);
+       /*
+        * This is sequence-controller node. Init seq-controller server on local
+        * MDT.
+        */
+       if (ss->ss_node_id == 0) {
+               LASSERT(ss->ss_control_seq == NULL);
 
-                OBD_ALLOC_PTR(ms->ms_control_seq);
-                if (ms->ms_control_seq == NULL)
-                        RETURN(-ENOMEM);
+               OBD_ALLOC_PTR(ss->ss_control_seq);
+               if (ss->ss_control_seq == NULL)
+                       RETURN(-ENOMEM);
 
-                rc = seq_server_init(ms->ms_control_seq,
-                                     m->mdt_bottom, uuid,
-                                     LUSTRE_SEQ_CONTROLLER,
-                                     ms,
-                                     env);
+               rc = seq_server_init(ss->ss_control_seq,
+                                    m->mdt_bottom, uuid,
+                                    LUSTRE_SEQ_CONTROLLER,
+                                    ss,
+                                    env);
 
-                if (rc)
-                        GOTO(out_seq_fini, rc);
+               if (rc)
+                       GOTO(out_seq_fini, rc);
 
-                OBD_ALLOC_PTR(ms->ms_client_seq);
-                if (ms->ms_client_seq == NULL)
-                        GOTO(out_seq_fini, rc = -ENOMEM);
+               OBD_ALLOC_PTR(ss->ss_client_seq);
+               if (ss->ss_client_seq == NULL)
+                       GOTO(out_seq_fini, rc = -ENOMEM);
 
-                OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
-                if (prefix == NULL) {
-                        OBD_FREE_PTR(ms->ms_client_seq);
-                        GOTO(out_seq_fini, rc = -ENOMEM);
-                }
+               OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
+               if (prefix == NULL) {
+                       OBD_FREE_PTR(ss->ss_client_seq);
+                       GOTO(out_seq_fini, rc = -ENOMEM);
+               }
 
-                snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
-                         uuid);
+               snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
+                        uuid);
 
-                /*
-                 * Init seq-controller client after seq-controller server is
-                 * ready. Pass ms->ms_control_seq to it for direct talking.
-                 */
-                rc = seq_client_init(ms->ms_client_seq, NULL,
-                                     LUSTRE_SEQ_METADATA, prefix,
-                                     ms->ms_control_seq);
-                OBD_FREE(prefix, MAX_OBD_NAME + 5);
+               /*
+                * Init seq-controller client after seq-controller server is
+                * ready. Pass ss->ss_control_seq to it for direct talking.
+                */
+               rc = seq_client_init(ss->ss_client_seq, NULL,
+                                    LUSTRE_SEQ_METADATA, prefix,
+                                    ss->ss_control_seq);
+               OBD_FREE(prefix, MAX_OBD_NAME + 5);
 
-                if (rc)
-                        GOTO(out_seq_fini, rc);
-        }
+               if (rc)
+                       GOTO(out_seq_fini, rc);
+       }
 
-        /* Init seq-server on local MDT */
-        LASSERT(ms->ms_server_seq == NULL);
+       /* Init seq-server on local MDT */
+       LASSERT(ss->ss_server_seq == NULL);
 
-        OBD_ALLOC_PTR(ms->ms_server_seq);
-        if (ms->ms_server_seq == NULL)
-                GOTO(out_seq_fini, rc = -ENOMEM);
+       OBD_ALLOC_PTR(ss->ss_server_seq);
+       if (ss->ss_server_seq == NULL)
+               GOTO(out_seq_fini, rc = -ENOMEM);
 
-        rc = seq_server_init(ms->ms_server_seq,
-                             m->mdt_bottom, uuid,
-                             LUSTRE_SEQ_SERVER,
-                             ms,
-                             env);
-        if (rc)
-                GOTO(out_seq_fini, rc = -ENOMEM);
+       rc = seq_server_init(ss->ss_server_seq,
+                            m->mdt_bottom, uuid,
+                            LUSTRE_SEQ_SERVER,
+                            ss,
+                            env);
+       if (rc)
+               GOTO(out_seq_fini, rc = -ENOMEM);
 
-        /* Assign seq-controller client to local seq-server. */
-        if (ms->ms_node_id == 0) {
-                LASSERT(ms->ms_client_seq != NULL);
+       /* Assign seq-controller client to local seq-server. */
+       if (ss->ss_node_id == 0) {
+               LASSERT(ss->ss_client_seq != NULL);
 
-                rc = seq_server_set_cli(ms->ms_server_seq,
-                                        ms->ms_client_seq,
-                                        env);
-        }
+               rc = seq_server_set_cli(ss->ss_server_seq,
+                                       ss->ss_client_seq,
+                                       env);
+       }
 
-        EXIT;
+       EXIT;
 out_seq_fini:
-        if (rc)
-                mdt_seq_fini(env, m);
+       if (rc)
+               mdt_seq_fini(env, m);
 
-        return rc;
+       return rc;
 }
+
 /*
- * Init client sequence manager which is used by local MDS to talk to sequence
- * controller on remote node.
+ * FLD wrappers
  */
-static int mdt_seq_init_cli(const struct lu_env *env,
-                            struct mdt_device *m,
-                            struct lustre_cfg *cfg)
+static int mdt_fld_fini(const struct lu_env *env,
+                        struct mdt_device *m)
 {
-        struct md_site    *ms = mdt_md_site(m);
-        struct obd_device *mdc;
-        struct obd_uuid   *uuidp, *mdcuuidp;
-        char              *uuid_str, *mdc_uuid_str;
-        int                rc;
-        int                index;
-        struct mdt_thread_info *info;
-        char *p, *index_string = lustre_cfg_string(cfg, 2);
-        ENTRY;
-
-        info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
-        uuidp = &info->mti_u.uuid[0];
-        mdcuuidp = &info->mti_u.uuid[1];
+       struct seq_server_site *ss = mdt_seq_site(m);
+       ENTRY;
 
-        LASSERT(index_string);
+       if (ss && ss->ss_server_fld) {
+               fld_server_fini(env, ss->ss_server_fld);
+               OBD_FREE_PTR(ss->ss_server_fld);
+               ss->ss_server_fld = NULL;
+       }
 
-        index = simple_strtol(index_string, &p, 10);
-        if (*p) {
-                CERROR("Invalid index in lustre_cgf, offset 2\n");
-                RETURN(-EINVAL);
-        }
-
-        /* check if this is adding the first MDC and controller is not yet
-         * initialized. */
-        if (index != 0 || ms->ms_client_seq)
-                RETURN(0);
-
-        uuid_str = lustre_cfg_string(cfg, 1);
-        mdc_uuid_str = lustre_cfg_string(cfg, 4);
-        obd_str2uuid(uuidp, uuid_str);
-        obd_str2uuid(mdcuuidp, mdc_uuid_str);
-
-        mdc = class_find_client_obd(uuidp, LUSTRE_MDC_NAME, mdcuuidp);
-        if (!mdc) {
-                CERROR("can't find controller MDC by uuid %s\n",
-                       uuid_str);
-                rc = -ENOENT;
-        } else if (!mdc->obd_set_up) {
-                CERROR("target %s not set up\n", mdc->obd_name);
-                rc = -EINVAL;
-        } else {
-                LASSERT(ms->ms_control_exp);
-                OBD_ALLOC_PTR(ms->ms_client_seq);
-                if (ms->ms_client_seq != NULL) {
-                        char *prefix;
-
-                        OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
-                        if (!prefix)
-                                RETURN(-ENOMEM);
-
-                        snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
-                                 mdc->obd_name);
-
-                        rc = seq_client_init(ms->ms_client_seq,
-                                             ms->ms_control_exp,
-                                             LUSTRE_SEQ_METADATA,
-                                             prefix, NULL);
-                        OBD_FREE(prefix, MAX_OBD_NAME + 5);
-                } else
-                        rc = -ENOMEM;
-
-                if (rc)
-                        RETURN(rc);
-
-                LASSERT(ms->ms_server_seq != NULL);
-                rc = seq_server_set_cli(ms->ms_server_seq, ms->ms_client_seq,
-                                        env);
-        }
-
-        RETURN(rc);
-}
-
-static void mdt_seq_fini_cli(struct mdt_device *m)
-{
-        struct md_site *ms;
-
-        ENTRY;
-
-        ms = mdt_md_site(m);
-
-        if (ms != NULL) {
-                if (ms->ms_server_seq)
-                        seq_server_set_cli(ms->ms_server_seq,
-                                   NULL, NULL);
-
-                if (ms->ms_control_exp) {
-                        class_export_put(ms->ms_control_exp);
-                        ms->ms_control_exp = NULL;
-                }
-        }
-        EXIT;
-}
-
-/*
- * FLD wrappers
- */
-static int mdt_fld_fini(const struct lu_env *env,
-                        struct mdt_device *m)
-{
-        struct md_site *ms = mdt_md_site(m);
-        ENTRY;
-
-        if (ms && ms->ms_server_fld) {
-                fld_server_fini(ms->ms_server_fld, env);
-                OBD_FREE_PTR(ms->ms_server_fld);
-                ms->ms_server_fld = NULL;
-        }
-
-        RETURN(0);
+       RETURN(0);
 }
 
 static int mdt_fld_init(const struct lu_env *env,
                         const char *uuid,
                         struct mdt_device *m)
 {
-        struct md_site *ms;
-        int rc;
-        ENTRY;
-
-        ms = mdt_md_site(m);
-
-        OBD_ALLOC_PTR(ms->ms_server_fld);
-        if (ms->ms_server_fld == NULL)
-                RETURN(rc = -ENOMEM);
-
-        rc = fld_server_init(ms->ms_server_fld,
-                             m->mdt_bottom, uuid,
-                             env, ms->ms_node_id);
-        if (rc) {
-                OBD_FREE_PTR(ms->ms_server_fld);
-                ms->ms_server_fld = NULL;
-                RETURN(rc);
-        }
-
-        RETURN(0);
-}
-
-/* device init/fini methods */
-static void mdt_stop_ptlrpc_service(struct mdt_device *m)
-{
-        ENTRY;
-        if (m->mdt_regular_service != NULL) {
-                ptlrpc_unregister_service(m->mdt_regular_service);
-                m->mdt_regular_service = NULL;
-        }
-        if (m->mdt_readpage_service != NULL) {
-                ptlrpc_unregister_service(m->mdt_readpage_service);
-                m->mdt_readpage_service = NULL;
-        }
-        if (m->mdt_xmds_service != NULL) {
-                ptlrpc_unregister_service(m->mdt_xmds_service);
-                m->mdt_xmds_service = NULL;
-        }
-        if (m->mdt_setattr_service != NULL) {
-                ptlrpc_unregister_service(m->mdt_setattr_service);
-                m->mdt_setattr_service = NULL;
-        }
-        if (m->mdt_mdsc_service != NULL) {
-                ptlrpc_unregister_service(m->mdt_mdsc_service);
-                m->mdt_mdsc_service = NULL;
-        }
-        if (m->mdt_mdss_service != NULL) {
-                ptlrpc_unregister_service(m->mdt_mdss_service);
-                m->mdt_mdss_service = NULL;
-        }
-        if (m->mdt_dtss_service != NULL) {
-                ptlrpc_unregister_service(m->mdt_dtss_service);
-                m->mdt_dtss_service = NULL;
-        }
-        if (m->mdt_fld_service != NULL) {
-                ptlrpc_unregister_service(m->mdt_fld_service);
-                m->mdt_fld_service = NULL;
-        }
-        EXIT;
-}
-
-static int mdt_start_ptlrpc_service(struct mdt_device *m)
-{
-        static struct ptlrpc_service_conf conf;
-        cfs_proc_dir_entry_t *procfs_entry;
-       int rc = 0;
+       struct seq_server_site *ss;
+       int rc;
        ENTRY;
 
-       m->mdt_ldlm_client = &m->mdt_md_dev.md_lu_dev.ld_obd->obd_ldlm_client;
-       ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
-                          "mdt_ldlm_client", m->mdt_ldlm_client);
+       ss = mdt_seq_site(m);
 
-       procfs_entry = m->mdt_md_dev.md_lu_dev.ld_obd->obd_proc_entry;
-
-       conf = (typeof(conf)) {
-               .psc_name               = LUSTRE_MDT_NAME,
-               .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
-               .psc_buf                = {
-                       .bc_nbufs               = MDS_NBUFS,
-                       .bc_buf_size            = MDS_BUFSIZE,
-                       .bc_req_max_size        = MDS_MAXREQSIZE,
-                       .bc_rep_max_size        = MDS_MAXREPSIZE,
-                       .bc_req_portal          = MDS_REQUEST_PORTAL,
-                       .bc_rep_portal          = MDC_REPLY_PORTAL,
-               },
-               /*
-                * We'd like to have a mechanism to set this on a per-device
-                * basis, but alas...
-                */
-               .psc_thr                = {
-                       .tc_thr_name            = LUSTRE_MDT_NAME,
-                       .tc_thr_factor          = MDT_THR_FACTOR,
-                       .tc_nthrs_init          = MDT_NTHRS_INIT,
-                       .tc_nthrs_base          = MDT_NTHRS_BASE,
-                       .tc_nthrs_max           = MDT_NTHRS_MAX,
-                       .tc_nthrs_user          = mds_num_threads,
-                       .tc_cpu_affinity        = 1,
-                       .tc_ctx_tags            = LCT_MD_THREAD,
-               },
-               .psc_cpt                = {
-                       .cc_pattern             = mds_num_cpts,
-               },
-               .psc_ops                = {
-                       .so_req_handler         = mdt_regular_handle,
-                       .so_req_printer         = target_print_req,
-                       .so_hpreq_handler       = ptlrpc_hpreq_handler,
-               },
-       };
-       m->mdt_regular_service = ptlrpc_register_service(&conf, procfs_entry);
-       if (IS_ERR(m->mdt_regular_service)) {
-               rc = PTR_ERR(m->mdt_regular_service);
-               CERROR("failed to start regular mdt service: %d\n", rc);
-               m->mdt_regular_service = NULL;
+       OBD_ALLOC_PTR(ss->ss_server_fld);
+       if (ss->ss_server_fld == NULL)
+               RETURN(rc = -ENOMEM);
 
+       rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
+                            ss->ss_node_id, LU_SEQ_RANGE_MDT);
+       if (rc) {
+               OBD_FREE_PTR(ss->ss_server_fld);
+               ss->ss_server_fld = NULL;
                RETURN(rc);
        }
 
-       /*
-        * readpage service configuration. Parameters have to be adjusted,
-        * ideally.
-        */
-       memset(&conf, 0, sizeof(conf));
-       conf = (typeof(conf)) {
-               .psc_name               = LUSTRE_MDT_NAME "_readpage",
-               .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
-               .psc_buf                = {
-                       .bc_nbufs               = MDS_NBUFS,
-                       .bc_buf_size            = MDS_BUFSIZE,
-                       .bc_req_max_size        = MDS_MAXREQSIZE,
-                       .bc_rep_max_size        = MDS_MAXREPSIZE,
-                       .bc_req_portal          = MDS_READPAGE_PORTAL,
-                       .bc_rep_portal          = MDC_REPLY_PORTAL,
-               },
-               .psc_thr                = {
-                       .tc_thr_name            = "mdt_rdpg",
-                       .tc_thr_factor          = MDT_RDPG_THR_FACTOR,
-                       .tc_nthrs_init          = MDT_RDPG_NTHRS_INIT,
-                       .tc_nthrs_base          = MDT_RDPG_NTHRS_BASE,
-                       .tc_nthrs_max           = MDT_RDPG_NTHRS_MAX,
-                       .tc_nthrs_user          = mds_rdpg_num_threads,
-                       .tc_cpu_affinity        = 1,
-                       .tc_ctx_tags            = LCT_MD_THREAD,
-               },
-               .psc_cpt                = {
-                       .cc_pattern             = mds_rdpg_num_cpts,
-               },
-               .psc_ops                = {
-                       .so_req_handler         = mdt_readpage_handle,
-                       .so_req_printer         = target_print_req,
-               },
-       };
-       m->mdt_readpage_service = ptlrpc_register_service(&conf, procfs_entry);
-       if (IS_ERR(m->mdt_readpage_service)) {
-               rc = PTR_ERR(m->mdt_readpage_service);
-               CERROR("failed to start readpage service: %d\n", rc);
-               m->mdt_readpage_service = NULL;
-
-               GOTO(err_mdt_svc, rc);
-        }
-
-        /*
-         * setattr service configuration.
-         *
-         * XXX To keep the compatibility with old client(< 2.2), we need to
-         * preserve this portal for a certain time, it should be removed
-         * eventually. LU-617.
-         */
-       memset(&conf, 0, sizeof(conf));
-       conf = (typeof(conf)) {
-               .psc_name               = LUSTRE_MDT_NAME "_setattr",
-               .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
-               .psc_buf                = {
-                       .bc_nbufs               = MDS_NBUFS,
-                       .bc_buf_size            = MDS_BUFSIZE,
-                       .bc_req_max_size        = MDS_MAXREQSIZE,
-                       .bc_rep_max_size        = MDS_MAXREPSIZE,
-                       .bc_req_portal          = MDS_SETATTR_PORTAL,
-                       .bc_rep_portal          = MDC_REPLY_PORTAL,
-               },
-               .psc_thr                = {
-                       .tc_thr_name            = "mdt_attr",
-                       .tc_thr_factor          = MDT_SETA_THR_FACTOR,
-                       .tc_nthrs_init          = MDT_SETA_NTHRS_INIT,
-                       .tc_nthrs_base          = MDT_SETA_NTHRS_BASE,
-                       .tc_nthrs_max           = MDT_SETA_NTHRS_MAX,
-                       .tc_nthrs_user          = mds_attr_num_threads,
-                       .tc_cpu_affinity        = 1,
-                       .tc_ctx_tags            = LCT_MD_THREAD,
-               },
-               .psc_cpt                = {
-                       .cc_pattern             = mds_attr_num_cpts,
-               },
-               .psc_ops                = {
-                       .so_req_handler         = mdt_regular_handle,
-                       .so_req_printer         = target_print_req,
-               },
-       };
-       m->mdt_setattr_service = ptlrpc_register_service(&conf, procfs_entry);
-       if (IS_ERR(m->mdt_setattr_service)) {
-               rc = PTR_ERR(m->mdt_setattr_service);
-               CERROR("failed to start setattr service: %d\n", rc);
-               m->mdt_setattr_service = NULL;
-
-               GOTO(err_mdt_svc, rc);
-       }
-
-       /*
-        * sequence controller service configuration
-        */
-       memset(&conf, 0, sizeof(conf));
-       conf = (typeof(conf)) {
-               .psc_name               = LUSTRE_MDT_NAME "_mdsc",
-               .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
-               .psc_buf                = {
-                       .bc_nbufs               = MDS_NBUFS,
-                       .bc_buf_size            = MDS_BUFSIZE,
-                       .bc_req_max_size        = SEQ_MAXREQSIZE,
-                       .bc_rep_max_size        = SEQ_MAXREPSIZE,
-                       .bc_req_portal          = SEQ_CONTROLLER_PORTAL,
-                       .bc_rep_portal          = MDC_REPLY_PORTAL,
-               },
-               .psc_thr                = {
-                       .tc_thr_name            = "mdt_mdsc",
-                       .tc_nthrs_init          = MDT_OTHR_NTHRS_INIT,
-                       .tc_nthrs_max           = MDT_OTHR_NTHRS_MAX,
-                       .tc_ctx_tags            = LCT_MD_THREAD,
-               },
-               .psc_ops                = {
-                       .so_req_handler         = mdt_mdsc_handle,
-                       .so_req_printer         = target_print_req,
-               },
-       };
-       m->mdt_mdsc_service = ptlrpc_register_service(&conf, procfs_entry);
-       if (IS_ERR(m->mdt_mdsc_service)) {
-               rc = PTR_ERR(m->mdt_mdsc_service);
-               CERROR("failed to start seq controller service: %d\n", rc);
-               m->mdt_mdsc_service = NULL;
-
-               GOTO(err_mdt_svc, rc);
-       }
+       RETURN(0);
+}
 
-       /*
-        * metadata sequence server service configuration
-        */
-       memset(&conf, 0, sizeof(conf));
-       conf = (typeof(conf)) {
-               .psc_name               = LUSTRE_MDT_NAME "_mdss",
-               .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
-               .psc_buf                = {
-                       .bc_nbufs               = MDS_NBUFS,
-                       .bc_buf_size            = MDS_BUFSIZE,
-                       .bc_req_max_size        = SEQ_MAXREQSIZE,
-                       .bc_rep_max_size        = SEQ_MAXREPSIZE,
-                       .bc_req_portal          = SEQ_METADATA_PORTAL,
-                       .bc_rep_portal          = MDC_REPLY_PORTAL,
-               },
-               .psc_thr                = {
-                       .tc_thr_name            = "mdt_mdss",
-                       .tc_nthrs_init          = MDT_OTHR_NTHRS_INIT,
-                       .tc_nthrs_max           = MDT_OTHR_NTHRS_MAX,
-                       .tc_ctx_tags            = LCT_MD_THREAD | LCT_DT_THREAD
-               },
-               .psc_ops                = {
-                       .so_req_handler         = mdt_mdss_handle,
-                       .so_req_printer         = target_print_req,
-               },
-        };
-       m->mdt_mdss_service = ptlrpc_register_service(&conf, procfs_entry);
-       if (IS_ERR(m->mdt_mdss_service)) {
-               rc = PTR_ERR(m->mdt_mdss_service);
-               CERROR("failed to start metadata seq server service: %d\n", rc);
-               m->mdt_mdss_service = NULL;
+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;
+       ENTRY;
 
-               GOTO(err_mdt_svc, rc);
-       }
+       LASSERT(top);
 
-       /*
-        * Data sequence server service configuration. We want to have really
-        * cluster-wide sequences space. This is why we start only one sequence
-        * controller which manages space.
-        */
-       memset(&conf, 0, sizeof(conf));
-       conf = (typeof(conf)) {
-               .psc_name               = LUSTRE_MDT_NAME "_dtss",
-               .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
-               .psc_buf                = {
-                       .bc_nbufs               = MDS_NBUFS,
-                       .bc_buf_size            = MDS_BUFSIZE,
-                       .bc_req_max_size        = SEQ_MAXREQSIZE,
-                       .bc_rep_max_size        = SEQ_MAXREPSIZE,
-                       .bc_req_portal          = SEQ_DATA_PORTAL,
-                       .bc_rep_portal          = OSC_REPLY_PORTAL,
-               },
-               .psc_thr                = {
-                       .tc_thr_name            = "mdt_dtss",
-                       .tc_nthrs_init          = MDT_OTHR_NTHRS_INIT,
-                       .tc_nthrs_max           = MDT_OTHR_NTHRS_MAX,
-                       .tc_ctx_tags            = LCT_MD_THREAD | LCT_DT_THREAD
-               },
-               .psc_ops                = {
-                       .so_req_handler         = mdt_dtss_handle,
-                       .so_req_printer         = target_print_req,
-               },
-        };
-       m->mdt_dtss_service = ptlrpc_register_service(&conf, procfs_entry);
-       if (IS_ERR(m->mdt_dtss_service)) {
-               rc = PTR_ERR(m->mdt_dtss_service);
-               CERROR("failed to start data seq server service: %d\n", rc);
-               m->mdt_dtss_service = NULL;
+       info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
+       LASSERT(info != NULL);
 
-               GOTO(err_mdt_svc, rc);
-       }
+       bufs = &info->mti_u.bufs;
 
-       /* FLD service start */
-       memset(&conf, 0, sizeof(conf));
-       conf = (typeof(conf)) {
-               .psc_name            = LUSTRE_MDT_NAME "_fld",
-                .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
-               .psc_buf                = {
-                       .bc_nbufs               = MDS_NBUFS,
-                       .bc_buf_size            = MDS_BUFSIZE,
-                       .bc_req_max_size        = FLD_MAXREQSIZE,
-                       .bc_rep_max_size        = FLD_MAXREPSIZE,
-                       .bc_req_portal          = FLD_REQUEST_PORTAL,
-                       .bc_rep_portal          = MDC_REPLY_PORTAL,
-               },
-               .psc_thr                = {
-                       .tc_thr_name            = "mdt_fld",
-                       .tc_nthrs_init          = MDT_OTHR_NTHRS_INIT,
-                       .tc_nthrs_max           = MDT_OTHR_NTHRS_MAX,
-                       .tc_ctx_tags            = LCT_DT_THREAD | LCT_MD_THREAD
-               },
-               .psc_ops                = {
-                       .so_req_handler         = mdt_fld_handle,
-                       .so_req_printer         = target_print_req,
-               },
-       };
-       m->mdt_fld_service = ptlrpc_register_service(&conf, procfs_entry);
-       if (IS_ERR(m->mdt_fld_service)) {
-               rc = PTR_ERR(m->mdt_fld_service);
-               CERROR("failed to start fld service: %d\n", rc);
-               m->mdt_fld_service = NULL;
+       LASSERT(m->mdt_child_exp);
+       LASSERT(m->mdt_child_exp->exp_obd);
+       obd = m->mdt_child_exp->exp_obd;
 
-               GOTO(err_mdt_svc, rc);
+       /* process cleanup, pass mdt obd name to get obd umount flags */
+       /* XXX: this is needed because all layers are referenced by
+        * 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, 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;
        }
-
-       /*
-        * mds-mds service configuration. Separate portal is used to allow
-        * mds-mds requests be not blocked during recovery.
-        */
-       memset(&conf, 0, sizeof(conf));
-       conf = (typeof(conf)) {
-               .psc_name               = LUSTRE_MDT_NAME "_mds",
-               .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
-               .psc_buf                = {
-                       .bc_nbufs               = MDS_NBUFS,
-                       .bc_buf_size            = MDS_BUFSIZE,
-                       .bc_req_max_size        = MDS_MAXREQSIZE,
-                       .bc_rep_max_size        = MDS_MAXREPSIZE,
-                       .bc_req_portal          = MDS_MDS_PORTAL,
-                       .bc_rep_portal          = MDC_REPLY_PORTAL,
-               },
-               .psc_thr                = {
-                       .tc_thr_name            = "mdt_mds",
-                       .tc_nthrs_init          = MDT_OTHR_NTHRS_INIT,
-                       .tc_nthrs_max           = MDT_OTHR_NTHRS_MAX,
-                       .tc_ctx_tags            = LCT_MD_THREAD,
-               },
-               .psc_ops                = {
-                       .so_req_handler         = mdt_xmds_handle,
-                       .so_req_printer         = target_print_req,
-                       .so_hpreq_handler       = ptlrpc_hpreq_handler,
-               },
-       };
-       m->mdt_xmds_service = ptlrpc_register_service(&conf, procfs_entry);
-       if (IS_ERR(m->mdt_xmds_service)) {
-               rc = PTR_ERR(m->mdt_xmds_service);
-               CERROR("failed to start xmds service: %d\n", rc);
-               m->mdt_xmds_service = NULL;
-
-               GOTO(err_mdt_svc, rc);
-        }
-
-        EXIT;
-err_mdt_svc:
-        if (rc)
-                mdt_stop_ptlrpc_service(m);
-
-        return rc;
+       top->ld_ops->ldo_process_config(env, top, lcfg);
+       lustre_cfg_free(lcfg);
+       EXIT;
 }
 
 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;
@@ -4588,7 +3607,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);
        }
 
@@ -4598,7 +3617,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);
        }
 
@@ -4714,9 +3733,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);
 
 
@@ -4733,7 +3752,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:
@@ -4769,7 +3788,7 @@ static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
        LASSERT(mdt->mdt_qmt_dev == NULL);
 
        /* quota master is on MDT0 only for now */
-       if (mdt->mdt_mite.ms_node_id != 0)
+       if (mdt->mdt_seq_site.ss_node_id != 0)
                RETURN(0);
 
        /* MGS generates config commands which look as follows:
@@ -4844,7 +3863,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);
@@ -4899,10 +3918,128 @@ static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
        EXIT;
 }
 
+/* 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)
+{
+       struct mdt_thread_info  *info = tsi2mdt_info(tsi);
+       int                      rc;
+
+       rc = mdt_getxattr(info);
+
+       mdt_thread_info_fini(info);
+       return rc;
+}
+
+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),
+TGT_RPC_HANDLER(MDS_FIRST_OPC,
+               HABEO_REFERO,           MDS_SET_INFO,   mdt_set_info,
+               &RQF_OBD_SET_INFO, LUSTRE_MDS_VERSION),
+TGT_MDT_HDL(0,                         MDS_GET_INFO,   mdt_get_info),
+TGT_MDT_HDL(0          | HABEO_REFERO, MDS_GETSTATUS,  mdt_getstatus),
+TGT_MDT_HDL(HABEO_CORPUS,              MDS_GETATTR,    mdt_getattr),
+TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO,        MDS_GETATTR_NAME,
+                                                       mdt_getattr_name),
+TGT_MDT_HDL(HABEO_CORPUS,              MDS_GETXATTR,   mdt_tgt_getxattr),
+TGT_MDT_HDL(0          | HABEO_REFERO, MDS_STATFS,     mdt_statfs),
+TGT_MDT_HDL(0          | MUTABOR,      MDS_REINT,      mdt_reint),
+TGT_MDT_HDL(HABEO_CORPUS,              MDS_CLOSE,      mdt_close),
+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),
+TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_REGISTER,
+                                                       mdt_hsm_ct_register),
+TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_UNREGISTER,
+                                                       mdt_hsm_ct_unregister),
+TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_STATE_GET,
+                                                       mdt_hsm_state_get),
+TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_STATE_SET,
+                                                       mdt_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)
+};
+
+static struct tgt_handler mdt_sec_ctx_ops[] = {
+TGT_SEC_HDL_VAR(0,                     SEC_CTX_INIT,     mdt_sec_ctx_handle),
+TGT_SEC_HDL_VAR(0,                     SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
+TGT_SEC_HDL_VAR(0,                     SEC_CTX_FINI,     mdt_sec_ctx_handle)
+};
+
+static struct tgt_handler mdt_quota_ops[] = {
+TGT_QUOTA_HDL(HABEO_REFERO,            QUOTA_DQACQ,      mdt_quota_dqacq),
+};
+
+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         = mdt_sec_ctx_ops
+       },
+       {
+               .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_opc_start  = QUOTA_DQACQ,
+               .tos_opc_end    = QUOTA_LAST_OPC,
+               .tos_hs         = mdt_quota_ops
+       },
+       {
+               .tos_opc_start  = LLOG_FIRST_OPC,
+               .tos_opc_end    = LLOG_LAST_OPC,
+               .tos_hs         = tgt_llog_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;
 
@@ -4910,7 +4047,14 @@ static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
 
         ping_evictor_stop();
 
-        mdt_stop_ptlrpc_service(m);
+       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();
@@ -4937,49 +4081,49 @@ 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);
+
         mdt_seq_fini(env, m);
-        mdt_seq_fini_cli(m);
         mdt_fld_fini(env, m);
-        sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
 
         next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
         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;
-       write_unlock(&m->mdt_sptlrpc_lock);
+       write_lock(&m->mdt_lut.lut_sptlrpc_lock);
+       sptlrpc_rule_set_free(&m->mdt_lut.lut_sptlrpc_rset);
+       m->mdt_lut.lut_sptlrpc_rset = tmp_rset;
+       write_unlock(&m->mdt_lut.lut_sptlrpc_lock);
 
-        return 0;
+       return 0;
 }
 
 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
@@ -4994,15 +4138,15 @@ static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
         struct lustre_mount_info  *lmi = NULL;
         struct lustre_sb_info     *lsi;
         struct lu_site            *s;
-        struct md_site            *mite;
+       struct seq_server_site    *ss_site;
         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
@@ -5026,6 +4170,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);
@@ -5036,15 +4185,9 @@ static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
                 LASSERT(num);
                 node_id = simple_strtol(num, NULL, 10);
                obd->u.obt.obt_magic = OBT_MAGIC;
-        }
-
-       rwlock_init(&m->mdt_sptlrpc_lock);
-        sptlrpc_rule_set_init(&m->mdt_sptlrpc_rset);
+       }
 
        spin_lock_init(&m->mdt_ioepoch_lock);
-        m->mdt_opts.mo_compat_resname = 0;
-        m->mdt_opts.mo_mds_capa = 1;
-        m->mdt_opts.mo_oss_capa = 1;
         m->mdt_capa_timeout = CAPA_TIMEOUT;
         m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
         m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
@@ -5056,49 +4199,56 @@ static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
        init_rwsem(&m->mdt_squash_sem);
        spin_lock_init(&m->mdt_osfs_lock);
        m->mdt_osfs_age = cfs_time_shift_64(-1000);
+       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;
-       mite = &m->mdt_mite;
-       s->ld_md_site = mite;
-       mite->ms_lu = s;
+       s = mdt_lu_site(m);
+       ss_site = mdt_seq_site(m);
+       s->ld_seq_site = ss_site;
+       ss_site->ss_lu = s;
 
         /* set server index */
-       mite->ms_node_id = node_id;
+       ss_site->ss_node_id = node_id;
 
-        /* failover is the default
-         * FIXME: we do not failout mds0/mgs, which may cause some problems.
-         * assumed whose ms_node_id == 0 XXX
-         * */
+       /* failover is the default
+        * FIXME: we do not failout mds0/mgs, which may cause some problems.
+        * assumed whose ss_node_id == 0 XXX
+        * */
         obd->obd_replayable = 1;
         /* 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;
-                }
-        }
+       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 = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom);
-        if (rc)
-                GOTO(err_fini_stack, rc);
+       rc = mdt_fld_init(env, mdt_obd_name(m), m);
+       if (rc)
+               GOTO(err_fini_stack, rc);
+
+       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,
@@ -5115,13 +4265,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);
 
@@ -5129,30 +4292,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) {
@@ -5164,11 +4332,11 @@ static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
        if (rc)
                GOTO(err_procfs, rc);
 
-        rc = mdt_start_ptlrpc_service(m);
-        if (rc)
-               GOTO(err_quota, rc);
+       m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
+       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
@@ -5183,36 +4351,34 @@ static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
                 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
 
         RETURN(0);
-
-        ping_evictor_stop();
-        mdt_stop_ptlrpc_service(m);
-err_quota:
-       mdt_quota_fini(env, m);
 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);
-        mdt_fld_fini(env, m);
-        tgt_fini(env, &m->mdt_lut);
+       mdt_seq_fini(env, m);
+err_fini_fld:
+       mdt_fld_fini(env, m);
 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
@@ -5277,26 +4443,22 @@ 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,
                                              cfg, obd);
-               if (rc > 0 || rc == -ENOSYS)
-                       /* we don't understand; pass it on */
-                       rc = next->ld_ops->ldo_process_config(env, next, cfg);
+               if (rc > 0 || rc == -ENOSYS) {
+                       /* is it an HSM var ? */
+                       rc = class_process_proc_param(PARAM_HSM,
+                                                     hsm_cdt_get_proc_vars(),
+                                                     cfg, obd);
+                       if (rc > 0 || rc == -ENOSYS)
+                               /* we don't understand; pass it on */
+                               rc = next->ld_ops->ldo_process_config(env, next,
+                                                                     cfg);
+               }
 
                if (old_cfg != NULL)
                        lustre_cfg_free(cfg);
 
                break;
        }
-        case LCFG_ADD_MDC:
-                /*
-                 * Add mdc hook to get first MDT uuid and connect it to
-                 * ls->controller to use for seq manager.
-                 */
-                rc = next->ld_ops->ldo_process_config(env, next, cfg);
-                if (rc)
-                        CERROR("Can't add mdc, rc %d\n", rc);
-                else
-                        rc = mdt_seq_init_cli(env, mdt_dev(d), cfg);
-                break;
         default:
                 /* others are passed further */
                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
@@ -5313,12 +4475,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);
@@ -5326,9 +4488,10 @@ 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);
+               RETURN(o);
+       }
+       RETURN(NULL);
 }
 
 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
@@ -5363,11 +4526,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,
@@ -5387,6 +4553,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;
@@ -5401,21 +4568,30 @@ static int mdt_prepare(const struct lu_env *env,
        if (rc)
                RETURN(rc);
 
-       rc = mdt_fld_init(env, obd->obd_name, mdt);
+       rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
        if (rc)
                RETURN(rc);
 
-       rc = mdt_seq_init(env, obd->obd_name, mdt);
-       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 = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
-                                                 &mdt->mdt_md_root_fid);
-       if (rc)
-               RETURN(rc);
+       if (mdt->mdt_seq_site.ss_node_id == 0) {
+               rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
+                                                        &mdt->mdt_md_root_fid);
+               if (rc)
+                       RETURN(rc);
+       }
 
        LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
-       target_recovery_init(&mdt->mdt_lut, mdt_recovery_handle);
+       target_recovery_init(&mdt->mdt_lut, tgt_request_handle);
        set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
        LASSERT(obd->obd_no_conn);
        spin_lock(&obd->obd_dev_lock);
@@ -5428,7 +4604,7 @@ static int mdt_prepare(const struct lu_env *env,
        RETURN(rc);
 }
 
-static const struct lu_device_operations mdt_lu_ops = {
+const struct lu_device_operations mdt_lu_ops = {
         .ldo_object_alloc   = mdt_object_alloc,
         .ldo_process_config = mdt_process_config,
        .ldo_prepare        = mdt_prepare,
@@ -5466,12 +4642,12 @@ static int mdt_obd_set_info_async(const struct lu_env *env,
  * Compute the compatibility flags for a connection request based on
  * features mutually supported by client and server.
  *
- * The obd_export::exp_connect_flags field in \a exp must not be updated
- * here, otherwise a partially initialized value may be exposed. After
- * the connection request is successfully processed, the top-level MDT
- * connect request handler atomically updates the export connect flags
- * from the obd_connect_data::ocd_connect_flags field of the reply.
- * \see mdt_connect().
+ * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
+ * must not be updated here, otherwise a partially initialized value may
+ * be exposed. After the connection request is successfully processed,
+ * the top-level MDT connect request handler atomically updates the export
+ * connect flags from the obd_connect_data::ocd_connect_flags field of the
+ * reply. \see mdt_connect().
  *
  * \param exp   the obd_export associated with this client/target pair
  * \param mdt   the target device for the connection
@@ -5508,7 +4684,7 @@ static int mdt_connect_internal(struct obd_export *exp,
 
        if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
                data->ocd_brw_size = min(data->ocd_brw_size,
-                       (__u32)(PTLRPC_MAX_BRW_PAGES << CFS_PAGE_SHIFT));
+                                        (__u32)MD_MAX_BRW_SIZE);
                if (data->ocd_brw_size == 0) {
                        CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
                               " ocd_version: %x ocd_grant: %d "
@@ -5516,7 +4692,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);
@@ -5524,78 +4700,48 @@ static int mdt_connect_internal(struct obd_export *exp,
                }
        }
 
-       /* NB: Disregard the rule against updating exp_connect_flags in this
-        * case, since tgt_client_new() needs to know if this is a lightweight
-        * connection, and it is safe to expose this flag before connection
-        * processing completes. */
+       /* NB: Disregard the rule against updating
+        * exp_connect_data.ocd_connect_flags in this case, since
+        * tgt_client_new() needs to know if this is a lightweight
+        * connection, and it is safe to expose this flag before
+        * connection processing completes. */
        if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
                spin_lock(&exp->exp_lock);
-               exp->exp_connect_flags |=  OBD_CONNECT_LIGHTWEIGHT;
+               *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
                spin_unlock(&exp->exp_lock);
        }
 
        data->ocd_version = LUSTRE_VERSION_CODE;
-       exp->exp_mdt_data.med_ibits_known = data->ocd_ibits_known;
 
        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;
        }
 
        if (mdt->mdt_som_conf &&
-           !(data->ocd_connect_flags & (OBD_CONNECT_MDS_MDS|OBD_CONNECT_SOM))){
+           !(data->ocd_connect_flags & (OBD_CONNECT_LIGHTWEIGHT |
+                                        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;
        }
 
-       return 0;
-}
-
-static int mdt_connect_check_sptlrpc(struct mdt_device *mdt,
-                                    struct obd_export *exp,
-                                    struct ptlrpc_request *req)
-{
-       struct sptlrpc_flavor   flvr;
-       int                     rc = 0;
+       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);
+                       spin_unlock(&exp->exp_obd->obd_dev_lock);
+               } else {
+                       data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
+               }
+       }
 
-       if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
-               read_lock(&mdt->mdt_sptlrpc_lock);
-               sptlrpc_target_choose_flavor(&mdt->mdt_sptlrpc_rset,
-                                            req->rq_sp_from,
-                                            req->rq_peer.nid,
-                                            &flvr);
-               read_unlock(&mdt->mdt_sptlrpc_lock);
+       data->ocd_max_easize = mdt->mdt_max_ea_size;
 
-               spin_lock(&exp->exp_lock);
-
-                exp->exp_sp_peer = req->rq_sp_from;
-                exp->exp_flvr = flvr;
-
-                if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_ANY &&
-                    exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
-                        CERROR("unauthorized rpc flavor %x from %s, "
-                               "expect %x\n", req->rq_flvr.sf_rpc,
-                               libcfs_nid2str(req->rq_peer.nid),
-                               exp->exp_flvr.sf_rpc);
-                        rc = -EACCES;
-                }
-
-               spin_unlock(&exp->exp_lock);
-        } else {
-                if (exp->exp_sp_peer != req->rq_sp_from) {
-                        CERROR("RPC source %s doesn't match %s\n",
-                               sptlrpc_part2name(req->rq_sp_from),
-                               sptlrpc_part2name(exp->exp_sp_peer));
-                        rc = -EACCES;
-                } else {
-                        rc = sptlrpc_target_export_check(exp, req);
-                }
-        }
-
-        return rc;
-}
+       return 0;
+}
 
 /* mds_connect copy */
 static int mdt_obd_connect(const struct lu_env *env,
@@ -5604,11 +4750,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;
 
@@ -5616,16 +4760,15 @@ 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
         * 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)) {
+       if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) && data != NULL &&
+           !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
                rc = obd_health_check(env, mdt->mdt_child_exp->exp_obd);
                if (rc)
                        RETURN(-EAGAIN);
@@ -5639,26 +4782,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;
@@ -5675,23 +4809,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);
@@ -5699,6 +4822,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;
@@ -5743,6 +4891,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;
@@ -5839,32 +5005,199 @@ static int mdt_destroy_export(struct obd_export *exp)
         RETURN(0);
 }
 
-static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key,
-                            void *val, int vallen)
+/** 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)
 {
-        struct mdt_device *mdt = mdt_dev(info->mti_exp->exp_obd->obd_lu_dev);
-        struct getinfo_fid2path *fpout, *fpin;
-        int rc = 0;
+       int rc;
 
-        fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
-        fpout = val;
+       LASSERT(ldata->ld_buf->lb_buf != NULL);
+
+       if (!mdt_object_exists(mdt_obj))
+               return -ENODATA;
+
+       rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
+                         ldata->ld_buf, XATTR_NAME_LINK);
+       if (rc == -ERANGE) {
+               /* Buf was too small, figure out what we need. */
+               lu_buf_free(ldata->ld_buf);
+               rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
+                                 ldata->ld_buf, XATTR_NAME_LINK);
+               if (rc < 0)
+                       return rc;
+               ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
+               if (ldata->ld_buf->lb_buf == NULL)
+                       return -ENOMEM;
+               rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
+                                 ldata->ld_buf, XATTR_NAME_LINK);
+       }
+       if (rc < 0)
+               return rc;
 
-        if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
-                lustre_swab_fid2path(fpin);
+       return linkea_init(ldata);
+}
 
-        memcpy(fpout, fpin, sizeof(*fpin));
-        if (fpout->gf_pathlen != vallen - sizeof(*fpin))
-                RETURN(-EINVAL);
+static int mdt_path_current(struct mdt_thread_info *info,
+                           struct path_lookup_info *pli)
+{
+       struct mdt_device       *mdt = info->mti_mdt;
+       struct mdt_object       *mdt_obj;
+       struct link_ea_header   *leh;
+       struct link_ea_entry    *lee;
+       struct lu_name          *tmpname = &info->mti_name;
+       struct lu_fid           *tmpfid = &info->mti_tmp_fid1;
+       struct lu_buf           *buf = &info->mti_big_buf;
+       char                    *ptr;
+       int                     reclen;
+       struct linkea_data      ldata = { 0 };
+       int                     rc = 0;
+       ENTRY;
 
-        rc = mdt_fid2path(info->mti_env, mdt, fpout);
-        RETURN(rc);
+       /* temp buffer for path element, the buffer will be finally freed
+        * in mdt_thread_info_fini */
+       buf = lu_buf_check_and_alloc(buf, PATH_MAX);
+       if (buf->lb_buf == NULL)
+               RETURN(-ENOMEM);
+
+       ldata.ld_buf = buf;
+       ptr = pli->pli_path + pli->pli_pathlen - 1;
+       *ptr = 0;
+       --ptr;
+       pli->pli_fidcount = 0;
+       pli->pli_fids[0] = *(struct lu_fid *)mdt_object_fid(pli->pli_mdt_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]);
+               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);
+               }
+
+               rc = mdt_links_read(info, mdt_obj, &ldata);
+               mdt_object_put(info->mti_env, mdt_obj);
+               if (rc != 0)
+                       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) {
+                       int count;
+                       for (count = 0; count < pli->pli_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)
+                               /* indicate to user there are more links */
+                               pli->pli_linkno++;
+               }
+
+               /* Pack the name in the end of the buffer */
+               ptr -= tmpname->ln_namelen;
+               if (ptr - 1 <= pli->pli_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;
+       }
+
+remote_out:
+       ptr++; /* skip leading / */
+       memmove(pli->pli_path, ptr, pli->pli_path + pli->pli_pathlen - ptr);
+
+       EXIT;
+out:
+       return rc;
+}
+
+/* Returns the full path to this fid, as of changelog record recno. */
+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 mdt_device       *mdt = info->mti_mdt;
+       struct path_lookup_info *pli;
+       int                     tries = 3;
+       int                     rc = -EAGAIN;
+       ENTRY;
+
+       if (pathlen < 3)
+               RETURN(-EOVERFLOW);
+
+       if (lu_fid_eq(&mdt->mdt_md_root_fid, mdt_object_fid(obj))) {
+               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);
+
+       RETURN(rc);
 }
 
-static int mdt_fid2path(const struct lu_env *env, struct mdt_device *mdt,
-               struct getinfo_fid2path *fp)
+static int mdt_fid2path(struct mdt_thread_info *info,
+                       struct getinfo_fid2path *fp)
 {
+       struct mdt_device *mdt = info->mti_mdt;
        struct mdt_object *obj;
-       struct obd_device *obd = mdt2obd_dev(mdt);
        int    rc;
        ENTRY;
 
@@ -5874,119 +5207,156 @@ static int mdt_fid2path(const struct lu_env *env, struct mdt_device *mdt,
        if (!fid_is_sane(&fp->gf_fid))
                RETURN(-EINVAL);
 
-       if (!fid_is_norm(&fp->gf_fid) && !fid_is_igif(&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(env, mdt, &fp->gf_fid);
+       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);
        }
 
-       rc = lu_object_exists(&obj->mot_obj.mo_lu);
-       if (rc <= 0) {
-               if (rc == -1)
-                       rc = -EREMOTE;
-               else
-                       rc = -ENOENT;
-               mdt_object_put(env, obj);
+       if (mdt_object_remote(obj))
+               rc = -EREMOTE;
+       else if (!mdt_object_exists(obj))
+               rc = -ENOENT;
+       else
+               rc = 0;
+
+       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);
        }
 
-       rc = mo_path(env, md_object_next(&obj->mot_obj), fp->gf_path,
-                       fp->gf_pathlen, &fp->gf_recno, &fp->gf_linkno);
-       mdt_object_put(env, obj);
+       rc = mdt_path(info, obj, fp->gf_path, fp->gf_pathlen, &fp->gf_recno,
+                     &fp->gf_linkno, &fp->gf_fid);
+
+       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);
+
+       mdt_object_put(info->mti_env, obj);
 
        RETURN(rc);
 }
 
-static int mdt_get_info(struct mdt_thread_info *info)
+static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key,
+                           void *val, int vallen)
 {
-        struct ptlrpc_request *req = mdt_info_req(info);
-        char *key;
-        int keylen;
-        __u32 *vallen;
-        void *valout;
-        int rc;
-        ENTRY;
+       struct getinfo_fid2path *fpout, *fpin;
+       int rc = 0;
 
-        key = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_KEY);
-        if (key == NULL) {
-                CDEBUG(D_IOCTL, "No GETINFO key");
-                RETURN(-EFAULT);
-        }
-        keylen = req_capsule_get_size(info->mti_pill, &RMF_GETINFO_KEY,
-                                      RCL_CLIENT);
+       fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
+       fpout = val;
 
-        vallen = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_VALLEN);
-        if (vallen == NULL) {
-                CDEBUG(D_IOCTL, "Unable to get RMF_GETINFO_VALLEN buffer");
-                RETURN(-EFAULT);
-        }
+       if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
+               lustre_swab_fid2path(fpin);
 
-        req_capsule_set_size(info->mti_pill, &RMF_GETINFO_VAL, RCL_SERVER,
-                             *vallen);
-        rc = req_capsule_server_pack(info->mti_pill);
-        valout = req_capsule_server_get(info->mti_pill, &RMF_GETINFO_VAL);
-        if (valout == NULL) {
-                CDEBUG(D_IOCTL, "Unable to get get-info RPC out buffer");
-                RETURN(-EFAULT);
-        }
+       memcpy(fpout, fpin, sizeof(*fpin));
+       if (fpout->gf_pathlen != vallen - sizeof(*fpin))
+               RETURN(-EINVAL);
 
-        if (KEY_IS(KEY_FID2PATH))
-                rc = mdt_rpc_fid2path(info, key, valout, *vallen);
-        else
-                rc = -EINVAL;
+       rc = mdt_fid2path(info, fpout);
+       RETURN(rc);
+}
 
-        lustre_msg_set_status(req->rq_repmsg, rc);
+int mdt_get_info(struct tgt_session_info *tsi)
+{
+       char    *key;
+       int      keylen;
+       __u32   *vallen;
+       void    *valout;
+       int      rc;
 
-        RETURN(rc);
+       ENTRY;
+
+       key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
+       if (key == NULL) {
+               CDEBUG(D_IOCTL, "No GETINFO key");
+               RETURN(err_serious(-EFAULT));
+       }
+       keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
+                                     RCL_CLIENT);
+
+       vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
+       if (vallen == NULL) {
+               CDEBUG(D_IOCTL, "Unable to get RMF_GETINFO_VALLEN buffer");
+               RETURN(err_serious(-EFAULT));
+       }
+
+       req_capsule_set_size(tsi->tsi_pill, &RMF_GETINFO_VAL, RCL_SERVER,
+                            *vallen);
+       rc = req_capsule_server_pack(tsi->tsi_pill);
+       if (rc)
+               RETURN(err_serious(rc));
+
+       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");
+               RETURN(err_serious(-EFAULT));
+       }
+
+       if (KEY_IS(KEY_FID2PATH)) {
+               struct mdt_thread_info  *info = tsi2mdt_info(tsi);
+
+               rc = mdt_rpc_fid2path(info, key, valout, *vallen);
+               mdt_thread_info_fini(info);
+       } else {
+               rc = -EINVAL;
+       }
+       RETURN(rc);
 }
 
 /* Pass the ioc down */
 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
-                         unsigned int cmd, int len, void *data)
+                        unsigned int cmd, int len, void *data)
 {
-        struct lu_context ioctl_session;
-        struct md_device *next = mdt->mdt_child;
-        int rc;
-        ENTRY;
+       struct lu_context ioctl_session;
+       struct md_device *next = mdt->mdt_child;
+       int rc;
+       ENTRY;
 
-        rc = lu_context_init(&ioctl_session, LCT_SESSION);
-        if (rc)
-                RETURN(rc);
-        ioctl_session.lc_thread = (struct ptlrpc_thread *)cfs_current();
-        lu_context_enter(&ioctl_session);
-        env->le_ses = &ioctl_session;
+       rc = lu_context_init(&ioctl_session, LCT_SESSION);
+       if (rc)
+               RETURN(rc);
+       ioctl_session.lc_thread = (struct ptlrpc_thread *)current;
+       lu_context_enter(&ioctl_session);
+       env->le_ses = &ioctl_session;
 
-        LASSERT(next->md_ops->mdo_iocontrol);
-        rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
+       LASSERT(next->md_ops->mdo_iocontrol);
+       rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
 
-        lu_context_exit(&ioctl_session);
-        lu_context_fini(&ioctl_session);
-        RETURN(rc);
+       lu_context_exit(&ioctl_session);
+       lu_context_fini(&ioctl_session);
+       RETURN(rc);
 }
 
 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
 {
-        struct obd_ioctl_data *data = karg;
-        struct lu_fid *fid = (struct lu_fid *)data->ioc_inlbuf1;
-        __u64 version;
-        struct mdt_object *obj;
-        struct mdt_lock_handle  *lh;
-        int rc;
-        ENTRY;
+       struct obd_ioctl_data *data = karg;
+       struct lu_fid *fid;
+       __u64 version;
+       struct mdt_object *obj;
+       struct mdt_lock_handle  *lh;
+       int rc;
+       ENTRY;
 
-        CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
-        if (!fid_is_sane(fid))
-                RETURN(-EINVAL);
+       if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
+           data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
+               RETURN(-EINVAL);
+
+       fid = (struct lu_fid *)data->ioc_inlbuf1;
+
+       if (!fid_is_sane(fid))
+               RETURN(-EINVAL);
+
+       CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
 
         lh = &mti->mti_lh[MDT_LH_PARENT];
         mdt_lock_reg_init(lh, LCK_CR);
@@ -5995,24 +5365,23 @@ static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
         if (IS_ERR(obj))
                 RETURN(PTR_ERR(obj));
 
-        rc = mdt_object_exists(obj);
-        if (rc < 0) {
-                rc = -EREMOTE;
-                /**
-                 * before calling version get the correct MDS should be
-                 * fid, this is error to find remote object here
-                 */
-                CERROR("nonlocal object "DFID"\n", PFID(fid));
-        } else if (rc == 0) {
-                *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
-                rc = -ENOENT;
-        } else {
-                version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
-               *(__u64 *)data->ioc_inlbuf2 = version;
-                rc = 0;
-        }
-        mdt_object_unlock_put(mti, obj, lh, 1);
-        RETURN(rc);
+       if (mdt_object_remote(obj)) {
+               rc = -EREMOTE;
+               /**
+                * before calling version get the correct MDS should be
+                * fid, this is error to find remote object here
+                */
+               CERROR("nonlocal object "DFID"\n", PFID(fid));
+       } else if (!mdt_object_exists(obj)) {
+               *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
+               rc = -ENOENT;
+       } else {
+               version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
+              *(__u64 *)data->ioc_inlbuf2 = version;
+               rc = 0;
+       }
+       mdt_object_unlock_put(mti, obj, lh, 1);
+       RETURN(rc);
 }
 
 /* ioctls on obd dev */
@@ -6038,29 +5407,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: {
@@ -6074,11 +5449,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);
@@ -6107,63 +5482,6 @@ int mdt_obd_postrecov(struct obd_device *obd)
         return rc;
 }
 
-/**
- * Send a copytool req to a client
- * Note this sends a request RPC from a server (MDT) to a client (MDC),
- * backwards of normal comms.
- */
-int mdt_hsm_copytool_send(struct obd_export *exp)
-{
-        struct kuc_hdr *lh;
-        struct hsm_action_list *hal;
-        struct hsm_action_item *hai;
-        int rc, len;
-        ENTRY;
-
-        CWARN("%s: writing to mdc at %s\n", exp->exp_obd->obd_name,
-              libcfs_nid2str(exp->exp_connection->c_peer.nid));
-
-        len = sizeof(*lh) + sizeof(*hal) + MTI_NAME_MAXLEN +
-                /* for mockup below */ 2 * cfs_size_round(sizeof(*hai));
-        OBD_ALLOC(lh, len);
-        if (lh == NULL)
-                RETURN(-ENOMEM);
-
-        lh->kuc_magic = KUC_MAGIC;
-        lh->kuc_transport = KUC_TRANSPORT_HSM;
-        lh->kuc_msgtype = HMT_ACTION_LIST;
-        lh->kuc_msglen = len;
-
-        hal = (struct hsm_action_list *)(lh + 1);
-        hal->hal_version = HAL_VERSION;
-        hal->hal_archive_num = 1;
-        obd_uuid2fsname(hal->hal_fsname, exp->exp_obd->obd_name,
-                        MTI_NAME_MAXLEN);
-
-        /* mock up an action list */
-        hal->hal_count = 2;
-        hai = hai_zero(hal);
-        hai->hai_action = HSMA_ARCHIVE;
-        hai->hai_fid.f_oid = 0xA00A;
-        hai->hai_len = sizeof(*hai);
-        hai = hai_next(hai);
-        hai->hai_action = HSMA_RESTORE;
-        hai->hai_fid.f_oid = 0xB00B;
-        hai->hai_len = sizeof(*hai);
-
-        /* Uses the ldlm reverse import; this rpc will be seen by
-          the ldlm_callback_handler */
-        rc = do_set_info_async(exp->exp_imp_reverse,
-                               LDLM_SET_INFO, LUSTRE_OBD_VERSION,
-                               sizeof(KEY_HSM_COPYTOOL_SEND),
-                               KEY_HSM_COPYTOOL_SEND,
-                               len, lh, NULL);
-
-        OBD_FREE(lh, len);
-
-        RETURN(rc);
-}
-
 static struct obd_ops mdt_obd_device_ops = {
         .o_owner          = THIS_MODULE,
         .o_set_info_async = mdt_obd_set_info_async,
@@ -6189,12 +5507,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,
@@ -6208,7 +5527,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);
@@ -6285,16 +5604,7 @@ int mdt_cos_is_enabled(struct mdt_device *mdt)
         return mdt->mdt_opts.mo_cos != 0;
 }
 
-/* type constructor/destructor: mdt_type_init, mdt_type_fini */
-LU_TYPE_INIT_FINI(mdt, &mdt_thread_key);
-
 static struct lu_device_type_operations mdt_device_type_ops = {
-        .ldto_init = mdt_type_init,
-        .ldto_fini = mdt_type_fini,
-
-        .ldto_start = mdt_type_start,
-        .ldto_stop  = mdt_type_stop,
-
         .ldto_device_alloc = mdt_device_alloc,
         .ldto_device_free  = mdt_device_free,
         .ldto_device_fini  = mdt_device_fini
@@ -6309,260 +5619,43 @@ static struct lu_device_type mdt_device_type = {
 
 static int __init mdt_mod_init(void)
 {
-        struct lprocfs_static_vars lvars;
-        int rc;
+       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;
 
-       if (mdt_num_threads != 0 && mds_num_threads == 0) {
-               LCONSOLE_INFO("mdt_num_threads module parameter is deprecated,"
-                             "use mds_num_threads instead or unset both for"
-                             "dynamic thread startup\n");
-               mds_num_threads = mdt_num_threads;
-       }
-
-        lprocfs_mdt_init_vars(&lvars);
-        rc = class_register_type(&mdt_obd_device_ops, NULL,
-                                 lvars.module_vars, LUSTRE_MDT_NAME,
-                                 &mdt_device_type);
+       rc = mds_mod_init();
+       if (rc)
+               GOTO(lu_fini, rc);
 
+       lprocfs_mdt_init_vars(&lvars);
+       rc = class_register_type(&mdt_obd_device_ops, NULL,
+                                lvars.module_vars, LUSTRE_MDT_NAME,
+                                &mdt_device_type);
+       if (rc)
+               GOTO(mds_fini, rc);
+lu_fini:
        if (rc)
                lu_kmem_fini(mdt_caches);
-        return rc;
+mds_fini:
+       if (rc)
+               mds_mod_exit();
+       return rc;
 }
 
 static void __exit mdt_mod_exit(void)
 {
-        class_unregister_type(LUSTRE_MDT_NAME);
+       class_unregister_type(LUSTRE_MDT_NAME);
+       mds_mod_exit();
        lu_kmem_fini(mdt_caches);
 }
 
-#define DEFINE_RPC_HANDLER(base, flags, opc, fn, fmt)                  \
-[opc - base] = {                                                       \
-       .mh_name        = #opc,                                         \
-       .mh_fail_id     = OBD_FAIL_ ## opc ## _NET,                     \
-       .mh_opc         = opc,                                          \
-       .mh_flags       = flags,                                        \
-       .mh_act         = fn,                                           \
-       .mh_fmt         = fmt                                           \
-}
-
-/* Request with a format known in advance */
-#define DEF_MDT_HDL(flags, name, fn)                                   \
-       DEFINE_RPC_HANDLER(MDS_GETATTR, flags, name, fn, &RQF_ ## name)
-
-/* Request with a format we do not yet know */
-#define DEF_MDT_HDL_VAR(flags, name, fn)                               \
-       DEFINE_RPC_HANDLER(MDS_GETATTR, flags, name, fn, NULL)
-
-/* Map one non-standard request format handler.  This should probably get
- * a common OBD_SET_INFO RPC opcode instead of this mismatch. */
-#define RQF_MDS_SET_INFO RQF_OBD_SET_INFO
-
-static struct mdt_handler mdt_mds_ops[] = {
-DEF_MDT_HDL(0,                         MDS_CONNECT,      mdt_connect),
-DEF_MDT_HDL(0,                         MDS_DISCONNECT,   mdt_disconnect),
-DEF_MDT_HDL(0,                         MDS_SET_INFO,     mdt_set_info),
-DEF_MDT_HDL(0,                         MDS_GET_INFO,     mdt_get_info),
-DEF_MDT_HDL(0          | HABEO_REFERO, MDS_GETSTATUS,    mdt_getstatus),
-DEF_MDT_HDL(HABEO_CORPUS,              MDS_GETATTR,      mdt_getattr),
-DEF_MDT_HDL(HABEO_CORPUS| HABEO_REFERO,        MDS_GETATTR_NAME, mdt_getattr_name),
-DEF_MDT_HDL(HABEO_CORPUS,              MDS_GETXATTR,     mdt_getxattr),
-DEF_MDT_HDL(0          | HABEO_REFERO, MDS_STATFS,       mdt_statfs),
-DEF_MDT_HDL(0          | MUTABOR,      MDS_REINT,        mdt_reint),
-DEF_MDT_HDL(HABEO_CORPUS,              MDS_CLOSE,        mdt_close),
-DEF_MDT_HDL(HABEO_CORPUS,              MDS_DONE_WRITING, mdt_done_writing),
-DEF_MDT_HDL(0          | HABEO_REFERO, MDS_PIN,          mdt_pin),
-DEF_MDT_HDL_VAR(0,                     MDS_SYNC,         mdt_sync),
-DEF_MDT_HDL(HABEO_CORPUS| HABEO_REFERO,        MDS_IS_SUBDIR,    mdt_is_subdir),
-DEF_MDT_HDL(0,                         MDS_QUOTACHECK,   mdt_quotacheck),
-DEF_MDT_HDL(0,                         MDS_QUOTACTL,     mdt_quotactl)
-};
-
-#define DEF_OBD_HDL(flags, name, fn)                                   \
-       DEFINE_RPC_HANDLER(OBD_PING, flags, name, fn, NULL)
-
-static struct mdt_handler mdt_obd_ops[] = {
-DEF_OBD_HDL(0,                         OBD_PING,         mdt_obd_ping),
-DEF_OBD_HDL(0,                         OBD_LOG_CANCEL,   mdt_obd_log_cancel),
-DEF_OBD_HDL(0,                         OBD_QC_CALLBACK,  mdt_obd_qc_callback),
-DEF_OBD_HDL(0,                         OBD_IDX_READ,     mdt_obd_idx_read)
-};
-
-#define DEF_DLM_HDL_VAR(flags, name, fn)                               \
-       DEFINE_RPC_HANDLER(LDLM_ENQUEUE, flags, name, fn, NULL)
-#define DEF_DLM_HDL(flags, name, fn)                                   \
-       DEFINE_RPC_HANDLER(LDLM_ENQUEUE, flags, name, fn, &RQF_ ## name)
-
-static struct mdt_handler mdt_dlm_ops[] = {
-DEF_DLM_HDL    (HABEO_CLAVIS,          LDLM_ENQUEUE,     mdt_enqueue),
-DEF_DLM_HDL_VAR(HABEO_CLAVIS,          LDLM_CONVERT,     mdt_convert),
-DEF_DLM_HDL_VAR(0,                     LDLM_BL_CALLBACK, mdt_bl_callback),
-DEF_DLM_HDL_VAR(0,                     LDLM_CP_CALLBACK, mdt_cp_callback)
-};
-
-#define DEF_LLOG_HDL(flags, name, fn)                                  \
-       DEFINE_RPC_HANDLER(LLOG_ORIGIN_HANDLE_CREATE, flags, name, fn, NULL)
-
-static struct mdt_handler mdt_llog_ops[] = {
-DEF_LLOG_HDL(0,                LLOG_ORIGIN_HANDLE_CREATE,        mdt_llog_create),
-DEF_LLOG_HDL(0,                LLOG_ORIGIN_HANDLE_NEXT_BLOCK,    mdt_llog_next_block),
-DEF_LLOG_HDL(0,                LLOG_ORIGIN_HANDLE_READ_HEADER,   mdt_llog_read_header),
-DEF_LLOG_HDL(0,                LLOG_ORIGIN_HANDLE_WRITE_REC,     NULL),
-DEF_LLOG_HDL(0,                LLOG_ORIGIN_HANDLE_CLOSE,         NULL),
-DEF_LLOG_HDL(0,                LLOG_ORIGIN_CONNECT,              NULL),
-DEF_LLOG_HDL(0,                LLOG_CATINFO,                     NULL),
-DEF_LLOG_HDL(0,                LLOG_ORIGIN_HANDLE_PREV_BLOCK,    mdt_llog_prev_block),
-DEF_LLOG_HDL(0,                LLOG_ORIGIN_HANDLE_DESTROY,       mdt_llog_destroy),
-};
-
-#define DEF_SEC_HDL(flags, name, fn)                                   \
-       DEFINE_RPC_HANDLER(SEC_CTX_INIT, flags, name, fn, NULL)
-
-static struct mdt_handler mdt_sec_ctx_ops[] = {
-DEF_SEC_HDL(0,                         SEC_CTX_INIT,     mdt_sec_ctx_handle),
-DEF_SEC_HDL(0,                         SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
-DEF_SEC_HDL(0,                         SEC_CTX_FINI,     mdt_sec_ctx_handle)
-};
-
-#define DEF_QUOTA_HDL(flags, name, fn)                         \
-       DEFINE_RPC_HANDLER(QUOTA_DQACQ, flags, name, fn, &RQF_ ## name)
-
-static struct mdt_handler mdt_quota_ops[] = {
-DEF_QUOTA_HDL(HABEO_REFERO,            QUOTA_DQACQ,      mdt_quota_dqacq),
-};
-
-static struct mdt_opc_slice mdt_regular_handlers[] = {
-        {
-                .mos_opc_start = MDS_GETATTR,
-                .mos_opc_end   = MDS_LAST_OPC,
-                .mos_hs        = mdt_mds_ops
-        },
-        {
-                .mos_opc_start = OBD_PING,
-                .mos_opc_end   = OBD_LAST_OPC,
-                .mos_hs        = mdt_obd_ops
-        },
-        {
-                .mos_opc_start = LDLM_ENQUEUE,
-                .mos_opc_end   = LDLM_LAST_OPC,
-                .mos_hs        = mdt_dlm_ops
-        },
-        {
-                .mos_opc_start = LLOG_ORIGIN_HANDLE_CREATE,
-                .mos_opc_end   = LLOG_LAST_OPC,
-                .mos_hs        = mdt_llog_ops
-        },
-        {
-                .mos_opc_start = SEC_CTX_INIT,
-                .mos_opc_end   = SEC_LAST_OPC,
-                .mos_hs        = mdt_sec_ctx_ops
-        },
-       {
-               .mos_opc_start = QUOTA_DQACQ,
-               .mos_opc_end   = QUOTA_LAST_OPC,
-               .mos_hs        = mdt_quota_ops
-       },
-        {
-                .mos_hs        = NULL
-        }
-};
-
-/* Readpage/readdir handlers */
-static struct mdt_handler mdt_readpage_ops[] = {
-DEF_MDT_HDL(0,                 MDS_CONNECT,  mdt_connect),
-DEF_MDT_HDL(HABEO_CORPUS | HABEO_REFERO, MDS_READPAGE, mdt_readpage),
-/* XXX: this is ugly and should be fixed one day, see mdc_close() for
- * detailed comments. --umka */
-DEF_MDT_HDL(HABEO_CORPUS,              MDS_CLOSE,        mdt_close),
-DEF_MDT_HDL(HABEO_CORPUS,              MDS_DONE_WRITING, mdt_done_writing),
-};
-
-static struct mdt_opc_slice mdt_readpage_handlers[] = {
-        {
-                .mos_opc_start = MDS_GETATTR,
-                .mos_opc_end   = MDS_LAST_OPC,
-                .mos_hs        = mdt_readpage_ops
-        },
-       {
-               .mos_opc_start = OBD_FIRST_OPC,
-               .mos_opc_end   = OBD_LAST_OPC,
-               .mos_hs        = mdt_obd_ops
-       },
-        {
-                .mos_hs        = NULL
-        }
-};
-
-/* Cross MDT operation handlers for DNE */
-static struct mdt_handler mdt_xmds_ops[] = {
-DEF_MDT_HDL(0,                         MDS_CONNECT,      mdt_connect),
-DEF_MDT_HDL(HABEO_CORPUS,              MDS_GETATTR,      mdt_getattr),
-DEF_MDT_HDL(0          | MUTABOR,      MDS_REINT,        mdt_reint),
-DEF_MDT_HDL(HABEO_CORPUS| HABEO_REFERO,        MDS_IS_SUBDIR,    mdt_is_subdir),
-};
-
-static struct mdt_opc_slice mdt_xmds_handlers[] = {
-        {
-                .mos_opc_start = MDS_GETATTR,
-                .mos_opc_end   = MDS_LAST_OPC,
-                .mos_hs        = mdt_xmds_ops
-        },
-        {
-                .mos_opc_start = OBD_PING,
-                .mos_opc_end   = OBD_LAST_OPC,
-                .mos_hs        = mdt_obd_ops
-        },
-        {
-                .mos_opc_start = SEC_CTX_INIT,
-                .mos_opc_end   = SEC_LAST_OPC,
-                .mos_hs        = mdt_sec_ctx_ops
-        },
-        {
-                .mos_hs        = NULL
-        }
-};
-
-/* Sequence service handlers */
-#define DEF_SEQ_HDL(flags, name, fn)                                   \
-       DEFINE_RPC_HANDLER(SEQ_QUERY, flags, name, fn, &RQF_ ## name)
-
-static struct mdt_handler mdt_seq_ops[] = {
-DEF_SEQ_HDL(0,                         SEQ_QUERY,        (void *)seq_query),
-};
-
-static struct mdt_opc_slice mdt_seq_handlers[] = {
-        {
-                .mos_opc_start = SEQ_QUERY,
-                .mos_opc_end   = SEQ_LAST_OPC,
-                .mos_hs        = mdt_seq_ops
-        },
-        {
-                .mos_hs        = NULL
-        }
-};
-
-/* FID Location Database handlers */
-#define DEF_FLD_HDL(flags, name, fn)                                   \
-       DEFINE_RPC_HANDLER(FLD_QUERY, flags, name, fn, &RQF_ ## name)
-
-static struct mdt_handler mdt_fld_ops[] = {
-DEF_FLD_HDL(0,                         FLD_QUERY,        (void *)fld_query),
-};
-
-static struct mdt_opc_slice mdt_fld_handlers[] = {
-        {
-                .mos_opc_start = FLD_QUERY,
-                .mos_opc_end   = FLD_LAST_OPC,
-                .mos_hs        = mdt_fld_ops
-        },
-        {
-                .mos_hs        = NULL
-        }
-};
-
 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
 MODULE_LICENSE("GPL");