Whamcloud - gitweb
38cff007658d6a108acddd4b69a5ccdc21976d6d
[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   = current_uid();
2095         ucred->uc_gid   = ucred->uc_o_gid   = current_gid();
2096         ucred->uc_fsuid = ucred->uc_o_fsuid = current_fsuid();
2097         ucred->uc_fsgid = ucred->uc_o_fsgid = current_fsgid();
2098         ucred->uc_cap   = cfs_curproc_cap_pack();
2099
2100         /* remove fs privilege for non-root user. */
2101         if (ucred->uc_fsuid)
2102                 ucred->uc_cap &= ~CFS_CAP_FS_MASK;
2103         ucred->uc_valid = UCRED_NEW;
2104 }
2105
2106 static void echo_ucred_fini(struct lu_env *env)
2107 {
2108         struct lu_ucred *ucred = lu_ucred(env);
2109         ucred->uc_valid = UCRED_INIT;
2110 }
2111
2112 #define ECHO_MD_CTX_TAG (LCT_REMEMBER | LCT_MD_THREAD)
2113 #define ECHO_MD_SES_TAG (LCT_REMEMBER | LCT_SESSION | LCT_SERVER_SESSION)
2114 static int echo_md_handler(struct echo_device *ed, int command,
2115                            char *path, int path_len, __u64 id, int count,
2116                            struct obd_ioctl_data *data)
2117 {
2118         struct echo_thread_info *info;
2119         struct lu_device      *ld = ed->ed_next;
2120         struct lu_env         *env;
2121         int                    refcheck;
2122         struct lu_object      *parent;
2123         char                  *name = NULL;
2124         int                    namelen = data->ioc_plen2;
2125         int                    rc = 0;
2126         ENTRY;
2127
2128         if (ld == NULL) {
2129                 CERROR("MD echo client is not being initialized properly\n");
2130                 RETURN(-EINVAL);
2131         }
2132
2133         if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
2134                 CERROR("Only support MDD layer right now!\n");
2135                 RETURN(-EINVAL);
2136         }
2137
2138         env = cl_env_get(&refcheck);
2139         if (IS_ERR(env))
2140                 RETURN(PTR_ERR(env));
2141
2142         rc = lu_env_refill_by_tags(env, ECHO_MD_CTX_TAG, ECHO_MD_SES_TAG);
2143         if (rc != 0)
2144                 GOTO(out_env, rc);
2145
2146         /* init big_lmm buffer */
2147         info = echo_env_info(env);
2148         LASSERT(info->eti_big_lmm == NULL);
2149         OBD_ALLOC_LARGE(info->eti_big_lmm, MIN_MD_SIZE);
2150         if (info->eti_big_lmm == NULL)
2151                 GOTO(out_env, rc = -ENOMEM);
2152         info->eti_big_lmmsize = MIN_MD_SIZE;
2153
2154         parent = echo_resolve_path(env, ed, path, path_len);
2155         if (IS_ERR(parent)) {
2156                 CERROR("Can not resolve the path %s: rc = %ld\n", path,
2157                         PTR_ERR(parent));
2158                 GOTO(out_free, rc = PTR_ERR(parent));
2159         }
2160
2161         if (namelen > 0) {
2162                 OBD_ALLOC(name, namelen + 1);
2163                 if (name == NULL)
2164                         GOTO(out_put, rc = -ENOMEM);
2165                 if (copy_from_user(name, data->ioc_pbuf2, namelen))
2166                         GOTO(out_name, rc = -EFAULT);
2167         }
2168
2169         echo_ucred_init(env);
2170
2171         switch (command) {
2172         case ECHO_MD_CREATE:
2173         case ECHO_MD_MKDIR: {
2174                 struct echo_thread_info *info = echo_env_info(env);
2175                 __u32 mode = data->ioc_obdo2.o_mode;
2176                 struct lu_fid *fid = &info->eti_fid;
2177                 int stripe_count = (int)data->ioc_obdo2.o_misc;
2178                 int stripe_index = (int)data->ioc_obdo2.o_stripe_idx;
2179
2180                 rc = ostid_to_fid(fid, &data->ioc_obdo1.o_oi, 0);
2181                 if (rc != 0)
2182                         break;
2183
2184                 /* In the function below, .hs_keycmp resolves to
2185                  * lu_obj_hop_keycmp() */
2186                 /* coverity[overrun-buffer-val] */
2187                 rc = echo_create_md_object(env, ed, parent, fid, name, namelen,
2188                                            id, mode, count, stripe_count,
2189                                            stripe_index);
2190                 break;
2191         }
2192         case ECHO_MD_DESTROY:
2193         case ECHO_MD_RMDIR: {
2194                 __u32 mode = data->ioc_obdo2.o_mode;
2195
2196                 rc = echo_destroy_object(env, ed, parent, name, namelen,
2197                                          id, mode, count);
2198                 break;
2199         }
2200         case ECHO_MD_LOOKUP:
2201                 rc = echo_lookup_object(env, ed, parent, id, count);
2202                 break;
2203         case ECHO_MD_GETATTR:
2204                 rc = echo_getattr_object(env, ed, parent, id, count);
2205                 break;
2206         case ECHO_MD_SETATTR:
2207                 rc = echo_setattr_object(env, ed, parent, id, count);
2208                 break;
2209         default:
2210                 CERROR("unknown command %d\n", command);
2211                 rc = -EINVAL;
2212                 break;
2213         }
2214         echo_ucred_fini(env);
2215
2216 out_name:
2217         if (name != NULL)
2218                 OBD_FREE(name, namelen + 1);
2219 out_put:
2220         lu_object_put(env, parent);
2221 out_free:
2222         LASSERT(info->eti_big_lmm);
2223         OBD_FREE_LARGE(info->eti_big_lmm, info->eti_big_lmmsize);
2224         info->eti_big_lmm = NULL;
2225         info->eti_big_lmmsize = 0;
2226 out_env:
2227         cl_env_put(env, &refcheck);
2228         return rc;
2229 }
2230 #endif /* HAVE_SERVER_SUPPORT */
2231
2232 static int echo_create_object(const struct lu_env *env, struct echo_device *ed,
2233                               int on_target, struct obdo *oa, void *ulsm,
2234                               int ulsm_nob, struct obd_trans_info *oti)
2235 {
2236         struct echo_object     *eco;
2237         struct echo_client_obd *ec = ed->ed_ec;
2238         struct lov_stripe_md   *lsm = NULL;
2239         int                     rc;
2240         int                     created = 0;
2241         ENTRY;
2242
2243         if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
2244             (on_target ||                       /* set_stripe */
2245              ec->ec_nstripes != 0)) {           /* LOV */
2246                 CERROR ("No valid oid\n");
2247                 RETURN(-EINVAL);
2248         }
2249
2250         rc = echo_alloc_memmd(ed, &lsm);
2251         if (rc < 0) {
2252                 CERROR("Cannot allocate md: rc = %d\n", rc);
2253                 GOTO(failed, rc);
2254         }
2255
2256         if (ulsm != NULL) {
2257                 int i, idx;
2258
2259                 rc = echo_copyin_lsm (ed, lsm, ulsm, ulsm_nob);
2260                 if (rc != 0)
2261                         GOTO(failed, rc);
2262
2263                 if (lsm->lsm_stripe_count == 0)
2264                         lsm->lsm_stripe_count = ec->ec_nstripes;
2265
2266                 if (lsm->lsm_stripe_size == 0)
2267                         lsm->lsm_stripe_size = PAGE_CACHE_SIZE;
2268
2269                 idx = cfs_rand();
2270
2271                 /* setup stripes: indices + default ids if required */
2272                 for (i = 0; i < lsm->lsm_stripe_count; i++) {
2273                         if (ostid_id(&lsm->lsm_oinfo[i]->loi_oi) == 0)
2274                                 lsm->lsm_oinfo[i]->loi_oi = lsm->lsm_oi;
2275
2276                         lsm->lsm_oinfo[i]->loi_ost_idx =
2277                                 (idx + i) % ec->ec_nstripes;
2278                 }
2279         }
2280
2281         /* setup object ID here for !on_target and LOV hint */
2282         if (oa->o_valid & OBD_MD_FLID) {
2283                 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2284                 lsm->lsm_oi = oa->o_oi;
2285         }
2286
2287         if (ostid_id(&lsm->lsm_oi) == 0)
2288                 ostid_set_id(&lsm->lsm_oi, ++last_object_id);
2289
2290         rc = 0;
2291         if (on_target) {
2292                 /* Only echo objects are allowed to be created */
2293                 LASSERT((oa->o_valid & OBD_MD_FLGROUP) &&
2294                         (ostid_seq(&oa->o_oi) == FID_SEQ_ECHO));
2295                 rc = obd_create(env, ec->ec_exp, oa, &lsm, oti);
2296                 if (rc != 0) {
2297                         CERROR("Cannot create objects: rc = %d\n", rc);
2298                         GOTO(failed, rc);
2299                 }
2300                 created = 1;
2301         }
2302
2303         /* See what object ID we were given */
2304         oa->o_oi = lsm->lsm_oi;
2305         oa->o_valid |= OBD_MD_FLID;
2306
2307         eco = cl_echo_object_find(ed, &lsm);
2308         if (IS_ERR(eco))
2309                 GOTO(failed, rc = PTR_ERR(eco));
2310         cl_echo_object_put(eco);
2311
2312         CDEBUG(D_INFO, "oa oid "DOSTID"\n", POSTID(&oa->o_oi));
2313         EXIT;
2314
2315  failed:
2316         if (created && rc)
2317                 obd_destroy(env, ec->ec_exp, oa, lsm, oti, NULL, NULL);
2318         if (lsm)
2319                 echo_free_memmd(ed, &lsm);
2320         if (rc)
2321                 CERROR("create object failed with: rc = %d\n", rc);
2322         return (rc);
2323 }
2324
2325 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
2326                            struct obdo *oa)
2327 {
2328         struct lov_stripe_md   *lsm = NULL;
2329         struct echo_object     *eco;
2330         int                     rc;
2331         ENTRY;
2332
2333         if ((oa->o_valid & OBD_MD_FLID) == 0 || ostid_id(&oa->o_oi) == 0) {
2334                 /* disallow use of object id 0 */
2335                 CERROR ("No valid oid\n");
2336                 RETURN(-EINVAL);
2337         }
2338
2339         rc = echo_alloc_memmd(ed, &lsm);
2340         if (rc < 0)
2341                 RETURN(rc);
2342
2343         lsm->lsm_oi = oa->o_oi;
2344         if (!(oa->o_valid & OBD_MD_FLGROUP))
2345                 ostid_set_seq_echo(&lsm->lsm_oi);
2346
2347         rc = 0;
2348         eco = cl_echo_object_find(ed, &lsm);
2349         if (!IS_ERR(eco))
2350                 *ecop = eco;
2351         else
2352                 rc = PTR_ERR(eco);
2353         if (lsm)
2354                 echo_free_memmd(ed, &lsm);
2355         RETURN(rc);
2356 }
2357
2358 static void echo_put_object(struct echo_object *eco)
2359 {
2360         if (cl_echo_object_put(eco))
2361                 CERROR("echo client: drop an object failed");
2362 }
2363
2364 static void
2365 echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
2366 {
2367         unsigned long stripe_count;
2368         unsigned long stripe_size;
2369         unsigned long width;
2370         unsigned long woffset;
2371         int           stripe_index;
2372         obd_off       offset;
2373
2374         if (lsm->lsm_stripe_count <= 1)
2375                 return;
2376
2377         offset       = *offp;
2378         stripe_size  = lsm->lsm_stripe_size;
2379         stripe_count = lsm->lsm_stripe_count;
2380
2381         /* width = # bytes in all stripes */
2382         width = stripe_size * stripe_count;
2383
2384         /* woffset = offset within a width; offset = whole number of widths */
2385         woffset = do_div (offset, width);
2386
2387         stripe_index = woffset / stripe_size;
2388
2389         *idp = ostid_id(&lsm->lsm_oinfo[stripe_index]->loi_oi);
2390         *offp = offset * stripe_size + woffset % stripe_size;
2391 }
2392
2393 static void
2394 echo_client_page_debug_setup(struct lov_stripe_md *lsm,
2395                              struct page *page, int rw, obd_id id,
2396                              obd_off offset, obd_off count)
2397 {
2398         char    *addr;
2399         obd_off  stripe_off;
2400         obd_id   stripe_id;
2401         int      delta;
2402
2403         /* no partial pages on the client */
2404         LASSERT(count == PAGE_CACHE_SIZE);
2405
2406         addr = kmap(page);
2407
2408         for (delta = 0; delta < PAGE_CACHE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2409                 if (rw == OBD_BRW_WRITE) {
2410                         stripe_off = offset + delta;
2411                         stripe_id = id;
2412                         echo_get_stripe_off_id(lsm, &stripe_off, &stripe_id);
2413                 } else {
2414                         stripe_off = 0xdeadbeef00c0ffeeULL;
2415                         stripe_id = 0xdeadbeef00c0ffeeULL;
2416                 }
2417                 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
2418                                   stripe_off, stripe_id);
2419         }
2420
2421         kunmap(page);
2422 }
2423
2424 static int echo_client_page_debug_check(struct lov_stripe_md *lsm,
2425                                         struct page *page, obd_id id,
2426                                         obd_off offset, obd_off count)
2427 {
2428         obd_off stripe_off;
2429         obd_id  stripe_id;
2430         char   *addr;
2431         int     delta;
2432         int     rc;
2433         int     rc2;
2434
2435         /* no partial pages on the client */
2436         LASSERT(count == PAGE_CACHE_SIZE);
2437
2438         addr = kmap(page);
2439
2440         for (rc = delta = 0; delta < PAGE_CACHE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2441                 stripe_off = offset + delta;
2442                 stripe_id = id;
2443                 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
2444
2445                 rc2 = block_debug_check("test_brw",
2446                                         addr + delta, OBD_ECHO_BLOCK_SIZE,
2447                                         stripe_off, stripe_id);
2448                 if (rc2 != 0) {
2449                         CERROR ("Error in echo object "LPX64"\n", id);
2450                         rc = rc2;
2451                 }
2452         }
2453
2454         kunmap(page);
2455         return rc;
2456 }
2457
2458 static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
2459                             struct echo_object *eco, obd_off offset,
2460                             obd_size count, int async,
2461                             struct obd_trans_info *oti)
2462 {
2463         struct lov_stripe_md   *lsm = eco->eo_lsm;
2464         obd_count               npages;
2465         struct brw_page        *pga;
2466         struct brw_page        *pgp;
2467         struct page            **pages;
2468         obd_off                 off;
2469         int                     i;
2470         int                     rc;
2471         int                     verify;
2472         int                     gfp_mask;
2473         int                     brw_flags = 0;
2474         ENTRY;
2475
2476         verify = (ostid_id(&oa->o_oi) != ECHO_PERSISTENT_OBJID &&
2477                   (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
2478                   (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
2479
2480         gfp_mask = ((ostid_id(&oa->o_oi) & 2) == 0) ? GFP_IOFS : GFP_HIGHUSER;
2481
2482         LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
2483         LASSERT(lsm != NULL);
2484         LASSERT(ostid_id(&lsm->lsm_oi) == ostid_id(&oa->o_oi));
2485
2486         if (count <= 0 ||
2487             (count & (~CFS_PAGE_MASK)) != 0)
2488                 RETURN(-EINVAL);
2489
2490         /* XXX think again with misaligned I/O */
2491         npages = count >> PAGE_CACHE_SHIFT;
2492
2493         if (rw == OBD_BRW_WRITE)
2494                 brw_flags = OBD_BRW_ASYNC;
2495
2496         OBD_ALLOC(pga, npages * sizeof(*pga));
2497         if (pga == NULL)
2498                 RETURN(-ENOMEM);
2499
2500         OBD_ALLOC(pages, npages * sizeof(*pages));
2501         if (pages == NULL) {
2502                 OBD_FREE(pga, npages * sizeof(*pga));
2503                 RETURN(-ENOMEM);
2504         }
2505
2506         for (i = 0, pgp = pga, off = offset;
2507              i < npages;
2508              i++, pgp++, off += PAGE_CACHE_SIZE) {
2509
2510                 LASSERT (pgp->pg == NULL);      /* for cleanup */
2511
2512                 rc = -ENOMEM;
2513                 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
2514                 if (pgp->pg == NULL)
2515                         goto out;
2516
2517                 pages[i] = pgp->pg;
2518                 pgp->count = PAGE_CACHE_SIZE;
2519                 pgp->off = off;
2520                 pgp->flag = brw_flags;
2521
2522                 if (verify)
2523                         echo_client_page_debug_setup(lsm, pgp->pg, rw,
2524                                                      ostid_id(&oa->o_oi), off,
2525                                                      pgp->count);
2526         }
2527
2528         /* brw mode can only be used at client */
2529         LASSERT(ed->ed_next != NULL);
2530         rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
2531
2532  out:
2533         if (rc != 0 || rw != OBD_BRW_READ)
2534                 verify = 0;
2535
2536         for (i = 0, pgp = pga; i < npages; i++, pgp++) {
2537                 if (pgp->pg == NULL)
2538                         continue;
2539
2540                 if (verify) {
2541                         int vrc;
2542                         vrc = echo_client_page_debug_check(lsm, pgp->pg,
2543                                                            ostid_id(&oa->o_oi),
2544                                                            pgp->off, pgp->count);
2545                         if (vrc != 0 && rc == 0)
2546                                 rc = vrc;
2547                 }
2548                 OBD_PAGE_FREE(pgp->pg);
2549         }
2550         OBD_FREE(pga, npages * sizeof(*pga));
2551         OBD_FREE(pages, npages * sizeof(*pages));
2552         RETURN(rc);
2553 }
2554
2555 static int echo_client_prep_commit(const struct lu_env *env,
2556                                    struct obd_export *exp, int rw,
2557                                    struct obdo *oa, struct echo_object *eco,
2558                                    obd_off offset, obd_size count,
2559                                    obd_size batch, struct obd_trans_info *oti,
2560                                    int async)
2561 {
2562         struct lov_stripe_md *lsm = eco->eo_lsm;
2563         struct obd_ioobj ioo;
2564         struct niobuf_local *lnb;
2565         struct niobuf_remote *rnb;
2566         obd_off off;
2567         obd_size npages, tot_pages;
2568         int i, ret = 0, brw_flags = 0;
2569
2570         ENTRY;
2571
2572         if (count <= 0 || (count & (~CFS_PAGE_MASK)) != 0 ||
2573             (lsm != NULL && ostid_id(&lsm->lsm_oi) != ostid_id(&oa->o_oi)))
2574                 RETURN(-EINVAL);
2575
2576         npages = batch >> PAGE_CACHE_SHIFT;
2577         tot_pages = count >> PAGE_CACHE_SHIFT;
2578
2579         OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
2580         OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
2581
2582         if (lnb == NULL || rnb == NULL)
2583                 GOTO(out, ret = -ENOMEM);
2584
2585         if (rw == OBD_BRW_WRITE && async)
2586                 brw_flags |= OBD_BRW_ASYNC;
2587
2588         obdo_to_ioobj(oa, &ioo);
2589
2590         off = offset;
2591
2592         for(; tot_pages; tot_pages -= npages) {
2593                 int lpages;
2594
2595                 if (tot_pages < npages)
2596                         npages = tot_pages;
2597
2598                 for (i = 0; i < npages; i++, off += PAGE_CACHE_SIZE) {
2599                         rnb[i].offset = off;
2600                         rnb[i].len = PAGE_CACHE_SIZE;
2601                         rnb[i].flags = brw_flags;
2602                 }
2603
2604                 ioo.ioo_bufcnt = npages;
2605                 oti->oti_transno = 0;
2606
2607                 lpages = npages;
2608                 ret = obd_preprw(env, rw, exp, oa, 1, &ioo, rnb, &lpages,
2609                                  lnb, oti, NULL);
2610                 if (ret != 0)
2611                         GOTO(out, ret);
2612                 LASSERT(lpages == npages);
2613
2614                 for (i = 0; i < lpages; i++) {
2615                         struct page *page = lnb[i].page;
2616
2617                         /* read past eof? */
2618                         if (page == NULL && lnb[i].rc == 0)
2619                                 continue;
2620
2621                         if (async)
2622                                 lnb[i].flags |= OBD_BRW_ASYNC;
2623
2624                         if (ostid_id(&oa->o_oi) == ECHO_PERSISTENT_OBJID ||
2625                             (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
2626                             (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
2627                                 continue;
2628
2629                         if (rw == OBD_BRW_WRITE)
2630                                 echo_client_page_debug_setup(lsm, page, rw,
2631                                                             ostid_id(&oa->o_oi),
2632                                                              rnb[i].offset,
2633                                                              rnb[i].len);
2634                         else
2635                                 echo_client_page_debug_check(lsm, page,
2636                                                             ostid_id(&oa->o_oi),
2637                                                              rnb[i].offset,
2638                                                              rnb[i].len);
2639                 }
2640
2641                 ret = obd_commitrw(env, rw, exp, oa, 1, &ioo,
2642                                    rnb, npages, lnb, oti, ret);
2643                 if (ret != 0)
2644                         GOTO(out, ret);
2645
2646                 /* Reset oti otherwise it would confuse ldiskfs. */
2647                 memset(oti, 0, sizeof(*oti));
2648
2649                 /* Reuse env context. */
2650                 lu_context_exit((struct lu_context *)&env->le_ctx);
2651                 lu_context_enter((struct lu_context *)&env->le_ctx);
2652         }
2653
2654 out:
2655         if (lnb)
2656                 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
2657         if (rnb)
2658                 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
2659         RETURN(ret);
2660 }
2661
2662 static int echo_client_brw_ioctl(const struct lu_env *env, int rw,
2663                                  struct obd_export *exp,
2664                                  struct obd_ioctl_data *data,
2665                                  struct obd_trans_info *dummy_oti)
2666 {
2667         struct obd_device *obd = class_exp2obd(exp);
2668         struct echo_device *ed = obd2echo_dev(obd);
2669         struct echo_client_obd *ec = ed->ed_ec;
2670         struct obdo *oa = &data->ioc_obdo1;
2671         struct echo_object *eco;
2672         int rc;
2673         int async = 1;
2674         long test_mode;
2675         ENTRY;
2676
2677         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2678
2679         rc = echo_get_object(&eco, ed, oa);
2680         if (rc)
2681                 RETURN(rc);
2682
2683         oa->o_valid &= ~OBD_MD_FLHANDLE;
2684
2685         /* OFD/obdfilter works only via prep/commit */
2686         test_mode = (long)data->ioc_pbuf1;
2687         if (test_mode == 1)
2688                 async = 0;
2689
2690         if (ed->ed_next == NULL && test_mode != 3) {
2691                 test_mode = 3;
2692                 data->ioc_plen1 = data->ioc_count;
2693         }
2694
2695         /* Truncate batch size to maximum */
2696         if (data->ioc_plen1 > PTLRPC_MAX_BRW_SIZE)
2697                 data->ioc_plen1 = PTLRPC_MAX_BRW_SIZE;
2698
2699         switch (test_mode) {
2700         case 1:
2701                 /* fall through */
2702         case 2:
2703                 rc = echo_client_kbrw(ed, rw, oa,
2704                                       eco, data->ioc_offset,
2705                                       data->ioc_count, async, dummy_oti);
2706                 break;
2707         case 3:
2708                 rc = echo_client_prep_commit(env, ec->ec_exp, rw, oa,
2709                                              eco, data->ioc_offset,
2710                                              data->ioc_count, data->ioc_plen1,
2711                                              dummy_oti, async);
2712                 break;
2713         default:
2714                 rc = -EINVAL;
2715         }
2716         echo_put_object(eco);
2717         RETURN(rc);
2718 }
2719
2720 static int
2721 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
2722                     int mode, obd_off offset, obd_size nob)
2723 {
2724         struct echo_device     *ed = obd2echo_dev(exp->exp_obd);
2725         struct lustre_handle   *ulh = &oa->o_handle;
2726         struct echo_object     *eco;
2727         obd_off                 end;
2728         int                     rc;
2729         ENTRY;
2730
2731         if (ed->ed_next == NULL)
2732                 RETURN(-EOPNOTSUPP);
2733
2734         if (!(mode == LCK_PR || mode == LCK_PW))
2735                 RETURN(-EINVAL);
2736
2737         if ((offset & (~CFS_PAGE_MASK)) != 0 ||
2738             (nob & (~CFS_PAGE_MASK)) != 0)
2739                 RETURN(-EINVAL);
2740
2741         rc = echo_get_object (&eco, ed, oa);
2742         if (rc != 0)
2743                 RETURN(rc);
2744
2745         end = (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
2746         rc = cl_echo_enqueue(eco, offset, end, mode, &ulh->cookie);
2747         if (rc == 0) {
2748                 oa->o_valid |= OBD_MD_FLHANDLE;
2749                 CDEBUG(D_INFO, "Cookie is "LPX64"\n", ulh->cookie);
2750         }
2751         echo_put_object(eco);
2752         RETURN(rc);
2753 }
2754
2755 static int
2756 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
2757 {
2758         struct echo_device *ed     = obd2echo_dev(exp->exp_obd);
2759         __u64               cookie = oa->o_handle.cookie;
2760
2761         if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
2762                 return -EINVAL;
2763
2764         CDEBUG(D_INFO, "Cookie is "LPX64"\n", cookie);
2765         return cl_echo_cancel(ed, cookie);
2766 }
2767
2768 static int
2769 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2770                       void *karg, void *uarg)
2771 {
2772 #ifdef HAVE_SERVER_SUPPORT
2773         struct tgt_session_info *tsi;
2774 #endif
2775         struct obd_device      *obd = exp->exp_obd;
2776         struct echo_device     *ed = obd2echo_dev(obd);
2777         struct echo_client_obd *ec = ed->ed_ec;
2778         struct echo_object     *eco;
2779         struct obd_ioctl_data  *data = karg;
2780         struct obd_trans_info   dummy_oti;
2781         struct lu_env          *env;
2782         struct oti_req_ack_lock *ack_lock;
2783         struct obdo            *oa;
2784         struct lu_fid           fid;
2785         int                     rw = OBD_BRW_READ;
2786         int                     rc = 0;
2787         int                     i;
2788 #ifdef HAVE_SERVER_SUPPORT
2789         struct lu_context        echo_session;
2790 #endif
2791         ENTRY;
2792
2793         memset(&dummy_oti, 0, sizeof(dummy_oti));
2794
2795         oa = &data->ioc_obdo1;
2796         if (!(oa->o_valid & OBD_MD_FLGROUP)) {
2797                 oa->o_valid |= OBD_MD_FLGROUP;
2798                 ostid_set_seq_echo(&oa->o_oi);
2799         }
2800
2801         /* This FID is unpacked just for validation at this point */
2802         rc = ostid_to_fid(&fid, &oa->o_oi, 0);
2803         if (rc < 0)
2804                 RETURN(rc);
2805
2806         OBD_ALLOC_PTR(env);
2807         if (env == NULL)
2808                 RETURN(-ENOMEM);
2809
2810         rc = lu_env_init(env, LCT_DT_THREAD);
2811         if (rc)
2812                 GOTO(out_alloc, rc = -ENOMEM);
2813
2814 #ifdef HAVE_SERVER_SUPPORT
2815         env->le_ses = &echo_session;
2816         rc = lu_context_init(env->le_ses, LCT_SERVER_SESSION | LCT_NOREF);
2817         if (unlikely(rc < 0))
2818                 GOTO(out_env, rc);
2819         lu_context_enter(env->le_ses);
2820
2821         tsi = tgt_ses_info(env);
2822         tsi->tsi_exp = ec->ec_exp;
2823         tsi->tsi_jobid = NULL;
2824 #endif
2825         switch (cmd) {
2826         case OBD_IOC_CREATE:                    /* may create echo object */
2827                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2828                         GOTO (out, rc = -EPERM);
2829
2830                 rc = echo_create_object(env, ed, 1, oa, data->ioc_pbuf1,
2831                                         data->ioc_plen1, &dummy_oti);
2832                 GOTO(out, rc);
2833
2834 #ifdef HAVE_SERVER_SUPPORT
2835         case OBD_IOC_ECHO_MD: {
2836                 int count;
2837                 int cmd;
2838                 char *dir = NULL;
2839                 int dirlen;
2840                 __u64 id;
2841
2842                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2843                         GOTO(out, rc = -EPERM);
2844
2845                 count = data->ioc_count;
2846                 cmd = data->ioc_command;
2847
2848                 id = data->ioc_obdo2.o_oi.oi.oi_id;
2849                 dirlen = data->ioc_plen1;
2850                 OBD_ALLOC(dir, dirlen + 1);
2851                 if (dir == NULL)
2852                         GOTO(out, rc = -ENOMEM);
2853
2854                 if (copy_from_user(dir, data->ioc_pbuf1, dirlen)) {
2855                         OBD_FREE(dir, data->ioc_plen1 + 1);
2856                         GOTO(out, rc = -EFAULT);
2857                 }
2858
2859                 rc = echo_md_handler(ed, cmd, dir, dirlen, id, count, data);
2860                 OBD_FREE(dir, dirlen + 1);
2861                 GOTO(out, rc);
2862         }
2863         case OBD_IOC_ECHO_ALLOC_SEQ: {
2864                 struct lu_env   *cl_env;
2865                 int              refcheck;
2866                 __u64            seq;
2867                 int              max_count;
2868
2869                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2870                         GOTO(out, rc = -EPERM);
2871
2872                 cl_env = cl_env_get(&refcheck);
2873                 if (IS_ERR(cl_env))
2874                         GOTO(out, rc = PTR_ERR(cl_env));
2875
2876                 rc = lu_env_refill_by_tags(cl_env, ECHO_MD_CTX_TAG,
2877                                             ECHO_MD_SES_TAG);
2878                 if (rc != 0) {
2879                         cl_env_put(cl_env, &refcheck);
2880                         GOTO(out, rc);
2881                 }
2882
2883                 rc = seq_client_get_seq(cl_env, ed->ed_cl_seq, &seq);
2884                 cl_env_put(cl_env, &refcheck);
2885                 if (rc < 0) {
2886                         CERROR("%s: Can not alloc seq: rc = %d\n",
2887                                obd->obd_name, rc);
2888                         GOTO(out, rc);
2889                 }
2890
2891                 if (copy_to_user(data->ioc_pbuf1, &seq, data->ioc_plen1))
2892                         return -EFAULT;
2893
2894                 max_count = LUSTRE_METADATA_SEQ_MAX_WIDTH;
2895                 if (copy_to_user(data->ioc_pbuf2, &max_count,
2896                                      data->ioc_plen2))
2897                         return -EFAULT;
2898                 GOTO(out, rc);
2899         }
2900 #endif /* HAVE_SERVER_SUPPORT */
2901         case OBD_IOC_DESTROY:
2902                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2903                         GOTO (out, rc = -EPERM);
2904
2905                 rc = echo_get_object(&eco, ed, oa);
2906                 if (rc == 0) {
2907                         rc = obd_destroy(env, ec->ec_exp, oa, eco->eo_lsm,
2908                                          &dummy_oti, NULL, NULL);
2909                         if (rc == 0)
2910                                 eco->eo_deleted = 1;
2911                         echo_put_object(eco);
2912                 }
2913                 GOTO(out, rc);
2914
2915         case OBD_IOC_GETATTR:
2916                 rc = echo_get_object(&eco, ed, oa);
2917                 if (rc == 0) {
2918                         struct obd_info oinfo = { { { 0 } } };
2919                         oinfo.oi_md = eco->eo_lsm;
2920                         oinfo.oi_oa = oa;
2921                         rc = obd_getattr(env, ec->ec_exp, &oinfo);
2922                         echo_put_object(eco);
2923                 }
2924                 GOTO(out, rc);
2925
2926         case OBD_IOC_SETATTR:
2927                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2928                         GOTO (out, rc = -EPERM);
2929
2930                 rc = echo_get_object(&eco, ed, oa);
2931                 if (rc == 0) {
2932                         struct obd_info oinfo = { { { 0 } } };
2933                         oinfo.oi_oa = oa;
2934                         oinfo.oi_md = eco->eo_lsm;
2935
2936                         rc = obd_setattr(env, ec->ec_exp, &oinfo, NULL);
2937                         echo_put_object(eco);
2938                 }
2939                 GOTO(out, rc);
2940
2941         case OBD_IOC_BRW_WRITE:
2942                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2943                         GOTO (out, rc = -EPERM);
2944
2945                 rw = OBD_BRW_WRITE;
2946                 /* fall through */
2947         case OBD_IOC_BRW_READ:
2948                 rc = echo_client_brw_ioctl(env, rw, exp, data, &dummy_oti);
2949                 GOTO(out, rc);
2950
2951         case ECHO_IOC_GET_STRIPE:
2952                 rc = echo_get_object(&eco, ed, oa);
2953                 if (rc == 0) {
2954                         rc = echo_copyout_lsm(eco->eo_lsm, data->ioc_pbuf1,
2955                                               data->ioc_plen1);
2956                         echo_put_object(eco);
2957                 }
2958                 GOTO(out, rc);
2959
2960         case ECHO_IOC_SET_STRIPE:
2961                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2962                         GOTO (out, rc = -EPERM);
2963
2964                 if (data->ioc_pbuf1 == NULL) {  /* unset */
2965                         rc = echo_get_object(&eco, ed, oa);
2966                         if (rc == 0) {
2967                                 eco->eo_deleted = 1;
2968                                 echo_put_object(eco);
2969                         }
2970                 } else {
2971                         rc = echo_create_object(env, ed, 0, oa,
2972                                                 data->ioc_pbuf1,
2973                                                 data->ioc_plen1, &dummy_oti);
2974                 }
2975                 GOTO (out, rc);
2976
2977         case ECHO_IOC_ENQUEUE:
2978                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2979                         GOTO (out, rc = -EPERM);
2980
2981                 rc = echo_client_enqueue(exp, oa,
2982                                          data->ioc_conn1, /* lock mode */
2983                                          data->ioc_offset,
2984                                          data->ioc_count);/*extent*/
2985                 GOTO (out, rc);
2986
2987         case ECHO_IOC_CANCEL:
2988                 rc = echo_client_cancel(exp, oa);
2989                 GOTO (out, rc);
2990
2991         default:
2992                 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
2993                 GOTO (out, rc = -ENOTTY);
2994         }
2995
2996         EXIT;
2997 out:
2998 #ifdef HAVE_SERVER_SUPPORT
2999         lu_context_exit(env->le_ses);
3000         lu_context_fini(env->le_ses);
3001 out_env:
3002 #endif
3003         lu_env_fini(env);
3004 out_alloc:
3005         OBD_FREE_PTR(env);
3006
3007         /* XXX this should be in a helper also called by target_send_reply */
3008         for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
3009              i++, ack_lock++) {
3010                 if (!ack_lock->mode)
3011                         break;
3012                 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
3013         }
3014
3015         return rc;
3016 }
3017
3018 static int echo_client_setup(const struct lu_env *env,
3019                              struct obd_device *obddev, struct lustre_cfg *lcfg)
3020 {
3021         struct echo_client_obd *ec = &obddev->u.echo_client;
3022         struct obd_device *tgt;
3023         struct obd_uuid echo_uuid = { "ECHO_UUID" };
3024         struct obd_connect_data *ocd = NULL;
3025         int rc;
3026         ENTRY;
3027
3028         if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
3029                 CERROR("requires a TARGET OBD name\n");
3030                 RETURN(-EINVAL);
3031         }
3032
3033         tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
3034         if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
3035                 CERROR("device not attached or not set up (%s)\n",
3036                        lustre_cfg_string(lcfg, 1));
3037                 RETURN(-EINVAL);
3038         }
3039
3040         spin_lock_init(&ec->ec_lock);
3041         CFS_INIT_LIST_HEAD (&ec->ec_objects);
3042         CFS_INIT_LIST_HEAD (&ec->ec_locks);
3043         ec->ec_unique = 0;
3044         ec->ec_nstripes = 0;
3045
3046         if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
3047 #ifdef HAVE_SERVER_SUPPORT
3048                 lu_context_tags_update(ECHO_MD_CTX_TAG);
3049                 lu_session_tags_update(ECHO_MD_SES_TAG);
3050 #else
3051                 CERROR("Local operations are NOT supported on client side. "
3052                        "Only remote operations are supported. Metadata client "
3053                        "must be run on server side.\n");
3054 #endif
3055                 RETURN(0);
3056         }
3057
3058         OBD_ALLOC(ocd, sizeof(*ocd));
3059         if (ocd == NULL) {
3060                 CERROR("Can't alloc ocd connecting to %s\n",
3061                        lustre_cfg_string(lcfg, 1));
3062                 return -ENOMEM;
3063         }
3064
3065         ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
3066                                  OBD_CONNECT_BRW_SIZE |
3067                                  OBD_CONNECT_GRANT | OBD_CONNECT_FULL20 |
3068                                  OBD_CONNECT_64BITHASH | OBD_CONNECT_LVB_TYPE |
3069                                  OBD_CONNECT_FID;
3070         ocd->ocd_brw_size = DT_MAX_BRW_SIZE;
3071         ocd->ocd_version = LUSTRE_VERSION_CODE;
3072         ocd->ocd_group = FID_SEQ_ECHO;
3073
3074         rc = obd_connect(env, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
3075         if (rc == 0) {
3076                 /* Turn off pinger because it connects to tgt obd directly. */
3077                 spin_lock(&tgt->obd_dev_lock);
3078                 cfs_list_del_init(&ec->ec_exp->exp_obd_chain_timed);
3079                 spin_unlock(&tgt->obd_dev_lock);
3080         }
3081
3082         OBD_FREE(ocd, sizeof(*ocd));
3083
3084         if (rc != 0) {
3085                 CERROR("fail to connect to device %s\n",
3086                        lustre_cfg_string(lcfg, 1));
3087                 return (rc);
3088         }
3089
3090         RETURN(rc);
3091 }
3092
3093 static int echo_client_cleanup(struct obd_device *obddev)
3094 {
3095         struct echo_device *ed = obd2echo_dev(obddev);
3096         struct echo_client_obd *ec = &obddev->u.echo_client;
3097         int rc;
3098         ENTRY;
3099
3100         /*Do nothing for Metadata echo client*/
3101         if (ed == NULL )
3102                 RETURN(0);
3103
3104         if (ed->ed_next_ismd) {
3105 #ifdef HAVE_SERVER_SUPPORT
3106                 lu_context_tags_clear(ECHO_MD_CTX_TAG);
3107                 lu_session_tags_clear(ECHO_MD_SES_TAG);
3108 #else
3109                 CERROR("This is client-side only module, does not support "
3110                         "metadata echo client.\n");
3111 #endif
3112                 RETURN(0);
3113         }
3114
3115         if (!cfs_list_empty(&obddev->obd_exports)) {
3116                 CERROR("still has clients!\n");
3117                 RETURN(-EBUSY);
3118         }
3119
3120         LASSERT(atomic_read(&ec->ec_exp->exp_refcount) > 0);
3121         rc = obd_disconnect(ec->ec_exp);
3122         if (rc != 0)
3123                 CERROR("fail to disconnect device: %d\n", rc);
3124
3125         RETURN(rc);
3126 }
3127
3128 static int echo_client_connect(const struct lu_env *env,
3129                                struct obd_export **exp,
3130                                struct obd_device *src, struct obd_uuid *cluuid,
3131                                struct obd_connect_data *data, void *localdata)
3132 {
3133         int                rc;
3134         struct lustre_handle conn = { 0 };
3135
3136         ENTRY;
3137         rc = class_connect(&conn, src, cluuid);
3138         if (rc == 0) {
3139                 *exp = class_conn2export(&conn);
3140         }
3141
3142         RETURN (rc);
3143 }
3144
3145 static int echo_client_disconnect(struct obd_export *exp)
3146 {
3147         int                     rc;
3148         ENTRY;
3149
3150         if (exp == NULL)
3151                 GOTO(out, rc = -EINVAL);
3152
3153         rc = class_disconnect(exp);
3154         GOTO(out, rc);
3155  out:
3156         return rc;
3157 }
3158
3159 static struct obd_ops echo_client_obd_ops = {
3160         .o_owner       = THIS_MODULE,
3161         .o_iocontrol   = echo_client_iocontrol,
3162         .o_connect     = echo_client_connect,
3163         .o_disconnect  = echo_client_disconnect
3164 };
3165
3166 int echo_client_init(void)
3167 {
3168         int rc;
3169
3170         rc = lu_kmem_init(echo_caches);
3171         if (rc == 0) {
3172                 rc = class_register_type(&echo_client_obd_ops, NULL, NULL,
3173 #ifndef HAVE_ONLY_PROCFS_SEQ
3174                                         NULL,
3175 #endif
3176                                         LUSTRE_ECHO_CLIENT_NAME,
3177                                         &echo_device_type);
3178                 if (rc)
3179                         lu_kmem_fini(echo_caches);
3180         }
3181         return rc;
3182 }
3183
3184 void echo_client_exit(void)
3185 {
3186         class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
3187         lu_kmem_fini(echo_caches);
3188 }
3189
3190 #ifdef __KERNEL__
3191 static int __init obdecho_init(void)
3192 {
3193         int rc;
3194
3195         ENTRY;
3196         LCONSOLE_INFO("Echo OBD driver; http://www.lustre.org/\n");
3197
3198         LASSERT(PAGE_CACHE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
3199
3200 # ifdef HAVE_SERVER_SUPPORT
3201         rc = echo_persistent_pages_init();
3202         if (rc != 0)
3203                 goto failed_0;
3204
3205         rc = class_register_type(&echo_obd_ops, NULL, NULL,
3206 #ifndef HAVE_ONLY_PROCFS_SEQ
3207                                 NULL,
3208 #endif
3209                                 LUSTRE_ECHO_NAME, NULL);
3210         if (rc != 0)
3211                 goto failed_1;
3212 # endif
3213
3214         rc = echo_client_init();
3215
3216 # ifdef HAVE_SERVER_SUPPORT
3217         if (rc == 0)
3218                 RETURN(0);
3219
3220         class_unregister_type(LUSTRE_ECHO_NAME);
3221 failed_1:
3222         echo_persistent_pages_fini();
3223 failed_0:
3224 # endif
3225         RETURN(rc);
3226 }
3227
3228 static void /*__exit*/ obdecho_exit(void)
3229 {
3230         echo_client_exit();
3231
3232 # ifdef HAVE_SERVER_SUPPORT
3233         class_unregister_type(LUSTRE_ECHO_NAME);
3234         echo_persistent_pages_fini();
3235 # endif
3236 }
3237
3238 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3239 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
3240 MODULE_LICENSE("GPL");
3241
3242 cfs_module(obdecho, LUSTRE_VERSION_STRING, obdecho_init, obdecho_exit);
3243 #endif /* __KERNEL__ */
3244
3245 /** @} echo_client */