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