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