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