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