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