Whamcloud - gitweb
Merge b_md into HEAD
[fs/lustre-release.git] / lustre / obdecho / echo_client.c
index 763ddb8..6c4eb6d 100644 (file)
@@ -1,7 +1,7 @@
 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
  * vim:expandtab:shiftwidth=8:tabstop=8:
  *
- *  Copyright (c) 2001, 2002 Cluster File Systems, Inc.
+ *  Copyright (c) 2001-2003 Cluster File Systems, Inc.
  *
  *   This file is part of Lustre, http://www.lustre.org.
  *
@@ -22,6 +22,8 @@
 #include <linux/version.h>
 #include <linux/module.h>
 #include <linux/fs.h>
+#include <linux/iobuf.h>
+#include <asm/div64.h>
 
 #define DEBUG_SUBSYSTEM S_ECHO
 
 #include <linux/obd_echo.h>
 #include <linux/lustre_debug.h>
 #include <linux/lprocfs_status.h>
+#include <linux/lustre_lite.h>                  /* for LL_IOC_LOV_SETSTRIPE */
 
-static int echo_iocontrol(long cmd, struct lustre_handle *obdconn, int len,
-                          void *karg, void *uarg)
+#if 0
+static void
+echo_printk_object (char *msg, struct ec_object *eco)
+{
+        struct lov_stripe_md *lsm = eco->eco_lsm;
+        int                   i;
+
+        printk (KERN_INFO "%s: object %p: "LPX64", refs %d%s: "LPX64
+                "=%u!%u@%d\n", msg, eco, eco->eco_id, eco->eco_refcount,
+                eco->eco_deleted ? "(deleted) " : "",
+                lsm->lsm_object_id, lsm->lsm_stripe_size,
+                lsm->lsm_stripe_count, lsm->lsm_stripe_offset);
+
+        for (i = 0; i < lsm->lsm_stripe_count; i++)
+                printk (KERN_INFO "   [%2u]"LPX64"\n",
+                        lsm->lsm_oinfo[i].loi_ost_idx,
+                        lsm->lsm_oinfo[i].loi_id);
+}
+#endif
+
+static struct ec_object *
+echo_find_object_locked (struct obd_device *obd, obd_id id)
 {
-        struct obd_device *obd = class_conn2obd(obdconn);
         struct echo_client_obd *ec = &obd->u.echo_client;
-        struct obd_ioctl_data *data = karg;
-        int rw = OBD_BRW_READ, rc = 0;
-        struct lov_stripe_md *lsm = NULL;
+        struct ec_object       *eco = NULL;
+        struct list_head       *el;
+
+        list_for_each (el, &ec->ec_objects) {
+                eco = list_entry (el, struct ec_object, eco_obj_chain);
+
+                if (eco->eco_id == id)
+                        return (eco);
+        }
+        return (NULL);
+}
+
+static int
+echo_copyout_lsm (struct lov_stripe_md *lsm, void *ulsm, int ulsm_nob)
+{
+        int nob;
+
+        nob = offsetof (struct lov_stripe_md, lsm_oinfo[lsm->lsm_stripe_count]);
+        if (nob > ulsm_nob)
+                return (-EINVAL);
+
+        if (copy_to_user (ulsm, lsm, nob))
+                return (-EFAULT);
+
+        return (0);
+}
+
+static int
+echo_copyin_lsm (struct obd_device *obd, struct lov_stripe_md *lsm,
+                 void *ulsm, int ulsm_nob)
+{
+        struct echo_client_obd *ec = &obd->u.echo_client;
+        int                     nob;
+
+        if (ulsm_nob < sizeof (*lsm))
+                return (-EINVAL);
+
+        if (copy_from_user (lsm, ulsm, sizeof (*lsm)))
+                return (-EFAULT);
+
+        nob = lsm->lsm_stripe_count * sizeof (lsm->lsm_oinfo[0]);
+
+        if (ulsm_nob < nob ||
+            lsm->lsm_stripe_count > ec->ec_nstripes ||
+            lsm->lsm_magic != LOV_MAGIC ||
+            (lsm->lsm_stripe_offset != 0 &&
+             lsm->lsm_stripe_offset != 0xffffffff &&
+             lsm->lsm_stripe_offset >= ec->ec_nstripes) ||
+            (lsm->lsm_stripe_size & (PAGE_SIZE - 1)) != 0 ||
+            ((__u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count > ~0UL))
+                return (-EINVAL);
+
+        LASSERT (ec->ec_lsmsize >= sizeof (*lsm) + nob);
+
+        if (copy_from_user(lsm->lsm_oinfo,
+                           ((struct lov_stripe_md *)ulsm)->lsm_oinfo, nob))
+                return (-EFAULT);
+
+        return (0);
+}
+
+static struct ec_object *
+echo_allocate_object (struct obd_device *obd) 
+{
+        struct echo_client_obd *ec = &obd->u.echo_client;
+        struct ec_object       *eco;
+        
+        OBD_ALLOC (eco, sizeof (*eco));
+        if (eco == NULL)
+                return (NULL);
+
+        OBD_ALLOC (eco->eco_lsm, ec->ec_lsmsize);
+        if (eco->eco_lsm == NULL) {
+                OBD_FREE (eco, sizeof (*eco));
+                return (NULL);
+        }
+        
+        eco->eco_device = obd;
+        eco->eco_deleted = 0;
+        eco->eco_refcount = 0;
+        eco->eco_lsm->lsm_magic = LOV_MAGIC;
+        /* leave stripe count 0 by default */
+
+        return (eco);
+}
+
+static void
+echo_free_object (struct ec_object *eco) 
+{
+        struct obd_device      *obd = eco->eco_device;
+        struct echo_client_obd *ec = &obd->u.echo_client;
+
+        LASSERT (eco->eco_refcount == 0);
+        OBD_FREE (eco->eco_lsm, ec->ec_lsmsize);
+        OBD_FREE (eco, sizeof (*eco));
+}
+
+static int
+echo_create_object (struct obd_device *obd, int on_target, struct obdo *oa,
+                    void *ulsm, int ulsm_nob)
+{
+        struct echo_client_obd *ec = &obd->u.echo_client;
+        struct ec_object       *eco2;
+        struct ec_object       *eco;
+        struct lov_stripe_md   *lsm;
+        int                     rc;
+        int                     i;
+        
+        if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
+            (on_target ||                       /* set_stripe */
+             ec->ec_nstripes != 0)) {           /* LOV */
+                CERROR ("No valid oid\n");
+                return (-EINVAL);
+        }
+
+        eco = echo_allocate_object (obd);
+        if (eco == NULL)
+                return (-ENOMEM);
+        
+        lsm = eco->eco_lsm;
+
+        if (ulsm != NULL) {
+                rc = echo_copyin_lsm (obd, lsm, ulsm, ulsm_nob);
+                if (rc != 0)
+                        goto failed;
+        }
+        
+        /* setup object ID here for !on_target and LOV hint */
+        if ((oa->o_valid & OBD_MD_FLID) != 0)
+                eco->eco_id = lsm->lsm_object_id = oa->o_id;
+        
+        /* defaults -> actual values */
+        if (lsm->lsm_stripe_offset == 0xffffffff)
+                lsm->lsm_stripe_offset = 0;
+
+        if (lsm->lsm_stripe_count == 0)
+                lsm->lsm_stripe_count = ec->ec_nstripes;
+
+        if (lsm->lsm_stripe_size == 0)
+                lsm->lsm_stripe_size = PAGE_SIZE;
+
+        /* setup stripes: indices + default ids if required */
+        for (i = 0; i < lsm->lsm_stripe_count; i++) {
+                if (lsm->lsm_oinfo[i].loi_id == 0)
+                        lsm->lsm_oinfo[i].loi_id = lsm->lsm_object_id;
+
+                lsm->lsm_oinfo[i].loi_ost_idx =
+                        (lsm->lsm_stripe_offset + i) % ec->ec_nstripes;
+        }
+        
+        if (on_target) {
+                rc = obd_create (&ec->ec_conn, oa, &lsm, NULL);
+                if (rc != 0)
+                        goto failed;
+                
+                /* See what object ID we were given */
+                LASSERT ((oa->o_valid & OBD_MD_FLID) != 0);
+                eco->eco_id = lsm->lsm_object_id = oa->o_id;
+        }
+        
+        spin_lock (&ec->ec_lock);
+
+        eco2 = echo_find_object_locked (obd, oa->o_id);
+        if (eco2 != NULL) {                     /* conflict */
+                spin_unlock (&ec->ec_lock);
+                
+                CERROR ("Can't create object id "LPX64": id already exists%s\n", 
+                        oa->o_id, on_target ? " (undoing create)" : "");
+                
+                if (on_target)
+                        obd_destroy (&ec->ec_conn, oa, lsm, NULL);
+                
+                rc = -EEXIST;
+                goto failed;
+        }
+        
+        list_add (&eco->eco_obj_chain, &ec->ec_objects);
+        spin_unlock (&ec->ec_lock);
+        CDEBUG (D_INFO,
+                "created %p: "LPX64"=%u#%u&%d refs %d del %d\n",
+                eco, eco->eco_id,
+                eco->eco_lsm->lsm_stripe_size,
+                eco->eco_lsm->lsm_stripe_count,
+                eco->eco_lsm->lsm_stripe_offset,
+                eco->eco_refcount, eco->eco_deleted);
+        return (0);
+
+ failed:
+        echo_free_object (eco);
+        return (rc);
+}
+
+static int
+echo_get_object (struct ec_object **ecop, struct obd_device *obd, struct obdo *oa)
+{
+        struct echo_client_obd *ec = &obd->u.echo_client;
+        struct ec_object       *eco;
+        struct ec_object       *eco2;
+        int                     rc;
+
+        if ((oa->o_valid & OBD_MD_FLID) == 0) 
+        {
+                CERROR ("No valid oid\n");
+                return (-EINVAL);
+        }
+        
+        spin_lock (&ec->ec_lock);
+        eco = echo_find_object_locked (obd, oa->o_id);
+        if (eco != NULL) {
+                if (eco->eco_deleted)           /* being deleted */
+                        return (-EAGAIN);       /* (see comment in cleanup) */
+                
+                eco->eco_refcount++;
+                spin_unlock (&ec->ec_lock);
+                *ecop = eco;
+                CDEBUG (D_INFO,
+                        "found %p: "LPX64"=%u#%u&%d refs %d del %d\n",
+                        eco, eco->eco_id,
+                        eco->eco_lsm->lsm_stripe_size,
+                        eco->eco_lsm->lsm_stripe_count,
+                        eco->eco_lsm->lsm_stripe_offset,
+                        eco->eco_refcount, eco->eco_deleted);
+                return (0);
+        }
+        spin_unlock (&ec->ec_lock);
+
+        if (ec->ec_nstripes != 0)               /* striping required */
+                return (-ENOENT);
+
+        eco = echo_allocate_object (obd);
+        if (eco == NULL)
+                return (-ENOMEM);
+
+        eco->eco_id = eco->eco_lsm->lsm_object_id = oa->o_id;
+
+        spin_lock (&ec->ec_lock);
+
+        eco2 = echo_find_object_locked (obd, oa->o_id);
+        if (eco2 == NULL) {                     /* didn't race */
+                list_add (&eco->eco_obj_chain, &ec->ec_objects);
+                spin_unlock (&ec->ec_lock);
+                eco->eco_refcount = 1;
+                *ecop = eco;
+                CDEBUG (D_INFO,
+                        "created %p: "LPX64"=%u#%u&%d refs %d del %d\n",
+                        eco, eco->eco_id,
+                        eco->eco_lsm->lsm_stripe_size,
+                        eco->eco_lsm->lsm_stripe_count,
+                        eco->eco_lsm->lsm_stripe_offset,
+                        eco->eco_refcount, eco->eco_deleted);
+                return (0);
+        }
+
+        if (eco2->eco_deleted)
+                rc = -EAGAIN;                   /* lose race */
+        else {
+                eco2->eco_refcount++;           /* take existing */
+                *ecop = eco2;
+                rc = 0;
+                LASSERT (eco2->eco_id == eco2->eco_lsm->lsm_object_id);
+                CDEBUG (D_INFO,
+                        "found(2) %p: "LPX64"=%u#%u&%d refs %d del %d\n",
+                        eco2, eco2->eco_id,
+                        eco2->eco_lsm->lsm_stripe_size,
+                        eco2->eco_lsm->lsm_stripe_count,
+                        eco2->eco_lsm->lsm_stripe_offset,
+                        eco2->eco_refcount, eco2->eco_deleted);
+        }
+
+        spin_unlock (&ec->ec_lock);
+        
+        echo_free_object (eco);
+        return (rc);
+}
+
+static void
+echo_put_object (struct ec_object *eco)
+{
+        struct obd_device      *obd = eco->eco_device;
+        struct echo_client_obd *ec = &obd->u.echo_client;
+
+        /* Release caller's ref on the object.
+         * delete => mark for deletion when last ref goes
+         */
+
+        spin_lock (&ec->ec_lock);
+
+        eco->eco_refcount--;
+        LASSERT (eco->eco_refcount >= 0);
+
+        if (eco->eco_refcount != 0 ||
+            !eco->eco_deleted) {
+                spin_unlock (&ec->ec_lock);
+                return;
+        }
+
+        spin_unlock (&ec->ec_lock);
+
+        /* NB leave obj in the object list.  We must prevent anyone from
+         * attempting to enqueue on this object number until we can be
+         * sure there will be no more lock callbacks.
+         */
+        obd_cancel_unused (&ec->ec_conn, eco->eco_lsm, 0);
+        
+        /* now we can let it go */
+        spin_lock (&ec->ec_lock);
+        list_del (&eco->eco_obj_chain);
+        spin_unlock (&ec->ec_lock);
+        
+        LASSERT (eco->eco_refcount == 0);
+
+        echo_free_object (eco);
+}
+
+static void
+echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
+{
+        unsigned long stripe_count;
+        unsigned long stripe_size;
+        unsigned long width;
+        unsigned long woffset;
+        int           stripe_index;
+        obd_off       offset;
+
+        if (lsm->lsm_stripe_count <= 1)
+                return;
+
+        offset       = *offp;
+        stripe_size  = lsm->lsm_stripe_size;
+        stripe_count = lsm->lsm_stripe_count;
+
+        /* width = # bytes in all stripes */
+        width = stripe_size * stripe_count;
+
+        /* woffset = offset within a width; offset = whole number of widths */
+        woffset = do_div (offset, width);
+
+        stripe_index = woffset / stripe_size;
+
+        *idp = lsm->lsm_oinfo[stripe_index].loi_id;
+        *offp = offset * stripe_size + woffset % stripe_size;
+}
+
+static int
+echo_client_kbrw (struct obd_device *obd, int rw,
+                  struct obdo *oa, struct lov_stripe_md *lsm,
+                  obd_off offset, obd_size count)
+{
+        struct echo_client_obd *ec = &obd->u.echo_client;
+        struct obd_brw_set     *set;
+        obd_count               npages;
+        struct brw_page        *pga;
+        struct brw_page        *pgp;
+        obd_off                 off;
+        int                     i;
+        int                     rc;
+        int                     verify;
+        int                     gfp_mask;
+
+        /* oa_id  == 0    => speed test (no verification) else...
+         * oa & 1         => use HIGHMEM
+         */
+        verify = (oa->o_id != 0);
+        gfp_mask = ((oa->o_id & 1) == 0) ? GFP_KERNEL : GFP_HIGHUSER;
+
+        LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
+
+        if (count <= 0 ||
+            (count & (PAGE_SIZE - 1)) != 0 ||
+            (lsm != NULL &&
+             lsm->lsm_object_id != oa->o_id))
+                return (-EINVAL);
+
+        set = obd_brw_set_new();
+        if (set == NULL)
+                return (-ENOMEM);
+
+        /* XXX think again with misaligned I/O */
+        npages = count >> PAGE_SHIFT;
+
+        rc = -ENOMEM;
+        OBD_ALLOC(pga, npages * sizeof(*pga));
+        if (pga == NULL)
+                goto out_0;
+
+        for (i = 0, pgp = pga, off = offset;
+             i < npages;
+             i++, pgp++, off += PAGE_SIZE) {
+
+                LASSERT (pgp->pg == NULL);      /* for cleanup */
+
+                rc = -ENOMEM;
+                pgp->pg = alloc_pages (gfp_mask, 0);
+                if (pgp->pg == NULL)
+                        goto out_1;
+
+                pgp->count = PAGE_SIZE;
+                pgp->off = off;
+                pgp->flag = 0;
+
+                if (verify) {
+                        void *addr = kmap(pgp->pg);
+                        obd_off      stripe_off = off;
+                        obd_id       stripe_id = oa->o_id;
+
+                        if (rw == OBD_BRW_WRITE) {
+                                echo_get_stripe_off_id(lsm, &stripe_off,
+                                                       &stripe_id);
+                                page_debug_setup(addr, pgp->count,
+                                                 stripe_off, stripe_id);
+                        } else {
+                                page_debug_setup(addr, pgp->count,
+                                                 0xdeadbeef00c0ffee,
+                                                 0xdeadbeef00c0ffee);
+                        }
+                        kunmap(pgp->pg);
+                }
+        }
+
+        set->brw_callback = ll_brw_sync_wait;
+        rc = obd_brw(rw, &ec->ec_conn, lsm, npages, pga, set, NULL);
+        if (rc == 0)
+                rc = ll_brw_sync_wait(set, CB_PHASE_START);
+
+ out_1:
+        if (rc != 0)
+                verify = 0;
+
+        for (i = 0, pgp = pga; i < npages; i++, pgp++) {
+                if (pgp->pg == NULL)
+                        continue;
+
+                if (verify) {
+                        void    *addr = kmap(pgp->pg);
+                        obd_off  stripe_off = pgp->off;
+                        obd_id   stripe_id  = oa->o_id;
+                        int      vrc;
+
+                        echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
+                        vrc = page_debug_check("test_brw", addr, pgp->count,
+                                               stripe_off, stripe_id);
+                        if (vrc != 0 && rc == 0)
+                                rc = vrc;
+                        
+                        kunmap(pgp->pg);
+                }
+                __free_pages(pgp->pg, 0);
+        }
+        OBD_FREE(pga, npages * sizeof(*pga));
+ out_0:
+        obd_brw_set_free(set);
+        return (rc);
+}
+
+static int
+echo_client_ubrw (struct obd_device *obd, int rw,
+                  struct obdo *oa, struct lov_stripe_md *lsm,
+                  obd_off offset, obd_size count, char *buffer)
+{
+        struct echo_client_obd *ec = &obd->u.echo_client;
+        struct obd_brw_set     *set;
+        obd_count               npages;
+        struct brw_page        *pga;
+        struct brw_page        *pgp;
+        obd_off                 off;
+        struct kiobuf          *kiobuf;
+        int                     i;
+        int                     rc;
+
+        LASSERT (rw == OBD_BRW_WRITE ||
+                 rw == OBD_BRW_READ);
+
+        /* NB: for now, only whole pages, page aligned */
+
+        if (count <= 0 ||
+            ((long)buffer & (PAGE_SIZE - 1)) != 0 ||
+            (count & (PAGE_SIZE - 1)) != 0 ||
+            (lsm != NULL && lsm->lsm_object_id != oa->o_id))
+                return (-EINVAL);
+
+        set = obd_brw_set_new();
+        if (set == NULL)
+                return (-ENOMEM);
+
+        /* XXX think again with misaligned I/O */
+        npages = count >> PAGE_SHIFT;
+
+        rc = -ENOMEM;
+        OBD_ALLOC(pga, npages * sizeof(*pga));
+        if (pga == NULL)
+                goto out_0;
+
+        rc = alloc_kiovec (1, &kiobuf);
+        if (rc != 0)
+                goto out_1;
+
+        rc = map_user_kiobuf ((rw == OBD_BRW_READ) ? READ : WRITE,
+                              kiobuf, (unsigned long)buffer, count);
+        if (rc != 0)
+                goto out_2;
+
+        LASSERT (kiobuf->offset == 0);
+        LASSERT (kiobuf->nr_pages == npages);
+
+        for (i = 0, off = offset, pgp = pga;
+             i < npages;
+             i++, off += PAGE_SIZE, pgp++) {
+                pgp->off = off;
+                pgp->pg = kiobuf->maplist[i];
+                pgp->count = PAGE_SIZE;
+                pgp->flag = 0;
+        }
+
+        set->brw_callback = ll_brw_sync_wait;
+        rc = obd_brw(rw, &ec->ec_conn, lsm, npages, pga, set, NULL);
+
+        if (rc == 0)
+                rc = ll_brw_sync_wait(set, CB_PHASE_START);
+
+        //        if (rw == OBD_BRW_READ)
+        //                mark_dirty_kiobuf (kiobuf, count);
+
+        unmap_kiobuf (kiobuf);
+ out_2:
+        free_kiovec (1, &kiobuf);
+ out_1:
+        OBD_FREE(pga, npages * sizeof(*pga));
+ out_0:
+        obd_brw_set_free(set);
+        return (rc);
+}
+
+static int
+echo_open (struct obd_export *exp, struct obdo *oa)
+{
+        struct obd_device      *obd = exp->exp_obd;
+        struct echo_client_obd *ec = &obd->u.echo_client;
+        struct lustre_handle   *ufh = obdo_handle (oa);
+        struct ec_open_object  *ecoo;
+        struct ec_object       *eco;
+        int                     rc;
+        
+        rc = echo_get_object (&eco, obd, oa);
+        if (rc != 0)
+                return (rc);
+        
+        rc = -ENOMEM;
+        OBD_ALLOC (ecoo, sizeof (*ecoo));
+        if (ecoo == NULL)
+                goto failed_0;
+
+        rc = obd_open (&ec->ec_conn, oa, eco->eco_lsm, NULL);
+        if (rc != 0)
+                goto failed_1;
+        
+        memcpy (&ecoo->ecoo_oa, oa, sizeof (*oa));
+        ecoo->ecoo_object = eco;
+        /* ecoo takes ref from echo_get_object() above */
+
+        spin_lock (&ec->ec_lock);
+
+        list_add (&ecoo->ecoo_exp_chain,
+                  &exp->exp_ec_data.eced_open_head);
+        
+        ufh->addr = (__u64)((long) ecoo);
+        ufh->cookie = ecoo->ecoo_cookie = ec->ec_unique++;
+        
+        spin_unlock (&ec->ec_lock);
+        return (0);
+        
+ failed_1:
+        OBD_FREE (ecoo, sizeof (*ecoo));
+ failed_0:
+        echo_put_object (eco);
+        return (rc);
+}
+
+static int
+echo_close (struct obd_export *exp, struct obdo *oa)
+{
+        struct obd_device      *obd = exp->exp_obd;
+        struct echo_client_obd *ec = &obd->u.echo_client;
+        struct lustre_handle   *ufh = obdo_handle (oa);
+        struct ec_open_object  *ecoo = NULL;
+        int                     found = 0;
+        struct list_head       *el;
+        int                     rc;
+        
+        if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
+                return (-EINVAL);
+        
+        spin_lock (&ec->ec_lock);
+
+        list_for_each (el, &exp->exp_ec_data.eced_open_head) {
+                ecoo = list_entry (el, struct ec_open_object, ecoo_exp_chain);
+                if ((__u64)((long)ecoo) == ufh->addr) {
+                        found = (ecoo->ecoo_cookie == ufh->cookie);
+                        if (found)
+                                list_del (&ecoo->ecoo_exp_chain);
+                        break;
+                }
+        }
+
+        spin_unlock (&ec->ec_lock);
+        
+        if (!found)
+                return (-EINVAL);
+
+        rc = obd_close (&ec->ec_conn, &ecoo->ecoo_oa,
+                        ecoo->ecoo_object->eco_lsm, NULL);
+        
+        echo_put_object (ecoo->ecoo_object);
+        OBD_FREE (ecoo, sizeof (*ecoo));
+
+        return (rc);
+}
+
+static int
+echo_ldlm_callback (struct ldlm_lock *lock, struct ldlm_lock_desc *new,
+                    void *data, int flag)
+{
+        struct ec_object       *eco = (struct ec_object *)data;
+        struct echo_client_obd *ec = &(eco->eco_device->u.echo_client);
+        struct lustre_handle    lockh;
+        struct list_head       *el;
+        int                     found = 0;
+        int                     rc;
+
+        ldlm_lock2handle (lock, &lockh);
+
+        /* #ifdef this out if we're not feeling paranoid */
+        spin_lock (&ec->ec_lock);
+        list_for_each (el, &ec->ec_objects) {
+                found = (eco == list_entry (el, struct ec_object, eco_obj_chain));
+                if (found)
+                        break;
+        }
+        spin_unlock (&ec->ec_lock);
+        LASSERT (found);
+        
+        switch (flag) {
+        case LDLM_CB_BLOCKING:
+                CDEBUG (D_INFO, "blocking callback on "LPX64", handle "LPX64"."LPX64"\n", 
+                        eco->eco_id, lockh.addr, lockh.cookie);
+                rc = ldlm_cli_cancel (&lockh);
+                if (rc != ELDLM_OK)
+                        CERROR ("ldlm_cli_cancel failed: %d\n", rc);
+                break;
+
+        case LDLM_CB_CANCELING:
+                CDEBUG (D_INFO, "canceling callback on "LPX64", handle "LPX64"."LPX64"\n", 
+                        eco->eco_id, lockh.addr, lockh.cookie);
+                break;
+
+        default:
+                LBUG ();
+        }
+
+        return (0);
+}
+
+static int
+echo_enqueue (struct obd_export *exp, struct obdo *oa,
+              int mode, obd_off offset, obd_size nob)
+{
+        struct obd_device      *obd = exp->exp_obd;
+        struct echo_client_obd *ec = &obd->u.echo_client;
+        struct lustre_handle   *ulh = obdo_handle (oa);
+        struct ec_object       *eco;
+        struct ec_lock         *ecl;
+        int                     flags;
+        int                     rc;
+
+        if (!(mode == LCK_PR || mode == LCK_PW))
+                return (-EINVAL);
+
+        if ((offset & (PAGE_SIZE - 1)) != 0 ||
+            (nob & (PAGE_SIZE - 1)) != 0)
+                return (-EINVAL);
+
+        rc = echo_get_object (&eco, obd, oa);
+        if (rc != 0)
+                return (rc);
+
+        rc = -ENOMEM;
+        OBD_ALLOC (ecl, sizeof (*ecl));
+        if (ecl == NULL)
+                goto failed_0;
+
+        ecl->ecl_mode = mode;
+        ecl->ecl_object = eco;
+        ecl->ecl_extent.start = offset;
+        ecl->ecl_extent.end = (nob == 0) ? ((obd_off)-1) : (offset + nob - 1);
+        
+        flags = 0;
+        rc = obd_enqueue (&ec->ec_conn, eco->eco_lsm, NULL,
+                          LDLM_EXTENT, &ecl->ecl_extent, sizeof (ecl->ecl_extent),
+                          mode, &flags, echo_ldlm_callback, eco, sizeof (*eco),
+                          &ecl->ecl_handle);
+        if (rc != 0)
+                goto failed_1;
+
+        CDEBUG (D_INFO, "enqueue handle "LPX64"."LPX64"\n",
+                ecl->ecl_handle.addr, ecl->ecl_handle.cookie);
+
+        /* NB ecl takes object ref from echo_get_object() above */
+
+        spin_lock (&ec->ec_lock);
+
+        list_add (&ecl->ecl_exp_chain, &exp->exp_ec_data.eced_locks);
+
+        ulh->addr = (__u64)((long)ecl);
+        ulh->cookie = ecl->ecl_cookie = ec->ec_unique++;
+        
+        spin_unlock (&ec->ec_lock);
+
+        oa->o_valid |= OBD_MD_FLHANDLE;
+        return (0);
+
+ failed_1:
+        OBD_FREE (ecl, sizeof (*ecl));
+ failed_0:
+        echo_put_object (eco);
+        return (rc);
+}
+
+static int
+echo_cancel (struct obd_export *exp, struct obdo *oa)
+{
+        struct obd_device      *obd = exp->exp_obd;
+        struct echo_client_obd *ec = &obd->u.echo_client;
+        struct lustre_handle   *ulh = obdo_handle (oa);
+        struct ec_lock         *ecl = NULL;
+        int                     found = 0;
+        struct list_head       *el;
+        int                     rc;
+
+        if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
+                return (-EINVAL);
+        
+        spin_lock (&ec->ec_lock);
+        
+        list_for_each (el, &exp->exp_ec_data.eced_locks) {
+                ecl = list_entry (el, struct ec_lock, ecl_exp_chain);
+                
+                if ((__u64)((long)ecl) == ulh->addr) {
+                        found = (ecl->ecl_cookie == ulh->cookie);
+                        if (found)
+                                list_del (&ecl->ecl_exp_chain);
+                        break;
+                }
+        }
+        
+        spin_unlock (&ec->ec_lock);
+        
+        if (!found)
+                return (-ENOENT);
+        
+        rc = obd_cancel (&ec->ec_conn, 
+                         ecl->ecl_object->eco_lsm,
+                         ecl->ecl_mode,
+                         &ecl->ecl_handle);
+        
+        echo_put_object (ecl->ecl_object);
+        OBD_FREE (ecl, sizeof (*ecl));
+        
+        return (rc);
+}
+
+static int echo_iocontrol(unsigned int cmd, struct lustre_handle *obdconn,
+                          int len, void *karg, void *uarg)
+{
+        struct obd_export      *exp = class_conn2export (obdconn);
+        struct obd_device      *obd;
+        struct echo_client_obd *ec;
+        struct ec_object       *eco;
+        struct obd_ioctl_data  *data = karg;
+        int                     rw = OBD_BRW_READ;
+        int                     rc = 0;
         ENTRY;
 
-        if (obd == NULL) {
+        if (exp == NULL) {
                 CERROR("ioctl: No device\n");
                 GOTO(out, rc = -EINVAL);
         }
 
-        if (data->ioc_inllen1 == sizeof(*lsm)) {
-                lsm = (struct lov_stripe_md *)data->ioc_inlbuf1;
-        } else if (data->ioc_inllen1 != 0) {
-                CERROR("nonzero ioc_inllen1 != sizeof(struct lov_stripe_md)\n");
-                GOTO(out, rc = -EINVAL);
-        }
+        obd = exp->exp_obd;
+        ec = &obd->u.echo_client;
 
         switch (cmd) {
-        case OBD_IOC_CREATE: {
-                struct lov_stripe_md *tmp_lsm = NULL;
-                rc = obd_create(&ec->conn, &data->ioc_obdo1, &tmp_lsm);
-                if (lsm)
-                        memcpy(lsm, tmp_lsm, sizeof(*tmp_lsm));
+        case OBD_IOC_CREATE:                    /* may create echo object */
+                if (!capable (CAP_SYS_ADMIN))
+                        GOTO (out, rc = -EPERM);
+                
+                rc = echo_create_object (obd, 1, &data->ioc_obdo1,
+                                         data->ioc_pbuf1, data->ioc_plen1);
+                GOTO(out, rc);
 
+        case OBD_IOC_DESTROY:
+                if (!capable (CAP_SYS_ADMIN))
+                        GOTO (out, rc = -EPERM);
+       
+                rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
+                if (rc == 0) {
+                        rc = obd_destroy(&ec->ec_conn, &data->ioc_obdo1,
+                                         eco->eco_lsm, NULL);
+                        if (rc == 0)
+                                eco->eco_deleted = 1;
+                        echo_put_object(eco);
+                }
                 GOTO(out, rc);
-        }
 
         case OBD_IOC_GETATTR:
-                rc = obd_getattr(&ec->conn, &data->ioc_obdo1, lsm);
+                rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
+                if (rc == 0) {
+                        rc = obd_getattr(&ec->ec_conn, &data->ioc_obdo1,
+                                         eco->eco_lsm);
+                        echo_put_object(eco);
+                }
                 GOTO(out, rc);
 
         case OBD_IOC_SETATTR:
-                rc = obd_setattr(&ec->conn, &data->ioc_obdo1, lsm);
-                GOTO(out, rc);
-
-        case OBD_IOC_DESTROY:
-                rc = obd_destroy(&ec->conn, &data->ioc_obdo1, lsm);
+                if (!capable (CAP_SYS_ADMIN))
+                        GOTO (out, rc = -EPERM);
+       
+                rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
+                if (rc == 0) {
+                        rc = obd_setattr(&ec->ec_conn, &data->ioc_obdo1,
+                                         eco->eco_lsm, NULL);
+                        echo_put_object(eco);
+                }
                 GOTO(out, rc);
 
         case OBD_IOC_OPEN:
-                rc = obd_open(&ec->conn, &data->ioc_obdo1, lsm);
+                rc = echo_open (exp, &data->ioc_obdo1);
                 GOTO(out, rc);
 
         case OBD_IOC_CLOSE:
-                rc = obd_close(&ec->conn, &data->ioc_obdo1, lsm);
+                rc = echo_close (exp, &data->ioc_obdo1);
                 GOTO(out, rc);
 
         case OBD_IOC_BRW_WRITE:
+                if (!capable (CAP_SYS_ADMIN))
+                        GOTO (out, rc = -EPERM);
+       
                 rw = OBD_BRW_WRITE;
-        case OBD_IOC_BRW_READ: {
-                struct lov_stripe_md tmp_lsm;
-                struct obd_brw_set *set;
-                obd_count pages = 0;
-                struct brw_page *pga, *pgp;
-                __u64 off, id = data->ioc_obdo1.o_id;
-                int gfp_mask = (id & 1) ? GFP_HIGHUSER : GFP_KERNEL;
-                int j, verify = (id != 0);
-
-                if (lsm && lsm->lsm_object_id != id) {
-                        CERROR("LSM object ID ("LPU64") != id ("LPU64")\n",
-                               lsm->lsm_object_id, id);
-                        GOTO(out, rc = -EINVAL);
-                }
-
-                if (!lsm) {
-                        memset(&tmp_lsm, 0, sizeof(tmp_lsm));
-                        lsm = &tmp_lsm;
-                        lsm->lsm_object_id = id;
+                /* fall through */
+        case OBD_IOC_BRW_READ:
+                rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
+                if (rc == 0) {
+                        if (data->ioc_pbuf2 == NULL) // NULL user data pointer
+                                rc = echo_client_kbrw(obd, rw, &data->ioc_obdo1,
+                                                      eco->eco_lsm,
+                                                      data->ioc_offset,
+                                                      data->ioc_count);
+                        else
+                                rc = echo_client_ubrw(obd, rw, &data->ioc_obdo1,
+                                                      eco->eco_lsm,
+                                                      data->ioc_offset,
+                                                      data->ioc_count,
+                                                      data->ioc_pbuf2);
+                        echo_put_object(eco);
                 }
+                GOTO(out, rc);
 
-                if (data->ioc_count < 0) {
-                        CERROR("invalid buffer size: "LPD64"\n",
-                               data->ioc_count);
-                        GOTO(out, rc = -EINVAL);
+        case ECHO_IOC_GET_STRIPE:
+                rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
+                if (rc == 0) {
+                        rc = echo_copyout_lsm(eco->eco_lsm, data->ioc_pbuf1,
+                                              data->ioc_plen1);
+                        echo_put_object(eco);
                 }
+                GOTO(out, rc);
 
-                set = obd_brw_set_new();
-                if (set == NULL)
-                        GOTO(out, rc = -ENOMEM);
-
-                pages = data->ioc_count / PAGE_SIZE;
-                off = data->ioc_offset;
-
-                CDEBUG(D_INODE, "BRW %s with %d pages @ "LPX64"\n",
-                       rw == OBD_BRW_READ ? "read" : "write", pages, off);
-                OBD_ALLOC(pga, pages * sizeof(*pga));
-                if (!pga) {
-                        CERROR("no memory for %d BRW per-page data\n", pages);
-                        GOTO(brw_free, rc = -ENOMEM);
-                }
+        case ECHO_IOC_SET_STRIPE:
+                if (!capable (CAP_SYS_ADMIN))
+                        GOTO (out, rc = -EPERM);
 
-                for (j = 0, pgp = pga; j < pages; j++, off += PAGE_SIZE, pgp++){
-                        pgp->pg = alloc_pages(gfp_mask, 0);
-                        if (!pgp->pg) {
-                                CERROR("no memory for brw pages\n");
-                                GOTO(brw_cleanup, rc = -ENOMEM);
-                        }
-                        pgp->count = PAGE_SIZE;
-                        pgp->off = off;
-                        pgp->flag = 0;
-
-                        if (verify) {
-                                void *addr = kmap(pgp->pg);
-
-                                if (rw == OBD_BRW_WRITE)
-                                        page_debug_setup(addr, pgp->count,
-                                                         pgp->off, id);
-                                else
-                                        page_debug_setup(addr, pgp->count,
-                                                         0xdeadbeef00c0ffee,
-                                                         0xdeadbeef00c0ffee);
-                                kunmap(pgp->pg);
+                if (data->ioc_pbuf1 == NULL) {  /* unset */
+                        rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
+                        if (rc == 0) {
+                                eco->eco_deleted = 1;
+                                echo_put_object(eco);
                         }
+                } else {
+                        rc = echo_create_object(obd, 0, &data->ioc_obdo1,
+                                                data->ioc_pbuf1, data->ioc_plen1);
                 }
+                GOTO (out, rc);
+
+        case ECHO_IOC_ENQUEUE:
+                if (!capable (CAP_SYS_ADMIN))
+                        GOTO (out, rc = -EPERM);
+       
+                rc = echo_enqueue (exp, &data->ioc_obdo1, 
+                                   data->ioc_conn1, /* lock mode */
+                                   data->ioc_offset, data->ioc_count); /* extent */
+                GOTO (out, rc);
+
+        case ECHO_IOC_CANCEL:
+                rc = echo_cancel (exp, &data->ioc_obdo1);
+                GOTO (out, rc);
 
-                set->brw_callback = ll_brw_sync_wait;
-                rc = obd_brw(rw, &ec->conn, lsm, j, pga, set);
-                if (rc)
-                        CERROR("test_brw: error from obd_brw: rc = %d\n", rc);
-                else {
-                        rc = ll_brw_sync_wait(set, CB_PHASE_START);
-                        if (rc)
-                                CERROR("test_brw: error from callback: rc = "
-                                       "%d\n", rc);
-                }
-                EXIT;
-        brw_cleanup:
-                for (j = 0, pgp = pga; j < pages; j++, pgp++) {
-                        if (pgp->pg == NULL)
-                                continue;
-
-                        if (verify && !rc) {
-                                void *addr = kmap(pgp->pg);
-
-                                rc = page_debug_check("test_brw", addr,
-                                                       PAGE_SIZE, pgp->off, id);
-                                kunmap(pgp->pg);
-                        }
-                        __free_pages(pgp->pg, 0);
-                }
-        brw_free:
-                obd_brw_set_free(set);
-                OBD_FREE(pga, pages * sizeof(*pga));
-                GOTO(out, rc);
-        }
         default:
-                return -ENOTTY;
+                CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
+                GOTO (out, rc = -ENOTTY);
         }
 
  out:
@@ -194,6 +975,9 @@ static int echo_setup(struct obd_device *obddev, obd_count len, void *buf)
         struct obd_ioctl_data* data = buf;
         struct echo_client_obd *ec = &obddev->u.echo_client;
         struct obd_device *tgt;
+        struct obd_uuid uuid;
+        struct lov_stripe_md *lsm = NULL;
+        struct obd_uuid echo_uuid = { "ECHO_UUID" };
         int rc;
         ENTRY;
 
@@ -206,28 +990,42 @@ static int echo_setup(struct obd_device *obddev, obd_count len, void *buf)
                 RETURN(-EINVAL);
         }
 
-        MOD_INC_USE_COUNT;
-        tgt = class_uuid2obd(data->ioc_inlbuf1);
+        obd_str2uuid(&uuid, data->ioc_inlbuf1);
+        tgt = class_uuid2obd(&uuid);
         if (!tgt || !(tgt->obd_flags & OBD_ATTACHED) ||
             !(tgt->obd_flags & OBD_SET_UP)) {
                 CERROR("device not attached or not set up (%d)\n",
                        data->ioc_dev);
-                GOTO(error_dec, rc = -EINVAL);
+                RETURN(rc = -EINVAL);
         }
 
-        rc = obd_connect(&ec->conn, tgt, NULL, NULL, NULL);
+        spin_lock_init (&ec->ec_lock);
+        INIT_LIST_HEAD (&ec->ec_objects);
+        ec->ec_unique = 0;
+
+        rc = obd_connect(&ec->ec_conn, tgt, &echo_uuid, NULL, NULL);
         if (rc) {
                 CERROR("fail to connect to device %d\n", data->ioc_dev);
-                GOTO(error_dec, rc = -EINVAL);
+                return (rc);
         }
-        RETURN(rc);
-error_dec:
-        MOD_DEC_USE_COUNT;
+
+        ec->ec_lsmsize = obd_alloc_memmd (&ec->ec_conn, &lsm);
+        if (ec->ec_lsmsize < 0) {
+                CERROR ("Can't get # stripes: %d\n", rc);
+                obd_disconnect (&ec->ec_conn);
+                rc = ec->ec_lsmsize;
+        } else {
+                ec->ec_nstripes = lsm->lsm_stripe_count;
+                obd_free_memmd (&ec->ec_conn, &lsm);
+        }
+
         RETURN(rc);
 }
 
 static int echo_cleanup(struct obd_device * obddev)
 {
+        struct list_head       *el;
+        struct ec_object       *eco;
         struct echo_client_obd *ec = &obddev->u.echo_client;
         int rc;
         ENTRY;
@@ -237,36 +1035,107 @@ static int echo_cleanup(struct obd_device * obddev)
                 RETURN(-EBUSY);
         }
 
-        rc = obd_disconnect(&ec->conn);
-        if (rc) {
-                CERROR("fail to disconnect device: %d\n", rc);
-                RETURN(-EINVAL);
+        /* XXX assuming sole access */
+        while (!list_empty (&ec->ec_objects)) {
+                el = ec->ec_objects.next;
+                eco = list_entry (el, struct ec_object, eco_obj_chain);
+
+                LASSERT (eco->eco_refcount == 0);
+                eco->eco_refcount = 1;
+                eco->eco_deleted = 1;
+                echo_put_object (eco);
         }
 
-        MOD_DEC_USE_COUNT;
-        RETURN(0);
+        rc = obd_disconnect (&ec->ec_conn);
+        if (rc != 0)
+                CERROR("fail to disconnect device: %d\n", rc);
+
+        RETURN (rc);
 }
 
 static int echo_connect(struct lustre_handle *conn, struct obd_device *src,
-                        obd_uuid_t cluuid, struct recovd_obd *recovd,
+                        struct obd_uuid *cluuid, struct recovd_obd *recovd,
                         ptlrpc_recovery_cb_t recover)
 {
-        return class_connect(conn, src, cluuid);
+        struct obd_export *exp;
+        int                rc;
+
+        rc = class_connect(conn, src, cluuid);
+        if (rc == 0) {
+                exp = class_conn2export (conn);
+                INIT_LIST_HEAD (&exp->exp_ec_data.eced_open_head);
+                INIT_LIST_HEAD (&exp->exp_ec_data.eced_locks);
+        }
+
+        RETURN (rc);
+}
+
+static int echo_disconnect(struct lustre_handle *conn)
+{
+        struct obd_export      *exp = class_conn2export (conn);
+        struct obd_device      *obd;
+        struct echo_client_obd *ec;
+        struct ec_open_object  *ecoo;
+        struct ec_lock         *ecl;
+        int                     rc;
+
+        if (exp == NULL)
+                return (-EINVAL);
+
+        obd = exp->exp_obd;
+        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);
+                
+                rc = obd_cancel (&ec->ec_conn, ecl->ecl_object->eco_lsm,
+                                 ecl->ecl_mode, &ecl->ecl_handle);
+
+                CERROR ("Cancel lock on object "LPX64" on disconnect (%d)\n",
+                        ecl->ecl_object->eco_id, rc);
+                
+                echo_put_object (ecl->ecl_object);
+                OBD_FREE (ecl, sizeof (*ecl));
+        }
+
+        /* no more contention on export's open handle list  */
+        while (!list_empty (&exp->exp_ec_data.eced_open_head)) {
+                ecoo = list_entry (exp->exp_ec_data.eced_open_head.next,
+                                   struct ec_open_object, ecoo_exp_chain);
+                list_del (&ecoo->ecoo_exp_chain);
+
+                rc = obd_close (&ec->ec_conn, &ecoo->ecoo_oa,
+                                ecoo->ecoo_object->eco_lsm, NULL);
+
+                CDEBUG (D_INFO, "Closed object "LPX64" on disconnect (%d)\n",
+                        ecoo->ecoo_oa.o_id, rc);
+
+                echo_put_object (ecoo->ecoo_object);
+                OBD_FREE (ecoo, sizeof (*ecoo));
+        }
+
+        rc = class_disconnect (conn);
+        RETURN (rc);
 }
 
 static struct obd_ops echo_obd_ops = {
+        o_owner:       THIS_MODULE,
         o_setup:       echo_setup,
         o_cleanup:     echo_cleanup,
         o_iocontrol:   echo_iocontrol,
         o_connect:     echo_connect,
-        o_disconnect:  class_disconnect
+        o_disconnect:  echo_disconnect
 };
 
 int echo_client_init(void)
 {
-        extern struct lprocfs_vars status_class_var[];
+        struct lprocfs_static_vars lvars;
 
-        return class_register_type(&echo_obd_ops, status_class_var,
+        lprocfs_init_vars(&lvars);
+        return class_register_type(&echo_obd_ops, lvars.module_vars,
                                    OBD_ECHO_CLIENT_DEVICENAME);
 }