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