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