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, 2014, 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, u64 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 u64 start, u64 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, u64 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 u64 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, oti);
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);
2123 static void echo_client_page_debug_setup(struct page *page, int rw, u64 id,
2124 u64 offset, u64 count)
2131 /* no partial pages on the client */
2132 LASSERT(count == PAGE_CACHE_SIZE);
2136 for (delta = 0; delta < PAGE_CACHE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2137 if (rw == OBD_BRW_WRITE) {
2138 stripe_off = offset + delta;
2141 stripe_off = 0xdeadbeef00c0ffeeULL;
2142 stripe_id = 0xdeadbeef00c0ffeeULL;
2144 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
2145 stripe_off, stripe_id);
2152 echo_client_page_debug_check(struct page *page, u64 id, u64 offset, u64 count)
2161 /* no partial pages on the client */
2162 LASSERT(count == PAGE_CACHE_SIZE);
2166 for (rc = delta = 0; delta < PAGE_CACHE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2167 stripe_off = offset + delta;
2170 rc2 = block_debug_check("test_brw",
2171 addr + delta, OBD_ECHO_BLOCK_SIZE,
2172 stripe_off, stripe_id);
2174 CERROR ("Error in echo object "LPX64"\n", id);
2183 static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
2184 struct echo_object *eco, u64 offset,
2185 u64 count, int async,
2186 struct obd_trans_info *oti)
2189 struct brw_page *pga;
2190 struct brw_page *pgp;
2191 struct page **pages;
2200 verify = (ostid_id(&oa->o_oi) != ECHO_PERSISTENT_OBJID &&
2201 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
2202 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
2204 gfp_mask = ((ostid_id(&oa->o_oi) & 2) == 0) ? GFP_IOFS : GFP_HIGHUSER;
2206 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
2208 if ((count & (~CFS_PAGE_MASK)) != 0)
2211 /* XXX think again with misaligned I/O */
2212 npages = count >> PAGE_CACHE_SHIFT;
2214 if (rw == OBD_BRW_WRITE)
2215 brw_flags = OBD_BRW_ASYNC;
2217 OBD_ALLOC(pga, npages * sizeof(*pga));
2221 OBD_ALLOC(pages, npages * sizeof(*pages));
2222 if (pages == NULL) {
2223 OBD_FREE(pga, npages * sizeof(*pga));
2227 for (i = 0, pgp = pga, off = offset;
2229 i++, pgp++, off += PAGE_CACHE_SIZE) {
2231 LASSERT (pgp->pg == NULL); /* for cleanup */
2234 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
2235 if (pgp->pg == NULL)
2239 pgp->count = PAGE_CACHE_SIZE;
2241 pgp->flag = brw_flags;
2244 echo_client_page_debug_setup(pgp->pg, rw,
2245 ostid_id(&oa->o_oi), off,
2249 /* brw mode can only be used at client */
2250 LASSERT(ed->ed_next != NULL);
2251 rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
2254 if (rc != 0 || rw != OBD_BRW_READ)
2257 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
2258 if (pgp->pg == NULL)
2263 vrc = echo_client_page_debug_check(pgp->pg,
2264 ostid_id(&oa->o_oi),
2265 pgp->off, pgp->count);
2266 if (vrc != 0 && rc == 0)
2269 OBD_PAGE_FREE(pgp->pg);
2271 OBD_FREE(pga, npages * sizeof(*pga));
2272 OBD_FREE(pages, npages * sizeof(*pages));
2276 static int echo_client_prep_commit(const struct lu_env *env,
2277 struct obd_export *exp, int rw,
2278 struct obdo *oa, struct echo_object *eco,
2279 u64 offset, u64 count,
2280 u64 batch, struct obd_trans_info *oti,
2283 struct obd_ioobj ioo;
2284 struct niobuf_local *lnb;
2285 struct niobuf_remote *rnb;
2287 u64 npages, tot_pages;
2288 int i, ret = 0, brw_flags = 0;
2292 if (count <= 0 || (count & ~PAGE_CACHE_MASK) != 0)
2295 npages = batch >> PAGE_CACHE_SHIFT;
2296 tot_pages = count >> PAGE_CACHE_SHIFT;
2298 OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
2299 OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
2301 if (lnb == NULL || rnb == NULL)
2302 GOTO(out, ret = -ENOMEM);
2304 if (rw == OBD_BRW_WRITE && async)
2305 brw_flags |= OBD_BRW_ASYNC;
2307 obdo_to_ioobj(oa, &ioo);
2311 for(; tot_pages; tot_pages -= npages) {
2314 if (tot_pages < npages)
2317 for (i = 0; i < npages; i++, off += PAGE_CACHE_SIZE) {
2318 rnb[i].rnb_offset = off;
2319 rnb[i].rnb_len = PAGE_CACHE_SIZE;
2320 rnb[i].rnb_flags = brw_flags;
2323 ioo.ioo_bufcnt = npages;
2326 ret = obd_preprw(env, rw, exp, oa, 1, &ioo, rnb, &lpages,
2330 LASSERT(lpages == npages);
2332 for (i = 0; i < lpages; i++) {
2333 struct page *page = lnb[i].lnb_page;
2335 /* read past eof? */
2336 if (page == NULL && lnb[i].lnb_rc == 0)
2340 lnb[i].lnb_flags |= OBD_BRW_ASYNC;
2342 if (ostid_id(&oa->o_oi) == ECHO_PERSISTENT_OBJID ||
2343 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
2344 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
2347 if (rw == OBD_BRW_WRITE)
2348 echo_client_page_debug_setup(page, rw,
2349 ostid_id(&oa->o_oi),
2353 echo_client_page_debug_check(page,
2354 ostid_id(&oa->o_oi),
2359 ret = obd_commitrw(env, rw, exp, oa, 1, &ioo,
2360 rnb, npages, lnb, oti, ret);
2364 /* Reset oti otherwise it would confuse ldiskfs. */
2365 memset(oti, 0, sizeof(*oti));
2367 /* Reuse env context. */
2368 lu_context_exit((struct lu_context *)&env->le_ctx);
2369 lu_context_enter((struct lu_context *)&env->le_ctx);
2374 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
2376 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
2380 static int echo_client_brw_ioctl(const struct lu_env *env, int rw,
2381 struct obd_export *exp,
2382 struct obd_ioctl_data *data,
2383 struct obd_trans_info *dummy_oti)
2385 struct obd_device *obd = class_exp2obd(exp);
2386 struct echo_device *ed = obd2echo_dev(obd);
2387 struct echo_client_obd *ec = ed->ed_ec;
2388 struct obdo *oa = &data->ioc_obdo1;
2389 struct echo_object *eco;
2395 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2397 rc = echo_get_object(&eco, ed, oa);
2401 oa->o_valid &= ~OBD_MD_FLHANDLE;
2403 /* OFD/obdfilter works only via prep/commit */
2404 test_mode = (long)data->ioc_pbuf1;
2405 if (ed->ed_next == NULL && test_mode != 3) {
2407 data->ioc_plen1 = data->ioc_count;
2413 /* Truncate batch size to maximum */
2414 if (data->ioc_plen1 > PTLRPC_MAX_BRW_SIZE)
2415 data->ioc_plen1 = PTLRPC_MAX_BRW_SIZE;
2417 switch (test_mode) {
2421 rc = echo_client_kbrw(ed, rw, oa,
2422 eco, data->ioc_offset,
2423 data->ioc_count, async, dummy_oti);
2426 rc = echo_client_prep_commit(env, ec->ec_exp, rw, oa,
2427 eco, data->ioc_offset,
2428 data->ioc_count, data->ioc_plen1,
2434 echo_put_object(eco);
2439 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2440 void *karg, void *uarg)
2442 #ifdef HAVE_SERVER_SUPPORT
2443 struct tgt_session_info *tsi;
2445 struct obd_device *obd = exp->exp_obd;
2446 struct echo_device *ed = obd2echo_dev(obd);
2447 struct echo_client_obd *ec = ed->ed_ec;
2448 struct echo_object *eco;
2449 struct obd_ioctl_data *data = karg;
2450 struct obd_trans_info dummy_oti;
2452 struct oti_req_ack_lock *ack_lock;
2455 int rw = OBD_BRW_READ;
2458 #ifdef HAVE_SERVER_SUPPORT
2459 struct lu_context echo_session;
2463 memset(&dummy_oti, 0, sizeof(dummy_oti));
2465 oa = &data->ioc_obdo1;
2466 if (!(oa->o_valid & OBD_MD_FLGROUP)) {
2467 oa->o_valid |= OBD_MD_FLGROUP;
2468 ostid_set_seq_echo(&oa->o_oi);
2471 /* This FID is unpacked just for validation at this point */
2472 rc = ostid_to_fid(&fid, &oa->o_oi, 0);
2480 rc = lu_env_init(env, LCT_DT_THREAD);
2482 GOTO(out_alloc, rc = -ENOMEM);
2484 #ifdef HAVE_SERVER_SUPPORT
2485 env->le_ses = &echo_session;
2486 rc = lu_context_init(env->le_ses, LCT_SERVER_SESSION | LCT_NOREF);
2487 if (unlikely(rc < 0))
2489 lu_context_enter(env->le_ses);
2491 tsi = tgt_ses_info(env);
2492 tsi->tsi_exp = ec->ec_exp;
2493 tsi->tsi_jobid = NULL;
2496 case OBD_IOC_CREATE: /* may create echo object */
2497 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2498 GOTO (out, rc = -EPERM);
2500 rc = echo_create_object(env, ed, oa, &dummy_oti);
2503 #ifdef HAVE_SERVER_SUPPORT
2504 case OBD_IOC_ECHO_MD: {
2511 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2512 GOTO(out, rc = -EPERM);
2514 count = data->ioc_count;
2515 cmd = data->ioc_command;
2517 id = data->ioc_obdo2.o_oi.oi.oi_id;
2518 dirlen = data->ioc_plen1;
2519 OBD_ALLOC(dir, dirlen + 1);
2521 GOTO(out, rc = -ENOMEM);
2523 if (copy_from_user(dir, data->ioc_pbuf1, dirlen)) {
2524 OBD_FREE(dir, data->ioc_plen1 + 1);
2525 GOTO(out, rc = -EFAULT);
2528 rc = echo_md_handler(ed, cmd, dir, dirlen, id, count, data);
2529 OBD_FREE(dir, dirlen + 1);
2532 case OBD_IOC_ECHO_ALLOC_SEQ: {
2533 struct lu_env *cl_env;
2538 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2539 GOTO(out, rc = -EPERM);
2541 cl_env = cl_env_get(&refcheck);
2543 GOTO(out, rc = PTR_ERR(cl_env));
2545 rc = lu_env_refill_by_tags(cl_env, ECHO_MD_CTX_TAG,
2548 cl_env_put(cl_env, &refcheck);
2552 rc = seq_client_get_seq(cl_env, ed->ed_cl_seq, &seq);
2553 cl_env_put(cl_env, &refcheck);
2555 CERROR("%s: Can not alloc seq: rc = %d\n",
2560 if (copy_to_user(data->ioc_pbuf1, &seq, data->ioc_plen1))
2563 max_count = LUSTRE_METADATA_SEQ_MAX_WIDTH;
2564 if (copy_to_user(data->ioc_pbuf2, &max_count,
2569 #endif /* HAVE_SERVER_SUPPORT */
2570 case OBD_IOC_DESTROY:
2571 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2572 GOTO (out, rc = -EPERM);
2574 rc = echo_get_object(&eco, ed, oa);
2576 rc = obd_destroy(env, ec->ec_exp, oa, &dummy_oti);
2578 eco->eo_deleted = 1;
2579 echo_put_object(eco);
2583 case OBD_IOC_GETATTR:
2584 rc = echo_get_object(&eco, ed, oa);
2586 struct obd_info oinfo = {
2590 rc = obd_getattr(env, ec->ec_exp, &oinfo);
2591 echo_put_object(eco);
2595 case OBD_IOC_SETATTR:
2596 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2597 GOTO (out, rc = -EPERM);
2599 rc = echo_get_object(&eco, ed, oa);
2601 struct obd_info oinfo = {
2605 rc = obd_setattr(env, ec->ec_exp, &oinfo, NULL);
2606 echo_put_object(eco);
2610 case OBD_IOC_BRW_WRITE:
2611 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2612 GOTO (out, rc = -EPERM);
2616 case OBD_IOC_BRW_READ:
2617 rc = echo_client_brw_ioctl(env, rw, exp, data, &dummy_oti);
2621 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
2622 GOTO (out, rc = -ENOTTY);
2627 #ifdef HAVE_SERVER_SUPPORT
2628 lu_context_exit(env->le_ses);
2629 lu_context_fini(env->le_ses);
2636 /* XXX this should be in a helper also called by target_send_reply */
2637 for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
2639 if (!ack_lock->mode)
2641 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
2647 static int echo_client_setup(const struct lu_env *env,
2648 struct obd_device *obddev, struct lustre_cfg *lcfg)
2650 struct echo_client_obd *ec = &obddev->u.echo_client;
2651 struct obd_device *tgt;
2652 struct obd_uuid echo_uuid = { "ECHO_UUID" };
2653 struct obd_connect_data *ocd = NULL;
2657 if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
2658 CERROR("requires a TARGET OBD name\n");
2662 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
2663 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
2664 CERROR("device not attached or not set up (%s)\n",
2665 lustre_cfg_string(lcfg, 1));
2669 spin_lock_init(&ec->ec_lock);
2670 INIT_LIST_HEAD(&ec->ec_objects);
2671 INIT_LIST_HEAD(&ec->ec_locks);
2674 if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
2675 #ifdef HAVE_SERVER_SUPPORT
2676 lu_context_tags_update(ECHO_MD_CTX_TAG);
2677 lu_session_tags_update(ECHO_MD_SES_TAG);
2679 CERROR("Local operations are NOT supported on client side. "
2680 "Only remote operations are supported. Metadata client "
2681 "must be run on server side.\n");
2686 OBD_ALLOC(ocd, sizeof(*ocd));
2688 CERROR("Can't alloc ocd connecting to %s\n",
2689 lustre_cfg_string(lcfg, 1));
2693 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
2694 OBD_CONNECT_BRW_SIZE |
2695 OBD_CONNECT_GRANT | OBD_CONNECT_FULL20 |
2696 OBD_CONNECT_64BITHASH | OBD_CONNECT_LVB_TYPE |
2698 ocd->ocd_brw_size = DT_MAX_BRW_SIZE;
2699 ocd->ocd_version = LUSTRE_VERSION_CODE;
2700 ocd->ocd_group = FID_SEQ_ECHO;
2702 rc = obd_connect(env, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
2704 /* Turn off pinger because it connects to tgt obd directly. */
2705 spin_lock(&tgt->obd_dev_lock);
2706 list_del_init(&ec->ec_exp->exp_obd_chain_timed);
2707 spin_unlock(&tgt->obd_dev_lock);
2710 OBD_FREE(ocd, sizeof(*ocd));
2713 CERROR("fail to connect to device %s\n",
2714 lustre_cfg_string(lcfg, 1));
2721 static int echo_client_cleanup(struct obd_device *obddev)
2723 struct echo_device *ed = obd2echo_dev(obddev);
2724 struct echo_client_obd *ec = &obddev->u.echo_client;
2728 /*Do nothing for Metadata echo client*/
2732 if (ed->ed_next_ismd) {
2733 #ifdef HAVE_SERVER_SUPPORT
2734 lu_context_tags_clear(ECHO_MD_CTX_TAG);
2735 lu_session_tags_clear(ECHO_MD_SES_TAG);
2737 CERROR("This is client-side only module, does not support "
2738 "metadata echo client.\n");
2743 if (!list_empty(&obddev->obd_exports)) {
2744 CERROR("still has clients!\n");
2748 LASSERT(atomic_read(&ec->ec_exp->exp_refcount) > 0);
2749 rc = obd_disconnect(ec->ec_exp);
2751 CERROR("fail to disconnect device: %d\n", rc);
2756 static int echo_client_connect(const struct lu_env *env,
2757 struct obd_export **exp,
2758 struct obd_device *src, struct obd_uuid *cluuid,
2759 struct obd_connect_data *data, void *localdata)
2762 struct lustre_handle conn = { 0 };
2765 rc = class_connect(&conn, src, cluuid);
2767 *exp = class_conn2export(&conn);
2773 static int echo_client_disconnect(struct obd_export *exp)
2779 GOTO(out, rc = -EINVAL);
2781 rc = class_disconnect(exp);
2787 static struct obd_ops echo_client_obd_ops = {
2788 .o_owner = THIS_MODULE,
2789 .o_iocontrol = echo_client_iocontrol,
2790 .o_connect = echo_client_connect,
2791 .o_disconnect = echo_client_disconnect
2794 static int echo_client_init(void)
2798 rc = lu_kmem_init(echo_caches);
2800 rc = class_register_type(&echo_client_obd_ops, NULL, true, NULL,
2801 LUSTRE_ECHO_CLIENT_NAME,
2804 lu_kmem_fini(echo_caches);
2809 static void echo_client_exit(void)
2811 class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
2812 lu_kmem_fini(echo_caches);
2815 static int __init obdecho_init(void)
2820 LCONSOLE_INFO("Echo OBD driver; http://www.lustre.org/\n");
2822 LASSERT(PAGE_CACHE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
2824 # ifdef HAVE_SERVER_SUPPORT
2825 rc = echo_persistent_pages_init();
2829 rc = class_register_type(&echo_obd_ops, NULL, true, NULL,
2830 LUSTRE_ECHO_NAME, NULL);
2835 rc = echo_client_init();
2837 # ifdef HAVE_SERVER_SUPPORT
2841 class_unregister_type(LUSTRE_ECHO_NAME);
2843 echo_persistent_pages_fini();
2849 static void /*__exit*/ obdecho_exit(void)
2853 # ifdef HAVE_SERVER_SUPPORT
2854 class_unregister_type(LUSTRE_ECHO_NAME);
2855 echo_persistent_pages_fini();
2859 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2860 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
2861 MODULE_LICENSE("GPL");
2863 cfs_module(obdecho, LUSTRE_VERSION_STRING, obdecho_init, obdecho_exit);
2865 /** @} echo_client */