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