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