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