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