Whamcloud - gitweb
b=14184
[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                 OBD_PAGE_ALLOC(pgp->pg, 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                 OBD_PAGE_FREE(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;
738                 OBD_PAGE_ALLOC(page, CFS_ALLOC_STD);
739                 if (page == NULL)
740                         GOTO(out, rc = -ENOMEM);
741
742                 OBD_ALLOC(eap, sizeof(*eap));
743                 if (eap == NULL) {
744                         OBD_PAGE_FREE(page);
745                         GOTO(out, rc = -ENOMEM);
746                 }
747
748                 eap->eap_magic = EAP_MAGIC;
749                 eap->eap_page = page;
750                 eap->eap_eas = &eas;
751                 list_add_tail(&eap->eap_item, &eas.eas_avail);
752                 aps[i] = eap;
753         }
754
755         /* first we spin queueing io and being woken by its completion */
756         spin_lock(&eas.eas_lock);
757         for(;;) {
758                 int rc;
759
760                 /* sleep until we have a page to send */
761                 spin_unlock(&eas.eas_lock);
762                 rc = wait_event_interruptible(eas.eas_waitq,
763                                               eas_should_wake(&eas));
764                 spin_lock(&eas.eas_lock);
765                 if (rc && !eas.eas_rc)
766                         eas.eas_rc = rc;
767                 if (eas.eas_rc)
768                         break;
769                 if (list_empty(&eas.eas_avail))
770                         continue;
771                 eap = list_entry(eas.eas_avail.next, struct echo_async_page,
772                                  eap_item);
773                 list_del(&eap->eap_item);
774                 spin_unlock(&eas.eas_lock);
775
776                 /* unbind the eap from its old page offset */
777                 if (eap->eap_cookie != NULL) {
778                         obd_teardown_async_page(exp, lsm, NULL,
779                                                 eap->eap_cookie);
780                         eap->eap_cookie = NULL;
781                 }
782
783                 eas.eas_next_offset += CFS_PAGE_SIZE;
784                 eap->eap_off = eas.eas_next_offset;
785
786                 rc = obd_prep_async_page(exp, lsm, NULL, eap->eap_page,
787                                          eap->eap_off, &ec_async_page_ops,
788                                          eap, &eap->eap_cookie);
789                 if (rc) {
790                         spin_lock(&eas.eas_lock);
791                         eas.eas_rc = rc;
792                         break;
793                 }
794
795                 if (oa->o_id != ECHO_PERSISTENT_OBJID &&
796                     (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
797                     (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0)
798                         echo_client_page_debug_setup(lsm, eap->eap_page, rw,
799                                                      oa->o_id,
800                                                      eap->eap_off, CFS_PAGE_SIZE);
801
802                 /* always asserts urgent, which isn't quite right */
803                 rc = obd_queue_async_io(exp, lsm, NULL, eap->eap_cookie,
804                                         rw, 0, CFS_PAGE_SIZE, 0,
805                                         ASYNC_READY | ASYNC_URGENT |
806                                         ASYNC_COUNT_STABLE);
807                 spin_lock(&eas.eas_lock);
808                 if (rc && !eas.eas_rc) {
809                         eas.eas_rc = rc;
810                         break;
811                 }
812                 eas.eas_in_flight++;
813                 if (eas.eas_next_offset == eas.eas_end_offset)
814                         break;
815         }
816
817         /* still hold the eas_lock here.. */
818
819         /* now we just spin waiting for all the rpcs to complete */
820         while(eas.eas_in_flight) {
821                 spin_unlock(&eas.eas_lock);
822                 wait_event_interruptible(eas.eas_waitq,
823                                          eas.eas_in_flight == 0);
824                 spin_lock(&eas.eas_lock);
825         }
826         spin_unlock(&eas.eas_lock);
827
828 out:
829         if (aps != NULL) {
830                 for (i = 0; i < npages; ++ i) {
831                         cfs_page_t *page;
832
833                         eap = aps[i];
834                         page = eap->eap_page;
835                         if (eap->eap_cookie != NULL)
836                                 obd_teardown_async_page(exp, lsm, NULL,
837                                                         eap->eap_cookie);
838                         OBD_FREE(eap, sizeof(*eap));
839                         OBD_PAGE_FREE(page);
840                 }
841                 OBD_FREE(aps, npages * sizeof aps[0]);
842         }
843
844         RETURN(rc);
845 }
846
847 static int echo_client_prep_commit(struct obd_export *exp, int rw,
848                                    struct obdo *oa, struct lov_stripe_md *lsm,
849                                    obd_off offset, obd_size count,
850                                    obd_size batch, struct obd_trans_info *oti)
851 {
852         struct obd_ioobj ioo;
853         struct niobuf_local *lnb;
854         struct niobuf_remote *rnb;
855         obd_off off;
856         obd_size npages, tot_pages;
857         int i, ret = 0;
858         ENTRY;
859
860         if (count <= 0 || (count & (~CFS_PAGE_MASK)) != 0 ||
861             (lsm != NULL && lsm->lsm_object_id != oa->o_id))
862                 RETURN(-EINVAL);
863
864         npages = batch >> CFS_PAGE_SHIFT;
865         tot_pages = count >> CFS_PAGE_SHIFT;
866
867         OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
868         OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
869
870         if (lnb == NULL || rnb == NULL)
871                 GOTO(out, ret = -ENOMEM);
872
873         obdo_to_ioobj(oa, &ioo);
874
875         off = offset;
876
877         for(; tot_pages; tot_pages -= npages) {
878                 if (tot_pages < npages)
879                         npages = tot_pages;
880
881                 for (i = 0; i < npages; i++, off += CFS_PAGE_SIZE) {
882                         rnb[i].offset = off;
883                         rnb[i].len = CFS_PAGE_SIZE;
884                 }
885
886                 ioo.ioo_bufcnt = npages;
887                 oti->oti_transno = 0;
888
889                 ret = obd_preprw(rw, exp, oa, 1, &ioo, npages, rnb, lnb, oti,
890                                  NULL);
891                 if (ret != 0)
892                         GOTO(out, ret);
893
894                 for (i = 0; i < npages; i++) {
895                         cfs_page_t *page = lnb[i].page;
896
897                         /* read past eof? */
898                         if (page == NULL && lnb[i].rc == 0)
899                                 continue;
900
901                         if (oa->o_id == ECHO_PERSISTENT_OBJID ||
902                             (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
903                             (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
904                                 continue;
905
906                         if (rw == OBD_BRW_WRITE)
907                                 echo_client_page_debug_setup(lsm, page, rw,
908                                                              oa->o_id,
909                                                              rnb[i].offset,
910                                                              rnb[i].len);
911                         else
912                                 echo_client_page_debug_check(lsm, page,
913                                                              oa->o_id,
914                                                              rnb[i].offset,
915                                                              rnb[i].len);
916                 }
917
918                 ret = obd_commitrw(rw, exp, oa, 1, &ioo, npages, lnb, oti, ret);
919                 if (ret != 0)
920                         GOTO(out, ret);
921         }
922
923 out:
924         if (lnb)
925                 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
926         if (rnb)
927                 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
928         RETURN(ret);
929 }
930
931 int echo_client_brw_ioctl(int rw, struct obd_export *exp,
932                           struct obd_ioctl_data *data)
933 {
934         struct obd_device *obd = class_exp2obd(exp);
935         struct echo_client_obd *ec = &obd->u.echo_client;
936         struct obd_trans_info dummy_oti = { .oti_thread_id = -1 };
937         struct ec_object *eco;
938         int rc;
939         ENTRY;
940
941         rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
942         if (rc)
943                 RETURN(rc);
944
945         data->ioc_obdo1.o_valid &= ~OBD_MD_FLHANDLE;
946         data->ioc_obdo1.o_valid |= OBD_MD_FLGROUP;
947         data->ioc_obdo1.o_gr = FILTER_GROUP_ECHO;
948
949         switch((long)data->ioc_pbuf1) {
950         case 1:
951                 if (data->ioc_pbuf2 == NULL) { // NULL user data pointer
952                         rc = echo_client_kbrw(obd, rw, &data->ioc_obdo1,
953                                               eco->eco_lsm, data->ioc_offset,
954                                               data->ioc_count, &dummy_oti);
955                 } else {
956 #ifdef __KERNEL__
957                         rc = echo_client_ubrw(obd, rw, &data->ioc_obdo1,
958                                               eco->eco_lsm, data->ioc_offset,
959                                               data->ioc_count, data->ioc_pbuf2,
960                                               &dummy_oti);
961 #endif
962                 }
963                 break;
964         case 2:
965                 rc = echo_client_async_page(ec->ec_exp, rw, &data->ioc_obdo1,
966                                            eco->eco_lsm, data->ioc_offset,
967                                            data->ioc_count, data->ioc_plen1);
968                 break;
969         case 3:
970                 rc = echo_client_prep_commit(ec->ec_exp, rw, &data->ioc_obdo1,
971                                             eco->eco_lsm, data->ioc_offset,
972                                             data->ioc_count, data->ioc_plen1,
973                                             &dummy_oti);
974                 break;
975         default:
976                 rc = -EINVAL;
977         }
978         echo_put_object(eco);
979         RETURN(rc);
980 }
981
982 static int
983 echo_ldlm_callback (struct ldlm_lock *lock, struct ldlm_lock_desc *new,
984                     void *data, int flag)
985 {
986         struct ec_object       *eco = (struct ec_object *)data;
987         struct echo_client_obd *ec = &(eco->eco_device->u.echo_client);
988         struct lustre_handle    lockh;
989         struct list_head       *el;
990         int                     found = 0;
991         int                     rc;
992
993         ldlm_lock2handle (lock, &lockh);
994
995         /* #ifdef this out if we're not feeling paranoid */
996         spin_lock (&ec->ec_lock);
997         list_for_each (el, &ec->ec_objects) {
998                 found = (eco == list_entry(el, struct ec_object,
999                                            eco_obj_chain));
1000                 if (found)
1001                         break;
1002         }
1003         spin_unlock (&ec->ec_lock);
1004         LASSERT (found);
1005
1006         switch (flag) {
1007         case LDLM_CB_BLOCKING:
1008                 CDEBUG(D_INFO, "blocking callback on "LPX64", handle "LPX64"\n",
1009                        eco->eco_id, lockh.cookie);
1010                 rc = ldlm_cli_cancel (&lockh);
1011                 if (rc != ELDLM_OK)
1012                         CERROR ("ldlm_cli_cancel failed: %d\n", rc);
1013                 break;
1014
1015         case LDLM_CB_CANCELING:
1016                 CDEBUG(D_INFO, "cancel callback on "LPX64", handle "LPX64"\n",
1017                        eco->eco_id, lockh.cookie);
1018                 break;
1019
1020         default:
1021                 LBUG ();
1022         }
1023
1024         return (0);
1025 }
1026
1027 static int
1028 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
1029                     int mode, obd_off offset, obd_size nob)
1030 {
1031         struct obd_device      *obd = exp->exp_obd;
1032         struct echo_client_obd *ec = &obd->u.echo_client;
1033         struct lustre_handle   *ulh = obdo_handle (oa);
1034         struct ldlm_enqueue_info einfo = { 0 };
1035         struct obd_info oinfo = { { { 0 } } };
1036         struct ec_object       *eco;
1037         struct ec_lock         *ecl;
1038         int                     rc;
1039
1040         if (!(mode == LCK_PR || mode == LCK_PW))
1041                 return -EINVAL;
1042
1043         if ((offset & (~CFS_PAGE_MASK)) != 0 ||
1044             (nob & (~CFS_PAGE_MASK)) != 0)
1045                 return -EINVAL;
1046
1047         rc = echo_get_object (&eco, obd, oa);
1048         if (rc != 0)
1049                 return rc;
1050
1051         rc = -ENOMEM;
1052         OBD_ALLOC (ecl, sizeof (*ecl));
1053         if (ecl == NULL)
1054                 goto failed_0;
1055
1056         ecl->ecl_mode = mode;
1057         ecl->ecl_object = eco;
1058         ecl->ecl_policy.l_extent.start = offset;
1059         ecl->ecl_policy.l_extent.end =
1060                 (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
1061
1062         einfo.ei_type = LDLM_EXTENT;
1063         einfo.ei_mode = mode;
1064         einfo.ei_cb_bl = echo_ldlm_callback;
1065         einfo.ei_cb_cp = ldlm_completion_ast;
1066         einfo.ei_cb_gl = NULL;
1067         einfo.ei_cbdata = eco;
1068
1069         oinfo.oi_policy = ecl->ecl_policy;
1070         oinfo.oi_lockh = &ecl->ecl_lock_handle;
1071         oinfo.oi_md = eco->eco_lsm;
1072         rc = obd_enqueue(ec->ec_exp, &oinfo, &einfo, NULL);
1073         if (rc != 0)
1074                 goto failed_1;
1075
1076         CDEBUG(D_INFO, "enqueue handle "LPX64"\n", ecl->ecl_lock_handle.cookie);
1077
1078         /* NB ecl takes object ref from echo_get_object() above */
1079         spin_lock(&ec->ec_lock);
1080
1081         list_add(&ecl->ecl_exp_chain, &exp->exp_ec_data.eced_locks);
1082         ulh->cookie = ecl->ecl_cookie = ec->ec_unique++;
1083
1084         spin_unlock(&ec->ec_lock);
1085
1086         oa->o_valid |= OBD_MD_FLHANDLE;
1087         return 0;
1088
1089  failed_1:
1090         OBD_FREE (ecl, sizeof (*ecl));
1091  failed_0:
1092         echo_put_object (eco);
1093         return (rc);
1094 }
1095
1096 static int
1097 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
1098 {
1099         struct obd_device      *obd = exp->exp_obd;
1100         struct echo_client_obd *ec = &obd->u.echo_client;
1101         struct lustre_handle   *ulh = obdo_handle (oa);
1102         struct ec_lock         *ecl = NULL;
1103         int                     found = 0;
1104         struct list_head       *el;
1105         int                     rc;
1106
1107         if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
1108                 return -EINVAL;
1109
1110         spin_lock (&ec->ec_lock);
1111
1112         list_for_each (el, &exp->exp_ec_data.eced_locks) {
1113                 ecl = list_entry (el, struct ec_lock, ecl_exp_chain);
1114                 found = (ecl->ecl_cookie == ulh->cookie);
1115                 if (found) {
1116                         list_del (&ecl->ecl_exp_chain);
1117                         break;
1118                 }
1119         }
1120
1121         spin_unlock (&ec->ec_lock);
1122
1123         if (!found)
1124                 return (-ENOENT);
1125
1126         rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm, ecl->ecl_mode,
1127                         &ecl->ecl_lock_handle);
1128
1129         echo_put_object (ecl->ecl_object);
1130         OBD_FREE (ecl, sizeof (*ecl));
1131
1132         return rc;
1133 }
1134
1135 static int
1136 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
1137                       int len, void *karg, void *uarg)
1138 {
1139         struct obd_device      *obd;
1140         struct echo_client_obd *ec;
1141         struct ec_object       *eco;
1142         struct obd_ioctl_data  *data = karg;
1143         struct obd_trans_info   dummy_oti;
1144         struct oti_req_ack_lock *ack_lock;
1145         struct obdo            *oa;
1146         int                     rw = OBD_BRW_READ;
1147         int                     rc = 0;
1148         int                     i;
1149         ENTRY;
1150
1151         unlock_kernel();
1152
1153         memset(&dummy_oti, 0, sizeof(dummy_oti));
1154
1155         obd = exp->exp_obd;
1156         ec = &obd->u.echo_client;
1157
1158         switch (cmd) {
1159         case OBD_IOC_CREATE:                    /* may create echo object */
1160                 if (!capable (CAP_SYS_ADMIN))
1161                         GOTO (out, rc = -EPERM);
1162
1163                 rc = echo_create_object (obd, 1, &data->ioc_obdo1,
1164                                          data->ioc_pbuf1, data->ioc_plen1,
1165                                          &dummy_oti);
1166                 GOTO(out, rc);
1167
1168         case OBD_IOC_DESTROY:
1169                 if (!capable (CAP_SYS_ADMIN))
1170                         GOTO (out, rc = -EPERM);
1171
1172                 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1173                 if (rc == 0) {
1174                         oa = &data->ioc_obdo1;
1175                         oa->o_gr = FILTER_GROUP_ECHO;
1176                         oa->o_valid |= OBD_MD_FLGROUP;
1177                         rc = obd_destroy(ec->ec_exp, oa, eco->eco_lsm,
1178                                          &dummy_oti, NULL);
1179                         if (rc == 0)
1180                                 eco->eco_deleted = 1;
1181                         echo_put_object(eco);
1182                 }
1183                 GOTO(out, rc);
1184
1185         case OBD_IOC_GETATTR:
1186                 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1187                 if (rc == 0) {
1188                         struct obd_info oinfo = { { { 0 } } };
1189                         oinfo.oi_md = eco->eco_lsm;
1190                         oinfo.oi_oa = &data->ioc_obdo1;
1191                         rc = obd_getattr(ec->ec_exp, &oinfo);
1192                         echo_put_object(eco);
1193                 }
1194                 GOTO(out, rc);
1195
1196         case OBD_IOC_SETATTR:
1197                 if (!capable (CAP_SYS_ADMIN))
1198                         GOTO (out, rc = -EPERM);
1199
1200                 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1201                 if (rc == 0) {
1202                         struct obd_info oinfo = { { { 0 } } };
1203                         oinfo.oi_oa = &data->ioc_obdo1;
1204                         oinfo.oi_md = eco->eco_lsm;
1205
1206                         rc = obd_setattr(ec->ec_exp, &oinfo, NULL);
1207                         echo_put_object(eco);
1208                 }
1209                 GOTO(out, rc);
1210
1211         case OBD_IOC_BRW_WRITE:
1212                 if (!capable (CAP_SYS_ADMIN))
1213                         GOTO (out, rc = -EPERM);
1214
1215                 rw = OBD_BRW_WRITE;
1216                 /* fall through */
1217         case OBD_IOC_BRW_READ:
1218                 rc = echo_client_brw_ioctl(rw, exp, data);
1219                 GOTO(out, rc);
1220
1221         case ECHO_IOC_GET_STRIPE:
1222                 rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
1223                 if (rc == 0) {
1224                         rc = echo_copyout_lsm(eco->eco_lsm, data->ioc_pbuf1,
1225                                               data->ioc_plen1);
1226                         echo_put_object(eco);
1227                 }
1228                 GOTO(out, rc);
1229
1230         case ECHO_IOC_SET_STRIPE:
1231                 if (!capable (CAP_SYS_ADMIN))
1232                         GOTO (out, rc = -EPERM);
1233
1234                 if (data->ioc_pbuf1 == NULL) {  /* unset */
1235                         rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
1236                         if (rc == 0) {
1237                                 eco->eco_deleted = 1;
1238                                 echo_put_object(eco);
1239                         }
1240                 } else {
1241                         rc = echo_create_object(obd, 0, &data->ioc_obdo1,
1242                                                 data->ioc_pbuf1,
1243                                                 data->ioc_plen1, &dummy_oti);
1244                 }
1245                 GOTO (out, rc);
1246
1247         case ECHO_IOC_ENQUEUE:
1248                 if (!capable (CAP_SYS_ADMIN))
1249                         GOTO (out, rc = -EPERM);
1250
1251                 rc = echo_client_enqueue(exp, &data->ioc_obdo1,
1252                                          data->ioc_conn1, /* lock mode */
1253                                    data->ioc_offset, data->ioc_count);/*extent*/
1254                 GOTO (out, rc);
1255
1256         case ECHO_IOC_CANCEL:
1257                 rc = echo_client_cancel(exp, &data->ioc_obdo1);
1258                 GOTO (out, rc);
1259
1260         default:
1261                 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
1262                 GOTO (out, rc = -ENOTTY);
1263         }
1264
1265         EXIT;
1266  out:
1267
1268         /* XXX this should be in a helper also called by target_send_reply */
1269         for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
1270              i++, ack_lock++) {
1271                 if (!ack_lock->mode)
1272                         break;
1273                 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
1274         }
1275
1276         lock_kernel();
1277
1278         return rc;
1279 }
1280
1281 static int echo_client_setup(struct obd_device *obddev, struct lustre_cfg *lcfg)
1282 {
1283         struct echo_client_obd *ec = &obddev->u.echo_client;
1284         struct obd_device *tgt;
1285         struct lustre_handle conn = {0, };
1286         struct obd_uuid echo_uuid = { "ECHO_UUID" };
1287         struct obd_connect_data *ocd = NULL;
1288         int rc;
1289         ENTRY;
1290
1291         if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1292                 CERROR("requires a TARGET OBD name\n");
1293                 RETURN(-EINVAL);
1294         }
1295
1296         tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
1297         if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
1298                 CERROR("device not attached or not set up (%s)\n",
1299                        lustre_cfg_string(lcfg, 1));
1300                 RETURN(-EINVAL);
1301         }
1302
1303         spin_lock_init (&ec->ec_lock);
1304         CFS_INIT_LIST_HEAD (&ec->ec_objects);
1305         ec->ec_unique = 0;
1306
1307         OBD_ALLOC(ocd, sizeof(*ocd));
1308         if (ocd == NULL) {
1309                 CERROR("Can't alloc ocd connecting to %s\n",
1310                        lustre_cfg_string(lcfg, 1));
1311                 return -ENOMEM;
1312         }
1313
1314         ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL;
1315         ocd->ocd_version = LUSTRE_VERSION_CODE;
1316         ocd->ocd_group = FILTER_GROUP_ECHO;
1317
1318         rc = obd_connect(NULL, &conn, tgt, &echo_uuid, ocd);
1319
1320         OBD_FREE(ocd, sizeof(*ocd));
1321
1322         if (rc != 0) {
1323                 CERROR("fail to connect to device %s\n",
1324                        lustre_cfg_string(lcfg, 1));
1325                 return (rc);
1326         }
1327         ec->ec_exp = class_conn2export(&conn);
1328
1329         RETURN(rc);
1330 }
1331
1332 static int echo_client_cleanup(struct obd_device *obddev)
1333 {
1334         struct list_head       *el;
1335         struct ec_object       *eco;
1336         struct echo_client_obd *ec = &obddev->u.echo_client;
1337         int rc;
1338         ENTRY;
1339
1340         if (!list_empty(&obddev->obd_exports)) {
1341                 CERROR("still has clients!\n");
1342                 RETURN(-EBUSY);
1343         }
1344
1345         /* XXX assuming sole access */
1346         while (!list_empty(&ec->ec_objects)) {
1347                 el = ec->ec_objects.next;
1348                 eco = list_entry(el, struct ec_object, eco_obj_chain);
1349
1350                 LASSERT(eco->eco_refcount == 0);
1351                 eco->eco_refcount = 1;
1352                 eco->eco_deleted = 1;
1353                 echo_put_object(eco);
1354         }
1355
1356         rc = obd_disconnect(ec->ec_exp);
1357         if (rc != 0)
1358                 CERROR("fail to disconnect device: %d\n", rc);
1359
1360         RETURN(rc);
1361 }
1362
1363 static int echo_client_connect(const struct lu_env *env,
1364                                struct lustre_handle *conn,
1365                                struct obd_device *src, struct obd_uuid *cluuid,
1366                                struct obd_connect_data *data)
1367 {
1368         struct obd_export *exp;
1369         int                rc;
1370
1371         ENTRY;
1372         rc = class_connect(conn, src, cluuid);
1373         if (rc == 0) {
1374                 exp = class_conn2export(conn);
1375                 CFS_INIT_LIST_HEAD(&exp->exp_ec_data.eced_locks);
1376                 class_export_put(exp);
1377         }
1378
1379         RETURN (rc);
1380 }
1381
1382 static int echo_client_disconnect(struct obd_export *exp)
1383 {
1384         struct obd_device      *obd;
1385         struct echo_client_obd *ec;
1386         struct ec_lock         *ecl;
1387         int                     rc;
1388         ENTRY;
1389
1390         if (exp == NULL)
1391                 GOTO(out, rc = -EINVAL);
1392
1393         obd = exp->exp_obd;
1394         ec = &obd->u.echo_client;
1395
1396         /* no more contention on export's lock list */
1397         while (!list_empty (&exp->exp_ec_data.eced_locks)) {
1398                 ecl = list_entry (exp->exp_ec_data.eced_locks.next,
1399                                   struct ec_lock, ecl_exp_chain);
1400                 list_del (&ecl->ecl_exp_chain);
1401
1402                 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm,
1403                                  ecl->ecl_mode, &ecl->ecl_lock_handle);
1404
1405                 CDEBUG (D_INFO, "Cancel lock on object "LPX64" on disconnect "
1406                         "(%d)\n", ecl->ecl_object->eco_id, rc);
1407
1408                 echo_put_object (ecl->ecl_object);
1409                 OBD_FREE (ecl, sizeof (*ecl));
1410         }
1411
1412         rc = class_disconnect(exp);
1413         GOTO(out, rc);
1414  out:
1415         return rc;
1416 }
1417
1418 static struct obd_ops echo_obd_ops = {
1419         .o_owner       = THIS_MODULE,
1420         .o_setup       = echo_client_setup,
1421         .o_cleanup     = echo_client_cleanup,
1422         .o_iocontrol   = echo_client_iocontrol,
1423         .o_connect     = echo_client_connect,
1424         .o_disconnect  = echo_client_disconnect
1425 };
1426
1427 int echo_client_init(void)
1428 {
1429         struct lprocfs_static_vars lvars = { 0 };
1430
1431         lprocfs_echo_init_vars(&lvars);
1432         return class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
1433                                    LUSTRE_ECHO_CLIENT_NAME, NULL);
1434 }
1435
1436 void echo_client_exit(void)
1437 {
1438         class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
1439 }