Whamcloud - gitweb
LU-2710 build: avoid function resolution mistakes by Coverity
[fs/lustre-release.git] / lustre / obdecho / echo_client.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
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 #include <lustre_fid.h>
51 #include <lustre_acl.h>
52 #include <lustre_net.h>
53 #include <obd_lov.h>
54
55 #include "echo_internal.h"
56
57 /** \defgroup echo_client Echo Client
58  * @{
59  */
60
61 struct echo_device {
62         struct cl_device        ed_cl;
63         struct echo_client_obd *ed_ec;
64
65         struct cl_site          ed_site_myself;
66         struct cl_site         *ed_site;
67         struct lu_device       *ed_next;
68         int                     ed_next_islov;
69         int                     ed_next_ismd;
70         struct lu_client_seq   *ed_cl_seq;
71 };
72
73 struct echo_object {
74         struct cl_object        eo_cl;
75         struct cl_object_header eo_hdr;
76
77         struct echo_device     *eo_dev;
78         cfs_list_t              eo_obj_chain;
79         struct lov_stripe_md   *eo_lsm;
80         cfs_atomic_t            eo_npages;
81         int                     eo_deleted;
82 };
83
84 struct echo_object_conf {
85         struct cl_object_conf  eoc_cl;
86         struct lov_stripe_md **eoc_md;
87 };
88
89 struct echo_page {
90         struct cl_page_slice   ep_cl;
91         struct mutex            ep_lock;
92         cfs_page_t            *ep_vmpage;
93 };
94
95 struct echo_lock {
96         struct cl_lock_slice   el_cl;
97         cfs_list_t             el_chain;
98         struct echo_object    *el_object;
99         __u64                  el_cookie;
100         cfs_atomic_t           el_refcount;
101 };
102
103 struct echo_io {
104         struct cl_io_slice     ei_cl;
105 };
106
107 #if 0
108 struct echo_req {
109         struct cl_req_slice er_cl;
110 };
111 #endif
112
113 static int echo_client_setup(const struct lu_env *env,
114                              struct obd_device *obddev,
115                              struct lustre_cfg *lcfg);
116 static int echo_client_cleanup(struct obd_device *obddev);
117
118
119 /** \defgroup echo_helpers Helper functions
120  * @{
121  */
122 static inline struct echo_device *cl2echo_dev(const struct cl_device *dev)
123 {
124         return container_of0(dev, struct echo_device, ed_cl);
125 }
126
127 static inline struct cl_device *echo_dev2cl(struct echo_device *d)
128 {
129         return &d->ed_cl;
130 }
131
132 static inline struct echo_device *obd2echo_dev(const struct obd_device *obd)
133 {
134         return cl2echo_dev(lu2cl_dev(obd->obd_lu_dev));
135 }
136
137 static inline struct cl_object *echo_obj2cl(struct echo_object *eco)
138 {
139         return &eco->eo_cl;
140 }
141
142 static inline struct echo_object *cl2echo_obj(const struct cl_object *o)
143 {
144         return container_of(o, struct echo_object, eo_cl);
145 }
146
147 static inline struct echo_page *cl2echo_page(const struct cl_page_slice *s)
148 {
149         return container_of(s, struct echo_page, ep_cl);
150 }
151
152 static inline struct echo_lock *cl2echo_lock(const struct cl_lock_slice *s)
153 {
154         return container_of(s, struct echo_lock, el_cl);
155 }
156
157 static inline struct cl_lock *echo_lock2cl(const struct echo_lock *ecl)
158 {
159         return ecl->el_cl.cls_lock;
160 }
161
162 static struct lu_context_key echo_thread_key;
163 static inline struct echo_thread_info *echo_env_info(const struct lu_env *env)
164 {
165         struct echo_thread_info *info;
166         info = lu_context_key_get(&env->le_ctx, &echo_thread_key);
167         LASSERT(info != NULL);
168         return info;
169 }
170
171 static inline
172 struct echo_object_conf *cl2echo_conf(const struct cl_object_conf *c)
173 {
174         return container_of(c, struct echo_object_conf, eoc_cl);
175 }
176
177 static inline void lsm2fid(struct lov_stripe_md *lsm, struct lu_fid *fid)
178 {
179         fid_zero(fid);
180         fid->f_seq = FID_SEQ_ECHO;
181         /* truncated to 32 bits by assignment */
182         fid->f_oid = lsm->lsm_object_id;
183         fid->f_ver = lsm->lsm_object_id >> 32;
184 }
185 /** @} echo_helpers */
186
187 static struct echo_object *cl_echo_object_find(struct echo_device *d,
188                                                struct lov_stripe_md **lsm);
189 static int cl_echo_object_put(struct echo_object *eco);
190 static int cl_echo_enqueue   (struct echo_object *eco, obd_off start,
191                               obd_off end, int mode, __u64 *cookie);
192 static int cl_echo_cancel    (struct echo_device *d, __u64 cookie);
193 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
194                               cfs_page_t **pages, int npages, int async);
195
196 static struct echo_thread_info *echo_env_info(const struct lu_env *env);
197
198 struct echo_thread_info {
199         struct echo_object_conf eti_conf;
200         struct lustre_md        eti_md;
201
202         struct cl_2queue        eti_queue;
203         struct cl_io            eti_io;
204         struct cl_lock_descr    eti_descr;
205         struct lu_fid           eti_fid;
206         struct lu_fid           eti_fid2;
207         struct md_op_spec       eti_spec;
208         struct lov_mds_md_v3    eti_lmm;
209         struct lov_user_md_v3   eti_lum;
210         struct md_attr          eti_ma;
211         struct lu_name          eti_lname;
212         /* per-thread values, can be re-used */
213         void                    *eti_big_lmm;
214         int                     eti_big_lmmsize;
215         char                    eti_name[20];
216         struct lu_buf           eti_buf;
217         char                    eti_xattr_buf[LUSTRE_POSIX_ACL_MAX_SIZE];
218 };
219
220 /* No session used right now */
221 struct echo_session_info {
222         unsigned long dummy;
223 };
224
225 static cfs_mem_cache_t *echo_lock_kmem;
226 static cfs_mem_cache_t *echo_object_kmem;
227 static cfs_mem_cache_t *echo_thread_kmem;
228 static cfs_mem_cache_t *echo_session_kmem;
229 //static cfs_mem_cache_t *echo_req_kmem;
230
231 static struct lu_kmem_descr echo_caches[] = {
232         {
233                 .ckd_cache = &echo_lock_kmem,
234                 .ckd_name  = "echo_lock_kmem",
235                 .ckd_size  = sizeof (struct echo_lock)
236         },
237         {
238                 .ckd_cache = &echo_object_kmem,
239                 .ckd_name  = "echo_object_kmem",
240                 .ckd_size  = sizeof (struct echo_object)
241         },
242         {
243                 .ckd_cache = &echo_thread_kmem,
244                 .ckd_name  = "echo_thread_kmem",
245                 .ckd_size  = sizeof (struct echo_thread_info)
246         },
247         {
248                 .ckd_cache = &echo_session_kmem,
249                 .ckd_name  = "echo_session_kmem",
250                 .ckd_size  = sizeof (struct echo_session_info)
251         },
252 #if 0
253         {
254                 .ckd_cache = &echo_req_kmem,
255                 .ckd_name  = "echo_req_kmem",
256                 .ckd_size  = sizeof (struct echo_req)
257         },
258 #endif
259         {
260                 .ckd_cache = NULL
261         }
262 };
263
264 /** \defgroup echo_page Page operations
265  *
266  * Echo page operations.
267  *
268  * @{
269  */
270 static cfs_page_t *echo_page_vmpage(const struct lu_env *env,
271                                     const struct cl_page_slice *slice)
272 {
273         return cl2echo_page(slice)->ep_vmpage;
274 }
275
276 static int echo_page_own(const struct lu_env *env,
277                          const struct cl_page_slice *slice,
278                          struct cl_io *io, int nonblock)
279 {
280         struct echo_page *ep = cl2echo_page(slice);
281
282         if (!nonblock)
283                 mutex_lock(&ep->ep_lock);
284         else if (!mutex_trylock(&ep->ep_lock))
285                 return -EAGAIN;
286         return 0;
287 }
288
289 static void echo_page_disown(const struct lu_env *env,
290                              const struct cl_page_slice *slice,
291                              struct cl_io *io)
292 {
293         struct echo_page *ep = cl2echo_page(slice);
294
295         LASSERT(mutex_is_locked(&ep->ep_lock));
296         mutex_unlock(&ep->ep_lock);
297 }
298
299 static void echo_page_discard(const struct lu_env *env,
300                               const struct cl_page_slice *slice,
301                               struct cl_io *unused)
302 {
303         cl_page_delete(env, slice->cpl_page);
304 }
305
306 static int echo_page_is_vmlocked(const struct lu_env *env,
307                                  const struct cl_page_slice *slice)
308 {
309         if (mutex_is_locked(&cl2echo_page(slice)->ep_lock))
310                 return -EBUSY;
311         return -ENODATA;
312 }
313
314 static void echo_page_completion(const struct lu_env *env,
315                                  const struct cl_page_slice *slice,
316                                  int ioret)
317 {
318         LASSERT(slice->cpl_page->cp_sync_io != NULL);
319 }
320
321 static void echo_page_fini(const struct lu_env *env,
322                            struct cl_page_slice *slice)
323 {
324         struct echo_page *ep    = cl2echo_page(slice);
325         struct echo_object *eco = cl2echo_obj(slice->cpl_obj);
326         cfs_page_t *vmpage      = ep->ep_vmpage;
327         ENTRY;
328
329         cfs_atomic_dec(&eco->eo_npages);
330         page_cache_release(vmpage);
331         EXIT;
332 }
333
334 static int echo_page_prep(const struct lu_env *env,
335                           const struct cl_page_slice *slice,
336                           struct cl_io *unused)
337 {
338         return 0;
339 }
340
341 static int echo_page_print(const struct lu_env *env,
342                            const struct cl_page_slice *slice,
343                            void *cookie, lu_printer_t printer)
344 {
345         struct echo_page *ep = cl2echo_page(slice);
346
347         (*printer)(env, cookie, LUSTRE_ECHO_CLIENT_NAME"-page@%p %d vm@%p\n",
348                    ep, mutex_is_locked(&ep->ep_lock), ep->ep_vmpage);
349         return 0;
350 }
351
352 static const struct cl_page_operations echo_page_ops = {
353         .cpo_own           = echo_page_own,
354         .cpo_disown        = echo_page_disown,
355         .cpo_discard       = echo_page_discard,
356         .cpo_vmpage        = echo_page_vmpage,
357         .cpo_fini          = echo_page_fini,
358         .cpo_print         = echo_page_print,
359         .cpo_is_vmlocked   = echo_page_is_vmlocked,
360         .io = {
361                 [CRT_READ] = {
362                         .cpo_prep        = echo_page_prep,
363                         .cpo_completion  = echo_page_completion,
364                 },
365                 [CRT_WRITE] = {
366                         .cpo_prep        = echo_page_prep,
367                         .cpo_completion  = echo_page_completion,
368                 }
369         }
370 };
371 /** @} echo_page */
372
373 /** \defgroup echo_lock Locking
374  *
375  * echo lock operations
376  *
377  * @{
378  */
379 static void echo_lock_fini(const struct lu_env *env,
380                            struct cl_lock_slice *slice)
381 {
382         struct echo_lock *ecl = cl2echo_lock(slice);
383
384         LASSERT(cfs_list_empty(&ecl->el_chain));
385         OBD_SLAB_FREE_PTR(ecl, echo_lock_kmem);
386 }
387
388 static void echo_lock_delete(const struct lu_env *env,
389                              const struct cl_lock_slice *slice)
390 {
391         struct echo_lock *ecl      = cl2echo_lock(slice);
392
393         LASSERT(cfs_list_empty(&ecl->el_chain));
394 }
395
396 static int echo_lock_fits_into(const struct lu_env *env,
397                                const struct cl_lock_slice *slice,
398                                const struct cl_lock_descr *need,
399                                const struct cl_io *unused)
400 {
401         return 1;
402 }
403
404 static struct cl_lock_operations echo_lock_ops = {
405         .clo_fini      = echo_lock_fini,
406         .clo_delete    = echo_lock_delete,
407         .clo_fits_into = echo_lock_fits_into
408 };
409
410 /** @} echo_lock */
411
412 /** \defgroup echo_cl_ops cl_object operations
413  *
414  * operations for cl_object
415  *
416  * @{
417  */
418 static int echo_page_init(const struct lu_env *env, struct cl_object *obj,
419                         struct cl_page *page, cfs_page_t *vmpage)
420 {
421         struct echo_page *ep = cl_object_page_slice(obj, page);
422         struct echo_object *eco = cl2echo_obj(obj);
423         ENTRY;
424
425         ep->ep_vmpage = vmpage;
426         page_cache_get(vmpage);
427         mutex_init(&ep->ep_lock);
428         cl_page_slice_add(page, &ep->ep_cl, obj, &echo_page_ops);
429         cfs_atomic_inc(&eco->eo_npages);
430         RETURN(0);
431 }
432
433 static int echo_io_init(const struct lu_env *env, struct cl_object *obj,
434                         struct cl_io *io)
435 {
436         return 0;
437 }
438
439 static int echo_lock_init(const struct lu_env *env,
440                           struct cl_object *obj, struct cl_lock *lock,
441                           const struct cl_io *unused)
442 {
443         struct echo_lock *el;
444         ENTRY;
445
446         OBD_SLAB_ALLOC_PTR_GFP(el, echo_lock_kmem, CFS_ALLOC_IO);
447         if (el != NULL) {
448                 cl_lock_slice_add(lock, &el->el_cl, obj, &echo_lock_ops);
449                 el->el_object = cl2echo_obj(obj);
450                 CFS_INIT_LIST_HEAD(&el->el_chain);
451                 cfs_atomic_set(&el->el_refcount, 0);
452         }
453         RETURN(el == NULL ? -ENOMEM : 0);
454 }
455
456 static int echo_conf_set(const struct lu_env *env, struct cl_object *obj,
457                          const struct cl_object_conf *conf)
458 {
459         return 0;
460 }
461
462 static const struct cl_object_operations echo_cl_obj_ops = {
463         .coo_page_init = echo_page_init,
464         .coo_lock_init = echo_lock_init,
465         .coo_io_init   = echo_io_init,
466         .coo_conf_set  = echo_conf_set
467 };
468 /** @} echo_cl_ops */
469
470 /** \defgroup echo_lu_ops lu_object operations
471  *
472  * operations for echo lu object.
473  *
474  * @{
475  */
476 static int echo_object_init(const struct lu_env *env, struct lu_object *obj,
477                             const struct lu_object_conf *conf)
478 {
479         struct echo_device *ed         = cl2echo_dev(lu2cl_dev(obj->lo_dev));
480         struct echo_client_obd *ec     = ed->ed_ec;
481         struct echo_object *eco        = cl2echo_obj(lu2cl(obj));
482         ENTRY;
483
484         if (ed->ed_next) {
485                 struct lu_object  *below;
486                 struct lu_device  *under;
487
488                 under = ed->ed_next;
489                 below = under->ld_ops->ldo_object_alloc(env, obj->lo_header,
490                                                         under);
491                 if (below == NULL)
492                         RETURN(-ENOMEM);
493                 lu_object_add(obj, below);
494         }
495
496         if (!ed->ed_next_ismd) {
497                 const struct cl_object_conf *cconf = lu2cl_conf(conf);
498                 struct echo_object_conf *econf = cl2echo_conf(cconf);
499
500                 LASSERT(econf->eoc_md);
501                 eco->eo_lsm = *econf->eoc_md;
502                 /* clear the lsm pointer so that it won't get freed. */
503                 *econf->eoc_md = NULL;
504         } else {
505                 eco->eo_lsm = NULL;
506         }
507
508         eco->eo_dev = ed;
509         cfs_atomic_set(&eco->eo_npages, 0);
510         cl_object_page_init(lu2cl(obj), sizeof(struct echo_page));
511
512         spin_lock(&ec->ec_lock);
513         cfs_list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
514         spin_unlock(&ec->ec_lock);
515
516         RETURN(0);
517 }
518
519 /* taken from osc_unpackmd() */
520 static int echo_alloc_memmd(struct echo_device *ed,
521                             struct lov_stripe_md **lsmp)
522 {
523         int lsm_size;
524
525         ENTRY;
526
527         /* If export is lov/osc then use their obd method */
528         if (ed->ed_next != NULL)
529                 return obd_alloc_memmd(ed->ed_ec->ec_exp, lsmp);
530         /* OFD has no unpackmd method, do everything here */
531         lsm_size = lov_stripe_md_size(1);
532
533         LASSERT(*lsmp == NULL);
534         OBD_ALLOC(*lsmp, lsm_size);
535         if (*lsmp == NULL)
536                 RETURN(-ENOMEM);
537
538         OBD_ALLOC((*lsmp)->lsm_oinfo[0], sizeof(struct lov_oinfo));
539         if ((*lsmp)->lsm_oinfo[0] == NULL) {
540                 OBD_FREE(*lsmp, lsm_size);
541                 RETURN(-ENOMEM);
542         }
543
544         loi_init((*lsmp)->lsm_oinfo[0]);
545         (*lsmp)->lsm_maxbytes = LUSTRE_STRIPE_MAXBYTES;
546
547         RETURN(lsm_size);
548 }
549
550 static int echo_free_memmd(struct echo_device *ed, struct lov_stripe_md **lsmp)
551 {
552         int lsm_size;
553
554         ENTRY;
555
556         /* If export is lov/osc then use their obd method */
557         if (ed->ed_next != NULL)
558                 return obd_free_memmd(ed->ed_ec->ec_exp, lsmp);
559         /* OFD has no unpackmd method, do everything here */
560         lsm_size = lov_stripe_md_size(1);
561
562         LASSERT(*lsmp != NULL);
563         OBD_FREE((*lsmp)->lsm_oinfo[0], sizeof(struct lov_oinfo));
564         OBD_FREE(*lsmp, lsm_size);
565         *lsmp = NULL;
566         RETURN(0);
567 }
568
569 static void echo_object_free(const struct lu_env *env, struct lu_object *obj)
570 {
571         struct echo_object *eco    = cl2echo_obj(lu2cl(obj));
572         struct echo_client_obd *ec = eco->eo_dev->ed_ec;
573         ENTRY;
574
575         LASSERT(cfs_atomic_read(&eco->eo_npages) == 0);
576
577         spin_lock(&ec->ec_lock);
578         cfs_list_del_init(&eco->eo_obj_chain);
579         spin_unlock(&ec->ec_lock);
580
581         lu_object_fini(obj);
582         lu_object_header_fini(obj->lo_header);
583
584         if (eco->eo_lsm)
585                 echo_free_memmd(eco->eo_dev, &eco->eo_lsm);
586         OBD_SLAB_FREE_PTR(eco, echo_object_kmem);
587         EXIT;
588 }
589
590 static int echo_object_print(const struct lu_env *env, void *cookie,
591                             lu_printer_t p, const struct lu_object *o)
592 {
593         struct echo_object *obj = cl2echo_obj(lu2cl(o));
594
595         return (*p)(env, cookie, "echoclient-object@%p", obj);
596 }
597
598 static const struct lu_object_operations echo_lu_obj_ops = {
599         .loo_object_init      = echo_object_init,
600         .loo_object_delete    = NULL,
601         .loo_object_release   = NULL,
602         .loo_object_free      = echo_object_free,
603         .loo_object_print     = echo_object_print,
604         .loo_object_invariant = NULL
605 };
606 /** @} echo_lu_ops */
607
608 /** \defgroup echo_lu_dev_ops  lu_device operations
609  *
610  * Operations for echo lu device.
611  *
612  * @{
613  */
614 static struct lu_object *echo_object_alloc(const struct lu_env *env,
615                                            const struct lu_object_header *hdr,
616                                            struct lu_device *dev)
617 {
618         struct echo_object *eco;
619         struct lu_object *obj = NULL;
620         ENTRY;
621
622         /* we're the top dev. */
623         LASSERT(hdr == NULL);
624         OBD_SLAB_ALLOC_PTR_GFP(eco, echo_object_kmem, CFS_ALLOC_IO);
625         if (eco != NULL) {
626                 struct cl_object_header *hdr = &eco->eo_hdr;
627
628                 obj = &echo_obj2cl(eco)->co_lu;
629                 cl_object_header_init(hdr);
630                 lu_object_init(obj, &hdr->coh_lu, dev);
631                 lu_object_add_top(&hdr->coh_lu, obj);
632
633                 eco->eo_cl.co_ops = &echo_cl_obj_ops;
634                 obj->lo_ops       = &echo_lu_obj_ops;
635         }
636         RETURN(obj);
637 }
638
639 static struct lu_device_operations echo_device_lu_ops = {
640         .ldo_object_alloc   = echo_object_alloc,
641 };
642
643 /** @} echo_lu_dev_ops */
644
645 static struct cl_device_operations echo_device_cl_ops = {
646 };
647
648 /** \defgroup echo_init Setup and teardown
649  *
650  * Init and fini functions for echo client.
651  *
652  * @{
653  */
654 static int echo_site_init(const struct lu_env *env, struct echo_device *ed)
655 {
656         struct cl_site *site = &ed->ed_site_myself;
657         int rc;
658
659         /* initialize site */
660         rc = cl_site_init(site, &ed->ed_cl);
661         if (rc) {
662                 CERROR("Cannot initilize site for echo client(%d)\n", rc);
663                 return rc;
664         }
665
666         rc = lu_site_init_finish(&site->cs_lu);
667         if (rc)
668                 return rc;
669
670         ed->ed_site = site;
671         return 0;
672 }
673
674 static void echo_site_fini(const struct lu_env *env, struct echo_device *ed)
675 {
676         if (ed->ed_site) {
677                 if (!ed->ed_next_ismd)
678                         cl_site_fini(ed->ed_site);
679                 ed->ed_site = NULL;
680         }
681 }
682
683 static void *echo_thread_key_init(const struct lu_context *ctx,
684                           struct lu_context_key *key)
685 {
686         struct echo_thread_info *info;
687
688         OBD_SLAB_ALLOC_PTR_GFP(info, echo_thread_kmem, CFS_ALLOC_IO);
689         if (info == NULL)
690                 info = ERR_PTR(-ENOMEM);
691         return info;
692 }
693
694 static void echo_thread_key_fini(const struct lu_context *ctx,
695                          struct lu_context_key *key, void *data)
696 {
697         struct echo_thread_info *info = data;
698         OBD_SLAB_FREE_PTR(info, echo_thread_kmem);
699 }
700
701 static void echo_thread_key_exit(const struct lu_context *ctx,
702                          struct lu_context_key *key, void *data)
703 {
704 }
705
706 static struct lu_context_key echo_thread_key = {
707         .lct_tags = LCT_CL_THREAD,
708         .lct_init = echo_thread_key_init,
709         .lct_fini = echo_thread_key_fini,
710         .lct_exit = echo_thread_key_exit
711 };
712
713 static void *echo_session_key_init(const struct lu_context *ctx,
714                                   struct lu_context_key *key)
715 {
716         struct echo_session_info *session;
717
718         OBD_SLAB_ALLOC_PTR_GFP(session, echo_session_kmem, CFS_ALLOC_IO);
719         if (session == NULL)
720                 session = ERR_PTR(-ENOMEM);
721         return session;
722 }
723
724 static void echo_session_key_fini(const struct lu_context *ctx,
725                                  struct lu_context_key *key, void *data)
726 {
727         struct echo_session_info *session = data;
728         OBD_SLAB_FREE_PTR(session, echo_session_kmem);
729 }
730
731 static void echo_session_key_exit(const struct lu_context *ctx,
732                                  struct lu_context_key *key, void *data)
733 {
734 }
735
736 static struct lu_context_key echo_session_key = {
737         .lct_tags = LCT_SESSION,
738         .lct_init = echo_session_key_init,
739         .lct_fini = echo_session_key_fini,
740         .lct_exit = echo_session_key_exit
741 };
742
743 LU_TYPE_INIT_FINI(echo, &echo_thread_key, &echo_session_key);
744
745 #define ECHO_SEQ_WIDTH 0xffffffff
746 static int echo_fid_init(struct echo_device *ed, char *obd_name,
747                          struct seq_server_site *ss)
748 {
749         char *prefix;
750         int rc;
751         ENTRY;
752
753         OBD_ALLOC_PTR(ed->ed_cl_seq);
754         if (ed->ed_cl_seq == NULL)
755                 RETURN(-ENOMEM);
756
757         OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
758         if (prefix == NULL)
759                 GOTO(out_free_seq, rc = -ENOMEM);
760
761         snprintf(prefix, MAX_OBD_NAME + 5, "srv-%s", obd_name);
762
763         /* Init client side sequence-manager */
764         rc = seq_client_init(ed->ed_cl_seq, NULL,
765                              LUSTRE_SEQ_METADATA,
766                              prefix, ss->ss_server_seq);
767         ed->ed_cl_seq->lcs_width = ECHO_SEQ_WIDTH;
768         OBD_FREE(prefix, MAX_OBD_NAME + 5);
769         if (rc)
770                 GOTO(out_free_seq, rc);
771
772         RETURN(0);
773
774 out_free_seq:
775         OBD_FREE_PTR(ed->ed_cl_seq);
776         ed->ed_cl_seq = NULL;
777         RETURN(rc);
778 }
779
780 static int echo_fid_fini(struct obd_device *obddev)
781 {
782         struct echo_device *ed = obd2echo_dev(obddev);
783         ENTRY;
784
785         if (ed->ed_cl_seq != NULL) {
786                 seq_client_fini(ed->ed_cl_seq);
787                 OBD_FREE_PTR(ed->ed_cl_seq);
788                 ed->ed_cl_seq = NULL;
789         }
790
791         RETURN(0);
792 }
793
794 static struct lu_device *echo_device_alloc(const struct lu_env *env,
795                                            struct lu_device_type *t,
796                                            struct lustre_cfg *cfg)
797 {
798         struct lu_device   *next;
799         struct echo_device *ed;
800         struct cl_device   *cd;
801         struct obd_device  *obd = NULL; /* to keep compiler happy */
802         struct obd_device  *tgt;
803         const char *tgt_type_name;
804         int rc;
805         int cleanup = 0;
806         ENTRY;
807
808         OBD_ALLOC_PTR(ed);
809         if (ed == NULL)
810                 GOTO(out, rc = -ENOMEM);
811
812         cleanup = 1;
813         cd = &ed->ed_cl;
814         rc = cl_device_init(cd, t);
815         if (rc)
816                 GOTO(out, rc);
817
818         cd->cd_lu_dev.ld_ops = &echo_device_lu_ops;
819         cd->cd_ops = &echo_device_cl_ops;
820
821         cleanup = 2;
822         obd = class_name2obd(lustre_cfg_string(cfg, 0));
823         LASSERT(obd != NULL);
824         LASSERT(env != NULL);
825
826         tgt = class_name2obd(lustre_cfg_string(cfg, 1));
827         if (tgt == NULL) {
828                 CERROR("Can not find tgt device %s\n",
829                         lustre_cfg_string(cfg, 1));
830                 GOTO(out, rc = -ENODEV);
831         }
832
833         next = tgt->obd_lu_dev;
834         if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
835                 ed->ed_next_ismd = 1;
836         } else {
837                 ed->ed_next_ismd = 0;
838                 rc = echo_site_init(env, ed);
839                 if (rc)
840                         GOTO(out, rc);
841         }
842         cleanup = 3;
843
844         rc = echo_client_setup(env, obd, cfg);
845         if (rc)
846                 GOTO(out, rc);
847
848         ed->ed_ec = &obd->u.echo_client;
849         cleanup = 4;
850
851         if (ed->ed_next_ismd) {
852                 /* Suppose to connect to some Metadata layer */
853                 struct lu_site *ls;
854                 struct lu_device *ld;
855                 int    found = 0;
856
857                 if (next == NULL) {
858                         CERROR("%s is not lu device type!\n",
859                                lustre_cfg_string(cfg, 1));
860                         GOTO(out, rc = -EINVAL);
861                 }
862
863                 tgt_type_name = lustre_cfg_string(cfg, 2);
864                 if (!tgt_type_name) {
865                         CERROR("%s no type name for echo %s setup\n",
866                                 lustre_cfg_string(cfg, 1),
867                                 tgt->obd_type->typ_name);
868                         GOTO(out, rc = -EINVAL);
869                 }
870
871                 ls = next->ld_site;
872
873                 spin_lock(&ls->ls_ld_lock);
874                 cfs_list_for_each_entry(ld, &ls->ls_ld_linkage, ld_linkage) {
875                         if (strcmp(ld->ld_type->ldt_name, tgt_type_name) == 0) {
876                                 found = 1;
877                                 break;
878                         }
879                 }
880                 spin_unlock(&ls->ls_ld_lock);
881
882                 if (found == 0) {
883                         CERROR("%s is not lu device type!\n",
884                                lustre_cfg_string(cfg, 1));
885                         GOTO(out, rc = -EINVAL);
886                 }
887
888                 next = ld;
889                 /* For MD echo client, it will use the site in MDS stack */
890                 ed->ed_site_myself.cs_lu = *ls;
891                 ed->ed_site = &ed->ed_site_myself;
892                 ed->ed_cl.cd_lu_dev.ld_site = &ed->ed_site_myself.cs_lu;
893                 rc = echo_fid_init(ed, obd->obd_name, lu_site2seq(ls));
894                 if (rc) {
895                         CERROR("echo fid init error %d\n", rc);
896                         GOTO(out, rc);
897                 }
898         } else {
899                  /* if echo client is to be stacked upon ost device, the next is
900                   * NULL since ost is not a clio device so far */
901                 if (next != NULL && !lu_device_is_cl(next))
902                         next = NULL;
903
904                 tgt_type_name = tgt->obd_type->typ_name;
905                 if (next != NULL) {
906                         LASSERT(next != NULL);
907                         if (next->ld_site != NULL)
908                                 GOTO(out, rc = -EBUSY);
909
910                         next->ld_site = &ed->ed_site->cs_lu;
911                         rc = next->ld_type->ldt_ops->ldto_device_init(env, next,
912                                                      next->ld_type->ldt_name,
913                                                      NULL);
914                         if (rc)
915                                 GOTO(out, rc);
916
917                         /* Tricky case, I have to determine the obd type since
918                          * CLIO uses the different parameters to initialize
919                          * objects for lov & osc. */
920                         if (strcmp(tgt_type_name, LUSTRE_LOV_NAME) == 0)
921                                 ed->ed_next_islov = 1;
922                         else
923                                 LASSERT(strcmp(tgt_type_name,
924                                                LUSTRE_OSC_NAME) == 0);
925                 } else
926                         LASSERT(strcmp(tgt_type_name, LUSTRE_OST_NAME) == 0);
927         }
928
929         ed->ed_next = next;
930         RETURN(&cd->cd_lu_dev);
931 out:
932         switch(cleanup) {
933         case 4: {
934                 int rc2;
935                 rc2 = echo_client_cleanup(obd);
936                 if (rc2)
937                         CERROR("Cleanup obd device %s error(%d)\n",
938                                obd->obd_name, rc2);
939         }
940
941         case 3:
942                 echo_site_fini(env, ed);
943         case 2:
944                 cl_device_fini(&ed->ed_cl);
945         case 1:
946                 OBD_FREE_PTR(ed);
947         case 0:
948         default:
949                 break;
950         }
951         return(ERR_PTR(rc));
952 }
953
954 static int echo_device_init(const struct lu_env *env, struct lu_device *d,
955                           const char *name, struct lu_device *next)
956 {
957         LBUG();
958         return 0;
959 }
960
961 static struct lu_device *echo_device_fini(const struct lu_env *env,
962                                           struct lu_device *d)
963 {
964         struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
965         struct lu_device *next = ed->ed_next;
966
967         while (next && !ed->ed_next_ismd)
968                 next = next->ld_type->ldt_ops->ldto_device_fini(env, next);
969         return NULL;
970 }
971
972 static void echo_lock_release(const struct lu_env *env,
973                               struct echo_lock *ecl,
974                               int still_used)
975 {
976         struct cl_lock *clk = echo_lock2cl(ecl);
977
978         cl_lock_get(clk);
979         cl_unuse(env, clk);
980         cl_lock_release(env, clk, "ec enqueue", ecl->el_object);
981         if (!still_used) {
982                 cl_lock_mutex_get(env, clk);
983                 cl_lock_cancel(env, clk);
984                 cl_lock_delete(env, clk);
985                 cl_lock_mutex_put(env, clk);
986         }
987         cl_lock_put(env, clk);
988 }
989
990 static struct lu_device *echo_device_free(const struct lu_env *env,
991                                           struct lu_device *d)
992 {
993         struct echo_device     *ed   = cl2echo_dev(lu2cl_dev(d));
994         struct echo_client_obd *ec   = ed->ed_ec;
995         struct echo_object     *eco;
996         struct lu_device       *next = ed->ed_next;
997
998         CDEBUG(D_INFO, "echo device:%p is going to be freed, next = %p\n",
999                ed, next);
1000
1001         lu_site_purge(env, &ed->ed_site->cs_lu, -1);
1002
1003         /* check if there are objects still alive.
1004          * It shouldn't have any object because lu_site_purge would cleanup
1005          * all of cached objects. Anyway, probably the echo device is being
1006          * parallelly accessed.
1007          */
1008         spin_lock(&ec->ec_lock);
1009         cfs_list_for_each_entry(eco, &ec->ec_objects, eo_obj_chain)
1010                 eco->eo_deleted = 1;
1011         spin_unlock(&ec->ec_lock);
1012
1013         /* purge again */
1014         lu_site_purge(env, &ed->ed_site->cs_lu, -1);
1015
1016         CDEBUG(D_INFO,
1017                "Waiting for the reference of echo object to be dropped\n");
1018
1019         /* Wait for the last reference to be dropped. */
1020         spin_lock(&ec->ec_lock);
1021         while (!cfs_list_empty(&ec->ec_objects)) {
1022                 spin_unlock(&ec->ec_lock);
1023                 CERROR("echo_client still has objects at cleanup time, "
1024                        "wait for 1 second\n");
1025                 cfs_schedule_timeout_and_set_state(CFS_TASK_UNINT,
1026                                                    cfs_time_seconds(1));
1027                 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
1028                 spin_lock(&ec->ec_lock);
1029         }
1030         spin_unlock(&ec->ec_lock);
1031
1032         LASSERT(cfs_list_empty(&ec->ec_locks));
1033
1034         CDEBUG(D_INFO, "No object exists, exiting...\n");
1035
1036         echo_client_cleanup(d->ld_obd);
1037         echo_fid_fini(d->ld_obd);
1038         while (next && !ed->ed_next_ismd)
1039                 next = next->ld_type->ldt_ops->ldto_device_free(env, next);
1040
1041         LASSERT(ed->ed_site == lu2cl_site(d->ld_site));
1042         echo_site_fini(env, ed);
1043         cl_device_fini(&ed->ed_cl);
1044         OBD_FREE_PTR(ed);
1045
1046         return NULL;
1047 }
1048
1049 static const struct lu_device_type_operations echo_device_type_ops = {
1050         .ldto_init = echo_type_init,
1051         .ldto_fini = echo_type_fini,
1052
1053         .ldto_start = echo_type_start,
1054         .ldto_stop  = echo_type_stop,
1055
1056         .ldto_device_alloc = echo_device_alloc,
1057         .ldto_device_free  = echo_device_free,
1058         .ldto_device_init  = echo_device_init,
1059         .ldto_device_fini  = echo_device_fini
1060 };
1061
1062 static struct lu_device_type echo_device_type = {
1063         .ldt_tags     = LU_DEVICE_CL,
1064         .ldt_name     = LUSTRE_ECHO_CLIENT_NAME,
1065         .ldt_ops      = &echo_device_type_ops,
1066         .ldt_ctx_tags = LCT_CL_THREAD | LCT_MD_THREAD | LCT_DT_THREAD,
1067 };
1068 /** @} echo_init */
1069
1070 /** \defgroup echo_exports Exported operations
1071  *
1072  * exporting functions to echo client
1073  *
1074  * @{
1075  */
1076
1077 /* Interfaces to echo client obd device */
1078 static struct echo_object *cl_echo_object_find(struct echo_device *d,
1079                                                struct lov_stripe_md **lsmp)
1080 {
1081         struct lu_env *env;
1082         struct echo_thread_info *info;
1083         struct echo_object_conf *conf;
1084         struct lov_stripe_md    *lsm;
1085         struct echo_object *eco;
1086         struct cl_object   *obj;
1087         struct lu_fid *fid;
1088         int refcheck;
1089         ENTRY;
1090
1091         LASSERT(lsmp);
1092         lsm = *lsmp;
1093         LASSERT(lsm);
1094         LASSERT(lsm->lsm_object_id);
1095
1096         /* Never return an object if the obd is to be freed. */
1097         if (echo_dev2cl(d)->cd_lu_dev.ld_obd->obd_stopping)
1098                 RETURN(ERR_PTR(-ENODEV));
1099
1100         env = cl_env_get(&refcheck);
1101         if (IS_ERR(env))
1102                 RETURN((void *)env);
1103
1104         info = echo_env_info(env);
1105         conf = &info->eti_conf;
1106         if (d->ed_next) {
1107                 if (!d->ed_next_islov) {
1108                         struct lov_oinfo *oinfo = lsm->lsm_oinfo[0];
1109                         LASSERT(oinfo != NULL);
1110                         oinfo->loi_id = lsm->lsm_object_id;
1111                         oinfo->loi_seq = lsm->lsm_object_seq;
1112                         conf->eoc_cl.u.coc_oinfo = oinfo;
1113                 } else {
1114                         struct lustre_md *md;
1115                         md = &info->eti_md;
1116                         memset(md, 0, sizeof *md);
1117                         md->lsm = lsm;
1118                         conf->eoc_cl.u.coc_md = md;
1119                 }
1120         }
1121         conf->eoc_md = lsmp;
1122
1123         fid  = &info->eti_fid;
1124         lsm2fid(lsm, fid);
1125
1126         /* In the function below, .hs_keycmp resolves to
1127          * lu_obj_hop_keycmp() */
1128         /* coverity[overrun-buffer-val] */
1129         obj = cl_object_find(env, echo_dev2cl(d), fid, &conf->eoc_cl);
1130         if (IS_ERR(obj))
1131                 GOTO(out, eco = (void*)obj);
1132
1133         eco = cl2echo_obj(obj);
1134         if (eco->eo_deleted) {
1135                 cl_object_put(env, obj);
1136                 eco = ERR_PTR(-EAGAIN);
1137         }
1138
1139 out:
1140         cl_env_put(env, &refcheck);
1141         RETURN(eco);
1142 }
1143
1144 static int cl_echo_object_put(struct echo_object *eco)
1145 {
1146         struct lu_env *env;
1147         struct cl_object *obj = echo_obj2cl(eco);
1148         int refcheck;
1149         ENTRY;
1150
1151         env = cl_env_get(&refcheck);
1152         if (IS_ERR(env))
1153                 RETURN(PTR_ERR(env));
1154
1155         /* an external function to kill an object? */
1156         if (eco->eo_deleted) {
1157                 struct lu_object_header *loh = obj->co_lu.lo_header;
1158                 LASSERT(&eco->eo_hdr == luh2coh(loh));
1159                 set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
1160         }
1161
1162         cl_object_put(env, obj);
1163         cl_env_put(env, &refcheck);
1164         RETURN(0);
1165 }
1166
1167 static int cl_echo_enqueue0(struct lu_env *env, struct echo_object *eco,
1168                             obd_off start, obd_off end, int mode,
1169                             __u64 *cookie , __u32 enqflags)
1170 {
1171         struct cl_io *io;
1172         struct cl_lock *lck;
1173         struct cl_object *obj;
1174         struct cl_lock_descr *descr;
1175         struct echo_thread_info *info;
1176         int rc = -ENOMEM;
1177         ENTRY;
1178
1179         info = echo_env_info(env);
1180         io = &info->eti_io;
1181         descr = &info->eti_descr;
1182         obj = echo_obj2cl(eco);
1183
1184         descr->cld_obj   = obj;
1185         descr->cld_start = cl_index(obj, start);
1186         descr->cld_end   = cl_index(obj, end);
1187         descr->cld_mode  = mode == LCK_PW ? CLM_WRITE : CLM_READ;
1188         descr->cld_enq_flags = enqflags;
1189         io->ci_obj = obj;
1190
1191         lck = cl_lock_request(env, io, descr, "ec enqueue", eco);
1192         if (lck) {
1193                 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
1194                 struct echo_lock *el;
1195
1196                 rc = cl_wait(env, lck);
1197                 if (rc == 0) {
1198                         el = cl2echo_lock(cl_lock_at(lck, &echo_device_type));
1199                         spin_lock(&ec->ec_lock);
1200                         if (cfs_list_empty(&el->el_chain)) {
1201                                 cfs_list_add(&el->el_chain, &ec->ec_locks);
1202                                 el->el_cookie = ++ec->ec_unique;
1203                         }
1204                         cfs_atomic_inc(&el->el_refcount);
1205                         *cookie = el->el_cookie;
1206                         spin_unlock(&ec->ec_lock);
1207                 } else {
1208                         cl_lock_release(env, lck, "ec enqueue", cfs_current());
1209                 }
1210         }
1211         RETURN(rc);
1212 }
1213
1214 static int cl_echo_enqueue(struct echo_object *eco, obd_off start, obd_off end,
1215                            int mode, __u64 *cookie)
1216 {
1217         struct echo_thread_info *info;
1218         struct lu_env *env;
1219         struct cl_io *io;
1220         int refcheck;
1221         int result;
1222         ENTRY;
1223
1224         env = cl_env_get(&refcheck);
1225         if (IS_ERR(env))
1226                 RETURN(PTR_ERR(env));
1227
1228         info = echo_env_info(env);
1229         io = &info->eti_io;
1230
1231         io->ci_ignore_layout = 1;
1232         result = cl_io_init(env, io, CIT_MISC, echo_obj2cl(eco));
1233         if (result < 0)
1234                 GOTO(out, result);
1235         LASSERT(result == 0);
1236
1237         result = cl_echo_enqueue0(env, eco, start, end, mode, cookie, 0);
1238         cl_io_fini(env, io);
1239
1240         EXIT;
1241 out:
1242         cl_env_put(env, &refcheck);
1243         return result;
1244 }
1245
1246 static int cl_echo_cancel0(struct lu_env *env, struct echo_device *ed,
1247                            __u64 cookie)
1248 {
1249         struct echo_client_obd *ec = ed->ed_ec;
1250         struct echo_lock       *ecl = NULL;
1251         cfs_list_t             *el;
1252         int found = 0, still_used = 0;
1253         ENTRY;
1254
1255         LASSERT(ec != NULL);
1256         spin_lock(&ec->ec_lock);
1257         cfs_list_for_each (el, &ec->ec_locks) {
1258                 ecl = cfs_list_entry (el, struct echo_lock, el_chain);
1259                 CDEBUG(D_INFO, "ecl: %p, cookie: "LPX64"\n", ecl, ecl->el_cookie);
1260                 found = (ecl->el_cookie == cookie);
1261                 if (found) {
1262                         if (cfs_atomic_dec_and_test(&ecl->el_refcount))
1263                                 cfs_list_del_init(&ecl->el_chain);
1264                         else
1265                                 still_used = 1;
1266                         break;
1267                 }
1268         }
1269         spin_unlock(&ec->ec_lock);
1270
1271         if (!found)
1272                 RETURN(-ENOENT);
1273
1274         echo_lock_release(env, ecl, still_used);
1275         RETURN(0);
1276 }
1277
1278 static int cl_echo_cancel(struct echo_device *ed, __u64 cookie)
1279 {
1280         struct lu_env *env;
1281         int refcheck;
1282         int rc;
1283         ENTRY;
1284
1285         env = cl_env_get(&refcheck);
1286         if (IS_ERR(env))
1287                 RETURN(PTR_ERR(env));
1288
1289         rc = cl_echo_cancel0(env, ed, cookie);
1290
1291         cl_env_put(env, &refcheck);
1292         RETURN(rc);
1293 }
1294
1295 static int cl_echo_async_brw(const struct lu_env *env, struct cl_io *io,
1296                              enum cl_req_type unused, struct cl_2queue *queue)
1297 {
1298         struct cl_page *clp;
1299         struct cl_page *temp;
1300         int result = 0;
1301         ENTRY;
1302
1303         cl_page_list_for_each_safe(clp, temp, &queue->c2_qin) {
1304                 int rc;
1305                 rc = cl_page_cache_add(env, io, clp, CRT_WRITE);
1306                 if (rc == 0)
1307                         continue;
1308                 result = result ?: rc;
1309         }
1310         RETURN(result);
1311 }
1312
1313 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
1314                               cfs_page_t **pages, int npages, int async)
1315 {
1316         struct lu_env           *env;
1317         struct echo_thread_info *info;
1318         struct cl_object        *obj = echo_obj2cl(eco);
1319         struct echo_device      *ed  = eco->eo_dev;
1320         struct cl_2queue        *queue;
1321         struct cl_io            *io;
1322         struct cl_page          *clp;
1323         struct lustre_handle    lh = { 0 };
1324         int page_size = cl_page_size(obj);
1325         int refcheck;
1326         int rc;
1327         int i;
1328         ENTRY;
1329
1330         LASSERT((offset & ~CFS_PAGE_MASK) == 0);
1331         LASSERT(ed->ed_next != NULL);
1332         env = cl_env_get(&refcheck);
1333         if (IS_ERR(env))
1334                 RETURN(PTR_ERR(env));
1335
1336         info    = echo_env_info(env);
1337         io      = &info->eti_io;
1338         queue   = &info->eti_queue;
1339
1340         cl_2queue_init(queue);
1341
1342         io->ci_ignore_layout = 1;
1343         rc = cl_io_init(env, io, CIT_MISC, obj);
1344         if (rc < 0)
1345                 GOTO(out, rc);
1346         LASSERT(rc == 0);
1347
1348
1349         rc = cl_echo_enqueue0(env, eco, offset,
1350                               offset + npages * CFS_PAGE_SIZE - 1,
1351                               rw == READ ? LCK_PR : LCK_PW, &lh.cookie,
1352                               CEF_NEVER);
1353         if (rc < 0)
1354                 GOTO(error_lock, rc);
1355
1356         for (i = 0; i < npages; i++) {
1357                 LASSERT(pages[i]);
1358                 clp = cl_page_find(env, obj, cl_index(obj, offset),
1359                                    pages[i], CPT_TRANSIENT);
1360                 if (IS_ERR(clp)) {
1361                         rc = PTR_ERR(clp);
1362                         break;
1363                 }
1364                 LASSERT(clp->cp_type == CPT_TRANSIENT);
1365
1366                 rc = cl_page_own(env, io, clp);
1367                 if (rc) {
1368                         LASSERT(clp->cp_state == CPS_FREEING);
1369                         cl_page_put(env, clp);
1370                         break;
1371                 }
1372
1373                 cl_2queue_add(queue, clp);
1374
1375                 /* drop the reference count for cl_page_find, so that the page
1376                  * will be freed in cl_2queue_fini. */
1377                 cl_page_put(env, clp);
1378                 cl_page_clip(env, clp, 0, page_size);
1379
1380                 offset += page_size;
1381         }
1382
1383         if (rc == 0) {
1384                 enum cl_req_type typ = rw == READ ? CRT_READ : CRT_WRITE;
1385
1386                 async = async && (typ == CRT_WRITE);
1387                 if (async)
1388                         rc = cl_echo_async_brw(env, io, typ, queue);
1389                 else
1390                         rc = cl_io_submit_sync(env, io, typ, queue, 0);
1391                 CDEBUG(D_INFO, "echo_client %s write returns %d\n",
1392                        async ? "async" : "sync", rc);
1393         }
1394
1395         cl_echo_cancel0(env, ed, lh.cookie);
1396         EXIT;
1397 error_lock:
1398         cl_2queue_discard(env, io, queue);
1399         cl_2queue_disown(env, io, queue);
1400         cl_2queue_fini(env, queue);
1401         cl_io_fini(env, io);
1402 out:
1403         cl_env_put(env, &refcheck);
1404         return rc;
1405 }
1406 /** @} echo_exports */
1407
1408
1409 static obd_id last_object_id;
1410
1411 static int
1412 echo_copyout_lsm (struct lov_stripe_md *lsm, void *_ulsm, int ulsm_nob)
1413 {
1414         struct lov_stripe_md *ulsm = _ulsm;
1415         int nob, i;
1416
1417         nob = offsetof (struct lov_stripe_md, lsm_oinfo[lsm->lsm_stripe_count]);
1418         if (nob > ulsm_nob)
1419                 return (-EINVAL);
1420
1421         if (cfs_copy_to_user (ulsm, lsm, sizeof(ulsm)))
1422                 return (-EFAULT);
1423
1424         for (i = 0; i < lsm->lsm_stripe_count; i++) {
1425                 if (cfs_copy_to_user (ulsm->lsm_oinfo[i], lsm->lsm_oinfo[i],
1426                                       sizeof(lsm->lsm_oinfo[0])))
1427                         return (-EFAULT);
1428         }
1429         return 0;
1430 }
1431
1432 static int
1433 echo_copyin_lsm (struct echo_device *ed, struct lov_stripe_md *lsm,
1434                  void *ulsm, int ulsm_nob)
1435 {
1436         struct echo_client_obd *ec = ed->ed_ec;
1437         int                     i;
1438
1439         if (ulsm_nob < sizeof (*lsm))
1440                 return (-EINVAL);
1441
1442         if (cfs_copy_from_user (lsm, ulsm, sizeof (*lsm)))
1443                 return (-EFAULT);
1444
1445         if (lsm->lsm_stripe_count > ec->ec_nstripes ||
1446             lsm->lsm_magic != LOV_MAGIC ||
1447             (lsm->lsm_stripe_size & (~CFS_PAGE_MASK)) != 0 ||
1448             ((__u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count > ~0UL))
1449                 return (-EINVAL);
1450
1451
1452         for (i = 0; i < lsm->lsm_stripe_count; i++) {
1453                 if (cfs_copy_from_user(lsm->lsm_oinfo[i],
1454                                        ((struct lov_stripe_md *)ulsm)-> \
1455                                        lsm_oinfo[i],
1456                                        sizeof(lsm->lsm_oinfo[0])))
1457                         return (-EFAULT);
1458         }
1459         return (0);
1460 }
1461
1462 static inline void echo_md_build_name(struct lu_name *lname, char *name,
1463                                       __u64 id)
1464 {
1465         sprintf(name, LPU64, id);
1466         lname->ln_name = name;
1467         lname->ln_namelen = strlen(name);
1468 }
1469
1470 /* similar to mdt_attr_get_complex */
1471 static int echo_big_lmm_get(const struct lu_env *env, struct md_object *o,
1472                             struct md_attr *ma)
1473 {
1474         struct echo_thread_info *info = echo_env_info(env);
1475         int                      rc;
1476
1477         ENTRY;
1478
1479         LASSERT(ma->ma_lmm_size > 0);
1480
1481         rc = mo_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LOV);
1482         if (rc < 0)
1483                 RETURN(rc);
1484
1485         /* big_lmm may need to be grown */
1486         if (info->eti_big_lmmsize < rc) {
1487                 int size = size_roundup_power2(rc);
1488
1489                 if (info->eti_big_lmmsize > 0) {
1490                         /* free old buffer */
1491                         LASSERT(info->eti_big_lmm);
1492                         OBD_FREE_LARGE(info->eti_big_lmm,
1493                                        info->eti_big_lmmsize);
1494                         info->eti_big_lmm = NULL;
1495                         info->eti_big_lmmsize = 0;
1496                 }
1497
1498                 OBD_ALLOC_LARGE(info->eti_big_lmm, size);
1499                 if (info->eti_big_lmm == NULL)
1500                         RETURN(-ENOMEM);
1501                 info->eti_big_lmmsize = size;
1502         }
1503         LASSERT(info->eti_big_lmmsize >= rc);
1504
1505         info->eti_buf.lb_buf = info->eti_big_lmm;
1506         info->eti_buf.lb_len = info->eti_big_lmmsize;
1507         rc = mo_xattr_get(env, o, &info->eti_buf, XATTR_NAME_LOV);
1508         if (rc < 0)
1509                 RETURN(rc);
1510
1511         ma->ma_valid |= MA_LOV;
1512         ma->ma_lmm = info->eti_big_lmm;
1513         ma->ma_lmm_size = rc;
1514
1515         RETURN(0);
1516 }
1517
1518 int echo_attr_get_complex(const struct lu_env *env, struct md_object *next,
1519                           struct md_attr *ma)
1520 {
1521         struct echo_thread_info *info = echo_env_info(env);
1522         struct lu_buf           *buf = &info->eti_buf;
1523         cfs_umode_t              mode = lu_object_attr(&next->mo_lu);
1524         int                      need = ma->ma_need;
1525         int                      rc = 0, rc2;
1526
1527         ENTRY;
1528
1529         ma->ma_valid = 0;
1530
1531         if (need & MA_INODE) {
1532                 ma->ma_need = MA_INODE;
1533                 rc = mo_attr_get(env, next, ma);
1534                 if (rc)
1535                         GOTO(out, rc);
1536                 ma->ma_valid |= MA_INODE;
1537         }
1538
1539         if (need & MA_LOV) {
1540                 if (S_ISREG(mode) || S_ISDIR(mode)) {
1541                         LASSERT(ma->ma_lmm_size > 0);
1542                         buf->lb_buf = ma->ma_lmm;
1543                         buf->lb_len = ma->ma_lmm_size;
1544                         rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LOV);
1545                         if (rc2 > 0) {
1546                                 ma->ma_lmm_size = rc2;
1547                                 ma->ma_valid |= MA_LOV;
1548                         } else if (rc2 == -ENODATA) {
1549                                 /* no LOV EA */
1550                                 ma->ma_lmm_size = 0;
1551                         } else if (rc2 == -ERANGE) {
1552                                 rc2 = echo_big_lmm_get(env, next, ma);
1553                                 if (rc2 < 0)
1554                                         GOTO(out, rc = rc2);
1555                         } else {
1556                                 GOTO(out, rc = rc2);
1557                         }
1558                 }
1559         }
1560
1561 #ifdef CONFIG_FS_POSIX_ACL
1562         if (need & MA_ACL_DEF && S_ISDIR(mode)) {
1563                 buf->lb_buf = ma->ma_acl;
1564                 buf->lb_len = ma->ma_acl_size;
1565                 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1566                 if (rc2 > 0) {
1567                         ma->ma_acl_size = rc2;
1568                         ma->ma_valid |= MA_ACL_DEF;
1569                 } else if (rc2 == -ENODATA) {
1570                         /* no ACLs */
1571                         ma->ma_acl_size = 0;
1572                 } else {
1573                         GOTO(out, rc = rc2);
1574                 }
1575         }
1576 #endif
1577 out:
1578         ma->ma_need = need;
1579         CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
1580                rc, ma->ma_valid, ma->ma_lmm);
1581         RETURN(rc);
1582 }
1583
1584 static int
1585 echo_md_create_internal(const struct lu_env *env, struct echo_device *ed,
1586                         struct md_object *parent, struct lu_fid *fid,
1587                         struct lu_name *lname, struct md_op_spec *spec,
1588                         struct md_attr *ma)
1589 {
1590         struct lu_object        *ec_child, *child;
1591         struct lu_device        *ld = ed->ed_next;
1592         struct echo_thread_info *info = echo_env_info(env);
1593         struct lu_fid           *fid2 = &info->eti_fid2;
1594         struct lu_object_conf    conf = { .loc_flags = LOC_F_NEW };
1595         int                      rc;
1596
1597         ENTRY;
1598
1599         rc = mdo_lookup(env, parent, lname, fid2, spec);
1600         if (rc == 0)
1601                 return -EEXIST;
1602         else if (rc != -ENOENT)
1603                 return rc;
1604
1605         ec_child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev,
1606                                      fid, &conf);
1607         if (IS_ERR(ec_child)) {
1608                 CERROR("Can not find the child "DFID": rc = %ld\n", PFID(fid),
1609                         PTR_ERR(ec_child));
1610                 RETURN(PTR_ERR(ec_child));
1611         }
1612
1613         child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1614         if (child == NULL) {
1615                 CERROR("Can not locate the child "DFID"\n", PFID(fid));
1616                 GOTO(out_put, rc = -EINVAL);
1617         }
1618
1619         CDEBUG(D_RPCTRACE, "Start creating object "DFID" %s %p\n",
1620                PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1621
1622         /*
1623          * Do not perform lookup sanity check. We know that name does not exist.
1624          */
1625         spec->sp_cr_lookup = 0;
1626         rc = mdo_create(env, parent, lname, lu2md(child), spec, ma);
1627         if (rc) {
1628                 CERROR("Can not create child "DFID": rc = %d\n", PFID(fid), rc);
1629                 GOTO(out_put, rc);
1630         }
1631         CDEBUG(D_RPCTRACE, "End creating object "DFID" %s %p rc  = %d\n",
1632                PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent, rc);
1633         EXIT;
1634 out_put:
1635         lu_object_put(env, ec_child);
1636         return rc;
1637 }
1638
1639 static int echo_set_lmm_size(const struct lu_env *env, struct lu_device *ld,
1640                              struct md_attr *ma)
1641 {
1642         struct echo_thread_info *info = echo_env_info(env);
1643
1644         if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
1645                 ma->ma_lmm = (void *)&info->eti_lmm;
1646                 ma->ma_lmm_size = sizeof(info->eti_lmm);
1647         } else {
1648                 LASSERT(info->eti_big_lmmsize);
1649                 ma->ma_lmm = info->eti_big_lmm;
1650                 ma->ma_lmm_size = info->eti_big_lmmsize;
1651         }
1652
1653         return 0;
1654 }
1655
1656 static int echo_create_md_object(const struct lu_env *env,
1657                                  struct echo_device *ed,
1658                                  struct lu_object *ec_parent,
1659                                  struct lu_fid *fid,
1660                                  char *name, int namelen,
1661                                  __u64 id, __u32 mode, int count,
1662                                  int stripe_count, int stripe_offset)
1663 {
1664         struct lu_object        *parent;
1665         struct echo_thread_info *info = echo_env_info(env);
1666         struct lu_name          *lname = &info->eti_lname;
1667         struct md_op_spec       *spec = &info->eti_spec;
1668         struct md_attr          *ma = &info->eti_ma;
1669         struct lu_device        *ld = ed->ed_next;
1670         int                      rc = 0;
1671         int                      i;
1672
1673         ENTRY;
1674
1675         if (ec_parent == NULL)
1676                 return -1;
1677         parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1678         if (parent == NULL)
1679                 RETURN(-ENXIO);
1680
1681         memset(ma, 0, sizeof(*ma));
1682         memset(spec, 0, sizeof(*spec));
1683         if (stripe_count != 0) {
1684                 spec->sp_cr_flags |= FMODE_WRITE;
1685                 echo_set_lmm_size(env, ld, ma);
1686                 if (stripe_count != -1) {
1687                         struct lov_user_md_v3 *lum = &info->eti_lum;
1688
1689                         lum->lmm_magic = LOV_USER_MAGIC_V3;
1690                         lum->lmm_stripe_count = stripe_count;
1691                         lum->lmm_stripe_offset = stripe_offset;
1692                         lum->lmm_pattern = 0;
1693                         spec->u.sp_ea.eadata = lum;
1694                         spec->u.sp_ea.eadatalen = sizeof(*lum);
1695                         spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1696                 }
1697         }
1698
1699         ma->ma_attr.la_mode = mode;
1700         ma->ma_attr.la_valid = LA_CTIME | LA_MODE;
1701         ma->ma_attr.la_ctime = cfs_time_current_64();
1702
1703         if (name != NULL) {
1704                 lname->ln_name = name;
1705                 lname->ln_namelen = namelen;
1706                 /* If name is specified, only create one object by name */
1707                 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1708                                              spec, ma);
1709                 RETURN(rc);
1710         }
1711
1712         /* Create multiple object sequenced by id */
1713         for (i = 0; i < count; i++) {
1714                 char *tmp_name = info->eti_name;
1715
1716                 echo_md_build_name(lname, tmp_name, id);
1717
1718                 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1719                                              spec, ma);
1720                 if (rc) {
1721                         CERROR("Can not create child %s: rc = %d\n", tmp_name,
1722                                 rc);
1723                         break;
1724                 }
1725                 id++;
1726                 fid->f_oid++;
1727         }
1728
1729         RETURN(rc);
1730 }
1731
1732 static struct lu_object *echo_md_lookup(const struct lu_env *env,
1733                                         struct echo_device *ed,
1734                                         struct md_object *parent,
1735                                         struct lu_name *lname)
1736 {
1737         struct echo_thread_info *info = echo_env_info(env);
1738         struct lu_fid           *fid = &info->eti_fid;
1739         struct lu_object        *child;
1740         int    rc;
1741         ENTRY;
1742
1743         CDEBUG(D_INFO, "lookup %s in parent "DFID" %p\n", lname->ln_name,
1744                PFID(fid), parent);
1745         rc = mdo_lookup(env, parent, lname, fid, NULL);
1746         if (rc) {
1747                 CERROR("lookup %s: rc = %d\n", lname->ln_name, rc);
1748                 RETURN(ERR_PTR(rc));
1749         }
1750
1751         /* In the function below, .hs_keycmp resolves to
1752          * lu_obj_hop_keycmp() */
1753         /* coverity[overrun-buffer-val] */
1754         child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1755
1756         RETURN(child);
1757 }
1758
1759 static int echo_setattr_object(const struct lu_env *env,
1760                                struct echo_device *ed,
1761                                struct lu_object *ec_parent,
1762                                __u64 id, int count)
1763 {
1764         struct lu_object        *parent;
1765         struct echo_thread_info *info = echo_env_info(env);
1766         struct lu_name          *lname = &info->eti_lname;
1767         char                    *name = info->eti_name;
1768         struct lu_device        *ld = ed->ed_next;
1769         struct lu_buf           *buf = &info->eti_buf;
1770         int                      rc = 0;
1771         int                      i;
1772
1773         ENTRY;
1774
1775         if (ec_parent == NULL)
1776                 return -1;
1777         parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1778         if (parent == NULL)
1779                 RETURN(-ENXIO);
1780
1781         for (i = 0; i < count; i++) {
1782                 struct lu_object *ec_child, *child;
1783
1784                 echo_md_build_name(lname, name, id);
1785
1786                 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1787                 if (IS_ERR(ec_child)) {
1788                         CERROR("Can't find child %s: rc = %ld\n",
1789                                 lname->ln_name, PTR_ERR(ec_child));
1790                         RETURN(PTR_ERR(ec_child));
1791                 }
1792
1793                 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1794                 if (child == NULL) {
1795                         CERROR("Can not locate the child %s\n", lname->ln_name);
1796                         lu_object_put(env, ec_child);
1797                         rc = -EINVAL;
1798                         break;
1799                 }
1800
1801                 CDEBUG(D_RPCTRACE, "Start setattr object "DFID"\n",
1802                        PFID(lu_object_fid(child)));
1803
1804                 buf->lb_buf = info->eti_xattr_buf;
1805                 buf->lb_len = sizeof(info->eti_xattr_buf);
1806
1807                 sprintf(name, "%s.test1", XATTR_USER_PREFIX);
1808                 rc = mo_xattr_set(env, lu2md(child), buf, name,
1809                                   LU_XATTR_CREATE);
1810                 if (rc < 0) {
1811                         CERROR("Can not setattr child "DFID": rc = %d\n",
1812                                 PFID(lu_object_fid(child)), rc);
1813                         lu_object_put(env, ec_child);
1814                         break;
1815                 }
1816                 CDEBUG(D_RPCTRACE, "End setattr object "DFID"\n",
1817                        PFID(lu_object_fid(child)));
1818                 id++;
1819                 lu_object_put(env, ec_child);
1820         }
1821         RETURN(rc);
1822 }
1823
1824 static int echo_getattr_object(const struct lu_env *env,
1825                                struct echo_device *ed,
1826                                struct lu_object *ec_parent,
1827                                __u64 id, int count)
1828 {
1829         struct lu_object        *parent;
1830         struct echo_thread_info *info = echo_env_info(env);
1831         struct lu_name          *lname = &info->eti_lname;
1832         char                    *name = info->eti_name;
1833         struct md_attr          *ma = &info->eti_ma;
1834         struct lu_device        *ld = ed->ed_next;
1835         int                      rc = 0;
1836         int                      i;
1837
1838         ENTRY;
1839
1840         if (ec_parent == NULL)
1841                 return -1;
1842         parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1843         if (parent == NULL)
1844                 RETURN(-ENXIO);
1845
1846         memset(ma, 0, sizeof(*ma));
1847         ma->ma_need |= MA_INODE | MA_LOV | MA_PFID | MA_HSM | MA_ACL_DEF;
1848         ma->ma_acl = info->eti_xattr_buf;
1849         ma->ma_acl_size = sizeof(info->eti_xattr_buf);
1850
1851         for (i = 0; i < count; i++) {
1852                 struct lu_object *ec_child, *child;
1853
1854                 ma->ma_valid = 0;
1855                 echo_md_build_name(lname, name, id);
1856                 echo_set_lmm_size(env, ld, ma);
1857
1858                 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1859                 if (IS_ERR(ec_child)) {
1860                         CERROR("Can't find child %s: rc = %ld\n",
1861                                lname->ln_name, PTR_ERR(ec_child));
1862                         RETURN(PTR_ERR(ec_child));
1863                 }
1864
1865                 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1866                 if (child == NULL) {
1867                         CERROR("Can not locate the child %s\n", lname->ln_name);
1868                         lu_object_put(env, ec_child);
1869                         RETURN(-EINVAL);
1870                 }
1871
1872                 CDEBUG(D_RPCTRACE, "Start getattr object "DFID"\n",
1873                        PFID(lu_object_fid(child)));
1874                 rc = echo_attr_get_complex(env, lu2md(child), ma);
1875                 if (rc) {
1876                         CERROR("Can not getattr child "DFID": rc = %d\n",
1877                                 PFID(lu_object_fid(child)), rc);
1878                         lu_object_put(env, ec_child);
1879                         break;
1880                 }
1881                 CDEBUG(D_RPCTRACE, "End getattr object "DFID"\n",
1882                        PFID(lu_object_fid(child)));
1883                 id++;
1884                 lu_object_put(env, ec_child);
1885         }
1886
1887         RETURN(rc);
1888 }
1889
1890 static int echo_lookup_object(const struct lu_env *env,
1891                               struct echo_device *ed,
1892                               struct lu_object *ec_parent,
1893                               __u64 id, int count)
1894 {
1895         struct lu_object        *parent;
1896         struct echo_thread_info *info = echo_env_info(env);
1897         struct lu_name          *lname = &info->eti_lname;
1898         char                    *name = info->eti_name;
1899         struct lu_fid           *fid = &info->eti_fid;
1900         struct lu_device        *ld = ed->ed_next;
1901         int                      rc = 0;
1902         int                      i;
1903
1904         if (ec_parent == NULL)
1905                 return -1;
1906         parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1907         if (parent == NULL)
1908                 return -ENXIO;
1909
1910         /*prepare the requests*/
1911         for (i = 0; i < count; i++) {
1912                 echo_md_build_name(lname, name, id);
1913
1914                 CDEBUG(D_RPCTRACE, "Start lookup object "DFID" %s %p\n",
1915                        PFID(lu_object_fid(parent)), lname->ln_name, parent);
1916
1917                 rc = mdo_lookup(env, lu2md(parent), lname, fid, NULL);
1918                 if (rc) {
1919                         CERROR("Can not lookup child %s: rc = %d\n", name, rc);
1920                         break;
1921                 }
1922                 CDEBUG(D_RPCTRACE, "End lookup object "DFID" %s %p\n",
1923                        PFID(lu_object_fid(parent)), lname->ln_name, parent);
1924
1925                 id++;
1926         }
1927         return rc;
1928 }
1929
1930 static int echo_md_destroy_internal(const struct lu_env *env,
1931                                     struct echo_device *ed,
1932                                     struct md_object *parent,
1933                                     struct lu_name *lname,
1934                                     struct md_attr *ma)
1935 {
1936         struct lu_device   *ld = ed->ed_next;
1937         struct lu_object   *ec_child;
1938         struct lu_object   *child;
1939         int                 rc;
1940
1941         ENTRY;
1942
1943         ec_child = echo_md_lookup(env, ed, parent, lname);
1944         if (IS_ERR(ec_child)) {
1945                 CERROR("Can't find child %s: rc = %ld\n", lname->ln_name,
1946                         PTR_ERR(ec_child));
1947                 RETURN(PTR_ERR(ec_child));
1948         }
1949
1950         child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1951         if (child == NULL) {
1952                 CERROR("Can not locate the child %s\n", lname->ln_name);
1953                 GOTO(out_put, rc = -EINVAL);
1954         }
1955
1956         CDEBUG(D_RPCTRACE, "Start destroy object "DFID" %s %p\n",
1957                PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1958
1959         rc = mdo_unlink(env, parent, lu2md(child), lname, ma);
1960         if (rc) {
1961                 CERROR("Can not unlink child %s: rc = %d\n",
1962                         lname->ln_name, rc);
1963                 GOTO(out_put, rc);
1964         }
1965         CDEBUG(D_RPCTRACE, "End destroy object "DFID" %s %p\n",
1966                PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1967 out_put:
1968         lu_object_put(env, ec_child);
1969         return rc;
1970 }
1971
1972 static int echo_destroy_object(const struct lu_env *env,
1973                                struct echo_device *ed,
1974                                struct lu_object *ec_parent,
1975                                char *name, int namelen,
1976                                __u64 id, __u32 mode,
1977                                int count)
1978 {
1979         struct echo_thread_info *info = echo_env_info(env);
1980         struct lu_name          *lname = &info->eti_lname;
1981         struct md_attr          *ma = &info->eti_ma;
1982         struct lu_device        *ld = ed->ed_next;
1983         struct lu_object        *parent;
1984         int                      rc = 0;
1985         int                      i;
1986         ENTRY;
1987
1988         parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1989         if (parent == NULL)
1990                 RETURN(-EINVAL);
1991
1992         memset(ma, 0, sizeof(*ma));
1993         ma->ma_attr.la_mode = mode;
1994         ma->ma_attr.la_valid = LA_CTIME;
1995         ma->ma_attr.la_ctime = cfs_time_current_64();
1996         ma->ma_need = MA_INODE;
1997         ma->ma_valid = 0;
1998
1999         if (name != NULL) {
2000                 lname->ln_name = name;
2001                 lname->ln_namelen = namelen;
2002                 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
2003                                               ma);
2004                 RETURN(rc);
2005         }
2006
2007         /*prepare the requests*/
2008         for (i = 0; i < count; i++) {
2009                 char *tmp_name = info->eti_name;
2010
2011                 ma->ma_valid = 0;
2012                 echo_md_build_name(lname, tmp_name, id);
2013
2014                 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
2015                                               ma);
2016                 if (rc) {
2017                         CERROR("Can not unlink child %s: rc = %d\n", name, rc);
2018                         break;
2019                 }
2020                 id++;
2021         }
2022
2023         RETURN(rc);
2024 }
2025
2026 static struct lu_object *echo_resolve_path(const struct lu_env *env,
2027                                            struct echo_device *ed, char *path,
2028                                            int path_len)
2029 {
2030         struct lu_device        *ld = ed->ed_next;
2031         struct md_device        *md = lu2md_dev(ld);
2032         struct echo_thread_info *info = echo_env_info(env);
2033         struct lu_fid           *fid = &info->eti_fid;
2034         struct lu_name          *lname = &info->eti_lname;
2035         struct lu_object        *parent = NULL;
2036         struct lu_object        *child = NULL;
2037         int rc = 0;
2038         ENTRY;
2039
2040         /*Only support MDD layer right now*/
2041         rc = md->md_ops->mdo_root_get(env, md, fid);
2042         if (rc) {
2043                 CERROR("get root error: rc = %d\n", rc);
2044                 RETURN(ERR_PTR(rc));
2045         }
2046
2047         /* In the function below, .hs_keycmp resolves to
2048          * lu_obj_hop_keycmp() */
2049         /* coverity[overrun-buffer-val] */
2050         parent = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
2051         if (IS_ERR(parent)) {
2052                 CERROR("Can not find the parent "DFID": rc = %ld\n",
2053                         PFID(fid), PTR_ERR(parent));
2054                 RETURN(parent);
2055         }
2056
2057         while (1) {
2058                 struct lu_object *ld_parent;
2059                 char *e;
2060
2061                 e = strsep(&path, "/");
2062                 if (e == NULL)
2063                         break;
2064
2065                 if (e[0] == 0) {
2066                         if (!path || path[0] == '\0')
2067                                 break;
2068                         continue;
2069                 }
2070
2071                 lname->ln_name = e;
2072                 lname->ln_namelen = strlen(e);
2073
2074                 ld_parent = lu_object_locate(parent->lo_header, ld->ld_type);
2075                 if (ld_parent == NULL) {
2076                         lu_object_put(env, parent);
2077                         rc = -EINVAL;
2078                         break;
2079                 }
2080
2081                 child = echo_md_lookup(env, ed, lu2md(ld_parent), lname);
2082                 lu_object_put(env, parent);
2083                 if (IS_ERR(child)) {
2084                         rc = (int)PTR_ERR(child);
2085                         CERROR("lookup %s under parent "DFID": rc = %d\n",
2086                                 lname->ln_name, PFID(lu_object_fid(ld_parent)),
2087                                 rc);
2088                         break;
2089                 }
2090                 parent = child;
2091         }
2092         if (rc)
2093                 RETURN(ERR_PTR(rc));
2094
2095         RETURN(parent);
2096 }
2097
2098 static void echo_ucred_init(struct lu_env *env)
2099 {
2100         struct lu_ucred *ucred = lu_ucred(env);
2101
2102         ucred->uc_valid = UCRED_INVALID;
2103
2104         ucred->uc_suppgids[0] = -1;
2105         ucred->uc_suppgids[1] = -1;
2106
2107         ucred->uc_uid   = ucred->uc_o_uid   = cfs_curproc_uid();
2108         ucred->uc_gid   = ucred->uc_o_gid   = cfs_curproc_gid();
2109         ucred->uc_fsuid = ucred->uc_o_fsuid = cfs_curproc_fsuid();
2110         ucred->uc_fsgid = ucred->uc_o_fsgid = cfs_curproc_fsgid();
2111         ucred->uc_cap   = cfs_curproc_cap_pack();
2112
2113         /* remove fs privilege for non-root user. */
2114         if (ucred->uc_fsuid)
2115                 ucred->uc_cap &= ~CFS_CAP_FS_MASK;
2116         ucred->uc_valid = UCRED_NEW;
2117 }
2118
2119 static void echo_ucred_fini(struct lu_env *env)
2120 {
2121         struct lu_ucred *ucred = lu_ucred(env);
2122         ucred->uc_valid = UCRED_INIT;
2123 }
2124
2125 #define ECHO_MD_CTX_TAG (LCT_REMEMBER | LCT_MD_THREAD)
2126 #define ECHO_MD_SES_TAG (LCT_REMEMBER | LCT_SESSION)
2127 static int echo_md_handler(struct echo_device *ed, int command,
2128                            char *path, int path_len, int id, int count,
2129                            struct obd_ioctl_data *data)
2130 {
2131         struct echo_thread_info *info;
2132         struct lu_device      *ld = ed->ed_next;
2133         struct lu_env         *env;
2134         int                    refcheck;
2135         struct lu_object      *parent;
2136         char                  *name = NULL;
2137         int                    namelen = data->ioc_plen2;
2138         int                    rc = 0;
2139         ENTRY;
2140
2141         if (ld == NULL) {
2142                 CERROR("MD echo client is not being initialized properly\n");
2143                 RETURN(-EINVAL);
2144         }
2145
2146         if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
2147                 CERROR("Only support MDD layer right now!\n");
2148                 RETURN(-EINVAL);
2149         }
2150
2151         env = cl_env_get(&refcheck);
2152         if (IS_ERR(env))
2153                 RETURN(PTR_ERR(env));
2154
2155         rc = lu_env_refill_by_tags(env, ECHO_MD_CTX_TAG, ECHO_MD_SES_TAG);
2156         if (rc != 0)
2157                 GOTO(out_env, rc);
2158
2159         /* init big_lmm buffer */
2160         info = echo_env_info(env);
2161         LASSERT(info->eti_big_lmm == NULL);
2162         OBD_ALLOC_LARGE(info->eti_big_lmm, MIN_MD_SIZE);
2163         if (info->eti_big_lmm == NULL)
2164                 GOTO(out_env, rc = -ENOMEM);
2165         info->eti_big_lmmsize = MIN_MD_SIZE;
2166
2167         parent = echo_resolve_path(env, ed, path, path_len);
2168         if (IS_ERR(parent)) {
2169                 CERROR("Can not resolve the path %s: rc = %ld\n", path,
2170                         PTR_ERR(parent));
2171                 GOTO(out_free, rc = PTR_ERR(parent));
2172         }
2173
2174         if (namelen > 0) {
2175                 OBD_ALLOC(name, namelen + 1);
2176                 if (name == NULL)
2177                         GOTO(out_put, rc = -ENOMEM);
2178                 if (cfs_copy_from_user(name, data->ioc_pbuf2, namelen))
2179                         GOTO(out_name, rc = -EFAULT);
2180         }
2181
2182         echo_ucred_init(env);
2183
2184         switch (command) {
2185         case ECHO_MD_CREATE:
2186         case ECHO_MD_MKDIR: {
2187                 struct echo_thread_info *info = echo_env_info(env);
2188                 __u32 mode = data->ioc_obdo2.o_mode;
2189                 struct lu_fid *fid = &info->eti_fid;
2190                 int stripe_count = (int)data->ioc_obdo2.o_misc;
2191                 int stripe_index = (int)data->ioc_obdo2.o_stripe_idx;
2192
2193                 fid->f_seq = data->ioc_obdo1.o_seq;
2194                 fid->f_oid = (__u32)data->ioc_obdo1.o_id;
2195                 fid->f_ver = 0;
2196                 /* In the function below, .hs_keycmp resolves to
2197                  * lu_obj_hop_keycmp() */
2198                 /* coverity[overrun-buffer-val] */
2199                 rc = echo_create_md_object(env, ed, parent, fid, name, namelen,
2200                                            id, mode, count, stripe_count,
2201                                            stripe_index);
2202                 break;
2203         }
2204         case ECHO_MD_DESTROY:
2205         case ECHO_MD_RMDIR: {
2206                 __u32 mode = data->ioc_obdo2.o_mode;
2207
2208                 rc = echo_destroy_object(env, ed, parent, name, namelen,
2209                                          id, mode, count);
2210                 break;
2211         }
2212         case ECHO_MD_LOOKUP:
2213                 rc = echo_lookup_object(env, ed, parent, id, count);
2214                 break;
2215         case ECHO_MD_GETATTR:
2216                 rc = echo_getattr_object(env, ed, parent, id, count);
2217                 break;
2218         case ECHO_MD_SETATTR:
2219                 rc = echo_setattr_object(env, ed, parent, id, count);
2220                 break;
2221         default:
2222                 CERROR("unknown command %d\n", command);
2223                 rc = -EINVAL;
2224                 break;
2225         }
2226         echo_ucred_fini(env);
2227
2228 out_name:
2229         if (name != NULL)
2230                 OBD_FREE(name, namelen + 1);
2231 out_put:
2232         lu_object_put(env, parent);
2233 out_free:
2234         LASSERT(info->eti_big_lmm);
2235         OBD_FREE_LARGE(info->eti_big_lmm, info->eti_big_lmmsize);
2236         info->eti_big_lmm = NULL;
2237         info->eti_big_lmmsize = 0;
2238 out_env:
2239         cl_env_put(env, &refcheck);
2240         return rc;
2241 }
2242
2243 static int echo_create_object(const struct lu_env *env, struct echo_device *ed,
2244                               int on_target, struct obdo *oa, void *ulsm,
2245                               int ulsm_nob, struct obd_trans_info *oti)
2246 {
2247         struct echo_object     *eco;
2248         struct echo_client_obd *ec = ed->ed_ec;
2249         struct lov_stripe_md   *lsm = NULL;
2250         int                     rc;
2251         int                     created = 0;
2252         ENTRY;
2253
2254         if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
2255             (on_target ||                       /* set_stripe */
2256              ec->ec_nstripes != 0)) {           /* LOV */
2257                 CERROR ("No valid oid\n");
2258                 RETURN(-EINVAL);
2259         }
2260
2261         rc = echo_alloc_memmd(ed, &lsm);
2262         if (rc < 0) {
2263                 CERROR("Cannot allocate md: rc = %d\n", rc);
2264                 GOTO(failed, rc);
2265         }
2266
2267         if (ulsm != NULL) {
2268                 int i, idx;
2269
2270                 rc = echo_copyin_lsm (ed, lsm, ulsm, ulsm_nob);
2271                 if (rc != 0)
2272                         GOTO(failed, rc);
2273
2274                 if (lsm->lsm_stripe_count == 0)
2275                         lsm->lsm_stripe_count = ec->ec_nstripes;
2276
2277                 if (lsm->lsm_stripe_size == 0)
2278                         lsm->lsm_stripe_size = CFS_PAGE_SIZE;
2279
2280                 idx = cfs_rand();
2281
2282                 /* setup stripes: indices + default ids if required */
2283                 for (i = 0; i < lsm->lsm_stripe_count; i++) {
2284                         if (lsm->lsm_oinfo[i]->loi_id == 0)
2285                                 lsm->lsm_oinfo[i]->loi_id = lsm->lsm_object_id;
2286
2287                         lsm->lsm_oinfo[i]->loi_ost_idx =
2288                                 (idx + i) % ec->ec_nstripes;
2289                 }
2290         }
2291
2292         /* setup object ID here for !on_target and LOV hint */
2293         if (oa->o_valid & OBD_MD_FLID)
2294                 lsm->lsm_object_id = oa->o_id;
2295
2296         if (lsm->lsm_object_id == 0)
2297                 lsm->lsm_object_id = ++last_object_id;
2298
2299         rc = 0;
2300         if (on_target) {
2301                 /* Only echo objects are allowed to be created */
2302                 LASSERT((oa->o_valid & OBD_MD_FLGROUP) &&
2303                         (oa->o_seq == FID_SEQ_ECHO));
2304                 rc = obd_create(env, ec->ec_exp, oa, &lsm, oti);
2305                 if (rc != 0) {
2306                         CERROR("Cannot create objects: rc = %d\n", rc);
2307                         GOTO(failed, rc);
2308                 }
2309                 created = 1;
2310         }
2311
2312         /* See what object ID we were given */
2313         oa->o_id = lsm->lsm_object_id;
2314         oa->o_valid |= OBD_MD_FLID;
2315
2316         eco = cl_echo_object_find(ed, &lsm);
2317         if (IS_ERR(eco))
2318                 GOTO(failed, rc = PTR_ERR(eco));
2319         cl_echo_object_put(eco);
2320
2321         CDEBUG(D_INFO, "oa->o_id = %lx\n", (long)oa->o_id);
2322         EXIT;
2323
2324  failed:
2325         if (created && rc)
2326                 obd_destroy(env, ec->ec_exp, oa, lsm, oti, NULL, NULL);
2327         if (lsm)
2328                 echo_free_memmd(ed, &lsm);
2329         if (rc)
2330                 CERROR("create object failed with: rc = %d\n", rc);
2331         return (rc);
2332 }
2333
2334 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
2335                            struct obdo *oa)
2336 {
2337         struct lov_stripe_md   *lsm = NULL;
2338         struct echo_object     *eco;
2339         int                     rc;
2340         ENTRY;
2341
2342         if ((oa->o_valid & OBD_MD_FLID) == 0 ||
2343             oa->o_id == 0)  /* disallow use of object id 0 */
2344         {
2345                 CERROR ("No valid oid\n");
2346                 RETURN(-EINVAL);
2347         }
2348
2349         rc = echo_alloc_memmd(ed, &lsm);
2350         if (rc < 0)
2351                 RETURN(rc);
2352
2353         lsm->lsm_object_id = oa->o_id;
2354         if (oa->o_valid & OBD_MD_FLGROUP)
2355                 lsm->lsm_object_seq = oa->o_seq;
2356         else
2357                 lsm->lsm_object_seq = FID_SEQ_ECHO;
2358
2359         rc = 0;
2360         eco = cl_echo_object_find(ed, &lsm);
2361         if (!IS_ERR(eco))
2362                 *ecop = eco;
2363         else
2364                 rc = PTR_ERR(eco);
2365         if (lsm)
2366                 echo_free_memmd(ed, &lsm);
2367         RETURN(rc);
2368 }
2369
2370 static void echo_put_object(struct echo_object *eco)
2371 {
2372         if (cl_echo_object_put(eco))
2373                 CERROR("echo client: drop an object failed");
2374 }
2375
2376 static void
2377 echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
2378 {
2379         unsigned long stripe_count;
2380         unsigned long stripe_size;
2381         unsigned long width;
2382         unsigned long woffset;
2383         int           stripe_index;
2384         obd_off       offset;
2385
2386         if (lsm->lsm_stripe_count <= 1)
2387                 return;
2388
2389         offset       = *offp;
2390         stripe_size  = lsm->lsm_stripe_size;
2391         stripe_count = lsm->lsm_stripe_count;
2392
2393         /* width = # bytes in all stripes */
2394         width = stripe_size * stripe_count;
2395
2396         /* woffset = offset within a width; offset = whole number of widths */
2397         woffset = do_div (offset, width);
2398
2399         stripe_index = woffset / stripe_size;
2400
2401         *idp = lsm->lsm_oinfo[stripe_index]->loi_id;
2402         *offp = offset * stripe_size + woffset % stripe_size;
2403 }
2404
2405 static void
2406 echo_client_page_debug_setup(struct lov_stripe_md *lsm,
2407                              cfs_page_t *page, int rw, obd_id id,
2408                              obd_off offset, obd_off count)
2409 {
2410         char    *addr;
2411         obd_off  stripe_off;
2412         obd_id   stripe_id;
2413         int      delta;
2414
2415         /* no partial pages on the client */
2416         LASSERT(count == CFS_PAGE_SIZE);
2417
2418         addr = cfs_kmap(page);
2419
2420         for (delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2421                 if (rw == OBD_BRW_WRITE) {
2422                         stripe_off = offset + delta;
2423                         stripe_id = id;
2424                         echo_get_stripe_off_id(lsm, &stripe_off, &stripe_id);
2425                 } else {
2426                         stripe_off = 0xdeadbeef00c0ffeeULL;
2427                         stripe_id = 0xdeadbeef00c0ffeeULL;
2428                 }
2429                 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
2430                                   stripe_off, stripe_id);
2431         }
2432
2433         cfs_kunmap(page);
2434 }
2435
2436 static int echo_client_page_debug_check(struct lov_stripe_md *lsm,
2437                                         cfs_page_t *page, obd_id id,
2438                                         obd_off offset, obd_off count)
2439 {
2440         obd_off stripe_off;
2441         obd_id  stripe_id;
2442         char   *addr;
2443         int     delta;
2444         int     rc;
2445         int     rc2;
2446
2447         /* no partial pages on the client */
2448         LASSERT(count == CFS_PAGE_SIZE);
2449
2450         addr = cfs_kmap(page);
2451
2452         for (rc = delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2453                 stripe_off = offset + delta;
2454                 stripe_id = id;
2455                 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
2456
2457                 rc2 = block_debug_check("test_brw",
2458                                         addr + delta, OBD_ECHO_BLOCK_SIZE,
2459                                         stripe_off, stripe_id);
2460                 if (rc2 != 0) {
2461                         CERROR ("Error in echo object "LPX64"\n", id);
2462                         rc = rc2;
2463                 }
2464         }
2465
2466         cfs_kunmap(page);
2467         return rc;
2468 }
2469
2470 static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
2471                             struct echo_object *eco, obd_off offset,
2472                             obd_size count, int async,
2473                             struct obd_trans_info *oti)
2474 {
2475         struct lov_stripe_md   *lsm = eco->eo_lsm;
2476         obd_count               npages;
2477         struct brw_page        *pga;
2478         struct brw_page        *pgp;
2479         cfs_page_t            **pages;
2480         obd_off                 off;
2481         int                     i;
2482         int                     rc;
2483         int                     verify;
2484         int                     gfp_mask;
2485         int                     brw_flags = 0;
2486         ENTRY;
2487
2488         verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
2489                   (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
2490                   (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
2491
2492         gfp_mask = ((oa->o_id & 2) == 0) ? CFS_ALLOC_STD : CFS_ALLOC_HIGHUSER;
2493
2494         LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
2495         LASSERT(lsm != NULL);
2496         LASSERT(lsm->lsm_object_id == oa->o_id);
2497
2498         if (count <= 0 ||
2499             (count & (~CFS_PAGE_MASK)) != 0)
2500                 RETURN(-EINVAL);
2501
2502         /* XXX think again with misaligned I/O */
2503         npages = count >> CFS_PAGE_SHIFT;
2504
2505         if (rw == OBD_BRW_WRITE)
2506                 brw_flags = OBD_BRW_ASYNC;
2507
2508         OBD_ALLOC(pga, npages * sizeof(*pga));
2509         if (pga == NULL)
2510                 RETURN(-ENOMEM);
2511
2512         OBD_ALLOC(pages, npages * sizeof(*pages));
2513         if (pages == NULL) {
2514                 OBD_FREE(pga, npages * sizeof(*pga));
2515                 RETURN(-ENOMEM);
2516         }
2517
2518         for (i = 0, pgp = pga, off = offset;
2519              i < npages;
2520              i++, pgp++, off += CFS_PAGE_SIZE) {
2521
2522                 LASSERT (pgp->pg == NULL);      /* for cleanup */
2523
2524                 rc = -ENOMEM;
2525                 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
2526                 if (pgp->pg == NULL)
2527                         goto out;
2528
2529                 pages[i] = pgp->pg;
2530                 pgp->count = CFS_PAGE_SIZE;
2531                 pgp->off = off;
2532                 pgp->flag = brw_flags;
2533
2534                 if (verify)
2535                         echo_client_page_debug_setup(lsm, pgp->pg, rw,
2536                                                      oa->o_id, off, pgp->count);
2537         }
2538
2539         /* brw mode can only be used at client */
2540         LASSERT(ed->ed_next != NULL);
2541         rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
2542
2543  out:
2544         if (rc != 0 || rw != OBD_BRW_READ)
2545                 verify = 0;
2546
2547         for (i = 0, pgp = pga; i < npages; i++, pgp++) {
2548                 if (pgp->pg == NULL)
2549                         continue;
2550
2551                 if (verify) {
2552                         int vrc;
2553                         vrc = echo_client_page_debug_check(lsm, pgp->pg, oa->o_id,
2554                                                            pgp->off, pgp->count);
2555                         if (vrc != 0 && rc == 0)
2556                                 rc = vrc;
2557                 }
2558                 OBD_PAGE_FREE(pgp->pg);
2559         }
2560         OBD_FREE(pga, npages * sizeof(*pga));
2561         OBD_FREE(pages, npages * sizeof(*pages));
2562         RETURN(rc);
2563 }
2564
2565 static int echo_client_prep_commit(const struct lu_env *env,
2566                                    struct obd_export *exp, int rw,
2567                                    struct obdo *oa, struct echo_object *eco,
2568                                    obd_off offset, obd_size count,
2569                                    obd_size batch, struct obd_trans_info *oti,
2570                                    int async)
2571 {
2572         struct lov_stripe_md *lsm = eco->eo_lsm;
2573         struct obd_ioobj ioo;
2574         struct niobuf_local *lnb;
2575         struct niobuf_remote *rnb;
2576         obd_off off;
2577         obd_size npages, tot_pages;
2578         int i, ret = 0, brw_flags = 0;
2579
2580         ENTRY;
2581
2582         if (count <= 0 || (count & (~CFS_PAGE_MASK)) != 0 ||
2583             (lsm != NULL && lsm->lsm_object_id != oa->o_id))
2584                 RETURN(-EINVAL);
2585
2586         npages = batch >> CFS_PAGE_SHIFT;
2587         tot_pages = count >> CFS_PAGE_SHIFT;
2588
2589         OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
2590         OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
2591
2592         if (lnb == NULL || rnb == NULL)
2593                 GOTO(out, ret = -ENOMEM);
2594
2595         if (rw == OBD_BRW_WRITE && async)
2596                 brw_flags |= OBD_BRW_ASYNC;
2597
2598         obdo_to_ioobj(oa, &ioo);
2599
2600         off = offset;
2601
2602         for(; tot_pages; tot_pages -= npages) {
2603                 int lpages;
2604
2605                 if (tot_pages < npages)
2606                         npages = tot_pages;
2607
2608                 for (i = 0; i < npages; i++, off += CFS_PAGE_SIZE) {
2609                         rnb[i].offset = off;
2610                         rnb[i].len = CFS_PAGE_SIZE;
2611                         rnb[i].flags = brw_flags;
2612                 }
2613
2614                 ioo.ioo_bufcnt = npages;
2615                 oti->oti_transno = 0;
2616
2617                 lpages = npages;
2618                 ret = obd_preprw(env, rw, exp, oa, 1, &ioo, rnb, &lpages,
2619                                  lnb, oti, NULL);
2620                 if (ret != 0)
2621                         GOTO(out, ret);
2622                 LASSERT(lpages == npages);
2623
2624                 for (i = 0; i < lpages; i++) {
2625                         cfs_page_t *page = lnb[i].page;
2626
2627                         /* read past eof? */
2628                         if (page == NULL && lnb[i].rc == 0)
2629                                 continue;
2630
2631                         if (async)
2632                                 lnb[i].flags |= OBD_BRW_ASYNC;
2633
2634                         if (oa->o_id == ECHO_PERSISTENT_OBJID ||
2635                             (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
2636                             (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
2637                                 continue;
2638
2639                         if (rw == OBD_BRW_WRITE)
2640                                 echo_client_page_debug_setup(lsm, page, rw,
2641                                                              oa->o_id,
2642                                                              rnb[i].offset,
2643                                                              rnb[i].len);
2644                         else
2645                                 echo_client_page_debug_check(lsm, page,
2646                                                              oa->o_id,
2647                                                              rnb[i].offset,
2648                                                              rnb[i].len);
2649                 }
2650
2651                 ret = obd_commitrw(env, rw, exp, oa, 1, &ioo,
2652                                    rnb, npages, lnb, oti, ret);
2653                 if (ret != 0)
2654                         GOTO(out, ret);
2655
2656                 /* Reset oti otherwise it would confuse ldiskfs. */
2657                 memset(oti, 0, sizeof(*oti));
2658         }
2659
2660 out:
2661         if (lnb)
2662                 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
2663         if (rnb)
2664                 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
2665         RETURN(ret);
2666 }
2667
2668 static int echo_client_brw_ioctl(const struct lu_env *env, int rw,
2669                                  struct obd_export *exp,
2670                                  struct obd_ioctl_data *data,
2671                                  struct obd_trans_info *dummy_oti)
2672 {
2673         struct obd_device *obd = class_exp2obd(exp);
2674         struct echo_device *ed = obd2echo_dev(obd);
2675         struct echo_client_obd *ec = ed->ed_ec;
2676         struct obdo *oa = &data->ioc_obdo1;
2677         struct echo_object *eco;
2678         int rc;
2679         int async = 1;
2680         long test_mode;
2681         ENTRY;
2682
2683         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2684
2685         rc = echo_get_object(&eco, ed, oa);
2686         if (rc)
2687                 RETURN(rc);
2688
2689         oa->o_valid &= ~OBD_MD_FLHANDLE;
2690
2691         /* OFD/obdfilter works only via prep/commit */
2692         test_mode = (long)data->ioc_pbuf1;
2693         if (test_mode == 1)
2694                 async = 0;
2695
2696         if (ed->ed_next == NULL && test_mode != 3) {
2697                 test_mode = 3;
2698                 data->ioc_plen1 = data->ioc_count;
2699         }
2700
2701         /* Truncate batch size to maximum */
2702         if (data->ioc_plen1 > PTLRPC_MAX_BRW_SIZE)
2703                 data->ioc_plen1 = PTLRPC_MAX_BRW_SIZE;
2704
2705         switch (test_mode) {
2706         case 1:
2707                 /* fall through */
2708         case 2:
2709                 rc = echo_client_kbrw(ed, rw, oa,
2710                                       eco, data->ioc_offset,
2711                                       data->ioc_count, async, dummy_oti);
2712                 break;
2713         case 3:
2714                 rc = echo_client_prep_commit(env, ec->ec_exp, rw, oa,
2715                                              eco, data->ioc_offset,
2716                                              data->ioc_count, data->ioc_plen1,
2717                                              dummy_oti, async);
2718                 break;
2719         default:
2720                 rc = -EINVAL;
2721         }
2722         echo_put_object(eco);
2723         RETURN(rc);
2724 }
2725
2726 static int
2727 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
2728                     int mode, obd_off offset, obd_size nob)
2729 {
2730         struct echo_device     *ed = obd2echo_dev(exp->exp_obd);
2731         struct lustre_handle   *ulh = &oa->o_handle;
2732         struct echo_object     *eco;
2733         obd_off                 end;
2734         int                     rc;
2735         ENTRY;
2736
2737         if (ed->ed_next == NULL)
2738                 RETURN(-EOPNOTSUPP);
2739
2740         if (!(mode == LCK_PR || mode == LCK_PW))
2741                 RETURN(-EINVAL);
2742
2743         if ((offset & (~CFS_PAGE_MASK)) != 0 ||
2744             (nob & (~CFS_PAGE_MASK)) != 0)
2745                 RETURN(-EINVAL);
2746
2747         rc = echo_get_object (&eco, ed, oa);
2748         if (rc != 0)
2749                 RETURN(rc);
2750
2751         end = (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
2752         rc = cl_echo_enqueue(eco, offset, end, mode, &ulh->cookie);
2753         if (rc == 0) {
2754                 oa->o_valid |= OBD_MD_FLHANDLE;
2755                 CDEBUG(D_INFO, "Cookie is "LPX64"\n", ulh->cookie);
2756         }
2757         echo_put_object(eco);
2758         RETURN(rc);
2759 }
2760
2761 static int
2762 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
2763 {
2764         struct echo_device *ed     = obd2echo_dev(exp->exp_obd);
2765         __u64               cookie = oa->o_handle.cookie;
2766
2767         if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
2768                 return -EINVAL;
2769
2770         CDEBUG(D_INFO, "Cookie is "LPX64"\n", cookie);
2771         return cl_echo_cancel(ed, cookie);
2772 }
2773
2774 static int
2775 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2776                       void *karg, void *uarg)
2777 {
2778         struct obd_device      *obd = exp->exp_obd;
2779         struct echo_device     *ed = obd2echo_dev(obd);
2780         struct echo_client_obd *ec = ed->ed_ec;
2781         struct echo_object     *eco;
2782         struct obd_ioctl_data  *data = karg;
2783         struct obd_trans_info   dummy_oti;
2784         struct lu_env          *env;
2785         struct oti_req_ack_lock *ack_lock;
2786         struct obdo            *oa;
2787         struct lu_fid           fid;
2788         int                     rw = OBD_BRW_READ;
2789         int                     rc = 0;
2790         int                     i;
2791         ENTRY;
2792
2793         memset(&dummy_oti, 0, sizeof(dummy_oti));
2794
2795         oa = &data->ioc_obdo1;
2796         if (!(oa->o_valid & OBD_MD_FLGROUP)) {
2797                 oa->o_valid |= OBD_MD_FLGROUP;
2798                 oa->o_seq = FID_SEQ_ECHO;
2799         }
2800
2801         /* This FID is unpacked just for validation at this point */
2802         rc = fid_ostid_unpack(&fid, &oa->o_oi, 0);
2803         if (rc < 0)
2804                 RETURN(rc);
2805
2806         OBD_ALLOC_PTR(env);
2807         if (env == NULL)
2808                 RETURN(-ENOMEM);
2809
2810         rc = lu_env_init(env, LCT_DT_THREAD);
2811         if (rc)
2812                 GOTO(out, rc = -ENOMEM);
2813
2814         switch (cmd) {
2815         case OBD_IOC_CREATE:                    /* may create echo object */
2816                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2817                         GOTO (out, rc = -EPERM);
2818
2819                 rc = echo_create_object(env, ed, 1, oa, data->ioc_pbuf1,
2820                                         data->ioc_plen1, &dummy_oti);
2821                 GOTO(out, rc);
2822
2823         case OBD_IOC_ECHO_MD: {
2824                 int count;
2825                 int cmd;
2826                 char *dir = NULL;
2827                 int dirlen;
2828                 __u64 id;
2829
2830                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2831                         GOTO(out, rc = -EPERM);
2832
2833                 count = data->ioc_count;
2834                 cmd = data->ioc_command;
2835
2836                 id = data->ioc_obdo2.o_id;
2837
2838                 dirlen = data->ioc_plen1;
2839                 OBD_ALLOC(dir, dirlen + 1);
2840                 if (dir == NULL)
2841                         GOTO(out, rc = -ENOMEM);
2842
2843                 if (cfs_copy_from_user(dir, data->ioc_pbuf1, dirlen)) {
2844                         OBD_FREE(dir, data->ioc_plen1 + 1);
2845                         GOTO(out, rc = -EFAULT);
2846                 }
2847
2848                 rc = echo_md_handler(ed, cmd, dir, dirlen, id, count, data);
2849                 OBD_FREE(dir, dirlen + 1);
2850                 GOTO(out, rc);
2851         }
2852         case OBD_IOC_ECHO_ALLOC_SEQ: {
2853                 struct lu_env   *cl_env;
2854                 int              refcheck;
2855                 __u64            seq;
2856                 int              max_count;
2857
2858                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2859                         GOTO(out, rc = -EPERM);
2860
2861                 cl_env = cl_env_get(&refcheck);
2862                 if (IS_ERR(cl_env))
2863                         GOTO(out, rc = PTR_ERR(cl_env));
2864
2865                 rc = lu_env_refill_by_tags(cl_env, ECHO_MD_CTX_TAG,
2866                                             ECHO_MD_SES_TAG);
2867                 if (rc != 0) {
2868                         cl_env_put(cl_env, &refcheck);
2869                         GOTO(out, rc);
2870                 }
2871
2872                 rc = seq_client_get_seq(cl_env, ed->ed_cl_seq, &seq);
2873                 cl_env_put(cl_env, &refcheck);
2874                 if (rc < 0) {
2875                         CERROR("%s: Can not alloc seq: rc = %d\n",
2876                                obd->obd_name, rc);
2877                         GOTO(out, rc);
2878                 }
2879
2880                 if (cfs_copy_to_user(data->ioc_pbuf1, &seq, data->ioc_plen1))
2881                         return -EFAULT;
2882
2883                 max_count = LUSTRE_METADATA_SEQ_MAX_WIDTH;
2884                 if (cfs_copy_to_user(data->ioc_pbuf2, &max_count,
2885                                      data->ioc_plen2))
2886                         return -EFAULT;
2887                 GOTO(out, rc);
2888         }
2889         case OBD_IOC_DESTROY:
2890                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2891                         GOTO (out, rc = -EPERM);
2892
2893                 rc = echo_get_object(&eco, ed, oa);
2894                 if (rc == 0) {
2895                         rc = obd_destroy(env, ec->ec_exp, oa, eco->eo_lsm,
2896                                          &dummy_oti, NULL, NULL);
2897                         if (rc == 0)
2898                                 eco->eo_deleted = 1;
2899                         echo_put_object(eco);
2900                 }
2901                 GOTO(out, rc);
2902
2903         case OBD_IOC_GETATTR:
2904                 rc = echo_get_object(&eco, ed, oa);
2905                 if (rc == 0) {
2906                         struct obd_info oinfo = { { { 0 } } };
2907                         oinfo.oi_md = eco->eo_lsm;
2908                         oinfo.oi_oa = oa;
2909                         rc = obd_getattr(env, ec->ec_exp, &oinfo);
2910                         echo_put_object(eco);
2911                 }
2912                 GOTO(out, rc);
2913
2914         case OBD_IOC_SETATTR:
2915                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2916                         GOTO (out, rc = -EPERM);
2917
2918                 rc = echo_get_object(&eco, ed, oa);
2919                 if (rc == 0) {
2920                         struct obd_info oinfo = { { { 0 } } };
2921                         oinfo.oi_oa = oa;
2922                         oinfo.oi_md = eco->eo_lsm;
2923
2924                         rc = obd_setattr(env, ec->ec_exp, &oinfo, NULL);
2925                         echo_put_object(eco);
2926                 }
2927                 GOTO(out, rc);
2928
2929         case OBD_IOC_BRW_WRITE:
2930                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2931                         GOTO (out, rc = -EPERM);
2932
2933                 rw = OBD_BRW_WRITE;
2934                 /* fall through */
2935         case OBD_IOC_BRW_READ:
2936                 rc = echo_client_brw_ioctl(env, rw, exp, data, &dummy_oti);
2937                 GOTO(out, rc);
2938
2939         case ECHO_IOC_GET_STRIPE:
2940                 rc = echo_get_object(&eco, ed, oa);
2941                 if (rc == 0) {
2942                         rc = echo_copyout_lsm(eco->eo_lsm, data->ioc_pbuf1,
2943                                               data->ioc_plen1);
2944                         echo_put_object(eco);
2945                 }
2946                 GOTO(out, rc);
2947
2948         case ECHO_IOC_SET_STRIPE:
2949                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2950                         GOTO (out, rc = -EPERM);
2951
2952                 if (data->ioc_pbuf1 == NULL) {  /* unset */
2953                         rc = echo_get_object(&eco, ed, oa);
2954                         if (rc == 0) {
2955                                 eco->eo_deleted = 1;
2956                                 echo_put_object(eco);
2957                         }
2958                 } else {
2959                         rc = echo_create_object(env, ed, 0, oa,
2960                                                 data->ioc_pbuf1,
2961                                                 data->ioc_plen1, &dummy_oti);
2962                 }
2963                 GOTO (out, rc);
2964
2965         case ECHO_IOC_ENQUEUE:
2966                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2967                         GOTO (out, rc = -EPERM);
2968
2969                 rc = echo_client_enqueue(exp, oa,
2970                                          data->ioc_conn1, /* lock mode */
2971                                          data->ioc_offset,
2972                                          data->ioc_count);/*extent*/
2973                 GOTO (out, rc);
2974
2975         case ECHO_IOC_CANCEL:
2976                 rc = echo_client_cancel(exp, oa);
2977                 GOTO (out, rc);
2978
2979         default:
2980                 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
2981                 GOTO (out, rc = -ENOTTY);
2982         }
2983
2984         EXIT;
2985 out:
2986         lu_env_fini(env);
2987         OBD_FREE_PTR(env);
2988
2989         /* XXX this should be in a helper also called by target_send_reply */
2990         for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
2991              i++, ack_lock++) {
2992                 if (!ack_lock->mode)
2993                         break;
2994                 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
2995         }
2996
2997         return rc;
2998 }
2999
3000 static int echo_client_setup(const struct lu_env *env,
3001                              struct obd_device *obddev, struct lustre_cfg *lcfg)
3002 {
3003         struct echo_client_obd *ec = &obddev->u.echo_client;
3004         struct obd_device *tgt;
3005         struct obd_uuid echo_uuid = { "ECHO_UUID" };
3006         struct obd_connect_data *ocd = NULL;
3007         int rc;
3008         ENTRY;
3009
3010         if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
3011                 CERROR("requires a TARGET OBD name\n");
3012                 RETURN(-EINVAL);
3013         }
3014
3015         tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
3016         if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
3017                 CERROR("device not attached or not set up (%s)\n",
3018                        lustre_cfg_string(lcfg, 1));
3019                 RETURN(-EINVAL);
3020         }
3021
3022         spin_lock_init(&ec->ec_lock);
3023         CFS_INIT_LIST_HEAD (&ec->ec_objects);
3024         CFS_INIT_LIST_HEAD (&ec->ec_locks);
3025         ec->ec_unique = 0;
3026         ec->ec_nstripes = 0;
3027
3028         if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
3029                 lu_context_tags_update(ECHO_MD_CTX_TAG);
3030                 lu_session_tags_update(ECHO_MD_SES_TAG);
3031                 RETURN(0);
3032         }
3033
3034         OBD_ALLOC(ocd, sizeof(*ocd));
3035         if (ocd == NULL) {
3036                 CERROR("Can't alloc ocd connecting to %s\n",
3037                        lustre_cfg_string(lcfg, 1));
3038                 return -ENOMEM;
3039         }
3040
3041         ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
3042                                  OBD_CONNECT_GRANT | OBD_CONNECT_FULL20 |
3043                                  OBD_CONNECT_64BITHASH | OBD_CONNECT_LVB_TYPE;
3044         ocd->ocd_version = LUSTRE_VERSION_CODE;
3045         ocd->ocd_group = FID_SEQ_ECHO;
3046
3047         rc = obd_connect(env, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
3048         if (rc == 0) {
3049                 /* Turn off pinger because it connects to tgt obd directly. */
3050                 spin_lock(&tgt->obd_dev_lock);
3051                 cfs_list_del_init(&ec->ec_exp->exp_obd_chain_timed);
3052                 spin_unlock(&tgt->obd_dev_lock);
3053         }
3054
3055         OBD_FREE(ocd, sizeof(*ocd));
3056
3057         if (rc != 0) {
3058                 CERROR("fail to connect to device %s\n",
3059                        lustre_cfg_string(lcfg, 1));
3060                 return (rc);
3061         }
3062
3063         RETURN(rc);
3064 }
3065
3066 static int echo_client_cleanup(struct obd_device *obddev)
3067 {
3068         struct echo_device *ed = obd2echo_dev(obddev);
3069         struct echo_client_obd *ec = &obddev->u.echo_client;
3070         int rc;
3071         ENTRY;
3072
3073         /*Do nothing for Metadata echo client*/
3074         if (ed == NULL )
3075                 RETURN(0);
3076
3077         if (ed->ed_next_ismd) {
3078                 lu_context_tags_clear(ECHO_MD_CTX_TAG);
3079                 lu_session_tags_clear(ECHO_MD_SES_TAG);
3080                 RETURN(0);
3081         }
3082
3083         if (!cfs_list_empty(&obddev->obd_exports)) {
3084                 CERROR("still has clients!\n");
3085                 RETURN(-EBUSY);
3086         }
3087
3088         LASSERT(cfs_atomic_read(&ec->ec_exp->exp_refcount) > 0);
3089         rc = obd_disconnect(ec->ec_exp);
3090         if (rc != 0)
3091                 CERROR("fail to disconnect device: %d\n", rc);
3092
3093         RETURN(rc);
3094 }
3095
3096 static int echo_client_connect(const struct lu_env *env,
3097                                struct obd_export **exp,
3098                                struct obd_device *src, struct obd_uuid *cluuid,
3099                                struct obd_connect_data *data, void *localdata)
3100 {
3101         int                rc;
3102         struct lustre_handle conn = { 0 };
3103
3104         ENTRY;
3105         rc = class_connect(&conn, src, cluuid);
3106         if (rc == 0) {
3107                 *exp = class_conn2export(&conn);
3108         }
3109
3110         RETURN (rc);
3111 }
3112
3113 static int echo_client_disconnect(struct obd_export *exp)
3114 {
3115 #if 0
3116         struct obd_device      *obd;
3117         struct echo_client_obd *ec;
3118         struct ec_lock         *ecl;
3119 #endif
3120         int                     rc;
3121         ENTRY;
3122
3123         if (exp == NULL)
3124                 GOTO(out, rc = -EINVAL);
3125
3126 #if 0
3127         obd = exp->exp_obd;
3128         ec = &obd->u.echo_client;
3129
3130         /* no more contention on export's lock list */
3131         while (!cfs_list_empty (&exp->exp_ec_data.eced_locks)) {
3132                 ecl = cfs_list_entry (exp->exp_ec_data.eced_locks.next,
3133                                       struct ec_lock, ecl_exp_chain);
3134                 cfs_list_del (&ecl->ecl_exp_chain);
3135
3136                 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm,
3137                                  ecl->ecl_mode, &ecl->ecl_lock_handle);
3138
3139                 CDEBUG (D_INFO, "Cancel lock on object "LPX64" on disconnect "
3140                         "(%d)\n", ecl->ecl_object->eco_id, rc);
3141
3142                 echo_put_object (ecl->ecl_object);
3143                 OBD_FREE (ecl, sizeof (*ecl));
3144         }
3145 #endif
3146
3147         rc = class_disconnect(exp);
3148         GOTO(out, rc);
3149  out:
3150         return rc;
3151 }
3152
3153 static struct obd_ops echo_client_obd_ops = {
3154         .o_owner       = THIS_MODULE,
3155
3156 #if 0
3157         .o_setup       = echo_client_setup,
3158         .o_cleanup     = echo_client_cleanup,
3159 #endif
3160
3161         .o_iocontrol   = echo_client_iocontrol,
3162         .o_connect     = echo_client_connect,
3163         .o_disconnect  = echo_client_disconnect
3164 };
3165
3166 int echo_client_init(void)
3167 {
3168         struct lprocfs_static_vars lvars = { 0 };
3169         int rc;
3170
3171         lprocfs_echo_init_vars(&lvars);
3172
3173         rc = lu_kmem_init(echo_caches);
3174         if (rc == 0) {
3175                 rc = class_register_type(&echo_client_obd_ops, NULL,
3176                                          lvars.module_vars,
3177                                          LUSTRE_ECHO_CLIENT_NAME,
3178                                          &echo_device_type);
3179                 if (rc)
3180                         lu_kmem_fini(echo_caches);
3181         }
3182         return rc;
3183 }
3184
3185 void echo_client_exit(void)
3186 {
3187         class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
3188         lu_kmem_fini(echo_caches);
3189 }
3190
3191 #ifdef __KERNEL__
3192 static int __init obdecho_init(void)
3193 {
3194         struct lprocfs_static_vars lvars;
3195         int rc;
3196
3197         ENTRY;
3198         LCONSOLE_INFO("Echo OBD driver; http://www.lustre.org/\n");
3199
3200         LASSERT(CFS_PAGE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
3201
3202         lprocfs_echo_init_vars(&lvars);
3203
3204 # ifdef HAVE_SERVER_SUPPORT
3205         rc = echo_persistent_pages_init();
3206         if (rc != 0)
3207                 goto failed_0;
3208
3209         rc = class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
3210                                  LUSTRE_ECHO_NAME, NULL);
3211         if (rc != 0)
3212                 goto failed_1;
3213 # endif
3214
3215         rc = echo_client_init();
3216
3217 # ifdef HAVE_SERVER_SUPPORT
3218         if (rc == 0)
3219                 RETURN(0);
3220
3221         class_unregister_type(LUSTRE_ECHO_NAME);
3222 failed_1:
3223         echo_persistent_pages_fini();
3224 failed_0:
3225 # endif
3226         RETURN(rc);
3227 }
3228
3229 static void /*__exit*/ obdecho_exit(void)
3230 {
3231         echo_client_exit();
3232
3233 # ifdef HAVE_SERVER_SUPPORT
3234         class_unregister_type(LUSTRE_ECHO_NAME);
3235         echo_persistent_pages_fini();
3236 # endif
3237 }
3238
3239 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3240 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
3241 MODULE_LICENSE("GPL");
3242
3243 cfs_module(obdecho, LUSTRE_VERSION_STRING, obdecho_init, obdecho_exit);
3244 #endif /* __KERNEL__ */
3245
3246 /** @} echo_client */