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
38 #include <libcfs/libcfs.h>
41 #include <obd_support.h>
42 #include <obd_class.h>
43 #include <lustre_debug.h>
44 #include <lprocfs_status.h>
45 #include <cl_object.h>
46 #include <lustre_fid.h>
47 #include <lustre_acl.h>
48 #include <lustre_ioctl.h>
49 #include <lustre_net.h>
50 #ifdef HAVE_SERVER_SUPPORT
51 # include <md_object.h>
52 #endif /* HAVE_SERVER_SUPPORT */
54 #include "echo_internal.h"
56 /** \defgroup echo_client Echo Client
61 struct cl_device ed_cl;
62 struct echo_client_obd *ed_ec;
64 struct cl_site ed_site_myself;
65 struct cl_site *ed_site;
66 struct lu_device *ed_next;
69 struct lu_client_seq *ed_cl_seq;
73 struct cl_object eo_cl;
74 struct cl_object_header eo_hdr;
76 struct echo_device *eo_dev;
77 struct list_head eo_obj_chain;
78 struct lov_stripe_md *eo_lsm;
83 struct echo_object_conf {
84 struct cl_object_conf eoc_cl;
85 struct lov_stripe_md **eoc_md;
89 struct cl_page_slice ep_cl;
94 struct cl_lock_slice el_cl;
95 struct list_head el_chain;
96 struct echo_object *el_object;
101 static int echo_client_setup(const struct lu_env *env,
102 struct obd_device *obddev,
103 struct lustre_cfg *lcfg);
104 static int echo_client_cleanup(struct obd_device *obddev);
107 /** \defgroup echo_helpers Helper functions
110 static inline struct echo_device *cl2echo_dev(const struct cl_device *dev)
112 return container_of0(dev, struct echo_device, ed_cl);
115 static inline struct cl_device *echo_dev2cl(struct echo_device *d)
120 static inline struct echo_device *obd2echo_dev(const struct obd_device *obd)
122 return cl2echo_dev(lu2cl_dev(obd->obd_lu_dev));
125 static inline struct cl_object *echo_obj2cl(struct echo_object *eco)
130 static inline struct echo_object *cl2echo_obj(const struct cl_object *o)
132 return container_of(o, struct echo_object, eo_cl);
135 static inline struct echo_page *cl2echo_page(const struct cl_page_slice *s)
137 return container_of(s, struct echo_page, ep_cl);
140 static inline struct echo_lock *cl2echo_lock(const struct cl_lock_slice *s)
142 return container_of(s, struct echo_lock, el_cl);
145 static inline struct cl_lock *echo_lock2cl(const struct echo_lock *ecl)
147 return ecl->el_cl.cls_lock;
150 static struct lu_context_key echo_thread_key;
151 static inline struct echo_thread_info *echo_env_info(const struct lu_env *env)
153 struct echo_thread_info *info;
154 info = lu_context_key_get(&env->le_ctx, &echo_thread_key);
155 LASSERT(info != NULL);
160 struct echo_object_conf *cl2echo_conf(const struct cl_object_conf *c)
162 return container_of(c, struct echo_object_conf, eoc_cl);
165 /** @} echo_helpers */
167 static struct echo_object *cl_echo_object_find(struct echo_device *d,
168 struct lov_stripe_md **lsm);
169 static int cl_echo_object_put(struct echo_object *eco);
170 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
171 struct page **pages, int npages, int async);
173 struct echo_thread_info {
174 struct echo_object_conf eti_conf;
175 struct lustre_md eti_md;
177 struct cl_2queue eti_queue;
179 struct cl_lock_descr eti_descr;
180 struct lu_fid eti_fid;
181 struct lu_fid eti_fid2;
182 #ifdef HAVE_SERVER_SUPPORT
183 struct md_op_spec eti_spec;
184 struct lov_mds_md_v3 eti_lmm;
185 struct lov_user_md_v3 eti_lum;
186 struct md_attr eti_ma;
187 struct lu_name eti_lname;
188 /* per-thread values, can be re-used */
192 struct lu_buf eti_buf;
193 char eti_xattr_buf[LUSTRE_POSIX_ACL_MAX_SIZE];
197 /* No session used right now */
198 struct echo_session_info {
202 static struct kmem_cache *echo_lock_kmem;
203 static struct kmem_cache *echo_object_kmem;
204 static struct kmem_cache *echo_thread_kmem;
205 static struct kmem_cache *echo_session_kmem;
206 /* static struct kmem_cache *echo_req_kmem; */
208 static struct lu_kmem_descr echo_caches[] = {
210 .ckd_cache = &echo_lock_kmem,
211 .ckd_name = "echo_lock_kmem",
212 .ckd_size = sizeof (struct echo_lock)
215 .ckd_cache = &echo_object_kmem,
216 .ckd_name = "echo_object_kmem",
217 .ckd_size = sizeof (struct echo_object)
220 .ckd_cache = &echo_thread_kmem,
221 .ckd_name = "echo_thread_kmem",
222 .ckd_size = sizeof (struct echo_thread_info)
225 .ckd_cache = &echo_session_kmem,
226 .ckd_name = "echo_session_kmem",
227 .ckd_size = sizeof (struct echo_session_info)
234 /** \defgroup echo_page Page operations
236 * Echo page operations.
240 static int echo_page_own(const struct lu_env *env,
241 const struct cl_page_slice *slice,
242 struct cl_io *io, int nonblock)
244 struct echo_page *ep = cl2echo_page(slice);
247 mutex_lock(&ep->ep_lock);
248 else if (!mutex_trylock(&ep->ep_lock))
253 static void echo_page_disown(const struct lu_env *env,
254 const struct cl_page_slice *slice,
257 struct echo_page *ep = cl2echo_page(slice);
259 LASSERT(mutex_is_locked(&ep->ep_lock));
260 mutex_unlock(&ep->ep_lock);
263 static void echo_page_discard(const struct lu_env *env,
264 const struct cl_page_slice *slice,
265 struct cl_io *unused)
267 cl_page_delete(env, slice->cpl_page);
270 static int echo_page_is_vmlocked(const struct lu_env *env,
271 const struct cl_page_slice *slice)
273 if (mutex_is_locked(&cl2echo_page(slice)->ep_lock))
278 static void echo_page_completion(const struct lu_env *env,
279 const struct cl_page_slice *slice,
282 LASSERT(slice->cpl_page->cp_sync_io != NULL);
285 static void echo_page_fini(const struct lu_env *env,
286 struct cl_page_slice *slice)
288 struct echo_object *eco = cl2echo_obj(slice->cpl_obj);
291 atomic_dec(&eco->eo_npages);
292 page_cache_release(slice->cpl_page->cp_vmpage);
296 static int echo_page_prep(const struct lu_env *env,
297 const struct cl_page_slice *slice,
298 struct cl_io *unused)
303 static int echo_page_print(const struct lu_env *env,
304 const struct cl_page_slice *slice,
305 void *cookie, lu_printer_t printer)
307 struct echo_page *ep = cl2echo_page(slice);
309 (*printer)(env, cookie, LUSTRE_ECHO_CLIENT_NAME"-page@%p %d vm@%p\n",
310 ep, mutex_is_locked(&ep->ep_lock),
311 slice->cpl_page->cp_vmpage);
315 static const struct cl_page_operations echo_page_ops = {
316 .cpo_own = echo_page_own,
317 .cpo_disown = echo_page_disown,
318 .cpo_discard = echo_page_discard,
319 .cpo_fini = echo_page_fini,
320 .cpo_print = echo_page_print,
321 .cpo_is_vmlocked = echo_page_is_vmlocked,
324 .cpo_prep = echo_page_prep,
325 .cpo_completion = echo_page_completion,
328 .cpo_prep = echo_page_prep,
329 .cpo_completion = echo_page_completion,
335 /** \defgroup echo_lock Locking
337 * echo lock operations
341 static void echo_lock_fini(const struct lu_env *env,
342 struct cl_lock_slice *slice)
344 struct echo_lock *ecl = cl2echo_lock(slice);
346 LASSERT(list_empty(&ecl->el_chain));
347 OBD_SLAB_FREE_PTR(ecl, echo_lock_kmem);
350 static void echo_lock_delete(const struct lu_env *env,
351 const struct cl_lock_slice *slice)
353 struct echo_lock *ecl = cl2echo_lock(slice);
355 LASSERT(list_empty(&ecl->el_chain));
358 static int echo_lock_fits_into(const struct lu_env *env,
359 const struct cl_lock_slice *slice,
360 const struct cl_lock_descr *need,
361 const struct cl_io *unused)
366 static struct cl_lock_operations echo_lock_ops = {
367 .clo_fini = echo_lock_fini,
368 .clo_delete = echo_lock_delete,
369 .clo_fits_into = echo_lock_fits_into
374 /** \defgroup echo_cl_ops cl_object operations
376 * operations for cl_object
380 static int echo_page_init(const struct lu_env *env, struct cl_object *obj,
381 struct cl_page *page, pgoff_t index)
383 struct echo_page *ep = cl_object_page_slice(obj, page);
384 struct echo_object *eco = cl2echo_obj(obj);
387 page_cache_get(page->cp_vmpage);
388 mutex_init(&ep->ep_lock);
389 cl_page_slice_add(page, &ep->ep_cl, obj, index, &echo_page_ops);
390 atomic_inc(&eco->eo_npages);
394 static int echo_io_init(const struct lu_env *env, struct cl_object *obj,
400 static int echo_lock_init(const struct lu_env *env,
401 struct cl_object *obj, struct cl_lock *lock,
402 const struct cl_io *unused)
404 struct echo_lock *el;
407 OBD_SLAB_ALLOC_PTR_GFP(el, echo_lock_kmem, GFP_NOFS);
409 cl_lock_slice_add(lock, &el->el_cl, obj, &echo_lock_ops);
410 el->el_object = cl2echo_obj(obj);
411 INIT_LIST_HEAD(&el->el_chain);
412 atomic_set(&el->el_refcount, 0);
414 RETURN(el == NULL ? -ENOMEM : 0);
417 static int echo_conf_set(const struct lu_env *env, struct cl_object *obj,
418 const struct cl_object_conf *conf)
423 static const struct cl_object_operations echo_cl_obj_ops = {
424 .coo_page_init = echo_page_init,
425 .coo_lock_init = echo_lock_init,
426 .coo_io_init = echo_io_init,
427 .coo_conf_set = echo_conf_set
429 /** @} echo_cl_ops */
431 /** \defgroup echo_lu_ops lu_object operations
433 * operations for echo lu object.
437 static int echo_object_init(const struct lu_env *env, struct lu_object *obj,
438 const struct lu_object_conf *conf)
440 struct echo_device *ed = cl2echo_dev(lu2cl_dev(obj->lo_dev));
441 struct echo_client_obd *ec = ed->ed_ec;
442 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
446 struct lu_object *below;
447 struct lu_device *under;
450 below = under->ld_ops->ldo_object_alloc(env, obj->lo_header,
454 lu_object_add(obj, below);
457 if (!ed->ed_next_ismd) {
458 const struct cl_object_conf *cconf = lu2cl_conf(conf);
459 struct echo_object_conf *econf = cl2echo_conf(cconf);
461 LASSERT(econf->eoc_md);
462 eco->eo_lsm = *econf->eoc_md;
463 /* clear the lsm pointer so that it won't get freed. */
464 *econf->eoc_md = NULL;
470 atomic_set(&eco->eo_npages, 0);
471 cl_object_page_init(lu2cl(obj), sizeof(struct echo_page));
473 spin_lock(&ec->ec_lock);
474 list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
475 spin_unlock(&ec->ec_lock);
480 /* taken from osc_unpackmd() */
481 static int echo_alloc_memmd(struct echo_device *ed,
482 struct lov_stripe_md **lsmp)
488 /* If export is lov/osc then use their obd method */
489 if (ed->ed_next != NULL)
490 return obd_alloc_memmd(ed->ed_ec->ec_exp, lsmp);
491 /* OFD has no unpackmd method, do everything here */
492 lsm_size = lov_stripe_md_size(1);
494 LASSERT(*lsmp == NULL);
495 OBD_ALLOC(*lsmp, lsm_size);
499 OBD_ALLOC((*lsmp)->lsm_oinfo[0], sizeof(struct lov_oinfo));
500 if ((*lsmp)->lsm_oinfo[0] == NULL) {
501 OBD_FREE(*lsmp, lsm_size);
505 loi_init((*lsmp)->lsm_oinfo[0]);
506 (*lsmp)->lsm_maxbytes = LUSTRE_EXT3_STRIPE_MAXBYTES;
507 ostid_set_seq_echo(&(*lsmp)->lsm_oi);
512 static int echo_free_memmd(struct echo_device *ed, struct lov_stripe_md **lsmp)
518 /* If export is lov/osc then use their obd method */
519 if (ed->ed_next != NULL)
520 return obd_free_memmd(ed->ed_ec->ec_exp, lsmp);
521 /* OFD has no unpackmd method, do everything here */
522 lsm_size = lov_stripe_md_size(1);
524 LASSERT(*lsmp != NULL);
525 OBD_FREE((*lsmp)->lsm_oinfo[0], sizeof(struct lov_oinfo));
526 OBD_FREE(*lsmp, lsm_size);
531 static void echo_object_free(const struct lu_env *env, struct lu_object *obj)
533 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
534 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
537 LASSERT(atomic_read(&eco->eo_npages) == 0);
539 spin_lock(&ec->ec_lock);
540 list_del_init(&eco->eo_obj_chain);
541 spin_unlock(&ec->ec_lock);
544 lu_object_header_fini(obj->lo_header);
547 echo_free_memmd(eco->eo_dev, &eco->eo_lsm);
548 OBD_SLAB_FREE_PTR(eco, echo_object_kmem);
552 static int echo_object_print(const struct lu_env *env, void *cookie,
553 lu_printer_t p, const struct lu_object *o)
555 struct echo_object *obj = cl2echo_obj(lu2cl(o));
557 return (*p)(env, cookie, "echoclient-object@%p", obj);
560 static const struct lu_object_operations echo_lu_obj_ops = {
561 .loo_object_init = echo_object_init,
562 .loo_object_delete = NULL,
563 .loo_object_release = NULL,
564 .loo_object_free = echo_object_free,
565 .loo_object_print = echo_object_print,
566 .loo_object_invariant = NULL
568 /** @} echo_lu_ops */
570 /** \defgroup echo_lu_dev_ops lu_device operations
572 * Operations for echo lu device.
576 static struct lu_object *echo_object_alloc(const struct lu_env *env,
577 const struct lu_object_header *hdr,
578 struct lu_device *dev)
580 struct echo_object *eco;
581 struct lu_object *obj = NULL;
584 /* we're the top dev. */
585 LASSERT(hdr == NULL);
586 OBD_SLAB_ALLOC_PTR_GFP(eco, echo_object_kmem, GFP_NOFS);
588 struct cl_object_header *hdr = &eco->eo_hdr;
590 obj = &echo_obj2cl(eco)->co_lu;
591 cl_object_header_init(hdr);
592 hdr->coh_page_bufsize = cfs_size_round(sizeof(struct cl_page));
594 lu_object_init(obj, &hdr->coh_lu, dev);
595 lu_object_add_top(&hdr->coh_lu, obj);
597 eco->eo_cl.co_ops = &echo_cl_obj_ops;
598 obj->lo_ops = &echo_lu_obj_ops;
603 static struct lu_device_operations echo_device_lu_ops = {
604 .ldo_object_alloc = echo_object_alloc,
607 /** @} echo_lu_dev_ops */
609 static struct cl_device_operations echo_device_cl_ops = {
612 /** \defgroup echo_init Setup and teardown
614 * Init and fini functions for echo client.
618 static int echo_site_init(const struct lu_env *env, struct echo_device *ed)
620 struct cl_site *site = &ed->ed_site_myself;
623 /* initialize site */
624 rc = cl_site_init(site, &ed->ed_cl);
626 CERROR("Cannot initilize site for echo client(%d)\n", rc);
630 rc = lu_site_init_finish(&site->cs_lu);
638 static void echo_site_fini(const struct lu_env *env, struct echo_device *ed)
641 if (!ed->ed_next_ismd)
642 cl_site_fini(ed->ed_site);
647 static void *echo_thread_key_init(const struct lu_context *ctx,
648 struct lu_context_key *key)
650 struct echo_thread_info *info;
652 OBD_SLAB_ALLOC_PTR_GFP(info, echo_thread_kmem, GFP_NOFS);
654 info = ERR_PTR(-ENOMEM);
658 static void echo_thread_key_fini(const struct lu_context *ctx,
659 struct lu_context_key *key, void *data)
661 struct echo_thread_info *info = data;
662 OBD_SLAB_FREE_PTR(info, echo_thread_kmem);
665 static void echo_thread_key_exit(const struct lu_context *ctx,
666 struct lu_context_key *key, void *data)
670 static struct lu_context_key echo_thread_key = {
671 .lct_tags = LCT_CL_THREAD,
672 .lct_init = echo_thread_key_init,
673 .lct_fini = echo_thread_key_fini,
674 .lct_exit = echo_thread_key_exit
677 static void *echo_session_key_init(const struct lu_context *ctx,
678 struct lu_context_key *key)
680 struct echo_session_info *session;
682 OBD_SLAB_ALLOC_PTR_GFP(session, echo_session_kmem, GFP_NOFS);
684 session = ERR_PTR(-ENOMEM);
688 static void echo_session_key_fini(const struct lu_context *ctx,
689 struct lu_context_key *key, void *data)
691 struct echo_session_info *session = data;
692 OBD_SLAB_FREE_PTR(session, echo_session_kmem);
695 static void echo_session_key_exit(const struct lu_context *ctx,
696 struct lu_context_key *key, void *data)
700 static struct lu_context_key echo_session_key = {
701 .lct_tags = LCT_SESSION,
702 .lct_init = echo_session_key_init,
703 .lct_fini = echo_session_key_fini,
704 .lct_exit = echo_session_key_exit
707 LU_TYPE_INIT_FINI(echo, &echo_thread_key, &echo_session_key);
709 #ifdef HAVE_SERVER_SUPPORT
710 # define ECHO_SEQ_WIDTH 0xffffffff
711 static int echo_fid_init(struct echo_device *ed, char *obd_name,
712 struct seq_server_site *ss)
718 OBD_ALLOC_PTR(ed->ed_cl_seq);
719 if (ed->ed_cl_seq == NULL)
722 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
724 GOTO(out_free_seq, rc = -ENOMEM);
726 snprintf(prefix, MAX_OBD_NAME + 5, "srv-%s", obd_name);
728 /* Init client side sequence-manager */
729 rc = seq_client_init(ed->ed_cl_seq, NULL,
731 prefix, ss->ss_server_seq);
732 ed->ed_cl_seq->lcs_width = ECHO_SEQ_WIDTH;
733 OBD_FREE(prefix, MAX_OBD_NAME + 5);
735 GOTO(out_free_seq, rc);
740 OBD_FREE_PTR(ed->ed_cl_seq);
741 ed->ed_cl_seq = NULL;
745 static int echo_fid_fini(struct obd_device *obddev)
747 struct echo_device *ed = obd2echo_dev(obddev);
750 if (ed->ed_cl_seq != NULL) {
751 seq_client_fini(ed->ed_cl_seq);
752 OBD_FREE_PTR(ed->ed_cl_seq);
753 ed->ed_cl_seq = NULL;
758 #endif /* HAVE_SERVER_SUPPORT */
760 static struct lu_device *echo_device_alloc(const struct lu_env *env,
761 struct lu_device_type *t,
762 struct lustre_cfg *cfg)
764 struct lu_device *next;
765 struct echo_device *ed;
766 struct cl_device *cd;
767 struct obd_device *obd = NULL; /* to keep compiler happy */
768 struct obd_device *tgt;
769 const char *tgt_type_name;
776 GOTO(out, rc = -ENOMEM);
780 rc = cl_device_init(cd, t);
784 cd->cd_lu_dev.ld_ops = &echo_device_lu_ops;
785 cd->cd_ops = &echo_device_cl_ops;
788 obd = class_name2obd(lustre_cfg_string(cfg, 0));
789 LASSERT(obd != NULL);
790 LASSERT(env != NULL);
792 tgt = class_name2obd(lustre_cfg_string(cfg, 1));
794 CERROR("Can not find tgt device %s\n",
795 lustre_cfg_string(cfg, 1));
796 GOTO(out, rc = -ENODEV);
799 next = tgt->obd_lu_dev;
800 if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
801 ed->ed_next_ismd = 1;
803 ed->ed_next_ismd = 0;
804 rc = echo_site_init(env, ed);
810 rc = echo_client_setup(env, obd, cfg);
814 ed->ed_ec = &obd->u.echo_client;
817 if (ed->ed_next_ismd) {
818 #ifdef HAVE_SERVER_SUPPORT
819 /* Suppose to connect to some Metadata layer */
821 struct lu_device *ld;
825 CERROR("%s is not lu device type!\n",
826 lustre_cfg_string(cfg, 1));
827 GOTO(out, rc = -EINVAL);
830 tgt_type_name = lustre_cfg_string(cfg, 2);
831 if (!tgt_type_name) {
832 CERROR("%s no type name for echo %s setup\n",
833 lustre_cfg_string(cfg, 1),
834 tgt->obd_type->typ_name);
835 GOTO(out, rc = -EINVAL);
840 spin_lock(&ls->ls_ld_lock);
841 list_for_each_entry(ld, &ls->ls_ld_linkage, ld_linkage) {
842 if (strcmp(ld->ld_type->ldt_name, tgt_type_name) == 0) {
847 spin_unlock(&ls->ls_ld_lock);
850 CERROR("%s is not lu device type!\n",
851 lustre_cfg_string(cfg, 1));
852 GOTO(out, rc = -EINVAL);
856 /* For MD echo client, it will use the site in MDS stack */
857 ed->ed_site_myself.cs_lu = *ls;
858 ed->ed_site = &ed->ed_site_myself;
859 ed->ed_cl.cd_lu_dev.ld_site = &ed->ed_site_myself.cs_lu;
860 rc = echo_fid_init(ed, obd->obd_name, lu_site2seq(ls));
862 CERROR("echo fid init error %d\n", rc);
865 #else /* !HAVE_SERVER_SUPPORT */
866 CERROR("Local operations are NOT supported on client side. "
867 "Only remote operations are supported. Metadata client "
868 "must be run on server side.\n");
869 GOTO(out, rc = -EOPNOTSUPP);
872 /* if echo client is to be stacked upon ost device, the next is
873 * NULL since ost is not a clio device so far */
874 if (next != NULL && !lu_device_is_cl(next))
877 tgt_type_name = tgt->obd_type->typ_name;
879 LASSERT(next != NULL);
880 if (next->ld_site != NULL)
881 GOTO(out, rc = -EBUSY);
883 next->ld_site = &ed->ed_site->cs_lu;
884 rc = next->ld_type->ldt_ops->ldto_device_init(env, next,
885 next->ld_type->ldt_name,
890 /* Tricky case, I have to determine the obd type since
891 * CLIO uses the different parameters to initialize
892 * objects for lov & osc. */
893 if (strcmp(tgt_type_name, LUSTRE_LOV_NAME) == 0)
894 ed->ed_next_islov = 1;
896 LASSERT(strcmp(tgt_type_name,
897 LUSTRE_OSC_NAME) == 0);
899 LASSERT(strcmp(tgt_type_name, LUSTRE_OST_NAME) == 0);
903 RETURN(&cd->cd_lu_dev);
908 rc2 = echo_client_cleanup(obd);
910 CERROR("Cleanup obd device %s error(%d)\n",
915 echo_site_fini(env, ed);
917 cl_device_fini(&ed->ed_cl);
927 static int echo_device_init(const struct lu_env *env, struct lu_device *d,
928 const char *name, struct lu_device *next)
934 static struct lu_device *echo_device_fini(const struct lu_env *env,
937 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
938 struct lu_device *next = ed->ed_next;
940 while (next && !ed->ed_next_ismd)
941 next = next->ld_type->ldt_ops->ldto_device_fini(env, next);
945 static void echo_lock_release(const struct lu_env *env,
946 struct echo_lock *ecl,
949 struct cl_lock *clk = echo_lock2cl(ecl);
953 cl_lock_release(env, clk, "ec enqueue", ecl->el_object);
955 cl_lock_mutex_get(env, clk);
956 cl_lock_cancel(env, clk);
957 cl_lock_delete(env, clk);
958 cl_lock_mutex_put(env, clk);
960 cl_lock_put(env, clk);
963 static struct lu_device *echo_device_free(const struct lu_env *env,
966 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
967 struct echo_client_obd *ec = ed->ed_ec;
968 struct echo_object *eco;
969 struct lu_device *next = ed->ed_next;
971 CDEBUG(D_INFO, "echo device:%p is going to be freed, next = %p\n",
974 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
976 /* check if there are objects still alive.
977 * It shouldn't have any object because lu_site_purge would cleanup
978 * all of cached objects. Anyway, probably the echo device is being
979 * parallelly accessed.
981 spin_lock(&ec->ec_lock);
982 list_for_each_entry(eco, &ec->ec_objects, eo_obj_chain)
984 spin_unlock(&ec->ec_lock);
987 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
990 "Waiting for the reference of echo object to be dropped\n");
992 /* Wait for the last reference to be dropped. */
993 spin_lock(&ec->ec_lock);
994 while (!list_empty(&ec->ec_objects)) {
995 spin_unlock(&ec->ec_lock);
996 CERROR("echo_client still has objects at cleanup time, "
997 "wait for 1 second\n");
998 schedule_timeout_and_set_state(TASK_UNINTERRUPTIBLE,
999 cfs_time_seconds(1));
1000 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
1001 spin_lock(&ec->ec_lock);
1003 spin_unlock(&ec->ec_lock);
1005 LASSERT(list_empty(&ec->ec_locks));
1007 CDEBUG(D_INFO, "No object exists, exiting...\n");
1009 echo_client_cleanup(d->ld_obd);
1010 #ifdef HAVE_SERVER_SUPPORT
1011 echo_fid_fini(d->ld_obd);
1013 while (next && !ed->ed_next_ismd)
1014 next = next->ld_type->ldt_ops->ldto_device_free(env, next);
1016 LASSERT(ed->ed_site == lu2cl_site(d->ld_site));
1017 echo_site_fini(env, ed);
1018 cl_device_fini(&ed->ed_cl);
1024 static const struct lu_device_type_operations echo_device_type_ops = {
1025 .ldto_init = echo_type_init,
1026 .ldto_fini = echo_type_fini,
1028 .ldto_start = echo_type_start,
1029 .ldto_stop = echo_type_stop,
1031 .ldto_device_alloc = echo_device_alloc,
1032 .ldto_device_free = echo_device_free,
1033 .ldto_device_init = echo_device_init,
1034 .ldto_device_fini = echo_device_fini
1037 static struct lu_device_type echo_device_type = {
1038 .ldt_tags = LU_DEVICE_CL,
1039 .ldt_name = LUSTRE_ECHO_CLIENT_NAME,
1040 .ldt_ops = &echo_device_type_ops,
1041 .ldt_ctx_tags = LCT_CL_THREAD | LCT_MD_THREAD | LCT_DT_THREAD,
1045 /** \defgroup echo_exports Exported operations
1047 * exporting functions to echo client
1052 /* Interfaces to echo client obd device */
1053 static struct echo_object *cl_echo_object_find(struct echo_device *d,
1054 struct lov_stripe_md **lsmp)
1057 struct echo_thread_info *info;
1058 struct echo_object_conf *conf;
1059 struct lov_stripe_md *lsm;
1060 struct echo_object *eco;
1061 struct cl_object *obj;
1070 LASSERTF(ostid_id(&lsm->lsm_oi) != 0, DOSTID"\n", POSTID(&lsm->lsm_oi));
1071 LASSERTF(ostid_seq(&lsm->lsm_oi) == FID_SEQ_ECHO, DOSTID"\n",
1072 POSTID(&lsm->lsm_oi));
1074 /* Never return an object if the obd is to be freed. */
1075 if (echo_dev2cl(d)->cd_lu_dev.ld_obd->obd_stopping)
1076 RETURN(ERR_PTR(-ENODEV));
1078 env = cl_env_get(&refcheck);
1080 RETURN((void *)env);
1082 info = echo_env_info(env);
1083 conf = &info->eti_conf;
1085 if (!d->ed_next_islov) {
1086 struct lov_oinfo *oinfo = lsm->lsm_oinfo[0];
1087 LASSERT(oinfo != NULL);
1088 oinfo->loi_oi = lsm->lsm_oi;
1089 conf->eoc_cl.u.coc_oinfo = oinfo;
1091 struct lustre_md *md;
1093 memset(md, 0, sizeof *md);
1095 conf->eoc_cl.u.coc_md = md;
1098 conf->eoc_md = lsmp;
1100 fid = &info->eti_fid;
1101 rc = ostid_to_fid(fid, &lsm->lsm_oi, 0);
1103 GOTO(out, eco = ERR_PTR(rc));
1105 /* In the function below, .hs_keycmp resolves to
1106 * lu_obj_hop_keycmp() */
1107 /* coverity[overrun-buffer-val] */
1108 obj = cl_object_find(env, echo_dev2cl(d), fid, &conf->eoc_cl);
1110 GOTO(out, eco = (void*)obj);
1112 eco = cl2echo_obj(obj);
1113 if (eco->eo_deleted) {
1114 cl_object_put(env, obj);
1115 eco = ERR_PTR(-EAGAIN);
1119 cl_env_put(env, &refcheck);
1123 static int cl_echo_object_put(struct echo_object *eco)
1126 struct cl_object *obj = echo_obj2cl(eco);
1130 env = cl_env_get(&refcheck);
1132 RETURN(PTR_ERR(env));
1134 /* an external function to kill an object? */
1135 if (eco->eo_deleted) {
1136 struct lu_object_header *loh = obj->co_lu.lo_header;
1137 LASSERT(&eco->eo_hdr == luh2coh(loh));
1138 set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
1141 cl_object_put(env, obj);
1142 cl_env_put(env, &refcheck);
1146 static int cl_echo_enqueue0(struct lu_env *env, struct echo_object *eco,
1147 obd_off start, obd_off end, int mode,
1148 __u64 *cookie , __u32 enqflags)
1151 struct cl_lock *lck;
1152 struct cl_object *obj;
1153 struct cl_lock_descr *descr;
1154 struct echo_thread_info *info;
1158 info = echo_env_info(env);
1160 descr = &info->eti_descr;
1161 obj = echo_obj2cl(eco);
1163 descr->cld_obj = obj;
1164 descr->cld_start = cl_index(obj, start);
1165 descr->cld_end = cl_index(obj, end);
1166 descr->cld_mode = mode == LCK_PW ? CLM_WRITE : CLM_READ;
1167 descr->cld_enq_flags = enqflags;
1170 lck = cl_lock_request(env, io, descr, "ec enqueue", eco);
1172 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
1173 struct echo_lock *el;
1175 rc = cl_wait(env, lck);
1177 el = cl2echo_lock(cl_lock_at(lck, &echo_device_type));
1178 spin_lock(&ec->ec_lock);
1179 if (list_empty(&el->el_chain)) {
1180 list_add(&el->el_chain, &ec->ec_locks);
1181 el->el_cookie = ++ec->ec_unique;
1183 atomic_inc(&el->el_refcount);
1184 *cookie = el->el_cookie;
1185 spin_unlock(&ec->ec_lock);
1187 cl_lock_release(env, lck, "ec enqueue", current);
1193 static int cl_echo_cancel0(struct lu_env *env, struct echo_device *ed,
1196 struct echo_client_obd *ec = ed->ed_ec;
1197 struct echo_lock *ecl = NULL;
1198 struct list_head *el;
1199 int found = 0, still_used = 0;
1202 LASSERT(ec != NULL);
1203 spin_lock(&ec->ec_lock);
1204 list_for_each(el, &ec->ec_locks) {
1205 ecl = list_entry(el, struct echo_lock, el_chain);
1206 CDEBUG(D_INFO, "ecl: %p, cookie: "LPX64"\n", ecl, ecl->el_cookie);
1207 found = (ecl->el_cookie == cookie);
1209 if (atomic_dec_and_test(&ecl->el_refcount))
1210 list_del_init(&ecl->el_chain);
1216 spin_unlock(&ec->ec_lock);
1221 echo_lock_release(env, ecl, still_used);
1225 static void echo_commit_callback(const struct lu_env *env, struct cl_io *io,
1226 struct cl_page *page)
1228 struct echo_thread_info *info;
1229 struct cl_2queue *queue;
1231 info = echo_env_info(env);
1232 LASSERT(io == &info->eti_io);
1234 queue = &info->eti_queue;
1235 cl_page_list_add(&queue->c2_qout, page);
1238 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
1239 struct page **pages, int npages, int async)
1242 struct echo_thread_info *info;
1243 struct cl_object *obj = echo_obj2cl(eco);
1244 struct echo_device *ed = eco->eo_dev;
1245 struct cl_2queue *queue;
1247 struct cl_page *clp;
1248 struct lustre_handle lh = { 0 };
1249 int page_size = cl_page_size(obj);
1255 LASSERT((offset & ~CFS_PAGE_MASK) == 0);
1256 LASSERT(ed->ed_next != NULL);
1257 env = cl_env_get(&refcheck);
1259 RETURN(PTR_ERR(env));
1261 info = echo_env_info(env);
1263 queue = &info->eti_queue;
1265 cl_2queue_init(queue);
1267 io->ci_ignore_layout = 1;
1268 rc = cl_io_init(env, io, CIT_MISC, obj);
1274 rc = cl_echo_enqueue0(env, eco, offset,
1275 offset + npages * PAGE_CACHE_SIZE - 1,
1276 rw == READ ? LCK_PR : LCK_PW, &lh.cookie,
1279 GOTO(error_lock, rc);
1281 for (i = 0; i < npages; i++) {
1283 clp = cl_page_find(env, obj, cl_index(obj, offset),
1284 pages[i], CPT_TRANSIENT);
1289 LASSERT(clp->cp_type == CPT_TRANSIENT);
1291 rc = cl_page_own(env, io, clp);
1293 LASSERT(clp->cp_state == CPS_FREEING);
1294 cl_page_put(env, clp);
1298 cl_2queue_add(queue, clp);
1300 /* drop the reference count for cl_page_find, so that the page
1301 * will be freed in cl_2queue_fini. */
1302 cl_page_put(env, clp);
1303 cl_page_clip(env, clp, 0, page_size);
1305 offset += page_size;
1309 enum cl_req_type typ = rw == READ ? CRT_READ : CRT_WRITE;
1311 async = async && (typ == CRT_WRITE);
1313 rc = cl_io_commit_async(env, io, &queue->c2_qin,
1315 echo_commit_callback);
1317 rc = cl_io_submit_sync(env, io, typ, queue, 0);
1318 CDEBUG(D_INFO, "echo_client %s write returns %d\n",
1319 async ? "async" : "sync", rc);
1322 cl_echo_cancel0(env, ed, lh.cookie);
1325 cl_2queue_discard(env, io, queue);
1326 cl_2queue_disown(env, io, queue);
1327 cl_2queue_fini(env, queue);
1328 cl_io_fini(env, io);
1330 cl_env_put(env, &refcheck);
1333 /** @} echo_exports */
1336 static obd_id last_object_id;
1339 echo_copyin_lsm (struct echo_device *ed, struct lov_stripe_md *lsm,
1340 void __user *ulsm, int ulsm_nob)
1342 struct echo_client_obd *ec = ed->ed_ec;
1345 if (ulsm_nob < sizeof (*lsm))
1348 if (copy_from_user (lsm, ulsm, sizeof (*lsm)))
1351 if (lsm->lsm_stripe_count > ec->ec_nstripes ||
1352 lsm->lsm_magic != LOV_MAGIC ||
1353 (lsm->lsm_stripe_size & (~CFS_PAGE_MASK)) != 0 ||
1354 ((__u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count > ~0UL))
1358 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1359 if (copy_from_user(lsm->lsm_oinfo[i],
1360 ((struct lov_stripe_md *)ulsm)-> \
1362 sizeof(lsm->lsm_oinfo[0])))
1368 #ifdef HAVE_SERVER_SUPPORT
1369 static inline void echo_md_build_name(struct lu_name *lname, char *name,
1372 sprintf(name, LPU64, id);
1373 lname->ln_name = name;
1374 lname->ln_namelen = strlen(name);
1377 /* similar to mdt_attr_get_complex */
1378 static int echo_big_lmm_get(const struct lu_env *env, struct md_object *o,
1381 struct echo_thread_info *info = echo_env_info(env);
1386 LASSERT(ma->ma_lmm_size > 0);
1388 rc = mo_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LOV);
1392 /* big_lmm may need to be grown */
1393 if (info->eti_big_lmmsize < rc) {
1394 int size = size_roundup_power2(rc);
1396 if (info->eti_big_lmmsize > 0) {
1397 /* free old buffer */
1398 LASSERT(info->eti_big_lmm);
1399 OBD_FREE_LARGE(info->eti_big_lmm,
1400 info->eti_big_lmmsize);
1401 info->eti_big_lmm = NULL;
1402 info->eti_big_lmmsize = 0;
1405 OBD_ALLOC_LARGE(info->eti_big_lmm, size);
1406 if (info->eti_big_lmm == NULL)
1408 info->eti_big_lmmsize = size;
1410 LASSERT(info->eti_big_lmmsize >= rc);
1412 info->eti_buf.lb_buf = info->eti_big_lmm;
1413 info->eti_buf.lb_len = info->eti_big_lmmsize;
1414 rc = mo_xattr_get(env, o, &info->eti_buf, XATTR_NAME_LOV);
1418 ma->ma_valid |= MA_LOV;
1419 ma->ma_lmm = info->eti_big_lmm;
1420 ma->ma_lmm_size = rc;
1425 static int echo_attr_get_complex(const struct lu_env *env,
1426 struct md_object *next,
1429 struct echo_thread_info *info = echo_env_info(env);
1430 struct lu_buf *buf = &info->eti_buf;
1431 umode_t mode = lu_object_attr(&next->mo_lu);
1432 int need = ma->ma_need;
1439 if (need & MA_INODE) {
1440 ma->ma_need = MA_INODE;
1441 rc = mo_attr_get(env, next, ma);
1444 ma->ma_valid |= MA_INODE;
1447 if (need & MA_LOV) {
1448 if (S_ISREG(mode) || S_ISDIR(mode)) {
1449 LASSERT(ma->ma_lmm_size > 0);
1450 buf->lb_buf = ma->ma_lmm;
1451 buf->lb_len = ma->ma_lmm_size;
1452 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LOV);
1454 ma->ma_lmm_size = rc2;
1455 ma->ma_valid |= MA_LOV;
1456 } else if (rc2 == -ENODATA) {
1458 ma->ma_lmm_size = 0;
1459 } else if (rc2 == -ERANGE) {
1460 rc2 = echo_big_lmm_get(env, next, ma);
1462 GOTO(out, rc = rc2);
1464 GOTO(out, rc = rc2);
1469 #ifdef CONFIG_FS_POSIX_ACL
1470 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
1471 buf->lb_buf = ma->ma_acl;
1472 buf->lb_len = ma->ma_acl_size;
1473 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1475 ma->ma_acl_size = rc2;
1476 ma->ma_valid |= MA_ACL_DEF;
1477 } else if (rc2 == -ENODATA) {
1479 ma->ma_acl_size = 0;
1481 GOTO(out, rc = rc2);
1487 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
1488 rc, ma->ma_valid, ma->ma_lmm);
1493 echo_md_create_internal(const struct lu_env *env, struct echo_device *ed,
1494 struct md_object *parent, struct lu_fid *fid,
1495 struct lu_name *lname, struct md_op_spec *spec,
1498 struct lu_object *ec_child, *child;
1499 struct lu_device *ld = ed->ed_next;
1500 struct echo_thread_info *info = echo_env_info(env);
1501 struct lu_fid *fid2 = &info->eti_fid2;
1502 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
1507 rc = mdo_lookup(env, parent, lname, fid2, spec);
1510 else if (rc != -ENOENT)
1513 ec_child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev,
1515 if (IS_ERR(ec_child)) {
1516 CERROR("Can not find the child "DFID": rc = %ld\n", PFID(fid),
1518 RETURN(PTR_ERR(ec_child));
1521 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1522 if (child == NULL) {
1523 CERROR("Can not locate the child "DFID"\n", PFID(fid));
1524 GOTO(out_put, rc = -EINVAL);
1527 CDEBUG(D_RPCTRACE, "Start creating object "DFID" %s %p\n",
1528 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1531 * Do not perform lookup sanity check. We know that name does not exist.
1533 spec->sp_cr_lookup = 0;
1534 rc = mdo_create(env, parent, lname, lu2md(child), spec, ma);
1536 CERROR("Can not create child "DFID": rc = %d\n", PFID(fid), rc);
1539 CDEBUG(D_RPCTRACE, "End creating object "DFID" %s %p rc = %d\n",
1540 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent, rc);
1543 lu_object_put(env, ec_child);
1547 static int echo_set_lmm_size(const struct lu_env *env, struct lu_device *ld,
1550 struct echo_thread_info *info = echo_env_info(env);
1552 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
1553 ma->ma_lmm = (void *)&info->eti_lmm;
1554 ma->ma_lmm_size = sizeof(info->eti_lmm);
1556 LASSERT(info->eti_big_lmmsize);
1557 ma->ma_lmm = info->eti_big_lmm;
1558 ma->ma_lmm_size = info->eti_big_lmmsize;
1564 static int echo_create_md_object(const struct lu_env *env,
1565 struct echo_device *ed,
1566 struct lu_object *ec_parent,
1568 char *name, int namelen,
1569 __u64 id, __u32 mode, int count,
1570 int stripe_count, int stripe_offset)
1572 struct lu_object *parent;
1573 struct echo_thread_info *info = echo_env_info(env);
1574 struct lu_name *lname = &info->eti_lname;
1575 struct md_op_spec *spec = &info->eti_spec;
1576 struct md_attr *ma = &info->eti_ma;
1577 struct lu_device *ld = ed->ed_next;
1583 if (ec_parent == NULL)
1585 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1589 memset(ma, 0, sizeof(*ma));
1590 memset(spec, 0, sizeof(*spec));
1591 if (stripe_count != 0) {
1592 spec->sp_cr_flags |= FMODE_WRITE;
1593 echo_set_lmm_size(env, ld, ma);
1594 if (stripe_count != -1) {
1595 struct lov_user_md_v3 *lum = &info->eti_lum;
1597 lum->lmm_magic = LOV_USER_MAGIC_V3;
1598 lum->lmm_stripe_count = stripe_count;
1599 lum->lmm_stripe_offset = stripe_offset;
1600 lum->lmm_pattern = 0;
1601 spec->u.sp_ea.eadata = lum;
1602 spec->u.sp_ea.eadatalen = sizeof(*lum);
1603 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1607 ma->ma_attr.la_mode = mode;
1608 ma->ma_attr.la_valid = LA_CTIME | LA_MODE;
1609 ma->ma_attr.la_ctime = cfs_time_current_64();
1612 lname->ln_name = name;
1613 lname->ln_namelen = namelen;
1614 /* If name is specified, only create one object by name */
1615 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1620 /* Create multiple object sequenced by id */
1621 for (i = 0; i < count; i++) {
1622 char *tmp_name = info->eti_name;
1624 echo_md_build_name(lname, tmp_name, id);
1626 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1629 CERROR("Can not create child %s: rc = %d\n", tmp_name,
1640 static struct lu_object *echo_md_lookup(const struct lu_env *env,
1641 struct echo_device *ed,
1642 struct md_object *parent,
1643 struct lu_name *lname)
1645 struct echo_thread_info *info = echo_env_info(env);
1646 struct lu_fid *fid = &info->eti_fid;
1647 struct lu_object *child;
1651 CDEBUG(D_INFO, "lookup %s in parent "DFID" %p\n", lname->ln_name,
1653 rc = mdo_lookup(env, parent, lname, fid, NULL);
1655 CERROR("lookup %s: rc = %d\n", lname->ln_name, rc);
1656 RETURN(ERR_PTR(rc));
1659 /* In the function below, .hs_keycmp resolves to
1660 * lu_obj_hop_keycmp() */
1661 /* coverity[overrun-buffer-val] */
1662 child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1667 static int echo_setattr_object(const struct lu_env *env,
1668 struct echo_device *ed,
1669 struct lu_object *ec_parent,
1670 __u64 id, int count)
1672 struct lu_object *parent;
1673 struct echo_thread_info *info = echo_env_info(env);
1674 struct lu_name *lname = &info->eti_lname;
1675 char *name = info->eti_name;
1676 struct lu_device *ld = ed->ed_next;
1677 struct lu_buf *buf = &info->eti_buf;
1683 if (ec_parent == NULL)
1685 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1689 for (i = 0; i < count; i++) {
1690 struct lu_object *ec_child, *child;
1692 echo_md_build_name(lname, name, id);
1694 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1695 if (IS_ERR(ec_child)) {
1696 CERROR("Can't find child %s: rc = %ld\n",
1697 lname->ln_name, PTR_ERR(ec_child));
1698 RETURN(PTR_ERR(ec_child));
1701 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1702 if (child == NULL) {
1703 CERROR("Can not locate the child %s\n", lname->ln_name);
1704 lu_object_put(env, ec_child);
1709 CDEBUG(D_RPCTRACE, "Start setattr object "DFID"\n",
1710 PFID(lu_object_fid(child)));
1712 buf->lb_buf = info->eti_xattr_buf;
1713 buf->lb_len = sizeof(info->eti_xattr_buf);
1715 sprintf(name, "%s.test1", XATTR_USER_PREFIX);
1716 rc = mo_xattr_set(env, lu2md(child), buf, name,
1719 CERROR("Can not setattr child "DFID": rc = %d\n",
1720 PFID(lu_object_fid(child)), rc);
1721 lu_object_put(env, ec_child);
1724 CDEBUG(D_RPCTRACE, "End setattr object "DFID"\n",
1725 PFID(lu_object_fid(child)));
1727 lu_object_put(env, ec_child);
1732 static int echo_getattr_object(const struct lu_env *env,
1733 struct echo_device *ed,
1734 struct lu_object *ec_parent,
1735 __u64 id, int count)
1737 struct lu_object *parent;
1738 struct echo_thread_info *info = echo_env_info(env);
1739 struct lu_name *lname = &info->eti_lname;
1740 char *name = info->eti_name;
1741 struct md_attr *ma = &info->eti_ma;
1742 struct lu_device *ld = ed->ed_next;
1748 if (ec_parent == NULL)
1750 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1754 memset(ma, 0, sizeof(*ma));
1755 ma->ma_need |= MA_INODE | MA_LOV | MA_PFID | MA_HSM | MA_ACL_DEF;
1756 ma->ma_acl = info->eti_xattr_buf;
1757 ma->ma_acl_size = sizeof(info->eti_xattr_buf);
1759 for (i = 0; i < count; i++) {
1760 struct lu_object *ec_child, *child;
1763 echo_md_build_name(lname, name, id);
1764 echo_set_lmm_size(env, ld, ma);
1766 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1767 if (IS_ERR(ec_child)) {
1768 CERROR("Can't find child %s: rc = %ld\n",
1769 lname->ln_name, PTR_ERR(ec_child));
1770 RETURN(PTR_ERR(ec_child));
1773 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1774 if (child == NULL) {
1775 CERROR("Can not locate the child %s\n", lname->ln_name);
1776 lu_object_put(env, ec_child);
1780 CDEBUG(D_RPCTRACE, "Start getattr object "DFID"\n",
1781 PFID(lu_object_fid(child)));
1782 rc = echo_attr_get_complex(env, lu2md(child), ma);
1784 CERROR("Can not getattr child "DFID": rc = %d\n",
1785 PFID(lu_object_fid(child)), rc);
1786 lu_object_put(env, ec_child);
1789 CDEBUG(D_RPCTRACE, "End getattr object "DFID"\n",
1790 PFID(lu_object_fid(child)));
1792 lu_object_put(env, ec_child);
1798 static int echo_lookup_object(const struct lu_env *env,
1799 struct echo_device *ed,
1800 struct lu_object *ec_parent,
1801 __u64 id, int count)
1803 struct lu_object *parent;
1804 struct echo_thread_info *info = echo_env_info(env);
1805 struct lu_name *lname = &info->eti_lname;
1806 char *name = info->eti_name;
1807 struct lu_fid *fid = &info->eti_fid;
1808 struct lu_device *ld = ed->ed_next;
1812 if (ec_parent == NULL)
1814 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1818 /*prepare the requests*/
1819 for (i = 0; i < count; i++) {
1820 echo_md_build_name(lname, name, id);
1822 CDEBUG(D_RPCTRACE, "Start lookup object "DFID" %s %p\n",
1823 PFID(lu_object_fid(parent)), lname->ln_name, parent);
1825 rc = mdo_lookup(env, lu2md(parent), lname, fid, NULL);
1827 CERROR("Can not lookup child %s: rc = %d\n", name, rc);
1830 CDEBUG(D_RPCTRACE, "End lookup object "DFID" %s %p\n",
1831 PFID(lu_object_fid(parent)), lname->ln_name, parent);
1838 static int echo_md_destroy_internal(const struct lu_env *env,
1839 struct echo_device *ed,
1840 struct md_object *parent,
1841 struct lu_name *lname,
1844 struct lu_device *ld = ed->ed_next;
1845 struct lu_object *ec_child;
1846 struct lu_object *child;
1851 ec_child = echo_md_lookup(env, ed, parent, lname);
1852 if (IS_ERR(ec_child)) {
1853 CERROR("Can't find child %s: rc = %ld\n", lname->ln_name,
1855 RETURN(PTR_ERR(ec_child));
1858 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1859 if (child == NULL) {
1860 CERROR("Can not locate the child %s\n", lname->ln_name);
1861 GOTO(out_put, rc = -EINVAL);
1864 if (lu_object_remote(child)) {
1865 CERROR("Can not destroy remote object %s: rc = %d\n",
1866 lname->ln_name, -EPERM);
1867 GOTO(out_put, rc = -EPERM);
1869 CDEBUG(D_RPCTRACE, "Start destroy object "DFID" %s %p\n",
1870 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1872 rc = mdo_unlink(env, parent, lu2md(child), lname, ma, 0);
1874 CERROR("Can not unlink child %s: rc = %d\n",
1875 lname->ln_name, rc);
1878 CDEBUG(D_RPCTRACE, "End destroy object "DFID" %s %p\n",
1879 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1881 lu_object_put(env, ec_child);
1885 static int echo_destroy_object(const struct lu_env *env,
1886 struct echo_device *ed,
1887 struct lu_object *ec_parent,
1888 char *name, int namelen,
1889 __u64 id, __u32 mode,
1892 struct echo_thread_info *info = echo_env_info(env);
1893 struct lu_name *lname = &info->eti_lname;
1894 struct md_attr *ma = &info->eti_ma;
1895 struct lu_device *ld = ed->ed_next;
1896 struct lu_object *parent;
1901 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1905 memset(ma, 0, sizeof(*ma));
1906 ma->ma_attr.la_mode = mode;
1907 ma->ma_attr.la_valid = LA_CTIME;
1908 ma->ma_attr.la_ctime = cfs_time_current_64();
1909 ma->ma_need = MA_INODE;
1913 lname->ln_name = name;
1914 lname->ln_namelen = namelen;
1915 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
1920 /*prepare the requests*/
1921 for (i = 0; i < count; i++) {
1922 char *tmp_name = info->eti_name;
1925 echo_md_build_name(lname, tmp_name, id);
1927 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
1930 CERROR("Can not unlink child %s: rc = %d\n", name, rc);
1939 static struct lu_object *echo_resolve_path(const struct lu_env *env,
1940 struct echo_device *ed, char *path,
1943 struct lu_device *ld = ed->ed_next;
1944 struct md_device *md = lu2md_dev(ld);
1945 struct echo_thread_info *info = echo_env_info(env);
1946 struct lu_fid *fid = &info->eti_fid;
1947 struct lu_name *lname = &info->eti_lname;
1948 struct lu_object *parent = NULL;
1949 struct lu_object *child = NULL;
1953 /*Only support MDD layer right now*/
1954 rc = md->md_ops->mdo_root_get(env, md, fid);
1956 CERROR("get root error: rc = %d\n", rc);
1957 RETURN(ERR_PTR(rc));
1960 /* In the function below, .hs_keycmp resolves to
1961 * lu_obj_hop_keycmp() */
1962 /* coverity[overrun-buffer-val] */
1963 parent = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1964 if (IS_ERR(parent)) {
1965 CERROR("Can not find the parent "DFID": rc = %ld\n",
1966 PFID(fid), PTR_ERR(parent));
1971 struct lu_object *ld_parent;
1974 e = strsep(&path, "/");
1979 if (!path || path[0] == '\0')
1985 lname->ln_namelen = strlen(e);
1987 ld_parent = lu_object_locate(parent->lo_header, ld->ld_type);
1988 if (ld_parent == NULL) {
1989 lu_object_put(env, parent);
1994 child = echo_md_lookup(env, ed, lu2md(ld_parent), lname);
1995 lu_object_put(env, parent);
1996 if (IS_ERR(child)) {
1997 rc = (int)PTR_ERR(child);
1998 CERROR("lookup %s under parent "DFID": rc = %d\n",
1999 lname->ln_name, PFID(lu_object_fid(ld_parent)),
2006 RETURN(ERR_PTR(rc));
2011 static void echo_ucred_init(struct lu_env *env)
2013 struct lu_ucred *ucred = lu_ucred(env);
2015 ucred->uc_valid = UCRED_INVALID;
2017 ucred->uc_suppgids[0] = -1;
2018 ucred->uc_suppgids[1] = -1;
2020 ucred->uc_uid = ucred->uc_o_uid =
2021 from_kuid(&init_user_ns, current_uid());
2022 ucred->uc_gid = ucred->uc_o_gid =
2023 from_kgid(&init_user_ns, current_gid());
2024 ucred->uc_fsuid = ucred->uc_o_fsuid =
2025 from_kuid(&init_user_ns, current_fsuid());
2026 ucred->uc_fsgid = ucred->uc_o_fsgid =
2027 from_kgid(&init_user_ns, current_fsgid());
2028 ucred->uc_cap = cfs_curproc_cap_pack();
2030 /* remove fs privilege for non-root user. */
2031 if (ucred->uc_fsuid)
2032 ucred->uc_cap &= ~CFS_CAP_FS_MASK;
2033 ucred->uc_valid = UCRED_NEW;
2036 static void echo_ucred_fini(struct lu_env *env)
2038 struct lu_ucred *ucred = lu_ucred(env);
2039 ucred->uc_valid = UCRED_INIT;
2042 #define ECHO_MD_CTX_TAG (LCT_REMEMBER | LCT_MD_THREAD)
2043 #define ECHO_MD_SES_TAG (LCT_REMEMBER | LCT_SESSION | LCT_SERVER_SESSION)
2044 static int echo_md_handler(struct echo_device *ed, int command,
2045 char *path, int path_len, __u64 id, int count,
2046 struct obd_ioctl_data *data)
2048 struct echo_thread_info *info;
2049 struct lu_device *ld = ed->ed_next;
2052 struct lu_object *parent;
2054 int namelen = data->ioc_plen2;
2059 CERROR("MD echo client is not being initialized properly\n");
2063 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
2064 CERROR("Only support MDD layer right now!\n");
2068 env = cl_env_get(&refcheck);
2070 RETURN(PTR_ERR(env));
2072 rc = lu_env_refill_by_tags(env, ECHO_MD_CTX_TAG, ECHO_MD_SES_TAG);
2076 /* init big_lmm buffer */
2077 info = echo_env_info(env);
2078 LASSERT(info->eti_big_lmm == NULL);
2079 OBD_ALLOC_LARGE(info->eti_big_lmm, MIN_MD_SIZE);
2080 if (info->eti_big_lmm == NULL)
2081 GOTO(out_env, rc = -ENOMEM);
2082 info->eti_big_lmmsize = MIN_MD_SIZE;
2084 parent = echo_resolve_path(env, ed, path, path_len);
2085 if (IS_ERR(parent)) {
2086 CERROR("Can not resolve the path %s: rc = %ld\n", path,
2088 GOTO(out_free, rc = PTR_ERR(parent));
2092 OBD_ALLOC(name, namelen + 1);
2094 GOTO(out_put, rc = -ENOMEM);
2095 if (copy_from_user(name, data->ioc_pbuf2, namelen))
2096 GOTO(out_name, rc = -EFAULT);
2099 echo_ucred_init(env);
2102 case ECHO_MD_CREATE:
2103 case ECHO_MD_MKDIR: {
2104 struct echo_thread_info *info = echo_env_info(env);
2105 __u32 mode = data->ioc_obdo2.o_mode;
2106 struct lu_fid *fid = &info->eti_fid;
2107 int stripe_count = (int)data->ioc_obdo2.o_misc;
2108 int stripe_index = (int)data->ioc_obdo2.o_stripe_idx;
2110 rc = ostid_to_fid(fid, &data->ioc_obdo1.o_oi, 0);
2114 /* In the function below, .hs_keycmp resolves to
2115 * lu_obj_hop_keycmp() */
2116 /* coverity[overrun-buffer-val] */
2117 rc = echo_create_md_object(env, ed, parent, fid, name, namelen,
2118 id, mode, count, stripe_count,
2122 case ECHO_MD_DESTROY:
2123 case ECHO_MD_RMDIR: {
2124 __u32 mode = data->ioc_obdo2.o_mode;
2126 rc = echo_destroy_object(env, ed, parent, name, namelen,
2130 case ECHO_MD_LOOKUP:
2131 rc = echo_lookup_object(env, ed, parent, id, count);
2133 case ECHO_MD_GETATTR:
2134 rc = echo_getattr_object(env, ed, parent, id, count);
2136 case ECHO_MD_SETATTR:
2137 rc = echo_setattr_object(env, ed, parent, id, count);
2140 CERROR("unknown command %d\n", command);
2144 echo_ucred_fini(env);
2148 OBD_FREE(name, namelen + 1);
2150 lu_object_put(env, parent);
2152 LASSERT(info->eti_big_lmm);
2153 OBD_FREE_LARGE(info->eti_big_lmm, info->eti_big_lmmsize);
2154 info->eti_big_lmm = NULL;
2155 info->eti_big_lmmsize = 0;
2157 cl_env_put(env, &refcheck);
2160 #endif /* HAVE_SERVER_SUPPORT */
2162 static int echo_create_object(const struct lu_env *env, struct echo_device *ed,
2163 int on_target, struct obdo *oa, void __user *ulsm,
2164 int ulsm_nob, struct obd_trans_info *oti)
2166 struct echo_object *eco;
2167 struct echo_client_obd *ec = ed->ed_ec;
2168 struct lov_stripe_md *lsm = NULL;
2173 if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
2174 (on_target || /* set_stripe */
2175 ec->ec_nstripes != 0)) { /* LOV */
2176 CERROR ("No valid oid\n");
2180 rc = echo_alloc_memmd(ed, &lsm);
2182 CERROR("Cannot allocate md: rc = %d\n", rc);
2189 rc = echo_copyin_lsm (ed, lsm, ulsm, ulsm_nob);
2193 if (lsm->lsm_stripe_count == 0)
2194 lsm->lsm_stripe_count = ec->ec_nstripes;
2196 if (lsm->lsm_stripe_size == 0)
2197 lsm->lsm_stripe_size = PAGE_CACHE_SIZE;
2201 /* setup stripes: indices + default ids if required */
2202 for (i = 0; i < lsm->lsm_stripe_count; i++) {
2203 if (ostid_id(&lsm->lsm_oinfo[i]->loi_oi) == 0)
2204 lsm->lsm_oinfo[i]->loi_oi = lsm->lsm_oi;
2206 lsm->lsm_oinfo[i]->loi_ost_idx =
2207 (idx + i) % ec->ec_nstripes;
2211 /* setup object ID here for !on_target and LOV hint */
2212 if (oa->o_valid & OBD_MD_FLID) {
2213 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2214 lsm->lsm_oi = oa->o_oi;
2217 if (ostid_id(&lsm->lsm_oi) == 0)
2218 ostid_set_id(&lsm->lsm_oi, ++last_object_id);
2222 /* Only echo objects are allowed to be created */
2223 LASSERT((oa->o_valid & OBD_MD_FLGROUP) &&
2224 (ostid_seq(&oa->o_oi) == FID_SEQ_ECHO));
2225 rc = obd_create(env, ec->ec_exp, oa, &lsm, oti);
2227 CERROR("Cannot create objects: rc = %d\n", rc);
2233 /* See what object ID we were given */
2234 oa->o_oi = lsm->lsm_oi;
2235 oa->o_valid |= OBD_MD_FLID;
2237 eco = cl_echo_object_find(ed, &lsm);
2239 GOTO(failed, rc = PTR_ERR(eco));
2240 cl_echo_object_put(eco);
2242 CDEBUG(D_INFO, "oa oid "DOSTID"\n", POSTID(&oa->o_oi));
2247 obd_destroy(env, ec->ec_exp, oa, lsm, oti, NULL, NULL);
2249 echo_free_memmd(ed, &lsm);
2251 CERROR("create object failed with: rc = %d\n", rc);
2255 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
2258 struct lov_stripe_md *lsm = NULL;
2259 struct echo_object *eco;
2263 if ((oa->o_valid & OBD_MD_FLID) == 0 || ostid_id(&oa->o_oi) == 0) {
2264 /* disallow use of object id 0 */
2265 CERROR ("No valid oid\n");
2269 rc = echo_alloc_memmd(ed, &lsm);
2273 lsm->lsm_oi = oa->o_oi;
2274 if (!(oa->o_valid & OBD_MD_FLGROUP))
2275 ostid_set_seq_echo(&lsm->lsm_oi);
2278 eco = cl_echo_object_find(ed, &lsm);
2284 echo_free_memmd(ed, &lsm);
2288 static void echo_put_object(struct echo_object *eco)
2292 rc = cl_echo_object_put(eco);
2294 CERROR("%s: echo client drop an object failed: rc = %d\n",
2295 eco->eo_dev->ed_ec->ec_exp->exp_obd->obd_name, rc);
2299 echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
2301 unsigned long stripe_count;
2302 unsigned long stripe_size;
2303 unsigned long width;
2304 unsigned long woffset;
2308 if (lsm->lsm_stripe_count <= 1)
2312 stripe_size = lsm->lsm_stripe_size;
2313 stripe_count = lsm->lsm_stripe_count;
2315 /* width = # bytes in all stripes */
2316 width = stripe_size * stripe_count;
2318 /* woffset = offset within a width; offset = whole number of widths */
2319 woffset = do_div (offset, width);
2321 stripe_index = woffset / stripe_size;
2323 *idp = ostid_id(&lsm->lsm_oinfo[stripe_index]->loi_oi);
2324 *offp = offset * stripe_size + woffset % stripe_size;
2328 echo_client_page_debug_setup(struct lov_stripe_md *lsm,
2329 struct page *page, int rw, obd_id id,
2330 obd_off offset, obd_off count)
2337 /* no partial pages on the client */
2338 LASSERT(count == PAGE_CACHE_SIZE);
2342 for (delta = 0; delta < PAGE_CACHE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2343 if (rw == OBD_BRW_WRITE) {
2344 stripe_off = offset + delta;
2346 echo_get_stripe_off_id(lsm, &stripe_off, &stripe_id);
2348 stripe_off = 0xdeadbeef00c0ffeeULL;
2349 stripe_id = 0xdeadbeef00c0ffeeULL;
2351 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
2352 stripe_off, stripe_id);
2358 static int echo_client_page_debug_check(struct lov_stripe_md *lsm,
2359 struct page *page, obd_id id,
2360 obd_off offset, obd_off count)
2369 /* no partial pages on the client */
2370 LASSERT(count == PAGE_CACHE_SIZE);
2374 for (rc = delta = 0; delta < PAGE_CACHE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2375 stripe_off = offset + delta;
2377 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
2379 rc2 = block_debug_check("test_brw",
2380 addr + delta, OBD_ECHO_BLOCK_SIZE,
2381 stripe_off, stripe_id);
2383 CERROR ("Error in echo object "LPX64"\n", id);
2392 static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
2393 struct echo_object *eco, obd_off offset,
2394 obd_size count, int async,
2395 struct obd_trans_info *oti)
2397 struct lov_stripe_md *lsm = eco->eo_lsm;
2399 struct brw_page *pga;
2400 struct brw_page *pgp;
2401 struct page **pages;
2410 verify = (ostid_id(&oa->o_oi) != ECHO_PERSISTENT_OBJID &&
2411 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
2412 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
2414 gfp_mask = ((ostid_id(&oa->o_oi) & 2) == 0) ? GFP_IOFS : GFP_HIGHUSER;
2416 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
2417 LASSERT(lsm != NULL);
2418 LASSERT(ostid_id(&lsm->lsm_oi) == ostid_id(&oa->o_oi));
2421 (count & (~CFS_PAGE_MASK)) != 0)
2424 /* XXX think again with misaligned I/O */
2425 npages = count >> PAGE_CACHE_SHIFT;
2427 if (rw == OBD_BRW_WRITE)
2428 brw_flags = OBD_BRW_ASYNC;
2430 OBD_ALLOC(pga, npages * sizeof(*pga));
2434 OBD_ALLOC(pages, npages * sizeof(*pages));
2435 if (pages == NULL) {
2436 OBD_FREE(pga, npages * sizeof(*pga));
2440 for (i = 0, pgp = pga, off = offset;
2442 i++, pgp++, off += PAGE_CACHE_SIZE) {
2444 LASSERT (pgp->pg == NULL); /* for cleanup */
2447 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
2448 if (pgp->pg == NULL)
2452 pgp->count = PAGE_CACHE_SIZE;
2454 pgp->flag = brw_flags;
2457 echo_client_page_debug_setup(lsm, pgp->pg, rw,
2458 ostid_id(&oa->o_oi), off,
2462 /* brw mode can only be used at client */
2463 LASSERT(ed->ed_next != NULL);
2464 rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
2467 if (rc != 0 || rw != OBD_BRW_READ)
2470 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
2471 if (pgp->pg == NULL)
2476 vrc = echo_client_page_debug_check(lsm, pgp->pg,
2477 ostid_id(&oa->o_oi),
2478 pgp->off, pgp->count);
2479 if (vrc != 0 && rc == 0)
2482 OBD_PAGE_FREE(pgp->pg);
2484 OBD_FREE(pga, npages * sizeof(*pga));
2485 OBD_FREE(pages, npages * sizeof(*pages));
2489 static int echo_client_prep_commit(const struct lu_env *env,
2490 struct obd_export *exp, int rw,
2491 struct obdo *oa, struct echo_object *eco,
2492 obd_off offset, obd_size count,
2493 obd_size batch, struct obd_trans_info *oti,
2496 struct lov_stripe_md *lsm = eco->eo_lsm;
2497 struct obd_ioobj ioo;
2498 struct niobuf_local *lnb;
2499 struct niobuf_remote *rnb;
2501 obd_size npages, tot_pages;
2502 int i, ret = 0, brw_flags = 0;
2506 if (count <= 0 || (count & (~CFS_PAGE_MASK)) != 0 ||
2507 (lsm != NULL && ostid_id(&lsm->lsm_oi) != ostid_id(&oa->o_oi)))
2510 npages = batch >> PAGE_CACHE_SHIFT;
2511 tot_pages = count >> PAGE_CACHE_SHIFT;
2513 OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
2514 OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
2516 if (lnb == NULL || rnb == NULL)
2517 GOTO(out, ret = -ENOMEM);
2519 if (rw == OBD_BRW_WRITE && async)
2520 brw_flags |= OBD_BRW_ASYNC;
2522 obdo_to_ioobj(oa, &ioo);
2526 for(; tot_pages; tot_pages -= npages) {
2529 if (tot_pages < npages)
2532 for (i = 0; i < npages; i++, off += PAGE_CACHE_SIZE) {
2533 rnb[i].rnb_offset = off;
2534 rnb[i].rnb_len = PAGE_CACHE_SIZE;
2535 rnb[i].rnb_flags = brw_flags;
2538 ioo.ioo_bufcnt = npages;
2541 ret = obd_preprw(env, rw, exp, oa, 1, &ioo, rnb, &lpages,
2545 LASSERT(lpages == npages);
2547 for (i = 0; i < lpages; i++) {
2548 struct page *page = lnb[i].lnb_page;
2550 /* read past eof? */
2551 if (page == NULL && lnb[i].lnb_rc == 0)
2555 lnb[i].lnb_flags |= OBD_BRW_ASYNC;
2557 if (ostid_id(&oa->o_oi) == ECHO_PERSISTENT_OBJID ||
2558 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
2559 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
2562 if (rw == OBD_BRW_WRITE)
2563 echo_client_page_debug_setup(lsm, page, rw,
2564 ostid_id(&oa->o_oi),
2568 echo_client_page_debug_check(lsm, page,
2569 ostid_id(&oa->o_oi),
2574 ret = obd_commitrw(env, rw, exp, oa, 1, &ioo,
2575 rnb, npages, lnb, oti, ret);
2579 /* Reset oti otherwise it would confuse ldiskfs. */
2580 memset(oti, 0, sizeof(*oti));
2582 /* Reuse env context. */
2583 lu_context_exit((struct lu_context *)&env->le_ctx);
2584 lu_context_enter((struct lu_context *)&env->le_ctx);
2589 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
2591 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
2595 static int echo_client_brw_ioctl(const struct lu_env *env, int rw,
2596 struct obd_export *exp,
2597 struct obd_ioctl_data *data,
2598 struct obd_trans_info *dummy_oti)
2600 struct obd_device *obd = class_exp2obd(exp);
2601 struct echo_device *ed = obd2echo_dev(obd);
2602 struct echo_client_obd *ec = ed->ed_ec;
2603 struct obdo *oa = &data->ioc_obdo1;
2604 struct echo_object *eco;
2610 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2612 rc = echo_get_object(&eco, ed, oa);
2616 oa->o_valid &= ~OBD_MD_FLHANDLE;
2618 /* OFD/obdfilter works only via prep/commit */
2619 test_mode = (long)data->ioc_pbuf1;
2620 if (ed->ed_next == NULL && test_mode != 3) {
2622 data->ioc_plen1 = data->ioc_count;
2628 /* Truncate batch size to maximum */
2629 if (data->ioc_plen1 > PTLRPC_MAX_BRW_SIZE)
2630 data->ioc_plen1 = PTLRPC_MAX_BRW_SIZE;
2632 switch (test_mode) {
2636 rc = echo_client_kbrw(ed, rw, oa,
2637 eco, data->ioc_offset,
2638 data->ioc_count, async, dummy_oti);
2641 rc = echo_client_prep_commit(env, ec->ec_exp, rw, oa,
2642 eco, data->ioc_offset,
2643 data->ioc_count, data->ioc_plen1,
2649 echo_put_object(eco);
2654 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2655 void *karg, void *uarg)
2657 #ifdef HAVE_SERVER_SUPPORT
2658 struct tgt_session_info *tsi;
2660 struct obd_device *obd = exp->exp_obd;
2661 struct echo_device *ed = obd2echo_dev(obd);
2662 struct echo_client_obd *ec = ed->ed_ec;
2663 struct echo_object *eco;
2664 struct obd_ioctl_data *data = karg;
2665 struct obd_trans_info dummy_oti;
2667 struct oti_req_ack_lock *ack_lock;
2670 int rw = OBD_BRW_READ;
2673 #ifdef HAVE_SERVER_SUPPORT
2674 struct lu_context echo_session;
2678 memset(&dummy_oti, 0, sizeof(dummy_oti));
2680 oa = &data->ioc_obdo1;
2681 if (!(oa->o_valid & OBD_MD_FLGROUP)) {
2682 oa->o_valid |= OBD_MD_FLGROUP;
2683 ostid_set_seq_echo(&oa->o_oi);
2686 /* This FID is unpacked just for validation at this point */
2687 rc = ostid_to_fid(&fid, &oa->o_oi, 0);
2695 rc = lu_env_init(env, LCT_DT_THREAD);
2697 GOTO(out_alloc, rc = -ENOMEM);
2699 #ifdef HAVE_SERVER_SUPPORT
2700 env->le_ses = &echo_session;
2701 rc = lu_context_init(env->le_ses, LCT_SERVER_SESSION | LCT_NOREF);
2702 if (unlikely(rc < 0))
2704 lu_context_enter(env->le_ses);
2706 tsi = tgt_ses_info(env);
2707 tsi->tsi_exp = ec->ec_exp;
2708 tsi->tsi_jobid = NULL;
2711 case OBD_IOC_CREATE: /* may create echo object */
2712 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2713 GOTO (out, rc = -EPERM);
2715 rc = echo_create_object(env, ed, 1, oa, data->ioc_pbuf1,
2716 data->ioc_plen1, &dummy_oti);
2719 #ifdef HAVE_SERVER_SUPPORT
2720 case OBD_IOC_ECHO_MD: {
2727 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2728 GOTO(out, rc = -EPERM);
2730 count = data->ioc_count;
2731 cmd = data->ioc_command;
2733 id = data->ioc_obdo2.o_oi.oi.oi_id;
2734 dirlen = data->ioc_plen1;
2735 OBD_ALLOC(dir, dirlen + 1);
2737 GOTO(out, rc = -ENOMEM);
2739 if (copy_from_user(dir, data->ioc_pbuf1, dirlen)) {
2740 OBD_FREE(dir, data->ioc_plen1 + 1);
2741 GOTO(out, rc = -EFAULT);
2744 rc = echo_md_handler(ed, cmd, dir, dirlen, id, count, data);
2745 OBD_FREE(dir, dirlen + 1);
2748 case OBD_IOC_ECHO_ALLOC_SEQ: {
2749 struct lu_env *cl_env;
2754 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2755 GOTO(out, rc = -EPERM);
2757 cl_env = cl_env_get(&refcheck);
2759 GOTO(out, rc = PTR_ERR(cl_env));
2761 rc = lu_env_refill_by_tags(cl_env, ECHO_MD_CTX_TAG,
2764 cl_env_put(cl_env, &refcheck);
2768 rc = seq_client_get_seq(cl_env, ed->ed_cl_seq, &seq);
2769 cl_env_put(cl_env, &refcheck);
2771 CERROR("%s: Can not alloc seq: rc = %d\n",
2776 if (copy_to_user(data->ioc_pbuf1, &seq, data->ioc_plen1))
2779 max_count = LUSTRE_METADATA_SEQ_MAX_WIDTH;
2780 if (copy_to_user(data->ioc_pbuf2, &max_count,
2785 #endif /* HAVE_SERVER_SUPPORT */
2786 case OBD_IOC_DESTROY:
2787 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2788 GOTO (out, rc = -EPERM);
2790 rc = echo_get_object(&eco, ed, oa);
2792 rc = obd_destroy(env, ec->ec_exp, oa, eco->eo_lsm,
2793 &dummy_oti, NULL, NULL);
2795 eco->eo_deleted = 1;
2796 echo_put_object(eco);
2800 case OBD_IOC_GETATTR:
2801 rc = echo_get_object(&eco, ed, oa);
2803 struct obd_info oinfo = { { { 0 } } };
2804 oinfo.oi_md = eco->eo_lsm;
2806 rc = obd_getattr(env, ec->ec_exp, &oinfo);
2807 echo_put_object(eco);
2811 case OBD_IOC_SETATTR:
2812 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2813 GOTO (out, rc = -EPERM);
2815 rc = echo_get_object(&eco, ed, oa);
2817 struct obd_info oinfo = { { { 0 } } };
2819 oinfo.oi_md = eco->eo_lsm;
2821 rc = obd_setattr(env, ec->ec_exp, &oinfo, NULL);
2822 echo_put_object(eco);
2826 case OBD_IOC_BRW_WRITE:
2827 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2828 GOTO (out, rc = -EPERM);
2832 case OBD_IOC_BRW_READ:
2833 rc = echo_client_brw_ioctl(env, rw, exp, data, &dummy_oti);
2837 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
2838 GOTO (out, rc = -ENOTTY);
2843 #ifdef HAVE_SERVER_SUPPORT
2844 lu_context_exit(env->le_ses);
2845 lu_context_fini(env->le_ses);
2852 /* XXX this should be in a helper also called by target_send_reply */
2853 for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
2855 if (!ack_lock->mode)
2857 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
2863 static int echo_client_setup(const struct lu_env *env,
2864 struct obd_device *obddev, struct lustre_cfg *lcfg)
2866 struct echo_client_obd *ec = &obddev->u.echo_client;
2867 struct obd_device *tgt;
2868 struct obd_uuid echo_uuid = { "ECHO_UUID" };
2869 struct obd_connect_data *ocd = NULL;
2873 if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
2874 CERROR("requires a TARGET OBD name\n");
2878 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
2879 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
2880 CERROR("device not attached or not set up (%s)\n",
2881 lustre_cfg_string(lcfg, 1));
2885 spin_lock_init(&ec->ec_lock);
2886 INIT_LIST_HEAD(&ec->ec_objects);
2887 INIT_LIST_HEAD(&ec->ec_locks);
2889 ec->ec_nstripes = 0;
2891 if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
2892 #ifdef HAVE_SERVER_SUPPORT
2893 lu_context_tags_update(ECHO_MD_CTX_TAG);
2894 lu_session_tags_update(ECHO_MD_SES_TAG);
2896 CERROR("Local operations are NOT supported on client side. "
2897 "Only remote operations are supported. Metadata client "
2898 "must be run on server side.\n");
2903 OBD_ALLOC(ocd, sizeof(*ocd));
2905 CERROR("Can't alloc ocd connecting to %s\n",
2906 lustre_cfg_string(lcfg, 1));
2910 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
2911 OBD_CONNECT_BRW_SIZE |
2912 OBD_CONNECT_GRANT | OBD_CONNECT_FULL20 |
2913 OBD_CONNECT_64BITHASH | OBD_CONNECT_LVB_TYPE |
2915 ocd->ocd_brw_size = DT_MAX_BRW_SIZE;
2916 ocd->ocd_version = LUSTRE_VERSION_CODE;
2917 ocd->ocd_group = FID_SEQ_ECHO;
2919 rc = obd_connect(env, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
2921 /* Turn off pinger because it connects to tgt obd directly. */
2922 spin_lock(&tgt->obd_dev_lock);
2923 list_del_init(&ec->ec_exp->exp_obd_chain_timed);
2924 spin_unlock(&tgt->obd_dev_lock);
2927 OBD_FREE(ocd, sizeof(*ocd));
2930 CERROR("fail to connect to device %s\n",
2931 lustre_cfg_string(lcfg, 1));
2938 static int echo_client_cleanup(struct obd_device *obddev)
2940 struct echo_device *ed = obd2echo_dev(obddev);
2941 struct echo_client_obd *ec = &obddev->u.echo_client;
2945 /*Do nothing for Metadata echo client*/
2949 if (ed->ed_next_ismd) {
2950 #ifdef HAVE_SERVER_SUPPORT
2951 lu_context_tags_clear(ECHO_MD_CTX_TAG);
2952 lu_session_tags_clear(ECHO_MD_SES_TAG);
2954 CERROR("This is client-side only module, does not support "
2955 "metadata echo client.\n");
2960 if (!list_empty(&obddev->obd_exports)) {
2961 CERROR("still has clients!\n");
2965 LASSERT(atomic_read(&ec->ec_exp->exp_refcount) > 0);
2966 rc = obd_disconnect(ec->ec_exp);
2968 CERROR("fail to disconnect device: %d\n", rc);
2973 static int echo_client_connect(const struct lu_env *env,
2974 struct obd_export **exp,
2975 struct obd_device *src, struct obd_uuid *cluuid,
2976 struct obd_connect_data *data, void *localdata)
2979 struct lustre_handle conn = { 0 };
2982 rc = class_connect(&conn, src, cluuid);
2984 *exp = class_conn2export(&conn);
2990 static int echo_client_disconnect(struct obd_export *exp)
2996 GOTO(out, rc = -EINVAL);
2998 rc = class_disconnect(exp);
3004 static struct obd_ops echo_client_obd_ops = {
3005 .o_owner = THIS_MODULE,
3006 .o_iocontrol = echo_client_iocontrol,
3007 .o_connect = echo_client_connect,
3008 .o_disconnect = echo_client_disconnect
3011 int echo_client_init(void)
3015 rc = lu_kmem_init(echo_caches);
3017 rc = class_register_type(&echo_client_obd_ops, NULL, true, NULL,
3018 LUSTRE_ECHO_CLIENT_NAME,
3021 lu_kmem_fini(echo_caches);
3026 void echo_client_exit(void)
3028 class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
3029 lu_kmem_fini(echo_caches);
3032 static int __init obdecho_init(void)
3037 LCONSOLE_INFO("Echo OBD driver; http://www.lustre.org/\n");
3039 LASSERT(PAGE_CACHE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
3041 # ifdef HAVE_SERVER_SUPPORT
3042 rc = echo_persistent_pages_init();
3046 rc = class_register_type(&echo_obd_ops, NULL, true, NULL,
3047 LUSTRE_ECHO_NAME, NULL);
3052 rc = echo_client_init();
3054 # ifdef HAVE_SERVER_SUPPORT
3058 class_unregister_type(LUSTRE_ECHO_NAME);
3060 echo_persistent_pages_fini();
3066 static void /*__exit*/ obdecho_exit(void)
3070 # ifdef HAVE_SERVER_SUPPORT
3071 class_unregister_type(LUSTRE_ECHO_NAME);
3072 echo_persistent_pages_fini();
3076 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3077 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
3078 MODULE_LICENSE("GPL");
3080 cfs_module(obdecho, LUSTRE_VERSION_STRING, obdecho_init, obdecho_exit);
3082 /** @} echo_client */