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