4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 #define DEBUG_SUBSYSTEM S_ECHO
35 #include <linux/user_namespace.h>
36 #ifdef HAVE_UIDGID_HEADER
37 # include <linux/uidgid.h>
39 #include <libcfs/libcfs.h>
42 #include <obd_support.h>
43 #include <obd_class.h>
44 #include <lustre_debug.h>
45 #include <lprocfs_status.h>
46 #include <cl_object.h>
47 #include <lustre_fid.h>
48 #include <lustre_lmv.h>
49 #include <lustre_acl.h>
50 #include <uapi/linux/lustre/lustre_ioctl.h>
51 #include <lustre_net.h>
52 #ifdef HAVE_SERVER_SUPPORT
53 # include <md_object.h>
55 #define ETI_NAME_LEN 20
57 #endif /* HAVE_SERVER_SUPPORT */
59 #include "echo_internal.h"
61 /** \defgroup echo_client Echo Client
66 struct cl_device ed_cl;
67 struct echo_client_obd *ed_ec;
69 struct cl_site ed_site_myself;
70 struct lu_site *ed_site;
71 struct lu_device *ed_next;
73 struct lu_client_seq *ed_cl_seq;
74 #ifdef HAVE_SERVER_SUPPORT
75 struct local_oid_storage *ed_los;
76 struct lu_fid ed_root_fid;
77 #endif /* HAVE_SERVER_SUPPORT */
81 struct cl_object eo_cl;
82 struct cl_object_header eo_hdr;
83 struct echo_device *eo_dev;
84 struct list_head eo_obj_chain;
85 struct lov_oinfo *eo_oinfo;
90 struct echo_object_conf {
91 struct cl_object_conf eoc_cl;
92 struct lov_oinfo **eoc_oinfo;
96 struct cl_page_slice ep_cl;
101 struct cl_lock_slice el_cl;
102 struct list_head el_chain;
103 struct echo_object *el_object;
105 atomic_t el_refcount;
108 #ifdef HAVE_SERVER_SUPPORT
109 static const char echo_md_root_dir_name[] = "ROOT_ECHO";
112 * In order to use the values of members in struct mdd_device,
113 * we define an alias structure here.
115 struct echo_md_device {
116 struct md_device emd_md_dev;
117 struct obd_export *emd_child_exp;
118 struct dt_device *emd_child;
119 struct dt_device *emd_bottom;
120 struct lu_fid emd_root_fid;
121 struct lu_fid emd_local_root_fid;
123 #endif /* HAVE_SERVER_SUPPORT */
125 static int echo_client_setup(const struct lu_env *env,
126 struct obd_device *obddev,
127 struct lustre_cfg *lcfg);
128 static int echo_client_cleanup(struct obd_device *obddev);
131 /** \defgroup echo_helpers Helper functions
134 static inline struct echo_device *cl2echo_dev(const struct cl_device *dev)
136 return container_of0(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;
175 static inline struct echo_thread_info *echo_env_info(const struct lu_env *env)
177 struct echo_thread_info *info;
178 info = lu_context_key_get(&env->le_ctx, &echo_thread_key);
179 LASSERT(info != NULL);
184 struct echo_object_conf *cl2echo_conf(const struct cl_object_conf *c)
186 return container_of(c, struct echo_object_conf, eoc_cl);
189 #ifdef HAVE_SERVER_SUPPORT
190 static inline struct echo_md_device *lu2emd_dev(struct lu_device *d)
192 return container_of0(d, struct echo_md_device, emd_md_dev.md_lu_dev);
195 static inline struct lu_device *emd2lu_dev(struct echo_md_device *d)
197 return &d->emd_md_dev.md_lu_dev;
200 static inline struct seq_server_site *echo_md_seq_site(struct echo_md_device *d)
202 return emd2lu_dev(d)->ld_site->ld_seq_site;
205 static inline struct obd_device *emd2obd_dev(struct echo_md_device *d)
207 return d->emd_md_dev.md_lu_dev.ld_obd;
209 #endif /* HAVE_SERVER_SUPPORT */
211 /** @} echo_helpers */
213 static int cl_echo_object_put(struct echo_object *eco);
214 static int cl_echo_object_brw(struct echo_object *eco, int rw, u64 offset,
215 struct page **pages, int npages, int async);
217 struct echo_thread_info {
218 struct echo_object_conf eti_conf;
219 struct lustre_md eti_md;
221 struct cl_2queue eti_queue;
223 struct cl_lock eti_lock;
224 struct lu_fid eti_fid;
225 struct lu_fid eti_fid2;
226 #ifdef HAVE_SERVER_SUPPORT
227 struct md_op_spec eti_spec;
228 struct lov_mds_md_v3 eti_lmm;
229 struct lov_user_md_v3 eti_lum;
230 struct md_attr eti_ma;
231 struct lu_name eti_lname;
232 /* per-thread values, can be re-used */
233 void *eti_big_lmm; /* may be vmalloc'd */
235 char eti_name[ETI_NAME_LEN];
236 struct lu_buf eti_buf;
237 /* If we want to test large ACL, then need to enlarge the buffer. */
238 char eti_xattr_buf[LUSTRE_POSIX_ACL_MAX_SIZE_OLD];
242 /* No session used right now */
243 struct echo_session_info {
247 static struct kmem_cache *echo_lock_kmem;
248 static struct kmem_cache *echo_object_kmem;
249 static struct kmem_cache *echo_thread_kmem;
250 static struct kmem_cache *echo_session_kmem;
251 /* static struct kmem_cache *echo_req_kmem; */
253 static struct lu_kmem_descr echo_caches[] = {
255 .ckd_cache = &echo_lock_kmem,
256 .ckd_name = "echo_lock_kmem",
257 .ckd_size = sizeof (struct echo_lock)
260 .ckd_cache = &echo_object_kmem,
261 .ckd_name = "echo_object_kmem",
262 .ckd_size = sizeof (struct echo_object)
265 .ckd_cache = &echo_thread_kmem,
266 .ckd_name = "echo_thread_kmem",
267 .ckd_size = sizeof (struct echo_thread_info)
270 .ckd_cache = &echo_session_kmem,
271 .ckd_name = "echo_session_kmem",
272 .ckd_size = sizeof (struct echo_session_info)
279 /** \defgroup echo_page Page operations
281 * Echo page operations.
285 static int echo_page_own(const struct lu_env *env,
286 const struct cl_page_slice *slice,
287 struct cl_io *io, int nonblock)
289 struct echo_page *ep = cl2echo_page(slice);
292 mutex_lock(&ep->ep_lock);
293 else if (!mutex_trylock(&ep->ep_lock))
298 static void echo_page_disown(const struct lu_env *env,
299 const struct cl_page_slice *slice,
302 struct echo_page *ep = cl2echo_page(slice);
304 LASSERT(mutex_is_locked(&ep->ep_lock));
305 mutex_unlock(&ep->ep_lock);
308 static void echo_page_discard(const struct lu_env *env,
309 const struct cl_page_slice *slice,
310 struct cl_io *unused)
312 cl_page_delete(env, slice->cpl_page);
315 static int echo_page_is_vmlocked(const struct lu_env *env,
316 const struct cl_page_slice *slice)
318 if (mutex_is_locked(&cl2echo_page(slice)->ep_lock))
323 static void echo_page_completion(const struct lu_env *env,
324 const struct cl_page_slice *slice,
327 LASSERT(slice->cpl_page->cp_sync_io != NULL);
330 static void echo_page_fini(const struct lu_env *env,
331 struct cl_page_slice *slice)
333 struct echo_object *eco = cl2echo_obj(slice->cpl_obj);
336 atomic_dec(&eco->eo_npages);
337 put_page(slice->cpl_page->cp_vmpage);
341 static int echo_page_prep(const struct lu_env *env,
342 const struct cl_page_slice *slice,
343 struct cl_io *unused)
348 static int echo_page_print(const struct lu_env *env,
349 const struct cl_page_slice *slice,
350 void *cookie, lu_printer_t printer)
352 struct echo_page *ep = cl2echo_page(slice);
354 (*printer)(env, cookie, LUSTRE_ECHO_CLIENT_NAME"-page@%p %d vm@%p\n",
355 ep, mutex_is_locked(&ep->ep_lock),
356 slice->cpl_page->cp_vmpage);
360 static const struct cl_page_operations echo_page_ops = {
361 .cpo_own = echo_page_own,
362 .cpo_disown = echo_page_disown,
363 .cpo_discard = echo_page_discard,
364 .cpo_fini = echo_page_fini,
365 .cpo_print = echo_page_print,
366 .cpo_is_vmlocked = echo_page_is_vmlocked,
369 .cpo_prep = echo_page_prep,
370 .cpo_completion = echo_page_completion,
373 .cpo_prep = echo_page_prep,
374 .cpo_completion = echo_page_completion,
380 /** \defgroup echo_lock Locking
382 * echo lock operations
386 static void echo_lock_fini(const struct lu_env *env,
387 struct cl_lock_slice *slice)
389 struct echo_lock *ecl = cl2echo_lock(slice);
391 LASSERT(list_empty(&ecl->el_chain));
392 OBD_SLAB_FREE_PTR(ecl, echo_lock_kmem);
395 static struct cl_lock_operations echo_lock_ops = {
396 .clo_fini = echo_lock_fini,
401 /** \defgroup echo_cl_ops cl_object operations
403 * operations for cl_object
407 static int echo_page_init(const struct lu_env *env, struct cl_object *obj,
408 struct cl_page *page, pgoff_t index)
410 struct echo_page *ep = cl_object_page_slice(obj, page);
411 struct echo_object *eco = cl2echo_obj(obj);
414 get_page(page->cp_vmpage);
415 mutex_init(&ep->ep_lock);
416 cl_page_slice_add(page, &ep->ep_cl, obj, index, &echo_page_ops);
417 atomic_inc(&eco->eo_npages);
421 static int echo_io_init(const struct lu_env *env, struct cl_object *obj,
427 static int echo_lock_init(const struct lu_env *env,
428 struct cl_object *obj, struct cl_lock *lock,
429 const struct cl_io *unused)
431 struct echo_lock *el;
434 OBD_SLAB_ALLOC_PTR_GFP(el, echo_lock_kmem, GFP_NOFS);
436 cl_lock_slice_add(lock, &el->el_cl, obj, &echo_lock_ops);
437 el->el_object = cl2echo_obj(obj);
438 INIT_LIST_HEAD(&el->el_chain);
439 atomic_set(&el->el_refcount, 0);
441 RETURN(el == NULL ? -ENOMEM : 0);
444 static int echo_conf_set(const struct lu_env *env, struct cl_object *obj,
445 const struct cl_object_conf *conf)
450 static const struct cl_object_operations echo_cl_obj_ops = {
451 .coo_page_init = echo_page_init,
452 .coo_lock_init = echo_lock_init,
453 .coo_io_init = echo_io_init,
454 .coo_conf_set = echo_conf_set
456 /** @} echo_cl_ops */
458 /** \defgroup echo_lu_ops lu_object operations
460 * operations for echo lu object.
464 static int echo_object_init(const struct lu_env *env, struct lu_object *obj,
465 const struct lu_object_conf *conf)
467 struct echo_device *ed = cl2echo_dev(lu2cl_dev(obj->lo_dev));
468 struct echo_client_obd *ec = ed->ed_ec;
469 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
473 struct lu_object *below;
474 struct lu_device *under;
477 below = under->ld_ops->ldo_object_alloc(env, obj->lo_header,
481 lu_object_add(obj, below);
484 if (!ed->ed_next_ismd) {
485 const struct cl_object_conf *cconf = lu2cl_conf(conf);
486 struct echo_object_conf *econf = cl2echo_conf(cconf);
488 LASSERT(econf->eoc_oinfo != NULL);
490 /* Transfer the oinfo pointer to eco that it won't be
492 eco->eo_oinfo = *econf->eoc_oinfo;
493 *econf->eoc_oinfo = NULL;
495 eco->eo_oinfo = NULL;
499 atomic_set(&eco->eo_npages, 0);
500 cl_object_page_init(lu2cl(obj), sizeof(struct echo_page));
502 spin_lock(&ec->ec_lock);
503 list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
504 spin_unlock(&ec->ec_lock);
509 static void echo_object_free(const struct lu_env *env, struct lu_object *obj)
511 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
512 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
515 LASSERT(atomic_read(&eco->eo_npages) == 0);
517 spin_lock(&ec->ec_lock);
518 list_del_init(&eco->eo_obj_chain);
519 spin_unlock(&ec->ec_lock);
522 lu_object_header_fini(obj->lo_header);
524 if (eco->eo_oinfo != NULL)
525 OBD_FREE_PTR(eco->eo_oinfo);
527 OBD_SLAB_FREE_PTR(eco, echo_object_kmem);
531 static int echo_object_print(const struct lu_env *env, void *cookie,
532 lu_printer_t p, const struct lu_object *o)
534 struct echo_object *obj = cl2echo_obj(lu2cl(o));
536 return (*p)(env, cookie, "echoclient-object@%p", obj);
539 static const struct lu_object_operations echo_lu_obj_ops = {
540 .loo_object_init = echo_object_init,
541 .loo_object_delete = NULL,
542 .loo_object_release = NULL,
543 .loo_object_free = echo_object_free,
544 .loo_object_print = echo_object_print,
545 .loo_object_invariant = NULL
547 /** @} echo_lu_ops */
549 /** \defgroup echo_lu_dev_ops lu_device operations
551 * Operations for echo lu device.
555 static struct lu_object *echo_object_alloc(const struct lu_env *env,
556 const struct lu_object_header *hdr,
557 struct lu_device *dev)
559 struct echo_object *eco;
560 struct lu_object *obj = NULL;
563 /* we're the top dev. */
564 LASSERT(hdr == NULL);
565 OBD_SLAB_ALLOC_PTR_GFP(eco, echo_object_kmem, GFP_NOFS);
567 struct cl_object_header *hdr = &eco->eo_hdr;
569 obj = &echo_obj2cl(eco)->co_lu;
570 cl_object_header_init(hdr);
571 hdr->coh_page_bufsize = cfs_size_round(sizeof(struct cl_page));
573 lu_object_init(obj, &hdr->coh_lu, dev);
574 lu_object_add_top(&hdr->coh_lu, obj);
576 eco->eo_cl.co_ops = &echo_cl_obj_ops;
577 obj->lo_ops = &echo_lu_obj_ops;
582 static struct lu_device_operations echo_device_lu_ops = {
583 .ldo_object_alloc = echo_object_alloc,
586 /** @} echo_lu_dev_ops */
588 /** \defgroup echo_init Setup and teardown
590 * Init and fini functions for echo client.
594 static int echo_site_init(const struct lu_env *env, struct echo_device *ed)
596 struct cl_site *site = &ed->ed_site_myself;
599 /* initialize site */
600 rc = cl_site_init(site, &ed->ed_cl);
602 CERROR("Cannot initialize site for echo client(%d)\n", rc);
606 rc = lu_site_init_finish(&site->cs_lu);
612 ed->ed_site = &site->cs_lu;
616 static void echo_site_fini(const struct lu_env *env, struct echo_device *ed)
619 if (!ed->ed_next_ismd)
620 lu_site_fini(ed->ed_site);
625 static void *echo_thread_key_init(const struct lu_context *ctx,
626 struct lu_context_key *key)
628 struct echo_thread_info *info;
630 OBD_SLAB_ALLOC_PTR_GFP(info, echo_thread_kmem, GFP_NOFS);
632 info = ERR_PTR(-ENOMEM);
636 static void echo_thread_key_fini(const struct lu_context *ctx,
637 struct lu_context_key *key, void *data)
639 struct echo_thread_info *info = data;
640 OBD_SLAB_FREE_PTR(info, echo_thread_kmem);
643 static struct lu_context_key echo_thread_key = {
644 .lct_tags = LCT_CL_THREAD,
645 .lct_init = echo_thread_key_init,
646 .lct_fini = echo_thread_key_fini,
649 static void *echo_session_key_init(const struct lu_context *ctx,
650 struct lu_context_key *key)
652 struct echo_session_info *session;
654 OBD_SLAB_ALLOC_PTR_GFP(session, echo_session_kmem, GFP_NOFS);
656 session = ERR_PTR(-ENOMEM);
660 static void echo_session_key_fini(const struct lu_context *ctx,
661 struct lu_context_key *key, void *data)
663 struct echo_session_info *session = data;
664 OBD_SLAB_FREE_PTR(session, echo_session_kmem);
667 static struct lu_context_key echo_session_key = {
668 .lct_tags = LCT_SESSION,
669 .lct_init = echo_session_key_init,
670 .lct_fini = echo_session_key_fini,
673 LU_TYPE_INIT_FINI(echo, &echo_thread_key, &echo_session_key);
675 #ifdef HAVE_SERVER_SUPPORT
676 # define ECHO_SEQ_WIDTH 0xffffffff
677 static int echo_fid_init(struct echo_device *ed, char *obd_name,
678 struct seq_server_site *ss)
684 OBD_ALLOC_PTR(ed->ed_cl_seq);
685 if (ed->ed_cl_seq == NULL)
688 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
690 GOTO(out_free_seq, rc = -ENOMEM);
692 snprintf(prefix, MAX_OBD_NAME + 5, "srv-%s", obd_name);
694 /* Init client side sequence-manager */
695 rc = seq_client_init(ed->ed_cl_seq, NULL,
697 prefix, ss->ss_server_seq);
698 ed->ed_cl_seq->lcs_width = ECHO_SEQ_WIDTH;
699 OBD_FREE(prefix, MAX_OBD_NAME + 5);
701 GOTO(out_free_seq, rc);
706 OBD_FREE_PTR(ed->ed_cl_seq);
707 ed->ed_cl_seq = NULL;
711 static int echo_fid_fini(struct obd_device *obddev)
713 struct echo_device *ed = obd2echo_dev(obddev);
716 if (ed->ed_cl_seq != NULL) {
717 seq_client_fini(ed->ed_cl_seq);
718 OBD_FREE_PTR(ed->ed_cl_seq);
719 ed->ed_cl_seq = NULL;
725 static void echo_ed_los_fini(const struct lu_env *env, struct echo_device *ed)
729 if (ed != NULL && ed->ed_next_ismd && ed->ed_los != NULL) {
730 local_oid_storage_fini(env, ed->ed_los);
736 echo_md_local_file_create(const struct lu_env *env, struct echo_md_device *emd,
737 struct local_oid_storage *los,
738 const struct lu_fid *pfid, const char *name,
739 __u32 mode, struct lu_fid *fid)
741 struct dt_object *parent = NULL;
742 struct dt_object *dto = NULL;
746 LASSERT(!fid_is_zero(pfid));
747 parent = dt_locate(env, emd->emd_bottom, pfid);
748 if (unlikely(IS_ERR(parent)))
749 RETURN(PTR_ERR(parent));
751 /* create local file with @fid */
752 dto = local_file_find_or_create_with_fid(env, emd->emd_bottom, fid,
755 GOTO(out_put, rc = PTR_ERR(dto));
757 *fid = *lu_object_fid(&dto->do_lu);
758 /* since stack is not fully set up the local_storage uses own stack
759 * and we should drop its object from cache */
760 dt_object_put_nocache(env, dto);
764 dt_object_put(env, parent);
769 echo_md_root_get(const struct lu_env *env, struct echo_md_device *emd,
770 struct echo_device *ed)
776 /* Setup local dirs */
777 fid.f_seq = FID_SEQ_LOCAL_NAME;
780 rc = local_oid_storage_init(env, emd->emd_bottom, &fid, &ed->ed_los);
784 lu_echo_root_fid(&fid);
785 if (echo_md_seq_site(emd)->ss_node_id == 0) {
786 rc = echo_md_local_file_create(env, emd, ed->ed_los,
787 &emd->emd_local_root_fid,
788 echo_md_root_dir_name, S_IFDIR |
789 S_IRUGO | S_IWUSR | S_IXUGO,
792 CERROR("%s: create md echo root fid failed: rc = %d\n",
793 emd2obd_dev(emd)->obd_name, rc);
797 ed->ed_root_fid = fid;
801 echo_ed_los_fini(env, ed);
805 #endif /* HAVE_SERVER_SUPPORT */
807 static struct lu_device *echo_device_alloc(const struct lu_env *env,
808 struct lu_device_type *t,
809 struct lustre_cfg *cfg)
811 struct lu_device *next;
812 struct echo_device *ed;
813 struct cl_device *cd;
814 struct obd_device *obd = NULL; /* to keep compiler happy */
815 struct obd_device *tgt;
816 const char *tgt_type_name;
823 GOTO(out, rc = -ENOMEM);
827 rc = cl_device_init(cd, t);
831 cd->cd_lu_dev.ld_ops = &echo_device_lu_ops;
834 obd = class_name2obd(lustre_cfg_string(cfg, 0));
835 LASSERT(obd != NULL);
836 LASSERT(env != NULL);
838 tgt = class_name2obd(lustre_cfg_string(cfg, 1));
840 CERROR("Can not find tgt device %s\n",
841 lustre_cfg_string(cfg, 1));
842 GOTO(out, rc = -ENODEV);
845 next = tgt->obd_lu_dev;
847 if (strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME) == 0) {
848 ed->ed_next_ismd = 1;
849 } else if (strcmp(tgt->obd_type->typ_name, LUSTRE_OST_NAME) == 0 ||
850 strcmp(tgt->obd_type->typ_name, LUSTRE_OSC_NAME) == 0) {
851 ed->ed_next_ismd = 0;
852 rc = echo_site_init(env, ed);
856 GOTO(out, rc = -EINVAL);
861 rc = echo_client_setup(env, obd, cfg);
865 ed->ed_ec = &obd->u.echo_client;
868 if (ed->ed_next_ismd) {
869 #ifdef HAVE_SERVER_SUPPORT
870 /* Suppose to connect to some Metadata layer */
871 struct lu_site *ls = NULL;
872 struct lu_device *ld = NULL;
873 struct md_device *md = NULL;
874 struct echo_md_device *emd = NULL;
878 CERROR("%s is not lu device type!\n",
879 lustre_cfg_string(cfg, 1));
880 GOTO(out, rc = -EINVAL);
883 tgt_type_name = lustre_cfg_string(cfg, 2);
884 if (!tgt_type_name) {
885 CERROR("%s no type name for echo %s setup\n",
886 lustre_cfg_string(cfg, 1),
887 tgt->obd_type->typ_name);
888 GOTO(out, rc = -EINVAL);
893 spin_lock(&ls->ls_ld_lock);
894 list_for_each_entry(ld, &ls->ls_ld_linkage, ld_linkage) {
895 if (strcmp(ld->ld_type->ldt_name, tgt_type_name) == 0) {
900 spin_unlock(&ls->ls_ld_lock);
903 CERROR("%s is not lu device type!\n",
904 lustre_cfg_string(cfg, 1));
905 GOTO(out, rc = -EINVAL);
909 /* For MD echo client, it will use the site in MDS stack */
911 ed->ed_cl.cd_lu_dev.ld_site = ls;
912 rc = echo_fid_init(ed, obd->obd_name, lu_site2seq(ls));
914 CERROR("echo fid init error %d\n", rc);
918 md = lu2md_dev(next);
919 emd = lu2emd_dev(&md->md_lu_dev);
920 rc = echo_md_root_get(env, emd, ed);
922 CERROR("%s: get root error: rc = %d\n",
923 emd2obd_dev(emd)->obd_name, rc);
926 #else /* !HAVE_SERVER_SUPPORT */
927 CERROR("Local operations are NOT supported on client side. "
928 "Only remote operations are supported. Metadata client "
929 "must be run on server side.\n");
930 GOTO(out, rc = -EOPNOTSUPP);
931 #endif /* HAVE_SERVER_SUPPORT */
933 /* if echo client is to be stacked upon ost device, the next is
934 * NULL since ost is not a clio device so far */
935 if (next != NULL && !lu_device_is_cl(next))
938 tgt_type_name = tgt->obd_type->typ_name;
940 LASSERT(next != NULL);
941 if (next->ld_site != NULL)
942 GOTO(out, rc = -EBUSY);
944 next->ld_site = ed->ed_site;
945 rc = next->ld_type->ldt_ops->ldto_device_init(env, next,
946 next->ld_type->ldt_name,
951 LASSERT(strcmp(tgt_type_name, LUSTRE_OST_NAME) == 0);
955 RETURN(&cd->cd_lu_dev);
960 rc2 = echo_client_cleanup(obd);
962 CERROR("Cleanup obd device %s error(%d)\n",
967 echo_site_fini(env, ed);
969 cl_device_fini(&ed->ed_cl);
979 static int echo_device_init(const struct lu_env *env, struct lu_device *d,
980 const char *name, struct lu_device *next)
986 static struct lu_device *echo_device_fini(const struct lu_env *env,
989 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
990 struct lu_device *next = ed->ed_next;
992 while (next && !ed->ed_next_ismd)
993 next = next->ld_type->ldt_ops->ldto_device_fini(env, next);
997 static void echo_lock_release(const struct lu_env *env,
998 struct echo_lock *ecl,
1001 struct cl_lock *clk = echo_lock2cl(ecl);
1003 cl_lock_release(env, clk);
1006 static struct lu_device *echo_device_free(const struct lu_env *env,
1007 struct lu_device *d)
1009 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
1010 struct echo_client_obd *ec = ed->ed_ec;
1011 struct echo_object *eco;
1012 struct lu_device *next = ed->ed_next;
1014 CDEBUG(D_INFO, "echo device:%p is going to be freed, next = %p\n",
1017 lu_site_purge(env, ed->ed_site, -1);
1019 /* check if there are objects still alive.
1020 * It shouldn't have any object because lu_site_purge would cleanup
1021 * all of cached objects. Anyway, probably the echo device is being
1022 * parallelly accessed.
1024 spin_lock(&ec->ec_lock);
1025 list_for_each_entry(eco, &ec->ec_objects, eo_obj_chain)
1026 eco->eo_deleted = 1;
1027 spin_unlock(&ec->ec_lock);
1030 lu_site_purge(env, ed->ed_site, -1);
1033 "Waiting for the reference of echo object to be dropped\n");
1035 /* Wait for the last reference to be dropped. */
1036 spin_lock(&ec->ec_lock);
1037 while (!list_empty(&ec->ec_objects)) {
1038 spin_unlock(&ec->ec_lock);
1039 CERROR("echo_client still has objects at cleanup time, "
1040 "wait for 1 second\n");
1041 set_current_state(TASK_UNINTERRUPTIBLE);
1042 schedule_timeout(cfs_time_seconds(1));
1043 lu_site_purge(env, ed->ed_site, -1);
1044 spin_lock(&ec->ec_lock);
1046 spin_unlock(&ec->ec_lock);
1048 LASSERT(list_empty(&ec->ec_locks));
1050 CDEBUG(D_INFO, "No object exists, exiting...\n");
1052 echo_client_cleanup(d->ld_obd);
1053 #ifdef HAVE_SERVER_SUPPORT
1054 echo_fid_fini(d->ld_obd);
1055 echo_ed_los_fini(env, ed);
1057 while (next && !ed->ed_next_ismd)
1058 next = next->ld_type->ldt_ops->ldto_device_free(env, next);
1060 LASSERT(ed->ed_site == d->ld_site);
1061 echo_site_fini(env, ed);
1062 cl_device_fini(&ed->ed_cl);
1065 cl_env_cache_purge(~0);
1070 static const struct lu_device_type_operations echo_device_type_ops = {
1071 .ldto_init = echo_type_init,
1072 .ldto_fini = echo_type_fini,
1074 .ldto_start = echo_type_start,
1075 .ldto_stop = echo_type_stop,
1077 .ldto_device_alloc = echo_device_alloc,
1078 .ldto_device_free = echo_device_free,
1079 .ldto_device_init = echo_device_init,
1080 .ldto_device_fini = echo_device_fini
1083 static struct lu_device_type echo_device_type = {
1084 .ldt_tags = LU_DEVICE_CL,
1085 .ldt_name = LUSTRE_ECHO_CLIENT_NAME,
1086 .ldt_ops = &echo_device_type_ops,
1087 .ldt_ctx_tags = LCT_CL_THREAD | LCT_MD_THREAD | LCT_DT_THREAD,
1091 /** \defgroup echo_exports Exported operations
1093 * exporting functions to echo client
1098 /* Interfaces to echo client obd device */
1099 static struct echo_object *
1100 cl_echo_object_find(struct echo_device *d, const struct ost_id *oi)
1103 struct echo_thread_info *info;
1104 struct echo_object_conf *conf;
1105 struct echo_object *eco;
1106 struct cl_object *obj;
1107 struct lov_oinfo *oinfo = NULL;
1113 LASSERTF(ostid_id(oi) != 0, DOSTID"\n", POSTID(oi));
1114 LASSERTF(ostid_seq(oi) == FID_SEQ_ECHO, DOSTID"\n", POSTID(oi));
1116 /* Never return an object if the obd is to be freed. */
1117 if (echo_dev2cl(d)->cd_lu_dev.ld_obd->obd_stopping)
1118 RETURN(ERR_PTR(-ENODEV));
1120 env = cl_env_get(&refcheck);
1122 RETURN((void *)env);
1124 info = echo_env_info(env);
1125 conf = &info->eti_conf;
1127 OBD_ALLOC_PTR(oinfo);
1129 GOTO(out, eco = ERR_PTR(-ENOMEM));
1131 oinfo->loi_oi = *oi;
1132 conf->eoc_cl.u.coc_oinfo = oinfo;
1135 /* If echo_object_init() is successful then ownership of oinfo
1136 * is transferred to the object. */
1137 conf->eoc_oinfo = &oinfo;
1139 fid = &info->eti_fid;
1140 rc = ostid_to_fid(fid, oi, 0);
1142 GOTO(out, eco = ERR_PTR(rc));
1144 /* In the function below, .hs_keycmp resolves to
1145 * lu_obj_hop_keycmp() */
1146 /* coverity[overrun-buffer-val] */
1147 obj = cl_object_find(env, echo_dev2cl(d), fid, &conf->eoc_cl);
1149 GOTO(out, eco = (void*)obj);
1151 eco = cl2echo_obj(obj);
1152 if (eco->eo_deleted) {
1153 cl_object_put(env, obj);
1154 eco = ERR_PTR(-EAGAIN);
1159 OBD_FREE_PTR(oinfo);
1161 cl_env_put(env, &refcheck);
1165 static int cl_echo_object_put(struct echo_object *eco)
1168 struct cl_object *obj = echo_obj2cl(eco);
1172 env = cl_env_get(&refcheck);
1174 RETURN(PTR_ERR(env));
1176 /* an external function to kill an object? */
1177 if (eco->eo_deleted) {
1178 struct lu_object_header *loh = obj->co_lu.lo_header;
1179 LASSERT(&eco->eo_hdr == luh2coh(loh));
1180 set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
1183 cl_object_put(env, obj);
1184 cl_env_put(env, &refcheck);
1188 static int cl_echo_enqueue0(struct lu_env *env, struct echo_object *eco,
1189 u64 start, u64 end, int mode,
1190 __u64 *cookie , __u32 enqflags)
1193 struct cl_lock *lck;
1194 struct cl_object *obj;
1195 struct cl_lock_descr *descr;
1196 struct echo_thread_info *info;
1200 info = echo_env_info(env);
1202 lck = &info->eti_lock;
1203 obj = echo_obj2cl(eco);
1205 memset(lck, 0, sizeof(*lck));
1206 descr = &lck->cll_descr;
1207 descr->cld_obj = obj;
1208 descr->cld_start = cl_index(obj, start);
1209 descr->cld_end = cl_index(obj, end);
1210 descr->cld_mode = mode == LCK_PW ? CLM_WRITE : CLM_READ;
1211 descr->cld_enq_flags = enqflags;
1214 rc = cl_lock_request(env, io, lck);
1216 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
1217 struct echo_lock *el;
1219 el = cl2echo_lock(cl_lock_at(lck, &echo_device_type));
1220 spin_lock(&ec->ec_lock);
1221 if (list_empty(&el->el_chain)) {
1222 list_add(&el->el_chain, &ec->ec_locks);
1223 el->el_cookie = ++ec->ec_unique;
1225 atomic_inc(&el->el_refcount);
1226 *cookie = el->el_cookie;
1227 spin_unlock(&ec->ec_lock);
1232 static int cl_echo_cancel0(struct lu_env *env, struct echo_device *ed,
1235 struct echo_client_obd *ec = ed->ed_ec;
1236 struct echo_lock *ecl = NULL;
1237 struct list_head *el;
1238 int found = 0, still_used = 0;
1241 LASSERT(ec != NULL);
1242 spin_lock(&ec->ec_lock);
1243 list_for_each(el, &ec->ec_locks) {
1244 ecl = list_entry(el, struct echo_lock, el_chain);
1245 CDEBUG(D_INFO, "ecl: %p, cookie: %#llx\n", ecl, ecl->el_cookie);
1246 found = (ecl->el_cookie == cookie);
1248 if (atomic_dec_and_test(&ecl->el_refcount))
1249 list_del_init(&ecl->el_chain);
1255 spin_unlock(&ec->ec_lock);
1260 echo_lock_release(env, ecl, still_used);
1264 static void echo_commit_callback(const struct lu_env *env, struct cl_io *io,
1265 struct cl_page *page)
1267 struct echo_thread_info *info;
1268 struct cl_2queue *queue;
1270 info = echo_env_info(env);
1271 LASSERT(io == &info->eti_io);
1273 queue = &info->eti_queue;
1274 cl_page_list_add(&queue->c2_qout, page);
1277 static int cl_echo_object_brw(struct echo_object *eco, int rw, u64 offset,
1278 struct page **pages, int npages, int async)
1281 struct echo_thread_info *info;
1282 struct cl_object *obj = echo_obj2cl(eco);
1283 struct echo_device *ed = eco->eo_dev;
1284 struct cl_2queue *queue;
1286 struct cl_page *clp;
1287 struct lustre_handle lh = { 0 };
1288 int page_size = cl_page_size(obj);
1294 LASSERT((offset & ~PAGE_MASK) == 0);
1295 LASSERT(ed->ed_next != NULL);
1296 env = cl_env_get(&refcheck);
1298 RETURN(PTR_ERR(env));
1300 info = echo_env_info(env);
1302 queue = &info->eti_queue;
1304 cl_2queue_init(queue);
1306 io->ci_ignore_layout = 1;
1307 rc = cl_io_init(env, io, CIT_MISC, obj);
1313 rc = cl_echo_enqueue0(env, eco, offset,
1314 offset + npages * PAGE_SIZE - 1,
1315 rw == READ ? LCK_PR : LCK_PW, &lh.cookie,
1318 GOTO(error_lock, rc);
1320 for (i = 0; i < npages; i++) {
1322 clp = cl_page_find(env, obj, cl_index(obj, offset),
1323 pages[i], CPT_TRANSIENT);
1328 LASSERT(clp->cp_type == CPT_TRANSIENT);
1330 rc = cl_page_own(env, io, clp);
1332 LASSERT(clp->cp_state == CPS_FREEING);
1333 cl_page_put(env, clp);
1337 cl_2queue_add(queue, clp);
1339 /* drop the reference count for cl_page_find, so that the page
1340 * will be freed in cl_2queue_fini. */
1341 cl_page_put(env, clp);
1342 cl_page_clip(env, clp, 0, page_size);
1344 offset += page_size;
1348 enum cl_req_type typ = rw == READ ? CRT_READ : CRT_WRITE;
1350 async = async && (typ == CRT_WRITE);
1352 rc = cl_io_commit_async(env, io, &queue->c2_qin,
1354 echo_commit_callback);
1356 rc = cl_io_submit_sync(env, io, typ, queue, 0);
1357 CDEBUG(D_INFO, "echo_client %s write returns %d\n",
1358 async ? "async" : "sync", rc);
1361 cl_echo_cancel0(env, ed, lh.cookie);
1364 cl_2queue_discard(env, io, queue);
1365 cl_2queue_disown(env, io, queue);
1366 cl_2queue_fini(env, queue);
1367 cl_io_fini(env, io);
1369 cl_env_put(env, &refcheck);
1372 /** @} echo_exports */
1375 static u64 last_object_id;
1377 #ifdef HAVE_SERVER_SUPPORT
1378 static inline void echo_md_build_name(struct lu_name *lname, char *name,
1381 snprintf(name, ETI_NAME_LEN, "%llu", id);
1382 lname->ln_name = name;
1383 lname->ln_namelen = strlen(name);
1386 /* similar to mdt_attr_get_complex */
1387 static int echo_big_lmm_get(const struct lu_env *env, struct md_object *o,
1390 struct echo_thread_info *info = echo_env_info(env);
1395 LASSERT(ma->ma_lmm_size > 0);
1397 LASSERT(ma->ma_need & (MA_LOV | MA_LMV));
1398 if (ma->ma_need & MA_LOV)
1399 rc = mo_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LOV);
1401 rc = mo_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LMV);
1406 /* big_lmm may need to be grown */
1407 if (info->eti_big_lmmsize < rc) {
1408 int size = size_roundup_power2(rc);
1410 if (info->eti_big_lmmsize > 0) {
1411 /* free old buffer */
1412 LASSERT(info->eti_big_lmm);
1413 OBD_FREE_LARGE(info->eti_big_lmm,
1414 info->eti_big_lmmsize);
1415 info->eti_big_lmm = NULL;
1416 info->eti_big_lmmsize = 0;
1419 OBD_ALLOC_LARGE(info->eti_big_lmm, size);
1420 if (info->eti_big_lmm == NULL)
1422 info->eti_big_lmmsize = size;
1424 LASSERT(info->eti_big_lmmsize >= rc);
1426 info->eti_buf.lb_buf = info->eti_big_lmm;
1427 info->eti_buf.lb_len = info->eti_big_lmmsize;
1428 if (ma->ma_need & MA_LOV)
1429 rc = mo_xattr_get(env, o, &info->eti_buf, XATTR_NAME_LOV);
1431 rc = mo_xattr_get(env, o, &info->eti_buf, XATTR_NAME_LMV);
1435 if (ma->ma_need & MA_LOV)
1436 ma->ma_valid |= MA_LOV;
1438 ma->ma_valid |= MA_LMV;
1440 ma->ma_lmm = info->eti_big_lmm;
1441 ma->ma_lmm_size = rc;
1446 static int echo_attr_get_complex(const struct lu_env *env,
1447 struct md_object *next,
1450 struct echo_thread_info *info = echo_env_info(env);
1451 struct lu_buf *buf = &info->eti_buf;
1452 umode_t mode = lu_object_attr(&next->mo_lu);
1459 if (ma->ma_need & MA_INODE) {
1460 rc = mo_attr_get(env, next, ma);
1463 ma->ma_valid |= MA_INODE;
1466 if ((ma->ma_need & MA_LOV) && (S_ISREG(mode) || S_ISDIR(mode))) {
1467 LASSERT(ma->ma_lmm_size > 0);
1468 buf->lb_buf = ma->ma_lmm;
1469 buf->lb_len = ma->ma_lmm_size;
1470 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LOV);
1472 ma->ma_lmm_size = rc2;
1473 ma->ma_valid |= MA_LOV;
1474 } else if (rc2 == -ENODATA) {
1476 ma->ma_lmm_size = 0;
1477 } else if (rc2 == -ERANGE) {
1478 rc2 = echo_big_lmm_get(env, next, ma);
1480 GOTO(out, rc = rc2);
1482 GOTO(out, rc = rc2);
1486 if ((ma->ma_need & MA_LMV) && S_ISDIR(mode)) {
1487 LASSERT(ma->ma_lmm_size > 0);
1488 buf->lb_buf = ma->ma_lmm;
1489 buf->lb_len = ma->ma_lmm_size;
1490 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LMV);
1492 ma->ma_lmm_size = rc2;
1493 ma->ma_valid |= MA_LMV;
1494 } else if (rc2 == -ENODATA) {
1496 ma->ma_lmm_size = 0;
1497 } else if (rc2 == -ERANGE) {
1498 rc2 = echo_big_lmm_get(env, next, ma);
1500 GOTO(out, rc = rc2);
1502 GOTO(out, rc = rc2);
1506 #ifdef CONFIG_FS_POSIX_ACL
1507 if ((ma->ma_need & MA_ACL_DEF) && S_ISDIR(mode)) {
1508 buf->lb_buf = ma->ma_acl;
1509 buf->lb_len = ma->ma_acl_size;
1510 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1512 ma->ma_acl_size = rc2;
1513 ma->ma_valid |= MA_ACL_DEF;
1514 } else if (rc2 == -ENODATA) {
1516 ma->ma_acl_size = 0;
1518 GOTO(out, rc = rc2);
1523 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = %#llx ma_lmm=%p\n",
1524 rc, ma->ma_valid, ma->ma_lmm);
1529 echo_md_create_internal(const struct lu_env *env, struct echo_device *ed,
1530 struct md_object *parent, struct lu_fid *fid,
1531 struct lu_name *lname, struct md_op_spec *spec,
1534 struct lu_object *ec_child, *child;
1535 struct lu_device *ld = ed->ed_next;
1536 struct echo_thread_info *info = echo_env_info(env);
1537 struct lu_fid *fid2 = &info->eti_fid2;
1538 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
1543 rc = mdo_lookup(env, parent, lname, fid2, spec);
1546 else if (rc != -ENOENT)
1549 ec_child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev,
1551 if (IS_ERR(ec_child)) {
1552 CERROR("Can not find the child "DFID": rc = %ld\n", PFID(fid),
1554 RETURN(PTR_ERR(ec_child));
1557 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1558 if (child == NULL) {
1559 CERROR("Can not locate the child "DFID"\n", PFID(fid));
1560 GOTO(out_put, rc = -EINVAL);
1563 CDEBUG(D_RPCTRACE, "Start creating object "DFID" %s %p\n",
1564 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1567 * Do not perform lookup sanity check. We know that name does not exist.
1569 spec->sp_cr_lookup = 0;
1570 rc = mdo_create(env, parent, lname, lu2md(child), spec, ma);
1572 CERROR("Can not create child "DFID": rc = %d\n", PFID(fid), rc);
1575 CDEBUG(D_RPCTRACE, "End creating object "DFID" %s %p rc = %d\n",
1576 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent, rc);
1579 lu_object_put(env, ec_child);
1583 static int echo_set_lmm_size(const struct lu_env *env, struct lu_device *ld,
1586 struct echo_thread_info *info = echo_env_info(env);
1588 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
1589 ma->ma_lmm = (void *)&info->eti_lmm;
1590 ma->ma_lmm_size = sizeof(info->eti_lmm);
1592 LASSERT(info->eti_big_lmmsize);
1593 ma->ma_lmm = info->eti_big_lmm;
1594 ma->ma_lmm_size = info->eti_big_lmmsize;
1601 echo_md_dir_stripe_choose(const struct lu_env *env, struct echo_device *ed,
1602 struct lu_object *obj, const char *name,
1603 unsigned int namelen, __u64 id,
1604 struct lu_object **new_parent)
1606 struct echo_thread_info *info = echo_env_info(env);
1607 struct md_attr *ma = &info->eti_ma;
1608 struct lmv_mds_md_v1 *lmv;
1609 struct lu_device *ld = ed->ed_next;
1611 struct lu_name tmp_ln_name;
1612 struct lu_fid stripe_fid;
1613 struct lu_object *stripe_obj;
1616 LASSERT(obj != NULL);
1617 LASSERT(S_ISDIR(obj->lo_header->loh_attr));
1619 memset(ma, 0, sizeof(*ma));
1620 echo_set_lmm_size(env, ld, ma);
1621 ma->ma_need = MA_LMV;
1622 rc = echo_attr_get_complex(env, lu2md(obj), ma);
1624 CERROR("Can not getattr child "DFID": rc = %d\n",
1625 PFID(lu_object_fid(obj)), rc);
1629 if (!(ma->ma_valid & MA_LMV)) {
1634 lmv = (struct lmv_mds_md_v1 *)ma->ma_lmm;
1635 if (le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_V1) {
1637 CERROR("Invalid mds md magic %x "DFID": rc = %d\n",
1638 le32_to_cpu(lmv->lmv_magic), PFID(lu_object_fid(obj)),
1644 tmp_ln_name.ln_name = name;
1645 tmp_ln_name.ln_namelen = namelen;
1648 echo_md_build_name(&tmp_ln_name, info->eti_name, id);
1651 idx = lmv_name_to_stripe_index(LMV_HASH_TYPE_FNV_1A_64,
1652 le32_to_cpu(lmv->lmv_stripe_count),
1653 tmp_ln_name.ln_name, tmp_ln_name.ln_namelen);
1655 LASSERT(idx < le32_to_cpu(lmv->lmv_stripe_count));
1656 fid_le_to_cpu(&stripe_fid, &lmv->lmv_stripe_fids[idx]);
1658 stripe_obj = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, &stripe_fid,
1660 if (IS_ERR(stripe_obj)) {
1661 rc = PTR_ERR(stripe_obj);
1662 CERROR("Can not find the parent "DFID": rc = %d\n",
1663 PFID(&stripe_fid), rc);
1667 *new_parent = lu_object_locate(stripe_obj->lo_header, ld->ld_type);
1668 if (*new_parent == NULL) {
1669 lu_object_put(env, stripe_obj);
1676 static int echo_create_md_object(const struct lu_env *env,
1677 struct echo_device *ed,
1678 struct lu_object *ec_parent,
1680 char *name, int namelen,
1681 __u64 id, __u32 mode, int count,
1682 int stripe_count, int stripe_offset)
1684 struct lu_object *parent;
1685 struct lu_object *new_parent;
1686 struct echo_thread_info *info = echo_env_info(env);
1687 struct lu_name *lname = &info->eti_lname;
1688 struct md_op_spec *spec = &info->eti_spec;
1689 struct md_attr *ma = &info->eti_ma;
1690 struct lu_device *ld = ed->ed_next;
1696 if (ec_parent == NULL)
1698 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1702 rc = echo_md_dir_stripe_choose(env, ed, parent, name, namelen,
1707 LASSERT(new_parent != NULL);
1708 memset(ma, 0, sizeof(*ma));
1709 memset(spec, 0, sizeof(*spec));
1710 echo_set_lmm_size(env, ld, ma);
1711 if (stripe_count != 0) {
1712 spec->sp_cr_flags |= MDS_FMODE_WRITE;
1713 if (stripe_count != -1) {
1714 if (S_ISDIR(mode)) {
1715 struct lmv_user_md *lmu;
1717 lmu = (struct lmv_user_md *)&info->eti_lum;
1718 lmu->lum_magic = LMV_USER_MAGIC;
1719 lmu->lum_stripe_offset = stripe_offset;
1720 lmu->lum_stripe_count = stripe_count;
1721 lmu->lum_hash_type = LMV_HASH_TYPE_FNV_1A_64;
1722 spec->u.sp_ea.eadata = lmu;
1723 spec->u.sp_ea.eadatalen = sizeof(*lmu);
1725 struct lov_user_md_v3 *lum = &info->eti_lum;
1727 lum->lmm_magic = LOV_USER_MAGIC_V3;
1728 lum->lmm_stripe_count = stripe_count;
1729 lum->lmm_stripe_offset = stripe_offset;
1730 lum->lmm_pattern = LOV_PATTERN_NONE;
1731 spec->u.sp_ea.eadata = lum;
1732 spec->u.sp_ea.eadatalen = sizeof(*lum);
1734 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1738 ma->ma_attr.la_mode = mode;
1739 ma->ma_attr.la_valid = LA_CTIME | LA_MODE;
1740 ma->ma_attr.la_ctime = ktime_get_real_seconds();
1743 lname->ln_name = name;
1744 lname->ln_namelen = namelen;
1745 /* If name is specified, only create one object by name */
1746 rc = echo_md_create_internal(env, ed, lu2md(new_parent), fid,
1751 /* Create multiple object sequenced by id */
1752 for (i = 0; i < count; i++) {
1753 char *tmp_name = info->eti_name;
1755 echo_md_build_name(lname, tmp_name, id);
1757 rc = echo_md_create_internal(env, ed, lu2md(new_parent),
1758 fid, lname, spec, ma);
1760 CERROR("Can not create child %s: rc = %d\n", tmp_name,
1769 if (new_parent != parent)
1770 lu_object_put(env, new_parent);
1775 static struct lu_object *echo_md_lookup(const struct lu_env *env,
1776 struct echo_device *ed,
1777 struct md_object *parent,
1778 struct lu_name *lname)
1780 struct echo_thread_info *info = echo_env_info(env);
1781 struct lu_fid *fid = &info->eti_fid;
1782 struct lu_object *child;
1786 CDEBUG(D_INFO, "lookup %s in parent "DFID" %p\n", lname->ln_name,
1789 rc = mdo_lookup(env, parent, lname, fid, NULL);
1791 CERROR("lookup %s: rc = %d\n", lname->ln_name, rc);
1792 RETURN(ERR_PTR(rc));
1795 /* In the function below, .hs_keycmp resolves to
1796 * lu_obj_hop_keycmp() */
1797 /* coverity[overrun-buffer-val] */
1798 child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1803 static int echo_setattr_object(const struct lu_env *env,
1804 struct echo_device *ed,
1805 struct lu_object *ec_parent,
1806 __u64 id, int count)
1808 struct lu_object *parent;
1809 struct lu_object *new_parent;
1810 struct echo_thread_info *info = echo_env_info(env);
1811 struct lu_name *lname = &info->eti_lname;
1812 char *name = info->eti_name;
1813 struct lu_device *ld = ed->ed_next;
1814 struct lu_buf *buf = &info->eti_buf;
1820 if (ec_parent == NULL)
1822 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1826 rc = echo_md_dir_stripe_choose(env, ed, parent, NULL, 0, id,
1831 for (i = 0; i < count; i++) {
1832 struct lu_object *ec_child, *child;
1834 echo_md_build_name(lname, name, id);
1836 ec_child = echo_md_lookup(env, ed, lu2md(new_parent), lname);
1837 if (IS_ERR(ec_child)) {
1838 rc = PTR_ERR(ec_child);
1839 CERROR("Can't find child %s: rc = %d\n",
1840 lname->ln_name, rc);
1844 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1845 if (child == NULL) {
1846 CERROR("Can not locate the child %s\n", lname->ln_name);
1847 lu_object_put(env, ec_child);
1852 CDEBUG(D_RPCTRACE, "Start setattr object "DFID"\n",
1853 PFID(lu_object_fid(child)));
1855 buf->lb_buf = info->eti_xattr_buf;
1856 buf->lb_len = sizeof(info->eti_xattr_buf);
1858 sprintf(name, "%s.test1", XATTR_USER_PREFIX);
1859 rc = mo_xattr_set(env, lu2md(child), buf, name,
1862 CERROR("Can not setattr child "DFID": rc = %d\n",
1863 PFID(lu_object_fid(child)), rc);
1864 lu_object_put(env, ec_child);
1867 CDEBUG(D_RPCTRACE, "End setattr object "DFID"\n",
1868 PFID(lu_object_fid(child)));
1870 lu_object_put(env, ec_child);
1873 if (new_parent != parent)
1874 lu_object_put(env, new_parent);
1879 static int echo_getattr_object(const struct lu_env *env,
1880 struct echo_device *ed,
1881 struct lu_object *ec_parent,
1882 __u64 id, int count)
1884 struct lu_object *parent;
1885 struct lu_object *new_parent;
1886 struct echo_thread_info *info = echo_env_info(env);
1887 struct lu_name *lname = &info->eti_lname;
1888 char *name = info->eti_name;
1889 struct md_attr *ma = &info->eti_ma;
1890 struct lu_device *ld = ed->ed_next;
1896 if (ec_parent == NULL)
1898 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1902 rc = echo_md_dir_stripe_choose(env, ed, parent, NULL, 0, id,
1907 memset(ma, 0, sizeof(*ma));
1908 ma->ma_need |= MA_INODE | MA_LOV | MA_PFID | MA_HSM | MA_ACL_DEF;
1909 ma->ma_acl = info->eti_xattr_buf;
1910 ma->ma_acl_size = sizeof(info->eti_xattr_buf);
1912 for (i = 0; i < count; i++) {
1913 struct lu_object *ec_child, *child;
1916 echo_md_build_name(lname, name, id);
1917 echo_set_lmm_size(env, ld, ma);
1919 ec_child = echo_md_lookup(env, ed, lu2md(new_parent), lname);
1920 if (IS_ERR(ec_child)) {
1921 CERROR("Can't find child %s: rc = %ld\n",
1922 lname->ln_name, PTR_ERR(ec_child));
1923 RETURN(PTR_ERR(ec_child));
1926 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1927 if (child == NULL) {
1928 CERROR("Can not locate the child %s\n", lname->ln_name);
1929 lu_object_put(env, ec_child);
1933 CDEBUG(D_RPCTRACE, "Start getattr object "DFID"\n",
1934 PFID(lu_object_fid(child)));
1935 rc = echo_attr_get_complex(env, lu2md(child), ma);
1937 CERROR("Can not getattr child "DFID": rc = %d\n",
1938 PFID(lu_object_fid(child)), rc);
1939 lu_object_put(env, ec_child);
1942 CDEBUG(D_RPCTRACE, "End getattr object "DFID"\n",
1943 PFID(lu_object_fid(child)));
1945 lu_object_put(env, ec_child);
1948 if (new_parent != parent)
1949 lu_object_put(env, new_parent);
1954 static int echo_lookup_object(const struct lu_env *env,
1955 struct echo_device *ed,
1956 struct lu_object *ec_parent,
1957 __u64 id, int count)
1959 struct lu_object *parent;
1960 struct lu_object *new_parent;
1961 struct echo_thread_info *info = echo_env_info(env);
1962 struct lu_name *lname = &info->eti_lname;
1963 char *name = info->eti_name;
1964 struct lu_fid *fid = &info->eti_fid;
1965 struct lu_device *ld = ed->ed_next;
1969 if (ec_parent == NULL)
1971 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1975 rc = echo_md_dir_stripe_choose(env, ed, parent, NULL, 0, id,
1980 /*prepare the requests*/
1981 for (i = 0; i < count; i++) {
1982 echo_md_build_name(lname, name, id);
1984 CDEBUG(D_RPCTRACE, "Start lookup object "DFID" %s %p\n",
1985 PFID(lu_object_fid(new_parent)), lname->ln_name,
1988 rc = mdo_lookup(env, lu2md(new_parent), lname, fid, NULL);
1990 CERROR("Can not lookup child %s: rc = %d\n", name, rc);
1994 CDEBUG(D_RPCTRACE, "End lookup object "DFID" %s %p\n",
1995 PFID(lu_object_fid(new_parent)), lname->ln_name,
2001 if (new_parent != parent)
2002 lu_object_put(env, new_parent);
2007 static int echo_md_destroy_internal(const struct lu_env *env,
2008 struct echo_device *ed,
2009 struct md_object *parent,
2010 struct lu_name *lname,
2013 struct lu_device *ld = ed->ed_next;
2014 struct lu_object *ec_child;
2015 struct lu_object *child;
2020 ec_child = echo_md_lookup(env, ed, parent, lname);
2021 if (IS_ERR(ec_child)) {
2022 CERROR("Can't find child %s: rc = %ld\n", lname->ln_name,
2024 RETURN(PTR_ERR(ec_child));
2027 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
2028 if (child == NULL) {
2029 CERROR("Can not locate the child %s\n", lname->ln_name);
2030 GOTO(out_put, rc = -EINVAL);
2033 if (lu_object_remote(child)) {
2034 CERROR("Can not destroy remote object %s: rc = %d\n",
2035 lname->ln_name, -EPERM);
2036 GOTO(out_put, rc = -EPERM);
2038 CDEBUG(D_RPCTRACE, "Start destroy object "DFID" %s %p\n",
2039 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
2041 rc = mdo_unlink(env, parent, lu2md(child), lname, ma, 0);
2043 CERROR("Can not unlink child %s: rc = %d\n",
2044 lname->ln_name, rc);
2047 CDEBUG(D_RPCTRACE, "End destroy object "DFID" %s %p\n",
2048 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
2050 lu_object_put(env, ec_child);
2054 static int echo_destroy_object(const struct lu_env *env,
2055 struct echo_device *ed,
2056 struct lu_object *ec_parent,
2057 char *name, int namelen,
2058 __u64 id, __u32 mode,
2061 struct echo_thread_info *info = echo_env_info(env);
2062 struct lu_name *lname = &info->eti_lname;
2063 struct md_attr *ma = &info->eti_ma;
2064 struct lu_device *ld = ed->ed_next;
2065 struct lu_object *parent;
2066 struct lu_object *new_parent;
2071 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
2075 rc = echo_md_dir_stripe_choose(env, ed, parent, name, namelen,
2080 memset(ma, 0, sizeof(*ma));
2081 ma->ma_attr.la_mode = mode;
2082 ma->ma_attr.la_valid = LA_CTIME;
2083 ma->ma_attr.la_ctime = ktime_get_real_seconds();
2084 ma->ma_need = MA_INODE;
2088 lname->ln_name = name;
2089 lname->ln_namelen = namelen;
2090 rc = echo_md_destroy_internal(env, ed, lu2md(new_parent), lname,
2095 /*prepare the requests*/
2096 for (i = 0; i < count; i++) {
2097 char *tmp_name = info->eti_name;
2100 echo_md_build_name(lname, tmp_name, id);
2102 rc = echo_md_destroy_internal(env, ed, lu2md(new_parent), lname,
2105 CERROR("Can not unlink child %s: rc = %d\n", name, rc);
2112 if (new_parent != parent)
2113 lu_object_put(env, new_parent);
2118 static struct lu_object *echo_resolve_path(const struct lu_env *env,
2119 struct echo_device *ed, char *path,
2122 struct lu_device *ld = ed->ed_next;
2123 struct echo_thread_info *info = echo_env_info(env);
2124 struct lu_fid *fid = &info->eti_fid;
2125 struct lu_name *lname = &info->eti_lname;
2126 struct lu_object *parent = NULL;
2127 struct lu_object *child = NULL;
2131 *fid = ed->ed_root_fid;
2133 /* In the function below, .hs_keycmp resolves to
2134 * lu_obj_hop_keycmp() */
2135 /* coverity[overrun-buffer-val] */
2136 parent = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
2137 if (IS_ERR(parent)) {
2138 CERROR("Can not find the parent "DFID": rc = %ld\n",
2139 PFID(fid), PTR_ERR(parent));
2144 struct lu_object *ld_parent;
2147 e = strsep(&path, "/");
2152 if (!path || path[0] == '\0')
2158 lname->ln_namelen = strlen(e);
2160 ld_parent = lu_object_locate(parent->lo_header, ld->ld_type);
2161 if (ld_parent == NULL) {
2162 lu_object_put(env, parent);
2167 child = echo_md_lookup(env, ed, lu2md(ld_parent), lname);
2168 lu_object_put(env, parent);
2169 if (IS_ERR(child)) {
2170 rc = (int)PTR_ERR(child);
2171 CERROR("lookup %s under parent "DFID": rc = %d\n",
2172 lname->ln_name, PFID(lu_object_fid(ld_parent)),
2179 RETURN(ERR_PTR(rc));
2184 static void echo_ucred_init(struct lu_env *env)
2186 struct lu_ucred *ucred = lu_ucred(env);
2188 ucred->uc_valid = UCRED_INVALID;
2190 ucred->uc_suppgids[0] = -1;
2191 ucred->uc_suppgids[1] = -1;
2193 ucred->uc_uid = ucred->uc_o_uid =
2194 from_kuid(&init_user_ns, current_uid());
2195 ucred->uc_gid = ucred->uc_o_gid =
2196 from_kgid(&init_user_ns, current_gid());
2197 ucred->uc_fsuid = ucred->uc_o_fsuid =
2198 from_kuid(&init_user_ns, current_fsuid());
2199 ucred->uc_fsgid = ucred->uc_o_fsgid =
2200 from_kgid(&init_user_ns, current_fsgid());
2201 ucred->uc_cap = cfs_curproc_cap_pack();
2203 /* remove fs privilege for non-root user. */
2204 if (ucred->uc_fsuid)
2205 ucred->uc_cap &= ~CFS_CAP_FS_MASK;
2206 ucred->uc_valid = UCRED_NEW;
2209 static void echo_ucred_fini(struct lu_env *env)
2211 struct lu_ucred *ucred = lu_ucred(env);
2212 ucred->uc_valid = UCRED_INIT;
2215 #define ECHO_MD_CTX_TAG (LCT_REMEMBER | LCT_MD_THREAD)
2216 #define ECHO_MD_SES_TAG (LCT_REMEMBER | LCT_SESSION | LCT_SERVER_SESSION)
2217 static int echo_md_handler(struct echo_device *ed, int command,
2218 char *path, int path_len, __u64 id, int count,
2219 struct obd_ioctl_data *data)
2221 struct echo_thread_info *info;
2222 struct lu_device *ld = ed->ed_next;
2225 struct lu_object *parent;
2227 int namelen = data->ioc_plen2;
2232 CERROR("MD echo client is not being initialized properly\n");
2236 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
2237 CERROR("Only support MDD layer right now!\n");
2241 env = cl_env_get(&refcheck);
2243 RETURN(PTR_ERR(env));
2245 rc = lu_env_refill_by_tags(env, ECHO_MD_CTX_TAG, ECHO_MD_SES_TAG);
2249 /* init big_lmm buffer */
2250 info = echo_env_info(env);
2251 LASSERT(info->eti_big_lmm == NULL);
2252 OBD_ALLOC_LARGE(info->eti_big_lmm, MIN_MD_SIZE);
2253 if (info->eti_big_lmm == NULL)
2254 GOTO(out_env, rc = -ENOMEM);
2255 info->eti_big_lmmsize = MIN_MD_SIZE;
2257 parent = echo_resolve_path(env, ed, path, path_len);
2258 if (IS_ERR(parent)) {
2259 CERROR("Can not resolve the path %s: rc = %ld\n", path,
2261 GOTO(out_free, rc = PTR_ERR(parent));
2265 OBD_ALLOC(name, namelen + 1);
2267 GOTO(out_put, rc = -ENOMEM);
2268 if (copy_from_user(name, data->ioc_pbuf2, namelen))
2269 GOTO(out_name, rc = -EFAULT);
2272 echo_ucred_init(env);
2275 case ECHO_MD_CREATE:
2276 case ECHO_MD_MKDIR: {
2277 struct echo_thread_info *info = echo_env_info(env);
2278 __u32 mode = data->ioc_obdo2.o_mode;
2279 struct lu_fid *fid = &info->eti_fid;
2280 int stripe_count = (int)data->ioc_obdo2.o_misc;
2281 int stripe_index = (int)data->ioc_obdo2.o_stripe_idx;
2283 rc = ostid_to_fid(fid, &data->ioc_obdo1.o_oi, 0);
2287 /* In the function below, .hs_keycmp resolves to
2288 * lu_obj_hop_keycmp() */
2289 /* coverity[overrun-buffer-val] */
2290 rc = echo_create_md_object(env, ed, parent, fid, name, namelen,
2291 id, mode, count, stripe_count,
2295 case ECHO_MD_DESTROY:
2296 case ECHO_MD_RMDIR: {
2297 __u32 mode = data->ioc_obdo2.o_mode;
2299 rc = echo_destroy_object(env, ed, parent, name, namelen,
2303 case ECHO_MD_LOOKUP:
2304 rc = echo_lookup_object(env, ed, parent, id, count);
2306 case ECHO_MD_GETATTR:
2307 rc = echo_getattr_object(env, ed, parent, id, count);
2309 case ECHO_MD_SETATTR:
2310 rc = echo_setattr_object(env, ed, parent, id, count);
2313 CERROR("unknown command %d\n", command);
2317 echo_ucred_fini(env);
2321 OBD_FREE(name, namelen + 1);
2323 lu_object_put(env, parent);
2325 LASSERT(info->eti_big_lmm);
2326 OBD_FREE_LARGE(info->eti_big_lmm, info->eti_big_lmmsize);
2327 info->eti_big_lmm = NULL;
2328 info->eti_big_lmmsize = 0;
2330 cl_env_put(env, &refcheck);
2333 #endif /* HAVE_SERVER_SUPPORT */
2335 static int echo_create_object(const struct lu_env *env, struct echo_device *ed,
2338 struct echo_object *eco;
2339 struct echo_client_obd *ec = ed->ed_ec;
2344 if (!(oa->o_valid & OBD_MD_FLID) ||
2345 !(oa->o_valid & OBD_MD_FLGROUP) ||
2346 !fid_seq_is_echo(ostid_seq(&oa->o_oi))) {
2347 CERROR("invalid oid "DOSTID"\n", POSTID(&oa->o_oi));
2351 if (ostid_id(&oa->o_oi) == 0) {
2352 rc = ostid_set_id(&oa->o_oi, ++last_object_id);
2357 rc = obd_create(env, ec->ec_exp, oa);
2359 CERROR("Cannot create objects: rc = %d\n", rc);
2365 oa->o_valid |= OBD_MD_FLID;
2367 eco = cl_echo_object_find(ed, &oa->o_oi);
2369 GOTO(failed, rc = PTR_ERR(eco));
2370 cl_echo_object_put(eco);
2372 CDEBUG(D_INFO, "oa oid "DOSTID"\n", POSTID(&oa->o_oi));
2376 if (created && rc != 0)
2377 obd_destroy(env, ec->ec_exp, oa);
2380 CERROR("create object failed with: rc = %d\n", rc);
2385 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
2388 struct echo_object *eco;
2392 if (!(oa->o_valid & OBD_MD_FLID) ||
2393 !(oa->o_valid & OBD_MD_FLGROUP) ||
2394 ostid_id(&oa->o_oi) == 0) {
2395 CERROR("invalid oid "DOSTID"\n", POSTID(&oa->o_oi));
2400 eco = cl_echo_object_find(ed, &oa->o_oi);
2409 static void echo_put_object(struct echo_object *eco)
2413 rc = cl_echo_object_put(eco);
2415 CERROR("%s: echo client drop an object failed: rc = %d\n",
2416 eco->eo_dev->ed_ec->ec_exp->exp_obd->obd_name, rc);
2419 static void echo_client_page_debug_setup(struct page *page, int rw, u64 id,
2420 u64 offset, u64 count)
2427 /* no partial pages on the client */
2428 LASSERT(count == PAGE_SIZE);
2432 for (delta = 0; delta < PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2433 if (rw == OBD_BRW_WRITE) {
2434 stripe_off = offset + delta;
2437 stripe_off = 0xdeadbeef00c0ffeeULL;
2438 stripe_id = 0xdeadbeef00c0ffeeULL;
2440 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
2441 stripe_off, stripe_id);
2448 echo_client_page_debug_check(struct page *page, u64 id, u64 offset, u64 count)
2457 /* no partial pages on the client */
2458 LASSERT(count == PAGE_SIZE);
2462 for (rc = delta = 0; delta < PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2463 stripe_off = offset + delta;
2466 rc2 = block_debug_check("test_brw",
2467 addr + delta, OBD_ECHO_BLOCK_SIZE,
2468 stripe_off, stripe_id);
2470 CERROR("Error in echo object %#llx\n", id);
2479 static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
2480 struct echo_object *eco, u64 offset,
2481 u64 count, int async)
2484 struct brw_page *pga;
2485 struct brw_page *pgp;
2486 struct page **pages;
2495 verify = (ostid_id(&oa->o_oi) != ECHO_PERSISTENT_OBJID &&
2496 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
2497 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
2499 gfp_mask = ((ostid_id(&oa->o_oi) & 2) == 0) ? GFP_KERNEL : GFP_HIGHUSER;
2501 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
2503 if ((count & (~PAGE_MASK)) != 0)
2506 /* XXX think again with misaligned I/O */
2507 npages = count >> PAGE_SHIFT;
2509 if (rw == OBD_BRW_WRITE)
2510 brw_flags = OBD_BRW_ASYNC;
2512 OBD_ALLOC(pga, npages * sizeof(*pga));
2516 OBD_ALLOC(pages, npages * sizeof(*pages));
2517 if (pages == NULL) {
2518 OBD_FREE(pga, npages * sizeof(*pga));
2522 for (i = 0, pgp = pga, off = offset;
2524 i++, pgp++, off += PAGE_SIZE) {
2526 LASSERT(pgp->pg == NULL); /* for cleanup */
2529 pgp->pg = alloc_page(gfp_mask);
2530 if (pgp->pg == NULL)
2534 pgp->count = PAGE_SIZE;
2536 pgp->flag = brw_flags;
2539 echo_client_page_debug_setup(pgp->pg, rw,
2540 ostid_id(&oa->o_oi), off,
2544 /* brw mode can only be used at client */
2545 LASSERT(ed->ed_next != NULL);
2546 rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
2549 if (rc != 0 || rw != OBD_BRW_READ)
2552 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
2553 if (pgp->pg == NULL)
2558 vrc = echo_client_page_debug_check(pgp->pg,
2559 ostid_id(&oa->o_oi),
2560 pgp->off, pgp->count);
2561 if (vrc != 0 && rc == 0)
2564 __free_page(pgp->pg);
2566 OBD_FREE(pga, npages * sizeof(*pga));
2567 OBD_FREE(pages, npages * sizeof(*pages));
2571 static int echo_client_prep_commit(const struct lu_env *env,
2572 struct obd_export *exp, int rw,
2573 struct obdo *oa, struct echo_object *eco,
2574 u64 offset, u64 count,
2575 u64 batch, int async)
2577 struct obd_ioobj ioo;
2578 struct niobuf_local *lnb;
2579 struct niobuf_remote rnb;
2581 u64 npages, tot_pages, apc;
2582 int i, ret = 0, brw_flags = 0;
2586 if (count <= 0 || (count & ~PAGE_MASK) != 0)
2589 apc = npages = batch >> PAGE_SHIFT;
2590 tot_pages = count >> PAGE_SHIFT;
2592 OBD_ALLOC_LARGE(lnb, apc * sizeof(struct niobuf_local));
2596 if (rw == OBD_BRW_WRITE && async)
2597 brw_flags |= OBD_BRW_ASYNC;
2599 obdo_to_ioobj(oa, &ioo);
2603 for (; tot_pages > 0; tot_pages -= npages) {
2606 if (tot_pages < npages)
2609 rnb.rnb_offset = off;
2610 rnb.rnb_len = npages * PAGE_SIZE;
2611 rnb.rnb_flags = brw_flags;
2613 off += npages * PAGE_SIZE;
2616 ret = obd_preprw(env, rw, exp, oa, 1, &ioo, &rnb, &lpages, lnb);
2620 for (i = 0; i < lpages; i++) {
2621 struct page *page = lnb[i].lnb_page;
2623 /* read past eof? */
2624 if (page == NULL && lnb[i].lnb_rc == 0)
2628 lnb[i].lnb_flags |= OBD_BRW_ASYNC;
2630 if (ostid_id(&oa->o_oi) == ECHO_PERSISTENT_OBJID ||
2631 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
2632 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
2635 if (rw == OBD_BRW_WRITE)
2636 echo_client_page_debug_setup(page, rw,
2637 ostid_id(&oa->o_oi),
2638 lnb[i].lnb_file_offset,
2641 echo_client_page_debug_check(page,
2642 ostid_id(&oa->o_oi),
2643 lnb[i].lnb_file_offset,
2647 ret = obd_commitrw(env, rw, exp, oa, 1, &ioo, &rnb, npages, lnb,
2652 /* Reuse env context. */
2653 lu_context_exit((struct lu_context *)&env->le_ctx);
2654 lu_context_enter((struct lu_context *)&env->le_ctx);
2658 OBD_FREE_LARGE(lnb, apc * sizeof(struct niobuf_local));
2663 static int echo_client_brw_ioctl(const struct lu_env *env, int rw,
2664 struct obd_export *exp,
2665 struct obd_ioctl_data *data)
2667 struct obd_device *obd = class_exp2obd(exp);
2668 struct echo_device *ed = obd2echo_dev(obd);
2669 struct echo_client_obd *ec = ed->ed_ec;
2670 struct obdo *oa = &data->ioc_obdo1;
2671 struct echo_object *eco;
2677 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2679 rc = echo_get_object(&eco, ed, oa);
2683 oa->o_valid &= ~OBD_MD_FLHANDLE;
2685 /* OFD/obdfilter works only via prep/commit */
2686 test_mode = (long)data->ioc_pbuf1;
2687 if (ed->ed_next == NULL && test_mode != 3) {
2689 data->ioc_plen1 = data->ioc_count;
2695 /* Truncate batch size to maximum */
2696 if (data->ioc_plen1 > PTLRPC_MAX_BRW_SIZE)
2697 data->ioc_plen1 = PTLRPC_MAX_BRW_SIZE;
2699 switch (test_mode) {
2703 rc = echo_client_kbrw(ed, rw, oa, eco, data->ioc_offset,
2704 data->ioc_count, async);
2707 rc = echo_client_prep_commit(env, ec->ec_exp, rw, oa, eco,
2708 data->ioc_offset, data->ioc_count,
2709 data->ioc_plen1, async);
2715 echo_put_object(eco);
2721 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2722 void *karg, void __user *uarg)
2724 #ifdef HAVE_SERVER_SUPPORT
2725 struct tgt_session_info *tsi;
2727 struct obd_device *obd = exp->exp_obd;
2728 struct echo_device *ed = obd2echo_dev(obd);
2729 struct echo_client_obd *ec = ed->ed_ec;
2730 struct echo_object *eco;
2731 struct obd_ioctl_data *data = karg;
2735 int rw = OBD_BRW_READ;
2737 #ifdef HAVE_SERVER_SUPPORT
2738 struct lu_context echo_session;
2742 oa = &data->ioc_obdo1;
2743 if (!(oa->o_valid & OBD_MD_FLGROUP)) {
2744 oa->o_valid |= OBD_MD_FLGROUP;
2745 ostid_set_seq_echo(&oa->o_oi);
2748 /* This FID is unpacked just for validation at this point */
2749 rc = ostid_to_fid(&fid, &oa->o_oi, 0);
2757 rc = lu_env_init(env, LCT_DT_THREAD);
2759 GOTO(out_alloc, rc = -ENOMEM);
2761 #ifdef HAVE_SERVER_SUPPORT
2762 env->le_ses = &echo_session;
2763 rc = lu_context_init(env->le_ses, LCT_SERVER_SESSION | LCT_NOREF);
2764 if (unlikely(rc < 0))
2766 lu_context_enter(env->le_ses);
2768 tsi = tgt_ses_info(env);
2769 tsi->tsi_exp = ec->ec_exp;
2770 tsi->tsi_jobid = NULL;
2773 case OBD_IOC_CREATE: /* may create echo object */
2774 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2775 GOTO (out, rc = -EPERM);
2777 rc = echo_create_object(env, ed, oa);
2780 #ifdef HAVE_SERVER_SUPPORT
2781 case OBD_IOC_ECHO_MD: {
2788 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2789 GOTO(out, rc = -EPERM);
2791 count = data->ioc_count;
2792 cmd = data->ioc_command;
2794 id = data->ioc_obdo2.o_oi.oi.oi_id;
2795 dirlen = data->ioc_plen1;
2796 OBD_ALLOC(dir, dirlen + 1);
2798 GOTO(out, rc = -ENOMEM);
2800 if (copy_from_user(dir, data->ioc_pbuf1, dirlen)) {
2801 OBD_FREE(dir, data->ioc_plen1 + 1);
2802 GOTO(out, rc = -EFAULT);
2805 rc = echo_md_handler(ed, cmd, dir, dirlen, id, count, data);
2806 OBD_FREE(dir, dirlen + 1);
2809 case OBD_IOC_ECHO_ALLOC_SEQ: {
2810 struct lu_env *cl_env;
2815 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2816 GOTO(out, rc = -EPERM);
2818 cl_env = cl_env_get(&refcheck);
2820 GOTO(out, rc = PTR_ERR(cl_env));
2822 rc = lu_env_refill_by_tags(cl_env, ECHO_MD_CTX_TAG,
2825 cl_env_put(cl_env, &refcheck);
2829 rc = seq_client_get_seq(cl_env, ed->ed_cl_seq, &seq);
2830 cl_env_put(cl_env, &refcheck);
2832 CERROR("%s: Can not alloc seq: rc = %d\n",
2837 if (copy_to_user(data->ioc_pbuf1, &seq, data->ioc_plen1))
2840 max_count = LUSTRE_METADATA_SEQ_MAX_WIDTH;
2841 if (copy_to_user(data->ioc_pbuf2, &max_count,
2846 #endif /* HAVE_SERVER_SUPPORT */
2847 case OBD_IOC_DESTROY:
2848 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2849 GOTO (out, rc = -EPERM);
2851 rc = echo_get_object(&eco, ed, oa);
2853 rc = obd_destroy(env, ec->ec_exp, oa);
2855 eco->eo_deleted = 1;
2856 echo_put_object(eco);
2860 case OBD_IOC_GETATTR:
2861 rc = echo_get_object(&eco, ed, oa);
2863 rc = obd_getattr(env, ec->ec_exp, oa);
2864 echo_put_object(eco);
2868 case OBD_IOC_SETATTR:
2869 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2870 GOTO (out, rc = -EPERM);
2872 rc = echo_get_object(&eco, ed, oa);
2874 rc = obd_setattr(env, ec->ec_exp, oa);
2875 echo_put_object(eco);
2879 case OBD_IOC_BRW_WRITE:
2880 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2881 GOTO (out, rc = -EPERM);
2885 case OBD_IOC_BRW_READ:
2886 rc = echo_client_brw_ioctl(env, rw, exp, data);
2890 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
2891 GOTO (out, rc = -ENOTTY);
2896 #ifdef HAVE_SERVER_SUPPORT
2897 lu_context_exit(env->le_ses);
2898 lu_context_fini(env->le_ses);
2908 static int echo_client_setup(const struct lu_env *env,
2909 struct obd_device *obddev, struct lustre_cfg *lcfg)
2911 struct echo_client_obd *ec = &obddev->u.echo_client;
2912 struct obd_device *tgt;
2913 struct obd_uuid echo_uuid = { "ECHO_UUID" };
2914 struct obd_connect_data *ocd = NULL;
2918 if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
2919 CERROR("requires a TARGET OBD name\n");
2923 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
2924 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
2925 CERROR("device not attached or not set up (%s)\n",
2926 lustre_cfg_string(lcfg, 1));
2930 spin_lock_init(&ec->ec_lock);
2931 INIT_LIST_HEAD(&ec->ec_objects);
2932 INIT_LIST_HEAD(&ec->ec_locks);
2935 if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
2936 #ifdef HAVE_SERVER_SUPPORT
2937 lu_context_tags_update(ECHO_MD_CTX_TAG);
2938 lu_session_tags_update(ECHO_MD_SES_TAG);
2940 CERROR("Local operations are NOT supported on client side. "
2941 "Only remote operations are supported. Metadata client "
2942 "must be run on server side.\n");
2947 OBD_ALLOC(ocd, sizeof(*ocd));
2949 CERROR("Can't alloc ocd connecting to %s\n",
2950 lustre_cfg_string(lcfg, 1));
2954 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
2955 OBD_CONNECT_BRW_SIZE |
2956 OBD_CONNECT_GRANT | OBD_CONNECT_FULL20 |
2957 OBD_CONNECT_64BITHASH | OBD_CONNECT_LVB_TYPE |
2959 ocd->ocd_brw_size = DT_MAX_BRW_SIZE;
2960 ocd->ocd_version = LUSTRE_VERSION_CODE;
2961 ocd->ocd_group = FID_SEQ_ECHO;
2963 rc = obd_connect(env, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
2965 /* Turn off pinger because it connects to tgt obd directly. */
2966 spin_lock(&tgt->obd_dev_lock);
2967 list_del_init(&ec->ec_exp->exp_obd_chain_timed);
2968 spin_unlock(&tgt->obd_dev_lock);
2971 OBD_FREE(ocd, sizeof(*ocd));
2974 CERROR("fail to connect to device %s\n",
2975 lustre_cfg_string(lcfg, 1));
2982 static int echo_client_cleanup(struct obd_device *obddev)
2984 struct echo_device *ed = obd2echo_dev(obddev);
2985 struct echo_client_obd *ec = &obddev->u.echo_client;
2989 /*Do nothing for Metadata echo client*/
2993 if (ed->ed_next_ismd) {
2994 #ifdef HAVE_SERVER_SUPPORT
2995 lu_context_tags_clear(ECHO_MD_CTX_TAG);
2996 lu_session_tags_clear(ECHO_MD_SES_TAG);
2998 CERROR("This is client-side only module, does not support "
2999 "metadata echo client.\n");
3004 if (!list_empty(&obddev->obd_exports)) {
3005 CERROR("still has clients!\n");
3009 LASSERT(atomic_read(&ec->ec_exp->exp_refcount) > 0);
3010 rc = obd_disconnect(ec->ec_exp);
3012 CERROR("fail to disconnect device: %d\n", rc);
3017 static int echo_client_connect(const struct lu_env *env,
3018 struct obd_export **exp,
3019 struct obd_device *src, struct obd_uuid *cluuid,
3020 struct obd_connect_data *data, void *localdata)
3023 struct lustre_handle conn = { 0 };
3026 rc = class_connect(&conn, src, cluuid);
3028 *exp = class_conn2export(&conn);
3034 static int echo_client_disconnect(struct obd_export *exp)
3040 GOTO(out, rc = -EINVAL);
3042 rc = class_disconnect(exp);
3048 static struct obd_ops echo_client_obd_ops = {
3049 .o_owner = THIS_MODULE,
3050 .o_iocontrol = echo_client_iocontrol,
3051 .o_connect = echo_client_connect,
3052 .o_disconnect = echo_client_disconnect
3055 static int __init obdecho_init(void)
3060 LCONSOLE_INFO("Echo OBD driver; http://www.lustre.org/\n");
3062 LASSERT(PAGE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
3064 # ifdef HAVE_SERVER_SUPPORT
3065 rc = echo_persistent_pages_init();
3069 rc = class_register_type(&echo_obd_ops, NULL, true, NULL,
3070 LUSTRE_ECHO_NAME, &echo_srv_type);
3075 rc = lu_kmem_init(echo_caches);
3077 rc = class_register_type(&echo_client_obd_ops, NULL, false,
3078 NULL, LUSTRE_ECHO_CLIENT_NAME,
3081 lu_kmem_fini(echo_caches);
3084 # ifdef HAVE_SERVER_SUPPORT
3088 class_unregister_type(LUSTRE_ECHO_NAME);
3090 echo_persistent_pages_fini();
3096 static void __exit obdecho_exit(void)
3098 class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
3099 lu_kmem_fini(echo_caches);
3101 #ifdef HAVE_SERVER_SUPPORT
3102 class_unregister_type(LUSTRE_ECHO_NAME);
3103 echo_persistent_pages_fini();
3107 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
3108 MODULE_DESCRIPTION("Lustre Echo Client test driver");
3109 MODULE_VERSION(LUSTRE_VERSION_STRING);
3110 MODULE_LICENSE("GPL");
3112 module_init(obdecho_init);
3113 module_exit(obdecho_exit);
3115 /** @} echo_client */