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