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