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, 2017, 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_lmv.h>
49 #include <lustre_acl.h>
50 #include <uapi/linux/lustre/lustre_ioctl.h>
51 #include <lustre_net.h>
52 #ifdef HAVE_SERVER_SUPPORT
53 # include <md_object.h>
55 #define ETI_NAME_LEN 20
57 #endif /* HAVE_SERVER_SUPPORT */
59 #include "echo_internal.h"
61 /** \defgroup echo_client Echo Client
66 struct cl_device ed_cl;
67 struct echo_client_obd *ed_ec;
69 struct cl_site ed_site_myself;
70 struct lu_site *ed_site;
71 struct lu_device *ed_next;
73 struct lu_client_seq *ed_cl_seq;
74 #ifdef HAVE_SERVER_SUPPORT
75 struct local_oid_storage *ed_los;
76 struct lu_fid ed_root_fid;
77 #endif /* HAVE_SERVER_SUPPORT */
81 struct cl_object eo_cl;
82 struct cl_object_header eo_hdr;
83 struct echo_device *eo_dev;
84 struct list_head eo_obj_chain;
85 struct lov_oinfo *eo_oinfo;
90 struct echo_object_conf {
91 struct cl_object_conf eoc_cl;
92 struct lov_oinfo **eoc_oinfo;
96 struct cl_page_slice ep_cl;
101 struct cl_lock_slice el_cl;
102 struct list_head el_chain;
103 struct echo_object *el_object;
105 atomic_t el_refcount;
108 #ifdef HAVE_SERVER_SUPPORT
109 static const char echo_md_root_dir_name[] = "ROOT_ECHO";
112 * In order to use the values of members in struct mdd_device,
113 * we define an alias structure here.
115 struct echo_md_device {
116 struct md_device emd_md_dev;
117 struct obd_export *emd_child_exp;
118 struct dt_device *emd_child;
119 struct dt_device *emd_bottom;
120 struct lu_fid emd_root_fid;
121 struct lu_fid emd_local_root_fid;
123 #endif /* HAVE_SERVER_SUPPORT */
125 static int echo_client_setup(const struct lu_env *env,
126 struct obd_device *obddev,
127 struct lustre_cfg *lcfg);
128 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;
175 static inline struct echo_thread_info *echo_env_info(const struct lu_env *env)
177 struct echo_thread_info *info;
179 info = lu_context_key_get(&env->le_ctx, &echo_thread_key);
180 LASSERT(info != NULL);
185 struct echo_object_conf *cl2echo_conf(const struct cl_object_conf *c)
187 return container_of(c, struct echo_object_conf, eoc_cl);
190 #ifdef HAVE_SERVER_SUPPORT
191 static inline struct echo_md_device *lu2emd_dev(struct lu_device *d)
193 return container_of0(d, struct echo_md_device, emd_md_dev.md_lu_dev);
196 static inline struct lu_device *emd2lu_dev(struct echo_md_device *d)
198 return &d->emd_md_dev.md_lu_dev;
201 static inline struct seq_server_site *echo_md_seq_site(struct echo_md_device *d)
203 return emd2lu_dev(d)->ld_site->ld_seq_site;
206 static inline struct obd_device *emd2obd_dev(struct echo_md_device *d)
208 return d->emd_md_dev.md_lu_dev.ld_obd;
210 #endif /* HAVE_SERVER_SUPPORT */
212 /** @} echo_helpers */
214 static int cl_echo_object_put(struct echo_object *eco);
215 static int cl_echo_object_brw(struct echo_object *eco, int rw, u64 offset,
216 struct page **pages, int npages, int async);
218 struct echo_thread_info {
219 struct echo_object_conf eti_conf;
220 struct lustre_md eti_md;
221 struct cl_2queue eti_queue;
223 struct cl_lock eti_lock;
224 struct lu_fid eti_fid;
225 struct lu_fid eti_fid2;
226 #ifdef HAVE_SERVER_SUPPORT
227 struct md_op_spec eti_spec;
228 struct lov_mds_md_v3 eti_lmm;
229 struct lov_user_md_v3 eti_lum;
230 struct md_attr eti_ma;
231 struct lu_name eti_lname;
232 /* per-thread values, can be re-used */
233 void *eti_big_lmm; /* may be vmalloc'd */
235 char eti_name[ETI_NAME_LEN];
236 struct lu_buf eti_buf;
237 /* If we want to test large ACL, then need to enlarge the buffer. */
238 char eti_xattr_buf[LUSTRE_POSIX_ACL_MAX_SIZE_OLD];
242 /* No session used right now */
243 struct echo_session_info {
247 static struct kmem_cache *echo_lock_kmem;
248 static struct kmem_cache *echo_object_kmem;
249 static struct kmem_cache *echo_thread_kmem;
250 static struct kmem_cache *echo_session_kmem;
251 /* static struct kmem_cache *echo_req_kmem; */
253 static struct lu_kmem_descr echo_caches[] = {
255 .ckd_cache = &echo_lock_kmem,
256 .ckd_name = "echo_lock_kmem",
257 .ckd_size = sizeof(struct echo_lock)
260 .ckd_cache = &echo_object_kmem,
261 .ckd_name = "echo_object_kmem",
262 .ckd_size = sizeof(struct echo_object)
265 .ckd_cache = &echo_thread_kmem,
266 .ckd_name = "echo_thread_kmem",
267 .ckd_size = sizeof(struct echo_thread_info)
270 .ckd_cache = &echo_session_kmem,
271 .ckd_name = "echo_session_kmem",
272 .ckd_size = sizeof(struct echo_session_info)
279 /** \defgroup echo_page Page operations
281 * Echo page operations.
285 static int echo_page_own(const struct lu_env *env,
286 const struct cl_page_slice *slice,
287 struct cl_io *io, int nonblock)
289 struct echo_page *ep = cl2echo_page(slice);
292 mutex_lock(&ep->ep_lock);
293 else if (!mutex_trylock(&ep->ep_lock))
298 static void echo_page_disown(const struct lu_env *env,
299 const struct cl_page_slice *slice,
302 struct echo_page *ep = cl2echo_page(slice);
304 LASSERT(mutex_is_locked(&ep->ep_lock));
305 mutex_unlock(&ep->ep_lock);
308 static void echo_page_discard(const struct lu_env *env,
309 const struct cl_page_slice *slice,
310 struct cl_io *unused)
312 cl_page_delete(env, slice->cpl_page);
315 static int echo_page_is_vmlocked(const struct lu_env *env,
316 const struct cl_page_slice *slice)
318 if (mutex_is_locked(&cl2echo_page(slice)->ep_lock))
323 static void echo_page_completion(const struct lu_env *env,
324 const struct cl_page_slice *slice,
327 LASSERT(slice->cpl_page->cp_sync_io != NULL);
330 static void echo_page_fini(const struct lu_env *env,
331 struct cl_page_slice *slice,
332 struct pagevec *pvec)
334 struct echo_object *eco = cl2echo_obj(slice->cpl_obj);
337 atomic_dec(&eco->eo_npages);
338 put_page(slice->cpl_page->cp_vmpage);
342 static int echo_page_prep(const struct lu_env *env,
343 const struct cl_page_slice *slice,
344 struct cl_io *unused)
349 static int echo_page_print(const struct lu_env *env,
350 const struct cl_page_slice *slice,
351 void *cookie, lu_printer_t printer)
353 struct echo_page *ep = cl2echo_page(slice);
355 (*printer)(env, cookie, LUSTRE_ECHO_CLIENT_NAME"-page@%p %d vm@%p\n",
356 ep, mutex_is_locked(&ep->ep_lock),
357 slice->cpl_page->cp_vmpage);
361 static const struct cl_page_operations echo_page_ops = {
362 .cpo_own = echo_page_own,
363 .cpo_disown = echo_page_disown,
364 .cpo_discard = echo_page_discard,
365 .cpo_fini = echo_page_fini,
366 .cpo_print = echo_page_print,
367 .cpo_is_vmlocked = echo_page_is_vmlocked,
370 .cpo_prep = echo_page_prep,
371 .cpo_completion = echo_page_completion,
374 .cpo_prep = echo_page_prep,
375 .cpo_completion = echo_page_completion,
382 /** \defgroup echo_lock Locking
384 * echo lock operations
388 static void echo_lock_fini(const struct lu_env *env,
389 struct cl_lock_slice *slice)
391 struct echo_lock *ecl = cl2echo_lock(slice);
393 LASSERT(list_empty(&ecl->el_chain));
394 OBD_SLAB_FREE_PTR(ecl, echo_lock_kmem);
397 static struct cl_lock_operations echo_lock_ops = {
398 .clo_fini = echo_lock_fini,
403 /** \defgroup echo_cl_ops cl_object operations
405 * operations for cl_object
409 static int echo_page_init(const struct lu_env *env, struct cl_object *obj,
410 struct cl_page *page, pgoff_t index)
412 struct echo_page *ep = cl_object_page_slice(obj, page);
413 struct echo_object *eco = cl2echo_obj(obj);
416 get_page(page->cp_vmpage);
417 mutex_init(&ep->ep_lock);
418 cl_page_slice_add(page, &ep->ep_cl, obj, index, &echo_page_ops);
419 atomic_inc(&eco->eo_npages);
423 static int echo_io_init(const struct lu_env *env, struct cl_object *obj,
429 static int echo_lock_init(const struct lu_env *env,
430 struct cl_object *obj, struct cl_lock *lock,
431 const struct cl_io *unused)
433 struct echo_lock *el;
436 OBD_SLAB_ALLOC_PTR_GFP(el, echo_lock_kmem, GFP_NOFS);
438 cl_lock_slice_add(lock, &el->el_cl, obj, &echo_lock_ops);
439 el->el_object = cl2echo_obj(obj);
440 INIT_LIST_HEAD(&el->el_chain);
441 atomic_set(&el->el_refcount, 0);
443 RETURN(el ? 0 : -ENOMEM);
446 static int echo_conf_set(const struct lu_env *env, struct cl_object *obj,
447 const struct cl_object_conf *conf)
452 static const struct cl_object_operations echo_cl_obj_ops = {
453 .coo_page_init = echo_page_init,
454 .coo_lock_init = echo_lock_init,
455 .coo_io_init = echo_io_init,
456 .coo_conf_set = echo_conf_set
458 /** @} echo_cl_ops */
460 /** \defgroup echo_lu_ops lu_object operations
462 * operations for echo lu object.
466 static int echo_object_init(const struct lu_env *env, struct lu_object *obj,
467 const struct lu_object_conf *conf)
469 struct echo_device *ed = cl2echo_dev(lu2cl_dev(obj->lo_dev));
470 struct echo_client_obd *ec = ed->ed_ec;
471 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
475 struct lu_object *below;
476 struct lu_device *under;
479 below = under->ld_ops->ldo_object_alloc(env, obj->lo_header,
483 lu_object_add(obj, below);
486 if (!ed->ed_next_ismd) {
487 const struct cl_object_conf *cconf = lu2cl_conf(conf);
488 struct echo_object_conf *econf = cl2echo_conf(cconf);
490 LASSERT(econf->eoc_oinfo != NULL);
493 * Transfer the oinfo pointer to eco that it won't be
496 eco->eo_oinfo = *econf->eoc_oinfo;
497 *econf->eoc_oinfo = NULL;
499 eco->eo_oinfo = NULL;
503 atomic_set(&eco->eo_npages, 0);
504 cl_object_page_init(lu2cl(obj), sizeof(struct echo_page));
506 spin_lock(&ec->ec_lock);
507 list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
508 spin_unlock(&ec->ec_lock);
513 static void echo_object_free(const struct lu_env *env, struct lu_object *obj)
515 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
516 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
519 LASSERT(atomic_read(&eco->eo_npages) == 0);
521 spin_lock(&ec->ec_lock);
522 list_del_init(&eco->eo_obj_chain);
523 spin_unlock(&ec->ec_lock);
526 lu_object_header_fini(obj->lo_header);
529 OBD_FREE_PTR(eco->eo_oinfo);
531 OBD_SLAB_FREE_PTR(eco, echo_object_kmem);
535 static int echo_object_print(const struct lu_env *env, void *cookie,
536 lu_printer_t p, const struct lu_object *o)
538 struct echo_object *obj = cl2echo_obj(lu2cl(o));
540 return (*p)(env, cookie, "echoclient-object@%p", obj);
543 static const struct lu_object_operations echo_lu_obj_ops = {
544 .loo_object_init = echo_object_init,
545 .loo_object_delete = NULL,
546 .loo_object_release = NULL,
547 .loo_object_free = echo_object_free,
548 .loo_object_print = echo_object_print,
549 .loo_object_invariant = NULL
551 /** @} echo_lu_ops */
553 /** \defgroup echo_lu_dev_ops lu_device operations
555 * Operations for echo lu device.
559 static struct lu_object *echo_object_alloc(const struct lu_env *env,
560 const struct lu_object_header *hdr,
561 struct lu_device *dev)
563 struct echo_object *eco;
564 struct lu_object *obj = NULL;
567 /* we're the top dev. */
568 LASSERT(hdr == NULL);
569 OBD_SLAB_ALLOC_PTR_GFP(eco, echo_object_kmem, GFP_NOFS);
571 struct cl_object_header *hdr = &eco->eo_hdr;
573 obj = &echo_obj2cl(eco)->co_lu;
574 cl_object_header_init(hdr);
575 hdr->coh_page_bufsize = cfs_size_round(sizeof(struct cl_page));
577 lu_object_init(obj, &hdr->coh_lu, dev);
578 lu_object_add_top(&hdr->coh_lu, obj);
580 eco->eo_cl.co_ops = &echo_cl_obj_ops;
581 obj->lo_ops = &echo_lu_obj_ops;
586 static struct lu_device_operations echo_device_lu_ops = {
587 .ldo_object_alloc = echo_object_alloc,
590 /** @} echo_lu_dev_ops */
592 /** \defgroup echo_init Setup and teardown
594 * Init and fini functions for echo client.
598 static int echo_site_init(const struct lu_env *env, struct echo_device *ed)
600 struct cl_site *site = &ed->ed_site_myself;
603 /* initialize site */
604 rc = cl_site_init(site, &ed->ed_cl);
606 CERROR("Cannot initialize site for echo client(%d)\n", rc);
610 rc = lu_site_init_finish(&site->cs_lu);
616 ed->ed_site = &site->cs_lu;
620 static void echo_site_fini(const struct lu_env *env, struct echo_device *ed)
623 if (!ed->ed_next_ismd)
624 lu_site_fini(ed->ed_site);
629 static void *echo_thread_key_init(const struct lu_context *ctx,
630 struct lu_context_key *key)
632 struct echo_thread_info *info;
634 OBD_SLAB_ALLOC_PTR_GFP(info, echo_thread_kmem, GFP_NOFS);
636 info = ERR_PTR(-ENOMEM);
640 static void echo_thread_key_fini(const struct lu_context *ctx,
641 struct lu_context_key *key, void *data)
643 struct echo_thread_info *info = data;
645 OBD_SLAB_FREE_PTR(info, echo_thread_kmem);
648 static struct lu_context_key echo_thread_key = {
649 .lct_tags = LCT_CL_THREAD,
650 .lct_init = echo_thread_key_init,
651 .lct_fini = echo_thread_key_fini,
654 static void *echo_session_key_init(const struct lu_context *ctx,
655 struct lu_context_key *key)
657 struct echo_session_info *session;
659 OBD_SLAB_ALLOC_PTR_GFP(session, echo_session_kmem, GFP_NOFS);
661 session = ERR_PTR(-ENOMEM);
665 static void echo_session_key_fini(const struct lu_context *ctx,
666 struct lu_context_key *key, void *data)
668 struct echo_session_info *session = data;
670 OBD_SLAB_FREE_PTR(session, echo_session_kmem);
673 static struct lu_context_key echo_session_key = {
674 .lct_tags = LCT_SESSION,
675 .lct_init = echo_session_key_init,
676 .lct_fini = echo_session_key_fini,
679 LU_TYPE_INIT_FINI(echo, &echo_thread_key, &echo_session_key);
681 #ifdef HAVE_SERVER_SUPPORT
682 # define ECHO_SEQ_WIDTH 0xffffffff
683 static int echo_fid_init(struct echo_device *ed, char *obd_name,
684 struct seq_server_site *ss)
690 OBD_ALLOC_PTR(ed->ed_cl_seq);
694 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
696 GOTO(out_free_seq, rc = -ENOMEM);
698 snprintf(prefix, MAX_OBD_NAME + 5, "srv-%s", obd_name);
700 /* Init client side sequence-manager */
701 rc = seq_client_init(ed->ed_cl_seq, NULL,
703 prefix, ss->ss_server_seq);
704 ed->ed_cl_seq->lcs_width = ECHO_SEQ_WIDTH;
705 OBD_FREE(prefix, MAX_OBD_NAME + 5);
707 GOTO(out_free_seq, rc);
712 OBD_FREE_PTR(ed->ed_cl_seq);
713 ed->ed_cl_seq = NULL;
717 static int echo_fid_fini(struct obd_device *obddev)
719 struct echo_device *ed = obd2echo_dev(obddev);
723 seq_client_fini(ed->ed_cl_seq);
724 OBD_FREE_PTR(ed->ed_cl_seq);
725 ed->ed_cl_seq = NULL;
731 static void echo_ed_los_fini(const struct lu_env *env, struct echo_device *ed)
734 if (ed != NULL && ed->ed_next_ismd && ed->ed_los != NULL) {
735 local_oid_storage_fini(env, ed->ed_los);
741 echo_md_local_file_create(const struct lu_env *env, struct echo_md_device *emd,
742 struct local_oid_storage *los,
743 const struct lu_fid *pfid, const char *name,
744 __u32 mode, struct lu_fid *fid)
746 struct dt_object *parent = NULL;
747 struct dt_object *dto = NULL;
751 LASSERT(!fid_is_zero(pfid));
752 parent = dt_locate(env, emd->emd_bottom, pfid);
753 if (unlikely(IS_ERR(parent)))
754 RETURN(PTR_ERR(parent));
756 /* create local file with @fid */
757 dto = local_file_find_or_create_with_fid(env, emd->emd_bottom, fid,
760 GOTO(out_put, rc = PTR_ERR(dto));
762 *fid = *lu_object_fid(&dto->do_lu);
764 * since stack is not fully set up the local_storage uses own stack
765 * and we should drop its object from cache
767 dt_object_put_nocache(env, dto);
771 dt_object_put(env, parent);
776 echo_md_root_get(const struct lu_env *env, struct echo_md_device *emd,
777 struct echo_device *ed)
783 /* Setup local dirs */
784 fid.f_seq = FID_SEQ_LOCAL_NAME;
787 rc = local_oid_storage_init(env, emd->emd_bottom, &fid, &ed->ed_los);
791 lu_echo_root_fid(&fid);
792 if (echo_md_seq_site(emd)->ss_node_id == 0) {
793 rc = echo_md_local_file_create(env, emd, ed->ed_los,
794 &emd->emd_local_root_fid,
795 echo_md_root_dir_name, S_IFDIR |
796 S_IRUGO | S_IWUSR | S_IXUGO,
799 CERROR("%s: create md echo root fid failed: rc = %d\n",
800 emd2obd_dev(emd)->obd_name, rc);
804 ed->ed_root_fid = fid;
808 echo_ed_los_fini(env, ed);
812 #endif /* HAVE_SERVER_SUPPORT */
814 static struct lu_device *echo_device_alloc(const struct lu_env *env,
815 struct lu_device_type *t,
816 struct lustre_cfg *cfg)
818 struct lu_device *next;
819 struct echo_device *ed;
820 struct cl_device *cd;
821 struct obd_device *obd = NULL; /* to keep compiler happy */
822 struct obd_device *tgt;
823 const char *tgt_type_name;
830 GOTO(out, rc = -ENOMEM);
834 rc = cl_device_init(cd, t);
838 cd->cd_lu_dev.ld_ops = &echo_device_lu_ops;
841 obd = class_name2obd(lustre_cfg_string(cfg, 0));
842 LASSERT(obd != NULL);
843 LASSERT(env != NULL);
845 tgt = class_name2obd(lustre_cfg_string(cfg, 1));
847 CERROR("Can not find tgt device %s\n",
848 lustre_cfg_string(cfg, 1));
849 GOTO(out, rc = -ENODEV);
852 next = tgt->obd_lu_dev;
854 if (strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME) == 0) {
855 ed->ed_next_ismd = 1;
856 } else if (strcmp(tgt->obd_type->typ_name, LUSTRE_OST_NAME) == 0 ||
857 strcmp(tgt->obd_type->typ_name, LUSTRE_OSC_NAME) == 0) {
858 ed->ed_next_ismd = 0;
859 rc = echo_site_init(env, ed);
863 GOTO(out, rc = -EINVAL);
868 rc = echo_client_setup(env, obd, cfg);
872 ed->ed_ec = &obd->u.echo_client;
875 if (ed->ed_next_ismd) {
876 #ifdef HAVE_SERVER_SUPPORT
877 /* Suppose to connect to some Metadata layer */
878 struct lu_site *ls = NULL;
879 struct lu_device *ld = NULL;
880 struct md_device *md = NULL;
881 struct echo_md_device *emd = NULL;
885 CERROR("%s is not lu device type!\n",
886 lustre_cfg_string(cfg, 1));
887 GOTO(out, rc = -EINVAL);
890 tgt_type_name = lustre_cfg_string(cfg, 2);
891 if (!tgt_type_name) {
892 CERROR("%s no type name for echo %s setup\n",
893 lustre_cfg_string(cfg, 1),
894 tgt->obd_type->typ_name);
895 GOTO(out, rc = -EINVAL);
900 spin_lock(&ls->ls_ld_lock);
901 list_for_each_entry(ld, &ls->ls_ld_linkage, ld_linkage) {
902 if (strcmp(ld->ld_type->ldt_name, tgt_type_name) == 0) {
907 spin_unlock(&ls->ls_ld_lock);
910 CERROR("%s is not lu device type!\n",
911 lustre_cfg_string(cfg, 1));
912 GOTO(out, rc = -EINVAL);
916 /* For MD echo client, it will use the site in MDS stack */
918 ed->ed_cl.cd_lu_dev.ld_site = ls;
919 rc = echo_fid_init(ed, obd->obd_name, lu_site2seq(ls));
921 CERROR("echo fid init error %d\n", rc);
925 md = lu2md_dev(next);
926 emd = lu2emd_dev(&md->md_lu_dev);
927 rc = echo_md_root_get(env, emd, ed);
929 CERROR("%s: get root error: rc = %d\n",
930 emd2obd_dev(emd)->obd_name, rc);
933 #else /* !HAVE_SERVER_SUPPORT */
935 "Local operations are NOT supported on client side. Only remote operations are supported. Metadata client must be run on server side.\n");
936 GOTO(out, rc = -EOPNOTSUPP);
937 #endif /* HAVE_SERVER_SUPPORT */
940 * if echo client is to be stacked upon ost device, the next is
941 * NULL since ost is not a clio device so far
943 if (next != NULL && !lu_device_is_cl(next))
946 tgt_type_name = tgt->obd_type->typ_name;
948 LASSERT(next != NULL);
950 GOTO(out, rc = -EBUSY);
952 next->ld_site = ed->ed_site;
953 rc = next->ld_type->ldt_ops->ldto_device_init(env, next,
954 next->ld_type->ldt_name,
959 LASSERT(strcmp(tgt_type_name, LUSTRE_OST_NAME) == 0);
964 RETURN(&cd->cd_lu_dev);
970 rc2 = echo_client_cleanup(obd);
972 CERROR("Cleanup obd device %s error(%d)\n",
977 echo_site_fini(env, ed);
979 cl_device_fini(&ed->ed_cl);
989 static int echo_device_init(const struct lu_env *env, struct lu_device *d,
990 const char *name, struct lu_device *next)
996 static struct lu_device *echo_device_fini(const struct lu_env *env,
999 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
1000 struct lu_device *next = ed->ed_next;
1002 while (next && !ed->ed_next_ismd)
1003 next = next->ld_type->ldt_ops->ldto_device_fini(env, next);
1007 static void echo_lock_release(const struct lu_env *env,
1008 struct echo_lock *ecl,
1011 struct cl_lock *clk = echo_lock2cl(ecl);
1013 cl_lock_release(env, clk);
1016 static struct lu_device *echo_device_free(const struct lu_env *env,
1017 struct lu_device *d)
1019 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
1020 struct echo_client_obd *ec = ed->ed_ec;
1021 struct echo_object *eco;
1022 struct lu_device *next = ed->ed_next;
1024 CDEBUG(D_INFO, "echo device:%p is going to be freed, next = %p\n",
1027 lu_site_purge(env, ed->ed_site, -1);
1030 * check if there are objects still alive.
1031 * It shouldn't have any object because lu_site_purge would cleanup
1032 * all of cached objects. Anyway, probably the echo device is being
1033 * parallelly accessed.
1035 spin_lock(&ec->ec_lock);
1036 list_for_each_entry(eco, &ec->ec_objects, eo_obj_chain)
1037 eco->eo_deleted = 1;
1038 spin_unlock(&ec->ec_lock);
1041 lu_site_purge(env, ed->ed_site, -1);
1044 "Waiting for the reference of echo object to be dropped\n");
1046 /* Wait for the last reference to be dropped. */
1047 spin_lock(&ec->ec_lock);
1048 while (!list_empty(&ec->ec_objects)) {
1049 spin_unlock(&ec->ec_lock);
1051 "echo_client still has objects at cleanup time, wait for 1 second\n");
1052 set_current_state(TASK_UNINTERRUPTIBLE);
1053 schedule_timeout(cfs_time_seconds(1));
1054 lu_site_purge(env, ed->ed_site, -1);
1055 spin_lock(&ec->ec_lock);
1057 spin_unlock(&ec->ec_lock);
1059 LASSERT(list_empty(&ec->ec_locks));
1061 CDEBUG(D_INFO, "No object exists, exiting...\n");
1063 echo_client_cleanup(d->ld_obd);
1064 #ifdef HAVE_SERVER_SUPPORT
1065 echo_fid_fini(d->ld_obd);
1066 echo_ed_los_fini(env, ed);
1068 while (next && !ed->ed_next_ismd)
1069 next = next->ld_type->ldt_ops->ldto_device_free(env, next);
1071 LASSERT(ed->ed_site == d->ld_site);
1072 echo_site_fini(env, ed);
1073 cl_device_fini(&ed->ed_cl);
1076 cl_env_cache_purge(~0);
1081 static const struct lu_device_type_operations echo_device_type_ops = {
1082 .ldto_init = echo_type_init,
1083 .ldto_fini = echo_type_fini,
1085 .ldto_start = echo_type_start,
1086 .ldto_stop = echo_type_stop,
1088 .ldto_device_alloc = echo_device_alloc,
1089 .ldto_device_free = echo_device_free,
1090 .ldto_device_init = echo_device_init,
1091 .ldto_device_fini = echo_device_fini
1094 static struct lu_device_type echo_device_type = {
1095 .ldt_tags = LU_DEVICE_CL,
1096 .ldt_name = LUSTRE_ECHO_CLIENT_NAME,
1097 .ldt_ops = &echo_device_type_ops,
1098 .ldt_ctx_tags = LCT_CL_THREAD | LCT_MD_THREAD | LCT_DT_THREAD,
1102 /** \defgroup echo_exports Exported operations
1104 * exporting functions to echo client
1109 /* Interfaces to echo client obd device */
1110 static struct echo_object *
1111 cl_echo_object_find(struct echo_device *d, const struct ost_id *oi)
1114 struct echo_thread_info *info;
1115 struct echo_object_conf *conf;
1116 struct echo_object *eco;
1117 struct cl_object *obj;
1118 struct lov_oinfo *oinfo = NULL;
1124 LASSERTF(ostid_id(oi) != 0, DOSTID"\n", POSTID(oi));
1125 LASSERTF(ostid_seq(oi) == FID_SEQ_ECHO, DOSTID"\n", POSTID(oi));
1127 /* Never return an object if the obd is to be freed. */
1128 if (echo_dev2cl(d)->cd_lu_dev.ld_obd->obd_stopping)
1129 RETURN(ERR_PTR(-ENODEV));
1131 env = cl_env_get(&refcheck);
1133 RETURN((void *)env);
1135 info = echo_env_info(env);
1136 conf = &info->eti_conf;
1138 OBD_ALLOC_PTR(oinfo);
1140 GOTO(out, eco = ERR_PTR(-ENOMEM));
1142 oinfo->loi_oi = *oi;
1143 conf->eoc_cl.u.coc_oinfo = oinfo;
1147 * If echo_object_init() is successful then ownership of oinfo
1148 * is transferred to the object.
1150 conf->eoc_oinfo = &oinfo;
1152 fid = &info->eti_fid;
1153 rc = ostid_to_fid(fid, oi, 0);
1155 GOTO(out, eco = ERR_PTR(rc));
1158 * In the function below, .hs_keycmp resolves to
1159 * lu_obj_hop_keycmp()
1161 /* coverity[overrun-buffer-val] */
1162 obj = cl_object_find(env, echo_dev2cl(d), fid, &conf->eoc_cl);
1164 GOTO(out, eco = (void *)obj);
1166 eco = cl2echo_obj(obj);
1167 if (eco->eo_deleted) {
1168 cl_object_put(env, obj);
1169 eco = ERR_PTR(-EAGAIN);
1174 OBD_FREE_PTR(oinfo);
1176 cl_env_put(env, &refcheck);
1180 static int cl_echo_object_put(struct echo_object *eco)
1183 struct cl_object *obj = echo_obj2cl(eco);
1187 env = cl_env_get(&refcheck);
1189 RETURN(PTR_ERR(env));
1191 /* an external function to kill an object? */
1192 if (eco->eo_deleted) {
1193 struct lu_object_header *loh = obj->co_lu.lo_header;
1195 LASSERT(&eco->eo_hdr == luh2coh(loh));
1196 set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
1199 cl_object_put(env, obj);
1200 cl_env_put(env, &refcheck);
1204 static int cl_echo_enqueue0(struct lu_env *env, struct echo_object *eco,
1205 u64 start, u64 end, int mode,
1206 __u64 *cookie, __u32 enqflags)
1209 struct cl_lock *lck;
1210 struct cl_object *obj;
1211 struct cl_lock_descr *descr;
1212 struct echo_thread_info *info;
1216 info = echo_env_info(env);
1218 lck = &info->eti_lock;
1219 obj = echo_obj2cl(eco);
1221 memset(lck, 0, sizeof(*lck));
1222 descr = &lck->cll_descr;
1223 descr->cld_obj = obj;
1224 descr->cld_start = cl_index(obj, start);
1225 descr->cld_end = cl_index(obj, end);
1226 descr->cld_mode = mode == LCK_PW ? CLM_WRITE : CLM_READ;
1227 descr->cld_enq_flags = enqflags;
1230 rc = cl_lock_request(env, io, lck);
1232 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
1233 struct echo_lock *el;
1235 el = cl2echo_lock(cl_lock_at(lck, &echo_device_type));
1236 spin_lock(&ec->ec_lock);
1237 if (list_empty(&el->el_chain)) {
1238 list_add(&el->el_chain, &ec->ec_locks);
1239 el->el_cookie = ++ec->ec_unique;
1241 atomic_inc(&el->el_refcount);
1242 *cookie = el->el_cookie;
1243 spin_unlock(&ec->ec_lock);
1248 static int cl_echo_cancel0(struct lu_env *env, struct echo_device *ed,
1251 struct echo_client_obd *ec = ed->ed_ec;
1252 struct echo_lock *ecl = NULL;
1253 struct list_head *el;
1254 int found = 0, still_used = 0;
1257 LASSERT(ec != NULL);
1258 spin_lock(&ec->ec_lock);
1259 list_for_each(el, &ec->ec_locks) {
1260 ecl = list_entry(el, struct echo_lock, el_chain);
1261 CDEBUG(D_INFO, "ecl: %p, cookie: %#llx\n", ecl, ecl->el_cookie);
1262 found = (ecl->el_cookie == cookie);
1264 if (atomic_dec_and_test(&ecl->el_refcount))
1265 list_del_init(&ecl->el_chain);
1271 spin_unlock(&ec->ec_lock);
1276 echo_lock_release(env, ecl, still_used);
1280 static void echo_commit_callback(const struct lu_env *env, struct cl_io *io,
1281 struct cl_page *page)
1283 struct echo_thread_info *info;
1284 struct cl_2queue *queue;
1286 info = echo_env_info(env);
1287 LASSERT(io == &info->eti_io);
1289 queue = &info->eti_queue;
1290 cl_page_list_add(&queue->c2_qout, page);
1293 static int cl_echo_object_brw(struct echo_object *eco, int rw, u64 offset,
1294 struct page **pages, int npages, int async)
1297 struct echo_thread_info *info;
1298 struct cl_object *obj = echo_obj2cl(eco);
1299 struct echo_device *ed = eco->eo_dev;
1300 struct cl_2queue *queue;
1302 struct cl_page *clp;
1303 struct lustre_handle lh = { 0 };
1304 int page_size = cl_page_size(obj);
1310 LASSERT((offset & ~PAGE_MASK) == 0);
1311 LASSERT(ed->ed_next != NULL);
1312 env = cl_env_get(&refcheck);
1314 RETURN(PTR_ERR(env));
1316 info = echo_env_info(env);
1318 queue = &info->eti_queue;
1320 cl_2queue_init(queue);
1322 io->ci_ignore_layout = 1;
1323 rc = cl_io_init(env, io, CIT_MISC, obj);
1328 rc = cl_echo_enqueue0(env, eco, offset,
1329 offset + npages * PAGE_SIZE - 1,
1330 rw == READ ? LCK_PR : LCK_PW, &lh.cookie,
1333 GOTO(error_lock, rc);
1335 for (i = 0; i < npages; i++) {
1337 clp = cl_page_find(env, obj, cl_index(obj, offset),
1338 pages[i], CPT_TRANSIENT);
1343 LASSERT(clp->cp_type == CPT_TRANSIENT);
1345 rc = cl_page_own(env, io, clp);
1347 LASSERT(clp->cp_state == CPS_FREEING);
1348 cl_page_put(env, clp);
1352 cl_2queue_add(queue, clp);
1355 * drop the reference count for cl_page_find, so that the page
1356 * will be freed in cl_2queue_fini.
1358 cl_page_put(env, clp);
1359 cl_page_clip(env, clp, 0, page_size);
1361 offset += page_size;
1365 enum cl_req_type typ = rw == READ ? CRT_READ : CRT_WRITE;
1367 async = async && (typ == CRT_WRITE);
1369 rc = cl_io_commit_async(env, io, &queue->c2_qin,
1371 echo_commit_callback);
1373 rc = cl_io_submit_sync(env, io, typ, queue, 0);
1374 CDEBUG(D_INFO, "echo_client %s write returns %d\n",
1375 async ? "async" : "sync", rc);
1378 cl_echo_cancel0(env, ed, lh.cookie);
1381 cl_2queue_discard(env, io, queue);
1382 cl_2queue_disown(env, io, queue);
1383 cl_2queue_fini(env, queue);
1384 cl_io_fini(env, io);
1386 cl_env_put(env, &refcheck);
1389 /** @} echo_exports */
1391 static u64 last_object_id;
1393 #ifdef HAVE_SERVER_SUPPORT
1394 static inline void echo_md_build_name(struct lu_name *lname, char *name,
1397 snprintf(name, ETI_NAME_LEN, "%llu", id);
1398 lname->ln_name = name;
1399 lname->ln_namelen = strlen(name);
1402 /* similar to mdt_attr_get_complex */
1403 static int echo_big_lmm_get(const struct lu_env *env, struct md_object *o,
1406 struct echo_thread_info *info = echo_env_info(env);
1411 LASSERT(ma->ma_lmm_size > 0);
1413 LASSERT(ma->ma_need & (MA_LOV | MA_LMV));
1414 if (ma->ma_need & MA_LOV)
1415 rc = mo_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LOV);
1417 rc = mo_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LMV);
1422 /* big_lmm may need to be grown */
1423 if (info->eti_big_lmmsize < rc) {
1424 int size = size_roundup_power2(rc);
1426 if (info->eti_big_lmmsize > 0) {
1427 /* free old buffer */
1428 LASSERT(info->eti_big_lmm);
1429 OBD_FREE_LARGE(info->eti_big_lmm,
1430 info->eti_big_lmmsize);
1431 info->eti_big_lmm = NULL;
1432 info->eti_big_lmmsize = 0;
1435 OBD_ALLOC_LARGE(info->eti_big_lmm, size);
1436 if (!info->eti_big_lmm)
1438 info->eti_big_lmmsize = size;
1440 LASSERT(info->eti_big_lmmsize >= rc);
1442 info->eti_buf.lb_buf = info->eti_big_lmm;
1443 info->eti_buf.lb_len = info->eti_big_lmmsize;
1444 if (ma->ma_need & MA_LOV)
1445 rc = mo_xattr_get(env, o, &info->eti_buf, XATTR_NAME_LOV);
1447 rc = mo_xattr_get(env, o, &info->eti_buf, XATTR_NAME_LMV);
1451 if (ma->ma_need & MA_LOV)
1452 ma->ma_valid |= MA_LOV;
1454 ma->ma_valid |= MA_LMV;
1456 ma->ma_lmm = info->eti_big_lmm;
1457 ma->ma_lmm_size = rc;
1462 static int echo_attr_get_complex(const struct lu_env *env,
1463 struct md_object *next,
1466 struct echo_thread_info *info = echo_env_info(env);
1467 struct lu_buf *buf = &info->eti_buf;
1468 umode_t mode = lu_object_attr(&next->mo_lu);
1475 if (ma->ma_need & MA_INODE) {
1476 rc = mo_attr_get(env, next, ma);
1479 ma->ma_valid |= MA_INODE;
1482 if ((ma->ma_need & MA_LOV) && (S_ISREG(mode) || S_ISDIR(mode))) {
1483 LASSERT(ma->ma_lmm_size > 0);
1484 buf->lb_buf = ma->ma_lmm;
1485 buf->lb_len = ma->ma_lmm_size;
1486 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LOV);
1488 ma->ma_lmm_size = rc2;
1489 ma->ma_valid |= MA_LOV;
1490 } else if (rc2 == -ENODATA) {
1492 ma->ma_lmm_size = 0;
1493 } else if (rc2 == -ERANGE) {
1494 rc2 = echo_big_lmm_get(env, next, ma);
1496 GOTO(out, rc = rc2);
1498 GOTO(out, rc = rc2);
1502 if ((ma->ma_need & MA_LMV) && S_ISDIR(mode)) {
1503 LASSERT(ma->ma_lmm_size > 0);
1504 buf->lb_buf = ma->ma_lmm;
1505 buf->lb_len = ma->ma_lmm_size;
1506 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LMV);
1508 ma->ma_lmm_size = rc2;
1509 ma->ma_valid |= MA_LMV;
1510 } else if (rc2 == -ENODATA) {
1512 ma->ma_lmm_size = 0;
1513 } else if (rc2 == -ERANGE) {
1514 rc2 = echo_big_lmm_get(env, next, ma);
1516 GOTO(out, rc = rc2);
1518 GOTO(out, rc = rc2);
1522 #ifdef CONFIG_FS_POSIX_ACL
1523 if ((ma->ma_need & MA_ACL_DEF) && S_ISDIR(mode)) {
1524 buf->lb_buf = ma->ma_acl;
1525 buf->lb_len = ma->ma_acl_size;
1526 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1528 ma->ma_acl_size = rc2;
1529 ma->ma_valid |= MA_ACL_DEF;
1530 } else if (rc2 == -ENODATA) {
1532 ma->ma_acl_size = 0;
1534 GOTO(out, rc = rc2);
1539 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = %#llx ma_lmm=%p\n",
1540 rc, ma->ma_valid, ma->ma_lmm);
1545 echo_md_create_internal(const struct lu_env *env, struct echo_device *ed,
1546 struct md_object *parent, struct lu_fid *fid,
1547 struct lu_name *lname, struct md_op_spec *spec,
1550 struct lu_object *ec_child, *child;
1551 struct lu_device *ld = ed->ed_next;
1552 struct echo_thread_info *info = echo_env_info(env);
1553 struct lu_fid *fid2 = &info->eti_fid2;
1554 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
1559 rc = mdo_lookup(env, parent, lname, fid2, spec);
1562 else if (rc != -ENOENT)
1565 ec_child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev,
1567 if (IS_ERR(ec_child)) {
1568 CERROR("Can not find the child "DFID": rc = %ld\n", PFID(fid),
1570 RETURN(PTR_ERR(ec_child));
1573 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1575 CERROR("Can not locate the child "DFID"\n", PFID(fid));
1576 GOTO(out_put, rc = -EINVAL);
1579 CDEBUG(D_RPCTRACE, "Start creating object "DFID" %s %p\n",
1580 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1583 * Do not perform lookup sanity check. We know that name does not exist.
1585 spec->sp_cr_lookup = 0;
1586 rc = mdo_create(env, parent, lname, lu2md(child), spec, ma);
1588 CERROR("Can not create child "DFID": rc = %d\n", PFID(fid), rc);
1591 CDEBUG(D_RPCTRACE, "End creating object "DFID" %s %p rc = %d\n",
1592 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent, rc);
1595 lu_object_put(env, ec_child);
1599 static int echo_set_lmm_size(const struct lu_env *env, struct lu_device *ld,
1602 struct echo_thread_info *info = echo_env_info(env);
1604 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
1605 ma->ma_lmm = (void *)&info->eti_lmm;
1606 ma->ma_lmm_size = sizeof(info->eti_lmm);
1608 LASSERT(info->eti_big_lmmsize);
1609 ma->ma_lmm = info->eti_big_lmm;
1610 ma->ma_lmm_size = info->eti_big_lmmsize;
1617 echo_md_dir_stripe_choose(const struct lu_env *env, struct echo_device *ed,
1618 struct lu_object *obj, const char *name,
1619 unsigned int namelen, __u64 id,
1620 struct lu_object **new_parent)
1622 struct echo_thread_info *info = echo_env_info(env);
1623 struct md_attr *ma = &info->eti_ma;
1624 struct lmv_mds_md_v1 *lmv;
1625 struct lu_device *ld = ed->ed_next;
1627 struct lu_name tmp_ln_name;
1628 struct lu_fid stripe_fid;
1629 struct lu_object *stripe_obj;
1632 LASSERT(obj != NULL);
1633 LASSERT(S_ISDIR(obj->lo_header->loh_attr));
1635 memset(ma, 0, sizeof(*ma));
1636 echo_set_lmm_size(env, ld, ma);
1637 ma->ma_need = MA_LMV;
1638 rc = echo_attr_get_complex(env, lu2md(obj), ma);
1640 CERROR("Can not getattr child "DFID": rc = %d\n",
1641 PFID(lu_object_fid(obj)), rc);
1645 if (!(ma->ma_valid & MA_LMV)) {
1650 lmv = (struct lmv_mds_md_v1 *)ma->ma_lmm;
1651 if (le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_V1) {
1653 CERROR("Invalid mds md magic %x "DFID": rc = %d\n",
1654 le32_to_cpu(lmv->lmv_magic), PFID(lu_object_fid(obj)),
1660 tmp_ln_name.ln_name = name;
1661 tmp_ln_name.ln_namelen = namelen;
1664 echo_md_build_name(&tmp_ln_name, info->eti_name, id);
1667 idx = lmv_name_to_stripe_index(LMV_HASH_TYPE_FNV_1A_64,
1668 le32_to_cpu(lmv->lmv_stripe_count),
1669 tmp_ln_name.ln_name, tmp_ln_name.ln_namelen);
1671 LASSERT(idx < le32_to_cpu(lmv->lmv_stripe_count));
1672 fid_le_to_cpu(&stripe_fid, &lmv->lmv_stripe_fids[idx]);
1674 stripe_obj = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, &stripe_fid,
1676 if (IS_ERR(stripe_obj)) {
1677 rc = PTR_ERR(stripe_obj);
1678 CERROR("Can not find the parent "DFID": rc = %d\n",
1679 PFID(&stripe_fid), rc);
1683 *new_parent = lu_object_locate(stripe_obj->lo_header, ld->ld_type);
1685 lu_object_put(env, stripe_obj);
1692 static int echo_create_md_object(const struct lu_env *env,
1693 struct echo_device *ed,
1694 struct lu_object *ec_parent,
1696 char *name, int namelen,
1697 __u64 id, __u32 mode, int count,
1698 int stripe_count, int stripe_offset)
1700 struct lu_object *parent;
1701 struct lu_object *new_parent;
1702 struct echo_thread_info *info = echo_env_info(env);
1703 struct lu_name *lname = &info->eti_lname;
1704 struct md_op_spec *spec = &info->eti_spec;
1705 struct md_attr *ma = &info->eti_ma;
1706 struct lu_device *ld = ed->ed_next;
1714 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1718 rc = echo_md_dir_stripe_choose(env, ed, parent, name, namelen,
1723 LASSERT(new_parent != NULL);
1724 memset(ma, 0, sizeof(*ma));
1725 memset(spec, 0, sizeof(*spec));
1726 echo_set_lmm_size(env, ld, ma);
1727 if (stripe_count != 0) {
1728 spec->sp_cr_flags |= MDS_FMODE_WRITE;
1729 if (stripe_count != -1) {
1730 if (S_ISDIR(mode)) {
1731 struct lmv_user_md *lmu;
1733 lmu = (struct lmv_user_md *)&info->eti_lum;
1734 lmu->lum_magic = LMV_USER_MAGIC;
1735 lmu->lum_stripe_offset = stripe_offset;
1736 lmu->lum_stripe_count = stripe_count;
1737 lmu->lum_hash_type = LMV_HASH_TYPE_FNV_1A_64;
1738 spec->u.sp_ea.eadata = lmu;
1739 spec->u.sp_ea.eadatalen = sizeof(*lmu);
1741 struct lov_user_md_v3 *lum = &info->eti_lum;
1743 lum->lmm_magic = LOV_USER_MAGIC_V3;
1744 lum->lmm_stripe_count = stripe_count;
1745 lum->lmm_stripe_offset = stripe_offset;
1746 lum->lmm_pattern = LOV_PATTERN_NONE;
1747 spec->u.sp_ea.eadata = lum;
1748 spec->u.sp_ea.eadatalen = sizeof(*lum);
1750 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1754 ma->ma_attr.la_mode = mode;
1755 ma->ma_attr.la_valid = LA_CTIME | LA_MODE;
1756 ma->ma_attr.la_ctime = ktime_get_real_seconds();
1759 lname->ln_name = name;
1760 lname->ln_namelen = namelen;
1761 /* If name is specified, only create one object by name */
1762 rc = echo_md_create_internal(env, ed, lu2md(new_parent), fid,
1767 /* Create multiple object sequenced by id */
1768 for (i = 0; i < count; i++) {
1769 char *tmp_name = info->eti_name;
1771 echo_md_build_name(lname, tmp_name, id);
1773 rc = echo_md_create_internal(env, ed, lu2md(new_parent),
1774 fid, lname, spec, ma);
1776 CERROR("Can not create child %s: rc = %d\n", tmp_name,
1785 if (new_parent != parent)
1786 lu_object_put(env, new_parent);
1791 static struct lu_object *echo_md_lookup(const struct lu_env *env,
1792 struct echo_device *ed,
1793 struct md_object *parent,
1794 struct lu_name *lname)
1796 struct echo_thread_info *info = echo_env_info(env);
1797 struct lu_fid *fid = &info->eti_fid;
1798 struct lu_object *child;
1802 CDEBUG(D_INFO, "lookup %s in parent "DFID" %p\n", lname->ln_name,
1805 rc = mdo_lookup(env, parent, lname, fid, NULL);
1807 CERROR("lookup %s: rc = %d\n", lname->ln_name, rc);
1808 RETURN(ERR_PTR(rc));
1812 * In the function below, .hs_keycmp resolves to
1813 * lu_obj_hop_keycmp()
1815 /* coverity[overrun-buffer-val] */
1816 child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1821 static int echo_setattr_object(const struct lu_env *env,
1822 struct echo_device *ed,
1823 struct lu_object *ec_parent,
1824 __u64 id, int count)
1826 struct lu_object *parent;
1827 struct lu_object *new_parent;
1828 struct echo_thread_info *info = echo_env_info(env);
1829 struct lu_name *lname = &info->eti_lname;
1830 char *name = info->eti_name;
1831 struct lu_device *ld = ed->ed_next;
1832 struct lu_buf *buf = &info->eti_buf;
1840 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1844 rc = echo_md_dir_stripe_choose(env, ed, parent, NULL, 0, id,
1849 for (i = 0; i < count; i++) {
1850 struct lu_object *ec_child, *child;
1852 echo_md_build_name(lname, name, id);
1854 ec_child = echo_md_lookup(env, ed, lu2md(new_parent), lname);
1855 if (IS_ERR(ec_child)) {
1856 rc = PTR_ERR(ec_child);
1857 CERROR("Can't find child %s: rc = %d\n",
1858 lname->ln_name, rc);
1862 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1864 CERROR("Can not locate the child %s\n", lname->ln_name);
1865 lu_object_put(env, ec_child);
1870 CDEBUG(D_RPCTRACE, "Start setattr object "DFID"\n",
1871 PFID(lu_object_fid(child)));
1873 buf->lb_buf = info->eti_xattr_buf;
1874 buf->lb_len = sizeof(info->eti_xattr_buf);
1876 sprintf(name, "%s.test1", XATTR_USER_PREFIX);
1877 rc = mo_xattr_set(env, lu2md(child), buf, name,
1880 CERROR("Can not setattr child "DFID": rc = %d\n",
1881 PFID(lu_object_fid(child)), rc);
1882 lu_object_put(env, ec_child);
1885 CDEBUG(D_RPCTRACE, "End setattr object "DFID"\n",
1886 PFID(lu_object_fid(child)));
1888 lu_object_put(env, ec_child);
1891 if (new_parent != parent)
1892 lu_object_put(env, new_parent);
1897 static int echo_getattr_object(const struct lu_env *env,
1898 struct echo_device *ed,
1899 struct lu_object *ec_parent,
1900 __u64 id, int count)
1902 struct lu_object *parent;
1903 struct lu_object *new_parent;
1904 struct echo_thread_info *info = echo_env_info(env);
1905 struct lu_name *lname = &info->eti_lname;
1906 char *name = info->eti_name;
1907 struct md_attr *ma = &info->eti_ma;
1908 struct lu_device *ld = ed->ed_next;
1916 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1920 rc = echo_md_dir_stripe_choose(env, ed, parent, NULL, 0, id,
1925 memset(ma, 0, sizeof(*ma));
1926 ma->ma_need |= MA_INODE | MA_LOV | MA_PFID | MA_HSM | MA_ACL_DEF;
1927 ma->ma_acl = info->eti_xattr_buf;
1928 ma->ma_acl_size = sizeof(info->eti_xattr_buf);
1930 for (i = 0; i < count; i++) {
1931 struct lu_object *ec_child, *child;
1934 echo_md_build_name(lname, name, id);
1935 echo_set_lmm_size(env, ld, ma);
1937 ec_child = echo_md_lookup(env, ed, lu2md(new_parent), lname);
1938 if (IS_ERR(ec_child)) {
1939 CERROR("Can't find child %s: rc = %ld\n",
1940 lname->ln_name, PTR_ERR(ec_child));
1941 RETURN(PTR_ERR(ec_child));
1944 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1946 CERROR("Can not locate the child %s\n", lname->ln_name);
1947 lu_object_put(env, ec_child);
1951 CDEBUG(D_RPCTRACE, "Start getattr object "DFID"\n",
1952 PFID(lu_object_fid(child)));
1953 rc = echo_attr_get_complex(env, lu2md(child), ma);
1955 CERROR("Can not getattr child "DFID": rc = %d\n",
1956 PFID(lu_object_fid(child)), rc);
1957 lu_object_put(env, ec_child);
1960 CDEBUG(D_RPCTRACE, "End getattr object "DFID"\n",
1961 PFID(lu_object_fid(child)));
1963 lu_object_put(env, ec_child);
1966 if (new_parent != parent)
1967 lu_object_put(env, new_parent);
1972 static int echo_lookup_object(const struct lu_env *env,
1973 struct echo_device *ed,
1974 struct lu_object *ec_parent,
1975 __u64 id, int count)
1977 struct lu_object *parent;
1978 struct lu_object *new_parent;
1979 struct echo_thread_info *info = echo_env_info(env);
1980 struct lu_name *lname = &info->eti_lname;
1981 char *name = info->eti_name;
1982 struct lu_fid *fid = &info->eti_fid;
1983 struct lu_device *ld = ed->ed_next;
1989 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1993 rc = echo_md_dir_stripe_choose(env, ed, parent, NULL, 0, id,
1998 /*prepare the requests*/
1999 for (i = 0; i < count; i++) {
2000 echo_md_build_name(lname, name, id);
2002 CDEBUG(D_RPCTRACE, "Start lookup object "DFID" %s %p\n",
2003 PFID(lu_object_fid(new_parent)), lname->ln_name,
2006 rc = mdo_lookup(env, lu2md(new_parent), lname, fid, NULL);
2008 CERROR("Can not lookup child %s: rc = %d\n", name, rc);
2012 CDEBUG(D_RPCTRACE, "End lookup object "DFID" %s %p\n",
2013 PFID(lu_object_fid(new_parent)), lname->ln_name,
2019 if (new_parent != parent)
2020 lu_object_put(env, new_parent);
2025 static int echo_md_destroy_internal(const struct lu_env *env,
2026 struct echo_device *ed,
2027 struct md_object *parent,
2028 struct lu_name *lname,
2031 struct lu_device *ld = ed->ed_next;
2032 struct lu_object *ec_child;
2033 struct lu_object *child;
2038 ec_child = echo_md_lookup(env, ed, parent, lname);
2039 if (IS_ERR(ec_child)) {
2040 CERROR("Can't find child %s: rc = %ld\n", lname->ln_name,
2042 RETURN(PTR_ERR(ec_child));
2045 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
2047 CERROR("Can not locate the child %s\n", lname->ln_name);
2048 GOTO(out_put, rc = -EINVAL);
2051 if (lu_object_remote(child)) {
2052 CERROR("Can not destroy remote object %s: rc = %d\n",
2053 lname->ln_name, -EPERM);
2054 GOTO(out_put, rc = -EPERM);
2056 CDEBUG(D_RPCTRACE, "Start destroy object "DFID" %s %p\n",
2057 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
2059 rc = mdo_unlink(env, parent, lu2md(child), lname, ma, 0);
2061 CERROR("Can not unlink child %s: rc = %d\n",
2062 lname->ln_name, rc);
2065 CDEBUG(D_RPCTRACE, "End destroy object "DFID" %s %p\n",
2066 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
2068 lu_object_put(env, ec_child);
2072 static int echo_destroy_object(const struct lu_env *env,
2073 struct echo_device *ed,
2074 struct lu_object *ec_parent,
2075 char *name, int namelen,
2076 __u64 id, __u32 mode,
2079 struct echo_thread_info *info = echo_env_info(env);
2080 struct lu_name *lname = &info->eti_lname;
2081 struct md_attr *ma = &info->eti_ma;
2082 struct lu_device *ld = ed->ed_next;
2083 struct lu_object *parent;
2084 struct lu_object *new_parent;
2089 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
2093 rc = echo_md_dir_stripe_choose(env, ed, parent, name, namelen,
2098 memset(ma, 0, sizeof(*ma));
2099 ma->ma_attr.la_mode = mode;
2100 ma->ma_attr.la_valid = LA_CTIME;
2101 ma->ma_attr.la_ctime = ktime_get_real_seconds();
2102 ma->ma_need = MA_INODE;
2106 lname->ln_name = name;
2107 lname->ln_namelen = namelen;
2108 rc = echo_md_destroy_internal(env, ed, lu2md(new_parent), lname,
2113 /*prepare the requests*/
2114 for (i = 0; i < count; i++) {
2115 char *tmp_name = info->eti_name;
2118 echo_md_build_name(lname, tmp_name, id);
2120 rc = echo_md_destroy_internal(env, ed, lu2md(new_parent), lname,
2123 CERROR("Can not unlink child %s: rc = %d\n", name, rc);
2130 if (new_parent != parent)
2131 lu_object_put(env, new_parent);
2136 static struct lu_object *echo_resolve_path(const struct lu_env *env,
2137 struct echo_device *ed, char *path,
2140 struct lu_device *ld = ed->ed_next;
2141 struct echo_thread_info *info = echo_env_info(env);
2142 struct lu_fid *fid = &info->eti_fid;
2143 struct lu_name *lname = &info->eti_lname;
2144 struct lu_object *parent = NULL;
2145 struct lu_object *child = NULL;
2149 *fid = ed->ed_root_fid;
2152 * In the function below, .hs_keycmp resolves to
2153 * lu_obj_hop_keycmp()
2155 /* coverity[overrun-buffer-val] */
2156 parent = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
2157 if (IS_ERR(parent)) {
2158 CERROR("Can not find the parent "DFID": rc = %ld\n",
2159 PFID(fid), PTR_ERR(parent));
2164 struct lu_object *ld_parent;
2167 e = strsep(&path, "/");
2172 if (!path || path[0] == '\0')
2178 lname->ln_namelen = strlen(e);
2180 ld_parent = lu_object_locate(parent->lo_header, ld->ld_type);
2182 lu_object_put(env, parent);
2187 child = echo_md_lookup(env, ed, lu2md(ld_parent), lname);
2188 lu_object_put(env, parent);
2189 if (IS_ERR(child)) {
2190 rc = (int)PTR_ERR(child);
2191 CERROR("lookup %s under parent "DFID": rc = %d\n",
2192 lname->ln_name, PFID(lu_object_fid(ld_parent)),
2199 RETURN(ERR_PTR(rc));
2204 static void echo_ucred_init(struct lu_env *env)
2206 struct lu_ucred *ucred = lu_ucred(env);
2208 ucred->uc_valid = UCRED_INVALID;
2210 ucred->uc_suppgids[0] = -1;
2211 ucred->uc_suppgids[1] = -1;
2213 ucred->uc_uid = ucred->uc_o_uid =
2214 from_kuid(&init_user_ns, current_uid());
2215 ucred->uc_gid = ucred->uc_o_gid =
2216 from_kgid(&init_user_ns, current_gid());
2217 ucred->uc_fsuid = ucred->uc_o_fsuid =
2218 from_kuid(&init_user_ns, current_fsuid());
2219 ucred->uc_fsgid = ucred->uc_o_fsgid =
2220 from_kgid(&init_user_ns, current_fsgid());
2221 ucred->uc_cap = cfs_curproc_cap_pack();
2223 /* remove fs privilege for non-root user. */
2224 if (ucred->uc_fsuid)
2225 ucred->uc_cap &= ~CFS_CAP_FS_MASK;
2226 ucred->uc_valid = UCRED_NEW;
2229 static void echo_ucred_fini(struct lu_env *env)
2231 struct lu_ucred *ucred = lu_ucred(env);
2233 ucred->uc_valid = UCRED_INIT;
2236 #define ECHO_MD_CTX_TAG (LCT_REMEMBER | LCT_MD_THREAD)
2237 #define ECHO_MD_SES_TAG (LCT_REMEMBER | LCT_SESSION | LCT_SERVER_SESSION)
2238 static int echo_md_handler(struct echo_device *ed, int command,
2239 char *path, int path_len, __u64 id, int count,
2240 struct obd_ioctl_data *data)
2242 struct echo_thread_info *info;
2243 struct lu_device *ld = ed->ed_next;
2246 struct lu_object *parent;
2248 int namelen = data->ioc_plen2;
2253 CERROR("MD echo client is not being initialized properly\n");
2257 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
2258 CERROR("Only support MDD layer right now!\n");
2262 env = cl_env_get(&refcheck);
2264 RETURN(PTR_ERR(env));
2266 rc = lu_env_refill_by_tags(env, ECHO_MD_CTX_TAG, ECHO_MD_SES_TAG);
2270 /* init big_lmm buffer */
2271 info = echo_env_info(env);
2272 LASSERT(info->eti_big_lmm == NULL);
2273 OBD_ALLOC_LARGE(info->eti_big_lmm, MIN_MD_SIZE);
2274 if (!info->eti_big_lmm)
2275 GOTO(out_env, rc = -ENOMEM);
2276 info->eti_big_lmmsize = MIN_MD_SIZE;
2278 parent = echo_resolve_path(env, ed, path, path_len);
2279 if (IS_ERR(parent)) {
2280 CERROR("Can not resolve the path %s: rc = %ld\n", path,
2282 GOTO(out_free, rc = PTR_ERR(parent));
2286 OBD_ALLOC(name, namelen + 1);
2288 GOTO(out_put, rc = -ENOMEM);
2289 if (copy_from_user(name, data->ioc_pbuf2, namelen))
2290 GOTO(out_name, rc = -EFAULT);
2293 echo_ucred_init(env);
2296 case ECHO_MD_CREATE:
2297 case ECHO_MD_MKDIR: {
2298 struct echo_thread_info *info = echo_env_info(env);
2299 __u32 mode = data->ioc_obdo2.o_mode;
2300 struct lu_fid *fid = &info->eti_fid;
2301 int stripe_count = (int)data->ioc_obdo2.o_misc;
2302 int stripe_index = (int)data->ioc_obdo2.o_stripe_idx;
2304 rc = ostid_to_fid(fid, &data->ioc_obdo1.o_oi, 0);
2309 * In the function below, .hs_keycmp resolves to
2310 * lu_obj_hop_keycmp()
2312 /* coverity[overrun-buffer-val] */
2313 rc = echo_create_md_object(env, ed, parent, fid, name, namelen,
2314 id, mode, count, stripe_count,
2318 case ECHO_MD_DESTROY:
2319 case ECHO_MD_RMDIR: {
2320 __u32 mode = data->ioc_obdo2.o_mode;
2322 rc = echo_destroy_object(env, ed, parent, name, namelen,
2326 case ECHO_MD_LOOKUP:
2327 rc = echo_lookup_object(env, ed, parent, id, count);
2329 case ECHO_MD_GETATTR:
2330 rc = echo_getattr_object(env, ed, parent, id, count);
2332 case ECHO_MD_SETATTR:
2333 rc = echo_setattr_object(env, ed, parent, id, count);
2336 CERROR("unknown command %d\n", command);
2340 echo_ucred_fini(env);
2344 OBD_FREE(name, namelen + 1);
2346 lu_object_put(env, parent);
2348 LASSERT(info->eti_big_lmm);
2349 OBD_FREE_LARGE(info->eti_big_lmm, info->eti_big_lmmsize);
2350 info->eti_big_lmm = NULL;
2351 info->eti_big_lmmsize = 0;
2353 cl_env_put(env, &refcheck);
2356 #endif /* HAVE_SERVER_SUPPORT */
2358 static int echo_create_object(const struct lu_env *env, struct echo_device *ed,
2361 struct echo_object *eco;
2362 struct echo_client_obd *ec = ed->ed_ec;
2367 if (!(oa->o_valid & OBD_MD_FLID) ||
2368 !(oa->o_valid & OBD_MD_FLGROUP) ||
2369 !fid_seq_is_echo(ostid_seq(&oa->o_oi))) {
2370 CERROR("invalid oid "DOSTID"\n", POSTID(&oa->o_oi));
2374 if (ostid_id(&oa->o_oi) == 0) {
2375 rc = ostid_set_id(&oa->o_oi, ++last_object_id);
2380 rc = obd_create(env, ec->ec_exp, oa);
2382 CERROR("Cannot create objects: rc = %d\n", rc);
2388 oa->o_valid |= OBD_MD_FLID;
2390 eco = cl_echo_object_find(ed, &oa->o_oi);
2392 GOTO(failed, rc = PTR_ERR(eco));
2393 cl_echo_object_put(eco);
2395 CDEBUG(D_INFO, "oa oid "DOSTID"\n", POSTID(&oa->o_oi));
2399 if (created && rc != 0)
2400 obd_destroy(env, ec->ec_exp, oa);
2403 CERROR("create object failed with: rc = %d\n", rc);
2408 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
2411 struct echo_object *eco;
2415 if (!(oa->o_valid & OBD_MD_FLID) ||
2416 !(oa->o_valid & OBD_MD_FLGROUP) ||
2417 ostid_id(&oa->o_oi) == 0) {
2418 CERROR("invalid oid "DOSTID"\n", POSTID(&oa->o_oi));
2423 eco = cl_echo_object_find(ed, &oa->o_oi);
2432 static void echo_put_object(struct echo_object *eco)
2436 rc = cl_echo_object_put(eco);
2438 CERROR("%s: echo client drop an object failed: rc = %d\n",
2439 eco->eo_dev->ed_ec->ec_exp->exp_obd->obd_name, rc);
2442 static void echo_client_page_debug_setup(struct page *page, int rw, u64 id,
2443 u64 offset, u64 count)
2450 /* no partial pages on the client */
2451 LASSERT(count == PAGE_SIZE);
2455 for (delta = 0; delta < PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2456 if (rw == OBD_BRW_WRITE) {
2457 stripe_off = offset + delta;
2460 stripe_off = 0xdeadbeef00c0ffeeULL;
2461 stripe_id = 0xdeadbeef00c0ffeeULL;
2463 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
2464 stripe_off, stripe_id);
2471 echo_client_page_debug_check(struct page *page, u64 id, u64 offset, u64 count)
2480 /* no partial pages on the client */
2481 LASSERT(count == PAGE_SIZE);
2485 for (rc = delta = 0; delta < PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2486 stripe_off = offset + delta;
2489 rc2 = block_debug_check("test_brw",
2490 addr + delta, OBD_ECHO_BLOCK_SIZE,
2491 stripe_off, stripe_id);
2493 CERROR("Error in echo object %#llx\n", id);
2502 static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
2503 struct echo_object *eco, u64 offset,
2504 u64 count, int async)
2507 struct brw_page *pga;
2508 struct brw_page *pgp;
2509 struct page **pages;
2518 verify = (ostid_id(&oa->o_oi) != ECHO_PERSISTENT_OBJID &&
2519 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
2520 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
2522 gfp_mask = ((ostid_id(&oa->o_oi) & 2) == 0) ? GFP_KERNEL : GFP_HIGHUSER;
2524 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
2526 if ((count & (~PAGE_MASK)) != 0)
2529 /* XXX think again with misaligned I/O */
2530 npages = count >> PAGE_SHIFT;
2532 if (rw == OBD_BRW_WRITE)
2533 brw_flags = OBD_BRW_ASYNC;
2535 OBD_ALLOC(pga, npages * sizeof(*pga));
2539 OBD_ALLOC(pages, npages * sizeof(*pages));
2541 OBD_FREE(pga, npages * sizeof(*pga));
2545 for (i = 0, pgp = pga, off = offset;
2547 i++, pgp++, off += PAGE_SIZE) {
2549 LASSERT(pgp->pg == NULL); /* for cleanup */
2552 pgp->pg = alloc_page(gfp_mask);
2557 pgp->count = PAGE_SIZE;
2559 pgp->flag = brw_flags;
2562 echo_client_page_debug_setup(pgp->pg, rw,
2563 ostid_id(&oa->o_oi), off,
2567 /* brw mode can only be used at client */
2568 LASSERT(ed->ed_next != NULL);
2569 rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
2572 if (rc != 0 || rw != OBD_BRW_READ)
2575 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
2582 vrc = echo_client_page_debug_check(pgp->pg,
2583 ostid_id(&oa->o_oi),
2586 if (vrc != 0 && rc == 0)
2589 __free_page(pgp->pg);
2591 OBD_FREE(pga, npages * sizeof(*pga));
2592 OBD_FREE(pages, npages * sizeof(*pages));
2596 static int echo_client_prep_commit(const struct lu_env *env,
2597 struct obd_export *exp, int rw,
2598 struct obdo *oa, struct echo_object *eco,
2599 u64 offset, u64 count,
2600 u64 batch, int async)
2602 struct obd_ioobj ioo;
2603 struct niobuf_local *lnb;
2604 struct niobuf_remote rnb;
2606 u64 npages, tot_pages, apc;
2607 int i, ret = 0, brw_flags = 0;
2610 if (count <= 0 || (count & ~PAGE_MASK) != 0)
2613 apc = npages = batch >> PAGE_SHIFT;
2614 tot_pages = count >> PAGE_SHIFT;
2616 OBD_ALLOC_LARGE(lnb, apc * sizeof(struct niobuf_local));
2620 if (rw == OBD_BRW_WRITE && async)
2621 brw_flags |= OBD_BRW_ASYNC;
2623 obdo_to_ioobj(oa, &ioo);
2627 for (; tot_pages > 0; tot_pages -= npages) {
2630 if (tot_pages < npages)
2633 rnb.rnb_offset = off;
2634 rnb.rnb_len = npages * PAGE_SIZE;
2635 rnb.rnb_flags = brw_flags;
2637 off += npages * PAGE_SIZE;
2640 ret = obd_preprw(env, rw, exp, oa, 1, &ioo, &rnb, &lpages, lnb);
2644 for (i = 0; i < lpages; i++) {
2645 struct page *page = lnb[i].lnb_page;
2647 /* read past eof? */
2648 if (!page && lnb[i].lnb_rc == 0)
2652 lnb[i].lnb_flags |= OBD_BRW_ASYNC;
2654 if (ostid_id(&oa->o_oi) == ECHO_PERSISTENT_OBJID ||
2655 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
2656 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
2659 if (rw == OBD_BRW_WRITE)
2660 echo_client_page_debug_setup(page, rw,
2661 ostid_id(&oa->o_oi),
2662 lnb[i].lnb_file_offset,
2665 echo_client_page_debug_check(page,
2666 ostid_id(&oa->o_oi),
2667 lnb[i].lnb_file_offset,
2671 ret = obd_commitrw(env, rw, exp, oa, 1, &ioo, &rnb, npages, lnb,
2676 /* Reuse env context. */
2677 lu_context_exit((struct lu_context *)&env->le_ctx);
2678 lu_context_enter((struct lu_context *)&env->le_ctx);
2682 OBD_FREE_LARGE(lnb, apc * sizeof(struct niobuf_local));
2687 static int echo_client_brw_ioctl(const struct lu_env *env, int rw,
2688 struct obd_export *exp,
2689 struct obd_ioctl_data *data)
2691 struct obd_device *obd = class_exp2obd(exp);
2692 struct echo_device *ed = obd2echo_dev(obd);
2693 struct echo_client_obd *ec = ed->ed_ec;
2694 struct obdo *oa = &data->ioc_obdo1;
2695 struct echo_object *eco;
2701 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2703 rc = echo_get_object(&eco, ed, oa);
2707 oa->o_valid &= ~OBD_MD_FLHANDLE;
2709 /* OFD/obdfilter works only via prep/commit */
2710 test_mode = (long)data->ioc_pbuf1;
2711 if (!ed->ed_next && test_mode != 3) {
2713 data->ioc_plen1 = data->ioc_count;
2719 /* Truncate batch size to maximum */
2720 if (data->ioc_plen1 > PTLRPC_MAX_BRW_SIZE)
2721 data->ioc_plen1 = PTLRPC_MAX_BRW_SIZE;
2723 switch (test_mode) {
2727 rc = echo_client_kbrw(ed, rw, oa, eco, data->ioc_offset,
2728 data->ioc_count, async);
2731 rc = echo_client_prep_commit(env, ec->ec_exp, rw, oa, eco,
2732 data->ioc_offset, data->ioc_count,
2733 data->ioc_plen1, async);
2739 echo_put_object(eco);
2745 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2746 void *karg, void __user *uarg)
2748 #ifdef HAVE_SERVER_SUPPORT
2749 struct tgt_session_info *tsi;
2751 struct obd_device *obd = exp->exp_obd;
2752 struct echo_device *ed = obd2echo_dev(obd);
2753 struct echo_client_obd *ec = ed->ed_ec;
2754 struct echo_object *eco;
2755 struct obd_ioctl_data *data = karg;
2759 int rw = OBD_BRW_READ;
2761 #ifdef HAVE_SERVER_SUPPORT
2762 struct lu_context echo_session;
2766 oa = &data->ioc_obdo1;
2767 if (!(oa->o_valid & OBD_MD_FLGROUP)) {
2768 oa->o_valid |= OBD_MD_FLGROUP;
2769 ostid_set_seq_echo(&oa->o_oi);
2772 /* This FID is unpacked just for validation at this point */
2773 rc = ostid_to_fid(&fid, &oa->o_oi, 0);
2781 rc = lu_env_init(env, LCT_DT_THREAD);
2783 GOTO(out_alloc, rc = -ENOMEM);
2786 GOTO(out_env_fini, rc = -ENOMEM);
2788 #ifdef HAVE_SERVER_SUPPORT
2789 env->le_ses = &echo_session;
2790 rc = lu_context_init(env->le_ses, LCT_SERVER_SESSION | LCT_NOREF);
2791 if (unlikely(rc < 0))
2793 lu_context_enter(env->le_ses);
2795 tsi = tgt_ses_info(env);
2796 tsi->tsi_exp = ec->ec_exp;
2797 tsi->tsi_jobid = NULL;
2800 case OBD_IOC_CREATE: /* may create echo object */
2801 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2802 GOTO(out, rc = -EPERM);
2804 rc = echo_create_object(env, ed, oa);
2807 #ifdef HAVE_SERVER_SUPPORT
2808 case OBD_IOC_ECHO_MD: {
2815 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2816 GOTO(out, rc = -EPERM);
2818 count = data->ioc_count;
2819 cmd = data->ioc_command;
2821 id = data->ioc_obdo2.o_oi.oi.oi_id;
2822 dirlen = data->ioc_plen1;
2823 OBD_ALLOC(dir, dirlen + 1);
2825 GOTO(out, rc = -ENOMEM);
2827 if (copy_from_user(dir, data->ioc_pbuf1, dirlen)) {
2828 OBD_FREE(dir, data->ioc_plen1 + 1);
2829 GOTO(out, rc = -EFAULT);
2832 rc = echo_md_handler(ed, cmd, dir, dirlen, id, count, data);
2833 OBD_FREE(dir, dirlen + 1);
2836 case OBD_IOC_ECHO_ALLOC_SEQ: {
2837 struct lu_env *cl_env;
2842 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2843 GOTO(out, rc = -EPERM);
2845 cl_env = cl_env_get(&refcheck);
2847 GOTO(out, rc = PTR_ERR(cl_env));
2849 rc = lu_env_refill_by_tags(cl_env, ECHO_MD_CTX_TAG,
2852 cl_env_put(cl_env, &refcheck);
2856 rc = seq_client_get_seq(cl_env, ed->ed_cl_seq, &seq);
2857 cl_env_put(cl_env, &refcheck);
2859 CERROR("%s: Can not alloc seq: rc = %d\n",
2864 if (copy_to_user(data->ioc_pbuf1, &seq, data->ioc_plen1))
2867 max_count = LUSTRE_METADATA_SEQ_MAX_WIDTH;
2868 if (copy_to_user(data->ioc_pbuf2, &max_count,
2873 #endif /* HAVE_SERVER_SUPPORT */
2874 case OBD_IOC_DESTROY:
2875 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2876 GOTO(out, rc = -EPERM);
2878 rc = echo_get_object(&eco, ed, oa);
2880 rc = obd_destroy(env, ec->ec_exp, oa);
2882 eco->eo_deleted = 1;
2883 echo_put_object(eco);
2887 case OBD_IOC_GETATTR:
2888 rc = echo_get_object(&eco, ed, oa);
2890 rc = obd_getattr(env, ec->ec_exp, oa);
2891 echo_put_object(eco);
2895 case OBD_IOC_SETATTR:
2896 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2897 GOTO(out, rc = -EPERM);
2899 rc = echo_get_object(&eco, ed, oa);
2901 rc = obd_setattr(env, ec->ec_exp, oa);
2902 echo_put_object(eco);
2906 case OBD_IOC_BRW_WRITE:
2907 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2908 GOTO(out, rc = -EPERM);
2912 case OBD_IOC_BRW_READ:
2913 rc = echo_client_brw_ioctl(env, rw, exp, data);
2917 CERROR("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
2918 GOTO(out, rc = -ENOTTY);
2923 #ifdef HAVE_SERVER_SUPPORT
2924 lu_context_exit(env->le_ses);
2925 lu_context_fini(env->le_ses);
2937 static int echo_client_setup(const struct lu_env *env,
2938 struct obd_device *obddev, struct lustre_cfg *lcfg)
2940 struct echo_client_obd *ec = &obddev->u.echo_client;
2941 struct obd_device *tgt;
2942 struct obd_uuid echo_uuid = { "ECHO_UUID" };
2943 struct obd_connect_data *ocd = NULL;
2947 if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
2948 CERROR("requires a TARGET OBD name\n");
2952 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
2953 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
2954 CERROR("device not attached or not set up (%s)\n",
2955 lustre_cfg_string(lcfg, 1));
2959 spin_lock_init(&ec->ec_lock);
2960 INIT_LIST_HEAD(&ec->ec_objects);
2961 INIT_LIST_HEAD(&ec->ec_locks);
2964 if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
2965 #ifdef HAVE_SERVER_SUPPORT
2966 lu_context_tags_update(ECHO_MD_CTX_TAG);
2967 lu_session_tags_update(ECHO_MD_SES_TAG);
2970 "Local operations are NOT supported on client side. Only remote operations are supported. Metadata client must be run on server side.\n");
2975 OBD_ALLOC(ocd, sizeof(*ocd));
2977 CERROR("Can't alloc ocd connecting to %s\n",
2978 lustre_cfg_string(lcfg, 1));
2982 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
2983 OBD_CONNECT_BRW_SIZE |
2984 OBD_CONNECT_GRANT | OBD_CONNECT_FULL20 |
2985 OBD_CONNECT_64BITHASH | OBD_CONNECT_LVB_TYPE |
2987 ocd->ocd_brw_size = DT_MAX_BRW_SIZE;
2988 ocd->ocd_version = LUSTRE_VERSION_CODE;
2989 ocd->ocd_group = FID_SEQ_ECHO;
2991 rc = obd_connect(env, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
2993 /* Turn off pinger because it connects to tgt obd directly. */
2994 spin_lock(&tgt->obd_dev_lock);
2995 list_del_init(&ec->ec_exp->exp_obd_chain_timed);
2996 spin_unlock(&tgt->obd_dev_lock);
2999 OBD_FREE(ocd, sizeof(*ocd));
3002 CERROR("fail to connect to device %s\n",
3003 lustre_cfg_string(lcfg, 1));
3010 static int echo_client_cleanup(struct obd_device *obddev)
3012 struct echo_device *ed = obd2echo_dev(obddev);
3013 struct echo_client_obd *ec = &obddev->u.echo_client;
3017 /*Do nothing for Metadata echo client*/
3021 if (ed->ed_next_ismd) {
3022 #ifdef HAVE_SERVER_SUPPORT
3023 lu_context_tags_clear(ECHO_MD_CTX_TAG);
3024 lu_session_tags_clear(ECHO_MD_SES_TAG);
3027 "This is client-side only module, does not support metadata echo client.\n");
3032 if (!list_empty(&obddev->obd_exports)) {
3033 CERROR("still has clients!\n");
3037 LASSERT(atomic_read(&ec->ec_exp->exp_refcount) > 0);
3038 rc = obd_disconnect(ec->ec_exp);
3040 CERROR("fail to disconnect device: %d\n", rc);
3045 static int echo_client_connect(const struct lu_env *env,
3046 struct obd_export **exp,
3047 struct obd_device *src, struct obd_uuid *cluuid,
3048 struct obd_connect_data *data, void *localdata)
3051 struct lustre_handle conn = { 0 };
3054 rc = class_connect(&conn, src, cluuid);
3056 *exp = class_conn2export(&conn);
3061 static int echo_client_disconnect(struct obd_export *exp)
3067 GOTO(out, rc = -EINVAL);
3069 rc = class_disconnect(exp);
3075 static struct obd_ops echo_client_obd_ops = {
3076 .o_owner = THIS_MODULE,
3077 .o_iocontrol = echo_client_iocontrol,
3078 .o_connect = echo_client_connect,
3079 .o_disconnect = echo_client_disconnect
3082 static int __init obdecho_init(void)
3087 LCONSOLE_INFO("Echo OBD driver; http://www.lustre.org/\n");
3089 LASSERT(PAGE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
3091 # ifdef HAVE_SERVER_SUPPORT
3092 rc = echo_persistent_pages_init();
3096 rc = class_register_type(&echo_obd_ops, NULL, true, NULL,
3097 LUSTRE_ECHO_NAME, &echo_srv_type);
3102 rc = lu_kmem_init(echo_caches);
3104 rc = class_register_type(&echo_client_obd_ops, NULL, false,
3105 NULL, LUSTRE_ECHO_CLIENT_NAME,
3108 lu_kmem_fini(echo_caches);
3111 # ifdef HAVE_SERVER_SUPPORT
3115 class_unregister_type(LUSTRE_ECHO_NAME);
3117 echo_persistent_pages_fini();
3123 static void __exit obdecho_exit(void)
3125 class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
3126 lu_kmem_fini(echo_caches);
3128 #ifdef HAVE_SERVER_SUPPORT
3129 class_unregister_type(LUSTRE_ECHO_NAME);
3130 echo_persistent_pages_fini();
3134 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
3135 MODULE_DESCRIPTION("Lustre Echo Client test driver");
3136 MODULE_VERSION(LUSTRE_VERSION_STRING);
3137 MODULE_LICENSE("GPL");
3139 module_init(obdecho_init);
3140 module_exit(obdecho_exit);
3142 /** @} echo_client */