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