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