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