Whamcloud - gitweb
LU-1403 ucred: ucred cleanup
[fs/lustre-release.git] / lustre / obdecho / echo_client.c
index 669761b..0a11a3d 100644 (file)
@@ -1,6 +1,4 @@
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
- *
+/*
  * GPL HEADER START
  *
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
@@ -29,8 +27,7 @@
  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2011 Whamcloud, Inc.
- *
+ * Copyright (c) 2011, 2012, Whamcloud, Inc.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -52,6 +49,8 @@
 #include <cl_object.h>
 #include <lustre_fid.h>
 #include <lustre_acl.h>
+#include <lustre_net.h>
+#include <obd_lov.h>
 
 #include "echo_internal.h"
 
@@ -89,7 +88,7 @@ struct echo_object_conf {
 
 struct echo_page {
         struct cl_page_slice   ep_cl;
-        cfs_mutex_t            ep_lock;
+       struct mutex            ep_lock;
         cfs_page_t            *ep_vmpage;
 };
 
@@ -204,12 +203,17 @@ struct echo_thread_info {
         struct cl_io            eti_io;
         struct cl_lock_descr    eti_descr;
         struct lu_fid           eti_fid;
+       struct lu_fid           eti_fid2;
         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;
+       /* per-thread values, can be re-used */
+       void                    *eti_big_lmm;
+       int                     eti_big_lmmsize;
         char                    eti_name[20];
+        struct lu_buf           eti_buf;
         char                    eti_xattr_buf[LUSTRE_POSIX_ACL_MAX_SIZE];
 };
 
@@ -269,8 +273,8 @@ static struct lu_kmem_descr echo_caches[] = {
  *
  * @{
  */
-cfs_page_t *echo_page_vmpage(const struct lu_env *env,
-                             const struct cl_page_slice *slice)
+static cfs_page_t *echo_page_vmpage(const struct lu_env *env,
+                                    const struct cl_page_slice *slice)
 {
         return cl2echo_page(slice)->ep_vmpage;
 }
@@ -282,8 +286,8 @@ static int echo_page_own(const struct lu_env *env,
         struct echo_page *ep = cl2echo_page(slice);
 
         if (!nonblock)
-                cfs_mutex_lock(&ep->ep_lock);
-        else if (!cfs_mutex_trylock(&ep->ep_lock))
+               mutex_lock(&ep->ep_lock);
+       else if (!mutex_trylock(&ep->ep_lock))
                 return -EAGAIN;
         return 0;
 }
@@ -294,8 +298,8 @@ static void echo_page_disown(const struct lu_env *env,
 {
         struct echo_page *ep = cl2echo_page(slice);
 
-        LASSERT(cfs_mutex_is_locked(&ep->ep_lock));
-        cfs_mutex_unlock(&ep->ep_lock);
+       LASSERT(mutex_is_locked(&ep->ep_lock));
+       mutex_unlock(&ep->ep_lock);
 }
 
 static void echo_page_discard(const struct lu_env *env,
@@ -308,7 +312,7 @@ 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)
 {
-        if (cfs_mutex_is_locked(&cl2echo_page(slice)->ep_lock))
+       if (mutex_is_locked(&cl2echo_page(slice)->ep_lock))
                 return -EBUSY;
         return -ENODATA;
 }
@@ -348,7 +352,7 @@ 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 %d vm@%p\n",
-                   ep, cfs_mutex_is_locked(&ep->ep_lock), ep->ep_vmpage);
+                  ep, mutex_is_locked(&ep->ep_lock), ep->ep_vmpage);
         return 0;
 }
 
@@ -430,7 +434,7 @@ static struct cl_page *echo_page_init(const struct lu_env *env,
                 struct echo_object *eco = cl2echo_obj(obj);
                 ep->ep_vmpage = vmpage;
                 page_cache_get(vmpage);
-                cfs_mutex_init(&ep->ep_lock);
+               mutex_init(&ep->ep_lock);
                 cl_page_slice_add(page, &ep->ep_cl, obj, &echo_page_ops);
                 cfs_atomic_inc(&eco->eo_npages);
         }
@@ -515,33 +519,82 @@ static int echo_object_init(const struct lu_env *env, struct lu_object *obj,
         eco->eo_dev = ed;
         cfs_atomic_set(&eco->eo_npages, 0);
 
-        cfs_spin_lock(&ec->ec_lock);
-        cfs_list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
-        cfs_spin_unlock(&ec->ec_lock);
+       spin_lock(&ec->ec_lock);
+       cfs_list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
+       spin_unlock(&ec->ec_lock);
 
-        RETURN(0);
+       RETURN(0);
+}
+
+/* taken from osc_unpackmd() */
+static int echo_alloc_memmd(struct echo_device *ed,
+                           struct lov_stripe_md **lsmp)
+{
+       int lsm_size;
+
+       ENTRY;
+
+       /* If export is lov/osc then use their obd method */
+       if (ed->ed_next != NULL)
+               return obd_alloc_memmd(ed->ed_ec->ec_exp, lsmp);
+       /* OFD has no unpackmd method, do everything here */
+       lsm_size = lov_stripe_md_size(1);
+
+       LASSERT(*lsmp == NULL);
+       OBD_ALLOC(*lsmp, lsm_size);
+       if (*lsmp == NULL)
+               RETURN(-ENOMEM);
+
+       OBD_ALLOC((*lsmp)->lsm_oinfo[0], sizeof(struct lov_oinfo));
+       if ((*lsmp)->lsm_oinfo[0] == NULL) {
+               OBD_FREE(*lsmp, lsm_size);
+               RETURN(-ENOMEM);
+       }
+
+       loi_init((*lsmp)->lsm_oinfo[0]);
+       (*lsmp)->lsm_maxbytes = LUSTRE_STRIPE_MAXBYTES;
+
+       RETURN(lsm_size);
+}
+
+static int echo_free_memmd(struct echo_device *ed, struct lov_stripe_md **lsmp)
+{
+       int lsm_size;
+
+       ENTRY;
+
+       /* If export is lov/osc then use their obd method */
+       if (ed->ed_next != NULL)
+               return obd_free_memmd(ed->ed_ec->ec_exp, lsmp);
+       /* OFD has no unpackmd method, do everything here */
+       lsm_size = lov_stripe_md_size(1);
+
+       LASSERT(*lsmp != NULL);
+       OBD_FREE((*lsmp)->lsm_oinfo[0], sizeof(struct lov_oinfo));
+       OBD_FREE(*lsmp, lsm_size);
+       *lsmp = NULL;
+       RETURN(0);
 }
 
 static void echo_object_free(const struct lu_env *env, struct lu_object *obj)
 {
         struct echo_object *eco    = cl2echo_obj(lu2cl(obj));
         struct echo_client_obd *ec = eco->eo_dev->ed_ec;
-        struct lov_stripe_md *lsm  = eco->eo_lsm;
         ENTRY;
 
         LASSERT(cfs_atomic_read(&eco->eo_npages) == 0);
 
-        cfs_spin_lock(&ec->ec_lock);
+       spin_lock(&ec->ec_lock);
         cfs_list_del_init(&eco->eo_obj_chain);
-        cfs_spin_unlock(&ec->ec_lock);
+       spin_unlock(&ec->ec_lock);
 
         lu_object_fini(obj);
         lu_object_header_fini(obj->lo_header);
 
-        if (lsm)
-                obd_free_memmd(ec->ec_exp, &lsm);
-        OBD_SLAB_FREE_PTR(eco, echo_object_kmem);
-        EXIT;
+       if (eco->eo_lsm)
+               echo_free_memmd(eco->eo_dev, &eco->eo_lsm);
+       OBD_SLAB_FREE_PTR(eco, echo_object_kmem);
+       EXIT;
 }
 
 static int echo_object_print(const struct lu_env *env, void *cookie,
@@ -784,7 +837,7 @@ static struct lu_device *echo_device_alloc(const struct lu_env *env,
         if (tgt == NULL) {
                 CERROR("Can not find tgt device %s\n",
                         lustre_cfg_string(cfg, 1));
-                GOTO(out, rc);
+                GOTO(out, rc = -ENODEV);
         }
 
         next = tgt->obd_lu_dev;
@@ -827,14 +880,14 @@ static struct lu_device *echo_device_alloc(const struct lu_env *env,
 
                 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);
+               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;
+                       }
+               }
+               spin_unlock(&ls->ls_ld_lock);
 
                 if (found == 0) {
                         CERROR("%s is not lu device type!\n",
@@ -962,29 +1015,29 @@ static struct lu_device *echo_device_free(const struct lu_env *env,
          * 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);
-
-        /* purge again */
-        lu_site_purge(env, &ed->ed_site->cs_lu, -1);
-
-        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);
+       spin_lock(&ec->ec_lock);
+       cfs_list_for_each_entry(eco, &ec->ec_objects, eo_obj_chain)
+               eco->eo_deleted = 1;
+       spin_unlock(&ec->ec_lock);
+
+       /* purge again */
+       lu_site_purge(env, &ed->ed_site->cs_lu, -1);
+
+       CDEBUG(D_INFO,
+              "Waiting for the reference of echo object to be dropped\n");
+
+       /* Wait for the last reference to be dropped. */
+       spin_lock(&ec->ec_lock);
+       while (!cfs_list_empty(&ec->ec_objects)) {
+               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);
+               spin_lock(&ec->ec_lock);
+       }
+       spin_unlock(&ec->ec_lock);
 
         LASSERT(cfs_list_empty(&ec->ec_locks));
 
@@ -992,7 +1045,6 @@ static struct lu_device *echo_device_free(const struct lu_env *env,
 
         echo_client_cleanup(d->ld_obd);
         echo_fid_fini(d->ld_obd);
-
         while (next && !ed->ed_next_ismd)
                 next = next->ld_type->ldt_ops->ldto_device_free(env, next);
 
@@ -1081,6 +1133,9 @@ static struct echo_object *cl_echo_object_find(struct echo_device *d,
         fid  = &info->eti_fid;
         lsm2fid(lsm, fid);
 
+       /* In the function below, .hs_keycmp resolves to
+        * lu_obj_hop_keycmp() */
+       /* coverity[overrun-buffer-val] */
         obj = cl_object_find(env, echo_dev2cl(d), fid, &conf->eoc_cl);
         if (IS_ERR(obj))
                 GOTO(out, eco = (void*)obj);
@@ -1111,7 +1166,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));
-                cfs_set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
+               set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
         }
 
         cl_object_put(env, obj);
@@ -1151,18 +1206,19 @@ static int cl_echo_enqueue0(struct lu_env *env, struct echo_object *eco,
                 rc = cl_wait(env, lck);
                 if (rc == 0) {
                         el = cl2echo_lock(cl_lock_at(lck, &echo_device_type));
-                        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);
+                       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;
+                       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,
@@ -1182,6 +1238,7 @@ static int cl_echo_enqueue(struct echo_object *eco, obd_off start, obd_off end,
         info = echo_env_info(env);
         io = &info->eti_io;
 
+       io->ci_ignore_layout = 1;
         result = cl_io_init(env, io, CIT_MISC, echo_obj2cl(eco));
         if (result < 0)
                 GOTO(out, result);
@@ -1206,7 +1263,7 @@ static int cl_echo_cancel0(struct lu_env *env, struct echo_device *ed,
         ENTRY;
 
         LASSERT(ec != NULL);
-        cfs_spin_lock (&ec->ec_lock);
+       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);
@@ -1219,7 +1276,7 @@ static int cl_echo_cancel0(struct lu_env *env, struct echo_device *ed,
                         break;
                 }
         }
-        cfs_spin_unlock (&ec->ec_lock);
+       spin_unlock(&ec->ec_lock);
 
         if (!found)
                 RETURN(-ENOENT);
@@ -1291,6 +1348,8 @@ static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
         queue   = &info->eti_queue;
 
         cl_2queue_init(queue);
+
+       io->ci_ignore_layout = 1;
         rc = cl_io_init(env, io, CIT_MISC, obj);
         if (rc < 0)
                 GOTO(out, rc);
@@ -1338,8 +1397,7 @@ static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
                 if (async)
                         rc = cl_echo_async_brw(env, io, typ, queue);
                 else
-                        rc = cl_io_submit_sync(env, io, typ, queue,
-                                               CRP_NORMAL, 0);
+                       rc = cl_io_submit_sync(env, io, typ, queue, 0);
                 CDEBUG(D_INFO, "echo_client %s write returns %d\n",
                        async ? "async" : "sync", rc);
         }
@@ -1412,31 +1470,154 @@ echo_copyin_lsm (struct echo_device *ed, struct lov_stripe_md *lsm,
 }
 
 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);
+                                     __u64 id)
+{
+       sprintf(name, LPU64, id);
+       lname->ln_name = name;
+       lname->ln_namelen = strlen(name);
+}
+
+/* similar to mdt_attr_get_complex */
+static int echo_big_lmm_get(const struct lu_env *env, struct md_object *o,
+                           struct md_attr *ma)
+{
+       struct echo_thread_info *info = echo_env_info(env);
+       int                      rc;
+
+       ENTRY;
+
+       LASSERT(ma->ma_lmm_size > 0);
+
+       rc = mo_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LOV);
+       if (rc < 0)
+               RETURN(rc);
+
+       /* big_lmm may need to be grown */
+       if (info->eti_big_lmmsize < rc) {
+               int size = size_roundup_power2(rc);
+
+               if (info->eti_big_lmmsize > 0) {
+                       /* free old buffer */
+                       LASSERT(info->eti_big_lmm);
+                       OBD_FREE_LARGE(info->eti_big_lmm,
+                                      info->eti_big_lmmsize);
+                       info->eti_big_lmm = NULL;
+                       info->eti_big_lmmsize = 0;
+               }
+
+               OBD_ALLOC_LARGE(info->eti_big_lmm, size);
+               if (info->eti_big_lmm == NULL)
+                       RETURN(-ENOMEM);
+               info->eti_big_lmmsize = size;
+       }
+       LASSERT(info->eti_big_lmmsize >= rc);
+
+       info->eti_buf.lb_buf = info->eti_big_lmm;
+       info->eti_buf.lb_len = info->eti_big_lmmsize;
+       rc = mo_xattr_get(env, o, &info->eti_buf, XATTR_NAME_LOV);
+       if (rc < 0)
+               RETURN(rc);
+
+       ma->ma_valid |= MA_LOV;
+       ma->ma_lmm = info->eti_big_lmm;
+       ma->ma_lmm_size = rc;
+
+       RETURN(0);
+}
+
+int echo_attr_get_complex(const struct lu_env *env, struct md_object *next,
+                         struct md_attr *ma)
+{
+       struct echo_thread_info *info = echo_env_info(env);
+       struct lu_buf           *buf = &info->eti_buf;
+       cfs_umode_t              mode = lu_object_attr(&next->mo_lu);
+       int                      need = ma->ma_need;
+       int                      rc = 0, rc2;
+
+       ENTRY;
+
+       ma->ma_valid = 0;
+
+       if (need & MA_INODE) {
+               ma->ma_need = MA_INODE;
+               rc = mo_attr_get(env, next, ma);
+               if (rc)
+                       GOTO(out, rc);
+               ma->ma_valid |= MA_INODE;
+       }
+
+       if (need & MA_LOV) {
+               if (S_ISREG(mode) || S_ISDIR(mode)) {
+                       LASSERT(ma->ma_lmm_size > 0);
+                       buf->lb_buf = ma->ma_lmm;
+                       buf->lb_len = ma->ma_lmm_size;
+                       rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LOV);
+                       if (rc2 > 0) {
+                               ma->ma_lmm_size = rc2;
+                               ma->ma_valid |= MA_LOV;
+                       } else if (rc2 == -ENODATA) {
+                               /* no LOV EA */
+                               ma->ma_lmm_size = 0;
+                       } else if (rc2 == -ERANGE) {
+                               rc2 = echo_big_lmm_get(env, next, ma);
+                               if (rc2 < 0)
+                                       GOTO(out, rc = rc2);
+                       } else {
+                               GOTO(out, rc = rc2);
+                       }
+               }
+       }
+
+#ifdef CONFIG_FS_POSIX_ACL
+       if (need & MA_ACL_DEF && S_ISDIR(mode)) {
+               buf->lb_buf = ma->ma_acl;
+               buf->lb_len = ma->ma_acl_size;
+               rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
+               if (rc2 > 0) {
+                       ma->ma_acl_size = rc2;
+                       ma->ma_valid |= MA_ACL_DEF;
+               } else if (rc2 == -ENODATA) {
+                       /* no ACLs */
+                       ma->ma_acl_size = 0;
+               } else {
+                       GOTO(out, rc = rc2);
+               }
+       }
+#endif
+out:
+       ma->ma_need = need;
+       CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
+              rc, ma->ma_valid, ma->ma_lmm);
+       RETURN(rc);
 }
 
-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);
+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;
+       struct echo_thread_info *info = echo_env_info(env);
+       struct lu_fid           *fid2 = &info->eti_fid2;
+       struct lu_object_conf    conf = { .loc_flags = LOC_F_NEW };
+       int                      rc;
+
+       ENTRY;
+
+       rc = mdo_lookup(env, parent, lname, fid2, spec);
+       if (rc == 0)
+               return -EEXIST;
+       else if (rc != -ENOENT)
+               return rc;
+
+       ec_child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev,
+                                    fid, &conf);
         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);
+               RETURN(PTR_ERR(ec_child));
         }
 
         child = lu_object_locate(ec_child->lo_header, ld->ld_type);
@@ -1448,6 +1629,10 @@ static int echo_md_create_internal(const struct lu_env *env,
         CDEBUG(D_RPCTRACE, "Start creating object "DFID" %s %p\n",
                PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
 
+       /*
+        * Do not perform lookup sanity check. We know that name does not exist.
+        */
+       spec->sp_cr_lookup = 0;
         rc = mdo_create(env, parent, lname, lu2md(child), spec, ma);
         if (rc) {
                 CERROR("Can not create child "DFID": rc = %d\n", PFID(fid), rc);
@@ -1455,45 +1640,27 @@ static int echo_md_create_internal(const struct lu_env *env,
         }
         CDEBUG(D_RPCTRACE, "End creating object "DFID" %s %p rc  = %d\n",
                PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent, rc);
+       EXIT;
 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)
+static int echo_set_lmm_size(const struct lu_env *env, struct lu_device *ld,
+                            struct md_attr *ma)
 {
-        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);
-        }
+       struct echo_thread_info *info = echo_env_info(env);
 
-        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);
+       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);
+       } else {
+               LASSERT(info->eti_big_lmmsize);
+               ma->ma_lmm = info->eti_big_lmm;
+               ma->ma_lmm_size = info->eti_big_lmmsize;
+       }
 
-        RETURN(0);
+       return 0;
 }
 
 static int echo_create_md_object(const struct lu_env *env,
@@ -1511,35 +1678,34 @@ static int echo_create_md_object(const struct lu_env *env,
         struct md_attr          *ma = &info->eti_ma;
         struct lu_device        *ld = ed->ed_next;
         int                      rc = 0;
-        int                      max_lmm_size = 0;
         int                      i;
 
+       ENTRY;
+
         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));
-        }
+       if (parent == NULL)
+               RETURN(-ENXIO);
 
         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);
+               echo_set_lmm_size(env, ld, ma);
                 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->u.sp_ea.eadatalen = sizeof(*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_valid = LA_CTIME | LA_MODE;
         ma->ma_attr.la_ctime = cfs_time_current_64();
 
         if (name != NULL) {
@@ -1548,7 +1714,7 @@ static int echo_create_md_object(const struct lu_env *env,
                 /* 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);
+               RETURN(rc);
         }
 
         /* Create multiple object sequenced by id */
@@ -1568,12 +1734,7 @@ static int echo_create_md_object(const struct lu_env *env,
                 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;
+       RETURN(rc);
 }
 
 static struct lu_object *echo_md_lookup(const struct lu_env *env,
@@ -1609,27 +1770,17 @@ static int echo_setattr_object(const struct lu_env *env,
         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;
+        struct lu_buf           *buf = &info->eti_buf;
         int                      rc = 0;
         int                      i;
 
+       ENTRY;
+
         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);
-        }
+       if (parent == NULL)
+               RETURN(-ENXIO);
 
-        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;
 
@@ -1650,21 +1801,27 @@ static int echo_setattr_object(const struct lu_env *env,
                         break;
                 }
 
-                CDEBUG(D_RPCTRACE, "Start getattr object "DFID"\n",
+                CDEBUG(D_RPCTRACE, "Start setattr 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",
+
+               buf->lb_buf = info->eti_xattr_buf;
+               buf->lb_len = sizeof(info->eti_xattr_buf);
+
+                sprintf(name, "%s.test1", XATTR_USER_PREFIX);
+                rc = mo_xattr_set(env, lu2md(child), buf, name,
+                                  LU_XATTR_CREATE);
+               if (rc < 0) {
+                        CERROR("Can not setattr 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",
+                CDEBUG(D_RPCTRACE, "End setattr object "DFID"\n",
                        PFID(lu_object_fid(child)));
                 id++;
                 lu_object_put(env, ec_child);
         }
-        return rc;
+       RETURN(rc);
 }
 
 static int echo_getattr_object(const struct lu_env *env,
@@ -1678,21 +1835,16 @@ static int echo_getattr_object(const struct lu_env *env,
         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;
 
+       ENTRY;
+
         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);
-        }
+       if (parent == NULL)
+               RETURN(-ENXIO);
 
         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);
@@ -1700,7 +1852,9 @@ static int echo_getattr_object(const struct lu_env *env,
         for (i = 0; i < count; i++) {
                 struct lu_object *ec_child, *child;
 
+                ma->ma_valid = 0;
                 echo_md_build_name(lname, name, id);
+               echo_set_lmm_size(env, ld, ma);
 
                 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
                 if (IS_ERR(ec_child)) {
@@ -1713,12 +1867,12 @@ static int echo_getattr_object(const struct lu_env *env,
                 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);
+                       RETURN(-EINVAL);
                 }
 
                 CDEBUG(D_RPCTRACE, "Start getattr object "DFID"\n",
                        PFID(lu_object_fid(child)));
-                rc = mo_attr_get(env, lu2md(child), ma);
+               rc = echo_attr_get_complex(env, lu2md(child), ma);
                 if (rc) {
                         CERROR("Can not getattr child "DFID": rc = %d\n",
                                 PFID(lu_object_fid(child)), rc);
@@ -1731,12 +1885,7 @@ static int echo_getattr_object(const struct lu_env *env,
                 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;
+       RETURN(rc);
 }
 
 static int echo_lookup_object(const struct lu_env *env,
@@ -1754,10 +1903,8 @@ static int echo_lookup_object(const struct lu_env *env,
         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);
-        }
+       if (parent == NULL)
+               return -ENXIO;
 
         /*prepare the requests*/
         for (i = 0; i < count; i++) {
@@ -1765,6 +1912,7 @@ static int echo_lookup_object(const struct lu_env *env,
 
                 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);
@@ -1789,6 +1937,8 @@ static int echo_md_destroy_internal(const struct lu_env *env,
         struct lu_object   *child;
         int                 rc;
 
+       ENTRY;
+
         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,
@@ -1831,7 +1981,6 @@ static int echo_destroy_object(const struct lu_env *env,
         struct lu_device        *ld = ed->ed_next;
         struct lu_object        *parent;
         int                      rc = 0;
-        int                      max_lmm_size = 0;
         int                      i;
         ENTRY;
 
@@ -1846,28 +1995,19 @@ static int echo_destroy_object(const struct lu_env *env,
         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);
+               RETURN(rc);
         }
 
         /*prepare the requests*/
         for (i = 0; i < count; i++) {
                 char *tmp_name = info->eti_name;
 
+                ma->ma_valid = 0;
                 echo_md_build_name(lname, tmp_name, id);
 
                 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
@@ -1879,17 +2019,12 @@ static int echo_destroy_object(const struct lu_env *env,
                 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);
+       RETURN(rc);
 }
 
-struct lu_object *echo_resolve_path(const struct lu_env *env,
-                                    struct echo_device *ed, char *path,
-                                    int path_len)
+static 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);
@@ -1902,8 +2037,6 @@ struct lu_object *echo_resolve_path(const struct lu_env *env,
         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);
@@ -1958,17 +2091,47 @@ struct lu_object *echo_resolve_path(const struct lu_env *env,
         RETURN(parent);
 }
 
+static void echo_ucred_init(struct lu_env *env)
+{
+       struct lu_ucred *ucred = lu_ucred(env);
+
+       ucred->uc_valid = UCRED_INVALID;
+
+       ucred->uc_suppgids[0] = -1;
+       ucred->uc_suppgids[1] = -1;
+
+       ucred->uc_uid   = ucred->uc_o_uid   = cfs_curproc_uid();
+       ucred->uc_gid   = ucred->uc_o_gid   = cfs_curproc_gid();
+       ucred->uc_fsuid = ucred->uc_o_fsuid = cfs_curproc_fsuid();
+       ucred->uc_fsgid = ucred->uc_o_fsgid = cfs_curproc_fsgid();
+       ucred->uc_cap   = cfs_curproc_cap_pack();
+
+       /* remove fs privilege for non-root user. */
+       if (ucred->uc_fsuid)
+               ucred->uc_cap &= ~CFS_CAP_FS_MASK;
+       ucred->uc_valid = UCRED_NEW;
+}
+
+static void echo_ucred_fini(struct lu_env *env)
+{
+       struct lu_ucred *ucred = lu_ucred(env);
+       ucred->uc_valid = UCRED_INIT;
+}
+
+#define ECHO_MD_CTX_TAG (LCT_REMEMBER | LCT_MD_THREAD)
+#define ECHO_MD_SES_TAG (LCT_REMEMBER | LCT_SESSION)
 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 echo_thread_info *info;
         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;
+        int                    rc = 0;
         ENTRY;
 
         if (ld == NULL) {
@@ -1984,26 +2147,36 @@ static int echo_md_handler(struct echo_device *ed, int command,
         env = cl_env_get(&refcheck);
         if (IS_ERR(env))
                 RETURN(PTR_ERR(env));
-        lu_env_refill(env);
+
+        rc = lu_env_refill_by_tags(env, ECHO_MD_CTX_TAG, ECHO_MD_SES_TAG);
+       if (rc != 0)
+               GOTO(out_env, rc);
+
+       /* init big_lmm buffer */
+       info = echo_env_info(env);
+       LASSERT(info->eti_big_lmm == NULL);
+       OBD_ALLOC_LARGE(info->eti_big_lmm, MIN_MD_SIZE);
+       if (info->eti_big_lmm == NULL)
+               GOTO(out_env, rc = -ENOMEM);
+       info->eti_big_lmmsize = MIN_MD_SIZE;
 
         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));
+               GOTO(out_free, rc = 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);
-                }
+                       GOTO(out_put, rc = -ENOMEM);
+               if (cfs_copy_from_user(name, data->ioc_pbuf2, namelen))
+                       GOTO(out_name, rc = -EFAULT);
         }
 
+       echo_ucred_init(env);
+
         switch (command) {
         case ECHO_MD_CREATE:
         case ECHO_MD_MKDIR: {
@@ -2016,6 +2189,9 @@ static int echo_md_handler(struct echo_device *ed, int command,
                 fid->f_seq = data->ioc_obdo1.o_seq;
                 fid->f_oid = (__u32)data->ioc_obdo1.o_id;
                 fid->f_ver = 0;
+               /* In the function below, .hs_keycmp resolves to
+                * lu_obj_hop_keycmp() */
+               /* coverity[overrun-buffer-val] */
                 rc = echo_create_md_object(env, ed, parent, fid, name, namelen,
                                            id, mode, count, stripe_count,
                                            stripe_index);
@@ -2043,16 +2219,26 @@ static int echo_md_handler(struct echo_device *ed, int command,
                 rc = -EINVAL;
                 break;
         }
+       echo_ucred_fini(env);
+
+out_name:
         if (name != NULL)
                 OBD_FREE(name, namelen + 1);
+out_put:
         lu_object_put(env, parent);
+out_free:
+       LASSERT(info->eti_big_lmm);
+       OBD_FREE_LARGE(info->eti_big_lmm, info->eti_big_lmmsize);
+       info->eti_big_lmm = NULL;
+       info->eti_big_lmmsize = 0;
+out_env:
         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)
+static int echo_create_object(const struct lu_env *env, struct echo_device *ed,
+                              int on_target, struct obdo *oa, void *ulsm,
+                              int ulsm_nob, struct obd_trans_info *oti)
 {
         struct echo_object     *eco;
         struct echo_client_obd *ec = ed->ed_ec;
@@ -2068,7 +2254,7 @@ static int echo_create_object(struct echo_device *ed, int on_target,
                 RETURN(-EINVAL);
         }
 
-        rc = obd_alloc_memmd(ec->ec_exp, &lsm);
+       rc = echo_alloc_memmd(ed, &lsm);
         if (rc < 0) {
                 CERROR("Cannot allocate md: rc = %d\n", rc);
                 GOTO(failed, rc);
@@ -2111,7 +2297,7 @@ static int echo_create_object(struct echo_device *ed, int on_target,
                 /* 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);
+                rc = obd_create(env, ec->ec_exp, oa, &lsm, oti);
                 if (rc != 0) {
                         CERROR("Cannot create objects: rc = %d\n", rc);
                         GOTO(failed, rc);
@@ -2133,9 +2319,9 @@ static int echo_create_object(struct echo_device *ed, int on_target,
 
  failed:
         if (created && rc)
-                obd_destroy(ec->ec_exp, oa, lsm, oti, NULL, NULL);
+                obd_destroy(env, ec->ec_exp, oa, lsm, oti, NULL, NULL);
         if (lsm)
-                obd_free_memmd(ec->ec_exp, &lsm);
+               echo_free_memmd(ed, &lsm);
         if (rc)
                 CERROR("create object failed with: rc = %d\n", rc);
         return (rc);
@@ -2144,7 +2330,6 @@ static int echo_create_object(struct echo_device *ed, int on_target,
 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
                            struct obdo *oa)
 {
-        struct echo_client_obd *ec  = ed->ed_ec;
         struct lov_stripe_md   *lsm = NULL;
         struct echo_object     *eco;
         int                     rc;
@@ -2157,7 +2342,7 @@ static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
                 RETURN(-EINVAL);
         }
 
-        rc = obd_alloc_memmd(ec->ec_exp, &lsm);
+       rc = echo_alloc_memmd(ed, &lsm);
         if (rc < 0)
                 RETURN(rc);
 
@@ -2174,7 +2359,7 @@ static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
         else
                 rc = PTR_ERR(eco);
         if (lsm)
-                obd_free_memmd(ec->ec_exp, &lsm);
+               echo_free_memmd(ed, &lsm);
         RETURN(rc);
 }
 
@@ -2283,7 +2468,6 @@ static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
                             obd_size count, int async,
                             struct obd_trans_info *oti)
 {
-        struct echo_client_obd *ec  = ed->ed_ec;
         struct lov_stripe_md   *lsm = eco->eo_lsm;
         obd_count               npages;
         struct brw_page        *pga;
@@ -2348,13 +2532,9 @@ static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
                                                      oa->o_id, off, pgp->count);
         }
 
-        if (ed->ed_next == NULL) {
-                struct obd_info oinfo = { { { 0 } } };
-                oinfo.oi_oa = oa;
-                oinfo.oi_md = lsm;
-                rc = obd_brw(rw, ec->ec_exp, &oinfo, npages, pga, oti);
-        } else
-                rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
+        /* brw mode can only be used at client */
+        LASSERT(ed->ed_next != NULL);
+        rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
 
  out:
         if (rc != 0 || rw != OBD_BRW_READ)
@@ -2378,10 +2558,12 @@ static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
         RETURN(rc);
 }
 
-static int echo_client_prep_commit(struct obd_export *exp, int rw,
-                                   struct obdo *oa, struct echo_object *eco,
-                                   obd_off offset, obd_size count,
-                                   obd_size batch, struct obd_trans_info *oti)
+static int echo_client_prep_commit(const struct lu_env *env,
+                                  struct obd_export *exp, int rw,
+                                  struct obdo *oa, struct echo_object *eco,
+                                  obd_off offset, obd_size count,
+                                  obd_size batch, struct obd_trans_info *oti,
+                                  int async)
 {
         struct lov_stripe_md *lsm = eco->eo_lsm;
         struct obd_ioobj ioo;
@@ -2389,7 +2571,8 @@ static int echo_client_prep_commit(struct obd_export *exp, int rw,
         struct niobuf_remote *rnb;
         obd_off off;
         obd_size npages, tot_pages;
-        int i, ret = 0;
+       int i, ret = 0, brw_flags = 0;
+
         ENTRY;
 
         if (count <= 0 || (count & (~CFS_PAGE_MASK)) != 0 ||
@@ -2405,6 +2588,9 @@ static int echo_client_prep_commit(struct obd_export *exp, int rw,
         if (lnb == NULL || rnb == NULL)
                 GOTO(out, ret = -ENOMEM);
 
+       if (rw == OBD_BRW_WRITE && async)
+               brw_flags |= OBD_BRW_ASYNC;
+
         obdo_to_ioobj(oa, &ioo);
 
         off = offset;
@@ -2418,14 +2604,15 @@ static int echo_client_prep_commit(struct obd_export *exp, int rw,
                 for (i = 0; i < npages; i++, off += CFS_PAGE_SIZE) {
                         rnb[i].offset = off;
                         rnb[i].len = CFS_PAGE_SIZE;
+                       rnb[i].flags = brw_flags;
                 }
 
                 ioo.ioo_bufcnt = npages;
                 oti->oti_transno = 0;
 
                 lpages = npages;
-                ret = obd_preprw(rw, exp, oa, 1, &ioo, rnb, &lpages, lnb, oti,
-                                 NULL);
+               ret = obd_preprw(env, rw, exp, oa, 1, &ioo, rnb, &lpages,
+                                 lnb, oti, NULL);
                 if (ret != 0)
                         GOTO(out, ret);
                 LASSERT(lpages == npages);
@@ -2437,6 +2624,9 @@ static int echo_client_prep_commit(struct obd_export *exp, int rw,
                         if (page == NULL && lnb[i].rc == 0)
                                 continue;
 
+                        if (async)
+                                lnb[i].flags |= OBD_BRW_ASYNC;
+
                         if (oa->o_id == ECHO_PERSISTENT_OBJID ||
                             (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
                             (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
@@ -2454,7 +2644,8 @@ static int echo_client_prep_commit(struct obd_export *exp, int rw,
                                                              rnb[i].len);
                 }
 
-                ret = obd_commitrw(rw, exp, oa, 1,&ioo,rnb,npages,lnb,oti,ret);
+               ret = obd_commitrw(env, rw, exp, oa, 1, &ioo,
+                                  rnb, npages, lnb, oti, ret);
                 if (ret != 0)
                         GOTO(out, ret);
 
@@ -2470,17 +2661,19 @@ out:
         RETURN(ret);
 }
 
-static int echo_client_brw_ioctl(int rw, struct obd_export *exp,
-                                 struct obd_ioctl_data *data)
+static int echo_client_brw_ioctl(const struct lu_env *env, int rw,
+                                struct obd_export *exp,
+                                struct obd_ioctl_data *data,
+                                struct obd_trans_info *dummy_oti)
 {
         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 = { 0 };
         struct obdo *oa = &data->ioc_obdo1;
         struct echo_object *eco;
         int rc;
         int async = 1;
+        long test_mode;
         ENTRY;
 
         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
@@ -2491,20 +2684,33 @@ static int echo_client_brw_ioctl(int rw, struct obd_export *exp,
 
         oa->o_valid &= ~OBD_MD_FLHANDLE;
 
-        switch((long)data->ioc_pbuf1) {
-        case 1:
+       /* OFD/obdfilter works only via prep/commit */
+        test_mode = (long)data->ioc_pbuf1;
+        if (test_mode == 1)
                 async = 0;
+
+        if (ed->ed_next == NULL && test_mode != 3) {
+                test_mode = 3;
+                data->ioc_plen1 = data->ioc_count;
+        }
+
+        /* Truncate batch size to maximum */
+        if (data->ioc_plen1 > PTLRPC_MAX_BRW_SIZE)
+                data->ioc_plen1 = PTLRPC_MAX_BRW_SIZE;
+
+        switch (test_mode) {
+        case 1:
                 /* fall through */
         case 2:
                 rc = echo_client_kbrw(ed, rw, oa,
                                       eco, data->ioc_offset,
-                                      data->ioc_count, async, &dummy_oti);
+                                     data->ioc_count, async, dummy_oti);
                 break;
         case 3:
-                rc = echo_client_prep_commit(ec->ec_exp, rw, oa,
-                                            eco, data->ioc_offset,
-                                            data->ioc_count, data->ioc_plen1,
-                                            &dummy_oti);
+               rc = echo_client_prep_commit(env, ec->ec_exp, rw, oa,
+                                            eco, data->ioc_offset,
+                                            data->ioc_count, data->ioc_plen1,
+                                            dummy_oti, async);
                 break;
         default:
                 rc = -EINVAL;
@@ -2562,8 +2768,8 @@ echo_client_cancel(struct obd_export *exp, struct obdo *oa)
 }
 
 static int
-echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
-                      int len, void *karg, void *uarg)
+echo_client_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
+                      void *karg, void *uarg)
 {
         struct obd_device      *obd = exp->exp_obd;
         struct echo_device     *ed = obd2echo_dev(obd);
@@ -2571,6 +2777,7 @@ echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
         struct echo_object     *eco;
         struct obd_ioctl_data  *data = karg;
         struct obd_trans_info   dummy_oti;
+        struct lu_env          *env;
         struct oti_req_ack_lock *ack_lock;
         struct obdo            *oa;
         struct lu_fid           fid;
@@ -2579,10 +2786,6 @@ echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
         int                     i;
         ENTRY;
 
-#ifndef HAVE_UNLOCKED_IOCTL
-        cfs_unlock_kernel();
-#endif
-
         memset(&dummy_oti, 0, sizeof(dummy_oti));
 
         oa = &data->ioc_obdo1;
@@ -2596,14 +2799,21 @@ echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
         if (rc < 0)
                 RETURN(rc);
 
+        OBD_ALLOC_PTR(env);
+        if (env == NULL)
+                RETURN(-ENOMEM);
+
+        rc = lu_env_init(env, LCT_DT_THREAD);
+        if (rc)
+                GOTO(out, rc = -ENOMEM);
+
         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, oa,
-                                         data->ioc_pbuf1, data->ioc_plen1,
-                                         &dummy_oti);
+                rc = echo_create_object(env, ed, 1, oa, data->ioc_pbuf1,
+                                        data->ioc_plen1, &dummy_oti);
                 GOTO(out, rc);
 
         case OBD_IOC_ECHO_MD: {
@@ -2636,7 +2846,7 @@ echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
                 GOTO(out, rc);
         }
         case OBD_IOC_ECHO_ALLOC_SEQ: {
-                struct lu_env   *env;
+                struct lu_env   *cl_env;
                 int              refcheck;
                 __u64            seq;
                 int              max_count;
@@ -2644,12 +2854,19 @@ echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
                 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));
-                lu_env_refill(env);
-                rc = seq_client_get_seq(env, ed->ed_cl_seq, &seq);
-                cl_env_put(env, &refcheck);
+                cl_env = cl_env_get(&refcheck);
+                if (IS_ERR(cl_env))
+                        GOTO(out, rc = PTR_ERR(cl_env));
+
+                rc = lu_env_refill_by_tags(cl_env, ECHO_MD_CTX_TAG,
+                                            ECHO_MD_SES_TAG);
+                if (rc != 0) {
+                        cl_env_put(cl_env, &refcheck);
+                        GOTO(out, rc);
+                }
+
+                rc = seq_client_get_seq(cl_env, ed->ed_cl_seq, &seq);
+                cl_env_put(cl_env, &refcheck);
                 if (rc < 0) {
                         CERROR("%s: Can not alloc seq: rc = %d\n",
                                obd->obd_name, rc);
@@ -2669,9 +2886,9 @@ 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, oa);
+                rc = echo_get_object(&eco, ed, oa);
                 if (rc == 0) {
-                        rc = obd_destroy(ec->ec_exp, oa, eco->eo_lsm,
+                        rc = obd_destroy(env, ec->ec_exp, oa, eco->eo_lsm,
                                          &dummy_oti, NULL, NULL);
                         if (rc == 0)
                                 eco->eo_deleted = 1;
@@ -2680,12 +2897,12 @@ echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
                 GOTO(out, rc);
 
         case OBD_IOC_GETATTR:
-                rc = echo_get_object (&eco, ed, oa);
+                rc = echo_get_object(&eco, ed, oa);
                 if (rc == 0) {
                         struct obd_info oinfo = { { { 0 } } };
                         oinfo.oi_md = eco->eo_lsm;
                         oinfo.oi_oa = oa;
-                        rc = obd_getattr(ec->ec_exp, &oinfo);
+                        rc = obd_getattr(env, ec->ec_exp, &oinfo);
                         echo_put_object(eco);
                 }
                 GOTO(out, rc);
@@ -2694,13 +2911,13 @@ 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, oa);
+                rc = echo_get_object(&eco, ed, oa);
                 if (rc == 0) {
                         struct obd_info oinfo = { { { 0 } } };
                         oinfo.oi_oa = oa;
                         oinfo.oi_md = eco->eo_lsm;
 
-                        rc = obd_setattr(ec->ec_exp, &oinfo, NULL);
+                        rc = obd_setattr(env, ec->ec_exp, &oinfo, NULL);
                         echo_put_object(eco);
                 }
                 GOTO(out, rc);
@@ -2712,7 +2929,7 @@ echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
                 rw = OBD_BRW_WRITE;
                 /* fall through */
         case OBD_IOC_BRW_READ:
-                rc = echo_client_brw_ioctl(rw, exp, data);
+               rc = echo_client_brw_ioctl(env, rw, exp, data, &dummy_oti);
                 GOTO(out, rc);
 
         case ECHO_IOC_GET_STRIPE:
@@ -2735,7 +2952,7 @@ echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
                                 echo_put_object(eco);
                         }
                 } else {
-                        rc = echo_create_object(ed, 0, oa,
+                        rc = echo_create_object(env, ed, 0, oa,
                                                 data->ioc_pbuf1,
                                                 data->ioc_plen1, &dummy_oti);
                 }
@@ -2761,7 +2978,9 @@ echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
         }
 
         EXIT;
- out:
+out:
+        lu_env_fini(env);
+        OBD_FREE_PTR(env);
 
         /* XXX this should be in a helper also called by target_send_reply */
         for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
@@ -2771,10 +2990,6 @@ echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
                 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
         }
 
-#ifndef HAVE_UNLOCKED_IOCTL
-        cfs_lock_kernel();
-#endif
-
         return rc;
 }
 
@@ -2800,15 +3015,15 @@ static int echo_client_setup(const struct lu_env *env,
                 RETURN(-EINVAL);
         }
 
-        cfs_spin_lock_init (&ec->ec_lock);
+       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)) {
-                cl_set_ctx_tags(LCT_REMEMBER | LCT_NOREF | LCT_MD_THREAD);
-                cl_set_ses_tags(LCT_SESSION | LCT_REMEMBER | LCT_NOREF);
+                lu_context_tags_update(ECHO_MD_CTX_TAG);
+                lu_session_tags_update(ECHO_MD_SES_TAG);
                 RETURN(0);
         }
 
@@ -2821,16 +3036,16 @@ static int echo_client_setup(const struct lu_env *env,
 
         ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
                                  OBD_CONNECT_GRANT | OBD_CONNECT_FULL20 |
-                                 OBD_CONNECT_64BITHASH;
+                                OBD_CONNECT_64BITHASH | OBD_CONNECT_LVB_TYPE;
         ocd->ocd_version = LUSTRE_VERSION_CODE;
         ocd->ocd_group = FID_SEQ_ECHO;
 
         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);
+               spin_lock(&tgt->obd_dev_lock);
+               cfs_list_del_init(&ec->ec_exp->exp_obd_chain_timed);
+               spin_unlock(&tgt->obd_dev_lock);
         }
 
         OBD_FREE(ocd, sizeof(*ocd));
@@ -2852,8 +3067,14 @@ static int echo_client_cleanup(struct obd_device *obddev)
         ENTRY;
 
         /*Do nothing for Metadata echo client*/
-        if (!ed || ed->ed_next_ismd)
+        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");
@@ -2925,7 +3146,7 @@ static int echo_client_disconnect(struct obd_export *exp)
         return rc;
 }
 
-static struct obd_ops echo_obd_ops = {
+static struct obd_ops echo_client_obd_ops = {
         .o_owner       = THIS_MODULE,
 
 #if 0
@@ -2947,7 +3168,7 @@ int echo_client_init(void)
 
         rc = lu_kmem_init(echo_caches);
         if (rc == 0) {
-                rc = class_register_type(&echo_obd_ops, NULL,
+                rc = class_register_type(&echo_client_obd_ops, NULL,
                                          lvars.module_vars,
                                          LUSTRE_ECHO_CLIENT_NAME,
                                          &echo_device_type);
@@ -2963,4 +3184,59 @@ void echo_client_exit(void)
         lu_kmem_fini(echo_caches);
 }
 
+#ifdef __KERNEL__
+static int __init obdecho_init(void)
+{
+        struct lprocfs_static_vars lvars;
+        int rc;
+
+        ENTRY;
+        LCONSOLE_INFO("Echo OBD driver; http://www.lustre.org/\n");
+
+        LASSERT(CFS_PAGE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
+
+        lprocfs_echo_init_vars(&lvars);
+
+# ifdef HAVE_SERVER_SUPPORT
+        rc = echo_persistent_pages_init();
+        if (rc != 0)
+                goto failed_0;
+
+        rc = class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
+                                 LUSTRE_ECHO_NAME, NULL);
+        if (rc != 0)
+                goto failed_1;
+# endif
+
+        rc = echo_client_init();
+
+# ifdef HAVE_SERVER_SUPPORT
+        if (rc == 0)
+                RETURN(0);
+
+        class_unregister_type(LUSTRE_ECHO_NAME);
+failed_1:
+        echo_persistent_pages_fini();
+failed_0:
+# endif
+        RETURN(rc);
+}
+
+static void /*__exit*/ obdecho_exit(void)
+{
+        echo_client_exit();
+
+# ifdef HAVE_SERVER_SUPPORT
+        class_unregister_type(LUSTRE_ECHO_NAME);
+        echo_persistent_pages_fini();
+# endif
+}
+
+MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
+MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
+MODULE_LICENSE("GPL");
+
+cfs_module(obdecho, LUSTRE_VERSION_STRING, obdecho_init, obdecho_exit);
+#endif /* __KERNEL__ */
+
 /** @} echo_client */