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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 #define DEBUG_SUBSYSTEM S_ECHO
35 #include <linux/user_namespace.h>
36 #ifdef HAVE_UIDGID_HEADER
37 # include <linux/uidgid.h>
39 #include <libcfs/libcfs.h>
42 #include <obd_support.h>
43 #include <obd_class.h>
44 #include <lustre_debug.h>
45 #include <lprocfs_status.h>
46 #include <cl_object.h>
47 #include <lustre_fid.h>
48 #include <lustre_acl.h>
49 #include <lustre_ioctl.h>
50 #include <lustre_net.h>
51 #ifdef HAVE_SERVER_SUPPORT
52 # include <md_object.h>
54 #define ETI_NAME_LEN 20
56 #endif /* HAVE_SERVER_SUPPORT */
58 #include "echo_internal.h"
60 /** \defgroup echo_client Echo Client
65 struct cl_device ed_cl;
66 struct echo_client_obd *ed_ec;
68 struct cl_site ed_site_myself;
69 struct lu_site *ed_site;
70 struct lu_device *ed_next;
72 struct lu_client_seq *ed_cl_seq;
73 #ifdef HAVE_SERVER_SUPPORT
74 struct local_oid_storage *ed_los;
75 struct lu_fid ed_root_fid;
76 #endif /* HAVE_SERVER_SUPPORT */
80 struct cl_object eo_cl;
81 struct cl_object_header eo_hdr;
82 struct echo_device *eo_dev;
83 struct list_head eo_obj_chain;
84 struct lov_oinfo *eo_oinfo;
89 struct echo_object_conf {
90 struct cl_object_conf eoc_cl;
91 struct lov_oinfo **eoc_oinfo;
95 struct cl_page_slice ep_cl;
100 struct cl_lock_slice el_cl;
101 struct list_head el_chain;
102 struct echo_object *el_object;
104 atomic_t el_refcount;
107 #ifdef HAVE_SERVER_SUPPORT
108 static const char echo_md_root_dir_name[] = "ROOT_ECHO";
111 * In order to use the values of members in struct mdd_device,
112 * we define an alias structure here.
114 struct echo_md_device {
115 struct md_device emd_md_dev;
116 struct obd_export *emd_child_exp;
117 struct dt_device *emd_child;
118 struct dt_device *emd_bottom;
119 struct lu_fid emd_root_fid;
120 struct lu_fid emd_local_root_fid;
122 #endif /* HAVE_SERVER_SUPPORT */
124 static int echo_client_setup(const struct lu_env *env,
125 struct obd_device *obddev,
126 struct lustre_cfg *lcfg);
127 static int echo_client_cleanup(struct obd_device *obddev);
130 /** \defgroup echo_helpers Helper functions
133 static inline struct echo_device *cl2echo_dev(const struct cl_device *dev)
135 return container_of0(dev, struct echo_device, ed_cl);
138 static inline struct cl_device *echo_dev2cl(struct echo_device *d)
143 static inline struct echo_device *obd2echo_dev(const struct obd_device *obd)
145 return cl2echo_dev(lu2cl_dev(obd->obd_lu_dev));
148 static inline struct cl_object *echo_obj2cl(struct echo_object *eco)
153 static inline struct echo_object *cl2echo_obj(const struct cl_object *o)
155 return container_of(o, struct echo_object, eo_cl);
158 static inline struct echo_page *cl2echo_page(const struct cl_page_slice *s)
160 return container_of(s, struct echo_page, ep_cl);
163 static inline struct echo_lock *cl2echo_lock(const struct cl_lock_slice *s)
165 return container_of(s, struct echo_lock, el_cl);
168 static inline struct cl_lock *echo_lock2cl(const struct echo_lock *ecl)
170 return ecl->el_cl.cls_lock;
173 static struct lu_context_key echo_thread_key;
174 static inline struct echo_thread_info *echo_env_info(const struct lu_env *env)
176 struct echo_thread_info *info;
177 info = lu_context_key_get(&env->le_ctx, &echo_thread_key);
178 LASSERT(info != NULL);
183 struct echo_object_conf *cl2echo_conf(const struct cl_object_conf *c)
185 return container_of(c, struct echo_object_conf, eoc_cl);
188 #ifdef HAVE_SERVER_SUPPORT
189 static inline struct echo_md_device *lu2emd_dev(struct lu_device *d)
191 return container_of0(d, struct echo_md_device, emd_md_dev.md_lu_dev);
194 static inline struct lu_device *emd2lu_dev(struct echo_md_device *d)
196 return &d->emd_md_dev.md_lu_dev;
199 static inline struct seq_server_site *echo_md_seq_site(struct echo_md_device *d)
201 return emd2lu_dev(d)->ld_site->ld_seq_site;
204 static inline struct obd_device *emd2obd_dev(struct echo_md_device *d)
206 return d->emd_md_dev.md_lu_dev.ld_obd;
208 #endif /* HAVE_SERVER_SUPPORT */
210 /** @} echo_helpers */
212 static int cl_echo_object_put(struct echo_object *eco);
213 static int cl_echo_object_brw(struct echo_object *eco, int rw, u64 offset,
214 struct page **pages, int npages, int async);
216 struct echo_thread_info {
217 struct echo_object_conf eti_conf;
218 struct lustre_md eti_md;
220 struct cl_2queue eti_queue;
222 struct cl_lock eti_lock;
223 struct lu_fid eti_fid;
224 struct lu_fid eti_fid2;
225 #ifdef HAVE_SERVER_SUPPORT
226 struct md_op_spec eti_spec;
227 struct lov_mds_md_v3 eti_lmm;
228 struct lov_user_md_v3 eti_lum;
229 struct md_attr eti_ma;
230 struct lu_name eti_lname;
231 /* per-thread values, can be re-used */
232 void *eti_big_lmm; /* may be vmalloc'd */
234 char eti_name[ETI_NAME_LEN];
235 struct lu_buf eti_buf;
236 char eti_xattr_buf[LUSTRE_POSIX_ACL_MAX_SIZE];
240 /* No session used right now */
241 struct echo_session_info {
245 static struct kmem_cache *echo_lock_kmem;
246 static struct kmem_cache *echo_object_kmem;
247 static struct kmem_cache *echo_thread_kmem;
248 static struct kmem_cache *echo_session_kmem;
249 /* static struct kmem_cache *echo_req_kmem; */
251 static struct lu_kmem_descr echo_caches[] = {
253 .ckd_cache = &echo_lock_kmem,
254 .ckd_name = "echo_lock_kmem",
255 .ckd_size = sizeof (struct echo_lock)
258 .ckd_cache = &echo_object_kmem,
259 .ckd_name = "echo_object_kmem",
260 .ckd_size = sizeof (struct echo_object)
263 .ckd_cache = &echo_thread_kmem,
264 .ckd_name = "echo_thread_kmem",
265 .ckd_size = sizeof (struct echo_thread_info)
268 .ckd_cache = &echo_session_kmem,
269 .ckd_name = "echo_session_kmem",
270 .ckd_size = sizeof (struct echo_session_info)
277 /** \defgroup echo_page Page operations
279 * Echo page operations.
283 static int echo_page_own(const struct lu_env *env,
284 const struct cl_page_slice *slice,
285 struct cl_io *io, int nonblock)
287 struct echo_page *ep = cl2echo_page(slice);
290 mutex_lock(&ep->ep_lock);
291 else if (!mutex_trylock(&ep->ep_lock))
296 static void echo_page_disown(const struct lu_env *env,
297 const struct cl_page_slice *slice,
300 struct echo_page *ep = cl2echo_page(slice);
302 LASSERT(mutex_is_locked(&ep->ep_lock));
303 mutex_unlock(&ep->ep_lock);
306 static void echo_page_discard(const struct lu_env *env,
307 const struct cl_page_slice *slice,
308 struct cl_io *unused)
310 cl_page_delete(env, slice->cpl_page);
313 static int echo_page_is_vmlocked(const struct lu_env *env,
314 const struct cl_page_slice *slice)
316 if (mutex_is_locked(&cl2echo_page(slice)->ep_lock))
321 static void echo_page_completion(const struct lu_env *env,
322 const struct cl_page_slice *slice,
325 LASSERT(slice->cpl_page->cp_sync_io != NULL);
328 static void echo_page_fini(const struct lu_env *env,
329 struct cl_page_slice *slice)
331 struct echo_object *eco = cl2echo_obj(slice->cpl_obj);
334 atomic_dec(&eco->eo_npages);
335 put_page(slice->cpl_page->cp_vmpage);
339 static int echo_page_prep(const struct lu_env *env,
340 const struct cl_page_slice *slice,
341 struct cl_io *unused)
346 static int echo_page_print(const struct lu_env *env,
347 const struct cl_page_slice *slice,
348 void *cookie, lu_printer_t printer)
350 struct echo_page *ep = cl2echo_page(slice);
352 (*printer)(env, cookie, LUSTRE_ECHO_CLIENT_NAME"-page@%p %d vm@%p\n",
353 ep, mutex_is_locked(&ep->ep_lock),
354 slice->cpl_page->cp_vmpage);
358 static const struct cl_page_operations echo_page_ops = {
359 .cpo_own = echo_page_own,
360 .cpo_disown = echo_page_disown,
361 .cpo_discard = echo_page_discard,
362 .cpo_fini = echo_page_fini,
363 .cpo_print = echo_page_print,
364 .cpo_is_vmlocked = echo_page_is_vmlocked,
367 .cpo_prep = echo_page_prep,
368 .cpo_completion = echo_page_completion,
371 .cpo_prep = echo_page_prep,
372 .cpo_completion = echo_page_completion,
378 /** \defgroup echo_lock Locking
380 * echo lock operations
384 static void echo_lock_fini(const struct lu_env *env,
385 struct cl_lock_slice *slice)
387 struct echo_lock *ecl = cl2echo_lock(slice);
389 LASSERT(list_empty(&ecl->el_chain));
390 OBD_SLAB_FREE_PTR(ecl, echo_lock_kmem);
393 static struct cl_lock_operations echo_lock_ops = {
394 .clo_fini = echo_lock_fini,
399 /** \defgroup echo_cl_ops cl_object operations
401 * operations for cl_object
405 static int echo_page_init(const struct lu_env *env, struct cl_object *obj,
406 struct cl_page *page, pgoff_t index)
408 struct echo_page *ep = cl_object_page_slice(obj, page);
409 struct echo_object *eco = cl2echo_obj(obj);
412 get_page(page->cp_vmpage);
413 mutex_init(&ep->ep_lock);
414 cl_page_slice_add(page, &ep->ep_cl, obj, index, &echo_page_ops);
415 atomic_inc(&eco->eo_npages);
419 static int echo_io_init(const struct lu_env *env, struct cl_object *obj,
425 static int echo_lock_init(const struct lu_env *env,
426 struct cl_object *obj, struct cl_lock *lock,
427 const struct cl_io *unused)
429 struct echo_lock *el;
432 OBD_SLAB_ALLOC_PTR_GFP(el, echo_lock_kmem, GFP_NOFS);
434 cl_lock_slice_add(lock, &el->el_cl, obj, &echo_lock_ops);
435 el->el_object = cl2echo_obj(obj);
436 INIT_LIST_HEAD(&el->el_chain);
437 atomic_set(&el->el_refcount, 0);
439 RETURN(el == NULL ? -ENOMEM : 0);
442 static int echo_conf_set(const struct lu_env *env, struct cl_object *obj,
443 const struct cl_object_conf *conf)
448 static const struct cl_object_operations echo_cl_obj_ops = {
449 .coo_page_init = echo_page_init,
450 .coo_lock_init = echo_lock_init,
451 .coo_io_init = echo_io_init,
452 .coo_conf_set = echo_conf_set
454 /** @} echo_cl_ops */
456 /** \defgroup echo_lu_ops lu_object operations
458 * operations for echo lu object.
462 static int echo_object_init(const struct lu_env *env, struct lu_object *obj,
463 const struct lu_object_conf *conf)
465 struct echo_device *ed = cl2echo_dev(lu2cl_dev(obj->lo_dev));
466 struct echo_client_obd *ec = ed->ed_ec;
467 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
471 struct lu_object *below;
472 struct lu_device *under;
475 below = under->ld_ops->ldo_object_alloc(env, obj->lo_header,
479 lu_object_add(obj, below);
482 if (!ed->ed_next_ismd) {
483 const struct cl_object_conf *cconf = lu2cl_conf(conf);
484 struct echo_object_conf *econf = cl2echo_conf(cconf);
486 LASSERT(econf->eoc_oinfo != NULL);
488 /* Transfer the oinfo pointer to eco that it won't be
490 eco->eo_oinfo = *econf->eoc_oinfo;
491 *econf->eoc_oinfo = NULL;
493 eco->eo_oinfo = NULL;
497 atomic_set(&eco->eo_npages, 0);
498 cl_object_page_init(lu2cl(obj), sizeof(struct echo_page));
500 spin_lock(&ec->ec_lock);
501 list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
502 spin_unlock(&ec->ec_lock);
507 static void echo_object_free(const struct lu_env *env, struct lu_object *obj)
509 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
510 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
513 LASSERT(atomic_read(&eco->eo_npages) == 0);
515 spin_lock(&ec->ec_lock);
516 list_del_init(&eco->eo_obj_chain);
517 spin_unlock(&ec->ec_lock);
520 lu_object_header_fini(obj->lo_header);
522 if (eco->eo_oinfo != NULL)
523 OBD_FREE_PTR(eco->eo_oinfo);
525 OBD_SLAB_FREE_PTR(eco, echo_object_kmem);
529 static int echo_object_print(const struct lu_env *env, void *cookie,
530 lu_printer_t p, const struct lu_object *o)
532 struct echo_object *obj = cl2echo_obj(lu2cl(o));
534 return (*p)(env, cookie, "echoclient-object@%p", obj);
537 static const struct lu_object_operations echo_lu_obj_ops = {
538 .loo_object_init = echo_object_init,
539 .loo_object_delete = NULL,
540 .loo_object_release = NULL,
541 .loo_object_free = echo_object_free,
542 .loo_object_print = echo_object_print,
543 .loo_object_invariant = NULL
545 /** @} echo_lu_ops */
547 /** \defgroup echo_lu_dev_ops lu_device operations
549 * Operations for echo lu device.
553 static struct lu_object *echo_object_alloc(const struct lu_env *env,
554 const struct lu_object_header *hdr,
555 struct lu_device *dev)
557 struct echo_object *eco;
558 struct lu_object *obj = NULL;
561 /* we're the top dev. */
562 LASSERT(hdr == NULL);
563 OBD_SLAB_ALLOC_PTR_GFP(eco, echo_object_kmem, GFP_NOFS);
565 struct cl_object_header *hdr = &eco->eo_hdr;
567 obj = &echo_obj2cl(eco)->co_lu;
568 cl_object_header_init(hdr);
569 hdr->coh_page_bufsize = cfs_size_round(sizeof(struct cl_page));
571 lu_object_init(obj, &hdr->coh_lu, dev);
572 lu_object_add_top(&hdr->coh_lu, obj);
574 eco->eo_cl.co_ops = &echo_cl_obj_ops;
575 obj->lo_ops = &echo_lu_obj_ops;
580 static struct lu_device_operations echo_device_lu_ops = {
581 .ldo_object_alloc = echo_object_alloc,
584 /** @} echo_lu_dev_ops */
586 /** \defgroup echo_init Setup and teardown
588 * Init and fini functions for echo client.
592 static int echo_site_init(const struct lu_env *env, struct echo_device *ed)
594 struct cl_site *site = &ed->ed_site_myself;
597 /* initialize site */
598 rc = cl_site_init(site, &ed->ed_cl);
600 CERROR("Cannot initialize site for echo client(%d)\n", rc);
604 rc = lu_site_init_finish(&site->cs_lu);
610 ed->ed_site = &site->cs_lu;
614 static void echo_site_fini(const struct lu_env *env, struct echo_device *ed)
617 if (!ed->ed_next_ismd)
618 lu_site_fini(ed->ed_site);
623 static void *echo_thread_key_init(const struct lu_context *ctx,
624 struct lu_context_key *key)
626 struct echo_thread_info *info;
628 OBD_SLAB_ALLOC_PTR_GFP(info, echo_thread_kmem, GFP_NOFS);
630 info = ERR_PTR(-ENOMEM);
634 static void echo_thread_key_fini(const struct lu_context *ctx,
635 struct lu_context_key *key, void *data)
637 struct echo_thread_info *info = data;
638 OBD_SLAB_FREE_PTR(info, echo_thread_kmem);
641 static struct lu_context_key echo_thread_key = {
642 .lct_tags = LCT_CL_THREAD,
643 .lct_init = echo_thread_key_init,
644 .lct_fini = echo_thread_key_fini,
647 static void *echo_session_key_init(const struct lu_context *ctx,
648 struct lu_context_key *key)
650 struct echo_session_info *session;
652 OBD_SLAB_ALLOC_PTR_GFP(session, echo_session_kmem, GFP_NOFS);
654 session = ERR_PTR(-ENOMEM);
658 static void echo_session_key_fini(const struct lu_context *ctx,
659 struct lu_context_key *key, void *data)
661 struct echo_session_info *session = data;
662 OBD_SLAB_FREE_PTR(session, echo_session_kmem);
665 static struct lu_context_key echo_session_key = {
666 .lct_tags = LCT_SESSION,
667 .lct_init = echo_session_key_init,
668 .lct_fini = echo_session_key_fini,
671 LU_TYPE_INIT_FINI(echo, &echo_thread_key, &echo_session_key);
673 #ifdef HAVE_SERVER_SUPPORT
674 # define ECHO_SEQ_WIDTH 0xffffffff
675 static int echo_fid_init(struct echo_device *ed, char *obd_name,
676 struct seq_server_site *ss)
682 OBD_ALLOC_PTR(ed->ed_cl_seq);
683 if (ed->ed_cl_seq == NULL)
686 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
688 GOTO(out_free_seq, rc = -ENOMEM);
690 snprintf(prefix, MAX_OBD_NAME + 5, "srv-%s", obd_name);
692 /* Init client side sequence-manager */
693 rc = seq_client_init(ed->ed_cl_seq, NULL,
695 prefix, ss->ss_server_seq);
696 ed->ed_cl_seq->lcs_width = ECHO_SEQ_WIDTH;
697 OBD_FREE(prefix, MAX_OBD_NAME + 5);
699 GOTO(out_free_seq, rc);
704 OBD_FREE_PTR(ed->ed_cl_seq);
705 ed->ed_cl_seq = NULL;
709 static int echo_fid_fini(struct obd_device *obddev)
711 struct echo_device *ed = obd2echo_dev(obddev);
714 if (ed->ed_cl_seq != NULL) {
715 seq_client_fini(ed->ed_cl_seq);
716 OBD_FREE_PTR(ed->ed_cl_seq);
717 ed->ed_cl_seq = NULL;
723 static void echo_ed_los_fini(const struct lu_env *env, struct echo_device *ed)
727 if (ed != NULL && ed->ed_next_ismd && ed->ed_los != NULL) {
728 local_oid_storage_fini(env, ed->ed_los);
734 echo_md_local_file_create(const struct lu_env *env, struct echo_md_device *emd,
735 struct local_oid_storage *los,
736 const struct lu_fid *pfid, const char *name,
737 __u32 mode, struct lu_fid *fid)
739 struct dt_object *parent = NULL;
740 struct dt_object *dto = NULL;
744 LASSERT(!fid_is_zero(pfid));
745 parent = dt_locate(env, emd->emd_bottom, pfid);
746 if (unlikely(IS_ERR(parent)))
747 RETURN(PTR_ERR(parent));
749 /* create local file with @fid */
750 dto = local_file_find_or_create_with_fid(env, emd->emd_bottom, fid,
753 GOTO(out_put, rc = PTR_ERR(dto));
755 *fid = *lu_object_fid(&dto->do_lu);
756 /* since stack is not fully set up the local_storage uses own stack
757 * and we should drop its object from cache */
758 dt_object_put_nocache(env, dto);
762 dt_object_put(env, parent);
767 echo_md_root_get(const struct lu_env *env, struct echo_md_device *emd,
768 struct echo_device *ed)
774 /* Setup local dirs */
775 fid.f_seq = FID_SEQ_LOCAL_NAME;
778 rc = local_oid_storage_init(env, emd->emd_bottom, &fid, &ed->ed_los);
782 lu_echo_root_fid(&fid);
783 if (echo_md_seq_site(emd)->ss_node_id == 0) {
784 rc = echo_md_local_file_create(env, emd, ed->ed_los,
785 &emd->emd_local_root_fid,
786 echo_md_root_dir_name, S_IFDIR |
787 S_IRUGO | S_IWUSR | S_IXUGO,
790 CERROR("%s: create md echo root fid failed: rc = %d\n",
791 emd2obd_dev(emd)->obd_name, rc);
795 ed->ed_root_fid = fid;
799 echo_ed_los_fini(env, ed);
803 #endif /* HAVE_SERVER_SUPPORT */
805 static struct lu_device *echo_device_alloc(const struct lu_env *env,
806 struct lu_device_type *t,
807 struct lustre_cfg *cfg)
809 struct lu_device *next;
810 struct echo_device *ed;
811 struct cl_device *cd;
812 struct obd_device *obd = NULL; /* to keep compiler happy */
813 struct obd_device *tgt;
814 const char *tgt_type_name;
821 GOTO(out, rc = -ENOMEM);
825 rc = cl_device_init(cd, t);
829 cd->cd_lu_dev.ld_ops = &echo_device_lu_ops;
832 obd = class_name2obd(lustre_cfg_string(cfg, 0));
833 LASSERT(obd != NULL);
834 LASSERT(env != NULL);
836 tgt = class_name2obd(lustre_cfg_string(cfg, 1));
838 CERROR("Can not find tgt device %s\n",
839 lustre_cfg_string(cfg, 1));
840 GOTO(out, rc = -ENODEV);
843 next = tgt->obd_lu_dev;
845 if (strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME) == 0) {
846 ed->ed_next_ismd = 1;
847 } else if (strcmp(tgt->obd_type->typ_name, LUSTRE_OST_NAME) == 0 ||
848 strcmp(tgt->obd_type->typ_name, LUSTRE_OSC_NAME) == 0) {
849 ed->ed_next_ismd = 0;
850 rc = echo_site_init(env, ed);
854 GOTO(out, rc = -EINVAL);
859 rc = echo_client_setup(env, obd, cfg);
863 ed->ed_ec = &obd->u.echo_client;
866 if (ed->ed_next_ismd) {
867 #ifdef HAVE_SERVER_SUPPORT
868 /* Suppose to connect to some Metadata layer */
869 struct lu_site *ls = NULL;
870 struct lu_device *ld = NULL;
871 struct md_device *md = NULL;
872 struct echo_md_device *emd = NULL;
876 CERROR("%s is not lu device type!\n",
877 lustre_cfg_string(cfg, 1));
878 GOTO(out, rc = -EINVAL);
881 tgt_type_name = lustre_cfg_string(cfg, 2);
882 if (!tgt_type_name) {
883 CERROR("%s no type name for echo %s setup\n",
884 lustre_cfg_string(cfg, 1),
885 tgt->obd_type->typ_name);
886 GOTO(out, rc = -EINVAL);
891 spin_lock(&ls->ls_ld_lock);
892 list_for_each_entry(ld, &ls->ls_ld_linkage, ld_linkage) {
893 if (strcmp(ld->ld_type->ldt_name, tgt_type_name) == 0) {
898 spin_unlock(&ls->ls_ld_lock);
901 CERROR("%s is not lu device type!\n",
902 lustre_cfg_string(cfg, 1));
903 GOTO(out, rc = -EINVAL);
907 /* For MD echo client, it will use the site in MDS stack */
909 ed->ed_cl.cd_lu_dev.ld_site = ls;
910 rc = echo_fid_init(ed, obd->obd_name, lu_site2seq(ls));
912 CERROR("echo fid init error %d\n", rc);
916 md = lu2md_dev(next);
917 emd = lu2emd_dev(&md->md_lu_dev);
918 rc = echo_md_root_get(env, emd, ed);
920 CERROR("%s: get root error: rc = %d\n",
921 emd2obd_dev(emd)->obd_name, rc);
924 #else /* !HAVE_SERVER_SUPPORT */
925 CERROR("Local operations are NOT supported on client side. "
926 "Only remote operations are supported. Metadata client "
927 "must be run on server side.\n");
928 GOTO(out, rc = -EOPNOTSUPP);
929 #endif /* HAVE_SERVER_SUPPORT */
931 /* if echo client is to be stacked upon ost device, the next is
932 * NULL since ost is not a clio device so far */
933 if (next != NULL && !lu_device_is_cl(next))
936 tgt_type_name = tgt->obd_type->typ_name;
938 LASSERT(next != NULL);
939 if (next->ld_site != NULL)
940 GOTO(out, rc = -EBUSY);
942 next->ld_site = ed->ed_site;
943 rc = next->ld_type->ldt_ops->ldto_device_init(env, next,
944 next->ld_type->ldt_name,
949 LASSERT(strcmp(tgt_type_name, LUSTRE_OST_NAME) == 0);
953 RETURN(&cd->cd_lu_dev);
958 rc2 = echo_client_cleanup(obd);
960 CERROR("Cleanup obd device %s error(%d)\n",
965 echo_site_fini(env, ed);
967 cl_device_fini(&ed->ed_cl);
977 static int echo_device_init(const struct lu_env *env, struct lu_device *d,
978 const char *name, struct lu_device *next)
984 static struct lu_device *echo_device_fini(const struct lu_env *env,
987 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
988 struct lu_device *next = ed->ed_next;
990 while (next && !ed->ed_next_ismd)
991 next = next->ld_type->ldt_ops->ldto_device_fini(env, next);
995 static void echo_lock_release(const struct lu_env *env,
996 struct echo_lock *ecl,
999 struct cl_lock *clk = echo_lock2cl(ecl);
1001 cl_lock_release(env, clk);
1004 static struct lu_device *echo_device_free(const struct lu_env *env,
1005 struct lu_device *d)
1007 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
1008 struct echo_client_obd *ec = ed->ed_ec;
1009 struct echo_object *eco;
1010 struct lu_device *next = ed->ed_next;
1012 CDEBUG(D_INFO, "echo device:%p is going to be freed, next = %p\n",
1015 lu_site_purge(env, ed->ed_site, -1);
1017 /* check if there are objects still alive.
1018 * It shouldn't have any object because lu_site_purge would cleanup
1019 * all of cached objects. Anyway, probably the echo device is being
1020 * parallelly accessed.
1022 spin_lock(&ec->ec_lock);
1023 list_for_each_entry(eco, &ec->ec_objects, eo_obj_chain)
1024 eco->eo_deleted = 1;
1025 spin_unlock(&ec->ec_lock);
1028 lu_site_purge(env, ed->ed_site, -1);
1031 "Waiting for the reference of echo object to be dropped\n");
1033 /* Wait for the last reference to be dropped. */
1034 spin_lock(&ec->ec_lock);
1035 while (!list_empty(&ec->ec_objects)) {
1036 spin_unlock(&ec->ec_lock);
1037 CERROR("echo_client still has objects at cleanup time, "
1038 "wait for 1 second\n");
1039 set_current_state(TASK_UNINTERRUPTIBLE);
1040 schedule_timeout(cfs_time_seconds(1));
1041 lu_site_purge(env, ed->ed_site, -1);
1042 spin_lock(&ec->ec_lock);
1044 spin_unlock(&ec->ec_lock);
1046 LASSERT(list_empty(&ec->ec_locks));
1048 CDEBUG(D_INFO, "No object exists, exiting...\n");
1050 echo_client_cleanup(d->ld_obd);
1051 #ifdef HAVE_SERVER_SUPPORT
1052 echo_fid_fini(d->ld_obd);
1053 echo_ed_los_fini(env, ed);
1055 while (next && !ed->ed_next_ismd)
1056 next = next->ld_type->ldt_ops->ldto_device_free(env, next);
1058 LASSERT(ed->ed_site == d->ld_site);
1059 echo_site_fini(env, ed);
1060 cl_device_fini(&ed->ed_cl);
1063 cl_env_cache_purge(~0);
1068 static const struct lu_device_type_operations echo_device_type_ops = {
1069 .ldto_init = echo_type_init,
1070 .ldto_fini = echo_type_fini,
1072 .ldto_start = echo_type_start,
1073 .ldto_stop = echo_type_stop,
1075 .ldto_device_alloc = echo_device_alloc,
1076 .ldto_device_free = echo_device_free,
1077 .ldto_device_init = echo_device_init,
1078 .ldto_device_fini = echo_device_fini
1081 static struct lu_device_type echo_device_type = {
1082 .ldt_tags = LU_DEVICE_CL,
1083 .ldt_name = LUSTRE_ECHO_CLIENT_NAME,
1084 .ldt_ops = &echo_device_type_ops,
1085 .ldt_ctx_tags = LCT_CL_THREAD | LCT_MD_THREAD | LCT_DT_THREAD,
1089 /** \defgroup echo_exports Exported operations
1091 * exporting functions to echo client
1096 /* Interfaces to echo client obd device */
1097 static struct echo_object *
1098 cl_echo_object_find(struct echo_device *d, const struct ost_id *oi)
1101 struct echo_thread_info *info;
1102 struct echo_object_conf *conf;
1103 struct echo_object *eco;
1104 struct cl_object *obj;
1105 struct lov_oinfo *oinfo = NULL;
1111 LASSERTF(ostid_id(oi) != 0, DOSTID"\n", POSTID(oi));
1112 LASSERTF(ostid_seq(oi) == FID_SEQ_ECHO, DOSTID"\n", POSTID(oi));
1114 /* Never return an object if the obd is to be freed. */
1115 if (echo_dev2cl(d)->cd_lu_dev.ld_obd->obd_stopping)
1116 RETURN(ERR_PTR(-ENODEV));
1118 env = cl_env_get(&refcheck);
1120 RETURN((void *)env);
1122 info = echo_env_info(env);
1123 conf = &info->eti_conf;
1125 OBD_ALLOC_PTR(oinfo);
1127 GOTO(out, eco = ERR_PTR(-ENOMEM));
1129 oinfo->loi_oi = *oi;
1130 conf->eoc_cl.u.coc_oinfo = oinfo;
1133 /* If echo_object_init() is successful then ownership of oinfo
1134 * is transferred to the object. */
1135 conf->eoc_oinfo = &oinfo;
1137 fid = &info->eti_fid;
1138 rc = ostid_to_fid(fid, oi, 0);
1140 GOTO(out, eco = ERR_PTR(rc));
1142 /* In the function below, .hs_keycmp resolves to
1143 * lu_obj_hop_keycmp() */
1144 /* coverity[overrun-buffer-val] */
1145 obj = cl_object_find(env, echo_dev2cl(d), fid, &conf->eoc_cl);
1147 GOTO(out, eco = (void*)obj);
1149 eco = cl2echo_obj(obj);
1150 if (eco->eo_deleted) {
1151 cl_object_put(env, obj);
1152 eco = ERR_PTR(-EAGAIN);
1157 OBD_FREE_PTR(oinfo);
1159 cl_env_put(env, &refcheck);
1163 static int cl_echo_object_put(struct echo_object *eco)
1166 struct cl_object *obj = echo_obj2cl(eco);
1170 env = cl_env_get(&refcheck);
1172 RETURN(PTR_ERR(env));
1174 /* an external function to kill an object? */
1175 if (eco->eo_deleted) {
1176 struct lu_object_header *loh = obj->co_lu.lo_header;
1177 LASSERT(&eco->eo_hdr == luh2coh(loh));
1178 set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
1181 cl_object_put(env, obj);
1182 cl_env_put(env, &refcheck);
1186 static int cl_echo_enqueue0(struct lu_env *env, struct echo_object *eco,
1187 u64 start, u64 end, int mode,
1188 __u64 *cookie , __u32 enqflags)
1191 struct cl_lock *lck;
1192 struct cl_object *obj;
1193 struct cl_lock_descr *descr;
1194 struct echo_thread_info *info;
1198 info = echo_env_info(env);
1200 lck = &info->eti_lock;
1201 obj = echo_obj2cl(eco);
1203 memset(lck, 0, sizeof(*lck));
1204 descr = &lck->cll_descr;
1205 descr->cld_obj = obj;
1206 descr->cld_start = cl_index(obj, start);
1207 descr->cld_end = cl_index(obj, end);
1208 descr->cld_mode = mode == LCK_PW ? CLM_WRITE : CLM_READ;
1209 descr->cld_enq_flags = enqflags;
1212 rc = cl_lock_request(env, io, lck);
1214 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
1215 struct echo_lock *el;
1217 el = cl2echo_lock(cl_lock_at(lck, &echo_device_type));
1218 spin_lock(&ec->ec_lock);
1219 if (list_empty(&el->el_chain)) {
1220 list_add(&el->el_chain, &ec->ec_locks);
1221 el->el_cookie = ++ec->ec_unique;
1223 atomic_inc(&el->el_refcount);
1224 *cookie = el->el_cookie;
1225 spin_unlock(&ec->ec_lock);
1230 static int cl_echo_cancel0(struct lu_env *env, struct echo_device *ed,
1233 struct echo_client_obd *ec = ed->ed_ec;
1234 struct echo_lock *ecl = NULL;
1235 struct list_head *el;
1236 int found = 0, still_used = 0;
1239 LASSERT(ec != NULL);
1240 spin_lock(&ec->ec_lock);
1241 list_for_each(el, &ec->ec_locks) {
1242 ecl = list_entry(el, struct echo_lock, el_chain);
1243 CDEBUG(D_INFO, "ecl: %p, cookie: %#llx\n", ecl, ecl->el_cookie);
1244 found = (ecl->el_cookie == cookie);
1246 if (atomic_dec_and_test(&ecl->el_refcount))
1247 list_del_init(&ecl->el_chain);
1253 spin_unlock(&ec->ec_lock);
1258 echo_lock_release(env, ecl, still_used);
1262 static void echo_commit_callback(const struct lu_env *env, struct cl_io *io,
1263 struct cl_page *page)
1265 struct echo_thread_info *info;
1266 struct cl_2queue *queue;
1268 info = echo_env_info(env);
1269 LASSERT(io == &info->eti_io);
1271 queue = &info->eti_queue;
1272 cl_page_list_add(&queue->c2_qout, page);
1275 static int cl_echo_object_brw(struct echo_object *eco, int rw, u64 offset,
1276 struct page **pages, int npages, int async)
1279 struct echo_thread_info *info;
1280 struct cl_object *obj = echo_obj2cl(eco);
1281 struct echo_device *ed = eco->eo_dev;
1282 struct cl_2queue *queue;
1284 struct cl_page *clp;
1285 struct lustre_handle lh = { 0 };
1286 int page_size = cl_page_size(obj);
1292 LASSERT((offset & ~PAGE_MASK) == 0);
1293 LASSERT(ed->ed_next != NULL);
1294 env = cl_env_get(&refcheck);
1296 RETURN(PTR_ERR(env));
1298 info = echo_env_info(env);
1300 queue = &info->eti_queue;
1302 cl_2queue_init(queue);
1304 io->ci_ignore_layout = 1;
1305 rc = cl_io_init(env, io, CIT_MISC, obj);
1311 rc = cl_echo_enqueue0(env, eco, offset,
1312 offset + npages * PAGE_SIZE - 1,
1313 rw == READ ? LCK_PR : LCK_PW, &lh.cookie,
1316 GOTO(error_lock, rc);
1318 for (i = 0; i < npages; i++) {
1320 clp = cl_page_find(env, obj, cl_index(obj, offset),
1321 pages[i], CPT_TRANSIENT);
1326 LASSERT(clp->cp_type == CPT_TRANSIENT);
1328 rc = cl_page_own(env, io, clp);
1330 LASSERT(clp->cp_state == CPS_FREEING);
1331 cl_page_put(env, clp);
1335 cl_2queue_add(queue, clp);
1337 /* drop the reference count for cl_page_find, so that the page
1338 * will be freed in cl_2queue_fini. */
1339 cl_page_put(env, clp);
1340 cl_page_clip(env, clp, 0, page_size);
1342 offset += page_size;
1346 enum cl_req_type typ = rw == READ ? CRT_READ : CRT_WRITE;
1348 async = async && (typ == CRT_WRITE);
1350 rc = cl_io_commit_async(env, io, &queue->c2_qin,
1352 echo_commit_callback);
1354 rc = cl_io_submit_sync(env, io, typ, queue, 0);
1355 CDEBUG(D_INFO, "echo_client %s write returns %d\n",
1356 async ? "async" : "sync", rc);
1359 cl_echo_cancel0(env, ed, lh.cookie);
1362 cl_2queue_discard(env, io, queue);
1363 cl_2queue_disown(env, io, queue);
1364 cl_2queue_fini(env, queue);
1365 cl_io_fini(env, io);
1367 cl_env_put(env, &refcheck);
1370 /** @} echo_exports */
1373 static u64 last_object_id;
1375 #ifdef HAVE_SERVER_SUPPORT
1376 static inline void echo_md_build_name(struct lu_name *lname, char *name,
1379 snprintf(name, ETI_NAME_LEN, "%llu", id);
1380 lname->ln_name = name;
1381 lname->ln_namelen = strlen(name);
1384 /* similar to mdt_attr_get_complex */
1385 static int echo_big_lmm_get(const struct lu_env *env, struct md_object *o,
1388 struct echo_thread_info *info = echo_env_info(env);
1393 LASSERT(ma->ma_lmm_size > 0);
1395 rc = mo_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LOV);
1399 /* big_lmm may need to be grown */
1400 if (info->eti_big_lmmsize < rc) {
1401 int size = size_roundup_power2(rc);
1403 if (info->eti_big_lmmsize > 0) {
1404 /* free old buffer */
1405 LASSERT(info->eti_big_lmm);
1406 OBD_FREE_LARGE(info->eti_big_lmm,
1407 info->eti_big_lmmsize);
1408 info->eti_big_lmm = NULL;
1409 info->eti_big_lmmsize = 0;
1412 OBD_ALLOC_LARGE(info->eti_big_lmm, size);
1413 if (info->eti_big_lmm == NULL)
1415 info->eti_big_lmmsize = size;
1417 LASSERT(info->eti_big_lmmsize >= rc);
1419 info->eti_buf.lb_buf = info->eti_big_lmm;
1420 info->eti_buf.lb_len = info->eti_big_lmmsize;
1421 rc = mo_xattr_get(env, o, &info->eti_buf, XATTR_NAME_LOV);
1425 ma->ma_valid |= MA_LOV;
1426 ma->ma_lmm = info->eti_big_lmm;
1427 ma->ma_lmm_size = rc;
1432 static int echo_attr_get_complex(const struct lu_env *env,
1433 struct md_object *next,
1436 struct echo_thread_info *info = echo_env_info(env);
1437 struct lu_buf *buf = &info->eti_buf;
1438 umode_t mode = lu_object_attr(&next->mo_lu);
1439 int need = ma->ma_need;
1446 if (need & MA_INODE) {
1447 ma->ma_need = MA_INODE;
1448 rc = mo_attr_get(env, next, ma);
1451 ma->ma_valid |= MA_INODE;
1454 if (need & MA_LOV) {
1455 if (S_ISREG(mode) || S_ISDIR(mode)) {
1456 LASSERT(ma->ma_lmm_size > 0);
1457 buf->lb_buf = ma->ma_lmm;
1458 buf->lb_len = ma->ma_lmm_size;
1459 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LOV);
1461 ma->ma_lmm_size = rc2;
1462 ma->ma_valid |= MA_LOV;
1463 } else if (rc2 == -ENODATA) {
1465 ma->ma_lmm_size = 0;
1466 } else if (rc2 == -ERANGE) {
1467 rc2 = echo_big_lmm_get(env, next, ma);
1469 GOTO(out, rc = rc2);
1471 GOTO(out, rc = rc2);
1476 #ifdef CONFIG_FS_POSIX_ACL
1477 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
1478 buf->lb_buf = ma->ma_acl;
1479 buf->lb_len = ma->ma_acl_size;
1480 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1482 ma->ma_acl_size = rc2;
1483 ma->ma_valid |= MA_ACL_DEF;
1484 } else if (rc2 == -ENODATA) {
1486 ma->ma_acl_size = 0;
1488 GOTO(out, rc = rc2);
1494 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = %#llx ma_lmm=%p\n",
1495 rc, ma->ma_valid, ma->ma_lmm);
1500 echo_md_create_internal(const struct lu_env *env, struct echo_device *ed,
1501 struct md_object *parent, struct lu_fid *fid,
1502 struct lu_name *lname, struct md_op_spec *spec,
1505 struct lu_object *ec_child, *child;
1506 struct lu_device *ld = ed->ed_next;
1507 struct echo_thread_info *info = echo_env_info(env);
1508 struct lu_fid *fid2 = &info->eti_fid2;
1509 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
1514 rc = mdo_lookup(env, parent, lname, fid2, spec);
1517 else if (rc != -ENOENT)
1520 ec_child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev,
1522 if (IS_ERR(ec_child)) {
1523 CERROR("Can not find the child "DFID": rc = %ld\n", PFID(fid),
1525 RETURN(PTR_ERR(ec_child));
1528 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1529 if (child == NULL) {
1530 CERROR("Can not locate the child "DFID"\n", PFID(fid));
1531 GOTO(out_put, rc = -EINVAL);
1534 CDEBUG(D_RPCTRACE, "Start creating object "DFID" %s %p\n",
1535 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1538 * Do not perform lookup sanity check. We know that name does not exist.
1540 spec->sp_cr_lookup = 0;
1541 rc = mdo_create(env, parent, lname, lu2md(child), spec, ma);
1543 CERROR("Can not create child "DFID": rc = %d\n", PFID(fid), rc);
1546 CDEBUG(D_RPCTRACE, "End creating object "DFID" %s %p rc = %d\n",
1547 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent, rc);
1550 lu_object_put(env, ec_child);
1554 static int echo_set_lmm_size(const struct lu_env *env, struct lu_device *ld,
1557 struct echo_thread_info *info = echo_env_info(env);
1559 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
1560 ma->ma_lmm = (void *)&info->eti_lmm;
1561 ma->ma_lmm_size = sizeof(info->eti_lmm);
1563 LASSERT(info->eti_big_lmmsize);
1564 ma->ma_lmm = info->eti_big_lmm;
1565 ma->ma_lmm_size = info->eti_big_lmmsize;
1571 static int echo_create_md_object(const struct lu_env *env,
1572 struct echo_device *ed,
1573 struct lu_object *ec_parent,
1575 char *name, int namelen,
1576 __u64 id, __u32 mode, int count,
1577 int stripe_count, int stripe_offset)
1579 struct lu_object *parent;
1580 struct echo_thread_info *info = echo_env_info(env);
1581 struct lu_name *lname = &info->eti_lname;
1582 struct md_op_spec *spec = &info->eti_spec;
1583 struct md_attr *ma = &info->eti_ma;
1584 struct lu_device *ld = ed->ed_next;
1590 if (ec_parent == NULL)
1592 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1596 memset(ma, 0, sizeof(*ma));
1597 memset(spec, 0, sizeof(*spec));
1598 if (stripe_count != 0) {
1599 spec->sp_cr_flags |= FMODE_WRITE;
1600 echo_set_lmm_size(env, ld, ma);
1601 if (stripe_count != -1) {
1602 struct lov_user_md_v3 *lum = &info->eti_lum;
1604 lum->lmm_magic = LOV_USER_MAGIC_V3;
1605 lum->lmm_stripe_count = stripe_count;
1606 lum->lmm_stripe_offset = stripe_offset;
1607 lum->lmm_pattern = 0;
1608 spec->u.sp_ea.eadata = lum;
1609 spec->u.sp_ea.eadatalen = sizeof(*lum);
1610 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1614 ma->ma_attr.la_mode = mode;
1615 ma->ma_attr.la_valid = LA_CTIME | LA_MODE;
1616 ma->ma_attr.la_ctime = cfs_time_current_64();
1619 lname->ln_name = name;
1620 lname->ln_namelen = namelen;
1621 /* If name is specified, only create one object by name */
1622 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1627 /* Create multiple object sequenced by id */
1628 for (i = 0; i < count; i++) {
1629 char *tmp_name = info->eti_name;
1631 echo_md_build_name(lname, tmp_name, id);
1633 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1636 CERROR("Can not create child %s: rc = %d\n", tmp_name,
1647 static struct lu_object *echo_md_lookup(const struct lu_env *env,
1648 struct echo_device *ed,
1649 struct md_object *parent,
1650 struct lu_name *lname)
1652 struct echo_thread_info *info = echo_env_info(env);
1653 struct lu_fid *fid = &info->eti_fid;
1654 struct lu_object *child;
1658 CDEBUG(D_INFO, "lookup %s in parent "DFID" %p\n", lname->ln_name,
1660 rc = mdo_lookup(env, parent, lname, fid, NULL);
1662 CERROR("lookup %s: rc = %d\n", lname->ln_name, rc);
1663 RETURN(ERR_PTR(rc));
1666 /* In the function below, .hs_keycmp resolves to
1667 * lu_obj_hop_keycmp() */
1668 /* coverity[overrun-buffer-val] */
1669 child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1674 static int echo_setattr_object(const struct lu_env *env,
1675 struct echo_device *ed,
1676 struct lu_object *ec_parent,
1677 __u64 id, int count)
1679 struct lu_object *parent;
1680 struct echo_thread_info *info = echo_env_info(env);
1681 struct lu_name *lname = &info->eti_lname;
1682 char *name = info->eti_name;
1683 struct lu_device *ld = ed->ed_next;
1684 struct lu_buf *buf = &info->eti_buf;
1690 if (ec_parent == NULL)
1692 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1696 for (i = 0; i < count; i++) {
1697 struct lu_object *ec_child, *child;
1699 echo_md_build_name(lname, name, id);
1701 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1702 if (IS_ERR(ec_child)) {
1703 CERROR("Can't find child %s: rc = %ld\n",
1704 lname->ln_name, PTR_ERR(ec_child));
1705 RETURN(PTR_ERR(ec_child));
1708 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1709 if (child == NULL) {
1710 CERROR("Can not locate the child %s\n", lname->ln_name);
1711 lu_object_put(env, ec_child);
1716 CDEBUG(D_RPCTRACE, "Start setattr object "DFID"\n",
1717 PFID(lu_object_fid(child)));
1719 buf->lb_buf = info->eti_xattr_buf;
1720 buf->lb_len = sizeof(info->eti_xattr_buf);
1722 sprintf(name, "%s.test1", XATTR_USER_PREFIX);
1723 rc = mo_xattr_set(env, lu2md(child), buf, name,
1726 CERROR("Can not setattr child "DFID": rc = %d\n",
1727 PFID(lu_object_fid(child)), rc);
1728 lu_object_put(env, ec_child);
1731 CDEBUG(D_RPCTRACE, "End setattr object "DFID"\n",
1732 PFID(lu_object_fid(child)));
1734 lu_object_put(env, ec_child);
1739 static int echo_getattr_object(const struct lu_env *env,
1740 struct echo_device *ed,
1741 struct lu_object *ec_parent,
1742 __u64 id, int count)
1744 struct lu_object *parent;
1745 struct echo_thread_info *info = echo_env_info(env);
1746 struct lu_name *lname = &info->eti_lname;
1747 char *name = info->eti_name;
1748 struct md_attr *ma = &info->eti_ma;
1749 struct lu_device *ld = ed->ed_next;
1755 if (ec_parent == NULL)
1757 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1761 memset(ma, 0, sizeof(*ma));
1762 ma->ma_need |= MA_INODE | MA_LOV | MA_PFID | MA_HSM | MA_ACL_DEF;
1763 ma->ma_acl = info->eti_xattr_buf;
1764 ma->ma_acl_size = sizeof(info->eti_xattr_buf);
1766 for (i = 0; i < count; i++) {
1767 struct lu_object *ec_child, *child;
1770 echo_md_build_name(lname, name, id);
1771 echo_set_lmm_size(env, ld, ma);
1773 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1774 if (IS_ERR(ec_child)) {
1775 CERROR("Can't find child %s: rc = %ld\n",
1776 lname->ln_name, PTR_ERR(ec_child));
1777 RETURN(PTR_ERR(ec_child));
1780 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1781 if (child == NULL) {
1782 CERROR("Can not locate the child %s\n", lname->ln_name);
1783 lu_object_put(env, ec_child);
1787 CDEBUG(D_RPCTRACE, "Start getattr object "DFID"\n",
1788 PFID(lu_object_fid(child)));
1789 rc = echo_attr_get_complex(env, lu2md(child), ma);
1791 CERROR("Can not getattr child "DFID": rc = %d\n",
1792 PFID(lu_object_fid(child)), rc);
1793 lu_object_put(env, ec_child);
1796 CDEBUG(D_RPCTRACE, "End getattr object "DFID"\n",
1797 PFID(lu_object_fid(child)));
1799 lu_object_put(env, ec_child);
1805 static int echo_lookup_object(const struct lu_env *env,
1806 struct echo_device *ed,
1807 struct lu_object *ec_parent,
1808 __u64 id, int count)
1810 struct lu_object *parent;
1811 struct echo_thread_info *info = echo_env_info(env);
1812 struct lu_name *lname = &info->eti_lname;
1813 char *name = info->eti_name;
1814 struct lu_fid *fid = &info->eti_fid;
1815 struct lu_device *ld = ed->ed_next;
1819 if (ec_parent == NULL)
1821 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1825 /*prepare the requests*/
1826 for (i = 0; i < count; i++) {
1827 echo_md_build_name(lname, name, id);
1829 CDEBUG(D_RPCTRACE, "Start lookup object "DFID" %s %p\n",
1830 PFID(lu_object_fid(parent)), lname->ln_name, parent);
1832 rc = mdo_lookup(env, lu2md(parent), lname, fid, NULL);
1834 CERROR("Can not lookup child %s: rc = %d\n", name, rc);
1837 CDEBUG(D_RPCTRACE, "End lookup object "DFID" %s %p\n",
1838 PFID(lu_object_fid(parent)), lname->ln_name, parent);
1845 static int echo_md_destroy_internal(const struct lu_env *env,
1846 struct echo_device *ed,
1847 struct md_object *parent,
1848 struct lu_name *lname,
1851 struct lu_device *ld = ed->ed_next;
1852 struct lu_object *ec_child;
1853 struct lu_object *child;
1858 ec_child = echo_md_lookup(env, ed, parent, lname);
1859 if (IS_ERR(ec_child)) {
1860 CERROR("Can't find child %s: rc = %ld\n", lname->ln_name,
1862 RETURN(PTR_ERR(ec_child));
1865 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1866 if (child == NULL) {
1867 CERROR("Can not locate the child %s\n", lname->ln_name);
1868 GOTO(out_put, rc = -EINVAL);
1871 if (lu_object_remote(child)) {
1872 CERROR("Can not destroy remote object %s: rc = %d\n",
1873 lname->ln_name, -EPERM);
1874 GOTO(out_put, rc = -EPERM);
1876 CDEBUG(D_RPCTRACE, "Start destroy object "DFID" %s %p\n",
1877 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1879 rc = mdo_unlink(env, parent, lu2md(child), lname, ma, 0);
1881 CERROR("Can not unlink child %s: rc = %d\n",
1882 lname->ln_name, rc);
1885 CDEBUG(D_RPCTRACE, "End destroy object "DFID" %s %p\n",
1886 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1888 lu_object_put(env, ec_child);
1892 static int echo_destroy_object(const struct lu_env *env,
1893 struct echo_device *ed,
1894 struct lu_object *ec_parent,
1895 char *name, int namelen,
1896 __u64 id, __u32 mode,
1899 struct echo_thread_info *info = echo_env_info(env);
1900 struct lu_name *lname = &info->eti_lname;
1901 struct md_attr *ma = &info->eti_ma;
1902 struct lu_device *ld = ed->ed_next;
1903 struct lu_object *parent;
1908 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1912 memset(ma, 0, sizeof(*ma));
1913 ma->ma_attr.la_mode = mode;
1914 ma->ma_attr.la_valid = LA_CTIME;
1915 ma->ma_attr.la_ctime = cfs_time_current_64();
1916 ma->ma_need = MA_INODE;
1920 lname->ln_name = name;
1921 lname->ln_namelen = namelen;
1922 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
1927 /*prepare the requests*/
1928 for (i = 0; i < count; i++) {
1929 char *tmp_name = info->eti_name;
1932 echo_md_build_name(lname, tmp_name, id);
1934 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
1937 CERROR("Can not unlink child %s: rc = %d\n", name, rc);
1946 static struct lu_object *echo_resolve_path(const struct lu_env *env,
1947 struct echo_device *ed, char *path,
1950 struct lu_device *ld = ed->ed_next;
1951 struct echo_thread_info *info = echo_env_info(env);
1952 struct lu_fid *fid = &info->eti_fid;
1953 struct lu_name *lname = &info->eti_lname;
1954 struct lu_object *parent = NULL;
1955 struct lu_object *child = NULL;
1959 *fid = ed->ed_root_fid;
1961 /* In the function below, .hs_keycmp resolves to
1962 * lu_obj_hop_keycmp() */
1963 /* coverity[overrun-buffer-val] */
1964 parent = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1965 if (IS_ERR(parent)) {
1966 CERROR("Can not find the parent "DFID": rc = %ld\n",
1967 PFID(fid), PTR_ERR(parent));
1972 struct lu_object *ld_parent;
1975 e = strsep(&path, "/");
1980 if (!path || path[0] == '\0')
1986 lname->ln_namelen = strlen(e);
1988 ld_parent = lu_object_locate(parent->lo_header, ld->ld_type);
1989 if (ld_parent == NULL) {
1990 lu_object_put(env, parent);
1995 child = echo_md_lookup(env, ed, lu2md(ld_parent), lname);
1996 lu_object_put(env, parent);
1997 if (IS_ERR(child)) {
1998 rc = (int)PTR_ERR(child);
1999 CERROR("lookup %s under parent "DFID": rc = %d\n",
2000 lname->ln_name, PFID(lu_object_fid(ld_parent)),
2007 RETURN(ERR_PTR(rc));
2012 static void echo_ucred_init(struct lu_env *env)
2014 struct lu_ucred *ucred = lu_ucred(env);
2016 ucred->uc_valid = UCRED_INVALID;
2018 ucred->uc_suppgids[0] = -1;
2019 ucred->uc_suppgids[1] = -1;
2021 ucred->uc_uid = ucred->uc_o_uid =
2022 from_kuid(&init_user_ns, current_uid());
2023 ucred->uc_gid = ucred->uc_o_gid =
2024 from_kgid(&init_user_ns, current_gid());
2025 ucred->uc_fsuid = ucred->uc_o_fsuid =
2026 from_kuid(&init_user_ns, current_fsuid());
2027 ucred->uc_fsgid = ucred->uc_o_fsgid =
2028 from_kgid(&init_user_ns, current_fsgid());
2029 ucred->uc_cap = cfs_curproc_cap_pack();
2031 /* remove fs privilege for non-root user. */
2032 if (ucred->uc_fsuid)
2033 ucred->uc_cap &= ~CFS_CAP_FS_MASK;
2034 ucred->uc_valid = UCRED_NEW;
2037 static void echo_ucred_fini(struct lu_env *env)
2039 struct lu_ucred *ucred = lu_ucred(env);
2040 ucred->uc_valid = UCRED_INIT;
2043 #define ECHO_MD_CTX_TAG (LCT_REMEMBER | LCT_MD_THREAD)
2044 #define ECHO_MD_SES_TAG (LCT_REMEMBER | LCT_SESSION | LCT_SERVER_SESSION)
2045 static int echo_md_handler(struct echo_device *ed, int command,
2046 char *path, int path_len, __u64 id, int count,
2047 struct obd_ioctl_data *data)
2049 struct echo_thread_info *info;
2050 struct lu_device *ld = ed->ed_next;
2053 struct lu_object *parent;
2055 int namelen = data->ioc_plen2;
2060 CERROR("MD echo client is not being initialized properly\n");
2064 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
2065 CERROR("Only support MDD layer right now!\n");
2069 env = cl_env_get(&refcheck);
2071 RETURN(PTR_ERR(env));
2073 rc = lu_env_refill_by_tags(env, ECHO_MD_CTX_TAG, ECHO_MD_SES_TAG);
2077 /* init big_lmm buffer */
2078 info = echo_env_info(env);
2079 LASSERT(info->eti_big_lmm == NULL);
2080 OBD_ALLOC_LARGE(info->eti_big_lmm, MIN_MD_SIZE);
2081 if (info->eti_big_lmm == NULL)
2082 GOTO(out_env, rc = -ENOMEM);
2083 info->eti_big_lmmsize = MIN_MD_SIZE;
2085 parent = echo_resolve_path(env, ed, path, path_len);
2086 if (IS_ERR(parent)) {
2087 CERROR("Can not resolve the path %s: rc = %ld\n", path,
2089 GOTO(out_free, rc = PTR_ERR(parent));
2093 OBD_ALLOC(name, namelen + 1);
2095 GOTO(out_put, rc = -ENOMEM);
2096 if (copy_from_user(name, data->ioc_pbuf2, namelen))
2097 GOTO(out_name, rc = -EFAULT);
2100 echo_ucred_init(env);
2103 case ECHO_MD_CREATE:
2104 case ECHO_MD_MKDIR: {
2105 struct echo_thread_info *info = echo_env_info(env);
2106 __u32 mode = data->ioc_obdo2.o_mode;
2107 struct lu_fid *fid = &info->eti_fid;
2108 int stripe_count = (int)data->ioc_obdo2.o_misc;
2109 int stripe_index = (int)data->ioc_obdo2.o_stripe_idx;
2111 rc = ostid_to_fid(fid, &data->ioc_obdo1.o_oi, 0);
2115 /* In the function below, .hs_keycmp resolves to
2116 * lu_obj_hop_keycmp() */
2117 /* coverity[overrun-buffer-val] */
2118 rc = echo_create_md_object(env, ed, parent, fid, name, namelen,
2119 id, mode, count, stripe_count,
2123 case ECHO_MD_DESTROY:
2124 case ECHO_MD_RMDIR: {
2125 __u32 mode = data->ioc_obdo2.o_mode;
2127 rc = echo_destroy_object(env, ed, parent, name, namelen,
2131 case ECHO_MD_LOOKUP:
2132 rc = echo_lookup_object(env, ed, parent, id, count);
2134 case ECHO_MD_GETATTR:
2135 rc = echo_getattr_object(env, ed, parent, id, count);
2137 case ECHO_MD_SETATTR:
2138 rc = echo_setattr_object(env, ed, parent, id, count);
2141 CERROR("unknown command %d\n", command);
2145 echo_ucred_fini(env);
2149 OBD_FREE(name, namelen + 1);
2151 lu_object_put(env, parent);
2153 LASSERT(info->eti_big_lmm);
2154 OBD_FREE_LARGE(info->eti_big_lmm, info->eti_big_lmmsize);
2155 info->eti_big_lmm = NULL;
2156 info->eti_big_lmmsize = 0;
2158 cl_env_put(env, &refcheck);
2161 #endif /* HAVE_SERVER_SUPPORT */
2163 static int echo_create_object(const struct lu_env *env, struct echo_device *ed,
2166 struct echo_object *eco;
2167 struct echo_client_obd *ec = ed->ed_ec;
2172 if (!(oa->o_valid & OBD_MD_FLID) ||
2173 !(oa->o_valid & OBD_MD_FLGROUP) ||
2174 !fid_seq_is_echo(ostid_seq(&oa->o_oi))) {
2175 CERROR("invalid oid "DOSTID"\n", POSTID(&oa->o_oi));
2179 if (ostid_id(&oa->o_oi) == 0)
2180 ostid_set_id(&oa->o_oi, ++last_object_id);
2182 rc = obd_create(env, ec->ec_exp, oa);
2184 CERROR("Cannot create objects: rc = %d\n", rc);
2190 oa->o_valid |= OBD_MD_FLID;
2192 eco = cl_echo_object_find(ed, &oa->o_oi);
2194 GOTO(failed, rc = PTR_ERR(eco));
2195 cl_echo_object_put(eco);
2197 CDEBUG(D_INFO, "oa oid "DOSTID"\n", POSTID(&oa->o_oi));
2201 if (created && rc != 0)
2202 obd_destroy(env, ec->ec_exp, oa);
2205 CERROR("create object failed with: rc = %d\n", rc);
2210 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
2213 struct echo_object *eco;
2217 if (!(oa->o_valid & OBD_MD_FLID) ||
2218 !(oa->o_valid & OBD_MD_FLGROUP) ||
2219 ostid_id(&oa->o_oi) == 0) {
2220 CERROR("invalid oid "DOSTID"\n", POSTID(&oa->o_oi));
2225 eco = cl_echo_object_find(ed, &oa->o_oi);
2234 static void echo_put_object(struct echo_object *eco)
2238 rc = cl_echo_object_put(eco);
2240 CERROR("%s: echo client drop an object failed: rc = %d\n",
2241 eco->eo_dev->ed_ec->ec_exp->exp_obd->obd_name, rc);
2244 static void echo_client_page_debug_setup(struct page *page, int rw, u64 id,
2245 u64 offset, u64 count)
2252 /* no partial pages on the client */
2253 LASSERT(count == PAGE_SIZE);
2257 for (delta = 0; delta < PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2258 if (rw == OBD_BRW_WRITE) {
2259 stripe_off = offset + delta;
2262 stripe_off = 0xdeadbeef00c0ffeeULL;
2263 stripe_id = 0xdeadbeef00c0ffeeULL;
2265 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
2266 stripe_off, stripe_id);
2273 echo_client_page_debug_check(struct page *page, u64 id, u64 offset, u64 count)
2282 /* no partial pages on the client */
2283 LASSERT(count == PAGE_SIZE);
2287 for (rc = delta = 0; delta < PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2288 stripe_off = offset + delta;
2291 rc2 = block_debug_check("test_brw",
2292 addr + delta, OBD_ECHO_BLOCK_SIZE,
2293 stripe_off, stripe_id);
2295 CERROR("Error in echo object %#llx\n", id);
2304 static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
2305 struct echo_object *eco, u64 offset,
2306 u64 count, int async)
2309 struct brw_page *pga;
2310 struct brw_page *pgp;
2311 struct page **pages;
2320 verify = (ostid_id(&oa->o_oi) != ECHO_PERSISTENT_OBJID &&
2321 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
2322 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
2324 gfp_mask = ((ostid_id(&oa->o_oi) & 2) == 0) ? GFP_KERNEL : GFP_HIGHUSER;
2326 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
2328 if ((count & (~PAGE_MASK)) != 0)
2331 /* XXX think again with misaligned I/O */
2332 npages = count >> PAGE_SHIFT;
2334 if (rw == OBD_BRW_WRITE)
2335 brw_flags = OBD_BRW_ASYNC;
2337 OBD_ALLOC(pga, npages * sizeof(*pga));
2341 OBD_ALLOC(pages, npages * sizeof(*pages));
2342 if (pages == NULL) {
2343 OBD_FREE(pga, npages * sizeof(*pga));
2347 for (i = 0, pgp = pga, off = offset;
2349 i++, pgp++, off += PAGE_SIZE) {
2351 LASSERT(pgp->pg == NULL); /* for cleanup */
2354 pgp->pg = alloc_page(gfp_mask);
2355 if (pgp->pg == NULL)
2359 pgp->count = PAGE_SIZE;
2361 pgp->flag = brw_flags;
2364 echo_client_page_debug_setup(pgp->pg, rw,
2365 ostid_id(&oa->o_oi), off,
2369 /* brw mode can only be used at client */
2370 LASSERT(ed->ed_next != NULL);
2371 rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
2374 if (rc != 0 || rw != OBD_BRW_READ)
2377 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
2378 if (pgp->pg == NULL)
2383 vrc = echo_client_page_debug_check(pgp->pg,
2384 ostid_id(&oa->o_oi),
2385 pgp->off, pgp->count);
2386 if (vrc != 0 && rc == 0)
2389 __free_page(pgp->pg);
2391 OBD_FREE(pga, npages * sizeof(*pga));
2392 OBD_FREE(pages, npages * sizeof(*pages));
2396 static int echo_client_prep_commit(const struct lu_env *env,
2397 struct obd_export *exp, int rw,
2398 struct obdo *oa, struct echo_object *eco,
2399 u64 offset, u64 count,
2400 u64 batch, int async)
2402 struct obd_ioobj ioo;
2403 struct niobuf_local *lnb;
2404 struct niobuf_remote rnb;
2406 u64 npages, tot_pages, apc;
2407 int i, ret = 0, brw_flags = 0;
2411 if (count <= 0 || (count & ~PAGE_MASK) != 0)
2414 apc = npages = batch >> PAGE_SHIFT;
2415 tot_pages = count >> PAGE_SHIFT;
2417 OBD_ALLOC(lnb, apc * sizeof(struct niobuf_local));
2421 if (rw == OBD_BRW_WRITE && async)
2422 brw_flags |= OBD_BRW_ASYNC;
2424 obdo_to_ioobj(oa, &ioo);
2428 for (; tot_pages > 0; tot_pages -= npages) {
2431 if (tot_pages < npages)
2434 rnb.rnb_offset = off;
2435 rnb.rnb_len = npages * PAGE_SIZE;
2436 rnb.rnb_flags = brw_flags;
2438 off += npages * PAGE_SIZE;
2441 ret = obd_preprw(env, rw, exp, oa, 1, &ioo, &rnb, &lpages, lnb);
2445 for (i = 0; i < lpages; i++) {
2446 struct page *page = lnb[i].lnb_page;
2448 /* read past eof? */
2449 if (page == NULL && lnb[i].lnb_rc == 0)
2453 lnb[i].lnb_flags |= OBD_BRW_ASYNC;
2455 if (ostid_id(&oa->o_oi) == ECHO_PERSISTENT_OBJID ||
2456 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
2457 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
2460 if (rw == OBD_BRW_WRITE)
2461 echo_client_page_debug_setup(page, rw,
2462 ostid_id(&oa->o_oi),
2463 lnb[i].lnb_file_offset,
2466 echo_client_page_debug_check(page,
2467 ostid_id(&oa->o_oi),
2468 lnb[i].lnb_file_offset,
2472 ret = obd_commitrw(env, rw, exp, oa, 1, &ioo, &rnb, npages, lnb,
2477 /* Reuse env context. */
2478 lu_context_exit((struct lu_context *)&env->le_ctx);
2479 lu_context_enter((struct lu_context *)&env->le_ctx);
2483 OBD_FREE(lnb, apc * sizeof(struct niobuf_local));
2488 static int echo_client_brw_ioctl(const struct lu_env *env, int rw,
2489 struct obd_export *exp,
2490 struct obd_ioctl_data *data)
2492 struct obd_device *obd = class_exp2obd(exp);
2493 struct echo_device *ed = obd2echo_dev(obd);
2494 struct echo_client_obd *ec = ed->ed_ec;
2495 struct obdo *oa = &data->ioc_obdo1;
2496 struct echo_object *eco;
2502 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2504 rc = echo_get_object(&eco, ed, oa);
2508 oa->o_valid &= ~OBD_MD_FLHANDLE;
2510 /* OFD/obdfilter works only via prep/commit */
2511 test_mode = (long)data->ioc_pbuf1;
2512 if (ed->ed_next == NULL && test_mode != 3) {
2514 data->ioc_plen1 = data->ioc_count;
2520 /* Truncate batch size to maximum */
2521 if (data->ioc_plen1 > PTLRPC_MAX_BRW_SIZE)
2522 data->ioc_plen1 = PTLRPC_MAX_BRW_SIZE;
2524 switch (test_mode) {
2528 rc = echo_client_kbrw(ed, rw, oa, eco, data->ioc_offset,
2529 data->ioc_count, async);
2532 rc = echo_client_prep_commit(env, ec->ec_exp, rw, oa, eco,
2533 data->ioc_offset, data->ioc_count,
2534 data->ioc_plen1, async);
2540 echo_put_object(eco);
2546 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2547 void *karg, void __user *uarg)
2549 #ifdef HAVE_SERVER_SUPPORT
2550 struct tgt_session_info *tsi;
2552 struct obd_device *obd = exp->exp_obd;
2553 struct echo_device *ed = obd2echo_dev(obd);
2554 struct echo_client_obd *ec = ed->ed_ec;
2555 struct echo_object *eco;
2556 struct obd_ioctl_data *data = karg;
2560 int rw = OBD_BRW_READ;
2562 #ifdef HAVE_SERVER_SUPPORT
2563 struct lu_context echo_session;
2567 oa = &data->ioc_obdo1;
2568 if (!(oa->o_valid & OBD_MD_FLGROUP)) {
2569 oa->o_valid |= OBD_MD_FLGROUP;
2570 ostid_set_seq_echo(&oa->o_oi);
2573 /* This FID is unpacked just for validation at this point */
2574 rc = ostid_to_fid(&fid, &oa->o_oi, 0);
2582 rc = lu_env_init(env, LCT_DT_THREAD);
2584 GOTO(out_alloc, rc = -ENOMEM);
2586 #ifdef HAVE_SERVER_SUPPORT
2587 env->le_ses = &echo_session;
2588 rc = lu_context_init(env->le_ses, LCT_SERVER_SESSION | LCT_NOREF);
2589 if (unlikely(rc < 0))
2591 lu_context_enter(env->le_ses);
2593 tsi = tgt_ses_info(env);
2594 tsi->tsi_exp = ec->ec_exp;
2595 tsi->tsi_jobid = NULL;
2598 case OBD_IOC_CREATE: /* may create echo object */
2599 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2600 GOTO (out, rc = -EPERM);
2602 rc = echo_create_object(env, ed, oa);
2605 #ifdef HAVE_SERVER_SUPPORT
2606 case OBD_IOC_ECHO_MD: {
2613 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2614 GOTO(out, rc = -EPERM);
2616 count = data->ioc_count;
2617 cmd = data->ioc_command;
2619 id = data->ioc_obdo2.o_oi.oi.oi_id;
2620 dirlen = data->ioc_plen1;
2621 OBD_ALLOC(dir, dirlen + 1);
2623 GOTO(out, rc = -ENOMEM);
2625 if (copy_from_user(dir, data->ioc_pbuf1, dirlen)) {
2626 OBD_FREE(dir, data->ioc_plen1 + 1);
2627 GOTO(out, rc = -EFAULT);
2630 rc = echo_md_handler(ed, cmd, dir, dirlen, id, count, data);
2631 OBD_FREE(dir, dirlen + 1);
2634 case OBD_IOC_ECHO_ALLOC_SEQ: {
2635 struct lu_env *cl_env;
2640 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2641 GOTO(out, rc = -EPERM);
2643 cl_env = cl_env_get(&refcheck);
2645 GOTO(out, rc = PTR_ERR(cl_env));
2647 rc = lu_env_refill_by_tags(cl_env, ECHO_MD_CTX_TAG,
2650 cl_env_put(cl_env, &refcheck);
2654 rc = seq_client_get_seq(cl_env, ed->ed_cl_seq, &seq);
2655 cl_env_put(cl_env, &refcheck);
2657 CERROR("%s: Can not alloc seq: rc = %d\n",
2662 if (copy_to_user(data->ioc_pbuf1, &seq, data->ioc_plen1))
2665 max_count = LUSTRE_METADATA_SEQ_MAX_WIDTH;
2666 if (copy_to_user(data->ioc_pbuf2, &max_count,
2671 #endif /* HAVE_SERVER_SUPPORT */
2672 case OBD_IOC_DESTROY:
2673 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2674 GOTO (out, rc = -EPERM);
2676 rc = echo_get_object(&eco, ed, oa);
2678 rc = obd_destroy(env, ec->ec_exp, oa);
2680 eco->eo_deleted = 1;
2681 echo_put_object(eco);
2685 case OBD_IOC_GETATTR:
2686 rc = echo_get_object(&eco, ed, oa);
2688 rc = obd_getattr(env, ec->ec_exp, oa);
2689 echo_put_object(eco);
2693 case OBD_IOC_SETATTR:
2694 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2695 GOTO (out, rc = -EPERM);
2697 rc = echo_get_object(&eco, ed, oa);
2699 rc = obd_setattr(env, ec->ec_exp, oa);
2700 echo_put_object(eco);
2704 case OBD_IOC_BRW_WRITE:
2705 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2706 GOTO (out, rc = -EPERM);
2710 case OBD_IOC_BRW_READ:
2711 rc = echo_client_brw_ioctl(env, rw, exp, data);
2715 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
2716 GOTO (out, rc = -ENOTTY);
2721 #ifdef HAVE_SERVER_SUPPORT
2722 lu_context_exit(env->le_ses);
2723 lu_context_fini(env->le_ses);
2733 static int echo_client_setup(const struct lu_env *env,
2734 struct obd_device *obddev, struct lustre_cfg *lcfg)
2736 struct echo_client_obd *ec = &obddev->u.echo_client;
2737 struct obd_device *tgt;
2738 struct obd_uuid echo_uuid = { "ECHO_UUID" };
2739 struct obd_connect_data *ocd = NULL;
2743 if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
2744 CERROR("requires a TARGET OBD name\n");
2748 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
2749 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
2750 CERROR("device not attached or not set up (%s)\n",
2751 lustre_cfg_string(lcfg, 1));
2755 spin_lock_init(&ec->ec_lock);
2756 INIT_LIST_HEAD(&ec->ec_objects);
2757 INIT_LIST_HEAD(&ec->ec_locks);
2760 if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
2761 #ifdef HAVE_SERVER_SUPPORT
2762 lu_context_tags_update(ECHO_MD_CTX_TAG);
2763 lu_session_tags_update(ECHO_MD_SES_TAG);
2765 CERROR("Local operations are NOT supported on client side. "
2766 "Only remote operations are supported. Metadata client "
2767 "must be run on server side.\n");
2772 OBD_ALLOC(ocd, sizeof(*ocd));
2774 CERROR("Can't alloc ocd connecting to %s\n",
2775 lustre_cfg_string(lcfg, 1));
2779 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
2780 OBD_CONNECT_BRW_SIZE |
2781 OBD_CONNECT_GRANT | OBD_CONNECT_FULL20 |
2782 OBD_CONNECT_64BITHASH | OBD_CONNECT_LVB_TYPE |
2784 ocd->ocd_brw_size = DT_MAX_BRW_SIZE;
2785 ocd->ocd_version = LUSTRE_VERSION_CODE;
2786 ocd->ocd_group = FID_SEQ_ECHO;
2788 rc = obd_connect(env, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
2790 /* Turn off pinger because it connects to tgt obd directly. */
2791 spin_lock(&tgt->obd_dev_lock);
2792 list_del_init(&ec->ec_exp->exp_obd_chain_timed);
2793 spin_unlock(&tgt->obd_dev_lock);
2796 OBD_FREE(ocd, sizeof(*ocd));
2799 CERROR("fail to connect to device %s\n",
2800 lustre_cfg_string(lcfg, 1));
2807 static int echo_client_cleanup(struct obd_device *obddev)
2809 struct echo_device *ed = obd2echo_dev(obddev);
2810 struct echo_client_obd *ec = &obddev->u.echo_client;
2814 /*Do nothing for Metadata echo client*/
2818 if (ed->ed_next_ismd) {
2819 #ifdef HAVE_SERVER_SUPPORT
2820 lu_context_tags_clear(ECHO_MD_CTX_TAG);
2821 lu_session_tags_clear(ECHO_MD_SES_TAG);
2823 CERROR("This is client-side only module, does not support "
2824 "metadata echo client.\n");
2829 if (!list_empty(&obddev->obd_exports)) {
2830 CERROR("still has clients!\n");
2834 LASSERT(atomic_read(&ec->ec_exp->exp_refcount) > 0);
2835 rc = obd_disconnect(ec->ec_exp);
2837 CERROR("fail to disconnect device: %d\n", rc);
2842 static int echo_client_connect(const struct lu_env *env,
2843 struct obd_export **exp,
2844 struct obd_device *src, struct obd_uuid *cluuid,
2845 struct obd_connect_data *data, void *localdata)
2848 struct lustre_handle conn = { 0 };
2851 rc = class_connect(&conn, src, cluuid);
2853 *exp = class_conn2export(&conn);
2859 static int echo_client_disconnect(struct obd_export *exp)
2865 GOTO(out, rc = -EINVAL);
2867 rc = class_disconnect(exp);
2873 static struct obd_ops echo_client_obd_ops = {
2874 .o_owner = THIS_MODULE,
2875 .o_iocontrol = echo_client_iocontrol,
2876 .o_connect = echo_client_connect,
2877 .o_disconnect = echo_client_disconnect
2880 static int __init obdecho_init(void)
2885 LCONSOLE_INFO("Echo OBD driver; http://www.lustre.org/\n");
2887 LASSERT(PAGE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
2889 # ifdef HAVE_SERVER_SUPPORT
2890 rc = echo_persistent_pages_init();
2894 rc = class_register_type(&echo_obd_ops, NULL, true, NULL,
2895 LUSTRE_ECHO_NAME, NULL);
2900 rc = lu_kmem_init(echo_caches);
2902 rc = class_register_type(&echo_client_obd_ops, NULL, true, NULL,
2903 LUSTRE_ECHO_CLIENT_NAME,
2906 lu_kmem_fini(echo_caches);
2909 # ifdef HAVE_SERVER_SUPPORT
2913 class_unregister_type(LUSTRE_ECHO_NAME);
2915 echo_persistent_pages_fini();
2921 static void __exit obdecho_exit(void)
2923 class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
2924 lu_kmem_fini(echo_caches);
2926 #ifdef HAVE_SERVER_SUPPORT
2927 class_unregister_type(LUSTRE_ECHO_NAME);
2928 echo_persistent_pages_fini();
2932 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2933 MODULE_DESCRIPTION("Lustre Echo Client test driver");
2934 MODULE_VERSION(LUSTRE_VERSION_STRING);
2935 MODULE_LICENSE("GPL");
2937 module_init(obdecho_init);
2938 module_exit(obdecho_exit);
2940 /** @} echo_client */