Whamcloud - gitweb
LU-1067 obdecho: Recheck client env ctx for echo md client.
[fs/lustre-release.git] / lustre / obdecho / echo_client.c
index 01b9572..5deec15 100644 (file)
  * GPL HEADER END
  */
 /*
- * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
+ *
+ * Copyright (c) 2011 Whamcloud, Inc.
+ *
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
 #include <lustre_debug.h>
 #include <lprocfs_status.h>
 #include <cl_object.h>
+#include <lustre_fid.h>
+#include <lustre_acl.h>
 
 #include "echo_internal.h"
 
+/** \defgroup echo_client Echo Client
+ * @{
+ */
+
 struct echo_device {
         struct cl_device        ed_cl;
         struct echo_client_obd *ed_ec;
@@ -58,6 +67,8 @@ struct echo_device {
         struct cl_site         *ed_site;
         struct lu_device       *ed_next;
         int                     ed_next_islov;
+        int                     ed_next_ismd;
+        struct lu_client_seq   *ed_cl_seq;
 };
 
 struct echo_object {
@@ -65,9 +76,9 @@ struct echo_object {
         struct cl_object_header eo_hdr;
 
         struct echo_device     *eo_dev;
-        struct list_head        eo_obj_chain;
+        cfs_list_t              eo_obj_chain;
         struct lov_stripe_md   *eo_lsm;
-        atomic_t                eo_npages;
+        cfs_atomic_t            eo_npages;
         int                     eo_deleted;
 };
 
@@ -78,15 +89,16 @@ struct echo_object_conf {
 
 struct echo_page {
         struct cl_page_slice   ep_cl;
-        struct cl_sync_io     *ep_sync_io;
+        cfs_mutex_t            ep_lock;
         cfs_page_t            *ep_vmpage;
 };
 
 struct echo_lock {
         struct cl_lock_slice   el_cl;
-        struct list_head       el_chain;
+        cfs_list_t             el_chain;
         struct echo_object    *el_object;
         __u64                  el_cookie;
+        cfs_atomic_t           el_refcount;
 };
 
 struct echo_io {
@@ -99,12 +111,13 @@ struct echo_req {
 };
 #endif
 
-static int echo_client_setup(struct obd_device *obddev,
+static int echo_client_setup(const struct lu_env *env,
+                             struct obd_device *obddev,
                              struct lustre_cfg *lcfg);
 static int echo_client_cleanup(struct obd_device *obddev);
 
 
-/** \defgroup echo_helpers
+/** \defgroup echo_helpers Helper functions
  * @{
  */
 static inline struct echo_device *cl2echo_dev(const struct cl_device *dev)
@@ -165,8 +178,10 @@ struct echo_object_conf *cl2echo_conf(const struct cl_object_conf *c)
 static inline void lsm2fid(struct lov_stripe_md *lsm, struct lu_fid *fid)
 {
         fid_zero(fid);
-        fid->f_seq = lsm->lsm_object_gr << 16 | lsm->lsm_object_id >> 32;
+        fid->f_seq = FID_SEQ_ECHO;
+        /* truncated to 32 bits by assignment */
         fid->f_oid = lsm->lsm_object_id;
+        fid->f_ver = lsm->lsm_object_id >> 32;
 }
 /** @} echo_helpers */
 
@@ -187,9 +202,15 @@ struct echo_thread_info {
 
         struct cl_2queue        eti_queue;
         struct cl_io            eti_io;
-        struct cl_sync_io       eti_anchor;
         struct cl_lock_descr    eti_descr;
         struct lu_fid           eti_fid;
+        struct md_op_spec       eti_spec;
+        struct lov_mds_md_v3    eti_lmm;
+        struct lov_user_md_v3   eti_lum;
+        struct md_attr          eti_ma;
+        struct lu_name          eti_lname;
+        char                    eti_name[20];
+        char                    eti_xattr_buf[LUSTRE_POSIX_ACL_MAX_SIZE];
 };
 
 /* No session used right now */
@@ -242,7 +263,7 @@ static struct lu_kmem_descr echo_caches[] = {
         }
 };
 
-/** defgroup echo_page echo_page
+/** \defgroup echo_page Page operations
  *
  * Echo page operations.
  *
@@ -254,9 +275,32 @@ cfs_page_t *echo_page_vmpage(const struct lu_env *env,
         return cl2echo_page(slice)->ep_vmpage;
 }
 
+static int echo_page_own(const struct lu_env *env,
+                         const struct cl_page_slice *slice,
+                         struct cl_io *io, int nonblock)
+{
+        struct echo_page *ep = cl2echo_page(slice);
+
+        if (!nonblock)
+                cfs_mutex_lock(&ep->ep_lock);
+        else if (!cfs_mutex_trylock(&ep->ep_lock))
+                return -EAGAIN;
+        return 0;
+}
+
+static void echo_page_disown(const struct lu_env *env,
+                             const struct cl_page_slice *slice,
+                             struct cl_io *io)
+{
+        struct echo_page *ep = cl2echo_page(slice);
+
+        LASSERT(cfs_mutex_is_locked(&ep->ep_lock));
+        cfs_mutex_unlock(&ep->ep_lock);
+}
+
 static void echo_page_discard(const struct lu_env *env,
                               const struct cl_page_slice *slice,
-                              struct cl_io *_)
+                              struct cl_io *unused)
 {
         cl_page_delete(env, slice->cpl_page);
 }
@@ -264,21 +308,16 @@ static void echo_page_discard(const struct lu_env *env,
 static int echo_page_is_vmlocked(const struct lu_env *env,
                                  const struct cl_page_slice *slice)
 {
-        return 1;
+        if (cfs_mutex_is_locked(&cl2echo_page(slice)->ep_lock))
+                return -EBUSY;
+        return -ENODATA;
 }
 
 static void echo_page_completion(const struct lu_env *env,
                                  const struct cl_page_slice *slice,
                                  int ioret)
 {
-        struct echo_page *ecp     = cl2echo_page(slice);
-        struct cl_sync_io *anchor = ecp->ep_sync_io;
-        ENTRY;
-
-        LASSERT(anchor != NULL);
-        ecp->ep_sync_io = NULL;
-        cl_sync_io_note(anchor, ioret);
-        EXIT;
+        LASSERT(slice->cpl_page->cp_sync_io != NULL);
 }
 
 static void echo_page_fini(const struct lu_env *env,
@@ -289,7 +328,7 @@ static void echo_page_fini(const struct lu_env *env,
         cfs_page_t *vmpage      = ep->ep_vmpage;
         ENTRY;
 
-        atomic_dec(&eco->eo_npages);
+        cfs_atomic_dec(&eco->eo_npages);
         page_cache_release(vmpage);
         OBD_SLAB_FREE_PTR(ep, echo_page_kmem);
         EXIT;
@@ -297,7 +336,7 @@ static void echo_page_fini(const struct lu_env *env,
 
 static int echo_page_prep(const struct lu_env *env,
                           const struct cl_page_slice *slice,
-                          struct cl_io *_)
+                          struct cl_io *unused)
 {
         return 0;
 }
@@ -308,12 +347,14 @@ static int echo_page_print(const struct lu_env *env,
 {
         struct echo_page *ep = cl2echo_page(slice);
 
-        (*printer)(env, cookie, LUSTRE_ECHO_CLIENT_NAME"-page@%p vm@%p\n",
-                   ep, ep->ep_vmpage);
+        (*printer)(env, cookie, LUSTRE_ECHO_CLIENT_NAME"-page@%p %d vm@%p\n",
+                   ep, cfs_mutex_is_locked(&ep->ep_lock), ep->ep_vmpage);
         return 0;
 }
 
 static const struct cl_page_operations echo_page_ops = {
+        .cpo_own           = echo_page_own,
+        .cpo_disown        = echo_page_disown,
         .cpo_discard       = echo_page_discard,
         .cpo_vmpage        = echo_page_vmpage,
         .cpo_fini          = echo_page_fini,
@@ -332,7 +373,7 @@ static const struct cl_page_operations echo_page_ops = {
 };
 /** @} echo_page */
 
-/** \defgroup echo_lock echo_lock
+/** \defgroup echo_lock Locking
  *
  * echo lock operations
  *
@@ -343,7 +384,7 @@ static void echo_lock_fini(const struct lu_env *env,
 {
         struct echo_lock *ecl = cl2echo_lock(slice);
 
-        LASSERT(list_empty(&ecl->el_chain));
+        LASSERT(cfs_list_empty(&ecl->el_chain));
         OBD_SLAB_FREE_PTR(ecl, echo_lock_kmem);
 }
 
@@ -352,13 +393,13 @@ static void echo_lock_delete(const struct lu_env *env,
 {
         struct echo_lock *ecl      = cl2echo_lock(slice);
 
-        LASSERT(list_empty(&ecl->el_chain));
+        LASSERT(cfs_list_empty(&ecl->el_chain));
 }
 
 static int echo_lock_fits_into(const struct lu_env *env,
                                const struct cl_lock_slice *slice,
                                const struct cl_lock_descr *need,
-                               const struct cl_io *_)
+                               const struct cl_io *unused)
 {
         return 1;
 }
@@ -371,7 +412,7 @@ static struct cl_lock_operations echo_lock_ops = {
 
 /** @} echo_lock */
 
-/** \defgroup echo_cl_ops echo_cl_ops
+/** \defgroup echo_cl_ops cl_object operations
  *
  * operations for cl_object
  *
@@ -384,13 +425,14 @@ static struct cl_page *echo_page_init(const struct lu_env *env,
         struct echo_page *ep;
         ENTRY;
 
-        OBD_SLAB_ALLOC_PTR(ep, echo_page_kmem);
+        OBD_SLAB_ALLOC_PTR_GFP(ep, echo_page_kmem, CFS_ALLOC_IO);
         if (ep != NULL) {
                 struct echo_object *eco = cl2echo_obj(obj);
                 ep->ep_vmpage = vmpage;
                 page_cache_get(vmpage);
+                cfs_mutex_init(&ep->ep_lock);
                 cl_page_slice_add(page, &ep->ep_cl, obj, &echo_page_ops);
-                atomic_inc(&eco->eo_npages);
+                cfs_atomic_inc(&eco->eo_npages);
         }
         RETURN(ERR_PTR(ep ? 0 : -ENOMEM));
 }
@@ -403,16 +445,17 @@ static int echo_io_init(const struct lu_env *env, struct cl_object *obj,
 
 static int echo_lock_init(const struct lu_env *env,
                           struct cl_object *obj, struct cl_lock *lock,
-                          const struct cl_io *_)
+                          const struct cl_io *unused)
 {
         struct echo_lock *el;
         ENTRY;
 
-        OBD_SLAB_ALLOC_PTR(el, echo_lock_kmem);
+        OBD_SLAB_ALLOC_PTR_GFP(el, echo_lock_kmem, CFS_ALLOC_IO);
         if (el != NULL) {
                 cl_lock_slice_add(lock, &el->el_cl, obj, &echo_lock_ops);
                 el->el_object = cl2echo_obj(obj);
                 CFS_INIT_LIST_HEAD(&el->el_chain);
+                cfs_atomic_set(&el->el_refcount, 0);
         }
         RETURN(el == NULL ? -ENOMEM : 0);
 }
@@ -431,7 +474,7 @@ static const struct cl_object_operations echo_cl_obj_ops = {
 };
 /** @} echo_cl_ops */
 
-/** \defgroup echo_lu_ops echo_lu_ops
+/** \defgroup echo_lu_ops lu_object operations
  *
  * operations for echo lu object.
  *
@@ -440,8 +483,6 @@ static const struct cl_object_operations echo_cl_obj_ops = {
 static int echo_object_init(const struct lu_env *env, struct lu_object *obj,
                             const struct lu_object_conf *conf)
 {
-        const struct cl_object_conf *cconf = lu2cl_conf(conf);
-        struct echo_object_conf *econf = cl2echo_conf(cconf);
         struct echo_device *ed         = cl2echo_dev(lu2cl_dev(obj->lo_dev));
         struct echo_client_obd *ec     = ed->ed_ec;
         struct echo_object *eco        = cl2echo_obj(lu2cl(obj));
@@ -459,17 +500,24 @@ static int echo_object_init(const struct lu_env *env, struct lu_object *obj,
                 lu_object_add(obj, below);
         }
 
-        LASSERT(econf->eoc_md);
-        eco->eo_lsm = *econf->eoc_md;
-        eco->eo_dev = ed;
-        atomic_set(&eco->eo_npages, 0);
+        if (!ed->ed_next_ismd) {
+                const struct cl_object_conf *cconf = lu2cl_conf(conf);
+                struct echo_object_conf *econf = cl2echo_conf(cconf);
+
+                LASSERT(econf->eoc_md);
+                eco->eo_lsm = *econf->eoc_md;
+                /* clear the lsm pointer so that it won't get freed. */
+                *econf->eoc_md = NULL;
+        } else {
+                eco->eo_lsm = NULL;
+        }
 
-        /* clear the lsm pointer so that it won't get freed. */
-        *econf->eoc_md = NULL;
+        eco->eo_dev = ed;
+        cfs_atomic_set(&eco->eo_npages, 0);
 
-        spin_lock(&ec->ec_lock);
-        list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
-        spin_unlock(&ec->ec_lock);
+        cfs_spin_lock(&ec->ec_lock);
+        cfs_list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
+        cfs_spin_unlock(&ec->ec_lock);
 
         RETURN(0);
 }
@@ -481,11 +529,11 @@ static void echo_object_free(const struct lu_env *env, struct lu_object *obj)
         struct lov_stripe_md *lsm  = eco->eo_lsm;
         ENTRY;
 
-        LASSERT(atomic_read(&eco->eo_npages) == 0);
+        LASSERT(cfs_atomic_read(&eco->eo_npages) == 0);
 
-        spin_lock(&ec->ec_lock);
-        list_del_init(&eco->eo_obj_chain);
-        spin_unlock(&ec->ec_lock);
+        cfs_spin_lock(&ec->ec_lock);
+        cfs_list_del_init(&eco->eo_obj_chain);
+        cfs_spin_unlock(&ec->ec_lock);
 
         lu_object_fini(obj);
         lu_object_header_fini(obj->lo_header);
@@ -504,7 +552,6 @@ static int echo_object_print(const struct lu_env *env, void *cookie,
         return (*p)(env, cookie, "echoclient-object@%p", obj);
 }
 
-
 static const struct lu_object_operations echo_lu_obj_ops = {
         .loo_object_init      = echo_object_init,
         .loo_object_delete    = NULL,
@@ -515,15 +562,15 @@ static const struct lu_object_operations echo_lu_obj_ops = {
 };
 /** @} echo_lu_ops */
 
-/** \defgroup echo_lu_dev_ops
+/** \defgroup echo_lu_dev_ops  lu_device operations
  *
  * Operations for echo lu device.
  *
  * @{
  */
 static struct lu_object *echo_object_alloc(const struct lu_env *env,
-                                         const struct lu_object_header *hdr,
-                                         struct lu_device *dev)
+                                           const struct lu_object_header *hdr,
+                                           struct lu_device *dev)
 {
         struct echo_object *eco;
         struct lu_object *obj = NULL;
@@ -531,7 +578,7 @@ static struct lu_object *echo_object_alloc(const struct lu_env *env,
 
         /* we're the top dev. */
         LASSERT(hdr == NULL);
-        OBD_SLAB_ALLOC_PTR(eco, echo_object_kmem);
+        OBD_SLAB_ALLOC_PTR_GFP(eco, echo_object_kmem, CFS_ALLOC_IO);
         if (eco != NULL) {
                 struct cl_object_header *hdr = &eco->eo_hdr;
 
@@ -549,12 +596,13 @@ static struct lu_object *echo_object_alloc(const struct lu_env *env,
 static struct lu_device_operations echo_device_lu_ops = {
         .ldo_object_alloc   = echo_object_alloc,
 };
+
 /** @} echo_lu_dev_ops */
 
 static struct cl_device_operations echo_device_cl_ops = {
 };
 
-/** \defgroup echo_init echo_init
+/** \defgroup echo_init Setup and teardown
  *
  * Init and fini functions for echo client.
  *
@@ -583,7 +631,8 @@ static int echo_site_init(const struct lu_env *env, struct echo_device *ed)
 static void echo_site_fini(const struct lu_env *env, struct echo_device *ed)
 {
         if (ed->ed_site) {
-                cl_site_fini(ed->ed_site);
+                if (!ed->ed_next_ismd)
+                        cl_site_fini(ed->ed_site);
                 ed->ed_site = NULL;
         }
 }
@@ -593,7 +642,7 @@ static void *echo_thread_key_init(const struct lu_context *ctx,
 {
         struct echo_thread_info *info;
 
-        OBD_SLAB_ALLOC_PTR(info, echo_thread_kmem);
+        OBD_SLAB_ALLOC_PTR_GFP(info, echo_thread_kmem, CFS_ALLOC_IO);
         if (info == NULL)
                 info = ERR_PTR(-ENOMEM);
         return info;
@@ -623,7 +672,7 @@ static void *echo_session_key_init(const struct lu_context *ctx,
 {
         struct echo_session_info *session;
 
-        OBD_SLAB_ALLOC_PTR(session, echo_session_kmem);
+        OBD_SLAB_ALLOC_PTR_GFP(session, echo_session_kmem, CFS_ALLOC_IO);
         if (session == NULL)
                 session = ERR_PTR(-ENOMEM);
         return session;
@@ -650,6 +699,55 @@ static struct lu_context_key echo_session_key = {
 
 LU_TYPE_INIT_FINI(echo, &echo_thread_key, &echo_session_key);
 
+#define ECHO_SEQ_WIDTH 0xffffffff
+static int echo_fid_init(struct echo_device *ed, char *obd_name,
+                         struct md_site *ms)
+{
+        char *prefix;
+        int rc;
+        ENTRY;
+
+        OBD_ALLOC_PTR(ed->ed_cl_seq);
+        if (ed->ed_cl_seq == NULL)
+                RETURN(-ENOMEM);
+
+        OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
+        if (prefix == NULL)
+                GOTO(out_free_seq, rc = -ENOMEM);
+
+        snprintf(prefix, MAX_OBD_NAME + 5, "srv-%s", obd_name);
+
+        /* Init client side sequence-manager */
+        rc = seq_client_init(ed->ed_cl_seq, NULL,
+                             LUSTRE_SEQ_METADATA,
+                             prefix, ms->ms_server_seq);
+        ed->ed_cl_seq->lcs_width = ECHO_SEQ_WIDTH;
+        OBD_FREE(prefix, MAX_OBD_NAME + 5);
+        if (rc)
+                GOTO(out_free_seq, rc);
+
+        RETURN(0);
+
+out_free_seq:
+        OBD_FREE_PTR(ed->ed_cl_seq);
+        ed->ed_cl_seq = NULL;
+        RETURN(rc);
+}
+
+static int echo_fid_fini(struct obd_device *obddev)
+{
+        struct echo_device *ed = obd2echo_dev(obddev);
+        ENTRY;
+
+        if (ed->ed_cl_seq != NULL) {
+                seq_client_fini(ed->ed_cl_seq);
+                OBD_FREE_PTR(ed->ed_cl_seq);
+                ed->ed_cl_seq = NULL;
+        }
+
+        RETURN(0);
+}
+
 static struct lu_device *echo_device_alloc(const struct lu_env *env,
                                            struct lu_device_type *t,
                                            struct lustre_cfg *cfg)
@@ -678,55 +776,115 @@ static struct lu_device *echo_device_alloc(const struct lu_env *env,
         cd->cd_ops = &echo_device_cl_ops;
 
         cleanup = 2;
-        rc = echo_site_init(env, ed);
-        if (rc)
+        obd = class_name2obd(lustre_cfg_string(cfg, 0));
+        LASSERT(obd != NULL);
+        LASSERT(env != NULL);
+
+        tgt = class_name2obd(lustre_cfg_string(cfg, 1));
+        if (tgt == NULL) {
+                CERROR("Can not find tgt device %s\n",
+                        lustre_cfg_string(cfg, 1));
                 GOTO(out, rc);
+        }
 
+        next = tgt->obd_lu_dev;
+        if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
+                ed->ed_next_ismd = 1;
+        } else {
+                ed->ed_next_ismd = 0;
+                rc = echo_site_init(env, ed);
+                if (rc)
+                        GOTO(out, rc);
+        }
         cleanup = 3;
-        obd = class_name2obd(lustre_cfg_string(cfg, 0));
-        LASSERT(obd != NULL);
-        rc = echo_client_setup(obd, cfg);
+
+        rc = echo_client_setup(env, obd, cfg);
         if (rc)
                 GOTO(out, rc);
-        ed->ed_ec = &obd->u.echo_client;
 
+        ed->ed_ec = &obd->u.echo_client;
         cleanup = 4;
-        tgt = class_name2obd(lustre_cfg_string(cfg, 1));
-        LASSERT(tgt != NULL);
-        next = tgt->obd_lu_dev;
-        if (!lu_device_is_cl(next))
-                next = NULL;
 
-        /*
-         * if echo client is to be stacked upon ost device, the next is NULL
-         * since ost is not a clio device so far
-         */
-        tgt_type_name = tgt->obd_type->typ_name;
-        if (next != NULL) {
-                LASSERT(next != NULL);
-                if (next->ld_site != NULL)
-                        GOTO(out, rc = -EBUSY);
-
-                next->ld_site = &ed->ed_site->cs_lu;
-                rc = next->ld_type->ldt_ops->ldto_device_init(env, next,
-                                             next->ld_type->ldt_name, NULL);
-                if (rc)
-                        GOTO(out, rc);
+        if (ed->ed_next_ismd) {
+                /* Suppose to connect to some Metadata layer */
+                struct lu_site *ls;
+                struct lu_device *ld;
+                int    found = 0;
 
-                /* Trikcy case, I have to determine the obd type since clio
-                 * uses the different parameters to initialize objects for
-                 * lov & osc.
-                 */
-                if (strcmp(tgt_type_name, LUSTRE_LOV_NAME) == 0)
-                        ed->ed_next_islov = 1;
-                else
-                        LASSERT(strcmp(tgt_type_name, LUSTRE_OSC_NAME) == 0);
-        } else
-                LASSERT(strcmp(tgt_type_name, LUSTRE_OST_NAME) == 0);
+                if (next == NULL) {
+                        CERROR("%s is not lu device type!\n",
+                               lustre_cfg_string(cfg, 1));
+                        GOTO(out, rc = -EINVAL);
+                }
+
+                tgt_type_name = lustre_cfg_string(cfg, 2);
+                if (!tgt_type_name) {
+                        CERROR("%s no type name for echo %s setup\n",
+                                lustre_cfg_string(cfg, 1),
+                                tgt->obd_type->typ_name);
+                        GOTO(out, rc = -EINVAL);
+                }
+
+                ls = next->ld_site;
+
+                cfs_spin_lock(&ls->ls_ld_lock);
+                cfs_list_for_each_entry(ld, &ls->ls_ld_linkage, ld_linkage) {
+                        if (strcmp(ld->ld_type->ldt_name, tgt_type_name) == 0) {
+                                found = 1;
+                                break;
+                        }
+                }
+                cfs_spin_unlock(&ls->ls_ld_lock);
+
+                if (found == 0) {
+                        CERROR("%s is not lu device type!\n",
+                               lustre_cfg_string(cfg, 1));
+                        GOTO(out, rc = -EINVAL);
+                }
+
+                next = ld;
+                /* For MD echo client, it will use the site in MDS stack */
+                ed->ed_site_myself.cs_lu = *ls;
+                ed->ed_site = &ed->ed_site_myself;
+                ed->ed_cl.cd_lu_dev.ld_site = &ed->ed_site_myself.cs_lu;
+                rc = echo_fid_init(ed, obd->obd_name, lu_site2md(ls));
+                if (rc) {
+                        CERROR("echo fid init error %d\n", rc);
+                        GOTO(out, rc);
+                }
+        } else {
+                 /* if echo client is to be stacked upon ost device, the next is
+                  * NULL since ost is not a clio device so far */
+                if (next != NULL && !lu_device_is_cl(next))
+                        next = NULL;
+
+                tgt_type_name = tgt->obd_type->typ_name;
+                if (next != NULL) {
+                        LASSERT(next != NULL);
+                        if (next->ld_site != NULL)
+                                GOTO(out, rc = -EBUSY);
+
+                        next->ld_site = &ed->ed_site->cs_lu;
+                        rc = next->ld_type->ldt_ops->ldto_device_init(env, next,
+                                                     next->ld_type->ldt_name,
+                                                     NULL);
+                        if (rc)
+                                GOTO(out, rc);
+
+                        /* Tricky case, I have to determine the obd type since
+                         * CLIO uses the different parameters to initialize
+                         * objects for lov & osc. */
+                        if (strcmp(tgt_type_name, LUSTRE_LOV_NAME) == 0)
+                                ed->ed_next_islov = 1;
+                        else
+                                LASSERT(strcmp(tgt_type_name,
+                                               LUSTRE_OSC_NAME) == 0);
+                } else
+                        LASSERT(strcmp(tgt_type_name, LUSTRE_OST_NAME) == 0);
+        }
 
         ed->ed_next = next;
         RETURN(&cd->cd_lu_dev);
-
 out:
         switch(cleanup) {
         case 4: {
@@ -763,67 +921,78 @@ static struct lu_device *echo_device_fini(const struct lu_env *env,
         struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
         struct lu_device *next = ed->ed_next;
 
-        while (next)
+        while (next && !ed->ed_next_ismd)
                 next = next->ld_type->ldt_ops->ldto_device_fini(env, next);
         return NULL;
 }
 
+static void echo_lock_release(const struct lu_env *env,
+                              struct echo_lock *ecl,
+                              int still_used)
+{
+        struct cl_lock *clk = echo_lock2cl(ecl);
+
+        cl_lock_get(clk);
+        cl_unuse(env, clk);
+        cl_lock_release(env, clk, "ec enqueue", ecl->el_object);
+        if (!still_used) {
+                cl_lock_mutex_get(env, clk);
+                cl_lock_cancel(env, clk);
+                cl_lock_delete(env, clk);
+                cl_lock_mutex_put(env, clk);
+        }
+        cl_lock_put(env, clk);
+}
+
 static struct lu_device *echo_device_free(const struct lu_env *env,
                                           struct lu_device *d)
 {
         struct echo_device     *ed   = cl2echo_dev(lu2cl_dev(d));
         struct echo_client_obd *ec   = ed->ed_ec;
+        struct echo_object     *eco;
         struct lu_device       *next = ed->ed_next;
 
-        printk("ed = %p, ec = %p, next = %p\n", ed, ec, next);
-
-        /* destroy locks */
-        spin_lock(&ec->ec_lock);
-        while (!list_empty(&ec->ec_locks)) {
-                struct echo_lock *ecl = list_entry(ec->ec_locks.next,
-                                                   struct echo_lock, el_chain);
-                struct cl_lock *lock  = echo_lock2cl(ecl);
+        CDEBUG(D_INFO, "echo device:%p is going to be freed, next = %p\n",
+               ed, next);
 
-                list_del_init(&ecl->el_chain);
-                spin_unlock(&ec->ec_lock);
-
-                CERROR("echo client: pending lock %p\n", ecl);
-
-                cl_lock_get(lock);
-                cl_unuse(env, lock);
-                cl_lock_release(env, lock, "ec enqueue", ecl->el_object);
-
-                cl_lock_mutex_get(env, lock);
-                cl_lock_cancel(env, lock);
-                cl_lock_delete(env, lock);
-                cl_lock_mutex_put(env, lock);
-                cl_lock_put(env, lock);
+        lu_site_purge(env, &ed->ed_site->cs_lu, -1);
 
-                spin_lock(&ec->ec_lock);
-        }
-        spin_unlock(&ec->ec_lock);
+        /* check if there are objects still alive.
+         * It shouldn't have any object because lu_site_purge would cleanup
+         * all of cached objects. Anyway, probably the echo device is being
+         * parallelly accessed.
+         */
+        cfs_spin_lock(&ec->ec_lock);
+        cfs_list_for_each_entry(eco, &ec->ec_objects, eo_obj_chain)
+                eco->eo_deleted = 1;
+        cfs_spin_unlock(&ec->ec_lock);
 
-        LASSERT(ed->ed_site);
+        /* purge again */
         lu_site_purge(env, &ed->ed_site->cs_lu, -1);
 
-        /* check if there are objects still alive, assume only one reference */
-        spin_lock(&ec->ec_lock);
-        while (!list_empty(&ec->ec_objects)) {
-                struct echo_object *eco;
-                eco = list_entry(ec->ec_objects.next, struct echo_object,
-                                 eo_obj_chain);
-                spin_unlock(&ec->ec_lock);
+        CDEBUG(D_INFO,
+               "Waiting for the reference of echo object to be dropped\n");
+
+        /* Wait for the last reference to be dropped. */
+        cfs_spin_lock(&ec->ec_lock);
+        while (!cfs_list_empty(&ec->ec_objects)) {
+                cfs_spin_unlock(&ec->ec_lock);
+                CERROR("echo_client still has objects at cleanup time, "
+                       "wait for 1 second\n");
+                cfs_schedule_timeout_and_set_state(CFS_TASK_UNINT,
+                                                   cfs_time_seconds(1));
+                lu_site_purge(env, &ed->ed_site->cs_lu, -1);
+                cfs_spin_lock(&ec->ec_lock);
+        }
+        cfs_spin_unlock(&ec->ec_lock);
 
-                eco->eo_deleted = 1;
-                cl_echo_object_put(eco);
+        LASSERT(cfs_list_empty(&ec->ec_locks));
 
-                spin_lock(&ec->ec_lock);
-        }
-        spin_unlock(&ec->ec_lock);
+        CDEBUG(D_INFO, "No object exists, exiting...\n");
 
         echo_client_cleanup(d->ld_obd);
-
-        while (next)
+        echo_fid_fini(d->ld_obd);
+        while (next && !ed->ed_next_ismd)
                 next = next->ld_type->ldt_ops->ldto_device_free(env, next);
 
         LASSERT(ed->ed_site == lu2cl_site(d->ld_site));
@@ -851,11 +1020,11 @@ static struct lu_device_type echo_device_type = {
         .ldt_tags     = LU_DEVICE_CL,
         .ldt_name     = LUSTRE_ECHO_CLIENT_NAME,
         .ldt_ops      = &echo_device_type_ops,
-        .ldt_ctx_tags = LCT_CL_THREAD
+        .ldt_ctx_tags = LCT_CL_THREAD | LCT_MD_THREAD | LCT_DT_THREAD,
 };
 /** @} echo_init */
 
-/** \defgroup echo_exports
+/** \defgroup echo_exports Exported operations
  *
  * exporting functions to echo client
  *
@@ -881,6 +1050,10 @@ static struct echo_object *cl_echo_object_find(struct echo_device *d,
         LASSERT(lsm);
         LASSERT(lsm->lsm_object_id);
 
+        /* Never return an object if the obd is to be freed. */
+        if (echo_dev2cl(d)->cd_lu_dev.ld_obd->obd_stopping)
+                RETURN(ERR_PTR(-ENODEV));
+
         env = cl_env_get(&refcheck);
         if (IS_ERR(env))
                 RETURN((void *)env);
@@ -892,7 +1065,7 @@ static struct echo_object *cl_echo_object_find(struct echo_device *d,
                         struct lov_oinfo *oinfo = lsm->lsm_oinfo[0];
                         LASSERT(oinfo != NULL);
                         oinfo->loi_id = lsm->lsm_object_id;
-                        oinfo->loi_gr = lsm->lsm_object_gr;
+                        oinfo->loi_seq = lsm->lsm_object_seq;
                         conf->eoc_cl.u.coc_oinfo = oinfo;
                 } else {
                         struct lustre_md *md;
@@ -937,8 +1110,7 @@ static int cl_echo_object_put(struct echo_object *eco)
         if (eco->eo_deleted) {
                 struct lu_object_header *loh = obj->co_lu.lo_header;
                 LASSERT(&eco->eo_hdr == luh2coh(loh));
-                set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
-                cl_object_prune(env, obj);
+                cfs_set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
         }
 
         cl_object_put(env, obj);
@@ -946,53 +1118,75 @@ static int cl_echo_object_put(struct echo_object *eco)
         RETURN(0);
 }
 
-static int cl_echo_enqueue(struct echo_object *eco, obd_off start, obd_off end,
-                           int mode, __u64 *cookie)
+static int cl_echo_enqueue0(struct lu_env *env, struct echo_object *eco,
+                            obd_off start, obd_off end, int mode,
+                            __u64 *cookie , __u32 enqflags)
 {
-        struct lu_env *env;
-        struct cl_lock *lck;
-        struct echo_thread_info *info;
         struct cl_io *io;
+        struct cl_lock *lck;
+        struct cl_object *obj;
         struct cl_lock_descr *descr;
-        struct cl_object *obj = echo_obj2cl(eco);
-        int refcheck;
-        int result;
+        struct echo_thread_info *info;
+        int rc = -ENOMEM;
         ENTRY;
 
-        env = cl_env_get(&refcheck);
-        if (IS_ERR(env))
-                RETURN(PTR_ERR(env));
-
         info = echo_env_info(env);
+        io = &info->eti_io;
         descr = &info->eti_descr;
+        obj = echo_obj2cl(eco);
+
         descr->cld_obj   = obj;
         descr->cld_start = cl_index(obj, start);
         descr->cld_end   = cl_index(obj, end);
         descr->cld_mode  = mode == LCK_PW ? CLM_WRITE : CLM_READ;
-
-        io = &info->eti_io;
+        descr->cld_enq_flags = enqflags;
         io->ci_obj = obj;
-        result = cl_io_init(env, io, CIT_MISC, obj);
-        if (result < 0)
-                GOTO(out, result);
-        LASSERT(result == 0);
 
-        result = -ENOMEM;
-        lck = cl_lock_request(env, io, descr, CEF_ASYNC, "ec enqueue", eco);
+        lck = cl_lock_request(env, io, descr, "ec enqueue", eco);
         if (lck) {
                 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
                 struct echo_lock *el;
 
-                result = cl_wait(env, lck);
-                if (result == 0) {
+                rc = cl_wait(env, lck);
+                if (rc == 0) {
                         el = cl2echo_lock(cl_lock_at(lck, &echo_device_type));
-                        spin_lock(&ec->ec_lock);
-                        list_add(&el->el_chain, &ec->ec_locks);
-                        *cookie = el->el_cookie = ++ec->ec_unique;
-                        spin_unlock(&ec->ec_lock);
+                        cfs_spin_lock(&ec->ec_lock);
+                        if (cfs_list_empty(&el->el_chain)) {
+                                cfs_list_add(&el->el_chain, &ec->ec_locks);
+                                el->el_cookie = ++ec->ec_unique;
+                        }
+                        cfs_atomic_inc(&el->el_refcount);
+                        *cookie = el->el_cookie;
+                        cfs_spin_unlock(&ec->ec_lock);
                 } else
                         cl_lock_release(env, lck, "ec enqueue", cfs_current());
         }
+        RETURN(rc);
+}
+
+static int cl_echo_enqueue(struct echo_object *eco, obd_off start, obd_off end,
+                           int mode, __u64 *cookie)
+{
+        struct echo_thread_info *info;
+        struct lu_env *env;
+        struct cl_io *io;
+        int refcheck;
+        int result;
+        ENTRY;
+
+        env = cl_env_get(&refcheck);
+        if (IS_ERR(env))
+                RETURN(PTR_ERR(env));
+
+        info = echo_env_info(env);
+        io = &info->eti_io;
+
+        result = cl_io_init(env, io, CIT_MISC, echo_obj2cl(eco));
+        if (result < 0)
+                GOTO(out, result);
+        LASSERT(result == 0);
+
+        result = cl_echo_enqueue0(env, eco, start, end, mode, cookie, 0);
         cl_io_fini(env, io);
 
         EXIT;
@@ -1001,71 +1195,71 @@ out:
         return result;
 }
 
-static int cl_echo_cancel(struct echo_device *ed, __u64 cookie)
+static int cl_echo_cancel0(struct lu_env *env, struct echo_device *ed,
+                           __u64 cookie)
 {
         struct echo_client_obd *ec = ed->ed_ec;
         struct echo_lock       *ecl = NULL;
-        struct list_head       *el;
-        int found = 0;
-        int result;
-
-        struct lu_env *env;
-        int refcheck;
+        cfs_list_t             *el;
+        int found = 0, still_used = 0;
         ENTRY;
 
-        env = cl_env_get(&refcheck);
-        if (IS_ERR(env))
-                RETURN(PTR_ERR(env));
-
-        spin_lock (&ec->ec_lock);
-        list_for_each (el, &ec->ec_locks) {
-                ecl = list_entry (el, struct echo_lock, el_chain);
-                CDEBUG(D_INFO, "ecl: %p, cookie: %llx\n", ecl, ecl->el_cookie);
+        LASSERT(ec != NULL);
+        cfs_spin_lock (&ec->ec_lock);
+        cfs_list_for_each (el, &ec->ec_locks) {
+                ecl = cfs_list_entry (el, struct echo_lock, el_chain);
+                CDEBUG(D_INFO, "ecl: %p, cookie: "LPX64"\n", ecl, ecl->el_cookie);
                 found = (ecl->el_cookie == cookie);
                 if (found) {
-                        list_del_init(&ecl->el_chain);
+                        if (cfs_atomic_dec_and_test(&ecl->el_refcount))
+                                cfs_list_del_init(&ecl->el_chain);
+                        else
+                                still_used = 1;
                         break;
                 }
         }
-        spin_unlock (&ec->ec_lock);
+        cfs_spin_unlock (&ec->ec_lock);
+
+        if (!found)
+                RETURN(-ENOENT);
 
-        result = -ENOENT;
-        if (found) {
-                struct cl_lock *clk = echo_lock2cl(ecl);
+        echo_lock_release(env, ecl, still_used);
+        RETURN(0);
+}
 
-                cl_lock_get(clk);
-                cl_unuse(env, clk);
-                cl_lock_release(env, clk, "ec enqueue", ecl->el_object);
+static int cl_echo_cancel(struct echo_device *ed, __u64 cookie)
+{
+        struct lu_env *env;
+        int refcheck;
+        int rc;
+        ENTRY;
+
+        env = cl_env_get(&refcheck);
+        if (IS_ERR(env))
+                RETURN(PTR_ERR(env));
+
+        rc = cl_echo_cancel0(env, ed, cookie);
 
-                cl_lock_mutex_get(env, clk);
-                cl_lock_cancel(env, clk);
-                cl_lock_delete(env, clk);
-                cl_lock_mutex_put(env, clk);
-                cl_lock_put(env, clk);
-                result = 0;
-        }
         cl_env_put(env, &refcheck);
-        RETURN(result);
+        RETURN(rc);
 }
 
 static int cl_echo_async_brw(const struct lu_env *env, struct cl_io *io,
-                             enum cl_req_type _, struct cl_2queue *queue)
+                             enum cl_req_type unused, struct cl_2queue *queue)
 {
         struct cl_page *clp;
         struct cl_page *temp;
         int result = 0;
         ENTRY;
 
-        cl_page_list_splice(&queue->c2_qin, &queue->c2_qout);
-        cl_page_list_for_each_safe(clp, temp, &queue->c2_qout) {
+        cl_page_list_for_each_safe(clp, temp, &queue->c2_qin) {
                 int rc;
                 rc = cl_page_cache_add(env, io, clp, CRT_WRITE);
                 if (rc == 0)
                         continue;
-                cl_page_list_move(&queue->c2_qin, &queue->c2_qout, clp);
                 result = result ?: rc;
         }
-        RETURN(list_empty(&queue->c2_qout.pl_pages) ? result : 0);
+        RETURN(result);
 }
 
 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
@@ -1075,18 +1269,17 @@ static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
         struct echo_thread_info *info;
         struct cl_object        *obj = echo_obj2cl(eco);
         struct echo_device      *ed  = eco->eo_dev;
-        struct cl_sync_io       *anchor;
         struct cl_2queue        *queue;
         struct cl_io            *io;
         struct cl_page          *clp;
-        struct echo_page        *ep;
-
+        struct lustre_handle    lh = { 0 };
         int page_size = cl_page_size(obj);
         int refcheck;
         int rc;
         int i;
         ENTRY;
 
+        LASSERT((offset & ~CFS_PAGE_MASK) == 0);
         LASSERT(ed->ed_next != NULL);
         env = cl_env_get(&refcheck);
         if (IS_ERR(env))
@@ -1094,16 +1287,22 @@ static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
 
         info    = echo_env_info(env);
         io      = &info->eti_io;
-        anchor  = &info->eti_anchor;
         queue   = &info->eti_queue;
 
-        cl_sync_io_init(anchor, npages);
         cl_2queue_init(queue);
         rc = cl_io_init(env, io, CIT_MISC, obj);
         if (rc < 0)
                 GOTO(out, rc);
         LASSERT(rc == 0);
 
+
+        rc = cl_echo_enqueue0(env, eco, offset,
+                              offset + npages * CFS_PAGE_SIZE - 1,
+                              rw == READ ? LCK_PR : LCK_PW, &lh.cookie,
+                              CEF_NEVER);
+        if (rc < 0)
+                GOTO(error_lock, rc);
+
         for (i = 0; i < npages; i++) {
                 LASSERT(pages[i]);
                 clp = cl_page_find(env, obj, cl_index(obj, offset),
@@ -1121,13 +1320,13 @@ static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
                         break;
                 }
 
-                ep = cl2echo_page(cl_page_at(clp, &echo_device_type));
-                ep->ep_sync_io = anchor;
                 cl_2queue_add(queue, clp);
 
                 /* drop the reference count for cl_page_find, so that the page
                  * will be freed in cl_2queue_fini. */
                 cl_page_put(env, clp);
+                cl_page_clip(env, clp, 0, page_size);
+
                 offset += page_size;
         }
 
@@ -1135,30 +1334,22 @@ static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
                 enum cl_req_type typ = rw == READ ? CRT_READ : CRT_WRITE;
 
                 async = async && (typ == CRT_WRITE);
-                rc = (async ? cl_echo_async_brw : cl_io_submit_rw)(env, io,
-                                                                   typ, queue);
+                if (async)
+                        rc = cl_echo_async_brw(env, io, typ, queue);
+                else
+                        rc = cl_io_submit_sync(env, io, typ, queue,
+                                               CRP_NORMAL, 0);
                 CDEBUG(D_INFO, "echo_client %s write returns %d\n",
                        async ? "async" : "sync", rc);
-                if (rc == 0) {
-                        /*
-                         * If some pages weren't sent for any reason (e.g.,
-                         * direct-io read found up-to-date pages in the
-                         * cache), count them as completed to avoid infinite
-                         * wait.
-                         */
-                        cl_page_list_for_each(clp, &queue->c2_qin)
-                                cl_sync_io_note(anchor, +1);
-                        /* wait for the IO to be finished. */
-                        rc = cl_sync_io_wait(env, io, &queue->c2_qout, anchor);
-                }
         }
 
+        cl_echo_cancel0(env, ed, lh.cookie);
+        EXIT;
+error_lock:
         cl_2queue_discard(env, io, queue);
         cl_2queue_disown(env, io, queue);
         cl_2queue_fini(env, queue);
         cl_io_fini(env, io);
-
-        EXIT;
 out:
         cl_env_put(env, &refcheck);
         return rc;
@@ -1178,12 +1369,12 @@ echo_copyout_lsm (struct lov_stripe_md *lsm, void *_ulsm, int ulsm_nob)
         if (nob > ulsm_nob)
                 return (-EINVAL);
 
-        if (copy_to_user (ulsm, lsm, sizeof(ulsm)))
+        if (cfs_copy_to_user (ulsm, lsm, sizeof(ulsm)))
                 return (-EFAULT);
 
         for (i = 0; i < lsm->lsm_stripe_count; i++) {
-                if (copy_to_user (ulsm->lsm_oinfo[i], lsm->lsm_oinfo[i],
-                                  sizeof(lsm->lsm_oinfo[0])))
+                if (cfs_copy_to_user (ulsm->lsm_oinfo[i], lsm->lsm_oinfo[i],
+                                      sizeof(lsm->lsm_oinfo[0])))
                         return (-EFAULT);
         }
         return 0;
@@ -1199,7 +1390,7 @@ echo_copyin_lsm (struct echo_device *ed, struct lov_stripe_md *lsm,
         if (ulsm_nob < sizeof (*lsm))
                 return (-EINVAL);
 
-        if (copy_from_user (lsm, ulsm, sizeof (*lsm)))
+        if (cfs_copy_from_user (lsm, ulsm, sizeof (*lsm)))
                 return (-EFAULT);
 
         if (lsm->lsm_stripe_count > ec->ec_nstripes ||
@@ -1210,14 +1401,662 @@ echo_copyin_lsm (struct echo_device *ed, struct lov_stripe_md *lsm,
 
 
         for (i = 0; i < lsm->lsm_stripe_count; i++) {
-                if (copy_from_user(lsm->lsm_oinfo[i],
-                                   ((struct lov_stripe_md *)ulsm)->lsm_oinfo[i],
-                                   sizeof(lsm->lsm_oinfo[0])))
+                if (cfs_copy_from_user(lsm->lsm_oinfo[i],
+                                       ((struct lov_stripe_md *)ulsm)-> \
+                                       lsm_oinfo[i],
+                                       sizeof(lsm->lsm_oinfo[0])))
                         return (-EFAULT);
         }
         return (0);
 }
 
+static inline void echo_md_build_name(struct lu_name *lname, char *name,
+                                      __u64 id)
+{
+        sprintf(name, "%llu", id);
+        lname->ln_name = name;
+        lname->ln_namelen = strlen(name);
+}
+
+static int echo_md_create_internal(const struct lu_env *env,
+                                   struct echo_device *ed,
+                                   struct md_object *parent,
+                                   struct lu_fid *fid,
+                                   struct lu_name *lname,
+                                   struct md_op_spec *spec,
+                                   struct md_attr *ma)
+{
+        struct lu_object        *ec_child, *child;
+        struct lu_device        *ld = ed->ed_next;
+        int                      rc;
+
+        ec_child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev,
+                                     fid, NULL);
+        if (IS_ERR(ec_child)) {
+                CERROR("Can not find the child "DFID": rc = %ld\n", PFID(fid),
+                        PTR_ERR(ec_child));
+                return PTR_ERR(ec_child);
+        }
+
+        child = lu_object_locate(ec_child->lo_header, ld->ld_type);
+        if (child == NULL) {
+                CERROR("Can not locate the child "DFID"\n", PFID(fid));
+                GOTO(out_put, rc = -EINVAL);
+        }
+
+        CDEBUG(D_RPCTRACE, "Start creating object "DFID" %s %p\n",
+               PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
+
+        rc = mdo_create(env, parent, lname, lu2md(child), spec, ma);
+        if (rc) {
+                CERROR("Can not create child "DFID": rc = %d\n", PFID(fid), rc);
+                GOTO(out_put, rc);
+        }
+        CDEBUG(D_RPCTRACE, "End creating object "DFID" %s %p rc  = %d\n",
+               PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent, rc);
+out_put:
+        lu_object_put(env, ec_child);
+        return rc;
+}
+
+static int echo_set_lmm_size(const struct lu_env *env,
+                             struct lu_device *ld,
+                             struct md_attr *ma,
+                             int *max_lmm_size)
+{
+        struct echo_thread_info *info = echo_env_info(env);
+        struct md_device *md = lu2md_dev(ld);
+        int tmp, rc;
+        ENTRY;
+
+        LASSERT(max_lmm_size != NULL);
+        if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
+                ma->ma_lmm = (void *)&info->eti_lmm;
+                ma->ma_lmm_size = sizeof(info->eti_lmm);
+                *max_lmm_size = 0;
+                RETURN(0);
+        }
+
+        md = lu2md_dev(ld);
+        rc = md->md_ops->mdo_maxsize_get(env, md,
+                                         max_lmm_size, &tmp);
+        if (rc)
+                RETURN(rc);
+
+        if (*max_lmm_size == 0)
+                /* In case xattr is set in echo_setattr_object */
+                *max_lmm_size = sizeof(struct lov_user_md_v3);
+
+        ma->ma_lmm_size = *max_lmm_size;
+        OBD_ALLOC(ma->ma_lmm, ma->ma_lmm_size);
+        if (ma->ma_lmm == NULL)
+                RETURN(-ENOMEM);
+
+        RETURN(0);
+}
+
+static int echo_create_md_object(const struct lu_env *env,
+                                 struct echo_device *ed,
+                                 struct lu_object *ec_parent,
+                                 struct lu_fid *fid,
+                                 char *name, int namelen,
+                                 __u64 id, __u32 mode, int count,
+                                 int stripe_count, int stripe_offset)
+{
+        struct lu_object        *parent;
+        struct echo_thread_info *info = echo_env_info(env);
+        struct lu_name          *lname = &info->eti_lname;
+        struct md_op_spec       *spec = &info->eti_spec;
+        struct md_attr          *ma = &info->eti_ma;
+        struct lu_device        *ld = ed->ed_next;
+        int                      rc = 0;
+        int                      max_lmm_size = 0;
+        int                      i;
+
+        parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
+        if (ec_parent == NULL) {
+                lu_object_put(env, ec_parent);
+                RETURN(PTR_ERR(parent));
+        }
+
+        memset(ma, 0, sizeof(*ma));
+        memset(spec, 0, sizeof(*spec));
+        if (stripe_count != 0) {
+                spec->sp_cr_flags |= FMODE_WRITE;
+                rc = echo_set_lmm_size(env, ld, ma, &max_lmm_size);
+                if (rc)
+                        GOTO(out_free, rc);
+                if (stripe_count != -1) {
+                        struct lov_user_md_v3 *lum = &info->eti_lum;
+                        lum->lmm_magic = LOV_USER_MAGIC_V3;
+                        lum->lmm_stripe_count = stripe_count;
+                        lum->lmm_stripe_offset = stripe_offset;
+                        lum->lmm_pattern = 0;
+                        spec->u.sp_ea.eadata = lum;
+                        spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
+                }
+        }
+
+        ma->ma_attr.la_mode = mode;
+        ma->ma_attr.la_valid = LA_CTIME;
+        ma->ma_attr.la_ctime = cfs_time_current_64();
+
+        if (name != NULL) {
+                lname->ln_name = name;
+                lname->ln_namelen = namelen;
+                /* If name is specified, only create one object by name */
+                rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
+                                             spec, ma);
+                GOTO(out_free, rc);
+        }
+
+        /* Create multiple object sequenced by id */
+        for (i = 0; i < count; i++) {
+                char *tmp_name = info->eti_name;
+
+                echo_md_build_name(lname, tmp_name, id);
+
+                rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
+                                             spec, ma);
+                if (rc) {
+                        CERROR("Can not create child %s: rc = %d\n", tmp_name,
+                                rc);
+                        break;
+                }
+                id++;
+                fid->f_oid++;
+        }
+
+out_free:
+        if (!strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME) &&
+             max_lmm_size > 0  && ma->ma_lmm != NULL)
+                OBD_FREE(ma->ma_lmm, max_lmm_size);
+
+        return rc;
+}
+
+static struct lu_object *echo_md_lookup(const struct lu_env *env,
+                                        struct echo_device *ed,
+                                        struct md_object *parent,
+                                        struct lu_name *lname)
+{
+        struct echo_thread_info *info = echo_env_info(env);
+        struct lu_fid           *fid = &info->eti_fid;
+        struct lu_object        *child;
+        int    rc;
+        ENTRY;
+
+        CDEBUG(D_INFO, "lookup %s in parent "DFID" %p\n", lname->ln_name,
+               PFID(fid), parent);
+        rc = mdo_lookup(env, parent, lname, fid, NULL);
+        if (rc) {
+                CERROR("lookup %s: rc = %d\n", lname->ln_name, rc);
+                RETURN(ERR_PTR(rc));
+        }
+
+        child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
+
+        RETURN(child);
+}
+
+static int echo_setattr_object(const struct lu_env *env,
+                               struct echo_device *ed,
+                               struct lu_object *ec_parent,
+                               __u64 id, int count)
+{
+        struct lu_object        *parent;
+        struct echo_thread_info *info = echo_env_info(env);
+        struct lu_name          *lname = &info->eti_lname;
+        char                    *name = info->eti_name;
+        struct md_attr          *ma = &info->eti_ma;
+        struct lu_device        *ld = ed->ed_next;
+        struct lov_user_md_v3   *lum = &info->eti_lum;
+        int                      rc = 0;
+        int                      i;
+
+        parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
+        if (ec_parent == NULL) {
+                lu_object_put(env, ec_parent);
+                return PTR_ERR(parent);
+        }
+
+        memset(ma, 0, sizeof(*ma));
+        lum->lmm_magic = LOV_USER_MAGIC_V3;
+        lum->lmm_stripe_count = 1;
+        lum->lmm_stripe_offset = -1;
+        lum->lmm_pattern = 0;
+
+        ma->ma_lmm = (struct lov_mds_md *)lum;
+        ma->ma_lmm_size = sizeof(*lum);
+        ma->ma_valid = MA_LOV | MA_HSM;
+        for (i = 0; i < count; i++) {
+                struct lu_object *ec_child, *child;
+
+                echo_md_build_name(lname, name, id);
+
+                ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
+                if (IS_ERR(ec_child)) {
+                        CERROR("Can't find child %s: rc = %ld\n",
+                                lname->ln_name, PTR_ERR(ec_child));
+                        RETURN(PTR_ERR(ec_child));
+                }
+
+                child = lu_object_locate(ec_child->lo_header, ld->ld_type);
+                if (child == NULL) {
+                        CERROR("Can not locate the child %s\n", lname->ln_name);
+                        lu_object_put(env, ec_child);
+                        rc = -EINVAL;
+                        break;
+                }
+
+                CDEBUG(D_RPCTRACE, "Start getattr object "DFID"\n",
+                       PFID(lu_object_fid(child)));
+                rc = mo_attr_set(env, lu2md(child), ma);
+                if (rc) {
+                        CERROR("Can not getattr child "DFID": rc = %d\n",
+                                PFID(lu_object_fid(child)), rc);
+                        lu_object_put(env, ec_child);
+                        break;
+                }
+                CDEBUG(D_RPCTRACE, "End getattr object "DFID"\n",
+                       PFID(lu_object_fid(child)));
+                id++;
+                lu_object_put(env, ec_child);
+        }
+        return rc;
+}
+
+static int echo_getattr_object(const struct lu_env *env,
+                               struct echo_device *ed,
+                               struct lu_object *ec_parent,
+                               __u64 id, int count)
+{
+        struct lu_object        *parent;
+        struct echo_thread_info *info = echo_env_info(env);
+        struct lu_name          *lname = &info->eti_lname;
+        char                    *name = info->eti_name;
+        struct md_attr          *ma = &info->eti_ma;
+        struct lu_device        *ld = ed->ed_next;
+        int                      max_lmm_size;
+        int                      rc = 0;
+        int                      i;
+
+        parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
+        if (ec_parent == NULL) {
+                lu_object_put(env, ec_parent);
+                return PTR_ERR(parent);
+        }
+
+        memset(ma, 0, sizeof(*ma));
+        rc = echo_set_lmm_size(env, ld, ma, &max_lmm_size);
+        if (rc)
+                GOTO(out_free, rc);
+
+        ma->ma_need |= MA_INODE | MA_LOV | MA_PFID | MA_HSM | MA_ACL_DEF;
+        ma->ma_acl = info->eti_xattr_buf;
+        ma->ma_acl_size = sizeof(info->eti_xattr_buf);
+
+        for (i = 0; i < count; i++) {
+                struct lu_object *ec_child, *child;
+
+                echo_md_build_name(lname, name, id);
+
+                ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
+                if (IS_ERR(ec_child)) {
+                        CERROR("Can't find child %s: rc = %ld\n",
+                               lname->ln_name, PTR_ERR(ec_child));
+                        RETURN(PTR_ERR(ec_child));
+                }
+
+                child = lu_object_locate(ec_child->lo_header, ld->ld_type);
+                if (child == NULL) {
+                        CERROR("Can not locate the child %s\n", lname->ln_name);
+                        lu_object_put(env, ec_child);
+                        GOTO(out_free, rc = -EINVAL);
+                }
+
+                CDEBUG(D_RPCTRACE, "Start getattr object "DFID"\n",
+                       PFID(lu_object_fid(child)));
+                rc = mo_attr_get(env, lu2md(child), ma);
+                if (rc) {
+                        CERROR("Can not getattr child "DFID": rc = %d\n",
+                                PFID(lu_object_fid(child)), rc);
+                        lu_object_put(env, ec_child);
+                        break;
+                }
+                CDEBUG(D_RPCTRACE, "End getattr object "DFID"\n",
+                       PFID(lu_object_fid(child)));
+                id++;
+                lu_object_put(env, ec_child);
+        }
+
+out_free:
+        if (!strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME) &&
+             max_lmm_size > 0 && ma->ma_lmm)
+                OBD_FREE(ma->ma_lmm, max_lmm_size);
+
+        return rc;
+}
+
+static int echo_lookup_object(const struct lu_env *env,
+                              struct echo_device *ed,
+                              struct lu_object *ec_parent,
+                              __u64 id, int count)
+{
+        struct lu_object        *parent;
+        struct echo_thread_info *info = echo_env_info(env);
+        struct lu_name          *lname = &info->eti_lname;
+        char                    *name = info->eti_name;
+        struct lu_fid           *fid = &info->eti_fid;
+        struct lu_device        *ld = ed->ed_next;
+        int                      rc = 0;
+        int                      i;
+
+        parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
+        if (ec_parent == NULL) {
+                lu_object_put(env, ec_parent);
+                return PTR_ERR(parent);
+        }
+
+        /*prepare the requests*/
+        for (i = 0; i < count; i++) {
+                echo_md_build_name(lname, name, id);
+
+                CDEBUG(D_RPCTRACE, "Start lookup object "DFID" %s %p\n",
+                       PFID(lu_object_fid(parent)), lname->ln_name, parent);
+                rc = mdo_lookup(env, lu2md(parent), lname, fid, NULL);
+                if (rc) {
+                        CERROR("Can not lookup child %s: rc = %d\n", name, rc);
+                        break;
+                }
+                CDEBUG(D_RPCTRACE, "End lookup object "DFID" %s %p\n",
+                       PFID(lu_object_fid(parent)), lname->ln_name, parent);
+
+                id++;
+        }
+        return rc;
+}
+
+static int echo_md_destroy_internal(const struct lu_env *env,
+                                    struct echo_device *ed,
+                                    struct md_object *parent,
+                                    struct lu_name *lname,
+                                    struct md_attr *ma)
+{
+        struct lu_device   *ld = ed->ed_next;
+        struct lu_object   *ec_child;
+        struct lu_object   *child;
+        int                 rc;
+
+        ec_child = echo_md_lookup(env, ed, parent, lname);
+        if (IS_ERR(ec_child)) {
+                CERROR("Can't find child %s: rc = %ld\n", lname->ln_name,
+                        PTR_ERR(ec_child));
+                RETURN(PTR_ERR(ec_child));
+        }
+
+        child = lu_object_locate(ec_child->lo_header, ld->ld_type);
+        if (child == NULL) {
+                CERROR("Can not locate the child %s\n", lname->ln_name);
+                GOTO(out_put, rc = -EINVAL);
+        }
+
+        CDEBUG(D_RPCTRACE, "Start destroy object "DFID" %s %p\n",
+               PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
+
+        rc = mdo_unlink(env, parent, lu2md(child), lname, ma);
+        if (rc) {
+                CERROR("Can not unlink child %s: rc = %d\n",
+                        lname->ln_name, rc);
+                GOTO(out_put, rc);
+        }
+        CDEBUG(D_RPCTRACE, "End destroy object "DFID" %s %p\n",
+               PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
+out_put:
+        lu_object_put(env, ec_child);
+        return rc;
+}
+
+static int echo_destroy_object(const struct lu_env *env,
+                               struct echo_device *ed,
+                               struct lu_object *ec_parent,
+                               char *name, int namelen,
+                               __u64 id, __u32 mode,
+                               int count)
+{
+        struct echo_thread_info *info = echo_env_info(env);
+        struct lu_name          *lname = &info->eti_lname;
+        struct md_attr          *ma = &info->eti_ma;
+        struct lu_device        *ld = ed->ed_next;
+        struct lu_object        *parent;
+        int                      rc = 0;
+        int                      max_lmm_size = 0;
+        int                      i;
+        ENTRY;
+
+        parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
+        if (parent == NULL)
+                RETURN(-EINVAL);
+
+        memset(ma, 0, sizeof(*ma));
+        ma->ma_attr.la_mode = mode;
+        ma->ma_attr.la_valid = LA_CTIME;
+        ma->ma_attr.la_ctime = cfs_time_current_64();
+        ma->ma_need = MA_INODE;
+        ma->ma_valid = 0;
+
+        rc = echo_set_lmm_size(env, ld, ma, &max_lmm_size);
+        if (rc)
+                GOTO(out_free, rc);
+
+        /*FIXME: Do not need logcookie for now, and check stripes*/
+        ma->ma_cookie = NULL;
+        ma->ma_cookie_size = 0;
+        ma->ma_need = MA_INODE | MA_LOV;
+        ma->ma_valid = 0;
+
+        if (name != NULL) {
+                lname->ln_name = name;
+                lname->ln_namelen = namelen;
+                rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
+                                              ma);
+                GOTO(out_free, rc);
+        }
+
+        /*prepare the requests*/
+        for (i = 0; i < count; i++) {
+                char *tmp_name = info->eti_name;
+
+                echo_md_build_name(lname, tmp_name, id);
+
+                rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
+                                              ma);
+                if (rc) {
+                        CERROR("Can not unlink child %s: rc = %d\n", name, rc);
+                        break;
+                }
+                id++;
+        }
+
+out_free:
+        if (!strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME) &&
+             max_lmm_size > 0 && ma->ma_lmm)
+                OBD_FREE(ma->ma_lmm, max_lmm_size);
+
+        RETURN(rc);
+}
+
+struct lu_object *echo_resolve_path(const struct lu_env *env,
+                                    struct echo_device *ed, char *path,
+                                    int path_len)
+{
+        struct lu_device        *ld = ed->ed_next;
+        struct md_device        *md = lu2md_dev(ld);
+        struct echo_thread_info *info = echo_env_info(env);
+        struct lu_fid           *fid = &info->eti_fid;
+        struct lu_name          *lname = &info->eti_lname;
+        struct lu_object        *parent = NULL;
+        struct lu_object        *child = NULL;
+        int rc = 0;
+        ENTRY;
+
+        /*Only support MDD layer right now*/
+        LASSERT(!strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME));
+
+        rc = md->md_ops->mdo_root_get(env, md, fid);
+        if (rc) {
+                CERROR("get root error: rc = %d\n", rc);
+                RETURN(ERR_PTR(rc));
+        }
+
+        parent = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
+        if (IS_ERR(parent)) {
+                CERROR("Can not find the parent "DFID": rc = %ld\n",
+                        PFID(fid), PTR_ERR(parent));
+                RETURN(parent);
+        }
+
+        while (1) {
+                struct lu_object *ld_parent;
+                char *e;
+
+                e = strsep(&path, "/");
+                if (e == NULL)
+                        break;
+
+                if (e[0] == 0) {
+                        if (!path || path[0] == '\0')
+                                break;
+                        continue;
+                }
+
+                lname->ln_name = e;
+                lname->ln_namelen = strlen(e);
+
+                ld_parent = lu_object_locate(parent->lo_header, ld->ld_type);
+                if (ld_parent == NULL) {
+                        lu_object_put(env, parent);
+                        rc = -EINVAL;
+                        break;
+                }
+
+                child = echo_md_lookup(env, ed, lu2md(ld_parent), lname);
+                lu_object_put(env, parent);
+                if (IS_ERR(child)) {
+                        rc = (int)PTR_ERR(child);
+                        CERROR("lookup %s under parent "DFID": rc = %d\n",
+                                lname->ln_name, PFID(lu_object_fid(ld_parent)),
+                                rc);
+                        break;
+                }
+                parent = child;
+        }
+        if (rc)
+                RETURN(ERR_PTR(rc));
+
+        RETURN(parent);
+}
+
+#define ECHO_MD_CTX_TAG (LCT_REMEMBER | LCT_NOREF | LCT_MD_THREAD)
+#define ECHO_MD_SES_TAG (LCT_SESSION | LCT_REMEMBER | LCT_NOREF)
+
+static int echo_md_handler(struct echo_device *ed, int command,
+                           char *path, int path_len, int id, int count,
+                           struct obd_ioctl_data *data)
+{
+        struct lu_device      *ld = ed->ed_next;
+        struct lu_env         *env;
+        int                    refcheck;
+        struct lu_object      *parent;
+        char                  *name = NULL;
+        int                    namelen = data->ioc_plen2;
+        int                    rc = 0;
+        ENTRY;
+
+        if (ld == NULL) {
+                CERROR("MD echo client is not being initialized properly\n");
+                RETURN(-EINVAL);
+        }
+
+        if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
+                CERROR("Only support MDD layer right now!\n");
+                RETURN(-EINVAL);
+        }
+
+        env = cl_env_get(&refcheck);
+        if (IS_ERR(env))
+                RETURN(PTR_ERR(env));
+
+        rc = lu_env_refill_by_tags(env, ECHO_MD_CTX_TAG, ECHO_MD_SES_TAG);
+        if (rc != 0) {
+                cl_env_put(env, &refcheck);
+                RETURN(rc);
+        }
+
+        parent = echo_resolve_path(env, ed, path, path_len);
+        if (IS_ERR(parent)) {
+                CERROR("Can not resolve the path %s: rc = %ld\n", path,
+                        PTR_ERR(parent));
+                cl_env_put(env, &refcheck);
+                RETURN(PTR_ERR(parent));
+        }
+
+        if (namelen > 0) {
+                OBD_ALLOC(name, namelen + 1);
+                if (name == NULL)
+                        RETURN(-ENOMEM);
+                if (cfs_copy_from_user(name, data->ioc_pbuf2, namelen)) {
+                        OBD_FREE(name, namelen + 1);
+                        RETURN(-EFAULT);
+                }
+        }
+
+        switch (command) {
+        case ECHO_MD_CREATE:
+        case ECHO_MD_MKDIR: {
+                struct echo_thread_info *info = echo_env_info(env);
+                __u32 mode = data->ioc_obdo2.o_mode;
+                struct lu_fid *fid = &info->eti_fid;
+                int stripe_count = (int)data->ioc_obdo2.o_misc;
+                int stripe_index = (int)data->ioc_obdo2.o_stripe_idx;
+
+                fid->f_seq = data->ioc_obdo1.o_seq;
+                fid->f_oid = (__u32)data->ioc_obdo1.o_id;
+                fid->f_ver = 0;
+                rc = echo_create_md_object(env, ed, parent, fid, name, namelen,
+                                           id, mode, count, stripe_count,
+                                           stripe_index);
+                break;
+        }
+        case ECHO_MD_DESTROY:
+        case ECHO_MD_RMDIR: {
+                __u32 mode = data->ioc_obdo2.o_mode;
+
+                rc = echo_destroy_object(env, ed, parent, name, namelen,
+                                         id, mode, count);
+                break;
+        }
+        case ECHO_MD_LOOKUP:
+                rc = echo_lookup_object(env, ed, parent, id, count);
+                break;
+        case ECHO_MD_GETATTR:
+                rc = echo_getattr_object(env, ed, parent, id, count);
+                break;
+        case ECHO_MD_SETATTR:
+                rc = echo_setattr_object(env, ed, parent, id, count);
+                break;
+        default:
+                CERROR("unknown command %d\n", command);
+                rc = -EINVAL;
+                break;
+        }
+        if (name != NULL)
+                OBD_FREE(name, namelen + 1);
+        lu_object_put(env, parent);
+        cl_env_put(env, &refcheck);
+        return rc;
+}
+
 static int echo_create_object(struct echo_device *ed, int on_target,
                               struct obdo *oa, void *ulsm, int ulsm_nob,
                               struct obd_trans_info *oti)
@@ -1238,7 +2077,7 @@ static int echo_create_object(struct echo_device *ed, int on_target,
 
         rc = obd_alloc_memmd(ec->ec_exp, &lsm);
         if (rc < 0) {
-                CERROR("Cannot allocate md, rc = %d\n", rc);
+                CERROR("Cannot allocate md: rc = %d\n", rc);
                 GOTO(failed, rc);
         }
 
@@ -1255,7 +2094,7 @@ static int echo_create_object(struct echo_device *ed, int on_target,
                 if (lsm->lsm_stripe_size == 0)
                         lsm->lsm_stripe_size = CFS_PAGE_SIZE;
 
-                idx = ll_rand();
+                idx = cfs_rand();
 
                 /* setup stripes: indices + default ids if required */
                 for (i = 0; i < lsm->lsm_stripe_count; i++) {
@@ -1276,12 +2115,12 @@ static int echo_create_object(struct echo_device *ed, int on_target,
 
         rc = 0;
         if (on_target) {
-                oa->o_gr = FILTER_GROUP_ECHO;
-                oa->o_valid |= OBD_MD_FLGROUP;
-
+                /* Only echo objects are allowed to be created */
+                LASSERT((oa->o_valid & OBD_MD_FLGROUP) &&
+                        (oa->o_seq == FID_SEQ_ECHO));
                 rc = obd_create(ec->ec_exp, oa, &lsm, oti);
                 if (rc != 0) {
-                        CERROR("Cannot create objects, rc = %d\n", rc);
+                        CERROR("Cannot create objects: rc = %d\n", rc);
                         GOTO(failed, rc);
                 }
                 created = 1;
@@ -1305,7 +2144,7 @@ static int echo_create_object(struct echo_device *ed, int on_target,
         if (lsm)
                 obd_free_memmd(ec->ec_exp, &lsm);
         if (rc)
-                CERROR("create object failed with rc = %d\n", rc);
+                CERROR("create object failed with: rc = %d\n", rc);
         return (rc);
 }
 
@@ -1331,9 +2170,9 @@ static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
 
         lsm->lsm_object_id = oa->o_id;
         if (oa->o_valid & OBD_MD_FLGROUP)
-                lsm->lsm_object_gr = oa->o_gr;
+                lsm->lsm_object_seq = oa->o_seq;
         else
-                lsm->lsm_object_gr = FILTER_GROUP_ECHO;
+                lsm->lsm_object_seq = FID_SEQ_ECHO;
 
         rc = 0;
         eco = cl_echo_object_find(ed, &lsm);
@@ -1462,6 +2301,7 @@ static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
         int                     rc;
         int                     verify;
         int                     gfp_mask;
+        int                     brw_flags = 0;
         ENTRY;
 
         verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
@@ -1481,6 +2321,9 @@ static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
         /* XXX think again with misaligned I/O */
         npages = count >> CFS_PAGE_SHIFT;
 
+        if (rw == OBD_BRW_WRITE)
+                brw_flags = OBD_BRW_ASYNC;
+
         OBD_ALLOC(pga, npages * sizeof(*pga));
         if (pga == NULL)
                 RETURN(-ENOMEM);
@@ -1505,7 +2348,7 @@ static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
                 pages[i] = pgp->pg;
                 pgp->count = CFS_PAGE_SIZE;
                 pgp->off = off;
-                pgp->flag = 0;
+                pgp->flag = brw_flags;
 
                 if (verify)
                         echo_client_page_debug_setup(lsm, pgp->pg, rw,
@@ -1621,6 +2464,9 @@ static int echo_client_prep_commit(struct obd_export *exp, int rw,
                 ret = obd_commitrw(rw, exp, oa, 1,&ioo,rnb,npages,lnb,oti,ret);
                 if (ret != 0)
                         GOTO(out, ret);
+
+                /* Reset oti otherwise it would confuse ldiskfs. */
+                memset(oti, 0, sizeof(*oti));
         }
 
 out:
@@ -1637,31 +2483,32 @@ static int echo_client_brw_ioctl(int rw, struct obd_export *exp,
         struct obd_device *obd = class_exp2obd(exp);
         struct echo_device *ed = obd2echo_dev(obd);
         struct echo_client_obd *ec = ed->ed_ec;
-        struct obd_trans_info dummy_oti = { .oti_thread = NULL };
+        struct obd_trans_info dummy_oti = { 0 };
+        struct obdo *oa = &data->ioc_obdo1;
         struct echo_object *eco;
         int rc;
         int async = 1;
         ENTRY;
 
-        rc = echo_get_object(&eco, ed, &data->ioc_obdo1);
+        LASSERT(oa->o_valid & OBD_MD_FLGROUP);
+
+        rc = echo_get_object(&eco, ed, oa);
         if (rc)
                 RETURN(rc);
 
-        data->ioc_obdo1.o_valid &= ~OBD_MD_FLHANDLE;
-        data->ioc_obdo1.o_valid |= OBD_MD_FLGROUP;
-        data->ioc_obdo1.o_gr = FILTER_GROUP_ECHO;
+        oa->o_valid &= ~OBD_MD_FLHANDLE;
 
         switch((long)data->ioc_pbuf1) {
         case 1:
                 async = 0;
                 /* fall through */
         case 2:
-                rc = echo_client_kbrw(ed, rw, &data->ioc_obdo1,
+                rc = echo_client_kbrw(ed, rw, oa,
                                       eco, data->ioc_offset,
                                       data->ioc_count, async, &dummy_oti);
                 break;
         case 3:
-                rc = echo_client_prep_commit(ec->ec_exp, rw, &data->ioc_obdo1,
+                rc = echo_client_prep_commit(ec->ec_exp, rw, oa,
                                             eco, data->ioc_offset,
                                             data->ioc_count, data->ioc_plen1,
                                             &dummy_oti);
@@ -1702,7 +2549,7 @@ echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
         rc = cl_echo_enqueue(eco, offset, end, mode, &ulh->cookie);
         if (rc == 0) {
                 oa->o_valid |= OBD_MD_FLHANDLE;
-                CDEBUG(D_INFO, "Cookie is %llx\n", ulh->cookie);
+                CDEBUG(D_INFO, "Cookie is "LPX64"\n", ulh->cookie);
         }
         echo_put_object(eco);
         RETURN(rc);
@@ -1717,7 +2564,7 @@ echo_client_cancel(struct obd_export *exp, struct obdo *oa)
         if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
                 return -EINVAL;
 
-        CDEBUG(D_INFO, "Cookie is %llx\n", cookie);
+        CDEBUG(D_INFO, "Cookie is "LPX64"\n", cookie);
         return cl_echo_cancel(ed, cookie);
 }
 
@@ -1733,34 +2580,111 @@ echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
         struct obd_trans_info   dummy_oti;
         struct oti_req_ack_lock *ack_lock;
         struct obdo            *oa;
+        struct lu_fid           fid;
         int                     rw = OBD_BRW_READ;
         int                     rc = 0;
         int                     i;
         ENTRY;
 
-        unlock_kernel();
+#ifndef HAVE_UNLOCKED_IOCTL
+        cfs_unlock_kernel();
+#endif
 
         memset(&dummy_oti, 0, sizeof(dummy_oti));
 
+        oa = &data->ioc_obdo1;
+        if (!(oa->o_valid & OBD_MD_FLGROUP)) {
+                oa->o_valid |= OBD_MD_FLGROUP;
+                oa->o_seq = FID_SEQ_ECHO;
+        }
+
+        /* This FID is unpacked just for validation at this point */
+        rc = fid_ostid_unpack(&fid, &oa->o_oi, 0);
+        if (rc < 0)
+                RETURN(rc);
+
         switch (cmd) {
         case OBD_IOC_CREATE:                    /* may create echo object */
                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
                         GOTO (out, rc = -EPERM);
 
-                rc = echo_create_object (ed, 1, &data->ioc_obdo1,
+                rc = echo_create_object (ed, 1, oa,
                                          data->ioc_pbuf1, data->ioc_plen1,
                                          &dummy_oti);
                 GOTO(out, rc);
 
+        case OBD_IOC_ECHO_MD: {
+                int count;
+                int cmd;
+                char *dir = NULL;
+                int dirlen;
+                __u64 id;
+
+                if (!cfs_capable(CFS_CAP_SYS_ADMIN))
+                        GOTO(out, rc = -EPERM);
+
+                count = data->ioc_count;
+                cmd = data->ioc_command;
+
+                id = data->ioc_obdo2.o_id;
+
+                dirlen = data->ioc_plen1;
+                OBD_ALLOC(dir, dirlen + 1);
+                if (dir == NULL)
+                        GOTO(out, rc = -ENOMEM);
+
+                if (cfs_copy_from_user(dir, data->ioc_pbuf1, dirlen)) {
+                        OBD_FREE(dir, data->ioc_plen1 + 1);
+                        GOTO(out, rc = -EFAULT);
+                }
+
+                rc = echo_md_handler(ed, cmd, dir, dirlen, id, count, data);
+                OBD_FREE(dir, dirlen + 1);
+                GOTO(out, rc);
+        }
+        case OBD_IOC_ECHO_ALLOC_SEQ: {
+                struct lu_env   *env;
+                int              refcheck;
+                __u64            seq;
+                int              max_count;
+
+                if (!cfs_capable(CFS_CAP_SYS_ADMIN))
+                        GOTO(out, rc = -EPERM);
+
+                env = cl_env_get(&refcheck);
+                if (IS_ERR(env))
+                        GOTO(out, rc = PTR_ERR(env));
+
+                rc = lu_env_refill_by_tags(env, ECHO_MD_CTX_TAG,
+                                            ECHO_MD_SES_TAG);
+                if (rc != 0) {
+                        cl_env_put(env, &refcheck);
+                        GOTO(out, rc);
+                }
+
+                rc = seq_client_get_seq(env, ed->ed_cl_seq, &seq);
+                cl_env_put(env, &refcheck);
+                if (rc < 0) {
+                        CERROR("%s: Can not alloc seq: rc = %d\n",
+                               obd->obd_name, rc);
+                        GOTO(out, rc);
+                }
+
+                if (cfs_copy_to_user(data->ioc_pbuf1, &seq, data->ioc_plen1))
+                        return -EFAULT;
+
+                max_count = LUSTRE_SEQ_MAX_WIDTH;
+                if (cfs_copy_to_user(data->ioc_pbuf2, &max_count,
+                                     data->ioc_plen2))
+                        return -EFAULT;
+                GOTO(out, rc);
+        }
         case OBD_IOC_DESTROY:
                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
                         GOTO (out, rc = -EPERM);
 
-                rc = echo_get_object (&eco, ed, &data->ioc_obdo1);
+                rc = echo_get_object (&eco, ed, oa);
                 if (rc == 0) {
-                        oa = &data->ioc_obdo1;
-                        oa->o_gr = FILTER_GROUP_ECHO;
-                        oa->o_valid |= OBD_MD_FLGROUP;
                         rc = obd_destroy(ec->ec_exp, oa, eco->eo_lsm,
                                          &dummy_oti, NULL, NULL);
                         if (rc == 0)
@@ -1770,11 +2694,11 @@ echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
                 GOTO(out, rc);
 
         case OBD_IOC_GETATTR:
-                rc = echo_get_object (&eco, ed, &data->ioc_obdo1);
+                rc = echo_get_object (&eco, ed, oa);
                 if (rc == 0) {
                         struct obd_info oinfo = { { { 0 } } };
                         oinfo.oi_md = eco->eo_lsm;
-                        oinfo.oi_oa = &data->ioc_obdo1;
+                        oinfo.oi_oa = oa;
                         rc = obd_getattr(ec->ec_exp, &oinfo);
                         echo_put_object(eco);
                 }
@@ -1784,10 +2708,10 @@ echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
                         GOTO (out, rc = -EPERM);
 
-                rc = echo_get_object (&eco, ed, &data->ioc_obdo1);
+                rc = echo_get_object (&eco, ed, oa);
                 if (rc == 0) {
                         struct obd_info oinfo = { { { 0 } } };
-                        oinfo.oi_oa = &data->ioc_obdo1;
+                        oinfo.oi_oa = oa;
                         oinfo.oi_md = eco->eo_lsm;
 
                         rc = obd_setattr(ec->ec_exp, &oinfo, NULL);
@@ -1806,7 +2730,7 @@ echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
                 GOTO(out, rc);
 
         case ECHO_IOC_GET_STRIPE:
-                rc = echo_get_object(&eco, ed, &data->ioc_obdo1);
+                rc = echo_get_object(&eco, ed, oa);
                 if (rc == 0) {
                         rc = echo_copyout_lsm(eco->eo_lsm, data->ioc_pbuf1,
                                               data->ioc_plen1);
@@ -1819,13 +2743,13 @@ echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
                         GOTO (out, rc = -EPERM);
 
                 if (data->ioc_pbuf1 == NULL) {  /* unset */
-                        rc = echo_get_object(&eco, ed, &data->ioc_obdo1);
+                        rc = echo_get_object(&eco, ed, oa);
                         if (rc == 0) {
                                 eco->eo_deleted = 1;
                                 echo_put_object(eco);
                         }
                 } else {
-                        rc = echo_create_object(ed, 0, &data->ioc_obdo1,
+                        rc = echo_create_object(ed, 0, oa,
                                                 data->ioc_pbuf1,
                                                 data->ioc_plen1, &dummy_oti);
                 }
@@ -1835,14 +2759,14 @@ echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
                         GOTO (out, rc = -EPERM);
 
-                rc = echo_client_enqueue(exp, &data->ioc_obdo1,
+                rc = echo_client_enqueue(exp, oa,
                                          data->ioc_conn1, /* lock mode */
                                          data->ioc_offset,
                                          data->ioc_count);/*extent*/
                 GOTO (out, rc);
 
         case ECHO_IOC_CANCEL:
-                rc = echo_client_cancel(exp, &data->ioc_obdo1);
+                rc = echo_client_cancel(exp, oa);
                 GOTO (out, rc);
 
         default:
@@ -1861,16 +2785,18 @@ echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
                 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
         }
 
-        lock_kernel();
+#ifndef HAVE_UNLOCKED_IOCTL
+        cfs_lock_kernel();
+#endif
 
         return rc;
 }
 
-static int echo_client_setup(struct obd_device *obddev, struct lustre_cfg *lcfg)
+static int echo_client_setup(const struct lu_env *env,
+                             struct obd_device *obddev, struct lustre_cfg *lcfg)
 {
         struct echo_client_obd *ec = &obddev->u.echo_client;
         struct obd_device *tgt;
-        struct lustre_handle conn = {0, };
         struct obd_uuid echo_uuid = { "ECHO_UUID" };
         struct obd_connect_data *ocd = NULL;
         int rc;
@@ -1888,12 +2814,18 @@ static int echo_client_setup(struct obd_device *obddev, struct lustre_cfg *lcfg)
                 RETURN(-EINVAL);
         }
 
-        spin_lock_init (&ec->ec_lock);
+        cfs_spin_lock_init (&ec->ec_lock);
         CFS_INIT_LIST_HEAD (&ec->ec_objects);
         CFS_INIT_LIST_HEAD (&ec->ec_locks);
         ec->ec_unique = 0;
         ec->ec_nstripes = 0;
 
+        if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
+                lu_context_tags_update(ECHO_MD_CTX_TAG);
+                lu_session_tags_update(ECHO_MD_SES_TAG);
+                RETURN(0);
+        }
+
         OBD_ALLOC(ocd, sizeof(*ocd));
         if (ocd == NULL) {
                 CERROR("Can't alloc ocd connecting to %s\n",
@@ -1902,11 +2834,18 @@ static int echo_client_setup(struct obd_device *obddev, struct lustre_cfg *lcfg)
         }
 
         ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
-                                 OBD_CONNECT_GRANT;
+                                 OBD_CONNECT_GRANT | OBD_CONNECT_FULL20 |
+                                 OBD_CONNECT_64BITHASH;
         ocd->ocd_version = LUSTRE_VERSION_CODE;
-        ocd->ocd_group = FILTER_GROUP_ECHO;
+        ocd->ocd_group = FID_SEQ_ECHO;
 
-        rc = obd_connect(NULL, &conn, tgt, &echo_uuid, ocd, NULL);
+        rc = obd_connect(env, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
+        if (rc == 0) {
+                /* Turn off pinger because it connects to tgt obd directly. */
+                cfs_spin_lock(&tgt->obd_dev_lock);
+                cfs_list_del_init(&ec->ec_exp->exp_obd_chain_timed);
+                cfs_spin_unlock(&tgt->obd_dev_lock);
+        }
 
         OBD_FREE(ocd, sizeof(*ocd));
 
@@ -1915,22 +2854,33 @@ static int echo_client_setup(struct obd_device *obddev, struct lustre_cfg *lcfg)
                        lustre_cfg_string(lcfg, 1));
                 return (rc);
         }
-        ec->ec_exp = class_conn2export(&conn);
 
         RETURN(rc);
 }
 
 static int echo_client_cleanup(struct obd_device *obddev)
 {
+        struct echo_device *ed = obd2echo_dev(obddev);
         struct echo_client_obd *ec = &obddev->u.echo_client;
         int rc;
         ENTRY;
 
-        if (!list_empty(&obddev->obd_exports)) {
+        /*Do nothing for Metadata echo client*/
+        if (ed == NULL )
+                RETURN(0);
+
+        if (ed->ed_next_ismd) {
+                lu_context_tags_clear(ECHO_MD_CTX_TAG);
+                lu_session_tags_clear(ECHO_MD_SES_TAG);
+                RETURN(0);
+        }
+
+        if (!cfs_list_empty(&obddev->obd_exports)) {
                 CERROR("still has clients!\n");
                 RETURN(-EBUSY);
         }
 
+        LASSERT(cfs_atomic_read(&ec->ec_exp->exp_refcount) > 0);
         rc = obd_disconnect(ec->ec_exp);
         if (rc != 0)
                 CERROR("fail to disconnect device: %d\n", rc);
@@ -1939,18 +2889,17 @@ static int echo_client_cleanup(struct obd_device *obddev)
 }
 
 static int echo_client_connect(const struct lu_env *env,
-                               struct lustre_handle *conn,
+                               struct obd_export **exp,
                                struct obd_device *src, struct obd_uuid *cluuid,
                                struct obd_connect_data *data, void *localdata)
 {
-        struct obd_export *exp;
         int                rc;
+        struct lustre_handle conn = { 0 };
 
         ENTRY;
-        rc = class_connect(conn, src, cluuid);
+        rc = class_connect(&conn, src, cluuid);
         if (rc == 0) {
-                exp = class_conn2export(conn);
-                class_export_put(exp);
+                *exp = class_conn2export(&conn);
         }
 
         RETURN (rc);
@@ -1974,10 +2923,10 @@ static int echo_client_disconnect(struct obd_export *exp)
         ec = &obd->u.echo_client;
 
         /* no more contention on export's lock list */
-        while (!list_empty (&exp->exp_ec_data.eced_locks)) {
-                ecl = list_entry (exp->exp_ec_data.eced_locks.next,
-                                  struct ec_lock, ecl_exp_chain);
-                list_del (&ecl->ecl_exp_chain);
+        while (!cfs_list_empty (&exp->exp_ec_data.eced_locks)) {
+                ecl = cfs_list_entry (exp->exp_ec_data.eced_locks.next,
+                                      struct ec_lock, ecl_exp_chain);
+                cfs_list_del (&ecl->ecl_exp_chain);
 
                 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm,
                                  ecl->ecl_mode, &ecl->ecl_lock_handle);
@@ -2015,10 +2964,16 @@ int echo_client_init(void)
         int rc;
 
         lprocfs_echo_init_vars(&lvars);
-        rc = class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
-                                 LUSTRE_ECHO_CLIENT_NAME, &echo_device_type);
-        if (rc == 0)
-                lu_kmem_init(echo_caches);
+
+        rc = lu_kmem_init(echo_caches);
+        if (rc == 0) {
+                rc = class_register_type(&echo_obd_ops, NULL,
+                                         lvars.module_vars,
+                                         LUSTRE_ECHO_CLIENT_NAME,
+                                         &echo_device_type);
+                if (rc)
+                        lu_kmem_fini(echo_caches);
+        }
         return rc;
 }
 
@@ -2028,3 +2983,4 @@ void echo_client_exit(void)
         lu_kmem_fini(echo_caches);
 }
 
+/** @} echo_client */