Whamcloud - gitweb
198cd2576a58fd443e0ab0563d634f08c791d1c2
[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         obd_count               npages;
497         struct brw_page        *pga;
498         struct brw_page        *pgp;
499         obd_off                 off;
500         int                     i;
501         int                     rc;
502         int                     verify;
503         int                     gfp_mask;
504
505         verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
506                   (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
507                   (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
508
509         gfp_mask = ((oa->o_id & 2) == 0) ? CFS_ALLOC_STD : CFS_ALLOC_HIGHUSER;
510
511         LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
512         LASSERT(lsm != NULL);
513         LASSERT(lsm->lsm_object_id == oa->o_id);
514
515         if (count <= 0 ||
516             (count & (CFS_PAGE_SIZE - 1)) != 0)
517                 return (-EINVAL);
518
519         /* XXX think again with misaligned I/O */
520         npages = count >> CFS_PAGE_SHIFT;
521
522         OBD_ALLOC(pga, npages * sizeof(*pga));
523         if (pga == NULL)
524                 return (-ENOMEM);
525
526         for (i = 0, pgp = pga, off = offset;
527              i < npages;
528              i++, pgp++, off += CFS_PAGE_SIZE) {
529
530                 LASSERT (pgp->pg == NULL);      /* for cleanup */
531
532                 rc = -ENOMEM;
533                 pgp->pg = cfs_alloc_page (gfp_mask);
534                 if (pgp->pg == NULL)
535                         goto out;
536
537                 pgp->count = CFS_PAGE_SIZE;
538                 pgp->off = off;
539                 pgp->flag = 0;
540
541                 if (verify)
542                         echo_client_page_debug_setup(lsm, pgp->pg, rw,
543                                                      oa->o_id, off, pgp->count);
544         }
545
546         rc = obd_brw(rw, ec->ec_exp, oa, lsm, npages, pga, oti);
547
548  out:
549         if (rc != 0 || rw != OBD_BRW_READ)
550                 verify = 0;
551
552         for (i = 0, pgp = pga; i < npages; i++, pgp++) {
553                 if (pgp->pg == NULL)
554                         continue;
555
556                 if (verify) {
557                         int vrc;
558                         vrc = echo_client_page_debug_check(lsm, pgp->pg, oa->o_id,
559                                                            pgp->off, pgp->count);
560                         if (vrc != 0 && rc == 0)
561                                 rc = vrc;
562                 }
563                 cfs_free_page(pgp->pg);
564         }
565         OBD_FREE(pga, npages * sizeof(*pga));
566         return (rc);
567 }
568
569 #ifdef __KERNEL__
570 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
571 static int echo_client_ubrw(struct obd_device *obd, int rw,
572                             struct obdo *oa, struct lov_stripe_md *lsm,
573                             obd_off offset, obd_size count, char *buffer,
574                             struct obd_trans_info *oti)
575 {
576         struct echo_client_obd *ec = &obd->u.echo_client;
577         obd_count               npages;
578         struct brw_page        *pga;
579         struct brw_page        *pgp;
580         obd_off                 off;
581         struct kiobuf          *kiobuf;
582         int                     i;
583         int                     rc;
584
585         LASSERT (rw == OBD_BRW_WRITE ||
586                  rw == OBD_BRW_READ);
587
588         /* NB: for now, only whole pages, page aligned */
589
590         if (count <= 0 ||
591             ((long)buffer & (CFS_PAGE_SIZE - 1)) != 0 ||
592             (count & (CFS_PAGE_SIZE - 1)) != 0 ||
593             (lsm != NULL && lsm->lsm_object_id != oa->o_id))
594                 return (-EINVAL);
595
596         /* XXX think again with misaligned I/O */
597         npages = count >> CFS_PAGE_SHIFT;
598
599         OBD_ALLOC(pga, npages * sizeof(*pga));
600         if (pga == NULL)
601                 return (-ENOMEM);
602
603         rc = alloc_kiovec (1, &kiobuf);
604         if (rc != 0)
605                 goto out_1;
606
607         rc = map_user_kiobuf ((rw == OBD_BRW_READ) ? READ : WRITE,
608                               kiobuf, (unsigned long)buffer, count);
609         if (rc != 0)
610                 goto out_2;
611
612         LASSERT (kiobuf->offset == 0);
613         LASSERT (kiobuf->nr_pages == npages);
614
615         for (i = 0, off = offset, pgp = pga;
616              i < npages;
617              i++, off += CFS_PAGE_SIZE, pgp++) {
618                 pgp->off = off;
619                 pgp->pg = kiobuf->maplist[i];
620                 pgp->count = CFS_PAGE_SIZE;
621                 pgp->flag = 0;
622         }
623
624         rc = obd_brw(rw, ec->ec_exp, oa, lsm, npages, pga, oti);
625
626         //        if (rw == OBD_BRW_READ)
627         //                mark_dirty_kiobuf (kiobuf, count);
628
629         unmap_kiobuf (kiobuf);
630  out_2:
631         free_kiovec (1, &kiobuf);
632  out_1:
633         OBD_FREE(pga, npages * sizeof(*pga));
634         return (rc);
635 }
636 #else
637 static int echo_client_ubrw(struct obd_device *obd, int rw,
638                             struct obdo *oa, struct lov_stripe_md *lsm,
639                             obd_off offset, obd_size count, char *buffer,
640                             struct obd_trans_info *oti)
641 {
642 #warning "echo_client_ubrw() needs to be ported on 2.6 yet"
643         LBUG();
644         return 0;
645 }
646 #endif
647 #endif
648
649 struct echo_async_state;
650
651 #define EAP_MAGIC 79277927
652 struct echo_async_page {
653         int                     eap_magic;
654         cfs_page_t             *eap_page;
655         void                    *eap_cookie;
656         obd_off                 eap_off;
657         struct echo_async_state *eap_eas;
658         struct list_head        eap_item;
659 };
660
661 #define EAP_FROM_COOKIE(c)                                                      \
662         (LASSERT(((struct echo_async_page *)(c))->eap_magic == EAP_MAGIC),      \
663          (struct echo_async_page *)(c))
664
665 struct echo_async_state {
666         spinlock_t              eas_lock;
667         obd_off                 eas_next_offset;
668         obd_off                 eas_end_offset;
669         int                     eas_in_flight;
670         int                     eas_rc;
671         cfs_waitq_t             eas_waitq;
672         struct list_head        eas_avail;
673         struct obdo             eas_oa;
674         struct lov_stripe_md    *eas_lsm;
675 };
676
677 static int eas_should_wake(struct echo_async_state *eas)
678 {
679         unsigned long flags;
680         int rc = 0;
681         spin_lock_irqsave(&eas->eas_lock, flags);
682         if (eas->eas_rc == 0 && !list_empty(&eas->eas_avail))
683             rc = 1;
684         spin_unlock_irqrestore(&eas->eas_lock, flags);
685         return rc;
686 };
687
688 static int ec_ap_make_ready(void *data, int cmd)
689 {
690         /* our pages are issued ready */
691         LBUG();
692         return 0;
693 }
694 static int ec_ap_refresh_count(void *data, int cmd)
695 {
696         /* our pages are issued with a stable count */
697         LBUG();
698         return CFS_PAGE_SIZE;
699 }
700 static void ec_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
701 {
702         struct echo_async_page *eap = EAP_FROM_COOKIE(data);
703
704         memcpy(oa, &eap->eap_eas->eas_oa, sizeof(*oa));
705 }
706
707 static void ec_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
708 {
709         struct echo_async_page *eap = EAP_FROM_COOKIE(data);
710         struct echo_async_state *eas;
711         unsigned long flags;
712
713         eas = eap->eap_eas;
714
715         if (cmd == OBD_BRW_READ &&
716             eas->eas_oa.o_id != ECHO_PERSISTENT_OBJID &&
717             (eas->eas_oa.o_valid & OBD_MD_FLFLAGS) != 0 &&
718             (eas->eas_oa.o_flags & OBD_FL_DEBUG_CHECK) != 0)
719                 echo_client_page_debug_check(eas->eas_lsm, eap->eap_page,
720                                              eas->eas_oa.o_id, eap->eap_off,
721                                              CFS_PAGE_SIZE);
722
723         spin_lock_irqsave(&eas->eas_lock, flags);
724         if (rc && !eas->eas_rc)
725                 eas->eas_rc = rc;
726         eas->eas_in_flight--;
727         list_add(&eap->eap_item, &eas->eas_avail);
728         cfs_waitq_signal(&eas->eas_waitq);
729         spin_unlock_irqrestore(&eas->eas_lock, flags);
730 }
731
732 static struct obd_async_page_ops ec_async_page_ops = {
733         .ap_make_ready =        ec_ap_make_ready,
734         .ap_refresh_count =     ec_ap_refresh_count,
735         .ap_fill_obdo =         ec_ap_fill_obdo,
736         .ap_completion =        ec_ap_completion,
737 };
738
739 static int echo_client_async_page(struct obd_export *exp, int rw,
740                                    struct obdo *oa, struct lov_stripe_md *lsm,
741                                    obd_off offset, obd_size count,
742                                    obd_size batching)
743 {
744         obd_count npages, i;
745         struct echo_async_page *eap;
746         struct echo_async_state eas;
747         int rc = 0;
748         unsigned long flags;
749         struct echo_async_page **aps = NULL;
750
751         ENTRY;
752 #if 0
753         int                     verify;
754         int                     gfp_mask;
755
756         verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
757                   (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
758                   (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
759
760         gfp_mask = ((oa->o_id & 2) == 0) ? GFP_KERNEL : GFP_HIGHUSER;
761 #endif
762
763         LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
764
765         if (count <= 0 ||
766             (count & (CFS_PAGE_SIZE - 1)) != 0 ||
767             (lsm != NULL &&
768              lsm->lsm_object_id != oa->o_id))
769                 return (-EINVAL);
770
771         /* XXX think again with misaligned I/O */
772         npages = batching >> CFS_PAGE_SHIFT;
773
774         memcpy(&eas.eas_oa, oa, sizeof(*oa));
775         eas.eas_next_offset = offset;
776         eas.eas_end_offset = offset + count;
777         spin_lock_init(&eas.eas_lock);
778         cfs_waitq_init(&eas.eas_waitq);
779         eas.eas_in_flight = 0;
780         eas.eas_rc = 0;
781         eas.eas_lsm = lsm;
782         CFS_INIT_LIST_HEAD(&eas.eas_avail);
783
784         OBD_ALLOC(aps, npages * sizeof aps[0]);
785         if (aps == NULL)
786                 return (-ENOMEM);
787
788         /* prepare the group of pages that we're going to be keeping
789          * in flight */
790         for (i = 0; i < npages; i++) {
791                 cfs_page_t *page = cfs_alloc_page(CFS_ALLOC_STD);
792                 if (page == NULL)
793                         GOTO(out, rc = -ENOMEM);
794
795                 OBD_ALLOC(eap, sizeof(*eap));
796                 if (eap == NULL) {
797                         cfs_free_page(page);
798                         GOTO(out, rc = -ENOMEM);
799                 }
800
801                 eap->eap_magic = EAP_MAGIC;
802                 eap->eap_page = page;
803                 eap->eap_eas = &eas;
804                 list_add_tail(&eap->eap_item, &eas.eas_avail);
805                 aps[i] = eap;
806         }
807
808         /* first we spin queueing io and being woken by its completion */
809         spin_lock_irqsave(&eas.eas_lock, flags);
810         for(;;) {
811                 int rc;
812
813                 /* sleep until we have a page to send */
814                 spin_unlock_irqrestore(&eas.eas_lock, flags);
815                 rc = wait_event_interruptible(eas.eas_waitq,
816                                               eas_should_wake(&eas));
817                 spin_lock_irqsave(&eas.eas_lock, flags);
818                 if (rc && !eas.eas_rc)
819                         eas.eas_rc = rc;
820                 if (eas.eas_rc)
821                         break;
822                 if (list_empty(&eas.eas_avail))
823                         continue;
824                 eap = list_entry(eas.eas_avail.next, struct echo_async_page,
825                                  eap_item);
826                 list_del(&eap->eap_item);
827                 spin_unlock_irqrestore(&eas.eas_lock, flags);
828
829                 /* unbind the eap from its old page offset */
830                 if (eap->eap_cookie != NULL) {
831                         obd_teardown_async_page(exp, lsm, NULL,
832                                                 eap->eap_cookie);
833                         eap->eap_cookie = NULL;
834                 }
835
836                 eas.eas_next_offset += CFS_PAGE_SIZE;
837                 eap->eap_off = eas.eas_next_offset;
838
839                 rc = obd_prep_async_page(exp, lsm, NULL, eap->eap_page,
840                                          eap->eap_off, &ec_async_page_ops,
841                                          eap, &eap->eap_cookie);
842                 if (rc) {
843                         spin_lock_irqsave(&eas.eas_lock, flags);
844                         eas.eas_rc = rc;
845                         break;
846                 }
847
848                 if (oa->o_id != ECHO_PERSISTENT_OBJID &&
849                     (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
850                     (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0)
851                         echo_client_page_debug_setup(lsm, eap->eap_page, rw,
852                                                      oa->o_id,
853                                                      eap->eap_off, CFS_PAGE_SIZE);
854
855                 /* always asserts urgent, which isn't quite right */
856                 rc = obd_queue_async_io(exp, lsm, NULL, eap->eap_cookie,
857                                         rw, 0, CFS_PAGE_SIZE, 0,
858                                         ASYNC_READY | ASYNC_URGENT |
859                                         ASYNC_COUNT_STABLE);
860                 spin_lock_irqsave(&eas.eas_lock, flags);
861                 if (rc && !eas.eas_rc) {
862                         eas.eas_rc = rc;
863                         break;
864                 }
865                 eas.eas_in_flight++;
866                 if (eas.eas_next_offset == eas.eas_end_offset)
867                         break;
868         }
869
870         /* still hold the eas_lock here.. */
871
872         /* now we just spin waiting for all the rpcs to complete */
873         while(eas.eas_in_flight) {
874                 spin_unlock_irqrestore(&eas.eas_lock, flags);
875                 wait_event_interruptible(eas.eas_waitq,
876                                          eas.eas_in_flight == 0);
877                 spin_lock_irqsave(&eas.eas_lock, flags);
878         }
879         spin_unlock_irqrestore(&eas.eas_lock, flags);
880
881 out:
882         if (aps != NULL) {
883                 for (i = 0; i < npages; ++ i) {
884                         cfs_page_t *page;
885
886                         eap = aps[i];
887                         page = eap->eap_page;
888                         if (eap->eap_cookie != NULL)
889                                 obd_teardown_async_page(exp, lsm, NULL,
890                                                         eap->eap_cookie);
891                         OBD_FREE(eap, sizeof(*eap));
892                         cfs_free_page(page);
893                 }
894                 OBD_FREE(aps, npages * sizeof aps[0]);
895         }
896
897         RETURN(rc);
898 }
899
900 static int echo_client_prep_commit(struct obd_export *exp, int rw,
901                                    struct obdo *oa, struct lov_stripe_md *lsm,
902                                    obd_off offset, obd_size count,
903                                    obd_size batch, struct obd_trans_info *oti)
904 {
905         struct obd_ioobj ioo;
906         struct niobuf_local *lnb;
907         struct niobuf_remote *rnb;
908         obd_off off;
909         obd_size npages, tot_pages;
910         int i, ret = 0;
911         ENTRY;
912
913         if (count <= 0 || (count & (CFS_PAGE_SIZE - 1)) != 0 ||
914             (lsm != NULL && lsm->lsm_object_id != oa->o_id))
915                 RETURN(-EINVAL);
916
917         npages = batch >> CFS_PAGE_SHIFT;
918         tot_pages = count >> CFS_PAGE_SHIFT;
919
920         OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
921         OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
922
923         if (lnb == NULL || rnb == NULL)
924                 GOTO(out, ret = -ENOMEM);
925
926         obdo_to_ioobj(oa, &ioo);
927
928         off = offset;
929
930         for(; tot_pages; tot_pages -= npages) {
931                 if (tot_pages < npages)
932                         npages = tot_pages;
933
934                 for (i = 0; i < npages; i++, off += CFS_PAGE_SIZE) {
935                         rnb[i].offset = off;
936                         rnb[i].len = CFS_PAGE_SIZE;
937                 }
938
939                 ioo.ioo_bufcnt = npages;
940                 oti->oti_transno = 0;
941
942                 ret = obd_preprw(rw, exp, oa, 1, &ioo, npages, rnb, lnb, oti);
943                 if (ret != 0)
944                         GOTO(out, ret);
945
946                 for (i = 0; i < npages; i++) {
947                         cfs_page_t *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                         if (rw == OBD_BRW_WRITE)
959                                 echo_client_page_debug_setup(lsm, page, rw,
960                                                              oa->o_id,
961                                                              rnb[i].offset,
962                                                              rnb[i].len);
963                         else
964                                 echo_client_page_debug_check(lsm, page,
965                                                              oa->o_id,
966                                                              rnb[i].offset,
967                                                              rnb[i].len);
968                 }
969
970                 ret = obd_commitrw(rw, exp, oa, 1, &ioo, npages, lnb, oti, ret);
971                 if (ret != 0)
972                         GOTO(out, ret);
973         }
974
975 out:
976         if (lnb)
977                 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
978         if (rnb)
979                 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
980         RETURN(ret);
981 }
982
983 int echo_client_brw_ioctl(int rw, struct obd_export *exp,
984                           struct obd_ioctl_data *data)
985 {
986         struct obd_device *obd = class_exp2obd(exp);
987         struct echo_client_obd *ec = &obd->u.echo_client;
988         struct obd_trans_info dummy_oti = { .oti_thread_id = -1 };
989         struct ec_object *eco;
990         int rc;
991         ENTRY;
992
993         rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
994         if (rc)
995                 RETURN(rc);
996
997         data->ioc_obdo1.o_valid &= ~OBD_MD_FLHANDLE;
998         data->ioc_obdo1.o_valid |= OBD_MD_FLGROUP;
999         data->ioc_obdo1.o_gr = FILTER_GROUP_ECHO;
1000
1001         switch((long)data->ioc_pbuf1) {
1002         case 1:
1003                 if (data->ioc_pbuf2 == NULL) { // NULL user data pointer
1004                         rc = echo_client_kbrw(obd, rw, &data->ioc_obdo1,
1005                                               eco->eco_lsm, data->ioc_offset,
1006                                               data->ioc_count, &dummy_oti);
1007                 } else {
1008 #ifdef __KERNEL__
1009                         rc = echo_client_ubrw(obd, rw, &data->ioc_obdo1,
1010                                               eco->eco_lsm, data->ioc_offset,
1011                                               data->ioc_count, data->ioc_pbuf2,
1012                                               &dummy_oti);
1013 #endif
1014                 }
1015                 break;
1016         case 2:
1017                 rc = echo_client_async_page(ec->ec_exp, rw, &data->ioc_obdo1,
1018                                            eco->eco_lsm, data->ioc_offset,
1019                                            data->ioc_count, data->ioc_plen1);
1020                 break;
1021         case 3:
1022                 rc = echo_client_prep_commit(ec->ec_exp, rw, &data->ioc_obdo1,
1023                                             eco->eco_lsm, data->ioc_offset,
1024                                             data->ioc_count, data->ioc_plen1,
1025                                             &dummy_oti);
1026                 break;
1027         default:
1028                 rc = -EINVAL;
1029         }
1030         echo_put_object(eco);
1031         RETURN(rc);
1032 }
1033
1034 static int
1035 echo_ldlm_callback (struct ldlm_lock *lock, struct ldlm_lock_desc *new,
1036                     void *data, int flag)
1037 {
1038         struct ec_object       *eco = (struct ec_object *)data;
1039         struct echo_client_obd *ec = &(eco->eco_device->u.echo_client);
1040         struct lustre_handle    lockh;
1041         struct list_head       *el;
1042         int                     found = 0;
1043         int                     rc;
1044
1045         ldlm_lock2handle (lock, &lockh);
1046
1047         /* #ifdef this out if we're not feeling paranoid */
1048         spin_lock (&ec->ec_lock);
1049         list_for_each (el, &ec->ec_objects) {
1050                 found = (eco == list_entry(el, struct ec_object,
1051                                            eco_obj_chain));
1052                 if (found)
1053                         break;
1054         }
1055         spin_unlock (&ec->ec_lock);
1056         LASSERT (found);
1057
1058         switch (flag) {
1059         case LDLM_CB_BLOCKING:
1060                 CDEBUG(D_INFO, "blocking callback on "LPX64", handle "LPX64"\n",
1061                        eco->eco_id, lockh.cookie);
1062                 rc = ldlm_cli_cancel (&lockh);
1063                 if (rc != ELDLM_OK)
1064                         CERROR ("ldlm_cli_cancel failed: %d\n", rc);
1065                 break;
1066
1067         case LDLM_CB_CANCELING:
1068                 CDEBUG(D_INFO, "cancel callback on "LPX64", handle "LPX64"\n",
1069                        eco->eco_id, lockh.cookie);
1070                 break;
1071
1072         default:
1073                 LBUG ();
1074         }
1075
1076         return (0);
1077 }
1078
1079 static int
1080 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
1081                     int mode, obd_off offset, obd_size nob)
1082 {
1083         struct obd_device      *obd = exp->exp_obd;
1084         struct echo_client_obd *ec = &obd->u.echo_client;
1085         struct lustre_handle   *ulh = obdo_handle (oa);
1086         struct ec_object       *eco;
1087         struct ec_lock         *ecl;
1088         int                     flags;
1089         int                     rc;
1090
1091         if (!(mode == LCK_PR || mode == LCK_PW))
1092                 return -EINVAL;
1093
1094         if ((offset & (CFS_PAGE_SIZE - 1)) != 0 ||
1095             (nob & (CFS_PAGE_SIZE - 1)) != 0)
1096                 return -EINVAL;
1097
1098         rc = echo_get_object (&eco, obd, oa);
1099         if (rc != 0)
1100                 return rc;
1101
1102         rc = -ENOMEM;
1103         OBD_ALLOC (ecl, sizeof (*ecl));
1104         if (ecl == NULL)
1105                 goto failed_0;
1106
1107         ecl->ecl_mode = mode;
1108         ecl->ecl_object = eco;
1109         ecl->ecl_policy.l_extent.start = offset;
1110         ecl->ecl_policy.l_extent.end =
1111                 (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
1112
1113         flags = 0;
1114         rc = obd_enqueue(ec->ec_exp, eco->eco_lsm, LDLM_EXTENT,
1115                          &ecl->ecl_policy, mode, &flags, echo_ldlm_callback,
1116                          ldlm_completion_ast, NULL, eco, sizeof(struct ost_lvb),
1117                          lustre_swab_ost_lvb, &ecl->ecl_lock_handle);
1118         if (rc != 0)
1119                 goto failed_1;
1120
1121         CDEBUG(D_INFO, "enqueue handle "LPX64"\n", ecl->ecl_lock_handle.cookie);
1122
1123         /* NB ecl takes object ref from echo_get_object() above */
1124         spin_lock(&ec->ec_lock);
1125
1126         list_add(&ecl->ecl_exp_chain, &exp->exp_ec_data.eced_locks);
1127         ulh->cookie = ecl->ecl_cookie = ec->ec_unique++;
1128
1129         spin_unlock(&ec->ec_lock);
1130
1131         oa->o_valid |= OBD_MD_FLHANDLE;
1132         return 0;
1133
1134  failed_1:
1135         OBD_FREE (ecl, sizeof (*ecl));
1136  failed_0:
1137         echo_put_object (eco);
1138         return (rc);
1139 }
1140
1141 static int
1142 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
1143 {
1144         struct obd_device      *obd = exp->exp_obd;
1145         struct echo_client_obd *ec = &obd->u.echo_client;
1146         struct lustre_handle   *ulh = obdo_handle (oa);
1147         struct ec_lock         *ecl = NULL;
1148         int                     found = 0;
1149         struct list_head       *el;
1150         int                     rc;
1151
1152         if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
1153                 return -EINVAL;
1154
1155         spin_lock (&ec->ec_lock);
1156
1157         list_for_each (el, &exp->exp_ec_data.eced_locks) {
1158                 ecl = list_entry (el, struct ec_lock, ecl_exp_chain);
1159                 found = (ecl->ecl_cookie == ulh->cookie);
1160                 if (found) {
1161                         list_del (&ecl->ecl_exp_chain);
1162                         break;
1163                 }
1164         }
1165
1166         spin_unlock (&ec->ec_lock);
1167
1168         if (!found)
1169                 return (-ENOENT);
1170
1171         rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm, ecl->ecl_mode,
1172                         &ecl->ecl_lock_handle);
1173
1174         echo_put_object (ecl->ecl_object);
1175         OBD_FREE (ecl, sizeof (*ecl));
1176
1177         return rc;
1178 }
1179
1180 static int
1181 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
1182                       int len, void *karg, void *uarg)
1183 {
1184         struct obd_device      *obd;
1185         struct echo_client_obd *ec;
1186         struct ec_object       *eco;
1187         struct obd_ioctl_data  *data = karg;
1188         struct obd_trans_info   dummy_oti;
1189         struct oti_req_ack_lock *ack_lock;
1190         struct obdo            *oa;
1191         int                     rw = OBD_BRW_READ;
1192         int                     rc = 0;
1193         int                     i;
1194         ENTRY;
1195
1196         unlock_kernel();
1197
1198         memset(&dummy_oti, 0, sizeof(dummy_oti));
1199
1200         obd = exp->exp_obd;
1201         ec = &obd->u.echo_client;
1202
1203         switch (cmd) {
1204         case OBD_IOC_CREATE:                    /* may create echo object */
1205                 if (!capable (CAP_SYS_ADMIN))
1206                         GOTO (out, rc = -EPERM);
1207
1208                 rc = echo_create_object (obd, 1, &data->ioc_obdo1,
1209                                          data->ioc_pbuf1, data->ioc_plen1,
1210                                          &dummy_oti);
1211                 GOTO(out, rc);
1212
1213         case OBD_IOC_DESTROY:
1214                 if (!capable (CAP_SYS_ADMIN))
1215                         GOTO (out, rc = -EPERM);
1216
1217                 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1218                 if (rc == 0) {
1219                         oa = &data->ioc_obdo1;
1220                         oa->o_gr = FILTER_GROUP_ECHO;
1221                         oa->o_valid |= OBD_MD_FLGROUP;
1222                         rc = obd_destroy(ec->ec_exp, oa, eco->eco_lsm,
1223                                          &dummy_oti, NULL);
1224                         if (rc == 0)
1225                                 eco->eco_deleted = 1;
1226                         echo_put_object(eco);
1227                 }
1228                 GOTO(out, rc);
1229
1230         case OBD_IOC_GETATTR:
1231                 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1232                 if (rc == 0) {
1233                         rc = obd_getattr(ec->ec_exp, &data->ioc_obdo1,
1234                                          eco->eco_lsm);
1235                         echo_put_object(eco);
1236                 }
1237                 GOTO(out, rc);
1238
1239         case OBD_IOC_SETATTR:
1240                 if (!capable (CAP_SYS_ADMIN))
1241                         GOTO (out, rc = -EPERM);
1242
1243                 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1244                 if (rc == 0) {
1245                         rc = obd_setattr(ec->ec_exp, &data->ioc_obdo1,
1246                                          eco->eco_lsm, NULL);
1247                         echo_put_object(eco);
1248                 }
1249                 GOTO(out, rc);
1250
1251         case OBD_IOC_BRW_WRITE:
1252                 if (!capable (CAP_SYS_ADMIN))
1253                         GOTO (out, rc = -EPERM);
1254
1255                 rw = OBD_BRW_WRITE;
1256                 /* fall through */
1257         case OBD_IOC_BRW_READ:
1258                 rc = echo_client_brw_ioctl(rw, exp, data);
1259                 GOTO(out, rc);
1260
1261         case ECHO_IOC_GET_STRIPE:
1262                 rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
1263                 if (rc == 0) {
1264                         rc = echo_copyout_lsm(eco->eco_lsm, data->ioc_pbuf1,
1265                                               data->ioc_plen1);
1266                         echo_put_object(eco);
1267                 }
1268                 GOTO(out, rc);
1269
1270         case ECHO_IOC_SET_STRIPE:
1271                 if (!capable (CAP_SYS_ADMIN))
1272                         GOTO (out, rc = -EPERM);
1273
1274                 if (data->ioc_pbuf1 == NULL) {  /* unset */
1275                         rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
1276                         if (rc == 0) {
1277                                 eco->eco_deleted = 1;
1278                                 echo_put_object(eco);
1279                         }
1280                 } else {
1281                         rc = echo_create_object(obd, 0, &data->ioc_obdo1,
1282                                                 data->ioc_pbuf1,
1283                                                 data->ioc_plen1, &dummy_oti);
1284                 }
1285                 GOTO (out, rc);
1286
1287         case ECHO_IOC_ENQUEUE:
1288                 if (!capable (CAP_SYS_ADMIN))
1289                         GOTO (out, rc = -EPERM);
1290
1291                 rc = echo_client_enqueue(exp, &data->ioc_obdo1,
1292                                          data->ioc_conn1, /* lock mode */
1293                                    data->ioc_offset, data->ioc_count);/*extent*/
1294                 GOTO (out, rc);
1295
1296         case ECHO_IOC_CANCEL:
1297                 rc = echo_client_cancel(exp, &data->ioc_obdo1);
1298                 GOTO (out, rc);
1299
1300         default:
1301                 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
1302                 GOTO (out, rc = -ENOTTY);
1303         }
1304
1305         EXIT;
1306  out:
1307
1308         /* XXX this should be in a helper also called by target_send_reply */
1309         for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
1310              i++, ack_lock++) {
1311                 if (!ack_lock->mode)
1312                         break;
1313                 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
1314         }
1315
1316         lock_kernel();
1317
1318         return rc;
1319 }
1320
1321 static int echo_client_setup(struct obd_device *obddev, struct lustre_cfg *lcfg)
1322 {
1323         struct echo_client_obd *ec = &obddev->u.echo_client;
1324         struct obd_device *tgt;
1325         struct lustre_handle conn = {0, };
1326         struct obd_uuid echo_uuid = { "ECHO_UUID" };
1327         struct obd_connect_data *ocd = NULL;
1328         int rc;
1329         ENTRY;
1330
1331         if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1332                 CERROR("requires a TARGET OBD name\n");
1333                 RETURN(-EINVAL);
1334         }
1335
1336         tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
1337         if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
1338                 CERROR("device not attached or not set up (%s)\n",
1339                        lustre_cfg_string(lcfg, 1));
1340                 RETURN(-EINVAL);
1341         }
1342
1343         spin_lock_init (&ec->ec_lock);
1344         CFS_INIT_LIST_HEAD (&ec->ec_objects);
1345         ec->ec_unique = 0;
1346
1347         OBD_ALLOC(ocd, sizeof(*ocd));
1348         if (ocd == NULL) {
1349                 CERROR("Can't alloc ocd connecting to %s\n",
1350                        lustre_cfg_string(lcfg, 1));
1351                 return -ENOMEM;
1352         }
1353
1354         ocd->ocd_connect_flags = OBD_CONNECT_VERSION;
1355         ocd->ocd_version = LUSTRE_VERSION_CODE;
1356
1357         rc = obd_connect(NULL, &conn, tgt, &echo_uuid, ocd);
1358
1359         OBD_FREE(ocd, sizeof(*ocd));
1360
1361         if (rc != 0) {
1362                 CERROR("fail to connect to device %s\n",
1363                        lustre_cfg_string(lcfg, 1));
1364                 return (rc);
1365         }
1366         ec->ec_exp = class_conn2export(&conn);
1367
1368         RETURN(rc);
1369 }
1370
1371 static int echo_client_cleanup(struct obd_device *obddev)
1372 {
1373         struct list_head       *el;
1374         struct ec_object       *eco;
1375         struct echo_client_obd *ec = &obddev->u.echo_client;
1376         int rc;
1377         ENTRY;
1378
1379         if (!list_empty(&obddev->obd_exports)) {
1380                 CERROR("still has clients!\n");
1381                 RETURN(-EBUSY);
1382         }
1383
1384         /* XXX assuming sole access */
1385         while (!list_empty(&ec->ec_objects)) {
1386                 el = ec->ec_objects.next;
1387                 eco = list_entry(el, struct ec_object, eco_obj_chain);
1388
1389                 LASSERT(eco->eco_refcount == 0);
1390                 eco->eco_refcount = 1;
1391                 eco->eco_deleted = 1;
1392                 echo_put_object(eco);
1393         }
1394
1395         rc = obd_disconnect(ec->ec_exp);
1396         if (rc != 0)
1397                 CERROR("fail to disconnect device: %d\n", rc);
1398
1399         RETURN(rc);
1400 }
1401
1402 static int echo_client_connect(const struct lu_context *ctx,
1403                                struct lustre_handle *conn,
1404                                struct obd_device *src, struct obd_uuid *cluuid,
1405                                struct obd_connect_data *data)
1406 {
1407         struct obd_export *exp;
1408         int                rc;
1409
1410         ENTRY;
1411         rc = class_connect(conn, src, cluuid);
1412         if (rc == 0) {
1413                 exp = class_conn2export(conn);
1414                 CFS_INIT_LIST_HEAD(&exp->exp_ec_data.eced_locks);
1415                 class_export_put(exp);
1416         }
1417
1418         RETURN (rc);
1419 }
1420
1421 static int echo_client_disconnect(struct obd_export *exp)
1422 {
1423         struct obd_device      *obd;
1424         struct echo_client_obd *ec;
1425         struct ec_lock         *ecl;
1426         int                     rc;
1427         ENTRY;
1428
1429         if (exp == NULL)
1430                 GOTO(out, rc = -EINVAL);
1431
1432         obd = exp->exp_obd;
1433         ec = &obd->u.echo_client;
1434
1435         /* no more contention on export's lock list */
1436         while (!list_empty (&exp->exp_ec_data.eced_locks)) {
1437                 ecl = list_entry (exp->exp_ec_data.eced_locks.next,
1438                                   struct ec_lock, ecl_exp_chain);
1439                 list_del (&ecl->ecl_exp_chain);
1440
1441                 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm,
1442                                  ecl->ecl_mode, &ecl->ecl_lock_handle);
1443
1444                 CDEBUG (D_INFO, "Cancel lock on object "LPX64" on disconnect "
1445                         "(%d)\n", ecl->ecl_object->eco_id, rc);
1446
1447                 echo_put_object (ecl->ecl_object);
1448                 OBD_FREE (ecl, sizeof (*ecl));
1449         }
1450
1451         rc = class_disconnect(exp);
1452         GOTO(out, rc);
1453  out:
1454         return rc;
1455 }
1456
1457 static struct obd_ops echo_obd_ops = {
1458         .o_owner       = THIS_MODULE,
1459         .o_setup       = echo_client_setup,
1460         .o_cleanup     = echo_client_cleanup,
1461         .o_iocontrol   = echo_client_iocontrol,
1462         .o_connect     = echo_client_connect,
1463         .o_disconnect  = echo_client_disconnect
1464 };
1465
1466 int echo_client_init(void)
1467 {
1468         struct lprocfs_static_vars lvars;
1469
1470         lprocfs_init_vars(echo, &lvars);
1471         return class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
1472                                    LUSTRE_ECHO_CLIENT_NAME, NULL);
1473 }
1474
1475 void echo_client_exit(void)
1476 {
1477         class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
1478 }