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