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