Whamcloud - gitweb
LU-506 kernel: remove BKL
[fs/lustre-release.git] / lustre / obdecho / echo_client.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Whamcloud, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_ECHO
38 #ifdef __KERNEL__
39 #include <libcfs/libcfs.h>
40 #else
41 #include <liblustre.h>
42 #endif
43
44 #include <obd.h>
45 #include <obd_support.h>
46 #include <obd_class.h>
47 #include <lustre_debug.h>
48 #include <lprocfs_status.h>
49 #include <cl_object.h>
50 #include <lustre_fid.h>
51 #include <lustre_acl.h>
52 #include <lustre_net.h>
53
54 #include "echo_internal.h"
55
56 /** \defgroup echo_client Echo Client
57  * @{
58  */
59
60 struct echo_device {
61         struct cl_device        ed_cl;
62         struct echo_client_obd *ed_ec;
63
64         struct cl_site          ed_site_myself;
65         struct cl_site         *ed_site;
66         struct lu_device       *ed_next;
67         int                     ed_next_islov;
68         int                     ed_next_ismd;
69         struct lu_client_seq   *ed_cl_seq;
70 };
71
72 struct echo_object {
73         struct cl_object        eo_cl;
74         struct cl_object_header eo_hdr;
75
76         struct echo_device     *eo_dev;
77         cfs_list_t              eo_obj_chain;
78         struct lov_stripe_md   *eo_lsm;
79         cfs_atomic_t            eo_npages;
80         int                     eo_deleted;
81 };
82
83 struct echo_object_conf {
84         struct cl_object_conf  eoc_cl;
85         struct lov_stripe_md **eoc_md;
86 };
87
88 struct echo_page {
89         struct cl_page_slice   ep_cl;
90         cfs_mutex_t            ep_lock;
91         cfs_page_t            *ep_vmpage;
92 };
93
94 struct echo_lock {
95         struct cl_lock_slice   el_cl;
96         cfs_list_t             el_chain;
97         struct echo_object    *el_object;
98         __u64                  el_cookie;
99         cfs_atomic_t           el_refcount;
100 };
101
102 struct echo_io {
103         struct cl_io_slice     ei_cl;
104 };
105
106 #if 0
107 struct echo_req {
108         struct cl_req_slice er_cl;
109 };
110 #endif
111
112 static int echo_client_setup(const struct lu_env *env,
113                              struct obd_device *obddev,
114                              struct lustre_cfg *lcfg);
115 static int echo_client_cleanup(struct obd_device *obddev);
116
117
118 /** \defgroup echo_helpers Helper functions
119  * @{
120  */
121 static inline struct echo_device *cl2echo_dev(const struct cl_device *dev)
122 {
123         return container_of0(dev, struct echo_device, ed_cl);
124 }
125
126 static inline struct cl_device *echo_dev2cl(struct echo_device *d)
127 {
128         return &d->ed_cl;
129 }
130
131 static inline struct echo_device *obd2echo_dev(const struct obd_device *obd)
132 {
133         return cl2echo_dev(lu2cl_dev(obd->obd_lu_dev));
134 }
135
136 static inline struct cl_object *echo_obj2cl(struct echo_object *eco)
137 {
138         return &eco->eo_cl;
139 }
140
141 static inline struct echo_object *cl2echo_obj(const struct cl_object *o)
142 {
143         return container_of(o, struct echo_object, eo_cl);
144 }
145
146 static inline struct echo_page *cl2echo_page(const struct cl_page_slice *s)
147 {
148         return container_of(s, struct echo_page, ep_cl);
149 }
150
151 static inline struct echo_lock *cl2echo_lock(const struct cl_lock_slice *s)
152 {
153         return container_of(s, struct echo_lock, el_cl);
154 }
155
156 static inline struct cl_lock *echo_lock2cl(const struct echo_lock *ecl)
157 {
158         return ecl->el_cl.cls_lock;
159 }
160
161 static struct lu_context_key echo_thread_key;
162 static inline struct echo_thread_info *echo_env_info(const struct lu_env *env)
163 {
164         struct echo_thread_info *info;
165         info = lu_context_key_get(&env->le_ctx, &echo_thread_key);
166         LASSERT(info != NULL);
167         return info;
168 }
169
170 static inline
171 struct echo_object_conf *cl2echo_conf(const struct cl_object_conf *c)
172 {
173         return container_of(c, struct echo_object_conf, eoc_cl);
174 }
175
176 static inline void lsm2fid(struct lov_stripe_md *lsm, struct lu_fid *fid)
177 {
178         fid_zero(fid);
179         fid->f_seq = FID_SEQ_ECHO;
180         /* truncated to 32 bits by assignment */
181         fid->f_oid = lsm->lsm_object_id;
182         fid->f_ver = lsm->lsm_object_id >> 32;
183 }
184 /** @} echo_helpers */
185
186 static struct echo_object *cl_echo_object_find(struct echo_device *d,
187                                                struct lov_stripe_md **lsm);
188 static int cl_echo_object_put(struct echo_object *eco);
189 static int cl_echo_enqueue   (struct echo_object *eco, obd_off start,
190                               obd_off end, int mode, __u64 *cookie);
191 static int cl_echo_cancel    (struct echo_device *d, __u64 cookie);
192 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
193                               cfs_page_t **pages, int npages, int async);
194
195 static struct echo_thread_info *echo_env_info(const struct lu_env *env);
196
197 struct echo_thread_info {
198         struct echo_object_conf eti_conf;
199         struct lustre_md        eti_md;
200
201         struct cl_2queue        eti_queue;
202         struct cl_io            eti_io;
203         struct cl_lock_descr    eti_descr;
204         struct lu_fid           eti_fid;
205         struct md_op_spec       eti_spec;
206         struct lov_mds_md_v3    eti_lmm;
207         struct lov_user_md_v3   eti_lum;
208         struct md_attr          eti_ma;
209         struct lu_name          eti_lname;
210         char                    eti_name[20];
211         struct lu_buf           eti_buf;
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 static 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 {
1465         struct md_device *md = lu2md_dev(ld);
1466         int lmm_size, cookie_size, rc;
1467         ENTRY;
1468
1469         md = lu2md_dev(ld);
1470         rc = md->md_ops->mdo_maxsize_get(env, md,
1471                                          &lmm_size, &cookie_size);
1472         if (rc)
1473                 RETURN(rc);
1474
1475         ma->ma_lmm_size = lmm_size;
1476         if (lmm_size > 0) {
1477                 OBD_ALLOC(ma->ma_lmm, lmm_size);
1478                 if (ma->ma_lmm == NULL) {
1479                         ma->ma_lmm_size = 0;
1480                         RETURN(-ENOMEM);
1481                 }
1482         }
1483
1484         ma->ma_cookie_size = cookie_size;
1485         if (cookie_size > 0) {
1486                 OBD_ALLOC(ma->ma_cookie, cookie_size);
1487                 if (ma->ma_cookie == NULL) {
1488                         ma->ma_cookie_size = 0;
1489                         RETURN(-ENOMEM);
1490                 }
1491         }
1492
1493         RETURN(0);
1494 }
1495
1496 static int echo_create_md_object(const struct lu_env *env,
1497                                  struct echo_device *ed,
1498                                  struct lu_object *ec_parent,
1499                                  struct lu_fid *fid,
1500                                  char *name, int namelen,
1501                                  __u64 id, __u32 mode, int count,
1502                                  int stripe_count, int stripe_offset)
1503 {
1504         struct lu_object        *parent;
1505         struct echo_thread_info *info = echo_env_info(env);
1506         struct lu_name          *lname = &info->eti_lname;
1507         struct md_op_spec       *spec = &info->eti_spec;
1508         struct md_attr          *ma = &info->eti_ma;
1509         struct lu_device        *ld = ed->ed_next;
1510         int                      rc = 0;
1511         int                      i;
1512
1513         parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1514         if (ec_parent == NULL) {
1515                 lu_object_put(env, ec_parent);
1516                 RETURN(PTR_ERR(parent));
1517         }
1518
1519         memset(ma, 0, sizeof(*ma));
1520         memset(spec, 0, sizeof(*spec));
1521         if (stripe_count != 0) {
1522                 spec->sp_cr_flags |= FMODE_WRITE;
1523                 rc = echo_set_lmm_size(env, ld, ma);
1524                 if (rc)
1525                         GOTO(out_free, rc);
1526                 if (stripe_count != -1) {
1527                         struct lov_user_md_v3 *lum = &info->eti_lum;
1528                         lum->lmm_magic = LOV_USER_MAGIC_V3;
1529                         lum->lmm_stripe_count = stripe_count;
1530                         lum->lmm_stripe_offset = stripe_offset;
1531                         lum->lmm_pattern = 0;
1532                         spec->u.sp_ea.eadata = lum;
1533                         spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1534                 }
1535         }
1536
1537         ma->ma_attr.la_mode = mode;
1538         ma->ma_attr.la_valid = LA_CTIME;
1539         ma->ma_attr.la_ctime = cfs_time_current_64();
1540
1541         if (name != NULL) {
1542                 lname->ln_name = name;
1543                 lname->ln_namelen = namelen;
1544                 /* If name is specified, only create one object by name */
1545                 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1546                                              spec, ma);
1547                 GOTO(out_free, rc);
1548         }
1549
1550         /* Create multiple object sequenced by id */
1551         for (i = 0; i < count; i++) {
1552                 char *tmp_name = info->eti_name;
1553
1554                 echo_md_build_name(lname, tmp_name, id);
1555
1556                 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1557                                              spec, ma);
1558                 if (rc) {
1559                         CERROR("Can not create child %s: rc = %d\n", tmp_name,
1560                                 rc);
1561                         break;
1562                 }
1563                 id++;
1564                 fid->f_oid++;
1565         }
1566
1567 out_free:
1568         if (ma->ma_lmm_size > 0 && ma->ma_lmm != NULL)
1569                 OBD_FREE(ma->ma_lmm, ma->ma_lmm_size);
1570         if (ma->ma_cookie_size > 0 && ma->ma_cookie != NULL)
1571                 OBD_FREE(ma->ma_cookie, ma->ma_cookie_size);
1572
1573         return rc;
1574 }
1575
1576 static struct lu_object *echo_md_lookup(const struct lu_env *env,
1577                                         struct echo_device *ed,
1578                                         struct md_object *parent,
1579                                         struct lu_name *lname)
1580 {
1581         struct echo_thread_info *info = echo_env_info(env);
1582         struct lu_fid           *fid = &info->eti_fid;
1583         struct lu_object        *child;
1584         int    rc;
1585         ENTRY;
1586
1587         CDEBUG(D_INFO, "lookup %s in parent "DFID" %p\n", lname->ln_name,
1588                PFID(fid), parent);
1589         rc = mdo_lookup(env, parent, lname, fid, NULL);
1590         if (rc) {
1591                 CERROR("lookup %s: rc = %d\n", lname->ln_name, rc);
1592                 RETURN(ERR_PTR(rc));
1593         }
1594
1595         child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1596
1597         RETURN(child);
1598 }
1599
1600 static int echo_setattr_object(const struct lu_env *env,
1601                                struct echo_device *ed,
1602                                struct lu_object *ec_parent,
1603                                __u64 id, int count)
1604 {
1605         struct lu_object        *parent;
1606         struct echo_thread_info *info = echo_env_info(env);
1607         struct lu_name          *lname = &info->eti_lname;
1608         char                    *name = info->eti_name;
1609         struct lu_device        *ld = ed->ed_next;
1610         struct lu_buf           *buf = &info->eti_buf;
1611         int                      rc = 0;
1612         int                      i;
1613
1614         parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1615         if (ec_parent == NULL) {
1616                 lu_object_put(env, ec_parent);
1617                 return PTR_ERR(parent);
1618         }
1619
1620         buf->lb_buf = info->eti_xattr_buf;
1621         buf->lb_len = sizeof(info->eti_xattr_buf);
1622         for (i = 0; i < count; i++) {
1623                 struct lu_object *ec_child, *child;
1624
1625                 echo_md_build_name(lname, name, id);
1626
1627                 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1628                 if (IS_ERR(ec_child)) {
1629                         CERROR("Can't find child %s: rc = %ld\n",
1630                                 lname->ln_name, PTR_ERR(ec_child));
1631                         RETURN(PTR_ERR(ec_child));
1632                 }
1633
1634                 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1635                 if (child == NULL) {
1636                         CERROR("Can not locate the child %s\n", lname->ln_name);
1637                         lu_object_put(env, ec_child);
1638                         rc = -EINVAL;
1639                         break;
1640                 }
1641
1642                 CDEBUG(D_RPCTRACE, "Start setattr object "DFID"\n",
1643                        PFID(lu_object_fid(child)));
1644
1645                 sprintf(name, "%s.test1", XATTR_USER_PREFIX);
1646                 rc = mo_xattr_set(env, lu2md(child), buf, name,
1647                                   LU_XATTR_CREATE);
1648                 if (rc) {
1649                         CERROR("Can not setattr child "DFID": rc = %d\n",
1650                                 PFID(lu_object_fid(child)), rc);
1651                         lu_object_put(env, ec_child);
1652                         break;
1653                 }
1654                 CDEBUG(D_RPCTRACE, "End setattr object "DFID"\n",
1655                        PFID(lu_object_fid(child)));
1656                 id++;
1657                 lu_object_put(env, ec_child);
1658         }
1659         return rc;
1660 }
1661
1662 static int echo_getattr_object(const struct lu_env *env,
1663                                struct echo_device *ed,
1664                                struct lu_object *ec_parent,
1665                                __u64 id, int count)
1666 {
1667         struct lu_object        *parent;
1668         struct echo_thread_info *info = echo_env_info(env);
1669         struct lu_name          *lname = &info->eti_lname;
1670         char                    *name = info->eti_name;
1671         struct md_attr          *ma = &info->eti_ma;
1672         struct lu_device        *ld = ed->ed_next;
1673         int                      rc = 0;
1674         int                      i;
1675
1676         parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1677         if (ec_parent == NULL) {
1678                 lu_object_put(env, ec_parent);
1679                 return PTR_ERR(parent);
1680         }
1681
1682         memset(ma, 0, sizeof(*ma));
1683         rc = echo_set_lmm_size(env, ld, ma);
1684         if (rc)
1685                 GOTO(out_free, rc);
1686
1687         ma->ma_need |= MA_INODE | MA_LOV | MA_PFID | MA_HSM | MA_ACL_DEF;
1688         ma->ma_acl = info->eti_xattr_buf;
1689         ma->ma_acl_size = sizeof(info->eti_xattr_buf);
1690
1691         for (i = 0; i < count; i++) {
1692                 struct lu_object *ec_child, *child;
1693
1694                 ma->ma_valid = 0;
1695                 echo_md_build_name(lname, name, id);
1696
1697                 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1698                 if (IS_ERR(ec_child)) {
1699                         CERROR("Can't find child %s: rc = %ld\n",
1700                                lname->ln_name, PTR_ERR(ec_child));
1701                         RETURN(PTR_ERR(ec_child));
1702                 }
1703
1704                 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1705                 if (child == NULL) {
1706                         CERROR("Can not locate the child %s\n", lname->ln_name);
1707                         lu_object_put(env, ec_child);
1708                         GOTO(out_free, rc = -EINVAL);
1709                 }
1710
1711                 CDEBUG(D_RPCTRACE, "Start getattr object "DFID"\n",
1712                        PFID(lu_object_fid(child)));
1713                 rc = mo_attr_get(env, lu2md(child), ma);
1714                 if (rc) {
1715                         CERROR("Can not getattr child "DFID": rc = %d\n",
1716                                 PFID(lu_object_fid(child)), rc);
1717                         lu_object_put(env, ec_child);
1718                         break;
1719                 }
1720                 CDEBUG(D_RPCTRACE, "End getattr object "DFID"\n",
1721                        PFID(lu_object_fid(child)));
1722                 id++;
1723                 lu_object_put(env, ec_child);
1724         }
1725
1726 out_free:
1727         if (ma->ma_lmm_size > 0 && ma->ma_lmm != NULL)
1728                 OBD_FREE(ma->ma_lmm, ma->ma_lmm_size);
1729         if (ma->ma_cookie_size > 0 && ma->ma_cookie != NULL)
1730                 OBD_FREE(ma->ma_cookie, ma->ma_cookie_size);
1731         return rc;
1732 }
1733
1734 static int echo_lookup_object(const struct lu_env *env,
1735                               struct echo_device *ed,
1736                               struct lu_object *ec_parent,
1737                               __u64 id, int count)
1738 {
1739         struct lu_object        *parent;
1740         struct echo_thread_info *info = echo_env_info(env);
1741         struct lu_name          *lname = &info->eti_lname;
1742         char                    *name = info->eti_name;
1743         struct lu_fid           *fid = &info->eti_fid;
1744         struct lu_device        *ld = ed->ed_next;
1745         int                      rc = 0;
1746         int                      i;
1747
1748         parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1749         if (ec_parent == NULL) {
1750                 lu_object_put(env, ec_parent);
1751                 return PTR_ERR(parent);
1752         }
1753
1754         /*prepare the requests*/
1755         for (i = 0; i < count; i++) {
1756                 echo_md_build_name(lname, name, id);
1757
1758                 CDEBUG(D_RPCTRACE, "Start lookup object "DFID" %s %p\n",
1759                        PFID(lu_object_fid(parent)), lname->ln_name, parent);
1760
1761                 rc = mdo_lookup(env, lu2md(parent), lname, fid, NULL);
1762                 if (rc) {
1763                         CERROR("Can not lookup child %s: rc = %d\n", name, rc);
1764                         break;
1765                 }
1766                 CDEBUG(D_RPCTRACE, "End lookup object "DFID" %s %p\n",
1767                        PFID(lu_object_fid(parent)), lname->ln_name, parent);
1768
1769                 id++;
1770         }
1771         return rc;
1772 }
1773
1774 static int echo_md_destroy_internal(const struct lu_env *env,
1775                                     struct echo_device *ed,
1776                                     struct md_object *parent,
1777                                     struct lu_name *lname,
1778                                     struct md_attr *ma)
1779 {
1780         struct lu_device   *ld = ed->ed_next;
1781         struct lu_object   *ec_child;
1782         struct lu_object   *child;
1783         int                 rc;
1784
1785         ec_child = echo_md_lookup(env, ed, parent, lname);
1786         if (IS_ERR(ec_child)) {
1787                 CERROR("Can't find child %s: rc = %ld\n", lname->ln_name,
1788                         PTR_ERR(ec_child));
1789                 RETURN(PTR_ERR(ec_child));
1790         }
1791
1792         child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1793         if (child == NULL) {
1794                 CERROR("Can not locate the child %s\n", lname->ln_name);
1795                 GOTO(out_put, rc = -EINVAL);
1796         }
1797
1798         CDEBUG(D_RPCTRACE, "Start destroy object "DFID" %s %p\n",
1799                PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1800
1801 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2,3,50,0)
1802         /* After 2.4, MDT will send destroy RPC to OST directly, so no need
1803          * this flag */
1804         ma->ma_valid |= MA_FLAGS;
1805         ma->ma_attr_flags |= MDS_UNLINK_DESTROY;
1806 #else
1807 #warning "Please remove this after 2.4 (LOD/OSP)"
1808 #endif
1809         rc = mdo_unlink(env, parent, lu2md(child), lname, ma);
1810         if (rc) {
1811                 CERROR("Can not unlink child %s: rc = %d\n",
1812                         lname->ln_name, rc);
1813                 GOTO(out_put, rc);
1814         }
1815         CDEBUG(D_RPCTRACE, "End destroy object "DFID" %s %p\n",
1816                PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1817 out_put:
1818         lu_object_put(env, ec_child);
1819         return rc;
1820 }
1821
1822 static int echo_destroy_object(const struct lu_env *env,
1823                                struct echo_device *ed,
1824                                struct lu_object *ec_parent,
1825                                char *name, int namelen,
1826                                __u64 id, __u32 mode,
1827                                int count)
1828 {
1829         struct echo_thread_info *info = echo_env_info(env);
1830         struct lu_name          *lname = &info->eti_lname;
1831         struct md_attr          *ma = &info->eti_ma;
1832         struct lu_device        *ld = ed->ed_next;
1833         struct lu_object        *parent;
1834         int                      rc = 0;
1835         int                      i;
1836         ENTRY;
1837
1838         parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1839         if (parent == NULL)
1840                 RETURN(-EINVAL);
1841
1842         memset(ma, 0, sizeof(*ma));
1843         ma->ma_attr.la_mode = mode;
1844         ma->ma_attr.la_valid = LA_CTIME;
1845         ma->ma_attr.la_ctime = cfs_time_current_64();
1846         ma->ma_need = MA_INODE;
1847         ma->ma_valid = 0;
1848
1849         rc = echo_set_lmm_size(env, ld, ma);
1850         if (rc)
1851                 GOTO(out_free, rc);
1852         if (name != NULL) {
1853                 lname->ln_name = name;
1854                 lname->ln_namelen = namelen;
1855                 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
1856                                               ma);
1857                 GOTO(out_free, rc);
1858         }
1859
1860         /*prepare the requests*/
1861         for (i = 0; i < count; i++) {
1862                 char *tmp_name = info->eti_name;
1863
1864                 ma->ma_need |= MA_LOV;
1865                 ma->ma_valid = 0;
1866                 echo_md_build_name(lname, tmp_name, id);
1867
1868                 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
1869                                               ma);
1870                 if (rc) {
1871                         CERROR("Can not unlink child %s: rc = %d\n", name, rc);
1872                         break;
1873                 }
1874                 id++;
1875         }
1876
1877 out_free:
1878         if (ma->ma_lmm_size > 0 && ma->ma_lmm != NULL)
1879                 OBD_FREE(ma->ma_lmm, ma->ma_lmm_size);
1880         if (ma->ma_cookie_size > 0 && ma->ma_cookie != NULL)
1881                 OBD_FREE(ma->ma_cookie, ma->ma_cookie_size);
1882         RETURN(rc);
1883 }
1884
1885 static struct lu_object *echo_resolve_path(const struct lu_env *env,
1886                                            struct echo_device *ed, char *path,
1887                                            int path_len)
1888 {
1889         struct lu_device        *ld = ed->ed_next;
1890         struct md_device        *md = lu2md_dev(ld);
1891         struct echo_thread_info *info = echo_env_info(env);
1892         struct lu_fid           *fid = &info->eti_fid;
1893         struct lu_name          *lname = &info->eti_lname;
1894         struct lu_object        *parent = NULL;
1895         struct lu_object        *child = NULL;
1896         int rc = 0;
1897         ENTRY;
1898
1899         /*Only support MDD layer right now*/
1900         rc = md->md_ops->mdo_root_get(env, md, fid);
1901         if (rc) {
1902                 CERROR("get root error: rc = %d\n", rc);
1903                 RETURN(ERR_PTR(rc));
1904         }
1905
1906         parent = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1907         if (IS_ERR(parent)) {
1908                 CERROR("Can not find the parent "DFID": rc = %ld\n",
1909                         PFID(fid), PTR_ERR(parent));
1910                 RETURN(parent);
1911         }
1912
1913         while (1) {
1914                 struct lu_object *ld_parent;
1915                 char *e;
1916
1917                 e = strsep(&path, "/");
1918                 if (e == NULL)
1919                         break;
1920
1921                 if (e[0] == 0) {
1922                         if (!path || path[0] == '\0')
1923                                 break;
1924                         continue;
1925                 }
1926
1927                 lname->ln_name = e;
1928                 lname->ln_namelen = strlen(e);
1929
1930                 ld_parent = lu_object_locate(parent->lo_header, ld->ld_type);
1931                 if (ld_parent == NULL) {
1932                         lu_object_put(env, parent);
1933                         rc = -EINVAL;
1934                         break;
1935                 }
1936
1937                 child = echo_md_lookup(env, ed, lu2md(ld_parent), lname);
1938                 lu_object_put(env, parent);
1939                 if (IS_ERR(child)) {
1940                         rc = (int)PTR_ERR(child);
1941                         CERROR("lookup %s under parent "DFID": rc = %d\n",
1942                                 lname->ln_name, PFID(lu_object_fid(ld_parent)),
1943                                 rc);
1944                         break;
1945                 }
1946                 parent = child;
1947         }
1948         if (rc)
1949                 RETURN(ERR_PTR(rc));
1950
1951         RETURN(parent);
1952 }
1953
1954 #define ECHO_MD_CTX_TAG (LCT_REMEMBER | LCT_NOREF | LCT_MD_THREAD)
1955 #define ECHO_MD_SES_TAG (LCT_SESSION | LCT_REMEMBER | LCT_NOREF)
1956
1957 static int echo_md_handler(struct echo_device *ed, int command,
1958                            char *path, int path_len, int id, int count,
1959                            struct obd_ioctl_data *data)
1960 {
1961         struct lu_device      *ld = ed->ed_next;
1962         struct lu_env         *env;
1963         int                    refcheck;
1964         struct lu_object      *parent;
1965         char                  *name = NULL;
1966         int                    namelen = data->ioc_plen2;
1967         int                    rc = 0;
1968         ENTRY;
1969
1970         if (ld == NULL) {
1971                 CERROR("MD echo client is not being initialized properly\n");
1972                 RETURN(-EINVAL);
1973         }
1974
1975         if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
1976                 CERROR("Only support MDD layer right now!\n");
1977                 RETURN(-EINVAL);
1978         }
1979
1980         env = cl_env_get(&refcheck);
1981         if (IS_ERR(env))
1982                 RETURN(PTR_ERR(env));
1983
1984         rc = lu_env_refill_by_tags(env, ECHO_MD_CTX_TAG, ECHO_MD_SES_TAG);
1985         if (rc != 0) {
1986                 cl_env_put(env, &refcheck);
1987                 RETURN(rc);
1988         }
1989
1990         parent = echo_resolve_path(env, ed, path, path_len);
1991         if (IS_ERR(parent)) {
1992                 CERROR("Can not resolve the path %s: rc = %ld\n", path,
1993                         PTR_ERR(parent));
1994                 cl_env_put(env, &refcheck);
1995                 RETURN(PTR_ERR(parent));
1996         }
1997
1998         if (namelen > 0) {
1999                 OBD_ALLOC(name, namelen + 1);
2000                 if (name == NULL)
2001                         RETURN(-ENOMEM);
2002                 if (cfs_copy_from_user(name, data->ioc_pbuf2, namelen)) {
2003                         OBD_FREE(name, namelen + 1);
2004                         RETURN(-EFAULT);
2005                 }
2006         }
2007
2008         switch (command) {
2009         case ECHO_MD_CREATE:
2010         case ECHO_MD_MKDIR: {
2011                 struct echo_thread_info *info = echo_env_info(env);
2012                 __u32 mode = data->ioc_obdo2.o_mode;
2013                 struct lu_fid *fid = &info->eti_fid;
2014                 int stripe_count = (int)data->ioc_obdo2.o_misc;
2015                 int stripe_index = (int)data->ioc_obdo2.o_stripe_idx;
2016
2017                 fid->f_seq = data->ioc_obdo1.o_seq;
2018                 fid->f_oid = (__u32)data->ioc_obdo1.o_id;
2019                 fid->f_ver = 0;
2020                 rc = echo_create_md_object(env, ed, parent, fid, name, namelen,
2021                                            id, mode, count, stripe_count,
2022                                            stripe_index);
2023                 break;
2024         }
2025         case ECHO_MD_DESTROY:
2026         case ECHO_MD_RMDIR: {
2027                 __u32 mode = data->ioc_obdo2.o_mode;
2028
2029                 rc = echo_destroy_object(env, ed, parent, name, namelen,
2030                                          id, mode, count);
2031                 break;
2032         }
2033         case ECHO_MD_LOOKUP:
2034                 rc = echo_lookup_object(env, ed, parent, id, count);
2035                 break;
2036         case ECHO_MD_GETATTR:
2037                 rc = echo_getattr_object(env, ed, parent, id, count);
2038                 break;
2039         case ECHO_MD_SETATTR:
2040                 rc = echo_setattr_object(env, ed, parent, id, count);
2041                 break;
2042         default:
2043                 CERROR("unknown command %d\n", command);
2044                 rc = -EINVAL;
2045                 break;
2046         }
2047         if (name != NULL)
2048                 OBD_FREE(name, namelen + 1);
2049         lu_object_put(env, parent);
2050         cl_env_put(env, &refcheck);
2051         return rc;
2052 }
2053
2054 static int echo_create_object(const struct lu_env *env, struct echo_device *ed,
2055                               int on_target, struct obdo *oa, void *ulsm,
2056                               int ulsm_nob, struct obd_trans_info *oti)
2057 {
2058         struct echo_object     *eco;
2059         struct echo_client_obd *ec = ed->ed_ec;
2060         struct lov_stripe_md   *lsm = NULL;
2061         int                     rc;
2062         int                     created = 0;
2063         ENTRY;
2064
2065         if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
2066             (on_target ||                       /* set_stripe */
2067              ec->ec_nstripes != 0)) {           /* LOV */
2068                 CERROR ("No valid oid\n");
2069                 RETURN(-EINVAL);
2070         }
2071
2072         rc = obd_alloc_memmd(ec->ec_exp, &lsm);
2073         if (rc < 0) {
2074                 CERROR("Cannot allocate md: rc = %d\n", rc);
2075                 GOTO(failed, rc);
2076         }
2077
2078         if (ulsm != NULL) {
2079                 int i, idx;
2080
2081                 rc = echo_copyin_lsm (ed, lsm, ulsm, ulsm_nob);
2082                 if (rc != 0)
2083                         GOTO(failed, rc);
2084
2085                 if (lsm->lsm_stripe_count == 0)
2086                         lsm->lsm_stripe_count = ec->ec_nstripes;
2087
2088                 if (lsm->lsm_stripe_size == 0)
2089                         lsm->lsm_stripe_size = CFS_PAGE_SIZE;
2090
2091                 idx = cfs_rand();
2092
2093                 /* setup stripes: indices + default ids if required */
2094                 for (i = 0; i < lsm->lsm_stripe_count; i++) {
2095                         if (lsm->lsm_oinfo[i]->loi_id == 0)
2096                                 lsm->lsm_oinfo[i]->loi_id = lsm->lsm_object_id;
2097
2098                         lsm->lsm_oinfo[i]->loi_ost_idx =
2099                                 (idx + i) % ec->ec_nstripes;
2100                 }
2101         }
2102
2103         /* setup object ID here for !on_target and LOV hint */
2104         if (oa->o_valid & OBD_MD_FLID)
2105                 lsm->lsm_object_id = oa->o_id;
2106
2107         if (lsm->lsm_object_id == 0)
2108                 lsm->lsm_object_id = ++last_object_id;
2109
2110         rc = 0;
2111         if (on_target) {
2112                 /* Only echo objects are allowed to be created */
2113                 LASSERT((oa->o_valid & OBD_MD_FLGROUP) &&
2114                         (oa->o_seq == FID_SEQ_ECHO));
2115                 rc = obd_create(env, ec->ec_exp, oa, &lsm, oti);
2116                 if (rc != 0) {
2117                         CERROR("Cannot create objects: rc = %d\n", rc);
2118                         GOTO(failed, rc);
2119                 }
2120                 created = 1;
2121         }
2122
2123         /* See what object ID we were given */
2124         oa->o_id = lsm->lsm_object_id;
2125         oa->o_valid |= OBD_MD_FLID;
2126
2127         eco = cl_echo_object_find(ed, &lsm);
2128         if (IS_ERR(eco))
2129                 GOTO(failed, rc = PTR_ERR(eco));
2130         cl_echo_object_put(eco);
2131
2132         CDEBUG(D_INFO, "oa->o_id = %lx\n", (long)oa->o_id);
2133         EXIT;
2134
2135  failed:
2136         if (created && rc)
2137                 obd_destroy(env, ec->ec_exp, oa, lsm, oti, NULL, NULL);
2138         if (lsm)
2139                 obd_free_memmd(ec->ec_exp, &lsm);
2140         if (rc)
2141                 CERROR("create object failed with: rc = %d\n", rc);
2142         return (rc);
2143 }
2144
2145 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
2146                            struct obdo *oa)
2147 {
2148         struct echo_client_obd *ec  = ed->ed_ec;
2149         struct lov_stripe_md   *lsm = NULL;
2150         struct echo_object     *eco;
2151         int                     rc;
2152         ENTRY;
2153
2154         if ((oa->o_valid & OBD_MD_FLID) == 0 ||
2155             oa->o_id == 0)  /* disallow use of object id 0 */
2156         {
2157                 CERROR ("No valid oid\n");
2158                 RETURN(-EINVAL);
2159         }
2160
2161         rc = obd_alloc_memmd(ec->ec_exp, &lsm);
2162         if (rc < 0)
2163                 RETURN(rc);
2164
2165         lsm->lsm_object_id = oa->o_id;
2166         if (oa->o_valid & OBD_MD_FLGROUP)
2167                 lsm->lsm_object_seq = oa->o_seq;
2168         else
2169                 lsm->lsm_object_seq = FID_SEQ_ECHO;
2170
2171         rc = 0;
2172         eco = cl_echo_object_find(ed, &lsm);
2173         if (!IS_ERR(eco))
2174                 *ecop = eco;
2175         else
2176                 rc = PTR_ERR(eco);
2177         if (lsm)
2178                 obd_free_memmd(ec->ec_exp, &lsm);
2179         RETURN(rc);
2180 }
2181
2182 static void echo_put_object(struct echo_object *eco)
2183 {
2184         if (cl_echo_object_put(eco))
2185                 CERROR("echo client: drop an object failed");
2186 }
2187
2188 static void
2189 echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
2190 {
2191         unsigned long stripe_count;
2192         unsigned long stripe_size;
2193         unsigned long width;
2194         unsigned long woffset;
2195         int           stripe_index;
2196         obd_off       offset;
2197
2198         if (lsm->lsm_stripe_count <= 1)
2199                 return;
2200
2201         offset       = *offp;
2202         stripe_size  = lsm->lsm_stripe_size;
2203         stripe_count = lsm->lsm_stripe_count;
2204
2205         /* width = # bytes in all stripes */
2206         width = stripe_size * stripe_count;
2207
2208         /* woffset = offset within a width; offset = whole number of widths */
2209         woffset = do_div (offset, width);
2210
2211         stripe_index = woffset / stripe_size;
2212
2213         *idp = lsm->lsm_oinfo[stripe_index]->loi_id;
2214         *offp = offset * stripe_size + woffset % stripe_size;
2215 }
2216
2217 static void
2218 echo_client_page_debug_setup(struct lov_stripe_md *lsm,
2219                              cfs_page_t *page, int rw, obd_id id,
2220                              obd_off offset, obd_off count)
2221 {
2222         char    *addr;
2223         obd_off  stripe_off;
2224         obd_id   stripe_id;
2225         int      delta;
2226
2227         /* no partial pages on the client */
2228         LASSERT(count == CFS_PAGE_SIZE);
2229
2230         addr = cfs_kmap(page);
2231
2232         for (delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2233                 if (rw == OBD_BRW_WRITE) {
2234                         stripe_off = offset + delta;
2235                         stripe_id = id;
2236                         echo_get_stripe_off_id(lsm, &stripe_off, &stripe_id);
2237                 } else {
2238                         stripe_off = 0xdeadbeef00c0ffeeULL;
2239                         stripe_id = 0xdeadbeef00c0ffeeULL;
2240                 }
2241                 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
2242                                   stripe_off, stripe_id);
2243         }
2244
2245         cfs_kunmap(page);
2246 }
2247
2248 static int echo_client_page_debug_check(struct lov_stripe_md *lsm,
2249                                         cfs_page_t *page, obd_id id,
2250                                         obd_off offset, obd_off count)
2251 {
2252         obd_off stripe_off;
2253         obd_id  stripe_id;
2254         char   *addr;
2255         int     delta;
2256         int     rc;
2257         int     rc2;
2258
2259         /* no partial pages on the client */
2260         LASSERT(count == CFS_PAGE_SIZE);
2261
2262         addr = cfs_kmap(page);
2263
2264         for (rc = delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2265                 stripe_off = offset + delta;
2266                 stripe_id = id;
2267                 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
2268
2269                 rc2 = block_debug_check("test_brw",
2270                                         addr + delta, OBD_ECHO_BLOCK_SIZE,
2271                                         stripe_off, stripe_id);
2272                 if (rc2 != 0) {
2273                         CERROR ("Error in echo object "LPX64"\n", id);
2274                         rc = rc2;
2275                 }
2276         }
2277
2278         cfs_kunmap(page);
2279         return rc;
2280 }
2281
2282 static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
2283                             struct echo_object *eco, obd_off offset,
2284                             obd_size count, int async,
2285                             struct obd_trans_info *oti)
2286 {
2287         struct lov_stripe_md   *lsm = eco->eo_lsm;
2288         obd_count               npages;
2289         struct brw_page        *pga;
2290         struct brw_page        *pgp;
2291         cfs_page_t            **pages;
2292         obd_off                 off;
2293         int                     i;
2294         int                     rc;
2295         int                     verify;
2296         int                     gfp_mask;
2297         int                     brw_flags = 0;
2298         ENTRY;
2299
2300         verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
2301                   (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
2302                   (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
2303
2304         gfp_mask = ((oa->o_id & 2) == 0) ? CFS_ALLOC_STD : CFS_ALLOC_HIGHUSER;
2305
2306         LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
2307         LASSERT(lsm != NULL);
2308         LASSERT(lsm->lsm_object_id == oa->o_id);
2309
2310         if (count <= 0 ||
2311             (count & (~CFS_PAGE_MASK)) != 0)
2312                 RETURN(-EINVAL);
2313
2314         /* XXX think again with misaligned I/O */
2315         npages = count >> CFS_PAGE_SHIFT;
2316
2317         if (rw == OBD_BRW_WRITE)
2318                 brw_flags = OBD_BRW_ASYNC;
2319
2320         OBD_ALLOC(pga, npages * sizeof(*pga));
2321         if (pga == NULL)
2322                 RETURN(-ENOMEM);
2323
2324         OBD_ALLOC(pages, npages * sizeof(*pages));
2325         if (pages == NULL) {
2326                 OBD_FREE(pga, npages * sizeof(*pga));
2327                 RETURN(-ENOMEM);
2328         }
2329
2330         for (i = 0, pgp = pga, off = offset;
2331              i < npages;
2332              i++, pgp++, off += CFS_PAGE_SIZE) {
2333
2334                 LASSERT (pgp->pg == NULL);      /* for cleanup */
2335
2336                 rc = -ENOMEM;
2337                 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
2338                 if (pgp->pg == NULL)
2339                         goto out;
2340
2341                 pages[i] = pgp->pg;
2342                 pgp->count = CFS_PAGE_SIZE;
2343                 pgp->off = off;
2344                 pgp->flag = brw_flags;
2345
2346                 if (verify)
2347                         echo_client_page_debug_setup(lsm, pgp->pg, rw,
2348                                                      oa->o_id, off, pgp->count);
2349         }
2350
2351         /* brw mode can only be used at client */
2352         LASSERT(ed->ed_next != NULL);
2353         rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
2354
2355  out:
2356         if (rc != 0 || rw != OBD_BRW_READ)
2357                 verify = 0;
2358
2359         for (i = 0, pgp = pga; i < npages; i++, pgp++) {
2360                 if (pgp->pg == NULL)
2361                         continue;
2362
2363                 if (verify) {
2364                         int vrc;
2365                         vrc = echo_client_page_debug_check(lsm, pgp->pg, oa->o_id,
2366                                                            pgp->off, pgp->count);
2367                         if (vrc != 0 && rc == 0)
2368                                 rc = vrc;
2369                 }
2370                 OBD_PAGE_FREE(pgp->pg);
2371         }
2372         OBD_FREE(pga, npages * sizeof(*pga));
2373         OBD_FREE(pages, npages * sizeof(*pages));
2374         RETURN(rc);
2375 }
2376
2377 static int echo_client_prep_commit(struct obd_export *exp, int rw,
2378                                    struct obdo *oa, struct echo_object *eco,
2379                                    obd_off offset, obd_size count,
2380                                    obd_size batch, struct obd_trans_info *oti,
2381                                    int async)
2382 {
2383         struct lov_stripe_md *lsm = eco->eo_lsm;
2384         struct obd_ioobj ioo;
2385         struct niobuf_local *lnb;
2386         struct niobuf_remote *rnb;
2387         obd_off off;
2388         obd_size npages, tot_pages;
2389         int i, ret = 0;
2390         ENTRY;
2391
2392         if (count <= 0 || (count & (~CFS_PAGE_MASK)) != 0 ||
2393             (lsm != NULL && lsm->lsm_object_id != oa->o_id))
2394                 RETURN(-EINVAL);
2395
2396         npages = batch >> CFS_PAGE_SHIFT;
2397         tot_pages = count >> CFS_PAGE_SHIFT;
2398
2399         OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
2400         OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
2401
2402         if (lnb == NULL || rnb == NULL)
2403                 GOTO(out, ret = -ENOMEM);
2404
2405         obdo_to_ioobj(oa, &ioo);
2406
2407         off = offset;
2408
2409         for(; tot_pages; tot_pages -= npages) {
2410                 int lpages;
2411
2412                 if (tot_pages < npages)
2413                         npages = tot_pages;
2414
2415                 for (i = 0; i < npages; i++, off += CFS_PAGE_SIZE) {
2416                         rnb[i].offset = off;
2417                         rnb[i].len = CFS_PAGE_SIZE;
2418                 }
2419
2420                 ioo.ioo_bufcnt = npages;
2421                 oti->oti_transno = 0;
2422
2423                 lpages = npages;
2424                 ret = obd_preprw(NULL, rw, exp, oa, 1, &ioo, rnb, &lpages,
2425                                  lnb, oti, NULL);
2426                 if (ret != 0)
2427                         GOTO(out, ret);
2428                 LASSERT(lpages == npages);
2429
2430                 for (i = 0; i < lpages; i++) {
2431                         cfs_page_t *page = lnb[i].page;
2432
2433                         /* read past eof? */
2434                         if (page == NULL && lnb[i].rc == 0)
2435                                 continue;
2436
2437                         if (async)
2438                                 lnb[i].flags |= OBD_BRW_ASYNC;
2439
2440                         if (oa->o_id == ECHO_PERSISTENT_OBJID ||
2441                             (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
2442                             (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
2443                                 continue;
2444
2445                         if (rw == OBD_BRW_WRITE)
2446                                 echo_client_page_debug_setup(lsm, page, rw,
2447                                                              oa->o_id,
2448                                                              rnb[i].offset,
2449                                                              rnb[i].len);
2450                         else
2451                                 echo_client_page_debug_check(lsm, page,
2452                                                              oa->o_id,
2453                                                              rnb[i].offset,
2454                                                              rnb[i].len);
2455                 }
2456
2457                 ret = obd_commitrw(NULL, rw, exp, oa, 1, &ioo,
2458                                    rnb, npages, lnb, oti, ret);
2459                 if (ret != 0)
2460                         GOTO(out, ret);
2461
2462                 /* Reset oti otherwise it would confuse ldiskfs. */
2463                 memset(oti, 0, sizeof(*oti));
2464         }
2465
2466 out:
2467         if (lnb)
2468                 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
2469         if (rnb)
2470                 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
2471         RETURN(ret);
2472 }
2473
2474 static int echo_client_brw_ioctl(int rw, struct obd_export *exp,
2475                                  struct obd_ioctl_data *data)
2476 {
2477         struct obd_device *obd = class_exp2obd(exp);
2478         struct echo_device *ed = obd2echo_dev(obd);
2479         struct echo_client_obd *ec = ed->ed_ec;
2480         struct obd_trans_info dummy_oti = { 0 };
2481         struct obdo *oa = &data->ioc_obdo1;
2482         struct echo_object *eco;
2483         int rc;
2484         int async = 1;
2485         long test_mode;
2486         ENTRY;
2487
2488         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2489
2490         rc = echo_get_object(&eco, ed, oa);
2491         if (rc)
2492                 RETURN(rc);
2493
2494         oa->o_valid &= ~OBD_MD_FLHANDLE;
2495
2496         /* obdfilter doesn't support obd_brw now, simulate via prep + commit */
2497         test_mode = (long)data->ioc_pbuf1;
2498         if (test_mode == 1)
2499                 async = 0;
2500
2501         if (ed->ed_next == NULL && test_mode != 3) {
2502                 test_mode = 3;
2503                 data->ioc_plen1 = data->ioc_count;
2504         }
2505
2506         /* Truncate batch size to maximum */
2507         if (data->ioc_plen1 > PTLRPC_MAX_BRW_SIZE)
2508                 data->ioc_plen1 = PTLRPC_MAX_BRW_SIZE;
2509
2510         switch (test_mode) {
2511         case 1:
2512                 /* fall through */
2513         case 2:
2514                 rc = echo_client_kbrw(ed, rw, oa,
2515                                       eco, data->ioc_offset,
2516                                       data->ioc_count, async, &dummy_oti);
2517                 break;
2518         case 3:
2519                 rc = echo_client_prep_commit(ec->ec_exp, rw, oa,
2520                                             eco, data->ioc_offset,
2521                                             data->ioc_count, data->ioc_plen1,
2522                                             &dummy_oti, async);
2523                 break;
2524         default:
2525                 rc = -EINVAL;
2526         }
2527         echo_put_object(eco);
2528         RETURN(rc);
2529 }
2530
2531 static int
2532 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
2533                     int mode, obd_off offset, obd_size nob)
2534 {
2535         struct echo_device     *ed = obd2echo_dev(exp->exp_obd);
2536         struct lustre_handle   *ulh = &oa->o_handle;
2537         struct echo_object     *eco;
2538         obd_off                 end;
2539         int                     rc;
2540         ENTRY;
2541
2542         if (ed->ed_next == NULL)
2543                 RETURN(-EOPNOTSUPP);
2544
2545         if (!(mode == LCK_PR || mode == LCK_PW))
2546                 RETURN(-EINVAL);
2547
2548         if ((offset & (~CFS_PAGE_MASK)) != 0 ||
2549             (nob & (~CFS_PAGE_MASK)) != 0)
2550                 RETURN(-EINVAL);
2551
2552         rc = echo_get_object (&eco, ed, oa);
2553         if (rc != 0)
2554                 RETURN(rc);
2555
2556         end = (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
2557         rc = cl_echo_enqueue(eco, offset, end, mode, &ulh->cookie);
2558         if (rc == 0) {
2559                 oa->o_valid |= OBD_MD_FLHANDLE;
2560                 CDEBUG(D_INFO, "Cookie is "LPX64"\n", ulh->cookie);
2561         }
2562         echo_put_object(eco);
2563         RETURN(rc);
2564 }
2565
2566 static int
2567 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
2568 {
2569         struct echo_device *ed     = obd2echo_dev(exp->exp_obd);
2570         __u64               cookie = oa->o_handle.cookie;
2571
2572         if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
2573                 return -EINVAL;
2574
2575         CDEBUG(D_INFO, "Cookie is "LPX64"\n", cookie);
2576         return cl_echo_cancel(ed, cookie);
2577 }
2578
2579 static int
2580 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2581                       void *karg, void *uarg)
2582 {
2583         struct obd_device      *obd = exp->exp_obd;
2584         struct echo_device     *ed = obd2echo_dev(obd);
2585         struct echo_client_obd *ec = ed->ed_ec;
2586         struct echo_object     *eco;
2587         struct obd_ioctl_data  *data = karg;
2588         struct obd_trans_info   dummy_oti;
2589         struct lu_env          *env;
2590         struct oti_req_ack_lock *ack_lock;
2591         struct obdo            *oa;
2592         struct lu_fid           fid;
2593         int                     rw = OBD_BRW_READ;
2594         int                     rc = 0;
2595         int                     i;
2596         ENTRY;
2597
2598         memset(&dummy_oti, 0, sizeof(dummy_oti));
2599
2600         oa = &data->ioc_obdo1;
2601         if (!(oa->o_valid & OBD_MD_FLGROUP)) {
2602                 oa->o_valid |= OBD_MD_FLGROUP;
2603                 oa->o_seq = FID_SEQ_ECHO;
2604         }
2605
2606         /* This FID is unpacked just for validation at this point */
2607         rc = fid_ostid_unpack(&fid, &oa->o_oi, 0);
2608         if (rc < 0)
2609                 RETURN(rc);
2610
2611         OBD_ALLOC_PTR(env);
2612         if (env == NULL)
2613                 RETURN(-ENOMEM);
2614
2615         rc = lu_env_init(env, LCT_DT_THREAD);
2616         if (rc)
2617                 GOTO(out, rc = -ENOMEM);
2618
2619         switch (cmd) {
2620         case OBD_IOC_CREATE:                    /* may create echo object */
2621                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2622                         GOTO (out, rc = -EPERM);
2623
2624                 rc = echo_create_object(env, ed, 1, oa, data->ioc_pbuf1,
2625                                         data->ioc_plen1, &dummy_oti);
2626                 GOTO(out, rc);
2627
2628         case OBD_IOC_ECHO_MD: {
2629                 int count;
2630                 int cmd;
2631                 char *dir = NULL;
2632                 int dirlen;
2633                 __u64 id;
2634
2635                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2636                         GOTO(out, rc = -EPERM);
2637
2638                 count = data->ioc_count;
2639                 cmd = data->ioc_command;
2640
2641                 id = data->ioc_obdo2.o_id;
2642
2643                 dirlen = data->ioc_plen1;
2644                 OBD_ALLOC(dir, dirlen + 1);
2645                 if (dir == NULL)
2646                         GOTO(out, rc = -ENOMEM);
2647
2648                 if (cfs_copy_from_user(dir, data->ioc_pbuf1, dirlen)) {
2649                         OBD_FREE(dir, data->ioc_plen1 + 1);
2650                         GOTO(out, rc = -EFAULT);
2651                 }
2652
2653                 rc = echo_md_handler(ed, cmd, dir, dirlen, id, count, data);
2654                 OBD_FREE(dir, dirlen + 1);
2655                 GOTO(out, rc);
2656         }
2657         case OBD_IOC_ECHO_ALLOC_SEQ: {
2658                 struct lu_env   *cl_env;
2659                 int              refcheck;
2660                 __u64            seq;
2661                 int              max_count;
2662
2663                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2664                         GOTO(out, rc = -EPERM);
2665
2666                 cl_env = cl_env_get(&refcheck);
2667                 if (IS_ERR(cl_env))
2668                         GOTO(out, rc = PTR_ERR(cl_env));
2669
2670                 rc = lu_env_refill_by_tags(cl_env, ECHO_MD_CTX_TAG,
2671                                             ECHO_MD_SES_TAG);
2672                 if (rc != 0) {
2673                         cl_env_put(cl_env, &refcheck);
2674                         GOTO(out, rc);
2675                 }
2676
2677                 rc = seq_client_get_seq(cl_env, ed->ed_cl_seq, &seq);
2678                 cl_env_put(cl_env, &refcheck);
2679                 if (rc < 0) {
2680                         CERROR("%s: Can not alloc seq: rc = %d\n",
2681                                obd->obd_name, rc);
2682                         GOTO(out, rc);
2683                 }
2684
2685                 if (cfs_copy_to_user(data->ioc_pbuf1, &seq, data->ioc_plen1))
2686                         return -EFAULT;
2687
2688                 max_count = LUSTRE_SEQ_MAX_WIDTH;
2689                 if (cfs_copy_to_user(data->ioc_pbuf2, &max_count,
2690                                      data->ioc_plen2))
2691                         return -EFAULT;
2692                 GOTO(out, rc);
2693         }
2694         case OBD_IOC_DESTROY:
2695                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2696                         GOTO (out, rc = -EPERM);
2697
2698                 rc = echo_get_object(&eco, ed, oa);
2699                 if (rc == 0) {
2700                         rc = obd_destroy(env, ec->ec_exp, oa, eco->eo_lsm,
2701                                          &dummy_oti, NULL, NULL);
2702                         if (rc == 0)
2703                                 eco->eo_deleted = 1;
2704                         echo_put_object(eco);
2705                 }
2706                 GOTO(out, rc);
2707
2708         case OBD_IOC_GETATTR:
2709                 rc = echo_get_object(&eco, ed, oa);
2710                 if (rc == 0) {
2711                         struct obd_info oinfo = { { { 0 } } };
2712                         oinfo.oi_md = eco->eo_lsm;
2713                         oinfo.oi_oa = oa;
2714                         rc = obd_getattr(env, ec->ec_exp, &oinfo);
2715                         echo_put_object(eco);
2716                 }
2717                 GOTO(out, rc);
2718
2719         case OBD_IOC_SETATTR:
2720                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2721                         GOTO (out, rc = -EPERM);
2722
2723                 rc = echo_get_object(&eco, ed, oa);
2724                 if (rc == 0) {
2725                         struct obd_info oinfo = { { { 0 } } };
2726                         oinfo.oi_oa = oa;
2727                         oinfo.oi_md = eco->eo_lsm;
2728
2729                         rc = obd_setattr(env, ec->ec_exp, &oinfo, NULL);
2730                         echo_put_object(eco);
2731                 }
2732                 GOTO(out, rc);
2733
2734         case OBD_IOC_BRW_WRITE:
2735                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2736                         GOTO (out, rc = -EPERM);
2737
2738                 rw = OBD_BRW_WRITE;
2739                 /* fall through */
2740         case OBD_IOC_BRW_READ:
2741                 rc = echo_client_brw_ioctl(rw, exp, data);
2742                 GOTO(out, rc);
2743
2744         case ECHO_IOC_GET_STRIPE:
2745                 rc = echo_get_object(&eco, ed, oa);
2746                 if (rc == 0) {
2747                         rc = echo_copyout_lsm(eco->eo_lsm, data->ioc_pbuf1,
2748                                               data->ioc_plen1);
2749                         echo_put_object(eco);
2750                 }
2751                 GOTO(out, rc);
2752
2753         case ECHO_IOC_SET_STRIPE:
2754                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2755                         GOTO (out, rc = -EPERM);
2756
2757                 if (data->ioc_pbuf1 == NULL) {  /* unset */
2758                         rc = echo_get_object(&eco, ed, oa);
2759                         if (rc == 0) {
2760                                 eco->eo_deleted = 1;
2761                                 echo_put_object(eco);
2762                         }
2763                 } else {
2764                         rc = echo_create_object(env, ed, 0, oa,
2765                                                 data->ioc_pbuf1,
2766                                                 data->ioc_plen1, &dummy_oti);
2767                 }
2768                 GOTO (out, rc);
2769
2770         case ECHO_IOC_ENQUEUE:
2771                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2772                         GOTO (out, rc = -EPERM);
2773
2774                 rc = echo_client_enqueue(exp, oa,
2775                                          data->ioc_conn1, /* lock mode */
2776                                          data->ioc_offset,
2777                                          data->ioc_count);/*extent*/
2778                 GOTO (out, rc);
2779
2780         case ECHO_IOC_CANCEL:
2781                 rc = echo_client_cancel(exp, oa);
2782                 GOTO (out, rc);
2783
2784         default:
2785                 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
2786                 GOTO (out, rc = -ENOTTY);
2787         }
2788
2789         EXIT;
2790 out:
2791         lu_env_fini(env);
2792         OBD_FREE_PTR(env);
2793
2794         /* XXX this should be in a helper also called by target_send_reply */
2795         for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
2796              i++, ack_lock++) {
2797                 if (!ack_lock->mode)
2798                         break;
2799                 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
2800         }
2801
2802         return rc;
2803 }
2804
2805 static int echo_client_setup(const struct lu_env *env,
2806                              struct obd_device *obddev, struct lustre_cfg *lcfg)
2807 {
2808         struct echo_client_obd *ec = &obddev->u.echo_client;
2809         struct obd_device *tgt;
2810         struct obd_uuid echo_uuid = { "ECHO_UUID" };
2811         struct obd_connect_data *ocd = NULL;
2812         int rc;
2813         ENTRY;
2814
2815         if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
2816                 CERROR("requires a TARGET OBD name\n");
2817                 RETURN(-EINVAL);
2818         }
2819
2820         tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
2821         if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
2822                 CERROR("device not attached or not set up (%s)\n",
2823                        lustre_cfg_string(lcfg, 1));
2824                 RETURN(-EINVAL);
2825         }
2826
2827         cfs_spin_lock_init (&ec->ec_lock);
2828         CFS_INIT_LIST_HEAD (&ec->ec_objects);
2829         CFS_INIT_LIST_HEAD (&ec->ec_locks);
2830         ec->ec_unique = 0;
2831         ec->ec_nstripes = 0;
2832
2833         if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
2834                 lu_context_tags_update(ECHO_MD_CTX_TAG);
2835                 lu_session_tags_update(ECHO_MD_SES_TAG);
2836                 RETURN(0);
2837         }
2838
2839         OBD_ALLOC(ocd, sizeof(*ocd));
2840         if (ocd == NULL) {
2841                 CERROR("Can't alloc ocd connecting to %s\n",
2842                        lustre_cfg_string(lcfg, 1));
2843                 return -ENOMEM;
2844         }
2845
2846         ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
2847                                  OBD_CONNECT_GRANT | OBD_CONNECT_FULL20 |
2848                                  OBD_CONNECT_64BITHASH;
2849         ocd->ocd_version = LUSTRE_VERSION_CODE;
2850         ocd->ocd_group = FID_SEQ_ECHO;
2851
2852         rc = obd_connect(env, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
2853         if (rc == 0) {
2854                 /* Turn off pinger because it connects to tgt obd directly. */
2855                 cfs_spin_lock(&tgt->obd_dev_lock);
2856                 cfs_list_del_init(&ec->ec_exp->exp_obd_chain_timed);
2857                 cfs_spin_unlock(&tgt->obd_dev_lock);
2858         }
2859
2860         OBD_FREE(ocd, sizeof(*ocd));
2861
2862         if (rc != 0) {
2863                 CERROR("fail to connect to device %s\n",
2864                        lustre_cfg_string(lcfg, 1));
2865                 return (rc);
2866         }
2867
2868         RETURN(rc);
2869 }
2870
2871 static int echo_client_cleanup(struct obd_device *obddev)
2872 {
2873         struct echo_device *ed = obd2echo_dev(obddev);
2874         struct echo_client_obd *ec = &obddev->u.echo_client;
2875         int rc;
2876         ENTRY;
2877
2878         /*Do nothing for Metadata echo client*/
2879         if (ed == NULL )
2880                 RETURN(0);
2881
2882         if (ed->ed_next_ismd) {
2883                 lu_context_tags_clear(ECHO_MD_CTX_TAG);
2884                 lu_session_tags_clear(ECHO_MD_SES_TAG);
2885                 RETURN(0);
2886         }
2887
2888         if (!cfs_list_empty(&obddev->obd_exports)) {
2889                 CERROR("still has clients!\n");
2890                 RETURN(-EBUSY);
2891         }
2892
2893         LASSERT(cfs_atomic_read(&ec->ec_exp->exp_refcount) > 0);
2894         rc = obd_disconnect(ec->ec_exp);
2895         if (rc != 0)
2896                 CERROR("fail to disconnect device: %d\n", rc);
2897
2898         RETURN(rc);
2899 }
2900
2901 static int echo_client_connect(const struct lu_env *env,
2902                                struct obd_export **exp,
2903                                struct obd_device *src, struct obd_uuid *cluuid,
2904                                struct obd_connect_data *data, void *localdata)
2905 {
2906         int                rc;
2907         struct lustre_handle conn = { 0 };
2908
2909         ENTRY;
2910         rc = class_connect(&conn, src, cluuid);
2911         if (rc == 0) {
2912                 *exp = class_conn2export(&conn);
2913         }
2914
2915         RETURN (rc);
2916 }
2917
2918 static int echo_client_disconnect(struct obd_export *exp)
2919 {
2920 #if 0
2921         struct obd_device      *obd;
2922         struct echo_client_obd *ec;
2923         struct ec_lock         *ecl;
2924 #endif
2925         int                     rc;
2926         ENTRY;
2927
2928         if (exp == NULL)
2929                 GOTO(out, rc = -EINVAL);
2930
2931 #if 0
2932         obd = exp->exp_obd;
2933         ec = &obd->u.echo_client;
2934
2935         /* no more contention on export's lock list */
2936         while (!cfs_list_empty (&exp->exp_ec_data.eced_locks)) {
2937                 ecl = cfs_list_entry (exp->exp_ec_data.eced_locks.next,
2938                                       struct ec_lock, ecl_exp_chain);
2939                 cfs_list_del (&ecl->ecl_exp_chain);
2940
2941                 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm,
2942                                  ecl->ecl_mode, &ecl->ecl_lock_handle);
2943
2944                 CDEBUG (D_INFO, "Cancel lock on object "LPX64" on disconnect "
2945                         "(%d)\n", ecl->ecl_object->eco_id, rc);
2946
2947                 echo_put_object (ecl->ecl_object);
2948                 OBD_FREE (ecl, sizeof (*ecl));
2949         }
2950 #endif
2951
2952         rc = class_disconnect(exp);
2953         GOTO(out, rc);
2954  out:
2955         return rc;
2956 }
2957
2958 static struct obd_ops echo_client_obd_ops = {
2959         .o_owner       = THIS_MODULE,
2960
2961 #if 0
2962         .o_setup       = echo_client_setup,
2963         .o_cleanup     = echo_client_cleanup,
2964 #endif
2965
2966         .o_iocontrol   = echo_client_iocontrol,
2967         .o_connect     = echo_client_connect,
2968         .o_disconnect  = echo_client_disconnect
2969 };
2970
2971 int echo_client_init(void)
2972 {
2973         struct lprocfs_static_vars lvars = { 0 };
2974         int rc;
2975
2976         lprocfs_echo_init_vars(&lvars);
2977
2978         rc = lu_kmem_init(echo_caches);
2979         if (rc == 0) {
2980                 rc = class_register_type(&echo_client_obd_ops, NULL,
2981                                          lvars.module_vars,
2982                                          LUSTRE_ECHO_CLIENT_NAME,
2983                                          &echo_device_type);
2984                 if (rc)
2985                         lu_kmem_fini(echo_caches);
2986         }
2987         return rc;
2988 }
2989
2990 void echo_client_exit(void)
2991 {
2992         class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
2993         lu_kmem_fini(echo_caches);
2994 }
2995
2996 #ifdef __KERNEL__
2997 static int __init obdecho_init(void)
2998 {
2999         struct lprocfs_static_vars lvars;
3000         int rc;
3001
3002         ENTRY;
3003         LCONSOLE_INFO("Echo OBD driver; http://www.lustre.org/\n");
3004
3005         LASSERT(CFS_PAGE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
3006
3007         lprocfs_echo_init_vars(&lvars);
3008
3009 # ifdef HAVE_SERVER_SUPPORT
3010         rc = echo_persistent_pages_init();
3011         if (rc != 0)
3012                 goto failed_0;
3013
3014         rc = class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
3015                                  LUSTRE_ECHO_NAME, NULL);
3016         if (rc != 0)
3017                 goto failed_1;
3018 # endif
3019
3020         rc = echo_client_init();
3021
3022 # ifdef HAVE_SERVER_SUPPORT
3023         if (rc == 0)
3024                 RETURN(0);
3025
3026         class_unregister_type(LUSTRE_ECHO_NAME);
3027 failed_1:
3028         echo_persistent_pages_fini();
3029 failed_0:
3030 # endif
3031         RETURN(rc);
3032 }
3033
3034 static void /*__exit*/ obdecho_exit(void)
3035 {
3036         echo_client_exit();
3037
3038 # ifdef HAVE_SERVER_SUPPORT
3039         class_unregister_type(LUSTRE_ECHO_NAME);
3040         echo_persistent_pages_fini();
3041 # endif
3042 }
3043
3044 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3045 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
3046 MODULE_LICENSE("GPL");
3047
3048 cfs_module(obdecho, LUSTRE_VERSION_STRING, obdecho_init, obdecho_exit);
3049 #endif /* __KERNEL__ */
3050
3051 /** @} echo_client */