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/
32 #define DEBUG_SUBSYSTEM S_ECHO
34 #include <linux/user_namespace.h>
35 #include <linux/uidgid.h>
37 #include <libcfs/libcfs.h>
39 #include <obd_support.h>
40 #include <obd_class.h>
41 #include <lprocfs_status.h>
42 #include <cl_object.h>
43 #include <lustre_fid.h>
44 #include <lustre_lmv.h>
45 #include <lustre_acl.h>
46 #include <uapi/linux/lustre/lustre_ioctl.h>
47 #include <lustre_net.h>
48 #ifdef HAVE_SERVER_SUPPORT
49 # include <md_object.h>
51 #define ETI_NAME_LEN 20
53 #endif /* HAVE_SERVER_SUPPORT */
55 #include "echo_internal.h"
57 /** \defgroup echo_client Echo Client
61 /* echo thread key have a CL_THREAD flag, which set cl_env function directly */
62 #define ECHO_MD_CTX_TAG (LCT_REMEMBER | LCT_MD_THREAD)
63 #define ECHO_DT_CTX_TAG (LCT_REMEMBER | LCT_DT_THREAD)
64 #define ECHO_SES_TAG (LCT_REMEMBER | LCT_SESSION | LCT_SERVER_SESSION)
67 struct cl_device ed_cl;
68 struct echo_client_obd *ed_ec;
70 struct cl_site ed_site_myself;
71 struct lu_site *ed_site;
72 struct lu_device *ed_next;
74 struct lu_client_seq *ed_cl_seq;
75 #ifdef HAVE_SERVER_SUPPORT
76 struct local_oid_storage *ed_los;
77 struct lu_fid ed_root_fid;
78 #endif /* HAVE_SERVER_SUPPORT */
82 struct cl_object eo_cl;
83 struct cl_object_header eo_hdr;
84 struct echo_device *eo_dev;
85 struct list_head eo_obj_chain;
86 struct lov_oinfo *eo_oinfo;
91 struct echo_object_conf {
92 struct cl_object_conf eoc_cl;
93 struct lov_oinfo **eoc_oinfo;
97 struct cl_page_slice ep_cl;
98 unsigned long ep_lock;
102 struct cl_lock_slice el_cl;
103 struct list_head el_chain;
104 struct echo_object *el_object;
106 atomic_t el_refcount;
109 #ifdef HAVE_SERVER_SUPPORT
110 static const char echo_md_root_dir_name[] = "ROOT_ECHO";
113 * In order to use the values of members in struct mdd_device,
114 * we define an alias structure here.
116 struct echo_md_device {
117 struct md_device emd_md_dev;
118 struct obd_export *emd_child_exp;
119 struct dt_device *emd_child;
120 struct dt_device *emd_bottom;
121 struct lu_fid emd_root_fid;
122 struct lu_fid emd_local_root_fid;
124 #endif /* HAVE_SERVER_SUPPORT */
126 static int echo_client_setup(const struct lu_env *env,
127 struct obd_device *obd,
128 struct lustre_cfg *lcfg);
129 static int echo_client_cleanup(struct obd_device *obd);
131 /** \defgroup echo_helpers Helper functions
134 static inline struct echo_device *cl2echo_dev(const struct cl_device *dev)
136 return container_of_safe(dev, struct echo_device, ed_cl);
139 static inline struct cl_device *echo_dev2cl(struct echo_device *d)
144 static inline struct echo_device *obd2echo_dev(const struct obd_device *obd)
146 return cl2echo_dev(lu2cl_dev(obd->obd_lu_dev));
149 static inline struct cl_object *echo_obj2cl(struct echo_object *eco)
154 static inline struct echo_object *cl2echo_obj(const struct cl_object *o)
156 return container_of(o, struct echo_object, eo_cl);
159 static inline struct echo_page *cl2echo_page(const struct cl_page_slice *s)
161 return container_of(s, struct echo_page, ep_cl);
164 static inline struct echo_lock *cl2echo_lock(const struct cl_lock_slice *s)
166 return container_of(s, struct echo_lock, el_cl);
169 static inline struct cl_lock *echo_lock2cl(const struct echo_lock *ecl)
171 return ecl->el_cl.cls_lock;
174 static struct lu_context_key echo_thread_key;
176 static inline struct echo_thread_info *echo_env_info(const struct lu_env *env)
178 struct echo_thread_info *info;
180 info = lu_context_key_get(&env->le_ctx, &echo_thread_key);
181 LASSERT(info != NULL);
186 struct echo_object_conf *cl2echo_conf(const struct cl_object_conf *c)
188 return container_of(c, struct echo_object_conf, eoc_cl);
191 #ifdef HAVE_SERVER_SUPPORT
192 static inline struct echo_md_device *lu2emd_dev(struct lu_device *d)
194 return container_of_safe(d, struct echo_md_device,
195 emd_md_dev.md_lu_dev);
198 static inline struct lu_device *emd2lu_dev(struct echo_md_device *d)
200 return &d->emd_md_dev.md_lu_dev;
203 static inline struct seq_server_site *echo_md_seq_site(struct echo_md_device *d)
205 return emd2lu_dev(d)->ld_site->ld_seq_site;
208 static inline struct obd_device *emd2obd_dev(struct echo_md_device *d)
210 return d->emd_md_dev.md_lu_dev.ld_obd;
212 #endif /* HAVE_SERVER_SUPPORT */
214 /** @} echo_helpers */
216 static int cl_echo_object_put(struct echo_object *eco);
217 static int cl_echo_object_brw(struct echo_object *eco, int rw, u64 offset,
218 struct page **pages, int npages, int async);
220 struct echo_thread_info {
221 struct echo_object_conf eti_conf;
222 struct lustre_md eti_md;
223 struct cl_2queue eti_queue;
225 struct cl_lock eti_lock;
226 struct lu_fid eti_fid;
227 struct lu_fid eti_fid2;
228 #ifdef HAVE_SERVER_SUPPORT
229 struct md_op_spec eti_spec;
230 struct lov_mds_md_v3 eti_lmm;
231 struct lov_user_md_v3 eti_lum;
232 struct md_attr eti_ma;
233 struct lu_name eti_lname;
234 /* per-thread values, can be re-used */
235 void *eti_big_lmm; /* may be vmalloc'd */
237 char eti_name[ETI_NAME_LEN];
238 struct lu_buf eti_buf;
239 /* If we want to test large ACL, then need to enlarge the buffer. */
240 char eti_xattr_buf[LUSTRE_POSIX_ACL_MAX_SIZE_OLD];
244 /* No session used right now */
245 struct echo_session_info {
249 static struct kmem_cache *echo_lock_kmem;
250 static struct kmem_cache *echo_object_kmem;
251 static struct kmem_cache *echo_thread_kmem;
252 static struct kmem_cache *echo_session_kmem;
253 /* static struct kmem_cache *echo_req_kmem; */
255 static struct lu_kmem_descr echo_caches[] = {
257 .ckd_cache = &echo_lock_kmem,
258 .ckd_name = "echo_lock_kmem",
259 .ckd_size = sizeof(struct echo_lock)
262 .ckd_cache = &echo_object_kmem,
263 .ckd_name = "echo_object_kmem",
264 .ckd_size = sizeof(struct echo_object)
267 .ckd_cache = &echo_thread_kmem,
268 .ckd_name = "echo_thread_kmem",
269 .ckd_size = sizeof(struct echo_thread_info)
272 .ckd_cache = &echo_session_kmem,
273 .ckd_name = "echo_session_kmem",
274 .ckd_size = sizeof(struct echo_session_info)
281 /** \defgroup echo_page Page operations
283 * Echo page operations.
287 static int echo_page_own(const struct lu_env *env,
288 const struct cl_page_slice *slice,
289 struct cl_io *io, int nonblock)
291 struct echo_page *ep = cl2echo_page(slice);
294 if (test_and_set_bit(0, &ep->ep_lock))
297 while (test_and_set_bit(0, &ep->ep_lock))
298 wait_on_bit(&ep->ep_lock, 0, TASK_UNINTERRUPTIBLE);
303 static void echo_page_disown(const struct lu_env *env,
304 const struct cl_page_slice *slice,
307 struct echo_page *ep = cl2echo_page(slice);
309 LASSERT(test_bit(0, &ep->ep_lock));
310 clear_and_wake_up_bit(0, &ep->ep_lock);
313 static void echo_page_discard(const struct lu_env *env,
314 const struct cl_page_slice *slice,
315 struct cl_io *unused)
317 cl_page_delete(env, slice->cpl_page);
320 static int echo_page_is_vmlocked(const struct lu_env *env,
321 const struct cl_page_slice *slice)
323 if (test_bit(0, &cl2echo_page(slice)->ep_lock))
328 static void echo_page_completion(const struct lu_env *env,
329 const struct cl_page_slice *slice,
332 LASSERT(slice->cpl_page->cp_sync_io != NULL);
335 static void echo_page_fini(const struct lu_env *env,
336 struct cl_page_slice *slice,
337 struct pagevec *pvec)
339 struct echo_object *eco = cl2echo_obj(slice->cpl_obj);
342 atomic_dec(&eco->eo_npages);
343 put_page(slice->cpl_page->cp_vmpage);
347 static int echo_page_prep(const struct lu_env *env,
348 const struct cl_page_slice *slice,
349 struct cl_io *unused)
354 static int echo_page_print(const struct lu_env *env,
355 const struct cl_page_slice *slice,
356 void *cookie, lu_printer_t printer)
358 struct echo_page *ep = cl2echo_page(slice);
360 (*printer)(env, cookie, LUSTRE_ECHO_CLIENT_NAME"-page@%p %d vm@%p\n",
361 ep, test_bit(0, &ep->ep_lock),
362 slice->cpl_page->cp_vmpage);
366 static const struct cl_page_operations echo_page_ops = {
367 .cpo_own = echo_page_own,
368 .cpo_disown = echo_page_disown,
369 .cpo_discard = echo_page_discard,
370 .cpo_fini = echo_page_fini,
371 .cpo_print = echo_page_print,
372 .cpo_is_vmlocked = echo_page_is_vmlocked,
375 .cpo_prep = echo_page_prep,
376 .cpo_completion = echo_page_completion,
379 .cpo_prep = echo_page_prep,
380 .cpo_completion = echo_page_completion,
387 /** \defgroup echo_lock Locking
389 * echo lock operations
393 static void echo_lock_fini(const struct lu_env *env,
394 struct cl_lock_slice *slice)
396 struct echo_lock *ecl = cl2echo_lock(slice);
398 LASSERT(list_empty(&ecl->el_chain));
399 OBD_SLAB_FREE_PTR(ecl, echo_lock_kmem);
402 static const struct cl_lock_operations echo_lock_ops = {
403 .clo_fini = echo_lock_fini,
408 /** \defgroup echo_cl_ops cl_object operations
410 * operations for cl_object
414 static int echo_page_init(const struct lu_env *env, struct cl_object *obj,
415 struct cl_page *page, pgoff_t index)
417 struct echo_page *ep = cl_object_page_slice(obj, page);
418 struct echo_object *eco = cl2echo_obj(obj);
421 get_page(page->cp_vmpage);
423 * ep_lock is similar to the lock_page() lock, and
424 * cannot usefully be monitored by lockdep.
425 * So just use a bit in an "unsigned long" and use the
426 * wait_on_bit() interface to wait for the bit to be clear.
429 cl_page_slice_add(page, &ep->ep_cl, obj, &echo_page_ops);
430 atomic_inc(&eco->eo_npages);
434 static int echo_io_init(const struct lu_env *env, struct cl_object *obj,
440 static int echo_lock_init(const struct lu_env *env,
441 struct cl_object *obj, struct cl_lock *lock,
442 const struct cl_io *unused)
444 struct echo_lock *el;
447 OBD_SLAB_ALLOC_PTR_GFP(el, echo_lock_kmem, GFP_NOFS);
449 cl_lock_slice_add(lock, &el->el_cl, obj, &echo_lock_ops);
450 el->el_object = cl2echo_obj(obj);
451 INIT_LIST_HEAD(&el->el_chain);
452 atomic_set(&el->el_refcount, 0);
454 RETURN(el ? 0 : -ENOMEM);
457 static int echo_conf_set(const struct lu_env *env, struct cl_object *obj,
458 const struct cl_object_conf *conf)
463 static const struct cl_object_operations echo_cl_obj_ops = {
464 .coo_page_init = echo_page_init,
465 .coo_lock_init = echo_lock_init,
466 .coo_io_init = echo_io_init,
467 .coo_conf_set = echo_conf_set
469 /** @} echo_cl_ops */
471 /** \defgroup echo_lu_ops lu_object operations
473 * operations for echo lu object.
477 static int echo_object_init(const struct lu_env *env, struct lu_object *obj,
478 const struct lu_object_conf *conf)
480 struct echo_device *ed = cl2echo_dev(lu2cl_dev(obj->lo_dev));
481 struct echo_client_obd *ec = ed->ed_ec;
482 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
486 struct lu_object *below;
487 struct lu_device *under;
490 below = under->ld_ops->ldo_object_alloc(env, obj->lo_header,
494 lu_object_add(obj, below);
497 if (!ed->ed_next_ismd) {
498 const struct cl_object_conf *cconf = lu2cl_conf(conf);
499 struct echo_object_conf *econf = cl2echo_conf(cconf);
501 LASSERT(econf->eoc_oinfo != NULL);
504 * Transfer the oinfo pointer to eco that it won't be
507 eco->eo_oinfo = *econf->eoc_oinfo;
508 *econf->eoc_oinfo = NULL;
510 eco->eo_oinfo = NULL;
514 atomic_set(&eco->eo_npages, 0);
515 cl_object_page_init(lu2cl(obj), sizeof(struct echo_page));
517 spin_lock(&ec->ec_lock);
518 list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
519 spin_unlock(&ec->ec_lock);
524 static void echo_object_delete(const struct lu_env *env, struct lu_object *obj)
526 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
527 struct echo_client_obd *ec;
531 /* object delete called unconditolally - layer init or not */
532 if (eco->eo_dev == NULL)
535 ec = eco->eo_dev->ed_ec;
537 LASSERT(atomic_read(&eco->eo_npages) == 0);
539 spin_lock(&ec->ec_lock);
540 list_del_init(&eco->eo_obj_chain);
541 spin_unlock(&ec->ec_lock);
544 OBD_FREE_PTR(eco->eo_oinfo);
547 static void echo_object_free_rcu(struct rcu_head *head)
549 struct echo_object *eco = container_of(head, struct echo_object,
550 eo_hdr.coh_lu.loh_rcu);
552 kmem_cache_free(echo_object_kmem, eco);
555 static void echo_object_free(const struct lu_env *env, struct lu_object *obj)
557 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
562 lu_object_header_fini(obj->lo_header);
564 OBD_FREE_PRE(eco, sizeof(*eco), "slab-freed");
565 call_rcu(&eco->eo_hdr.coh_lu.loh_rcu, echo_object_free_rcu);
569 static int echo_object_print(const struct lu_env *env, void *cookie,
570 lu_printer_t p, const struct lu_object *o)
572 struct echo_object *obj = cl2echo_obj(lu2cl(o));
574 return (*p)(env, cookie, "echoclient-object@%p", obj);
577 static const struct lu_object_operations echo_lu_obj_ops = {
578 .loo_object_init = echo_object_init,
579 .loo_object_delete = echo_object_delete,
580 .loo_object_release = NULL,
581 .loo_object_free = echo_object_free,
582 .loo_object_print = echo_object_print,
583 .loo_object_invariant = NULL
585 /** @} echo_lu_ops */
587 /** \defgroup echo_lu_dev_ops lu_device operations
589 * Operations for echo lu device.
593 static struct lu_object *echo_object_alloc(const struct lu_env *env,
594 const struct lu_object_header *hdr,
595 struct lu_device *dev)
597 struct echo_object *eco;
598 struct lu_object *obj = NULL;
601 /* we're the top dev. */
602 LASSERT(hdr == NULL);
603 OBD_SLAB_ALLOC_PTR_GFP(eco, echo_object_kmem, GFP_NOFS);
605 struct cl_object_header *hdr = &eco->eo_hdr;
607 obj = &echo_obj2cl(eco)->co_lu;
608 cl_object_header_init(hdr);
609 hdr->coh_page_bufsize = cfs_size_round(sizeof(struct cl_page));
611 lu_object_init(obj, &hdr->coh_lu, dev);
612 lu_object_add_top(&hdr->coh_lu, obj);
614 eco->eo_cl.co_ops = &echo_cl_obj_ops;
615 obj->lo_ops = &echo_lu_obj_ops;
620 static const struct lu_device_operations echo_device_lu_ops = {
621 .ldo_object_alloc = echo_object_alloc,
624 /** @} echo_lu_dev_ops */
626 /** \defgroup echo_init Setup and teardown
628 * Init and fini functions for echo client.
632 static int echo_site_init(const struct lu_env *env, struct echo_device *ed)
634 struct cl_site *site = &ed->ed_site_myself;
637 /* initialize site */
638 rc = cl_site_init(site, &ed->ed_cl);
640 CERROR("Cannot initialize site for echo client(%d)\n", rc);
644 rc = lu_site_init_finish(&site->cs_lu);
650 ed->ed_site = &site->cs_lu;
654 static void echo_site_fini(const struct lu_env *env, struct echo_device *ed)
657 if (!ed->ed_next_ismd)
658 lu_site_fini(ed->ed_site);
663 static void *echo_thread_key_init(const struct lu_context *ctx,
664 struct lu_context_key *key)
666 struct echo_thread_info *info;
668 OBD_SLAB_ALLOC_PTR_GFP(info, echo_thread_kmem, GFP_NOFS);
670 info = ERR_PTR(-ENOMEM);
674 static void echo_thread_key_fini(const struct lu_context *ctx,
675 struct lu_context_key *key, void *data)
677 struct echo_thread_info *info = data;
679 OBD_SLAB_FREE_PTR(info, echo_thread_kmem);
682 static struct lu_context_key echo_thread_key = {
683 .lct_tags = LCT_CL_THREAD,
684 .lct_init = echo_thread_key_init,
685 .lct_fini = echo_thread_key_fini,
688 static void *echo_session_key_init(const struct lu_context *ctx,
689 struct lu_context_key *key)
691 struct echo_session_info *session;
693 OBD_SLAB_ALLOC_PTR_GFP(session, echo_session_kmem, GFP_NOFS);
695 session = ERR_PTR(-ENOMEM);
699 static void echo_session_key_fini(const struct lu_context *ctx,
700 struct lu_context_key *key, void *data)
702 struct echo_session_info *session = data;
704 OBD_SLAB_FREE_PTR(session, echo_session_kmem);
707 static struct lu_context_key echo_session_key = {
708 .lct_tags = LCT_SESSION,
709 .lct_init = echo_session_key_init,
710 .lct_fini = echo_session_key_fini,
713 LU_TYPE_INIT_FINI(echo, &echo_thread_key, &echo_session_key);
715 #ifdef HAVE_SERVER_SUPPORT
716 # define ECHO_SEQ_WIDTH 0xffffffff
717 static int echo_fid_init(struct echo_device *ed, char *obd_name,
718 struct seq_server_site *ss)
724 OBD_ALLOC_PTR(ed->ed_cl_seq);
728 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
730 GOTO(out_free_seq, rc = -ENOMEM);
732 snprintf(prefix, MAX_OBD_NAME + 5, "srv-%s", obd_name);
734 /* Init client side sequence-manager */
735 seq_client_init(ed->ed_cl_seq, NULL,
737 prefix, ss->ss_server_seq);
738 ed->ed_cl_seq->lcs_width = ECHO_SEQ_WIDTH;
739 OBD_FREE(prefix, MAX_OBD_NAME + 5);
744 OBD_FREE_PTR(ed->ed_cl_seq);
745 ed->ed_cl_seq = NULL;
749 static int echo_fid_fini(struct obd_device *obd)
751 struct echo_device *ed = obd2echo_dev(obd);
755 seq_client_fini(ed->ed_cl_seq);
756 OBD_FREE_PTR(ed->ed_cl_seq);
757 ed->ed_cl_seq = NULL;
763 static void echo_ed_los_fini(const struct lu_env *env, struct echo_device *ed)
766 if (ed != NULL && ed->ed_next_ismd && ed->ed_los != NULL) {
767 local_oid_storage_fini(env, ed->ed_los);
773 echo_md_local_file_create(const struct lu_env *env, struct echo_md_device *emd,
774 struct local_oid_storage *los,
775 const struct lu_fid *pfid, const char *name,
776 __u32 mode, struct lu_fid *fid)
778 struct dt_object *parent = NULL;
779 struct dt_object *dto = NULL;
783 LASSERT(!fid_is_zero(pfid));
784 parent = dt_locate(env, emd->emd_bottom, pfid);
785 if (unlikely(IS_ERR(parent)))
786 RETURN(PTR_ERR(parent));
788 /* create local file with @fid */
789 dto = local_file_find_or_create_with_fid(env, emd->emd_bottom, fid,
792 GOTO(out_put, rc = PTR_ERR(dto));
794 *fid = *lu_object_fid(&dto->do_lu);
796 * since stack is not fully set up the local_storage uses own stack
797 * and we should drop its object from cache
799 dt_object_put_nocache(env, dto);
803 dt_object_put(env, parent);
808 echo_md_root_get(const struct lu_env *env, struct echo_md_device *emd,
809 struct echo_device *ed)
815 /* Setup local dirs */
816 fid.f_seq = FID_SEQ_LOCAL_NAME;
819 rc = local_oid_storage_init(env, emd->emd_bottom, &fid, &ed->ed_los);
823 lu_echo_root_fid(&fid);
824 if (echo_md_seq_site(emd)->ss_node_id == 0) {
825 rc = echo_md_local_file_create(env, emd, ed->ed_los,
826 &emd->emd_local_root_fid,
827 echo_md_root_dir_name, S_IFDIR |
828 S_IRUGO | S_IWUSR | S_IXUGO,
831 CERROR("%s: create md echo root fid failed: rc = %d\n",
832 emd2obd_dev(emd)->obd_name, rc);
836 ed->ed_root_fid = fid;
840 echo_ed_los_fini(env, ed);
844 #endif /* HAVE_SERVER_SUPPORT */
846 static struct lu_device *echo_device_alloc(const struct lu_env *env,
847 struct lu_device_type *t,
848 struct lustre_cfg *cfg)
850 struct lu_device *next;
851 struct echo_device *ed;
852 struct cl_device *cd;
853 struct obd_device *obd = NULL; /* to keep compiler happy */
854 struct obd_device *tgt;
855 const char *tgt_type_name;
862 GOTO(out, rc = -ENOMEM);
866 rc = cl_device_init(cd, t);
870 cd->cd_lu_dev.ld_ops = &echo_device_lu_ops;
873 obd = class_name2obd(lustre_cfg_string(cfg, 0));
874 LASSERT(obd != NULL);
875 LASSERT(env != NULL);
877 tgt = class_name2obd(lustre_cfg_string(cfg, 1));
879 CERROR("Can not find tgt device %s\n",
880 lustre_cfg_string(cfg, 1));
881 GOTO(out, rc = -ENODEV);
884 next = tgt->obd_lu_dev;
886 if (strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME) == 0) {
887 ed->ed_next_ismd = 1;
888 } else if (strcmp(tgt->obd_type->typ_name, LUSTRE_OST_NAME) == 0 ||
889 strcmp(tgt->obd_type->typ_name, LUSTRE_OSC_NAME) == 0) {
890 ed->ed_next_ismd = 0;
891 rc = echo_site_init(env, ed);
895 GOTO(out, rc = -EINVAL);
900 rc = echo_client_setup(env, obd, cfg);
904 ed->ed_ec = &obd->u.echo_client;
907 if (ed->ed_next_ismd) {
908 #ifdef HAVE_SERVER_SUPPORT
909 /* Suppose to connect to some Metadata layer */
910 struct lu_site *ls = NULL;
911 struct lu_device *ld = NULL;
912 struct md_device *md = NULL;
913 struct echo_md_device *emd = NULL;
917 CERROR("%s is not lu device type!\n",
918 lustre_cfg_string(cfg, 1));
919 GOTO(out, rc = -EINVAL);
922 tgt_type_name = lustre_cfg_string(cfg, 2);
923 if (!tgt_type_name) {
924 CERROR("%s no type name for echo %s setup\n",
925 lustre_cfg_string(cfg, 1),
926 tgt->obd_type->typ_name);
927 GOTO(out, rc = -EINVAL);
932 spin_lock(&ls->ls_ld_lock);
933 list_for_each_entry(ld, &ls->ls_ld_linkage, ld_linkage) {
934 if (strcmp(ld->ld_type->ldt_name, tgt_type_name) == 0) {
939 spin_unlock(&ls->ls_ld_lock);
942 CERROR("%s is not lu device type!\n",
943 lustre_cfg_string(cfg, 1));
944 GOTO(out, rc = -EINVAL);
948 /* For MD echo client, it will use the site in MDS stack */
950 ed->ed_cl.cd_lu_dev.ld_site = ls;
951 rc = echo_fid_init(ed, obd->obd_name, lu_site2seq(ls));
953 CERROR("echo fid init error %d\n", rc);
957 md = lu2md_dev(next);
958 emd = lu2emd_dev(&md->md_lu_dev);
959 rc = echo_md_root_get(env, emd, ed);
961 CERROR("%s: get root error: rc = %d\n",
962 emd2obd_dev(emd)->obd_name, rc);
965 #else /* !HAVE_SERVER_SUPPORT */
967 "Local operations are NOT supported on client side. Only remote operations are supported. Metadata client must be run on server side.\n");
968 GOTO(out, rc = -EOPNOTSUPP);
969 #endif /* HAVE_SERVER_SUPPORT */
972 * if echo client is to be stacked upon ost device, the next is
973 * NULL since ost is not a clio device so far
975 if (next != NULL && !lu_device_is_cl(next))
978 tgt_type_name = tgt->obd_type->typ_name;
980 LASSERT(next != NULL);
982 GOTO(out, rc = -EBUSY);
984 next->ld_site = ed->ed_site;
985 rc = next->ld_type->ldt_ops->ldto_device_init(env, next,
986 next->ld_type->ldt_name,
991 LASSERT(strcmp(tgt_type_name, LUSTRE_OST_NAME) == 0);
996 RETURN(&cd->cd_lu_dev);
1002 rc2 = echo_client_cleanup(obd);
1004 CERROR("Cleanup obd device %s error(%d)\n",
1005 obd->obd_name, rc2);
1010 echo_site_fini(env, ed);
1013 cl_device_fini(&ed->ed_cl);
1025 static int echo_device_init(const struct lu_env *env, struct lu_device *d,
1026 const char *name, struct lu_device *next)
1032 static struct lu_device *echo_device_fini(const struct lu_env *env,
1033 struct lu_device *d)
1035 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
1036 struct lu_device *next = ed->ed_next;
1038 while (next && !ed->ed_next_ismd)
1039 next = next->ld_type->ldt_ops->ldto_device_fini(env, next);
1043 static void echo_lock_release(const struct lu_env *env,
1044 struct echo_lock *ecl,
1047 struct cl_lock *clk = echo_lock2cl(ecl);
1049 cl_lock_release(env, clk);
1052 static struct lu_device *echo_device_free(const struct lu_env *env,
1053 struct lu_device *d)
1055 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
1056 struct echo_client_obd *ec = ed->ed_ec;
1057 struct echo_object *eco;
1058 struct lu_device *next = ed->ed_next;
1060 CDEBUG(D_INFO, "echo device:%p is going to be freed, next = %p\n",
1063 lu_site_purge(env, ed->ed_site, -1);
1066 * check if there are objects still alive.
1067 * It shouldn't have any object because lu_site_purge would cleanup
1068 * all of cached objects. Anyway, probably the echo device is being
1069 * parallelly accessed.
1071 spin_lock(&ec->ec_lock);
1072 list_for_each_entry(eco, &ec->ec_objects, eo_obj_chain)
1073 eco->eo_deleted = 1;
1074 spin_unlock(&ec->ec_lock);
1077 lu_site_purge(env, ed->ed_site, -1);
1080 "Waiting for the reference of echo object to be dropped\n");
1082 /* Wait for the last reference to be dropped. */
1083 spin_lock(&ec->ec_lock);
1084 while (!list_empty(&ec->ec_objects)) {
1085 spin_unlock(&ec->ec_lock);
1087 "echo_client still has objects at cleanup time, wait for 1 second\n");
1088 schedule_timeout_uninterruptible(cfs_time_seconds(1));
1089 lu_site_purge(env, ed->ed_site, -1);
1090 spin_lock(&ec->ec_lock);
1092 spin_unlock(&ec->ec_lock);
1094 LASSERT(list_empty(&ec->ec_locks));
1096 CDEBUG(D_INFO, "No object exists, exiting...\n");
1098 echo_client_cleanup(d->ld_obd);
1099 #ifdef HAVE_SERVER_SUPPORT
1100 echo_fid_fini(d->ld_obd);
1101 echo_ed_los_fini(env, ed);
1103 while (next && !ed->ed_next_ismd)
1104 next = next->ld_type->ldt_ops->ldto_device_free(env, next);
1106 LASSERT(ed->ed_site == d->ld_site);
1107 echo_site_fini(env, ed);
1108 cl_device_fini(&ed->ed_cl);
1111 cl_env_cache_purge(~0);
1116 static const struct lu_device_type_operations echo_device_type_ops = {
1117 .ldto_init = echo_type_init,
1118 .ldto_fini = echo_type_fini,
1120 .ldto_start = echo_type_start,
1121 .ldto_stop = echo_type_stop,
1123 .ldto_device_alloc = echo_device_alloc,
1124 .ldto_device_free = echo_device_free,
1125 .ldto_device_init = echo_device_init,
1126 .ldto_device_fini = echo_device_fini
1129 static struct lu_device_type echo_device_type = {
1130 .ldt_tags = LU_DEVICE_CL,
1131 .ldt_name = LUSTRE_ECHO_CLIENT_NAME,
1132 .ldt_ops = &echo_device_type_ops,
1133 .ldt_ctx_tags = LCT_CL_THREAD | LCT_MD_THREAD | LCT_DT_THREAD,
1137 /** \defgroup echo_exports Exported operations
1139 * exporting functions to echo client
1144 /* Interfaces to echo client obd device */
1145 static struct echo_object *
1146 cl_echo_object_find(struct echo_device *d, const struct ost_id *oi)
1149 struct echo_thread_info *info;
1150 struct echo_object_conf *conf;
1151 struct echo_object *eco;
1152 struct cl_object *obj;
1153 struct lov_oinfo *oinfo = NULL;
1159 LASSERTF(ostid_id(oi) != 0, DOSTID"\n", POSTID(oi));
1160 LASSERTF(ostid_seq(oi) == FID_SEQ_ECHO, DOSTID"\n", POSTID(oi));
1162 /* Never return an object if the obd is to be freed. */
1163 if (echo_dev2cl(d)->cd_lu_dev.ld_obd->obd_stopping)
1164 RETURN(ERR_PTR(-ENODEV));
1166 env = cl_env_get(&refcheck);
1168 RETURN((void *)env);
1170 info = echo_env_info(env);
1171 conf = &info->eti_conf;
1173 OBD_ALLOC_PTR(oinfo);
1175 GOTO(out, eco = ERR_PTR(-ENOMEM));
1177 oinfo->loi_oi = *oi;
1178 conf->eoc_cl.u.coc_oinfo = oinfo;
1182 * If echo_object_init() is successful then ownership of oinfo
1183 * is transferred to the object.
1185 conf->eoc_oinfo = &oinfo;
1187 fid = &info->eti_fid;
1188 rc = ostid_to_fid(fid, oi, 0);
1190 GOTO(out, eco = ERR_PTR(rc));
1193 * In the function below, .hs_keycmp resolves to
1194 * lu_obj_hop_keycmp()
1196 /* coverity[overrun-buffer-val] */
1197 obj = cl_object_find(env, echo_dev2cl(d), fid, &conf->eoc_cl);
1199 GOTO(out, eco = (void *)obj);
1201 eco = cl2echo_obj(obj);
1202 if (eco->eo_deleted) {
1203 cl_object_put(env, obj);
1204 eco = ERR_PTR(-EAGAIN);
1209 OBD_FREE_PTR(oinfo);
1211 cl_env_put(env, &refcheck);
1215 static int cl_echo_object_put(struct echo_object *eco)
1218 struct cl_object *obj = echo_obj2cl(eco);
1222 env = cl_env_get(&refcheck);
1224 RETURN(PTR_ERR(env));
1226 /* an external function to kill an object? */
1227 if (eco->eo_deleted) {
1228 struct lu_object_header *loh = obj->co_lu.lo_header;
1230 LASSERT(&eco->eo_hdr == luh2coh(loh));
1231 set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
1234 cl_object_put(env, obj);
1235 cl_env_put(env, &refcheck);
1239 static int cl_echo_enqueue0(struct lu_env *env, struct echo_object *eco,
1240 u64 start, u64 end, int mode,
1241 __u64 *cookie, __u32 enqflags)
1244 struct cl_lock *lck;
1245 struct cl_object *obj;
1246 struct cl_lock_descr *descr;
1247 struct echo_thread_info *info;
1251 info = echo_env_info(env);
1253 lck = &info->eti_lock;
1254 obj = echo_obj2cl(eco);
1256 memset(lck, 0, sizeof(*lck));
1257 descr = &lck->cll_descr;
1258 descr->cld_obj = obj;
1259 descr->cld_start = cl_index(obj, start);
1260 descr->cld_end = cl_index(obj, end);
1261 descr->cld_mode = mode == LCK_PW ? CLM_WRITE : CLM_READ;
1262 descr->cld_enq_flags = enqflags;
1265 rc = cl_lock_request(env, io, lck);
1267 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
1268 struct echo_lock *el;
1270 el = cl2echo_lock(cl_lock_at(lck, &echo_device_type));
1271 spin_lock(&ec->ec_lock);
1272 if (list_empty(&el->el_chain)) {
1273 list_add(&el->el_chain, &ec->ec_locks);
1274 el->el_cookie = ++ec->ec_unique;
1276 atomic_inc(&el->el_refcount);
1277 *cookie = el->el_cookie;
1278 spin_unlock(&ec->ec_lock);
1283 static int cl_echo_cancel0(struct lu_env *env, struct echo_device *ed,
1286 struct echo_client_obd *ec = ed->ed_ec;
1287 struct echo_lock *ecl = NULL;
1288 struct list_head *el;
1289 int found = 0, still_used = 0;
1292 LASSERT(ec != NULL);
1293 spin_lock(&ec->ec_lock);
1294 list_for_each(el, &ec->ec_locks) {
1295 ecl = list_entry(el, struct echo_lock, el_chain);
1296 CDEBUG(D_INFO, "ecl: %p, cookie: %#llx\n", ecl, ecl->el_cookie);
1297 found = (ecl->el_cookie == cookie);
1299 if (atomic_dec_and_test(&ecl->el_refcount))
1300 list_del_init(&ecl->el_chain);
1306 spin_unlock(&ec->ec_lock);
1311 echo_lock_release(env, ecl, still_used);
1315 static void echo_commit_callback(const struct lu_env *env, struct cl_io *io,
1316 struct pagevec *pvec)
1318 struct echo_thread_info *info;
1319 struct cl_2queue *queue;
1322 info = echo_env_info(env);
1323 LASSERT(io == &info->eti_io);
1325 queue = &info->eti_queue;
1327 for (i = 0; i < pagevec_count(pvec); i++) {
1328 struct page *vmpage = pvec->pages[i];
1329 struct cl_page *page = (struct cl_page *)vmpage->private;
1331 cl_page_list_add(&queue->c2_qout, page);
1335 static int cl_echo_object_brw(struct echo_object *eco, int rw, u64 offset,
1336 struct page **pages, int npages, int async)
1339 struct echo_thread_info *info;
1340 struct cl_object *obj = echo_obj2cl(eco);
1341 struct echo_device *ed = eco->eo_dev;
1342 struct cl_2queue *queue;
1344 struct cl_page *clp;
1345 struct lustre_handle lh = { 0 };
1346 int page_size = cl_page_size(obj);
1352 LASSERT((offset & ~PAGE_MASK) == 0);
1353 LASSERT(ed->ed_next != NULL);
1354 env = cl_env_get(&refcheck);
1356 RETURN(PTR_ERR(env));
1358 info = echo_env_info(env);
1360 queue = &info->eti_queue;
1362 cl_2queue_init(queue);
1364 io->ci_ignore_layout = 1;
1365 rc = cl_io_init(env, io, CIT_MISC, obj);
1370 rc = cl_echo_enqueue0(env, eco, offset,
1371 offset + npages * PAGE_SIZE - 1,
1372 rw == READ ? LCK_PR : LCK_PW, &lh.cookie,
1375 GOTO(error_lock, rc);
1377 for (i = 0; i < npages; i++) {
1379 clp = cl_page_find(env, obj, cl_index(obj, offset),
1380 pages[i], CPT_TRANSIENT);
1385 LASSERT(clp->cp_type == CPT_TRANSIENT);
1387 rc = cl_page_own(env, io, clp);
1389 LASSERT(clp->cp_state == CPS_FREEING);
1390 cl_page_put(env, clp);
1394 cl_2queue_add(queue, clp);
1397 * drop the reference count for cl_page_find, so that the page
1398 * will be freed in cl_2queue_fini.
1400 cl_page_put(env, clp);
1401 cl_page_clip(env, clp, 0, page_size);
1403 offset += page_size;
1407 enum cl_req_type typ = rw == READ ? CRT_READ : CRT_WRITE;
1409 async = async && (typ == CRT_WRITE);
1411 rc = cl_io_commit_async(env, io, &queue->c2_qin,
1413 echo_commit_callback);
1415 rc = cl_io_submit_sync(env, io, typ, queue, 0);
1416 CDEBUG(D_INFO, "echo_client %s write returns %d\n",
1417 async ? "async" : "sync", rc);
1420 cl_echo_cancel0(env, ed, lh.cookie);
1423 cl_2queue_discard(env, io, queue);
1424 cl_2queue_disown(env, io, queue);
1425 cl_2queue_fini(env, queue);
1426 cl_io_fini(env, io);
1428 cl_env_put(env, &refcheck);
1431 /** @} echo_exports */
1433 static u64 last_object_id;
1435 #ifdef HAVE_SERVER_SUPPORT
1436 static inline void echo_md_build_name(struct lu_name *lname, char *name,
1439 snprintf(name, ETI_NAME_LEN, "%llu", id);
1440 lname->ln_name = name;
1441 lname->ln_namelen = strlen(name);
1444 /* similar to mdt_attr_get_complex */
1445 static int echo_big_lmm_get(const struct lu_env *env, struct md_object *o,
1448 struct echo_thread_info *info = echo_env_info(env);
1453 LASSERT(ma->ma_lmm_size > 0);
1455 LASSERT(ma->ma_need & (MA_LOV | MA_LMV));
1456 if (ma->ma_need & MA_LOV)
1457 rc = mo_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LOV);
1459 rc = mo_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LMV);
1464 /* big_lmm may need to be grown */
1465 if (info->eti_big_lmmsize < rc) {
1466 int size = size_roundup_power2(rc);
1468 if (info->eti_big_lmmsize > 0) {
1469 /* free old buffer */
1470 LASSERT(info->eti_big_lmm);
1471 OBD_FREE_LARGE(info->eti_big_lmm,
1472 info->eti_big_lmmsize);
1473 info->eti_big_lmm = NULL;
1474 info->eti_big_lmmsize = 0;
1477 OBD_ALLOC_LARGE(info->eti_big_lmm, size);
1478 if (!info->eti_big_lmm)
1480 info->eti_big_lmmsize = size;
1482 LASSERT(info->eti_big_lmmsize >= rc);
1484 info->eti_buf.lb_buf = info->eti_big_lmm;
1485 info->eti_buf.lb_len = info->eti_big_lmmsize;
1486 if (ma->ma_need & MA_LOV)
1487 rc = mo_xattr_get(env, o, &info->eti_buf, XATTR_NAME_LOV);
1489 rc = mo_xattr_get(env, o, &info->eti_buf, XATTR_NAME_LMV);
1493 if (ma->ma_need & MA_LOV)
1494 ma->ma_valid |= MA_LOV;
1496 ma->ma_valid |= MA_LMV;
1498 ma->ma_lmm = info->eti_big_lmm;
1499 ma->ma_lmm_size = rc;
1504 static int echo_attr_get_complex(const struct lu_env *env,
1505 struct md_object *next,
1508 struct echo_thread_info *info = echo_env_info(env);
1509 struct lu_buf *buf = &info->eti_buf;
1510 umode_t mode = lu_object_attr(&next->mo_lu);
1517 if (ma->ma_need & MA_INODE) {
1518 rc = mo_attr_get(env, next, ma);
1521 ma->ma_valid |= MA_INODE;
1524 if ((ma->ma_need & MA_LOV) && (S_ISREG(mode) || S_ISDIR(mode))) {
1525 LASSERT(ma->ma_lmm_size > 0);
1526 buf->lb_buf = ma->ma_lmm;
1527 buf->lb_len = ma->ma_lmm_size;
1528 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LOV);
1530 ma->ma_lmm_size = rc2;
1531 ma->ma_valid |= MA_LOV;
1532 } else if (rc2 == -ENODATA) {
1534 ma->ma_lmm_size = 0;
1535 } else if (rc2 == -ERANGE) {
1536 rc2 = echo_big_lmm_get(env, next, ma);
1538 GOTO(out, rc = rc2);
1540 GOTO(out, rc = rc2);
1544 if ((ma->ma_need & MA_LMV) && S_ISDIR(mode)) {
1545 LASSERT(ma->ma_lmm_size > 0);
1546 buf->lb_buf = ma->ma_lmm;
1547 buf->lb_len = ma->ma_lmm_size;
1548 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LMV);
1550 ma->ma_lmm_size = rc2;
1551 ma->ma_valid |= MA_LMV;
1552 } else if (rc2 == -ENODATA) {
1554 ma->ma_lmm_size = 0;
1555 } else if (rc2 == -ERANGE) {
1556 rc2 = echo_big_lmm_get(env, next, ma);
1558 GOTO(out, rc = rc2);
1560 GOTO(out, rc = rc2);
1564 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
1565 if ((ma->ma_need & MA_ACL_DEF) && S_ISDIR(mode)) {
1566 buf->lb_buf = ma->ma_acl;
1567 buf->lb_len = ma->ma_acl_size;
1568 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1570 ma->ma_acl_size = rc2;
1571 ma->ma_valid |= MA_ACL_DEF;
1572 } else if (rc2 == -ENODATA) {
1574 ma->ma_acl_size = 0;
1576 GOTO(out, rc = rc2);
1581 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = %#llx ma_lmm=%p\n",
1582 rc, ma->ma_valid, ma->ma_lmm);
1587 echo_md_create_internal(const struct lu_env *env, struct echo_device *ed,
1588 struct md_object *parent, struct lu_fid *fid,
1589 struct lu_name *lname, struct md_op_spec *spec,
1592 struct lu_object *ec_child, *child;
1593 struct lu_device *ld = ed->ed_next;
1594 struct echo_thread_info *info = echo_env_info(env);
1595 struct lu_fid *fid2 = &info->eti_fid2;
1596 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
1601 rc = mdo_lookup(env, parent, lname, fid2, spec);
1604 else if (rc != -ENOENT)
1607 ec_child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev,
1609 if (IS_ERR(ec_child)) {
1610 CERROR("Can not find the child "DFID": rc = %ld\n", PFID(fid),
1612 RETURN(PTR_ERR(ec_child));
1615 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1617 CERROR("Can not locate the child "DFID"\n", PFID(fid));
1618 GOTO(out_put, rc = -EINVAL);
1621 CDEBUG(D_RPCTRACE, "Start creating object "DFID" %s %p\n",
1622 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1625 * Do not perform lookup sanity check. We know that name does not exist.
1627 spec->sp_cr_lookup = 0;
1628 rc = mdo_create(env, parent, lname, lu2md(child), spec, ma);
1630 CERROR("Can not create child "DFID": rc = %d\n", PFID(fid), rc);
1633 CDEBUG(D_RPCTRACE, "End creating object "DFID" %s %p rc = %d\n",
1634 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent, rc);
1637 lu_object_put(env, ec_child);
1641 static int echo_set_lmm_size(const struct lu_env *env, struct lu_device *ld,
1644 struct echo_thread_info *info = echo_env_info(env);
1646 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
1647 ma->ma_lmm = (void *)&info->eti_lmm;
1648 ma->ma_lmm_size = sizeof(info->eti_lmm);
1650 LASSERT(info->eti_big_lmmsize);
1651 ma->ma_lmm = info->eti_big_lmm;
1652 ma->ma_lmm_size = info->eti_big_lmmsize;
1659 echo_md_dir_stripe_choose(const struct lu_env *env, struct echo_device *ed,
1660 struct lu_object *obj, const char *name,
1661 unsigned int namelen, __u64 id,
1662 struct lu_object **new_parent)
1664 struct echo_thread_info *info = echo_env_info(env);
1665 struct md_attr *ma = &info->eti_ma;
1666 struct lmv_mds_md_v1 *lmv;
1667 struct lu_device *ld = ed->ed_next;
1669 struct lu_name tmp_ln_name;
1670 struct lu_fid stripe_fid;
1671 struct lu_object *stripe_obj;
1674 LASSERT(obj != NULL);
1675 LASSERT(S_ISDIR(obj->lo_header->loh_attr));
1677 memset(ma, 0, sizeof(*ma));
1678 echo_set_lmm_size(env, ld, ma);
1679 ma->ma_need = MA_LMV;
1680 rc = echo_attr_get_complex(env, lu2md(obj), ma);
1682 CERROR("Can not getattr child "DFID": rc = %d\n",
1683 PFID(lu_object_fid(obj)), rc);
1687 if (!(ma->ma_valid & MA_LMV)) {
1692 lmv = (struct lmv_mds_md_v1 *)ma->ma_lmm;
1693 if (!lmv_is_sane(lmv)) {
1695 CERROR("Invalid mds md magic %x "DFID": rc = %d\n",
1696 le32_to_cpu(lmv->lmv_magic), PFID(lu_object_fid(obj)),
1702 tmp_ln_name.ln_name = name;
1703 tmp_ln_name.ln_namelen = namelen;
1706 echo_md_build_name(&tmp_ln_name, info->eti_name, id);
1709 idx = lmv_name_to_stripe_index(lmv, tmp_ln_name.ln_name,
1710 tmp_ln_name.ln_namelen);
1712 LASSERT(idx < le32_to_cpu(lmv->lmv_stripe_count));
1713 fid_le_to_cpu(&stripe_fid, &lmv->lmv_stripe_fids[idx]);
1715 stripe_obj = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, &stripe_fid,
1717 if (IS_ERR(stripe_obj)) {
1718 rc = PTR_ERR(stripe_obj);
1719 CERROR("Can not find the parent "DFID": rc = %d\n",
1720 PFID(&stripe_fid), rc);
1724 *new_parent = lu_object_locate(stripe_obj->lo_header, ld->ld_type);
1726 lu_object_put(env, stripe_obj);
1733 static int echo_create_md_object(const struct lu_env *env,
1734 struct echo_device *ed,
1735 struct lu_object *ec_parent,
1737 char *name, int namelen,
1738 __u64 id, __u32 mode, int count,
1739 int stripe_count, int stripe_offset)
1741 struct lu_object *parent;
1742 struct lu_object *new_parent;
1743 struct echo_thread_info *info = echo_env_info(env);
1744 struct lu_name *lname = &info->eti_lname;
1745 struct md_op_spec *spec = &info->eti_spec;
1746 struct md_attr *ma = &info->eti_ma;
1747 struct lu_device *ld = ed->ed_next;
1755 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1759 rc = echo_md_dir_stripe_choose(env, ed, parent, name, namelen,
1764 LASSERT(new_parent != NULL);
1765 memset(ma, 0, sizeof(*ma));
1766 memset(spec, 0, sizeof(*spec));
1767 echo_set_lmm_size(env, ld, ma);
1768 if (stripe_count != 0) {
1769 spec->sp_cr_flags |= MDS_FMODE_WRITE;
1770 if (stripe_count != -1) {
1771 if (S_ISDIR(mode)) {
1772 struct lmv_user_md *lmu;
1774 lmu = (struct lmv_user_md *)&info->eti_lum;
1775 lmu->lum_magic = LMV_USER_MAGIC;
1776 lmu->lum_stripe_offset = stripe_offset;
1777 lmu->lum_stripe_count = stripe_count;
1778 lmu->lum_hash_type = LMV_HASH_TYPE_FNV_1A_64;
1779 spec->u.sp_ea.eadata = lmu;
1780 spec->u.sp_ea.eadatalen = sizeof(*lmu);
1782 struct lov_user_md_v3 *lum = &info->eti_lum;
1784 lum->lmm_magic = LOV_USER_MAGIC_V3;
1785 lum->lmm_stripe_count = stripe_count;
1786 lum->lmm_stripe_offset = stripe_offset;
1787 lum->lmm_pattern = LOV_PATTERN_NONE;
1788 spec->u.sp_ea.eadata = lum;
1789 spec->u.sp_ea.eadatalen = sizeof(*lum);
1791 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1795 ma->ma_attr.la_mode = mode;
1796 ma->ma_attr.la_valid = LA_CTIME | LA_MODE;
1797 ma->ma_attr.la_ctime = ktime_get_real_seconds();
1800 lname->ln_name = name;
1801 lname->ln_namelen = namelen;
1802 /* If name is specified, only create one object by name */
1803 rc = echo_md_create_internal(env, ed, lu2md(new_parent), fid,
1808 /* Create multiple object sequenced by id */
1809 for (i = 0; i < count; i++) {
1810 char *tmp_name = info->eti_name;
1812 echo_md_build_name(lname, tmp_name, id);
1814 rc = echo_md_create_internal(env, ed, lu2md(new_parent),
1815 fid, lname, spec, ma);
1817 CERROR("Can not create child %s: rc = %d\n", tmp_name,
1826 if (new_parent != parent)
1827 lu_object_put(env, new_parent);
1832 static struct lu_object *echo_md_lookup(const struct lu_env *env,
1833 struct echo_device *ed,
1834 struct md_object *parent,
1835 struct lu_name *lname)
1837 struct echo_thread_info *info = echo_env_info(env);
1838 struct lu_fid *fid = &info->eti_fid;
1839 struct lu_object *child;
1843 CDEBUG(D_INFO, "lookup %s in parent "DFID" %p\n", lname->ln_name,
1846 rc = mdo_lookup(env, parent, lname, fid, NULL);
1848 CERROR("lookup %s: rc = %d\n", lname->ln_name, rc);
1849 RETURN(ERR_PTR(rc));
1853 * In the function below, .hs_keycmp resolves to
1854 * lu_obj_hop_keycmp()
1856 /* coverity[overrun-buffer-val] */
1857 child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1862 static int echo_setattr_object(const struct lu_env *env,
1863 struct echo_device *ed,
1864 struct lu_object *ec_parent,
1865 __u64 id, int count)
1867 struct lu_object *parent;
1868 struct lu_object *new_parent;
1869 struct echo_thread_info *info = echo_env_info(env);
1870 struct lu_name *lname = &info->eti_lname;
1871 char *name = info->eti_name;
1872 struct lu_device *ld = ed->ed_next;
1873 struct lu_buf *buf = &info->eti_buf;
1881 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1885 rc = echo_md_dir_stripe_choose(env, ed, parent, NULL, 0, id,
1890 for (i = 0; i < count; i++) {
1891 struct lu_object *ec_child, *child;
1893 echo_md_build_name(lname, name, id);
1895 ec_child = echo_md_lookup(env, ed, lu2md(new_parent), lname);
1896 if (IS_ERR(ec_child)) {
1897 rc = PTR_ERR(ec_child);
1898 CERROR("Can't find child %s: rc = %d\n",
1899 lname->ln_name, rc);
1903 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1905 CERROR("Can not locate the child %s\n", lname->ln_name);
1906 lu_object_put(env, ec_child);
1911 CDEBUG(D_RPCTRACE, "Start setattr object "DFID"\n",
1912 PFID(lu_object_fid(child)));
1914 buf->lb_buf = info->eti_xattr_buf;
1915 buf->lb_len = sizeof(info->eti_xattr_buf);
1917 sprintf(name, "%s.test1", XATTR_USER_PREFIX);
1918 rc = mo_xattr_set(env, lu2md(child), buf, name,
1921 CERROR("Can not setattr child "DFID": rc = %d\n",
1922 PFID(lu_object_fid(child)), rc);
1923 lu_object_put(env, ec_child);
1926 CDEBUG(D_RPCTRACE, "End setattr object "DFID"\n",
1927 PFID(lu_object_fid(child)));
1929 lu_object_put(env, ec_child);
1932 if (new_parent != parent)
1933 lu_object_put(env, new_parent);
1938 static int echo_getattr_object(const struct lu_env *env,
1939 struct echo_device *ed,
1940 struct lu_object *ec_parent,
1941 __u64 id, int count)
1943 struct lu_object *parent;
1944 struct lu_object *new_parent;
1945 struct echo_thread_info *info = echo_env_info(env);
1946 struct lu_name *lname = &info->eti_lname;
1947 char *name = info->eti_name;
1948 struct md_attr *ma = &info->eti_ma;
1949 struct lu_device *ld = ed->ed_next;
1957 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1961 rc = echo_md_dir_stripe_choose(env, ed, parent, NULL, 0, id,
1966 memset(ma, 0, sizeof(*ma));
1967 ma->ma_need |= MA_INODE | MA_LOV | MA_PFID | MA_HSM | MA_ACL_DEF;
1968 ma->ma_acl = info->eti_xattr_buf;
1969 ma->ma_acl_size = sizeof(info->eti_xattr_buf);
1971 for (i = 0; i < count; i++) {
1972 struct lu_object *ec_child, *child;
1975 echo_md_build_name(lname, name, id);
1976 echo_set_lmm_size(env, ld, ma);
1978 ec_child = echo_md_lookup(env, ed, lu2md(new_parent), lname);
1979 if (IS_ERR(ec_child)) {
1980 CERROR("Can't find child %s: rc = %ld\n",
1981 lname->ln_name, PTR_ERR(ec_child));
1982 RETURN(PTR_ERR(ec_child));
1985 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1987 CERROR("Can not locate the child %s\n", lname->ln_name);
1988 lu_object_put(env, ec_child);
1992 CDEBUG(D_RPCTRACE, "Start getattr object "DFID"\n",
1993 PFID(lu_object_fid(child)));
1994 rc = echo_attr_get_complex(env, lu2md(child), ma);
1996 CERROR("Can not getattr child "DFID": rc = %d\n",
1997 PFID(lu_object_fid(child)), rc);
1998 lu_object_put(env, ec_child);
2001 CDEBUG(D_RPCTRACE, "End getattr object "DFID"\n",
2002 PFID(lu_object_fid(child)));
2004 lu_object_put(env, ec_child);
2007 if (new_parent != parent)
2008 lu_object_put(env, new_parent);
2013 static int echo_lookup_object(const struct lu_env *env,
2014 struct echo_device *ed,
2015 struct lu_object *ec_parent,
2016 __u64 id, int count)
2018 struct lu_object *parent;
2019 struct lu_object *new_parent;
2020 struct echo_thread_info *info = echo_env_info(env);
2021 struct lu_name *lname = &info->eti_lname;
2022 char *name = info->eti_name;
2023 struct lu_fid *fid = &info->eti_fid;
2024 struct lu_device *ld = ed->ed_next;
2030 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
2034 rc = echo_md_dir_stripe_choose(env, ed, parent, NULL, 0, id,
2039 /*prepare the requests*/
2040 for (i = 0; i < count; i++) {
2041 echo_md_build_name(lname, name, id);
2043 CDEBUG(D_RPCTRACE, "Start lookup object "DFID" %s %p\n",
2044 PFID(lu_object_fid(new_parent)), lname->ln_name,
2047 rc = mdo_lookup(env, lu2md(new_parent), lname, fid, NULL);
2049 CERROR("Can not lookup child %s: rc = %d\n", name, rc);
2053 CDEBUG(D_RPCTRACE, "End lookup object "DFID" %s %p\n",
2054 PFID(lu_object_fid(new_parent)), lname->ln_name,
2060 if (new_parent != parent)
2061 lu_object_put(env, new_parent);
2066 static int echo_md_destroy_internal(const struct lu_env *env,
2067 struct echo_device *ed,
2068 struct md_object *parent,
2069 struct lu_name *lname,
2072 struct lu_device *ld = ed->ed_next;
2073 struct lu_object *ec_child;
2074 struct lu_object *child;
2079 ec_child = echo_md_lookup(env, ed, parent, lname);
2080 if (IS_ERR(ec_child)) {
2081 CERROR("Can't find child %s: rc = %ld\n", lname->ln_name,
2083 RETURN(PTR_ERR(ec_child));
2086 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
2088 CERROR("Can not locate the child %s\n", lname->ln_name);
2089 GOTO(out_put, rc = -EINVAL);
2092 if (lu_object_remote(child)) {
2093 CERROR("Can not destroy remote object %s: rc = %d\n",
2094 lname->ln_name, -EPERM);
2095 GOTO(out_put, rc = -EPERM);
2097 CDEBUG(D_RPCTRACE, "Start destroy object "DFID" %s %p\n",
2098 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
2100 rc = mdo_unlink(env, parent, lu2md(child), lname, ma, 0);
2102 CERROR("Can not unlink child %s: rc = %d\n",
2103 lname->ln_name, rc);
2106 CDEBUG(D_RPCTRACE, "End destroy object "DFID" %s %p\n",
2107 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
2109 lu_object_put(env, ec_child);
2113 static int echo_destroy_object(const struct lu_env *env,
2114 struct echo_device *ed,
2115 struct lu_object *ec_parent,
2116 char *name, int namelen,
2117 __u64 id, __u32 mode,
2120 struct echo_thread_info *info = echo_env_info(env);
2121 struct lu_name *lname = &info->eti_lname;
2122 struct md_attr *ma = &info->eti_ma;
2123 struct lu_device *ld = ed->ed_next;
2124 struct lu_object *parent;
2125 struct lu_object *new_parent;
2130 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
2134 rc = echo_md_dir_stripe_choose(env, ed, parent, name, namelen,
2139 memset(ma, 0, sizeof(*ma));
2140 ma->ma_attr.la_mode = mode;
2141 ma->ma_attr.la_valid = LA_CTIME;
2142 ma->ma_attr.la_ctime = ktime_get_real_seconds();
2143 ma->ma_need = MA_INODE;
2147 lname->ln_name = name;
2148 lname->ln_namelen = namelen;
2149 rc = echo_md_destroy_internal(env, ed, lu2md(new_parent), lname,
2154 /*prepare the requests*/
2155 for (i = 0; i < count; i++) {
2156 char *tmp_name = info->eti_name;
2159 echo_md_build_name(lname, tmp_name, id);
2161 rc = echo_md_destroy_internal(env, ed, lu2md(new_parent), lname,
2164 CERROR("Can not unlink child %s: rc = %d\n", name, rc);
2171 if (new_parent != parent)
2172 lu_object_put(env, new_parent);
2177 static struct lu_object *echo_resolve_path(const struct lu_env *env,
2178 struct echo_device *ed, char *path,
2181 struct lu_device *ld = ed->ed_next;
2182 struct echo_thread_info *info = echo_env_info(env);
2183 struct lu_fid *fid = &info->eti_fid;
2184 struct lu_name *lname = &info->eti_lname;
2185 struct lu_object *parent = NULL;
2186 struct lu_object *child = NULL;
2190 *fid = ed->ed_root_fid;
2193 * In the function below, .hs_keycmp resolves to
2194 * lu_obj_hop_keycmp()
2196 /* coverity[overrun-buffer-val] */
2197 parent = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
2198 if (IS_ERR(parent)) {
2199 CERROR("Can not find the parent "DFID": rc = %ld\n",
2200 PFID(fid), PTR_ERR(parent));
2205 struct lu_object *ld_parent;
2208 e = strsep(&path, "/");
2213 if (!path || path[0] == '\0')
2219 lname->ln_namelen = strlen(e);
2221 ld_parent = lu_object_locate(parent->lo_header, ld->ld_type);
2223 lu_object_put(env, parent);
2228 child = echo_md_lookup(env, ed, lu2md(ld_parent), lname);
2229 lu_object_put(env, parent);
2230 if (IS_ERR(child)) {
2231 rc = (int)PTR_ERR(child);
2232 CERROR("lookup %s under parent "DFID": rc = %d\n",
2233 lname->ln_name, PFID(lu_object_fid(ld_parent)),
2240 RETURN(ERR_PTR(rc));
2245 static void echo_ucred_init(struct lu_env *env)
2247 struct lu_ucred *ucred = lu_ucred(env);
2248 kernel_cap_t kcap = current_cap();
2250 ucred->uc_valid = UCRED_INVALID;
2252 ucred->uc_suppgids[0] = -1;
2253 ucred->uc_suppgids[1] = -1;
2255 ucred->uc_uid = ucred->uc_o_uid =
2256 from_kuid(&init_user_ns, current_uid());
2257 ucred->uc_gid = ucred->uc_o_gid =
2258 from_kgid(&init_user_ns, current_gid());
2259 ucred->uc_fsuid = ucred->uc_o_fsuid =
2260 from_kuid(&init_user_ns, current_fsuid());
2261 ucred->uc_fsgid = ucred->uc_o_fsgid =
2262 from_kgid(&init_user_ns, current_fsgid());
2263 ucred->uc_cap = cfs_curproc_cap_pack();
2265 /* remove fs privilege for non-root user. */
2266 if (ucred->uc_fsuid) {
2267 kcap = cap_drop_nfsd_set(kcap);
2268 kcap = cap_drop_fs_set(kcap);
2270 ucred->uc_cap = kcap.cap[0];
2271 ucred->uc_valid = UCRED_NEW;
2274 static void echo_ucred_fini(struct lu_env *env)
2276 struct lu_ucred *ucred = lu_ucred(env);
2278 ucred->uc_valid = UCRED_INIT;
2281 static int echo_md_handler(struct echo_device *ed, int command,
2282 char *path, int path_len, __u64 id, int count,
2283 struct obd_ioctl_data *data)
2285 struct echo_thread_info *info;
2286 struct lu_device *ld = ed->ed_next;
2289 struct lu_object *parent;
2291 int namelen = data->ioc_plen2;
2296 CERROR("MD echo client is not being initialized properly\n");
2300 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
2301 CERROR("Only support MDD layer right now!\n");
2305 env = cl_env_get(&refcheck);
2307 RETURN(PTR_ERR(env));
2309 rc = lu_env_refill_by_tags(env, ECHO_MD_CTX_TAG, ECHO_SES_TAG);
2313 /* init big_lmm buffer */
2314 info = echo_env_info(env);
2315 LASSERT(info->eti_big_lmm == NULL);
2316 OBD_ALLOC_LARGE(info->eti_big_lmm, MIN_MD_SIZE);
2317 if (!info->eti_big_lmm)
2318 GOTO(out_env, rc = -ENOMEM);
2319 info->eti_big_lmmsize = MIN_MD_SIZE;
2321 parent = echo_resolve_path(env, ed, path, path_len);
2322 if (IS_ERR(parent)) {
2323 CERROR("Can not resolve the path %s: rc = %ld\n", path,
2325 GOTO(out_free, rc = PTR_ERR(parent));
2329 OBD_ALLOC(name, namelen + 1);
2331 GOTO(out_put, rc = -ENOMEM);
2332 if (copy_from_user(name, data->ioc_pbuf2, namelen))
2333 GOTO(out_name, rc = -EFAULT);
2336 echo_ucred_init(env);
2339 case ECHO_MD_CREATE:
2340 case ECHO_MD_MKDIR: {
2341 struct echo_thread_info *info = echo_env_info(env);
2342 __u32 mode = data->ioc_obdo2.o_mode;
2343 struct lu_fid *fid = &info->eti_fid;
2344 int stripe_count = (int)data->ioc_obdo2.o_misc;
2345 int stripe_index = (int)data->ioc_obdo2.o_stripe_idx;
2347 rc = ostid_to_fid(fid, &data->ioc_obdo1.o_oi, 0);
2352 * In the function below, .hs_keycmp resolves to
2353 * lu_obj_hop_keycmp()
2355 /* coverity[overrun-buffer-val] */
2356 rc = echo_create_md_object(env, ed, parent, fid, name, namelen,
2357 id, mode, count, stripe_count,
2361 case ECHO_MD_DESTROY:
2362 case ECHO_MD_RMDIR: {
2363 __u32 mode = data->ioc_obdo2.o_mode;
2365 rc = echo_destroy_object(env, ed, parent, name, namelen,
2369 case ECHO_MD_LOOKUP:
2370 rc = echo_lookup_object(env, ed, parent, id, count);
2372 case ECHO_MD_GETATTR:
2373 rc = echo_getattr_object(env, ed, parent, id, count);
2375 case ECHO_MD_SETATTR:
2376 rc = echo_setattr_object(env, ed, parent, id, count);
2379 CERROR("unknown command %d\n", command);
2383 echo_ucred_fini(env);
2387 OBD_FREE(name, namelen + 1);
2389 lu_object_put(env, parent);
2391 LASSERT(info->eti_big_lmm);
2392 OBD_FREE_LARGE(info->eti_big_lmm, info->eti_big_lmmsize);
2393 info->eti_big_lmm = NULL;
2394 info->eti_big_lmmsize = 0;
2396 cl_env_put(env, &refcheck);
2399 #endif /* HAVE_SERVER_SUPPORT */
2401 static int echo_create_object(const struct lu_env *env, struct echo_device *ed,
2404 struct echo_object *eco;
2405 struct echo_client_obd *ec = ed->ed_ec;
2410 if (!(oa->o_valid & OBD_MD_FLID) ||
2411 !(oa->o_valid & OBD_MD_FLGROUP) ||
2412 !fid_seq_is_echo(ostid_seq(&oa->o_oi))) {
2413 CERROR("invalid oid "DOSTID"\n", POSTID(&oa->o_oi));
2417 if (ostid_id(&oa->o_oi) == 0) {
2418 rc = ostid_set_id(&oa->o_oi, ++last_object_id);
2423 rc = obd_create(env, ec->ec_exp, oa);
2425 CERROR("Cannot create objects: rc = %d\n", rc);
2431 oa->o_valid |= OBD_MD_FLID;
2433 eco = cl_echo_object_find(ed, &oa->o_oi);
2435 GOTO(failed, rc = PTR_ERR(eco));
2436 cl_echo_object_put(eco);
2438 CDEBUG(D_INFO, "oa oid "DOSTID"\n", POSTID(&oa->o_oi));
2442 if (created && rc != 0)
2443 obd_destroy(env, ec->ec_exp, oa);
2446 CERROR("create object failed with: rc = %d\n", rc);
2451 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
2454 struct echo_object *eco;
2458 if (!(oa->o_valid & OBD_MD_FLID) ||
2459 !(oa->o_valid & OBD_MD_FLGROUP) ||
2460 ostid_id(&oa->o_oi) == 0) {
2461 CERROR("invalid oid "DOSTID"\n", POSTID(&oa->o_oi));
2466 eco = cl_echo_object_find(ed, &oa->o_oi);
2475 static void echo_put_object(struct echo_object *eco)
2479 rc = cl_echo_object_put(eco);
2481 CERROR("%s: echo client drop an object failed: rc = %d\n",
2482 eco->eo_dev->ed_ec->ec_exp->exp_obd->obd_name, rc);
2485 static void echo_client_page_debug_setup(struct page *page, int rw, u64 id,
2486 u64 offset, u64 count)
2493 /* no partial pages on the client */
2494 LASSERT(count == PAGE_SIZE);
2498 for (delta = 0; delta < PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2499 if (rw == OBD_BRW_WRITE) {
2500 stripe_off = offset + delta;
2503 stripe_off = 0xdeadbeef00c0ffeeULL;
2504 stripe_id = 0xdeadbeef00c0ffeeULL;
2506 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
2507 stripe_off, stripe_id);
2514 echo_client_page_debug_check(struct page *page, u64 id, u64 offset, u64 count)
2523 /* no partial pages on the client */
2524 LASSERT(count == PAGE_SIZE);
2528 for (rc = delta = 0; delta < PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2529 stripe_off = offset + delta;
2532 rc2 = block_debug_check("test_brw",
2533 addr + delta, OBD_ECHO_BLOCK_SIZE,
2534 stripe_off, stripe_id);
2536 CERROR("Error in echo object %#llx\n", id);
2545 static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
2546 struct echo_object *eco, u64 offset,
2547 u64 count, int async)
2550 struct brw_page *pga;
2551 struct brw_page *pgp;
2552 struct page **pages;
2561 verify = (ostid_id(&oa->o_oi) != ECHO_PERSISTENT_OBJID &&
2562 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
2563 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
2565 gfp_mask = ((ostid_id(&oa->o_oi) & 2) == 0) ? GFP_KERNEL : GFP_HIGHUSER;
2567 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
2569 if ((count & (~PAGE_MASK)) != 0)
2572 /* XXX think again with misaligned I/O */
2573 npages = count >> PAGE_SHIFT;
2575 if (rw == OBD_BRW_WRITE)
2576 brw_flags = OBD_BRW_ASYNC;
2578 OBD_ALLOC_PTR_ARRAY_LARGE(pga, npages);
2582 OBD_ALLOC_PTR_ARRAY_LARGE(pages, npages);
2584 OBD_FREE_PTR_ARRAY_LARGE(pga, npages);
2588 for (i = 0, pgp = pga, off = offset;
2590 i++, pgp++, off += PAGE_SIZE) {
2592 LASSERT(pgp->pg == NULL); /* for cleanup */
2595 pgp->pg = alloc_page(gfp_mask);
2599 /* set mapping so page is not considered encrypted */
2600 pgp->pg->mapping = ECHO_MAPPING_UNENCRYPTED;
2602 pgp->count = PAGE_SIZE;
2604 pgp->flag = brw_flags;
2607 echo_client_page_debug_setup(pgp->pg, rw,
2608 ostid_id(&oa->o_oi), off,
2612 /* brw mode can only be used at client */
2613 LASSERT(ed->ed_next != NULL);
2614 rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
2617 if (rc != 0 || rw != OBD_BRW_READ)
2620 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
2627 vrc = echo_client_page_debug_check(pgp->pg,
2628 ostid_id(&oa->o_oi),
2631 if (vrc != 0 && rc == 0)
2634 __free_page(pgp->pg);
2636 OBD_FREE_PTR_ARRAY_LARGE(pga, npages);
2637 OBD_FREE_PTR_ARRAY_LARGE(pages, npages);
2641 static int echo_client_prep_commit(const struct lu_env *env,
2642 struct obd_export *exp, int rw,
2643 struct obdo *oa, struct echo_object *eco,
2644 u64 offset, u64 count,
2645 u64 batch, int async)
2647 struct obd_ioobj ioo;
2648 struct niobuf_local *lnb;
2649 struct niobuf_remote rnb;
2651 u64 npages, tot_pages, apc;
2652 int i, ret = 0, brw_flags = 0;
2655 if (count <= 0 || (count & ~PAGE_MASK) != 0)
2658 apc = npages = batch >> PAGE_SHIFT;
2659 tot_pages = count >> PAGE_SHIFT;
2661 OBD_ALLOC_PTR_ARRAY_LARGE(lnb, apc);
2665 if (rw == OBD_BRW_WRITE && async)
2666 brw_flags |= OBD_BRW_ASYNC;
2668 obdo_to_ioobj(oa, &ioo);
2672 for (; tot_pages > 0; tot_pages -= npages) {
2675 if (tot_pages < npages)
2678 rnb.rnb_offset = off;
2679 rnb.rnb_len = npages * PAGE_SIZE;
2680 rnb.rnb_flags = brw_flags;
2682 off += npages * PAGE_SIZE;
2685 ret = obd_preprw(env, rw, exp, oa, 1, &ioo, &rnb, &lpages, lnb);
2689 for (i = 0; i < lpages; i++) {
2690 struct page *page = lnb[i].lnb_page;
2692 /* read past eof? */
2693 if (!page && lnb[i].lnb_rc == 0)
2697 lnb[i].lnb_flags |= OBD_BRW_ASYNC;
2699 if (ostid_id(&oa->o_oi) == ECHO_PERSISTENT_OBJID ||
2700 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
2701 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
2704 if (rw == OBD_BRW_WRITE)
2705 echo_client_page_debug_setup(page, rw,
2706 ostid_id(&oa->o_oi),
2707 lnb[i].lnb_file_offset,
2710 echo_client_page_debug_check(page,
2711 ostid_id(&oa->o_oi),
2712 lnb[i].lnb_file_offset,
2716 ret = obd_commitrw(env, rw, exp, oa, 1, &ioo, &rnb, npages, lnb,
2721 /* Reuse env context. */
2722 lu_context_exit((struct lu_context *)&env->le_ctx);
2723 lu_context_enter((struct lu_context *)&env->le_ctx);
2727 OBD_FREE_PTR_ARRAY_LARGE(lnb, apc);
2732 static int echo_client_brw_ioctl(const struct lu_env *env, int rw,
2733 struct obd_export *exp,
2734 struct obd_ioctl_data *data)
2736 struct obd_device *obd = class_exp2obd(exp);
2737 struct echo_device *ed = obd2echo_dev(obd);
2738 struct echo_client_obd *ec = ed->ed_ec;
2739 struct obdo *oa = &data->ioc_obdo1;
2740 struct echo_object *eco;
2746 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2748 rc = echo_get_object(&eco, ed, oa);
2752 oa->o_valid &= ~OBD_MD_FLHANDLE;
2754 /* OFD/obdfilter works only via prep/commit */
2755 test_mode = (long)data->ioc_pbuf1;
2756 if (!ed->ed_next && test_mode != 3) {
2758 data->ioc_plen1 = data->ioc_count;
2764 /* Truncate batch size to maximum */
2765 if (data->ioc_plen1 > PTLRPC_MAX_BRW_SIZE)
2766 data->ioc_plen1 = PTLRPC_MAX_BRW_SIZE;
2768 switch (test_mode) {
2772 rc = echo_client_kbrw(ed, rw, oa, eco, data->ioc_offset,
2773 data->ioc_count, async);
2776 rc = echo_client_prep_commit(env, ec->ec_exp, rw, oa, eco,
2777 data->ioc_offset, data->ioc_count,
2778 data->ioc_plen1, async);
2784 echo_put_object(eco);
2790 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2791 void *karg, void __user *uarg)
2793 #ifdef HAVE_SERVER_SUPPORT
2794 struct tgt_session_info *tsi;
2796 struct obd_device *obd = exp->exp_obd;
2797 struct echo_device *ed = obd2echo_dev(obd);
2798 struct echo_client_obd *ec = ed->ed_ec;
2799 struct echo_object *eco;
2800 struct obd_ioctl_data *data = karg;
2802 unsigned long env_tags = 0;
2806 int rw = OBD_BRW_READ;
2810 oa = &data->ioc_obdo1;
2811 if (!(oa->o_valid & OBD_MD_FLGROUP)) {
2812 oa->o_valid |= OBD_MD_FLGROUP;
2813 ostid_set_seq_echo(&oa->o_oi);
2816 /* This FID is unpacked just for validation at this point */
2817 rc = ostid_to_fid(&fid, &oa->o_oi, 0);
2821 env = cl_env_get(&refcheck);
2823 RETURN(PTR_ERR(env));
2827 #ifdef HAVE_SERVER_SUPPORT
2828 if (cmd == OBD_IOC_ECHO_MD || cmd == OBD_IOC_ECHO_ALLOC_SEQ)
2829 env_tags = ECHO_MD_CTX_TAG;
2832 env_tags = ECHO_DT_CTX_TAG;
2834 rc = lu_env_refill_by_tags(env, env_tags, ECHO_SES_TAG);
2838 #ifdef HAVE_SERVER_SUPPORT
2839 tsi = tgt_ses_info(env);
2840 /* treat as local operation */
2841 tsi->tsi_exp = NULL;
2842 tsi->tsi_jobid = NULL;
2846 case OBD_IOC_CREATE: /* may create echo object */
2847 if (!capable(CAP_SYS_ADMIN))
2848 GOTO(out, rc = -EPERM);
2850 rc = echo_create_object(env, ed, oa);
2853 #ifdef HAVE_SERVER_SUPPORT
2854 case OBD_IOC_ECHO_MD: {
2861 if (!capable(CAP_SYS_ADMIN))
2862 GOTO(out, rc = -EPERM);
2864 count = data->ioc_count;
2865 cmd = data->ioc_command;
2867 id = data->ioc_obdo2.o_oi.oi.oi_id;
2868 dirlen = data->ioc_plen1;
2869 OBD_ALLOC(dir, dirlen + 1);
2871 GOTO(out, rc = -ENOMEM);
2873 if (copy_from_user(dir, data->ioc_pbuf1, dirlen)) {
2874 OBD_FREE(dir, data->ioc_plen1 + 1);
2875 GOTO(out, rc = -EFAULT);
2878 rc = echo_md_handler(ed, cmd, dir, dirlen, id, count, data);
2879 OBD_FREE(dir, dirlen + 1);
2882 case OBD_IOC_ECHO_ALLOC_SEQ: {
2886 if (!capable(CAP_SYS_ADMIN))
2887 GOTO(out, rc = -EPERM);
2889 rc = seq_client_get_seq(env, ed->ed_cl_seq, &seq);
2891 CERROR("%s: Can not alloc seq: rc = %d\n",
2896 if (copy_to_user(data->ioc_pbuf1, &seq, data->ioc_plen1))
2899 max_count = LUSTRE_METADATA_SEQ_MAX_WIDTH;
2900 if (copy_to_user(data->ioc_pbuf2, &max_count,
2905 #endif /* HAVE_SERVER_SUPPORT */
2906 case OBD_IOC_DESTROY:
2907 if (!capable(CAP_SYS_ADMIN))
2908 GOTO(out, rc = -EPERM);
2910 rc = echo_get_object(&eco, ed, oa);
2912 rc = obd_destroy(env, ec->ec_exp, oa);
2914 eco->eo_deleted = 1;
2915 echo_put_object(eco);
2919 case OBD_IOC_GETATTR:
2920 rc = echo_get_object(&eco, ed, oa);
2922 rc = obd_getattr(env, ec->ec_exp, oa);
2923 echo_put_object(eco);
2927 case OBD_IOC_SETATTR:
2928 if (!capable(CAP_SYS_ADMIN))
2929 GOTO(out, rc = -EPERM);
2931 rc = echo_get_object(&eco, ed, oa);
2933 rc = obd_setattr(env, ec->ec_exp, oa);
2934 echo_put_object(eco);
2938 case OBD_IOC_BRW_WRITE:
2939 if (!capable(CAP_SYS_ADMIN))
2940 GOTO(out, rc = -EPERM);
2944 case OBD_IOC_BRW_READ:
2945 rc = echo_client_brw_ioctl(env, rw, exp, data);
2949 CERROR("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
2950 GOTO(out, rc = -ENOTTY);
2956 cl_env_put(env, &refcheck);
2961 static int echo_client_setup(const struct lu_env *env,
2962 struct obd_device *obd, struct lustre_cfg *lcfg)
2964 struct echo_client_obd *ec = &obd->u.echo_client;
2965 struct obd_device *tgt;
2966 struct obd_uuid echo_uuid = { "ECHO_UUID" };
2967 struct obd_connect_data *ocd = NULL;
2971 if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
2972 CERROR("requires a TARGET OBD name\n");
2976 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
2977 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
2978 CERROR("device not attached or not set up (%s)\n",
2979 lustre_cfg_string(lcfg, 1));
2983 spin_lock_init(&ec->ec_lock);
2984 INIT_LIST_HEAD(&ec->ec_objects);
2985 INIT_LIST_HEAD(&ec->ec_locks);
2988 lu_context_tags_update(ECHO_DT_CTX_TAG);
2989 lu_session_tags_update(ECHO_SES_TAG);
2991 if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
2992 #ifdef HAVE_SERVER_SUPPORT
2993 lu_context_tags_update(ECHO_MD_CTX_TAG);
2996 "Local operations are NOT supported on client side. Only remote operations are supported. Metadata client must be run on server side.\n");
3001 OBD_ALLOC(ocd, sizeof(*ocd));
3003 CERROR("Can't alloc ocd connecting to %s\n",
3004 lustre_cfg_string(lcfg, 1));
3008 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
3009 OBD_CONNECT_BRW_SIZE |
3010 OBD_CONNECT_GRANT | OBD_CONNECT_FULL20 |
3011 OBD_CONNECT_64BITHASH | OBD_CONNECT_LVB_TYPE |
3012 OBD_CONNECT_FID | OBD_CONNECT_FLAGS2;
3013 ocd->ocd_connect_flags2 = OBD_CONNECT2_REP_MBITS;
3015 ocd->ocd_brw_size = DT_MAX_BRW_SIZE;
3016 ocd->ocd_version = LUSTRE_VERSION_CODE;
3017 ocd->ocd_group = FID_SEQ_ECHO;
3019 rc = obd_connect(env, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
3021 /* Turn off pinger because it connects to tgt obd directly. */
3022 spin_lock(&tgt->obd_dev_lock);
3023 list_del_init(&ec->ec_exp->exp_obd_chain_timed);
3024 spin_unlock(&tgt->obd_dev_lock);
3027 OBD_FREE(ocd, sizeof(*ocd));
3030 CERROR("fail to connect to device %s\n",
3031 lustre_cfg_string(lcfg, 1));
3038 static int echo_client_cleanup(struct obd_device *obd)
3040 struct echo_device *ed = obd2echo_dev(obd);
3041 struct echo_client_obd *ec = &obd->u.echo_client;
3045 /*Do nothing for Metadata echo client*/
3049 lu_session_tags_clear(ECHO_SES_TAG & ~LCT_SESSION);
3050 lu_context_tags_clear(ECHO_DT_CTX_TAG);
3051 if (ed->ed_next_ismd) {
3052 #ifdef HAVE_SERVER_SUPPORT
3053 lu_context_tags_clear(ECHO_MD_CTX_TAG);
3056 "This is client-side only module, does not support metadata echo client.\n");
3061 if (!list_empty(&obd->obd_exports)) {
3062 CERROR("still has clients!\n");
3066 LASSERT(refcount_read(&ec->ec_exp->exp_handle.h_ref) > 0);
3067 rc = obd_disconnect(ec->ec_exp);
3069 CERROR("fail to disconnect device: %d\n", rc);
3074 static int echo_client_connect(const struct lu_env *env,
3075 struct obd_export **exp,
3076 struct obd_device *src, struct obd_uuid *cluuid,
3077 struct obd_connect_data *data, void *localdata)
3080 struct lustre_handle conn = { 0 };
3083 rc = class_connect(&conn, src, cluuid);
3085 *exp = class_conn2export(&conn);
3090 static int echo_client_disconnect(struct obd_export *exp)
3096 GOTO(out, rc = -EINVAL);
3098 rc = class_disconnect(exp);
3104 static const struct obd_ops echo_client_obd_ops = {
3105 .o_owner = THIS_MODULE,
3106 .o_iocontrol = echo_client_iocontrol,
3107 .o_connect = echo_client_connect,
3108 .o_disconnect = echo_client_disconnect
3111 static int __init obdecho_init(void)
3116 LCONSOLE_INFO("Echo OBD driver; http://www.lustre.org/\n");
3118 LASSERT(PAGE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
3120 # ifdef HAVE_SERVER_SUPPORT
3121 rc = echo_persistent_pages_init();
3125 rc = class_register_type(&echo_obd_ops, NULL, true,
3126 LUSTRE_ECHO_NAME, &echo_srv_type);
3131 rc = lu_kmem_init(echo_caches);
3133 rc = class_register_type(&echo_client_obd_ops, NULL, false,
3134 LUSTRE_ECHO_CLIENT_NAME,
3137 lu_kmem_fini(echo_caches);
3140 # ifdef HAVE_SERVER_SUPPORT
3144 class_unregister_type(LUSTRE_ECHO_NAME);
3146 echo_persistent_pages_fini();
3152 static void __exit obdecho_exit(void)
3154 class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
3155 lu_kmem_fini(echo_caches);
3157 #ifdef HAVE_SERVER_SUPPORT
3158 class_unregister_type(LUSTRE_ECHO_NAME);
3159 echo_persistent_pages_fini();
3163 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
3164 MODULE_DESCRIPTION("Lustre Echo Client test driver");
3165 MODULE_VERSION(LUSTRE_VERSION_STRING);
3166 MODULE_LICENSE("GPL");
3168 module_init(obdecho_init);
3169 module_exit(obdecho_exit);
3171 /** @} echo_client */