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 struct page *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 struct page **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 #ifdef HAVE_SERVER_SUPPORT
201 struct md_op_spec eti_spec;
202 struct lov_mds_md_v3 eti_lmm;
203 struct lov_user_md_v3 eti_lum;
204 struct md_attr eti_ma;
205 struct lu_name eti_lname;
206 /* per-thread values, can be re-used */
210 struct lu_buf eti_buf;
211 char eti_xattr_buf[LUSTRE_POSIX_ACL_MAX_SIZE];
215 /* No session used right now */
216 struct echo_session_info {
220 static struct kmem_cache *echo_lock_kmem;
221 static struct kmem_cache *echo_object_kmem;
222 static struct kmem_cache *echo_thread_kmem;
223 static struct kmem_cache *echo_session_kmem;
224 /* static struct kmem_cache *echo_req_kmem; */
226 static struct lu_kmem_descr echo_caches[] = {
228 .ckd_cache = &echo_lock_kmem,
229 .ckd_name = "echo_lock_kmem",
230 .ckd_size = sizeof (struct echo_lock)
233 .ckd_cache = &echo_object_kmem,
234 .ckd_name = "echo_object_kmem",
235 .ckd_size = sizeof (struct echo_object)
238 .ckd_cache = &echo_thread_kmem,
239 .ckd_name = "echo_thread_kmem",
240 .ckd_size = sizeof (struct echo_thread_info)
243 .ckd_cache = &echo_session_kmem,
244 .ckd_name = "echo_session_kmem",
245 .ckd_size = sizeof (struct echo_session_info)
249 .ckd_cache = &echo_req_kmem,
250 .ckd_name = "echo_req_kmem",
251 .ckd_size = sizeof (struct echo_req)
259 /** \defgroup echo_page Page operations
261 * Echo page operations.
265 static struct page *echo_page_vmpage(const struct lu_env *env,
266 const struct cl_page_slice *slice)
268 return cl2echo_page(slice)->ep_vmpage;
271 static int echo_page_own(const struct lu_env *env,
272 const struct cl_page_slice *slice,
273 struct cl_io *io, int nonblock)
275 struct echo_page *ep = cl2echo_page(slice);
278 mutex_lock(&ep->ep_lock);
279 else if (!mutex_trylock(&ep->ep_lock))
284 static void echo_page_disown(const struct lu_env *env,
285 const struct cl_page_slice *slice,
288 struct echo_page *ep = cl2echo_page(slice);
290 LASSERT(mutex_is_locked(&ep->ep_lock));
291 mutex_unlock(&ep->ep_lock);
294 static void echo_page_discard(const struct lu_env *env,
295 const struct cl_page_slice *slice,
296 struct cl_io *unused)
298 cl_page_delete(env, slice->cpl_page);
301 static int echo_page_is_vmlocked(const struct lu_env *env,
302 const struct cl_page_slice *slice)
304 if (mutex_is_locked(&cl2echo_page(slice)->ep_lock))
309 static void echo_page_completion(const struct lu_env *env,
310 const struct cl_page_slice *slice,
313 LASSERT(slice->cpl_page->cp_sync_io != NULL);
316 static void echo_page_fini(const struct lu_env *env,
317 struct cl_page_slice *slice)
319 struct echo_object *eco = cl2echo_obj(slice->cpl_obj);
322 cfs_atomic_dec(&eco->eo_npages);
323 page_cache_release(cl2echo_page(slice)->ep_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, struct page *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, __GFP_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, __GFP_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, __GFP_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, __GFP_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 #ifdef HAVE_SERVER_SUPPORT
740 # define ECHO_SEQ_WIDTH 0xffffffff
741 static int echo_fid_init(struct echo_device *ed, char *obd_name,
742 struct seq_server_site *ss)
748 OBD_ALLOC_PTR(ed->ed_cl_seq);
749 if (ed->ed_cl_seq == NULL)
752 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
754 GOTO(out_free_seq, rc = -ENOMEM);
756 snprintf(prefix, MAX_OBD_NAME + 5, "srv-%s", obd_name);
758 /* Init client side sequence-manager */
759 rc = seq_client_init(ed->ed_cl_seq, NULL,
761 prefix, ss->ss_server_seq);
762 ed->ed_cl_seq->lcs_width = ECHO_SEQ_WIDTH;
763 OBD_FREE(prefix, MAX_OBD_NAME + 5);
765 GOTO(out_free_seq, rc);
770 OBD_FREE_PTR(ed->ed_cl_seq);
771 ed->ed_cl_seq = NULL;
775 static int echo_fid_fini(struct obd_device *obddev)
777 struct echo_device *ed = obd2echo_dev(obddev);
780 if (ed->ed_cl_seq != NULL) {
781 seq_client_fini(ed->ed_cl_seq);
782 OBD_FREE_PTR(ed->ed_cl_seq);
783 ed->ed_cl_seq = NULL;
788 #endif /* HAVE_SERVER_SUPPORT */
790 static struct lu_device *echo_device_alloc(const struct lu_env *env,
791 struct lu_device_type *t,
792 struct lustre_cfg *cfg)
794 struct lu_device *next;
795 struct echo_device *ed;
796 struct cl_device *cd;
797 struct obd_device *obd = NULL; /* to keep compiler happy */
798 struct obd_device *tgt;
799 const char *tgt_type_name;
806 GOTO(out, rc = -ENOMEM);
810 rc = cl_device_init(cd, t);
814 cd->cd_lu_dev.ld_ops = &echo_device_lu_ops;
815 cd->cd_ops = &echo_device_cl_ops;
818 obd = class_name2obd(lustre_cfg_string(cfg, 0));
819 LASSERT(obd != NULL);
820 LASSERT(env != NULL);
822 tgt = class_name2obd(lustre_cfg_string(cfg, 1));
824 CERROR("Can not find tgt device %s\n",
825 lustre_cfg_string(cfg, 1));
826 GOTO(out, rc = -ENODEV);
829 next = tgt->obd_lu_dev;
830 if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
831 ed->ed_next_ismd = 1;
833 ed->ed_next_ismd = 0;
834 rc = echo_site_init(env, ed);
840 rc = echo_client_setup(env, obd, cfg);
844 ed->ed_ec = &obd->u.echo_client;
847 if (ed->ed_next_ismd) {
848 #ifdef HAVE_SERVER_SUPPORT
849 /* Suppose to connect to some Metadata layer */
851 struct lu_device *ld;
855 CERROR("%s is not lu device type!\n",
856 lustre_cfg_string(cfg, 1));
857 GOTO(out, rc = -EINVAL);
860 tgt_type_name = lustre_cfg_string(cfg, 2);
861 if (!tgt_type_name) {
862 CERROR("%s no type name for echo %s setup\n",
863 lustre_cfg_string(cfg, 1),
864 tgt->obd_type->typ_name);
865 GOTO(out, rc = -EINVAL);
870 spin_lock(&ls->ls_ld_lock);
871 cfs_list_for_each_entry(ld, &ls->ls_ld_linkage, ld_linkage) {
872 if (strcmp(ld->ld_type->ldt_name, tgt_type_name) == 0) {
877 spin_unlock(&ls->ls_ld_lock);
880 CERROR("%s is not lu device type!\n",
881 lustre_cfg_string(cfg, 1));
882 GOTO(out, rc = -EINVAL);
886 /* For MD echo client, it will use the site in MDS stack */
887 ed->ed_site_myself.cs_lu = *ls;
888 ed->ed_site = &ed->ed_site_myself;
889 ed->ed_cl.cd_lu_dev.ld_site = &ed->ed_site_myself.cs_lu;
890 rc = echo_fid_init(ed, obd->obd_name, lu_site2seq(ls));
892 CERROR("echo fid init error %d\n", rc);
895 #else /* !HAVE_SERVER_SUPPORT */
896 CERROR("Local operations are NOT supported on client side. "
897 "Only remote operations are supported. Metadata client "
898 "must be run on server side.\n");
899 GOTO(out, rc = -EOPNOTSUPP);
902 /* if echo client is to be stacked upon ost device, the next is
903 * NULL since ost is not a clio device so far */
904 if (next != NULL && !lu_device_is_cl(next))
907 tgt_type_name = tgt->obd_type->typ_name;
909 LASSERT(next != NULL);
910 if (next->ld_site != NULL)
911 GOTO(out, rc = -EBUSY);
913 next->ld_site = &ed->ed_site->cs_lu;
914 rc = next->ld_type->ldt_ops->ldto_device_init(env, next,
915 next->ld_type->ldt_name,
920 /* Tricky case, I have to determine the obd type since
921 * CLIO uses the different parameters to initialize
922 * objects for lov & osc. */
923 if (strcmp(tgt_type_name, LUSTRE_LOV_NAME) == 0)
924 ed->ed_next_islov = 1;
926 LASSERT(strcmp(tgt_type_name,
927 LUSTRE_OSC_NAME) == 0);
929 LASSERT(strcmp(tgt_type_name, LUSTRE_OST_NAME) == 0);
933 RETURN(&cd->cd_lu_dev);
938 rc2 = echo_client_cleanup(obd);
940 CERROR("Cleanup obd device %s error(%d)\n",
945 echo_site_fini(env, ed);
947 cl_device_fini(&ed->ed_cl);
957 static int echo_device_init(const struct lu_env *env, struct lu_device *d,
958 const char *name, struct lu_device *next)
964 static struct lu_device *echo_device_fini(const struct lu_env *env,
967 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
968 struct lu_device *next = ed->ed_next;
970 while (next && !ed->ed_next_ismd)
971 next = next->ld_type->ldt_ops->ldto_device_fini(env, next);
975 static void echo_lock_release(const struct lu_env *env,
976 struct echo_lock *ecl,
979 struct cl_lock *clk = echo_lock2cl(ecl);
983 cl_lock_release(env, clk, "ec enqueue", ecl->el_object);
985 cl_lock_mutex_get(env, clk);
986 cl_lock_cancel(env, clk);
987 cl_lock_delete(env, clk);
988 cl_lock_mutex_put(env, clk);
990 cl_lock_put(env, clk);
993 static struct lu_device *echo_device_free(const struct lu_env *env,
996 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
997 struct echo_client_obd *ec = ed->ed_ec;
998 struct echo_object *eco;
999 struct lu_device *next = ed->ed_next;
1001 CDEBUG(D_INFO, "echo device:%p is going to be freed, next = %p\n",
1004 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
1006 /* check if there are objects still alive.
1007 * It shouldn't have any object because lu_site_purge would cleanup
1008 * all of cached objects. Anyway, probably the echo device is being
1009 * parallelly accessed.
1011 spin_lock(&ec->ec_lock);
1012 cfs_list_for_each_entry(eco, &ec->ec_objects, eo_obj_chain)
1013 eco->eo_deleted = 1;
1014 spin_unlock(&ec->ec_lock);
1017 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
1020 "Waiting for the reference of echo object to be dropped\n");
1022 /* Wait for the last reference to be dropped. */
1023 spin_lock(&ec->ec_lock);
1024 while (!cfs_list_empty(&ec->ec_objects)) {
1025 spin_unlock(&ec->ec_lock);
1026 CERROR("echo_client still has objects at cleanup time, "
1027 "wait for 1 second\n");
1028 schedule_timeout_and_set_state(TASK_UNINTERRUPTIBLE,
1029 cfs_time_seconds(1));
1030 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
1031 spin_lock(&ec->ec_lock);
1033 spin_unlock(&ec->ec_lock);
1035 LASSERT(cfs_list_empty(&ec->ec_locks));
1037 CDEBUG(D_INFO, "No object exists, exiting...\n");
1039 echo_client_cleanup(d->ld_obd);
1040 #ifdef HAVE_SERVER_SUPPORT
1041 echo_fid_fini(d->ld_obd);
1043 while (next && !ed->ed_next_ismd)
1044 next = next->ld_type->ldt_ops->ldto_device_free(env, next);
1046 LASSERT(ed->ed_site == lu2cl_site(d->ld_site));
1047 echo_site_fini(env, ed);
1048 cl_device_fini(&ed->ed_cl);
1054 static const struct lu_device_type_operations echo_device_type_ops = {
1055 .ldto_init = echo_type_init,
1056 .ldto_fini = echo_type_fini,
1058 .ldto_start = echo_type_start,
1059 .ldto_stop = echo_type_stop,
1061 .ldto_device_alloc = echo_device_alloc,
1062 .ldto_device_free = echo_device_free,
1063 .ldto_device_init = echo_device_init,
1064 .ldto_device_fini = echo_device_fini
1067 static struct lu_device_type echo_device_type = {
1068 .ldt_tags = LU_DEVICE_CL,
1069 .ldt_name = LUSTRE_ECHO_CLIENT_NAME,
1070 .ldt_ops = &echo_device_type_ops,
1071 .ldt_ctx_tags = LCT_CL_THREAD | LCT_MD_THREAD | LCT_DT_THREAD,
1075 /** \defgroup echo_exports Exported operations
1077 * exporting functions to echo client
1082 /* Interfaces to echo client obd device */
1083 static struct echo_object *cl_echo_object_find(struct echo_device *d,
1084 struct lov_stripe_md **lsmp)
1087 struct echo_thread_info *info;
1088 struct echo_object_conf *conf;
1089 struct lov_stripe_md *lsm;
1090 struct echo_object *eco;
1091 struct cl_object *obj;
1100 LASSERTF(ostid_id(&lsm->lsm_oi) != 0, DOSTID"\n", POSTID(&lsm->lsm_oi));
1101 LASSERTF(ostid_seq(&lsm->lsm_oi) == FID_SEQ_ECHO, DOSTID"\n",
1102 POSTID(&lsm->lsm_oi));
1104 /* Never return an object if the obd is to be freed. */
1105 if (echo_dev2cl(d)->cd_lu_dev.ld_obd->obd_stopping)
1106 RETURN(ERR_PTR(-ENODEV));
1108 env = cl_env_get(&refcheck);
1110 RETURN((void *)env);
1112 info = echo_env_info(env);
1113 conf = &info->eti_conf;
1115 if (!d->ed_next_islov) {
1116 struct lov_oinfo *oinfo = lsm->lsm_oinfo[0];
1117 LASSERT(oinfo != NULL);
1118 oinfo->loi_oi = lsm->lsm_oi;
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;
1131 rc = ostid_to_fid(fid, &lsm->lsm_oi, 0);
1133 GOTO(out, eco = ERR_PTR(rc));
1135 /* In the function below, .hs_keycmp resolves to
1136 * lu_obj_hop_keycmp() */
1137 /* coverity[overrun-buffer-val] */
1138 obj = cl_object_find(env, echo_dev2cl(d), fid, &conf->eoc_cl);
1140 GOTO(out, eco = (void*)obj);
1142 eco = cl2echo_obj(obj);
1143 if (eco->eo_deleted) {
1144 cl_object_put(env, obj);
1145 eco = ERR_PTR(-EAGAIN);
1149 cl_env_put(env, &refcheck);
1153 static int cl_echo_object_put(struct echo_object *eco)
1156 struct cl_object *obj = echo_obj2cl(eco);
1160 env = cl_env_get(&refcheck);
1162 RETURN(PTR_ERR(env));
1164 /* an external function to kill an object? */
1165 if (eco->eo_deleted) {
1166 struct lu_object_header *loh = obj->co_lu.lo_header;
1167 LASSERT(&eco->eo_hdr == luh2coh(loh));
1168 set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
1171 cl_object_put(env, obj);
1172 cl_env_put(env, &refcheck);
1176 static int cl_echo_enqueue0(struct lu_env *env, struct echo_object *eco,
1177 obd_off start, obd_off end, int mode,
1178 __u64 *cookie , __u32 enqflags)
1181 struct cl_lock *lck;
1182 struct cl_object *obj;
1183 struct cl_lock_descr *descr;
1184 struct echo_thread_info *info;
1188 info = echo_env_info(env);
1190 descr = &info->eti_descr;
1191 obj = echo_obj2cl(eco);
1193 descr->cld_obj = obj;
1194 descr->cld_start = cl_index(obj, start);
1195 descr->cld_end = cl_index(obj, end);
1196 descr->cld_mode = mode == LCK_PW ? CLM_WRITE : CLM_READ;
1197 descr->cld_enq_flags = enqflags;
1200 lck = cl_lock_request(env, io, descr, "ec enqueue", eco);
1202 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
1203 struct echo_lock *el;
1205 rc = cl_wait(env, lck);
1207 el = cl2echo_lock(cl_lock_at(lck, &echo_device_type));
1208 spin_lock(&ec->ec_lock);
1209 if (cfs_list_empty(&el->el_chain)) {
1210 cfs_list_add(&el->el_chain, &ec->ec_locks);
1211 el->el_cookie = ++ec->ec_unique;
1213 cfs_atomic_inc(&el->el_refcount);
1214 *cookie = el->el_cookie;
1215 spin_unlock(&ec->ec_lock);
1217 cl_lock_release(env, lck, "ec enqueue", current);
1223 static int cl_echo_enqueue(struct echo_object *eco, obd_off start, obd_off end,
1224 int mode, __u64 *cookie)
1226 struct echo_thread_info *info;
1233 env = cl_env_get(&refcheck);
1235 RETURN(PTR_ERR(env));
1237 info = echo_env_info(env);
1240 io->ci_ignore_layout = 1;
1241 result = cl_io_init(env, io, CIT_MISC, echo_obj2cl(eco));
1244 LASSERT(result == 0);
1246 result = cl_echo_enqueue0(env, eco, start, end, mode, cookie, 0);
1247 cl_io_fini(env, io);
1251 cl_env_put(env, &refcheck);
1255 static int cl_echo_cancel0(struct lu_env *env, struct echo_device *ed,
1258 struct echo_client_obd *ec = ed->ed_ec;
1259 struct echo_lock *ecl = NULL;
1261 int found = 0, still_used = 0;
1264 LASSERT(ec != NULL);
1265 spin_lock(&ec->ec_lock);
1266 cfs_list_for_each (el, &ec->ec_locks) {
1267 ecl = cfs_list_entry (el, struct echo_lock, el_chain);
1268 CDEBUG(D_INFO, "ecl: %p, cookie: "LPX64"\n", ecl, ecl->el_cookie);
1269 found = (ecl->el_cookie == cookie);
1271 if (cfs_atomic_dec_and_test(&ecl->el_refcount))
1272 cfs_list_del_init(&ecl->el_chain);
1278 spin_unlock(&ec->ec_lock);
1283 echo_lock_release(env, ecl, still_used);
1287 static int cl_echo_cancel(struct echo_device *ed, __u64 cookie)
1294 env = cl_env_get(&refcheck);
1296 RETURN(PTR_ERR(env));
1298 rc = cl_echo_cancel0(env, ed, cookie);
1300 cl_env_put(env, &refcheck);
1304 static int cl_echo_async_brw(const struct lu_env *env, struct cl_io *io,
1305 enum cl_req_type unused, struct cl_2queue *queue)
1307 struct cl_page *clp;
1308 struct cl_page *temp;
1312 cl_page_list_for_each_safe(clp, temp, &queue->c2_qin) {
1314 rc = cl_page_cache_add(env, io, clp, CRT_WRITE);
1317 result = result ?: rc;
1322 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
1323 struct page **pages, int npages, int async)
1326 struct echo_thread_info *info;
1327 struct cl_object *obj = echo_obj2cl(eco);
1328 struct echo_device *ed = eco->eo_dev;
1329 struct cl_2queue *queue;
1331 struct cl_page *clp;
1332 struct lustre_handle lh = { 0 };
1333 int page_size = cl_page_size(obj);
1339 LASSERT((offset & ~CFS_PAGE_MASK) == 0);
1340 LASSERT(ed->ed_next != NULL);
1341 env = cl_env_get(&refcheck);
1343 RETURN(PTR_ERR(env));
1345 info = echo_env_info(env);
1347 queue = &info->eti_queue;
1349 cl_2queue_init(queue);
1351 io->ci_ignore_layout = 1;
1352 rc = cl_io_init(env, io, CIT_MISC, obj);
1358 rc = cl_echo_enqueue0(env, eco, offset,
1359 offset + npages * PAGE_CACHE_SIZE - 1,
1360 rw == READ ? LCK_PR : LCK_PW, &lh.cookie,
1363 GOTO(error_lock, rc);
1365 for (i = 0; i < npages; i++) {
1367 clp = cl_page_find(env, obj, cl_index(obj, offset),
1368 pages[i], CPT_TRANSIENT);
1373 LASSERT(clp->cp_type == CPT_TRANSIENT);
1375 rc = cl_page_own(env, io, clp);
1377 LASSERT(clp->cp_state == CPS_FREEING);
1378 cl_page_put(env, clp);
1382 cl_2queue_add(queue, clp);
1384 /* drop the reference count for cl_page_find, so that the page
1385 * will be freed in cl_2queue_fini. */
1386 cl_page_put(env, clp);
1387 cl_page_clip(env, clp, 0, page_size);
1389 offset += page_size;
1393 enum cl_req_type typ = rw == READ ? CRT_READ : CRT_WRITE;
1395 async = async && (typ == CRT_WRITE);
1397 rc = cl_echo_async_brw(env, io, typ, queue);
1399 rc = cl_io_submit_sync(env, io, typ, queue, 0);
1400 CDEBUG(D_INFO, "echo_client %s write returns %d\n",
1401 async ? "async" : "sync", rc);
1404 cl_echo_cancel0(env, ed, lh.cookie);
1407 cl_2queue_discard(env, io, queue);
1408 cl_2queue_disown(env, io, queue);
1409 cl_2queue_fini(env, queue);
1410 cl_io_fini(env, io);
1412 cl_env_put(env, &refcheck);
1415 /** @} echo_exports */
1418 static obd_id last_object_id;
1421 echo_copyout_lsm (struct lov_stripe_md *lsm, void *_ulsm, int ulsm_nob)
1423 struct lov_stripe_md *ulsm = _ulsm;
1426 nob = offsetof (struct lov_stripe_md, lsm_oinfo[lsm->lsm_stripe_count]);
1430 if (copy_to_user (ulsm, lsm, sizeof(ulsm)))
1433 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1434 if (copy_to_user (ulsm->lsm_oinfo[i], lsm->lsm_oinfo[i],
1435 sizeof(lsm->lsm_oinfo[0])))
1442 echo_copyin_lsm (struct echo_device *ed, struct lov_stripe_md *lsm,
1443 void *ulsm, int ulsm_nob)
1445 struct echo_client_obd *ec = ed->ed_ec;
1448 if (ulsm_nob < sizeof (*lsm))
1451 if (copy_from_user (lsm, ulsm, sizeof (*lsm)))
1454 if (lsm->lsm_stripe_count > ec->ec_nstripes ||
1455 lsm->lsm_magic != LOV_MAGIC ||
1456 (lsm->lsm_stripe_size & (~CFS_PAGE_MASK)) != 0 ||
1457 ((__u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count > ~0UL))
1461 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1462 if (copy_from_user(lsm->lsm_oinfo[i],
1463 ((struct lov_stripe_md *)ulsm)-> \
1465 sizeof(lsm->lsm_oinfo[0])))
1471 #ifdef HAVE_SERVER_SUPPORT
1472 static inline void echo_md_build_name(struct lu_name *lname, char *name,
1475 sprintf(name, LPU64, id);
1476 lname->ln_name = name;
1477 lname->ln_namelen = strlen(name);
1480 /* similar to mdt_attr_get_complex */
1481 static int echo_big_lmm_get(const struct lu_env *env, struct md_object *o,
1484 struct echo_thread_info *info = echo_env_info(env);
1489 LASSERT(ma->ma_lmm_size > 0);
1491 rc = mo_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LOV);
1495 /* big_lmm may need to be grown */
1496 if (info->eti_big_lmmsize < rc) {
1497 int size = size_roundup_power2(rc);
1499 if (info->eti_big_lmmsize > 0) {
1500 /* free old buffer */
1501 LASSERT(info->eti_big_lmm);
1502 OBD_FREE_LARGE(info->eti_big_lmm,
1503 info->eti_big_lmmsize);
1504 info->eti_big_lmm = NULL;
1505 info->eti_big_lmmsize = 0;
1508 OBD_ALLOC_LARGE(info->eti_big_lmm, size);
1509 if (info->eti_big_lmm == NULL)
1511 info->eti_big_lmmsize = size;
1513 LASSERT(info->eti_big_lmmsize >= rc);
1515 info->eti_buf.lb_buf = info->eti_big_lmm;
1516 info->eti_buf.lb_len = info->eti_big_lmmsize;
1517 rc = mo_xattr_get(env, o, &info->eti_buf, XATTR_NAME_LOV);
1521 ma->ma_valid |= MA_LOV;
1522 ma->ma_lmm = info->eti_big_lmm;
1523 ma->ma_lmm_size = rc;
1528 int echo_attr_get_complex(const struct lu_env *env, struct md_object *next,
1531 struct echo_thread_info *info = echo_env_info(env);
1532 struct lu_buf *buf = &info->eti_buf;
1533 umode_t mode = lu_object_attr(&next->mo_lu);
1534 int need = ma->ma_need;
1541 if (need & MA_INODE) {
1542 ma->ma_need = MA_INODE;
1543 rc = mo_attr_get(env, next, ma);
1546 ma->ma_valid |= MA_INODE;
1549 if (need & MA_LOV) {
1550 if (S_ISREG(mode) || S_ISDIR(mode)) {
1551 LASSERT(ma->ma_lmm_size > 0);
1552 buf->lb_buf = ma->ma_lmm;
1553 buf->lb_len = ma->ma_lmm_size;
1554 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LOV);
1556 ma->ma_lmm_size = rc2;
1557 ma->ma_valid |= MA_LOV;
1558 } else if (rc2 == -ENODATA) {
1560 ma->ma_lmm_size = 0;
1561 } else if (rc2 == -ERANGE) {
1562 rc2 = echo_big_lmm_get(env, next, ma);
1564 GOTO(out, rc = rc2);
1566 GOTO(out, rc = rc2);
1571 #ifdef CONFIG_FS_POSIX_ACL
1572 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
1573 buf->lb_buf = ma->ma_acl;
1574 buf->lb_len = ma->ma_acl_size;
1575 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1577 ma->ma_acl_size = rc2;
1578 ma->ma_valid |= MA_ACL_DEF;
1579 } else if (rc2 == -ENODATA) {
1581 ma->ma_acl_size = 0;
1583 GOTO(out, rc = rc2);
1589 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
1590 rc, ma->ma_valid, ma->ma_lmm);
1595 echo_md_create_internal(const struct lu_env *env, struct echo_device *ed,
1596 struct md_object *parent, struct lu_fid *fid,
1597 struct lu_name *lname, struct md_op_spec *spec,
1600 struct lu_object *ec_child, *child;
1601 struct lu_device *ld = ed->ed_next;
1602 struct echo_thread_info *info = echo_env_info(env);
1603 struct lu_fid *fid2 = &info->eti_fid2;
1604 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
1609 rc = mdo_lookup(env, parent, lname, fid2, spec);
1612 else if (rc != -ENOENT)
1615 ec_child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev,
1617 if (IS_ERR(ec_child)) {
1618 CERROR("Can not find the child "DFID": rc = %ld\n", PFID(fid),
1620 RETURN(PTR_ERR(ec_child));
1623 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1624 if (child == NULL) {
1625 CERROR("Can not locate the child "DFID"\n", PFID(fid));
1626 GOTO(out_put, rc = -EINVAL);
1629 CDEBUG(D_RPCTRACE, "Start creating object "DFID" %s %p\n",
1630 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1633 * Do not perform lookup sanity check. We know that name does not exist.
1635 spec->sp_cr_lookup = 0;
1636 rc = mdo_create(env, parent, lname, lu2md(child), spec, ma);
1638 CERROR("Can not create child "DFID": rc = %d\n", PFID(fid), rc);
1641 CDEBUG(D_RPCTRACE, "End creating object "DFID" %s %p rc = %d\n",
1642 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent, rc);
1645 lu_object_put(env, ec_child);
1649 static int echo_set_lmm_size(const struct lu_env *env, struct lu_device *ld,
1652 struct echo_thread_info *info = echo_env_info(env);
1654 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
1655 ma->ma_lmm = (void *)&info->eti_lmm;
1656 ma->ma_lmm_size = sizeof(info->eti_lmm);
1658 LASSERT(info->eti_big_lmmsize);
1659 ma->ma_lmm = info->eti_big_lmm;
1660 ma->ma_lmm_size = info->eti_big_lmmsize;
1666 static int echo_create_md_object(const struct lu_env *env,
1667 struct echo_device *ed,
1668 struct lu_object *ec_parent,
1670 char *name, int namelen,
1671 __u64 id, __u32 mode, int count,
1672 int stripe_count, int stripe_offset)
1674 struct lu_object *parent;
1675 struct echo_thread_info *info = echo_env_info(env);
1676 struct lu_name *lname = &info->eti_lname;
1677 struct md_op_spec *spec = &info->eti_spec;
1678 struct md_attr *ma = &info->eti_ma;
1679 struct lu_device *ld = ed->ed_next;
1685 if (ec_parent == NULL)
1687 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1691 memset(ma, 0, sizeof(*ma));
1692 memset(spec, 0, sizeof(*spec));
1693 if (stripe_count != 0) {
1694 spec->sp_cr_flags |= FMODE_WRITE;
1695 echo_set_lmm_size(env, ld, ma);
1696 if (stripe_count != -1) {
1697 struct lov_user_md_v3 *lum = &info->eti_lum;
1699 lum->lmm_magic = LOV_USER_MAGIC_V3;
1700 lum->lmm_stripe_count = stripe_count;
1701 lum->lmm_stripe_offset = stripe_offset;
1702 lum->lmm_pattern = 0;
1703 spec->u.sp_ea.eadata = lum;
1704 spec->u.sp_ea.eadatalen = sizeof(*lum);
1705 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1709 ma->ma_attr.la_mode = mode;
1710 ma->ma_attr.la_valid = LA_CTIME | LA_MODE;
1711 ma->ma_attr.la_ctime = cfs_time_current_64();
1714 lname->ln_name = name;
1715 lname->ln_namelen = namelen;
1716 /* If name is specified, only create one object by name */
1717 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1722 /* Create multiple object sequenced by id */
1723 for (i = 0; i < count; i++) {
1724 char *tmp_name = info->eti_name;
1726 echo_md_build_name(lname, tmp_name, id);
1728 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1731 CERROR("Can not create child %s: rc = %d\n", tmp_name,
1742 static struct lu_object *echo_md_lookup(const struct lu_env *env,
1743 struct echo_device *ed,
1744 struct md_object *parent,
1745 struct lu_name *lname)
1747 struct echo_thread_info *info = echo_env_info(env);
1748 struct lu_fid *fid = &info->eti_fid;
1749 struct lu_object *child;
1753 CDEBUG(D_INFO, "lookup %s in parent "DFID" %p\n", lname->ln_name,
1755 rc = mdo_lookup(env, parent, lname, fid, NULL);
1757 CERROR("lookup %s: rc = %d\n", lname->ln_name, rc);
1758 RETURN(ERR_PTR(rc));
1761 /* In the function below, .hs_keycmp resolves to
1762 * lu_obj_hop_keycmp() */
1763 /* coverity[overrun-buffer-val] */
1764 child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1769 static int echo_setattr_object(const struct lu_env *env,
1770 struct echo_device *ed,
1771 struct lu_object *ec_parent,
1772 __u64 id, int count)
1774 struct lu_object *parent;
1775 struct echo_thread_info *info = echo_env_info(env);
1776 struct lu_name *lname = &info->eti_lname;
1777 char *name = info->eti_name;
1778 struct lu_device *ld = ed->ed_next;
1779 struct lu_buf *buf = &info->eti_buf;
1785 if (ec_parent == NULL)
1787 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1791 for (i = 0; i < count; i++) {
1792 struct lu_object *ec_child, *child;
1794 echo_md_build_name(lname, name, id);
1796 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1797 if (IS_ERR(ec_child)) {
1798 CERROR("Can't find child %s: rc = %ld\n",
1799 lname->ln_name, PTR_ERR(ec_child));
1800 RETURN(PTR_ERR(ec_child));
1803 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1804 if (child == NULL) {
1805 CERROR("Can not locate the child %s\n", lname->ln_name);
1806 lu_object_put(env, ec_child);
1811 CDEBUG(D_RPCTRACE, "Start setattr object "DFID"\n",
1812 PFID(lu_object_fid(child)));
1814 buf->lb_buf = info->eti_xattr_buf;
1815 buf->lb_len = sizeof(info->eti_xattr_buf);
1817 sprintf(name, "%s.test1", XATTR_USER_PREFIX);
1818 rc = mo_xattr_set(env, lu2md(child), buf, name,
1821 CERROR("Can not setattr child "DFID": rc = %d\n",
1822 PFID(lu_object_fid(child)), rc);
1823 lu_object_put(env, ec_child);
1826 CDEBUG(D_RPCTRACE, "End setattr object "DFID"\n",
1827 PFID(lu_object_fid(child)));
1829 lu_object_put(env, ec_child);
1834 static int echo_getattr_object(const struct lu_env *env,
1835 struct echo_device *ed,
1836 struct lu_object *ec_parent,
1837 __u64 id, int count)
1839 struct lu_object *parent;
1840 struct echo_thread_info *info = echo_env_info(env);
1841 struct lu_name *lname = &info->eti_lname;
1842 char *name = info->eti_name;
1843 struct md_attr *ma = &info->eti_ma;
1844 struct lu_device *ld = ed->ed_next;
1850 if (ec_parent == NULL)
1852 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1856 memset(ma, 0, sizeof(*ma));
1857 ma->ma_need |= MA_INODE | MA_LOV | MA_PFID | MA_HSM | MA_ACL_DEF;
1858 ma->ma_acl = info->eti_xattr_buf;
1859 ma->ma_acl_size = sizeof(info->eti_xattr_buf);
1861 for (i = 0; i < count; i++) {
1862 struct lu_object *ec_child, *child;
1865 echo_md_build_name(lname, name, id);
1866 echo_set_lmm_size(env, ld, ma);
1868 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1869 if (IS_ERR(ec_child)) {
1870 CERROR("Can't find child %s: rc = %ld\n",
1871 lname->ln_name, PTR_ERR(ec_child));
1872 RETURN(PTR_ERR(ec_child));
1875 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1876 if (child == NULL) {
1877 CERROR("Can not locate the child %s\n", lname->ln_name);
1878 lu_object_put(env, ec_child);
1882 CDEBUG(D_RPCTRACE, "Start getattr object "DFID"\n",
1883 PFID(lu_object_fid(child)));
1884 rc = echo_attr_get_complex(env, lu2md(child), ma);
1886 CERROR("Can not getattr child "DFID": rc = %d\n",
1887 PFID(lu_object_fid(child)), rc);
1888 lu_object_put(env, ec_child);
1891 CDEBUG(D_RPCTRACE, "End getattr object "DFID"\n",
1892 PFID(lu_object_fid(child)));
1894 lu_object_put(env, ec_child);
1900 static int echo_lookup_object(const struct lu_env *env,
1901 struct echo_device *ed,
1902 struct lu_object *ec_parent,
1903 __u64 id, int count)
1905 struct lu_object *parent;
1906 struct echo_thread_info *info = echo_env_info(env);
1907 struct lu_name *lname = &info->eti_lname;
1908 char *name = info->eti_name;
1909 struct lu_fid *fid = &info->eti_fid;
1910 struct lu_device *ld = ed->ed_next;
1914 if (ec_parent == NULL)
1916 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1920 /*prepare the requests*/
1921 for (i = 0; i < count; i++) {
1922 echo_md_build_name(lname, name, id);
1924 CDEBUG(D_RPCTRACE, "Start lookup object "DFID" %s %p\n",
1925 PFID(lu_object_fid(parent)), lname->ln_name, parent);
1927 rc = mdo_lookup(env, lu2md(parent), lname, fid, NULL);
1929 CERROR("Can not lookup child %s: rc = %d\n", name, rc);
1932 CDEBUG(D_RPCTRACE, "End lookup object "DFID" %s %p\n",
1933 PFID(lu_object_fid(parent)), lname->ln_name, parent);
1940 static int echo_md_destroy_internal(const struct lu_env *env,
1941 struct echo_device *ed,
1942 struct md_object *parent,
1943 struct lu_name *lname,
1946 struct lu_device *ld = ed->ed_next;
1947 struct lu_object *ec_child;
1948 struct lu_object *child;
1953 ec_child = echo_md_lookup(env, ed, parent, lname);
1954 if (IS_ERR(ec_child)) {
1955 CERROR("Can't find child %s: rc = %ld\n", lname->ln_name,
1957 RETURN(PTR_ERR(ec_child));
1960 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1961 if (child == NULL) {
1962 CERROR("Can not locate the child %s\n", lname->ln_name);
1963 GOTO(out_put, rc = -EINVAL);
1966 if (lu_object_remote(child)) {
1967 CERROR("Can not destroy remote object %s: rc = %d\n",
1968 lname->ln_name, -EPERM);
1969 GOTO(out_put, rc = -EPERM);
1971 CDEBUG(D_RPCTRACE, "Start destroy object "DFID" %s %p\n",
1972 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1974 rc = mdo_unlink(env, parent, lu2md(child), lname, ma, 0);
1976 CERROR("Can not unlink child %s: rc = %d\n",
1977 lname->ln_name, rc);
1980 CDEBUG(D_RPCTRACE, "End destroy object "DFID" %s %p\n",
1981 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1983 lu_object_put(env, ec_child);
1987 static int echo_destroy_object(const struct lu_env *env,
1988 struct echo_device *ed,
1989 struct lu_object *ec_parent,
1990 char *name, int namelen,
1991 __u64 id, __u32 mode,
1994 struct echo_thread_info *info = echo_env_info(env);
1995 struct lu_name *lname = &info->eti_lname;
1996 struct md_attr *ma = &info->eti_ma;
1997 struct lu_device *ld = ed->ed_next;
1998 struct lu_object *parent;
2003 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
2007 memset(ma, 0, sizeof(*ma));
2008 ma->ma_attr.la_mode = mode;
2009 ma->ma_attr.la_valid = LA_CTIME;
2010 ma->ma_attr.la_ctime = cfs_time_current_64();
2011 ma->ma_need = MA_INODE;
2015 lname->ln_name = name;
2016 lname->ln_namelen = namelen;
2017 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
2022 /*prepare the requests*/
2023 for (i = 0; i < count; i++) {
2024 char *tmp_name = info->eti_name;
2027 echo_md_build_name(lname, tmp_name, id);
2029 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
2032 CERROR("Can not unlink child %s: rc = %d\n", name, rc);
2041 static struct lu_object *echo_resolve_path(const struct lu_env *env,
2042 struct echo_device *ed, char *path,
2045 struct lu_device *ld = ed->ed_next;
2046 struct md_device *md = lu2md_dev(ld);
2047 struct echo_thread_info *info = echo_env_info(env);
2048 struct lu_fid *fid = &info->eti_fid;
2049 struct lu_name *lname = &info->eti_lname;
2050 struct lu_object *parent = NULL;
2051 struct lu_object *child = NULL;
2055 /*Only support MDD layer right now*/
2056 rc = md->md_ops->mdo_root_get(env, md, fid);
2058 CERROR("get root error: rc = %d\n", rc);
2059 RETURN(ERR_PTR(rc));
2062 /* In the function below, .hs_keycmp resolves to
2063 * lu_obj_hop_keycmp() */
2064 /* coverity[overrun-buffer-val] */
2065 parent = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
2066 if (IS_ERR(parent)) {
2067 CERROR("Can not find the parent "DFID": rc = %ld\n",
2068 PFID(fid), PTR_ERR(parent));
2073 struct lu_object *ld_parent;
2076 e = strsep(&path, "/");
2081 if (!path || path[0] == '\0')
2087 lname->ln_namelen = strlen(e);
2089 ld_parent = lu_object_locate(parent->lo_header, ld->ld_type);
2090 if (ld_parent == NULL) {
2091 lu_object_put(env, parent);
2096 child = echo_md_lookup(env, ed, lu2md(ld_parent), lname);
2097 lu_object_put(env, parent);
2098 if (IS_ERR(child)) {
2099 rc = (int)PTR_ERR(child);
2100 CERROR("lookup %s under parent "DFID": rc = %d\n",
2101 lname->ln_name, PFID(lu_object_fid(ld_parent)),
2108 RETURN(ERR_PTR(rc));
2113 static void echo_ucred_init(struct lu_env *env)
2115 struct lu_ucred *ucred = lu_ucred(env);
2117 ucred->uc_valid = UCRED_INVALID;
2119 ucred->uc_suppgids[0] = -1;
2120 ucred->uc_suppgids[1] = -1;
2122 ucred->uc_uid = ucred->uc_o_uid = current_uid();
2123 ucred->uc_gid = ucred->uc_o_gid = current_gid();
2124 ucred->uc_fsuid = ucred->uc_o_fsuid = current_fsuid();
2125 ucred->uc_fsgid = ucred->uc_o_fsgid = current_fsgid();
2126 ucred->uc_cap = cfs_curproc_cap_pack();
2128 /* remove fs privilege for non-root user. */
2129 if (ucred->uc_fsuid)
2130 ucred->uc_cap &= ~CFS_CAP_FS_MASK;
2131 ucred->uc_valid = UCRED_NEW;
2134 static void echo_ucred_fini(struct lu_env *env)
2136 struct lu_ucred *ucred = lu_ucred(env);
2137 ucred->uc_valid = UCRED_INIT;
2140 #define ECHO_MD_CTX_TAG (LCT_REMEMBER | LCT_MD_THREAD)
2141 #define ECHO_MD_SES_TAG (LCT_REMEMBER | LCT_SESSION | LCT_SERVER_SESSION)
2142 static int echo_md_handler(struct echo_device *ed, int command,
2143 char *path, int path_len, __u64 id, int count,
2144 struct obd_ioctl_data *data)
2146 struct echo_thread_info *info;
2147 struct lu_device *ld = ed->ed_next;
2150 struct lu_object *parent;
2152 int namelen = data->ioc_plen2;
2157 CERROR("MD echo client is not being initialized properly\n");
2161 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
2162 CERROR("Only support MDD layer right now!\n");
2166 env = cl_env_get(&refcheck);
2168 RETURN(PTR_ERR(env));
2170 rc = lu_env_refill_by_tags(env, ECHO_MD_CTX_TAG, ECHO_MD_SES_TAG);
2174 /* init big_lmm buffer */
2175 info = echo_env_info(env);
2176 LASSERT(info->eti_big_lmm == NULL);
2177 OBD_ALLOC_LARGE(info->eti_big_lmm, MIN_MD_SIZE);
2178 if (info->eti_big_lmm == NULL)
2179 GOTO(out_env, rc = -ENOMEM);
2180 info->eti_big_lmmsize = MIN_MD_SIZE;
2182 parent = echo_resolve_path(env, ed, path, path_len);
2183 if (IS_ERR(parent)) {
2184 CERROR("Can not resolve the path %s: rc = %ld\n", path,
2186 GOTO(out_free, rc = PTR_ERR(parent));
2190 OBD_ALLOC(name, namelen + 1);
2192 GOTO(out_put, rc = -ENOMEM);
2193 if (copy_from_user(name, data->ioc_pbuf2, namelen))
2194 GOTO(out_name, rc = -EFAULT);
2197 echo_ucred_init(env);
2200 case ECHO_MD_CREATE:
2201 case ECHO_MD_MKDIR: {
2202 struct echo_thread_info *info = echo_env_info(env);
2203 __u32 mode = data->ioc_obdo2.o_mode;
2204 struct lu_fid *fid = &info->eti_fid;
2205 int stripe_count = (int)data->ioc_obdo2.o_misc;
2206 int stripe_index = (int)data->ioc_obdo2.o_stripe_idx;
2208 rc = ostid_to_fid(fid, &data->ioc_obdo1.o_oi, 0);
2212 /* In the function below, .hs_keycmp resolves to
2213 * lu_obj_hop_keycmp() */
2214 /* coverity[overrun-buffer-val] */
2215 rc = echo_create_md_object(env, ed, parent, fid, name, namelen,
2216 id, mode, count, stripe_count,
2220 case ECHO_MD_DESTROY:
2221 case ECHO_MD_RMDIR: {
2222 __u32 mode = data->ioc_obdo2.o_mode;
2224 rc = echo_destroy_object(env, ed, parent, name, namelen,
2228 case ECHO_MD_LOOKUP:
2229 rc = echo_lookup_object(env, ed, parent, id, count);
2231 case ECHO_MD_GETATTR:
2232 rc = echo_getattr_object(env, ed, parent, id, count);
2234 case ECHO_MD_SETATTR:
2235 rc = echo_setattr_object(env, ed, parent, id, count);
2238 CERROR("unknown command %d\n", command);
2242 echo_ucred_fini(env);
2246 OBD_FREE(name, namelen + 1);
2248 lu_object_put(env, parent);
2250 LASSERT(info->eti_big_lmm);
2251 OBD_FREE_LARGE(info->eti_big_lmm, info->eti_big_lmmsize);
2252 info->eti_big_lmm = NULL;
2253 info->eti_big_lmmsize = 0;
2255 cl_env_put(env, &refcheck);
2258 #endif /* HAVE_SERVER_SUPPORT */
2260 static int echo_create_object(const struct lu_env *env, struct echo_device *ed,
2261 int on_target, struct obdo *oa, void *ulsm,
2262 int ulsm_nob, struct obd_trans_info *oti)
2264 struct echo_object *eco;
2265 struct echo_client_obd *ec = ed->ed_ec;
2266 struct lov_stripe_md *lsm = NULL;
2271 if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
2272 (on_target || /* set_stripe */
2273 ec->ec_nstripes != 0)) { /* LOV */
2274 CERROR ("No valid oid\n");
2278 rc = echo_alloc_memmd(ed, &lsm);
2280 CERROR("Cannot allocate md: rc = %d\n", rc);
2287 rc = echo_copyin_lsm (ed, lsm, ulsm, ulsm_nob);
2291 if (lsm->lsm_stripe_count == 0)
2292 lsm->lsm_stripe_count = ec->ec_nstripes;
2294 if (lsm->lsm_stripe_size == 0)
2295 lsm->lsm_stripe_size = PAGE_CACHE_SIZE;
2299 /* setup stripes: indices + default ids if required */
2300 for (i = 0; i < lsm->lsm_stripe_count; i++) {
2301 if (ostid_id(&lsm->lsm_oinfo[i]->loi_oi) == 0)
2302 lsm->lsm_oinfo[i]->loi_oi = lsm->lsm_oi;
2304 lsm->lsm_oinfo[i]->loi_ost_idx =
2305 (idx + i) % ec->ec_nstripes;
2309 /* setup object ID here for !on_target and LOV hint */
2310 if (oa->o_valid & OBD_MD_FLID) {
2311 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2312 lsm->lsm_oi = oa->o_oi;
2315 if (ostid_id(&lsm->lsm_oi) == 0)
2316 ostid_set_id(&lsm->lsm_oi, ++last_object_id);
2320 /* Only echo objects are allowed to be created */
2321 LASSERT((oa->o_valid & OBD_MD_FLGROUP) &&
2322 (ostid_seq(&oa->o_oi) == FID_SEQ_ECHO));
2323 rc = obd_create(env, ec->ec_exp, oa, &lsm, oti);
2325 CERROR("Cannot create objects: rc = %d\n", rc);
2331 /* See what object ID we were given */
2332 oa->o_oi = lsm->lsm_oi;
2333 oa->o_valid |= OBD_MD_FLID;
2335 eco = cl_echo_object_find(ed, &lsm);
2337 GOTO(failed, rc = PTR_ERR(eco));
2338 cl_echo_object_put(eco);
2340 CDEBUG(D_INFO, "oa oid "DOSTID"\n", POSTID(&oa->o_oi));
2345 obd_destroy(env, ec->ec_exp, oa, lsm, oti, NULL, NULL);
2347 echo_free_memmd(ed, &lsm);
2349 CERROR("create object failed with: rc = %d\n", rc);
2353 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
2356 struct lov_stripe_md *lsm = NULL;
2357 struct echo_object *eco;
2361 if ((oa->o_valid & OBD_MD_FLID) == 0 || ostid_id(&oa->o_oi) == 0) {
2362 /* disallow use of object id 0 */
2363 CERROR ("No valid oid\n");
2367 rc = echo_alloc_memmd(ed, &lsm);
2371 lsm->lsm_oi = oa->o_oi;
2372 if (!(oa->o_valid & OBD_MD_FLGROUP))
2373 ostid_set_seq_echo(&lsm->lsm_oi);
2376 eco = cl_echo_object_find(ed, &lsm);
2382 echo_free_memmd(ed, &lsm);
2386 static void echo_put_object(struct echo_object *eco)
2388 if (cl_echo_object_put(eco))
2389 CERROR("echo client: drop an object failed");
2393 echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
2395 unsigned long stripe_count;
2396 unsigned long stripe_size;
2397 unsigned long width;
2398 unsigned long woffset;
2402 if (lsm->lsm_stripe_count <= 1)
2406 stripe_size = lsm->lsm_stripe_size;
2407 stripe_count = lsm->lsm_stripe_count;
2409 /* width = # bytes in all stripes */
2410 width = stripe_size * stripe_count;
2412 /* woffset = offset within a width; offset = whole number of widths */
2413 woffset = do_div (offset, width);
2415 stripe_index = woffset / stripe_size;
2417 *idp = ostid_id(&lsm->lsm_oinfo[stripe_index]->loi_oi);
2418 *offp = offset * stripe_size + woffset % stripe_size;
2422 echo_client_page_debug_setup(struct lov_stripe_md *lsm,
2423 struct page *page, int rw, obd_id id,
2424 obd_off offset, obd_off count)
2431 /* no partial pages on the client */
2432 LASSERT(count == PAGE_CACHE_SIZE);
2436 for (delta = 0; delta < PAGE_CACHE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2437 if (rw == OBD_BRW_WRITE) {
2438 stripe_off = offset + delta;
2440 echo_get_stripe_off_id(lsm, &stripe_off, &stripe_id);
2442 stripe_off = 0xdeadbeef00c0ffeeULL;
2443 stripe_id = 0xdeadbeef00c0ffeeULL;
2445 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
2446 stripe_off, stripe_id);
2452 static int echo_client_page_debug_check(struct lov_stripe_md *lsm,
2453 struct page *page, obd_id id,
2454 obd_off offset, obd_off count)
2463 /* no partial pages on the client */
2464 LASSERT(count == PAGE_CACHE_SIZE);
2468 for (rc = delta = 0; delta < PAGE_CACHE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2469 stripe_off = offset + delta;
2471 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
2473 rc2 = block_debug_check("test_brw",
2474 addr + delta, OBD_ECHO_BLOCK_SIZE,
2475 stripe_off, stripe_id);
2477 CERROR ("Error in echo object "LPX64"\n", id);
2486 static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
2487 struct echo_object *eco, obd_off offset,
2488 obd_size count, int async,
2489 struct obd_trans_info *oti)
2491 struct lov_stripe_md *lsm = eco->eo_lsm;
2493 struct brw_page *pga;
2494 struct brw_page *pgp;
2495 struct page **pages;
2504 verify = (ostid_id(&oa->o_oi) != ECHO_PERSISTENT_OBJID &&
2505 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
2506 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
2508 gfp_mask = ((ostid_id(&oa->o_oi) & 2) == 0) ? GFP_IOFS : GFP_HIGHUSER;
2510 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
2511 LASSERT(lsm != NULL);
2512 LASSERT(ostid_id(&lsm->lsm_oi) == ostid_id(&oa->o_oi));
2515 (count & (~CFS_PAGE_MASK)) != 0)
2518 /* XXX think again with misaligned I/O */
2519 npages = count >> PAGE_CACHE_SHIFT;
2521 if (rw == OBD_BRW_WRITE)
2522 brw_flags = OBD_BRW_ASYNC;
2524 OBD_ALLOC(pga, npages * sizeof(*pga));
2528 OBD_ALLOC(pages, npages * sizeof(*pages));
2529 if (pages == NULL) {
2530 OBD_FREE(pga, npages * sizeof(*pga));
2534 for (i = 0, pgp = pga, off = offset;
2536 i++, pgp++, off += PAGE_CACHE_SIZE) {
2538 LASSERT (pgp->pg == NULL); /* for cleanup */
2541 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
2542 if (pgp->pg == NULL)
2546 pgp->count = PAGE_CACHE_SIZE;
2548 pgp->flag = brw_flags;
2551 echo_client_page_debug_setup(lsm, pgp->pg, rw,
2552 ostid_id(&oa->o_oi), off,
2556 /* brw mode can only be used at client */
2557 LASSERT(ed->ed_next != NULL);
2558 rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
2561 if (rc != 0 || rw != OBD_BRW_READ)
2564 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
2565 if (pgp->pg == NULL)
2570 vrc = echo_client_page_debug_check(lsm, pgp->pg,
2571 ostid_id(&oa->o_oi),
2572 pgp->off, pgp->count);
2573 if (vrc != 0 && rc == 0)
2576 OBD_PAGE_FREE(pgp->pg);
2578 OBD_FREE(pga, npages * sizeof(*pga));
2579 OBD_FREE(pages, npages * sizeof(*pages));
2583 static int echo_client_prep_commit(const struct lu_env *env,
2584 struct obd_export *exp, int rw,
2585 struct obdo *oa, struct echo_object *eco,
2586 obd_off offset, obd_size count,
2587 obd_size batch, struct obd_trans_info *oti,
2590 struct lov_stripe_md *lsm = eco->eo_lsm;
2591 struct obd_ioobj ioo;
2592 struct niobuf_local *lnb;
2593 struct niobuf_remote *rnb;
2595 obd_size npages, tot_pages;
2596 int i, ret = 0, brw_flags = 0;
2600 if (count <= 0 || (count & (~CFS_PAGE_MASK)) != 0 ||
2601 (lsm != NULL && ostid_id(&lsm->lsm_oi) != ostid_id(&oa->o_oi)))
2604 npages = batch >> PAGE_CACHE_SHIFT;
2605 tot_pages = count >> PAGE_CACHE_SHIFT;
2607 OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
2608 OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
2610 if (lnb == NULL || rnb == NULL)
2611 GOTO(out, ret = -ENOMEM);
2613 if (rw == OBD_BRW_WRITE && async)
2614 brw_flags |= OBD_BRW_ASYNC;
2616 obdo_to_ioobj(oa, &ioo);
2620 for(; tot_pages; tot_pages -= npages) {
2623 if (tot_pages < npages)
2626 for (i = 0; i < npages; i++, off += PAGE_CACHE_SIZE) {
2627 rnb[i].offset = off;
2628 rnb[i].len = PAGE_CACHE_SIZE;
2629 rnb[i].flags = brw_flags;
2632 ioo.ioo_bufcnt = npages;
2633 oti->oti_transno = 0;
2636 ret = obd_preprw(env, rw, exp, oa, 1, &ioo, rnb, &lpages,
2640 LASSERT(lpages == npages);
2642 for (i = 0; i < lpages; i++) {
2643 struct page *page = lnb[i].page;
2645 /* read past eof? */
2646 if (page == NULL && lnb[i].rc == 0)
2650 lnb[i].flags |= OBD_BRW_ASYNC;
2652 if (ostid_id(&oa->o_oi) == ECHO_PERSISTENT_OBJID ||
2653 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
2654 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
2657 if (rw == OBD_BRW_WRITE)
2658 echo_client_page_debug_setup(lsm, page, rw,
2659 ostid_id(&oa->o_oi),
2663 echo_client_page_debug_check(lsm, page,
2664 ostid_id(&oa->o_oi),
2669 ret = obd_commitrw(env, rw, exp, oa, 1, &ioo,
2670 rnb, npages, lnb, oti, ret);
2674 /* Reset oti otherwise it would confuse ldiskfs. */
2675 memset(oti, 0, sizeof(*oti));
2677 /* Reuse env context. */
2678 lu_context_exit((struct lu_context *)&env->le_ctx);
2679 lu_context_enter((struct lu_context *)&env->le_ctx);
2684 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
2686 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
2690 static int echo_client_brw_ioctl(const struct lu_env *env, int rw,
2691 struct obd_export *exp,
2692 struct obd_ioctl_data *data,
2693 struct obd_trans_info *dummy_oti)
2695 struct obd_device *obd = class_exp2obd(exp);
2696 struct echo_device *ed = obd2echo_dev(obd);
2697 struct echo_client_obd *ec = ed->ed_ec;
2698 struct obdo *oa = &data->ioc_obdo1;
2699 struct echo_object *eco;
2705 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2707 rc = echo_get_object(&eco, ed, oa);
2711 oa->o_valid &= ~OBD_MD_FLHANDLE;
2713 /* OFD/obdfilter works only via prep/commit */
2714 test_mode = (long)data->ioc_pbuf1;
2718 if (ed->ed_next == NULL && test_mode != 3) {
2720 data->ioc_plen1 = data->ioc_count;
2723 /* Truncate batch size to maximum */
2724 if (data->ioc_plen1 > PTLRPC_MAX_BRW_SIZE)
2725 data->ioc_plen1 = PTLRPC_MAX_BRW_SIZE;
2727 switch (test_mode) {
2731 rc = echo_client_kbrw(ed, rw, oa,
2732 eco, data->ioc_offset,
2733 data->ioc_count, async, dummy_oti);
2736 rc = echo_client_prep_commit(env, ec->ec_exp, rw, oa,
2737 eco, data->ioc_offset,
2738 data->ioc_count, data->ioc_plen1,
2744 echo_put_object(eco);
2749 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
2750 int mode, obd_off offset, obd_size nob)
2752 struct echo_device *ed = obd2echo_dev(exp->exp_obd);
2753 struct lustre_handle *ulh = &oa->o_handle;
2754 struct echo_object *eco;
2759 if (ed->ed_next == NULL)
2760 RETURN(-EOPNOTSUPP);
2762 if (!(mode == LCK_PR || mode == LCK_PW))
2765 if ((offset & (~CFS_PAGE_MASK)) != 0 ||
2766 (nob & (~CFS_PAGE_MASK)) != 0)
2769 rc = echo_get_object (&eco, ed, oa);
2773 end = (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
2774 rc = cl_echo_enqueue(eco, offset, end, mode, &ulh->cookie);
2776 oa->o_valid |= OBD_MD_FLHANDLE;
2777 CDEBUG(D_INFO, "Cookie is "LPX64"\n", ulh->cookie);
2779 echo_put_object(eco);
2784 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
2786 struct echo_device *ed = obd2echo_dev(exp->exp_obd);
2787 __u64 cookie = oa->o_handle.cookie;
2789 if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
2792 CDEBUG(D_INFO, "Cookie is "LPX64"\n", cookie);
2793 return cl_echo_cancel(ed, cookie);
2797 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2798 void *karg, void *uarg)
2800 #ifdef HAVE_SERVER_SUPPORT
2801 struct tgt_session_info *tsi;
2803 struct obd_device *obd = exp->exp_obd;
2804 struct echo_device *ed = obd2echo_dev(obd);
2805 struct echo_client_obd *ec = ed->ed_ec;
2806 struct echo_object *eco;
2807 struct obd_ioctl_data *data = karg;
2808 struct obd_trans_info dummy_oti;
2810 struct oti_req_ack_lock *ack_lock;
2813 int rw = OBD_BRW_READ;
2816 #ifdef HAVE_SERVER_SUPPORT
2817 struct lu_context echo_session;
2821 memset(&dummy_oti, 0, sizeof(dummy_oti));
2823 oa = &data->ioc_obdo1;
2824 if (!(oa->o_valid & OBD_MD_FLGROUP)) {
2825 oa->o_valid |= OBD_MD_FLGROUP;
2826 ostid_set_seq_echo(&oa->o_oi);
2829 /* This FID is unpacked just for validation at this point */
2830 rc = ostid_to_fid(&fid, &oa->o_oi, 0);
2838 rc = lu_env_init(env, LCT_DT_THREAD);
2840 GOTO(out_alloc, rc = -ENOMEM);
2842 #ifdef HAVE_SERVER_SUPPORT
2843 env->le_ses = &echo_session;
2844 rc = lu_context_init(env->le_ses, LCT_SERVER_SESSION | LCT_NOREF);
2845 if (unlikely(rc < 0))
2847 lu_context_enter(env->le_ses);
2849 tsi = tgt_ses_info(env);
2850 tsi->tsi_exp = ec->ec_exp;
2853 case OBD_IOC_CREATE: /* may create echo object */
2854 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2855 GOTO (out, rc = -EPERM);
2857 rc = echo_create_object(env, ed, 1, oa, data->ioc_pbuf1,
2858 data->ioc_plen1, &dummy_oti);
2861 #ifdef HAVE_SERVER_SUPPORT
2862 case OBD_IOC_ECHO_MD: {
2869 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2870 GOTO(out, rc = -EPERM);
2872 count = data->ioc_count;
2873 cmd = data->ioc_command;
2875 id = data->ioc_obdo2.o_oi.oi.oi_id;
2876 dirlen = data->ioc_plen1;
2877 OBD_ALLOC(dir, dirlen + 1);
2879 GOTO(out, rc = -ENOMEM);
2881 if (copy_from_user(dir, data->ioc_pbuf1, dirlen)) {
2882 OBD_FREE(dir, data->ioc_plen1 + 1);
2883 GOTO(out, rc = -EFAULT);
2886 rc = echo_md_handler(ed, cmd, dir, dirlen, id, count, data);
2887 OBD_FREE(dir, dirlen + 1);
2890 case OBD_IOC_ECHO_ALLOC_SEQ: {
2891 struct lu_env *cl_env;
2896 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2897 GOTO(out, rc = -EPERM);
2899 cl_env = cl_env_get(&refcheck);
2901 GOTO(out, rc = PTR_ERR(cl_env));
2903 rc = lu_env_refill_by_tags(cl_env, ECHO_MD_CTX_TAG,
2906 cl_env_put(cl_env, &refcheck);
2910 rc = seq_client_get_seq(cl_env, ed->ed_cl_seq, &seq);
2911 cl_env_put(cl_env, &refcheck);
2913 CERROR("%s: Can not alloc seq: rc = %d\n",
2918 if (copy_to_user(data->ioc_pbuf1, &seq, data->ioc_plen1))
2921 max_count = LUSTRE_METADATA_SEQ_MAX_WIDTH;
2922 if (copy_to_user(data->ioc_pbuf2, &max_count,
2927 #endif /* HAVE_SERVER_SUPPORT */
2928 case OBD_IOC_DESTROY:
2929 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2930 GOTO (out, rc = -EPERM);
2932 rc = echo_get_object(&eco, ed, oa);
2934 rc = obd_destroy(env, ec->ec_exp, oa, eco->eo_lsm,
2935 &dummy_oti, NULL, NULL);
2937 eco->eo_deleted = 1;
2938 echo_put_object(eco);
2942 case OBD_IOC_GETATTR:
2943 rc = echo_get_object(&eco, ed, oa);
2945 struct obd_info oinfo = { { { 0 } } };
2946 oinfo.oi_md = eco->eo_lsm;
2948 rc = obd_getattr(env, ec->ec_exp, &oinfo);
2949 echo_put_object(eco);
2953 case OBD_IOC_SETATTR:
2954 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2955 GOTO (out, rc = -EPERM);
2957 rc = echo_get_object(&eco, ed, oa);
2959 struct obd_info oinfo = { { { 0 } } };
2961 oinfo.oi_md = eco->eo_lsm;
2963 rc = obd_setattr(env, ec->ec_exp, &oinfo, NULL);
2964 echo_put_object(eco);
2968 case OBD_IOC_BRW_WRITE:
2969 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2970 GOTO (out, rc = -EPERM);
2974 case OBD_IOC_BRW_READ:
2975 rc = echo_client_brw_ioctl(env, rw, exp, data, &dummy_oti);
2978 case ECHO_IOC_GET_STRIPE:
2979 rc = echo_get_object(&eco, ed, oa);
2981 rc = echo_copyout_lsm(eco->eo_lsm, data->ioc_pbuf1,
2983 echo_put_object(eco);
2987 case ECHO_IOC_SET_STRIPE:
2988 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2989 GOTO (out, rc = -EPERM);
2991 if (data->ioc_pbuf1 == NULL) { /* unset */
2992 rc = echo_get_object(&eco, ed, oa);
2994 eco->eo_deleted = 1;
2995 echo_put_object(eco);
2998 rc = echo_create_object(env, ed, 0, oa,
3000 data->ioc_plen1, &dummy_oti);
3004 case ECHO_IOC_ENQUEUE:
3005 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
3006 GOTO (out, rc = -EPERM);
3008 rc = echo_client_enqueue(exp, oa,
3009 data->ioc_conn1, /* lock mode */
3011 data->ioc_count);/*extent*/
3014 case ECHO_IOC_CANCEL:
3015 rc = echo_client_cancel(exp, oa);
3019 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
3020 GOTO (out, rc = -ENOTTY);
3025 #ifdef HAVE_SERVER_SUPPORT
3026 lu_context_exit(env->le_ses);
3027 lu_context_fini(env->le_ses);
3034 /* XXX this should be in a helper also called by target_send_reply */
3035 for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
3037 if (!ack_lock->mode)
3039 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
3045 static int echo_client_setup(const struct lu_env *env,
3046 struct obd_device *obddev, struct lustre_cfg *lcfg)
3048 struct echo_client_obd *ec = &obddev->u.echo_client;
3049 struct obd_device *tgt;
3050 struct obd_uuid echo_uuid = { "ECHO_UUID" };
3051 struct obd_connect_data *ocd = NULL;
3055 if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
3056 CERROR("requires a TARGET OBD name\n");
3060 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
3061 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
3062 CERROR("device not attached or not set up (%s)\n",
3063 lustre_cfg_string(lcfg, 1));
3067 spin_lock_init(&ec->ec_lock);
3068 CFS_INIT_LIST_HEAD (&ec->ec_objects);
3069 CFS_INIT_LIST_HEAD (&ec->ec_locks);
3071 ec->ec_nstripes = 0;
3073 if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
3074 #ifdef HAVE_SERVER_SUPPORT
3075 lu_context_tags_update(ECHO_MD_CTX_TAG);
3076 lu_session_tags_update(ECHO_MD_SES_TAG);
3078 CERROR("Local operations are NOT supported on client side. "
3079 "Only remote operations are supported. Metadata client "
3080 "must be run on server side.\n");
3085 OBD_ALLOC(ocd, sizeof(*ocd));
3087 CERROR("Can't alloc ocd connecting to %s\n",
3088 lustre_cfg_string(lcfg, 1));
3092 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
3093 OBD_CONNECT_BRW_SIZE |
3094 OBD_CONNECT_GRANT | OBD_CONNECT_FULL20 |
3095 OBD_CONNECT_64BITHASH | OBD_CONNECT_LVB_TYPE |
3097 ocd->ocd_brw_size = DT_MAX_BRW_SIZE;
3098 ocd->ocd_version = LUSTRE_VERSION_CODE;
3099 ocd->ocd_group = FID_SEQ_ECHO;
3101 rc = obd_connect(env, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
3103 /* Turn off pinger because it connects to tgt obd directly. */
3104 spin_lock(&tgt->obd_dev_lock);
3105 cfs_list_del_init(&ec->ec_exp->exp_obd_chain_timed);
3106 spin_unlock(&tgt->obd_dev_lock);
3109 OBD_FREE(ocd, sizeof(*ocd));
3112 CERROR("fail to connect to device %s\n",
3113 lustre_cfg_string(lcfg, 1));
3120 static int echo_client_cleanup(struct obd_device *obddev)
3122 struct echo_device *ed = obd2echo_dev(obddev);
3123 struct echo_client_obd *ec = &obddev->u.echo_client;
3127 /*Do nothing for Metadata echo client*/
3131 if (ed->ed_next_ismd) {
3132 #ifdef HAVE_SERVER_SUPPORT
3133 lu_context_tags_clear(ECHO_MD_CTX_TAG);
3134 lu_session_tags_clear(ECHO_MD_SES_TAG);
3136 CERROR("This is client-side only module, does not support "
3137 "metadata echo client.\n");
3142 if (!cfs_list_empty(&obddev->obd_exports)) {
3143 CERROR("still has clients!\n");
3147 LASSERT(cfs_atomic_read(&ec->ec_exp->exp_refcount) > 0);
3148 rc = obd_disconnect(ec->ec_exp);
3150 CERROR("fail to disconnect device: %d\n", rc);
3155 static int echo_client_connect(const struct lu_env *env,
3156 struct obd_export **exp,
3157 struct obd_device *src, struct obd_uuid *cluuid,
3158 struct obd_connect_data *data, void *localdata)
3161 struct lustre_handle conn = { 0 };
3164 rc = class_connect(&conn, src, cluuid);
3166 *exp = class_conn2export(&conn);
3172 static int echo_client_disconnect(struct obd_export *exp)
3175 struct obd_device *obd;
3176 struct echo_client_obd *ec;
3177 struct ec_lock *ecl;
3183 GOTO(out, rc = -EINVAL);
3187 ec = &obd->u.echo_client;
3189 /* no more contention on export's lock list */
3190 while (!cfs_list_empty (&exp->exp_ec_data.eced_locks)) {
3191 ecl = cfs_list_entry (exp->exp_ec_data.eced_locks.next,
3192 struct ec_lock, ecl_exp_chain);
3193 cfs_list_del (&ecl->ecl_exp_chain);
3195 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm,
3196 ecl->ecl_mode, &ecl->ecl_lock_handle);
3198 CDEBUG (D_INFO, "Cancel lock on object "LPX64" on disconnect "
3199 "(%d)\n", ecl->ecl_object->eco_id, rc);
3201 echo_put_object (ecl->ecl_object);
3202 OBD_FREE (ecl, sizeof (*ecl));
3206 rc = class_disconnect(exp);
3212 static struct obd_ops echo_client_obd_ops = {
3213 .o_owner = THIS_MODULE,
3216 .o_setup = echo_client_setup,
3217 .o_cleanup = echo_client_cleanup,
3220 .o_iocontrol = echo_client_iocontrol,
3221 .o_connect = echo_client_connect,
3222 .o_disconnect = echo_client_disconnect
3225 int echo_client_init(void)
3227 struct lprocfs_static_vars lvars = { 0 };
3230 lprocfs_echo_init_vars(&lvars);
3232 rc = lu_kmem_init(echo_caches);
3234 rc = class_register_type(&echo_client_obd_ops, NULL, NULL,
3235 #ifndef HAVE_ONLY_PROCFS_SEQ
3238 LUSTRE_ECHO_CLIENT_NAME,
3241 lu_kmem_fini(echo_caches);
3246 void echo_client_exit(void)
3248 class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
3249 lu_kmem_fini(echo_caches);
3253 static int __init obdecho_init(void)
3255 struct lprocfs_static_vars lvars;
3259 LCONSOLE_INFO("Echo OBD driver; http://www.lustre.org/\n");
3261 LASSERT(PAGE_CACHE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
3263 lprocfs_echo_init_vars(&lvars);
3265 # ifdef HAVE_SERVER_SUPPORT
3266 rc = echo_persistent_pages_init();
3270 rc = class_register_type(&echo_obd_ops, NULL, NULL,
3271 #ifndef HAVE_ONLY_PROCFS_SEQ
3274 LUSTRE_ECHO_NAME, NULL);
3279 rc = echo_client_init();
3281 # ifdef HAVE_SERVER_SUPPORT
3285 class_unregister_type(LUSTRE_ECHO_NAME);
3287 echo_persistent_pages_fini();
3293 static void /*__exit*/ obdecho_exit(void)
3297 # ifdef HAVE_SERVER_SUPPORT
3298 class_unregister_type(LUSTRE_ECHO_NAME);
3299 echo_persistent_pages_fini();
3303 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3304 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
3305 MODULE_LICENSE("GPL");
3307 cfs_module(obdecho, LUSTRE_VERSION_STRING, obdecho_init, obdecho_exit);
3308 #endif /* __KERNEL__ */
3310 /** @} echo_client */