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