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, 2012, Whamcloud, Inc.
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 <lustre_fid.h>
51 #include <lustre_acl.h>
52 #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;
80 cfs_atomic_t eo_npages;
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;
92 cfs_page_t *ep_vmpage;
96 struct cl_lock_slice el_cl;
98 struct echo_object *el_object;
100 cfs_atomic_t el_refcount;
104 struct cl_io_slice ei_cl;
109 struct cl_req_slice er_cl;
113 static int echo_client_setup(const struct lu_env *env,
114 struct obd_device *obddev,
115 struct lustre_cfg *lcfg);
116 static int echo_client_cleanup(struct obd_device *obddev);
119 /** \defgroup echo_helpers Helper functions
122 static inline struct echo_device *cl2echo_dev(const struct cl_device *dev)
124 return container_of0(dev, struct echo_device, ed_cl);
127 static inline struct cl_device *echo_dev2cl(struct echo_device *d)
132 static inline struct echo_device *obd2echo_dev(const struct obd_device *obd)
134 return cl2echo_dev(lu2cl_dev(obd->obd_lu_dev));
137 static inline struct cl_object *echo_obj2cl(struct echo_object *eco)
142 static inline struct echo_object *cl2echo_obj(const struct cl_object *o)
144 return container_of(o, struct echo_object, eo_cl);
147 static inline struct echo_page *cl2echo_page(const struct cl_page_slice *s)
149 return container_of(s, struct echo_page, ep_cl);
152 static inline struct echo_lock *cl2echo_lock(const struct cl_lock_slice *s)
154 return container_of(s, struct echo_lock, el_cl);
157 static inline struct cl_lock *echo_lock2cl(const struct echo_lock *ecl)
159 return ecl->el_cl.cls_lock;
162 static struct lu_context_key echo_thread_key;
163 static inline struct echo_thread_info *echo_env_info(const struct lu_env *env)
165 struct echo_thread_info *info;
166 info = lu_context_key_get(&env->le_ctx, &echo_thread_key);
167 LASSERT(info != NULL);
172 struct echo_object_conf *cl2echo_conf(const struct cl_object_conf *c)
174 return container_of(c, struct echo_object_conf, eoc_cl);
177 static inline void lsm2fid(struct lov_stripe_md *lsm, struct lu_fid *fid)
180 fid->f_seq = FID_SEQ_ECHO;
181 /* truncated to 32 bits by assignment */
182 fid->f_oid = lsm->lsm_object_id;
183 fid->f_ver = lsm->lsm_object_id >> 32;
185 /** @} echo_helpers */
187 static struct echo_object *cl_echo_object_find(struct echo_device *d,
188 struct lov_stripe_md **lsm);
189 static int cl_echo_object_put(struct echo_object *eco);
190 static int cl_echo_enqueue (struct echo_object *eco, obd_off start,
191 obd_off end, int mode, __u64 *cookie);
192 static int cl_echo_cancel (struct echo_device *d, __u64 cookie);
193 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
194 cfs_page_t **pages, int npages, int async);
196 static struct echo_thread_info *echo_env_info(const struct lu_env *env);
198 struct echo_thread_info {
199 struct echo_object_conf eti_conf;
200 struct lustre_md eti_md;
202 struct cl_2queue eti_queue;
204 struct cl_lock_descr eti_descr;
205 struct lu_fid eti_fid;
206 struct lu_fid eti_fid2;
207 struct md_op_spec eti_spec;
208 struct lov_mds_md_v3 eti_lmm;
209 struct lov_user_md_v3 eti_lum;
210 struct md_attr eti_ma;
211 struct lu_name eti_lname;
213 struct lu_buf eti_buf;
214 char eti_xattr_buf[LUSTRE_POSIX_ACL_MAX_SIZE];
217 /* No session used right now */
218 struct echo_session_info {
222 static cfs_mem_cache_t *echo_page_kmem;
223 static cfs_mem_cache_t *echo_lock_kmem;
224 static cfs_mem_cache_t *echo_object_kmem;
225 static cfs_mem_cache_t *echo_thread_kmem;
226 static cfs_mem_cache_t *echo_session_kmem;
227 //static cfs_mem_cache_t *echo_req_kmem;
229 static struct lu_kmem_descr echo_caches[] = {
231 .ckd_cache = &echo_page_kmem,
232 .ckd_name = "echo_page_kmem",
233 .ckd_size = sizeof (struct echo_page)
236 .ckd_cache = &echo_lock_kmem,
237 .ckd_name = "echo_lock_kmem",
238 .ckd_size = sizeof (struct echo_lock)
241 .ckd_cache = &echo_object_kmem,
242 .ckd_name = "echo_object_kmem",
243 .ckd_size = sizeof (struct echo_object)
246 .ckd_cache = &echo_thread_kmem,
247 .ckd_name = "echo_thread_kmem",
248 .ckd_size = sizeof (struct echo_thread_info)
251 .ckd_cache = &echo_session_kmem,
252 .ckd_name = "echo_session_kmem",
253 .ckd_size = sizeof (struct echo_session_info)
257 .ckd_cache = &echo_req_kmem,
258 .ckd_name = "echo_req_kmem",
259 .ckd_size = sizeof (struct echo_req)
267 /** \defgroup echo_page Page operations
269 * Echo page operations.
273 static cfs_page_t *echo_page_vmpage(const struct lu_env *env,
274 const struct cl_page_slice *slice)
276 return cl2echo_page(slice)->ep_vmpage;
279 static int echo_page_own(const struct lu_env *env,
280 const struct cl_page_slice *slice,
281 struct cl_io *io, int nonblock)
283 struct echo_page *ep = cl2echo_page(slice);
286 cfs_mutex_lock(&ep->ep_lock);
287 else if (!cfs_mutex_trylock(&ep->ep_lock))
292 static void echo_page_disown(const struct lu_env *env,
293 const struct cl_page_slice *slice,
296 struct echo_page *ep = cl2echo_page(slice);
298 LASSERT(cfs_mutex_is_locked(&ep->ep_lock));
299 cfs_mutex_unlock(&ep->ep_lock);
302 static void echo_page_discard(const struct lu_env *env,
303 const struct cl_page_slice *slice,
304 struct cl_io *unused)
306 cl_page_delete(env, slice->cpl_page);
309 static int echo_page_is_vmlocked(const struct lu_env *env,
310 const struct cl_page_slice *slice)
312 if (cfs_mutex_is_locked(&cl2echo_page(slice)->ep_lock))
317 static void echo_page_completion(const struct lu_env *env,
318 const struct cl_page_slice *slice,
321 LASSERT(slice->cpl_page->cp_sync_io != NULL);
324 static void echo_page_fini(const struct lu_env *env,
325 struct cl_page_slice *slice)
327 struct echo_page *ep = cl2echo_page(slice);
328 struct echo_object *eco = cl2echo_obj(slice->cpl_obj);
329 cfs_page_t *vmpage = ep->ep_vmpage;
332 cfs_atomic_dec(&eco->eo_npages);
333 page_cache_release(vmpage);
334 OBD_SLAB_FREE_PTR(ep, echo_page_kmem);
338 static int echo_page_prep(const struct lu_env *env,
339 const struct cl_page_slice *slice,
340 struct cl_io *unused)
345 static int echo_page_print(const struct lu_env *env,
346 const struct cl_page_slice *slice,
347 void *cookie, lu_printer_t printer)
349 struct echo_page *ep = cl2echo_page(slice);
351 (*printer)(env, cookie, LUSTRE_ECHO_CLIENT_NAME"-page@%p %d vm@%p\n",
352 ep, cfs_mutex_is_locked(&ep->ep_lock), ep->ep_vmpage);
356 static const struct cl_page_operations echo_page_ops = {
357 .cpo_own = echo_page_own,
358 .cpo_disown = echo_page_disown,
359 .cpo_discard = echo_page_discard,
360 .cpo_vmpage = echo_page_vmpage,
361 .cpo_fini = echo_page_fini,
362 .cpo_print = echo_page_print,
363 .cpo_is_vmlocked = echo_page_is_vmlocked,
366 .cpo_prep = echo_page_prep,
367 .cpo_completion = echo_page_completion,
370 .cpo_prep = echo_page_prep,
371 .cpo_completion = echo_page_completion,
377 /** \defgroup echo_lock Locking
379 * echo lock operations
383 static void echo_lock_fini(const struct lu_env *env,
384 struct cl_lock_slice *slice)
386 struct echo_lock *ecl = cl2echo_lock(slice);
388 LASSERT(cfs_list_empty(&ecl->el_chain));
389 OBD_SLAB_FREE_PTR(ecl, echo_lock_kmem);
392 static void echo_lock_delete(const struct lu_env *env,
393 const struct cl_lock_slice *slice)
395 struct echo_lock *ecl = cl2echo_lock(slice);
397 LASSERT(cfs_list_empty(&ecl->el_chain));
400 static int echo_lock_fits_into(const struct lu_env *env,
401 const struct cl_lock_slice *slice,
402 const struct cl_lock_descr *need,
403 const struct cl_io *unused)
408 static struct cl_lock_operations echo_lock_ops = {
409 .clo_fini = echo_lock_fini,
410 .clo_delete = echo_lock_delete,
411 .clo_fits_into = echo_lock_fits_into
416 /** \defgroup echo_cl_ops cl_object operations
418 * operations for cl_object
422 static struct cl_page *echo_page_init(const struct lu_env *env,
423 struct cl_object *obj,
424 struct cl_page *page, cfs_page_t *vmpage)
426 struct echo_page *ep;
429 OBD_SLAB_ALLOC_PTR_GFP(ep, echo_page_kmem, CFS_ALLOC_IO);
431 struct echo_object *eco = cl2echo_obj(obj);
432 ep->ep_vmpage = vmpage;
433 page_cache_get(vmpage);
434 cfs_mutex_init(&ep->ep_lock);
435 cl_page_slice_add(page, &ep->ep_cl, obj, &echo_page_ops);
436 cfs_atomic_inc(&eco->eo_npages);
438 RETURN(ERR_PTR(ep ? 0 : -ENOMEM));
441 static int echo_io_init(const struct lu_env *env, struct cl_object *obj,
447 static int echo_lock_init(const struct lu_env *env,
448 struct cl_object *obj, struct cl_lock *lock,
449 const struct cl_io *unused)
451 struct echo_lock *el;
454 OBD_SLAB_ALLOC_PTR_GFP(el, echo_lock_kmem, CFS_ALLOC_IO);
456 cl_lock_slice_add(lock, &el->el_cl, obj, &echo_lock_ops);
457 el->el_object = cl2echo_obj(obj);
458 CFS_INIT_LIST_HEAD(&el->el_chain);
459 cfs_atomic_set(&el->el_refcount, 0);
461 RETURN(el == NULL ? -ENOMEM : 0);
464 static int echo_conf_set(const struct lu_env *env, struct cl_object *obj,
465 const struct cl_object_conf *conf)
470 static const struct cl_object_operations echo_cl_obj_ops = {
471 .coo_page_init = echo_page_init,
472 .coo_lock_init = echo_lock_init,
473 .coo_io_init = echo_io_init,
474 .coo_conf_set = echo_conf_set
476 /** @} echo_cl_ops */
478 /** \defgroup echo_lu_ops lu_object operations
480 * operations for echo lu object.
484 static int echo_object_init(const struct lu_env *env, struct lu_object *obj,
485 const struct lu_object_conf *conf)
487 struct echo_device *ed = cl2echo_dev(lu2cl_dev(obj->lo_dev));
488 struct echo_client_obd *ec = ed->ed_ec;
489 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
493 struct lu_object *below;
494 struct lu_device *under;
497 below = under->ld_ops->ldo_object_alloc(env, obj->lo_header,
501 lu_object_add(obj, below);
504 if (!ed->ed_next_ismd) {
505 const struct cl_object_conf *cconf = lu2cl_conf(conf);
506 struct echo_object_conf *econf = cl2echo_conf(cconf);
508 LASSERT(econf->eoc_md);
509 eco->eo_lsm = *econf->eoc_md;
510 /* clear the lsm pointer so that it won't get freed. */
511 *econf->eoc_md = NULL;
517 cfs_atomic_set(&eco->eo_npages, 0);
519 cfs_spin_lock(&ec->ec_lock);
520 cfs_list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
521 cfs_spin_unlock(&ec->ec_lock);
526 /* taken from osc_unpackmd() */
527 static int echo_alloc_memmd(struct echo_device *ed,
528 struct lov_stripe_md **lsmp)
534 /* If export is lov/osc then use their obd method */
535 if (ed->ed_next != NULL)
536 return obd_alloc_memmd(ed->ed_ec->ec_exp, lsmp);
537 /* OFD has no unpackmd method, do everything here */
538 lsm_size = lov_stripe_md_size(1);
540 LASSERT(*lsmp == NULL);
541 OBD_ALLOC(*lsmp, lsm_size);
545 OBD_ALLOC((*lsmp)->lsm_oinfo[0], sizeof(struct lov_oinfo));
546 if ((*lsmp)->lsm_oinfo[0] == NULL) {
547 OBD_FREE(*lsmp, lsm_size);
551 loi_init((*lsmp)->lsm_oinfo[0]);
552 (*lsmp)->lsm_maxbytes = LUSTRE_STRIPE_MAXBYTES;
557 static int echo_free_memmd(struct echo_device *ed, struct lov_stripe_md **lsmp)
563 /* If export is lov/osc then use their obd method */
564 if (ed->ed_next != NULL)
565 return obd_free_memmd(ed->ed_ec->ec_exp, lsmp);
566 /* OFD has no unpackmd method, do everything here */
567 lsm_size = lov_stripe_md_size(1);
569 LASSERT(*lsmp != NULL);
570 OBD_FREE((*lsmp)->lsm_oinfo[0], sizeof(struct lov_oinfo));
571 OBD_FREE(*lsmp, lsm_size);
576 static void echo_object_free(const struct lu_env *env, struct lu_object *obj)
578 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
579 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
582 LASSERT(cfs_atomic_read(&eco->eo_npages) == 0);
584 cfs_spin_lock(&ec->ec_lock);
585 cfs_list_del_init(&eco->eo_obj_chain);
586 cfs_spin_unlock(&ec->ec_lock);
589 lu_object_header_fini(obj->lo_header);
592 echo_free_memmd(eco->eo_dev, &eco->eo_lsm);
593 OBD_SLAB_FREE_PTR(eco, echo_object_kmem);
597 static int echo_object_print(const struct lu_env *env, void *cookie,
598 lu_printer_t p, const struct lu_object *o)
600 struct echo_object *obj = cl2echo_obj(lu2cl(o));
602 return (*p)(env, cookie, "echoclient-object@%p", obj);
605 static const struct lu_object_operations echo_lu_obj_ops = {
606 .loo_object_init = echo_object_init,
607 .loo_object_delete = NULL,
608 .loo_object_release = NULL,
609 .loo_object_free = echo_object_free,
610 .loo_object_print = echo_object_print,
611 .loo_object_invariant = NULL
613 /** @} echo_lu_ops */
615 /** \defgroup echo_lu_dev_ops lu_device operations
617 * Operations for echo lu device.
621 static struct lu_object *echo_object_alloc(const struct lu_env *env,
622 const struct lu_object_header *hdr,
623 struct lu_device *dev)
625 struct echo_object *eco;
626 struct lu_object *obj = NULL;
629 /* we're the top dev. */
630 LASSERT(hdr == NULL);
631 OBD_SLAB_ALLOC_PTR_GFP(eco, echo_object_kmem, CFS_ALLOC_IO);
633 struct cl_object_header *hdr = &eco->eo_hdr;
635 obj = &echo_obj2cl(eco)->co_lu;
636 cl_object_header_init(hdr);
637 lu_object_init(obj, &hdr->coh_lu, dev);
638 lu_object_add_top(&hdr->coh_lu, obj);
640 eco->eo_cl.co_ops = &echo_cl_obj_ops;
641 obj->lo_ops = &echo_lu_obj_ops;
646 static struct lu_device_operations echo_device_lu_ops = {
647 .ldo_object_alloc = echo_object_alloc,
650 /** @} echo_lu_dev_ops */
652 static struct cl_device_operations echo_device_cl_ops = {
655 /** \defgroup echo_init Setup and teardown
657 * Init and fini functions for echo client.
661 static int echo_site_init(const struct lu_env *env, struct echo_device *ed)
663 struct cl_site *site = &ed->ed_site_myself;
666 /* initialize site */
667 rc = cl_site_init(site, &ed->ed_cl);
669 CERROR("Cannot initilize site for echo client(%d)\n", rc);
673 rc = lu_site_init_finish(&site->cs_lu);
681 static void echo_site_fini(const struct lu_env *env, struct echo_device *ed)
684 if (!ed->ed_next_ismd)
685 cl_site_fini(ed->ed_site);
690 static void *echo_thread_key_init(const struct lu_context *ctx,
691 struct lu_context_key *key)
693 struct echo_thread_info *info;
695 OBD_SLAB_ALLOC_PTR_GFP(info, echo_thread_kmem, CFS_ALLOC_IO);
697 info = ERR_PTR(-ENOMEM);
701 static void echo_thread_key_fini(const struct lu_context *ctx,
702 struct lu_context_key *key, void *data)
704 struct echo_thread_info *info = data;
705 OBD_SLAB_FREE_PTR(info, echo_thread_kmem);
708 static void echo_thread_key_exit(const struct lu_context *ctx,
709 struct lu_context_key *key, void *data)
713 static struct lu_context_key echo_thread_key = {
714 .lct_tags = LCT_CL_THREAD,
715 .lct_init = echo_thread_key_init,
716 .lct_fini = echo_thread_key_fini,
717 .lct_exit = echo_thread_key_exit
720 static void *echo_session_key_init(const struct lu_context *ctx,
721 struct lu_context_key *key)
723 struct echo_session_info *session;
725 OBD_SLAB_ALLOC_PTR_GFP(session, echo_session_kmem, CFS_ALLOC_IO);
727 session = ERR_PTR(-ENOMEM);
731 static void echo_session_key_fini(const struct lu_context *ctx,
732 struct lu_context_key *key, void *data)
734 struct echo_session_info *session = data;
735 OBD_SLAB_FREE_PTR(session, echo_session_kmem);
738 static void echo_session_key_exit(const struct lu_context *ctx,
739 struct lu_context_key *key, void *data)
743 static struct lu_context_key echo_session_key = {
744 .lct_tags = LCT_SESSION,
745 .lct_init = echo_session_key_init,
746 .lct_fini = echo_session_key_fini,
747 .lct_exit = echo_session_key_exit
750 LU_TYPE_INIT_FINI(echo, &echo_thread_key, &echo_session_key);
752 #define ECHO_SEQ_WIDTH 0xffffffff
753 static int echo_fid_init(struct echo_device *ed, char *obd_name,
760 OBD_ALLOC_PTR(ed->ed_cl_seq);
761 if (ed->ed_cl_seq == NULL)
764 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
766 GOTO(out_free_seq, rc = -ENOMEM);
768 snprintf(prefix, MAX_OBD_NAME + 5, "srv-%s", obd_name);
770 /* Init client side sequence-manager */
771 rc = seq_client_init(ed->ed_cl_seq, NULL,
773 prefix, ms->ms_server_seq);
774 ed->ed_cl_seq->lcs_width = ECHO_SEQ_WIDTH;
775 OBD_FREE(prefix, MAX_OBD_NAME + 5);
777 GOTO(out_free_seq, rc);
782 OBD_FREE_PTR(ed->ed_cl_seq);
783 ed->ed_cl_seq = NULL;
787 static int echo_fid_fini(struct obd_device *obddev)
789 struct echo_device *ed = obd2echo_dev(obddev);
792 if (ed->ed_cl_seq != NULL) {
793 seq_client_fini(ed->ed_cl_seq);
794 OBD_FREE_PTR(ed->ed_cl_seq);
795 ed->ed_cl_seq = NULL;
801 static struct lu_device *echo_device_alloc(const struct lu_env *env,
802 struct lu_device_type *t,
803 struct lustre_cfg *cfg)
805 struct lu_device *next;
806 struct echo_device *ed;
807 struct cl_device *cd;
808 struct obd_device *obd = NULL; /* to keep compiler happy */
809 struct obd_device *tgt;
810 const char *tgt_type_name;
817 GOTO(out, rc = -ENOMEM);
821 rc = cl_device_init(cd, t);
825 cd->cd_lu_dev.ld_ops = &echo_device_lu_ops;
826 cd->cd_ops = &echo_device_cl_ops;
829 obd = class_name2obd(lustre_cfg_string(cfg, 0));
830 LASSERT(obd != NULL);
831 LASSERT(env != NULL);
833 tgt = class_name2obd(lustre_cfg_string(cfg, 1));
835 CERROR("Can not find tgt device %s\n",
836 lustre_cfg_string(cfg, 1));
837 GOTO(out, rc = -ENODEV);
840 next = tgt->obd_lu_dev;
841 if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
842 ed->ed_next_ismd = 1;
844 ed->ed_next_ismd = 0;
845 rc = echo_site_init(env, ed);
851 rc = echo_client_setup(env, obd, cfg);
855 ed->ed_ec = &obd->u.echo_client;
858 if (ed->ed_next_ismd) {
859 /* Suppose to connect to some Metadata layer */
861 struct lu_device *ld;
865 CERROR("%s is not lu device type!\n",
866 lustre_cfg_string(cfg, 1));
867 GOTO(out, rc = -EINVAL);
870 tgt_type_name = lustre_cfg_string(cfg, 2);
871 if (!tgt_type_name) {
872 CERROR("%s no type name for echo %s setup\n",
873 lustre_cfg_string(cfg, 1),
874 tgt->obd_type->typ_name);
875 GOTO(out, rc = -EINVAL);
880 cfs_spin_lock(&ls->ls_ld_lock);
881 cfs_list_for_each_entry(ld, &ls->ls_ld_linkage, ld_linkage) {
882 if (strcmp(ld->ld_type->ldt_name, tgt_type_name) == 0) {
887 cfs_spin_unlock(&ls->ls_ld_lock);
890 CERROR("%s is not lu device type!\n",
891 lustre_cfg_string(cfg, 1));
892 GOTO(out, rc = -EINVAL);
896 /* For MD echo client, it will use the site in MDS stack */
897 ed->ed_site_myself.cs_lu = *ls;
898 ed->ed_site = &ed->ed_site_myself;
899 ed->ed_cl.cd_lu_dev.ld_site = &ed->ed_site_myself.cs_lu;
900 rc = echo_fid_init(ed, obd->obd_name, lu_site2md(ls));
902 CERROR("echo fid init error %d\n", rc);
906 /* if echo client is to be stacked upon ost device, the next is
907 * NULL since ost is not a clio device so far */
908 if (next != NULL && !lu_device_is_cl(next))
911 tgt_type_name = tgt->obd_type->typ_name;
913 LASSERT(next != NULL);
914 if (next->ld_site != NULL)
915 GOTO(out, rc = -EBUSY);
917 next->ld_site = &ed->ed_site->cs_lu;
918 rc = next->ld_type->ldt_ops->ldto_device_init(env, next,
919 next->ld_type->ldt_name,
924 /* Tricky case, I have to determine the obd type since
925 * CLIO uses the different parameters to initialize
926 * objects for lov & osc. */
927 if (strcmp(tgt_type_name, LUSTRE_LOV_NAME) == 0)
928 ed->ed_next_islov = 1;
930 LASSERT(strcmp(tgt_type_name,
931 LUSTRE_OSC_NAME) == 0);
933 LASSERT(strcmp(tgt_type_name, LUSTRE_OST_NAME) == 0);
937 RETURN(&cd->cd_lu_dev);
942 rc2 = echo_client_cleanup(obd);
944 CERROR("Cleanup obd device %s error(%d)\n",
949 echo_site_fini(env, ed);
951 cl_device_fini(&ed->ed_cl);
961 static int echo_device_init(const struct lu_env *env, struct lu_device *d,
962 const char *name, struct lu_device *next)
968 static struct lu_device *echo_device_fini(const struct lu_env *env,
971 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
972 struct lu_device *next = ed->ed_next;
974 while (next && !ed->ed_next_ismd)
975 next = next->ld_type->ldt_ops->ldto_device_fini(env, next);
979 static void echo_lock_release(const struct lu_env *env,
980 struct echo_lock *ecl,
983 struct cl_lock *clk = echo_lock2cl(ecl);
987 cl_lock_release(env, clk, "ec enqueue", ecl->el_object);
989 cl_lock_mutex_get(env, clk);
990 cl_lock_cancel(env, clk);
991 cl_lock_delete(env, clk);
992 cl_lock_mutex_put(env, clk);
994 cl_lock_put(env, clk);
997 static struct lu_device *echo_device_free(const struct lu_env *env,
1000 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
1001 struct echo_client_obd *ec = ed->ed_ec;
1002 struct echo_object *eco;
1003 struct lu_device *next = ed->ed_next;
1005 CDEBUG(D_INFO, "echo device:%p is going to be freed, next = %p\n",
1008 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
1010 /* check if there are objects still alive.
1011 * It shouldn't have any object because lu_site_purge would cleanup
1012 * all of cached objects. Anyway, probably the echo device is being
1013 * parallelly accessed.
1015 cfs_spin_lock(&ec->ec_lock);
1016 cfs_list_for_each_entry(eco, &ec->ec_objects, eo_obj_chain)
1017 eco->eo_deleted = 1;
1018 cfs_spin_unlock(&ec->ec_lock);
1021 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
1024 "Waiting for the reference of echo object to be dropped\n");
1026 /* Wait for the last reference to be dropped. */
1027 cfs_spin_lock(&ec->ec_lock);
1028 while (!cfs_list_empty(&ec->ec_objects)) {
1029 cfs_spin_unlock(&ec->ec_lock);
1030 CERROR("echo_client still has objects at cleanup time, "
1031 "wait for 1 second\n");
1032 cfs_schedule_timeout_and_set_state(CFS_TASK_UNINT,
1033 cfs_time_seconds(1));
1034 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
1035 cfs_spin_lock(&ec->ec_lock);
1037 cfs_spin_unlock(&ec->ec_lock);
1039 LASSERT(cfs_list_empty(&ec->ec_locks));
1041 CDEBUG(D_INFO, "No object exists, exiting...\n");
1043 echo_client_cleanup(d->ld_obd);
1044 echo_fid_fini(d->ld_obd);
1045 while (next && !ed->ed_next_ismd)
1046 next = next->ld_type->ldt_ops->ldto_device_free(env, next);
1048 LASSERT(ed->ed_site == lu2cl_site(d->ld_site));
1049 echo_site_fini(env, ed);
1050 cl_device_fini(&ed->ed_cl);
1056 static const struct lu_device_type_operations echo_device_type_ops = {
1057 .ldto_init = echo_type_init,
1058 .ldto_fini = echo_type_fini,
1060 .ldto_start = echo_type_start,
1061 .ldto_stop = echo_type_stop,
1063 .ldto_device_alloc = echo_device_alloc,
1064 .ldto_device_free = echo_device_free,
1065 .ldto_device_init = echo_device_init,
1066 .ldto_device_fini = echo_device_fini
1069 static struct lu_device_type echo_device_type = {
1070 .ldt_tags = LU_DEVICE_CL,
1071 .ldt_name = LUSTRE_ECHO_CLIENT_NAME,
1072 .ldt_ops = &echo_device_type_ops,
1073 .ldt_ctx_tags = LCT_CL_THREAD | LCT_MD_THREAD | LCT_DT_THREAD,
1077 /** \defgroup echo_exports Exported operations
1079 * exporting functions to echo client
1084 /* Interfaces to echo client obd device */
1085 static struct echo_object *cl_echo_object_find(struct echo_device *d,
1086 struct lov_stripe_md **lsmp)
1089 struct echo_thread_info *info;
1090 struct echo_object_conf *conf;
1091 struct lov_stripe_md *lsm;
1092 struct echo_object *eco;
1093 struct cl_object *obj;
1101 LASSERT(lsm->lsm_object_id);
1103 /* Never return an object if the obd is to be freed. */
1104 if (echo_dev2cl(d)->cd_lu_dev.ld_obd->obd_stopping)
1105 RETURN(ERR_PTR(-ENODEV));
1107 env = cl_env_get(&refcheck);
1109 RETURN((void *)env);
1111 info = echo_env_info(env);
1112 conf = &info->eti_conf;
1114 if (!d->ed_next_islov) {
1115 struct lov_oinfo *oinfo = lsm->lsm_oinfo[0];
1116 LASSERT(oinfo != NULL);
1117 oinfo->loi_id = lsm->lsm_object_id;
1118 oinfo->loi_seq = lsm->lsm_object_seq;
1119 conf->eoc_cl.u.coc_oinfo = oinfo;
1121 struct lustre_md *md;
1123 memset(md, 0, sizeof *md);
1125 conf->eoc_cl.u.coc_md = md;
1128 conf->eoc_md = lsmp;
1130 fid = &info->eti_fid;
1133 /* In the function below, .hs_keycmp resolves to
1134 * lu_obj_hop_keycmp() */
1135 /* coverity[overrun-buffer-val] */
1136 obj = cl_object_find(env, echo_dev2cl(d), fid, &conf->eoc_cl);
1138 GOTO(out, eco = (void*)obj);
1140 eco = cl2echo_obj(obj);
1141 if (eco->eo_deleted) {
1142 cl_object_put(env, obj);
1143 eco = ERR_PTR(-EAGAIN);
1147 cl_env_put(env, &refcheck);
1151 static int cl_echo_object_put(struct echo_object *eco)
1154 struct cl_object *obj = echo_obj2cl(eco);
1158 env = cl_env_get(&refcheck);
1160 RETURN(PTR_ERR(env));
1162 /* an external function to kill an object? */
1163 if (eco->eo_deleted) {
1164 struct lu_object_header *loh = obj->co_lu.lo_header;
1165 LASSERT(&eco->eo_hdr == luh2coh(loh));
1166 cfs_set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
1169 cl_object_put(env, obj);
1170 cl_env_put(env, &refcheck);
1174 static int cl_echo_enqueue0(struct lu_env *env, struct echo_object *eco,
1175 obd_off start, obd_off end, int mode,
1176 __u64 *cookie , __u32 enqflags)
1179 struct cl_lock *lck;
1180 struct cl_object *obj;
1181 struct cl_lock_descr *descr;
1182 struct echo_thread_info *info;
1186 info = echo_env_info(env);
1188 descr = &info->eti_descr;
1189 obj = echo_obj2cl(eco);
1191 descr->cld_obj = obj;
1192 descr->cld_start = cl_index(obj, start);
1193 descr->cld_end = cl_index(obj, end);
1194 descr->cld_mode = mode == LCK_PW ? CLM_WRITE : CLM_READ;
1195 descr->cld_enq_flags = enqflags;
1198 lck = cl_lock_request(env, io, descr, "ec enqueue", eco);
1200 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
1201 struct echo_lock *el;
1203 rc = cl_wait(env, lck);
1205 el = cl2echo_lock(cl_lock_at(lck, &echo_device_type));
1206 cfs_spin_lock(&ec->ec_lock);
1207 if (cfs_list_empty(&el->el_chain)) {
1208 cfs_list_add(&el->el_chain, &ec->ec_locks);
1209 el->el_cookie = ++ec->ec_unique;
1211 cfs_atomic_inc(&el->el_refcount);
1212 *cookie = el->el_cookie;
1213 cfs_spin_unlock(&ec->ec_lock);
1215 cl_lock_release(env, lck, "ec enqueue", cfs_current());
1220 static int cl_echo_enqueue(struct echo_object *eco, obd_off start, obd_off end,
1221 int mode, __u64 *cookie)
1223 struct echo_thread_info *info;
1230 env = cl_env_get(&refcheck);
1232 RETURN(PTR_ERR(env));
1234 info = echo_env_info(env);
1237 io->ci_ignore_layout = 1;
1238 result = cl_io_init(env, io, CIT_MISC, echo_obj2cl(eco));
1241 LASSERT(result == 0);
1243 result = cl_echo_enqueue0(env, eco, start, end, mode, cookie, 0);
1244 cl_io_fini(env, io);
1248 cl_env_put(env, &refcheck);
1252 static int cl_echo_cancel0(struct lu_env *env, struct echo_device *ed,
1255 struct echo_client_obd *ec = ed->ed_ec;
1256 struct echo_lock *ecl = NULL;
1258 int found = 0, still_used = 0;
1261 LASSERT(ec != NULL);
1262 cfs_spin_lock (&ec->ec_lock);
1263 cfs_list_for_each (el, &ec->ec_locks) {
1264 ecl = cfs_list_entry (el, struct echo_lock, el_chain);
1265 CDEBUG(D_INFO, "ecl: %p, cookie: "LPX64"\n", ecl, ecl->el_cookie);
1266 found = (ecl->el_cookie == cookie);
1268 if (cfs_atomic_dec_and_test(&ecl->el_refcount))
1269 cfs_list_del_init(&ecl->el_chain);
1275 cfs_spin_unlock (&ec->ec_lock);
1280 echo_lock_release(env, ecl, still_used);
1284 static int cl_echo_cancel(struct echo_device *ed, __u64 cookie)
1291 env = cl_env_get(&refcheck);
1293 RETURN(PTR_ERR(env));
1295 rc = cl_echo_cancel0(env, ed, cookie);
1297 cl_env_put(env, &refcheck);
1301 static int cl_echo_async_brw(const struct lu_env *env, struct cl_io *io,
1302 enum cl_req_type unused, struct cl_2queue *queue)
1304 struct cl_page *clp;
1305 struct cl_page *temp;
1309 cl_page_list_for_each_safe(clp, temp, &queue->c2_qin) {
1311 rc = cl_page_cache_add(env, io, clp, CRT_WRITE);
1314 result = result ?: rc;
1319 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
1320 cfs_page_t **pages, int npages, int async)
1323 struct echo_thread_info *info;
1324 struct cl_object *obj = echo_obj2cl(eco);
1325 struct echo_device *ed = eco->eo_dev;
1326 struct cl_2queue *queue;
1328 struct cl_page *clp;
1329 struct lustre_handle lh = { 0 };
1330 int page_size = cl_page_size(obj);
1336 LASSERT((offset & ~CFS_PAGE_MASK) == 0);
1337 LASSERT(ed->ed_next != NULL);
1338 env = cl_env_get(&refcheck);
1340 RETURN(PTR_ERR(env));
1342 info = echo_env_info(env);
1344 queue = &info->eti_queue;
1346 cl_2queue_init(queue);
1348 io->ci_ignore_layout = 1;
1349 rc = cl_io_init(env, io, CIT_MISC, obj);
1355 rc = cl_echo_enqueue0(env, eco, offset,
1356 offset + npages * CFS_PAGE_SIZE - 1,
1357 rw == READ ? LCK_PR : LCK_PW, &lh.cookie,
1360 GOTO(error_lock, rc);
1362 for (i = 0; i < npages; i++) {
1364 clp = cl_page_find(env, obj, cl_index(obj, offset),
1365 pages[i], CPT_TRANSIENT);
1370 LASSERT(clp->cp_type == CPT_TRANSIENT);
1372 rc = cl_page_own(env, io, clp);
1374 LASSERT(clp->cp_state == CPS_FREEING);
1375 cl_page_put(env, clp);
1379 cl_2queue_add(queue, clp);
1381 /* drop the reference count for cl_page_find, so that the page
1382 * will be freed in cl_2queue_fini. */
1383 cl_page_put(env, clp);
1384 cl_page_clip(env, clp, 0, page_size);
1386 offset += page_size;
1390 enum cl_req_type typ = rw == READ ? CRT_READ : CRT_WRITE;
1392 async = async && (typ == CRT_WRITE);
1394 rc = cl_echo_async_brw(env, io, typ, queue);
1396 rc = cl_io_submit_sync(env, io, typ, queue, 0);
1397 CDEBUG(D_INFO, "echo_client %s write returns %d\n",
1398 async ? "async" : "sync", rc);
1401 cl_echo_cancel0(env, ed, lh.cookie);
1404 cl_2queue_discard(env, io, queue);
1405 cl_2queue_disown(env, io, queue);
1406 cl_2queue_fini(env, queue);
1407 cl_io_fini(env, io);
1409 cl_env_put(env, &refcheck);
1412 /** @} echo_exports */
1415 static obd_id last_object_id;
1418 echo_copyout_lsm (struct lov_stripe_md *lsm, void *_ulsm, int ulsm_nob)
1420 struct lov_stripe_md *ulsm = _ulsm;
1423 nob = offsetof (struct lov_stripe_md, lsm_oinfo[lsm->lsm_stripe_count]);
1427 if (cfs_copy_to_user (ulsm, lsm, sizeof(ulsm)))
1430 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1431 if (cfs_copy_to_user (ulsm->lsm_oinfo[i], lsm->lsm_oinfo[i],
1432 sizeof(lsm->lsm_oinfo[0])))
1439 echo_copyin_lsm (struct echo_device *ed, struct lov_stripe_md *lsm,
1440 void *ulsm, int ulsm_nob)
1442 struct echo_client_obd *ec = ed->ed_ec;
1445 if (ulsm_nob < sizeof (*lsm))
1448 if (cfs_copy_from_user (lsm, ulsm, sizeof (*lsm)))
1451 if (lsm->lsm_stripe_count > ec->ec_nstripes ||
1452 lsm->lsm_magic != LOV_MAGIC ||
1453 (lsm->lsm_stripe_size & (~CFS_PAGE_MASK)) != 0 ||
1454 ((__u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count > ~0UL))
1458 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1459 if (cfs_copy_from_user(lsm->lsm_oinfo[i],
1460 ((struct lov_stripe_md *)ulsm)-> \
1462 sizeof(lsm->lsm_oinfo[0])))
1468 static inline void echo_md_build_name(struct lu_name *lname, char *name,
1471 sprintf(name, LPU64, id);
1472 lname->ln_name = name;
1473 lname->ln_namelen = strlen(name);
1477 echo_md_create_internal(const struct lu_env *env, struct echo_device *ed,
1478 struct md_object *parent, struct lu_fid *fid,
1479 struct lu_name *lname, struct md_op_spec *spec,
1482 struct lu_object *ec_child, *child;
1483 struct lu_device *ld = ed->ed_next;
1484 struct echo_thread_info *info = echo_env_info(env);
1485 struct lu_fid *fid2 = &info->eti_fid2;
1486 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
1489 rc = mdo_lookup(env, parent, lname, fid2, spec);
1492 else if (rc != -ENOENT)
1495 ec_child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev,
1497 if (IS_ERR(ec_child)) {
1498 CERROR("Can not find the child "DFID": rc = %ld\n", PFID(fid),
1500 return PTR_ERR(ec_child);
1503 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1504 if (child == NULL) {
1505 CERROR("Can not locate the child "DFID"\n", PFID(fid));
1506 GOTO(out_put, rc = -EINVAL);
1509 CDEBUG(D_RPCTRACE, "Start creating object "DFID" %s %p\n",
1510 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1513 * Do not perform lookup sanity check. We know that name does not exist.
1515 spec->sp_cr_lookup = 0;
1516 rc = mdo_create(env, parent, lname, lu2md(child), spec, ma);
1518 CERROR("Can not create child "DFID": rc = %d\n", PFID(fid), rc);
1521 CDEBUG(D_RPCTRACE, "End creating object "DFID" %s %p rc = %d\n",
1522 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent, rc);
1524 lu_object_put(env, ec_child);
1528 static int echo_set_lmm_size(const struct lu_env *env,
1529 struct lu_device *ld,
1530 struct md_attr *ma, int *lmm_size,
1533 struct md_device *md = lu2md_dev(ld);
1538 rc = md->md_ops->mdo_maxsize_get(env, md,
1539 lmm_size, cookie_size);
1543 ma->ma_lmm_size = *lmm_size;
1544 if (*lmm_size > 0) {
1545 OBD_ALLOC(ma->ma_lmm, *lmm_size);
1546 if (ma->ma_lmm == NULL) {
1547 ma->ma_lmm_size = 0;
1552 ma->ma_cookie_size = *cookie_size;
1553 if (*cookie_size > 0) {
1554 OBD_ALLOC(ma->ma_cookie, *cookie_size);
1555 if (ma->ma_cookie == NULL) {
1556 ma->ma_cookie_size = 0;
1564 static int echo_create_md_object(const struct lu_env *env,
1565 struct echo_device *ed,
1566 struct lu_object *ec_parent,
1568 char *name, int namelen,
1569 __u64 id, __u32 mode, int count,
1570 int stripe_count, int stripe_offset)
1572 struct lu_object *parent;
1573 struct echo_thread_info *info = echo_env_info(env);
1574 struct lu_name *lname = &info->eti_lname;
1575 struct md_op_spec *spec = &info->eti_spec;
1576 struct md_attr *ma = &info->eti_ma;
1577 struct lu_device *ld = ed->ed_next;
1580 int cookie_size = 0;
1583 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1584 if (ec_parent == NULL) {
1585 lu_object_put(env, ec_parent);
1586 RETURN(PTR_ERR(parent));
1589 memset(ma, 0, sizeof(*ma));
1590 memset(spec, 0, sizeof(*spec));
1591 if (stripe_count != 0) {
1592 spec->sp_cr_flags |= FMODE_WRITE;
1593 rc = echo_set_lmm_size(env, ld, ma, &lmm_size, &cookie_size);
1596 if (stripe_count != -1) {
1597 struct lov_user_md_v3 *lum = &info->eti_lum;
1598 lum->lmm_magic = LOV_USER_MAGIC_V3;
1599 lum->lmm_stripe_count = stripe_count;
1600 lum->lmm_stripe_offset = stripe_offset;
1601 lum->lmm_pattern = 0;
1602 spec->u.sp_ea.eadata = lum;
1603 spec->u.sp_ea.eadatalen = sizeof(*lum);
1604 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1608 ma->ma_attr.la_mode = mode;
1609 ma->ma_attr.la_valid = LA_CTIME | LA_MODE;
1610 ma->ma_attr.la_ctime = cfs_time_current_64();
1613 lname->ln_name = name;
1614 lname->ln_namelen = namelen;
1615 /* If name is specified, only create one object by name */
1616 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1621 /* Create multiple object sequenced by id */
1622 for (i = 0; i < count; i++) {
1623 char *tmp_name = info->eti_name;
1625 echo_md_build_name(lname, tmp_name, id);
1627 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1630 CERROR("Can not create child %s: rc = %d\n", tmp_name,
1639 if (lmm_size > 0 && ma->ma_lmm != NULL)
1640 OBD_FREE(ma->ma_lmm, lmm_size);
1641 if (cookie_size > 0 && ma->ma_cookie != NULL)
1642 OBD_FREE(ma->ma_cookie, cookie_size);
1647 static struct lu_object *echo_md_lookup(const struct lu_env *env,
1648 struct echo_device *ed,
1649 struct md_object *parent,
1650 struct lu_name *lname)
1652 struct echo_thread_info *info = echo_env_info(env);
1653 struct lu_fid *fid = &info->eti_fid;
1654 struct lu_object *child;
1658 CDEBUG(D_INFO, "lookup %s in parent "DFID" %p\n", lname->ln_name,
1660 rc = mdo_lookup(env, parent, lname, fid, NULL);
1662 CERROR("lookup %s: rc = %d\n", lname->ln_name, rc);
1663 RETURN(ERR_PTR(rc));
1666 child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1671 static int echo_setattr_object(const struct lu_env *env,
1672 struct echo_device *ed,
1673 struct lu_object *ec_parent,
1674 __u64 id, int count)
1676 struct lu_object *parent;
1677 struct echo_thread_info *info = echo_env_info(env);
1678 struct lu_name *lname = &info->eti_lname;
1679 char *name = info->eti_name;
1680 struct lu_device *ld = ed->ed_next;
1681 struct lu_buf *buf = &info->eti_buf;
1685 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1686 if (ec_parent == NULL) {
1687 lu_object_put(env, ec_parent);
1688 return PTR_ERR(parent);
1691 buf->lb_buf = info->eti_xattr_buf;
1692 buf->lb_len = sizeof(info->eti_xattr_buf);
1693 for (i = 0; i < count; i++) {
1694 struct lu_object *ec_child, *child;
1696 echo_md_build_name(lname, name, id);
1698 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1699 if (IS_ERR(ec_child)) {
1700 CERROR("Can't find child %s: rc = %ld\n",
1701 lname->ln_name, PTR_ERR(ec_child));
1702 RETURN(PTR_ERR(ec_child));
1705 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1706 if (child == NULL) {
1707 CERROR("Can not locate the child %s\n", lname->ln_name);
1708 lu_object_put(env, ec_child);
1713 CDEBUG(D_RPCTRACE, "Start setattr object "DFID"\n",
1714 PFID(lu_object_fid(child)));
1716 sprintf(name, "%s.test1", XATTR_USER_PREFIX);
1717 rc = mo_xattr_set(env, lu2md(child), buf, name,
1720 CERROR("Can not setattr child "DFID": rc = %d\n",
1721 PFID(lu_object_fid(child)), rc);
1722 lu_object_put(env, ec_child);
1725 CDEBUG(D_RPCTRACE, "End setattr object "DFID"\n",
1726 PFID(lu_object_fid(child)));
1728 lu_object_put(env, ec_child);
1733 static int echo_getattr_object(const struct lu_env *env,
1734 struct echo_device *ed,
1735 struct lu_object *ec_parent,
1736 __u64 id, int count)
1738 struct lu_object *parent;
1739 struct echo_thread_info *info = echo_env_info(env);
1740 struct lu_name *lname = &info->eti_lname;
1741 char *name = info->eti_name;
1742 struct md_attr *ma = &info->eti_ma;
1743 struct lu_device *ld = ed->ed_next;
1746 int cookie_size = 0;
1749 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1750 if (ec_parent == NULL) {
1751 lu_object_put(env, ec_parent);
1752 return PTR_ERR(parent);
1755 memset(ma, 0, sizeof(*ma));
1756 rc = echo_set_lmm_size(env, ld, ma, &lmm_size, &cookie_size);
1760 ma->ma_need |= MA_INODE | MA_LOV | MA_PFID | MA_HSM | MA_ACL_DEF;
1761 ma->ma_acl = info->eti_xattr_buf;
1762 ma->ma_acl_size = sizeof(info->eti_xattr_buf);
1764 for (i = 0; i < count; i++) {
1765 struct lu_object *ec_child, *child;
1768 echo_md_build_name(lname, name, id);
1770 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1771 if (IS_ERR(ec_child)) {
1772 CERROR("Can't find child %s: rc = %ld\n",
1773 lname->ln_name, PTR_ERR(ec_child));
1774 RETURN(PTR_ERR(ec_child));
1777 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1778 if (child == NULL) {
1779 CERROR("Can not locate the child %s\n", lname->ln_name);
1780 lu_object_put(env, ec_child);
1781 GOTO(out_free, rc = -EINVAL);
1784 CDEBUG(D_RPCTRACE, "Start getattr object "DFID"\n",
1785 PFID(lu_object_fid(child)));
1786 rc = mo_attr_get(env, lu2md(child), ma);
1788 CERROR("Can not getattr child "DFID": rc = %d\n",
1789 PFID(lu_object_fid(child)), rc);
1790 lu_object_put(env, ec_child);
1793 CDEBUG(D_RPCTRACE, "End getattr object "DFID"\n",
1794 PFID(lu_object_fid(child)));
1796 lu_object_put(env, ec_child);
1800 if (lmm_size > 0 && ma->ma_lmm != NULL)
1801 OBD_FREE(ma->ma_lmm, lmm_size);
1802 if (cookie_size > 0 && ma->ma_cookie != NULL)
1803 OBD_FREE(ma->ma_cookie, cookie_size);
1807 static int echo_lookup_object(const struct lu_env *env,
1808 struct echo_device *ed,
1809 struct lu_object *ec_parent,
1810 __u64 id, int count)
1812 struct lu_object *parent;
1813 struct echo_thread_info *info = echo_env_info(env);
1814 struct lu_name *lname = &info->eti_lname;
1815 char *name = info->eti_name;
1816 struct lu_fid *fid = &info->eti_fid;
1817 struct lu_device *ld = ed->ed_next;
1821 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1822 if (ec_parent == NULL) {
1823 lu_object_put(env, ec_parent);
1824 return PTR_ERR(parent);
1827 /*prepare the requests*/
1828 for (i = 0; i < count; i++) {
1829 echo_md_build_name(lname, name, id);
1831 CDEBUG(D_RPCTRACE, "Start lookup object "DFID" %s %p\n",
1832 PFID(lu_object_fid(parent)), lname->ln_name, parent);
1834 rc = mdo_lookup(env, lu2md(parent), lname, fid, NULL);
1836 CERROR("Can not lookup child %s: rc = %d\n", name, rc);
1839 CDEBUG(D_RPCTRACE, "End lookup object "DFID" %s %p\n",
1840 PFID(lu_object_fid(parent)), lname->ln_name, parent);
1847 static int echo_md_destroy_internal(const struct lu_env *env,
1848 struct echo_device *ed,
1849 struct md_object *parent,
1850 struct lu_name *lname,
1853 struct lu_device *ld = ed->ed_next;
1854 struct lu_object *ec_child;
1855 struct lu_object *child;
1858 ec_child = echo_md_lookup(env, ed, parent, lname);
1859 if (IS_ERR(ec_child)) {
1860 CERROR("Can't find child %s: rc = %ld\n", lname->ln_name,
1862 RETURN(PTR_ERR(ec_child));
1865 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1866 if (child == NULL) {
1867 CERROR("Can not locate the child %s\n", lname->ln_name);
1868 GOTO(out_put, rc = -EINVAL);
1871 CDEBUG(D_RPCTRACE, "Start destroy object "DFID" %s %p\n",
1872 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1874 rc = mdo_unlink(env, parent, lu2md(child), lname, ma);
1876 CERROR("Can not unlink child %s: rc = %d\n",
1877 lname->ln_name, rc);
1880 CDEBUG(D_RPCTRACE, "End destroy object "DFID" %s %p\n",
1881 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1883 lu_object_put(env, ec_child);
1887 static int echo_destroy_object(const struct lu_env *env,
1888 struct echo_device *ed,
1889 struct lu_object *ec_parent,
1890 char *name, int namelen,
1891 __u64 id, __u32 mode,
1894 struct echo_thread_info *info = echo_env_info(env);
1895 struct lu_name *lname = &info->eti_lname;
1896 struct md_attr *ma = &info->eti_ma;
1897 struct lu_device *ld = ed->ed_next;
1898 struct lu_object *parent;
1901 int cookie_size = 0;
1905 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1909 memset(ma, 0, sizeof(*ma));
1910 ma->ma_attr.la_mode = mode;
1911 ma->ma_attr.la_valid = LA_CTIME;
1912 ma->ma_attr.la_ctime = cfs_time_current_64();
1913 ma->ma_need = MA_INODE;
1916 rc = echo_set_lmm_size(env, ld, ma, &lmm_size, &cookie_size);
1920 lname->ln_name = name;
1921 lname->ln_namelen = namelen;
1922 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
1927 /*prepare the requests*/
1928 for (i = 0; i < count; i++) {
1929 char *tmp_name = info->eti_name;
1931 ma->ma_need |= MA_LOV;
1933 echo_md_build_name(lname, tmp_name, id);
1935 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
1938 CERROR("Can not unlink child %s: rc = %d\n", name, rc);
1945 if (lmm_size > 0 && ma->ma_lmm != NULL)
1946 OBD_FREE(ma->ma_lmm, lmm_size);
1947 if (cookie_size > 0 && ma->ma_cookie != NULL)
1948 OBD_FREE(ma->ma_cookie, cookie_size);
1952 static struct lu_object *echo_resolve_path(const struct lu_env *env,
1953 struct echo_device *ed, char *path,
1956 struct lu_device *ld = ed->ed_next;
1957 struct md_device *md = lu2md_dev(ld);
1958 struct echo_thread_info *info = echo_env_info(env);
1959 struct lu_fid *fid = &info->eti_fid;
1960 struct lu_name *lname = &info->eti_lname;
1961 struct lu_object *parent = NULL;
1962 struct lu_object *child = NULL;
1966 /*Only support MDD layer right now*/
1967 rc = md->md_ops->mdo_root_get(env, md, fid);
1969 CERROR("get root error: rc = %d\n", rc);
1970 RETURN(ERR_PTR(rc));
1973 parent = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1974 if (IS_ERR(parent)) {
1975 CERROR("Can not find the parent "DFID": rc = %ld\n",
1976 PFID(fid), PTR_ERR(parent));
1981 struct lu_object *ld_parent;
1984 e = strsep(&path, "/");
1989 if (!path || path[0] == '\0')
1995 lname->ln_namelen = strlen(e);
1997 ld_parent = lu_object_locate(parent->lo_header, ld->ld_type);
1998 if (ld_parent == NULL) {
1999 lu_object_put(env, parent);
2004 child = echo_md_lookup(env, ed, lu2md(ld_parent), lname);
2005 lu_object_put(env, parent);
2006 if (IS_ERR(child)) {
2007 rc = (int)PTR_ERR(child);
2008 CERROR("lookup %s under parent "DFID": rc = %d\n",
2009 lname->ln_name, PFID(lu_object_fid(ld_parent)),
2016 RETURN(ERR_PTR(rc));
2021 #define ECHO_MD_CTX_TAG (LCT_REMEMBER | LCT_MD_THREAD)
2022 #define ECHO_MD_SES_TAG (LCT_REMEMBER | LCT_SESSION)
2023 static int echo_md_handler(struct echo_device *ed, int command,
2024 char *path, int path_len, int id, int count,
2025 struct obd_ioctl_data *data)
2027 struct lu_device *ld = ed->ed_next;
2030 struct lu_object *parent;
2032 int namelen = data->ioc_plen2;
2037 CERROR("MD echo client is not being initialized properly\n");
2041 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
2042 CERROR("Only support MDD layer right now!\n");
2046 env = cl_env_get(&refcheck);
2048 RETURN(PTR_ERR(env));
2050 rc = lu_env_refill_by_tags(env, ECHO_MD_CTX_TAG, ECHO_MD_SES_TAG);
2052 cl_env_put(env, &refcheck);
2056 parent = echo_resolve_path(env, ed, path, path_len);
2057 if (IS_ERR(parent)) {
2058 CERROR("Can not resolve the path %s: rc = %ld\n", path,
2060 cl_env_put(env, &refcheck);
2061 RETURN(PTR_ERR(parent));
2065 OBD_ALLOC(name, namelen + 1);
2068 if (cfs_copy_from_user(name, data->ioc_pbuf2, namelen)) {
2069 OBD_FREE(name, namelen + 1);
2075 case ECHO_MD_CREATE:
2076 case ECHO_MD_MKDIR: {
2077 struct echo_thread_info *info = echo_env_info(env);
2078 __u32 mode = data->ioc_obdo2.o_mode;
2079 struct lu_fid *fid = &info->eti_fid;
2080 int stripe_count = (int)data->ioc_obdo2.o_misc;
2081 int stripe_index = (int)data->ioc_obdo2.o_stripe_idx;
2083 fid->f_seq = data->ioc_obdo1.o_seq;
2084 fid->f_oid = (__u32)data->ioc_obdo1.o_id;
2086 /* In the function below, .hs_keycmp resolves to
2087 * lu_obj_hop_keycmp() */
2088 /* coverity[overrun-buffer-val] */
2089 rc = echo_create_md_object(env, ed, parent, fid, name, namelen,
2090 id, mode, count, stripe_count,
2094 case ECHO_MD_DESTROY:
2095 case ECHO_MD_RMDIR: {
2096 __u32 mode = data->ioc_obdo2.o_mode;
2098 rc = echo_destroy_object(env, ed, parent, name, namelen,
2102 case ECHO_MD_LOOKUP:
2103 rc = echo_lookup_object(env, ed, parent, id, count);
2105 case ECHO_MD_GETATTR:
2106 rc = echo_getattr_object(env, ed, parent, id, count);
2108 case ECHO_MD_SETATTR:
2109 rc = echo_setattr_object(env, ed, parent, id, count);
2112 CERROR("unknown command %d\n", command);
2117 OBD_FREE(name, namelen + 1);
2118 lu_object_put(env, parent);
2119 cl_env_put(env, &refcheck);
2123 static int echo_create_object(const struct lu_env *env, struct echo_device *ed,
2124 int on_target, struct obdo *oa, void *ulsm,
2125 int ulsm_nob, struct obd_trans_info *oti)
2127 struct echo_object *eco;
2128 struct echo_client_obd *ec = ed->ed_ec;
2129 struct lov_stripe_md *lsm = NULL;
2134 if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
2135 (on_target || /* set_stripe */
2136 ec->ec_nstripes != 0)) { /* LOV */
2137 CERROR ("No valid oid\n");
2141 rc = echo_alloc_memmd(ed, &lsm);
2143 CERROR("Cannot allocate md: rc = %d\n", rc);
2150 rc = echo_copyin_lsm (ed, lsm, ulsm, ulsm_nob);
2154 if (lsm->lsm_stripe_count == 0)
2155 lsm->lsm_stripe_count = ec->ec_nstripes;
2157 if (lsm->lsm_stripe_size == 0)
2158 lsm->lsm_stripe_size = CFS_PAGE_SIZE;
2162 /* setup stripes: indices + default ids if required */
2163 for (i = 0; i < lsm->lsm_stripe_count; i++) {
2164 if (lsm->lsm_oinfo[i]->loi_id == 0)
2165 lsm->lsm_oinfo[i]->loi_id = lsm->lsm_object_id;
2167 lsm->lsm_oinfo[i]->loi_ost_idx =
2168 (idx + i) % ec->ec_nstripes;
2172 /* setup object ID here for !on_target and LOV hint */
2173 if (oa->o_valid & OBD_MD_FLID)
2174 lsm->lsm_object_id = oa->o_id;
2176 if (lsm->lsm_object_id == 0)
2177 lsm->lsm_object_id = ++last_object_id;
2181 /* Only echo objects are allowed to be created */
2182 LASSERT((oa->o_valid & OBD_MD_FLGROUP) &&
2183 (oa->o_seq == FID_SEQ_ECHO));
2184 rc = obd_create(env, ec->ec_exp, oa, &lsm, oti);
2186 CERROR("Cannot create objects: rc = %d\n", rc);
2192 /* See what object ID we were given */
2193 oa->o_id = lsm->lsm_object_id;
2194 oa->o_valid |= OBD_MD_FLID;
2196 eco = cl_echo_object_find(ed, &lsm);
2198 GOTO(failed, rc = PTR_ERR(eco));
2199 cl_echo_object_put(eco);
2201 CDEBUG(D_INFO, "oa->o_id = %lx\n", (long)oa->o_id);
2206 obd_destroy(env, ec->ec_exp, oa, lsm, oti, NULL, NULL);
2208 echo_free_memmd(ed, &lsm);
2210 CERROR("create object failed with: rc = %d\n", rc);
2214 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
2217 struct lov_stripe_md *lsm = NULL;
2218 struct echo_object *eco;
2222 if ((oa->o_valid & OBD_MD_FLID) == 0 ||
2223 oa->o_id == 0) /* disallow use of object id 0 */
2225 CERROR ("No valid oid\n");
2229 rc = echo_alloc_memmd(ed, &lsm);
2233 lsm->lsm_object_id = oa->o_id;
2234 if (oa->o_valid & OBD_MD_FLGROUP)
2235 lsm->lsm_object_seq = oa->o_seq;
2237 lsm->lsm_object_seq = FID_SEQ_ECHO;
2240 eco = cl_echo_object_find(ed, &lsm);
2246 echo_free_memmd(ed, &lsm);
2250 static void echo_put_object(struct echo_object *eco)
2252 if (cl_echo_object_put(eco))
2253 CERROR("echo client: drop an object failed");
2257 echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
2259 unsigned long stripe_count;
2260 unsigned long stripe_size;
2261 unsigned long width;
2262 unsigned long woffset;
2266 if (lsm->lsm_stripe_count <= 1)
2270 stripe_size = lsm->lsm_stripe_size;
2271 stripe_count = lsm->lsm_stripe_count;
2273 /* width = # bytes in all stripes */
2274 width = stripe_size * stripe_count;
2276 /* woffset = offset within a width; offset = whole number of widths */
2277 woffset = do_div (offset, width);
2279 stripe_index = woffset / stripe_size;
2281 *idp = lsm->lsm_oinfo[stripe_index]->loi_id;
2282 *offp = offset * stripe_size + woffset % stripe_size;
2286 echo_client_page_debug_setup(struct lov_stripe_md *lsm,
2287 cfs_page_t *page, int rw, obd_id id,
2288 obd_off offset, obd_off count)
2295 /* no partial pages on the client */
2296 LASSERT(count == CFS_PAGE_SIZE);
2298 addr = cfs_kmap(page);
2300 for (delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2301 if (rw == OBD_BRW_WRITE) {
2302 stripe_off = offset + delta;
2304 echo_get_stripe_off_id(lsm, &stripe_off, &stripe_id);
2306 stripe_off = 0xdeadbeef00c0ffeeULL;
2307 stripe_id = 0xdeadbeef00c0ffeeULL;
2309 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
2310 stripe_off, stripe_id);
2316 static int echo_client_page_debug_check(struct lov_stripe_md *lsm,
2317 cfs_page_t *page, obd_id id,
2318 obd_off offset, obd_off count)
2327 /* no partial pages on the client */
2328 LASSERT(count == CFS_PAGE_SIZE);
2330 addr = cfs_kmap(page);
2332 for (rc = delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2333 stripe_off = offset + delta;
2335 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
2337 rc2 = block_debug_check("test_brw",
2338 addr + delta, OBD_ECHO_BLOCK_SIZE,
2339 stripe_off, stripe_id);
2341 CERROR ("Error in echo object "LPX64"\n", id);
2350 static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
2351 struct echo_object *eco, obd_off offset,
2352 obd_size count, int async,
2353 struct obd_trans_info *oti)
2355 struct lov_stripe_md *lsm = eco->eo_lsm;
2357 struct brw_page *pga;
2358 struct brw_page *pgp;
2368 verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
2369 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
2370 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
2372 gfp_mask = ((oa->o_id & 2) == 0) ? CFS_ALLOC_STD : CFS_ALLOC_HIGHUSER;
2374 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
2375 LASSERT(lsm != NULL);
2376 LASSERT(lsm->lsm_object_id == oa->o_id);
2379 (count & (~CFS_PAGE_MASK)) != 0)
2382 /* XXX think again with misaligned I/O */
2383 npages = count >> CFS_PAGE_SHIFT;
2385 if (rw == OBD_BRW_WRITE)
2386 brw_flags = OBD_BRW_ASYNC;
2388 OBD_ALLOC(pga, npages * sizeof(*pga));
2392 OBD_ALLOC(pages, npages * sizeof(*pages));
2393 if (pages == NULL) {
2394 OBD_FREE(pga, npages * sizeof(*pga));
2398 for (i = 0, pgp = pga, off = offset;
2400 i++, pgp++, off += CFS_PAGE_SIZE) {
2402 LASSERT (pgp->pg == NULL); /* for cleanup */
2405 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
2406 if (pgp->pg == NULL)
2410 pgp->count = CFS_PAGE_SIZE;
2412 pgp->flag = brw_flags;
2415 echo_client_page_debug_setup(lsm, pgp->pg, rw,
2416 oa->o_id, off, pgp->count);
2419 /* brw mode can only be used at client */
2420 LASSERT(ed->ed_next != NULL);
2421 rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
2424 if (rc != 0 || rw != OBD_BRW_READ)
2427 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
2428 if (pgp->pg == NULL)
2433 vrc = echo_client_page_debug_check(lsm, pgp->pg, oa->o_id,
2434 pgp->off, pgp->count);
2435 if (vrc != 0 && rc == 0)
2438 OBD_PAGE_FREE(pgp->pg);
2440 OBD_FREE(pga, npages * sizeof(*pga));
2441 OBD_FREE(pages, npages * sizeof(*pages));
2445 static int echo_client_prep_commit(const struct lu_env *env,
2446 struct obd_export *exp, int rw,
2447 struct obdo *oa, struct echo_object *eco,
2448 obd_off offset, obd_size count,
2449 obd_size batch, struct obd_trans_info *oti,
2452 struct lov_stripe_md *lsm = eco->eo_lsm;
2453 struct obd_ioobj ioo;
2454 struct niobuf_local *lnb;
2455 struct niobuf_remote *rnb;
2457 obd_size npages, tot_pages;
2458 int i, ret = 0, brw_flags = 0;
2462 if (count <= 0 || (count & (~CFS_PAGE_MASK)) != 0 ||
2463 (lsm != NULL && lsm->lsm_object_id != oa->o_id))
2466 npages = batch >> CFS_PAGE_SHIFT;
2467 tot_pages = count >> CFS_PAGE_SHIFT;
2469 OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
2470 OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
2472 if (lnb == NULL || rnb == NULL)
2473 GOTO(out, ret = -ENOMEM);
2475 if (rw == OBD_BRW_WRITE && async)
2476 brw_flags |= OBD_BRW_ASYNC;
2478 obdo_to_ioobj(oa, &ioo);
2482 for(; tot_pages; tot_pages -= npages) {
2485 if (tot_pages < npages)
2488 for (i = 0; i < npages; i++, off += CFS_PAGE_SIZE) {
2489 rnb[i].offset = off;
2490 rnb[i].len = CFS_PAGE_SIZE;
2491 rnb[i].flags = brw_flags;
2494 ioo.ioo_bufcnt = npages;
2495 oti->oti_transno = 0;
2498 ret = obd_preprw(env, rw, exp, oa, 1, &ioo, rnb, &lpages,
2502 LASSERT(lpages == npages);
2504 for (i = 0; i < lpages; i++) {
2505 cfs_page_t *page = lnb[i].page;
2507 /* read past eof? */
2508 if (page == NULL && lnb[i].rc == 0)
2512 lnb[i].flags |= OBD_BRW_ASYNC;
2514 if (oa->o_id == ECHO_PERSISTENT_OBJID ||
2515 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
2516 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
2519 if (rw == OBD_BRW_WRITE)
2520 echo_client_page_debug_setup(lsm, page, rw,
2525 echo_client_page_debug_check(lsm, page,
2531 ret = obd_commitrw(env, rw, exp, oa, 1, &ioo,
2532 rnb, npages, lnb, oti, ret);
2536 /* Reset oti otherwise it would confuse ldiskfs. */
2537 memset(oti, 0, sizeof(*oti));
2542 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
2544 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
2548 static int echo_client_brw_ioctl(const struct lu_env *env, int rw,
2549 struct obd_export *exp,
2550 struct obd_ioctl_data *data,
2551 struct obd_trans_info *dummy_oti)
2553 struct obd_device *obd = class_exp2obd(exp);
2554 struct echo_device *ed = obd2echo_dev(obd);
2555 struct echo_client_obd *ec = ed->ed_ec;
2556 struct obdo *oa = &data->ioc_obdo1;
2557 struct echo_object *eco;
2563 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2565 rc = echo_get_object(&eco, ed, oa);
2569 oa->o_valid &= ~OBD_MD_FLHANDLE;
2571 /* OFD/obdfilter works only via prep/commit */
2572 test_mode = (long)data->ioc_pbuf1;
2576 if (ed->ed_next == NULL && test_mode != 3) {
2578 data->ioc_plen1 = data->ioc_count;
2581 /* Truncate batch size to maximum */
2582 if (data->ioc_plen1 > PTLRPC_MAX_BRW_SIZE)
2583 data->ioc_plen1 = PTLRPC_MAX_BRW_SIZE;
2585 switch (test_mode) {
2589 rc = echo_client_kbrw(ed, rw, oa,
2590 eco, data->ioc_offset,
2591 data->ioc_count, async, dummy_oti);
2594 rc = echo_client_prep_commit(env, ec->ec_exp, rw, oa,
2595 eco, data->ioc_offset,
2596 data->ioc_count, data->ioc_plen1,
2602 echo_put_object(eco);
2607 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
2608 int mode, obd_off offset, obd_size nob)
2610 struct echo_device *ed = obd2echo_dev(exp->exp_obd);
2611 struct lustre_handle *ulh = &oa->o_handle;
2612 struct echo_object *eco;
2617 if (ed->ed_next == NULL)
2618 RETURN(-EOPNOTSUPP);
2620 if (!(mode == LCK_PR || mode == LCK_PW))
2623 if ((offset & (~CFS_PAGE_MASK)) != 0 ||
2624 (nob & (~CFS_PAGE_MASK)) != 0)
2627 rc = echo_get_object (&eco, ed, oa);
2631 end = (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
2632 rc = cl_echo_enqueue(eco, offset, end, mode, &ulh->cookie);
2634 oa->o_valid |= OBD_MD_FLHANDLE;
2635 CDEBUG(D_INFO, "Cookie is "LPX64"\n", ulh->cookie);
2637 echo_put_object(eco);
2642 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
2644 struct echo_device *ed = obd2echo_dev(exp->exp_obd);
2645 __u64 cookie = oa->o_handle.cookie;
2647 if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
2650 CDEBUG(D_INFO, "Cookie is "LPX64"\n", cookie);
2651 return cl_echo_cancel(ed, cookie);
2655 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2656 void *karg, void *uarg)
2658 struct obd_device *obd = exp->exp_obd;
2659 struct echo_device *ed = obd2echo_dev(obd);
2660 struct echo_client_obd *ec = ed->ed_ec;
2661 struct echo_object *eco;
2662 struct obd_ioctl_data *data = karg;
2663 struct obd_trans_info dummy_oti;
2665 struct oti_req_ack_lock *ack_lock;
2668 int rw = OBD_BRW_READ;
2673 memset(&dummy_oti, 0, sizeof(dummy_oti));
2675 oa = &data->ioc_obdo1;
2676 if (!(oa->o_valid & OBD_MD_FLGROUP)) {
2677 oa->o_valid |= OBD_MD_FLGROUP;
2678 oa->o_seq = FID_SEQ_ECHO;
2681 /* This FID is unpacked just for validation at this point */
2682 rc = fid_ostid_unpack(&fid, &oa->o_oi, 0);
2690 rc = lu_env_init(env, LCT_DT_THREAD);
2692 GOTO(out, rc = -ENOMEM);
2695 case OBD_IOC_CREATE: /* may create echo object */
2696 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2697 GOTO (out, rc = -EPERM);
2699 rc = echo_create_object(env, ed, 1, oa, data->ioc_pbuf1,
2700 data->ioc_plen1, &dummy_oti);
2703 case OBD_IOC_ECHO_MD: {
2710 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2711 GOTO(out, rc = -EPERM);
2713 count = data->ioc_count;
2714 cmd = data->ioc_command;
2716 id = data->ioc_obdo2.o_id;
2718 dirlen = data->ioc_plen1;
2719 OBD_ALLOC(dir, dirlen + 1);
2721 GOTO(out, rc = -ENOMEM);
2723 if (cfs_copy_from_user(dir, data->ioc_pbuf1, dirlen)) {
2724 OBD_FREE(dir, data->ioc_plen1 + 1);
2725 GOTO(out, rc = -EFAULT);
2728 rc = echo_md_handler(ed, cmd, dir, dirlen, id, count, data);
2729 OBD_FREE(dir, dirlen + 1);
2732 case OBD_IOC_ECHO_ALLOC_SEQ: {
2733 struct lu_env *cl_env;
2738 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2739 GOTO(out, rc = -EPERM);
2741 cl_env = cl_env_get(&refcheck);
2743 GOTO(out, rc = PTR_ERR(cl_env));
2745 rc = lu_env_refill_by_tags(cl_env, ECHO_MD_CTX_TAG,
2748 cl_env_put(cl_env, &refcheck);
2752 rc = seq_client_get_seq(cl_env, ed->ed_cl_seq, &seq);
2753 cl_env_put(cl_env, &refcheck);
2755 CERROR("%s: Can not alloc seq: rc = %d\n",
2760 if (cfs_copy_to_user(data->ioc_pbuf1, &seq, data->ioc_plen1))
2763 max_count = LUSTRE_SEQ_MAX_WIDTH;
2764 if (cfs_copy_to_user(data->ioc_pbuf2, &max_count,
2769 case OBD_IOC_DESTROY:
2770 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2771 GOTO (out, rc = -EPERM);
2773 rc = echo_get_object(&eco, ed, oa);
2775 rc = obd_destroy(env, ec->ec_exp, oa, eco->eo_lsm,
2776 &dummy_oti, NULL, NULL);
2778 eco->eo_deleted = 1;
2779 echo_put_object(eco);
2783 case OBD_IOC_GETATTR:
2784 rc = echo_get_object(&eco, ed, oa);
2786 struct obd_info oinfo = { { { 0 } } };
2787 oinfo.oi_md = eco->eo_lsm;
2789 rc = obd_getattr(env, ec->ec_exp, &oinfo);
2790 echo_put_object(eco);
2794 case OBD_IOC_SETATTR:
2795 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2796 GOTO (out, rc = -EPERM);
2798 rc = echo_get_object(&eco, ed, oa);
2800 struct obd_info oinfo = { { { 0 } } };
2802 oinfo.oi_md = eco->eo_lsm;
2804 rc = obd_setattr(env, ec->ec_exp, &oinfo, NULL);
2805 echo_put_object(eco);
2809 case OBD_IOC_BRW_WRITE:
2810 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2811 GOTO (out, rc = -EPERM);
2815 case OBD_IOC_BRW_READ:
2816 rc = echo_client_brw_ioctl(env, rw, exp, data, &dummy_oti);
2819 case ECHO_IOC_GET_STRIPE:
2820 rc = echo_get_object(&eco, ed, oa);
2822 rc = echo_copyout_lsm(eco->eo_lsm, data->ioc_pbuf1,
2824 echo_put_object(eco);
2828 case ECHO_IOC_SET_STRIPE:
2829 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2830 GOTO (out, rc = -EPERM);
2832 if (data->ioc_pbuf1 == NULL) { /* unset */
2833 rc = echo_get_object(&eco, ed, oa);
2835 eco->eo_deleted = 1;
2836 echo_put_object(eco);
2839 rc = echo_create_object(env, ed, 0, oa,
2841 data->ioc_plen1, &dummy_oti);
2845 case ECHO_IOC_ENQUEUE:
2846 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2847 GOTO (out, rc = -EPERM);
2849 rc = echo_client_enqueue(exp, oa,
2850 data->ioc_conn1, /* lock mode */
2852 data->ioc_count);/*extent*/
2855 case ECHO_IOC_CANCEL:
2856 rc = echo_client_cancel(exp, oa);
2860 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
2861 GOTO (out, rc = -ENOTTY);
2869 /* XXX this should be in a helper also called by target_send_reply */
2870 for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
2872 if (!ack_lock->mode)
2874 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
2880 static int echo_client_setup(const struct lu_env *env,
2881 struct obd_device *obddev, struct lustre_cfg *lcfg)
2883 struct echo_client_obd *ec = &obddev->u.echo_client;
2884 struct obd_device *tgt;
2885 struct obd_uuid echo_uuid = { "ECHO_UUID" };
2886 struct obd_connect_data *ocd = NULL;
2890 if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
2891 CERROR("requires a TARGET OBD name\n");
2895 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
2896 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
2897 CERROR("device not attached or not set up (%s)\n",
2898 lustre_cfg_string(lcfg, 1));
2902 cfs_spin_lock_init (&ec->ec_lock);
2903 CFS_INIT_LIST_HEAD (&ec->ec_objects);
2904 CFS_INIT_LIST_HEAD (&ec->ec_locks);
2906 ec->ec_nstripes = 0;
2908 if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
2909 lu_context_tags_update(ECHO_MD_CTX_TAG);
2910 lu_session_tags_update(ECHO_MD_SES_TAG);
2914 OBD_ALLOC(ocd, sizeof(*ocd));
2916 CERROR("Can't alloc ocd connecting to %s\n",
2917 lustre_cfg_string(lcfg, 1));
2921 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
2922 OBD_CONNECT_GRANT | OBD_CONNECT_FULL20 |
2923 OBD_CONNECT_64BITHASH;
2924 ocd->ocd_version = LUSTRE_VERSION_CODE;
2925 ocd->ocd_group = FID_SEQ_ECHO;
2927 rc = obd_connect(env, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
2929 /* Turn off pinger because it connects to tgt obd directly. */
2930 cfs_spin_lock(&tgt->obd_dev_lock);
2931 cfs_list_del_init(&ec->ec_exp->exp_obd_chain_timed);
2932 cfs_spin_unlock(&tgt->obd_dev_lock);
2935 OBD_FREE(ocd, sizeof(*ocd));
2938 CERROR("fail to connect to device %s\n",
2939 lustre_cfg_string(lcfg, 1));
2946 static int echo_client_cleanup(struct obd_device *obddev)
2948 struct echo_device *ed = obd2echo_dev(obddev);
2949 struct echo_client_obd *ec = &obddev->u.echo_client;
2953 /*Do nothing for Metadata echo client*/
2957 if (ed->ed_next_ismd) {
2958 lu_context_tags_clear(ECHO_MD_CTX_TAG);
2959 lu_session_tags_clear(ECHO_MD_SES_TAG);
2963 if (!cfs_list_empty(&obddev->obd_exports)) {
2964 CERROR("still has clients!\n");
2968 LASSERT(cfs_atomic_read(&ec->ec_exp->exp_refcount) > 0);
2969 rc = obd_disconnect(ec->ec_exp);
2971 CERROR("fail to disconnect device: %d\n", rc);
2976 static int echo_client_connect(const struct lu_env *env,
2977 struct obd_export **exp,
2978 struct obd_device *src, struct obd_uuid *cluuid,
2979 struct obd_connect_data *data, void *localdata)
2982 struct lustre_handle conn = { 0 };
2985 rc = class_connect(&conn, src, cluuid);
2987 *exp = class_conn2export(&conn);
2993 static int echo_client_disconnect(struct obd_export *exp)
2996 struct obd_device *obd;
2997 struct echo_client_obd *ec;
2998 struct ec_lock *ecl;
3004 GOTO(out, rc = -EINVAL);
3008 ec = &obd->u.echo_client;
3010 /* no more contention on export's lock list */
3011 while (!cfs_list_empty (&exp->exp_ec_data.eced_locks)) {
3012 ecl = cfs_list_entry (exp->exp_ec_data.eced_locks.next,
3013 struct ec_lock, ecl_exp_chain);
3014 cfs_list_del (&ecl->ecl_exp_chain);
3016 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm,
3017 ecl->ecl_mode, &ecl->ecl_lock_handle);
3019 CDEBUG (D_INFO, "Cancel lock on object "LPX64" on disconnect "
3020 "(%d)\n", ecl->ecl_object->eco_id, rc);
3022 echo_put_object (ecl->ecl_object);
3023 OBD_FREE (ecl, sizeof (*ecl));
3027 rc = class_disconnect(exp);
3033 static struct obd_ops echo_client_obd_ops = {
3034 .o_owner = THIS_MODULE,
3037 .o_setup = echo_client_setup,
3038 .o_cleanup = echo_client_cleanup,
3041 .o_iocontrol = echo_client_iocontrol,
3042 .o_connect = echo_client_connect,
3043 .o_disconnect = echo_client_disconnect
3046 int echo_client_init(void)
3048 struct lprocfs_static_vars lvars = { 0 };
3051 lprocfs_echo_init_vars(&lvars);
3053 rc = lu_kmem_init(echo_caches);
3055 rc = class_register_type(&echo_client_obd_ops, NULL,
3057 LUSTRE_ECHO_CLIENT_NAME,
3060 lu_kmem_fini(echo_caches);
3065 void echo_client_exit(void)
3067 class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
3068 lu_kmem_fini(echo_caches);
3072 static int __init obdecho_init(void)
3074 struct lprocfs_static_vars lvars;
3078 LCONSOLE_INFO("Echo OBD driver; http://www.lustre.org/\n");
3080 LASSERT(CFS_PAGE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
3082 lprocfs_echo_init_vars(&lvars);
3084 # ifdef HAVE_SERVER_SUPPORT
3085 rc = echo_persistent_pages_init();
3089 rc = class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
3090 LUSTRE_ECHO_NAME, NULL);
3095 rc = echo_client_init();
3097 # ifdef HAVE_SERVER_SUPPORT
3101 class_unregister_type(LUSTRE_ECHO_NAME);
3103 echo_persistent_pages_fini();
3109 static void /*__exit*/ obdecho_exit(void)
3113 # ifdef HAVE_SERVER_SUPPORT
3114 class_unregister_type(LUSTRE_ECHO_NAME);
3115 echo_persistent_pages_fini();
3119 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3120 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
3121 MODULE_LICENSE("GPL");
3123 cfs_module(obdecho, LUSTRE_VERSION_STRING, obdecho_init, obdecho_exit);
3124 #endif /* __KERNEL__ */
3126 /** @} echo_client */