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