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