Whamcloud - gitweb
b=17167 libcfs: ensure all libcfs exported symbols to have cfs_ prefix
[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         cfs_list_t              eo_obj_chain;
73         struct lov_stripe_md   *eo_lsm;
74         cfs_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         cfs_list_t             el_chain;
91         struct echo_object    *el_object;
92         __u64                  el_cookie;
93         cfs_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         cfs_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(cfs_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(cfs_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                 cfs_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                 cfs_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         cfs_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         cfs_spin_lock(&ec->ec_lock);
468         cfs_list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
469         cfs_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(cfs_atomic_read(&eco->eo_npages) == 0);
482
483         cfs_spin_lock(&ec->ec_lock);
484         cfs_list_del_init(&eco->eo_obj_chain);
485         cfs_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         cfs_spin_lock(&ec->ec_lock);
798         while (!cfs_list_empty(&ec->ec_locks)) {
799                 struct echo_lock *ecl = cfs_list_entry(ec->ec_locks.next,
800                                                        struct echo_lock,
801                                                        el_chain);
802                 int still_used = 0;
803
804                 if (cfs_atomic_dec_and_test(&ecl->el_refcount))
805                         cfs_list_del_init(&ecl->el_chain);
806                 else
807                         still_used = 1;
808                 cfs_spin_unlock(&ec->ec_lock);
809
810                 CERROR("echo client: pending lock %p refs %d\n",
811                        ecl, cfs_atomic_read(&ecl->el_refcount));
812
813                 echo_lock_release(env, ecl, still_used);
814                 cfs_spin_lock(&ec->ec_lock);
815         }
816         cfs_spin_unlock(&ec->ec_lock);
817
818         LASSERT(ed->ed_site);
819         lu_site_purge(env, &ed->ed_site->cs_lu, -1);
820
821         /* check if there are objects still alive.
822          * It shouldn't have any object because lu_site_purge would cleanup
823          * all of cached objects. Anyway, probably the echo device is being
824          * parallelly accessed.
825          */
826         cfs_spin_lock(&ec->ec_lock);
827         cfs_list_for_each_entry(eco, &ec->ec_objects, eo_obj_chain)
828                 eco->eo_deleted = 1;
829         cfs_spin_unlock(&ec->ec_lock);
830
831         /* purge again */
832         lu_site_purge(env, &ed->ed_site->cs_lu, -1);
833
834         CDEBUG(D_INFO,
835                "Waiting for the reference of echo object to be dropped\n");
836
837         /* Wait for the last reference to be dropped. */
838         cfs_spin_lock(&ec->ec_lock);
839         while (!cfs_list_empty(&ec->ec_objects)) {
840                 cfs_spin_unlock(&ec->ec_lock);
841                 CERROR("echo_client still has objects at cleanup time, "
842                        "wait for 1 second\n");
843                 cfs_schedule_timeout_and_set_state(CFS_TASK_UNINT,
844                                                    cfs_time_seconds(1));
845                 cfs_spin_lock(&ec->ec_lock);
846         }
847         cfs_spin_unlock(&ec->ec_lock);
848
849         CDEBUG(D_INFO, "No object exists, exiting...\n");
850
851         echo_client_cleanup(d->ld_obd);
852
853         while (next)
854                 next = next->ld_type->ldt_ops->ldto_device_free(env, next);
855
856         LASSERT(ed->ed_site == lu2cl_site(d->ld_site));
857         echo_site_fini(env, ed);
858         cl_device_fini(&ed->ed_cl);
859         OBD_FREE_PTR(ed);
860
861         return NULL;
862 }
863
864 static const struct lu_device_type_operations echo_device_type_ops = {
865         .ldto_init = echo_type_init,
866         .ldto_fini = echo_type_fini,
867
868         .ldto_start = echo_type_start,
869         .ldto_stop  = echo_type_stop,
870
871         .ldto_device_alloc = echo_device_alloc,
872         .ldto_device_free  = echo_device_free,
873         .ldto_device_init  = echo_device_init,
874         .ldto_device_fini  = echo_device_fini
875 };
876
877 static struct lu_device_type echo_device_type = {
878         .ldt_tags     = LU_DEVICE_CL,
879         .ldt_name     = LUSTRE_ECHO_CLIENT_NAME,
880         .ldt_ops      = &echo_device_type_ops,
881         .ldt_ctx_tags = LCT_CL_THREAD
882 };
883 /** @} echo_init */
884
885 /** \defgroup echo_exports Exported operations
886  *
887  * exporting functions to echo client
888  *
889  * @{
890  */
891
892 /* Interfaces to echo client obd device */
893 static struct echo_object *cl_echo_object_find(struct echo_device *d,
894                                                struct lov_stripe_md **lsmp)
895 {
896         struct lu_env *env;
897         struct echo_thread_info *info;
898         struct echo_object_conf *conf;
899         struct lov_stripe_md    *lsm;
900         struct echo_object *eco;
901         struct cl_object   *obj;
902         struct lu_fid *fid;
903         int refcheck;
904         ENTRY;
905
906         LASSERT(lsmp);
907         lsm = *lsmp;
908         LASSERT(lsm);
909         LASSERT(lsm->lsm_object_id);
910
911         /* Never return an object if the obd is to be freed. */
912         if (echo_dev2cl(d)->cd_lu_dev.ld_obd->obd_stopping)
913                 RETURN(ERR_PTR(-ENODEV));
914
915         env = cl_env_get(&refcheck);
916         if (IS_ERR(env))
917                 RETURN((void *)env);
918
919         info = echo_env_info(env);
920         conf = &info->eti_conf;
921         if (d->ed_next) {
922                 if (!d->ed_next_islov) {
923                         struct lov_oinfo *oinfo = lsm->lsm_oinfo[0];
924                         LASSERT(oinfo != NULL);
925                         oinfo->loi_id = lsm->lsm_object_id;
926                         oinfo->loi_gr = lsm->lsm_object_gr;
927                         conf->eoc_cl.u.coc_oinfo = oinfo;
928                 } else {
929                         struct lustre_md *md;
930                         md = &info->eti_md;
931                         memset(md, 0, sizeof *md);
932                         md->lsm = lsm;
933                         conf->eoc_cl.u.coc_md = md;
934                 }
935         }
936         conf->eoc_md = lsmp;
937
938         fid  = &info->eti_fid;
939         lsm2fid(lsm, fid);
940
941         obj = cl_object_find(env, echo_dev2cl(d), fid, &conf->eoc_cl);
942         if (IS_ERR(obj))
943                 GOTO(out, eco = (void*)obj);
944
945         eco = cl2echo_obj(obj);
946         if (eco->eo_deleted) {
947                 cl_object_put(env, obj);
948                 eco = ERR_PTR(-EAGAIN);
949         }
950
951 out:
952         cl_env_put(env, &refcheck);
953         RETURN(eco);
954 }
955
956 static int cl_echo_object_put(struct echo_object *eco)
957 {
958         struct lu_env *env;
959         struct cl_object *obj = echo_obj2cl(eco);
960         int refcheck;
961         ENTRY;
962
963         env = cl_env_get(&refcheck);
964         if (IS_ERR(env))
965                 RETURN(PTR_ERR(env));
966
967         /* an external function to kill an object? */
968         if (eco->eo_deleted) {
969                 struct lu_object_header *loh = obj->co_lu.lo_header;
970                 LASSERT(&eco->eo_hdr == luh2coh(loh));
971                 cfs_set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
972                 cl_object_prune(env, obj);
973         }
974
975         cl_object_put(env, obj);
976         cl_env_put(env, &refcheck);
977         RETURN(0);
978 }
979
980 static int cl_echo_enqueue0(struct lu_env *env, struct echo_object *eco,
981                             obd_off start, obd_off end, int mode,
982                             __u64 *cookie , __u32 enqflags)
983 {
984         struct cl_io *io;
985         struct cl_lock *lck;
986         struct cl_object *obj;
987         struct cl_lock_descr *descr;
988         struct echo_thread_info *info;
989         int rc = -ENOMEM;
990         ENTRY;
991
992         info = echo_env_info(env);
993         io = &info->eti_io;
994         descr = &info->eti_descr;
995         obj = echo_obj2cl(eco);
996
997         descr->cld_obj   = obj;
998         descr->cld_start = cl_index(obj, start);
999         descr->cld_end   = cl_index(obj, end);
1000         descr->cld_mode  = mode == LCK_PW ? CLM_WRITE : CLM_READ;
1001         descr->cld_enq_flags = CEF_ASYNC | enqflags;
1002         io->ci_obj = obj;
1003
1004         lck = cl_lock_request(env, io, descr, "ec enqueue", eco);
1005         if (lck) {
1006                 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
1007                 struct echo_lock *el;
1008
1009                 rc = cl_wait(env, lck);
1010                 if (rc == 0) {
1011                         el = cl2echo_lock(cl_lock_at(lck, &echo_device_type));
1012                         cfs_spin_lock(&ec->ec_lock);
1013                         if (cfs_list_empty(&el->el_chain)) {
1014                                 cfs_list_add(&el->el_chain, &ec->ec_locks);
1015                                 el->el_cookie = ++ec->ec_unique;
1016                         }
1017                         cfs_atomic_inc(&el->el_refcount);
1018                         *cookie = el->el_cookie;
1019                         cfs_spin_unlock(&ec->ec_lock);
1020                 } else
1021                         cl_lock_release(env, lck, "ec enqueue", cfs_current());
1022         }
1023         RETURN(rc);
1024 }
1025
1026 static int cl_echo_enqueue(struct echo_object *eco, obd_off start, obd_off end,
1027                            int mode, __u64 *cookie)
1028 {
1029         struct echo_thread_info *info;
1030         struct lu_env *env;
1031         struct cl_io *io;
1032         int refcheck;
1033         int result;
1034         ENTRY;
1035
1036         env = cl_env_get(&refcheck);
1037         if (IS_ERR(env))
1038                 RETURN(PTR_ERR(env));
1039
1040         info = echo_env_info(env);
1041         io = &info->eti_io;
1042
1043         result = cl_io_init(env, io, CIT_MISC, echo_obj2cl(eco));
1044         if (result < 0)
1045                 GOTO(out, result);
1046         LASSERT(result == 0);
1047
1048         result = cl_echo_enqueue0(env, eco, start, end, mode, cookie, 0);
1049         cl_io_fini(env, io);
1050
1051         EXIT;
1052 out:
1053         cl_env_put(env, &refcheck);
1054         return result;
1055 }
1056
1057 static int cl_echo_cancel0(struct lu_env *env, struct echo_device *ed,
1058                            __u64 cookie)
1059 {
1060         struct echo_client_obd *ec = ed->ed_ec;
1061         struct echo_lock       *ecl = NULL;
1062         cfs_list_t             *el;
1063         int found = 0, still_used = 0;
1064         ENTRY;
1065
1066         LASSERT(ec != NULL);
1067         cfs_spin_lock (&ec->ec_lock);
1068         cfs_list_for_each (el, &ec->ec_locks) {
1069                 ecl = cfs_list_entry (el, struct echo_lock, el_chain);
1070                 CDEBUG(D_INFO, "ecl: %p, cookie: %llx\n", ecl, ecl->el_cookie);
1071                 found = (ecl->el_cookie == cookie);
1072                 if (found) {
1073                         if (cfs_atomic_dec_and_test(&ecl->el_refcount))
1074                                 cfs_list_del_init(&ecl->el_chain);
1075                         else
1076                                 still_used = 1;
1077                         break;
1078                 }
1079         }
1080         cfs_spin_unlock (&ec->ec_lock);
1081
1082         if (!found)
1083                 RETURN(-ENOENT);
1084
1085         echo_lock_release(env, ecl, still_used);
1086         RETURN(0);
1087 }
1088
1089 static int cl_echo_cancel(struct echo_device *ed, __u64 cookie)
1090 {
1091         struct lu_env *env;
1092         int refcheck;
1093         int rc;
1094         ENTRY;
1095
1096         env = cl_env_get(&refcheck);
1097         if (IS_ERR(env))
1098                 RETURN(PTR_ERR(env));
1099
1100         rc = cl_echo_cancel0(env, ed, cookie);
1101
1102         cl_env_put(env, &refcheck);
1103         RETURN(rc);
1104 }
1105
1106 static int cl_echo_async_brw(const struct lu_env *env, struct cl_io *io,
1107                              enum cl_req_type unused, struct cl_2queue *queue)
1108 {
1109         struct cl_page *clp;
1110         struct cl_page *temp;
1111         int result = 0;
1112         ENTRY;
1113
1114         cl_page_list_for_each_safe(clp, temp, &queue->c2_qin) {
1115                 int rc;
1116                 rc = cl_page_cache_add(env, io, clp, CRT_WRITE);
1117                 if (rc == 0)
1118                         continue;
1119                 result = result ?: rc;
1120         }
1121         RETURN(result);
1122 }
1123
1124 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
1125                               cfs_page_t **pages, int npages, int async)
1126 {
1127         struct lu_env           *env;
1128         struct echo_thread_info *info;
1129         struct cl_object        *obj = echo_obj2cl(eco);
1130         struct echo_device      *ed  = eco->eo_dev;
1131         struct cl_2queue        *queue;
1132         struct cl_io            *io;
1133         struct cl_page          *clp;
1134         struct lustre_handle    lh = { 0 };
1135         int page_size = cl_page_size(obj);
1136         int refcheck;
1137         int rc;
1138         int i;
1139         ENTRY;
1140
1141         LASSERT((offset & ~CFS_PAGE_MASK) == 0);
1142         LASSERT(ed->ed_next != NULL);
1143         env = cl_env_get(&refcheck);
1144         if (IS_ERR(env))
1145                 RETURN(PTR_ERR(env));
1146
1147         info    = echo_env_info(env);
1148         io      = &info->eti_io;
1149         queue   = &info->eti_queue;
1150
1151         cl_2queue_init(queue);
1152         rc = cl_io_init(env, io, CIT_MISC, obj);
1153         if (rc < 0)
1154                 GOTO(out, rc);
1155         LASSERT(rc == 0);
1156
1157
1158         rc = cl_echo_enqueue0(env, eco, offset,
1159                               offset + npages * CFS_PAGE_SIZE - 1,
1160                               rw == READ ? LCK_PW : LCK_PW, &lh.cookie,
1161                               CILR_NEVER);
1162         if (rc < 0)
1163                 GOTO(error_lock, rc);
1164
1165         for (i = 0; i < npages; i++) {
1166                 LASSERT(pages[i]);
1167                 clp = cl_page_find(env, obj, cl_index(obj, offset),
1168                                    pages[i], CPT_TRANSIENT);
1169                 if (IS_ERR(clp)) {
1170                         rc = PTR_ERR(clp);
1171                         break;
1172                 }
1173                 LASSERT(clp->cp_type == CPT_TRANSIENT);
1174
1175                 rc = cl_page_own(env, io, clp);
1176                 if (rc) {
1177                         LASSERT(clp->cp_state == CPS_FREEING);
1178                         cl_page_put(env, clp);
1179                         break;
1180                 }
1181
1182                 cl_2queue_add(queue, clp);
1183
1184                 /* drop the reference count for cl_page_find, so that the page
1185                  * will be freed in cl_2queue_fini. */
1186                 cl_page_put(env, clp);
1187                 cl_page_clip(env, clp, 0, page_size);
1188
1189                 offset += page_size;
1190         }
1191
1192         if (rc == 0) {
1193                 enum cl_req_type typ = rw == READ ? CRT_READ : CRT_WRITE;
1194
1195                 async = async && (typ == CRT_WRITE);
1196                 if (async)
1197                         rc = cl_echo_async_brw(env, io, typ, queue);
1198                 else
1199                         rc = cl_io_submit_sync(env, io, typ, queue,
1200                                                CRP_NORMAL, 0);
1201                 CDEBUG(D_INFO, "echo_client %s write returns %d\n",
1202                        async ? "async" : "sync", rc);
1203         }
1204
1205         cl_echo_cancel0(env, ed, lh.cookie);
1206         EXIT;
1207 error_lock:
1208         cl_2queue_discard(env, io, queue);
1209         cl_2queue_disown(env, io, queue);
1210         cl_2queue_fini(env, queue);
1211         cl_io_fini(env, io);
1212 out:
1213         cl_env_put(env, &refcheck);
1214         return rc;
1215 }
1216 /** @} echo_exports */
1217
1218
1219 static obd_id last_object_id;
1220
1221 static int
1222 echo_copyout_lsm (struct lov_stripe_md *lsm, void *_ulsm, int ulsm_nob)
1223 {
1224         struct lov_stripe_md *ulsm = _ulsm;
1225         int nob, i;
1226
1227         nob = offsetof (struct lov_stripe_md, lsm_oinfo[lsm->lsm_stripe_count]);
1228         if (nob > ulsm_nob)
1229                 return (-EINVAL);
1230
1231         if (cfs_copy_to_user (ulsm, lsm, sizeof(ulsm)))
1232                 return (-EFAULT);
1233
1234         for (i = 0; i < lsm->lsm_stripe_count; i++) {
1235                 if (cfs_copy_to_user (ulsm->lsm_oinfo[i], lsm->lsm_oinfo[i],
1236                                       sizeof(lsm->lsm_oinfo[0])))
1237                         return (-EFAULT);
1238         }
1239         return 0;
1240 }
1241
1242 static int
1243 echo_copyin_lsm (struct echo_device *ed, struct lov_stripe_md *lsm,
1244                  void *ulsm, int ulsm_nob)
1245 {
1246         struct echo_client_obd *ec = ed->ed_ec;
1247         int                     i;
1248
1249         if (ulsm_nob < sizeof (*lsm))
1250                 return (-EINVAL);
1251
1252         if (cfs_copy_from_user (lsm, ulsm, sizeof (*lsm)))
1253                 return (-EFAULT);
1254
1255         if (lsm->lsm_stripe_count > ec->ec_nstripes ||
1256             lsm->lsm_magic != LOV_MAGIC ||
1257             (lsm->lsm_stripe_size & (~CFS_PAGE_MASK)) != 0 ||
1258             ((__u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count > ~0UL))
1259                 return (-EINVAL);
1260
1261
1262         for (i = 0; i < lsm->lsm_stripe_count; i++) {
1263                 if (cfs_copy_from_user(lsm->lsm_oinfo[i],
1264                                        ((struct lov_stripe_md *)ulsm)-> \
1265                                        lsm_oinfo[i],
1266                                        sizeof(lsm->lsm_oinfo[0])))
1267                         return (-EFAULT);
1268         }
1269         return (0);
1270 }
1271
1272 static int echo_create_object(struct echo_device *ed, int on_target,
1273                               struct obdo *oa, void *ulsm, int ulsm_nob,
1274                               struct obd_trans_info *oti)
1275 {
1276         struct echo_object     *eco;
1277         struct echo_client_obd *ec = ed->ed_ec;
1278         struct lov_stripe_md   *lsm = NULL;
1279         int                     rc;
1280         int                     created = 0;
1281         ENTRY;
1282
1283         if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
1284             (on_target ||                       /* set_stripe */
1285              ec->ec_nstripes != 0)) {           /* LOV */
1286                 CERROR ("No valid oid\n");
1287                 RETURN(-EINVAL);
1288         }
1289
1290         rc = obd_alloc_memmd(ec->ec_exp, &lsm);
1291         if (rc < 0) {
1292                 CERROR("Cannot allocate md, rc = %d\n", rc);
1293                 GOTO(failed, rc);
1294         }
1295
1296         if (ulsm != NULL) {
1297                 int i, idx;
1298
1299                 rc = echo_copyin_lsm (ed, lsm, ulsm, ulsm_nob);
1300                 if (rc != 0)
1301                         GOTO(failed, rc);
1302
1303                 if (lsm->lsm_stripe_count == 0)
1304                         lsm->lsm_stripe_count = ec->ec_nstripes;
1305
1306                 if (lsm->lsm_stripe_size == 0)
1307                         lsm->lsm_stripe_size = CFS_PAGE_SIZE;
1308
1309                 idx = ll_rand();
1310
1311                 /* setup stripes: indices + default ids if required */
1312                 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1313                         if (lsm->lsm_oinfo[i]->loi_id == 0)
1314                                 lsm->lsm_oinfo[i]->loi_id = lsm->lsm_object_id;
1315
1316                         lsm->lsm_oinfo[i]->loi_ost_idx =
1317                                 (idx + i) % ec->ec_nstripes;
1318                 }
1319         }
1320
1321         /* setup object ID here for !on_target and LOV hint */
1322         if (oa->o_valid & OBD_MD_FLID)
1323                 lsm->lsm_object_id = oa->o_id;
1324
1325         if (lsm->lsm_object_id == 0)
1326                 lsm->lsm_object_id = ++last_object_id;
1327
1328         rc = 0;
1329         if (on_target) {
1330                 /* Only echo objects are allowed to be created */
1331                 LASSERT((oa->o_valid & OBD_MD_FLGROUP) &&
1332                         (oa->o_gr == FILTER_GROUP_ECHO));
1333                 rc = obd_create(ec->ec_exp, oa, &lsm, oti);
1334                 if (rc != 0) {
1335                         CERROR("Cannot create objects, rc = %d\n", rc);
1336                         GOTO(failed, rc);
1337                 }
1338                 created = 1;
1339         }
1340
1341         /* See what object ID we were given */
1342         oa->o_id = lsm->lsm_object_id;
1343         oa->o_valid |= OBD_MD_FLID;
1344
1345         eco = cl_echo_object_find(ed, &lsm);
1346         if (IS_ERR(eco))
1347                 GOTO(failed, rc = PTR_ERR(eco));
1348         cl_echo_object_put(eco);
1349
1350         CDEBUG(D_INFO, "oa->o_id = %lx\n", (long)oa->o_id);
1351         EXIT;
1352
1353  failed:
1354         if (created && rc)
1355                 obd_destroy(ec->ec_exp, oa, lsm, oti, NULL, NULL);
1356         if (lsm)
1357                 obd_free_memmd(ec->ec_exp, &lsm);
1358         if (rc)
1359                 CERROR("create object failed with rc = %d\n", rc);
1360         return (rc);
1361 }
1362
1363 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
1364                            struct obdo *oa)
1365 {
1366         struct echo_client_obd *ec  = ed->ed_ec;
1367         struct lov_stripe_md   *lsm = NULL;
1368         struct echo_object     *eco;
1369         int                     rc;
1370         ENTRY;
1371
1372         if ((oa->o_valid & OBD_MD_FLID) == 0 ||
1373             oa->o_id == 0)  /* disallow use of object id 0 */
1374         {
1375                 CERROR ("No valid oid\n");
1376                 RETURN(-EINVAL);
1377         }
1378
1379         rc = obd_alloc_memmd(ec->ec_exp, &lsm);
1380         if (rc < 0)
1381                 RETURN(rc);
1382
1383         lsm->lsm_object_id = oa->o_id;
1384         if (oa->o_valid & OBD_MD_FLGROUP)
1385                 lsm->lsm_object_gr = oa->o_gr;
1386         else
1387                 lsm->lsm_object_gr = FILTER_GROUP_ECHO;
1388
1389         rc = 0;
1390         eco = cl_echo_object_find(ed, &lsm);
1391         if (!IS_ERR(eco))
1392                 *ecop = eco;
1393         else
1394                 rc = PTR_ERR(eco);
1395         if (lsm)
1396                 obd_free_memmd(ec->ec_exp, &lsm);
1397         RETURN(rc);
1398 }
1399
1400 static void echo_put_object(struct echo_object *eco)
1401 {
1402         if (cl_echo_object_put(eco))
1403                 CERROR("echo client: drop an object failed");
1404 }
1405
1406 static void
1407 echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
1408 {
1409         unsigned long stripe_count;
1410         unsigned long stripe_size;
1411         unsigned long width;
1412         unsigned long woffset;
1413         int           stripe_index;
1414         obd_off       offset;
1415
1416         if (lsm->lsm_stripe_count <= 1)
1417                 return;
1418
1419         offset       = *offp;
1420         stripe_size  = lsm->lsm_stripe_size;
1421         stripe_count = lsm->lsm_stripe_count;
1422
1423         /* width = # bytes in all stripes */
1424         width = stripe_size * stripe_count;
1425
1426         /* woffset = offset within a width; offset = whole number of widths */
1427         woffset = do_div (offset, width);
1428
1429         stripe_index = woffset / stripe_size;
1430
1431         *idp = lsm->lsm_oinfo[stripe_index]->loi_id;
1432         *offp = offset * stripe_size + woffset % stripe_size;
1433 }
1434
1435 static void
1436 echo_client_page_debug_setup(struct lov_stripe_md *lsm,
1437                              cfs_page_t *page, int rw, obd_id id,
1438                              obd_off offset, obd_off count)
1439 {
1440         char    *addr;
1441         obd_off  stripe_off;
1442         obd_id   stripe_id;
1443         int      delta;
1444
1445         /* no partial pages on the client */
1446         LASSERT(count == CFS_PAGE_SIZE);
1447
1448         addr = cfs_kmap(page);
1449
1450         for (delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
1451                 if (rw == OBD_BRW_WRITE) {
1452                         stripe_off = offset + delta;
1453                         stripe_id = id;
1454                         echo_get_stripe_off_id(lsm, &stripe_off, &stripe_id);
1455                 } else {
1456                         stripe_off = 0xdeadbeef00c0ffeeULL;
1457                         stripe_id = 0xdeadbeef00c0ffeeULL;
1458                 }
1459                 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
1460                                   stripe_off, stripe_id);
1461         }
1462
1463         cfs_kunmap(page);
1464 }
1465
1466 static int echo_client_page_debug_check(struct lov_stripe_md *lsm,
1467                                         cfs_page_t *page, obd_id id,
1468                                         obd_off offset, obd_off count)
1469 {
1470         obd_off stripe_off;
1471         obd_id  stripe_id;
1472         char   *addr;
1473         int     delta;
1474         int     rc;
1475         int     rc2;
1476
1477         /* no partial pages on the client */
1478         LASSERT(count == CFS_PAGE_SIZE);
1479
1480         addr = cfs_kmap(page);
1481
1482         for (rc = delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
1483                 stripe_off = offset + delta;
1484                 stripe_id = id;
1485                 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
1486
1487                 rc2 = block_debug_check("test_brw",
1488                                         addr + delta, OBD_ECHO_BLOCK_SIZE,
1489                                         stripe_off, stripe_id);
1490                 if (rc2 != 0) {
1491                         CERROR ("Error in echo object "LPX64"\n", id);
1492                         rc = rc2;
1493                 }
1494         }
1495
1496         cfs_kunmap(page);
1497         return rc;
1498 }
1499
1500 static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
1501                             struct echo_object *eco, obd_off offset,
1502                             obd_size count, int async,
1503                             struct obd_trans_info *oti)
1504 {
1505         struct echo_client_obd *ec  = ed->ed_ec;
1506         struct lov_stripe_md   *lsm = eco->eo_lsm;
1507         obd_count               npages;
1508         struct brw_page        *pga;
1509         struct brw_page        *pgp;
1510         cfs_page_t            **pages;
1511         obd_off                 off;
1512         int                     i;
1513         int                     rc;
1514         int                     verify;
1515         int                     gfp_mask;
1516         ENTRY;
1517
1518         verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
1519                   (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
1520                   (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
1521
1522         gfp_mask = ((oa->o_id & 2) == 0) ? CFS_ALLOC_STD : CFS_ALLOC_HIGHUSER;
1523
1524         LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
1525         LASSERT(lsm != NULL);
1526         LASSERT(lsm->lsm_object_id == oa->o_id);
1527
1528         if (count <= 0 ||
1529             (count & (~CFS_PAGE_MASK)) != 0)
1530                 RETURN(-EINVAL);
1531
1532         /* XXX think again with misaligned I/O */
1533         npages = count >> CFS_PAGE_SHIFT;
1534
1535         OBD_ALLOC(pga, npages * sizeof(*pga));
1536         if (pga == NULL)
1537                 RETURN(-ENOMEM);
1538
1539         OBD_ALLOC(pages, npages * sizeof(*pages));
1540         if (pages == NULL) {
1541                 OBD_FREE(pga, npages * sizeof(*pga));
1542                 RETURN(-ENOMEM);
1543         }
1544
1545         for (i = 0, pgp = pga, off = offset;
1546              i < npages;
1547              i++, pgp++, off += CFS_PAGE_SIZE) {
1548
1549                 LASSERT (pgp->pg == NULL);      /* for cleanup */
1550
1551                 rc = -ENOMEM;
1552                 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
1553                 if (pgp->pg == NULL)
1554                         goto out;
1555
1556                 pages[i] = pgp->pg;
1557                 pgp->count = CFS_PAGE_SIZE;
1558                 pgp->off = off;
1559                 pgp->flag = 0;
1560
1561                 if (verify)
1562                         echo_client_page_debug_setup(lsm, pgp->pg, rw,
1563                                                      oa->o_id, off, pgp->count);
1564         }
1565
1566         if (ed->ed_next == NULL) {
1567                 struct obd_info oinfo = { { { 0 } } };
1568                 oinfo.oi_oa = oa;
1569                 oinfo.oi_md = lsm;
1570                 rc = obd_brw(rw, ec->ec_exp, &oinfo, npages, pga, oti);
1571         } else
1572                 rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
1573
1574  out:
1575         if (rc != 0 || rw != OBD_BRW_READ)
1576                 verify = 0;
1577
1578         for (i = 0, pgp = pga; i < npages; i++, pgp++) {
1579                 if (pgp->pg == NULL)
1580                         continue;
1581
1582                 if (verify) {
1583                         int vrc;
1584                         vrc = echo_client_page_debug_check(lsm, pgp->pg, oa->o_id,
1585                                                            pgp->off, pgp->count);
1586                         if (vrc != 0 && rc == 0)
1587                                 rc = vrc;
1588                 }
1589                 OBD_PAGE_FREE(pgp->pg);
1590         }
1591         OBD_FREE(pga, npages * sizeof(*pga));
1592         OBD_FREE(pages, npages * sizeof(*pages));
1593         RETURN(rc);
1594 }
1595
1596 static int echo_client_prep_commit(struct obd_export *exp, int rw,
1597                                    struct obdo *oa, struct echo_object *eco,
1598                                    obd_off offset, obd_size count,
1599                                    obd_size batch, struct obd_trans_info *oti)
1600 {
1601         struct lov_stripe_md *lsm = eco->eo_lsm;
1602         struct obd_ioobj ioo;
1603         struct niobuf_local *lnb;
1604         struct niobuf_remote *rnb;
1605         obd_off off;
1606         obd_size npages, tot_pages;
1607         int i, ret = 0;
1608         ENTRY;
1609
1610         if (count <= 0 || (count & (~CFS_PAGE_MASK)) != 0 ||
1611             (lsm != NULL && lsm->lsm_object_id != oa->o_id))
1612                 RETURN(-EINVAL);
1613
1614         npages = batch >> CFS_PAGE_SHIFT;
1615         tot_pages = count >> CFS_PAGE_SHIFT;
1616
1617         OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
1618         OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
1619
1620         if (lnb == NULL || rnb == NULL)
1621                 GOTO(out, ret = -ENOMEM);
1622
1623         obdo_to_ioobj(oa, &ioo);
1624
1625         off = offset;
1626
1627         for(; tot_pages; tot_pages -= npages) {
1628                 int lpages;
1629
1630                 if (tot_pages < npages)
1631                         npages = tot_pages;
1632
1633                 for (i = 0; i < npages; i++, off += CFS_PAGE_SIZE) {
1634                         rnb[i].offset = off;
1635                         rnb[i].len = CFS_PAGE_SIZE;
1636                 }
1637
1638                 ioo.ioo_bufcnt = npages;
1639                 oti->oti_transno = 0;
1640
1641                 lpages = npages;
1642                 ret = obd_preprw(rw, exp, oa, 1, &ioo, rnb, &lpages, lnb, oti,
1643                                  NULL);
1644                 if (ret != 0)
1645                         GOTO(out, ret);
1646                 LASSERT(lpages == npages);
1647
1648                 for (i = 0; i < lpages; i++) {
1649                         cfs_page_t *page = lnb[i].page;
1650
1651                         /* read past eof? */
1652                         if (page == NULL && lnb[i].rc == 0)
1653                                 continue;
1654
1655                         if (oa->o_id == ECHO_PERSISTENT_OBJID ||
1656                             (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
1657                             (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
1658                                 continue;
1659
1660                         if (rw == OBD_BRW_WRITE)
1661                                 echo_client_page_debug_setup(lsm, page, rw,
1662                                                              oa->o_id,
1663                                                              rnb[i].offset,
1664                                                              rnb[i].len);
1665                         else
1666                                 echo_client_page_debug_check(lsm, page,
1667                                                              oa->o_id,
1668                                                              rnb[i].offset,
1669                                                              rnb[i].len);
1670                 }
1671
1672                 ret = obd_commitrw(rw, exp, oa, 1,&ioo,rnb,npages,lnb,oti,ret);
1673                 if (ret != 0)
1674                         GOTO(out, ret);
1675
1676                 /* Reset oti otherwise it would confuse ldiskfs. */
1677                 memset(oti, 0, sizeof(*oti));
1678         }
1679
1680 out:
1681         if (lnb)
1682                 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
1683         if (rnb)
1684                 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
1685         RETURN(ret);
1686 }
1687
1688 static int echo_client_brw_ioctl(int rw, struct obd_export *exp,
1689                                  struct obd_ioctl_data *data)
1690 {
1691         struct obd_device *obd = class_exp2obd(exp);
1692         struct echo_device *ed = obd2echo_dev(obd);
1693         struct echo_client_obd *ec = ed->ed_ec;
1694         struct obd_trans_info dummy_oti = { .oti_thread = NULL };
1695         struct obdo *oa = &data->ioc_obdo1;
1696         struct echo_object *eco;
1697         int rc;
1698         int async = 1;
1699         ENTRY;
1700
1701         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
1702
1703         rc = echo_get_object(&eco, ed, oa);
1704         if (rc)
1705                 RETURN(rc);
1706
1707         oa->o_valid &= ~OBD_MD_FLHANDLE;
1708
1709         switch((long)data->ioc_pbuf1) {
1710         case 1:
1711                 async = 0;
1712                 /* fall through */
1713         case 2:
1714                 rc = echo_client_kbrw(ed, rw, oa,
1715                                       eco, data->ioc_offset,
1716                                       data->ioc_count, async, &dummy_oti);
1717                 break;
1718         case 3:
1719                 rc = echo_client_prep_commit(ec->ec_exp, rw, oa,
1720                                             eco, data->ioc_offset,
1721                                             data->ioc_count, data->ioc_plen1,
1722                                             &dummy_oti);
1723                 break;
1724         default:
1725                 rc = -EINVAL;
1726         }
1727         echo_put_object(eco);
1728         RETURN(rc);
1729 }
1730
1731 static int
1732 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
1733                     int mode, obd_off offset, obd_size nob)
1734 {
1735         struct echo_device     *ed = obd2echo_dev(exp->exp_obd);
1736         struct lustre_handle   *ulh = &oa->o_handle;
1737         struct echo_object     *eco;
1738         obd_off                 end;
1739         int                     rc;
1740         ENTRY;
1741
1742         if (ed->ed_next == NULL)
1743                 RETURN(-EOPNOTSUPP);
1744
1745         if (!(mode == LCK_PR || mode == LCK_PW))
1746                 RETURN(-EINVAL);
1747
1748         if ((offset & (~CFS_PAGE_MASK)) != 0 ||
1749             (nob & (~CFS_PAGE_MASK)) != 0)
1750                 RETURN(-EINVAL);
1751
1752         rc = echo_get_object (&eco, ed, oa);
1753         if (rc != 0)
1754                 RETURN(rc);
1755
1756         end = (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
1757         rc = cl_echo_enqueue(eco, offset, end, mode, &ulh->cookie);
1758         if (rc == 0) {
1759                 oa->o_valid |= OBD_MD_FLHANDLE;
1760                 CDEBUG(D_INFO, "Cookie is %llx\n", ulh->cookie);
1761         }
1762         echo_put_object(eco);
1763         RETURN(rc);
1764 }
1765
1766 static int
1767 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
1768 {
1769         struct echo_device *ed     = obd2echo_dev(exp->exp_obd);
1770         __u64               cookie = oa->o_handle.cookie;
1771
1772         if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
1773                 return -EINVAL;
1774
1775         CDEBUG(D_INFO, "Cookie is %llx\n", cookie);
1776         return cl_echo_cancel(ed, cookie);
1777 }
1778
1779 static int
1780 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
1781                       int len, void *karg, void *uarg)
1782 {
1783         struct obd_device      *obd = exp->exp_obd;
1784         struct echo_device     *ed = obd2echo_dev(obd);
1785         struct echo_client_obd *ec = ed->ed_ec;
1786         struct echo_object     *eco;
1787         struct obd_ioctl_data  *data = karg;
1788         struct obd_trans_info   dummy_oti;
1789         struct oti_req_ack_lock *ack_lock;
1790         struct obdo            *oa;
1791         int                     rw = OBD_BRW_READ;
1792         int                     rc = 0;
1793         int                     i;
1794         ENTRY;
1795
1796         cfs_unlock_kernel();
1797
1798         memset(&dummy_oti, 0, sizeof(dummy_oti));
1799
1800         oa = &data->ioc_obdo1;
1801         if (!(oa->o_valid & OBD_MD_FLGROUP)) {
1802                 oa->o_valid |= OBD_MD_FLGROUP;
1803                 oa->o_gr = FILTER_GROUP_ECHO;
1804         }
1805         /* assume we can touch filter native objects with echo device. */
1806         /* LASSERT(oa->o_gr == FILTER_GROUP_ECHO); */
1807
1808         switch (cmd) {
1809         case OBD_IOC_CREATE:                    /* may create echo object */
1810                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1811                         GOTO (out, rc = -EPERM);
1812
1813                 rc = echo_create_object (ed, 1, oa,
1814                                          data->ioc_pbuf1, data->ioc_plen1,
1815                                          &dummy_oti);
1816                 GOTO(out, rc);
1817
1818         case OBD_IOC_DESTROY:
1819                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1820                         GOTO (out, rc = -EPERM);
1821
1822                 rc = echo_get_object (&eco, ed, oa);
1823                 if (rc == 0) {
1824                         rc = obd_destroy(ec->ec_exp, oa, eco->eo_lsm,
1825                                          &dummy_oti, NULL, NULL);
1826                         if (rc == 0)
1827                                 eco->eo_deleted = 1;
1828                         echo_put_object(eco);
1829                 }
1830                 GOTO(out, rc);
1831
1832         case OBD_IOC_GETATTR:
1833                 rc = echo_get_object (&eco, ed, oa);
1834                 if (rc == 0) {
1835                         struct obd_info oinfo = { { { 0 } } };
1836                         oinfo.oi_md = eco->eo_lsm;
1837                         oinfo.oi_oa = oa;
1838                         rc = obd_getattr(ec->ec_exp, &oinfo);
1839                         echo_put_object(eco);
1840                 }
1841                 GOTO(out, rc);
1842
1843         case OBD_IOC_SETATTR:
1844                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1845                         GOTO (out, rc = -EPERM);
1846
1847                 rc = echo_get_object (&eco, ed, oa);
1848                 if (rc == 0) {
1849                         struct obd_info oinfo = { { { 0 } } };
1850                         oinfo.oi_oa = oa;
1851                         oinfo.oi_md = eco->eo_lsm;
1852
1853                         rc = obd_setattr(ec->ec_exp, &oinfo, NULL);
1854                         echo_put_object(eco);
1855                 }
1856                 GOTO(out, rc);
1857
1858         case OBD_IOC_BRW_WRITE:
1859                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1860                         GOTO (out, rc = -EPERM);
1861
1862                 rw = OBD_BRW_WRITE;
1863                 /* fall through */
1864         case OBD_IOC_BRW_READ:
1865                 rc = echo_client_brw_ioctl(rw, exp, data);
1866                 GOTO(out, rc);
1867
1868         case ECHO_IOC_GET_STRIPE:
1869                 rc = echo_get_object(&eco, ed, oa);
1870                 if (rc == 0) {
1871                         rc = echo_copyout_lsm(eco->eo_lsm, data->ioc_pbuf1,
1872                                               data->ioc_plen1);
1873                         echo_put_object(eco);
1874                 }
1875                 GOTO(out, rc);
1876
1877         case ECHO_IOC_SET_STRIPE:
1878                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1879                         GOTO (out, rc = -EPERM);
1880
1881                 if (data->ioc_pbuf1 == NULL) {  /* unset */
1882                         rc = echo_get_object(&eco, ed, oa);
1883                         if (rc == 0) {
1884                                 eco->eo_deleted = 1;
1885                                 echo_put_object(eco);
1886                         }
1887                 } else {
1888                         rc = echo_create_object(ed, 0, oa,
1889                                                 data->ioc_pbuf1,
1890                                                 data->ioc_plen1, &dummy_oti);
1891                 }
1892                 GOTO (out, rc);
1893
1894         case ECHO_IOC_ENQUEUE:
1895                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1896                         GOTO (out, rc = -EPERM);
1897
1898                 rc = echo_client_enqueue(exp, oa,
1899                                          data->ioc_conn1, /* lock mode */
1900                                          data->ioc_offset,
1901                                          data->ioc_count);/*extent*/
1902                 GOTO (out, rc);
1903
1904         case ECHO_IOC_CANCEL:
1905                 rc = echo_client_cancel(exp, oa);
1906                 GOTO (out, rc);
1907
1908         default:
1909                 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
1910                 GOTO (out, rc = -ENOTTY);
1911         }
1912
1913         EXIT;
1914  out:
1915
1916         /* XXX this should be in a helper also called by target_send_reply */
1917         for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
1918              i++, ack_lock++) {
1919                 if (!ack_lock->mode)
1920                         break;
1921                 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
1922         }
1923
1924         cfs_lock_kernel();
1925
1926         return rc;
1927 }
1928
1929 static int echo_client_setup(struct obd_device *obddev, struct lustre_cfg *lcfg)
1930 {
1931         struct echo_client_obd *ec = &obddev->u.echo_client;
1932         struct obd_device *tgt;
1933         struct obd_uuid echo_uuid = { "ECHO_UUID" };
1934         struct obd_connect_data *ocd = NULL;
1935         int rc;
1936         ENTRY;
1937
1938         if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1939                 CERROR("requires a TARGET OBD name\n");
1940                 RETURN(-EINVAL);
1941         }
1942
1943         tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
1944         if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
1945                 CERROR("device not attached or not set up (%s)\n",
1946                        lustre_cfg_string(lcfg, 1));
1947                 RETURN(-EINVAL);
1948         }
1949
1950         cfs_spin_lock_init (&ec->ec_lock);
1951         CFS_INIT_LIST_HEAD (&ec->ec_objects);
1952         CFS_INIT_LIST_HEAD (&ec->ec_locks);
1953         ec->ec_unique = 0;
1954         ec->ec_nstripes = 0;
1955
1956         OBD_ALLOC(ocd, sizeof(*ocd));
1957         if (ocd == NULL) {
1958                 CERROR("Can't alloc ocd connecting to %s\n",
1959                        lustre_cfg_string(lcfg, 1));
1960                 return -ENOMEM;
1961         }
1962
1963         ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
1964                                  OBD_CONNECT_GRANT;
1965         ocd->ocd_version = LUSTRE_VERSION_CODE;
1966         ocd->ocd_group = FILTER_GROUP_ECHO;
1967
1968         rc = obd_connect(NULL, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
1969         if (rc == 0) {
1970                 /* Turn off pinger because it connects to tgt obd directly. */
1971                 cfs_spin_lock(&tgt->obd_dev_lock);
1972                 cfs_list_del_init(&ec->ec_exp->exp_obd_chain_timed);
1973                 cfs_spin_unlock(&tgt->obd_dev_lock);
1974         }
1975
1976         OBD_FREE(ocd, sizeof(*ocd));
1977
1978         if (rc != 0) {
1979                 CERROR("fail to connect to device %s\n",
1980                        lustre_cfg_string(lcfg, 1));
1981                 return (rc);
1982         }
1983
1984         RETURN(rc);
1985 }
1986
1987 static int echo_client_cleanup(struct obd_device *obddev)
1988 {
1989         struct echo_client_obd *ec = &obddev->u.echo_client;
1990         int rc;
1991         ENTRY;
1992
1993         if (!cfs_list_empty(&obddev->obd_exports)) {
1994                 CERROR("still has clients!\n");
1995                 RETURN(-EBUSY);
1996         }
1997
1998         LASSERT(cfs_atomic_read(&ec->ec_exp->exp_refcount) > 0);
1999         rc = obd_disconnect(ec->ec_exp);
2000         if (rc != 0)
2001                 CERROR("fail to disconnect device: %d\n", rc);
2002
2003         RETURN(rc);
2004 }
2005
2006 static int echo_client_connect(const struct lu_env *env,
2007                                struct obd_export **exp,
2008                                struct obd_device *src, struct obd_uuid *cluuid,
2009                                struct obd_connect_data *data, void *localdata)
2010 {
2011         int                rc;
2012         struct lustre_handle conn = { 0 };
2013
2014         ENTRY;
2015         rc = class_connect(&conn, src, cluuid);
2016         if (rc == 0) {
2017                 *exp = class_conn2export(&conn);
2018         }
2019
2020         RETURN (rc);
2021 }
2022
2023 static int echo_client_disconnect(struct obd_export *exp)
2024 {
2025 #if 0
2026         struct obd_device      *obd;
2027         struct echo_client_obd *ec;
2028         struct ec_lock         *ecl;
2029 #endif
2030         int                     rc;
2031         ENTRY;
2032
2033         if (exp == NULL)
2034                 GOTO(out, rc = -EINVAL);
2035
2036 #if 0
2037         obd = exp->exp_obd;
2038         ec = &obd->u.echo_client;
2039
2040         /* no more contention on export's lock list */
2041         while (!cfs_list_empty (&exp->exp_ec_data.eced_locks)) {
2042                 ecl = cfs_list_entry (exp->exp_ec_data.eced_locks.next,
2043                                       struct ec_lock, ecl_exp_chain);
2044                 cfs_list_del (&ecl->ecl_exp_chain);
2045
2046                 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm,
2047                                  ecl->ecl_mode, &ecl->ecl_lock_handle);
2048
2049                 CDEBUG (D_INFO, "Cancel lock on object "LPX64" on disconnect "
2050                         "(%d)\n", ecl->ecl_object->eco_id, rc);
2051
2052                 echo_put_object (ecl->ecl_object);
2053                 OBD_FREE (ecl, sizeof (*ecl));
2054         }
2055 #endif
2056
2057         rc = class_disconnect(exp);
2058         GOTO(out, rc);
2059  out:
2060         return rc;
2061 }
2062
2063 static struct obd_ops echo_obd_ops = {
2064         .o_owner       = THIS_MODULE,
2065
2066 #if 0
2067         .o_setup       = echo_client_setup,
2068         .o_cleanup     = echo_client_cleanup,
2069 #endif
2070
2071         .o_iocontrol   = echo_client_iocontrol,
2072         .o_connect     = echo_client_connect,
2073         .o_disconnect  = echo_client_disconnect
2074 };
2075
2076 int echo_client_init(void)
2077 {
2078         struct lprocfs_static_vars lvars = { 0 };
2079         int rc;
2080
2081         lprocfs_echo_init_vars(&lvars);
2082         rc = class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
2083                                  LUSTRE_ECHO_CLIENT_NAME, &echo_device_type);
2084         if (rc == 0)
2085                 lu_kmem_init(echo_caches);
2086         return rc;
2087 }
2088
2089 void echo_client_exit(void)
2090 {
2091         class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
2092         lu_kmem_fini(echo_caches);
2093 }
2094
2095 /** @} echo_client */