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>
56 #include "echo_internal.h"
58 /** \defgroup echo_client Echo Client
63 struct cl_device ed_cl;
64 struct echo_client_obd *ed_ec;
66 struct cl_site ed_site_myself;
67 struct cl_site *ed_site;
68 struct lu_device *ed_next;
71 struct lu_client_seq *ed_cl_seq;
75 struct cl_object eo_cl;
76 struct cl_object_header eo_hdr;
78 struct echo_device *eo_dev;
79 cfs_list_t eo_obj_chain;
80 struct lov_stripe_md *eo_lsm;
81 cfs_atomic_t eo_npages;
85 struct echo_object_conf {
86 struct cl_object_conf eoc_cl;
87 struct lov_stripe_md **eoc_md;
91 struct cl_page_slice ep_cl;
93 cfs_page_t *ep_vmpage;
97 struct cl_lock_slice el_cl;
99 struct echo_object *el_object;
101 cfs_atomic_t el_refcount;
105 struct cl_io_slice ei_cl;
110 struct cl_req_slice er_cl;
114 static int echo_client_setup(const struct lu_env *env,
115 struct obd_device *obddev,
116 struct lustre_cfg *lcfg);
117 static int echo_client_cleanup(struct obd_device *obddev);
120 /** \defgroup echo_helpers Helper functions
123 static inline struct echo_device *cl2echo_dev(const struct cl_device *dev)
125 return container_of0(dev, struct echo_device, ed_cl);
128 static inline struct cl_device *echo_dev2cl(struct echo_device *d)
133 static inline struct echo_device *obd2echo_dev(const struct obd_device *obd)
135 return cl2echo_dev(lu2cl_dev(obd->obd_lu_dev));
138 static inline struct cl_object *echo_obj2cl(struct echo_object *eco)
143 static inline struct echo_object *cl2echo_obj(const struct cl_object *o)
145 return container_of(o, struct echo_object, eo_cl);
148 static inline struct echo_page *cl2echo_page(const struct cl_page_slice *s)
150 return container_of(s, struct echo_page, ep_cl);
153 static inline struct echo_lock *cl2echo_lock(const struct cl_lock_slice *s)
155 return container_of(s, struct echo_lock, el_cl);
158 static inline struct cl_lock *echo_lock2cl(const struct echo_lock *ecl)
160 return ecl->el_cl.cls_lock;
163 static struct lu_context_key echo_thread_key;
164 static inline struct echo_thread_info *echo_env_info(const struct lu_env *env)
166 struct echo_thread_info *info;
167 info = lu_context_key_get(&env->le_ctx, &echo_thread_key);
168 LASSERT(info != NULL);
173 struct echo_object_conf *cl2echo_conf(const struct cl_object_conf *c)
175 return container_of(c, struct echo_object_conf, eoc_cl);
178 /** @} echo_helpers */
180 static struct echo_object *cl_echo_object_find(struct echo_device *d,
181 struct lov_stripe_md **lsm);
182 static int cl_echo_object_put(struct echo_object *eco);
183 static int cl_echo_enqueue (struct echo_object *eco, obd_off start,
184 obd_off end, int mode, __u64 *cookie);
185 static int cl_echo_cancel (struct echo_device *d, __u64 cookie);
186 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
187 cfs_page_t **pages, int npages, int async);
189 static struct echo_thread_info *echo_env_info(const struct lu_env *env);
191 struct echo_thread_info {
192 struct echo_object_conf eti_conf;
193 struct lustre_md eti_md;
195 struct cl_2queue eti_queue;
197 struct cl_lock_descr eti_descr;
198 struct lu_fid eti_fid;
199 struct lu_fid eti_fid2;
200 struct md_op_spec eti_spec;
201 struct lov_mds_md_v3 eti_lmm;
202 struct lov_user_md_v3 eti_lum;
203 struct md_attr eti_ma;
204 struct lu_name eti_lname;
205 /* per-thread values, can be re-used */
209 struct lu_buf eti_buf;
210 char eti_xattr_buf[LUSTRE_POSIX_ACL_MAX_SIZE];
213 /* No session used right now */
214 struct echo_session_info {
218 static cfs_mem_cache_t *echo_lock_kmem;
219 static cfs_mem_cache_t *echo_object_kmem;
220 static cfs_mem_cache_t *echo_thread_kmem;
221 static cfs_mem_cache_t *echo_session_kmem;
222 //static cfs_mem_cache_t *echo_req_kmem;
224 static struct lu_kmem_descr echo_caches[] = {
226 .ckd_cache = &echo_lock_kmem,
227 .ckd_name = "echo_lock_kmem",
228 .ckd_size = sizeof (struct echo_lock)
231 .ckd_cache = &echo_object_kmem,
232 .ckd_name = "echo_object_kmem",
233 .ckd_size = sizeof (struct echo_object)
236 .ckd_cache = &echo_thread_kmem,
237 .ckd_name = "echo_thread_kmem",
238 .ckd_size = sizeof (struct echo_thread_info)
241 .ckd_cache = &echo_session_kmem,
242 .ckd_name = "echo_session_kmem",
243 .ckd_size = sizeof (struct echo_session_info)
247 .ckd_cache = &echo_req_kmem,
248 .ckd_name = "echo_req_kmem",
249 .ckd_size = sizeof (struct echo_req)
257 /** \defgroup echo_page Page operations
259 * Echo page operations.
263 static cfs_page_t *echo_page_vmpage(const struct lu_env *env,
264 const struct cl_page_slice *slice)
266 return cl2echo_page(slice)->ep_vmpage;
269 static int echo_page_own(const struct lu_env *env,
270 const struct cl_page_slice *slice,
271 struct cl_io *io, int nonblock)
273 struct echo_page *ep = cl2echo_page(slice);
276 mutex_lock(&ep->ep_lock);
277 else if (!mutex_trylock(&ep->ep_lock))
282 static void echo_page_disown(const struct lu_env *env,
283 const struct cl_page_slice *slice,
286 struct echo_page *ep = cl2echo_page(slice);
288 LASSERT(mutex_is_locked(&ep->ep_lock));
289 mutex_unlock(&ep->ep_lock);
292 static void echo_page_discard(const struct lu_env *env,
293 const struct cl_page_slice *slice,
294 struct cl_io *unused)
296 cl_page_delete(env, slice->cpl_page);
299 static int echo_page_is_vmlocked(const struct lu_env *env,
300 const struct cl_page_slice *slice)
302 if (mutex_is_locked(&cl2echo_page(slice)->ep_lock))
307 static void echo_page_completion(const struct lu_env *env,
308 const struct cl_page_slice *slice,
311 LASSERT(slice->cpl_page->cp_sync_io != NULL);
314 static void echo_page_fini(const struct lu_env *env,
315 struct cl_page_slice *slice)
317 struct echo_page *ep = cl2echo_page(slice);
318 struct echo_object *eco = cl2echo_obj(slice->cpl_obj);
319 cfs_page_t *vmpage = ep->ep_vmpage;
322 cfs_atomic_dec(&eco->eo_npages);
323 page_cache_release(vmpage);
327 static int echo_page_prep(const struct lu_env *env,
328 const struct cl_page_slice *slice,
329 struct cl_io *unused)
334 static int echo_page_print(const struct lu_env *env,
335 const struct cl_page_slice *slice,
336 void *cookie, lu_printer_t printer)
338 struct echo_page *ep = cl2echo_page(slice);
340 (*printer)(env, cookie, LUSTRE_ECHO_CLIENT_NAME"-page@%p %d vm@%p\n",
341 ep, mutex_is_locked(&ep->ep_lock), ep->ep_vmpage);
345 static const struct cl_page_operations echo_page_ops = {
346 .cpo_own = echo_page_own,
347 .cpo_disown = echo_page_disown,
348 .cpo_discard = echo_page_discard,
349 .cpo_vmpage = echo_page_vmpage,
350 .cpo_fini = echo_page_fini,
351 .cpo_print = echo_page_print,
352 .cpo_is_vmlocked = echo_page_is_vmlocked,
355 .cpo_prep = echo_page_prep,
356 .cpo_completion = echo_page_completion,
359 .cpo_prep = echo_page_prep,
360 .cpo_completion = echo_page_completion,
366 /** \defgroup echo_lock Locking
368 * echo lock operations
372 static void echo_lock_fini(const struct lu_env *env,
373 struct cl_lock_slice *slice)
375 struct echo_lock *ecl = cl2echo_lock(slice);
377 LASSERT(cfs_list_empty(&ecl->el_chain));
378 OBD_SLAB_FREE_PTR(ecl, echo_lock_kmem);
381 static void echo_lock_delete(const struct lu_env *env,
382 const struct cl_lock_slice *slice)
384 struct echo_lock *ecl = cl2echo_lock(slice);
386 LASSERT(cfs_list_empty(&ecl->el_chain));
389 static int echo_lock_fits_into(const struct lu_env *env,
390 const struct cl_lock_slice *slice,
391 const struct cl_lock_descr *need,
392 const struct cl_io *unused)
397 static struct cl_lock_operations echo_lock_ops = {
398 .clo_fini = echo_lock_fini,
399 .clo_delete = echo_lock_delete,
400 .clo_fits_into = echo_lock_fits_into
405 /** \defgroup echo_cl_ops cl_object operations
407 * operations for cl_object
411 static int echo_page_init(const struct lu_env *env, struct cl_object *obj,
412 struct cl_page *page, cfs_page_t *vmpage)
414 struct echo_page *ep = cl_object_page_slice(obj, page);
415 struct echo_object *eco = cl2echo_obj(obj);
418 ep->ep_vmpage = vmpage;
419 page_cache_get(vmpage);
420 mutex_init(&ep->ep_lock);
421 cl_page_slice_add(page, &ep->ep_cl, obj, &echo_page_ops);
422 cfs_atomic_inc(&eco->eo_npages);
426 static int echo_io_init(const struct lu_env *env, struct cl_object *obj,
432 static int echo_lock_init(const struct lu_env *env,
433 struct cl_object *obj, struct cl_lock *lock,
434 const struct cl_io *unused)
436 struct echo_lock *el;
439 OBD_SLAB_ALLOC_PTR_GFP(el, echo_lock_kmem, CFS_ALLOC_IO);
441 cl_lock_slice_add(lock, &el->el_cl, obj, &echo_lock_ops);
442 el->el_object = cl2echo_obj(obj);
443 CFS_INIT_LIST_HEAD(&el->el_chain);
444 cfs_atomic_set(&el->el_refcount, 0);
446 RETURN(el == NULL ? -ENOMEM : 0);
449 static int echo_conf_set(const struct lu_env *env, struct cl_object *obj,
450 const struct cl_object_conf *conf)
455 static const struct cl_object_operations echo_cl_obj_ops = {
456 .coo_page_init = echo_page_init,
457 .coo_lock_init = echo_lock_init,
458 .coo_io_init = echo_io_init,
459 .coo_conf_set = echo_conf_set
461 /** @} echo_cl_ops */
463 /** \defgroup echo_lu_ops lu_object operations
465 * operations for echo lu object.
469 static int echo_object_init(const struct lu_env *env, struct lu_object *obj,
470 const struct lu_object_conf *conf)
472 struct echo_device *ed = cl2echo_dev(lu2cl_dev(obj->lo_dev));
473 struct echo_client_obd *ec = ed->ed_ec;
474 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
478 struct lu_object *below;
479 struct lu_device *under;
482 below = under->ld_ops->ldo_object_alloc(env, obj->lo_header,
486 lu_object_add(obj, below);
489 if (!ed->ed_next_ismd) {
490 const struct cl_object_conf *cconf = lu2cl_conf(conf);
491 struct echo_object_conf *econf = cl2echo_conf(cconf);
493 LASSERT(econf->eoc_md);
494 eco->eo_lsm = *econf->eoc_md;
495 /* clear the lsm pointer so that it won't get freed. */
496 *econf->eoc_md = NULL;
502 cfs_atomic_set(&eco->eo_npages, 0);
503 cl_object_page_init(lu2cl(obj), sizeof(struct echo_page));
505 spin_lock(&ec->ec_lock);
506 cfs_list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
507 spin_unlock(&ec->ec_lock);
512 /* taken from osc_unpackmd() */
513 static int echo_alloc_memmd(struct echo_device *ed,
514 struct lov_stripe_md **lsmp)
520 /* If export is lov/osc then use their obd method */
521 if (ed->ed_next != NULL)
522 return obd_alloc_memmd(ed->ed_ec->ec_exp, lsmp);
523 /* OFD has no unpackmd method, do everything here */
524 lsm_size = lov_stripe_md_size(1);
526 LASSERT(*lsmp == NULL);
527 OBD_ALLOC(*lsmp, lsm_size);
531 OBD_ALLOC((*lsmp)->lsm_oinfo[0], sizeof(struct lov_oinfo));
532 if ((*lsmp)->lsm_oinfo[0] == NULL) {
533 OBD_FREE(*lsmp, lsm_size);
537 loi_init((*lsmp)->lsm_oinfo[0]);
538 (*lsmp)->lsm_maxbytes = LUSTRE_STRIPE_MAXBYTES;
539 ostid_set_seq_echo(&(*lsmp)->lsm_oi);
544 static int echo_free_memmd(struct echo_device *ed, struct lov_stripe_md **lsmp)
550 /* If export is lov/osc then use their obd method */
551 if (ed->ed_next != NULL)
552 return obd_free_memmd(ed->ed_ec->ec_exp, lsmp);
553 /* OFD has no unpackmd method, do everything here */
554 lsm_size = lov_stripe_md_size(1);
556 LASSERT(*lsmp != NULL);
557 OBD_FREE((*lsmp)->lsm_oinfo[0], sizeof(struct lov_oinfo));
558 OBD_FREE(*lsmp, lsm_size);
563 static void echo_object_free(const struct lu_env *env, struct lu_object *obj)
565 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
566 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
569 LASSERT(cfs_atomic_read(&eco->eo_npages) == 0);
571 spin_lock(&ec->ec_lock);
572 cfs_list_del_init(&eco->eo_obj_chain);
573 spin_unlock(&ec->ec_lock);
576 lu_object_header_fini(obj->lo_header);
579 echo_free_memmd(eco->eo_dev, &eco->eo_lsm);
580 OBD_SLAB_FREE_PTR(eco, echo_object_kmem);
584 static int echo_object_print(const struct lu_env *env, void *cookie,
585 lu_printer_t p, const struct lu_object *o)
587 struct echo_object *obj = cl2echo_obj(lu2cl(o));
589 return (*p)(env, cookie, "echoclient-object@%p", obj);
592 static const struct lu_object_operations echo_lu_obj_ops = {
593 .loo_object_init = echo_object_init,
594 .loo_object_delete = NULL,
595 .loo_object_release = NULL,
596 .loo_object_free = echo_object_free,
597 .loo_object_print = echo_object_print,
598 .loo_object_invariant = NULL
600 /** @} echo_lu_ops */
602 /** \defgroup echo_lu_dev_ops lu_device operations
604 * Operations for echo lu device.
608 static struct lu_object *echo_object_alloc(const struct lu_env *env,
609 const struct lu_object_header *hdr,
610 struct lu_device *dev)
612 struct echo_object *eco;
613 struct lu_object *obj = NULL;
616 /* we're the top dev. */
617 LASSERT(hdr == NULL);
618 OBD_SLAB_ALLOC_PTR_GFP(eco, echo_object_kmem, CFS_ALLOC_IO);
620 struct cl_object_header *hdr = &eco->eo_hdr;
622 obj = &echo_obj2cl(eco)->co_lu;
623 cl_object_header_init(hdr);
624 lu_object_init(obj, &hdr->coh_lu, dev);
625 lu_object_add_top(&hdr->coh_lu, obj);
627 eco->eo_cl.co_ops = &echo_cl_obj_ops;
628 obj->lo_ops = &echo_lu_obj_ops;
633 static struct lu_device_operations echo_device_lu_ops = {
634 .ldo_object_alloc = echo_object_alloc,
637 /** @} echo_lu_dev_ops */
639 static struct cl_device_operations echo_device_cl_ops = {
642 /** \defgroup echo_init Setup and teardown
644 * Init and fini functions for echo client.
648 static int echo_site_init(const struct lu_env *env, struct echo_device *ed)
650 struct cl_site *site = &ed->ed_site_myself;
653 /* initialize site */
654 rc = cl_site_init(site, &ed->ed_cl);
656 CERROR("Cannot initilize site for echo client(%d)\n", rc);
660 rc = lu_site_init_finish(&site->cs_lu);
668 static void echo_site_fini(const struct lu_env *env, struct echo_device *ed)
671 if (!ed->ed_next_ismd)
672 cl_site_fini(ed->ed_site);
677 static void *echo_thread_key_init(const struct lu_context *ctx,
678 struct lu_context_key *key)
680 struct echo_thread_info *info;
682 OBD_SLAB_ALLOC_PTR_GFP(info, echo_thread_kmem, CFS_ALLOC_IO);
684 info = ERR_PTR(-ENOMEM);
688 static void echo_thread_key_fini(const struct lu_context *ctx,
689 struct lu_context_key *key, void *data)
691 struct echo_thread_info *info = data;
692 OBD_SLAB_FREE_PTR(info, echo_thread_kmem);
695 static void echo_thread_key_exit(const struct lu_context *ctx,
696 struct lu_context_key *key, void *data)
700 static struct lu_context_key echo_thread_key = {
701 .lct_tags = LCT_CL_THREAD,
702 .lct_init = echo_thread_key_init,
703 .lct_fini = echo_thread_key_fini,
704 .lct_exit = echo_thread_key_exit
707 static void *echo_session_key_init(const struct lu_context *ctx,
708 struct lu_context_key *key)
710 struct echo_session_info *session;
712 OBD_SLAB_ALLOC_PTR_GFP(session, echo_session_kmem, CFS_ALLOC_IO);
714 session = ERR_PTR(-ENOMEM);
718 static void echo_session_key_fini(const struct lu_context *ctx,
719 struct lu_context_key *key, void *data)
721 struct echo_session_info *session = data;
722 OBD_SLAB_FREE_PTR(session, echo_session_kmem);
725 static void echo_session_key_exit(const struct lu_context *ctx,
726 struct lu_context_key *key, void *data)
730 static struct lu_context_key echo_session_key = {
731 .lct_tags = LCT_SESSION,
732 .lct_init = echo_session_key_init,
733 .lct_fini = echo_session_key_fini,
734 .lct_exit = echo_session_key_exit
737 LU_TYPE_INIT_FINI(echo, &echo_thread_key, &echo_session_key);
739 #define ECHO_SEQ_WIDTH 0xffffffff
740 static int echo_fid_init(struct echo_device *ed, char *obd_name,
741 struct seq_server_site *ss)
747 OBD_ALLOC_PTR(ed->ed_cl_seq);
748 if (ed->ed_cl_seq == NULL)
751 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
753 GOTO(out_free_seq, rc = -ENOMEM);
755 snprintf(prefix, MAX_OBD_NAME + 5, "srv-%s", obd_name);
757 /* Init client side sequence-manager */
758 rc = seq_client_init(ed->ed_cl_seq, NULL,
760 prefix, ss->ss_server_seq);
761 ed->ed_cl_seq->lcs_width = ECHO_SEQ_WIDTH;
762 OBD_FREE(prefix, MAX_OBD_NAME + 5);
764 GOTO(out_free_seq, rc);
769 OBD_FREE_PTR(ed->ed_cl_seq);
770 ed->ed_cl_seq = NULL;
774 static int echo_fid_fini(struct obd_device *obddev)
776 struct echo_device *ed = obd2echo_dev(obddev);
779 if (ed->ed_cl_seq != NULL) {
780 seq_client_fini(ed->ed_cl_seq);
781 OBD_FREE_PTR(ed->ed_cl_seq);
782 ed->ed_cl_seq = NULL;
788 static struct lu_device *echo_device_alloc(const struct lu_env *env,
789 struct lu_device_type *t,
790 struct lustre_cfg *cfg)
792 struct lu_device *next;
793 struct echo_device *ed;
794 struct cl_device *cd;
795 struct obd_device *obd = NULL; /* to keep compiler happy */
796 struct obd_device *tgt;
797 const char *tgt_type_name;
804 GOTO(out, rc = -ENOMEM);
808 rc = cl_device_init(cd, t);
812 cd->cd_lu_dev.ld_ops = &echo_device_lu_ops;
813 cd->cd_ops = &echo_device_cl_ops;
816 obd = class_name2obd(lustre_cfg_string(cfg, 0));
817 LASSERT(obd != NULL);
818 LASSERT(env != NULL);
820 tgt = class_name2obd(lustre_cfg_string(cfg, 1));
822 CERROR("Can not find tgt device %s\n",
823 lustre_cfg_string(cfg, 1));
824 GOTO(out, rc = -ENODEV);
827 next = tgt->obd_lu_dev;
828 if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
829 ed->ed_next_ismd = 1;
831 ed->ed_next_ismd = 0;
832 rc = echo_site_init(env, ed);
838 rc = echo_client_setup(env, obd, cfg);
842 ed->ed_ec = &obd->u.echo_client;
845 if (ed->ed_next_ismd) {
846 /* Suppose to connect to some Metadata layer */
848 struct lu_device *ld;
852 CERROR("%s is not lu device type!\n",
853 lustre_cfg_string(cfg, 1));
854 GOTO(out, rc = -EINVAL);
857 tgt_type_name = lustre_cfg_string(cfg, 2);
858 if (!tgt_type_name) {
859 CERROR("%s no type name for echo %s setup\n",
860 lustre_cfg_string(cfg, 1),
861 tgt->obd_type->typ_name);
862 GOTO(out, rc = -EINVAL);
867 spin_lock(&ls->ls_ld_lock);
868 cfs_list_for_each_entry(ld, &ls->ls_ld_linkage, ld_linkage) {
869 if (strcmp(ld->ld_type->ldt_name, tgt_type_name) == 0) {
874 spin_unlock(&ls->ls_ld_lock);
877 CERROR("%s is not lu device type!\n",
878 lustre_cfg_string(cfg, 1));
879 GOTO(out, rc = -EINVAL);
883 /* For MD echo client, it will use the site in MDS stack */
884 ed->ed_site_myself.cs_lu = *ls;
885 ed->ed_site = &ed->ed_site_myself;
886 ed->ed_cl.cd_lu_dev.ld_site = &ed->ed_site_myself.cs_lu;
887 rc = echo_fid_init(ed, obd->obd_name, lu_site2seq(ls));
889 CERROR("echo fid init error %d\n", rc);
893 /* if echo client is to be stacked upon ost device, the next is
894 * NULL since ost is not a clio device so far */
895 if (next != NULL && !lu_device_is_cl(next))
898 tgt_type_name = tgt->obd_type->typ_name;
900 LASSERT(next != NULL);
901 if (next->ld_site != NULL)
902 GOTO(out, rc = -EBUSY);
904 next->ld_site = &ed->ed_site->cs_lu;
905 rc = next->ld_type->ldt_ops->ldto_device_init(env, next,
906 next->ld_type->ldt_name,
911 /* Tricky case, I have to determine the obd type since
912 * CLIO uses the different parameters to initialize
913 * objects for lov & osc. */
914 if (strcmp(tgt_type_name, LUSTRE_LOV_NAME) == 0)
915 ed->ed_next_islov = 1;
917 LASSERT(strcmp(tgt_type_name,
918 LUSTRE_OSC_NAME) == 0);
920 LASSERT(strcmp(tgt_type_name, LUSTRE_OST_NAME) == 0);
924 RETURN(&cd->cd_lu_dev);
929 rc2 = echo_client_cleanup(obd);
931 CERROR("Cleanup obd device %s error(%d)\n",
936 echo_site_fini(env, ed);
938 cl_device_fini(&ed->ed_cl);
948 static int echo_device_init(const struct lu_env *env, struct lu_device *d,
949 const char *name, struct lu_device *next)
955 static struct lu_device *echo_device_fini(const struct lu_env *env,
958 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
959 struct lu_device *next = ed->ed_next;
961 while (next && !ed->ed_next_ismd)
962 next = next->ld_type->ldt_ops->ldto_device_fini(env, next);
966 static void echo_lock_release(const struct lu_env *env,
967 struct echo_lock *ecl,
970 struct cl_lock *clk = echo_lock2cl(ecl);
974 cl_lock_release(env, clk, "ec enqueue", ecl->el_object);
976 cl_lock_mutex_get(env, clk);
977 cl_lock_cancel(env, clk);
978 cl_lock_delete(env, clk);
979 cl_lock_mutex_put(env, clk);
981 cl_lock_put(env, clk);
984 static struct lu_device *echo_device_free(const struct lu_env *env,
987 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
988 struct echo_client_obd *ec = ed->ed_ec;
989 struct echo_object *eco;
990 struct lu_device *next = ed->ed_next;
992 CDEBUG(D_INFO, "echo device:%p is going to be freed, next = %p\n",
995 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
997 /* check if there are objects still alive.
998 * It shouldn't have any object because lu_site_purge would cleanup
999 * all of cached objects. Anyway, probably the echo device is being
1000 * parallelly accessed.
1002 spin_lock(&ec->ec_lock);
1003 cfs_list_for_each_entry(eco, &ec->ec_objects, eo_obj_chain)
1004 eco->eo_deleted = 1;
1005 spin_unlock(&ec->ec_lock);
1008 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
1011 "Waiting for the reference of echo object to be dropped\n");
1013 /* Wait for the last reference to be dropped. */
1014 spin_lock(&ec->ec_lock);
1015 while (!cfs_list_empty(&ec->ec_objects)) {
1016 spin_unlock(&ec->ec_lock);
1017 CERROR("echo_client still has objects at cleanup time, "
1018 "wait for 1 second\n");
1019 cfs_schedule_timeout_and_set_state(CFS_TASK_UNINT,
1020 cfs_time_seconds(1));
1021 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
1022 spin_lock(&ec->ec_lock);
1024 spin_unlock(&ec->ec_lock);
1026 LASSERT(cfs_list_empty(&ec->ec_locks));
1028 CDEBUG(D_INFO, "No object exists, exiting...\n");
1030 echo_client_cleanup(d->ld_obd);
1031 echo_fid_fini(d->ld_obd);
1032 while (next && !ed->ed_next_ismd)
1033 next = next->ld_type->ldt_ops->ldto_device_free(env, next);
1035 LASSERT(ed->ed_site == lu2cl_site(d->ld_site));
1036 echo_site_fini(env, ed);
1037 cl_device_fini(&ed->ed_cl);
1043 static const struct lu_device_type_operations echo_device_type_ops = {
1044 .ldto_init = echo_type_init,
1045 .ldto_fini = echo_type_fini,
1047 .ldto_start = echo_type_start,
1048 .ldto_stop = echo_type_stop,
1050 .ldto_device_alloc = echo_device_alloc,
1051 .ldto_device_free = echo_device_free,
1052 .ldto_device_init = echo_device_init,
1053 .ldto_device_fini = echo_device_fini
1056 static struct lu_device_type echo_device_type = {
1057 .ldt_tags = LU_DEVICE_CL,
1058 .ldt_name = LUSTRE_ECHO_CLIENT_NAME,
1059 .ldt_ops = &echo_device_type_ops,
1060 .ldt_ctx_tags = LCT_CL_THREAD | LCT_MD_THREAD | LCT_DT_THREAD,
1064 /** \defgroup echo_exports Exported operations
1066 * exporting functions to echo client
1071 /* Interfaces to echo client obd device */
1072 static struct echo_object *cl_echo_object_find(struct echo_device *d,
1073 struct lov_stripe_md **lsmp)
1076 struct echo_thread_info *info;
1077 struct echo_object_conf *conf;
1078 struct lov_stripe_md *lsm;
1079 struct echo_object *eco;
1080 struct cl_object *obj;
1089 LASSERTF(ostid_id(&lsm->lsm_oi) != 0, DOSTID"\n", POSTID(&lsm->lsm_oi));
1090 LASSERTF(ostid_seq(&lsm->lsm_oi) == FID_SEQ_ECHO, DOSTID"\n",
1091 POSTID(&lsm->lsm_oi));
1093 /* Never return an object if the obd is to be freed. */
1094 if (echo_dev2cl(d)->cd_lu_dev.ld_obd->obd_stopping)
1095 RETURN(ERR_PTR(-ENODEV));
1097 env = cl_env_get(&refcheck);
1099 RETURN((void *)env);
1101 info = echo_env_info(env);
1102 conf = &info->eti_conf;
1104 if (!d->ed_next_islov) {
1105 struct lov_oinfo *oinfo = lsm->lsm_oinfo[0];
1106 LASSERT(oinfo != NULL);
1107 oinfo->loi_oi = lsm->lsm_oi;
1108 conf->eoc_cl.u.coc_oinfo = oinfo;
1110 struct lustre_md *md;
1112 memset(md, 0, sizeof *md);
1114 conf->eoc_cl.u.coc_md = md;
1117 conf->eoc_md = lsmp;
1119 fid = &info->eti_fid;
1120 rc = ostid_to_fid(fid, &lsm->lsm_oi, 0);
1122 GOTO(out, eco = ERR_PTR(rc));
1124 /* In the function below, .hs_keycmp resolves to
1125 * lu_obj_hop_keycmp() */
1126 /* coverity[overrun-buffer-val] */
1127 obj = cl_object_find(env, echo_dev2cl(d), fid, &conf->eoc_cl);
1129 GOTO(out, eco = (void*)obj);
1131 eco = cl2echo_obj(obj);
1132 if (eco->eo_deleted) {
1133 cl_object_put(env, obj);
1134 eco = ERR_PTR(-EAGAIN);
1138 cl_env_put(env, &refcheck);
1142 static int cl_echo_object_put(struct echo_object *eco)
1145 struct cl_object *obj = echo_obj2cl(eco);
1149 env = cl_env_get(&refcheck);
1151 RETURN(PTR_ERR(env));
1153 /* an external function to kill an object? */
1154 if (eco->eo_deleted) {
1155 struct lu_object_header *loh = obj->co_lu.lo_header;
1156 LASSERT(&eco->eo_hdr == luh2coh(loh));
1157 set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
1160 cl_object_put(env, obj);
1161 cl_env_put(env, &refcheck);
1165 static int cl_echo_enqueue0(struct lu_env *env, struct echo_object *eco,
1166 obd_off start, obd_off end, int mode,
1167 __u64 *cookie , __u32 enqflags)
1170 struct cl_lock *lck;
1171 struct cl_object *obj;
1172 struct cl_lock_descr *descr;
1173 struct echo_thread_info *info;
1177 info = echo_env_info(env);
1179 descr = &info->eti_descr;
1180 obj = echo_obj2cl(eco);
1182 descr->cld_obj = obj;
1183 descr->cld_start = cl_index(obj, start);
1184 descr->cld_end = cl_index(obj, end);
1185 descr->cld_mode = mode == LCK_PW ? CLM_WRITE : CLM_READ;
1186 descr->cld_enq_flags = enqflags;
1189 lck = cl_lock_request(env, io, descr, "ec enqueue", eco);
1191 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
1192 struct echo_lock *el;
1194 rc = cl_wait(env, lck);
1196 el = cl2echo_lock(cl_lock_at(lck, &echo_device_type));
1197 spin_lock(&ec->ec_lock);
1198 if (cfs_list_empty(&el->el_chain)) {
1199 cfs_list_add(&el->el_chain, &ec->ec_locks);
1200 el->el_cookie = ++ec->ec_unique;
1202 cfs_atomic_inc(&el->el_refcount);
1203 *cookie = el->el_cookie;
1204 spin_unlock(&ec->ec_lock);
1206 cl_lock_release(env, lck, "ec enqueue", cfs_current());
1212 static int cl_echo_enqueue(struct echo_object *eco, obd_off start, obd_off end,
1213 int mode, __u64 *cookie)
1215 struct echo_thread_info *info;
1222 env = cl_env_get(&refcheck);
1224 RETURN(PTR_ERR(env));
1226 info = echo_env_info(env);
1229 io->ci_ignore_layout = 1;
1230 result = cl_io_init(env, io, CIT_MISC, echo_obj2cl(eco));
1233 LASSERT(result == 0);
1235 result = cl_echo_enqueue0(env, eco, start, end, mode, cookie, 0);
1236 cl_io_fini(env, io);
1240 cl_env_put(env, &refcheck);
1244 static int cl_echo_cancel0(struct lu_env *env, struct echo_device *ed,
1247 struct echo_client_obd *ec = ed->ed_ec;
1248 struct echo_lock *ecl = NULL;
1250 int found = 0, still_used = 0;
1253 LASSERT(ec != NULL);
1254 spin_lock(&ec->ec_lock);
1255 cfs_list_for_each (el, &ec->ec_locks) {
1256 ecl = cfs_list_entry (el, struct echo_lock, el_chain);
1257 CDEBUG(D_INFO, "ecl: %p, cookie: "LPX64"\n", ecl, ecl->el_cookie);
1258 found = (ecl->el_cookie == cookie);
1260 if (cfs_atomic_dec_and_test(&ecl->el_refcount))
1261 cfs_list_del_init(&ecl->el_chain);
1267 spin_unlock(&ec->ec_lock);
1272 echo_lock_release(env, ecl, still_used);
1276 static int cl_echo_cancel(struct echo_device *ed, __u64 cookie)
1283 env = cl_env_get(&refcheck);
1285 RETURN(PTR_ERR(env));
1287 rc = cl_echo_cancel0(env, ed, cookie);
1289 cl_env_put(env, &refcheck);
1293 static int cl_echo_async_brw(const struct lu_env *env, struct cl_io *io,
1294 enum cl_req_type unused, struct cl_2queue *queue)
1296 struct cl_page *clp;
1297 struct cl_page *temp;
1301 cl_page_list_for_each_safe(clp, temp, &queue->c2_qin) {
1303 rc = cl_page_cache_add(env, io, clp, CRT_WRITE);
1306 result = result ?: rc;
1311 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
1312 cfs_page_t **pages, int npages, int async)
1315 struct echo_thread_info *info;
1316 struct cl_object *obj = echo_obj2cl(eco);
1317 struct echo_device *ed = eco->eo_dev;
1318 struct cl_2queue *queue;
1320 struct cl_page *clp;
1321 struct lustre_handle lh = { 0 };
1322 int page_size = cl_page_size(obj);
1328 LASSERT((offset & ~CFS_PAGE_MASK) == 0);
1329 LASSERT(ed->ed_next != NULL);
1330 env = cl_env_get(&refcheck);
1332 RETURN(PTR_ERR(env));
1334 info = echo_env_info(env);
1336 queue = &info->eti_queue;
1338 cl_2queue_init(queue);
1340 io->ci_ignore_layout = 1;
1341 rc = cl_io_init(env, io, CIT_MISC, obj);
1347 rc = cl_echo_enqueue0(env, eco, offset,
1348 offset + npages * CFS_PAGE_SIZE - 1,
1349 rw == READ ? LCK_PR : LCK_PW, &lh.cookie,
1352 GOTO(error_lock, rc);
1354 for (i = 0; i < npages; i++) {
1356 clp = cl_page_find(env, obj, cl_index(obj, offset),
1357 pages[i], CPT_TRANSIENT);
1362 LASSERT(clp->cp_type == CPT_TRANSIENT);
1364 rc = cl_page_own(env, io, clp);
1366 LASSERT(clp->cp_state == CPS_FREEING);
1367 cl_page_put(env, clp);
1371 cl_2queue_add(queue, clp);
1373 /* drop the reference count for cl_page_find, so that the page
1374 * will be freed in cl_2queue_fini. */
1375 cl_page_put(env, clp);
1376 cl_page_clip(env, clp, 0, page_size);
1378 offset += page_size;
1382 enum cl_req_type typ = rw == READ ? CRT_READ : CRT_WRITE;
1384 async = async && (typ == CRT_WRITE);
1386 rc = cl_echo_async_brw(env, io, typ, queue);
1388 rc = cl_io_submit_sync(env, io, typ, queue, 0);
1389 CDEBUG(D_INFO, "echo_client %s write returns %d\n",
1390 async ? "async" : "sync", rc);
1393 cl_echo_cancel0(env, ed, lh.cookie);
1396 cl_2queue_discard(env, io, queue);
1397 cl_2queue_disown(env, io, queue);
1398 cl_2queue_fini(env, queue);
1399 cl_io_fini(env, io);
1401 cl_env_put(env, &refcheck);
1404 /** @} echo_exports */
1407 static obd_id last_object_id;
1410 echo_copyout_lsm (struct lov_stripe_md *lsm, void *_ulsm, int ulsm_nob)
1412 struct lov_stripe_md *ulsm = _ulsm;
1415 nob = offsetof (struct lov_stripe_md, lsm_oinfo[lsm->lsm_stripe_count]);
1419 if (cfs_copy_to_user (ulsm, lsm, sizeof(ulsm)))
1422 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1423 if (cfs_copy_to_user (ulsm->lsm_oinfo[i], lsm->lsm_oinfo[i],
1424 sizeof(lsm->lsm_oinfo[0])))
1431 echo_copyin_lsm (struct echo_device *ed, struct lov_stripe_md *lsm,
1432 void *ulsm, int ulsm_nob)
1434 struct echo_client_obd *ec = ed->ed_ec;
1437 if (ulsm_nob < sizeof (*lsm))
1440 if (cfs_copy_from_user (lsm, ulsm, sizeof (*lsm)))
1443 if (lsm->lsm_stripe_count > ec->ec_nstripes ||
1444 lsm->lsm_magic != LOV_MAGIC ||
1445 (lsm->lsm_stripe_size & (~CFS_PAGE_MASK)) != 0 ||
1446 ((__u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count > ~0UL))
1450 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1451 if (cfs_copy_from_user(lsm->lsm_oinfo[i],
1452 ((struct lov_stripe_md *)ulsm)-> \
1454 sizeof(lsm->lsm_oinfo[0])))
1460 static inline void echo_md_build_name(struct lu_name *lname, char *name,
1463 sprintf(name, LPU64, id);
1464 lname->ln_name = name;
1465 lname->ln_namelen = strlen(name);
1468 /* similar to mdt_attr_get_complex */
1469 static int echo_big_lmm_get(const struct lu_env *env, struct md_object *o,
1472 struct echo_thread_info *info = echo_env_info(env);
1477 LASSERT(ma->ma_lmm_size > 0);
1479 rc = mo_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LOV);
1483 /* big_lmm may need to be grown */
1484 if (info->eti_big_lmmsize < rc) {
1485 int size = size_roundup_power2(rc);
1487 if (info->eti_big_lmmsize > 0) {
1488 /* free old buffer */
1489 LASSERT(info->eti_big_lmm);
1490 OBD_FREE_LARGE(info->eti_big_lmm,
1491 info->eti_big_lmmsize);
1492 info->eti_big_lmm = NULL;
1493 info->eti_big_lmmsize = 0;
1496 OBD_ALLOC_LARGE(info->eti_big_lmm, size);
1497 if (info->eti_big_lmm == NULL)
1499 info->eti_big_lmmsize = size;
1501 LASSERT(info->eti_big_lmmsize >= rc);
1503 info->eti_buf.lb_buf = info->eti_big_lmm;
1504 info->eti_buf.lb_len = info->eti_big_lmmsize;
1505 rc = mo_xattr_get(env, o, &info->eti_buf, XATTR_NAME_LOV);
1509 ma->ma_valid |= MA_LOV;
1510 ma->ma_lmm = info->eti_big_lmm;
1511 ma->ma_lmm_size = rc;
1516 int echo_attr_get_complex(const struct lu_env *env, struct md_object *next,
1519 struct echo_thread_info *info = echo_env_info(env);
1520 struct lu_buf *buf = &info->eti_buf;
1521 cfs_umode_t mode = lu_object_attr(&next->mo_lu);
1522 int need = ma->ma_need;
1529 if (need & MA_INODE) {
1530 ma->ma_need = MA_INODE;
1531 rc = mo_attr_get(env, next, ma);
1534 ma->ma_valid |= MA_INODE;
1537 if (need & MA_LOV) {
1538 if (S_ISREG(mode) || S_ISDIR(mode)) {
1539 LASSERT(ma->ma_lmm_size > 0);
1540 buf->lb_buf = ma->ma_lmm;
1541 buf->lb_len = ma->ma_lmm_size;
1542 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LOV);
1544 ma->ma_lmm_size = rc2;
1545 ma->ma_valid |= MA_LOV;
1546 } else if (rc2 == -ENODATA) {
1548 ma->ma_lmm_size = 0;
1549 } else if (rc2 == -ERANGE) {
1550 rc2 = echo_big_lmm_get(env, next, ma);
1552 GOTO(out, rc = rc2);
1554 GOTO(out, rc = rc2);
1559 #ifdef CONFIG_FS_POSIX_ACL
1560 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
1561 buf->lb_buf = ma->ma_acl;
1562 buf->lb_len = ma->ma_acl_size;
1563 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1565 ma->ma_acl_size = rc2;
1566 ma->ma_valid |= MA_ACL_DEF;
1567 } else if (rc2 == -ENODATA) {
1569 ma->ma_acl_size = 0;
1571 GOTO(out, rc = rc2);
1577 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
1578 rc, ma->ma_valid, ma->ma_lmm);
1583 echo_md_create_internal(const struct lu_env *env, struct echo_device *ed,
1584 struct md_object *parent, struct lu_fid *fid,
1585 struct lu_name *lname, struct md_op_spec *spec,
1588 struct lu_object *ec_child, *child;
1589 struct lu_device *ld = ed->ed_next;
1590 struct echo_thread_info *info = echo_env_info(env);
1591 struct lu_fid *fid2 = &info->eti_fid2;
1592 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
1597 rc = mdo_lookup(env, parent, lname, fid2, spec);
1600 else if (rc != -ENOENT)
1603 ec_child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev,
1605 if (IS_ERR(ec_child)) {
1606 CERROR("Can not find the child "DFID": rc = %ld\n", PFID(fid),
1608 RETURN(PTR_ERR(ec_child));
1611 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1612 if (child == NULL) {
1613 CERROR("Can not locate the child "DFID"\n", PFID(fid));
1614 GOTO(out_put, rc = -EINVAL);
1617 CDEBUG(D_RPCTRACE, "Start creating object "DFID" %s %p\n",
1618 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1621 * Do not perform lookup sanity check. We know that name does not exist.
1623 spec->sp_cr_lookup = 0;
1624 rc = mdo_create(env, parent, lname, lu2md(child), spec, ma);
1626 CERROR("Can not create child "DFID": rc = %d\n", PFID(fid), rc);
1629 CDEBUG(D_RPCTRACE, "End creating object "DFID" %s %p rc = %d\n",
1630 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent, rc);
1633 lu_object_put(env, ec_child);
1637 static int echo_set_lmm_size(const struct lu_env *env, struct lu_device *ld,
1640 struct echo_thread_info *info = echo_env_info(env);
1642 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
1643 ma->ma_lmm = (void *)&info->eti_lmm;
1644 ma->ma_lmm_size = sizeof(info->eti_lmm);
1646 LASSERT(info->eti_big_lmmsize);
1647 ma->ma_lmm = info->eti_big_lmm;
1648 ma->ma_lmm_size = info->eti_big_lmmsize;
1654 static int echo_create_md_object(const struct lu_env *env,
1655 struct echo_device *ed,
1656 struct lu_object *ec_parent,
1658 char *name, int namelen,
1659 __u64 id, __u32 mode, int count,
1660 int stripe_count, int stripe_offset)
1662 struct lu_object *parent;
1663 struct echo_thread_info *info = echo_env_info(env);
1664 struct lu_name *lname = &info->eti_lname;
1665 struct md_op_spec *spec = &info->eti_spec;
1666 struct md_attr *ma = &info->eti_ma;
1667 struct lu_device *ld = ed->ed_next;
1673 if (ec_parent == NULL)
1675 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1679 memset(ma, 0, sizeof(*ma));
1680 memset(spec, 0, sizeof(*spec));
1681 if (stripe_count != 0) {
1682 spec->sp_cr_flags |= FMODE_WRITE;
1683 echo_set_lmm_size(env, ld, ma);
1684 if (stripe_count != -1) {
1685 struct lov_user_md_v3 *lum = &info->eti_lum;
1687 lum->lmm_magic = LOV_USER_MAGIC_V3;
1688 lum->lmm_stripe_count = stripe_count;
1689 lum->lmm_stripe_offset = stripe_offset;
1690 lum->lmm_pattern = 0;
1691 spec->u.sp_ea.eadata = lum;
1692 spec->u.sp_ea.eadatalen = sizeof(*lum);
1693 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1697 ma->ma_attr.la_mode = mode;
1698 ma->ma_attr.la_valid = LA_CTIME | LA_MODE;
1699 ma->ma_attr.la_ctime = cfs_time_current_64();
1702 lname->ln_name = name;
1703 lname->ln_namelen = namelen;
1704 /* If name is specified, only create one object by name */
1705 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1710 /* Create multiple object sequenced by id */
1711 for (i = 0; i < count; i++) {
1712 char *tmp_name = info->eti_name;
1714 echo_md_build_name(lname, tmp_name, id);
1716 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1719 CERROR("Can not create child %s: rc = %d\n", tmp_name,
1730 static struct lu_object *echo_md_lookup(const struct lu_env *env,
1731 struct echo_device *ed,
1732 struct md_object *parent,
1733 struct lu_name *lname)
1735 struct echo_thread_info *info = echo_env_info(env);
1736 struct lu_fid *fid = &info->eti_fid;
1737 struct lu_object *child;
1741 CDEBUG(D_INFO, "lookup %s in parent "DFID" %p\n", lname->ln_name,
1743 rc = mdo_lookup(env, parent, lname, fid, NULL);
1745 CERROR("lookup %s: rc = %d\n", lname->ln_name, rc);
1746 RETURN(ERR_PTR(rc));
1749 /* In the function below, .hs_keycmp resolves to
1750 * lu_obj_hop_keycmp() */
1751 /* coverity[overrun-buffer-val] */
1752 child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1757 static int echo_setattr_object(const struct lu_env *env,
1758 struct echo_device *ed,
1759 struct lu_object *ec_parent,
1760 __u64 id, int count)
1762 struct lu_object *parent;
1763 struct echo_thread_info *info = echo_env_info(env);
1764 struct lu_name *lname = &info->eti_lname;
1765 char *name = info->eti_name;
1766 struct lu_device *ld = ed->ed_next;
1767 struct lu_buf *buf = &info->eti_buf;
1773 if (ec_parent == NULL)
1775 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1779 for (i = 0; i < count; i++) {
1780 struct lu_object *ec_child, *child;
1782 echo_md_build_name(lname, name, id);
1784 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1785 if (IS_ERR(ec_child)) {
1786 CERROR("Can't find child %s: rc = %ld\n",
1787 lname->ln_name, PTR_ERR(ec_child));
1788 RETURN(PTR_ERR(ec_child));
1791 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1792 if (child == NULL) {
1793 CERROR("Can not locate the child %s\n", lname->ln_name);
1794 lu_object_put(env, ec_child);
1799 CDEBUG(D_RPCTRACE, "Start setattr object "DFID"\n",
1800 PFID(lu_object_fid(child)));
1802 buf->lb_buf = info->eti_xattr_buf;
1803 buf->lb_len = sizeof(info->eti_xattr_buf);
1805 sprintf(name, "%s.test1", XATTR_USER_PREFIX);
1806 rc = mo_xattr_set(env, lu2md(child), buf, name,
1809 CERROR("Can not setattr child "DFID": rc = %d\n",
1810 PFID(lu_object_fid(child)), rc);
1811 lu_object_put(env, ec_child);
1814 CDEBUG(D_RPCTRACE, "End setattr object "DFID"\n",
1815 PFID(lu_object_fid(child)));
1817 lu_object_put(env, ec_child);
1822 static int echo_getattr_object(const struct lu_env *env,
1823 struct echo_device *ed,
1824 struct lu_object *ec_parent,
1825 __u64 id, int count)
1827 struct lu_object *parent;
1828 struct echo_thread_info *info = echo_env_info(env);
1829 struct lu_name *lname = &info->eti_lname;
1830 char *name = info->eti_name;
1831 struct md_attr *ma = &info->eti_ma;
1832 struct lu_device *ld = ed->ed_next;
1838 if (ec_parent == NULL)
1840 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1844 memset(ma, 0, sizeof(*ma));
1845 ma->ma_need |= MA_INODE | MA_LOV | MA_PFID | MA_HSM | MA_ACL_DEF;
1846 ma->ma_acl = info->eti_xattr_buf;
1847 ma->ma_acl_size = sizeof(info->eti_xattr_buf);
1849 for (i = 0; i < count; i++) {
1850 struct lu_object *ec_child, *child;
1853 echo_md_build_name(lname, name, id);
1854 echo_set_lmm_size(env, ld, ma);
1856 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1857 if (IS_ERR(ec_child)) {
1858 CERROR("Can't find child %s: rc = %ld\n",
1859 lname->ln_name, PTR_ERR(ec_child));
1860 RETURN(PTR_ERR(ec_child));
1863 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1864 if (child == NULL) {
1865 CERROR("Can not locate the child %s\n", lname->ln_name);
1866 lu_object_put(env, ec_child);
1870 CDEBUG(D_RPCTRACE, "Start getattr object "DFID"\n",
1871 PFID(lu_object_fid(child)));
1872 rc = echo_attr_get_complex(env, lu2md(child), ma);
1874 CERROR("Can not getattr child "DFID": rc = %d\n",
1875 PFID(lu_object_fid(child)), rc);
1876 lu_object_put(env, ec_child);
1879 CDEBUG(D_RPCTRACE, "End getattr object "DFID"\n",
1880 PFID(lu_object_fid(child)));
1882 lu_object_put(env, ec_child);
1888 static int echo_lookup_object(const struct lu_env *env,
1889 struct echo_device *ed,
1890 struct lu_object *ec_parent,
1891 __u64 id, int count)
1893 struct lu_object *parent;
1894 struct echo_thread_info *info = echo_env_info(env);
1895 struct lu_name *lname = &info->eti_lname;
1896 char *name = info->eti_name;
1897 struct lu_fid *fid = &info->eti_fid;
1898 struct lu_device *ld = ed->ed_next;
1902 if (ec_parent == NULL)
1904 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1908 /*prepare the requests*/
1909 for (i = 0; i < count; i++) {
1910 echo_md_build_name(lname, name, id);
1912 CDEBUG(D_RPCTRACE, "Start lookup object "DFID" %s %p\n",
1913 PFID(lu_object_fid(parent)), lname->ln_name, parent);
1915 rc = mdo_lookup(env, lu2md(parent), lname, fid, NULL);
1917 CERROR("Can not lookup child %s: rc = %d\n", name, rc);
1920 CDEBUG(D_RPCTRACE, "End lookup object "DFID" %s %p\n",
1921 PFID(lu_object_fid(parent)), lname->ln_name, parent);
1928 static int echo_md_destroy_internal(const struct lu_env *env,
1929 struct echo_device *ed,
1930 struct md_object *parent,
1931 struct lu_name *lname,
1934 struct lu_device *ld = ed->ed_next;
1935 struct lu_object *ec_child;
1936 struct lu_object *child;
1941 ec_child = echo_md_lookup(env, ed, parent, lname);
1942 if (IS_ERR(ec_child)) {
1943 CERROR("Can't find child %s: rc = %ld\n", lname->ln_name,
1945 RETURN(PTR_ERR(ec_child));
1948 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1949 if (child == NULL) {
1950 CERROR("Can not locate the child %s\n", lname->ln_name);
1951 GOTO(out_put, rc = -EINVAL);
1954 CDEBUG(D_RPCTRACE, "Start destroy object "DFID" %s %p\n",
1955 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1957 rc = mdo_unlink(env, parent, lu2md(child), lname, ma, 0);
1959 CERROR("Can not unlink child %s: rc = %d\n",
1960 lname->ln_name, rc);
1963 CDEBUG(D_RPCTRACE, "End destroy object "DFID" %s %p\n",
1964 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1966 lu_object_put(env, ec_child);
1970 static int echo_destroy_object(const struct lu_env *env,
1971 struct echo_device *ed,
1972 struct lu_object *ec_parent,
1973 char *name, int namelen,
1974 __u64 id, __u32 mode,
1977 struct echo_thread_info *info = echo_env_info(env);
1978 struct lu_name *lname = &info->eti_lname;
1979 struct md_attr *ma = &info->eti_ma;
1980 struct lu_device *ld = ed->ed_next;
1981 struct lu_object *parent;
1986 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1990 memset(ma, 0, sizeof(*ma));
1991 ma->ma_attr.la_mode = mode;
1992 ma->ma_attr.la_valid = LA_CTIME;
1993 ma->ma_attr.la_ctime = cfs_time_current_64();
1994 ma->ma_need = MA_INODE;
1998 lname->ln_name = name;
1999 lname->ln_namelen = namelen;
2000 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
2005 /*prepare the requests*/
2006 for (i = 0; i < count; i++) {
2007 char *tmp_name = info->eti_name;
2010 echo_md_build_name(lname, tmp_name, id);
2012 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
2015 CERROR("Can not unlink child %s: rc = %d\n", name, rc);
2024 static struct lu_object *echo_resolve_path(const struct lu_env *env,
2025 struct echo_device *ed, char *path,
2028 struct lu_device *ld = ed->ed_next;
2029 struct md_device *md = lu2md_dev(ld);
2030 struct echo_thread_info *info = echo_env_info(env);
2031 struct lu_fid *fid = &info->eti_fid;
2032 struct lu_name *lname = &info->eti_lname;
2033 struct lu_object *parent = NULL;
2034 struct lu_object *child = NULL;
2038 /*Only support MDD layer right now*/
2039 rc = md->md_ops->mdo_root_get(env, md, fid);
2041 CERROR("get root error: rc = %d\n", rc);
2042 RETURN(ERR_PTR(rc));
2045 /* In the function below, .hs_keycmp resolves to
2046 * lu_obj_hop_keycmp() */
2047 /* coverity[overrun-buffer-val] */
2048 parent = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
2049 if (IS_ERR(parent)) {
2050 CERROR("Can not find the parent "DFID": rc = %ld\n",
2051 PFID(fid), PTR_ERR(parent));
2056 struct lu_object *ld_parent;
2059 e = strsep(&path, "/");
2064 if (!path || path[0] == '\0')
2070 lname->ln_namelen = strlen(e);
2072 ld_parent = lu_object_locate(parent->lo_header, ld->ld_type);
2073 if (ld_parent == NULL) {
2074 lu_object_put(env, parent);
2079 child = echo_md_lookup(env, ed, lu2md(ld_parent), lname);
2080 lu_object_put(env, parent);
2081 if (IS_ERR(child)) {
2082 rc = (int)PTR_ERR(child);
2083 CERROR("lookup %s under parent "DFID": rc = %d\n",
2084 lname->ln_name, PFID(lu_object_fid(ld_parent)),
2091 RETURN(ERR_PTR(rc));
2096 static void echo_ucred_init(struct lu_env *env)
2098 struct lu_ucred *ucred = lu_ucred(env);
2100 ucred->uc_valid = UCRED_INVALID;
2102 ucred->uc_suppgids[0] = -1;
2103 ucred->uc_suppgids[1] = -1;
2105 ucred->uc_uid = ucred->uc_o_uid = cfs_curproc_uid();
2106 ucred->uc_gid = ucred->uc_o_gid = cfs_curproc_gid();
2107 ucred->uc_fsuid = ucred->uc_o_fsuid = cfs_curproc_fsuid();
2108 ucred->uc_fsgid = ucred->uc_o_fsgid = cfs_curproc_fsgid();
2109 ucred->uc_cap = cfs_curproc_cap_pack();
2111 /* remove fs privilege for non-root user. */
2112 if (ucred->uc_fsuid)
2113 ucred->uc_cap &= ~CFS_CAP_FS_MASK;
2114 ucred->uc_valid = UCRED_NEW;
2117 static void echo_ucred_fini(struct lu_env *env)
2119 struct lu_ucred *ucred = lu_ucred(env);
2120 ucred->uc_valid = UCRED_INIT;
2123 #define ECHO_MD_CTX_TAG (LCT_REMEMBER | LCT_MD_THREAD)
2124 #define ECHO_MD_SES_TAG (LCT_REMEMBER | LCT_SESSION)
2125 static int echo_md_handler(struct echo_device *ed, int command,
2126 char *path, int path_len, __u64 id, int count,
2127 struct obd_ioctl_data *data)
2129 struct echo_thread_info *info;
2130 struct lu_device *ld = ed->ed_next;
2133 struct lu_object *parent;
2135 int namelen = data->ioc_plen2;
2140 CERROR("MD echo client is not being initialized properly\n");
2144 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
2145 CERROR("Only support MDD layer right now!\n");
2149 env = cl_env_get(&refcheck);
2151 RETURN(PTR_ERR(env));
2153 rc = lu_env_refill_by_tags(env, ECHO_MD_CTX_TAG, ECHO_MD_SES_TAG);
2157 /* init big_lmm buffer */
2158 info = echo_env_info(env);
2159 LASSERT(info->eti_big_lmm == NULL);
2160 OBD_ALLOC_LARGE(info->eti_big_lmm, MIN_MD_SIZE);
2161 if (info->eti_big_lmm == NULL)
2162 GOTO(out_env, rc = -ENOMEM);
2163 info->eti_big_lmmsize = MIN_MD_SIZE;
2165 parent = echo_resolve_path(env, ed, path, path_len);
2166 if (IS_ERR(parent)) {
2167 CERROR("Can not resolve the path %s: rc = %ld\n", path,
2169 GOTO(out_free, rc = PTR_ERR(parent));
2173 OBD_ALLOC(name, namelen + 1);
2175 GOTO(out_put, rc = -ENOMEM);
2176 if (cfs_copy_from_user(name, data->ioc_pbuf2, namelen))
2177 GOTO(out_name, rc = -EFAULT);
2180 echo_ucred_init(env);
2183 case ECHO_MD_CREATE:
2184 case ECHO_MD_MKDIR: {
2185 struct echo_thread_info *info = echo_env_info(env);
2186 __u32 mode = data->ioc_obdo2.o_mode;
2187 struct lu_fid *fid = &info->eti_fid;
2188 int stripe_count = (int)data->ioc_obdo2.o_misc;
2189 int stripe_index = (int)data->ioc_obdo2.o_stripe_idx;
2191 rc = ostid_to_fid(fid, &data->ioc_obdo1.o_oi, 0);
2195 /* In the function below, .hs_keycmp resolves to
2196 * lu_obj_hop_keycmp() */
2197 /* coverity[overrun-buffer-val] */
2198 rc = echo_create_md_object(env, ed, parent, fid, name, namelen,
2199 id, mode, count, stripe_count,
2203 case ECHO_MD_DESTROY:
2204 case ECHO_MD_RMDIR: {
2205 __u32 mode = data->ioc_obdo2.o_mode;
2207 rc = echo_destroy_object(env, ed, parent, name, namelen,
2211 case ECHO_MD_LOOKUP:
2212 rc = echo_lookup_object(env, ed, parent, id, count);
2214 case ECHO_MD_GETATTR:
2215 rc = echo_getattr_object(env, ed, parent, id, count);
2217 case ECHO_MD_SETATTR:
2218 rc = echo_setattr_object(env, ed, parent, id, count);
2221 CERROR("unknown command %d\n", command);
2225 echo_ucred_fini(env);
2229 OBD_FREE(name, namelen + 1);
2231 lu_object_put(env, parent);
2233 LASSERT(info->eti_big_lmm);
2234 OBD_FREE_LARGE(info->eti_big_lmm, info->eti_big_lmmsize);
2235 info->eti_big_lmm = NULL;
2236 info->eti_big_lmmsize = 0;
2238 cl_env_put(env, &refcheck);
2242 static int echo_create_object(const struct lu_env *env, struct echo_device *ed,
2243 int on_target, struct obdo *oa, void *ulsm,
2244 int ulsm_nob, struct obd_trans_info *oti)
2246 struct echo_object *eco;
2247 struct echo_client_obd *ec = ed->ed_ec;
2248 struct lov_stripe_md *lsm = NULL;
2253 if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
2254 (on_target || /* set_stripe */
2255 ec->ec_nstripes != 0)) { /* LOV */
2256 CERROR ("No valid oid\n");
2260 rc = echo_alloc_memmd(ed, &lsm);
2262 CERROR("Cannot allocate md: rc = %d\n", rc);
2269 rc = echo_copyin_lsm (ed, lsm, ulsm, ulsm_nob);
2273 if (lsm->lsm_stripe_count == 0)
2274 lsm->lsm_stripe_count = ec->ec_nstripes;
2276 if (lsm->lsm_stripe_size == 0)
2277 lsm->lsm_stripe_size = CFS_PAGE_SIZE;
2281 /* setup stripes: indices + default ids if required */
2282 for (i = 0; i < lsm->lsm_stripe_count; i++) {
2283 if (ostid_id(&lsm->lsm_oinfo[i]->loi_oi) == 0)
2284 lsm->lsm_oinfo[i]->loi_oi = lsm->lsm_oi;
2286 lsm->lsm_oinfo[i]->loi_ost_idx =
2287 (idx + i) % ec->ec_nstripes;
2291 /* setup object ID here for !on_target and LOV hint */
2292 if (oa->o_valid & OBD_MD_FLID) {
2293 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2294 lsm->lsm_oi = oa->o_oi;
2297 if (ostid_id(&lsm->lsm_oi) == 0)
2298 ostid_set_id(&lsm->lsm_oi, ++last_object_id);
2302 /* Only echo objects are allowed to be created */
2303 LASSERT((oa->o_valid & OBD_MD_FLGROUP) &&
2304 (ostid_seq(&oa->o_oi) == FID_SEQ_ECHO));
2305 rc = obd_create(env, ec->ec_exp, oa, &lsm, oti);
2307 CERROR("Cannot create objects: rc = %d\n", rc);
2313 /* See what object ID we were given */
2314 oa->o_oi = lsm->lsm_oi;
2315 oa->o_valid |= OBD_MD_FLID;
2317 eco = cl_echo_object_find(ed, &lsm);
2319 GOTO(failed, rc = PTR_ERR(eco));
2320 cl_echo_object_put(eco);
2322 CDEBUG(D_INFO, "oa oid "DOSTID"\n", POSTID(&oa->o_oi));
2327 obd_destroy(env, ec->ec_exp, oa, lsm, oti, NULL, NULL);
2329 echo_free_memmd(ed, &lsm);
2331 CERROR("create object failed with: rc = %d\n", rc);
2335 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
2338 struct lov_stripe_md *lsm = NULL;
2339 struct echo_object *eco;
2343 if ((oa->o_valid & OBD_MD_FLID) == 0 || ostid_id(&oa->o_oi) == 0) {
2344 /* disallow use of object id 0 */
2345 CERROR ("No valid oid\n");
2349 rc = echo_alloc_memmd(ed, &lsm);
2353 lsm->lsm_oi = oa->o_oi;
2354 if (!(oa->o_valid & OBD_MD_FLGROUP))
2355 ostid_set_seq_echo(&lsm->lsm_oi);
2358 eco = cl_echo_object_find(ed, &lsm);
2364 echo_free_memmd(ed, &lsm);
2368 static void echo_put_object(struct echo_object *eco)
2370 if (cl_echo_object_put(eco))
2371 CERROR("echo client: drop an object failed");
2375 echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
2377 unsigned long stripe_count;
2378 unsigned long stripe_size;
2379 unsigned long width;
2380 unsigned long woffset;
2384 if (lsm->lsm_stripe_count <= 1)
2388 stripe_size = lsm->lsm_stripe_size;
2389 stripe_count = lsm->lsm_stripe_count;
2391 /* width = # bytes in all stripes */
2392 width = stripe_size * stripe_count;
2394 /* woffset = offset within a width; offset = whole number of widths */
2395 woffset = do_div (offset, width);
2397 stripe_index = woffset / stripe_size;
2399 *idp = ostid_id(&lsm->lsm_oinfo[stripe_index]->loi_oi);
2400 *offp = offset * stripe_size + woffset % stripe_size;
2404 echo_client_page_debug_setup(struct lov_stripe_md *lsm,
2405 cfs_page_t *page, int rw, obd_id id,
2406 obd_off offset, obd_off count)
2413 /* no partial pages on the client */
2414 LASSERT(count == CFS_PAGE_SIZE);
2416 addr = cfs_kmap(page);
2418 for (delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2419 if (rw == OBD_BRW_WRITE) {
2420 stripe_off = offset + delta;
2422 echo_get_stripe_off_id(lsm, &stripe_off, &stripe_id);
2424 stripe_off = 0xdeadbeef00c0ffeeULL;
2425 stripe_id = 0xdeadbeef00c0ffeeULL;
2427 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
2428 stripe_off, stripe_id);
2434 static int echo_client_page_debug_check(struct lov_stripe_md *lsm,
2435 cfs_page_t *page, obd_id id,
2436 obd_off offset, obd_off count)
2445 /* no partial pages on the client */
2446 LASSERT(count == CFS_PAGE_SIZE);
2448 addr = cfs_kmap(page);
2450 for (rc = delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2451 stripe_off = offset + delta;
2453 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
2455 rc2 = block_debug_check("test_brw",
2456 addr + delta, OBD_ECHO_BLOCK_SIZE,
2457 stripe_off, stripe_id);
2459 CERROR ("Error in echo object "LPX64"\n", id);
2468 static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
2469 struct echo_object *eco, obd_off offset,
2470 obd_size count, int async,
2471 struct obd_trans_info *oti)
2473 struct lov_stripe_md *lsm = eco->eo_lsm;
2475 struct brw_page *pga;
2476 struct brw_page *pgp;
2486 verify = (ostid_id(&oa->o_oi) != ECHO_PERSISTENT_OBJID &&
2487 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
2488 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
2490 gfp_mask = ((ostid_id(&oa->o_oi) & 2) == 0) ? CFS_ALLOC_STD : CFS_ALLOC_HIGHUSER;
2492 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
2493 LASSERT(lsm != NULL);
2494 LASSERT(ostid_id(&lsm->lsm_oi) == ostid_id(&oa->o_oi));
2497 (count & (~CFS_PAGE_MASK)) != 0)
2500 /* XXX think again with misaligned I/O */
2501 npages = count >> CFS_PAGE_SHIFT;
2503 if (rw == OBD_BRW_WRITE)
2504 brw_flags = OBD_BRW_ASYNC;
2506 OBD_ALLOC(pga, npages * sizeof(*pga));
2510 OBD_ALLOC(pages, npages * sizeof(*pages));
2511 if (pages == NULL) {
2512 OBD_FREE(pga, npages * sizeof(*pga));
2516 for (i = 0, pgp = pga, off = offset;
2518 i++, pgp++, off += CFS_PAGE_SIZE) {
2520 LASSERT (pgp->pg == NULL); /* for cleanup */
2523 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
2524 if (pgp->pg == NULL)
2528 pgp->count = CFS_PAGE_SIZE;
2530 pgp->flag = brw_flags;
2533 echo_client_page_debug_setup(lsm, pgp->pg, rw,
2534 ostid_id(&oa->o_oi), off,
2538 /* brw mode can only be used at client */
2539 LASSERT(ed->ed_next != NULL);
2540 rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
2543 if (rc != 0 || rw != OBD_BRW_READ)
2546 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
2547 if (pgp->pg == NULL)
2552 vrc = echo_client_page_debug_check(lsm, pgp->pg,
2553 ostid_id(&oa->o_oi),
2554 pgp->off, pgp->count);
2555 if (vrc != 0 && rc == 0)
2558 OBD_PAGE_FREE(pgp->pg);
2560 OBD_FREE(pga, npages * sizeof(*pga));
2561 OBD_FREE(pages, npages * sizeof(*pages));
2565 static int echo_client_prep_commit(const struct lu_env *env,
2566 struct obd_export *exp, int rw,
2567 struct obdo *oa, struct echo_object *eco,
2568 obd_off offset, obd_size count,
2569 obd_size batch, struct obd_trans_info *oti,
2572 struct lov_stripe_md *lsm = eco->eo_lsm;
2573 struct obd_ioobj ioo;
2574 struct niobuf_local *lnb;
2575 struct niobuf_remote *rnb;
2577 obd_size npages, tot_pages;
2578 int i, ret = 0, brw_flags = 0;
2582 if (count <= 0 || (count & (~CFS_PAGE_MASK)) != 0 ||
2583 (lsm != NULL && ostid_id(&lsm->lsm_oi) != ostid_id(&oa->o_oi)))
2586 npages = batch >> CFS_PAGE_SHIFT;
2587 tot_pages = count >> CFS_PAGE_SHIFT;
2589 OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
2590 OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
2592 if (lnb == NULL || rnb == NULL)
2593 GOTO(out, ret = -ENOMEM);
2595 if (rw == OBD_BRW_WRITE && async)
2596 brw_flags |= OBD_BRW_ASYNC;
2598 obdo_to_ioobj(oa, &ioo);
2602 for(; tot_pages; tot_pages -= npages) {
2605 if (tot_pages < npages)
2608 for (i = 0; i < npages; i++, off += CFS_PAGE_SIZE) {
2609 rnb[i].offset = off;
2610 rnb[i].len = CFS_PAGE_SIZE;
2611 rnb[i].flags = brw_flags;
2614 ioo.ioo_bufcnt = npages;
2615 oti->oti_transno = 0;
2618 ret = obd_preprw(env, rw, exp, oa, 1, &ioo, rnb, &lpages,
2622 LASSERT(lpages == npages);
2624 for (i = 0; i < lpages; i++) {
2625 cfs_page_t *page = lnb[i].page;
2627 /* read past eof? */
2628 if (page == NULL && lnb[i].rc == 0)
2632 lnb[i].flags |= OBD_BRW_ASYNC;
2634 if (ostid_id(&oa->o_oi) == ECHO_PERSISTENT_OBJID ||
2635 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
2636 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
2639 if (rw == OBD_BRW_WRITE)
2640 echo_client_page_debug_setup(lsm, page, rw,
2641 ostid_id(&oa->o_oi),
2645 echo_client_page_debug_check(lsm, page,
2646 ostid_id(&oa->o_oi),
2651 ret = obd_commitrw(env, rw, exp, oa, 1, &ioo,
2652 rnb, npages, lnb, oti, ret);
2656 /* Reset oti otherwise it would confuse ldiskfs. */
2657 memset(oti, 0, sizeof(*oti));
2659 /* Reuse env context. */
2660 lu_context_exit((struct lu_context *)&env->le_ctx);
2661 lu_context_enter((struct lu_context *)&env->le_ctx);
2666 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
2668 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
2672 static int echo_client_brw_ioctl(const struct lu_env *env, int rw,
2673 struct obd_export *exp,
2674 struct obd_ioctl_data *data,
2675 struct obd_trans_info *dummy_oti)
2677 struct obd_device *obd = class_exp2obd(exp);
2678 struct echo_device *ed = obd2echo_dev(obd);
2679 struct echo_client_obd *ec = ed->ed_ec;
2680 struct obdo *oa = &data->ioc_obdo1;
2681 struct echo_object *eco;
2687 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2689 rc = echo_get_object(&eco, ed, oa);
2693 oa->o_valid &= ~OBD_MD_FLHANDLE;
2695 /* OFD/obdfilter works only via prep/commit */
2696 test_mode = (long)data->ioc_pbuf1;
2700 if (ed->ed_next == NULL && test_mode != 3) {
2702 data->ioc_plen1 = data->ioc_count;
2705 /* Truncate batch size to maximum */
2706 if (data->ioc_plen1 > PTLRPC_MAX_BRW_SIZE)
2707 data->ioc_plen1 = PTLRPC_MAX_BRW_SIZE;
2709 switch (test_mode) {
2713 rc = echo_client_kbrw(ed, rw, oa,
2714 eco, data->ioc_offset,
2715 data->ioc_count, async, dummy_oti);
2718 rc = echo_client_prep_commit(env, ec->ec_exp, rw, oa,
2719 eco, data->ioc_offset,
2720 data->ioc_count, data->ioc_plen1,
2726 echo_put_object(eco);
2731 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
2732 int mode, obd_off offset, obd_size nob)
2734 struct echo_device *ed = obd2echo_dev(exp->exp_obd);
2735 struct lustre_handle *ulh = &oa->o_handle;
2736 struct echo_object *eco;
2741 if (ed->ed_next == NULL)
2742 RETURN(-EOPNOTSUPP);
2744 if (!(mode == LCK_PR || mode == LCK_PW))
2747 if ((offset & (~CFS_PAGE_MASK)) != 0 ||
2748 (nob & (~CFS_PAGE_MASK)) != 0)
2751 rc = echo_get_object (&eco, ed, oa);
2755 end = (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
2756 rc = cl_echo_enqueue(eco, offset, end, mode, &ulh->cookie);
2758 oa->o_valid |= OBD_MD_FLHANDLE;
2759 CDEBUG(D_INFO, "Cookie is "LPX64"\n", ulh->cookie);
2761 echo_put_object(eco);
2766 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
2768 struct echo_device *ed = obd2echo_dev(exp->exp_obd);
2769 __u64 cookie = oa->o_handle.cookie;
2771 if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
2774 CDEBUG(D_INFO, "Cookie is "LPX64"\n", cookie);
2775 return cl_echo_cancel(ed, cookie);
2779 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2780 void *karg, void *uarg)
2782 struct obd_device *obd = exp->exp_obd;
2783 struct echo_device *ed = obd2echo_dev(obd);
2784 struct echo_client_obd *ec = ed->ed_ec;
2785 struct echo_object *eco;
2786 struct obd_ioctl_data *data = karg;
2787 struct obd_trans_info dummy_oti;
2789 struct oti_req_ack_lock *ack_lock;
2792 int rw = OBD_BRW_READ;
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, rc = -ENOMEM);
2819 case OBD_IOC_CREATE: /* may create echo object */
2820 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2821 GOTO (out, rc = -EPERM);
2823 rc = echo_create_object(env, ed, 1, oa, data->ioc_pbuf1,
2824 data->ioc_plen1, &dummy_oti);
2827 case OBD_IOC_ECHO_MD: {
2834 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2835 GOTO(out, rc = -EPERM);
2837 count = data->ioc_count;
2838 cmd = data->ioc_command;
2840 id = ostid_id(&data->ioc_obdo2.o_oi);
2842 dirlen = data->ioc_plen1;
2843 OBD_ALLOC(dir, dirlen + 1);
2845 GOTO(out, rc = -ENOMEM);
2847 if (cfs_copy_from_user(dir, data->ioc_pbuf1, dirlen)) {
2848 OBD_FREE(dir, data->ioc_plen1 + 1);
2849 GOTO(out, rc = -EFAULT);
2852 rc = echo_md_handler(ed, cmd, dir, dirlen, id, count, data);
2853 OBD_FREE(dir, dirlen + 1);
2856 case OBD_IOC_ECHO_ALLOC_SEQ: {
2857 struct lu_env *cl_env;
2862 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2863 GOTO(out, rc = -EPERM);
2865 cl_env = cl_env_get(&refcheck);
2867 GOTO(out, rc = PTR_ERR(cl_env));
2869 rc = lu_env_refill_by_tags(cl_env, ECHO_MD_CTX_TAG,
2872 cl_env_put(cl_env, &refcheck);
2876 rc = seq_client_get_seq(cl_env, ed->ed_cl_seq, &seq);
2877 cl_env_put(cl_env, &refcheck);
2879 CERROR("%s: Can not alloc seq: rc = %d\n",
2884 if (cfs_copy_to_user(data->ioc_pbuf1, &seq, data->ioc_plen1))
2887 max_count = LUSTRE_METADATA_SEQ_MAX_WIDTH;
2888 if (cfs_copy_to_user(data->ioc_pbuf2, &max_count,
2893 case OBD_IOC_DESTROY:
2894 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2895 GOTO (out, rc = -EPERM);
2897 rc = echo_get_object(&eco, ed, oa);
2899 rc = obd_destroy(env, ec->ec_exp, oa, eco->eo_lsm,
2900 &dummy_oti, NULL, NULL);
2902 eco->eo_deleted = 1;
2903 echo_put_object(eco);
2907 case OBD_IOC_GETATTR:
2908 rc = echo_get_object(&eco, ed, oa);
2910 struct obd_info oinfo = { { { 0 } } };
2911 oinfo.oi_md = eco->eo_lsm;
2913 rc = obd_getattr(env, ec->ec_exp, &oinfo);
2914 echo_put_object(eco);
2918 case OBD_IOC_SETATTR:
2919 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2920 GOTO (out, rc = -EPERM);
2922 rc = echo_get_object(&eco, ed, oa);
2924 struct obd_info oinfo = { { { 0 } } };
2926 oinfo.oi_md = eco->eo_lsm;
2928 rc = obd_setattr(env, ec->ec_exp, &oinfo, NULL);
2929 echo_put_object(eco);
2933 case OBD_IOC_BRW_WRITE:
2934 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2935 GOTO (out, rc = -EPERM);
2939 case OBD_IOC_BRW_READ:
2940 rc = echo_client_brw_ioctl(env, rw, exp, data, &dummy_oti);
2943 case ECHO_IOC_GET_STRIPE:
2944 rc = echo_get_object(&eco, ed, oa);
2946 rc = echo_copyout_lsm(eco->eo_lsm, data->ioc_pbuf1,
2948 echo_put_object(eco);
2952 case ECHO_IOC_SET_STRIPE:
2953 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2954 GOTO (out, rc = -EPERM);
2956 if (data->ioc_pbuf1 == NULL) { /* unset */
2957 rc = echo_get_object(&eco, ed, oa);
2959 eco->eo_deleted = 1;
2960 echo_put_object(eco);
2963 rc = echo_create_object(env, ed, 0, oa,
2965 data->ioc_plen1, &dummy_oti);
2969 case ECHO_IOC_ENQUEUE:
2970 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2971 GOTO (out, rc = -EPERM);
2973 rc = echo_client_enqueue(exp, oa,
2974 data->ioc_conn1, /* lock mode */
2976 data->ioc_count);/*extent*/
2979 case ECHO_IOC_CANCEL:
2980 rc = echo_client_cancel(exp, oa);
2984 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
2985 GOTO (out, rc = -ENOTTY);
2993 /* XXX this should be in a helper also called by target_send_reply */
2994 for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
2996 if (!ack_lock->mode)
2998 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
3004 static int echo_client_setup(const struct lu_env *env,
3005 struct obd_device *obddev, struct lustre_cfg *lcfg)
3007 struct echo_client_obd *ec = &obddev->u.echo_client;
3008 struct obd_device *tgt;
3009 struct obd_uuid echo_uuid = { "ECHO_UUID" };
3010 struct obd_connect_data *ocd = NULL;
3014 if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
3015 CERROR("requires a TARGET OBD name\n");
3019 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
3020 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
3021 CERROR("device not attached or not set up (%s)\n",
3022 lustre_cfg_string(lcfg, 1));
3026 spin_lock_init(&ec->ec_lock);
3027 CFS_INIT_LIST_HEAD (&ec->ec_objects);
3028 CFS_INIT_LIST_HEAD (&ec->ec_locks);
3030 ec->ec_nstripes = 0;
3032 if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
3033 lu_context_tags_update(ECHO_MD_CTX_TAG);
3034 lu_session_tags_update(ECHO_MD_SES_TAG);
3038 OBD_ALLOC(ocd, sizeof(*ocd));
3040 CERROR("Can't alloc ocd connecting to %s\n",
3041 lustre_cfg_string(lcfg, 1));
3045 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
3046 OBD_CONNECT_BRW_SIZE |
3047 OBD_CONNECT_GRANT | OBD_CONNECT_FULL20 |
3048 OBD_CONNECT_64BITHASH | OBD_CONNECT_LVB_TYPE |
3050 ocd->ocd_brw_size = DT_MAX_BRW_SIZE;
3051 ocd->ocd_version = LUSTRE_VERSION_CODE;
3052 ocd->ocd_group = FID_SEQ_ECHO;
3054 rc = obd_connect(env, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
3056 /* Turn off pinger because it connects to tgt obd directly. */
3057 spin_lock(&tgt->obd_dev_lock);
3058 cfs_list_del_init(&ec->ec_exp->exp_obd_chain_timed);
3059 spin_unlock(&tgt->obd_dev_lock);
3062 OBD_FREE(ocd, sizeof(*ocd));
3065 CERROR("fail to connect to device %s\n",
3066 lustre_cfg_string(lcfg, 1));
3073 static int echo_client_cleanup(struct obd_device *obddev)
3075 struct echo_device *ed = obd2echo_dev(obddev);
3076 struct echo_client_obd *ec = &obddev->u.echo_client;
3080 /*Do nothing for Metadata echo client*/
3084 if (ed->ed_next_ismd) {
3085 lu_context_tags_clear(ECHO_MD_CTX_TAG);
3086 lu_session_tags_clear(ECHO_MD_SES_TAG);
3090 if (!cfs_list_empty(&obddev->obd_exports)) {
3091 CERROR("still has clients!\n");
3095 LASSERT(cfs_atomic_read(&ec->ec_exp->exp_refcount) > 0);
3096 rc = obd_disconnect(ec->ec_exp);
3098 CERROR("fail to disconnect device: %d\n", rc);
3103 static int echo_client_connect(const struct lu_env *env,
3104 struct obd_export **exp,
3105 struct obd_device *src, struct obd_uuid *cluuid,
3106 struct obd_connect_data *data, void *localdata)
3109 struct lustre_handle conn = { 0 };
3112 rc = class_connect(&conn, src, cluuid);
3114 *exp = class_conn2export(&conn);
3120 static int echo_client_disconnect(struct obd_export *exp)
3123 struct obd_device *obd;
3124 struct echo_client_obd *ec;
3125 struct ec_lock *ecl;
3131 GOTO(out, rc = -EINVAL);
3135 ec = &obd->u.echo_client;
3137 /* no more contention on export's lock list */
3138 while (!cfs_list_empty (&exp->exp_ec_data.eced_locks)) {
3139 ecl = cfs_list_entry (exp->exp_ec_data.eced_locks.next,
3140 struct ec_lock, ecl_exp_chain);
3141 cfs_list_del (&ecl->ecl_exp_chain);
3143 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm,
3144 ecl->ecl_mode, &ecl->ecl_lock_handle);
3146 CDEBUG (D_INFO, "Cancel lock on object "LPX64" on disconnect "
3147 "(%d)\n", ecl->ecl_object->eco_id, rc);
3149 echo_put_object (ecl->ecl_object);
3150 OBD_FREE (ecl, sizeof (*ecl));
3154 rc = class_disconnect(exp);
3160 static struct obd_ops echo_client_obd_ops = {
3161 .o_owner = THIS_MODULE,
3164 .o_setup = echo_client_setup,
3165 .o_cleanup = echo_client_cleanup,
3168 .o_iocontrol = echo_client_iocontrol,
3169 .o_connect = echo_client_connect,
3170 .o_disconnect = echo_client_disconnect
3173 int echo_client_init(void)
3175 struct lprocfs_static_vars lvars = { 0 };
3178 lprocfs_echo_init_vars(&lvars);
3180 rc = lu_kmem_init(echo_caches);
3182 rc = class_register_type(&echo_client_obd_ops, NULL,
3184 LUSTRE_ECHO_CLIENT_NAME,
3187 lu_kmem_fini(echo_caches);
3192 void echo_client_exit(void)
3194 class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
3195 lu_kmem_fini(echo_caches);
3199 static int __init obdecho_init(void)
3201 struct lprocfs_static_vars lvars;
3205 LCONSOLE_INFO("Echo OBD driver; http://www.lustre.org/\n");
3207 LASSERT(CFS_PAGE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
3209 lprocfs_echo_init_vars(&lvars);
3211 # ifdef HAVE_SERVER_SUPPORT
3212 rc = echo_persistent_pages_init();
3216 rc = class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
3217 LUSTRE_ECHO_NAME, NULL);
3222 rc = echo_client_init();
3224 # ifdef HAVE_SERVER_SUPPORT
3228 class_unregister_type(LUSTRE_ECHO_NAME);
3230 echo_persistent_pages_fini();
3236 static void /*__exit*/ obdecho_exit(void)
3240 # ifdef HAVE_SERVER_SUPPORT
3241 class_unregister_type(LUSTRE_ECHO_NAME);
3242 echo_persistent_pages_fini();
3246 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3247 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
3248 MODULE_LICENSE("GPL");
3250 cfs_module(obdecho, LUSTRE_VERSION_STRING, obdecho_init, obdecho_exit);
3251 #endif /* __KERNEL__ */
3253 /** @} echo_client */