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