Whamcloud - gitweb
Branch: HEAD
[fs/lustre-release.git] / lustre / obdecho / echo_client.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2001-2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #define DEBUG_SUBSYSTEM S_ECHO
23 #ifdef __KERNEL__
24 #include <linux/version.h>
25 #include <linux/module.h>
26 #include <linux/fs.h>
27 #include <linux/completion.h>
28 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
29 #include <linux/iobuf.h>
30 #endif
31 #include <asm/div64.h>
32 #include <linux/smp_lock.h>
33 #else
34 #include <liblustre.h>
35 #endif
36
37 #include <linux/obd.h>
38 #include <linux/obd_support.h>
39 #include <linux/obd_class.h>
40 #include <linux/obd_echo.h>
41 #include <linux/lustre_debug.h>
42 #include <linux/lprocfs_status.h>
43
44 static obd_id last_object_id;
45
46 #if 0
47 static void
48 echo_printk_object (char *msg, struct ec_object *eco)
49 {
50         struct lov_stripe_md *lsm = eco->eco_lsm;
51         int                   i;
52
53         printk (KERN_INFO "Lustre: %s: object %p: "LPX64", refs %d%s: "LPX64
54                 "=%u!%u\n", msg, eco, eco->eco_id, eco->eco_refcount,
55                 eco->eco_deleted ? "(deleted) " : "",
56                 lsm->lsm_object_id, lsm->lsm_stripe_size,
57                 lsm->lsm_stripe_count);
58
59         for (i = 0; i < lsm->lsm_stripe_count; i++)
60                 printk (KERN_INFO "Lustre:   @%2u:"LPX64"\n",
61                         lsm->lsm_oinfo[i].loi_ost_idx,
62                         lsm->lsm_oinfo[i].loi_id);
63 }
64 #endif
65
66 static struct ec_object *
67 echo_find_object_locked (struct obd_device *obd, obd_id id)
68 {
69         struct echo_client_obd *ec = &obd->u.echocli;
70         struct ec_object       *eco = NULL;
71         struct list_head       *el;
72
73         list_for_each (el, &ec->ec_objects) {
74                 eco = list_entry (el, struct ec_object, eco_obj_chain);
75
76                 if (eco->eco_id == id)
77                         return (eco);
78         }
79         return (NULL);
80 }
81
82 static int
83 echo_copyout_lsm (struct lov_stripe_md *lsm, void *ulsm, int ulsm_nob)
84 {
85         int nob;
86
87         nob = offsetof (struct lov_stripe_md, lsm_oinfo[lsm->lsm_stripe_count]);
88         if (nob > ulsm_nob)
89                 return (-EINVAL);
90
91         if (copy_to_user (ulsm, lsm, nob))
92                 return (-EFAULT);
93
94         return (0);
95 }
96
97 static int
98 echo_copyin_lsm (struct obd_device *obd, struct lov_stripe_md *lsm,
99                  void *ulsm, int ulsm_nob)
100 {
101         struct echo_client_obd *ec = &obd->u.echocli;
102         int                     nob;
103
104         if (ulsm_nob < sizeof (*lsm))
105                 return (-EINVAL);
106
107         if (copy_from_user (lsm, ulsm, sizeof (*lsm)))
108                 return (-EFAULT);
109
110         nob = lsm->lsm_stripe_count * sizeof (lsm->lsm_oinfo[0]);
111
112         if (ulsm_nob < nob ||
113             lsm->lsm_stripe_count > ec->ec_nstripes ||
114             lsm->lsm_magic != LOV_MAGIC ||
115             (lsm->lsm_stripe_size & (PAGE_SIZE - 1)) != 0 ||
116             ((__u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count > ~0UL))
117                 return (-EINVAL);
118
119         if (copy_from_user(lsm->lsm_oinfo,
120                            ((struct lov_stripe_md *)ulsm)->lsm_oinfo, nob))
121                 return (-EFAULT);
122
123         return (0);
124 }
125
126 static struct ec_object *
127 echo_allocate_object (struct obd_device *obd)
128 {
129         struct echo_client_obd *ec = &obd->u.echocli;
130         struct ec_object       *eco;
131         int rc;
132
133         OBD_ALLOC(eco, sizeof (*eco));
134         if (eco == NULL)
135                 return NULL;
136
137         rc = obd_alloc_memmd(ec->ec_exp, &eco->eco_lsm);
138         if (rc < 0) {
139                 OBD_FREE(eco, sizeof (*eco));
140                 return NULL;
141         }
142
143         eco->eco_device = obd;
144         eco->eco_deleted = 0;
145         eco->eco_refcount = 0;
146         eco->eco_lsm->lsm_magic = LOV_MAGIC;
147         /* leave stripe count 0 by default */
148
149         return (eco);
150 }
151
152 static void
153 echo_free_object (struct ec_object *eco)
154 {
155         struct obd_device      *obd = eco->eco_device;
156         struct echo_client_obd *ec = &obd->u.echocli;
157
158         LASSERT (eco->eco_refcount == 0);
159         obd_free_memmd(ec->ec_exp, &eco->eco_lsm);
160         OBD_FREE (eco, sizeof (*eco));
161 }
162
163 static int echo_create_object(struct obd_device *obd, int on_target,
164                               struct obdo *oa, void *ulsm, int ulsm_nob,
165                               struct obd_trans_info *oti)
166 {
167         struct echo_client_obd *ec = &obd->u.echocli;
168         struct ec_object       *eco2;
169         struct ec_object       *eco;
170         struct lov_stripe_md   *lsm;
171         int                     rc;
172         int                     i, idx;
173
174         if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
175             (on_target ||                       /* set_stripe */
176              ec->ec_nstripes != 0)) {           /* LOV */
177                 CERROR ("No valid oid\n");
178                 return (-EINVAL);
179         }
180
181         if (ulsm != NULL) {
182                 eco = echo_allocate_object (obd);
183                 if (eco == NULL)
184                         return (-ENOMEM);
185
186                 lsm = eco->eco_lsm;
187
188                 rc = echo_copyin_lsm (obd, lsm, ulsm, ulsm_nob);
189                 if (rc != 0)
190                         goto failed;
191
192                 /* setup object ID here for !on_target and LOV hint */
193                 if ((oa->o_valid & OBD_MD_FLID) != 0)
194                         eco->eco_id = lsm->lsm_object_id = oa->o_id;
195
196                 if (lsm->lsm_stripe_count == 0)
197                         lsm->lsm_stripe_count = ec->ec_nstripes;
198
199                 if (lsm->lsm_stripe_size == 0)
200                         lsm->lsm_stripe_size = PAGE_SIZE;
201
202                 idx = ll_insecure_random_int();
203
204                 /* setup stripes: indices + default ids if required */
205                 for (i = 0; i < lsm->lsm_stripe_count; i++) {
206                         if (lsm->lsm_oinfo[i].loi_id == 0)
207                                 lsm->lsm_oinfo[i].loi_id = lsm->lsm_object_id;
208
209                         lsm->lsm_oinfo[i].loi_ost_idx =
210                                 (idx + i) % ec->ec_nstripes;
211                         lsm->lsm_oinfo[i].loi_ost_gen = 1;
212                 }
213         } else {
214                 OBD_ALLOC(eco, sizeof(*eco));
215                 eco->eco_device = obd;
216                 lsm = NULL;
217         }
218
219         if (oa->o_id == 0)
220                 oa->o_id = ++last_object_id;
221
222         if (on_target) {
223                 oa->o_gr = FILTER_GROUP_ECHO;
224                 oa->o_valid |= OBD_MD_FLGROUP;
225                 rc = obd_create(ec->ec_exp, oa, NULL, 0, &lsm, oti);
226                 if (rc != 0)
227                         goto failed;
228
229                 /* See what object ID we were given */
230                 eco->eco_id = oa->o_id = lsm->lsm_object_id;
231                 oa->o_valid |= OBD_MD_FLID;
232
233                 LASSERT(eco->eco_lsm == NULL || eco->eco_lsm == lsm);
234                 eco->eco_lsm = lsm;
235         }
236
237         spin_lock (&ec->ec_lock);
238
239         eco2 = echo_find_object_locked (obd, oa->o_id);
240         if (eco2 != NULL) {                     /* conflict */
241                 spin_unlock (&ec->ec_lock);
242
243                 CERROR ("Can't create object id "LPX64": id already exists%s\n",
244                         oa->o_id, on_target ? " (undoing create)" : "");
245
246                 if (on_target)
247                         obd_destroy(ec->ec_exp, oa, lsm, oti);
248
249                 rc = -EEXIST;
250                 goto failed;
251         }
252
253         list_add (&eco->eco_obj_chain, &ec->ec_objects);
254         spin_unlock (&ec->ec_lock);
255         CDEBUG (D_INFO,
256                 "created %p: "LPX64"=%u#%u@%u refs %d del %d\n",
257                 eco, eco->eco_id,
258                 eco->eco_lsm->lsm_stripe_size,
259                 eco->eco_lsm->lsm_stripe_count,
260                 eco->eco_lsm->lsm_oinfo[0].loi_ost_idx,
261                 eco->eco_refcount, eco->eco_deleted);
262         return (0);
263
264  failed:
265         echo_free_object (eco);
266         return (rc);
267 }
268
269 static int
270 echo_get_object (struct ec_object **ecop, struct obd_device *obd,
271                  struct obdo *oa)
272 {
273         struct echo_client_obd *ec = &obd->u.echocli;
274         struct ec_object       *eco;
275         struct ec_object       *eco2;
276         int                     rc;
277
278         if ((oa->o_valid & OBD_MD_FLID) == 0 ||
279             oa->o_id == 0)                      /* disallow use of object id 0 */
280         {
281                 CERROR ("No valid oid\n");
282                 return (-EINVAL);
283         }
284
285         spin_lock (&ec->ec_lock);
286         eco = echo_find_object_locked (obd, oa->o_id);
287         if (eco != NULL) {
288                 if (eco->eco_deleted)           /* being deleted */
289                         return (-EAGAIN);       /* (see comment in cleanup) */
290
291                 eco->eco_refcount++;
292                 spin_unlock (&ec->ec_lock);
293                 *ecop = eco;
294                 CDEBUG (D_INFO,
295                         "found %p: "LPX64"=%u#%u@%u refs %d del %d\n",
296                         eco, eco->eco_id,
297                         eco->eco_lsm->lsm_stripe_size,
298                         eco->eco_lsm->lsm_stripe_count,
299                         eco->eco_lsm->lsm_oinfo[0].loi_ost_idx,
300                         eco->eco_refcount, eco->eco_deleted);
301                 return (0);
302         }
303         spin_unlock (&ec->ec_lock);
304
305         if (ec->ec_nstripes != 0)               /* striping required */
306                 return (-ENOENT);
307
308         eco = echo_allocate_object (obd);
309         if (eco == NULL)
310                 return (-ENOMEM);
311
312         eco->eco_id = eco->eco_lsm->lsm_object_id = oa->o_id;
313
314         spin_lock (&ec->ec_lock);
315
316         eco2 = echo_find_object_locked (obd, oa->o_id);
317         if (eco2 == NULL) {                     /* didn't race */
318                 list_add (&eco->eco_obj_chain, &ec->ec_objects);
319                 spin_unlock (&ec->ec_lock);
320                 eco->eco_refcount = 1;
321                 *ecop = eco;
322                 CDEBUG (D_INFO,
323                         "created %p: "LPX64"=%u#%u@%d refs %d del %d\n",
324                         eco, eco->eco_id,
325                         eco->eco_lsm->lsm_stripe_size,
326                         eco->eco_lsm->lsm_stripe_count,
327                         eco->eco_lsm->lsm_oinfo[0].loi_ost_idx,
328                         eco->eco_refcount, eco->eco_deleted);
329                 return (0);
330         }
331
332         if (eco2->eco_deleted)
333                 rc = -EAGAIN;                   /* lose race */
334         else {
335                 eco2->eco_refcount++;           /* take existing */
336                 *ecop = eco2;
337                 rc = 0;
338                 LASSERT (eco2->eco_id == eco2->eco_lsm->lsm_object_id);
339                 CDEBUG (D_INFO,
340                         "found(2) %p: "LPX64"=%u#%u@%d refs %d del %d\n",
341                         eco2, eco2->eco_id,
342                         eco2->eco_lsm->lsm_stripe_size,
343                         eco2->eco_lsm->lsm_stripe_count,
344                         eco2->eco_lsm->lsm_oinfo[0].loi_ost_idx,
345                         eco2->eco_refcount, eco2->eco_deleted);
346         }
347
348         spin_unlock (&ec->ec_lock);
349
350         echo_free_object (eco);
351         return (rc);
352 }
353
354 static void
355 echo_put_object (struct ec_object *eco)
356 {
357         struct obd_device      *obd = eco->eco_device;
358         struct echo_client_obd *ec = &obd->u.echocli;
359
360         /* Release caller's ref on the object.
361          * delete => mark for deletion when last ref goes
362          */
363
364         spin_lock (&ec->ec_lock);
365
366         eco->eco_refcount--;
367         LASSERT (eco->eco_refcount >= 0);
368
369         CDEBUG(D_INFO, "put %p: "LPX64"=%u#%u@%d refs %d del %d\n",
370                eco, eco->eco_id,
371                eco->eco_lsm->lsm_stripe_size,
372                eco->eco_lsm->lsm_stripe_count,
373                eco->eco_lsm->lsm_oinfo[0].loi_ost_idx,
374                eco->eco_refcount, eco->eco_deleted);
375
376         if (eco->eco_refcount != 0 || !eco->eco_deleted) {
377                 spin_unlock (&ec->ec_lock);
378                 return;
379         }
380
381         spin_unlock (&ec->ec_lock);
382
383         /* NB leave obj in the object list.  We must prevent anyone from
384          * attempting to enqueue on this object number until we can be
385          * sure there will be no more lock callbacks.
386          */
387         obd_cancel_unused(ec->ec_exp, eco->eco_lsm, 0, NULL);
388
389         /* now we can let it go */
390         spin_lock (&ec->ec_lock);
391         list_del (&eco->eco_obj_chain);
392         spin_unlock (&ec->ec_lock);
393
394         LASSERT (eco->eco_refcount == 0);
395
396         echo_free_object (eco);
397 }
398
399 static void
400 echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
401 {
402         unsigned long stripe_count;
403         unsigned long stripe_size;
404         unsigned long width;
405         unsigned long woffset;
406         int           stripe_index;
407         obd_off       offset;
408
409         if (lsm->lsm_stripe_count <= 1)
410                 return;
411
412         offset       = *offp;
413         stripe_size  = lsm->lsm_stripe_size;
414         stripe_count = lsm->lsm_stripe_count;
415
416         /* width = # bytes in all stripes */
417         width = stripe_size * stripe_count;
418
419         /* woffset = offset within a width; offset = whole number of widths */
420         woffset = do_div (offset, width);
421
422         stripe_index = woffset / stripe_size;
423
424         *idp = lsm->lsm_oinfo[stripe_index].loi_id;
425         *offp = offset * stripe_size + woffset % stripe_size;
426 }
427
428 static void 
429 echo_client_page_debug_setup(struct lov_stripe_md *lsm, 
430                              struct page *page, int rw, obd_id id, 
431                              obd_off offset, obd_off count)
432 {
433         char    *addr;
434         obd_off  stripe_off;
435         obd_id   stripe_id;
436         int      delta;
437
438         /* no partial pages on the client */
439         LASSERT(count == PAGE_SIZE);
440
441         addr = kmap(page);
442
443         for (delta = 0; delta < PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
444                 if (rw == OBD_BRW_WRITE) {
445                         stripe_off = offset + delta;
446                         stripe_id = id;
447                         echo_get_stripe_off_id(lsm, &stripe_off, &stripe_id);
448                 } else {
449                         stripe_off = 0xdeadbeef00c0ffeeULL;
450                         stripe_id = 0xdeadbeef00c0ffeeULL;
451                 }
452                 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE, 
453                                   stripe_off, stripe_id);
454         }
455
456         kunmap(page);
457 }
458
459 static int
460 echo_client_page_debug_check(struct lov_stripe_md *lsm, 
461                              struct page *page, obd_id id, 
462                              obd_off offset, obd_off count)
463 {
464         obd_off stripe_off;
465         obd_id  stripe_id;
466         char   *addr;
467         int     delta;
468         int     rc;
469         int     rc2;
470
471         /* no partial pages on the client */
472         LASSERT(count == PAGE_SIZE);
473
474         addr = kmap(page);
475
476         for (rc = delta = 0; delta < PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
477                 stripe_off = offset + delta;
478                 stripe_id = id;
479                 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
480
481                 rc2 = block_debug_check("test_brw", 
482                                         addr + delta, OBD_ECHO_BLOCK_SIZE, 
483                                         stripe_off, stripe_id);
484                 if (rc2 != 0) {
485                         CERROR ("Error in echo object "LPX64"\n", id);
486                         rc = rc2;
487                 }
488         }
489
490         kunmap(page);
491         return rc;
492 }
493
494 static int echo_client_kbrw(struct obd_device *obd, int rw, struct obdo *oa,
495                             struct lov_stripe_md *lsm, obd_off offset,
496                             obd_size count, struct obd_trans_info *oti)
497 {
498         struct echo_client_obd *ec = &obd->u.echocli;
499         obd_count               npages;
500         struct brw_page        *pga;
501         struct brw_page        *pgp;
502         obd_off                 off;
503         int                     i;
504         int                     rc;
505         int                     verify = 0;
506         int                     gfp_mask;
507
508         verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
509                   (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
510                   (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
511
512         gfp_mask = ((oa->o_id & 2) == 0) ? GFP_KERNEL : GFP_HIGHUSER;
513
514         LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
515
516         if (count <= 0 ||
517             (count & (PAGE_SIZE - 1)) != 0 ||
518             (lsm != NULL &&
519              lsm->lsm_object_id != oa->o_id))
520                 return (-EINVAL);
521
522         /* XXX think again with misaligned I/O */
523         npages = count >> PAGE_SHIFT;
524
525         OBD_ALLOC(pga, npages * sizeof(*pga));
526         if (pga == NULL)
527                 return (-ENOMEM);
528
529         for (i = 0, pgp = pga, off = offset;
530              i < npages;
531              i++, pgp++, off += PAGE_SIZE) {
532
533                 LASSERT (pgp->pg == NULL);      /* for cleanup */
534
535                 rc = -ENOMEM;
536                 pgp->pg = alloc_pages (gfp_mask, 0);
537                 if (pgp->pg == NULL)
538                         goto out;
539
540                 pgp->count = PAGE_SIZE;
541                 pgp->disk_offset = pgp->page_offset = off;
542                 pgp->flag = 0;
543
544                 if (verify)
545                         echo_client_page_debug_setup(lsm, pgp->pg, rw, 
546                                                      oa->o_id, off, pgp->count);
547         }
548
549         rc = obd_brw(rw, ec->ec_exp, oa, lsm, npages, pga, oti);
550
551  out:
552         if (rc != 0 || rw != OBD_BRW_READ)
553                 verify = 0;
554
555         for (i = 0, pgp = pga; i < npages; i++, pgp++) {
556                 if (pgp->pg == NULL)
557                         continue;
558
559                 if (verify) {
560                         int vrc;
561                         vrc = echo_client_page_debug_check(lsm, pgp->pg, oa->o_id,
562                                                            pgp->page_offset, 
563                                                            pgp->count);
564                         if (vrc != 0 && rc == 0)
565                                 rc = vrc;
566                 }
567                 __free_pages(pgp->pg, 0);
568         }
569         OBD_FREE(pga, npages * sizeof(*pga));
570         return (rc);
571 }
572
573 #ifdef __KERNEL__
574 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
575 static int echo_client_ubrw(struct obd_device *obd, int rw,
576                             struct obdo *oa, struct lov_stripe_md *lsm,
577                             obd_off offset, obd_size count, char *buffer,
578                             struct obd_trans_info *oti)
579 {
580         struct echo_client_obd *ec = &obd->u.echocli;
581         obd_count               npages;
582         struct brw_page        *pga;
583         struct brw_page        *pgp;
584         obd_off                 off;
585         struct kiobuf          *kiobuf;
586         int                     i;
587         int                     rc;
588
589         LASSERT (rw == OBD_BRW_WRITE ||
590                  rw == OBD_BRW_READ);
591
592         /* NB: for now, only whole pages, page aligned */
593
594         if (count <= 0 ||
595             ((long)buffer & (PAGE_SIZE - 1)) != 0 ||
596             (count & (PAGE_SIZE - 1)) != 0 ||
597             (lsm != NULL && lsm->lsm_object_id != oa->o_id))
598                 return (-EINVAL);
599
600         /* XXX think again with misaligned I/O */
601         npages = count >> PAGE_SHIFT;
602
603         OBD_ALLOC(pga, npages * sizeof(*pga));
604         if (pga == NULL)
605                 return (-ENOMEM);
606
607         rc = alloc_kiovec (1, &kiobuf);
608         if (rc != 0)
609                 goto out_1;
610
611         rc = map_user_kiobuf ((rw == OBD_BRW_READ) ? READ : WRITE,
612                               kiobuf, (unsigned long)buffer, count);
613         if (rc != 0)
614                 goto out_2;
615
616         LASSERT (kiobuf->offset == 0);
617         LASSERT (kiobuf->nr_pages == npages);
618
619         for (i = 0, off = offset, pgp = pga;
620              i < npages;
621              i++, off += PAGE_SIZE, pgp++) {
622                 pgp->disk_offset = pgp->page_offset = off;
623                 pgp->pg = kiobuf->maplist[i];
624                 pgp->count = PAGE_SIZE;
625                 pgp->flag = 0;
626         }
627
628         rc = obd_brw(rw, ec->ec_exp, oa, lsm, npages, pga, oti);
629
630         //        if (rw == OBD_BRW_READ)
631         //                mark_dirty_kiobuf (kiobuf, count);
632
633         unmap_kiobuf (kiobuf);
634  out_2:
635         free_kiovec (1, &kiobuf);
636  out_1:
637         OBD_FREE(pga, npages * sizeof(*pga));
638         return (rc);
639 }
640 #else
641 static int echo_client_ubrw(struct obd_device *obd, int rw,
642                             struct obdo *oa, struct lov_stripe_md *lsm,
643                             obd_off offset, obd_size count, char *buffer,
644                             struct obd_trans_info *oti)
645 {
646 #warning "echo_client_ubrw() needs to be ported on 2.6 yet"
647         LBUG();
648         return 0;
649 }
650 #endif
651 #endif
652
653 struct echo_async_state;
654
655 #define EAP_MAGIC 79277927
656 struct echo_async_page {
657         int                     eap_magic;
658         struct page             *eap_page;
659         void                    *eap_cookie;
660         obd_off                 eap_off;
661         struct echo_async_state *eap_eas;
662         struct list_head        eap_item;
663 };
664
665 #define EAP_FROM_COOKIE(c)                                                      \
666         (LASSERT(((struct echo_async_page *)(c))->eap_magic == EAP_MAGIC),      \
667          (struct echo_async_page *)(c))
668
669 struct echo_async_state {
670         spinlock_t              eas_lock;
671         obd_off                 eas_next_offset;
672         obd_off                 eas_end_offset;
673         int                     eas_in_flight;
674         int                     eas_rc;
675         wait_queue_head_t       eas_waitq;
676         struct list_head        eas_avail;
677         struct obdo             eas_oa;
678         struct lov_stripe_md    *eas_lsm;
679 };
680
681 static int eas_should_wake(struct echo_async_state *eas)
682 {
683         unsigned long flags;
684         int rc = 0;
685         spin_lock_irqsave(&eas->eas_lock, flags);
686         if (eas->eas_rc == 0 && !list_empty(&eas->eas_avail))
687             rc = 1;
688         spin_unlock_irqrestore(&eas->eas_lock, flags);
689         return rc;
690 };
691
692 static int ec_ap_make_ready(void *data, int cmd)
693 {
694         /* our pages are issued ready */
695         LBUG();
696         return 0;
697 }
698 static int ec_ap_refresh_count(void *data, int cmd)
699 {
700         /* our pages are issued with a stable count */
701         LBUG();
702         return PAGE_SIZE;
703 }
704 static void ec_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
705 {
706         struct echo_async_page *eap = EAP_FROM_COOKIE(data);
707
708         memcpy(oa, &eap->eap_eas->eas_oa, sizeof(*oa));
709 }
710
711 static void ec_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
712 {
713         struct echo_async_page *eap = EAP_FROM_COOKIE(data);
714         struct echo_async_state *eas;
715         unsigned long flags;
716
717         eas = eap->eap_eas;
718
719         if (cmd == OBD_BRW_READ &&
720             eas->eas_oa.o_id != ECHO_PERSISTENT_OBJID &&
721             (eas->eas_oa.o_valid & OBD_MD_FLFLAGS) != 0 &&
722             (eas->eas_oa.o_flags & OBD_FL_DEBUG_CHECK) != 0)
723                 echo_client_page_debug_check(eas->eas_lsm, eap->eap_page, 
724                                              eas->eas_oa.o_id, eap->eap_off,
725                                              PAGE_SIZE);
726
727         spin_lock_irqsave(&eas->eas_lock, flags);
728         if (rc && !eas->eas_rc)
729                 eas->eas_rc = rc;
730         eas->eas_in_flight--;
731         list_add(&eap->eap_item, &eas->eas_avail);
732         wake_up(&eas->eas_waitq);
733         spin_unlock_irqrestore(&eas->eas_lock, flags);
734 }
735
736 static struct obd_async_page_ops ec_async_page_ops = {
737         .ap_make_ready =        ec_ap_make_ready,
738         .ap_refresh_count =     ec_ap_refresh_count,
739         .ap_fill_obdo =         ec_ap_fill_obdo,
740         .ap_completion =        ec_ap_completion,
741 };
742
743 static int echo_client_async_page(struct obd_export *exp, int rw,
744                                    struct obdo *oa, struct lov_stripe_md *lsm,
745                                    obd_off offset, obd_size count,
746                                    obd_size batching)
747 {
748         obd_count npages, i;
749         struct echo_async_page *eap;
750         struct echo_async_state eas;
751         struct list_head *pos, *n;
752         int rc = 0;
753         unsigned long flags;
754         LIST_HEAD(pages);
755 #if 0
756         int                     verify;
757         int                     gfp_mask;
758         /* oa_id  == 0    => speed test (no verification) else...
759          * oa & 1         => use HIGHMEM
760          */
761         verify = (oa->o_id != 0);
762         gfp_mask = ((oa->o_id & 1) == 0) ? GFP_KERNEL : GFP_HIGHUSER;
763 #endif
764
765         LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
766
767         if (count <= 0 ||
768             (count & (PAGE_SIZE - 1)) != 0 ||
769             (lsm != NULL &&
770              lsm->lsm_object_id != oa->o_id))
771                 return (-EINVAL);
772
773         /* XXX think again with misaligned I/O */
774         npages = batching >> PAGE_SHIFT;
775
776         memcpy(&eas.eas_oa, oa, sizeof(*oa));
777         eas.eas_next_offset = offset;
778         eas.eas_end_offset = offset + count;
779         spin_lock_init(&eas.eas_lock);
780         init_waitqueue_head(&eas.eas_waitq);
781         eas.eas_in_flight = 0;
782         eas.eas_rc = 0;
783         eas.eas_lsm = lsm;
784         INIT_LIST_HEAD(&eas.eas_avail);
785
786         /* prepare the group of pages that we're going to be keeping
787          * in flight */
788         for (i = 0; i < npages; i++) {
789                 struct page *page = alloc_page(GFP_KERNEL);
790                 if (page == NULL)
791                         GOTO(out, rc = -ENOMEM);
792
793                 page->private = 0;
794                 list_add_tail(&PAGE_LIST(page), &pages);
795
796                 OBD_ALLOC(eap, sizeof(*eap));
797                 if (eap == NULL)
798                         GOTO(out, rc = -ENOMEM);
799
800                 eap->eap_magic = EAP_MAGIC;
801                 eap->eap_page = page;
802                 eap->eap_eas = &eas;
803                 page->private = (unsigned long)eap;
804                 list_add_tail(&eap->eap_item, &eas.eas_avail);
805         }
806
807         /* first we spin queueing io and being woken by its completion */
808         spin_lock_irqsave(&eas.eas_lock, flags);
809         for(;;) {
810                 int rc;
811
812                 /* sleep until we have a page to send */
813                 spin_unlock_irqrestore(&eas.eas_lock, flags);
814                 rc = wait_event_interruptible(eas.eas_waitq, 
815                                               eas_should_wake(&eas));
816                 spin_lock_irqsave(&eas.eas_lock, flags);
817                 if (rc && !eas.eas_rc)
818                         eas.eas_rc = rc;
819                 if (eas.eas_rc)
820                         break;
821                 if (list_empty(&eas.eas_avail))
822                         continue;
823                 eap = list_entry(eas.eas_avail.next, struct echo_async_page,
824                                  eap_item);
825                 list_del(&eap->eap_item);
826                 spin_unlock_irqrestore(&eas.eas_lock, flags);
827
828                 /* unbind the eap from its old page offset */
829                 if (eap->eap_cookie != NULL) {
830                         obd_teardown_async_page(exp, lsm, NULL, 
831                                                 eap->eap_cookie);
832                         eap->eap_cookie = NULL;
833                 }
834
835                 eas.eas_next_offset += PAGE_SIZE;
836                 eap->eap_off = eas.eas_next_offset;
837
838                 rc = obd_prep_async_page(exp, lsm, NULL, eap->eap_page,
839                                          eap->eap_off, &ec_async_page_ops,
840                                          eap, &eap->eap_cookie);
841                 if (rc) {
842                         spin_lock_irqsave(&eas.eas_lock, flags);
843                         eas.eas_rc = rc;
844                         break;
845                 }
846
847                 if (oa->o_id != ECHO_PERSISTENT_OBJID &&
848                     (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
849                     (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0)
850                         echo_client_page_debug_setup(lsm, eap->eap_page, rw, 
851                                                      oa->o_id, 
852                                                      eap->eap_off, PAGE_SIZE);
853
854                 /* always asserts urgent, which isn't quite right */
855                 rc = obd_queue_async_io(exp, lsm, NULL, eap->eap_cookie,
856                                         rw, 0, PAGE_SIZE, 0,
857                                         ASYNC_READY | ASYNC_URGENT |
858                                         ASYNC_COUNT_STABLE);
859                 spin_lock_irqsave(&eas.eas_lock, flags);
860                 if (rc && !eas.eas_rc) {
861                         eas.eas_rc = rc;
862                         break;
863                 }
864                 eas.eas_in_flight++;
865                 if (eas.eas_next_offset == eas.eas_end_offset)
866                         break;
867         } 
868
869         /* still hold the eas_lock here.. */
870
871         /* now we just spin waiting for all the rpcs to complete */
872         while(eas.eas_in_flight) {
873                 spin_unlock_irqrestore(&eas.eas_lock, flags);
874                 wait_event_interruptible(eas.eas_waitq, 
875                                          eas.eas_in_flight == 0);
876                 spin_lock_irqsave(&eas.eas_lock, flags);
877         }
878         spin_unlock_irqrestore(&eas.eas_lock, flags);
879
880 out:
881         list_for_each_safe(pos, n, &pages) {
882                 struct page *page = list_entry(pos, struct page, 
883                                                PAGE_LIST_ENTRY);
884
885                 list_del(&PAGE_LIST(page));
886                 if (page->private != 0) {
887                         eap = (struct echo_async_page *)page->private;
888                         if (eap->eap_cookie != NULL)
889                                 obd_teardown_async_page(exp, lsm, NULL, 
890                                                         eap->eap_cookie);
891                         OBD_FREE(eap, sizeof(*eap));
892                 }
893                 __free_page(page);
894         }
895
896         RETURN(rc);
897 }
898
899 static int echo_client_prep_commit(struct obd_export *exp, int rw,
900                                    struct obdo *oa, struct lov_stripe_md *lsm,
901                                    obd_off offset, obd_size count,  
902                                    obd_size batch, struct obd_trans_info *oti)
903 {
904         struct obd_ioobj ioo;
905         struct niobuf_local *lnb;
906         struct niobuf_remote *rnb;
907         obd_off off;
908         obd_size npages, tot_pages;
909         int i, ret = 0;
910         ENTRY;
911
912         if (count <= 0 || (count & (PAGE_SIZE - 1)) != 0 ||
913             (lsm != NULL && lsm->lsm_object_id != oa->o_id))
914                 RETURN(-EINVAL);
915
916         npages = batch >> PAGE_SHIFT;
917         tot_pages = count >> PAGE_SHIFT;
918
919         OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
920         OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
921
922         if (lnb == NULL || rnb == NULL)
923                 GOTO(out, ret = -ENOMEM);
924
925         obdo_to_ioobj(oa, &ioo);
926
927         off = offset;
928
929         for(; tot_pages; tot_pages -= npages) {
930                 if (tot_pages < npages)
931                         npages = tot_pages;
932
933                 for (i = 0; i < npages; i++, off += PAGE_SIZE) {
934                         rnb[i].offset = off;
935                         rnb[i].len = PAGE_SIZE;
936                 }
937
938                 /* XXX this can't be the best.. */
939                 memset(oti, 0, sizeof(*oti));
940                 ioo.ioo_bufcnt = npages;
941
942                 ret = obd_preprw(rw, exp, oa, 1, &ioo, npages, rnb, lnb,
943                                  oti, NULL);
944                 if (ret != 0)
945                         GOTO(out, ret);
946
947                 for (i = 0; i < npages; i++) {
948                         struct page *page = lnb[i].page;
949
950                         /* read past eof? */
951                         if (page == NULL && lnb[i].rc == 0)
952                                 continue;
953
954                         if (oa->o_id == ECHO_PERSISTENT_OBJID ||
955                             (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
956                             (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
957                                 continue;
958
959
960                         if (rw == OBD_BRW_WRITE)
961                                 echo_client_page_debug_setup(lsm, page, rw,
962                                                              oa->o_id,
963                                                              rnb[i].offset,
964                                                              rnb[i].len);
965                         else
966                                 echo_client_page_debug_check(lsm, page,
967                                                              oa->o_id,
968                                                              rnb[i].offset,
969                                                              rnb[i].len);
970                 }
971
972                 ret = obd_commitrw(rw, exp, oa, 1, &ioo, npages, lnb, oti, ret);
973                 if (ret != 0)
974                         GOTO(out, ret);
975         }
976
977 out:
978         if (lnb)
979                 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
980         if (rnb)
981                 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
982         RETURN(ret);
983 }
984
985 int echo_client_brw_ioctl(int rw, struct obd_export *exp,
986                           struct obd_ioctl_data *data)
987 {
988         struct obd_device *obd = class_exp2obd(exp);
989         struct echo_client_obd *ec = &obd->u.echocli;
990         struct obd_trans_info dummy_oti;
991         struct ec_object *eco;
992         int rc;
993         ENTRY;
994
995         rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
996         if (rc)
997                 RETURN(rc);
998
999         memset(&dummy_oti, 0, sizeof(dummy_oti));
1000
1001         data->ioc_obdo1.o_valid &= ~OBD_MD_FLHANDLE;
1002         data->ioc_obdo1.o_valid |= OBD_MD_FLGROUP;
1003         data->ioc_obdo1.o_gr = FILTER_GROUP_ECHO;
1004
1005         switch((long)data->ioc_pbuf1) {
1006         case 1:
1007                 if (data->ioc_pbuf2 == NULL) { // NULL user data pointer
1008                         rc = echo_client_kbrw(obd, rw, &data->ioc_obdo1,
1009                                               eco->eco_lsm, data->ioc_offset,
1010                                               data->ioc_count, &dummy_oti);
1011                 } else {
1012 #ifdef __KERNEL__
1013                         rc = echo_client_ubrw(obd, rw, &data->ioc_obdo1,
1014                                               eco->eco_lsm, data->ioc_offset,
1015                                               data->ioc_count, data->ioc_pbuf2,
1016                                               &dummy_oti);
1017 #endif
1018                 }
1019                 break;
1020         case 2:
1021                 rc = echo_client_async_page(ec->ec_exp, rw, &data->ioc_obdo1,
1022                                            eco->eco_lsm, data->ioc_offset,
1023                                            data->ioc_count, data->ioc_plen1);
1024                 break;
1025         case 3:
1026                 rc = echo_client_prep_commit(ec->ec_exp, rw, &data->ioc_obdo1,
1027                                             eco->eco_lsm, data->ioc_offset,
1028                                             data->ioc_count, data->ioc_plen1,
1029                                             &dummy_oti);
1030                 break;
1031         default:
1032                 rc = -EINVAL;
1033         }
1034         echo_put_object(eco);
1035         RETURN(rc);
1036 }
1037
1038 static int
1039 echo_ldlm_callback (struct ldlm_lock *lock, struct ldlm_lock_desc *new,
1040                     void *data, int flag)
1041 {
1042         struct ec_object       *eco = (struct ec_object *)data;
1043         struct echo_client_obd *ec = &(eco->eco_device->u.echocli);
1044         struct lustre_handle    lockh;
1045         struct list_head       *el;
1046         int                     found = 0;
1047         int                     rc;
1048
1049         ldlm_lock2handle (lock, &lockh);
1050
1051         /* #ifdef this out if we're not feeling paranoid */
1052         spin_lock (&ec->ec_lock);
1053         list_for_each (el, &ec->ec_objects) {
1054                 found = (eco == list_entry(el, struct ec_object,
1055                                            eco_obj_chain));
1056                 if (found)
1057                         break;
1058         }
1059         spin_unlock (&ec->ec_lock);
1060         LASSERT (found);
1061
1062         switch (flag) {
1063         case LDLM_CB_BLOCKING:
1064                 CDEBUG(D_INFO, "blocking callback on "LPX64", handle "LPX64"\n",
1065                        eco->eco_id, lockh.cookie);
1066                 rc = ldlm_cli_cancel (&lockh);
1067                 if (rc != ELDLM_OK)
1068                         CERROR ("ldlm_cli_cancel failed: %d\n", rc);
1069                 break;
1070
1071         case LDLM_CB_CANCELING:
1072                 CDEBUG(D_INFO, "cancel callback on "LPX64", handle "LPX64"\n",
1073                        eco->eco_id, lockh.cookie);
1074                 break;
1075
1076         default:
1077                 LBUG ();
1078         }
1079
1080         return (0);
1081 }
1082
1083 static int
1084 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
1085                     int mode, obd_off offset, obd_size nob)
1086 {
1087         struct obd_device      *obd = exp->exp_obd;
1088         struct echo_client_obd *ec = &obd->u.echocli;
1089         struct lustre_handle   *ulh = obdo_handle (oa);
1090         struct ec_object       *eco;
1091         struct ec_lock         *ecl;
1092         int                     flags;
1093         int                     rc;
1094
1095         if (!(mode == LCK_PR || mode == LCK_PW))
1096                 return -EINVAL;
1097
1098         if ((offset & (PAGE_SIZE - 1)) != 0 ||
1099             (nob & (PAGE_SIZE - 1)) != 0)
1100                 return -EINVAL;
1101
1102         rc = echo_get_object (&eco, obd, oa);
1103         if (rc != 0)
1104                 return rc;
1105
1106         rc = -ENOMEM;
1107         OBD_ALLOC (ecl, sizeof (*ecl));
1108         if (ecl == NULL)
1109                 goto failed_0;
1110
1111         ecl->ecl_mode = mode;
1112         ecl->ecl_object = eco;
1113         ecl->ecl_policy.l_extent.start = offset;
1114         ecl->ecl_policy.l_extent.end =
1115                 (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
1116
1117         flags = 0;
1118         rc = obd_enqueue(ec->ec_exp, eco->eco_lsm, LDLM_EXTENT,
1119                          &ecl->ecl_policy, mode, &flags, echo_ldlm_callback,
1120                          ldlm_completion_ast, NULL, eco, sizeof(struct ost_lvb),
1121                          lustre_swab_ost_lvb, &ecl->ecl_lock_handle);
1122         if (rc != 0)
1123                 goto failed_1;
1124
1125         CDEBUG(D_INFO, "enqueue handle "LPX64"\n", ecl->ecl_lock_handle.cookie);
1126
1127         /* NB ecl takes object ref from echo_get_object() above */
1128         spin_lock(&ec->ec_lock);
1129
1130         list_add(&ecl->ecl_exp_chain, &exp->exp_ec_data.eced_locks);
1131         ulh->cookie = ecl->ecl_cookie = ec->ec_unique++;
1132
1133         spin_unlock(&ec->ec_lock);
1134
1135         oa->o_valid |= OBD_MD_FLHANDLE;
1136         return 0;
1137
1138  failed_1:
1139         OBD_FREE (ecl, sizeof (*ecl));
1140  failed_0:
1141         echo_put_object (eco);
1142         return (rc);
1143 }
1144
1145 static int
1146 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
1147 {
1148         struct obd_device      *obd = exp->exp_obd;
1149         struct echo_client_obd *ec = &obd->u.echocli;
1150         struct lustre_handle   *ulh = obdo_handle (oa);
1151         struct ec_lock         *ecl = NULL;
1152         int                     found = 0;
1153         struct list_head       *el;
1154         int                     rc;
1155
1156         if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
1157                 return -EINVAL;
1158
1159         spin_lock (&ec->ec_lock);
1160
1161         list_for_each (el, &exp->exp_ec_data.eced_locks) {
1162                 ecl = list_entry (el, struct ec_lock, ecl_exp_chain);
1163                 found = (ecl->ecl_cookie == ulh->cookie);
1164                 if (found) {
1165                         list_del (&ecl->ecl_exp_chain);
1166                         break;
1167                 }
1168         }
1169
1170         spin_unlock (&ec->ec_lock);
1171
1172         if (!found)
1173                 return (-ENOENT);
1174
1175         rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm, ecl->ecl_mode,
1176                         &ecl->ecl_lock_handle);
1177
1178         echo_put_object (ecl->ecl_object);
1179         OBD_FREE (ecl, sizeof (*ecl));
1180
1181         return rc;
1182 }
1183
1184 static int
1185 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
1186                       int len, void *karg, void *uarg)
1187 {
1188         struct obd_device      *obd;
1189         struct echo_client_obd *ec;
1190         struct ec_object       *eco;
1191         struct obd_ioctl_data  *data = karg;
1192         struct obd_trans_info   dummy_oti;
1193         struct oti_req_ack_lock *ack_lock;
1194         struct obdo            *oa;
1195         int                     rw = OBD_BRW_READ;
1196         int                     rc = 0;
1197         int                     i;
1198         ENTRY;
1199
1200         unlock_kernel();
1201
1202         memset(&dummy_oti, 0, sizeof(dummy_oti));
1203
1204         obd = exp->exp_obd;
1205         ec = &obd->u.echocli;
1206
1207         switch (cmd) {
1208         case OBD_IOC_CREATE:                    /* may create echo object */
1209                 if (!capable (CAP_SYS_ADMIN))
1210                         GOTO (out, rc = -EPERM);
1211
1212                 rc = echo_create_object (obd, 1, &data->ioc_obdo1,
1213                                          data->ioc_pbuf1, data->ioc_plen1,
1214                                          &dummy_oti);
1215                 GOTO(out, rc);
1216
1217         case OBD_IOC_DESTROY:
1218                 if (!capable (CAP_SYS_ADMIN))
1219                         GOTO (out, rc = -EPERM);
1220
1221                 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1222                 if (rc == 0) {
1223                         oa = &data->ioc_obdo1;
1224                         oa->o_gr = FILTER_GROUP_ECHO;
1225                         oa->o_valid |= OBD_MD_FLGROUP;
1226                         rc = obd_destroy(ec->ec_exp, oa, eco->eco_lsm, 
1227                                          &dummy_oti);
1228                         if (rc == 0)
1229                                 eco->eco_deleted = 1;
1230                         echo_put_object(eco);
1231                 }
1232                 GOTO(out, rc);
1233
1234         case OBD_IOC_GETATTR:
1235                 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1236                 if (rc == 0) {
1237                         rc = obd_getattr(ec->ec_exp, &data->ioc_obdo1,
1238                                          eco->eco_lsm);
1239                         echo_put_object(eco);
1240                 }
1241                 GOTO(out, rc);
1242
1243         case OBD_IOC_SETATTR:
1244                 if (!capable (CAP_SYS_ADMIN))
1245                         GOTO (out, rc = -EPERM);
1246
1247                 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1248                 if (rc == 0) {
1249                         rc = obd_setattr(ec->ec_exp, &data->ioc_obdo1,
1250                                          eco->eco_lsm, NULL);
1251                         echo_put_object(eco);
1252                 }
1253                 GOTO(out, rc);
1254
1255         case OBD_IOC_BRW_WRITE:
1256                 if (!capable (CAP_SYS_ADMIN))
1257                         GOTO (out, rc = -EPERM);
1258
1259                 rw = OBD_BRW_WRITE;
1260                 /* fall through */
1261         case OBD_IOC_BRW_READ:
1262                 rc = echo_client_brw_ioctl(rw, exp, data);
1263                 GOTO(out, rc);
1264
1265         case ECHO_IOC_GET_STRIPE:
1266                 rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
1267                 if (rc == 0) {
1268                         rc = echo_copyout_lsm(eco->eco_lsm, data->ioc_pbuf1,
1269                                               data->ioc_plen1);
1270                         echo_put_object(eco);
1271                 }
1272                 GOTO(out, rc);
1273
1274         case ECHO_IOC_SET_STRIPE:
1275                 if (!capable (CAP_SYS_ADMIN))
1276                         GOTO (out, rc = -EPERM);
1277
1278                 if (data->ioc_pbuf1 == NULL) {  /* unset */
1279                         rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
1280                         if (rc == 0) {
1281                                 eco->eco_deleted = 1;
1282                                 echo_put_object(eco);
1283                         }
1284                 } else {
1285                         rc = echo_create_object(obd, 0, &data->ioc_obdo1,
1286                                                 data->ioc_pbuf1,
1287                                                 data->ioc_plen1, &dummy_oti);
1288                 }
1289                 GOTO (out, rc);
1290
1291         case ECHO_IOC_ENQUEUE:
1292                 if (!capable (CAP_SYS_ADMIN))
1293                         GOTO (out, rc = -EPERM);
1294
1295                 rc = echo_client_enqueue(exp, &data->ioc_obdo1,
1296                                          data->ioc_conn1, /* lock mode */
1297                                    data->ioc_offset, data->ioc_count);/*extent*/
1298                 GOTO (out, rc);
1299
1300         case ECHO_IOC_CANCEL:
1301                 rc = echo_client_cancel(exp, &data->ioc_obdo1);
1302                 GOTO (out, rc);
1303
1304         default:
1305                 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
1306                 GOTO (out, rc = -ENOTTY);
1307         }
1308
1309         EXIT;
1310  out:
1311
1312         /* XXX this should be in a helper also called by target_send_reply */
1313         for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4; 
1314              i++, ack_lock++) {
1315                 if (!ack_lock->mode)
1316                         break;
1317                 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
1318         }
1319
1320         lock_kernel();
1321
1322         return rc;
1323 }
1324
1325 static int
1326 echo_client_setup(struct obd_device *obddev, obd_count len, void *buf)
1327 {
1328         struct lustre_cfg* lcfg = buf;
1329         struct echo_client_obd *ec = &obddev->u.echocli;
1330         struct obd_device *tgt;
1331         struct lustre_handle conn = {0, };
1332         struct obd_uuid echo_uuid = { "ECHO_UUID" };
1333         int rc;
1334         ENTRY;
1335
1336         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1337                 CERROR("requires a TARGET OBD name\n");
1338                 RETURN(-EINVAL);
1339         }
1340
1341         tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
1342         if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
1343                 CERROR("device not attached or not set up (%s)\n",
1344                        lustre_cfg_string(lcfg, 1));
1345                 RETURN(-EINVAL);
1346         }
1347
1348         spin_lock_init (&ec->ec_lock);
1349         INIT_LIST_HEAD (&ec->ec_objects);
1350         ec->ec_unique = 0;
1351
1352         rc = obd_connect(&conn, tgt, &echo_uuid, NULL, FILTER_GROUP_ECHO);
1353         if (rc) {
1354                 CERROR("fail to connect to device %s\n", lustre_cfg_string(lcfg, 1));
1355                 return (rc);
1356         }
1357         ec->ec_exp = class_conn2export(&conn);
1358
1359         RETURN(rc);
1360 }
1361
1362 static int echo_client_cleanup(struct obd_device *obddev, int flags)
1363 {
1364         struct list_head       *el;
1365         struct ec_object       *eco;
1366         struct echo_client_obd *ec = &obddev->u.echocli;
1367         int rc;
1368         ENTRY;
1369
1370         if (!list_empty(&obddev->obd_exports)) {
1371                 CERROR("still has clients!\n");
1372                 RETURN(-EBUSY);
1373         }
1374
1375         /* XXX assuming sole access */
1376         while (!list_empty(&ec->ec_objects)) {
1377                 el = ec->ec_objects.next;
1378                 eco = list_entry(el, struct ec_object, eco_obj_chain);
1379
1380                 LASSERT(eco->eco_refcount == 0);
1381                 eco->eco_refcount = 1;
1382                 eco->eco_deleted = 1;
1383                 echo_put_object(eco);
1384         }
1385
1386         rc = obd_disconnect(ec->ec_exp, 0);
1387         if (rc != 0)
1388                 CERROR("fail to disconnect device: %d\n", rc);
1389
1390         RETURN(rc);
1391 }
1392
1393 static int echo_client_connect(struct lustre_handle *conn,
1394                                struct obd_device *src, 
1395                                struct obd_uuid *cluuid,
1396                                struct obd_connect_data *data,
1397                                unsigned long flags)
1398 {
1399         struct obd_export *exp;
1400         int                rc;
1401
1402         rc = class_connect(conn, src, cluuid);
1403         if (rc == 0) {
1404                 exp = class_conn2export(conn);
1405                 INIT_LIST_HEAD(&exp->exp_ec_data.eced_locks);
1406                 class_export_put(exp);
1407         }
1408
1409         RETURN (rc);
1410 }
1411
1412 static int echo_client_disconnect(struct obd_export *exp, 
1413                                   unsigned long flags)
1414 {
1415         struct obd_device      *obd;
1416         struct echo_client_obd *ec;
1417         struct ec_lock         *ecl;
1418         int                     rc;
1419         ENTRY;
1420
1421         if (exp == NULL)
1422                 GOTO(out, rc = -EINVAL);
1423
1424         obd = exp->exp_obd;
1425         ec = &obd->u.echocli;
1426
1427         /* no more contention on export's lock list */
1428         while (!list_empty (&exp->exp_ec_data.eced_locks)) {
1429                 ecl = list_entry (exp->exp_ec_data.eced_locks.next,
1430                                   struct ec_lock, ecl_exp_chain);
1431                 list_del (&ecl->ecl_exp_chain);
1432
1433                 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm,
1434                                  ecl->ecl_mode, &ecl->ecl_lock_handle);
1435
1436                 CDEBUG (D_INFO, "Cancel lock on object "LPX64" on disconnect "
1437                         "(%d)\n", ecl->ecl_object->eco_id, rc);
1438
1439                 echo_put_object (ecl->ecl_object);
1440                 OBD_FREE (ecl, sizeof (*ecl));
1441         }
1442
1443         rc = class_disconnect(exp, 0);
1444         GOTO(out, rc);
1445  out:
1446         return rc;
1447 }
1448
1449 static struct obd_ops echo_obd_ops = {
1450         .o_owner       = THIS_MODULE,
1451         .o_setup       = echo_client_setup,
1452         .o_cleanup     = echo_client_cleanup,
1453         .o_iocontrol   = echo_client_iocontrol,
1454         .o_connect     = echo_client_connect,
1455         .o_disconnect  = echo_client_disconnect
1456 };
1457
1458 int echo_client_init(void)
1459 {
1460         struct lprocfs_static_vars lvars;
1461
1462         lprocfs_init_vars(echo, &lvars);
1463         return class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
1464                                    OBD_ECHO_CLIENT_DEVICENAME);
1465 }
1466
1467 void echo_client_exit(void)
1468 {
1469         class_unregister_type(OBD_ECHO_CLIENT_DEVICENAME);
1470 }