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