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