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