Whamcloud - gitweb
3fcfdbc0bfb8aef82e4493eb47f97a29065243e9
[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 <lustre_debug.h>
48 #include <lprocfs_status.h>
49 #include <cl_object.h>
50
51 #include "echo_internal.h"
52
53 /** \defgroup echo_client Echo Client
54  * @{
55  */
56
57 struct echo_device {
58         struct cl_device        ed_cl;
59         struct echo_client_obd *ed_ec;
60
61         struct cl_site          ed_site_myself;
62         struct cl_site         *ed_site;
63         struct lu_device       *ed_next;
64         int                     ed_next_islov;
65 };
66
67 struct echo_object {
68         struct cl_object        eo_cl;
69         struct cl_object_header eo_hdr;
70
71         struct echo_device     *eo_dev;
72         struct list_head        eo_obj_chain;
73         struct lov_stripe_md   *eo_lsm;
74         atomic_t                eo_npages;
75         int                     eo_deleted;
76 };
77
78 struct echo_object_conf {
79         struct cl_object_conf  eoc_cl;
80         struct lov_stripe_md **eoc_md;
81 };
82
83 struct echo_page {
84         struct cl_page_slice   ep_cl;
85         cfs_page_t            *ep_vmpage;
86 };
87
88 struct echo_lock {
89         struct cl_lock_slice   el_cl;
90         struct list_head       el_chain;
91         struct echo_object    *el_object;
92         __u64                  el_cookie;
93 };
94
95 struct echo_io {
96         struct cl_io_slice     ei_cl;
97 };
98
99 #if 0
100 struct echo_req {
101         struct cl_req_slice er_cl;
102 };
103 #endif
104
105 static int echo_client_setup(struct obd_device *obddev,
106                              struct lustre_cfg *lcfg);
107 static int echo_client_cleanup(struct obd_device *obddev);
108
109
110 /** \defgroup echo_helpers Helper functions
111  * @{
112  */
113 static inline struct echo_device *cl2echo_dev(const struct cl_device *dev)
114 {
115         return container_of0(dev, struct echo_device, ed_cl);
116 }
117
118 static inline struct cl_device *echo_dev2cl(struct echo_device *d)
119 {
120         return &d->ed_cl;
121 }
122
123 static inline struct echo_device *obd2echo_dev(const struct obd_device *obd)
124 {
125         return cl2echo_dev(lu2cl_dev(obd->obd_lu_dev));
126 }
127
128 static inline struct cl_object *echo_obj2cl(struct echo_object *eco)
129 {
130         return &eco->eo_cl;
131 }
132
133 static inline struct echo_object *cl2echo_obj(const struct cl_object *o)
134 {
135         return container_of(o, struct echo_object, eo_cl);
136 }
137
138 static inline struct echo_page *cl2echo_page(const struct cl_page_slice *s)
139 {
140         return container_of(s, struct echo_page, ep_cl);
141 }
142
143 static inline struct echo_lock *cl2echo_lock(const struct cl_lock_slice *s)
144 {
145         return container_of(s, struct echo_lock, el_cl);
146 }
147
148 static inline struct cl_lock *echo_lock2cl(const struct echo_lock *ecl)
149 {
150         return ecl->el_cl.cls_lock;
151 }
152
153 static struct lu_context_key echo_thread_key;
154 static inline struct echo_thread_info *echo_env_info(const struct lu_env *env)
155 {
156         struct echo_thread_info *info;
157         info = lu_context_key_get(&env->le_ctx, &echo_thread_key);
158         LASSERT(info != NULL);
159         return info;
160 }
161
162 static inline
163 struct echo_object_conf *cl2echo_conf(const struct cl_object_conf *c)
164 {
165         return container_of(c, struct echo_object_conf, eoc_cl);
166 }
167
168 static inline void lsm2fid(struct lov_stripe_md *lsm, struct lu_fid *fid)
169 {
170         fid_zero(fid);
171         fid->f_seq = lsm->lsm_object_gr << 16 | lsm->lsm_object_id >> 32;
172         fid->f_oid = lsm->lsm_object_id;
173 }
174 /** @} echo_helpers */
175
176 static struct echo_object *cl_echo_object_find(struct echo_device *d,
177                                                struct lov_stripe_md **lsm);
178 static int cl_echo_object_put(struct echo_object *eco);
179 static int cl_echo_enqueue   (struct echo_object *eco, obd_off start,
180                               obd_off end, int mode, __u64 *cookie);
181 static int cl_echo_cancel    (struct echo_device *d, __u64 cookie);
182 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
183                               cfs_page_t **pages, int npages, int async);
184
185 static struct echo_thread_info *echo_env_info(const struct lu_env *env);
186
187 struct echo_thread_info {
188         struct echo_object_conf eti_conf;
189         struct lustre_md        eti_md;
190
191         struct cl_2queue        eti_queue;
192         struct cl_io            eti_io;
193         struct cl_lock_descr    eti_descr;
194         struct lu_fid           eti_fid;
195 };
196
197 /* No session used right now */
198 struct echo_session_info {
199         unsigned long dummy;
200 };
201
202 static cfs_mem_cache_t *echo_page_kmem;
203 static cfs_mem_cache_t *echo_lock_kmem;
204 static cfs_mem_cache_t *echo_object_kmem;
205 static cfs_mem_cache_t *echo_thread_kmem;
206 static cfs_mem_cache_t *echo_session_kmem;
207 //static cfs_mem_cache_t *echo_req_kmem;
208
209 static struct lu_kmem_descr echo_caches[] = {
210         {
211                 .ckd_cache = &echo_page_kmem,
212                 .ckd_name  = "echo_page_kmem",
213                 .ckd_size  = sizeof (struct echo_page)
214         },
215         {
216                 .ckd_cache = &echo_lock_kmem,
217                 .ckd_name  = "echo_lock_kmem",
218                 .ckd_size  = sizeof (struct echo_lock)
219         },
220         {
221                 .ckd_cache = &echo_object_kmem,
222                 .ckd_name  = "echo_object_kmem",
223                 .ckd_size  = sizeof (struct echo_object)
224         },
225         {
226                 .ckd_cache = &echo_thread_kmem,
227                 .ckd_name  = "echo_thread_kmem",
228                 .ckd_size  = sizeof (struct echo_thread_info)
229         },
230         {
231                 .ckd_cache = &echo_session_kmem,
232                 .ckd_name  = "echo_session_kmem",
233                 .ckd_size  = sizeof (struct echo_session_info)
234         },
235 #if 0
236         {
237                 .ckd_cache = &echo_req_kmem,
238                 .ckd_name  = "echo_req_kmem",
239                 .ckd_size  = sizeof (struct echo_req)
240         },
241 #endif
242         {
243                 .ckd_cache = NULL
244         }
245 };
246
247 /** \defgroup echo_page Page operations
248  *
249  * Echo page operations.
250  *
251  * @{
252  */
253 cfs_page_t *echo_page_vmpage(const struct lu_env *env,
254                              const struct cl_page_slice *slice)
255 {
256         return cl2echo_page(slice)->ep_vmpage;
257 }
258
259 static void echo_page_discard(const struct lu_env *env,
260                               const struct cl_page_slice *slice,
261                               struct cl_io *unused)
262 {
263         cl_page_delete(env, slice->cpl_page);
264 }
265
266 static int echo_page_is_vmlocked(const struct lu_env *env,
267                                  const struct cl_page_slice *slice)
268 {
269         return 1;
270 }
271
272 static void echo_page_completion(const struct lu_env *env,
273                                  const struct cl_page_slice *slice,
274                                  int ioret)
275 {
276         LASSERT(slice->cpl_page->cp_sync_io != NULL);
277 }
278
279 static void echo_page_fini(const struct lu_env *env,
280                            struct cl_page_slice *slice)
281 {
282         struct echo_page *ep    = cl2echo_page(slice);
283         struct echo_object *eco = cl2echo_obj(slice->cpl_obj);
284         cfs_page_t *vmpage      = ep->ep_vmpage;
285         ENTRY;
286
287         atomic_dec(&eco->eo_npages);
288         page_cache_release(vmpage);
289         OBD_SLAB_FREE_PTR(ep, echo_page_kmem);
290         EXIT;
291 }
292
293 static int echo_page_prep(const struct lu_env *env,
294                           const struct cl_page_slice *slice,
295                           struct cl_io *unused)
296 {
297         return 0;
298 }
299
300 static int echo_page_print(const struct lu_env *env,
301                            const struct cl_page_slice *slice,
302                            void *cookie, lu_printer_t printer)
303 {
304         struct echo_page *ep = cl2echo_page(slice);
305
306         (*printer)(env, cookie, LUSTRE_ECHO_CLIENT_NAME"-page@%p vm@%p\n",
307                    ep, ep->ep_vmpage);
308         return 0;
309 }
310
311 static const struct cl_page_operations echo_page_ops = {
312         .cpo_discard       = echo_page_discard,
313         .cpo_vmpage        = echo_page_vmpage,
314         .cpo_fini          = echo_page_fini,
315         .cpo_print         = echo_page_print,
316         .cpo_is_vmlocked   = echo_page_is_vmlocked,
317         .io = {
318                 [CRT_READ] = {
319                         .cpo_prep        = echo_page_prep,
320                         .cpo_completion  = echo_page_completion,
321                 },
322                 [CRT_WRITE] = {
323                         .cpo_prep        = echo_page_prep,
324                         .cpo_completion  = echo_page_completion,
325                 }
326         }
327 };
328 /** @} echo_page */
329
330 /** \defgroup echo_lock Locking
331  *
332  * echo lock operations
333  *
334  * @{
335  */
336 static void echo_lock_fini(const struct lu_env *env,
337                            struct cl_lock_slice *slice)
338 {
339         struct echo_lock *ecl = cl2echo_lock(slice);
340
341         LASSERT(list_empty(&ecl->el_chain));
342         OBD_SLAB_FREE_PTR(ecl, echo_lock_kmem);
343 }
344
345 static void echo_lock_delete(const struct lu_env *env,
346                              const struct cl_lock_slice *slice)
347 {
348         struct echo_lock *ecl      = cl2echo_lock(slice);
349
350         LASSERT(list_empty(&ecl->el_chain));
351 }
352
353 static int echo_lock_fits_into(const struct lu_env *env,
354                                const struct cl_lock_slice *slice,
355                                const struct cl_lock_descr *need,
356                                const struct cl_io *unused)
357 {
358         return 1;
359 }
360
361 static struct cl_lock_operations echo_lock_ops = {
362         .clo_fini      = echo_lock_fini,
363         .clo_delete    = echo_lock_delete,
364         .clo_fits_into = echo_lock_fits_into
365 };
366
367 /** @} echo_lock */
368
369 /** \defgroup echo_cl_ops cl_object operations
370  *
371  * operations for cl_object
372  *
373  * @{
374  */
375 static struct cl_page *echo_page_init(const struct lu_env *env,
376                                       struct cl_object *obj,
377                                       struct cl_page *page, cfs_page_t *vmpage)
378 {
379         struct echo_page *ep;
380         ENTRY;
381
382         OBD_SLAB_ALLOC_PTR_GFP(ep, echo_page_kmem, CFS_ALLOC_IO);
383         if (ep != NULL) {
384                 struct echo_object *eco = cl2echo_obj(obj);
385                 ep->ep_vmpage = vmpage;
386                 page_cache_get(vmpage);
387                 cl_page_slice_add(page, &ep->ep_cl, obj, &echo_page_ops);
388                 atomic_inc(&eco->eo_npages);
389         }
390         RETURN(ERR_PTR(ep ? 0 : -ENOMEM));
391 }
392
393 static int echo_io_init(const struct lu_env *env, struct cl_object *obj,
394                         struct cl_io *io)
395 {
396         return 0;
397 }
398
399 static int echo_lock_init(const struct lu_env *env,
400                           struct cl_object *obj, struct cl_lock *lock,
401                           const struct cl_io *unused)
402 {
403         struct echo_lock *el;
404         ENTRY;
405
406         OBD_SLAB_ALLOC_PTR_GFP(el, echo_lock_kmem, CFS_ALLOC_IO);
407         if (el != NULL) {
408                 cl_lock_slice_add(lock, &el->el_cl, obj, &echo_lock_ops);
409                 el->el_object = cl2echo_obj(obj);
410                 CFS_INIT_LIST_HEAD(&el->el_chain);
411         }
412         RETURN(el == NULL ? -ENOMEM : 0);
413 }
414
415 static int echo_conf_set(const struct lu_env *env, struct cl_object *obj,
416                          const struct cl_object_conf *conf)
417 {
418         return 0;
419 }
420
421 static const struct cl_object_operations echo_cl_obj_ops = {
422         .coo_page_init = echo_page_init,
423         .coo_lock_init = echo_lock_init,
424         .coo_io_init   = echo_io_init,
425         .coo_conf_set  = echo_conf_set
426 };
427 /** @} echo_cl_ops */
428
429 /** \defgroup echo_lu_ops lu_object operations
430  *
431  * operations for echo lu object.
432  *
433  * @{
434  */
435 static int echo_object_init(const struct lu_env *env, struct lu_object *obj,
436                             const struct lu_object_conf *conf)
437 {
438         const struct cl_object_conf *cconf = lu2cl_conf(conf);
439         struct echo_object_conf *econf = cl2echo_conf(cconf);
440         struct echo_device *ed         = cl2echo_dev(lu2cl_dev(obj->lo_dev));
441         struct echo_client_obd *ec     = ed->ed_ec;
442         struct echo_object *eco        = cl2echo_obj(lu2cl(obj));
443         ENTRY;
444
445         if (ed->ed_next) {
446                 struct lu_object  *below;
447                 struct lu_device  *under;
448
449                 under = ed->ed_next;
450                 below = under->ld_ops->ldo_object_alloc(env, obj->lo_header,
451                                                         under);
452                 if (below == NULL)
453                         RETURN(-ENOMEM);
454                 lu_object_add(obj, below);
455         }
456
457         LASSERT(econf->eoc_md);
458         eco->eo_lsm = *econf->eoc_md;
459         eco->eo_dev = ed;
460         atomic_set(&eco->eo_npages, 0);
461
462         /* clear the lsm pointer so that it won't get freed. */
463         *econf->eoc_md = NULL;
464
465         spin_lock(&ec->ec_lock);
466         list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
467         spin_unlock(&ec->ec_lock);
468
469         RETURN(0);
470 }
471
472 static void echo_object_free(const struct lu_env *env, struct lu_object *obj)
473 {
474         struct echo_object *eco    = cl2echo_obj(lu2cl(obj));
475         struct echo_client_obd *ec = eco->eo_dev->ed_ec;
476         struct lov_stripe_md *lsm  = eco->eo_lsm;
477         ENTRY;
478
479         LASSERT(atomic_read(&eco->eo_npages) == 0);
480
481         spin_lock(&ec->ec_lock);
482         list_del_init(&eco->eo_obj_chain);
483         spin_unlock(&ec->ec_lock);
484
485         lu_object_fini(obj);
486         lu_object_header_fini(obj->lo_header);
487
488         if (lsm)
489                 obd_free_memmd(ec->ec_exp, &lsm);
490         OBD_SLAB_FREE_PTR(eco, echo_object_kmem);
491         EXIT;
492 }
493
494 static int echo_object_print(const struct lu_env *env, void *cookie,
495                             lu_printer_t p, const struct lu_object *o)
496 {
497         struct echo_object *obj = cl2echo_obj(lu2cl(o));
498
499         return (*p)(env, cookie, "echoclient-object@%p", obj);
500 }
501
502
503 static const struct lu_object_operations echo_lu_obj_ops = {
504         .loo_object_init      = echo_object_init,
505         .loo_object_delete    = NULL,
506         .loo_object_release   = NULL,
507         .loo_object_free      = echo_object_free,
508         .loo_object_print     = echo_object_print,
509         .loo_object_invariant = NULL
510 };
511 /** @} echo_lu_ops */
512
513 /** \defgroup echo_lu_dev_ops  lu_device operations
514  *
515  * Operations for echo lu device.
516  *
517  * @{
518  */
519 static struct lu_object *echo_object_alloc(const struct lu_env *env,
520                                            const struct lu_object_header *hdr,
521                                            struct lu_device *dev)
522 {
523         struct echo_object *eco;
524         struct lu_object *obj = NULL;
525         ENTRY;
526
527         /* we're the top dev. */
528         LASSERT(hdr == NULL);
529         OBD_SLAB_ALLOC_PTR_GFP(eco, echo_object_kmem, CFS_ALLOC_IO);
530         if (eco != NULL) {
531                 struct cl_object_header *hdr = &eco->eo_hdr;
532
533                 obj = &echo_obj2cl(eco)->co_lu;
534                 cl_object_header_init(hdr);
535                 lu_object_init(obj, &hdr->coh_lu, dev);
536                 lu_object_add_top(&hdr->coh_lu, obj);
537
538                 eco->eo_cl.co_ops = &echo_cl_obj_ops;
539                 obj->lo_ops       = &echo_lu_obj_ops;
540         }
541         RETURN(obj);
542 }
543
544 static struct lu_device_operations echo_device_lu_ops = {
545         .ldo_object_alloc   = echo_object_alloc,
546 };
547 /** @} echo_lu_dev_ops */
548
549 static struct cl_device_operations echo_device_cl_ops = {
550 };
551
552 /** \defgroup echo_init Setup and teardown
553  *
554  * Init and fini functions for echo client.
555  *
556  * @{
557  */
558 static int echo_site_init(const struct lu_env *env, struct echo_device *ed)
559 {
560         struct cl_site *site = &ed->ed_site_myself;
561         int rc;
562
563         /* initialize site */
564         rc = cl_site_init(site, &ed->ed_cl);
565         if (rc) {
566                 CERROR("Cannot initilize site for echo client(%d)\n", rc);
567                 return rc;
568         }
569
570         rc = lu_site_init_finish(&site->cs_lu);
571         if (rc)
572                 return rc;
573
574         ed->ed_site = site;
575         return 0;
576 }
577
578 static void echo_site_fini(const struct lu_env *env, struct echo_device *ed)
579 {
580         if (ed->ed_site) {
581                 cl_site_fini(ed->ed_site);
582                 ed->ed_site = NULL;
583         }
584 }
585
586 static void *echo_thread_key_init(const struct lu_context *ctx,
587                           struct lu_context_key *key)
588 {
589         struct echo_thread_info *info;
590
591         OBD_SLAB_ALLOC_PTR_GFP(info, echo_thread_kmem, CFS_ALLOC_IO);
592         if (info == NULL)
593                 info = ERR_PTR(-ENOMEM);
594         return info;
595 }
596
597 static void echo_thread_key_fini(const struct lu_context *ctx,
598                          struct lu_context_key *key, void *data)
599 {
600         struct echo_thread_info *info = data;
601         OBD_SLAB_FREE_PTR(info, echo_thread_kmem);
602 }
603
604 static void echo_thread_key_exit(const struct lu_context *ctx,
605                          struct lu_context_key *key, void *data)
606 {
607 }
608
609 static struct lu_context_key echo_thread_key = {
610         .lct_tags = LCT_CL_THREAD,
611         .lct_init = echo_thread_key_init,
612         .lct_fini = echo_thread_key_fini,
613         .lct_exit = echo_thread_key_exit
614 };
615
616 static void *echo_session_key_init(const struct lu_context *ctx,
617                                   struct lu_context_key *key)
618 {
619         struct echo_session_info *session;
620
621         OBD_SLAB_ALLOC_PTR_GFP(session, echo_session_kmem, CFS_ALLOC_IO);
622         if (session == NULL)
623                 session = ERR_PTR(-ENOMEM);
624         return session;
625 }
626
627 static void echo_session_key_fini(const struct lu_context *ctx,
628                                  struct lu_context_key *key, void *data)
629 {
630         struct echo_session_info *session = data;
631         OBD_SLAB_FREE_PTR(session, echo_session_kmem);
632 }
633
634 static void echo_session_key_exit(const struct lu_context *ctx,
635                                  struct lu_context_key *key, void *data)
636 {
637 }
638
639 static struct lu_context_key echo_session_key = {
640         .lct_tags = LCT_SESSION,
641         .lct_init = echo_session_key_init,
642         .lct_fini = echo_session_key_fini,
643         .lct_exit = echo_session_key_exit
644 };
645
646 LU_TYPE_INIT_FINI(echo, &echo_thread_key, &echo_session_key);
647
648 static struct lu_device *echo_device_alloc(const struct lu_env *env,
649                                            struct lu_device_type *t,
650                                            struct lustre_cfg *cfg)
651 {
652         struct lu_device   *next;
653         struct echo_device *ed;
654         struct cl_device   *cd;
655         struct obd_device  *obd = NULL; /* to keep compiler happy */
656         struct obd_device  *tgt;
657         const char *tgt_type_name;
658         int rc;
659         int cleanup = 0;
660         ENTRY;
661
662         OBD_ALLOC_PTR(ed);
663         if (ed == NULL)
664                 GOTO(out, rc = -ENOMEM);
665
666         cleanup = 1;
667         cd = &ed->ed_cl;
668         rc = cl_device_init(cd, t);
669         if (rc)
670                 GOTO(out, rc);
671
672         cd->cd_lu_dev.ld_ops = &echo_device_lu_ops;
673         cd->cd_ops = &echo_device_cl_ops;
674
675         cleanup = 2;
676         rc = echo_site_init(env, ed);
677         if (rc)
678                 GOTO(out, rc);
679
680         cleanup = 3;
681         obd = class_name2obd(lustre_cfg_string(cfg, 0));
682         LASSERT(obd != NULL);
683         rc = echo_client_setup(obd, cfg);
684         if (rc)
685                 GOTO(out, rc);
686         ed->ed_ec = &obd->u.echo_client;
687
688         cleanup = 4;
689         tgt = class_name2obd(lustre_cfg_string(cfg, 1));
690         LASSERT(tgt != NULL);
691         next = tgt->obd_lu_dev;
692         if (next != NULL && !lu_device_is_cl(next))
693                 next = NULL;
694
695         /*
696          * if echo client is to be stacked upon ost device, the next is NULL
697          * since ost is not a clio device so far
698          */
699         tgt_type_name = tgt->obd_type->typ_name;
700         if (next != NULL) {
701                 LASSERT(next != NULL);
702                 if (next->ld_site != NULL)
703                         GOTO(out, rc = -EBUSY);
704
705                 next->ld_site = &ed->ed_site->cs_lu;
706                 rc = next->ld_type->ldt_ops->ldto_device_init(env, next,
707                                              next->ld_type->ldt_name, NULL);
708                 if (rc)
709                         GOTO(out, rc);
710
711                 /* Trikcy case, I have to determine the obd type since clio
712                  * uses the different parameters to initialize objects for
713                  * lov & osc.
714                  */
715                 if (strcmp(tgt_type_name, LUSTRE_LOV_NAME) == 0)
716                         ed->ed_next_islov = 1;
717                 else
718                         LASSERT(strcmp(tgt_type_name, LUSTRE_OSC_NAME) == 0);
719         } else
720                 LASSERT(strcmp(tgt_type_name, LUSTRE_OST_NAME) == 0);
721
722         ed->ed_next = next;
723         RETURN(&cd->cd_lu_dev);
724
725 out:
726         switch(cleanup) {
727         case 4: {
728                 int rc2;
729                 rc2 = echo_client_cleanup(obd);
730                 if (rc2)
731                         CERROR("Cleanup obd device %s error(%d)\n",
732                                obd->obd_name, rc2);
733         }
734
735         case 3:
736                 echo_site_fini(env, ed);
737         case 2:
738                 cl_device_fini(&ed->ed_cl);
739         case 1:
740                 OBD_FREE_PTR(ed);
741         case 0:
742         default:
743                 break;
744         }
745         return(ERR_PTR(rc));
746 }
747
748 static int echo_device_init(const struct lu_env *env, struct lu_device *d,
749                           const char *name, struct lu_device *next)
750 {
751         LBUG();
752         return 0;
753 }
754
755 static struct lu_device *echo_device_fini(const struct lu_env *env,
756                                           struct lu_device *d)
757 {
758         struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
759         struct lu_device *next = ed->ed_next;
760
761         while (next)
762                 next = next->ld_type->ldt_ops->ldto_device_fini(env, next);
763         return NULL;
764 }
765
766 static struct lu_device *echo_device_free(const struct lu_env *env,
767                                           struct lu_device *d)
768 {
769         struct echo_device     *ed   = cl2echo_dev(lu2cl_dev(d));
770         struct echo_client_obd *ec   = ed->ed_ec;
771         struct echo_object     *eco;
772         struct lu_device       *next = ed->ed_next;
773
774         CDEBUG(D_INFO, "echo device:%p is going to be freed, next = %p\n", ed, next);
775
776         /* destroy locks */
777         spin_lock(&ec->ec_lock);
778         while (!list_empty(&ec->ec_locks)) {
779                 struct echo_lock *ecl = list_entry(ec->ec_locks.next,
780                                                    struct echo_lock, el_chain);
781                 struct cl_lock *lock  = echo_lock2cl(ecl);
782
783                 list_del_init(&ecl->el_chain);
784                 spin_unlock(&ec->ec_lock);
785
786                 CERROR("echo client: pending lock %p\n", ecl);
787
788                 cl_lock_get(lock);
789                 cl_unuse(env, lock);
790                 cl_lock_release(env, lock, "ec enqueue", ecl->el_object);
791
792                 cl_lock_mutex_get(env, lock);
793                 cl_lock_cancel(env, lock);
794                 cl_lock_delete(env, lock);
795                 cl_lock_mutex_put(env, lock);
796                 cl_lock_put(env, lock);
797
798                 spin_lock(&ec->ec_lock);
799         }
800         spin_unlock(&ec->ec_lock);
801
802         LASSERT(ed->ed_site);
803         lu_site_purge(env, &ed->ed_site->cs_lu, -1);
804
805         /* check if there are objects still alive.
806          * It shouldn't have any object because lu_site_purge would cleanup
807          * all of cached objects. Anyway, probably the echo device is being
808          * parallelly accessed.
809          */
810         spin_lock(&ec->ec_lock);
811         list_for_each_entry(eco, &ec->ec_objects, eo_obj_chain)
812                 eco->eo_deleted = 1;
813         spin_unlock(&ec->ec_lock);
814
815         /* purge again */
816         lu_site_purge(env, &ed->ed_site->cs_lu, -1);
817
818         CDEBUG(D_INFO, "Waiting for the reference of echo object to be dropped\n");
819
820         /* Wait for the last reference to be dropped. */
821         spin_lock(&ec->ec_lock);
822         while (!list_empty(&ec->ec_objects)) {
823                 spin_unlock(&ec->ec_lock);
824                 CERROR("echo_client still has objects at cleanup time, "
825                        "wait for 1 second\n");
826                 cfs_schedule_timeout(CFS_TASK_UNINT, cfs_time_seconds(1));
827                 spin_lock(&ec->ec_lock);
828         }
829         spin_unlock(&ec->ec_lock);
830
831         CDEBUG(D_INFO, "No object exists, exiting...\n");
832
833         echo_client_cleanup(d->ld_obd);
834
835         while (next)
836                 next = next->ld_type->ldt_ops->ldto_device_free(env, next);
837
838         LASSERT(ed->ed_site == lu2cl_site(d->ld_site));
839         echo_site_fini(env, ed);
840         cl_device_fini(&ed->ed_cl);
841         OBD_FREE_PTR(ed);
842
843         return NULL;
844 }
845
846 static const struct lu_device_type_operations echo_device_type_ops = {
847         .ldto_init = echo_type_init,
848         .ldto_fini = echo_type_fini,
849
850         .ldto_start = echo_type_start,
851         .ldto_stop  = echo_type_stop,
852
853         .ldto_device_alloc = echo_device_alloc,
854         .ldto_device_free  = echo_device_free,
855         .ldto_device_init  = echo_device_init,
856         .ldto_device_fini  = echo_device_fini
857 };
858
859 static struct lu_device_type echo_device_type = {
860         .ldt_tags     = LU_DEVICE_CL,
861         .ldt_name     = LUSTRE_ECHO_CLIENT_NAME,
862         .ldt_ops      = &echo_device_type_ops,
863         .ldt_ctx_tags = LCT_CL_THREAD
864 };
865 /** @} echo_init */
866
867 /** \defgroup echo_exports Exported operations
868  *
869  * exporting functions to echo client
870  *
871  * @{
872  */
873
874 /* Interfaces to echo client obd device */
875 static struct echo_object *cl_echo_object_find(struct echo_device *d,
876                                                struct lov_stripe_md **lsmp)
877 {
878         struct lu_env *env;
879         struct echo_thread_info *info;
880         struct echo_object_conf *conf;
881         struct lov_stripe_md    *lsm;
882         struct echo_object *eco;
883         struct cl_object   *obj;
884         struct lu_fid *fid;
885         int refcheck;
886         ENTRY;
887
888         LASSERT(lsmp);
889         lsm = *lsmp;
890         LASSERT(lsm);
891         LASSERT(lsm->lsm_object_id);
892
893         /* Never return an object if the obd is to be freed. */
894         if (echo_dev2cl(d)->cd_lu_dev.ld_obd->obd_stopping)
895                 RETURN(ERR_PTR(-ENODEV));
896
897         env = cl_env_get(&refcheck);
898         if (IS_ERR(env))
899                 RETURN((void *)env);
900
901         info = echo_env_info(env);
902         conf = &info->eti_conf;
903         if (d->ed_next) {
904                 if (!d->ed_next_islov) {
905                         struct lov_oinfo *oinfo = lsm->lsm_oinfo[0];
906                         LASSERT(oinfo != NULL);
907                         oinfo->loi_id = lsm->lsm_object_id;
908                         oinfo->loi_gr = lsm->lsm_object_gr;
909                         conf->eoc_cl.u.coc_oinfo = oinfo;
910                 } else {
911                         struct lustre_md *md;
912                         md = &info->eti_md;
913                         memset(md, 0, sizeof *md);
914                         md->lsm = lsm;
915                         conf->eoc_cl.u.coc_md = md;
916                 }
917         }
918         conf->eoc_md = lsmp;
919
920         fid  = &info->eti_fid;
921         lsm2fid(lsm, fid);
922
923         obj = cl_object_find(env, echo_dev2cl(d), fid, &conf->eoc_cl);
924         if (IS_ERR(obj))
925                 GOTO(out, eco = (void*)obj);
926
927         eco = cl2echo_obj(obj);
928         if (eco->eo_deleted) {
929                 cl_object_put(env, obj);
930                 eco = ERR_PTR(-EAGAIN);
931         }
932
933 out:
934         cl_env_put(env, &refcheck);
935         RETURN(eco);
936 }
937
938 static int cl_echo_object_put(struct echo_object *eco)
939 {
940         struct lu_env *env;
941         struct cl_object *obj = echo_obj2cl(eco);
942         int refcheck;
943         ENTRY;
944
945         env = cl_env_get(&refcheck);
946         if (IS_ERR(env))
947                 RETURN(PTR_ERR(env));
948
949         /* an external function to kill an object? */
950         if (eco->eo_deleted) {
951                 struct lu_object_header *loh = obj->co_lu.lo_header;
952                 LASSERT(&eco->eo_hdr == luh2coh(loh));
953                 set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
954                 cl_object_prune(env, obj);
955         }
956
957         cl_object_put(env, obj);
958         cl_env_put(env, &refcheck);
959         RETURN(0);
960 }
961
962 static int cl_echo_enqueue(struct echo_object *eco, obd_off start, obd_off end,
963                            int mode, __u64 *cookie)
964 {
965         struct lu_env *env;
966         struct cl_lock *lck;
967         struct echo_thread_info *info;
968         struct cl_io *io;
969         struct cl_lock_descr *descr;
970         struct cl_object *obj = echo_obj2cl(eco);
971         int refcheck;
972         int result;
973         ENTRY;
974
975         env = cl_env_get(&refcheck);
976         if (IS_ERR(env))
977                 RETURN(PTR_ERR(env));
978
979         info = echo_env_info(env);
980         descr = &info->eti_descr;
981         descr->cld_obj   = obj;
982         descr->cld_start = cl_index(obj, start);
983         descr->cld_end   = cl_index(obj, end);
984         descr->cld_mode  = mode == LCK_PW ? CLM_WRITE : CLM_READ;
985
986         io = &info->eti_io;
987         io->ci_obj = obj;
988         result = cl_io_init(env, io, CIT_MISC, obj);
989         if (result < 0)
990                 GOTO(out, result);
991         LASSERT(result == 0);
992
993         result = -ENOMEM;
994         lck = cl_lock_request(env, io, descr, CEF_ASYNC, "ec enqueue", eco);
995         if (lck) {
996                 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
997                 struct echo_lock *el;
998
999                 result = cl_wait(env, lck);
1000                 if (result == 0) {
1001                         el = cl2echo_lock(cl_lock_at(lck, &echo_device_type));
1002                         spin_lock(&ec->ec_lock);
1003                         list_add(&el->el_chain, &ec->ec_locks);
1004                         *cookie = el->el_cookie = ++ec->ec_unique;
1005                         spin_unlock(&ec->ec_lock);
1006                 } else
1007                         cl_lock_release(env, lck, "ec enqueue", cfs_current());
1008         }
1009         cl_io_fini(env, io);
1010
1011         EXIT;
1012 out:
1013         cl_env_put(env, &refcheck);
1014         return result;
1015 }
1016
1017 static int cl_echo_cancel(struct echo_device *ed, __u64 cookie)
1018 {
1019         struct echo_client_obd *ec = ed->ed_ec;
1020         struct echo_lock       *ecl = NULL;
1021         struct list_head       *el;
1022         int found = 0;
1023         int result;
1024
1025         struct lu_env *env;
1026         int refcheck;
1027         ENTRY;
1028
1029         env = cl_env_get(&refcheck);
1030         if (IS_ERR(env))
1031                 RETURN(PTR_ERR(env));
1032
1033         spin_lock (&ec->ec_lock);
1034         list_for_each (el, &ec->ec_locks) {
1035                 ecl = list_entry (el, struct echo_lock, el_chain);
1036                 CDEBUG(D_INFO, "ecl: %p, cookie: %llx\n", ecl, ecl->el_cookie);
1037                 found = (ecl->el_cookie == cookie);
1038                 if (found) {
1039                         list_del_init(&ecl->el_chain);
1040                         break;
1041                 }
1042         }
1043         spin_unlock (&ec->ec_lock);
1044
1045         result = -ENOENT;
1046         if (found) {
1047                 struct cl_lock *clk = echo_lock2cl(ecl);
1048
1049                 cl_lock_get(clk);
1050                 cl_unuse(env, clk);
1051                 cl_lock_release(env, clk, "ec enqueue", ecl->el_object);
1052
1053                 cl_lock_mutex_get(env, clk);
1054                 cl_lock_cancel(env, clk);
1055                 cl_lock_delete(env, clk);
1056                 cl_lock_mutex_put(env, clk);
1057                 cl_lock_put(env, clk);
1058                 result = 0;
1059         }
1060         cl_env_put(env, &refcheck);
1061         RETURN(result);
1062 }
1063
1064 static int cl_echo_async_brw(const struct lu_env *env, struct cl_io *io,
1065                              enum cl_req_type unused, struct cl_2queue *queue)
1066 {
1067         struct cl_page *clp;
1068         struct cl_page *temp;
1069         int result = 0;
1070         ENTRY;
1071
1072         cl_page_list_for_each_safe(clp, temp, &queue->c2_qin) {
1073                 int rc;
1074                 rc = cl_page_cache_add(env, io, clp, CRT_WRITE);
1075                 if (rc == 0)
1076                         continue;
1077                 result = result ?: rc;
1078         }
1079         RETURN(result);
1080 }
1081
1082 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
1083                               cfs_page_t **pages, int npages, int async)
1084 {
1085         struct lu_env           *env;
1086         struct echo_thread_info *info;
1087         struct cl_object        *obj = echo_obj2cl(eco);
1088         struct echo_device      *ed  = eco->eo_dev;
1089         struct cl_2queue        *queue;
1090         struct cl_io            *io;
1091         struct cl_page          *clp;
1092
1093         int page_size = cl_page_size(obj);
1094         int refcheck;
1095         int rc;
1096         int i;
1097         ENTRY;
1098
1099         LASSERT(ed->ed_next != NULL);
1100         env = cl_env_get(&refcheck);
1101         if (IS_ERR(env))
1102                 RETURN(PTR_ERR(env));
1103
1104         info    = echo_env_info(env);
1105         io      = &info->eti_io;
1106         queue   = &info->eti_queue;
1107
1108         cl_2queue_init(queue);
1109         rc = cl_io_init(env, io, CIT_MISC, obj);
1110         if (rc < 0)
1111                 GOTO(out, rc);
1112         LASSERT(rc == 0);
1113
1114         for (i = 0; i < npages; i++) {
1115                 LASSERT(pages[i]);
1116                 clp = cl_page_find(env, obj, cl_index(obj, offset),
1117                                    pages[i], CPT_TRANSIENT);
1118                 if (IS_ERR(clp)) {
1119                         rc = PTR_ERR(clp);
1120                         break;
1121                 }
1122                 LASSERT(clp->cp_type == CPT_TRANSIENT);
1123
1124                 rc = cl_page_own(env, io, clp);
1125                 if (rc) {
1126                         LASSERT(clp->cp_state == CPS_FREEING);
1127                         cl_page_put(env, clp);
1128                         break;
1129                 }
1130
1131                 cl_2queue_add(queue, clp);
1132
1133                 /* drop the reference count for cl_page_find, so that the page
1134                  * will be freed in cl_2queue_fini. */
1135                 cl_page_put(env, clp);
1136                 offset += page_size;
1137         }
1138
1139         if (rc == 0) {
1140                 enum cl_req_type typ = rw == READ ? CRT_READ : CRT_WRITE;
1141
1142                 async = async && (typ == CRT_WRITE);
1143                 if (async)
1144                         rc = cl_echo_async_brw(env, io, typ, queue);
1145                 else
1146                         rc = cl_io_submit_sync(env, io, typ, queue,
1147                                                CRP_NORMAL, 0);
1148                 CDEBUG(D_INFO, "echo_client %s write returns %d\n",
1149                        async ? "async" : "sync", rc);
1150         }
1151
1152         cl_2queue_discard(env, io, queue);
1153         cl_2queue_disown(env, io, queue);
1154         cl_2queue_fini(env, queue);
1155         cl_io_fini(env, io);
1156
1157         EXIT;
1158 out:
1159         cl_env_put(env, &refcheck);
1160         return rc;
1161 }
1162 /** @} echo_exports */
1163
1164
1165 static obd_id last_object_id;
1166
1167 static int
1168 echo_copyout_lsm (struct lov_stripe_md *lsm, void *_ulsm, int ulsm_nob)
1169 {
1170         struct lov_stripe_md *ulsm = _ulsm;
1171         int nob, i;
1172
1173         nob = offsetof (struct lov_stripe_md, lsm_oinfo[lsm->lsm_stripe_count]);
1174         if (nob > ulsm_nob)
1175                 return (-EINVAL);
1176
1177         if (copy_to_user (ulsm, lsm, sizeof(ulsm)))
1178                 return (-EFAULT);
1179
1180         for (i = 0; i < lsm->lsm_stripe_count; i++) {
1181                 if (copy_to_user (ulsm->lsm_oinfo[i], lsm->lsm_oinfo[i],
1182                                   sizeof(lsm->lsm_oinfo[0])))
1183                         return (-EFAULT);
1184         }
1185         return 0;
1186 }
1187
1188 static int
1189 echo_copyin_lsm (struct echo_device *ed, struct lov_stripe_md *lsm,
1190                  void *ulsm, int ulsm_nob)
1191 {
1192         struct echo_client_obd *ec = ed->ed_ec;
1193         int                     i;
1194
1195         if (ulsm_nob < sizeof (*lsm))
1196                 return (-EINVAL);
1197
1198         if (copy_from_user (lsm, ulsm, sizeof (*lsm)))
1199                 return (-EFAULT);
1200
1201         if (lsm->lsm_stripe_count > ec->ec_nstripes ||
1202             lsm->lsm_magic != LOV_MAGIC ||
1203             (lsm->lsm_stripe_size & (~CFS_PAGE_MASK)) != 0 ||
1204             ((__u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count > ~0UL))
1205                 return (-EINVAL);
1206
1207
1208         for (i = 0; i < lsm->lsm_stripe_count; i++) {
1209                 if (copy_from_user(lsm->lsm_oinfo[i],
1210                                    ((struct lov_stripe_md *)ulsm)->lsm_oinfo[i],
1211                                    sizeof(lsm->lsm_oinfo[0])))
1212                         return (-EFAULT);
1213         }
1214         return (0);
1215 }
1216
1217 static int echo_create_object(struct echo_device *ed, int on_target,
1218                               struct obdo *oa, void *ulsm, int ulsm_nob,
1219                               struct obd_trans_info *oti)
1220 {
1221         struct echo_object     *eco;
1222         struct echo_client_obd *ec = ed->ed_ec;
1223         struct lov_stripe_md   *lsm = NULL;
1224         int                     rc;
1225         int                     created = 0;
1226         ENTRY;
1227
1228         if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
1229             (on_target ||                       /* set_stripe */
1230              ec->ec_nstripes != 0)) {           /* LOV */
1231                 CERROR ("No valid oid\n");
1232                 RETURN(-EINVAL);
1233         }
1234
1235         rc = obd_alloc_memmd(ec->ec_exp, &lsm);
1236         if (rc < 0) {
1237                 CERROR("Cannot allocate md, rc = %d\n", rc);
1238                 GOTO(failed, rc);
1239         }
1240
1241         if (ulsm != NULL) {
1242                 int i, idx;
1243
1244                 rc = echo_copyin_lsm (ed, lsm, ulsm, ulsm_nob);
1245                 if (rc != 0)
1246                         GOTO(failed, rc);
1247
1248                 if (lsm->lsm_stripe_count == 0)
1249                         lsm->lsm_stripe_count = ec->ec_nstripes;
1250
1251                 if (lsm->lsm_stripe_size == 0)
1252                         lsm->lsm_stripe_size = CFS_PAGE_SIZE;
1253
1254                 idx = ll_rand();
1255
1256                 /* setup stripes: indices + default ids if required */
1257                 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1258                         if (lsm->lsm_oinfo[i]->loi_id == 0)
1259                                 lsm->lsm_oinfo[i]->loi_id = lsm->lsm_object_id;
1260
1261                         lsm->lsm_oinfo[i]->loi_ost_idx =
1262                                 (idx + i) % ec->ec_nstripes;
1263                 }
1264         }
1265
1266         /* setup object ID here for !on_target and LOV hint */
1267         if (oa->o_valid & OBD_MD_FLID)
1268                 lsm->lsm_object_id = oa->o_id;
1269
1270         if (lsm->lsm_object_id == 0)
1271                 lsm->lsm_object_id = ++last_object_id;
1272
1273         rc = 0;
1274         if (on_target) {
1275                 /* Only echo objects are allowed to be created */
1276                 LASSERT((oa->o_valid & OBD_MD_FLGROUP) &&
1277                         (oa->o_gr == FILTER_GROUP_ECHO));
1278                 rc = obd_create(ec->ec_exp, oa, &lsm, oti);
1279                 if (rc != 0) {
1280                         CERROR("Cannot create objects, rc = %d\n", rc);
1281                         GOTO(failed, rc);
1282                 }
1283                 created = 1;
1284         }
1285
1286         /* See what object ID we were given */
1287         oa->o_id = lsm->lsm_object_id;
1288         oa->o_valid |= OBD_MD_FLID;
1289
1290         eco = cl_echo_object_find(ed, &lsm);
1291         if (IS_ERR(eco))
1292                 GOTO(failed, rc = PTR_ERR(eco));
1293         cl_echo_object_put(eco);
1294
1295         CDEBUG(D_INFO, "oa->o_id = %lx\n", (long)oa->o_id);
1296         EXIT;
1297
1298  failed:
1299         if (created && rc)
1300                 obd_destroy(ec->ec_exp, oa, lsm, oti, NULL, NULL);
1301         if (lsm)
1302                 obd_free_memmd(ec->ec_exp, &lsm);
1303         if (rc)
1304                 CERROR("create object failed with rc = %d\n", rc);
1305         return (rc);
1306 }
1307
1308 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
1309                            struct obdo *oa)
1310 {
1311         struct echo_client_obd *ec  = ed->ed_ec;
1312         struct lov_stripe_md   *lsm = NULL;
1313         struct echo_object     *eco;
1314         int                     rc;
1315         ENTRY;
1316
1317         if ((oa->o_valid & OBD_MD_FLID) == 0 ||
1318             oa->o_id == 0)  /* disallow use of object id 0 */
1319         {
1320                 CERROR ("No valid oid\n");
1321                 RETURN(-EINVAL);
1322         }
1323
1324         rc = obd_alloc_memmd(ec->ec_exp, &lsm);
1325         if (rc < 0)
1326                 RETURN(rc);
1327
1328         lsm->lsm_object_id = oa->o_id;
1329         if (oa->o_valid & OBD_MD_FLGROUP)
1330                 lsm->lsm_object_gr = oa->o_gr;
1331         else
1332                 lsm->lsm_object_gr = FILTER_GROUP_ECHO;
1333
1334         rc = 0;
1335         eco = cl_echo_object_find(ed, &lsm);
1336         if (!IS_ERR(eco))
1337                 *ecop = eco;
1338         else
1339                 rc = PTR_ERR(eco);
1340         if (lsm)
1341                 obd_free_memmd(ec->ec_exp, &lsm);
1342         RETURN(rc);
1343 }
1344
1345 static void echo_put_object(struct echo_object *eco)
1346 {
1347         if (cl_echo_object_put(eco))
1348                 CERROR("echo client: drop an object failed");
1349 }
1350
1351 static void
1352 echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
1353 {
1354         unsigned long stripe_count;
1355         unsigned long stripe_size;
1356         unsigned long width;
1357         unsigned long woffset;
1358         int           stripe_index;
1359         obd_off       offset;
1360
1361         if (lsm->lsm_stripe_count <= 1)
1362                 return;
1363
1364         offset       = *offp;
1365         stripe_size  = lsm->lsm_stripe_size;
1366         stripe_count = lsm->lsm_stripe_count;
1367
1368         /* width = # bytes in all stripes */
1369         width = stripe_size * stripe_count;
1370
1371         /* woffset = offset within a width; offset = whole number of widths */
1372         woffset = do_div (offset, width);
1373
1374         stripe_index = woffset / stripe_size;
1375
1376         *idp = lsm->lsm_oinfo[stripe_index]->loi_id;
1377         *offp = offset * stripe_size + woffset % stripe_size;
1378 }
1379
1380 static void
1381 echo_client_page_debug_setup(struct lov_stripe_md *lsm,
1382                              cfs_page_t *page, int rw, obd_id id,
1383                              obd_off offset, obd_off count)
1384 {
1385         char    *addr;
1386         obd_off  stripe_off;
1387         obd_id   stripe_id;
1388         int      delta;
1389
1390         /* no partial pages on the client */
1391         LASSERT(count == CFS_PAGE_SIZE);
1392
1393         addr = cfs_kmap(page);
1394
1395         for (delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
1396                 if (rw == OBD_BRW_WRITE) {
1397                         stripe_off = offset + delta;
1398                         stripe_id = id;
1399                         echo_get_stripe_off_id(lsm, &stripe_off, &stripe_id);
1400                 } else {
1401                         stripe_off = 0xdeadbeef00c0ffeeULL;
1402                         stripe_id = 0xdeadbeef00c0ffeeULL;
1403                 }
1404                 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
1405                                   stripe_off, stripe_id);
1406         }
1407
1408         cfs_kunmap(page);
1409 }
1410
1411 static int echo_client_page_debug_check(struct lov_stripe_md *lsm,
1412                                         cfs_page_t *page, obd_id id,
1413                                         obd_off offset, obd_off count)
1414 {
1415         obd_off stripe_off;
1416         obd_id  stripe_id;
1417         char   *addr;
1418         int     delta;
1419         int     rc;
1420         int     rc2;
1421
1422         /* no partial pages on the client */
1423         LASSERT(count == CFS_PAGE_SIZE);
1424
1425         addr = cfs_kmap(page);
1426
1427         for (rc = delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
1428                 stripe_off = offset + delta;
1429                 stripe_id = id;
1430                 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
1431
1432                 rc2 = block_debug_check("test_brw",
1433                                         addr + delta, OBD_ECHO_BLOCK_SIZE,
1434                                         stripe_off, stripe_id);
1435                 if (rc2 != 0) {
1436                         CERROR ("Error in echo object "LPX64"\n", id);
1437                         rc = rc2;
1438                 }
1439         }
1440
1441         cfs_kunmap(page);
1442         return rc;
1443 }
1444
1445 static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
1446                             struct echo_object *eco, obd_off offset,
1447                             obd_size count, int async,
1448                             struct obd_trans_info *oti)
1449 {
1450         struct echo_client_obd *ec  = ed->ed_ec;
1451         struct lov_stripe_md   *lsm = eco->eo_lsm;
1452         obd_count               npages;
1453         struct brw_page        *pga;
1454         struct brw_page        *pgp;
1455         cfs_page_t            **pages;
1456         obd_off                 off;
1457         int                     i;
1458         int                     rc;
1459         int                     verify;
1460         int                     gfp_mask;
1461         ENTRY;
1462
1463         verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
1464                   (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
1465                   (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
1466
1467         gfp_mask = ((oa->o_id & 2) == 0) ? CFS_ALLOC_STD : CFS_ALLOC_HIGHUSER;
1468
1469         LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
1470         LASSERT(lsm != NULL);
1471         LASSERT(lsm->lsm_object_id == oa->o_id);
1472
1473         if (count <= 0 ||
1474             (count & (~CFS_PAGE_MASK)) != 0)
1475                 RETURN(-EINVAL);
1476
1477         /* XXX think again with misaligned I/O */
1478         npages = count >> CFS_PAGE_SHIFT;
1479
1480         OBD_ALLOC(pga, npages * sizeof(*pga));
1481         if (pga == NULL)
1482                 RETURN(-ENOMEM);
1483
1484         OBD_ALLOC(pages, npages * sizeof(*pages));
1485         if (pages == NULL) {
1486                 OBD_FREE(pga, npages * sizeof(*pga));
1487                 RETURN(-ENOMEM);
1488         }
1489
1490         for (i = 0, pgp = pga, off = offset;
1491              i < npages;
1492              i++, pgp++, off += CFS_PAGE_SIZE) {
1493
1494                 LASSERT (pgp->pg == NULL);      /* for cleanup */
1495
1496                 rc = -ENOMEM;
1497                 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
1498                 if (pgp->pg == NULL)
1499                         goto out;
1500
1501                 pages[i] = pgp->pg;
1502                 pgp->count = CFS_PAGE_SIZE;
1503                 pgp->off = off;
1504                 pgp->flag = 0;
1505
1506                 if (verify)
1507                         echo_client_page_debug_setup(lsm, pgp->pg, rw,
1508                                                      oa->o_id, off, pgp->count);
1509         }
1510
1511         if (ed->ed_next == NULL) {
1512                 struct obd_info oinfo = { { { 0 } } };
1513                 oinfo.oi_oa = oa;
1514                 oinfo.oi_md = lsm;
1515                 rc = obd_brw(rw, ec->ec_exp, &oinfo, npages, pga, oti);
1516         } else
1517                 rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
1518
1519  out:
1520         if (rc != 0 || rw != OBD_BRW_READ)
1521                 verify = 0;
1522
1523         for (i = 0, pgp = pga; i < npages; i++, pgp++) {
1524                 if (pgp->pg == NULL)
1525                         continue;
1526
1527                 if (verify) {
1528                         int vrc;
1529                         vrc = echo_client_page_debug_check(lsm, pgp->pg, oa->o_id,
1530                                                            pgp->off, pgp->count);
1531                         if (vrc != 0 && rc == 0)
1532                                 rc = vrc;
1533                 }
1534                 OBD_PAGE_FREE(pgp->pg);
1535         }
1536         OBD_FREE(pga, npages * sizeof(*pga));
1537         OBD_FREE(pages, npages * sizeof(*pages));
1538         RETURN(rc);
1539 }
1540
1541 static int echo_client_prep_commit(struct obd_export *exp, int rw,
1542                                    struct obdo *oa, struct echo_object *eco,
1543                                    obd_off offset, obd_size count,
1544                                    obd_size batch, struct obd_trans_info *oti)
1545 {
1546         struct lov_stripe_md *lsm = eco->eo_lsm;
1547         struct obd_ioobj ioo;
1548         struct niobuf_local *lnb;
1549         struct niobuf_remote *rnb;
1550         obd_off off;
1551         obd_size npages, tot_pages;
1552         int i, ret = 0;
1553         ENTRY;
1554
1555         if (count <= 0 || (count & (~CFS_PAGE_MASK)) != 0 ||
1556             (lsm != NULL && lsm->lsm_object_id != oa->o_id))
1557                 RETURN(-EINVAL);
1558
1559         npages = batch >> CFS_PAGE_SHIFT;
1560         tot_pages = count >> CFS_PAGE_SHIFT;
1561
1562         OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
1563         OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
1564
1565         if (lnb == NULL || rnb == NULL)
1566                 GOTO(out, ret = -ENOMEM);
1567
1568         obdo_to_ioobj(oa, &ioo);
1569
1570         off = offset;
1571
1572         for(; tot_pages; tot_pages -= npages) {
1573                 int lpages;
1574
1575                 if (tot_pages < npages)
1576                         npages = tot_pages;
1577
1578                 for (i = 0; i < npages; i++, off += CFS_PAGE_SIZE) {
1579                         rnb[i].offset = off;
1580                         rnb[i].len = CFS_PAGE_SIZE;
1581                 }
1582
1583                 ioo.ioo_bufcnt = npages;
1584                 oti->oti_transno = 0;
1585
1586                 lpages = npages;
1587                 ret = obd_preprw(rw, exp, oa, 1, &ioo, rnb, &lpages, lnb, oti,
1588                                  NULL);
1589                 if (ret != 0)
1590                         GOTO(out, ret);
1591                 LASSERT(lpages == npages);
1592
1593                 for (i = 0; i < lpages; i++) {
1594                         cfs_page_t *page = lnb[i].page;
1595
1596                         /* read past eof? */
1597                         if (page == NULL && lnb[i].rc == 0)
1598                                 continue;
1599
1600                         if (oa->o_id == ECHO_PERSISTENT_OBJID ||
1601                             (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
1602                             (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
1603                                 continue;
1604
1605                         if (rw == OBD_BRW_WRITE)
1606                                 echo_client_page_debug_setup(lsm, page, rw,
1607                                                              oa->o_id,
1608                                                              rnb[i].offset,
1609                                                              rnb[i].len);
1610                         else
1611                                 echo_client_page_debug_check(lsm, page,
1612                                                              oa->o_id,
1613                                                              rnb[i].offset,
1614                                                              rnb[i].len);
1615                 }
1616
1617                 ret = obd_commitrw(rw, exp, oa, 1,&ioo,rnb,npages,lnb,oti,ret);
1618                 if (ret != 0)
1619                         GOTO(out, ret);
1620
1621                 /* Reset oti otherwise it would confuse ldiskfs. */
1622                 memset(oti, 0, sizeof(*oti));
1623         }
1624
1625 out:
1626         if (lnb)
1627                 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
1628         if (rnb)
1629                 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
1630         RETURN(ret);
1631 }
1632
1633 static int echo_client_brw_ioctl(int rw, struct obd_export *exp,
1634                                  struct obd_ioctl_data *data)
1635 {
1636         struct obd_device *obd = class_exp2obd(exp);
1637         struct echo_device *ed = obd2echo_dev(obd);
1638         struct echo_client_obd *ec = ed->ed_ec;
1639         struct obd_trans_info dummy_oti = { .oti_thread = NULL };
1640         struct obdo *oa = &data->ioc_obdo1;
1641         struct echo_object *eco;
1642         int rc;
1643         int async = 1;
1644         ENTRY;
1645
1646         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
1647
1648         rc = echo_get_object(&eco, ed, oa);
1649         if (rc)
1650                 RETURN(rc);
1651
1652         oa->o_valid &= ~OBD_MD_FLHANDLE;
1653
1654         switch((long)data->ioc_pbuf1) {
1655         case 1:
1656                 async = 0;
1657                 /* fall through */
1658         case 2:
1659                 rc = echo_client_kbrw(ed, rw, oa,
1660                                       eco, data->ioc_offset,
1661                                       data->ioc_count, async, &dummy_oti);
1662                 break;
1663         case 3:
1664                 rc = echo_client_prep_commit(ec->ec_exp, rw, oa,
1665                                             eco, data->ioc_offset,
1666                                             data->ioc_count, data->ioc_plen1,
1667                                             &dummy_oti);
1668                 break;
1669         default:
1670                 rc = -EINVAL;
1671         }
1672         echo_put_object(eco);
1673         RETURN(rc);
1674 }
1675
1676 static int
1677 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
1678                     int mode, obd_off offset, obd_size nob)
1679 {
1680         struct echo_device     *ed = obd2echo_dev(exp->exp_obd);
1681         struct lustre_handle   *ulh = &oa->o_handle;
1682         struct echo_object     *eco;
1683         obd_off                 end;
1684         int                     rc;
1685         ENTRY;
1686
1687         if (ed->ed_next == NULL)
1688                 RETURN(-EOPNOTSUPP);
1689
1690         if (!(mode == LCK_PR || mode == LCK_PW))
1691                 RETURN(-EINVAL);
1692
1693         if ((offset & (~CFS_PAGE_MASK)) != 0 ||
1694             (nob & (~CFS_PAGE_MASK)) != 0)
1695                 RETURN(-EINVAL);
1696
1697         rc = echo_get_object (&eco, ed, oa);
1698         if (rc != 0)
1699                 RETURN(rc);
1700
1701         end = (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
1702         rc = cl_echo_enqueue(eco, offset, end, mode, &ulh->cookie);
1703         if (rc == 0) {
1704                 oa->o_valid |= OBD_MD_FLHANDLE;
1705                 CDEBUG(D_INFO, "Cookie is %llx\n", ulh->cookie);
1706         }
1707         echo_put_object(eco);
1708         RETURN(rc);
1709 }
1710
1711 static int
1712 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
1713 {
1714         struct echo_device *ed     = obd2echo_dev(exp->exp_obd);
1715         __u64               cookie = oa->o_handle.cookie;
1716
1717         if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
1718                 return -EINVAL;
1719
1720         CDEBUG(D_INFO, "Cookie is %llx\n", cookie);
1721         return cl_echo_cancel(ed, cookie);
1722 }
1723
1724 static int
1725 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
1726                       int len, void *karg, void *uarg)
1727 {
1728         struct obd_device      *obd = exp->exp_obd;
1729         struct echo_device     *ed = obd2echo_dev(obd);
1730         struct echo_client_obd *ec = ed->ed_ec;
1731         struct echo_object     *eco;
1732         struct obd_ioctl_data  *data = karg;
1733         struct obd_trans_info   dummy_oti;
1734         struct oti_req_ack_lock *ack_lock;
1735         struct obdo            *oa;
1736         int                     rw = OBD_BRW_READ;
1737         int                     rc = 0;
1738         int                     i;
1739         ENTRY;
1740
1741         unlock_kernel();
1742
1743         memset(&dummy_oti, 0, sizeof(dummy_oti));
1744
1745         oa = &data->ioc_obdo1;
1746         if (!(oa->o_valid & OBD_MD_FLGROUP)) {
1747                 oa->o_valid |= OBD_MD_FLGROUP;
1748                 oa->o_gr = FILTER_GROUP_ECHO;
1749         }
1750         /* assume we can touch filter native objects with echo device. */
1751         /* LASSERT(oa->o_gr == FILTER_GROUP_ECHO); */
1752
1753         switch (cmd) {
1754         case OBD_IOC_CREATE:                    /* may create echo object */
1755                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1756                         GOTO (out, rc = -EPERM);
1757
1758                 rc = echo_create_object (ed, 1, oa,
1759                                          data->ioc_pbuf1, data->ioc_plen1,
1760                                          &dummy_oti);
1761                 GOTO(out, rc);
1762
1763         case OBD_IOC_DESTROY:
1764                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1765                         GOTO (out, rc = -EPERM);
1766
1767                 rc = echo_get_object (&eco, ed, oa);
1768                 if (rc == 0) {
1769                         rc = obd_destroy(ec->ec_exp, oa, eco->eo_lsm,
1770                                          &dummy_oti, NULL, NULL);
1771                         if (rc == 0)
1772                                 eco->eo_deleted = 1;
1773                         echo_put_object(eco);
1774                 }
1775                 GOTO(out, rc);
1776
1777         case OBD_IOC_GETATTR:
1778                 rc = echo_get_object (&eco, ed, oa);
1779                 if (rc == 0) {
1780                         struct obd_info oinfo = { { { 0 } } };
1781                         oinfo.oi_md = eco->eo_lsm;
1782                         oinfo.oi_oa = oa;
1783                         rc = obd_getattr(ec->ec_exp, &oinfo);
1784                         echo_put_object(eco);
1785                 }
1786                 GOTO(out, rc);
1787
1788         case OBD_IOC_SETATTR:
1789                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1790                         GOTO (out, rc = -EPERM);
1791
1792                 rc = echo_get_object (&eco, ed, oa);
1793                 if (rc == 0) {
1794                         struct obd_info oinfo = { { { 0 } } };
1795                         oinfo.oi_oa = oa;
1796                         oinfo.oi_md = eco->eo_lsm;
1797
1798                         rc = obd_setattr(ec->ec_exp, &oinfo, NULL);
1799                         echo_put_object(eco);
1800                 }
1801                 GOTO(out, rc);
1802
1803         case OBD_IOC_BRW_WRITE:
1804                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1805                         GOTO (out, rc = -EPERM);
1806
1807                 rw = OBD_BRW_WRITE;
1808                 /* fall through */
1809         case OBD_IOC_BRW_READ:
1810                 rc = echo_client_brw_ioctl(rw, exp, data);
1811                 GOTO(out, rc);
1812
1813         case ECHO_IOC_GET_STRIPE:
1814                 rc = echo_get_object(&eco, ed, oa);
1815                 if (rc == 0) {
1816                         rc = echo_copyout_lsm(eco->eo_lsm, data->ioc_pbuf1,
1817                                               data->ioc_plen1);
1818                         echo_put_object(eco);
1819                 }
1820                 GOTO(out, rc);
1821
1822         case ECHO_IOC_SET_STRIPE:
1823                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1824                         GOTO (out, rc = -EPERM);
1825
1826                 if (data->ioc_pbuf1 == NULL) {  /* unset */
1827                         rc = echo_get_object(&eco, ed, oa);
1828                         if (rc == 0) {
1829                                 eco->eo_deleted = 1;
1830                                 echo_put_object(eco);
1831                         }
1832                 } else {
1833                         rc = echo_create_object(ed, 0, oa,
1834                                                 data->ioc_pbuf1,
1835                                                 data->ioc_plen1, &dummy_oti);
1836                 }
1837                 GOTO (out, rc);
1838
1839         case ECHO_IOC_ENQUEUE:
1840                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1841                         GOTO (out, rc = -EPERM);
1842
1843                 rc = echo_client_enqueue(exp, oa,
1844                                          data->ioc_conn1, /* lock mode */
1845                                          data->ioc_offset,
1846                                          data->ioc_count);/*extent*/
1847                 GOTO (out, rc);
1848
1849         case ECHO_IOC_CANCEL:
1850                 rc = echo_client_cancel(exp, oa);
1851                 GOTO (out, rc);
1852
1853         default:
1854                 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
1855                 GOTO (out, rc = -ENOTTY);
1856         }
1857
1858         EXIT;
1859  out:
1860
1861         /* XXX this should be in a helper also called by target_send_reply */
1862         for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
1863              i++, ack_lock++) {
1864                 if (!ack_lock->mode)
1865                         break;
1866                 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
1867         }
1868
1869         lock_kernel();
1870
1871         return rc;
1872 }
1873
1874 static int echo_client_setup(struct obd_device *obddev, struct lustre_cfg *lcfg)
1875 {
1876         struct echo_client_obd *ec = &obddev->u.echo_client;
1877         struct obd_device *tgt;
1878         struct obd_uuid echo_uuid = { "ECHO_UUID" };
1879         struct obd_connect_data *ocd = NULL;
1880         int rc;
1881         ENTRY;
1882
1883         if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1884                 CERROR("requires a TARGET OBD name\n");
1885                 RETURN(-EINVAL);
1886         }
1887
1888         tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
1889         if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
1890                 CERROR("device not attached or not set up (%s)\n",
1891                        lustre_cfg_string(lcfg, 1));
1892                 RETURN(-EINVAL);
1893         }
1894
1895         spin_lock_init (&ec->ec_lock);
1896         CFS_INIT_LIST_HEAD (&ec->ec_objects);
1897         CFS_INIT_LIST_HEAD (&ec->ec_locks);
1898         ec->ec_unique = 0;
1899         ec->ec_nstripes = 0;
1900
1901         OBD_ALLOC(ocd, sizeof(*ocd));
1902         if (ocd == NULL) {
1903                 CERROR("Can't alloc ocd connecting to %s\n",
1904                        lustre_cfg_string(lcfg, 1));
1905                 return -ENOMEM;
1906         }
1907
1908         ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
1909                                  OBD_CONNECT_GRANT;
1910         ocd->ocd_version = LUSTRE_VERSION_CODE;
1911         ocd->ocd_group = FILTER_GROUP_ECHO;
1912
1913         rc = obd_connect(NULL, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
1914         if (rc == 0) {
1915                 /* Turn off pinger because it connects to tgt obd directly. */
1916                 spin_lock(&tgt->obd_dev_lock);
1917                 list_del_init(&ec->ec_exp->exp_obd_chain_timed);
1918                 spin_unlock(&tgt->obd_dev_lock);
1919         }
1920
1921         OBD_FREE(ocd, sizeof(*ocd));
1922
1923         if (rc != 0) {
1924                 CERROR("fail to connect to device %s\n",
1925                        lustre_cfg_string(lcfg, 1));
1926                 return (rc);
1927         }
1928
1929         RETURN(rc);
1930 }
1931
1932 static int echo_client_cleanup(struct obd_device *obddev)
1933 {
1934         struct echo_client_obd *ec = &obddev->u.echo_client;
1935         int rc;
1936         ENTRY;
1937
1938         if (!list_empty(&obddev->obd_exports)) {
1939                 CERROR("still has clients!\n");
1940                 RETURN(-EBUSY);
1941         }
1942
1943         LASSERT(atomic_read(&ec->ec_exp->exp_refcount) > 0);
1944         rc = obd_disconnect(ec->ec_exp);
1945         if (rc != 0)
1946                 CERROR("fail to disconnect device: %d\n", rc);
1947
1948         RETURN(rc);
1949 }
1950
1951 static int echo_client_connect(const struct lu_env *env,
1952                                struct obd_export **exp,
1953                                struct obd_device *src, struct obd_uuid *cluuid,
1954                                struct obd_connect_data *data, void *localdata)
1955 {
1956         int                rc;
1957         struct lustre_handle conn = { 0 };
1958
1959         ENTRY;
1960         rc = class_connect(&conn, src, cluuid);
1961         if (rc == 0) {
1962                 *exp = class_conn2export(&conn);
1963         }
1964
1965         RETURN (rc);
1966 }
1967
1968 static int echo_client_disconnect(struct obd_export *exp)
1969 {
1970 #if 0
1971         struct obd_device      *obd;
1972         struct echo_client_obd *ec;
1973         struct ec_lock         *ecl;
1974 #endif
1975         int                     rc;
1976         ENTRY;
1977
1978         if (exp == NULL)
1979                 GOTO(out, rc = -EINVAL);
1980
1981 #if 0
1982         obd = exp->exp_obd;
1983         ec = &obd->u.echo_client;
1984
1985         /* no more contention on export's lock list */
1986         while (!list_empty (&exp->exp_ec_data.eced_locks)) {
1987                 ecl = list_entry (exp->exp_ec_data.eced_locks.next,
1988                                   struct ec_lock, ecl_exp_chain);
1989                 list_del (&ecl->ecl_exp_chain);
1990
1991                 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm,
1992                                  ecl->ecl_mode, &ecl->ecl_lock_handle);
1993
1994                 CDEBUG (D_INFO, "Cancel lock on object "LPX64" on disconnect "
1995                         "(%d)\n", ecl->ecl_object->eco_id, rc);
1996
1997                 echo_put_object (ecl->ecl_object);
1998                 OBD_FREE (ecl, sizeof (*ecl));
1999         }
2000 #endif
2001
2002         rc = class_disconnect(exp);
2003         GOTO(out, rc);
2004  out:
2005         return rc;
2006 }
2007
2008 static struct obd_ops echo_obd_ops = {
2009         .o_owner       = THIS_MODULE,
2010
2011 #if 0
2012         .o_setup       = echo_client_setup,
2013         .o_cleanup     = echo_client_cleanup,
2014 #endif
2015
2016         .o_iocontrol   = echo_client_iocontrol,
2017         .o_connect     = echo_client_connect,
2018         .o_disconnect  = echo_client_disconnect
2019 };
2020
2021 int echo_client_init(void)
2022 {
2023         struct lprocfs_static_vars lvars = { 0 };
2024         int rc;
2025
2026         lprocfs_echo_init_vars(&lvars);
2027         rc = class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
2028                                  LUSTRE_ECHO_CLIENT_NAME, &echo_device_type);
2029         if (rc == 0)
2030                 lu_kmem_init(echo_caches);
2031         return rc;
2032 }
2033
2034 void echo_client_exit(void)
2035 {
2036         class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
2037         lu_kmem_fini(echo_caches);
2038 }
2039
2040 /** @} echo_client */