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