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