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