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