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