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