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
65 /* echo thread key have a CL_THREAD flag, which set cl_env function directly */
66 #define ECHO_MD_CTX_TAG (LCT_REMEMBER | LCT_MD_THREAD)
67 #define ECHO_DT_CTX_TAG (LCT_REMEMBER | LCT_DT_THREAD)
68 #define ECHO_SES_TAG (LCT_REMEMBER | LCT_SESSION | LCT_SERVER_SESSION)
71 struct cl_device ed_cl;
72 struct echo_client_obd *ed_ec;
74 struct cl_site ed_site_myself;
75 struct lu_site *ed_site;
76 struct lu_device *ed_next;
78 struct lu_client_seq *ed_cl_seq;
79 #ifdef HAVE_SERVER_SUPPORT
80 struct local_oid_storage *ed_los;
81 struct lu_fid ed_root_fid;
82 #endif /* HAVE_SERVER_SUPPORT */
86 struct cl_object eo_cl;
87 struct cl_object_header eo_hdr;
88 struct echo_device *eo_dev;
89 struct list_head eo_obj_chain;
90 struct lov_oinfo *eo_oinfo;
95 struct echo_object_conf {
96 struct cl_object_conf eoc_cl;
97 struct lov_oinfo **eoc_oinfo;
101 struct cl_page_slice ep_cl;
102 unsigned long ep_lock;
106 struct cl_lock_slice el_cl;
107 struct list_head el_chain;
108 struct echo_object *el_object;
110 atomic_t el_refcount;
113 #ifdef HAVE_SERVER_SUPPORT
114 static const char echo_md_root_dir_name[] = "ROOT_ECHO";
117 * In order to use the values of members in struct mdd_device,
118 * we define an alias structure here.
120 struct echo_md_device {
121 struct md_device emd_md_dev;
122 struct obd_export *emd_child_exp;
123 struct dt_device *emd_child;
124 struct dt_device *emd_bottom;
125 struct lu_fid emd_root_fid;
126 struct lu_fid emd_local_root_fid;
128 #endif /* HAVE_SERVER_SUPPORT */
130 static int echo_client_setup(const struct lu_env *env,
131 struct obd_device *obddev,
132 struct lustre_cfg *lcfg);
133 static int echo_client_cleanup(struct obd_device *obddev);
135 /** \defgroup echo_helpers Helper functions
138 static inline struct echo_device *cl2echo_dev(const struct cl_device *dev)
140 return container_of0(dev, struct echo_device, ed_cl);
143 static inline struct cl_device *echo_dev2cl(struct echo_device *d)
148 static inline struct echo_device *obd2echo_dev(const struct obd_device *obd)
150 return cl2echo_dev(lu2cl_dev(obd->obd_lu_dev));
153 static inline struct cl_object *echo_obj2cl(struct echo_object *eco)
158 static inline struct echo_object *cl2echo_obj(const struct cl_object *o)
160 return container_of(o, struct echo_object, eo_cl);
163 static inline struct echo_page *cl2echo_page(const struct cl_page_slice *s)
165 return container_of(s, struct echo_page, ep_cl);
168 static inline struct echo_lock *cl2echo_lock(const struct cl_lock_slice *s)
170 return container_of(s, struct echo_lock, el_cl);
173 static inline struct cl_lock *echo_lock2cl(const struct echo_lock *ecl)
175 return ecl->el_cl.cls_lock;
178 static struct lu_context_key echo_thread_key;
180 static inline struct echo_thread_info *echo_env_info(const struct lu_env *env)
182 struct echo_thread_info *info;
184 info = lu_context_key_get(&env->le_ctx, &echo_thread_key);
185 LASSERT(info != NULL);
190 struct echo_object_conf *cl2echo_conf(const struct cl_object_conf *c)
192 return container_of(c, struct echo_object_conf, eoc_cl);
195 #ifdef HAVE_SERVER_SUPPORT
196 static inline struct echo_md_device *lu2emd_dev(struct lu_device *d)
198 return container_of0(d, struct echo_md_device, emd_md_dev.md_lu_dev);
201 static inline struct lu_device *emd2lu_dev(struct echo_md_device *d)
203 return &d->emd_md_dev.md_lu_dev;
206 static inline struct seq_server_site *echo_md_seq_site(struct echo_md_device *d)
208 return emd2lu_dev(d)->ld_site->ld_seq_site;
211 static inline struct obd_device *emd2obd_dev(struct echo_md_device *d)
213 return d->emd_md_dev.md_lu_dev.ld_obd;
215 #endif /* HAVE_SERVER_SUPPORT */
217 /** @} echo_helpers */
219 static int cl_echo_object_put(struct echo_object *eco);
220 static int cl_echo_object_brw(struct echo_object *eco, int rw, u64 offset,
221 struct page **pages, int npages, int async);
223 struct echo_thread_info {
224 struct echo_object_conf eti_conf;
225 struct lustre_md eti_md;
226 struct cl_2queue eti_queue;
228 struct cl_lock eti_lock;
229 struct lu_fid eti_fid;
230 struct lu_fid eti_fid2;
231 #ifdef HAVE_SERVER_SUPPORT
232 struct md_op_spec eti_spec;
233 struct lov_mds_md_v3 eti_lmm;
234 struct lov_user_md_v3 eti_lum;
235 struct md_attr eti_ma;
236 struct lu_name eti_lname;
237 /* per-thread values, can be re-used */
238 void *eti_big_lmm; /* may be vmalloc'd */
240 char eti_name[ETI_NAME_LEN];
241 struct lu_buf eti_buf;
242 /* If we want to test large ACL, then need to enlarge the buffer. */
243 char eti_xattr_buf[LUSTRE_POSIX_ACL_MAX_SIZE_OLD];
247 /* No session used right now */
248 struct echo_session_info {
252 static struct kmem_cache *echo_lock_kmem;
253 static struct kmem_cache *echo_object_kmem;
254 static struct kmem_cache *echo_thread_kmem;
255 static struct kmem_cache *echo_session_kmem;
256 /* static struct kmem_cache *echo_req_kmem; */
258 static struct lu_kmem_descr echo_caches[] = {
260 .ckd_cache = &echo_lock_kmem,
261 .ckd_name = "echo_lock_kmem",
262 .ckd_size = sizeof(struct echo_lock)
265 .ckd_cache = &echo_object_kmem,
266 .ckd_name = "echo_object_kmem",
267 .ckd_size = sizeof(struct echo_object)
270 .ckd_cache = &echo_thread_kmem,
271 .ckd_name = "echo_thread_kmem",
272 .ckd_size = sizeof(struct echo_thread_info)
275 .ckd_cache = &echo_session_kmem,
276 .ckd_name = "echo_session_kmem",
277 .ckd_size = sizeof(struct echo_session_info)
284 /** \defgroup echo_page Page operations
286 * Echo page operations.
290 static int echo_page_own(const struct lu_env *env,
291 const struct cl_page_slice *slice,
292 struct cl_io *io, int nonblock)
294 struct echo_page *ep = cl2echo_page(slice);
297 if (test_and_set_bit(0, &ep->ep_lock))
300 while (test_and_set_bit(0, &ep->ep_lock))
301 wait_on_bit(&ep->ep_lock, 0, TASK_UNINTERRUPTIBLE);
306 static void echo_page_disown(const struct lu_env *env,
307 const struct cl_page_slice *slice,
310 struct echo_page *ep = cl2echo_page(slice);
312 LASSERT(test_bit(0, &ep->ep_lock));
313 clear_and_wake_up_bit(0, &ep->ep_lock);
316 static void echo_page_discard(const struct lu_env *env,
317 const struct cl_page_slice *slice,
318 struct cl_io *unused)
320 cl_page_delete(env, slice->cpl_page);
323 static int echo_page_is_vmlocked(const struct lu_env *env,
324 const struct cl_page_slice *slice)
326 if (test_bit(0, &cl2echo_page(slice)->ep_lock))
331 static void echo_page_completion(const struct lu_env *env,
332 const struct cl_page_slice *slice,
335 LASSERT(slice->cpl_page->cp_sync_io != NULL);
338 static void echo_page_fini(const struct lu_env *env,
339 struct cl_page_slice *slice,
340 struct pagevec *pvec)
342 struct echo_object *eco = cl2echo_obj(slice->cpl_obj);
345 atomic_dec(&eco->eo_npages);
346 put_page(slice->cpl_page->cp_vmpage);
350 static int echo_page_prep(const struct lu_env *env,
351 const struct cl_page_slice *slice,
352 struct cl_io *unused)
357 static int echo_page_print(const struct lu_env *env,
358 const struct cl_page_slice *slice,
359 void *cookie, lu_printer_t printer)
361 struct echo_page *ep = cl2echo_page(slice);
363 (*printer)(env, cookie, LUSTRE_ECHO_CLIENT_NAME"-page@%p %d vm@%p\n",
364 ep, test_bit(0, &ep->ep_lock),
365 slice->cpl_page->cp_vmpage);
369 static const struct cl_page_operations echo_page_ops = {
370 .cpo_own = echo_page_own,
371 .cpo_disown = echo_page_disown,
372 .cpo_discard = echo_page_discard,
373 .cpo_fini = echo_page_fini,
374 .cpo_print = echo_page_print,
375 .cpo_is_vmlocked = echo_page_is_vmlocked,
378 .cpo_prep = echo_page_prep,
379 .cpo_completion = echo_page_completion,
382 .cpo_prep = echo_page_prep,
383 .cpo_completion = echo_page_completion,
390 /** \defgroup echo_lock Locking
392 * echo lock operations
396 static void echo_lock_fini(const struct lu_env *env,
397 struct cl_lock_slice *slice)
399 struct echo_lock *ecl = cl2echo_lock(slice);
401 LASSERT(list_empty(&ecl->el_chain));
402 OBD_SLAB_FREE_PTR(ecl, echo_lock_kmem);
405 static struct cl_lock_operations echo_lock_ops = {
406 .clo_fini = echo_lock_fini,
411 /** \defgroup echo_cl_ops cl_object operations
413 * operations for cl_object
417 static int echo_page_init(const struct lu_env *env, struct cl_object *obj,
418 struct cl_page *page, pgoff_t index)
420 struct echo_page *ep = cl_object_page_slice(obj, page);
421 struct echo_object *eco = cl2echo_obj(obj);
424 get_page(page->cp_vmpage);
426 * ep_lock is similar to the lock_page() lock, and
427 * cannot usefully be monitored by lockdep.
428 * So just use a bit in an "unsigned long" and use the
429 * wait_on_bit() interface to wait for the bit to be clear.
432 cl_page_slice_add(page, &ep->ep_cl, obj, index, &echo_page_ops);
433 atomic_inc(&eco->eo_npages);
437 static int echo_io_init(const struct lu_env *env, struct cl_object *obj,
443 static int echo_lock_init(const struct lu_env *env,
444 struct cl_object *obj, struct cl_lock *lock,
445 const struct cl_io *unused)
447 struct echo_lock *el;
450 OBD_SLAB_ALLOC_PTR_GFP(el, echo_lock_kmem, GFP_NOFS);
452 cl_lock_slice_add(lock, &el->el_cl, obj, &echo_lock_ops);
453 el->el_object = cl2echo_obj(obj);
454 INIT_LIST_HEAD(&el->el_chain);
455 atomic_set(&el->el_refcount, 0);
457 RETURN(el ? 0 : -ENOMEM);
460 static int echo_conf_set(const struct lu_env *env, struct cl_object *obj,
461 const struct cl_object_conf *conf)
466 static const struct cl_object_operations echo_cl_obj_ops = {
467 .coo_page_init = echo_page_init,
468 .coo_lock_init = echo_lock_init,
469 .coo_io_init = echo_io_init,
470 .coo_conf_set = echo_conf_set
472 /** @} echo_cl_ops */
474 /** \defgroup echo_lu_ops lu_object operations
476 * operations for echo lu object.
480 static int echo_object_init(const struct lu_env *env, struct lu_object *obj,
481 const struct lu_object_conf *conf)
483 struct echo_device *ed = cl2echo_dev(lu2cl_dev(obj->lo_dev));
484 struct echo_client_obd *ec = ed->ed_ec;
485 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
489 struct lu_object *below;
490 struct lu_device *under;
493 below = under->ld_ops->ldo_object_alloc(env, obj->lo_header,
497 lu_object_add(obj, below);
500 if (!ed->ed_next_ismd) {
501 const struct cl_object_conf *cconf = lu2cl_conf(conf);
502 struct echo_object_conf *econf = cl2echo_conf(cconf);
504 LASSERT(econf->eoc_oinfo != NULL);
507 * Transfer the oinfo pointer to eco that it won't be
510 eco->eo_oinfo = *econf->eoc_oinfo;
511 *econf->eoc_oinfo = NULL;
513 eco->eo_oinfo = NULL;
517 atomic_set(&eco->eo_npages, 0);
518 cl_object_page_init(lu2cl(obj), sizeof(struct echo_page));
520 spin_lock(&ec->ec_lock);
521 list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
522 spin_unlock(&ec->ec_lock);
527 static void echo_object_free(const struct lu_env *env, struct lu_object *obj)
529 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
530 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
533 LASSERT(atomic_read(&eco->eo_npages) == 0);
535 spin_lock(&ec->ec_lock);
536 list_del_init(&eco->eo_obj_chain);
537 spin_unlock(&ec->ec_lock);
540 lu_object_header_fini(obj->lo_header);
543 OBD_FREE_PTR(eco->eo_oinfo);
545 OBD_SLAB_FREE_PTR(eco, echo_object_kmem);
549 static int echo_object_print(const struct lu_env *env, void *cookie,
550 lu_printer_t p, const struct lu_object *o)
552 struct echo_object *obj = cl2echo_obj(lu2cl(o));
554 return (*p)(env, cookie, "echoclient-object@%p", obj);
557 static const struct lu_object_operations echo_lu_obj_ops = {
558 .loo_object_init = echo_object_init,
559 .loo_object_delete = NULL,
560 .loo_object_release = NULL,
561 .loo_object_free = echo_object_free,
562 .loo_object_print = echo_object_print,
563 .loo_object_invariant = NULL
565 /** @} echo_lu_ops */
567 /** \defgroup echo_lu_dev_ops lu_device operations
569 * Operations for echo lu device.
573 static struct lu_object *echo_object_alloc(const struct lu_env *env,
574 const struct lu_object_header *hdr,
575 struct lu_device *dev)
577 struct echo_object *eco;
578 struct lu_object *obj = NULL;
581 /* we're the top dev. */
582 LASSERT(hdr == NULL);
583 OBD_SLAB_ALLOC_PTR_GFP(eco, echo_object_kmem, GFP_NOFS);
585 struct cl_object_header *hdr = &eco->eo_hdr;
587 obj = &echo_obj2cl(eco)->co_lu;
588 cl_object_header_init(hdr);
589 hdr->coh_page_bufsize = cfs_size_round(sizeof(struct cl_page));
591 lu_object_init(obj, &hdr->coh_lu, dev);
592 lu_object_add_top(&hdr->coh_lu, obj);
594 eco->eo_cl.co_ops = &echo_cl_obj_ops;
595 obj->lo_ops = &echo_lu_obj_ops;
600 static struct lu_device_operations echo_device_lu_ops = {
601 .ldo_object_alloc = echo_object_alloc,
604 /** @} echo_lu_dev_ops */
606 /** \defgroup echo_init Setup and teardown
608 * Init and fini functions for echo client.
612 static int echo_site_init(const struct lu_env *env, struct echo_device *ed)
614 struct cl_site *site = &ed->ed_site_myself;
617 /* initialize site */
618 rc = cl_site_init(site, &ed->ed_cl);
620 CERROR("Cannot initialize site for echo client(%d)\n", rc);
624 rc = lu_site_init_finish(&site->cs_lu);
630 ed->ed_site = &site->cs_lu;
634 static void echo_site_fini(const struct lu_env *env, struct echo_device *ed)
637 if (!ed->ed_next_ismd)
638 lu_site_fini(ed->ed_site);
643 static void *echo_thread_key_init(const struct lu_context *ctx,
644 struct lu_context_key *key)
646 struct echo_thread_info *info;
648 OBD_SLAB_ALLOC_PTR_GFP(info, echo_thread_kmem, GFP_NOFS);
650 info = ERR_PTR(-ENOMEM);
654 static void echo_thread_key_fini(const struct lu_context *ctx,
655 struct lu_context_key *key, void *data)
657 struct echo_thread_info *info = data;
659 OBD_SLAB_FREE_PTR(info, echo_thread_kmem);
662 static struct lu_context_key echo_thread_key = {
663 .lct_tags = LCT_CL_THREAD,
664 .lct_init = echo_thread_key_init,
665 .lct_fini = echo_thread_key_fini,
668 static void *echo_session_key_init(const struct lu_context *ctx,
669 struct lu_context_key *key)
671 struct echo_session_info *session;
673 OBD_SLAB_ALLOC_PTR_GFP(session, echo_session_kmem, GFP_NOFS);
675 session = ERR_PTR(-ENOMEM);
679 static void echo_session_key_fini(const struct lu_context *ctx,
680 struct lu_context_key *key, void *data)
682 struct echo_session_info *session = data;
684 OBD_SLAB_FREE_PTR(session, echo_session_kmem);
687 static struct lu_context_key echo_session_key = {
688 .lct_tags = LCT_SESSION,
689 .lct_init = echo_session_key_init,
690 .lct_fini = echo_session_key_fini,
693 LU_TYPE_INIT_FINI(echo, &echo_thread_key, &echo_session_key);
695 #ifdef HAVE_SERVER_SUPPORT
696 # define ECHO_SEQ_WIDTH 0xffffffff
697 static int echo_fid_init(struct echo_device *ed, char *obd_name,
698 struct seq_server_site *ss)
704 OBD_ALLOC_PTR(ed->ed_cl_seq);
708 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
710 GOTO(out_free_seq, rc = -ENOMEM);
712 snprintf(prefix, MAX_OBD_NAME + 5, "srv-%s", obd_name);
714 /* Init client side sequence-manager */
715 rc = seq_client_init(ed->ed_cl_seq, NULL,
717 prefix, ss->ss_server_seq);
718 ed->ed_cl_seq->lcs_width = ECHO_SEQ_WIDTH;
719 OBD_FREE(prefix, MAX_OBD_NAME + 5);
721 GOTO(out_free_seq, rc);
726 OBD_FREE_PTR(ed->ed_cl_seq);
727 ed->ed_cl_seq = NULL;
731 static int echo_fid_fini(struct obd_device *obddev)
733 struct echo_device *ed = obd2echo_dev(obddev);
737 seq_client_fini(ed->ed_cl_seq);
738 OBD_FREE_PTR(ed->ed_cl_seq);
739 ed->ed_cl_seq = NULL;
745 static void echo_ed_los_fini(const struct lu_env *env, struct echo_device *ed)
748 if (ed != NULL && ed->ed_next_ismd && ed->ed_los != NULL) {
749 local_oid_storage_fini(env, ed->ed_los);
755 echo_md_local_file_create(const struct lu_env *env, struct echo_md_device *emd,
756 struct local_oid_storage *los,
757 const struct lu_fid *pfid, const char *name,
758 __u32 mode, struct lu_fid *fid)
760 struct dt_object *parent = NULL;
761 struct dt_object *dto = NULL;
765 LASSERT(!fid_is_zero(pfid));
766 parent = dt_locate(env, emd->emd_bottom, pfid);
767 if (unlikely(IS_ERR(parent)))
768 RETURN(PTR_ERR(parent));
770 /* create local file with @fid */
771 dto = local_file_find_or_create_with_fid(env, emd->emd_bottom, fid,
774 GOTO(out_put, rc = PTR_ERR(dto));
776 *fid = *lu_object_fid(&dto->do_lu);
778 * since stack is not fully set up the local_storage uses own stack
779 * and we should drop its object from cache
781 dt_object_put_nocache(env, dto);
785 dt_object_put(env, parent);
790 echo_md_root_get(const struct lu_env *env, struct echo_md_device *emd,
791 struct echo_device *ed)
797 /* Setup local dirs */
798 fid.f_seq = FID_SEQ_LOCAL_NAME;
801 rc = local_oid_storage_init(env, emd->emd_bottom, &fid, &ed->ed_los);
805 lu_echo_root_fid(&fid);
806 if (echo_md_seq_site(emd)->ss_node_id == 0) {
807 rc = echo_md_local_file_create(env, emd, ed->ed_los,
808 &emd->emd_local_root_fid,
809 echo_md_root_dir_name, S_IFDIR |
810 S_IRUGO | S_IWUSR | S_IXUGO,
813 CERROR("%s: create md echo root fid failed: rc = %d\n",
814 emd2obd_dev(emd)->obd_name, rc);
818 ed->ed_root_fid = fid;
822 echo_ed_los_fini(env, ed);
826 #endif /* HAVE_SERVER_SUPPORT */
828 static struct lu_device *echo_device_alloc(const struct lu_env *env,
829 struct lu_device_type *t,
830 struct lustre_cfg *cfg)
832 struct lu_device *next;
833 struct echo_device *ed;
834 struct cl_device *cd;
835 struct obd_device *obd = NULL; /* to keep compiler happy */
836 struct obd_device *tgt;
837 const char *tgt_type_name;
844 GOTO(out, rc = -ENOMEM);
848 rc = cl_device_init(cd, t);
852 cd->cd_lu_dev.ld_ops = &echo_device_lu_ops;
855 obd = class_name2obd(lustre_cfg_string(cfg, 0));
856 LASSERT(obd != NULL);
857 LASSERT(env != NULL);
859 tgt = class_name2obd(lustre_cfg_string(cfg, 1));
861 CERROR("Can not find tgt device %s\n",
862 lustre_cfg_string(cfg, 1));
863 GOTO(out, rc = -ENODEV);
866 next = tgt->obd_lu_dev;
868 if (strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME) == 0) {
869 ed->ed_next_ismd = 1;
870 } else if (strcmp(tgt->obd_type->typ_name, LUSTRE_OST_NAME) == 0 ||
871 strcmp(tgt->obd_type->typ_name, LUSTRE_OSC_NAME) == 0) {
872 ed->ed_next_ismd = 0;
873 rc = echo_site_init(env, ed);
877 GOTO(out, rc = -EINVAL);
882 rc = echo_client_setup(env, obd, cfg);
886 ed->ed_ec = &obd->u.echo_client;
889 if (ed->ed_next_ismd) {
890 #ifdef HAVE_SERVER_SUPPORT
891 /* Suppose to connect to some Metadata layer */
892 struct lu_site *ls = NULL;
893 struct lu_device *ld = NULL;
894 struct md_device *md = NULL;
895 struct echo_md_device *emd = NULL;
899 CERROR("%s is not lu device type!\n",
900 lustre_cfg_string(cfg, 1));
901 GOTO(out, rc = -EINVAL);
904 tgt_type_name = lustre_cfg_string(cfg, 2);
905 if (!tgt_type_name) {
906 CERROR("%s no type name for echo %s setup\n",
907 lustre_cfg_string(cfg, 1),
908 tgt->obd_type->typ_name);
909 GOTO(out, rc = -EINVAL);
914 spin_lock(&ls->ls_ld_lock);
915 list_for_each_entry(ld, &ls->ls_ld_linkage, ld_linkage) {
916 if (strcmp(ld->ld_type->ldt_name, tgt_type_name) == 0) {
921 spin_unlock(&ls->ls_ld_lock);
924 CERROR("%s is not lu device type!\n",
925 lustre_cfg_string(cfg, 1));
926 GOTO(out, rc = -EINVAL);
930 /* For MD echo client, it will use the site in MDS stack */
932 ed->ed_cl.cd_lu_dev.ld_site = ls;
933 rc = echo_fid_init(ed, obd->obd_name, lu_site2seq(ls));
935 CERROR("echo fid init error %d\n", rc);
939 md = lu2md_dev(next);
940 emd = lu2emd_dev(&md->md_lu_dev);
941 rc = echo_md_root_get(env, emd, ed);
943 CERROR("%s: get root error: rc = %d\n",
944 emd2obd_dev(emd)->obd_name, rc);
947 #else /* !HAVE_SERVER_SUPPORT */
949 "Local operations are NOT supported on client side. Only remote operations are supported. Metadata client must be run on server side.\n");
950 GOTO(out, rc = -EOPNOTSUPP);
951 #endif /* HAVE_SERVER_SUPPORT */
954 * if echo client is to be stacked upon ost device, the next is
955 * NULL since ost is not a clio device so far
957 if (next != NULL && !lu_device_is_cl(next))
960 tgt_type_name = tgt->obd_type->typ_name;
962 LASSERT(next != NULL);
964 GOTO(out, rc = -EBUSY);
966 next->ld_site = ed->ed_site;
967 rc = next->ld_type->ldt_ops->ldto_device_init(env, next,
968 next->ld_type->ldt_name,
973 LASSERT(strcmp(tgt_type_name, LUSTRE_OST_NAME) == 0);
978 RETURN(&cd->cd_lu_dev);
984 rc2 = echo_client_cleanup(obd);
986 CERROR("Cleanup obd device %s error(%d)\n",
991 echo_site_fini(env, ed);
993 cl_device_fini(&ed->ed_cl);
1003 static int echo_device_init(const struct lu_env *env, struct lu_device *d,
1004 const char *name, struct lu_device *next)
1010 static struct lu_device *echo_device_fini(const struct lu_env *env,
1011 struct lu_device *d)
1013 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
1014 struct lu_device *next = ed->ed_next;
1016 while (next && !ed->ed_next_ismd)
1017 next = next->ld_type->ldt_ops->ldto_device_fini(env, next);
1021 static void echo_lock_release(const struct lu_env *env,
1022 struct echo_lock *ecl,
1025 struct cl_lock *clk = echo_lock2cl(ecl);
1027 cl_lock_release(env, clk);
1030 static struct lu_device *echo_device_free(const struct lu_env *env,
1031 struct lu_device *d)
1033 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
1034 struct echo_client_obd *ec = ed->ed_ec;
1035 struct echo_object *eco;
1036 struct lu_device *next = ed->ed_next;
1038 CDEBUG(D_INFO, "echo device:%p is going to be freed, next = %p\n",
1041 lu_site_purge(env, ed->ed_site, -1);
1044 * check if there are objects still alive.
1045 * It shouldn't have any object because lu_site_purge would cleanup
1046 * all of cached objects. Anyway, probably the echo device is being
1047 * parallelly accessed.
1049 spin_lock(&ec->ec_lock);
1050 list_for_each_entry(eco, &ec->ec_objects, eo_obj_chain)
1051 eco->eo_deleted = 1;
1052 spin_unlock(&ec->ec_lock);
1055 lu_site_purge(env, ed->ed_site, -1);
1058 "Waiting for the reference of echo object to be dropped\n");
1060 /* Wait for the last reference to be dropped. */
1061 spin_lock(&ec->ec_lock);
1062 while (!list_empty(&ec->ec_objects)) {
1063 spin_unlock(&ec->ec_lock);
1065 "echo_client still has objects at cleanup time, wait for 1 second\n");
1066 set_current_state(TASK_UNINTERRUPTIBLE);
1067 schedule_timeout(cfs_time_seconds(1));
1068 lu_site_purge(env, ed->ed_site, -1);
1069 spin_lock(&ec->ec_lock);
1071 spin_unlock(&ec->ec_lock);
1073 LASSERT(list_empty(&ec->ec_locks));
1075 CDEBUG(D_INFO, "No object exists, exiting...\n");
1077 echo_client_cleanup(d->ld_obd);
1078 #ifdef HAVE_SERVER_SUPPORT
1079 echo_fid_fini(d->ld_obd);
1080 echo_ed_los_fini(env, ed);
1082 while (next && !ed->ed_next_ismd)
1083 next = next->ld_type->ldt_ops->ldto_device_free(env, next);
1085 LASSERT(ed->ed_site == d->ld_site);
1086 echo_site_fini(env, ed);
1087 cl_device_fini(&ed->ed_cl);
1090 cl_env_cache_purge(~0);
1095 static const struct lu_device_type_operations echo_device_type_ops = {
1096 .ldto_init = echo_type_init,
1097 .ldto_fini = echo_type_fini,
1099 .ldto_start = echo_type_start,
1100 .ldto_stop = echo_type_stop,
1102 .ldto_device_alloc = echo_device_alloc,
1103 .ldto_device_free = echo_device_free,
1104 .ldto_device_init = echo_device_init,
1105 .ldto_device_fini = echo_device_fini
1108 static struct lu_device_type echo_device_type = {
1109 .ldt_tags = LU_DEVICE_CL,
1110 .ldt_name = LUSTRE_ECHO_CLIENT_NAME,
1111 .ldt_ops = &echo_device_type_ops,
1112 .ldt_ctx_tags = LCT_CL_THREAD | LCT_MD_THREAD | LCT_DT_THREAD,
1116 /** \defgroup echo_exports Exported operations
1118 * exporting functions to echo client
1123 /* Interfaces to echo client obd device */
1124 static struct echo_object *
1125 cl_echo_object_find(struct echo_device *d, const struct ost_id *oi)
1128 struct echo_thread_info *info;
1129 struct echo_object_conf *conf;
1130 struct echo_object *eco;
1131 struct cl_object *obj;
1132 struct lov_oinfo *oinfo = NULL;
1138 LASSERTF(ostid_id(oi) != 0, DOSTID"\n", POSTID(oi));
1139 LASSERTF(ostid_seq(oi) == FID_SEQ_ECHO, DOSTID"\n", POSTID(oi));
1141 /* Never return an object if the obd is to be freed. */
1142 if (echo_dev2cl(d)->cd_lu_dev.ld_obd->obd_stopping)
1143 RETURN(ERR_PTR(-ENODEV));
1145 env = cl_env_get(&refcheck);
1147 RETURN((void *)env);
1149 info = echo_env_info(env);
1150 conf = &info->eti_conf;
1152 OBD_ALLOC_PTR(oinfo);
1154 GOTO(out, eco = ERR_PTR(-ENOMEM));
1156 oinfo->loi_oi = *oi;
1157 conf->eoc_cl.u.coc_oinfo = oinfo;
1161 * If echo_object_init() is successful then ownership of oinfo
1162 * is transferred to the object.
1164 conf->eoc_oinfo = &oinfo;
1166 fid = &info->eti_fid;
1167 rc = ostid_to_fid(fid, oi, 0);
1169 GOTO(out, eco = ERR_PTR(rc));
1172 * In the function below, .hs_keycmp resolves to
1173 * lu_obj_hop_keycmp()
1175 /* coverity[overrun-buffer-val] */
1176 obj = cl_object_find(env, echo_dev2cl(d), fid, &conf->eoc_cl);
1178 GOTO(out, eco = (void *)obj);
1180 eco = cl2echo_obj(obj);
1181 if (eco->eo_deleted) {
1182 cl_object_put(env, obj);
1183 eco = ERR_PTR(-EAGAIN);
1188 OBD_FREE_PTR(oinfo);
1190 cl_env_put(env, &refcheck);
1194 static int cl_echo_object_put(struct echo_object *eco)
1197 struct cl_object *obj = echo_obj2cl(eco);
1201 env = cl_env_get(&refcheck);
1203 RETURN(PTR_ERR(env));
1205 /* an external function to kill an object? */
1206 if (eco->eo_deleted) {
1207 struct lu_object_header *loh = obj->co_lu.lo_header;
1209 LASSERT(&eco->eo_hdr == luh2coh(loh));
1210 set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
1213 cl_object_put(env, obj);
1214 cl_env_put(env, &refcheck);
1218 static int cl_echo_enqueue0(struct lu_env *env, struct echo_object *eco,
1219 u64 start, u64 end, int mode,
1220 __u64 *cookie, __u32 enqflags)
1223 struct cl_lock *lck;
1224 struct cl_object *obj;
1225 struct cl_lock_descr *descr;
1226 struct echo_thread_info *info;
1230 info = echo_env_info(env);
1232 lck = &info->eti_lock;
1233 obj = echo_obj2cl(eco);
1235 memset(lck, 0, sizeof(*lck));
1236 descr = &lck->cll_descr;
1237 descr->cld_obj = obj;
1238 descr->cld_start = cl_index(obj, start);
1239 descr->cld_end = cl_index(obj, end);
1240 descr->cld_mode = mode == LCK_PW ? CLM_WRITE : CLM_READ;
1241 descr->cld_enq_flags = enqflags;
1244 rc = cl_lock_request(env, io, lck);
1246 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
1247 struct echo_lock *el;
1249 el = cl2echo_lock(cl_lock_at(lck, &echo_device_type));
1250 spin_lock(&ec->ec_lock);
1251 if (list_empty(&el->el_chain)) {
1252 list_add(&el->el_chain, &ec->ec_locks);
1253 el->el_cookie = ++ec->ec_unique;
1255 atomic_inc(&el->el_refcount);
1256 *cookie = el->el_cookie;
1257 spin_unlock(&ec->ec_lock);
1262 static int cl_echo_cancel0(struct lu_env *env, struct echo_device *ed,
1265 struct echo_client_obd *ec = ed->ed_ec;
1266 struct echo_lock *ecl = NULL;
1267 struct list_head *el;
1268 int found = 0, still_used = 0;
1271 LASSERT(ec != NULL);
1272 spin_lock(&ec->ec_lock);
1273 list_for_each(el, &ec->ec_locks) {
1274 ecl = list_entry(el, struct echo_lock, el_chain);
1275 CDEBUG(D_INFO, "ecl: %p, cookie: %#llx\n", ecl, ecl->el_cookie);
1276 found = (ecl->el_cookie == cookie);
1278 if (atomic_dec_and_test(&ecl->el_refcount))
1279 list_del_init(&ecl->el_chain);
1285 spin_unlock(&ec->ec_lock);
1290 echo_lock_release(env, ecl, still_used);
1294 static void echo_commit_callback(const struct lu_env *env, struct cl_io *io,
1295 struct cl_page *page)
1297 struct echo_thread_info *info;
1298 struct cl_2queue *queue;
1300 info = echo_env_info(env);
1301 LASSERT(io == &info->eti_io);
1303 queue = &info->eti_queue;
1304 cl_page_list_add(&queue->c2_qout, page);
1307 static int cl_echo_object_brw(struct echo_object *eco, int rw, u64 offset,
1308 struct page **pages, int npages, int async)
1311 struct echo_thread_info *info;
1312 struct cl_object *obj = echo_obj2cl(eco);
1313 struct echo_device *ed = eco->eo_dev;
1314 struct cl_2queue *queue;
1316 struct cl_page *clp;
1317 struct lustre_handle lh = { 0 };
1318 int page_size = cl_page_size(obj);
1324 LASSERT((offset & ~PAGE_MASK) == 0);
1325 LASSERT(ed->ed_next != NULL);
1326 env = cl_env_get(&refcheck);
1328 RETURN(PTR_ERR(env));
1330 info = echo_env_info(env);
1332 queue = &info->eti_queue;
1334 cl_2queue_init(queue);
1336 io->ci_ignore_layout = 1;
1337 rc = cl_io_init(env, io, CIT_MISC, obj);
1342 rc = cl_echo_enqueue0(env, eco, offset,
1343 offset + npages * PAGE_SIZE - 1,
1344 rw == READ ? LCK_PR : LCK_PW, &lh.cookie,
1347 GOTO(error_lock, rc);
1349 for (i = 0; i < npages; i++) {
1351 clp = cl_page_find(env, obj, cl_index(obj, offset),
1352 pages[i], CPT_TRANSIENT);
1357 LASSERT(clp->cp_type == CPT_TRANSIENT);
1359 rc = cl_page_own(env, io, clp);
1361 LASSERT(clp->cp_state == CPS_FREEING);
1362 cl_page_put(env, clp);
1366 cl_2queue_add(queue, clp);
1369 * drop the reference count for cl_page_find, so that the page
1370 * will be freed in cl_2queue_fini.
1372 cl_page_put(env, clp);
1373 cl_page_clip(env, clp, 0, page_size);
1375 offset += page_size;
1379 enum cl_req_type typ = rw == READ ? CRT_READ : CRT_WRITE;
1381 async = async && (typ == CRT_WRITE);
1383 rc = cl_io_commit_async(env, io, &queue->c2_qin,
1385 echo_commit_callback);
1387 rc = cl_io_submit_sync(env, io, typ, queue, 0);
1388 CDEBUG(D_INFO, "echo_client %s write returns %d\n",
1389 async ? "async" : "sync", rc);
1392 cl_echo_cancel0(env, ed, lh.cookie);
1395 cl_2queue_discard(env, io, queue);
1396 cl_2queue_disown(env, io, queue);
1397 cl_2queue_fini(env, queue);
1398 cl_io_fini(env, io);
1400 cl_env_put(env, &refcheck);
1403 /** @} echo_exports */
1405 static u64 last_object_id;
1407 #ifdef HAVE_SERVER_SUPPORT
1408 static inline void echo_md_build_name(struct lu_name *lname, char *name,
1411 snprintf(name, ETI_NAME_LEN, "%llu", id);
1412 lname->ln_name = name;
1413 lname->ln_namelen = strlen(name);
1416 /* similar to mdt_attr_get_complex */
1417 static int echo_big_lmm_get(const struct lu_env *env, struct md_object *o,
1420 struct echo_thread_info *info = echo_env_info(env);
1425 LASSERT(ma->ma_lmm_size > 0);
1427 LASSERT(ma->ma_need & (MA_LOV | MA_LMV));
1428 if (ma->ma_need & MA_LOV)
1429 rc = mo_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LOV);
1431 rc = mo_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LMV);
1436 /* big_lmm may need to be grown */
1437 if (info->eti_big_lmmsize < rc) {
1438 int size = size_roundup_power2(rc);
1440 if (info->eti_big_lmmsize > 0) {
1441 /* free old buffer */
1442 LASSERT(info->eti_big_lmm);
1443 OBD_FREE_LARGE(info->eti_big_lmm,
1444 info->eti_big_lmmsize);
1445 info->eti_big_lmm = NULL;
1446 info->eti_big_lmmsize = 0;
1449 OBD_ALLOC_LARGE(info->eti_big_lmm, size);
1450 if (!info->eti_big_lmm)
1452 info->eti_big_lmmsize = size;
1454 LASSERT(info->eti_big_lmmsize >= rc);
1456 info->eti_buf.lb_buf = info->eti_big_lmm;
1457 info->eti_buf.lb_len = info->eti_big_lmmsize;
1458 if (ma->ma_need & MA_LOV)
1459 rc = mo_xattr_get(env, o, &info->eti_buf, XATTR_NAME_LOV);
1461 rc = mo_xattr_get(env, o, &info->eti_buf, XATTR_NAME_LMV);
1465 if (ma->ma_need & MA_LOV)
1466 ma->ma_valid |= MA_LOV;
1468 ma->ma_valid |= MA_LMV;
1470 ma->ma_lmm = info->eti_big_lmm;
1471 ma->ma_lmm_size = rc;
1476 static int echo_attr_get_complex(const struct lu_env *env,
1477 struct md_object *next,
1480 struct echo_thread_info *info = echo_env_info(env);
1481 struct lu_buf *buf = &info->eti_buf;
1482 umode_t mode = lu_object_attr(&next->mo_lu);
1489 if (ma->ma_need & MA_INODE) {
1490 rc = mo_attr_get(env, next, ma);
1493 ma->ma_valid |= MA_INODE;
1496 if ((ma->ma_need & MA_LOV) && (S_ISREG(mode) || S_ISDIR(mode))) {
1497 LASSERT(ma->ma_lmm_size > 0);
1498 buf->lb_buf = ma->ma_lmm;
1499 buf->lb_len = ma->ma_lmm_size;
1500 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LOV);
1502 ma->ma_lmm_size = rc2;
1503 ma->ma_valid |= MA_LOV;
1504 } else if (rc2 == -ENODATA) {
1506 ma->ma_lmm_size = 0;
1507 } else if (rc2 == -ERANGE) {
1508 rc2 = echo_big_lmm_get(env, next, ma);
1510 GOTO(out, rc = rc2);
1512 GOTO(out, rc = rc2);
1516 if ((ma->ma_need & MA_LMV) && S_ISDIR(mode)) {
1517 LASSERT(ma->ma_lmm_size > 0);
1518 buf->lb_buf = ma->ma_lmm;
1519 buf->lb_len = ma->ma_lmm_size;
1520 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LMV);
1522 ma->ma_lmm_size = rc2;
1523 ma->ma_valid |= MA_LMV;
1524 } else if (rc2 == -ENODATA) {
1526 ma->ma_lmm_size = 0;
1527 } else if (rc2 == -ERANGE) {
1528 rc2 = echo_big_lmm_get(env, next, ma);
1530 GOTO(out, rc = rc2);
1532 GOTO(out, rc = rc2);
1536 #ifdef CONFIG_FS_POSIX_ACL
1537 if ((ma->ma_need & MA_ACL_DEF) && S_ISDIR(mode)) {
1538 buf->lb_buf = ma->ma_acl;
1539 buf->lb_len = ma->ma_acl_size;
1540 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1542 ma->ma_acl_size = rc2;
1543 ma->ma_valid |= MA_ACL_DEF;
1544 } else if (rc2 == -ENODATA) {
1546 ma->ma_acl_size = 0;
1548 GOTO(out, rc = rc2);
1553 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = %#llx ma_lmm=%p\n",
1554 rc, ma->ma_valid, ma->ma_lmm);
1559 echo_md_create_internal(const struct lu_env *env, struct echo_device *ed,
1560 struct md_object *parent, struct lu_fid *fid,
1561 struct lu_name *lname, struct md_op_spec *spec,
1564 struct lu_object *ec_child, *child;
1565 struct lu_device *ld = ed->ed_next;
1566 struct echo_thread_info *info = echo_env_info(env);
1567 struct lu_fid *fid2 = &info->eti_fid2;
1568 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
1573 rc = mdo_lookup(env, parent, lname, fid2, spec);
1576 else if (rc != -ENOENT)
1579 ec_child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev,
1581 if (IS_ERR(ec_child)) {
1582 CERROR("Can not find the child "DFID": rc = %ld\n", PFID(fid),
1584 RETURN(PTR_ERR(ec_child));
1587 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1589 CERROR("Can not locate the child "DFID"\n", PFID(fid));
1590 GOTO(out_put, rc = -EINVAL);
1593 CDEBUG(D_RPCTRACE, "Start creating object "DFID" %s %p\n",
1594 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1597 * Do not perform lookup sanity check. We know that name does not exist.
1599 spec->sp_cr_lookup = 0;
1600 rc = mdo_create(env, parent, lname, lu2md(child), spec, ma);
1602 CERROR("Can not create child "DFID": rc = %d\n", PFID(fid), rc);
1605 CDEBUG(D_RPCTRACE, "End creating object "DFID" %s %p rc = %d\n",
1606 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent, rc);
1609 lu_object_put(env, ec_child);
1613 static int echo_set_lmm_size(const struct lu_env *env, struct lu_device *ld,
1616 struct echo_thread_info *info = echo_env_info(env);
1618 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
1619 ma->ma_lmm = (void *)&info->eti_lmm;
1620 ma->ma_lmm_size = sizeof(info->eti_lmm);
1622 LASSERT(info->eti_big_lmmsize);
1623 ma->ma_lmm = info->eti_big_lmm;
1624 ma->ma_lmm_size = info->eti_big_lmmsize;
1631 echo_md_dir_stripe_choose(const struct lu_env *env, struct echo_device *ed,
1632 struct lu_object *obj, const char *name,
1633 unsigned int namelen, __u64 id,
1634 struct lu_object **new_parent)
1636 struct echo_thread_info *info = echo_env_info(env);
1637 struct md_attr *ma = &info->eti_ma;
1638 struct lmv_mds_md_v1 *lmv;
1639 struct lu_device *ld = ed->ed_next;
1641 struct lu_name tmp_ln_name;
1642 struct lu_fid stripe_fid;
1643 struct lu_object *stripe_obj;
1646 LASSERT(obj != NULL);
1647 LASSERT(S_ISDIR(obj->lo_header->loh_attr));
1649 memset(ma, 0, sizeof(*ma));
1650 echo_set_lmm_size(env, ld, ma);
1651 ma->ma_need = MA_LMV;
1652 rc = echo_attr_get_complex(env, lu2md(obj), ma);
1654 CERROR("Can not getattr child "DFID": rc = %d\n",
1655 PFID(lu_object_fid(obj)), rc);
1659 if (!(ma->ma_valid & MA_LMV)) {
1664 lmv = (struct lmv_mds_md_v1 *)ma->ma_lmm;
1665 if (le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_V1) {
1667 CERROR("Invalid mds md magic %x "DFID": rc = %d\n",
1668 le32_to_cpu(lmv->lmv_magic), PFID(lu_object_fid(obj)),
1674 tmp_ln_name.ln_name = name;
1675 tmp_ln_name.ln_namelen = namelen;
1678 echo_md_build_name(&tmp_ln_name, info->eti_name, id);
1681 idx = lmv_name_to_stripe_index(LMV_HASH_TYPE_FNV_1A_64,
1682 le32_to_cpu(lmv->lmv_stripe_count),
1683 tmp_ln_name.ln_name, tmp_ln_name.ln_namelen);
1685 LASSERT(idx < le32_to_cpu(lmv->lmv_stripe_count));
1686 fid_le_to_cpu(&stripe_fid, &lmv->lmv_stripe_fids[idx]);
1688 stripe_obj = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, &stripe_fid,
1690 if (IS_ERR(stripe_obj)) {
1691 rc = PTR_ERR(stripe_obj);
1692 CERROR("Can not find the parent "DFID": rc = %d\n",
1693 PFID(&stripe_fid), rc);
1697 *new_parent = lu_object_locate(stripe_obj->lo_header, ld->ld_type);
1699 lu_object_put(env, stripe_obj);
1706 static int echo_create_md_object(const struct lu_env *env,
1707 struct echo_device *ed,
1708 struct lu_object *ec_parent,
1710 char *name, int namelen,
1711 __u64 id, __u32 mode, int count,
1712 int stripe_count, int stripe_offset)
1714 struct lu_object *parent;
1715 struct lu_object *new_parent;
1716 struct echo_thread_info *info = echo_env_info(env);
1717 struct lu_name *lname = &info->eti_lname;
1718 struct md_op_spec *spec = &info->eti_spec;
1719 struct md_attr *ma = &info->eti_ma;
1720 struct lu_device *ld = ed->ed_next;
1728 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1732 rc = echo_md_dir_stripe_choose(env, ed, parent, name, namelen,
1737 LASSERT(new_parent != NULL);
1738 memset(ma, 0, sizeof(*ma));
1739 memset(spec, 0, sizeof(*spec));
1740 echo_set_lmm_size(env, ld, ma);
1741 if (stripe_count != 0) {
1742 spec->sp_cr_flags |= MDS_FMODE_WRITE;
1743 if (stripe_count != -1) {
1744 if (S_ISDIR(mode)) {
1745 struct lmv_user_md *lmu;
1747 lmu = (struct lmv_user_md *)&info->eti_lum;
1748 lmu->lum_magic = LMV_USER_MAGIC;
1749 lmu->lum_stripe_offset = stripe_offset;
1750 lmu->lum_stripe_count = stripe_count;
1751 lmu->lum_hash_type = LMV_HASH_TYPE_FNV_1A_64;
1752 spec->u.sp_ea.eadata = lmu;
1753 spec->u.sp_ea.eadatalen = sizeof(*lmu);
1755 struct lov_user_md_v3 *lum = &info->eti_lum;
1757 lum->lmm_magic = LOV_USER_MAGIC_V3;
1758 lum->lmm_stripe_count = stripe_count;
1759 lum->lmm_stripe_offset = stripe_offset;
1760 lum->lmm_pattern = LOV_PATTERN_NONE;
1761 spec->u.sp_ea.eadata = lum;
1762 spec->u.sp_ea.eadatalen = sizeof(*lum);
1764 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1768 ma->ma_attr.la_mode = mode;
1769 ma->ma_attr.la_valid = LA_CTIME | LA_MODE;
1770 ma->ma_attr.la_ctime = ktime_get_real_seconds();
1773 lname->ln_name = name;
1774 lname->ln_namelen = namelen;
1775 /* If name is specified, only create one object by name */
1776 rc = echo_md_create_internal(env, ed, lu2md(new_parent), fid,
1781 /* Create multiple object sequenced by id */
1782 for (i = 0; i < count; i++) {
1783 char *tmp_name = info->eti_name;
1785 echo_md_build_name(lname, tmp_name, id);
1787 rc = echo_md_create_internal(env, ed, lu2md(new_parent),
1788 fid, lname, spec, ma);
1790 CERROR("Can not create child %s: rc = %d\n", tmp_name,
1799 if (new_parent != parent)
1800 lu_object_put(env, new_parent);
1805 static struct lu_object *echo_md_lookup(const struct lu_env *env,
1806 struct echo_device *ed,
1807 struct md_object *parent,
1808 struct lu_name *lname)
1810 struct echo_thread_info *info = echo_env_info(env);
1811 struct lu_fid *fid = &info->eti_fid;
1812 struct lu_object *child;
1816 CDEBUG(D_INFO, "lookup %s in parent "DFID" %p\n", lname->ln_name,
1819 rc = mdo_lookup(env, parent, lname, fid, NULL);
1821 CERROR("lookup %s: rc = %d\n", lname->ln_name, rc);
1822 RETURN(ERR_PTR(rc));
1826 * In the function below, .hs_keycmp resolves to
1827 * lu_obj_hop_keycmp()
1829 /* coverity[overrun-buffer-val] */
1830 child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1835 static int echo_setattr_object(const struct lu_env *env,
1836 struct echo_device *ed,
1837 struct lu_object *ec_parent,
1838 __u64 id, int count)
1840 struct lu_object *parent;
1841 struct lu_object *new_parent;
1842 struct echo_thread_info *info = echo_env_info(env);
1843 struct lu_name *lname = &info->eti_lname;
1844 char *name = info->eti_name;
1845 struct lu_device *ld = ed->ed_next;
1846 struct lu_buf *buf = &info->eti_buf;
1854 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1858 rc = echo_md_dir_stripe_choose(env, ed, parent, NULL, 0, id,
1863 for (i = 0; i < count; i++) {
1864 struct lu_object *ec_child, *child;
1866 echo_md_build_name(lname, name, id);
1868 ec_child = echo_md_lookup(env, ed, lu2md(new_parent), lname);
1869 if (IS_ERR(ec_child)) {
1870 rc = PTR_ERR(ec_child);
1871 CERROR("Can't find child %s: rc = %d\n",
1872 lname->ln_name, rc);
1876 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1878 CERROR("Can not locate the child %s\n", lname->ln_name);
1879 lu_object_put(env, ec_child);
1884 CDEBUG(D_RPCTRACE, "Start setattr object "DFID"\n",
1885 PFID(lu_object_fid(child)));
1887 buf->lb_buf = info->eti_xattr_buf;
1888 buf->lb_len = sizeof(info->eti_xattr_buf);
1890 sprintf(name, "%s.test1", XATTR_USER_PREFIX);
1891 rc = mo_xattr_set(env, lu2md(child), buf, name,
1894 CERROR("Can not setattr child "DFID": rc = %d\n",
1895 PFID(lu_object_fid(child)), rc);
1896 lu_object_put(env, ec_child);
1899 CDEBUG(D_RPCTRACE, "End setattr object "DFID"\n",
1900 PFID(lu_object_fid(child)));
1902 lu_object_put(env, ec_child);
1905 if (new_parent != parent)
1906 lu_object_put(env, new_parent);
1911 static int echo_getattr_object(const struct lu_env *env,
1912 struct echo_device *ed,
1913 struct lu_object *ec_parent,
1914 __u64 id, int count)
1916 struct lu_object *parent;
1917 struct lu_object *new_parent;
1918 struct echo_thread_info *info = echo_env_info(env);
1919 struct lu_name *lname = &info->eti_lname;
1920 char *name = info->eti_name;
1921 struct md_attr *ma = &info->eti_ma;
1922 struct lu_device *ld = ed->ed_next;
1930 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1934 rc = echo_md_dir_stripe_choose(env, ed, parent, NULL, 0, id,
1939 memset(ma, 0, sizeof(*ma));
1940 ma->ma_need |= MA_INODE | MA_LOV | MA_PFID | MA_HSM | MA_ACL_DEF;
1941 ma->ma_acl = info->eti_xattr_buf;
1942 ma->ma_acl_size = sizeof(info->eti_xattr_buf);
1944 for (i = 0; i < count; i++) {
1945 struct lu_object *ec_child, *child;
1948 echo_md_build_name(lname, name, id);
1949 echo_set_lmm_size(env, ld, ma);
1951 ec_child = echo_md_lookup(env, ed, lu2md(new_parent), lname);
1952 if (IS_ERR(ec_child)) {
1953 CERROR("Can't find child %s: rc = %ld\n",
1954 lname->ln_name, PTR_ERR(ec_child));
1955 RETURN(PTR_ERR(ec_child));
1958 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1960 CERROR("Can not locate the child %s\n", lname->ln_name);
1961 lu_object_put(env, ec_child);
1965 CDEBUG(D_RPCTRACE, "Start getattr object "DFID"\n",
1966 PFID(lu_object_fid(child)));
1967 rc = echo_attr_get_complex(env, lu2md(child), ma);
1969 CERROR("Can not getattr child "DFID": rc = %d\n",
1970 PFID(lu_object_fid(child)), rc);
1971 lu_object_put(env, ec_child);
1974 CDEBUG(D_RPCTRACE, "End getattr object "DFID"\n",
1975 PFID(lu_object_fid(child)));
1977 lu_object_put(env, ec_child);
1980 if (new_parent != parent)
1981 lu_object_put(env, new_parent);
1986 static int echo_lookup_object(const struct lu_env *env,
1987 struct echo_device *ed,
1988 struct lu_object *ec_parent,
1989 __u64 id, int count)
1991 struct lu_object *parent;
1992 struct lu_object *new_parent;
1993 struct echo_thread_info *info = echo_env_info(env);
1994 struct lu_name *lname = &info->eti_lname;
1995 char *name = info->eti_name;
1996 struct lu_fid *fid = &info->eti_fid;
1997 struct lu_device *ld = ed->ed_next;
2003 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
2007 rc = echo_md_dir_stripe_choose(env, ed, parent, NULL, 0, id,
2012 /*prepare the requests*/
2013 for (i = 0; i < count; i++) {
2014 echo_md_build_name(lname, name, id);
2016 CDEBUG(D_RPCTRACE, "Start lookup object "DFID" %s %p\n",
2017 PFID(lu_object_fid(new_parent)), lname->ln_name,
2020 rc = mdo_lookup(env, lu2md(new_parent), lname, fid, NULL);
2022 CERROR("Can not lookup child %s: rc = %d\n", name, rc);
2026 CDEBUG(D_RPCTRACE, "End lookup object "DFID" %s %p\n",
2027 PFID(lu_object_fid(new_parent)), lname->ln_name,
2033 if (new_parent != parent)
2034 lu_object_put(env, new_parent);
2039 static int echo_md_destroy_internal(const struct lu_env *env,
2040 struct echo_device *ed,
2041 struct md_object *parent,
2042 struct lu_name *lname,
2045 struct lu_device *ld = ed->ed_next;
2046 struct lu_object *ec_child;
2047 struct lu_object *child;
2052 ec_child = echo_md_lookup(env, ed, parent, lname);
2053 if (IS_ERR(ec_child)) {
2054 CERROR("Can't find child %s: rc = %ld\n", lname->ln_name,
2056 RETURN(PTR_ERR(ec_child));
2059 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
2061 CERROR("Can not locate the child %s\n", lname->ln_name);
2062 GOTO(out_put, rc = -EINVAL);
2065 if (lu_object_remote(child)) {
2066 CERROR("Can not destroy remote object %s: rc = %d\n",
2067 lname->ln_name, -EPERM);
2068 GOTO(out_put, rc = -EPERM);
2070 CDEBUG(D_RPCTRACE, "Start destroy object "DFID" %s %p\n",
2071 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
2073 rc = mdo_unlink(env, parent, lu2md(child), lname, ma, 0);
2075 CERROR("Can not unlink child %s: rc = %d\n",
2076 lname->ln_name, rc);
2079 CDEBUG(D_RPCTRACE, "End destroy object "DFID" %s %p\n",
2080 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
2082 lu_object_put(env, ec_child);
2086 static int echo_destroy_object(const struct lu_env *env,
2087 struct echo_device *ed,
2088 struct lu_object *ec_parent,
2089 char *name, int namelen,
2090 __u64 id, __u32 mode,
2093 struct echo_thread_info *info = echo_env_info(env);
2094 struct lu_name *lname = &info->eti_lname;
2095 struct md_attr *ma = &info->eti_ma;
2096 struct lu_device *ld = ed->ed_next;
2097 struct lu_object *parent;
2098 struct lu_object *new_parent;
2103 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
2107 rc = echo_md_dir_stripe_choose(env, ed, parent, name, namelen,
2112 memset(ma, 0, sizeof(*ma));
2113 ma->ma_attr.la_mode = mode;
2114 ma->ma_attr.la_valid = LA_CTIME;
2115 ma->ma_attr.la_ctime = ktime_get_real_seconds();
2116 ma->ma_need = MA_INODE;
2120 lname->ln_name = name;
2121 lname->ln_namelen = namelen;
2122 rc = echo_md_destroy_internal(env, ed, lu2md(new_parent), lname,
2127 /*prepare the requests*/
2128 for (i = 0; i < count; i++) {
2129 char *tmp_name = info->eti_name;
2132 echo_md_build_name(lname, tmp_name, id);
2134 rc = echo_md_destroy_internal(env, ed, lu2md(new_parent), lname,
2137 CERROR("Can not unlink child %s: rc = %d\n", name, rc);
2144 if (new_parent != parent)
2145 lu_object_put(env, new_parent);
2150 static struct lu_object *echo_resolve_path(const struct lu_env *env,
2151 struct echo_device *ed, char *path,
2154 struct lu_device *ld = ed->ed_next;
2155 struct echo_thread_info *info = echo_env_info(env);
2156 struct lu_fid *fid = &info->eti_fid;
2157 struct lu_name *lname = &info->eti_lname;
2158 struct lu_object *parent = NULL;
2159 struct lu_object *child = NULL;
2163 *fid = ed->ed_root_fid;
2166 * In the function below, .hs_keycmp resolves to
2167 * lu_obj_hop_keycmp()
2169 /* coverity[overrun-buffer-val] */
2170 parent = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
2171 if (IS_ERR(parent)) {
2172 CERROR("Can not find the parent "DFID": rc = %ld\n",
2173 PFID(fid), PTR_ERR(parent));
2178 struct lu_object *ld_parent;
2181 e = strsep(&path, "/");
2186 if (!path || path[0] == '\0')
2192 lname->ln_namelen = strlen(e);
2194 ld_parent = lu_object_locate(parent->lo_header, ld->ld_type);
2196 lu_object_put(env, parent);
2201 child = echo_md_lookup(env, ed, lu2md(ld_parent), lname);
2202 lu_object_put(env, parent);
2203 if (IS_ERR(child)) {
2204 rc = (int)PTR_ERR(child);
2205 CERROR("lookup %s under parent "DFID": rc = %d\n",
2206 lname->ln_name, PFID(lu_object_fid(ld_parent)),
2213 RETURN(ERR_PTR(rc));
2218 static void echo_ucred_init(struct lu_env *env)
2220 struct lu_ucred *ucred = lu_ucred(env);
2222 ucred->uc_valid = UCRED_INVALID;
2224 ucred->uc_suppgids[0] = -1;
2225 ucred->uc_suppgids[1] = -1;
2227 ucred->uc_uid = ucred->uc_o_uid =
2228 from_kuid(&init_user_ns, current_uid());
2229 ucred->uc_gid = ucred->uc_o_gid =
2230 from_kgid(&init_user_ns, current_gid());
2231 ucred->uc_fsuid = ucred->uc_o_fsuid =
2232 from_kuid(&init_user_ns, current_fsuid());
2233 ucred->uc_fsgid = ucred->uc_o_fsgid =
2234 from_kgid(&init_user_ns, current_fsgid());
2235 ucred->uc_cap = cfs_curproc_cap_pack();
2237 /* remove fs privilege for non-root user. */
2238 if (ucred->uc_fsuid)
2239 ucred->uc_cap &= ~CFS_CAP_FS_MASK;
2240 ucred->uc_valid = UCRED_NEW;
2243 static void echo_ucred_fini(struct lu_env *env)
2245 struct lu_ucred *ucred = lu_ucred(env);
2247 ucred->uc_valid = UCRED_INIT;
2250 static int echo_md_handler(struct echo_device *ed, int command,
2251 char *path, int path_len, __u64 id, int count,
2252 struct obd_ioctl_data *data)
2254 struct echo_thread_info *info;
2255 struct lu_device *ld = ed->ed_next;
2258 struct lu_object *parent;
2260 int namelen = data->ioc_plen2;
2265 CERROR("MD echo client is not being initialized properly\n");
2269 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
2270 CERROR("Only support MDD layer right now!\n");
2274 env = cl_env_get(&refcheck);
2276 RETURN(PTR_ERR(env));
2278 rc = lu_env_refill_by_tags(env, ECHO_MD_CTX_TAG, ECHO_SES_TAG);
2282 /* init big_lmm buffer */
2283 info = echo_env_info(env);
2284 LASSERT(info->eti_big_lmm == NULL);
2285 OBD_ALLOC_LARGE(info->eti_big_lmm, MIN_MD_SIZE);
2286 if (!info->eti_big_lmm)
2287 GOTO(out_env, rc = -ENOMEM);
2288 info->eti_big_lmmsize = MIN_MD_SIZE;
2290 parent = echo_resolve_path(env, ed, path, path_len);
2291 if (IS_ERR(parent)) {
2292 CERROR("Can not resolve the path %s: rc = %ld\n", path,
2294 GOTO(out_free, rc = PTR_ERR(parent));
2298 OBD_ALLOC(name, namelen + 1);
2300 GOTO(out_put, rc = -ENOMEM);
2301 if (copy_from_user(name, data->ioc_pbuf2, namelen))
2302 GOTO(out_name, rc = -EFAULT);
2305 echo_ucred_init(env);
2308 case ECHO_MD_CREATE:
2309 case ECHO_MD_MKDIR: {
2310 struct echo_thread_info *info = echo_env_info(env);
2311 __u32 mode = data->ioc_obdo2.o_mode;
2312 struct lu_fid *fid = &info->eti_fid;
2313 int stripe_count = (int)data->ioc_obdo2.o_misc;
2314 int stripe_index = (int)data->ioc_obdo2.o_stripe_idx;
2316 rc = ostid_to_fid(fid, &data->ioc_obdo1.o_oi, 0);
2321 * In the function below, .hs_keycmp resolves to
2322 * lu_obj_hop_keycmp()
2324 /* coverity[overrun-buffer-val] */
2325 rc = echo_create_md_object(env, ed, parent, fid, name, namelen,
2326 id, mode, count, stripe_count,
2330 case ECHO_MD_DESTROY:
2331 case ECHO_MD_RMDIR: {
2332 __u32 mode = data->ioc_obdo2.o_mode;
2334 rc = echo_destroy_object(env, ed, parent, name, namelen,
2338 case ECHO_MD_LOOKUP:
2339 rc = echo_lookup_object(env, ed, parent, id, count);
2341 case ECHO_MD_GETATTR:
2342 rc = echo_getattr_object(env, ed, parent, id, count);
2344 case ECHO_MD_SETATTR:
2345 rc = echo_setattr_object(env, ed, parent, id, count);
2348 CERROR("unknown command %d\n", command);
2352 echo_ucred_fini(env);
2356 OBD_FREE(name, namelen + 1);
2358 lu_object_put(env, parent);
2360 LASSERT(info->eti_big_lmm);
2361 OBD_FREE_LARGE(info->eti_big_lmm, info->eti_big_lmmsize);
2362 info->eti_big_lmm = NULL;
2363 info->eti_big_lmmsize = 0;
2365 cl_env_put(env, &refcheck);
2368 #endif /* HAVE_SERVER_SUPPORT */
2370 static int echo_create_object(const struct lu_env *env, struct echo_device *ed,
2373 struct echo_object *eco;
2374 struct echo_client_obd *ec = ed->ed_ec;
2379 if (!(oa->o_valid & OBD_MD_FLID) ||
2380 !(oa->o_valid & OBD_MD_FLGROUP) ||
2381 !fid_seq_is_echo(ostid_seq(&oa->o_oi))) {
2382 CERROR("invalid oid "DOSTID"\n", POSTID(&oa->o_oi));
2386 if (ostid_id(&oa->o_oi) == 0) {
2387 rc = ostid_set_id(&oa->o_oi, ++last_object_id);
2392 rc = obd_create(env, ec->ec_exp, oa);
2394 CERROR("Cannot create objects: rc = %d\n", rc);
2400 oa->o_valid |= OBD_MD_FLID;
2402 eco = cl_echo_object_find(ed, &oa->o_oi);
2404 GOTO(failed, rc = PTR_ERR(eco));
2405 cl_echo_object_put(eco);
2407 CDEBUG(D_INFO, "oa oid "DOSTID"\n", POSTID(&oa->o_oi));
2411 if (created && rc != 0)
2412 obd_destroy(env, ec->ec_exp, oa);
2415 CERROR("create object failed with: rc = %d\n", rc);
2420 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
2423 struct echo_object *eco;
2427 if (!(oa->o_valid & OBD_MD_FLID) ||
2428 !(oa->o_valid & OBD_MD_FLGROUP) ||
2429 ostid_id(&oa->o_oi) == 0) {
2430 CERROR("invalid oid "DOSTID"\n", POSTID(&oa->o_oi));
2435 eco = cl_echo_object_find(ed, &oa->o_oi);
2444 static void echo_put_object(struct echo_object *eco)
2448 rc = cl_echo_object_put(eco);
2450 CERROR("%s: echo client drop an object failed: rc = %d\n",
2451 eco->eo_dev->ed_ec->ec_exp->exp_obd->obd_name, rc);
2454 static void echo_client_page_debug_setup(struct page *page, int rw, u64 id,
2455 u64 offset, u64 count)
2462 /* no partial pages on the client */
2463 LASSERT(count == PAGE_SIZE);
2467 for (delta = 0; delta < PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2468 if (rw == OBD_BRW_WRITE) {
2469 stripe_off = offset + delta;
2472 stripe_off = 0xdeadbeef00c0ffeeULL;
2473 stripe_id = 0xdeadbeef00c0ffeeULL;
2475 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
2476 stripe_off, stripe_id);
2483 echo_client_page_debug_check(struct page *page, u64 id, u64 offset, u64 count)
2492 /* no partial pages on the client */
2493 LASSERT(count == PAGE_SIZE);
2497 for (rc = delta = 0; delta < PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2498 stripe_off = offset + delta;
2501 rc2 = block_debug_check("test_brw",
2502 addr + delta, OBD_ECHO_BLOCK_SIZE,
2503 stripe_off, stripe_id);
2505 CERROR("Error in echo object %#llx\n", id);
2514 static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
2515 struct echo_object *eco, u64 offset,
2516 u64 count, int async)
2519 struct brw_page *pga;
2520 struct brw_page *pgp;
2521 struct page **pages;
2530 verify = (ostid_id(&oa->o_oi) != ECHO_PERSISTENT_OBJID &&
2531 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
2532 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
2534 gfp_mask = ((ostid_id(&oa->o_oi) & 2) == 0) ? GFP_KERNEL : GFP_HIGHUSER;
2536 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
2538 if ((count & (~PAGE_MASK)) != 0)
2541 /* XXX think again with misaligned I/O */
2542 npages = count >> PAGE_SHIFT;
2544 if (rw == OBD_BRW_WRITE)
2545 brw_flags = OBD_BRW_ASYNC;
2547 OBD_ALLOC(pga, npages * sizeof(*pga));
2551 OBD_ALLOC(pages, npages * sizeof(*pages));
2553 OBD_FREE(pga, npages * sizeof(*pga));
2557 for (i = 0, pgp = pga, off = offset;
2559 i++, pgp++, off += PAGE_SIZE) {
2561 LASSERT(pgp->pg == NULL); /* for cleanup */
2564 pgp->pg = alloc_page(gfp_mask);
2569 pgp->count = PAGE_SIZE;
2571 pgp->flag = brw_flags;
2574 echo_client_page_debug_setup(pgp->pg, rw,
2575 ostid_id(&oa->o_oi), off,
2579 /* brw mode can only be used at client */
2580 LASSERT(ed->ed_next != NULL);
2581 rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
2584 if (rc != 0 || rw != OBD_BRW_READ)
2587 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
2594 vrc = echo_client_page_debug_check(pgp->pg,
2595 ostid_id(&oa->o_oi),
2598 if (vrc != 0 && rc == 0)
2601 __free_page(pgp->pg);
2603 OBD_FREE(pga, npages * sizeof(*pga));
2604 OBD_FREE(pages, npages * sizeof(*pages));
2608 static int echo_client_prep_commit(const struct lu_env *env,
2609 struct obd_export *exp, int rw,
2610 struct obdo *oa, struct echo_object *eco,
2611 u64 offset, u64 count,
2612 u64 batch, int async)
2614 struct obd_ioobj ioo;
2615 struct niobuf_local *lnb;
2616 struct niobuf_remote rnb;
2618 u64 npages, tot_pages, apc;
2619 int i, ret = 0, brw_flags = 0;
2622 if (count <= 0 || (count & ~PAGE_MASK) != 0)
2625 apc = npages = batch >> PAGE_SHIFT;
2626 tot_pages = count >> PAGE_SHIFT;
2628 OBD_ALLOC_LARGE(lnb, apc * sizeof(struct niobuf_local));
2632 if (rw == OBD_BRW_WRITE && async)
2633 brw_flags |= OBD_BRW_ASYNC;
2635 obdo_to_ioobj(oa, &ioo);
2639 for (; tot_pages > 0; tot_pages -= npages) {
2642 if (tot_pages < npages)
2645 rnb.rnb_offset = off;
2646 rnb.rnb_len = npages * PAGE_SIZE;
2647 rnb.rnb_flags = brw_flags;
2649 off += npages * PAGE_SIZE;
2652 ret = obd_preprw(env, rw, exp, oa, 1, &ioo, &rnb, &lpages, lnb);
2656 for (i = 0; i < lpages; i++) {
2657 struct page *page = lnb[i].lnb_page;
2659 /* read past eof? */
2660 if (!page && lnb[i].lnb_rc == 0)
2664 lnb[i].lnb_flags |= OBD_BRW_ASYNC;
2666 if (ostid_id(&oa->o_oi) == ECHO_PERSISTENT_OBJID ||
2667 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
2668 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
2671 if (rw == OBD_BRW_WRITE)
2672 echo_client_page_debug_setup(page, rw,
2673 ostid_id(&oa->o_oi),
2674 lnb[i].lnb_file_offset,
2677 echo_client_page_debug_check(page,
2678 ostid_id(&oa->o_oi),
2679 lnb[i].lnb_file_offset,
2683 ret = obd_commitrw(env, rw, exp, oa, 1, &ioo, &rnb, npages, lnb,
2688 /* Reuse env context. */
2689 lu_context_exit((struct lu_context *)&env->le_ctx);
2690 lu_context_enter((struct lu_context *)&env->le_ctx);
2694 OBD_FREE_LARGE(lnb, apc * sizeof(struct niobuf_local));
2699 static int echo_client_brw_ioctl(const struct lu_env *env, int rw,
2700 struct obd_export *exp,
2701 struct obd_ioctl_data *data)
2703 struct obd_device *obd = class_exp2obd(exp);
2704 struct echo_device *ed = obd2echo_dev(obd);
2705 struct echo_client_obd *ec = ed->ed_ec;
2706 struct obdo *oa = &data->ioc_obdo1;
2707 struct echo_object *eco;
2713 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2715 rc = echo_get_object(&eco, ed, oa);
2719 oa->o_valid &= ~OBD_MD_FLHANDLE;
2721 /* OFD/obdfilter works only via prep/commit */
2722 test_mode = (long)data->ioc_pbuf1;
2723 if (!ed->ed_next && test_mode != 3) {
2725 data->ioc_plen1 = data->ioc_count;
2731 /* Truncate batch size to maximum */
2732 if (data->ioc_plen1 > PTLRPC_MAX_BRW_SIZE)
2733 data->ioc_plen1 = PTLRPC_MAX_BRW_SIZE;
2735 switch (test_mode) {
2739 rc = echo_client_kbrw(ed, rw, oa, eco, data->ioc_offset,
2740 data->ioc_count, async);
2743 rc = echo_client_prep_commit(env, ec->ec_exp, rw, oa, eco,
2744 data->ioc_offset, data->ioc_count,
2745 data->ioc_plen1, async);
2751 echo_put_object(eco);
2757 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2758 void *karg, void __user *uarg)
2760 #ifdef HAVE_SERVER_SUPPORT
2761 struct tgt_session_info *tsi;
2763 struct obd_device *obd = exp->exp_obd;
2764 struct echo_device *ed = obd2echo_dev(obd);
2765 struct echo_client_obd *ec = ed->ed_ec;
2766 struct echo_object *eco;
2767 struct obd_ioctl_data *data = karg;
2769 unsigned long env_tags = 0;
2773 int rw = OBD_BRW_READ;
2777 oa = &data->ioc_obdo1;
2778 if (!(oa->o_valid & OBD_MD_FLGROUP)) {
2779 oa->o_valid |= OBD_MD_FLGROUP;
2780 ostid_set_seq_echo(&oa->o_oi);
2783 /* This FID is unpacked just for validation at this point */
2784 rc = ostid_to_fid(&fid, &oa->o_oi, 0);
2788 env = cl_env_get(&refcheck);
2790 RETURN(PTR_ERR(env));
2794 #ifdef HAVE_SERVER_SUPPORT
2795 if (cmd == OBD_IOC_ECHO_MD || cmd == OBD_IOC_ECHO_ALLOC_SEQ)
2796 env_tags = ECHO_MD_CTX_TAG;
2799 env_tags = ECHO_DT_CTX_TAG;
2801 rc = lu_env_refill_by_tags(env, env_tags, ECHO_SES_TAG);
2805 #ifdef HAVE_SERVER_SUPPORT
2806 tsi = tgt_ses_info(env);
2807 /* treat as local operation */
2808 tsi->tsi_exp = NULL;
2809 tsi->tsi_jobid = NULL;
2813 case OBD_IOC_CREATE: /* may create echo object */
2814 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2815 GOTO(out, rc = -EPERM);
2817 rc = echo_create_object(env, ed, oa);
2820 #ifdef HAVE_SERVER_SUPPORT
2821 case OBD_IOC_ECHO_MD: {
2828 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2829 GOTO(out, rc = -EPERM);
2831 count = data->ioc_count;
2832 cmd = data->ioc_command;
2834 id = data->ioc_obdo2.o_oi.oi.oi_id;
2835 dirlen = data->ioc_plen1;
2836 OBD_ALLOC(dir, dirlen + 1);
2838 GOTO(out, rc = -ENOMEM);
2840 if (copy_from_user(dir, data->ioc_pbuf1, dirlen)) {
2841 OBD_FREE(dir, data->ioc_plen1 + 1);
2842 GOTO(out, rc = -EFAULT);
2845 rc = echo_md_handler(ed, cmd, dir, dirlen, id, count, data);
2846 OBD_FREE(dir, dirlen + 1);
2849 case OBD_IOC_ECHO_ALLOC_SEQ: {
2853 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2854 GOTO(out, rc = -EPERM);
2856 rc = seq_client_get_seq(env, ed->ed_cl_seq, &seq);
2858 CERROR("%s: Can not alloc seq: rc = %d\n",
2863 if (copy_to_user(data->ioc_pbuf1, &seq, data->ioc_plen1))
2866 max_count = LUSTRE_METADATA_SEQ_MAX_WIDTH;
2867 if (copy_to_user(data->ioc_pbuf2, &max_count,
2872 #endif /* HAVE_SERVER_SUPPORT */
2873 case OBD_IOC_DESTROY:
2874 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2875 GOTO(out, rc = -EPERM);
2877 rc = echo_get_object(&eco, ed, oa);
2879 rc = obd_destroy(env, ec->ec_exp, oa);
2881 eco->eo_deleted = 1;
2882 echo_put_object(eco);
2886 case OBD_IOC_GETATTR:
2887 rc = echo_get_object(&eco, ed, oa);
2889 rc = obd_getattr(env, ec->ec_exp, oa);
2890 echo_put_object(eco);
2894 case OBD_IOC_SETATTR:
2895 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2896 GOTO(out, rc = -EPERM);
2898 rc = echo_get_object(&eco, ed, oa);
2900 rc = obd_setattr(env, ec->ec_exp, oa);
2901 echo_put_object(eco);
2905 case OBD_IOC_BRW_WRITE:
2906 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2907 GOTO(out, rc = -EPERM);
2911 case OBD_IOC_BRW_READ:
2912 rc = echo_client_brw_ioctl(env, rw, exp, data);
2916 CERROR("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
2917 GOTO(out, rc = -ENOTTY);
2923 cl_env_put(env, &refcheck);
2928 static int echo_client_setup(const struct lu_env *env,
2929 struct obd_device *obddev, struct lustre_cfg *lcfg)
2931 struct echo_client_obd *ec = &obddev->u.echo_client;
2932 struct obd_device *tgt;
2933 struct obd_uuid echo_uuid = { "ECHO_UUID" };
2934 struct obd_connect_data *ocd = NULL;
2938 if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
2939 CERROR("requires a TARGET OBD name\n");
2943 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
2944 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
2945 CERROR("device not attached or not set up (%s)\n",
2946 lustre_cfg_string(lcfg, 1));
2950 spin_lock_init(&ec->ec_lock);
2951 INIT_LIST_HEAD(&ec->ec_objects);
2952 INIT_LIST_HEAD(&ec->ec_locks);
2955 lu_context_tags_update(ECHO_DT_CTX_TAG);
2956 lu_session_tags_update(ECHO_SES_TAG);
2958 if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
2959 #ifdef HAVE_SERVER_SUPPORT
2960 lu_context_tags_update(ECHO_MD_CTX_TAG);
2963 "Local operations are NOT supported on client side. Only remote operations are supported. Metadata client must be run on server side.\n");
2968 OBD_ALLOC(ocd, sizeof(*ocd));
2970 CERROR("Can't alloc ocd connecting to %s\n",
2971 lustre_cfg_string(lcfg, 1));
2975 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
2976 OBD_CONNECT_BRW_SIZE |
2977 OBD_CONNECT_GRANT | OBD_CONNECT_FULL20 |
2978 OBD_CONNECT_64BITHASH | OBD_CONNECT_LVB_TYPE |
2980 ocd->ocd_brw_size = DT_MAX_BRW_SIZE;
2981 ocd->ocd_version = LUSTRE_VERSION_CODE;
2982 ocd->ocd_group = FID_SEQ_ECHO;
2984 rc = obd_connect(env, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
2986 /* Turn off pinger because it connects to tgt obd directly. */
2987 spin_lock(&tgt->obd_dev_lock);
2988 list_del_init(&ec->ec_exp->exp_obd_chain_timed);
2989 spin_unlock(&tgt->obd_dev_lock);
2992 OBD_FREE(ocd, sizeof(*ocd));
2995 CERROR("fail to connect to device %s\n",
2996 lustre_cfg_string(lcfg, 1));
3003 static int echo_client_cleanup(struct obd_device *obddev)
3005 struct echo_device *ed = obd2echo_dev(obddev);
3006 struct echo_client_obd *ec = &obddev->u.echo_client;
3010 /*Do nothing for Metadata echo client*/
3014 lu_session_tags_clear(ECHO_SES_TAG & ~LCT_SESSION);
3015 lu_context_tags_clear(ECHO_DT_CTX_TAG);
3016 if (ed->ed_next_ismd) {
3017 #ifdef HAVE_SERVER_SUPPORT
3018 lu_context_tags_clear(ECHO_MD_CTX_TAG);
3021 "This is client-side only module, does not support metadata echo client.\n");
3026 if (!list_empty(&obddev->obd_exports)) {
3027 CERROR("still has clients!\n");
3031 LASSERT(atomic_read(&ec->ec_exp->exp_refcount) > 0);
3032 rc = obd_disconnect(ec->ec_exp);
3034 CERROR("fail to disconnect device: %d\n", rc);
3039 static int echo_client_connect(const struct lu_env *env,
3040 struct obd_export **exp,
3041 struct obd_device *src, struct obd_uuid *cluuid,
3042 struct obd_connect_data *data, void *localdata)
3045 struct lustre_handle conn = { 0 };
3048 rc = class_connect(&conn, src, cluuid);
3050 *exp = class_conn2export(&conn);
3055 static int echo_client_disconnect(struct obd_export *exp)
3061 GOTO(out, rc = -EINVAL);
3063 rc = class_disconnect(exp);
3069 static struct obd_ops echo_client_obd_ops = {
3070 .o_owner = THIS_MODULE,
3071 .o_iocontrol = echo_client_iocontrol,
3072 .o_connect = echo_client_connect,
3073 .o_disconnect = echo_client_disconnect
3076 static int __init obdecho_init(void)
3081 LCONSOLE_INFO("Echo OBD driver; http://www.lustre.org/\n");
3083 LASSERT(PAGE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
3085 # ifdef HAVE_SERVER_SUPPORT
3086 rc = echo_persistent_pages_init();
3090 rc = class_register_type(&echo_obd_ops, NULL, true, NULL,
3091 LUSTRE_ECHO_NAME, &echo_srv_type);
3096 rc = lu_kmem_init(echo_caches);
3098 rc = class_register_type(&echo_client_obd_ops, NULL, false,
3099 NULL, LUSTRE_ECHO_CLIENT_NAME,
3102 lu_kmem_fini(echo_caches);
3105 # ifdef HAVE_SERVER_SUPPORT
3109 class_unregister_type(LUSTRE_ECHO_NAME);
3111 echo_persistent_pages_fini();
3117 static void __exit obdecho_exit(void)
3119 class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
3120 lu_kmem_fini(echo_caches);
3122 #ifdef HAVE_SERVER_SUPPORT
3123 class_unregister_type(LUSTRE_ECHO_NAME);
3124 echo_persistent_pages_fini();
3128 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
3129 MODULE_DESCRIPTION("Lustre Echo Client test driver");
3130 MODULE_VERSION(LUSTRE_VERSION_STRING);
3131 MODULE_LICENSE("GPL");
3133 module_init(obdecho_init);
3134 module_exit(obdecho_exit);
3136 /** @} echo_client */