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;
74 struct echo_device *eo_dev;
75 struct list_head eo_obj_chain;
76 struct lov_oinfo *eo_oinfo;
81 struct echo_object_conf {
82 struct cl_object_conf eoc_cl;
83 struct lov_oinfo **eoc_oinfo;
87 struct cl_page_slice ep_cl;
92 struct cl_lock_slice el_cl;
93 struct list_head el_chain;
94 struct echo_object *el_object;
99 static int echo_client_setup(const struct lu_env *env,
100 struct obd_device *obddev,
101 struct lustre_cfg *lcfg);
102 static int echo_client_cleanup(struct obd_device *obddev);
105 /** \defgroup echo_helpers Helper functions
108 static inline struct echo_device *cl2echo_dev(const struct cl_device *dev)
110 return container_of0(dev, struct echo_device, ed_cl);
113 static inline struct cl_device *echo_dev2cl(struct echo_device *d)
118 static inline struct echo_device *obd2echo_dev(const struct obd_device *obd)
120 return cl2echo_dev(lu2cl_dev(obd->obd_lu_dev));
123 static inline struct cl_object *echo_obj2cl(struct echo_object *eco)
128 static inline struct echo_object *cl2echo_obj(const struct cl_object *o)
130 return container_of(o, struct echo_object, eo_cl);
133 static inline struct echo_page *cl2echo_page(const struct cl_page_slice *s)
135 return container_of(s, struct echo_page, ep_cl);
138 static inline struct echo_lock *cl2echo_lock(const struct cl_lock_slice *s)
140 return container_of(s, struct echo_lock, el_cl);
143 static inline struct cl_lock *echo_lock2cl(const struct echo_lock *ecl)
145 return ecl->el_cl.cls_lock;
148 static struct lu_context_key echo_thread_key;
149 static inline struct echo_thread_info *echo_env_info(const struct lu_env *env)
151 struct echo_thread_info *info;
152 info = lu_context_key_get(&env->le_ctx, &echo_thread_key);
153 LASSERT(info != NULL);
158 struct echo_object_conf *cl2echo_conf(const struct cl_object_conf *c)
160 return container_of(c, struct echo_object_conf, eoc_cl);
163 /** @} echo_helpers */
165 static int cl_echo_object_put(struct echo_object *eco);
166 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
167 struct page **pages, int npages, int async);
169 struct echo_thread_info {
170 struct echo_object_conf eti_conf;
171 struct lustre_md eti_md;
173 struct cl_2queue eti_queue;
175 struct cl_lock eti_lock;
176 struct lu_fid eti_fid;
177 struct lu_fid eti_fid2;
178 #ifdef HAVE_SERVER_SUPPORT
179 struct md_op_spec eti_spec;
180 struct lov_mds_md_v3 eti_lmm;
181 struct lov_user_md_v3 eti_lum;
182 struct md_attr eti_ma;
183 struct lu_name eti_lname;
184 /* per-thread values, can be re-used */
188 struct lu_buf eti_buf;
189 char eti_xattr_buf[LUSTRE_POSIX_ACL_MAX_SIZE];
193 /* No session used right now */
194 struct echo_session_info {
198 static struct kmem_cache *echo_lock_kmem;
199 static struct kmem_cache *echo_object_kmem;
200 static struct kmem_cache *echo_thread_kmem;
201 static struct kmem_cache *echo_session_kmem;
202 /* static struct kmem_cache *echo_req_kmem; */
204 static struct lu_kmem_descr echo_caches[] = {
206 .ckd_cache = &echo_lock_kmem,
207 .ckd_name = "echo_lock_kmem",
208 .ckd_size = sizeof (struct echo_lock)
211 .ckd_cache = &echo_object_kmem,
212 .ckd_name = "echo_object_kmem",
213 .ckd_size = sizeof (struct echo_object)
216 .ckd_cache = &echo_thread_kmem,
217 .ckd_name = "echo_thread_kmem",
218 .ckd_size = sizeof (struct echo_thread_info)
221 .ckd_cache = &echo_session_kmem,
222 .ckd_name = "echo_session_kmem",
223 .ckd_size = sizeof (struct echo_session_info)
230 /** \defgroup echo_page Page operations
232 * Echo page operations.
236 static int echo_page_own(const struct lu_env *env,
237 const struct cl_page_slice *slice,
238 struct cl_io *io, int nonblock)
240 struct echo_page *ep = cl2echo_page(slice);
243 mutex_lock(&ep->ep_lock);
244 else if (!mutex_trylock(&ep->ep_lock))
249 static void echo_page_disown(const struct lu_env *env,
250 const struct cl_page_slice *slice,
253 struct echo_page *ep = cl2echo_page(slice);
255 LASSERT(mutex_is_locked(&ep->ep_lock));
256 mutex_unlock(&ep->ep_lock);
259 static void echo_page_discard(const struct lu_env *env,
260 const struct cl_page_slice *slice,
261 struct cl_io *unused)
263 cl_page_delete(env, slice->cpl_page);
266 static int echo_page_is_vmlocked(const struct lu_env *env,
267 const struct cl_page_slice *slice)
269 if (mutex_is_locked(&cl2echo_page(slice)->ep_lock))
274 static void echo_page_completion(const struct lu_env *env,
275 const struct cl_page_slice *slice,
278 LASSERT(slice->cpl_page->cp_sync_io != NULL);
281 static void echo_page_fini(const struct lu_env *env,
282 struct cl_page_slice *slice)
284 struct echo_object *eco = cl2echo_obj(slice->cpl_obj);
287 atomic_dec(&eco->eo_npages);
288 page_cache_release(slice->cpl_page->cp_vmpage);
292 static int echo_page_prep(const struct lu_env *env,
293 const struct cl_page_slice *slice,
294 struct cl_io *unused)
299 static int echo_page_print(const struct lu_env *env,
300 const struct cl_page_slice *slice,
301 void *cookie, lu_printer_t printer)
303 struct echo_page *ep = cl2echo_page(slice);
305 (*printer)(env, cookie, LUSTRE_ECHO_CLIENT_NAME"-page@%p %d vm@%p\n",
306 ep, mutex_is_locked(&ep->ep_lock),
307 slice->cpl_page->cp_vmpage);
311 static const struct cl_page_operations echo_page_ops = {
312 .cpo_own = echo_page_own,
313 .cpo_disown = echo_page_disown,
314 .cpo_discard = echo_page_discard,
315 .cpo_fini = echo_page_fini,
316 .cpo_print = echo_page_print,
317 .cpo_is_vmlocked = echo_page_is_vmlocked,
320 .cpo_prep = echo_page_prep,
321 .cpo_completion = echo_page_completion,
324 .cpo_prep = echo_page_prep,
325 .cpo_completion = echo_page_completion,
331 /** \defgroup echo_lock Locking
333 * echo lock operations
337 static void echo_lock_fini(const struct lu_env *env,
338 struct cl_lock_slice *slice)
340 struct echo_lock *ecl = cl2echo_lock(slice);
342 LASSERT(list_empty(&ecl->el_chain));
343 OBD_SLAB_FREE_PTR(ecl, echo_lock_kmem);
346 static struct cl_lock_operations echo_lock_ops = {
347 .clo_fini = echo_lock_fini,
352 /** \defgroup echo_cl_ops cl_object operations
354 * operations for cl_object
358 static int echo_page_init(const struct lu_env *env, struct cl_object *obj,
359 struct cl_page *page, pgoff_t index)
361 struct echo_page *ep = cl_object_page_slice(obj, page);
362 struct echo_object *eco = cl2echo_obj(obj);
365 page_cache_get(page->cp_vmpage);
366 mutex_init(&ep->ep_lock);
367 cl_page_slice_add(page, &ep->ep_cl, obj, index, &echo_page_ops);
368 atomic_inc(&eco->eo_npages);
372 static int echo_io_init(const struct lu_env *env, struct cl_object *obj,
378 static int echo_lock_init(const struct lu_env *env,
379 struct cl_object *obj, struct cl_lock *lock,
380 const struct cl_io *unused)
382 struct echo_lock *el;
385 OBD_SLAB_ALLOC_PTR_GFP(el, echo_lock_kmem, GFP_NOFS);
387 cl_lock_slice_add(lock, &el->el_cl, obj, &echo_lock_ops);
388 el->el_object = cl2echo_obj(obj);
389 INIT_LIST_HEAD(&el->el_chain);
390 atomic_set(&el->el_refcount, 0);
392 RETURN(el == NULL ? -ENOMEM : 0);
395 static int echo_conf_set(const struct lu_env *env, struct cl_object *obj,
396 const struct cl_object_conf *conf)
401 static const struct cl_object_operations echo_cl_obj_ops = {
402 .coo_page_init = echo_page_init,
403 .coo_lock_init = echo_lock_init,
404 .coo_io_init = echo_io_init,
405 .coo_conf_set = echo_conf_set
407 /** @} echo_cl_ops */
409 /** \defgroup echo_lu_ops lu_object operations
411 * operations for echo lu object.
415 static int echo_object_init(const struct lu_env *env, struct lu_object *obj,
416 const struct lu_object_conf *conf)
418 struct echo_device *ed = cl2echo_dev(lu2cl_dev(obj->lo_dev));
419 struct echo_client_obd *ec = ed->ed_ec;
420 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
424 struct lu_object *below;
425 struct lu_device *under;
428 below = under->ld_ops->ldo_object_alloc(env, obj->lo_header,
432 lu_object_add(obj, below);
435 if (!ed->ed_next_ismd) {
436 const struct cl_object_conf *cconf = lu2cl_conf(conf);
437 struct echo_object_conf *econf = cl2echo_conf(cconf);
439 LASSERT(econf->eoc_oinfo != NULL);
441 /* Transfer the oinfo pointer to eco that it won't be
443 eco->eo_oinfo = *econf->eoc_oinfo;
444 *econf->eoc_oinfo = NULL;
446 eco->eo_oinfo = NULL;
450 atomic_set(&eco->eo_npages, 0);
451 cl_object_page_init(lu2cl(obj), sizeof(struct echo_page));
453 spin_lock(&ec->ec_lock);
454 list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
455 spin_unlock(&ec->ec_lock);
460 static void echo_object_free(const struct lu_env *env, struct lu_object *obj)
462 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
463 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
466 LASSERT(atomic_read(&eco->eo_npages) == 0);
468 spin_lock(&ec->ec_lock);
469 list_del_init(&eco->eo_obj_chain);
470 spin_unlock(&ec->ec_lock);
473 lu_object_header_fini(obj->lo_header);
475 if (eco->eo_oinfo != NULL)
476 OBD_FREE_PTR(eco->eo_oinfo);
478 OBD_SLAB_FREE_PTR(eco, echo_object_kmem);
482 static int echo_object_print(const struct lu_env *env, void *cookie,
483 lu_printer_t p, const struct lu_object *o)
485 struct echo_object *obj = cl2echo_obj(lu2cl(o));
487 return (*p)(env, cookie, "echoclient-object@%p", obj);
490 static const struct lu_object_operations echo_lu_obj_ops = {
491 .loo_object_init = echo_object_init,
492 .loo_object_delete = NULL,
493 .loo_object_release = NULL,
494 .loo_object_free = echo_object_free,
495 .loo_object_print = echo_object_print,
496 .loo_object_invariant = NULL
498 /** @} echo_lu_ops */
500 /** \defgroup echo_lu_dev_ops lu_device operations
502 * Operations for echo lu device.
506 static struct lu_object *echo_object_alloc(const struct lu_env *env,
507 const struct lu_object_header *hdr,
508 struct lu_device *dev)
510 struct echo_object *eco;
511 struct lu_object *obj = NULL;
514 /* we're the top dev. */
515 LASSERT(hdr == NULL);
516 OBD_SLAB_ALLOC_PTR_GFP(eco, echo_object_kmem, GFP_NOFS);
518 struct cl_object_header *hdr = &eco->eo_hdr;
520 obj = &echo_obj2cl(eco)->co_lu;
521 cl_object_header_init(hdr);
522 hdr->coh_page_bufsize = cfs_size_round(sizeof(struct cl_page));
524 lu_object_init(obj, &hdr->coh_lu, dev);
525 lu_object_add_top(&hdr->coh_lu, obj);
527 eco->eo_cl.co_ops = &echo_cl_obj_ops;
528 obj->lo_ops = &echo_lu_obj_ops;
533 static struct lu_device_operations echo_device_lu_ops = {
534 .ldo_object_alloc = echo_object_alloc,
537 /** @} echo_lu_dev_ops */
539 static struct cl_device_operations echo_device_cl_ops = {
542 /** \defgroup echo_init Setup and teardown
544 * Init and fini functions for echo client.
548 static int echo_site_init(const struct lu_env *env, struct echo_device *ed)
550 struct cl_site *site = &ed->ed_site_myself;
553 /* initialize site */
554 rc = cl_site_init(site, &ed->ed_cl);
556 CERROR("Cannot initilize site for echo client(%d)\n", rc);
560 rc = lu_site_init_finish(&site->cs_lu);
568 static void echo_site_fini(const struct lu_env *env, struct echo_device *ed)
571 if (!ed->ed_next_ismd)
572 cl_site_fini(ed->ed_site);
577 static void *echo_thread_key_init(const struct lu_context *ctx,
578 struct lu_context_key *key)
580 struct echo_thread_info *info;
582 OBD_SLAB_ALLOC_PTR_GFP(info, echo_thread_kmem, GFP_NOFS);
584 info = ERR_PTR(-ENOMEM);
588 static void echo_thread_key_fini(const struct lu_context *ctx,
589 struct lu_context_key *key, void *data)
591 struct echo_thread_info *info = data;
592 OBD_SLAB_FREE_PTR(info, echo_thread_kmem);
595 static void echo_thread_key_exit(const struct lu_context *ctx,
596 struct lu_context_key *key, void *data)
600 static struct lu_context_key echo_thread_key = {
601 .lct_tags = LCT_CL_THREAD,
602 .lct_init = echo_thread_key_init,
603 .lct_fini = echo_thread_key_fini,
604 .lct_exit = echo_thread_key_exit
607 static void *echo_session_key_init(const struct lu_context *ctx,
608 struct lu_context_key *key)
610 struct echo_session_info *session;
612 OBD_SLAB_ALLOC_PTR_GFP(session, echo_session_kmem, GFP_NOFS);
614 session = ERR_PTR(-ENOMEM);
618 static void echo_session_key_fini(const struct lu_context *ctx,
619 struct lu_context_key *key, void *data)
621 struct echo_session_info *session = data;
622 OBD_SLAB_FREE_PTR(session, echo_session_kmem);
625 static void echo_session_key_exit(const struct lu_context *ctx,
626 struct lu_context_key *key, void *data)
630 static struct lu_context_key echo_session_key = {
631 .lct_tags = LCT_SESSION,
632 .lct_init = echo_session_key_init,
633 .lct_fini = echo_session_key_fini,
634 .lct_exit = echo_session_key_exit
637 LU_TYPE_INIT_FINI(echo, &echo_thread_key, &echo_session_key);
639 #ifdef HAVE_SERVER_SUPPORT
640 # define ECHO_SEQ_WIDTH 0xffffffff
641 static int echo_fid_init(struct echo_device *ed, char *obd_name,
642 struct seq_server_site *ss)
648 OBD_ALLOC_PTR(ed->ed_cl_seq);
649 if (ed->ed_cl_seq == NULL)
652 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
654 GOTO(out_free_seq, rc = -ENOMEM);
656 snprintf(prefix, MAX_OBD_NAME + 5, "srv-%s", obd_name);
658 /* Init client side sequence-manager */
659 rc = seq_client_init(ed->ed_cl_seq, NULL,
661 prefix, ss->ss_server_seq);
662 ed->ed_cl_seq->lcs_width = ECHO_SEQ_WIDTH;
663 OBD_FREE(prefix, MAX_OBD_NAME + 5);
665 GOTO(out_free_seq, rc);
670 OBD_FREE_PTR(ed->ed_cl_seq);
671 ed->ed_cl_seq = NULL;
675 static int echo_fid_fini(struct obd_device *obddev)
677 struct echo_device *ed = obd2echo_dev(obddev);
680 if (ed->ed_cl_seq != NULL) {
681 seq_client_fini(ed->ed_cl_seq);
682 OBD_FREE_PTR(ed->ed_cl_seq);
683 ed->ed_cl_seq = NULL;
688 #endif /* HAVE_SERVER_SUPPORT */
690 static struct lu_device *echo_device_alloc(const struct lu_env *env,
691 struct lu_device_type *t,
692 struct lustre_cfg *cfg)
694 struct lu_device *next;
695 struct echo_device *ed;
696 struct cl_device *cd;
697 struct obd_device *obd = NULL; /* to keep compiler happy */
698 struct obd_device *tgt;
699 const char *tgt_type_name;
706 GOTO(out, rc = -ENOMEM);
710 rc = cl_device_init(cd, t);
714 cd->cd_lu_dev.ld_ops = &echo_device_lu_ops;
715 cd->cd_ops = &echo_device_cl_ops;
718 obd = class_name2obd(lustre_cfg_string(cfg, 0));
719 LASSERT(obd != NULL);
720 LASSERT(env != NULL);
722 tgt = class_name2obd(lustre_cfg_string(cfg, 1));
724 CERROR("Can not find tgt device %s\n",
725 lustre_cfg_string(cfg, 1));
726 GOTO(out, rc = -ENODEV);
729 next = tgt->obd_lu_dev;
731 if (strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME) == 0) {
732 ed->ed_next_ismd = 1;
733 } else if (strcmp(tgt->obd_type->typ_name, LUSTRE_OST_NAME) == 0 ||
734 strcmp(tgt->obd_type->typ_name, LUSTRE_OSC_NAME) == 0) {
735 ed->ed_next_ismd = 0;
736 rc = echo_site_init(env, ed);
740 GOTO(out, rc = -EINVAL);
745 rc = echo_client_setup(env, obd, cfg);
749 ed->ed_ec = &obd->u.echo_client;
752 if (ed->ed_next_ismd) {
753 #ifdef HAVE_SERVER_SUPPORT
754 /* Suppose to connect to some Metadata layer */
756 struct lu_device *ld;
760 CERROR("%s is not lu device type!\n",
761 lustre_cfg_string(cfg, 1));
762 GOTO(out, rc = -EINVAL);
765 tgt_type_name = lustre_cfg_string(cfg, 2);
766 if (!tgt_type_name) {
767 CERROR("%s no type name for echo %s setup\n",
768 lustre_cfg_string(cfg, 1),
769 tgt->obd_type->typ_name);
770 GOTO(out, rc = -EINVAL);
775 spin_lock(&ls->ls_ld_lock);
776 list_for_each_entry(ld, &ls->ls_ld_linkage, ld_linkage) {
777 if (strcmp(ld->ld_type->ldt_name, tgt_type_name) == 0) {
782 spin_unlock(&ls->ls_ld_lock);
785 CERROR("%s is not lu device type!\n",
786 lustre_cfg_string(cfg, 1));
787 GOTO(out, rc = -EINVAL);
791 /* For MD echo client, it will use the site in MDS stack */
792 ed->ed_site_myself.cs_lu = *ls;
793 ed->ed_site = &ed->ed_site_myself;
794 ed->ed_cl.cd_lu_dev.ld_site = &ed->ed_site_myself.cs_lu;
795 rc = echo_fid_init(ed, obd->obd_name, lu_site2seq(ls));
797 CERROR("echo fid init error %d\n", rc);
800 #else /* !HAVE_SERVER_SUPPORT */
801 CERROR("Local operations are NOT supported on client side. "
802 "Only remote operations are supported. Metadata client "
803 "must be run on server side.\n");
804 GOTO(out, rc = -EOPNOTSUPP);
805 #endif /* HAVE_SERVER_SUPPORT */
807 /* if echo client is to be stacked upon ost device, the next is
808 * NULL since ost is not a clio device so far */
809 if (next != NULL && !lu_device_is_cl(next))
812 tgt_type_name = tgt->obd_type->typ_name;
814 LASSERT(next != NULL);
815 if (next->ld_site != NULL)
816 GOTO(out, rc = -EBUSY);
818 next->ld_site = &ed->ed_site->cs_lu;
819 rc = next->ld_type->ldt_ops->ldto_device_init(env, next,
820 next->ld_type->ldt_name,
825 LASSERT(strcmp(tgt_type_name, LUSTRE_OST_NAME) == 0);
829 RETURN(&cd->cd_lu_dev);
834 rc2 = echo_client_cleanup(obd);
836 CERROR("Cleanup obd device %s error(%d)\n",
841 echo_site_fini(env, ed);
843 cl_device_fini(&ed->ed_cl);
853 static int echo_device_init(const struct lu_env *env, struct lu_device *d,
854 const char *name, struct lu_device *next)
860 static struct lu_device *echo_device_fini(const struct lu_env *env,
863 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
864 struct lu_device *next = ed->ed_next;
866 while (next && !ed->ed_next_ismd)
867 next = next->ld_type->ldt_ops->ldto_device_fini(env, next);
871 static void echo_lock_release(const struct lu_env *env,
872 struct echo_lock *ecl,
875 struct cl_lock *clk = echo_lock2cl(ecl);
877 cl_lock_release(env, clk);
880 static struct lu_device *echo_device_free(const struct lu_env *env,
883 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
884 struct echo_client_obd *ec = ed->ed_ec;
885 struct echo_object *eco;
886 struct lu_device *next = ed->ed_next;
888 CDEBUG(D_INFO, "echo device:%p is going to be freed, next = %p\n",
891 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
893 /* check if there are objects still alive.
894 * It shouldn't have any object because lu_site_purge would cleanup
895 * all of cached objects. Anyway, probably the echo device is being
896 * parallelly accessed.
898 spin_lock(&ec->ec_lock);
899 list_for_each_entry(eco, &ec->ec_objects, eo_obj_chain)
901 spin_unlock(&ec->ec_lock);
904 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
907 "Waiting for the reference of echo object to be dropped\n");
909 /* Wait for the last reference to be dropped. */
910 spin_lock(&ec->ec_lock);
911 while (!list_empty(&ec->ec_objects)) {
912 spin_unlock(&ec->ec_lock);
913 CERROR("echo_client still has objects at cleanup time, "
914 "wait for 1 second\n");
915 schedule_timeout_and_set_state(TASK_UNINTERRUPTIBLE,
916 cfs_time_seconds(1));
917 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
918 spin_lock(&ec->ec_lock);
920 spin_unlock(&ec->ec_lock);
922 LASSERT(list_empty(&ec->ec_locks));
924 CDEBUG(D_INFO, "No object exists, exiting...\n");
926 echo_client_cleanup(d->ld_obd);
927 #ifdef HAVE_SERVER_SUPPORT
928 echo_fid_fini(d->ld_obd);
930 while (next && !ed->ed_next_ismd)
931 next = next->ld_type->ldt_ops->ldto_device_free(env, next);
933 LASSERT(ed->ed_site == lu2cl_site(d->ld_site));
934 echo_site_fini(env, ed);
935 cl_device_fini(&ed->ed_cl);
941 static const struct lu_device_type_operations echo_device_type_ops = {
942 .ldto_init = echo_type_init,
943 .ldto_fini = echo_type_fini,
945 .ldto_start = echo_type_start,
946 .ldto_stop = echo_type_stop,
948 .ldto_device_alloc = echo_device_alloc,
949 .ldto_device_free = echo_device_free,
950 .ldto_device_init = echo_device_init,
951 .ldto_device_fini = echo_device_fini
954 static struct lu_device_type echo_device_type = {
955 .ldt_tags = LU_DEVICE_CL,
956 .ldt_name = LUSTRE_ECHO_CLIENT_NAME,
957 .ldt_ops = &echo_device_type_ops,
958 .ldt_ctx_tags = LCT_CL_THREAD | LCT_MD_THREAD | LCT_DT_THREAD,
962 /** \defgroup echo_exports Exported operations
964 * exporting functions to echo client
969 /* Interfaces to echo client obd device */
970 static struct echo_object *
971 cl_echo_object_find(struct echo_device *d, const struct ost_id *oi)
974 struct echo_thread_info *info;
975 struct echo_object_conf *conf;
976 struct echo_object *eco;
977 struct cl_object *obj;
978 struct lov_oinfo *oinfo = NULL;
984 LASSERTF(ostid_id(oi) != 0, DOSTID"\n", POSTID(oi));
985 LASSERTF(ostid_seq(oi) == FID_SEQ_ECHO, DOSTID"\n", POSTID(oi));
987 /* Never return an object if the obd is to be freed. */
988 if (echo_dev2cl(d)->cd_lu_dev.ld_obd->obd_stopping)
989 RETURN(ERR_PTR(-ENODEV));
991 env = cl_env_get(&refcheck);
995 info = echo_env_info(env);
996 conf = &info->eti_conf;
998 OBD_ALLOC_PTR(oinfo);
1000 GOTO(out, eco = ERR_PTR(-ENOMEM));
1002 oinfo->loi_oi = *oi;
1003 conf->eoc_cl.u.coc_oinfo = oinfo;
1006 /* If echo_object_init() is successful then ownership of oinfo
1007 * is transferred to the object. */
1008 conf->eoc_oinfo = &oinfo;
1010 fid = &info->eti_fid;
1011 rc = ostid_to_fid(fid, oi, 0);
1013 GOTO(out, eco = ERR_PTR(rc));
1015 /* In the function below, .hs_keycmp resolves to
1016 * lu_obj_hop_keycmp() */
1017 /* coverity[overrun-buffer-val] */
1018 obj = cl_object_find(env, echo_dev2cl(d), fid, &conf->eoc_cl);
1020 GOTO(out, eco = (void*)obj);
1022 eco = cl2echo_obj(obj);
1023 if (eco->eo_deleted) {
1024 cl_object_put(env, obj);
1025 eco = ERR_PTR(-EAGAIN);
1030 OBD_FREE_PTR(oinfo);
1032 cl_env_put(env, &refcheck);
1036 static int cl_echo_object_put(struct echo_object *eco)
1039 struct cl_object *obj = echo_obj2cl(eco);
1043 env = cl_env_get(&refcheck);
1045 RETURN(PTR_ERR(env));
1047 /* an external function to kill an object? */
1048 if (eco->eo_deleted) {
1049 struct lu_object_header *loh = obj->co_lu.lo_header;
1050 LASSERT(&eco->eo_hdr == luh2coh(loh));
1051 set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
1054 cl_object_put(env, obj);
1055 cl_env_put(env, &refcheck);
1059 static int cl_echo_enqueue0(struct lu_env *env, struct echo_object *eco,
1060 obd_off start, obd_off end, int mode,
1061 __u64 *cookie , __u32 enqflags)
1064 struct cl_lock *lck;
1065 struct cl_object *obj;
1066 struct cl_lock_descr *descr;
1067 struct echo_thread_info *info;
1071 info = echo_env_info(env);
1073 lck = &info->eti_lock;
1074 obj = echo_obj2cl(eco);
1076 memset(lck, 0, sizeof(*lck));
1077 descr = &lck->cll_descr;
1078 descr->cld_obj = obj;
1079 descr->cld_start = cl_index(obj, start);
1080 descr->cld_end = cl_index(obj, end);
1081 descr->cld_mode = mode == LCK_PW ? CLM_WRITE : CLM_READ;
1082 descr->cld_enq_flags = enqflags;
1085 rc = cl_lock_request(env, io, lck);
1087 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
1088 struct echo_lock *el;
1090 el = cl2echo_lock(cl_lock_at(lck, &echo_device_type));
1091 spin_lock(&ec->ec_lock);
1092 if (list_empty(&el->el_chain)) {
1093 list_add(&el->el_chain, &ec->ec_locks);
1094 el->el_cookie = ++ec->ec_unique;
1096 atomic_inc(&el->el_refcount);
1097 *cookie = el->el_cookie;
1098 spin_unlock(&ec->ec_lock);
1103 static int cl_echo_cancel0(struct lu_env *env, struct echo_device *ed,
1106 struct echo_client_obd *ec = ed->ed_ec;
1107 struct echo_lock *ecl = NULL;
1108 struct list_head *el;
1109 int found = 0, still_used = 0;
1112 LASSERT(ec != NULL);
1113 spin_lock(&ec->ec_lock);
1114 list_for_each(el, &ec->ec_locks) {
1115 ecl = list_entry(el, struct echo_lock, el_chain);
1116 CDEBUG(D_INFO, "ecl: %p, cookie: "LPX64"\n", ecl, ecl->el_cookie);
1117 found = (ecl->el_cookie == cookie);
1119 if (atomic_dec_and_test(&ecl->el_refcount))
1120 list_del_init(&ecl->el_chain);
1126 spin_unlock(&ec->ec_lock);
1131 echo_lock_release(env, ecl, still_used);
1135 static void echo_commit_callback(const struct lu_env *env, struct cl_io *io,
1136 struct cl_page *page)
1138 struct echo_thread_info *info;
1139 struct cl_2queue *queue;
1141 info = echo_env_info(env);
1142 LASSERT(io == &info->eti_io);
1144 queue = &info->eti_queue;
1145 cl_page_list_add(&queue->c2_qout, page);
1148 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
1149 struct page **pages, int npages, int async)
1152 struct echo_thread_info *info;
1153 struct cl_object *obj = echo_obj2cl(eco);
1154 struct echo_device *ed = eco->eo_dev;
1155 struct cl_2queue *queue;
1157 struct cl_page *clp;
1158 struct lustre_handle lh = { 0 };
1159 int page_size = cl_page_size(obj);
1165 LASSERT((offset & ~CFS_PAGE_MASK) == 0);
1166 LASSERT(ed->ed_next != NULL);
1167 env = cl_env_get(&refcheck);
1169 RETURN(PTR_ERR(env));
1171 info = echo_env_info(env);
1173 queue = &info->eti_queue;
1175 cl_2queue_init(queue);
1177 io->ci_ignore_layout = 1;
1178 rc = cl_io_init(env, io, CIT_MISC, obj);
1184 rc = cl_echo_enqueue0(env, eco, offset,
1185 offset + npages * PAGE_CACHE_SIZE - 1,
1186 rw == READ ? LCK_PR : LCK_PW, &lh.cookie,
1189 GOTO(error_lock, rc);
1191 for (i = 0; i < npages; i++) {
1193 clp = cl_page_find(env, obj, cl_index(obj, offset),
1194 pages[i], CPT_TRANSIENT);
1199 LASSERT(clp->cp_type == CPT_TRANSIENT);
1201 rc = cl_page_own(env, io, clp);
1203 LASSERT(clp->cp_state == CPS_FREEING);
1204 cl_page_put(env, clp);
1208 cl_2queue_add(queue, clp);
1210 /* drop the reference count for cl_page_find, so that the page
1211 * will be freed in cl_2queue_fini. */
1212 cl_page_put(env, clp);
1213 cl_page_clip(env, clp, 0, page_size);
1215 offset += page_size;
1219 enum cl_req_type typ = rw == READ ? CRT_READ : CRT_WRITE;
1221 async = async && (typ == CRT_WRITE);
1223 rc = cl_io_commit_async(env, io, &queue->c2_qin,
1225 echo_commit_callback);
1227 rc = cl_io_submit_sync(env, io, typ, queue, 0);
1228 CDEBUG(D_INFO, "echo_client %s write returns %d\n",
1229 async ? "async" : "sync", rc);
1232 cl_echo_cancel0(env, ed, lh.cookie);
1235 cl_2queue_discard(env, io, queue);
1236 cl_2queue_disown(env, io, queue);
1237 cl_2queue_fini(env, queue);
1238 cl_io_fini(env, io);
1240 cl_env_put(env, &refcheck);
1243 /** @} echo_exports */
1246 static obd_id last_object_id;
1248 #ifdef HAVE_SERVER_SUPPORT
1249 static inline void echo_md_build_name(struct lu_name *lname, char *name,
1252 sprintf(name, LPU64, id);
1253 lname->ln_name = name;
1254 lname->ln_namelen = strlen(name);
1257 /* similar to mdt_attr_get_complex */
1258 static int echo_big_lmm_get(const struct lu_env *env, struct md_object *o,
1261 struct echo_thread_info *info = echo_env_info(env);
1266 LASSERT(ma->ma_lmm_size > 0);
1268 rc = mo_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LOV);
1272 /* big_lmm may need to be grown */
1273 if (info->eti_big_lmmsize < rc) {
1274 int size = size_roundup_power2(rc);
1276 if (info->eti_big_lmmsize > 0) {
1277 /* free old buffer */
1278 LASSERT(info->eti_big_lmm);
1279 OBD_FREE_LARGE(info->eti_big_lmm,
1280 info->eti_big_lmmsize);
1281 info->eti_big_lmm = NULL;
1282 info->eti_big_lmmsize = 0;
1285 OBD_ALLOC_LARGE(info->eti_big_lmm, size);
1286 if (info->eti_big_lmm == NULL)
1288 info->eti_big_lmmsize = size;
1290 LASSERT(info->eti_big_lmmsize >= rc);
1292 info->eti_buf.lb_buf = info->eti_big_lmm;
1293 info->eti_buf.lb_len = info->eti_big_lmmsize;
1294 rc = mo_xattr_get(env, o, &info->eti_buf, XATTR_NAME_LOV);
1298 ma->ma_valid |= MA_LOV;
1299 ma->ma_lmm = info->eti_big_lmm;
1300 ma->ma_lmm_size = rc;
1305 static int echo_attr_get_complex(const struct lu_env *env,
1306 struct md_object *next,
1309 struct echo_thread_info *info = echo_env_info(env);
1310 struct lu_buf *buf = &info->eti_buf;
1311 umode_t mode = lu_object_attr(&next->mo_lu);
1312 int need = ma->ma_need;
1319 if (need & MA_INODE) {
1320 ma->ma_need = MA_INODE;
1321 rc = mo_attr_get(env, next, ma);
1324 ma->ma_valid |= MA_INODE;
1327 if (need & MA_LOV) {
1328 if (S_ISREG(mode) || S_ISDIR(mode)) {
1329 LASSERT(ma->ma_lmm_size > 0);
1330 buf->lb_buf = ma->ma_lmm;
1331 buf->lb_len = ma->ma_lmm_size;
1332 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LOV);
1334 ma->ma_lmm_size = rc2;
1335 ma->ma_valid |= MA_LOV;
1336 } else if (rc2 == -ENODATA) {
1338 ma->ma_lmm_size = 0;
1339 } else if (rc2 == -ERANGE) {
1340 rc2 = echo_big_lmm_get(env, next, ma);
1342 GOTO(out, rc = rc2);
1344 GOTO(out, rc = rc2);
1349 #ifdef CONFIG_FS_POSIX_ACL
1350 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
1351 buf->lb_buf = ma->ma_acl;
1352 buf->lb_len = ma->ma_acl_size;
1353 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1355 ma->ma_acl_size = rc2;
1356 ma->ma_valid |= MA_ACL_DEF;
1357 } else if (rc2 == -ENODATA) {
1359 ma->ma_acl_size = 0;
1361 GOTO(out, rc = rc2);
1367 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
1368 rc, ma->ma_valid, ma->ma_lmm);
1373 echo_md_create_internal(const struct lu_env *env, struct echo_device *ed,
1374 struct md_object *parent, struct lu_fid *fid,
1375 struct lu_name *lname, struct md_op_spec *spec,
1378 struct lu_object *ec_child, *child;
1379 struct lu_device *ld = ed->ed_next;
1380 struct echo_thread_info *info = echo_env_info(env);
1381 struct lu_fid *fid2 = &info->eti_fid2;
1382 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
1387 rc = mdo_lookup(env, parent, lname, fid2, spec);
1390 else if (rc != -ENOENT)
1393 ec_child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev,
1395 if (IS_ERR(ec_child)) {
1396 CERROR("Can not find the child "DFID": rc = %ld\n", PFID(fid),
1398 RETURN(PTR_ERR(ec_child));
1401 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1402 if (child == NULL) {
1403 CERROR("Can not locate the child "DFID"\n", PFID(fid));
1404 GOTO(out_put, rc = -EINVAL);
1407 CDEBUG(D_RPCTRACE, "Start creating object "DFID" %s %p\n",
1408 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1411 * Do not perform lookup sanity check. We know that name does not exist.
1413 spec->sp_cr_lookup = 0;
1414 rc = mdo_create(env, parent, lname, lu2md(child), spec, ma);
1416 CERROR("Can not create child "DFID": rc = %d\n", PFID(fid), rc);
1419 CDEBUG(D_RPCTRACE, "End creating object "DFID" %s %p rc = %d\n",
1420 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent, rc);
1423 lu_object_put(env, ec_child);
1427 static int echo_set_lmm_size(const struct lu_env *env, struct lu_device *ld,
1430 struct echo_thread_info *info = echo_env_info(env);
1432 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
1433 ma->ma_lmm = (void *)&info->eti_lmm;
1434 ma->ma_lmm_size = sizeof(info->eti_lmm);
1436 LASSERT(info->eti_big_lmmsize);
1437 ma->ma_lmm = info->eti_big_lmm;
1438 ma->ma_lmm_size = info->eti_big_lmmsize;
1444 static int echo_create_md_object(const struct lu_env *env,
1445 struct echo_device *ed,
1446 struct lu_object *ec_parent,
1448 char *name, int namelen,
1449 __u64 id, __u32 mode, int count,
1450 int stripe_count, int stripe_offset)
1452 struct lu_object *parent;
1453 struct echo_thread_info *info = echo_env_info(env);
1454 struct lu_name *lname = &info->eti_lname;
1455 struct md_op_spec *spec = &info->eti_spec;
1456 struct md_attr *ma = &info->eti_ma;
1457 struct lu_device *ld = ed->ed_next;
1463 if (ec_parent == NULL)
1465 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1469 memset(ma, 0, sizeof(*ma));
1470 memset(spec, 0, sizeof(*spec));
1471 if (stripe_count != 0) {
1472 spec->sp_cr_flags |= FMODE_WRITE;
1473 echo_set_lmm_size(env, ld, ma);
1474 if (stripe_count != -1) {
1475 struct lov_user_md_v3 *lum = &info->eti_lum;
1477 lum->lmm_magic = LOV_USER_MAGIC_V3;
1478 lum->lmm_stripe_count = stripe_count;
1479 lum->lmm_stripe_offset = stripe_offset;
1480 lum->lmm_pattern = 0;
1481 spec->u.sp_ea.eadata = lum;
1482 spec->u.sp_ea.eadatalen = sizeof(*lum);
1483 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1487 ma->ma_attr.la_mode = mode;
1488 ma->ma_attr.la_valid = LA_CTIME | LA_MODE;
1489 ma->ma_attr.la_ctime = cfs_time_current_64();
1492 lname->ln_name = name;
1493 lname->ln_namelen = namelen;
1494 /* If name is specified, only create one object by name */
1495 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1500 /* Create multiple object sequenced by id */
1501 for (i = 0; i < count; i++) {
1502 char *tmp_name = info->eti_name;
1504 echo_md_build_name(lname, tmp_name, id);
1506 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1509 CERROR("Can not create child %s: rc = %d\n", tmp_name,
1520 static struct lu_object *echo_md_lookup(const struct lu_env *env,
1521 struct echo_device *ed,
1522 struct md_object *parent,
1523 struct lu_name *lname)
1525 struct echo_thread_info *info = echo_env_info(env);
1526 struct lu_fid *fid = &info->eti_fid;
1527 struct lu_object *child;
1531 CDEBUG(D_INFO, "lookup %s in parent "DFID" %p\n", lname->ln_name,
1533 rc = mdo_lookup(env, parent, lname, fid, NULL);
1535 CERROR("lookup %s: rc = %d\n", lname->ln_name, rc);
1536 RETURN(ERR_PTR(rc));
1539 /* In the function below, .hs_keycmp resolves to
1540 * lu_obj_hop_keycmp() */
1541 /* coverity[overrun-buffer-val] */
1542 child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1547 static int echo_setattr_object(const struct lu_env *env,
1548 struct echo_device *ed,
1549 struct lu_object *ec_parent,
1550 __u64 id, int count)
1552 struct lu_object *parent;
1553 struct echo_thread_info *info = echo_env_info(env);
1554 struct lu_name *lname = &info->eti_lname;
1555 char *name = info->eti_name;
1556 struct lu_device *ld = ed->ed_next;
1557 struct lu_buf *buf = &info->eti_buf;
1563 if (ec_parent == NULL)
1565 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1569 for (i = 0; i < count; i++) {
1570 struct lu_object *ec_child, *child;
1572 echo_md_build_name(lname, name, id);
1574 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1575 if (IS_ERR(ec_child)) {
1576 CERROR("Can't find child %s: rc = %ld\n",
1577 lname->ln_name, PTR_ERR(ec_child));
1578 RETURN(PTR_ERR(ec_child));
1581 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1582 if (child == NULL) {
1583 CERROR("Can not locate the child %s\n", lname->ln_name);
1584 lu_object_put(env, ec_child);
1589 CDEBUG(D_RPCTRACE, "Start setattr object "DFID"\n",
1590 PFID(lu_object_fid(child)));
1592 buf->lb_buf = info->eti_xattr_buf;
1593 buf->lb_len = sizeof(info->eti_xattr_buf);
1595 sprintf(name, "%s.test1", XATTR_USER_PREFIX);
1596 rc = mo_xattr_set(env, lu2md(child), buf, name,
1599 CERROR("Can not setattr child "DFID": rc = %d\n",
1600 PFID(lu_object_fid(child)), rc);
1601 lu_object_put(env, ec_child);
1604 CDEBUG(D_RPCTRACE, "End setattr object "DFID"\n",
1605 PFID(lu_object_fid(child)));
1607 lu_object_put(env, ec_child);
1612 static int echo_getattr_object(const struct lu_env *env,
1613 struct echo_device *ed,
1614 struct lu_object *ec_parent,
1615 __u64 id, int count)
1617 struct lu_object *parent;
1618 struct echo_thread_info *info = echo_env_info(env);
1619 struct lu_name *lname = &info->eti_lname;
1620 char *name = info->eti_name;
1621 struct md_attr *ma = &info->eti_ma;
1622 struct lu_device *ld = ed->ed_next;
1628 if (ec_parent == NULL)
1630 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1634 memset(ma, 0, sizeof(*ma));
1635 ma->ma_need |= MA_INODE | MA_LOV | MA_PFID | MA_HSM | MA_ACL_DEF;
1636 ma->ma_acl = info->eti_xattr_buf;
1637 ma->ma_acl_size = sizeof(info->eti_xattr_buf);
1639 for (i = 0; i < count; i++) {
1640 struct lu_object *ec_child, *child;
1643 echo_md_build_name(lname, name, id);
1644 echo_set_lmm_size(env, ld, ma);
1646 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1647 if (IS_ERR(ec_child)) {
1648 CERROR("Can't find child %s: rc = %ld\n",
1649 lname->ln_name, PTR_ERR(ec_child));
1650 RETURN(PTR_ERR(ec_child));
1653 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1654 if (child == NULL) {
1655 CERROR("Can not locate the child %s\n", lname->ln_name);
1656 lu_object_put(env, ec_child);
1660 CDEBUG(D_RPCTRACE, "Start getattr object "DFID"\n",
1661 PFID(lu_object_fid(child)));
1662 rc = echo_attr_get_complex(env, lu2md(child), ma);
1664 CERROR("Can not getattr child "DFID": rc = %d\n",
1665 PFID(lu_object_fid(child)), rc);
1666 lu_object_put(env, ec_child);
1669 CDEBUG(D_RPCTRACE, "End getattr object "DFID"\n",
1670 PFID(lu_object_fid(child)));
1672 lu_object_put(env, ec_child);
1678 static int echo_lookup_object(const struct lu_env *env,
1679 struct echo_device *ed,
1680 struct lu_object *ec_parent,
1681 __u64 id, int count)
1683 struct lu_object *parent;
1684 struct echo_thread_info *info = echo_env_info(env);
1685 struct lu_name *lname = &info->eti_lname;
1686 char *name = info->eti_name;
1687 struct lu_fid *fid = &info->eti_fid;
1688 struct lu_device *ld = ed->ed_next;
1692 if (ec_parent == NULL)
1694 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1698 /*prepare the requests*/
1699 for (i = 0; i < count; i++) {
1700 echo_md_build_name(lname, name, id);
1702 CDEBUG(D_RPCTRACE, "Start lookup object "DFID" %s %p\n",
1703 PFID(lu_object_fid(parent)), lname->ln_name, parent);
1705 rc = mdo_lookup(env, lu2md(parent), lname, fid, NULL);
1707 CERROR("Can not lookup child %s: rc = %d\n", name, rc);
1710 CDEBUG(D_RPCTRACE, "End lookup object "DFID" %s %p\n",
1711 PFID(lu_object_fid(parent)), lname->ln_name, parent);
1718 static int echo_md_destroy_internal(const struct lu_env *env,
1719 struct echo_device *ed,
1720 struct md_object *parent,
1721 struct lu_name *lname,
1724 struct lu_device *ld = ed->ed_next;
1725 struct lu_object *ec_child;
1726 struct lu_object *child;
1731 ec_child = echo_md_lookup(env, ed, parent, lname);
1732 if (IS_ERR(ec_child)) {
1733 CERROR("Can't find child %s: rc = %ld\n", lname->ln_name,
1735 RETURN(PTR_ERR(ec_child));
1738 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1739 if (child == NULL) {
1740 CERROR("Can not locate the child %s\n", lname->ln_name);
1741 GOTO(out_put, rc = -EINVAL);
1744 if (lu_object_remote(child)) {
1745 CERROR("Can not destroy remote object %s: rc = %d\n",
1746 lname->ln_name, -EPERM);
1747 GOTO(out_put, rc = -EPERM);
1749 CDEBUG(D_RPCTRACE, "Start destroy object "DFID" %s %p\n",
1750 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1752 rc = mdo_unlink(env, parent, lu2md(child), lname, ma, 0);
1754 CERROR("Can not unlink child %s: rc = %d\n",
1755 lname->ln_name, rc);
1758 CDEBUG(D_RPCTRACE, "End destroy object "DFID" %s %p\n",
1759 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1761 lu_object_put(env, ec_child);
1765 static int echo_destroy_object(const struct lu_env *env,
1766 struct echo_device *ed,
1767 struct lu_object *ec_parent,
1768 char *name, int namelen,
1769 __u64 id, __u32 mode,
1772 struct echo_thread_info *info = echo_env_info(env);
1773 struct lu_name *lname = &info->eti_lname;
1774 struct md_attr *ma = &info->eti_ma;
1775 struct lu_device *ld = ed->ed_next;
1776 struct lu_object *parent;
1781 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1785 memset(ma, 0, sizeof(*ma));
1786 ma->ma_attr.la_mode = mode;
1787 ma->ma_attr.la_valid = LA_CTIME;
1788 ma->ma_attr.la_ctime = cfs_time_current_64();
1789 ma->ma_need = MA_INODE;
1793 lname->ln_name = name;
1794 lname->ln_namelen = namelen;
1795 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
1800 /*prepare the requests*/
1801 for (i = 0; i < count; i++) {
1802 char *tmp_name = info->eti_name;
1805 echo_md_build_name(lname, tmp_name, id);
1807 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
1810 CERROR("Can not unlink child %s: rc = %d\n", name, rc);
1819 static struct lu_object *echo_resolve_path(const struct lu_env *env,
1820 struct echo_device *ed, char *path,
1823 struct lu_device *ld = ed->ed_next;
1824 struct md_device *md = lu2md_dev(ld);
1825 struct echo_thread_info *info = echo_env_info(env);
1826 struct lu_fid *fid = &info->eti_fid;
1827 struct lu_name *lname = &info->eti_lname;
1828 struct lu_object *parent = NULL;
1829 struct lu_object *child = NULL;
1833 /*Only support MDD layer right now*/
1834 rc = md->md_ops->mdo_root_get(env, md, fid);
1836 CERROR("get root error: rc = %d\n", rc);
1837 RETURN(ERR_PTR(rc));
1840 /* In the function below, .hs_keycmp resolves to
1841 * lu_obj_hop_keycmp() */
1842 /* coverity[overrun-buffer-val] */
1843 parent = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1844 if (IS_ERR(parent)) {
1845 CERROR("Can not find the parent "DFID": rc = %ld\n",
1846 PFID(fid), PTR_ERR(parent));
1851 struct lu_object *ld_parent;
1854 e = strsep(&path, "/");
1859 if (!path || path[0] == '\0')
1865 lname->ln_namelen = strlen(e);
1867 ld_parent = lu_object_locate(parent->lo_header, ld->ld_type);
1868 if (ld_parent == NULL) {
1869 lu_object_put(env, parent);
1874 child = echo_md_lookup(env, ed, lu2md(ld_parent), lname);
1875 lu_object_put(env, parent);
1876 if (IS_ERR(child)) {
1877 rc = (int)PTR_ERR(child);
1878 CERROR("lookup %s under parent "DFID": rc = %d\n",
1879 lname->ln_name, PFID(lu_object_fid(ld_parent)),
1886 RETURN(ERR_PTR(rc));
1891 static void echo_ucred_init(struct lu_env *env)
1893 struct lu_ucred *ucred = lu_ucred(env);
1895 ucred->uc_valid = UCRED_INVALID;
1897 ucred->uc_suppgids[0] = -1;
1898 ucred->uc_suppgids[1] = -1;
1900 ucred->uc_uid = ucred->uc_o_uid =
1901 from_kuid(&init_user_ns, current_uid());
1902 ucred->uc_gid = ucred->uc_o_gid =
1903 from_kgid(&init_user_ns, current_gid());
1904 ucred->uc_fsuid = ucred->uc_o_fsuid =
1905 from_kuid(&init_user_ns, current_fsuid());
1906 ucred->uc_fsgid = ucred->uc_o_fsgid =
1907 from_kgid(&init_user_ns, current_fsgid());
1908 ucred->uc_cap = cfs_curproc_cap_pack();
1910 /* remove fs privilege for non-root user. */
1911 if (ucred->uc_fsuid)
1912 ucred->uc_cap &= ~CFS_CAP_FS_MASK;
1913 ucred->uc_valid = UCRED_NEW;
1916 static void echo_ucred_fini(struct lu_env *env)
1918 struct lu_ucred *ucred = lu_ucred(env);
1919 ucred->uc_valid = UCRED_INIT;
1922 #define ECHO_MD_CTX_TAG (LCT_REMEMBER | LCT_MD_THREAD)
1923 #define ECHO_MD_SES_TAG (LCT_REMEMBER | LCT_SESSION | LCT_SERVER_SESSION)
1924 static int echo_md_handler(struct echo_device *ed, int command,
1925 char *path, int path_len, __u64 id, int count,
1926 struct obd_ioctl_data *data)
1928 struct echo_thread_info *info;
1929 struct lu_device *ld = ed->ed_next;
1932 struct lu_object *parent;
1934 int namelen = data->ioc_plen2;
1939 CERROR("MD echo client is not being initialized properly\n");
1943 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
1944 CERROR("Only support MDD layer right now!\n");
1948 env = cl_env_get(&refcheck);
1950 RETURN(PTR_ERR(env));
1952 rc = lu_env_refill_by_tags(env, ECHO_MD_CTX_TAG, ECHO_MD_SES_TAG);
1956 /* init big_lmm buffer */
1957 info = echo_env_info(env);
1958 LASSERT(info->eti_big_lmm == NULL);
1959 OBD_ALLOC_LARGE(info->eti_big_lmm, MIN_MD_SIZE);
1960 if (info->eti_big_lmm == NULL)
1961 GOTO(out_env, rc = -ENOMEM);
1962 info->eti_big_lmmsize = MIN_MD_SIZE;
1964 parent = echo_resolve_path(env, ed, path, path_len);
1965 if (IS_ERR(parent)) {
1966 CERROR("Can not resolve the path %s: rc = %ld\n", path,
1968 GOTO(out_free, rc = PTR_ERR(parent));
1972 OBD_ALLOC(name, namelen + 1);
1974 GOTO(out_put, rc = -ENOMEM);
1975 if (copy_from_user(name, data->ioc_pbuf2, namelen))
1976 GOTO(out_name, rc = -EFAULT);
1979 echo_ucred_init(env);
1982 case ECHO_MD_CREATE:
1983 case ECHO_MD_MKDIR: {
1984 struct echo_thread_info *info = echo_env_info(env);
1985 __u32 mode = data->ioc_obdo2.o_mode;
1986 struct lu_fid *fid = &info->eti_fid;
1987 int stripe_count = (int)data->ioc_obdo2.o_misc;
1988 int stripe_index = (int)data->ioc_obdo2.o_stripe_idx;
1990 rc = ostid_to_fid(fid, &data->ioc_obdo1.o_oi, 0);
1994 /* In the function below, .hs_keycmp resolves to
1995 * lu_obj_hop_keycmp() */
1996 /* coverity[overrun-buffer-val] */
1997 rc = echo_create_md_object(env, ed, parent, fid, name, namelen,
1998 id, mode, count, stripe_count,
2002 case ECHO_MD_DESTROY:
2003 case ECHO_MD_RMDIR: {
2004 __u32 mode = data->ioc_obdo2.o_mode;
2006 rc = echo_destroy_object(env, ed, parent, name, namelen,
2010 case ECHO_MD_LOOKUP:
2011 rc = echo_lookup_object(env, ed, parent, id, count);
2013 case ECHO_MD_GETATTR:
2014 rc = echo_getattr_object(env, ed, parent, id, count);
2016 case ECHO_MD_SETATTR:
2017 rc = echo_setattr_object(env, ed, parent, id, count);
2020 CERROR("unknown command %d\n", command);
2024 echo_ucred_fini(env);
2028 OBD_FREE(name, namelen + 1);
2030 lu_object_put(env, parent);
2032 LASSERT(info->eti_big_lmm);
2033 OBD_FREE_LARGE(info->eti_big_lmm, info->eti_big_lmmsize);
2034 info->eti_big_lmm = NULL;
2035 info->eti_big_lmmsize = 0;
2037 cl_env_put(env, &refcheck);
2040 #endif /* HAVE_SERVER_SUPPORT */
2042 static int echo_create_object(const struct lu_env *env, struct echo_device *ed,
2043 struct obdo *oa, struct obd_trans_info *oti)
2045 struct echo_object *eco;
2046 struct echo_client_obd *ec = ed->ed_ec;
2051 if (!(oa->o_valid & OBD_MD_FLID) ||
2052 !(oa->o_valid & OBD_MD_FLGROUP) ||
2053 !fid_seq_is_echo(ostid_seq(&oa->o_oi))) {
2054 CERROR("invalid oid "DOSTID"\n", POSTID(&oa->o_oi));
2058 if (ostid_id(&oa->o_oi) == 0)
2059 ostid_set_id(&oa->o_oi, ++last_object_id);
2061 rc = obd_create(env, ec->ec_exp, oa, oti);
2063 CERROR("Cannot create objects: rc = %d\n", rc);
2069 oa->o_valid |= OBD_MD_FLID;
2071 eco = cl_echo_object_find(ed, &oa->o_oi);
2073 GOTO(failed, rc = PTR_ERR(eco));
2074 cl_echo_object_put(eco);
2076 CDEBUG(D_INFO, "oa oid "DOSTID"\n", POSTID(&oa->o_oi));
2080 if (created && rc != 0)
2081 obd_destroy(env, ec->ec_exp, oa, NULL, oti, NULL, NULL);
2084 CERROR("create object failed with: rc = %d\n", rc);
2089 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
2092 struct echo_object *eco;
2096 if (!(oa->o_valid & OBD_MD_FLID) ||
2097 !(oa->o_valid & OBD_MD_FLGROUP) ||
2098 ostid_id(&oa->o_oi) == 0) {
2099 CERROR("invalid oid "DOSTID"\n", POSTID(&oa->o_oi));
2104 eco = cl_echo_object_find(ed, &oa->o_oi);
2113 static void echo_put_object(struct echo_object *eco)
2117 rc = cl_echo_object_put(eco);
2119 CERROR("%s: echo client drop an object failed: rc = %d\n",
2120 eco->eo_dev->ed_ec->ec_exp->exp_obd->obd_name, rc);
2124 echo_client_page_debug_setup(struct page *page, int rw, obd_id id,
2125 obd_off offset, obd_off count)
2132 /* no partial pages on the client */
2133 LASSERT(count == PAGE_CACHE_SIZE);
2137 for (delta = 0; delta < PAGE_CACHE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2138 if (rw == OBD_BRW_WRITE) {
2139 stripe_off = offset + delta;
2142 stripe_off = 0xdeadbeef00c0ffeeULL;
2143 stripe_id = 0xdeadbeef00c0ffeeULL;
2145 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
2146 stripe_off, stripe_id);
2153 echo_client_page_debug_check(struct page *page, obd_id id, obd_off offset,
2163 /* no partial pages on the client */
2164 LASSERT(count == PAGE_CACHE_SIZE);
2168 for (rc = delta = 0; delta < PAGE_CACHE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2169 stripe_off = offset + delta;
2172 rc2 = block_debug_check("test_brw",
2173 addr + delta, OBD_ECHO_BLOCK_SIZE,
2174 stripe_off, stripe_id);
2176 CERROR ("Error in echo object "LPX64"\n", id);
2185 static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
2186 struct echo_object *eco, obd_off offset,
2187 obd_size count, int async,
2188 struct obd_trans_info *oti)
2191 struct brw_page *pga;
2192 struct brw_page *pgp;
2193 struct page **pages;
2202 verify = (ostid_id(&oa->o_oi) != ECHO_PERSISTENT_OBJID &&
2203 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
2204 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
2206 gfp_mask = ((ostid_id(&oa->o_oi) & 2) == 0) ? GFP_IOFS : GFP_HIGHUSER;
2208 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
2211 (count & (~CFS_PAGE_MASK)) != 0)
2214 /* XXX think again with misaligned I/O */
2215 npages = count >> PAGE_CACHE_SHIFT;
2217 if (rw == OBD_BRW_WRITE)
2218 brw_flags = OBD_BRW_ASYNC;
2220 OBD_ALLOC(pga, npages * sizeof(*pga));
2224 OBD_ALLOC(pages, npages * sizeof(*pages));
2225 if (pages == NULL) {
2226 OBD_FREE(pga, npages * sizeof(*pga));
2230 for (i = 0, pgp = pga, off = offset;
2232 i++, pgp++, off += PAGE_CACHE_SIZE) {
2234 LASSERT (pgp->pg == NULL); /* for cleanup */
2237 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
2238 if (pgp->pg == NULL)
2242 pgp->count = PAGE_CACHE_SIZE;
2244 pgp->flag = brw_flags;
2247 echo_client_page_debug_setup(pgp->pg, rw,
2248 ostid_id(&oa->o_oi), off,
2252 /* brw mode can only be used at client */
2253 LASSERT(ed->ed_next != NULL);
2254 rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
2257 if (rc != 0 || rw != OBD_BRW_READ)
2260 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
2261 if (pgp->pg == NULL)
2266 vrc = echo_client_page_debug_check(pgp->pg,
2267 ostid_id(&oa->o_oi),
2268 pgp->off, pgp->count);
2269 if (vrc != 0 && rc == 0)
2272 OBD_PAGE_FREE(pgp->pg);
2274 OBD_FREE(pga, npages * sizeof(*pga));
2275 OBD_FREE(pages, npages * sizeof(*pages));
2279 static int echo_client_prep_commit(const struct lu_env *env,
2280 struct obd_export *exp, int rw,
2281 struct obdo *oa, struct echo_object *eco,
2282 obd_off offset, obd_size count,
2283 obd_size batch, struct obd_trans_info *oti,
2286 struct obd_ioobj ioo;
2287 struct niobuf_local *lnb;
2288 struct niobuf_remote *rnb;
2290 obd_size npages, tot_pages;
2291 int i, ret = 0, brw_flags = 0;
2295 if (count <= 0 || (count & ~PAGE_CACHE_MASK) != 0)
2298 npages = batch >> PAGE_CACHE_SHIFT;
2299 tot_pages = count >> PAGE_CACHE_SHIFT;
2301 OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
2302 OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
2304 if (lnb == NULL || rnb == NULL)
2305 GOTO(out, ret = -ENOMEM);
2307 if (rw == OBD_BRW_WRITE && async)
2308 brw_flags |= OBD_BRW_ASYNC;
2310 obdo_to_ioobj(oa, &ioo);
2314 for(; tot_pages; tot_pages -= npages) {
2317 if (tot_pages < npages)
2320 for (i = 0; i < npages; i++, off += PAGE_CACHE_SIZE) {
2321 rnb[i].rnb_offset = off;
2322 rnb[i].rnb_len = PAGE_CACHE_SIZE;
2323 rnb[i].rnb_flags = brw_flags;
2326 ioo.ioo_bufcnt = npages;
2329 ret = obd_preprw(env, rw, exp, oa, 1, &ioo, rnb, &lpages,
2333 LASSERT(lpages == npages);
2335 for (i = 0; i < lpages; i++) {
2336 struct page *page = lnb[i].lnb_page;
2338 /* read past eof? */
2339 if (page == NULL && lnb[i].lnb_rc == 0)
2343 lnb[i].lnb_flags |= OBD_BRW_ASYNC;
2345 if (ostid_id(&oa->o_oi) == ECHO_PERSISTENT_OBJID ||
2346 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
2347 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
2350 if (rw == OBD_BRW_WRITE)
2351 echo_client_page_debug_setup(page, rw,
2352 ostid_id(&oa->o_oi),
2356 echo_client_page_debug_check(page,
2357 ostid_id(&oa->o_oi),
2362 ret = obd_commitrw(env, rw, exp, oa, 1, &ioo,
2363 rnb, npages, lnb, oti, ret);
2367 /* Reset oti otherwise it would confuse ldiskfs. */
2368 memset(oti, 0, sizeof(*oti));
2370 /* Reuse env context. */
2371 lu_context_exit((struct lu_context *)&env->le_ctx);
2372 lu_context_enter((struct lu_context *)&env->le_ctx);
2377 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
2379 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
2383 static int echo_client_brw_ioctl(const struct lu_env *env, int rw,
2384 struct obd_export *exp,
2385 struct obd_ioctl_data *data,
2386 struct obd_trans_info *dummy_oti)
2388 struct obd_device *obd = class_exp2obd(exp);
2389 struct echo_device *ed = obd2echo_dev(obd);
2390 struct echo_client_obd *ec = ed->ed_ec;
2391 struct obdo *oa = &data->ioc_obdo1;
2392 struct echo_object *eco;
2398 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2400 rc = echo_get_object(&eco, ed, oa);
2404 oa->o_valid &= ~OBD_MD_FLHANDLE;
2406 /* OFD/obdfilter works only via prep/commit */
2407 test_mode = (long)data->ioc_pbuf1;
2408 if (ed->ed_next == NULL && test_mode != 3) {
2410 data->ioc_plen1 = data->ioc_count;
2416 /* Truncate batch size to maximum */
2417 if (data->ioc_plen1 > PTLRPC_MAX_BRW_SIZE)
2418 data->ioc_plen1 = PTLRPC_MAX_BRW_SIZE;
2420 switch (test_mode) {
2424 rc = echo_client_kbrw(ed, rw, oa,
2425 eco, data->ioc_offset,
2426 data->ioc_count, async, dummy_oti);
2429 rc = echo_client_prep_commit(env, ec->ec_exp, rw, oa,
2430 eco, data->ioc_offset,
2431 data->ioc_count, data->ioc_plen1,
2437 echo_put_object(eco);
2442 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2443 void *karg, void *uarg)
2445 #ifdef HAVE_SERVER_SUPPORT
2446 struct tgt_session_info *tsi;
2448 struct obd_device *obd = exp->exp_obd;
2449 struct echo_device *ed = obd2echo_dev(obd);
2450 struct echo_client_obd *ec = ed->ed_ec;
2451 struct echo_object *eco;
2452 struct obd_ioctl_data *data = karg;
2453 struct obd_trans_info dummy_oti;
2455 struct oti_req_ack_lock *ack_lock;
2458 int rw = OBD_BRW_READ;
2461 #ifdef HAVE_SERVER_SUPPORT
2462 struct lu_context echo_session;
2466 memset(&dummy_oti, 0, sizeof(dummy_oti));
2468 oa = &data->ioc_obdo1;
2469 if (!(oa->o_valid & OBD_MD_FLGROUP)) {
2470 oa->o_valid |= OBD_MD_FLGROUP;
2471 ostid_set_seq_echo(&oa->o_oi);
2474 /* This FID is unpacked just for validation at this point */
2475 rc = ostid_to_fid(&fid, &oa->o_oi, 0);
2483 rc = lu_env_init(env, LCT_DT_THREAD);
2485 GOTO(out_alloc, rc = -ENOMEM);
2487 #ifdef HAVE_SERVER_SUPPORT
2488 env->le_ses = &echo_session;
2489 rc = lu_context_init(env->le_ses, LCT_SERVER_SESSION | LCT_NOREF);
2490 if (unlikely(rc < 0))
2492 lu_context_enter(env->le_ses);
2494 tsi = tgt_ses_info(env);
2495 tsi->tsi_exp = ec->ec_exp;
2496 tsi->tsi_jobid = NULL;
2499 case OBD_IOC_CREATE: /* may create echo object */
2500 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2501 GOTO (out, rc = -EPERM);
2503 rc = echo_create_object(env, ed, oa, &dummy_oti);
2506 #ifdef HAVE_SERVER_SUPPORT
2507 case OBD_IOC_ECHO_MD: {
2514 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2515 GOTO(out, rc = -EPERM);
2517 count = data->ioc_count;
2518 cmd = data->ioc_command;
2520 id = data->ioc_obdo2.o_oi.oi.oi_id;
2521 dirlen = data->ioc_plen1;
2522 OBD_ALLOC(dir, dirlen + 1);
2524 GOTO(out, rc = -ENOMEM);
2526 if (copy_from_user(dir, data->ioc_pbuf1, dirlen)) {
2527 OBD_FREE(dir, data->ioc_plen1 + 1);
2528 GOTO(out, rc = -EFAULT);
2531 rc = echo_md_handler(ed, cmd, dir, dirlen, id, count, data);
2532 OBD_FREE(dir, dirlen + 1);
2535 case OBD_IOC_ECHO_ALLOC_SEQ: {
2536 struct lu_env *cl_env;
2541 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2542 GOTO(out, rc = -EPERM);
2544 cl_env = cl_env_get(&refcheck);
2546 GOTO(out, rc = PTR_ERR(cl_env));
2548 rc = lu_env_refill_by_tags(cl_env, ECHO_MD_CTX_TAG,
2551 cl_env_put(cl_env, &refcheck);
2555 rc = seq_client_get_seq(cl_env, ed->ed_cl_seq, &seq);
2556 cl_env_put(cl_env, &refcheck);
2558 CERROR("%s: Can not alloc seq: rc = %d\n",
2563 if (copy_to_user(data->ioc_pbuf1, &seq, data->ioc_plen1))
2566 max_count = LUSTRE_METADATA_SEQ_MAX_WIDTH;
2567 if (copy_to_user(data->ioc_pbuf2, &max_count,
2572 #endif /* HAVE_SERVER_SUPPORT */
2573 case OBD_IOC_DESTROY:
2574 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2575 GOTO (out, rc = -EPERM);
2577 rc = echo_get_object(&eco, ed, oa);
2579 rc = obd_destroy(env, ec->ec_exp, oa, NULL,
2580 &dummy_oti, NULL, NULL);
2582 eco->eo_deleted = 1;
2583 echo_put_object(eco);
2587 case OBD_IOC_GETATTR:
2588 rc = echo_get_object(&eco, ed, oa);
2590 struct obd_info oinfo = {
2594 rc = obd_getattr(env, ec->ec_exp, &oinfo);
2595 echo_put_object(eco);
2599 case OBD_IOC_SETATTR:
2600 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2601 GOTO (out, rc = -EPERM);
2603 rc = echo_get_object(&eco, ed, oa);
2605 struct obd_info oinfo = {
2609 rc = obd_setattr(env, ec->ec_exp, &oinfo, NULL);
2610 echo_put_object(eco);
2614 case OBD_IOC_BRW_WRITE:
2615 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2616 GOTO (out, rc = -EPERM);
2620 case OBD_IOC_BRW_READ:
2621 rc = echo_client_brw_ioctl(env, rw, exp, data, &dummy_oti);
2625 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
2626 GOTO (out, rc = -ENOTTY);
2631 #ifdef HAVE_SERVER_SUPPORT
2632 lu_context_exit(env->le_ses);
2633 lu_context_fini(env->le_ses);
2640 /* XXX this should be in a helper also called by target_send_reply */
2641 for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
2643 if (!ack_lock->mode)
2645 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
2651 static int echo_client_setup(const struct lu_env *env,
2652 struct obd_device *obddev, struct lustre_cfg *lcfg)
2654 struct echo_client_obd *ec = &obddev->u.echo_client;
2655 struct obd_device *tgt;
2656 struct obd_uuid echo_uuid = { "ECHO_UUID" };
2657 struct obd_connect_data *ocd = NULL;
2661 if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
2662 CERROR("requires a TARGET OBD name\n");
2666 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
2667 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
2668 CERROR("device not attached or not set up (%s)\n",
2669 lustre_cfg_string(lcfg, 1));
2673 spin_lock_init(&ec->ec_lock);
2674 INIT_LIST_HEAD(&ec->ec_objects);
2675 INIT_LIST_HEAD(&ec->ec_locks);
2678 if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
2679 #ifdef HAVE_SERVER_SUPPORT
2680 lu_context_tags_update(ECHO_MD_CTX_TAG);
2681 lu_session_tags_update(ECHO_MD_SES_TAG);
2683 CERROR("Local operations are NOT supported on client side. "
2684 "Only remote operations are supported. Metadata client "
2685 "must be run on server side.\n");
2690 OBD_ALLOC(ocd, sizeof(*ocd));
2692 CERROR("Can't alloc ocd connecting to %s\n",
2693 lustre_cfg_string(lcfg, 1));
2697 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
2698 OBD_CONNECT_BRW_SIZE |
2699 OBD_CONNECT_GRANT | OBD_CONNECT_FULL20 |
2700 OBD_CONNECT_64BITHASH | OBD_CONNECT_LVB_TYPE |
2702 ocd->ocd_brw_size = DT_MAX_BRW_SIZE;
2703 ocd->ocd_version = LUSTRE_VERSION_CODE;
2704 ocd->ocd_group = FID_SEQ_ECHO;
2706 rc = obd_connect(env, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
2708 /* Turn off pinger because it connects to tgt obd directly. */
2709 spin_lock(&tgt->obd_dev_lock);
2710 list_del_init(&ec->ec_exp->exp_obd_chain_timed);
2711 spin_unlock(&tgt->obd_dev_lock);
2714 OBD_FREE(ocd, sizeof(*ocd));
2717 CERROR("fail to connect to device %s\n",
2718 lustre_cfg_string(lcfg, 1));
2725 static int echo_client_cleanup(struct obd_device *obddev)
2727 struct echo_device *ed = obd2echo_dev(obddev);
2728 struct echo_client_obd *ec = &obddev->u.echo_client;
2732 /*Do nothing for Metadata echo client*/
2736 if (ed->ed_next_ismd) {
2737 #ifdef HAVE_SERVER_SUPPORT
2738 lu_context_tags_clear(ECHO_MD_CTX_TAG);
2739 lu_session_tags_clear(ECHO_MD_SES_TAG);
2741 CERROR("This is client-side only module, does not support "
2742 "metadata echo client.\n");
2747 if (!list_empty(&obddev->obd_exports)) {
2748 CERROR("still has clients!\n");
2752 LASSERT(atomic_read(&ec->ec_exp->exp_refcount) > 0);
2753 rc = obd_disconnect(ec->ec_exp);
2755 CERROR("fail to disconnect device: %d\n", rc);
2760 static int echo_client_connect(const struct lu_env *env,
2761 struct obd_export **exp,
2762 struct obd_device *src, struct obd_uuid *cluuid,
2763 struct obd_connect_data *data, void *localdata)
2766 struct lustre_handle conn = { 0 };
2769 rc = class_connect(&conn, src, cluuid);
2771 *exp = class_conn2export(&conn);
2777 static int echo_client_disconnect(struct obd_export *exp)
2783 GOTO(out, rc = -EINVAL);
2785 rc = class_disconnect(exp);
2791 static struct obd_ops echo_client_obd_ops = {
2792 .o_owner = THIS_MODULE,
2793 .o_iocontrol = echo_client_iocontrol,
2794 .o_connect = echo_client_connect,
2795 .o_disconnect = echo_client_disconnect
2798 int echo_client_init(void)
2802 rc = lu_kmem_init(echo_caches);
2804 rc = class_register_type(&echo_client_obd_ops, NULL, true, NULL,
2805 LUSTRE_ECHO_CLIENT_NAME,
2808 lu_kmem_fini(echo_caches);
2813 void echo_client_exit(void)
2815 class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
2816 lu_kmem_fini(echo_caches);
2819 static int __init obdecho_init(void)
2824 LCONSOLE_INFO("Echo OBD driver; http://www.lustre.org/\n");
2826 LASSERT(PAGE_CACHE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
2828 # ifdef HAVE_SERVER_SUPPORT
2829 rc = echo_persistent_pages_init();
2833 rc = class_register_type(&echo_obd_ops, NULL, true, NULL,
2834 LUSTRE_ECHO_NAME, NULL);
2839 rc = echo_client_init();
2841 # ifdef HAVE_SERVER_SUPPORT
2845 class_unregister_type(LUSTRE_ECHO_NAME);
2847 echo_persistent_pages_fini();
2853 static void /*__exit*/ obdecho_exit(void)
2857 # ifdef HAVE_SERVER_SUPPORT
2858 class_unregister_type(LUSTRE_ECHO_NAME);
2859 echo_persistent_pages_fini();
2863 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2864 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
2865 MODULE_LICENSE("GPL");
2867 cfs_module(obdecho, LUSTRE_VERSION_STRING, obdecho_init, obdecho_exit);
2869 /** @} echo_client */