4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_ECHO
38 #include <libcfs/libcfs.h>
41 #include <obd_support.h>
42 #include <obd_class.h>
43 #include <lustre_debug.h>
44 #include <lprocfs_status.h>
45 #include <cl_object.h>
46 #include <lustre_fid.h>
47 #include <lustre_acl.h>
48 #include <lustre_ioctl.h>
49 #include <lustre_net.h>
50 #ifdef HAVE_SERVER_SUPPORT
51 # include <md_object.h>
52 #endif /* HAVE_SERVER_SUPPORT */
54 #include "echo_internal.h"
56 /** \defgroup echo_client Echo Client
61 struct cl_device ed_cl;
62 struct echo_client_obd *ed_ec;
64 struct cl_site ed_site_myself;
65 struct cl_site *ed_site;
66 struct lu_device *ed_next;
68 struct lu_client_seq *ed_cl_seq;
72 struct cl_object eo_cl;
73 struct cl_object_header eo_hdr;
75 struct echo_device *eo_dev;
76 struct list_head eo_obj_chain;
77 struct lov_stripe_md *eo_lsm;
82 struct echo_object_conf {
83 struct cl_object_conf eoc_cl;
84 struct lov_stripe_md **eoc_md;
88 struct cl_page_slice ep_cl;
93 struct cl_lock_slice el_cl;
94 struct list_head el_chain;
95 struct echo_object *el_object;
100 static int echo_client_setup(const struct lu_env *env,
101 struct obd_device *obddev,
102 struct lustre_cfg *lcfg);
103 static int echo_client_cleanup(struct obd_device *obddev);
106 /** \defgroup echo_helpers Helper functions
109 static inline struct echo_device *cl2echo_dev(const struct cl_device *dev)
111 return container_of0(dev, struct echo_device, ed_cl);
114 static inline struct cl_device *echo_dev2cl(struct echo_device *d)
119 static inline struct echo_device *obd2echo_dev(const struct obd_device *obd)
121 return cl2echo_dev(lu2cl_dev(obd->obd_lu_dev));
124 static inline struct cl_object *echo_obj2cl(struct echo_object *eco)
129 static inline struct echo_object *cl2echo_obj(const struct cl_object *o)
131 return container_of(o, struct echo_object, eo_cl);
134 static inline struct echo_page *cl2echo_page(const struct cl_page_slice *s)
136 return container_of(s, struct echo_page, ep_cl);
139 static inline struct echo_lock *cl2echo_lock(const struct cl_lock_slice *s)
141 return container_of(s, struct echo_lock, el_cl);
144 static inline struct cl_lock *echo_lock2cl(const struct echo_lock *ecl)
146 return ecl->el_cl.cls_lock;
149 static struct lu_context_key echo_thread_key;
150 static inline struct echo_thread_info *echo_env_info(const struct lu_env *env)
152 struct echo_thread_info *info;
153 info = lu_context_key_get(&env->le_ctx, &echo_thread_key);
154 LASSERT(info != NULL);
159 struct echo_object_conf *cl2echo_conf(const struct cl_object_conf *c)
161 return container_of(c, struct echo_object_conf, eoc_cl);
164 /** @} echo_helpers */
166 static struct echo_object *cl_echo_object_find(struct echo_device *d,
167 struct lov_stripe_md **lsm);
168 static int cl_echo_object_put(struct echo_object *eco);
169 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
170 struct page **pages, int npages, int async);
172 struct echo_thread_info {
173 struct echo_object_conf eti_conf;
174 struct lustre_md eti_md;
176 struct cl_2queue eti_queue;
178 struct cl_lock eti_lock;
179 struct lu_fid eti_fid;
180 struct lu_fid eti_fid2;
181 #ifdef HAVE_SERVER_SUPPORT
182 struct md_op_spec eti_spec;
183 struct lov_mds_md_v3 eti_lmm;
184 struct lov_user_md_v3 eti_lum;
185 struct md_attr eti_ma;
186 struct lu_name eti_lname;
187 /* per-thread values, can be re-used */
191 struct lu_buf eti_buf;
192 char eti_xattr_buf[LUSTRE_POSIX_ACL_MAX_SIZE];
196 /* No session used right now */
197 struct echo_session_info {
201 static struct kmem_cache *echo_lock_kmem;
202 static struct kmem_cache *echo_object_kmem;
203 static struct kmem_cache *echo_thread_kmem;
204 static struct kmem_cache *echo_session_kmem;
205 /* static struct kmem_cache *echo_req_kmem; */
207 static struct lu_kmem_descr echo_caches[] = {
209 .ckd_cache = &echo_lock_kmem,
210 .ckd_name = "echo_lock_kmem",
211 .ckd_size = sizeof (struct echo_lock)
214 .ckd_cache = &echo_object_kmem,
215 .ckd_name = "echo_object_kmem",
216 .ckd_size = sizeof (struct echo_object)
219 .ckd_cache = &echo_thread_kmem,
220 .ckd_name = "echo_thread_kmem",
221 .ckd_size = sizeof (struct echo_thread_info)
224 .ckd_cache = &echo_session_kmem,
225 .ckd_name = "echo_session_kmem",
226 .ckd_size = sizeof (struct echo_session_info)
233 /** \defgroup echo_page Page operations
235 * Echo page operations.
239 static int echo_page_own(const struct lu_env *env,
240 const struct cl_page_slice *slice,
241 struct cl_io *io, int nonblock)
243 struct echo_page *ep = cl2echo_page(slice);
246 mutex_lock(&ep->ep_lock);
247 else if (!mutex_trylock(&ep->ep_lock))
252 static void echo_page_disown(const struct lu_env *env,
253 const struct cl_page_slice *slice,
256 struct echo_page *ep = cl2echo_page(slice);
258 LASSERT(mutex_is_locked(&ep->ep_lock));
259 mutex_unlock(&ep->ep_lock);
262 static void echo_page_discard(const struct lu_env *env,
263 const struct cl_page_slice *slice,
264 struct cl_io *unused)
266 cl_page_delete(env, slice->cpl_page);
269 static int echo_page_is_vmlocked(const struct lu_env *env,
270 const struct cl_page_slice *slice)
272 if (mutex_is_locked(&cl2echo_page(slice)->ep_lock))
277 static void echo_page_completion(const struct lu_env *env,
278 const struct cl_page_slice *slice,
281 LASSERT(slice->cpl_page->cp_sync_io != NULL);
284 static void echo_page_fini(const struct lu_env *env,
285 struct cl_page_slice *slice)
287 struct echo_object *eco = cl2echo_obj(slice->cpl_obj);
290 atomic_dec(&eco->eo_npages);
291 page_cache_release(slice->cpl_page->cp_vmpage);
295 static int echo_page_prep(const struct lu_env *env,
296 const struct cl_page_slice *slice,
297 struct cl_io *unused)
302 static int echo_page_print(const struct lu_env *env,
303 const struct cl_page_slice *slice,
304 void *cookie, lu_printer_t printer)
306 struct echo_page *ep = cl2echo_page(slice);
308 (*printer)(env, cookie, LUSTRE_ECHO_CLIENT_NAME"-page@%p %d vm@%p\n",
309 ep, mutex_is_locked(&ep->ep_lock),
310 slice->cpl_page->cp_vmpage);
314 static const struct cl_page_operations echo_page_ops = {
315 .cpo_own = echo_page_own,
316 .cpo_disown = echo_page_disown,
317 .cpo_discard = echo_page_discard,
318 .cpo_fini = echo_page_fini,
319 .cpo_print = echo_page_print,
320 .cpo_is_vmlocked = echo_page_is_vmlocked,
323 .cpo_prep = echo_page_prep,
324 .cpo_completion = echo_page_completion,
327 .cpo_prep = echo_page_prep,
328 .cpo_completion = echo_page_completion,
334 /** \defgroup echo_lock Locking
336 * echo lock operations
340 static void echo_lock_fini(const struct lu_env *env,
341 struct cl_lock_slice *slice)
343 struct echo_lock *ecl = cl2echo_lock(slice);
345 LASSERT(list_empty(&ecl->el_chain));
346 OBD_SLAB_FREE_PTR(ecl, echo_lock_kmem);
349 static struct cl_lock_operations echo_lock_ops = {
350 .clo_fini = echo_lock_fini,
355 /** \defgroup echo_cl_ops cl_object operations
357 * operations for cl_object
361 static int echo_page_init(const struct lu_env *env, struct cl_object *obj,
362 struct cl_page *page, pgoff_t index)
364 struct echo_page *ep = cl_object_page_slice(obj, page);
365 struct echo_object *eco = cl2echo_obj(obj);
368 page_cache_get(page->cp_vmpage);
369 mutex_init(&ep->ep_lock);
370 cl_page_slice_add(page, &ep->ep_cl, obj, index, &echo_page_ops);
371 atomic_inc(&eco->eo_npages);
375 static int echo_io_init(const struct lu_env *env, struct cl_object *obj,
381 static int echo_lock_init(const struct lu_env *env,
382 struct cl_object *obj, struct cl_lock *lock,
383 const struct cl_io *unused)
385 struct echo_lock *el;
388 OBD_SLAB_ALLOC_PTR_GFP(el, echo_lock_kmem, GFP_NOFS);
390 cl_lock_slice_add(lock, &el->el_cl, obj, &echo_lock_ops);
391 el->el_object = cl2echo_obj(obj);
392 INIT_LIST_HEAD(&el->el_chain);
393 atomic_set(&el->el_refcount, 0);
395 RETURN(el == NULL ? -ENOMEM : 0);
398 static int echo_conf_set(const struct lu_env *env, struct cl_object *obj,
399 const struct cl_object_conf *conf)
404 static const struct cl_object_operations echo_cl_obj_ops = {
405 .coo_page_init = echo_page_init,
406 .coo_lock_init = echo_lock_init,
407 .coo_io_init = echo_io_init,
408 .coo_conf_set = echo_conf_set
410 /** @} echo_cl_ops */
412 /** \defgroup echo_lu_ops lu_object operations
414 * operations for echo lu object.
418 static int echo_object_init(const struct lu_env *env, struct lu_object *obj,
419 const struct lu_object_conf *conf)
421 struct echo_device *ed = cl2echo_dev(lu2cl_dev(obj->lo_dev));
422 struct echo_client_obd *ec = ed->ed_ec;
423 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
427 struct lu_object *below;
428 struct lu_device *under;
431 below = under->ld_ops->ldo_object_alloc(env, obj->lo_header,
435 lu_object_add(obj, below);
438 if (!ed->ed_next_ismd) {
439 const struct cl_object_conf *cconf = lu2cl_conf(conf);
440 struct echo_object_conf *econf = cl2echo_conf(cconf);
442 LASSERT(econf->eoc_md);
443 eco->eo_lsm = *econf->eoc_md;
444 /* clear the lsm pointer so that it won't get freed. */
445 *econf->eoc_md = NULL;
451 atomic_set(&eco->eo_npages, 0);
452 cl_object_page_init(lu2cl(obj), sizeof(struct echo_page));
454 spin_lock(&ec->ec_lock);
455 list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
456 spin_unlock(&ec->ec_lock);
461 /* taken from osc_unpackmd() */
462 static int echo_alloc_memmd(struct echo_device *ed,
463 struct lov_stripe_md **lsmp)
469 /* If export is lov/osc then use their obd method */
470 if (ed->ed_next != NULL)
471 return obd_alloc_memmd(ed->ed_ec->ec_exp, lsmp);
472 /* OFD has no unpackmd method, do everything here */
473 lsm_size = lov_stripe_md_size(1);
475 LASSERT(*lsmp == NULL);
476 OBD_ALLOC(*lsmp, lsm_size);
480 OBD_ALLOC((*lsmp)->lsm_oinfo[0], sizeof(struct lov_oinfo));
481 if ((*lsmp)->lsm_oinfo[0] == NULL) {
482 OBD_FREE(*lsmp, lsm_size);
486 loi_init((*lsmp)->lsm_oinfo[0]);
487 (*lsmp)->lsm_maxbytes = LUSTRE_EXT3_STRIPE_MAXBYTES;
488 ostid_set_seq_echo(&(*lsmp)->lsm_oi);
493 static int echo_free_memmd(struct echo_device *ed, struct lov_stripe_md **lsmp)
499 /* If export is lov/osc then use their obd method */
500 if (ed->ed_next != NULL)
501 return obd_free_memmd(ed->ed_ec->ec_exp, lsmp);
502 /* OFD has no unpackmd method, do everything here */
503 lsm_size = lov_stripe_md_size(1);
505 LASSERT(*lsmp != NULL);
506 OBD_FREE((*lsmp)->lsm_oinfo[0], sizeof(struct lov_oinfo));
507 OBD_FREE(*lsmp, lsm_size);
512 static void echo_object_free(const struct lu_env *env, struct lu_object *obj)
514 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
515 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
518 LASSERT(atomic_read(&eco->eo_npages) == 0);
520 spin_lock(&ec->ec_lock);
521 list_del_init(&eco->eo_obj_chain);
522 spin_unlock(&ec->ec_lock);
525 lu_object_header_fini(obj->lo_header);
528 echo_free_memmd(eco->eo_dev, &eco->eo_lsm);
529 OBD_SLAB_FREE_PTR(eco, echo_object_kmem);
533 static int echo_object_print(const struct lu_env *env, void *cookie,
534 lu_printer_t p, const struct lu_object *o)
536 struct echo_object *obj = cl2echo_obj(lu2cl(o));
538 return (*p)(env, cookie, "echoclient-object@%p", obj);
541 static const struct lu_object_operations echo_lu_obj_ops = {
542 .loo_object_init = echo_object_init,
543 .loo_object_delete = NULL,
544 .loo_object_release = NULL,
545 .loo_object_free = echo_object_free,
546 .loo_object_print = echo_object_print,
547 .loo_object_invariant = NULL
549 /** @} echo_lu_ops */
551 /** \defgroup echo_lu_dev_ops lu_device operations
553 * Operations for echo lu device.
557 static struct lu_object *echo_object_alloc(const struct lu_env *env,
558 const struct lu_object_header *hdr,
559 struct lu_device *dev)
561 struct echo_object *eco;
562 struct lu_object *obj = NULL;
565 /* we're the top dev. */
566 LASSERT(hdr == NULL);
567 OBD_SLAB_ALLOC_PTR_GFP(eco, echo_object_kmem, GFP_NOFS);
569 struct cl_object_header *hdr = &eco->eo_hdr;
571 obj = &echo_obj2cl(eco)->co_lu;
572 cl_object_header_init(hdr);
573 hdr->coh_page_bufsize = cfs_size_round(sizeof(struct cl_page));
575 lu_object_init(obj, &hdr->coh_lu, dev);
576 lu_object_add_top(&hdr->coh_lu, obj);
578 eco->eo_cl.co_ops = &echo_cl_obj_ops;
579 obj->lo_ops = &echo_lu_obj_ops;
584 static struct lu_device_operations echo_device_lu_ops = {
585 .ldo_object_alloc = echo_object_alloc,
588 /** @} echo_lu_dev_ops */
590 static struct cl_device_operations echo_device_cl_ops = {
593 /** \defgroup echo_init Setup and teardown
595 * Init and fini functions for echo client.
599 static int echo_site_init(const struct lu_env *env, struct echo_device *ed)
601 struct cl_site *site = &ed->ed_site_myself;
604 /* initialize site */
605 rc = cl_site_init(site, &ed->ed_cl);
607 CERROR("Cannot initilize site for echo client(%d)\n", rc);
611 rc = lu_site_init_finish(&site->cs_lu);
619 static void echo_site_fini(const struct lu_env *env, struct echo_device *ed)
622 if (!ed->ed_next_ismd)
623 cl_site_fini(ed->ed_site);
628 static void *echo_thread_key_init(const struct lu_context *ctx,
629 struct lu_context_key *key)
631 struct echo_thread_info *info;
633 OBD_SLAB_ALLOC_PTR_GFP(info, echo_thread_kmem, GFP_NOFS);
635 info = ERR_PTR(-ENOMEM);
639 static void echo_thread_key_fini(const struct lu_context *ctx,
640 struct lu_context_key *key, void *data)
642 struct echo_thread_info *info = data;
643 OBD_SLAB_FREE_PTR(info, echo_thread_kmem);
646 static void echo_thread_key_exit(const struct lu_context *ctx,
647 struct lu_context_key *key, void *data)
651 static struct lu_context_key echo_thread_key = {
652 .lct_tags = LCT_CL_THREAD,
653 .lct_init = echo_thread_key_init,
654 .lct_fini = echo_thread_key_fini,
655 .lct_exit = echo_thread_key_exit
658 static void *echo_session_key_init(const struct lu_context *ctx,
659 struct lu_context_key *key)
661 struct echo_session_info *session;
663 OBD_SLAB_ALLOC_PTR_GFP(session, echo_session_kmem, GFP_NOFS);
665 session = ERR_PTR(-ENOMEM);
669 static void echo_session_key_fini(const struct lu_context *ctx,
670 struct lu_context_key *key, void *data)
672 struct echo_session_info *session = data;
673 OBD_SLAB_FREE_PTR(session, echo_session_kmem);
676 static void echo_session_key_exit(const struct lu_context *ctx,
677 struct lu_context_key *key, void *data)
681 static struct lu_context_key echo_session_key = {
682 .lct_tags = LCT_SESSION,
683 .lct_init = echo_session_key_init,
684 .lct_fini = echo_session_key_fini,
685 .lct_exit = echo_session_key_exit
688 LU_TYPE_INIT_FINI(echo, &echo_thread_key, &echo_session_key);
690 #ifdef HAVE_SERVER_SUPPORT
691 # define ECHO_SEQ_WIDTH 0xffffffff
692 static int echo_fid_init(struct echo_device *ed, char *obd_name,
693 struct seq_server_site *ss)
699 OBD_ALLOC_PTR(ed->ed_cl_seq);
700 if (ed->ed_cl_seq == NULL)
703 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
705 GOTO(out_free_seq, rc = -ENOMEM);
707 snprintf(prefix, MAX_OBD_NAME + 5, "srv-%s", obd_name);
709 /* Init client side sequence-manager */
710 rc = seq_client_init(ed->ed_cl_seq, NULL,
712 prefix, ss->ss_server_seq);
713 ed->ed_cl_seq->lcs_width = ECHO_SEQ_WIDTH;
714 OBD_FREE(prefix, MAX_OBD_NAME + 5);
716 GOTO(out_free_seq, rc);
721 OBD_FREE_PTR(ed->ed_cl_seq);
722 ed->ed_cl_seq = NULL;
726 static int echo_fid_fini(struct obd_device *obddev)
728 struct echo_device *ed = obd2echo_dev(obddev);
731 if (ed->ed_cl_seq != NULL) {
732 seq_client_fini(ed->ed_cl_seq);
733 OBD_FREE_PTR(ed->ed_cl_seq);
734 ed->ed_cl_seq = NULL;
739 #endif /* HAVE_SERVER_SUPPORT */
741 static struct lu_device *echo_device_alloc(const struct lu_env *env,
742 struct lu_device_type *t,
743 struct lustre_cfg *cfg)
745 struct lu_device *next;
746 struct echo_device *ed;
747 struct cl_device *cd;
748 struct obd_device *obd = NULL; /* to keep compiler happy */
749 struct obd_device *tgt;
750 const char *tgt_type_name;
757 GOTO(out, rc = -ENOMEM);
761 rc = cl_device_init(cd, t);
765 cd->cd_lu_dev.ld_ops = &echo_device_lu_ops;
766 cd->cd_ops = &echo_device_cl_ops;
769 obd = class_name2obd(lustre_cfg_string(cfg, 0));
770 LASSERT(obd != NULL);
771 LASSERT(env != NULL);
773 tgt = class_name2obd(lustre_cfg_string(cfg, 1));
775 CERROR("Can not find tgt device %s\n",
776 lustre_cfg_string(cfg, 1));
777 GOTO(out, rc = -ENODEV);
780 next = tgt->obd_lu_dev;
782 if (strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME) == 0) {
783 ed->ed_next_ismd = 1;
784 } else if (strcmp(tgt->obd_type->typ_name, LUSTRE_OST_NAME) == 0 ||
785 strcmp(tgt->obd_type->typ_name, LUSTRE_OSC_NAME) == 0) {
786 ed->ed_next_ismd = 0;
787 rc = echo_site_init(env, ed);
791 GOTO(out, rc = -EINVAL);
796 rc = echo_client_setup(env, obd, cfg);
800 ed->ed_ec = &obd->u.echo_client;
803 if (ed->ed_next_ismd) {
804 #ifdef HAVE_SERVER_SUPPORT
805 /* Suppose to connect to some Metadata layer */
807 struct lu_device *ld;
811 CERROR("%s is not lu device type!\n",
812 lustre_cfg_string(cfg, 1));
813 GOTO(out, rc = -EINVAL);
816 tgt_type_name = lustre_cfg_string(cfg, 2);
817 if (!tgt_type_name) {
818 CERROR("%s no type name for echo %s setup\n",
819 lustre_cfg_string(cfg, 1),
820 tgt->obd_type->typ_name);
821 GOTO(out, rc = -EINVAL);
826 spin_lock(&ls->ls_ld_lock);
827 list_for_each_entry(ld, &ls->ls_ld_linkage, ld_linkage) {
828 if (strcmp(ld->ld_type->ldt_name, tgt_type_name) == 0) {
833 spin_unlock(&ls->ls_ld_lock);
836 CERROR("%s is not lu device type!\n",
837 lustre_cfg_string(cfg, 1));
838 GOTO(out, rc = -EINVAL);
842 /* For MD echo client, it will use the site in MDS stack */
843 ed->ed_site_myself.cs_lu = *ls;
844 ed->ed_site = &ed->ed_site_myself;
845 ed->ed_cl.cd_lu_dev.ld_site = &ed->ed_site_myself.cs_lu;
846 rc = echo_fid_init(ed, obd->obd_name, lu_site2seq(ls));
848 CERROR("echo fid init error %d\n", rc);
851 #else /* !HAVE_SERVER_SUPPORT */
852 CERROR("Local operations are NOT supported on client side. "
853 "Only remote operations are supported. Metadata client "
854 "must be run on server side.\n");
855 GOTO(out, rc = -EOPNOTSUPP);
856 #endif /* HAVE_SERVER_SUPPORT */
858 /* if echo client is to be stacked upon ost device, the next is
859 * NULL since ost is not a clio device so far */
860 if (next != NULL && !lu_device_is_cl(next))
863 tgt_type_name = tgt->obd_type->typ_name;
865 LASSERT(next != NULL);
866 if (next->ld_site != NULL)
867 GOTO(out, rc = -EBUSY);
869 next->ld_site = &ed->ed_site->cs_lu;
870 rc = next->ld_type->ldt_ops->ldto_device_init(env, next,
871 next->ld_type->ldt_name,
876 LASSERT(strcmp(tgt_type_name, LUSTRE_OST_NAME) == 0);
880 RETURN(&cd->cd_lu_dev);
885 rc2 = echo_client_cleanup(obd);
887 CERROR("Cleanup obd device %s error(%d)\n",
892 echo_site_fini(env, ed);
894 cl_device_fini(&ed->ed_cl);
904 static int echo_device_init(const struct lu_env *env, struct lu_device *d,
905 const char *name, struct lu_device *next)
911 static struct lu_device *echo_device_fini(const struct lu_env *env,
914 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
915 struct lu_device *next = ed->ed_next;
917 while (next && !ed->ed_next_ismd)
918 next = next->ld_type->ldt_ops->ldto_device_fini(env, next);
922 static void echo_lock_release(const struct lu_env *env,
923 struct echo_lock *ecl,
926 struct cl_lock *clk = echo_lock2cl(ecl);
928 cl_lock_release(env, clk);
931 static struct lu_device *echo_device_free(const struct lu_env *env,
934 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
935 struct echo_client_obd *ec = ed->ed_ec;
936 struct echo_object *eco;
937 struct lu_device *next = ed->ed_next;
939 CDEBUG(D_INFO, "echo device:%p is going to be freed, next = %p\n",
942 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
944 /* check if there are objects still alive.
945 * It shouldn't have any object because lu_site_purge would cleanup
946 * all of cached objects. Anyway, probably the echo device is being
947 * parallelly accessed.
949 spin_lock(&ec->ec_lock);
950 list_for_each_entry(eco, &ec->ec_objects, eo_obj_chain)
952 spin_unlock(&ec->ec_lock);
955 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
958 "Waiting for the reference of echo object to be dropped\n");
960 /* Wait for the last reference to be dropped. */
961 spin_lock(&ec->ec_lock);
962 while (!list_empty(&ec->ec_objects)) {
963 spin_unlock(&ec->ec_lock);
964 CERROR("echo_client still has objects at cleanup time, "
965 "wait for 1 second\n");
966 schedule_timeout_and_set_state(TASK_UNINTERRUPTIBLE,
967 cfs_time_seconds(1));
968 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
969 spin_lock(&ec->ec_lock);
971 spin_unlock(&ec->ec_lock);
973 LASSERT(list_empty(&ec->ec_locks));
975 CDEBUG(D_INFO, "No object exists, exiting...\n");
977 echo_client_cleanup(d->ld_obd);
978 #ifdef HAVE_SERVER_SUPPORT
979 echo_fid_fini(d->ld_obd);
981 while (next && !ed->ed_next_ismd)
982 next = next->ld_type->ldt_ops->ldto_device_free(env, next);
984 LASSERT(ed->ed_site == lu2cl_site(d->ld_site));
985 echo_site_fini(env, ed);
986 cl_device_fini(&ed->ed_cl);
992 static const struct lu_device_type_operations echo_device_type_ops = {
993 .ldto_init = echo_type_init,
994 .ldto_fini = echo_type_fini,
996 .ldto_start = echo_type_start,
997 .ldto_stop = echo_type_stop,
999 .ldto_device_alloc = echo_device_alloc,
1000 .ldto_device_free = echo_device_free,
1001 .ldto_device_init = echo_device_init,
1002 .ldto_device_fini = echo_device_fini
1005 static struct lu_device_type echo_device_type = {
1006 .ldt_tags = LU_DEVICE_CL,
1007 .ldt_name = LUSTRE_ECHO_CLIENT_NAME,
1008 .ldt_ops = &echo_device_type_ops,
1009 .ldt_ctx_tags = LCT_CL_THREAD | LCT_MD_THREAD | LCT_DT_THREAD,
1013 /** \defgroup echo_exports Exported operations
1015 * exporting functions to echo client
1020 /* Interfaces to echo client obd device */
1021 static struct echo_object *cl_echo_object_find(struct echo_device *d,
1022 struct lov_stripe_md **lsmp)
1025 struct echo_thread_info *info;
1026 struct echo_object_conf *conf;
1027 struct lov_stripe_md *lsm;
1028 struct echo_object *eco;
1029 struct cl_object *obj;
1038 LASSERTF(ostid_id(&lsm->lsm_oi) != 0, DOSTID"\n", POSTID(&lsm->lsm_oi));
1039 LASSERTF(ostid_seq(&lsm->lsm_oi) == FID_SEQ_ECHO, DOSTID"\n",
1040 POSTID(&lsm->lsm_oi));
1042 /* Never return an object if the obd is to be freed. */
1043 if (echo_dev2cl(d)->cd_lu_dev.ld_obd->obd_stopping)
1044 RETURN(ERR_PTR(-ENODEV));
1046 env = cl_env_get(&refcheck);
1048 RETURN((void *)env);
1050 info = echo_env_info(env);
1051 conf = &info->eti_conf;
1053 struct lov_oinfo *oinfo = lsm->lsm_oinfo[0];
1055 LASSERT(oinfo != NULL);
1056 oinfo->loi_oi = lsm->lsm_oi;
1057 conf->eoc_cl.u.coc_oinfo = oinfo;
1059 conf->eoc_md = lsmp;
1061 fid = &info->eti_fid;
1062 rc = ostid_to_fid(fid, &lsm->lsm_oi, 0);
1064 GOTO(out, eco = ERR_PTR(rc));
1066 /* In the function below, .hs_keycmp resolves to
1067 * lu_obj_hop_keycmp() */
1068 /* coverity[overrun-buffer-val] */
1069 obj = cl_object_find(env, echo_dev2cl(d), fid, &conf->eoc_cl);
1071 GOTO(out, eco = (void*)obj);
1073 eco = cl2echo_obj(obj);
1074 if (eco->eo_deleted) {
1075 cl_object_put(env, obj);
1076 eco = ERR_PTR(-EAGAIN);
1080 cl_env_put(env, &refcheck);
1084 static int cl_echo_object_put(struct echo_object *eco)
1087 struct cl_object *obj = echo_obj2cl(eco);
1091 env = cl_env_get(&refcheck);
1093 RETURN(PTR_ERR(env));
1095 /* an external function to kill an object? */
1096 if (eco->eo_deleted) {
1097 struct lu_object_header *loh = obj->co_lu.lo_header;
1098 LASSERT(&eco->eo_hdr == luh2coh(loh));
1099 set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
1102 cl_object_put(env, obj);
1103 cl_env_put(env, &refcheck);
1107 static int cl_echo_enqueue0(struct lu_env *env, struct echo_object *eco,
1108 obd_off start, obd_off end, int mode,
1109 __u64 *cookie , __u32 enqflags)
1112 struct cl_lock *lck;
1113 struct cl_object *obj;
1114 struct cl_lock_descr *descr;
1115 struct echo_thread_info *info;
1119 info = echo_env_info(env);
1121 lck = &info->eti_lock;
1122 obj = echo_obj2cl(eco);
1124 memset(lck, 0, sizeof(*lck));
1125 descr = &lck->cll_descr;
1126 descr->cld_obj = obj;
1127 descr->cld_start = cl_index(obj, start);
1128 descr->cld_end = cl_index(obj, end);
1129 descr->cld_mode = mode == LCK_PW ? CLM_WRITE : CLM_READ;
1130 descr->cld_enq_flags = enqflags;
1133 rc = cl_lock_request(env, io, lck);
1135 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
1136 struct echo_lock *el;
1138 el = cl2echo_lock(cl_lock_at(lck, &echo_device_type));
1139 spin_lock(&ec->ec_lock);
1140 if (list_empty(&el->el_chain)) {
1141 list_add(&el->el_chain, &ec->ec_locks);
1142 el->el_cookie = ++ec->ec_unique;
1144 atomic_inc(&el->el_refcount);
1145 *cookie = el->el_cookie;
1146 spin_unlock(&ec->ec_lock);
1151 static int cl_echo_cancel0(struct lu_env *env, struct echo_device *ed,
1154 struct echo_client_obd *ec = ed->ed_ec;
1155 struct echo_lock *ecl = NULL;
1156 struct list_head *el;
1157 int found = 0, still_used = 0;
1160 LASSERT(ec != NULL);
1161 spin_lock(&ec->ec_lock);
1162 list_for_each(el, &ec->ec_locks) {
1163 ecl = list_entry(el, struct echo_lock, el_chain);
1164 CDEBUG(D_INFO, "ecl: %p, cookie: "LPX64"\n", ecl, ecl->el_cookie);
1165 found = (ecl->el_cookie == cookie);
1167 if (atomic_dec_and_test(&ecl->el_refcount))
1168 list_del_init(&ecl->el_chain);
1174 spin_unlock(&ec->ec_lock);
1179 echo_lock_release(env, ecl, still_used);
1183 static void echo_commit_callback(const struct lu_env *env, struct cl_io *io,
1184 struct cl_page *page)
1186 struct echo_thread_info *info;
1187 struct cl_2queue *queue;
1189 info = echo_env_info(env);
1190 LASSERT(io == &info->eti_io);
1192 queue = &info->eti_queue;
1193 cl_page_list_add(&queue->c2_qout, page);
1196 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
1197 struct page **pages, int npages, int async)
1200 struct echo_thread_info *info;
1201 struct cl_object *obj = echo_obj2cl(eco);
1202 struct echo_device *ed = eco->eo_dev;
1203 struct cl_2queue *queue;
1205 struct cl_page *clp;
1206 struct lustre_handle lh = { 0 };
1207 int page_size = cl_page_size(obj);
1213 LASSERT((offset & ~CFS_PAGE_MASK) == 0);
1214 LASSERT(ed->ed_next != NULL);
1215 env = cl_env_get(&refcheck);
1217 RETURN(PTR_ERR(env));
1219 info = echo_env_info(env);
1221 queue = &info->eti_queue;
1223 cl_2queue_init(queue);
1225 io->ci_ignore_layout = 1;
1226 rc = cl_io_init(env, io, CIT_MISC, obj);
1232 rc = cl_echo_enqueue0(env, eco, offset,
1233 offset + npages * PAGE_CACHE_SIZE - 1,
1234 rw == READ ? LCK_PR : LCK_PW, &lh.cookie,
1237 GOTO(error_lock, rc);
1239 for (i = 0; i < npages; i++) {
1241 clp = cl_page_find(env, obj, cl_index(obj, offset),
1242 pages[i], CPT_TRANSIENT);
1247 LASSERT(clp->cp_type == CPT_TRANSIENT);
1249 rc = cl_page_own(env, io, clp);
1251 LASSERT(clp->cp_state == CPS_FREEING);
1252 cl_page_put(env, clp);
1256 cl_2queue_add(queue, clp);
1258 /* drop the reference count for cl_page_find, so that the page
1259 * will be freed in cl_2queue_fini. */
1260 cl_page_put(env, clp);
1261 cl_page_clip(env, clp, 0, page_size);
1263 offset += page_size;
1267 enum cl_req_type typ = rw == READ ? CRT_READ : CRT_WRITE;
1269 async = async && (typ == CRT_WRITE);
1271 rc = cl_io_commit_async(env, io, &queue->c2_qin,
1273 echo_commit_callback);
1275 rc = cl_io_submit_sync(env, io, typ, queue, 0);
1276 CDEBUG(D_INFO, "echo_client %s write returns %d\n",
1277 async ? "async" : "sync", rc);
1280 cl_echo_cancel0(env, ed, lh.cookie);
1283 cl_2queue_discard(env, io, queue);
1284 cl_2queue_disown(env, io, queue);
1285 cl_2queue_fini(env, queue);
1286 cl_io_fini(env, io);
1288 cl_env_put(env, &refcheck);
1291 /** @} echo_exports */
1294 static obd_id last_object_id;
1296 #ifdef HAVE_SERVER_SUPPORT
1297 static inline void echo_md_build_name(struct lu_name *lname, char *name,
1300 sprintf(name, LPU64, id);
1301 lname->ln_name = name;
1302 lname->ln_namelen = strlen(name);
1305 /* similar to mdt_attr_get_complex */
1306 static int echo_big_lmm_get(const struct lu_env *env, struct md_object *o,
1309 struct echo_thread_info *info = echo_env_info(env);
1314 LASSERT(ma->ma_lmm_size > 0);
1316 rc = mo_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LOV);
1320 /* big_lmm may need to be grown */
1321 if (info->eti_big_lmmsize < rc) {
1322 int size = size_roundup_power2(rc);
1324 if (info->eti_big_lmmsize > 0) {
1325 /* free old buffer */
1326 LASSERT(info->eti_big_lmm);
1327 OBD_FREE_LARGE(info->eti_big_lmm,
1328 info->eti_big_lmmsize);
1329 info->eti_big_lmm = NULL;
1330 info->eti_big_lmmsize = 0;
1333 OBD_ALLOC_LARGE(info->eti_big_lmm, size);
1334 if (info->eti_big_lmm == NULL)
1336 info->eti_big_lmmsize = size;
1338 LASSERT(info->eti_big_lmmsize >= rc);
1340 info->eti_buf.lb_buf = info->eti_big_lmm;
1341 info->eti_buf.lb_len = info->eti_big_lmmsize;
1342 rc = mo_xattr_get(env, o, &info->eti_buf, XATTR_NAME_LOV);
1346 ma->ma_valid |= MA_LOV;
1347 ma->ma_lmm = info->eti_big_lmm;
1348 ma->ma_lmm_size = rc;
1353 static int echo_attr_get_complex(const struct lu_env *env,
1354 struct md_object *next,
1357 struct echo_thread_info *info = echo_env_info(env);
1358 struct lu_buf *buf = &info->eti_buf;
1359 umode_t mode = lu_object_attr(&next->mo_lu);
1360 int need = ma->ma_need;
1367 if (need & MA_INODE) {
1368 ma->ma_need = MA_INODE;
1369 rc = mo_attr_get(env, next, ma);
1372 ma->ma_valid |= MA_INODE;
1375 if (need & MA_LOV) {
1376 if (S_ISREG(mode) || S_ISDIR(mode)) {
1377 LASSERT(ma->ma_lmm_size > 0);
1378 buf->lb_buf = ma->ma_lmm;
1379 buf->lb_len = ma->ma_lmm_size;
1380 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LOV);
1382 ma->ma_lmm_size = rc2;
1383 ma->ma_valid |= MA_LOV;
1384 } else if (rc2 == -ENODATA) {
1386 ma->ma_lmm_size = 0;
1387 } else if (rc2 == -ERANGE) {
1388 rc2 = echo_big_lmm_get(env, next, ma);
1390 GOTO(out, rc = rc2);
1392 GOTO(out, rc = rc2);
1397 #ifdef CONFIG_FS_POSIX_ACL
1398 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
1399 buf->lb_buf = ma->ma_acl;
1400 buf->lb_len = ma->ma_acl_size;
1401 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1403 ma->ma_acl_size = rc2;
1404 ma->ma_valid |= MA_ACL_DEF;
1405 } else if (rc2 == -ENODATA) {
1407 ma->ma_acl_size = 0;
1409 GOTO(out, rc = rc2);
1415 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
1416 rc, ma->ma_valid, ma->ma_lmm);
1421 echo_md_create_internal(const struct lu_env *env, struct echo_device *ed,
1422 struct md_object *parent, struct lu_fid *fid,
1423 struct lu_name *lname, struct md_op_spec *spec,
1426 struct lu_object *ec_child, *child;
1427 struct lu_device *ld = ed->ed_next;
1428 struct echo_thread_info *info = echo_env_info(env);
1429 struct lu_fid *fid2 = &info->eti_fid2;
1430 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
1435 rc = mdo_lookup(env, parent, lname, fid2, spec);
1438 else if (rc != -ENOENT)
1441 ec_child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev,
1443 if (IS_ERR(ec_child)) {
1444 CERROR("Can not find the child "DFID": rc = %ld\n", PFID(fid),
1446 RETURN(PTR_ERR(ec_child));
1449 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1450 if (child == NULL) {
1451 CERROR("Can not locate the child "DFID"\n", PFID(fid));
1452 GOTO(out_put, rc = -EINVAL);
1455 CDEBUG(D_RPCTRACE, "Start creating object "DFID" %s %p\n",
1456 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1459 * Do not perform lookup sanity check. We know that name does not exist.
1461 spec->sp_cr_lookup = 0;
1462 rc = mdo_create(env, parent, lname, lu2md(child), spec, ma);
1464 CERROR("Can not create child "DFID": rc = %d\n", PFID(fid), rc);
1467 CDEBUG(D_RPCTRACE, "End creating object "DFID" %s %p rc = %d\n",
1468 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent, rc);
1471 lu_object_put(env, ec_child);
1475 static int echo_set_lmm_size(const struct lu_env *env, struct lu_device *ld,
1478 struct echo_thread_info *info = echo_env_info(env);
1480 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
1481 ma->ma_lmm = (void *)&info->eti_lmm;
1482 ma->ma_lmm_size = sizeof(info->eti_lmm);
1484 LASSERT(info->eti_big_lmmsize);
1485 ma->ma_lmm = info->eti_big_lmm;
1486 ma->ma_lmm_size = info->eti_big_lmmsize;
1492 static int echo_create_md_object(const struct lu_env *env,
1493 struct echo_device *ed,
1494 struct lu_object *ec_parent,
1496 char *name, int namelen,
1497 __u64 id, __u32 mode, int count,
1498 int stripe_count, int stripe_offset)
1500 struct lu_object *parent;
1501 struct echo_thread_info *info = echo_env_info(env);
1502 struct lu_name *lname = &info->eti_lname;
1503 struct md_op_spec *spec = &info->eti_spec;
1504 struct md_attr *ma = &info->eti_ma;
1505 struct lu_device *ld = ed->ed_next;
1511 if (ec_parent == NULL)
1513 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1517 memset(ma, 0, sizeof(*ma));
1518 memset(spec, 0, sizeof(*spec));
1519 if (stripe_count != 0) {
1520 spec->sp_cr_flags |= FMODE_WRITE;
1521 echo_set_lmm_size(env, ld, ma);
1522 if (stripe_count != -1) {
1523 struct lov_user_md_v3 *lum = &info->eti_lum;
1525 lum->lmm_magic = LOV_USER_MAGIC_V3;
1526 lum->lmm_stripe_count = stripe_count;
1527 lum->lmm_stripe_offset = stripe_offset;
1528 lum->lmm_pattern = 0;
1529 spec->u.sp_ea.eadata = lum;
1530 spec->u.sp_ea.eadatalen = sizeof(*lum);
1531 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1535 ma->ma_attr.la_mode = mode;
1536 ma->ma_attr.la_valid = LA_CTIME | LA_MODE;
1537 ma->ma_attr.la_ctime = cfs_time_current_64();
1540 lname->ln_name = name;
1541 lname->ln_namelen = namelen;
1542 /* If name is specified, only create one object by name */
1543 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1548 /* Create multiple object sequenced by id */
1549 for (i = 0; i < count; i++) {
1550 char *tmp_name = info->eti_name;
1552 echo_md_build_name(lname, tmp_name, id);
1554 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1557 CERROR("Can not create child %s: rc = %d\n", tmp_name,
1568 static struct lu_object *echo_md_lookup(const struct lu_env *env,
1569 struct echo_device *ed,
1570 struct md_object *parent,
1571 struct lu_name *lname)
1573 struct echo_thread_info *info = echo_env_info(env);
1574 struct lu_fid *fid = &info->eti_fid;
1575 struct lu_object *child;
1579 CDEBUG(D_INFO, "lookup %s in parent "DFID" %p\n", lname->ln_name,
1581 rc = mdo_lookup(env, parent, lname, fid, NULL);
1583 CERROR("lookup %s: rc = %d\n", lname->ln_name, rc);
1584 RETURN(ERR_PTR(rc));
1587 /* In the function below, .hs_keycmp resolves to
1588 * lu_obj_hop_keycmp() */
1589 /* coverity[overrun-buffer-val] */
1590 child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1595 static int echo_setattr_object(const struct lu_env *env,
1596 struct echo_device *ed,
1597 struct lu_object *ec_parent,
1598 __u64 id, int count)
1600 struct lu_object *parent;
1601 struct echo_thread_info *info = echo_env_info(env);
1602 struct lu_name *lname = &info->eti_lname;
1603 char *name = info->eti_name;
1604 struct lu_device *ld = ed->ed_next;
1605 struct lu_buf *buf = &info->eti_buf;
1611 if (ec_parent == NULL)
1613 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1617 for (i = 0; i < count; i++) {
1618 struct lu_object *ec_child, *child;
1620 echo_md_build_name(lname, name, id);
1622 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1623 if (IS_ERR(ec_child)) {
1624 CERROR("Can't find child %s: rc = %ld\n",
1625 lname->ln_name, PTR_ERR(ec_child));
1626 RETURN(PTR_ERR(ec_child));
1629 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1630 if (child == NULL) {
1631 CERROR("Can not locate the child %s\n", lname->ln_name);
1632 lu_object_put(env, ec_child);
1637 CDEBUG(D_RPCTRACE, "Start setattr object "DFID"\n",
1638 PFID(lu_object_fid(child)));
1640 buf->lb_buf = info->eti_xattr_buf;
1641 buf->lb_len = sizeof(info->eti_xattr_buf);
1643 sprintf(name, "%s.test1", XATTR_USER_PREFIX);
1644 rc = mo_xattr_set(env, lu2md(child), buf, name,
1647 CERROR("Can not setattr child "DFID": rc = %d\n",
1648 PFID(lu_object_fid(child)), rc);
1649 lu_object_put(env, ec_child);
1652 CDEBUG(D_RPCTRACE, "End setattr object "DFID"\n",
1653 PFID(lu_object_fid(child)));
1655 lu_object_put(env, ec_child);
1660 static int echo_getattr_object(const struct lu_env *env,
1661 struct echo_device *ed,
1662 struct lu_object *ec_parent,
1663 __u64 id, int count)
1665 struct lu_object *parent;
1666 struct echo_thread_info *info = echo_env_info(env);
1667 struct lu_name *lname = &info->eti_lname;
1668 char *name = info->eti_name;
1669 struct md_attr *ma = &info->eti_ma;
1670 struct lu_device *ld = ed->ed_next;
1676 if (ec_parent == NULL)
1678 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1682 memset(ma, 0, sizeof(*ma));
1683 ma->ma_need |= MA_INODE | MA_LOV | MA_PFID | MA_HSM | MA_ACL_DEF;
1684 ma->ma_acl = info->eti_xattr_buf;
1685 ma->ma_acl_size = sizeof(info->eti_xattr_buf);
1687 for (i = 0; i < count; i++) {
1688 struct lu_object *ec_child, *child;
1691 echo_md_build_name(lname, name, id);
1692 echo_set_lmm_size(env, ld, ma);
1694 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1695 if (IS_ERR(ec_child)) {
1696 CERROR("Can't find child %s: rc = %ld\n",
1697 lname->ln_name, PTR_ERR(ec_child));
1698 RETURN(PTR_ERR(ec_child));
1701 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1702 if (child == NULL) {
1703 CERROR("Can not locate the child %s\n", lname->ln_name);
1704 lu_object_put(env, ec_child);
1708 CDEBUG(D_RPCTRACE, "Start getattr object "DFID"\n",
1709 PFID(lu_object_fid(child)));
1710 rc = echo_attr_get_complex(env, lu2md(child), ma);
1712 CERROR("Can not getattr child "DFID": rc = %d\n",
1713 PFID(lu_object_fid(child)), rc);
1714 lu_object_put(env, ec_child);
1717 CDEBUG(D_RPCTRACE, "End getattr object "DFID"\n",
1718 PFID(lu_object_fid(child)));
1720 lu_object_put(env, ec_child);
1726 static int echo_lookup_object(const struct lu_env *env,
1727 struct echo_device *ed,
1728 struct lu_object *ec_parent,
1729 __u64 id, int count)
1731 struct lu_object *parent;
1732 struct echo_thread_info *info = echo_env_info(env);
1733 struct lu_name *lname = &info->eti_lname;
1734 char *name = info->eti_name;
1735 struct lu_fid *fid = &info->eti_fid;
1736 struct lu_device *ld = ed->ed_next;
1740 if (ec_parent == NULL)
1742 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1746 /*prepare the requests*/
1747 for (i = 0; i < count; i++) {
1748 echo_md_build_name(lname, name, id);
1750 CDEBUG(D_RPCTRACE, "Start lookup object "DFID" %s %p\n",
1751 PFID(lu_object_fid(parent)), lname->ln_name, parent);
1753 rc = mdo_lookup(env, lu2md(parent), lname, fid, NULL);
1755 CERROR("Can not lookup child %s: rc = %d\n", name, rc);
1758 CDEBUG(D_RPCTRACE, "End lookup object "DFID" %s %p\n",
1759 PFID(lu_object_fid(parent)), lname->ln_name, parent);
1766 static int echo_md_destroy_internal(const struct lu_env *env,
1767 struct echo_device *ed,
1768 struct md_object *parent,
1769 struct lu_name *lname,
1772 struct lu_device *ld = ed->ed_next;
1773 struct lu_object *ec_child;
1774 struct lu_object *child;
1779 ec_child = echo_md_lookup(env, ed, parent, lname);
1780 if (IS_ERR(ec_child)) {
1781 CERROR("Can't find child %s: rc = %ld\n", lname->ln_name,
1783 RETURN(PTR_ERR(ec_child));
1786 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1787 if (child == NULL) {
1788 CERROR("Can not locate the child %s\n", lname->ln_name);
1789 GOTO(out_put, rc = -EINVAL);
1792 if (lu_object_remote(child)) {
1793 CERROR("Can not destroy remote object %s: rc = %d\n",
1794 lname->ln_name, -EPERM);
1795 GOTO(out_put, rc = -EPERM);
1797 CDEBUG(D_RPCTRACE, "Start destroy object "DFID" %s %p\n",
1798 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1800 rc = mdo_unlink(env, parent, lu2md(child), lname, ma, 0);
1802 CERROR("Can not unlink child %s: rc = %d\n",
1803 lname->ln_name, rc);
1806 CDEBUG(D_RPCTRACE, "End destroy object "DFID" %s %p\n",
1807 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1809 lu_object_put(env, ec_child);
1813 static int echo_destroy_object(const struct lu_env *env,
1814 struct echo_device *ed,
1815 struct lu_object *ec_parent,
1816 char *name, int namelen,
1817 __u64 id, __u32 mode,
1820 struct echo_thread_info *info = echo_env_info(env);
1821 struct lu_name *lname = &info->eti_lname;
1822 struct md_attr *ma = &info->eti_ma;
1823 struct lu_device *ld = ed->ed_next;
1824 struct lu_object *parent;
1829 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1833 memset(ma, 0, sizeof(*ma));
1834 ma->ma_attr.la_mode = mode;
1835 ma->ma_attr.la_valid = LA_CTIME;
1836 ma->ma_attr.la_ctime = cfs_time_current_64();
1837 ma->ma_need = MA_INODE;
1841 lname->ln_name = name;
1842 lname->ln_namelen = namelen;
1843 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
1848 /*prepare the requests*/
1849 for (i = 0; i < count; i++) {
1850 char *tmp_name = info->eti_name;
1853 echo_md_build_name(lname, tmp_name, id);
1855 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
1858 CERROR("Can not unlink child %s: rc = %d\n", name, rc);
1867 static struct lu_object *echo_resolve_path(const struct lu_env *env,
1868 struct echo_device *ed, char *path,
1871 struct lu_device *ld = ed->ed_next;
1872 struct md_device *md = lu2md_dev(ld);
1873 struct echo_thread_info *info = echo_env_info(env);
1874 struct lu_fid *fid = &info->eti_fid;
1875 struct lu_name *lname = &info->eti_lname;
1876 struct lu_object *parent = NULL;
1877 struct lu_object *child = NULL;
1881 /*Only support MDD layer right now*/
1882 rc = md->md_ops->mdo_root_get(env, md, fid);
1884 CERROR("get root error: rc = %d\n", rc);
1885 RETURN(ERR_PTR(rc));
1888 /* In the function below, .hs_keycmp resolves to
1889 * lu_obj_hop_keycmp() */
1890 /* coverity[overrun-buffer-val] */
1891 parent = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1892 if (IS_ERR(parent)) {
1893 CERROR("Can not find the parent "DFID": rc = %ld\n",
1894 PFID(fid), PTR_ERR(parent));
1899 struct lu_object *ld_parent;
1902 e = strsep(&path, "/");
1907 if (!path || path[0] == '\0')
1913 lname->ln_namelen = strlen(e);
1915 ld_parent = lu_object_locate(parent->lo_header, ld->ld_type);
1916 if (ld_parent == NULL) {
1917 lu_object_put(env, parent);
1922 child = echo_md_lookup(env, ed, lu2md(ld_parent), lname);
1923 lu_object_put(env, parent);
1924 if (IS_ERR(child)) {
1925 rc = (int)PTR_ERR(child);
1926 CERROR("lookup %s under parent "DFID": rc = %d\n",
1927 lname->ln_name, PFID(lu_object_fid(ld_parent)),
1934 RETURN(ERR_PTR(rc));
1939 static void echo_ucred_init(struct lu_env *env)
1941 struct lu_ucred *ucred = lu_ucred(env);
1943 ucred->uc_valid = UCRED_INVALID;
1945 ucred->uc_suppgids[0] = -1;
1946 ucred->uc_suppgids[1] = -1;
1948 ucred->uc_uid = ucred->uc_o_uid =
1949 from_kuid(&init_user_ns, current_uid());
1950 ucred->uc_gid = ucred->uc_o_gid =
1951 from_kgid(&init_user_ns, current_gid());
1952 ucred->uc_fsuid = ucred->uc_o_fsuid =
1953 from_kuid(&init_user_ns, current_fsuid());
1954 ucred->uc_fsgid = ucred->uc_o_fsgid =
1955 from_kgid(&init_user_ns, current_fsgid());
1956 ucred->uc_cap = cfs_curproc_cap_pack();
1958 /* remove fs privilege for non-root user. */
1959 if (ucred->uc_fsuid)
1960 ucred->uc_cap &= ~CFS_CAP_FS_MASK;
1961 ucred->uc_valid = UCRED_NEW;
1964 static void echo_ucred_fini(struct lu_env *env)
1966 struct lu_ucred *ucred = lu_ucred(env);
1967 ucred->uc_valid = UCRED_INIT;
1970 #define ECHO_MD_CTX_TAG (LCT_REMEMBER | LCT_MD_THREAD)
1971 #define ECHO_MD_SES_TAG (LCT_REMEMBER | LCT_SESSION | LCT_SERVER_SESSION)
1972 static int echo_md_handler(struct echo_device *ed, int command,
1973 char *path, int path_len, __u64 id, int count,
1974 struct obd_ioctl_data *data)
1976 struct echo_thread_info *info;
1977 struct lu_device *ld = ed->ed_next;
1980 struct lu_object *parent;
1982 int namelen = data->ioc_plen2;
1987 CERROR("MD echo client is not being initialized properly\n");
1991 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
1992 CERROR("Only support MDD layer right now!\n");
1996 env = cl_env_get(&refcheck);
1998 RETURN(PTR_ERR(env));
2000 rc = lu_env_refill_by_tags(env, ECHO_MD_CTX_TAG, ECHO_MD_SES_TAG);
2004 /* init big_lmm buffer */
2005 info = echo_env_info(env);
2006 LASSERT(info->eti_big_lmm == NULL);
2007 OBD_ALLOC_LARGE(info->eti_big_lmm, MIN_MD_SIZE);
2008 if (info->eti_big_lmm == NULL)
2009 GOTO(out_env, rc = -ENOMEM);
2010 info->eti_big_lmmsize = MIN_MD_SIZE;
2012 parent = echo_resolve_path(env, ed, path, path_len);
2013 if (IS_ERR(parent)) {
2014 CERROR("Can not resolve the path %s: rc = %ld\n", path,
2016 GOTO(out_free, rc = PTR_ERR(parent));
2020 OBD_ALLOC(name, namelen + 1);
2022 GOTO(out_put, rc = -ENOMEM);
2023 if (copy_from_user(name, data->ioc_pbuf2, namelen))
2024 GOTO(out_name, rc = -EFAULT);
2027 echo_ucred_init(env);
2030 case ECHO_MD_CREATE:
2031 case ECHO_MD_MKDIR: {
2032 struct echo_thread_info *info = echo_env_info(env);
2033 __u32 mode = data->ioc_obdo2.o_mode;
2034 struct lu_fid *fid = &info->eti_fid;
2035 int stripe_count = (int)data->ioc_obdo2.o_misc;
2036 int stripe_index = (int)data->ioc_obdo2.o_stripe_idx;
2038 rc = ostid_to_fid(fid, &data->ioc_obdo1.o_oi, 0);
2042 /* In the function below, .hs_keycmp resolves to
2043 * lu_obj_hop_keycmp() */
2044 /* coverity[overrun-buffer-val] */
2045 rc = echo_create_md_object(env, ed, parent, fid, name, namelen,
2046 id, mode, count, stripe_count,
2050 case ECHO_MD_DESTROY:
2051 case ECHO_MD_RMDIR: {
2052 __u32 mode = data->ioc_obdo2.o_mode;
2054 rc = echo_destroy_object(env, ed, parent, name, namelen,
2058 case ECHO_MD_LOOKUP:
2059 rc = echo_lookup_object(env, ed, parent, id, count);
2061 case ECHO_MD_GETATTR:
2062 rc = echo_getattr_object(env, ed, parent, id, count);
2064 case ECHO_MD_SETATTR:
2065 rc = echo_setattr_object(env, ed, parent, id, count);
2068 CERROR("unknown command %d\n", command);
2072 echo_ucred_fini(env);
2076 OBD_FREE(name, namelen + 1);
2078 lu_object_put(env, parent);
2080 LASSERT(info->eti_big_lmm);
2081 OBD_FREE_LARGE(info->eti_big_lmm, info->eti_big_lmmsize);
2082 info->eti_big_lmm = NULL;
2083 info->eti_big_lmmsize = 0;
2085 cl_env_put(env, &refcheck);
2088 #endif /* HAVE_SERVER_SUPPORT */
2090 static int echo_create_object(const struct lu_env *env, struct echo_device *ed,
2091 struct obdo *oa, struct obd_trans_info *oti)
2093 struct echo_object *eco;
2094 struct echo_client_obd *ec = ed->ed_ec;
2095 struct lov_stripe_md *lsm = NULL;
2100 if ((oa->o_valid & OBD_MD_FLID) == 0) { /* no obj id */
2101 CERROR ("No valid oid\n");
2105 rc = echo_alloc_memmd(ed, &lsm);
2107 CERROR("Cannot allocate md: rc = %d\n", rc);
2111 /* setup object ID here */
2112 if (oa->o_valid & OBD_MD_FLID) {
2113 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2114 lsm->lsm_oi = oa->o_oi;
2117 if (ostid_id(&lsm->lsm_oi) == 0)
2118 ostid_set_id(&lsm->lsm_oi, ++last_object_id);
2120 /* Only echo objects are allowed to be created */
2121 LASSERT((oa->o_valid & OBD_MD_FLGROUP) &&
2122 (ostid_seq(&oa->o_oi) == FID_SEQ_ECHO));
2124 rc = obd_create(env, ec->ec_exp, oa, &lsm, oti);
2126 CERROR("Cannot create objects: rc = %d\n", rc);
2132 /* See what object ID we were given */
2133 oa->o_oi = lsm->lsm_oi;
2134 oa->o_valid |= OBD_MD_FLID;
2136 eco = cl_echo_object_find(ed, &lsm);
2138 GOTO(failed, rc = PTR_ERR(eco));
2139 cl_echo_object_put(eco);
2141 CDEBUG(D_INFO, "oa oid "DOSTID"\n", POSTID(&oa->o_oi));
2146 obd_destroy(env, ec->ec_exp, oa, lsm, oti, NULL, NULL);
2148 echo_free_memmd(ed, &lsm);
2150 CERROR("create object failed with: rc = %d\n", rc);
2154 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
2157 struct lov_stripe_md *lsm = NULL;
2158 struct echo_object *eco;
2162 if ((oa->o_valid & OBD_MD_FLID) == 0 || ostid_id(&oa->o_oi) == 0) {
2163 /* disallow use of object id 0 */
2164 CERROR ("No valid oid\n");
2168 rc = echo_alloc_memmd(ed, &lsm);
2172 lsm->lsm_oi = oa->o_oi;
2173 if (!(oa->o_valid & OBD_MD_FLGROUP))
2174 ostid_set_seq_echo(&lsm->lsm_oi);
2177 eco = cl_echo_object_find(ed, &lsm);
2183 echo_free_memmd(ed, &lsm);
2187 static void echo_put_object(struct echo_object *eco)
2191 rc = cl_echo_object_put(eco);
2193 CERROR("%s: echo client drop an object failed: rc = %d\n",
2194 eco->eo_dev->ed_ec->ec_exp->exp_obd->obd_name, rc);
2198 echo_client_page_debug_setup(struct page *page, int rw, obd_id id,
2199 obd_off offset, obd_off count)
2206 /* no partial pages on the client */
2207 LASSERT(count == PAGE_CACHE_SIZE);
2211 for (delta = 0; delta < PAGE_CACHE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2212 if (rw == OBD_BRW_WRITE) {
2213 stripe_off = offset + delta;
2216 stripe_off = 0xdeadbeef00c0ffeeULL;
2217 stripe_id = 0xdeadbeef00c0ffeeULL;
2219 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
2220 stripe_off, stripe_id);
2227 echo_client_page_debug_check(struct page *page, obd_id id, obd_off offset,
2237 /* no partial pages on the client */
2238 LASSERT(count == PAGE_CACHE_SIZE);
2242 for (rc = delta = 0; delta < PAGE_CACHE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2243 stripe_off = offset + delta;
2246 rc2 = block_debug_check("test_brw",
2247 addr + delta, OBD_ECHO_BLOCK_SIZE,
2248 stripe_off, stripe_id);
2250 CERROR ("Error in echo object "LPX64"\n", id);
2259 static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
2260 struct echo_object *eco, obd_off offset,
2261 obd_size count, int async,
2262 struct obd_trans_info *oti)
2265 struct brw_page *pga;
2266 struct brw_page *pgp;
2267 struct page **pages;
2276 verify = (ostid_id(&oa->o_oi) != ECHO_PERSISTENT_OBJID &&
2277 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
2278 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
2280 gfp_mask = ((ostid_id(&oa->o_oi) & 2) == 0) ? GFP_IOFS : GFP_HIGHUSER;
2282 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
2285 (count & (~CFS_PAGE_MASK)) != 0)
2288 /* XXX think again with misaligned I/O */
2289 npages = count >> PAGE_CACHE_SHIFT;
2291 if (rw == OBD_BRW_WRITE)
2292 brw_flags = OBD_BRW_ASYNC;
2294 OBD_ALLOC(pga, npages * sizeof(*pga));
2298 OBD_ALLOC(pages, npages * sizeof(*pages));
2299 if (pages == NULL) {
2300 OBD_FREE(pga, npages * sizeof(*pga));
2304 for (i = 0, pgp = pga, off = offset;
2306 i++, pgp++, off += PAGE_CACHE_SIZE) {
2308 LASSERT (pgp->pg == NULL); /* for cleanup */
2311 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
2312 if (pgp->pg == NULL)
2316 pgp->count = PAGE_CACHE_SIZE;
2318 pgp->flag = brw_flags;
2321 echo_client_page_debug_setup(pgp->pg, rw,
2322 ostid_id(&oa->o_oi), off,
2326 /* brw mode can only be used at client */
2327 LASSERT(ed->ed_next != NULL);
2328 rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
2331 if (rc != 0 || rw != OBD_BRW_READ)
2334 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
2335 if (pgp->pg == NULL)
2340 vrc = echo_client_page_debug_check(pgp->pg,
2341 ostid_id(&oa->o_oi),
2342 pgp->off, pgp->count);
2343 if (vrc != 0 && rc == 0)
2346 OBD_PAGE_FREE(pgp->pg);
2348 OBD_FREE(pga, npages * sizeof(*pga));
2349 OBD_FREE(pages, npages * sizeof(*pages));
2353 static int echo_client_prep_commit(const struct lu_env *env,
2354 struct obd_export *exp, int rw,
2355 struct obdo *oa, struct echo_object *eco,
2356 obd_off offset, obd_size count,
2357 obd_size batch, struct obd_trans_info *oti,
2360 struct obd_ioobj ioo;
2361 struct niobuf_local *lnb;
2362 struct niobuf_remote *rnb;
2364 obd_size npages, tot_pages;
2365 int i, ret = 0, brw_flags = 0;
2369 if (count <= 0 || (count & ~PAGE_CACHE_MASK) != 0)
2372 npages = batch >> PAGE_CACHE_SHIFT;
2373 tot_pages = count >> PAGE_CACHE_SHIFT;
2375 OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
2376 OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
2378 if (lnb == NULL || rnb == NULL)
2379 GOTO(out, ret = -ENOMEM);
2381 if (rw == OBD_BRW_WRITE && async)
2382 brw_flags |= OBD_BRW_ASYNC;
2384 obdo_to_ioobj(oa, &ioo);
2388 for(; tot_pages; tot_pages -= npages) {
2391 if (tot_pages < npages)
2394 for (i = 0; i < npages; i++, off += PAGE_CACHE_SIZE) {
2395 rnb[i].rnb_offset = off;
2396 rnb[i].rnb_len = PAGE_CACHE_SIZE;
2397 rnb[i].rnb_flags = brw_flags;
2400 ioo.ioo_bufcnt = npages;
2403 ret = obd_preprw(env, rw, exp, oa, 1, &ioo, rnb, &lpages,
2407 LASSERT(lpages == npages);
2409 for (i = 0; i < lpages; i++) {
2410 struct page *page = lnb[i].lnb_page;
2412 /* read past eof? */
2413 if (page == NULL && lnb[i].lnb_rc == 0)
2417 lnb[i].lnb_flags |= OBD_BRW_ASYNC;
2419 if (ostid_id(&oa->o_oi) == ECHO_PERSISTENT_OBJID ||
2420 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
2421 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
2424 if (rw == OBD_BRW_WRITE)
2425 echo_client_page_debug_setup(page, rw,
2426 ostid_id(&oa->o_oi),
2430 echo_client_page_debug_check(page,
2431 ostid_id(&oa->o_oi),
2436 ret = obd_commitrw(env, rw, exp, oa, 1, &ioo,
2437 rnb, npages, lnb, oti, ret);
2441 /* Reset oti otherwise it would confuse ldiskfs. */
2442 memset(oti, 0, sizeof(*oti));
2444 /* Reuse env context. */
2445 lu_context_exit((struct lu_context *)&env->le_ctx);
2446 lu_context_enter((struct lu_context *)&env->le_ctx);
2451 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
2453 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
2457 static int echo_client_brw_ioctl(const struct lu_env *env, int rw,
2458 struct obd_export *exp,
2459 struct obd_ioctl_data *data,
2460 struct obd_trans_info *dummy_oti)
2462 struct obd_device *obd = class_exp2obd(exp);
2463 struct echo_device *ed = obd2echo_dev(obd);
2464 struct echo_client_obd *ec = ed->ed_ec;
2465 struct obdo *oa = &data->ioc_obdo1;
2466 struct echo_object *eco;
2472 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2474 rc = echo_get_object(&eco, ed, oa);
2478 oa->o_valid &= ~OBD_MD_FLHANDLE;
2480 /* OFD/obdfilter works only via prep/commit */
2481 test_mode = (long)data->ioc_pbuf1;
2482 if (ed->ed_next == NULL && test_mode != 3) {
2484 data->ioc_plen1 = data->ioc_count;
2490 /* Truncate batch size to maximum */
2491 if (data->ioc_plen1 > PTLRPC_MAX_BRW_SIZE)
2492 data->ioc_plen1 = PTLRPC_MAX_BRW_SIZE;
2494 switch (test_mode) {
2498 rc = echo_client_kbrw(ed, rw, oa,
2499 eco, data->ioc_offset,
2500 data->ioc_count, async, dummy_oti);
2503 rc = echo_client_prep_commit(env, ec->ec_exp, rw, oa,
2504 eco, data->ioc_offset,
2505 data->ioc_count, data->ioc_plen1,
2511 echo_put_object(eco);
2516 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2517 void *karg, void *uarg)
2519 #ifdef HAVE_SERVER_SUPPORT
2520 struct tgt_session_info *tsi;
2522 struct obd_device *obd = exp->exp_obd;
2523 struct echo_device *ed = obd2echo_dev(obd);
2524 struct echo_client_obd *ec = ed->ed_ec;
2525 struct echo_object *eco;
2526 struct obd_ioctl_data *data = karg;
2527 struct obd_trans_info dummy_oti;
2529 struct oti_req_ack_lock *ack_lock;
2532 int rw = OBD_BRW_READ;
2535 #ifdef HAVE_SERVER_SUPPORT
2536 struct lu_context echo_session;
2540 memset(&dummy_oti, 0, sizeof(dummy_oti));
2542 oa = &data->ioc_obdo1;
2543 if (!(oa->o_valid & OBD_MD_FLGROUP)) {
2544 oa->o_valid |= OBD_MD_FLGROUP;
2545 ostid_set_seq_echo(&oa->o_oi);
2548 /* This FID is unpacked just for validation at this point */
2549 rc = ostid_to_fid(&fid, &oa->o_oi, 0);
2557 rc = lu_env_init(env, LCT_DT_THREAD);
2559 GOTO(out_alloc, rc = -ENOMEM);
2561 #ifdef HAVE_SERVER_SUPPORT
2562 env->le_ses = &echo_session;
2563 rc = lu_context_init(env->le_ses, LCT_SERVER_SESSION | LCT_NOREF);
2564 if (unlikely(rc < 0))
2566 lu_context_enter(env->le_ses);
2568 tsi = tgt_ses_info(env);
2569 tsi->tsi_exp = ec->ec_exp;
2570 tsi->tsi_jobid = NULL;
2573 case OBD_IOC_CREATE: /* may create echo object */
2574 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2575 GOTO (out, rc = -EPERM);
2577 rc = echo_create_object(env, ed, oa, &dummy_oti);
2580 #ifdef HAVE_SERVER_SUPPORT
2581 case OBD_IOC_ECHO_MD: {
2588 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2589 GOTO(out, rc = -EPERM);
2591 count = data->ioc_count;
2592 cmd = data->ioc_command;
2594 id = data->ioc_obdo2.o_oi.oi.oi_id;
2595 dirlen = data->ioc_plen1;
2596 OBD_ALLOC(dir, dirlen + 1);
2598 GOTO(out, rc = -ENOMEM);
2600 if (copy_from_user(dir, data->ioc_pbuf1, dirlen)) {
2601 OBD_FREE(dir, data->ioc_plen1 + 1);
2602 GOTO(out, rc = -EFAULT);
2605 rc = echo_md_handler(ed, cmd, dir, dirlen, id, count, data);
2606 OBD_FREE(dir, dirlen + 1);
2609 case OBD_IOC_ECHO_ALLOC_SEQ: {
2610 struct lu_env *cl_env;
2615 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2616 GOTO(out, rc = -EPERM);
2618 cl_env = cl_env_get(&refcheck);
2620 GOTO(out, rc = PTR_ERR(cl_env));
2622 rc = lu_env_refill_by_tags(cl_env, ECHO_MD_CTX_TAG,
2625 cl_env_put(cl_env, &refcheck);
2629 rc = seq_client_get_seq(cl_env, ed->ed_cl_seq, &seq);
2630 cl_env_put(cl_env, &refcheck);
2632 CERROR("%s: Can not alloc seq: rc = %d\n",
2637 if (copy_to_user(data->ioc_pbuf1, &seq, data->ioc_plen1))
2640 max_count = LUSTRE_METADATA_SEQ_MAX_WIDTH;
2641 if (copy_to_user(data->ioc_pbuf2, &max_count,
2646 #endif /* HAVE_SERVER_SUPPORT */
2647 case OBD_IOC_DESTROY:
2648 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2649 GOTO (out, rc = -EPERM);
2651 rc = echo_get_object(&eco, ed, oa);
2653 rc = obd_destroy(env, ec->ec_exp, oa, NULL,
2654 &dummy_oti, NULL, NULL);
2656 eco->eo_deleted = 1;
2657 echo_put_object(eco);
2661 case OBD_IOC_GETATTR:
2662 rc = echo_get_object(&eco, ed, oa);
2664 struct obd_info oinfo = {
2668 rc = obd_getattr(env, ec->ec_exp, &oinfo);
2669 echo_put_object(eco);
2673 case OBD_IOC_SETATTR:
2674 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2675 GOTO (out, rc = -EPERM);
2677 rc = echo_get_object(&eco, ed, oa);
2679 struct obd_info oinfo = {
2683 rc = obd_setattr(env, ec->ec_exp, &oinfo, NULL);
2684 echo_put_object(eco);
2688 case OBD_IOC_BRW_WRITE:
2689 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2690 GOTO (out, rc = -EPERM);
2694 case OBD_IOC_BRW_READ:
2695 rc = echo_client_brw_ioctl(env, rw, exp, data, &dummy_oti);
2699 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
2700 GOTO (out, rc = -ENOTTY);
2705 #ifdef HAVE_SERVER_SUPPORT
2706 lu_context_exit(env->le_ses);
2707 lu_context_fini(env->le_ses);
2714 /* XXX this should be in a helper also called by target_send_reply */
2715 for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
2717 if (!ack_lock->mode)
2719 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
2725 static int echo_client_setup(const struct lu_env *env,
2726 struct obd_device *obddev, struct lustre_cfg *lcfg)
2728 struct echo_client_obd *ec = &obddev->u.echo_client;
2729 struct obd_device *tgt;
2730 struct obd_uuid echo_uuid = { "ECHO_UUID" };
2731 struct obd_connect_data *ocd = NULL;
2735 if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
2736 CERROR("requires a TARGET OBD name\n");
2740 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
2741 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
2742 CERROR("device not attached or not set up (%s)\n",
2743 lustre_cfg_string(lcfg, 1));
2747 spin_lock_init(&ec->ec_lock);
2748 INIT_LIST_HEAD(&ec->ec_objects);
2749 INIT_LIST_HEAD(&ec->ec_locks);
2752 if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
2753 #ifdef HAVE_SERVER_SUPPORT
2754 lu_context_tags_update(ECHO_MD_CTX_TAG);
2755 lu_session_tags_update(ECHO_MD_SES_TAG);
2757 CERROR("Local operations are NOT supported on client side. "
2758 "Only remote operations are supported. Metadata client "
2759 "must be run on server side.\n");
2764 OBD_ALLOC(ocd, sizeof(*ocd));
2766 CERROR("Can't alloc ocd connecting to %s\n",
2767 lustre_cfg_string(lcfg, 1));
2771 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
2772 OBD_CONNECT_BRW_SIZE |
2773 OBD_CONNECT_GRANT | OBD_CONNECT_FULL20 |
2774 OBD_CONNECT_64BITHASH | OBD_CONNECT_LVB_TYPE |
2776 ocd->ocd_brw_size = DT_MAX_BRW_SIZE;
2777 ocd->ocd_version = LUSTRE_VERSION_CODE;
2778 ocd->ocd_group = FID_SEQ_ECHO;
2780 rc = obd_connect(env, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
2782 /* Turn off pinger because it connects to tgt obd directly. */
2783 spin_lock(&tgt->obd_dev_lock);
2784 list_del_init(&ec->ec_exp->exp_obd_chain_timed);
2785 spin_unlock(&tgt->obd_dev_lock);
2788 OBD_FREE(ocd, sizeof(*ocd));
2791 CERROR("fail to connect to device %s\n",
2792 lustre_cfg_string(lcfg, 1));
2799 static int echo_client_cleanup(struct obd_device *obddev)
2801 struct echo_device *ed = obd2echo_dev(obddev);
2802 struct echo_client_obd *ec = &obddev->u.echo_client;
2806 /*Do nothing for Metadata echo client*/
2810 if (ed->ed_next_ismd) {
2811 #ifdef HAVE_SERVER_SUPPORT
2812 lu_context_tags_clear(ECHO_MD_CTX_TAG);
2813 lu_session_tags_clear(ECHO_MD_SES_TAG);
2815 CERROR("This is client-side only module, does not support "
2816 "metadata echo client.\n");
2821 if (!list_empty(&obddev->obd_exports)) {
2822 CERROR("still has clients!\n");
2826 LASSERT(atomic_read(&ec->ec_exp->exp_refcount) > 0);
2827 rc = obd_disconnect(ec->ec_exp);
2829 CERROR("fail to disconnect device: %d\n", rc);
2834 static int echo_client_connect(const struct lu_env *env,
2835 struct obd_export **exp,
2836 struct obd_device *src, struct obd_uuid *cluuid,
2837 struct obd_connect_data *data, void *localdata)
2840 struct lustre_handle conn = { 0 };
2843 rc = class_connect(&conn, src, cluuid);
2845 *exp = class_conn2export(&conn);
2851 static int echo_client_disconnect(struct obd_export *exp)
2857 GOTO(out, rc = -EINVAL);
2859 rc = class_disconnect(exp);
2865 static struct obd_ops echo_client_obd_ops = {
2866 .o_owner = THIS_MODULE,
2867 .o_iocontrol = echo_client_iocontrol,
2868 .o_connect = echo_client_connect,
2869 .o_disconnect = echo_client_disconnect
2872 int echo_client_init(void)
2876 rc = lu_kmem_init(echo_caches);
2878 rc = class_register_type(&echo_client_obd_ops, NULL, true, NULL,
2879 LUSTRE_ECHO_CLIENT_NAME,
2882 lu_kmem_fini(echo_caches);
2887 void echo_client_exit(void)
2889 class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
2890 lu_kmem_fini(echo_caches);
2893 static int __init obdecho_init(void)
2898 LCONSOLE_INFO("Echo OBD driver; http://www.lustre.org/\n");
2900 LASSERT(PAGE_CACHE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
2902 # ifdef HAVE_SERVER_SUPPORT
2903 rc = echo_persistent_pages_init();
2907 rc = class_register_type(&echo_obd_ops, NULL, true, NULL,
2908 LUSTRE_ECHO_NAME, NULL);
2913 rc = echo_client_init();
2915 # ifdef HAVE_SERVER_SUPPORT
2919 class_unregister_type(LUSTRE_ECHO_NAME);
2921 echo_persistent_pages_fini();
2927 static void /*__exit*/ obdecho_exit(void)
2931 # ifdef HAVE_SERVER_SUPPORT
2932 class_unregister_type(LUSTRE_ECHO_NAME);
2933 echo_persistent_pages_fini();
2937 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2938 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
2939 MODULE_LICENSE("GPL");
2941 cfs_module(obdecho, LUSTRE_VERSION_STRING, obdecho_init, obdecho_exit);
2943 /** @} echo_client */