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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_ECHO
39 #include <libcfs/libcfs.h>
41 #include <liblustre.h>
45 #include <obd_support.h>
46 #include <obd_class.h>
47 #include <lustre_debug.h>
48 #include <lprocfs_status.h>
49 #include <cl_object.h>
50 #include <md_object.h>
51 #include <lustre_fid.h>
52 #include <lustre_acl.h>
53 #include <lustre_net.h>
55 #include "echo_internal.h"
57 /** \defgroup echo_client Echo Client
62 struct cl_device ed_cl;
63 struct echo_client_obd *ed_ec;
65 struct cl_site ed_site_myself;
66 struct cl_site *ed_site;
67 struct lu_device *ed_next;
70 struct lu_client_seq *ed_cl_seq;
74 struct cl_object eo_cl;
75 struct cl_object_header eo_hdr;
77 struct echo_device *eo_dev;
78 cfs_list_t eo_obj_chain;
79 struct lov_stripe_md *eo_lsm;
84 struct echo_object_conf {
85 struct cl_object_conf eoc_cl;
86 struct lov_stripe_md **eoc_md;
90 struct cl_page_slice ep_cl;
95 struct cl_lock_slice el_cl;
97 struct echo_object *el_object;
102 static int echo_client_setup(const struct lu_env *env,
103 struct obd_device *obddev,
104 struct lustre_cfg *lcfg);
105 static int echo_client_cleanup(struct obd_device *obddev);
108 /** \defgroup echo_helpers Helper functions
111 static inline struct echo_device *cl2echo_dev(const struct cl_device *dev)
113 return container_of0(dev, struct echo_device, ed_cl);
116 static inline struct cl_device *echo_dev2cl(struct echo_device *d)
121 static inline struct echo_device *obd2echo_dev(const struct obd_device *obd)
123 return cl2echo_dev(lu2cl_dev(obd->obd_lu_dev));
126 static inline struct cl_object *echo_obj2cl(struct echo_object *eco)
131 static inline struct echo_object *cl2echo_obj(const struct cl_object *o)
133 return container_of(o, struct echo_object, eo_cl);
136 static inline struct echo_page *cl2echo_page(const struct cl_page_slice *s)
138 return container_of(s, struct echo_page, ep_cl);
141 static inline struct echo_lock *cl2echo_lock(const struct cl_lock_slice *s)
143 return container_of(s, struct echo_lock, el_cl);
146 static inline struct cl_lock *echo_lock2cl(const struct echo_lock *ecl)
148 return ecl->el_cl.cls_lock;
151 static struct lu_context_key echo_thread_key;
152 static inline struct echo_thread_info *echo_env_info(const struct lu_env *env)
154 struct echo_thread_info *info;
155 info = lu_context_key_get(&env->le_ctx, &echo_thread_key);
156 LASSERT(info != NULL);
161 struct echo_object_conf *cl2echo_conf(const struct cl_object_conf *c)
163 return container_of(c, struct echo_object_conf, eoc_cl);
166 /** @} echo_helpers */
168 static struct echo_object *cl_echo_object_find(struct echo_device *d,
169 struct lov_stripe_md **lsm);
170 static int cl_echo_object_put(struct echo_object *eco);
171 static int cl_echo_enqueue (struct echo_object *eco, obd_off start,
172 obd_off end, int mode, __u64 *cookie);
173 static int cl_echo_cancel (struct echo_device *d, __u64 cookie);
174 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
175 struct page **pages, int npages, int async);
177 struct echo_thread_info {
178 struct echo_object_conf eti_conf;
179 struct lustre_md eti_md;
181 struct cl_2queue eti_queue;
183 struct cl_lock_descr eti_descr;
184 struct lu_fid eti_fid;
185 struct lu_fid eti_fid2;
186 #ifdef HAVE_SERVER_SUPPORT
187 struct md_op_spec eti_spec;
188 struct lov_mds_md_v3 eti_lmm;
189 struct lov_user_md_v3 eti_lum;
190 struct md_attr eti_ma;
191 struct lu_name eti_lname;
192 /* per-thread values, can be re-used */
196 struct lu_buf eti_buf;
197 char eti_xattr_buf[LUSTRE_POSIX_ACL_MAX_SIZE];
201 /* No session used right now */
202 struct echo_session_info {
206 static struct kmem_cache *echo_lock_kmem;
207 static struct kmem_cache *echo_object_kmem;
208 static struct kmem_cache *echo_thread_kmem;
209 static struct kmem_cache *echo_session_kmem;
210 /* static struct kmem_cache *echo_req_kmem; */
212 static struct lu_kmem_descr echo_caches[] = {
214 .ckd_cache = &echo_lock_kmem,
215 .ckd_name = "echo_lock_kmem",
216 .ckd_size = sizeof (struct echo_lock)
219 .ckd_cache = &echo_object_kmem,
220 .ckd_name = "echo_object_kmem",
221 .ckd_size = sizeof (struct echo_object)
224 .ckd_cache = &echo_thread_kmem,
225 .ckd_name = "echo_thread_kmem",
226 .ckd_size = sizeof (struct echo_thread_info)
229 .ckd_cache = &echo_session_kmem,
230 .ckd_name = "echo_session_kmem",
231 .ckd_size = sizeof (struct echo_session_info)
238 /** \defgroup echo_page Page operations
240 * Echo page operations.
244 static int echo_page_own(const struct lu_env *env,
245 const struct cl_page_slice *slice,
246 struct cl_io *io, int nonblock)
248 struct echo_page *ep = cl2echo_page(slice);
251 mutex_lock(&ep->ep_lock);
252 else if (!mutex_trylock(&ep->ep_lock))
257 static void echo_page_disown(const struct lu_env *env,
258 const struct cl_page_slice *slice,
261 struct echo_page *ep = cl2echo_page(slice);
263 LASSERT(mutex_is_locked(&ep->ep_lock));
264 mutex_unlock(&ep->ep_lock);
267 static void echo_page_discard(const struct lu_env *env,
268 const struct cl_page_slice *slice,
269 struct cl_io *unused)
271 cl_page_delete(env, slice->cpl_page);
274 static int echo_page_is_vmlocked(const struct lu_env *env,
275 const struct cl_page_slice *slice)
277 if (mutex_is_locked(&cl2echo_page(slice)->ep_lock))
282 static void echo_page_completion(const struct lu_env *env,
283 const struct cl_page_slice *slice,
286 LASSERT(slice->cpl_page->cp_sync_io != NULL);
289 static void echo_page_fini(const struct lu_env *env,
290 struct cl_page_slice *slice)
292 struct echo_object *eco = cl2echo_obj(slice->cpl_obj);
295 atomic_dec(&eco->eo_npages);
296 page_cache_release(slice->cpl_page->cp_vmpage);
300 static int echo_page_prep(const struct lu_env *env,
301 const struct cl_page_slice *slice,
302 struct cl_io *unused)
307 static int echo_page_print(const struct lu_env *env,
308 const struct cl_page_slice *slice,
309 void *cookie, lu_printer_t printer)
311 struct echo_page *ep = cl2echo_page(slice);
313 (*printer)(env, cookie, LUSTRE_ECHO_CLIENT_NAME"-page@%p %d vm@%p\n",
314 ep, mutex_is_locked(&ep->ep_lock),
315 slice->cpl_page->cp_vmpage);
319 static const struct cl_page_operations echo_page_ops = {
320 .cpo_own = echo_page_own,
321 .cpo_disown = echo_page_disown,
322 .cpo_discard = echo_page_discard,
323 .cpo_fini = echo_page_fini,
324 .cpo_print = echo_page_print,
325 .cpo_is_vmlocked = echo_page_is_vmlocked,
328 .cpo_prep = echo_page_prep,
329 .cpo_completion = echo_page_completion,
332 .cpo_prep = echo_page_prep,
333 .cpo_completion = echo_page_completion,
339 /** \defgroup echo_lock Locking
341 * echo lock operations
345 static void echo_lock_fini(const struct lu_env *env,
346 struct cl_lock_slice *slice)
348 struct echo_lock *ecl = cl2echo_lock(slice);
350 LASSERT(cfs_list_empty(&ecl->el_chain));
351 OBD_SLAB_FREE_PTR(ecl, echo_lock_kmem);
354 static void echo_lock_delete(const struct lu_env *env,
355 const struct cl_lock_slice *slice)
357 struct echo_lock *ecl = cl2echo_lock(slice);
359 LASSERT(cfs_list_empty(&ecl->el_chain));
362 static int echo_lock_fits_into(const struct lu_env *env,
363 const struct cl_lock_slice *slice,
364 const struct cl_lock_descr *need,
365 const struct cl_io *unused)
370 static struct cl_lock_operations echo_lock_ops = {
371 .clo_fini = echo_lock_fini,
372 .clo_delete = echo_lock_delete,
373 .clo_fits_into = echo_lock_fits_into
378 /** \defgroup echo_cl_ops cl_object operations
380 * operations for cl_object
384 static int echo_page_init(const struct lu_env *env, struct cl_object *obj,
385 struct cl_page *page, pgoff_t index)
387 struct echo_page *ep = cl_object_page_slice(obj, page);
388 struct echo_object *eco = cl2echo_obj(obj);
391 page_cache_get(page->cp_vmpage);
392 mutex_init(&ep->ep_lock);
393 cl_page_slice_add(page, &ep->ep_cl, obj, index, &echo_page_ops);
394 atomic_inc(&eco->eo_npages);
398 static int echo_io_init(const struct lu_env *env, struct cl_object *obj,
404 static int echo_lock_init(const struct lu_env *env,
405 struct cl_object *obj, struct cl_lock *lock,
406 const struct cl_io *unused)
408 struct echo_lock *el;
411 OBD_SLAB_ALLOC_PTR_GFP(el, echo_lock_kmem, GFP_NOFS);
413 cl_lock_slice_add(lock, &el->el_cl, obj, &echo_lock_ops);
414 el->el_object = cl2echo_obj(obj);
415 CFS_INIT_LIST_HEAD(&el->el_chain);
416 atomic_set(&el->el_refcount, 0);
418 RETURN(el == NULL ? -ENOMEM : 0);
421 static int echo_conf_set(const struct lu_env *env, struct cl_object *obj,
422 const struct cl_object_conf *conf)
427 static const struct cl_object_operations echo_cl_obj_ops = {
428 .coo_page_init = echo_page_init,
429 .coo_lock_init = echo_lock_init,
430 .coo_io_init = echo_io_init,
431 .coo_conf_set = echo_conf_set
433 /** @} echo_cl_ops */
435 /** \defgroup echo_lu_ops lu_object operations
437 * operations for echo lu object.
441 static int echo_object_init(const struct lu_env *env, struct lu_object *obj,
442 const struct lu_object_conf *conf)
444 struct echo_device *ed = cl2echo_dev(lu2cl_dev(obj->lo_dev));
445 struct echo_client_obd *ec = ed->ed_ec;
446 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
450 struct lu_object *below;
451 struct lu_device *under;
454 below = under->ld_ops->ldo_object_alloc(env, obj->lo_header,
458 lu_object_add(obj, below);
461 if (!ed->ed_next_ismd) {
462 const struct cl_object_conf *cconf = lu2cl_conf(conf);
463 struct echo_object_conf *econf = cl2echo_conf(cconf);
465 LASSERT(econf->eoc_md);
466 eco->eo_lsm = *econf->eoc_md;
467 /* clear the lsm pointer so that it won't get freed. */
468 *econf->eoc_md = NULL;
474 atomic_set(&eco->eo_npages, 0);
475 cl_object_page_init(lu2cl(obj), sizeof(struct echo_page));
477 spin_lock(&ec->ec_lock);
478 cfs_list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
479 spin_unlock(&ec->ec_lock);
484 /* taken from osc_unpackmd() */
485 static int echo_alloc_memmd(struct echo_device *ed,
486 struct lov_stripe_md **lsmp)
492 /* If export is lov/osc then use their obd method */
493 if (ed->ed_next != NULL)
494 return obd_alloc_memmd(ed->ed_ec->ec_exp, lsmp);
495 /* OFD has no unpackmd method, do everything here */
496 lsm_size = lov_stripe_md_size(1);
498 LASSERT(*lsmp == NULL);
499 OBD_ALLOC(*lsmp, lsm_size);
503 OBD_ALLOC((*lsmp)->lsm_oinfo[0], sizeof(struct lov_oinfo));
504 if ((*lsmp)->lsm_oinfo[0] == NULL) {
505 OBD_FREE(*lsmp, lsm_size);
509 loi_init((*lsmp)->lsm_oinfo[0]);
510 (*lsmp)->lsm_maxbytes = LUSTRE_STRIPE_MAXBYTES;
511 ostid_set_seq_echo(&(*lsmp)->lsm_oi);
516 static int echo_free_memmd(struct echo_device *ed, struct lov_stripe_md **lsmp)
522 /* If export is lov/osc then use their obd method */
523 if (ed->ed_next != NULL)
524 return obd_free_memmd(ed->ed_ec->ec_exp, lsmp);
525 /* OFD has no unpackmd method, do everything here */
526 lsm_size = lov_stripe_md_size(1);
528 LASSERT(*lsmp != NULL);
529 OBD_FREE((*lsmp)->lsm_oinfo[0], sizeof(struct lov_oinfo));
530 OBD_FREE(*lsmp, lsm_size);
535 static void echo_object_free(const struct lu_env *env, struct lu_object *obj)
537 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
538 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
541 LASSERT(atomic_read(&eco->eo_npages) == 0);
543 spin_lock(&ec->ec_lock);
544 cfs_list_del_init(&eco->eo_obj_chain);
545 spin_unlock(&ec->ec_lock);
548 lu_object_header_fini(obj->lo_header);
551 echo_free_memmd(eco->eo_dev, &eco->eo_lsm);
552 OBD_SLAB_FREE_PTR(eco, echo_object_kmem);
556 static int echo_object_print(const struct lu_env *env, void *cookie,
557 lu_printer_t p, const struct lu_object *o)
559 struct echo_object *obj = cl2echo_obj(lu2cl(o));
561 return (*p)(env, cookie, "echoclient-object@%p", obj);
564 static const struct lu_object_operations echo_lu_obj_ops = {
565 .loo_object_init = echo_object_init,
566 .loo_object_delete = NULL,
567 .loo_object_release = NULL,
568 .loo_object_free = echo_object_free,
569 .loo_object_print = echo_object_print,
570 .loo_object_invariant = NULL
572 /** @} echo_lu_ops */
574 /** \defgroup echo_lu_dev_ops lu_device operations
576 * Operations for echo lu device.
580 static struct lu_object *echo_object_alloc(const struct lu_env *env,
581 const struct lu_object_header *hdr,
582 struct lu_device *dev)
584 struct echo_object *eco;
585 struct lu_object *obj = NULL;
588 /* we're the top dev. */
589 LASSERT(hdr == NULL);
590 OBD_SLAB_ALLOC_PTR_GFP(eco, echo_object_kmem, GFP_NOFS);
592 struct cl_object_header *hdr = &eco->eo_hdr;
594 obj = &echo_obj2cl(eco)->co_lu;
595 cl_object_header_init(hdr);
596 hdr->coh_page_bufsize = cfs_size_round(sizeof(struct cl_page));
598 lu_object_init(obj, &hdr->coh_lu, dev);
599 lu_object_add_top(&hdr->coh_lu, obj);
601 eco->eo_cl.co_ops = &echo_cl_obj_ops;
602 obj->lo_ops = &echo_lu_obj_ops;
607 static struct lu_device_operations echo_device_lu_ops = {
608 .ldo_object_alloc = echo_object_alloc,
611 /** @} echo_lu_dev_ops */
613 static struct cl_device_operations echo_device_cl_ops = {
616 /** \defgroup echo_init Setup and teardown
618 * Init and fini functions for echo client.
622 static int echo_site_init(const struct lu_env *env, struct echo_device *ed)
624 struct cl_site *site = &ed->ed_site_myself;
627 /* initialize site */
628 rc = cl_site_init(site, &ed->ed_cl);
630 CERROR("Cannot initilize site for echo client(%d)\n", rc);
634 rc = lu_site_init_finish(&site->cs_lu);
642 static void echo_site_fini(const struct lu_env *env, struct echo_device *ed)
645 if (!ed->ed_next_ismd)
646 cl_site_fini(ed->ed_site);
651 static void *echo_thread_key_init(const struct lu_context *ctx,
652 struct lu_context_key *key)
654 struct echo_thread_info *info;
656 OBD_SLAB_ALLOC_PTR_GFP(info, echo_thread_kmem, GFP_NOFS);
658 info = ERR_PTR(-ENOMEM);
662 static void echo_thread_key_fini(const struct lu_context *ctx,
663 struct lu_context_key *key, void *data)
665 struct echo_thread_info *info = data;
666 OBD_SLAB_FREE_PTR(info, echo_thread_kmem);
669 static void echo_thread_key_exit(const struct lu_context *ctx,
670 struct lu_context_key *key, void *data)
674 static struct lu_context_key echo_thread_key = {
675 .lct_tags = LCT_CL_THREAD,
676 .lct_init = echo_thread_key_init,
677 .lct_fini = echo_thread_key_fini,
678 .lct_exit = echo_thread_key_exit
681 static void *echo_session_key_init(const struct lu_context *ctx,
682 struct lu_context_key *key)
684 struct echo_session_info *session;
686 OBD_SLAB_ALLOC_PTR_GFP(session, echo_session_kmem, GFP_NOFS);
688 session = ERR_PTR(-ENOMEM);
692 static void echo_session_key_fini(const struct lu_context *ctx,
693 struct lu_context_key *key, void *data)
695 struct echo_session_info *session = data;
696 OBD_SLAB_FREE_PTR(session, echo_session_kmem);
699 static void echo_session_key_exit(const struct lu_context *ctx,
700 struct lu_context_key *key, void *data)
704 static struct lu_context_key echo_session_key = {
705 .lct_tags = LCT_SESSION,
706 .lct_init = echo_session_key_init,
707 .lct_fini = echo_session_key_fini,
708 .lct_exit = echo_session_key_exit
711 LU_TYPE_INIT_FINI(echo, &echo_thread_key, &echo_session_key);
713 #ifdef HAVE_SERVER_SUPPORT
714 # define ECHO_SEQ_WIDTH 0xffffffff
715 static int echo_fid_init(struct echo_device *ed, char *obd_name,
716 struct seq_server_site *ss)
722 OBD_ALLOC_PTR(ed->ed_cl_seq);
723 if (ed->ed_cl_seq == NULL)
726 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
728 GOTO(out_free_seq, rc = -ENOMEM);
730 snprintf(prefix, MAX_OBD_NAME + 5, "srv-%s", obd_name);
732 /* Init client side sequence-manager */
733 rc = seq_client_init(ed->ed_cl_seq, NULL,
735 prefix, ss->ss_server_seq);
736 ed->ed_cl_seq->lcs_width = ECHO_SEQ_WIDTH;
737 OBD_FREE(prefix, MAX_OBD_NAME + 5);
739 GOTO(out_free_seq, rc);
744 OBD_FREE_PTR(ed->ed_cl_seq);
745 ed->ed_cl_seq = NULL;
749 static int echo_fid_fini(struct obd_device *obddev)
751 struct echo_device *ed = obd2echo_dev(obddev);
754 if (ed->ed_cl_seq != NULL) {
755 seq_client_fini(ed->ed_cl_seq);
756 OBD_FREE_PTR(ed->ed_cl_seq);
757 ed->ed_cl_seq = NULL;
762 #endif /* HAVE_SERVER_SUPPORT */
764 static struct lu_device *echo_device_alloc(const struct lu_env *env,
765 struct lu_device_type *t,
766 struct lustre_cfg *cfg)
768 struct lu_device *next;
769 struct echo_device *ed;
770 struct cl_device *cd;
771 struct obd_device *obd = NULL; /* to keep compiler happy */
772 struct obd_device *tgt;
773 const char *tgt_type_name;
780 GOTO(out, rc = -ENOMEM);
784 rc = cl_device_init(cd, t);
788 cd->cd_lu_dev.ld_ops = &echo_device_lu_ops;
789 cd->cd_ops = &echo_device_cl_ops;
792 obd = class_name2obd(lustre_cfg_string(cfg, 0));
793 LASSERT(obd != NULL);
794 LASSERT(env != NULL);
796 tgt = class_name2obd(lustre_cfg_string(cfg, 1));
798 CERROR("Can not find tgt device %s\n",
799 lustre_cfg_string(cfg, 1));
800 GOTO(out, rc = -ENODEV);
803 next = tgt->obd_lu_dev;
804 if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
805 ed->ed_next_ismd = 1;
807 ed->ed_next_ismd = 0;
808 rc = echo_site_init(env, ed);
814 rc = echo_client_setup(env, obd, cfg);
818 ed->ed_ec = &obd->u.echo_client;
821 if (ed->ed_next_ismd) {
822 #ifdef HAVE_SERVER_SUPPORT
823 /* Suppose to connect to some Metadata layer */
825 struct lu_device *ld;
829 CERROR("%s is not lu device type!\n",
830 lustre_cfg_string(cfg, 1));
831 GOTO(out, rc = -EINVAL);
834 tgt_type_name = lustre_cfg_string(cfg, 2);
835 if (!tgt_type_name) {
836 CERROR("%s no type name for echo %s setup\n",
837 lustre_cfg_string(cfg, 1),
838 tgt->obd_type->typ_name);
839 GOTO(out, rc = -EINVAL);
844 spin_lock(&ls->ls_ld_lock);
845 cfs_list_for_each_entry(ld, &ls->ls_ld_linkage, ld_linkage) {
846 if (strcmp(ld->ld_type->ldt_name, tgt_type_name) == 0) {
851 spin_unlock(&ls->ls_ld_lock);
854 CERROR("%s is not lu device type!\n",
855 lustre_cfg_string(cfg, 1));
856 GOTO(out, rc = -EINVAL);
860 /* For MD echo client, it will use the site in MDS stack */
861 ed->ed_site_myself.cs_lu = *ls;
862 ed->ed_site = &ed->ed_site_myself;
863 ed->ed_cl.cd_lu_dev.ld_site = &ed->ed_site_myself.cs_lu;
864 rc = echo_fid_init(ed, obd->obd_name, lu_site2seq(ls));
866 CERROR("echo fid init error %d\n", rc);
869 #else /* !HAVE_SERVER_SUPPORT */
870 CERROR("Local operations are NOT supported on client side. "
871 "Only remote operations are supported. Metadata client "
872 "must be run on server side.\n");
873 GOTO(out, rc = -EOPNOTSUPP);
876 /* if echo client is to be stacked upon ost device, the next is
877 * NULL since ost is not a clio device so far */
878 if (next != NULL && !lu_device_is_cl(next))
881 tgt_type_name = tgt->obd_type->typ_name;
883 LASSERT(next != NULL);
884 if (next->ld_site != NULL)
885 GOTO(out, rc = -EBUSY);
887 next->ld_site = &ed->ed_site->cs_lu;
888 rc = next->ld_type->ldt_ops->ldto_device_init(env, next,
889 next->ld_type->ldt_name,
894 /* Tricky case, I have to determine the obd type since
895 * CLIO uses the different parameters to initialize
896 * objects for lov & osc. */
897 if (strcmp(tgt_type_name, LUSTRE_LOV_NAME) == 0)
898 ed->ed_next_islov = 1;
900 LASSERT(strcmp(tgt_type_name,
901 LUSTRE_OSC_NAME) == 0);
903 LASSERT(strcmp(tgt_type_name, LUSTRE_OST_NAME) == 0);
907 RETURN(&cd->cd_lu_dev);
912 rc2 = echo_client_cleanup(obd);
914 CERROR("Cleanup obd device %s error(%d)\n",
919 echo_site_fini(env, ed);
921 cl_device_fini(&ed->ed_cl);
931 static int echo_device_init(const struct lu_env *env, struct lu_device *d,
932 const char *name, struct lu_device *next)
938 static struct lu_device *echo_device_fini(const struct lu_env *env,
941 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
942 struct lu_device *next = ed->ed_next;
944 while (next && !ed->ed_next_ismd)
945 next = next->ld_type->ldt_ops->ldto_device_fini(env, next);
949 static void echo_lock_release(const struct lu_env *env,
950 struct echo_lock *ecl,
953 struct cl_lock *clk = echo_lock2cl(ecl);
957 cl_lock_release(env, clk, "ec enqueue", ecl->el_object);
959 cl_lock_mutex_get(env, clk);
960 cl_lock_cancel(env, clk);
961 cl_lock_delete(env, clk);
962 cl_lock_mutex_put(env, clk);
964 cl_lock_put(env, clk);
967 static struct lu_device *echo_device_free(const struct lu_env *env,
970 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
971 struct echo_client_obd *ec = ed->ed_ec;
972 struct echo_object *eco;
973 struct lu_device *next = ed->ed_next;
975 CDEBUG(D_INFO, "echo device:%p is going to be freed, next = %p\n",
978 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
980 /* check if there are objects still alive.
981 * It shouldn't have any object because lu_site_purge would cleanup
982 * all of cached objects. Anyway, probably the echo device is being
983 * parallelly accessed.
985 spin_lock(&ec->ec_lock);
986 cfs_list_for_each_entry(eco, &ec->ec_objects, eo_obj_chain)
988 spin_unlock(&ec->ec_lock);
991 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
994 "Waiting for the reference of echo object to be dropped\n");
996 /* Wait for the last reference to be dropped. */
997 spin_lock(&ec->ec_lock);
998 while (!cfs_list_empty(&ec->ec_objects)) {
999 spin_unlock(&ec->ec_lock);
1000 CERROR("echo_client still has objects at cleanup time, "
1001 "wait for 1 second\n");
1002 schedule_timeout_and_set_state(TASK_UNINTERRUPTIBLE,
1003 cfs_time_seconds(1));
1004 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
1005 spin_lock(&ec->ec_lock);
1007 spin_unlock(&ec->ec_lock);
1009 LASSERT(cfs_list_empty(&ec->ec_locks));
1011 CDEBUG(D_INFO, "No object exists, exiting...\n");
1013 echo_client_cleanup(d->ld_obd);
1014 #ifdef HAVE_SERVER_SUPPORT
1015 echo_fid_fini(d->ld_obd);
1017 while (next && !ed->ed_next_ismd)
1018 next = next->ld_type->ldt_ops->ldto_device_free(env, next);
1020 LASSERT(ed->ed_site == lu2cl_site(d->ld_site));
1021 echo_site_fini(env, ed);
1022 cl_device_fini(&ed->ed_cl);
1028 static const struct lu_device_type_operations echo_device_type_ops = {
1029 .ldto_init = echo_type_init,
1030 .ldto_fini = echo_type_fini,
1032 .ldto_start = echo_type_start,
1033 .ldto_stop = echo_type_stop,
1035 .ldto_device_alloc = echo_device_alloc,
1036 .ldto_device_free = echo_device_free,
1037 .ldto_device_init = echo_device_init,
1038 .ldto_device_fini = echo_device_fini
1041 static struct lu_device_type echo_device_type = {
1042 .ldt_tags = LU_DEVICE_CL,
1043 .ldt_name = LUSTRE_ECHO_CLIENT_NAME,
1044 .ldt_ops = &echo_device_type_ops,
1045 .ldt_ctx_tags = LCT_CL_THREAD | LCT_MD_THREAD | LCT_DT_THREAD,
1049 /** \defgroup echo_exports Exported operations
1051 * exporting functions to echo client
1056 /* Interfaces to echo client obd device */
1057 static struct echo_object *cl_echo_object_find(struct echo_device *d,
1058 struct lov_stripe_md **lsmp)
1061 struct echo_thread_info *info;
1062 struct echo_object_conf *conf;
1063 struct lov_stripe_md *lsm;
1064 struct echo_object *eco;
1065 struct cl_object *obj;
1074 LASSERTF(ostid_id(&lsm->lsm_oi) != 0, DOSTID"\n", POSTID(&lsm->lsm_oi));
1075 LASSERTF(ostid_seq(&lsm->lsm_oi) == FID_SEQ_ECHO, DOSTID"\n",
1076 POSTID(&lsm->lsm_oi));
1078 /* Never return an object if the obd is to be freed. */
1079 if (echo_dev2cl(d)->cd_lu_dev.ld_obd->obd_stopping)
1080 RETURN(ERR_PTR(-ENODEV));
1082 env = cl_env_get(&refcheck);
1084 RETURN((void *)env);
1086 info = echo_env_info(env);
1087 conf = &info->eti_conf;
1089 if (!d->ed_next_islov) {
1090 struct lov_oinfo *oinfo = lsm->lsm_oinfo[0];
1091 LASSERT(oinfo != NULL);
1092 oinfo->loi_oi = lsm->lsm_oi;
1093 conf->eoc_cl.u.coc_oinfo = oinfo;
1095 struct lustre_md *md;
1097 memset(md, 0, sizeof *md);
1099 conf->eoc_cl.u.coc_md = md;
1102 conf->eoc_md = lsmp;
1104 fid = &info->eti_fid;
1105 rc = ostid_to_fid(fid, &lsm->lsm_oi, 0);
1107 GOTO(out, eco = ERR_PTR(rc));
1109 /* In the function below, .hs_keycmp resolves to
1110 * lu_obj_hop_keycmp() */
1111 /* coverity[overrun-buffer-val] */
1112 obj = cl_object_find(env, echo_dev2cl(d), fid, &conf->eoc_cl);
1114 GOTO(out, eco = (void*)obj);
1116 eco = cl2echo_obj(obj);
1117 if (eco->eo_deleted) {
1118 cl_object_put(env, obj);
1119 eco = ERR_PTR(-EAGAIN);
1123 cl_env_put(env, &refcheck);
1127 static int cl_echo_object_put(struct echo_object *eco)
1130 struct cl_object *obj = echo_obj2cl(eco);
1134 env = cl_env_get(&refcheck);
1136 RETURN(PTR_ERR(env));
1138 /* an external function to kill an object? */
1139 if (eco->eo_deleted) {
1140 struct lu_object_header *loh = obj->co_lu.lo_header;
1141 LASSERT(&eco->eo_hdr == luh2coh(loh));
1142 set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
1145 cl_object_put(env, obj);
1146 cl_env_put(env, &refcheck);
1150 static int cl_echo_enqueue0(struct lu_env *env, struct echo_object *eco,
1151 obd_off start, obd_off end, int mode,
1152 __u64 *cookie , __u32 enqflags)
1155 struct cl_lock *lck;
1156 struct cl_object *obj;
1157 struct cl_lock_descr *descr;
1158 struct echo_thread_info *info;
1162 info = echo_env_info(env);
1164 descr = &info->eti_descr;
1165 obj = echo_obj2cl(eco);
1167 descr->cld_obj = obj;
1168 descr->cld_start = cl_index(obj, start);
1169 descr->cld_end = cl_index(obj, end);
1170 descr->cld_mode = mode == LCK_PW ? CLM_WRITE : CLM_READ;
1171 descr->cld_enq_flags = enqflags;
1174 lck = cl_lock_request(env, io, descr, "ec enqueue", eco);
1176 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
1177 struct echo_lock *el;
1179 rc = cl_wait(env, lck);
1181 el = cl2echo_lock(cl_lock_at(lck, &echo_device_type));
1182 spin_lock(&ec->ec_lock);
1183 if (cfs_list_empty(&el->el_chain)) {
1184 cfs_list_add(&el->el_chain, &ec->ec_locks);
1185 el->el_cookie = ++ec->ec_unique;
1187 atomic_inc(&el->el_refcount);
1188 *cookie = el->el_cookie;
1189 spin_unlock(&ec->ec_lock);
1191 cl_lock_release(env, lck, "ec enqueue", current);
1197 static int cl_echo_enqueue(struct echo_object *eco, obd_off start, obd_off end,
1198 int mode, __u64 *cookie)
1200 struct echo_thread_info *info;
1207 env = cl_env_get(&refcheck);
1209 RETURN(PTR_ERR(env));
1211 info = echo_env_info(env);
1214 io->ci_ignore_layout = 1;
1215 result = cl_io_init(env, io, CIT_MISC, echo_obj2cl(eco));
1218 LASSERT(result == 0);
1220 result = cl_echo_enqueue0(env, eco, start, end, mode, cookie, 0);
1221 cl_io_fini(env, io);
1225 cl_env_put(env, &refcheck);
1229 static int cl_echo_cancel0(struct lu_env *env, struct echo_device *ed,
1232 struct echo_client_obd *ec = ed->ed_ec;
1233 struct echo_lock *ecl = NULL;
1235 int found = 0, still_used = 0;
1238 LASSERT(ec != NULL);
1239 spin_lock(&ec->ec_lock);
1240 cfs_list_for_each (el, &ec->ec_locks) {
1241 ecl = cfs_list_entry (el, struct echo_lock, el_chain);
1242 CDEBUG(D_INFO, "ecl: %p, cookie: "LPX64"\n", ecl, ecl->el_cookie);
1243 found = (ecl->el_cookie == cookie);
1245 if (atomic_dec_and_test(&ecl->el_refcount))
1246 cfs_list_del_init(&ecl->el_chain);
1252 spin_unlock(&ec->ec_lock);
1257 echo_lock_release(env, ecl, still_used);
1261 static int cl_echo_cancel(struct echo_device *ed, __u64 cookie)
1268 env = cl_env_get(&refcheck);
1270 RETURN(PTR_ERR(env));
1272 rc = cl_echo_cancel0(env, ed, cookie);
1274 cl_env_put(env, &refcheck);
1278 static void echo_commit_callback(const struct lu_env *env, struct cl_io *io,
1279 struct cl_page *page)
1281 struct echo_thread_info *info;
1282 struct cl_2queue *queue;
1284 info = echo_env_info(env);
1285 LASSERT(io == &info->eti_io);
1287 queue = &info->eti_queue;
1288 cl_page_list_add(&queue->c2_qout, page);
1291 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
1292 struct page **pages, int npages, int async)
1295 struct echo_thread_info *info;
1296 struct cl_object *obj = echo_obj2cl(eco);
1297 struct echo_device *ed = eco->eo_dev;
1298 struct cl_2queue *queue;
1300 struct cl_page *clp;
1301 struct lustre_handle lh = { 0 };
1302 int page_size = cl_page_size(obj);
1308 LASSERT((offset & ~CFS_PAGE_MASK) == 0);
1309 LASSERT(ed->ed_next != NULL);
1310 env = cl_env_get(&refcheck);
1312 RETURN(PTR_ERR(env));
1314 info = echo_env_info(env);
1316 queue = &info->eti_queue;
1318 cl_2queue_init(queue);
1320 io->ci_ignore_layout = 1;
1321 rc = cl_io_init(env, io, CIT_MISC, obj);
1327 rc = cl_echo_enqueue0(env, eco, offset,
1328 offset + npages * PAGE_CACHE_SIZE - 1,
1329 rw == READ ? LCK_PR : LCK_PW, &lh.cookie,
1332 GOTO(error_lock, rc);
1334 for (i = 0; i < npages; i++) {
1336 clp = cl_page_find(env, obj, cl_index(obj, offset),
1337 pages[i], CPT_TRANSIENT);
1342 LASSERT(clp->cp_type == CPT_TRANSIENT);
1344 rc = cl_page_own(env, io, clp);
1346 LASSERT(clp->cp_state == CPS_FREEING);
1347 cl_page_put(env, clp);
1351 cl_2queue_add(queue, clp);
1353 /* drop the reference count for cl_page_find, so that the page
1354 * will be freed in cl_2queue_fini. */
1355 cl_page_put(env, clp);
1356 cl_page_clip(env, clp, 0, page_size);
1358 offset += page_size;
1362 enum cl_req_type typ = rw == READ ? CRT_READ : CRT_WRITE;
1364 async = async && (typ == CRT_WRITE);
1366 rc = cl_io_commit_async(env, io, &queue->c2_qin,
1368 echo_commit_callback);
1370 rc = cl_io_submit_sync(env, io, typ, queue, 0);
1371 CDEBUG(D_INFO, "echo_client %s write returns %d\n",
1372 async ? "async" : "sync", rc);
1375 cl_echo_cancel0(env, ed, lh.cookie);
1378 cl_2queue_discard(env, io, queue);
1379 cl_2queue_disown(env, io, queue);
1380 cl_2queue_fini(env, queue);
1381 cl_io_fini(env, io);
1383 cl_env_put(env, &refcheck);
1386 /** @} echo_exports */
1389 static obd_id last_object_id;
1392 echo_copyout_lsm (struct lov_stripe_md *lsm, void *_ulsm, int ulsm_nob)
1394 struct lov_stripe_md *ulsm = _ulsm;
1397 nob = offsetof (struct lov_stripe_md, lsm_oinfo[lsm->lsm_stripe_count]);
1401 if (copy_to_user (ulsm, lsm, sizeof(*ulsm)))
1404 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1405 if (copy_to_user (ulsm->lsm_oinfo[i], lsm->lsm_oinfo[i],
1406 sizeof(lsm->lsm_oinfo[0])))
1413 echo_copyin_lsm (struct echo_device *ed, struct lov_stripe_md *lsm,
1414 void *ulsm, int ulsm_nob)
1416 struct echo_client_obd *ec = ed->ed_ec;
1419 if (ulsm_nob < sizeof (*lsm))
1422 if (copy_from_user (lsm, ulsm, sizeof (*lsm)))
1425 if (lsm->lsm_stripe_count > ec->ec_nstripes ||
1426 lsm->lsm_magic != LOV_MAGIC ||
1427 (lsm->lsm_stripe_size & (~CFS_PAGE_MASK)) != 0 ||
1428 ((__u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count > ~0UL))
1432 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1433 if (copy_from_user(lsm->lsm_oinfo[i],
1434 ((struct lov_stripe_md *)ulsm)-> \
1436 sizeof(lsm->lsm_oinfo[0])))
1442 #ifdef HAVE_SERVER_SUPPORT
1443 static inline void echo_md_build_name(struct lu_name *lname, char *name,
1446 sprintf(name, LPU64, id);
1447 lname->ln_name = name;
1448 lname->ln_namelen = strlen(name);
1451 /* similar to mdt_attr_get_complex */
1452 static int echo_big_lmm_get(const struct lu_env *env, struct md_object *o,
1455 struct echo_thread_info *info = echo_env_info(env);
1460 LASSERT(ma->ma_lmm_size > 0);
1462 rc = mo_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LOV);
1466 /* big_lmm may need to be grown */
1467 if (info->eti_big_lmmsize < rc) {
1468 int size = size_roundup_power2(rc);
1470 if (info->eti_big_lmmsize > 0) {
1471 /* free old buffer */
1472 LASSERT(info->eti_big_lmm);
1473 OBD_FREE_LARGE(info->eti_big_lmm,
1474 info->eti_big_lmmsize);
1475 info->eti_big_lmm = NULL;
1476 info->eti_big_lmmsize = 0;
1479 OBD_ALLOC_LARGE(info->eti_big_lmm, size);
1480 if (info->eti_big_lmm == NULL)
1482 info->eti_big_lmmsize = size;
1484 LASSERT(info->eti_big_lmmsize >= rc);
1486 info->eti_buf.lb_buf = info->eti_big_lmm;
1487 info->eti_buf.lb_len = info->eti_big_lmmsize;
1488 rc = mo_xattr_get(env, o, &info->eti_buf, XATTR_NAME_LOV);
1492 ma->ma_valid |= MA_LOV;
1493 ma->ma_lmm = info->eti_big_lmm;
1494 ma->ma_lmm_size = rc;
1499 static int echo_attr_get_complex(const struct lu_env *env,
1500 struct md_object *next,
1503 struct echo_thread_info *info = echo_env_info(env);
1504 struct lu_buf *buf = &info->eti_buf;
1505 umode_t mode = lu_object_attr(&next->mo_lu);
1506 int need = ma->ma_need;
1513 if (need & MA_INODE) {
1514 ma->ma_need = MA_INODE;
1515 rc = mo_attr_get(env, next, ma);
1518 ma->ma_valid |= MA_INODE;
1521 if (need & MA_LOV) {
1522 if (S_ISREG(mode) || S_ISDIR(mode)) {
1523 LASSERT(ma->ma_lmm_size > 0);
1524 buf->lb_buf = ma->ma_lmm;
1525 buf->lb_len = ma->ma_lmm_size;
1526 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LOV);
1528 ma->ma_lmm_size = rc2;
1529 ma->ma_valid |= MA_LOV;
1530 } else if (rc2 == -ENODATA) {
1532 ma->ma_lmm_size = 0;
1533 } else if (rc2 == -ERANGE) {
1534 rc2 = echo_big_lmm_get(env, next, ma);
1536 GOTO(out, rc = rc2);
1538 GOTO(out, rc = rc2);
1543 #ifdef CONFIG_FS_POSIX_ACL
1544 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
1545 buf->lb_buf = ma->ma_acl;
1546 buf->lb_len = ma->ma_acl_size;
1547 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1549 ma->ma_acl_size = rc2;
1550 ma->ma_valid |= MA_ACL_DEF;
1551 } else if (rc2 == -ENODATA) {
1553 ma->ma_acl_size = 0;
1555 GOTO(out, rc = rc2);
1561 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
1562 rc, ma->ma_valid, ma->ma_lmm);
1567 echo_md_create_internal(const struct lu_env *env, struct echo_device *ed,
1568 struct md_object *parent, struct lu_fid *fid,
1569 struct lu_name *lname, struct md_op_spec *spec,
1572 struct lu_object *ec_child, *child;
1573 struct lu_device *ld = ed->ed_next;
1574 struct echo_thread_info *info = echo_env_info(env);
1575 struct lu_fid *fid2 = &info->eti_fid2;
1576 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
1581 rc = mdo_lookup(env, parent, lname, fid2, spec);
1584 else if (rc != -ENOENT)
1587 ec_child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev,
1589 if (IS_ERR(ec_child)) {
1590 CERROR("Can not find the child "DFID": rc = %ld\n", PFID(fid),
1592 RETURN(PTR_ERR(ec_child));
1595 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1596 if (child == NULL) {
1597 CERROR("Can not locate the child "DFID"\n", PFID(fid));
1598 GOTO(out_put, rc = -EINVAL);
1601 CDEBUG(D_RPCTRACE, "Start creating object "DFID" %s %p\n",
1602 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1605 * Do not perform lookup sanity check. We know that name does not exist.
1607 spec->sp_cr_lookup = 0;
1608 rc = mdo_create(env, parent, lname, lu2md(child), spec, ma);
1610 CERROR("Can not create child "DFID": rc = %d\n", PFID(fid), rc);
1613 CDEBUG(D_RPCTRACE, "End creating object "DFID" %s %p rc = %d\n",
1614 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent, rc);
1617 lu_object_put(env, ec_child);
1621 static int echo_set_lmm_size(const struct lu_env *env, struct lu_device *ld,
1624 struct echo_thread_info *info = echo_env_info(env);
1626 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
1627 ma->ma_lmm = (void *)&info->eti_lmm;
1628 ma->ma_lmm_size = sizeof(info->eti_lmm);
1630 LASSERT(info->eti_big_lmmsize);
1631 ma->ma_lmm = info->eti_big_lmm;
1632 ma->ma_lmm_size = info->eti_big_lmmsize;
1638 static int echo_create_md_object(const struct lu_env *env,
1639 struct echo_device *ed,
1640 struct lu_object *ec_parent,
1642 char *name, int namelen,
1643 __u64 id, __u32 mode, int count,
1644 int stripe_count, int stripe_offset)
1646 struct lu_object *parent;
1647 struct echo_thread_info *info = echo_env_info(env);
1648 struct lu_name *lname = &info->eti_lname;
1649 struct md_op_spec *spec = &info->eti_spec;
1650 struct md_attr *ma = &info->eti_ma;
1651 struct lu_device *ld = ed->ed_next;
1657 if (ec_parent == NULL)
1659 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1663 memset(ma, 0, sizeof(*ma));
1664 memset(spec, 0, sizeof(*spec));
1665 if (stripe_count != 0) {
1666 spec->sp_cr_flags |= FMODE_WRITE;
1667 echo_set_lmm_size(env, ld, ma);
1668 if (stripe_count != -1) {
1669 struct lov_user_md_v3 *lum = &info->eti_lum;
1671 lum->lmm_magic = LOV_USER_MAGIC_V3;
1672 lum->lmm_stripe_count = stripe_count;
1673 lum->lmm_stripe_offset = stripe_offset;
1674 lum->lmm_pattern = 0;
1675 spec->u.sp_ea.eadata = lum;
1676 spec->u.sp_ea.eadatalen = sizeof(*lum);
1677 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1681 ma->ma_attr.la_mode = mode;
1682 ma->ma_attr.la_valid = LA_CTIME | LA_MODE;
1683 ma->ma_attr.la_ctime = cfs_time_current_64();
1686 lname->ln_name = name;
1687 lname->ln_namelen = namelen;
1688 /* If name is specified, only create one object by name */
1689 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1694 /* Create multiple object sequenced by id */
1695 for (i = 0; i < count; i++) {
1696 char *tmp_name = info->eti_name;
1698 echo_md_build_name(lname, tmp_name, id);
1700 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1703 CERROR("Can not create child %s: rc = %d\n", tmp_name,
1714 static struct lu_object *echo_md_lookup(const struct lu_env *env,
1715 struct echo_device *ed,
1716 struct md_object *parent,
1717 struct lu_name *lname)
1719 struct echo_thread_info *info = echo_env_info(env);
1720 struct lu_fid *fid = &info->eti_fid;
1721 struct lu_object *child;
1725 CDEBUG(D_INFO, "lookup %s in parent "DFID" %p\n", lname->ln_name,
1727 rc = mdo_lookup(env, parent, lname, fid, NULL);
1729 CERROR("lookup %s: rc = %d\n", lname->ln_name, rc);
1730 RETURN(ERR_PTR(rc));
1733 /* In the function below, .hs_keycmp resolves to
1734 * lu_obj_hop_keycmp() */
1735 /* coverity[overrun-buffer-val] */
1736 child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1741 static int echo_setattr_object(const struct lu_env *env,
1742 struct echo_device *ed,
1743 struct lu_object *ec_parent,
1744 __u64 id, int count)
1746 struct lu_object *parent;
1747 struct echo_thread_info *info = echo_env_info(env);
1748 struct lu_name *lname = &info->eti_lname;
1749 char *name = info->eti_name;
1750 struct lu_device *ld = ed->ed_next;
1751 struct lu_buf *buf = &info->eti_buf;
1757 if (ec_parent == NULL)
1759 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1763 for (i = 0; i < count; i++) {
1764 struct lu_object *ec_child, *child;
1766 echo_md_build_name(lname, name, id);
1768 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1769 if (IS_ERR(ec_child)) {
1770 CERROR("Can't find child %s: rc = %ld\n",
1771 lname->ln_name, PTR_ERR(ec_child));
1772 RETURN(PTR_ERR(ec_child));
1775 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1776 if (child == NULL) {
1777 CERROR("Can not locate the child %s\n", lname->ln_name);
1778 lu_object_put(env, ec_child);
1783 CDEBUG(D_RPCTRACE, "Start setattr object "DFID"\n",
1784 PFID(lu_object_fid(child)));
1786 buf->lb_buf = info->eti_xattr_buf;
1787 buf->lb_len = sizeof(info->eti_xattr_buf);
1789 sprintf(name, "%s.test1", XATTR_USER_PREFIX);
1790 rc = mo_xattr_set(env, lu2md(child), buf, name,
1793 CERROR("Can not setattr child "DFID": rc = %d\n",
1794 PFID(lu_object_fid(child)), rc);
1795 lu_object_put(env, ec_child);
1798 CDEBUG(D_RPCTRACE, "End setattr object "DFID"\n",
1799 PFID(lu_object_fid(child)));
1801 lu_object_put(env, ec_child);
1806 static int echo_getattr_object(const struct lu_env *env,
1807 struct echo_device *ed,
1808 struct lu_object *ec_parent,
1809 __u64 id, int count)
1811 struct lu_object *parent;
1812 struct echo_thread_info *info = echo_env_info(env);
1813 struct lu_name *lname = &info->eti_lname;
1814 char *name = info->eti_name;
1815 struct md_attr *ma = &info->eti_ma;
1816 struct lu_device *ld = ed->ed_next;
1822 if (ec_parent == NULL)
1824 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1828 memset(ma, 0, sizeof(*ma));
1829 ma->ma_need |= MA_INODE | MA_LOV | MA_PFID | MA_HSM | MA_ACL_DEF;
1830 ma->ma_acl = info->eti_xattr_buf;
1831 ma->ma_acl_size = sizeof(info->eti_xattr_buf);
1833 for (i = 0; i < count; i++) {
1834 struct lu_object *ec_child, *child;
1837 echo_md_build_name(lname, name, id);
1838 echo_set_lmm_size(env, ld, ma);
1840 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1841 if (IS_ERR(ec_child)) {
1842 CERROR("Can't find child %s: rc = %ld\n",
1843 lname->ln_name, PTR_ERR(ec_child));
1844 RETURN(PTR_ERR(ec_child));
1847 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1848 if (child == NULL) {
1849 CERROR("Can not locate the child %s\n", lname->ln_name);
1850 lu_object_put(env, ec_child);
1854 CDEBUG(D_RPCTRACE, "Start getattr object "DFID"\n",
1855 PFID(lu_object_fid(child)));
1856 rc = echo_attr_get_complex(env, lu2md(child), ma);
1858 CERROR("Can not getattr child "DFID": rc = %d\n",
1859 PFID(lu_object_fid(child)), rc);
1860 lu_object_put(env, ec_child);
1863 CDEBUG(D_RPCTRACE, "End getattr object "DFID"\n",
1864 PFID(lu_object_fid(child)));
1866 lu_object_put(env, ec_child);
1872 static int echo_lookup_object(const struct lu_env *env,
1873 struct echo_device *ed,
1874 struct lu_object *ec_parent,
1875 __u64 id, int count)
1877 struct lu_object *parent;
1878 struct echo_thread_info *info = echo_env_info(env);
1879 struct lu_name *lname = &info->eti_lname;
1880 char *name = info->eti_name;
1881 struct lu_fid *fid = &info->eti_fid;
1882 struct lu_device *ld = ed->ed_next;
1886 if (ec_parent == NULL)
1888 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1892 /*prepare the requests*/
1893 for (i = 0; i < count; i++) {
1894 echo_md_build_name(lname, name, id);
1896 CDEBUG(D_RPCTRACE, "Start lookup object "DFID" %s %p\n",
1897 PFID(lu_object_fid(parent)), lname->ln_name, parent);
1899 rc = mdo_lookup(env, lu2md(parent), lname, fid, NULL);
1901 CERROR("Can not lookup child %s: rc = %d\n", name, rc);
1904 CDEBUG(D_RPCTRACE, "End lookup object "DFID" %s %p\n",
1905 PFID(lu_object_fid(parent)), lname->ln_name, parent);
1912 static int echo_md_destroy_internal(const struct lu_env *env,
1913 struct echo_device *ed,
1914 struct md_object *parent,
1915 struct lu_name *lname,
1918 struct lu_device *ld = ed->ed_next;
1919 struct lu_object *ec_child;
1920 struct lu_object *child;
1925 ec_child = echo_md_lookup(env, ed, parent, lname);
1926 if (IS_ERR(ec_child)) {
1927 CERROR("Can't find child %s: rc = %ld\n", lname->ln_name,
1929 RETURN(PTR_ERR(ec_child));
1932 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1933 if (child == NULL) {
1934 CERROR("Can not locate the child %s\n", lname->ln_name);
1935 GOTO(out_put, rc = -EINVAL);
1938 if (lu_object_remote(child)) {
1939 CERROR("Can not destroy remote object %s: rc = %d\n",
1940 lname->ln_name, -EPERM);
1941 GOTO(out_put, rc = -EPERM);
1943 CDEBUG(D_RPCTRACE, "Start destroy object "DFID" %s %p\n",
1944 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1946 rc = mdo_unlink(env, parent, lu2md(child), lname, ma, 0);
1948 CERROR("Can not unlink child %s: rc = %d\n",
1949 lname->ln_name, rc);
1952 CDEBUG(D_RPCTRACE, "End destroy object "DFID" %s %p\n",
1953 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1955 lu_object_put(env, ec_child);
1959 static int echo_destroy_object(const struct lu_env *env,
1960 struct echo_device *ed,
1961 struct lu_object *ec_parent,
1962 char *name, int namelen,
1963 __u64 id, __u32 mode,
1966 struct echo_thread_info *info = echo_env_info(env);
1967 struct lu_name *lname = &info->eti_lname;
1968 struct md_attr *ma = &info->eti_ma;
1969 struct lu_device *ld = ed->ed_next;
1970 struct lu_object *parent;
1975 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1979 memset(ma, 0, sizeof(*ma));
1980 ma->ma_attr.la_mode = mode;
1981 ma->ma_attr.la_valid = LA_CTIME;
1982 ma->ma_attr.la_ctime = cfs_time_current_64();
1983 ma->ma_need = MA_INODE;
1987 lname->ln_name = name;
1988 lname->ln_namelen = namelen;
1989 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
1994 /*prepare the requests*/
1995 for (i = 0; i < count; i++) {
1996 char *tmp_name = info->eti_name;
1999 echo_md_build_name(lname, tmp_name, id);
2001 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
2004 CERROR("Can not unlink child %s: rc = %d\n", name, rc);
2013 static struct lu_object *echo_resolve_path(const struct lu_env *env,
2014 struct echo_device *ed, char *path,
2017 struct lu_device *ld = ed->ed_next;
2018 struct md_device *md = lu2md_dev(ld);
2019 struct echo_thread_info *info = echo_env_info(env);
2020 struct lu_fid *fid = &info->eti_fid;
2021 struct lu_name *lname = &info->eti_lname;
2022 struct lu_object *parent = NULL;
2023 struct lu_object *child = NULL;
2027 /*Only support MDD layer right now*/
2028 rc = md->md_ops->mdo_root_get(env, md, fid);
2030 CERROR("get root error: rc = %d\n", rc);
2031 RETURN(ERR_PTR(rc));
2034 /* In the function below, .hs_keycmp resolves to
2035 * lu_obj_hop_keycmp() */
2036 /* coverity[overrun-buffer-val] */
2037 parent = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
2038 if (IS_ERR(parent)) {
2039 CERROR("Can not find the parent "DFID": rc = %ld\n",
2040 PFID(fid), PTR_ERR(parent));
2045 struct lu_object *ld_parent;
2048 e = strsep(&path, "/");
2053 if (!path || path[0] == '\0')
2059 lname->ln_namelen = strlen(e);
2061 ld_parent = lu_object_locate(parent->lo_header, ld->ld_type);
2062 if (ld_parent == NULL) {
2063 lu_object_put(env, parent);
2068 child = echo_md_lookup(env, ed, lu2md(ld_parent), lname);
2069 lu_object_put(env, parent);
2070 if (IS_ERR(child)) {
2071 rc = (int)PTR_ERR(child);
2072 CERROR("lookup %s under parent "DFID": rc = %d\n",
2073 lname->ln_name, PFID(lu_object_fid(ld_parent)),
2080 RETURN(ERR_PTR(rc));
2085 static void echo_ucred_init(struct lu_env *env)
2087 struct lu_ucred *ucred = lu_ucred(env);
2089 ucred->uc_valid = UCRED_INVALID;
2091 ucred->uc_suppgids[0] = -1;
2092 ucred->uc_suppgids[1] = -1;
2094 ucred->uc_uid = ucred->uc_o_uid =
2095 from_kuid(&init_user_ns, current_uid());
2096 ucred->uc_gid = ucred->uc_o_gid =
2097 from_kgid(&init_user_ns, current_gid());
2098 ucred->uc_fsuid = ucred->uc_o_fsuid =
2099 from_kuid(&init_user_ns, current_fsuid());
2100 ucred->uc_fsgid = ucred->uc_o_fsgid =
2101 from_kgid(&init_user_ns, current_fsgid());
2102 ucred->uc_cap = cfs_curproc_cap_pack();
2104 /* remove fs privilege for non-root user. */
2105 if (ucred->uc_fsuid)
2106 ucred->uc_cap &= ~CFS_CAP_FS_MASK;
2107 ucred->uc_valid = UCRED_NEW;
2110 static void echo_ucred_fini(struct lu_env *env)
2112 struct lu_ucred *ucred = lu_ucred(env);
2113 ucred->uc_valid = UCRED_INIT;
2116 #define ECHO_MD_CTX_TAG (LCT_REMEMBER | LCT_MD_THREAD)
2117 #define ECHO_MD_SES_TAG (LCT_REMEMBER | LCT_SESSION | LCT_SERVER_SESSION)
2118 static int echo_md_handler(struct echo_device *ed, int command,
2119 char *path, int path_len, __u64 id, int count,
2120 struct obd_ioctl_data *data)
2122 struct echo_thread_info *info;
2123 struct lu_device *ld = ed->ed_next;
2126 struct lu_object *parent;
2128 int namelen = data->ioc_plen2;
2133 CERROR("MD echo client is not being initialized properly\n");
2137 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
2138 CERROR("Only support MDD layer right now!\n");
2142 env = cl_env_get(&refcheck);
2144 RETURN(PTR_ERR(env));
2146 rc = lu_env_refill_by_tags(env, ECHO_MD_CTX_TAG, ECHO_MD_SES_TAG);
2150 /* init big_lmm buffer */
2151 info = echo_env_info(env);
2152 LASSERT(info->eti_big_lmm == NULL);
2153 OBD_ALLOC_LARGE(info->eti_big_lmm, MIN_MD_SIZE);
2154 if (info->eti_big_lmm == NULL)
2155 GOTO(out_env, rc = -ENOMEM);
2156 info->eti_big_lmmsize = MIN_MD_SIZE;
2158 parent = echo_resolve_path(env, ed, path, path_len);
2159 if (IS_ERR(parent)) {
2160 CERROR("Can not resolve the path %s: rc = %ld\n", path,
2162 GOTO(out_free, rc = PTR_ERR(parent));
2166 OBD_ALLOC(name, namelen + 1);
2168 GOTO(out_put, rc = -ENOMEM);
2169 if (copy_from_user(name, data->ioc_pbuf2, namelen))
2170 GOTO(out_name, rc = -EFAULT);
2173 echo_ucred_init(env);
2176 case ECHO_MD_CREATE:
2177 case ECHO_MD_MKDIR: {
2178 struct echo_thread_info *info = echo_env_info(env);
2179 __u32 mode = data->ioc_obdo2.o_mode;
2180 struct lu_fid *fid = &info->eti_fid;
2181 int stripe_count = (int)data->ioc_obdo2.o_misc;
2182 int stripe_index = (int)data->ioc_obdo2.o_stripe_idx;
2184 rc = ostid_to_fid(fid, &data->ioc_obdo1.o_oi, 0);
2188 /* In the function below, .hs_keycmp resolves to
2189 * lu_obj_hop_keycmp() */
2190 /* coverity[overrun-buffer-val] */
2191 rc = echo_create_md_object(env, ed, parent, fid, name, namelen,
2192 id, mode, count, stripe_count,
2196 case ECHO_MD_DESTROY:
2197 case ECHO_MD_RMDIR: {
2198 __u32 mode = data->ioc_obdo2.o_mode;
2200 rc = echo_destroy_object(env, ed, parent, name, namelen,
2204 case ECHO_MD_LOOKUP:
2205 rc = echo_lookup_object(env, ed, parent, id, count);
2207 case ECHO_MD_GETATTR:
2208 rc = echo_getattr_object(env, ed, parent, id, count);
2210 case ECHO_MD_SETATTR:
2211 rc = echo_setattr_object(env, ed, parent, id, count);
2214 CERROR("unknown command %d\n", command);
2218 echo_ucred_fini(env);
2222 OBD_FREE(name, namelen + 1);
2224 lu_object_put(env, parent);
2226 LASSERT(info->eti_big_lmm);
2227 OBD_FREE_LARGE(info->eti_big_lmm, info->eti_big_lmmsize);
2228 info->eti_big_lmm = NULL;
2229 info->eti_big_lmmsize = 0;
2231 cl_env_put(env, &refcheck);
2234 #endif /* HAVE_SERVER_SUPPORT */
2236 static int echo_create_object(const struct lu_env *env, struct echo_device *ed,
2237 int on_target, struct obdo *oa, void *ulsm,
2238 int ulsm_nob, struct obd_trans_info *oti)
2240 struct echo_object *eco;
2241 struct echo_client_obd *ec = ed->ed_ec;
2242 struct lov_stripe_md *lsm = NULL;
2247 if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
2248 (on_target || /* set_stripe */
2249 ec->ec_nstripes != 0)) { /* LOV */
2250 CERROR ("No valid oid\n");
2254 rc = echo_alloc_memmd(ed, &lsm);
2256 CERROR("Cannot allocate md: rc = %d\n", rc);
2263 rc = echo_copyin_lsm (ed, lsm, ulsm, ulsm_nob);
2267 if (lsm->lsm_stripe_count == 0)
2268 lsm->lsm_stripe_count = ec->ec_nstripes;
2270 if (lsm->lsm_stripe_size == 0)
2271 lsm->lsm_stripe_size = PAGE_CACHE_SIZE;
2275 /* setup stripes: indices + default ids if required */
2276 for (i = 0; i < lsm->lsm_stripe_count; i++) {
2277 if (ostid_id(&lsm->lsm_oinfo[i]->loi_oi) == 0)
2278 lsm->lsm_oinfo[i]->loi_oi = lsm->lsm_oi;
2280 lsm->lsm_oinfo[i]->loi_ost_idx =
2281 (idx + i) % ec->ec_nstripes;
2285 /* setup object ID here for !on_target and LOV hint */
2286 if (oa->o_valid & OBD_MD_FLID) {
2287 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2288 lsm->lsm_oi = oa->o_oi;
2291 if (ostid_id(&lsm->lsm_oi) == 0)
2292 ostid_set_id(&lsm->lsm_oi, ++last_object_id);
2296 /* Only echo objects are allowed to be created */
2297 LASSERT((oa->o_valid & OBD_MD_FLGROUP) &&
2298 (ostid_seq(&oa->o_oi) == FID_SEQ_ECHO));
2299 rc = obd_create(env, ec->ec_exp, oa, &lsm, oti);
2301 CERROR("Cannot create objects: rc = %d\n", rc);
2307 /* See what object ID we were given */
2308 oa->o_oi = lsm->lsm_oi;
2309 oa->o_valid |= OBD_MD_FLID;
2311 eco = cl_echo_object_find(ed, &lsm);
2313 GOTO(failed, rc = PTR_ERR(eco));
2314 cl_echo_object_put(eco);
2316 CDEBUG(D_INFO, "oa oid "DOSTID"\n", POSTID(&oa->o_oi));
2321 obd_destroy(env, ec->ec_exp, oa, lsm, oti, NULL, NULL);
2323 echo_free_memmd(ed, &lsm);
2325 CERROR("create object failed with: rc = %d\n", rc);
2329 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
2332 struct lov_stripe_md *lsm = NULL;
2333 struct echo_object *eco;
2337 if ((oa->o_valid & OBD_MD_FLID) == 0 || ostid_id(&oa->o_oi) == 0) {
2338 /* disallow use of object id 0 */
2339 CERROR ("No valid oid\n");
2343 rc = echo_alloc_memmd(ed, &lsm);
2347 lsm->lsm_oi = oa->o_oi;
2348 if (!(oa->o_valid & OBD_MD_FLGROUP))
2349 ostid_set_seq_echo(&lsm->lsm_oi);
2352 eco = cl_echo_object_find(ed, &lsm);
2358 echo_free_memmd(ed, &lsm);
2362 static void echo_put_object(struct echo_object *eco)
2364 if (cl_echo_object_put(eco))
2365 CERROR("echo client: drop an object failed");
2369 echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
2371 unsigned long stripe_count;
2372 unsigned long stripe_size;
2373 unsigned long width;
2374 unsigned long woffset;
2378 if (lsm->lsm_stripe_count <= 1)
2382 stripe_size = lsm->lsm_stripe_size;
2383 stripe_count = lsm->lsm_stripe_count;
2385 /* width = # bytes in all stripes */
2386 width = stripe_size * stripe_count;
2388 /* woffset = offset within a width; offset = whole number of widths */
2389 woffset = do_div (offset, width);
2391 stripe_index = woffset / stripe_size;
2393 *idp = ostid_id(&lsm->lsm_oinfo[stripe_index]->loi_oi);
2394 *offp = offset * stripe_size + woffset % stripe_size;
2398 echo_client_page_debug_setup(struct lov_stripe_md *lsm,
2399 struct page *page, int rw, obd_id id,
2400 obd_off offset, obd_off count)
2407 /* no partial pages on the client */
2408 LASSERT(count == PAGE_CACHE_SIZE);
2412 for (delta = 0; delta < PAGE_CACHE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2413 if (rw == OBD_BRW_WRITE) {
2414 stripe_off = offset + delta;
2416 echo_get_stripe_off_id(lsm, &stripe_off, &stripe_id);
2418 stripe_off = 0xdeadbeef00c0ffeeULL;
2419 stripe_id = 0xdeadbeef00c0ffeeULL;
2421 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
2422 stripe_off, stripe_id);
2428 static int echo_client_page_debug_check(struct lov_stripe_md *lsm,
2429 struct page *page, obd_id id,
2430 obd_off offset, obd_off count)
2439 /* no partial pages on the client */
2440 LASSERT(count == PAGE_CACHE_SIZE);
2444 for (rc = delta = 0; delta < PAGE_CACHE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2445 stripe_off = offset + delta;
2447 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
2449 rc2 = block_debug_check("test_brw",
2450 addr + delta, OBD_ECHO_BLOCK_SIZE,
2451 stripe_off, stripe_id);
2453 CERROR ("Error in echo object "LPX64"\n", id);
2462 static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
2463 struct echo_object *eco, obd_off offset,
2464 obd_size count, int async,
2465 struct obd_trans_info *oti)
2467 struct lov_stripe_md *lsm = eco->eo_lsm;
2469 struct brw_page *pga;
2470 struct brw_page *pgp;
2471 struct page **pages;
2480 verify = (ostid_id(&oa->o_oi) != ECHO_PERSISTENT_OBJID &&
2481 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
2482 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
2484 gfp_mask = ((ostid_id(&oa->o_oi) & 2) == 0) ? GFP_IOFS : GFP_HIGHUSER;
2486 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
2487 LASSERT(lsm != NULL);
2488 LASSERT(ostid_id(&lsm->lsm_oi) == ostid_id(&oa->o_oi));
2491 (count & (~CFS_PAGE_MASK)) != 0)
2494 /* XXX think again with misaligned I/O */
2495 npages = count >> PAGE_CACHE_SHIFT;
2497 if (rw == OBD_BRW_WRITE)
2498 brw_flags = OBD_BRW_ASYNC;
2500 OBD_ALLOC(pga, npages * sizeof(*pga));
2504 OBD_ALLOC(pages, npages * sizeof(*pages));
2505 if (pages == NULL) {
2506 OBD_FREE(pga, npages * sizeof(*pga));
2510 for (i = 0, pgp = pga, off = offset;
2512 i++, pgp++, off += PAGE_CACHE_SIZE) {
2514 LASSERT (pgp->pg == NULL); /* for cleanup */
2517 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
2518 if (pgp->pg == NULL)
2522 pgp->count = PAGE_CACHE_SIZE;
2524 pgp->flag = brw_flags;
2527 echo_client_page_debug_setup(lsm, pgp->pg, rw,
2528 ostid_id(&oa->o_oi), off,
2532 /* brw mode can only be used at client */
2533 LASSERT(ed->ed_next != NULL);
2534 rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
2537 if (rc != 0 || rw != OBD_BRW_READ)
2540 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
2541 if (pgp->pg == NULL)
2546 vrc = echo_client_page_debug_check(lsm, pgp->pg,
2547 ostid_id(&oa->o_oi),
2548 pgp->off, pgp->count);
2549 if (vrc != 0 && rc == 0)
2552 OBD_PAGE_FREE(pgp->pg);
2554 OBD_FREE(pga, npages * sizeof(*pga));
2555 OBD_FREE(pages, npages * sizeof(*pages));
2559 static int echo_client_prep_commit(const struct lu_env *env,
2560 struct obd_export *exp, int rw,
2561 struct obdo *oa, struct echo_object *eco,
2562 obd_off offset, obd_size count,
2563 obd_size batch, struct obd_trans_info *oti,
2566 struct lov_stripe_md *lsm = eco->eo_lsm;
2567 struct obd_ioobj ioo;
2568 struct niobuf_local *lnb;
2569 struct niobuf_remote *rnb;
2571 obd_size npages, tot_pages;
2572 int i, ret = 0, brw_flags = 0;
2576 if (count <= 0 || (count & (~CFS_PAGE_MASK)) != 0 ||
2577 (lsm != NULL && ostid_id(&lsm->lsm_oi) != ostid_id(&oa->o_oi)))
2580 npages = batch >> PAGE_CACHE_SHIFT;
2581 tot_pages = count >> PAGE_CACHE_SHIFT;
2583 OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
2584 OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
2586 if (lnb == NULL || rnb == NULL)
2587 GOTO(out, ret = -ENOMEM);
2589 if (rw == OBD_BRW_WRITE && async)
2590 brw_flags |= OBD_BRW_ASYNC;
2592 obdo_to_ioobj(oa, &ioo);
2596 for(; tot_pages; tot_pages -= npages) {
2599 if (tot_pages < npages)
2602 for (i = 0; i < npages; i++, off += PAGE_CACHE_SIZE) {
2603 rnb[i].offset = off;
2604 rnb[i].len = PAGE_CACHE_SIZE;
2605 rnb[i].flags = brw_flags;
2608 ioo.ioo_bufcnt = npages;
2609 oti->oti_transno = 0;
2612 ret = obd_preprw(env, rw, exp, oa, 1, &ioo, rnb, &lpages,
2616 LASSERT(lpages == npages);
2618 for (i = 0; i < lpages; i++) {
2619 struct page *page = lnb[i].page;
2621 /* read past eof? */
2622 if (page == NULL && lnb[i].rc == 0)
2626 lnb[i].flags |= OBD_BRW_ASYNC;
2628 if (ostid_id(&oa->o_oi) == ECHO_PERSISTENT_OBJID ||
2629 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
2630 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
2633 if (rw == OBD_BRW_WRITE)
2634 echo_client_page_debug_setup(lsm, page, rw,
2635 ostid_id(&oa->o_oi),
2639 echo_client_page_debug_check(lsm, page,
2640 ostid_id(&oa->o_oi),
2645 ret = obd_commitrw(env, rw, exp, oa, 1, &ioo,
2646 rnb, npages, lnb, oti, ret);
2650 /* Reset oti otherwise it would confuse ldiskfs. */
2651 memset(oti, 0, sizeof(*oti));
2653 /* Reuse env context. */
2654 lu_context_exit((struct lu_context *)&env->le_ctx);
2655 lu_context_enter((struct lu_context *)&env->le_ctx);
2660 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
2662 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
2666 static int echo_client_brw_ioctl(const struct lu_env *env, int rw,
2667 struct obd_export *exp,
2668 struct obd_ioctl_data *data,
2669 struct obd_trans_info *dummy_oti)
2671 struct obd_device *obd = class_exp2obd(exp);
2672 struct echo_device *ed = obd2echo_dev(obd);
2673 struct echo_client_obd *ec = ed->ed_ec;
2674 struct obdo *oa = &data->ioc_obdo1;
2675 struct echo_object *eco;
2681 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2683 rc = echo_get_object(&eco, ed, oa);
2687 oa->o_valid &= ~OBD_MD_FLHANDLE;
2689 /* OFD/obdfilter works only via prep/commit */
2690 test_mode = (long)data->ioc_pbuf1;
2694 if (ed->ed_next == NULL && test_mode != 3) {
2696 data->ioc_plen1 = data->ioc_count;
2699 /* Truncate batch size to maximum */
2700 if (data->ioc_plen1 > PTLRPC_MAX_BRW_SIZE)
2701 data->ioc_plen1 = PTLRPC_MAX_BRW_SIZE;
2703 switch (test_mode) {
2707 rc = echo_client_kbrw(ed, rw, oa,
2708 eco, data->ioc_offset,
2709 data->ioc_count, async, dummy_oti);
2712 rc = echo_client_prep_commit(env, ec->ec_exp, rw, oa,
2713 eco, data->ioc_offset,
2714 data->ioc_count, data->ioc_plen1,
2720 echo_put_object(eco);
2725 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
2726 int mode, obd_off offset, obd_size nob)
2728 struct echo_device *ed = obd2echo_dev(exp->exp_obd);
2729 struct lustre_handle *ulh = &oa->o_handle;
2730 struct echo_object *eco;
2735 if (ed->ed_next == NULL)
2736 RETURN(-EOPNOTSUPP);
2738 if (!(mode == LCK_PR || mode == LCK_PW))
2741 if ((offset & (~CFS_PAGE_MASK)) != 0 ||
2742 (nob & (~CFS_PAGE_MASK)) != 0)
2745 rc = echo_get_object (&eco, ed, oa);
2749 end = (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
2750 rc = cl_echo_enqueue(eco, offset, end, mode, &ulh->cookie);
2752 oa->o_valid |= OBD_MD_FLHANDLE;
2753 CDEBUG(D_INFO, "Cookie is "LPX64"\n", ulh->cookie);
2755 echo_put_object(eco);
2760 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
2762 struct echo_device *ed = obd2echo_dev(exp->exp_obd);
2763 __u64 cookie = oa->o_handle.cookie;
2765 if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
2768 CDEBUG(D_INFO, "Cookie is "LPX64"\n", cookie);
2769 return cl_echo_cancel(ed, cookie);
2773 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2774 void *karg, void *uarg)
2776 #ifdef HAVE_SERVER_SUPPORT
2777 struct tgt_session_info *tsi;
2779 struct obd_device *obd = exp->exp_obd;
2780 struct echo_device *ed = obd2echo_dev(obd);
2781 struct echo_client_obd *ec = ed->ed_ec;
2782 struct echo_object *eco;
2783 struct obd_ioctl_data *data = karg;
2784 struct obd_trans_info dummy_oti;
2786 struct oti_req_ack_lock *ack_lock;
2789 int rw = OBD_BRW_READ;
2792 #ifdef HAVE_SERVER_SUPPORT
2793 struct lu_context echo_session;
2797 memset(&dummy_oti, 0, sizeof(dummy_oti));
2799 oa = &data->ioc_obdo1;
2800 if (!(oa->o_valid & OBD_MD_FLGROUP)) {
2801 oa->o_valid |= OBD_MD_FLGROUP;
2802 ostid_set_seq_echo(&oa->o_oi);
2805 /* This FID is unpacked just for validation at this point */
2806 rc = ostid_to_fid(&fid, &oa->o_oi, 0);
2814 rc = lu_env_init(env, LCT_DT_THREAD);
2816 GOTO(out_alloc, rc = -ENOMEM);
2818 #ifdef HAVE_SERVER_SUPPORT
2819 env->le_ses = &echo_session;
2820 rc = lu_context_init(env->le_ses, LCT_SERVER_SESSION | LCT_NOREF);
2821 if (unlikely(rc < 0))
2823 lu_context_enter(env->le_ses);
2825 tsi = tgt_ses_info(env);
2826 tsi->tsi_exp = ec->ec_exp;
2827 tsi->tsi_jobid = NULL;
2830 case OBD_IOC_CREATE: /* may create echo object */
2831 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2832 GOTO (out, rc = -EPERM);
2834 rc = echo_create_object(env, ed, 1, oa, data->ioc_pbuf1,
2835 data->ioc_plen1, &dummy_oti);
2838 #ifdef HAVE_SERVER_SUPPORT
2839 case OBD_IOC_ECHO_MD: {
2846 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2847 GOTO(out, rc = -EPERM);
2849 count = data->ioc_count;
2850 cmd = data->ioc_command;
2852 id = data->ioc_obdo2.o_oi.oi.oi_id;
2853 dirlen = data->ioc_plen1;
2854 OBD_ALLOC(dir, dirlen + 1);
2856 GOTO(out, rc = -ENOMEM);
2858 if (copy_from_user(dir, data->ioc_pbuf1, dirlen)) {
2859 OBD_FREE(dir, data->ioc_plen1 + 1);
2860 GOTO(out, rc = -EFAULT);
2863 rc = echo_md_handler(ed, cmd, dir, dirlen, id, count, data);
2864 OBD_FREE(dir, dirlen + 1);
2867 case OBD_IOC_ECHO_ALLOC_SEQ: {
2868 struct lu_env *cl_env;
2873 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2874 GOTO(out, rc = -EPERM);
2876 cl_env = cl_env_get(&refcheck);
2878 GOTO(out, rc = PTR_ERR(cl_env));
2880 rc = lu_env_refill_by_tags(cl_env, ECHO_MD_CTX_TAG,
2883 cl_env_put(cl_env, &refcheck);
2887 rc = seq_client_get_seq(cl_env, ed->ed_cl_seq, &seq);
2888 cl_env_put(cl_env, &refcheck);
2890 CERROR("%s: Can not alloc seq: rc = %d\n",
2895 if (copy_to_user(data->ioc_pbuf1, &seq, data->ioc_plen1))
2898 max_count = LUSTRE_METADATA_SEQ_MAX_WIDTH;
2899 if (copy_to_user(data->ioc_pbuf2, &max_count,
2904 #endif /* HAVE_SERVER_SUPPORT */
2905 case OBD_IOC_DESTROY:
2906 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2907 GOTO (out, rc = -EPERM);
2909 rc = echo_get_object(&eco, ed, oa);
2911 rc = obd_destroy(env, ec->ec_exp, oa, eco->eo_lsm,
2912 &dummy_oti, NULL, NULL);
2914 eco->eo_deleted = 1;
2915 echo_put_object(eco);
2919 case OBD_IOC_GETATTR:
2920 rc = echo_get_object(&eco, ed, oa);
2922 struct obd_info oinfo = { { { 0 } } };
2923 oinfo.oi_md = eco->eo_lsm;
2925 rc = obd_getattr(env, ec->ec_exp, &oinfo);
2926 echo_put_object(eco);
2930 case OBD_IOC_SETATTR:
2931 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2932 GOTO (out, rc = -EPERM);
2934 rc = echo_get_object(&eco, ed, oa);
2936 struct obd_info oinfo = { { { 0 } } };
2938 oinfo.oi_md = eco->eo_lsm;
2940 rc = obd_setattr(env, ec->ec_exp, &oinfo, NULL);
2941 echo_put_object(eco);
2945 case OBD_IOC_BRW_WRITE:
2946 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2947 GOTO (out, rc = -EPERM);
2951 case OBD_IOC_BRW_READ:
2952 rc = echo_client_brw_ioctl(env, rw, exp, data, &dummy_oti);
2955 case ECHO_IOC_GET_STRIPE:
2956 rc = echo_get_object(&eco, ed, oa);
2958 rc = echo_copyout_lsm(eco->eo_lsm, data->ioc_pbuf1,
2960 echo_put_object(eco);
2964 case ECHO_IOC_SET_STRIPE:
2965 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2966 GOTO (out, rc = -EPERM);
2968 if (data->ioc_pbuf1 == NULL) { /* unset */
2969 rc = echo_get_object(&eco, ed, oa);
2971 eco->eo_deleted = 1;
2972 echo_put_object(eco);
2975 rc = echo_create_object(env, ed, 0, oa,
2977 data->ioc_plen1, &dummy_oti);
2981 case ECHO_IOC_ENQUEUE:
2982 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2983 GOTO (out, rc = -EPERM);
2985 rc = echo_client_enqueue(exp, oa,
2986 data->ioc_conn1, /* lock mode */
2988 data->ioc_count);/*extent*/
2991 case ECHO_IOC_CANCEL:
2992 rc = echo_client_cancel(exp, oa);
2996 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
2997 GOTO (out, rc = -ENOTTY);
3002 #ifdef HAVE_SERVER_SUPPORT
3003 lu_context_exit(env->le_ses);
3004 lu_context_fini(env->le_ses);
3011 /* XXX this should be in a helper also called by target_send_reply */
3012 for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
3014 if (!ack_lock->mode)
3016 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
3022 static int echo_client_setup(const struct lu_env *env,
3023 struct obd_device *obddev, struct lustre_cfg *lcfg)
3025 struct echo_client_obd *ec = &obddev->u.echo_client;
3026 struct obd_device *tgt;
3027 struct obd_uuid echo_uuid = { "ECHO_UUID" };
3028 struct obd_connect_data *ocd = NULL;
3032 if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
3033 CERROR("requires a TARGET OBD name\n");
3037 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
3038 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
3039 CERROR("device not attached or not set up (%s)\n",
3040 lustre_cfg_string(lcfg, 1));
3044 spin_lock_init(&ec->ec_lock);
3045 CFS_INIT_LIST_HEAD (&ec->ec_objects);
3046 CFS_INIT_LIST_HEAD (&ec->ec_locks);
3048 ec->ec_nstripes = 0;
3050 if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
3051 #ifdef HAVE_SERVER_SUPPORT
3052 lu_context_tags_update(ECHO_MD_CTX_TAG);
3053 lu_session_tags_update(ECHO_MD_SES_TAG);
3055 CERROR("Local operations are NOT supported on client side. "
3056 "Only remote operations are supported. Metadata client "
3057 "must be run on server side.\n");
3062 OBD_ALLOC(ocd, sizeof(*ocd));
3064 CERROR("Can't alloc ocd connecting to %s\n",
3065 lustre_cfg_string(lcfg, 1));
3069 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
3070 OBD_CONNECT_BRW_SIZE |
3071 OBD_CONNECT_GRANT | OBD_CONNECT_FULL20 |
3072 OBD_CONNECT_64BITHASH | OBD_CONNECT_LVB_TYPE |
3074 ocd->ocd_brw_size = DT_MAX_BRW_SIZE;
3075 ocd->ocd_version = LUSTRE_VERSION_CODE;
3076 ocd->ocd_group = FID_SEQ_ECHO;
3078 rc = obd_connect(env, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
3080 /* Turn off pinger because it connects to tgt obd directly. */
3081 spin_lock(&tgt->obd_dev_lock);
3082 cfs_list_del_init(&ec->ec_exp->exp_obd_chain_timed);
3083 spin_unlock(&tgt->obd_dev_lock);
3086 OBD_FREE(ocd, sizeof(*ocd));
3089 CERROR("fail to connect to device %s\n",
3090 lustre_cfg_string(lcfg, 1));
3097 static int echo_client_cleanup(struct obd_device *obddev)
3099 struct echo_device *ed = obd2echo_dev(obddev);
3100 struct echo_client_obd *ec = &obddev->u.echo_client;
3104 /*Do nothing for Metadata echo client*/
3108 if (ed->ed_next_ismd) {
3109 #ifdef HAVE_SERVER_SUPPORT
3110 lu_context_tags_clear(ECHO_MD_CTX_TAG);
3111 lu_session_tags_clear(ECHO_MD_SES_TAG);
3113 CERROR("This is client-side only module, does not support "
3114 "metadata echo client.\n");
3119 if (!cfs_list_empty(&obddev->obd_exports)) {
3120 CERROR("still has clients!\n");
3124 LASSERT(atomic_read(&ec->ec_exp->exp_refcount) > 0);
3125 rc = obd_disconnect(ec->ec_exp);
3127 CERROR("fail to disconnect device: %d\n", rc);
3132 static int echo_client_connect(const struct lu_env *env,
3133 struct obd_export **exp,
3134 struct obd_device *src, struct obd_uuid *cluuid,
3135 struct obd_connect_data *data, void *localdata)
3138 struct lustre_handle conn = { 0 };
3141 rc = class_connect(&conn, src, cluuid);
3143 *exp = class_conn2export(&conn);
3149 static int echo_client_disconnect(struct obd_export *exp)
3155 GOTO(out, rc = -EINVAL);
3157 rc = class_disconnect(exp);
3163 static struct obd_ops echo_client_obd_ops = {
3164 .o_owner = THIS_MODULE,
3165 .o_iocontrol = echo_client_iocontrol,
3166 .o_connect = echo_client_connect,
3167 .o_disconnect = echo_client_disconnect
3170 int echo_client_init(void)
3174 rc = lu_kmem_init(echo_caches);
3176 rc = class_register_type(&echo_client_obd_ops, NULL, true, NULL,
3177 #ifndef HAVE_ONLY_PROCFS_SEQ
3180 LUSTRE_ECHO_CLIENT_NAME,
3183 lu_kmem_fini(echo_caches);
3188 void echo_client_exit(void)
3190 class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
3191 lu_kmem_fini(echo_caches);
3195 static int __init obdecho_init(void)
3200 LCONSOLE_INFO("Echo OBD driver; http://www.lustre.org/\n");
3202 LASSERT(PAGE_CACHE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
3204 # ifdef HAVE_SERVER_SUPPORT
3205 rc = echo_persistent_pages_init();
3209 rc = class_register_type(&echo_obd_ops, NULL, true, NULL,
3210 #ifndef HAVE_ONLY_PROCFS_SEQ
3213 LUSTRE_ECHO_NAME, NULL);
3218 rc = echo_client_init();
3220 # ifdef HAVE_SERVER_SUPPORT
3224 class_unregister_type(LUSTRE_ECHO_NAME);
3226 echo_persistent_pages_fini();
3232 static void /*__exit*/ obdecho_exit(void)
3236 # ifdef HAVE_SERVER_SUPPORT
3237 class_unregister_type(LUSTRE_ECHO_NAME);
3238 echo_persistent_pages_fini();
3242 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3243 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
3244 MODULE_LICENSE("GPL");
3246 cfs_module(obdecho, LUSTRE_VERSION_STRING, obdecho_init, obdecho_exit);
3247 #endif /* __KERNEL__ */
3249 /** @} echo_client */