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