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