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