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