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