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