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