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