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 <md_object.h>
47 #include <lustre_fid.h>
48 #include <lustre_acl.h>
49 #include <lustre_ioctl.h>
50 #include <lustre_net.h>
52 #include "echo_internal.h"
54 /** \defgroup echo_client Echo Client
59 struct cl_device ed_cl;
60 struct echo_client_obd *ed_ec;
62 struct cl_site ed_site_myself;
63 struct cl_site *ed_site;
64 struct lu_device *ed_next;
67 struct lu_client_seq *ed_cl_seq;
71 struct cl_object eo_cl;
72 struct cl_object_header eo_hdr;
74 struct echo_device *eo_dev;
75 struct list_head eo_obj_chain;
76 struct lov_stripe_md *eo_lsm;
81 struct echo_object_conf {
82 struct cl_object_conf eoc_cl;
83 struct lov_stripe_md **eoc_md;
87 struct cl_page_slice ep_cl;
92 struct cl_lock_slice el_cl;
93 struct list_head el_chain;
94 struct echo_object *el_object;
99 static int echo_client_setup(const struct lu_env *env,
100 struct obd_device *obddev,
101 struct lustre_cfg *lcfg);
102 static int echo_client_cleanup(struct obd_device *obddev);
105 /** \defgroup echo_helpers Helper functions
108 static inline struct echo_device *cl2echo_dev(const struct cl_device *dev)
110 return container_of0(dev, struct echo_device, ed_cl);
113 static inline struct cl_device *echo_dev2cl(struct echo_device *d)
118 static inline struct echo_device *obd2echo_dev(const struct obd_device *obd)
120 return cl2echo_dev(lu2cl_dev(obd->obd_lu_dev));
123 static inline struct cl_object *echo_obj2cl(struct echo_object *eco)
128 static inline struct echo_object *cl2echo_obj(const struct cl_object *o)
130 return container_of(o, struct echo_object, eo_cl);
133 static inline struct echo_page *cl2echo_page(const struct cl_page_slice *s)
135 return container_of(s, struct echo_page, ep_cl);
138 static inline struct echo_lock *cl2echo_lock(const struct cl_lock_slice *s)
140 return container_of(s, struct echo_lock, el_cl);
143 static inline struct cl_lock *echo_lock2cl(const struct echo_lock *ecl)
145 return ecl->el_cl.cls_lock;
148 static struct lu_context_key echo_thread_key;
149 static inline struct echo_thread_info *echo_env_info(const struct lu_env *env)
151 struct echo_thread_info *info;
152 info = lu_context_key_get(&env->le_ctx, &echo_thread_key);
153 LASSERT(info != NULL);
158 struct echo_object_conf *cl2echo_conf(const struct cl_object_conf *c)
160 return container_of(c, struct echo_object_conf, eoc_cl);
163 /** @} echo_helpers */
165 static struct echo_object *cl_echo_object_find(struct echo_device *d,
166 struct lov_stripe_md **lsm);
167 static int cl_echo_object_put(struct echo_object *eco);
168 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
169 struct page **pages, int npages, int async);
171 struct echo_thread_info {
172 struct echo_object_conf eti_conf;
173 struct lustre_md eti_md;
175 struct cl_2queue eti_queue;
177 struct cl_lock eti_lock;
178 struct lu_fid eti_fid;
179 struct lu_fid eti_fid2;
180 #ifdef HAVE_SERVER_SUPPORT
181 struct md_op_spec eti_spec;
182 struct lov_mds_md_v3 eti_lmm;
183 struct lov_user_md_v3 eti_lum;
184 struct md_attr eti_ma;
185 struct lu_name eti_lname;
186 /* per-thread values, can be re-used */
190 struct lu_buf eti_buf;
191 char eti_xattr_buf[LUSTRE_POSIX_ACL_MAX_SIZE];
195 /* No session used right now */
196 struct echo_session_info {
200 static struct kmem_cache *echo_lock_kmem;
201 static struct kmem_cache *echo_object_kmem;
202 static struct kmem_cache *echo_thread_kmem;
203 static struct kmem_cache *echo_session_kmem;
204 /* static struct kmem_cache *echo_req_kmem; */
206 static struct lu_kmem_descr echo_caches[] = {
208 .ckd_cache = &echo_lock_kmem,
209 .ckd_name = "echo_lock_kmem",
210 .ckd_size = sizeof (struct echo_lock)
213 .ckd_cache = &echo_object_kmem,
214 .ckd_name = "echo_object_kmem",
215 .ckd_size = sizeof (struct echo_object)
218 .ckd_cache = &echo_thread_kmem,
219 .ckd_name = "echo_thread_kmem",
220 .ckd_size = sizeof (struct echo_thread_info)
223 .ckd_cache = &echo_session_kmem,
224 .ckd_name = "echo_session_kmem",
225 .ckd_size = sizeof (struct echo_session_info)
232 /** \defgroup echo_page Page operations
234 * Echo page operations.
238 static int echo_page_own(const struct lu_env *env,
239 const struct cl_page_slice *slice,
240 struct cl_io *io, int nonblock)
242 struct echo_page *ep = cl2echo_page(slice);
245 mutex_lock(&ep->ep_lock);
246 else if (!mutex_trylock(&ep->ep_lock))
251 static void echo_page_disown(const struct lu_env *env,
252 const struct cl_page_slice *slice,
255 struct echo_page *ep = cl2echo_page(slice);
257 LASSERT(mutex_is_locked(&ep->ep_lock));
258 mutex_unlock(&ep->ep_lock);
261 static void echo_page_discard(const struct lu_env *env,
262 const struct cl_page_slice *slice,
263 struct cl_io *unused)
265 cl_page_delete(env, slice->cpl_page);
268 static int echo_page_is_vmlocked(const struct lu_env *env,
269 const struct cl_page_slice *slice)
271 if (mutex_is_locked(&cl2echo_page(slice)->ep_lock))
276 static void echo_page_completion(const struct lu_env *env,
277 const struct cl_page_slice *slice,
280 LASSERT(slice->cpl_page->cp_sync_io != NULL);
283 static void echo_page_fini(const struct lu_env *env,
284 struct cl_page_slice *slice)
286 struct echo_object *eco = cl2echo_obj(slice->cpl_obj);
289 atomic_dec(&eco->eo_npages);
290 page_cache_release(slice->cpl_page->cp_vmpage);
294 static int echo_page_prep(const struct lu_env *env,
295 const struct cl_page_slice *slice,
296 struct cl_io *unused)
301 static int echo_page_print(const struct lu_env *env,
302 const struct cl_page_slice *slice,
303 void *cookie, lu_printer_t printer)
305 struct echo_page *ep = cl2echo_page(slice);
307 (*printer)(env, cookie, LUSTRE_ECHO_CLIENT_NAME"-page@%p %d vm@%p\n",
308 ep, mutex_is_locked(&ep->ep_lock),
309 slice->cpl_page->cp_vmpage);
313 static const struct cl_page_operations echo_page_ops = {
314 .cpo_own = echo_page_own,
315 .cpo_disown = echo_page_disown,
316 .cpo_discard = echo_page_discard,
317 .cpo_fini = echo_page_fini,
318 .cpo_print = echo_page_print,
319 .cpo_is_vmlocked = echo_page_is_vmlocked,
322 .cpo_prep = echo_page_prep,
323 .cpo_completion = echo_page_completion,
326 .cpo_prep = echo_page_prep,
327 .cpo_completion = echo_page_completion,
333 /** \defgroup echo_lock Locking
335 * echo lock operations
339 static void echo_lock_fini(const struct lu_env *env,
340 struct cl_lock_slice *slice)
342 struct echo_lock *ecl = cl2echo_lock(slice);
344 LASSERT(list_empty(&ecl->el_chain));
345 OBD_SLAB_FREE_PTR(ecl, echo_lock_kmem);
348 static struct cl_lock_operations echo_lock_ops = {
349 .clo_fini = echo_lock_fini,
354 /** \defgroup echo_cl_ops cl_object operations
356 * operations for cl_object
360 static int echo_page_init(const struct lu_env *env, struct cl_object *obj,
361 struct cl_page *page, pgoff_t index)
363 struct echo_page *ep = cl_object_page_slice(obj, page);
364 struct echo_object *eco = cl2echo_obj(obj);
367 page_cache_get(page->cp_vmpage);
368 mutex_init(&ep->ep_lock);
369 cl_page_slice_add(page, &ep->ep_cl, obj, index, &echo_page_ops);
370 atomic_inc(&eco->eo_npages);
374 static int echo_io_init(const struct lu_env *env, struct cl_object *obj,
380 static int echo_lock_init(const struct lu_env *env,
381 struct cl_object *obj, struct cl_lock *lock,
382 const struct cl_io *unused)
384 struct echo_lock *el;
387 OBD_SLAB_ALLOC_PTR_GFP(el, echo_lock_kmem, GFP_NOFS);
389 cl_lock_slice_add(lock, &el->el_cl, obj, &echo_lock_ops);
390 el->el_object = cl2echo_obj(obj);
391 INIT_LIST_HEAD(&el->el_chain);
392 atomic_set(&el->el_refcount, 0);
394 RETURN(el == NULL ? -ENOMEM : 0);
397 static int echo_conf_set(const struct lu_env *env, struct cl_object *obj,
398 const struct cl_object_conf *conf)
403 static const struct cl_object_operations echo_cl_obj_ops = {
404 .coo_page_init = echo_page_init,
405 .coo_lock_init = echo_lock_init,
406 .coo_io_init = echo_io_init,
407 .coo_conf_set = echo_conf_set
409 /** @} echo_cl_ops */
411 /** \defgroup echo_lu_ops lu_object operations
413 * operations for echo lu object.
417 static int echo_object_init(const struct lu_env *env, struct lu_object *obj,
418 const struct lu_object_conf *conf)
420 struct echo_device *ed = cl2echo_dev(lu2cl_dev(obj->lo_dev));
421 struct echo_client_obd *ec = ed->ed_ec;
422 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
426 struct lu_object *below;
427 struct lu_device *under;
430 below = under->ld_ops->ldo_object_alloc(env, obj->lo_header,
434 lu_object_add(obj, below);
437 if (!ed->ed_next_ismd) {
438 const struct cl_object_conf *cconf = lu2cl_conf(conf);
439 struct echo_object_conf *econf = cl2echo_conf(cconf);
441 LASSERT(econf->eoc_md);
442 eco->eo_lsm = *econf->eoc_md;
443 /* clear the lsm pointer so that it won't get freed. */
444 *econf->eoc_md = NULL;
450 atomic_set(&eco->eo_npages, 0);
451 cl_object_page_init(lu2cl(obj), sizeof(struct echo_page));
453 spin_lock(&ec->ec_lock);
454 list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
455 spin_unlock(&ec->ec_lock);
460 /* taken from osc_unpackmd() */
461 static int echo_alloc_memmd(struct echo_device *ed,
462 struct lov_stripe_md **lsmp)
468 /* If export is lov/osc then use their obd method */
469 if (ed->ed_next != NULL)
470 return obd_alloc_memmd(ed->ed_ec->ec_exp, lsmp);
471 /* OFD has no unpackmd method, do everything here */
472 lsm_size = lov_stripe_md_size(1);
474 LASSERT(*lsmp == NULL);
475 OBD_ALLOC(*lsmp, lsm_size);
479 OBD_ALLOC((*lsmp)->lsm_oinfo[0], sizeof(struct lov_oinfo));
480 if ((*lsmp)->lsm_oinfo[0] == NULL) {
481 OBD_FREE(*lsmp, lsm_size);
485 loi_init((*lsmp)->lsm_oinfo[0]);
486 (*lsmp)->lsm_maxbytes = LUSTRE_EXT3_STRIPE_MAXBYTES;
487 ostid_set_seq_echo(&(*lsmp)->lsm_oi);
492 static int echo_free_memmd(struct echo_device *ed, struct lov_stripe_md **lsmp)
498 /* If export is lov/osc then use their obd method */
499 if (ed->ed_next != NULL)
500 return obd_free_memmd(ed->ed_ec->ec_exp, lsmp);
501 /* OFD has no unpackmd method, do everything here */
502 lsm_size = lov_stripe_md_size(1);
504 LASSERT(*lsmp != NULL);
505 OBD_FREE((*lsmp)->lsm_oinfo[0], sizeof(struct lov_oinfo));
506 OBD_FREE(*lsmp, lsm_size);
511 static void echo_object_free(const struct lu_env *env, struct lu_object *obj)
513 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
514 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
517 LASSERT(atomic_read(&eco->eo_npages) == 0);
519 spin_lock(&ec->ec_lock);
520 list_del_init(&eco->eo_obj_chain);
521 spin_unlock(&ec->ec_lock);
524 lu_object_header_fini(obj->lo_header);
527 echo_free_memmd(eco->eo_dev, &eco->eo_lsm);
528 OBD_SLAB_FREE_PTR(eco, echo_object_kmem);
532 static int echo_object_print(const struct lu_env *env, void *cookie,
533 lu_printer_t p, const struct lu_object *o)
535 struct echo_object *obj = cl2echo_obj(lu2cl(o));
537 return (*p)(env, cookie, "echoclient-object@%p", obj);
540 static const struct lu_object_operations echo_lu_obj_ops = {
541 .loo_object_init = echo_object_init,
542 .loo_object_delete = NULL,
543 .loo_object_release = NULL,
544 .loo_object_free = echo_object_free,
545 .loo_object_print = echo_object_print,
546 .loo_object_invariant = NULL
548 /** @} echo_lu_ops */
550 /** \defgroup echo_lu_dev_ops lu_device operations
552 * Operations for echo lu device.
556 static struct lu_object *echo_object_alloc(const struct lu_env *env,
557 const struct lu_object_header *hdr,
558 struct lu_device *dev)
560 struct echo_object *eco;
561 struct lu_object *obj = NULL;
564 /* we're the top dev. */
565 LASSERT(hdr == NULL);
566 OBD_SLAB_ALLOC_PTR_GFP(eco, echo_object_kmem, GFP_NOFS);
568 struct cl_object_header *hdr = &eco->eo_hdr;
570 obj = &echo_obj2cl(eco)->co_lu;
571 cl_object_header_init(hdr);
572 hdr->coh_page_bufsize = cfs_size_round(sizeof(struct cl_page));
574 lu_object_init(obj, &hdr->coh_lu, dev);
575 lu_object_add_top(&hdr->coh_lu, obj);
577 eco->eo_cl.co_ops = &echo_cl_obj_ops;
578 obj->lo_ops = &echo_lu_obj_ops;
583 static struct lu_device_operations echo_device_lu_ops = {
584 .ldo_object_alloc = echo_object_alloc,
587 /** @} echo_lu_dev_ops */
589 static struct cl_device_operations echo_device_cl_ops = {
592 /** \defgroup echo_init Setup and teardown
594 * Init and fini functions for echo client.
598 static int echo_site_init(const struct lu_env *env, struct echo_device *ed)
600 struct cl_site *site = &ed->ed_site_myself;
603 /* initialize site */
604 rc = cl_site_init(site, &ed->ed_cl);
606 CERROR("Cannot initilize site for echo client(%d)\n", rc);
610 rc = lu_site_init_finish(&site->cs_lu);
618 static void echo_site_fini(const struct lu_env *env, struct echo_device *ed)
621 if (!ed->ed_next_ismd)
622 cl_site_fini(ed->ed_site);
627 static void *echo_thread_key_init(const struct lu_context *ctx,
628 struct lu_context_key *key)
630 struct echo_thread_info *info;
632 OBD_SLAB_ALLOC_PTR_GFP(info, echo_thread_kmem, GFP_NOFS);
634 info = ERR_PTR(-ENOMEM);
638 static void echo_thread_key_fini(const struct lu_context *ctx,
639 struct lu_context_key *key, void *data)
641 struct echo_thread_info *info = data;
642 OBD_SLAB_FREE_PTR(info, echo_thread_kmem);
645 static void echo_thread_key_exit(const struct lu_context *ctx,
646 struct lu_context_key *key, void *data)
650 static struct lu_context_key echo_thread_key = {
651 .lct_tags = LCT_CL_THREAD,
652 .lct_init = echo_thread_key_init,
653 .lct_fini = echo_thread_key_fini,
654 .lct_exit = echo_thread_key_exit
657 static void *echo_session_key_init(const struct lu_context *ctx,
658 struct lu_context_key *key)
660 struct echo_session_info *session;
662 OBD_SLAB_ALLOC_PTR_GFP(session, echo_session_kmem, GFP_NOFS);
664 session = ERR_PTR(-ENOMEM);
668 static void echo_session_key_fini(const struct lu_context *ctx,
669 struct lu_context_key *key, void *data)
671 struct echo_session_info *session = data;
672 OBD_SLAB_FREE_PTR(session, echo_session_kmem);
675 static void echo_session_key_exit(const struct lu_context *ctx,
676 struct lu_context_key *key, void *data)
680 static struct lu_context_key echo_session_key = {
681 .lct_tags = LCT_SESSION,
682 .lct_init = echo_session_key_init,
683 .lct_fini = echo_session_key_fini,
684 .lct_exit = echo_session_key_exit
687 LU_TYPE_INIT_FINI(echo, &echo_thread_key, &echo_session_key);
689 #ifdef HAVE_SERVER_SUPPORT
690 # define ECHO_SEQ_WIDTH 0xffffffff
691 static int echo_fid_init(struct echo_device *ed, char *obd_name,
692 struct seq_server_site *ss)
698 OBD_ALLOC_PTR(ed->ed_cl_seq);
699 if (ed->ed_cl_seq == NULL)
702 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
704 GOTO(out_free_seq, rc = -ENOMEM);
706 snprintf(prefix, MAX_OBD_NAME + 5, "srv-%s", obd_name);
708 /* Init client side sequence-manager */
709 rc = seq_client_init(ed->ed_cl_seq, NULL,
711 prefix, ss->ss_server_seq);
712 ed->ed_cl_seq->lcs_width = ECHO_SEQ_WIDTH;
713 OBD_FREE(prefix, MAX_OBD_NAME + 5);
715 GOTO(out_free_seq, rc);
720 OBD_FREE_PTR(ed->ed_cl_seq);
721 ed->ed_cl_seq = NULL;
725 static int echo_fid_fini(struct obd_device *obddev)
727 struct echo_device *ed = obd2echo_dev(obddev);
730 if (ed->ed_cl_seq != NULL) {
731 seq_client_fini(ed->ed_cl_seq);
732 OBD_FREE_PTR(ed->ed_cl_seq);
733 ed->ed_cl_seq = NULL;
738 #endif /* HAVE_SERVER_SUPPORT */
740 static struct lu_device *echo_device_alloc(const struct lu_env *env,
741 struct lu_device_type *t,
742 struct lustre_cfg *cfg)
744 struct lu_device *next;
745 struct echo_device *ed;
746 struct cl_device *cd;
747 struct obd_device *obd = NULL; /* to keep compiler happy */
748 struct obd_device *tgt;
749 const char *tgt_type_name;
756 GOTO(out, rc = -ENOMEM);
760 rc = cl_device_init(cd, t);
764 cd->cd_lu_dev.ld_ops = &echo_device_lu_ops;
765 cd->cd_ops = &echo_device_cl_ops;
768 obd = class_name2obd(lustre_cfg_string(cfg, 0));
769 LASSERT(obd != NULL);
770 LASSERT(env != NULL);
772 tgt = class_name2obd(lustre_cfg_string(cfg, 1));
774 CERROR("Can not find tgt device %s\n",
775 lustre_cfg_string(cfg, 1));
776 GOTO(out, rc = -ENODEV);
779 next = tgt->obd_lu_dev;
780 if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
781 ed->ed_next_ismd = 1;
783 ed->ed_next_ismd = 0;
784 rc = echo_site_init(env, ed);
790 rc = echo_client_setup(env, obd, cfg);
794 ed->ed_ec = &obd->u.echo_client;
797 if (ed->ed_next_ismd) {
798 #ifdef HAVE_SERVER_SUPPORT
799 /* Suppose to connect to some Metadata layer */
801 struct lu_device *ld;
805 CERROR("%s is not lu device type!\n",
806 lustre_cfg_string(cfg, 1));
807 GOTO(out, rc = -EINVAL);
810 tgt_type_name = lustre_cfg_string(cfg, 2);
811 if (!tgt_type_name) {
812 CERROR("%s no type name for echo %s setup\n",
813 lustre_cfg_string(cfg, 1),
814 tgt->obd_type->typ_name);
815 GOTO(out, rc = -EINVAL);
820 spin_lock(&ls->ls_ld_lock);
821 list_for_each_entry(ld, &ls->ls_ld_linkage, ld_linkage) {
822 if (strcmp(ld->ld_type->ldt_name, tgt_type_name) == 0) {
827 spin_unlock(&ls->ls_ld_lock);
830 CERROR("%s is not lu device type!\n",
831 lustre_cfg_string(cfg, 1));
832 GOTO(out, rc = -EINVAL);
836 /* For MD echo client, it will use the site in MDS stack */
837 ed->ed_site_myself.cs_lu = *ls;
838 ed->ed_site = &ed->ed_site_myself;
839 ed->ed_cl.cd_lu_dev.ld_site = &ed->ed_site_myself.cs_lu;
840 rc = echo_fid_init(ed, obd->obd_name, lu_site2seq(ls));
842 CERROR("echo fid init error %d\n", rc);
845 #else /* !HAVE_SERVER_SUPPORT */
846 CERROR("Local operations are NOT supported on client side. "
847 "Only remote operations are supported. Metadata client "
848 "must be run on server side.\n");
849 GOTO(out, rc = -EOPNOTSUPP);
852 /* if echo client is to be stacked upon ost device, the next is
853 * NULL since ost is not a clio device so far */
854 if (next != NULL && !lu_device_is_cl(next))
857 tgt_type_name = tgt->obd_type->typ_name;
859 LASSERT(next != NULL);
860 if (next->ld_site != NULL)
861 GOTO(out, rc = -EBUSY);
863 next->ld_site = &ed->ed_site->cs_lu;
864 rc = next->ld_type->ldt_ops->ldto_device_init(env, next,
865 next->ld_type->ldt_name,
870 /* Tricky case, I have to determine the obd type since
871 * CLIO uses the different parameters to initialize
872 * objects for lov & osc. */
873 if (strcmp(tgt_type_name, LUSTRE_LOV_NAME) == 0)
874 ed->ed_next_islov = 1;
876 LASSERT(strcmp(tgt_type_name,
877 LUSTRE_OSC_NAME) == 0);
879 LASSERT(strcmp(tgt_type_name, LUSTRE_OST_NAME) == 0);
883 RETURN(&cd->cd_lu_dev);
888 rc2 = echo_client_cleanup(obd);
890 CERROR("Cleanup obd device %s error(%d)\n",
895 echo_site_fini(env, ed);
897 cl_device_fini(&ed->ed_cl);
907 static int echo_device_init(const struct lu_env *env, struct lu_device *d,
908 const char *name, struct lu_device *next)
914 static struct lu_device *echo_device_fini(const struct lu_env *env,
917 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
918 struct lu_device *next = ed->ed_next;
920 while (next && !ed->ed_next_ismd)
921 next = next->ld_type->ldt_ops->ldto_device_fini(env, next);
925 static void echo_lock_release(const struct lu_env *env,
926 struct echo_lock *ecl,
929 struct cl_lock *clk = echo_lock2cl(ecl);
931 cl_lock_release(env, clk);
934 static struct lu_device *echo_device_free(const struct lu_env *env,
937 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
938 struct echo_client_obd *ec = ed->ed_ec;
939 struct echo_object *eco;
940 struct lu_device *next = ed->ed_next;
942 CDEBUG(D_INFO, "echo device:%p is going to be freed, next = %p\n",
945 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
947 /* check if there are objects still alive.
948 * It shouldn't have any object because lu_site_purge would cleanup
949 * all of cached objects. Anyway, probably the echo device is being
950 * parallelly accessed.
952 spin_lock(&ec->ec_lock);
953 list_for_each_entry(eco, &ec->ec_objects, eo_obj_chain)
955 spin_unlock(&ec->ec_lock);
958 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
961 "Waiting for the reference of echo object to be dropped\n");
963 /* Wait for the last reference to be dropped. */
964 spin_lock(&ec->ec_lock);
965 while (!list_empty(&ec->ec_objects)) {
966 spin_unlock(&ec->ec_lock);
967 CERROR("echo_client still has objects at cleanup time, "
968 "wait for 1 second\n");
969 schedule_timeout_and_set_state(TASK_UNINTERRUPTIBLE,
970 cfs_time_seconds(1));
971 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
972 spin_lock(&ec->ec_lock);
974 spin_unlock(&ec->ec_lock);
976 LASSERT(list_empty(&ec->ec_locks));
978 CDEBUG(D_INFO, "No object exists, exiting...\n");
980 echo_client_cleanup(d->ld_obd);
981 #ifdef HAVE_SERVER_SUPPORT
982 echo_fid_fini(d->ld_obd);
984 while (next && !ed->ed_next_ismd)
985 next = next->ld_type->ldt_ops->ldto_device_free(env, next);
987 LASSERT(ed->ed_site == lu2cl_site(d->ld_site));
988 echo_site_fini(env, ed);
989 cl_device_fini(&ed->ed_cl);
995 static const struct lu_device_type_operations echo_device_type_ops = {
996 .ldto_init = echo_type_init,
997 .ldto_fini = echo_type_fini,
999 .ldto_start = echo_type_start,
1000 .ldto_stop = echo_type_stop,
1002 .ldto_device_alloc = echo_device_alloc,
1003 .ldto_device_free = echo_device_free,
1004 .ldto_device_init = echo_device_init,
1005 .ldto_device_fini = echo_device_fini
1008 static struct lu_device_type echo_device_type = {
1009 .ldt_tags = LU_DEVICE_CL,
1010 .ldt_name = LUSTRE_ECHO_CLIENT_NAME,
1011 .ldt_ops = &echo_device_type_ops,
1012 .ldt_ctx_tags = LCT_CL_THREAD | LCT_MD_THREAD | LCT_DT_THREAD,
1016 /** \defgroup echo_exports Exported operations
1018 * exporting functions to echo client
1023 /* Interfaces to echo client obd device */
1024 static struct echo_object *cl_echo_object_find(struct echo_device *d,
1025 struct lov_stripe_md **lsmp)
1028 struct echo_thread_info *info;
1029 struct echo_object_conf *conf;
1030 struct lov_stripe_md *lsm;
1031 struct echo_object *eco;
1032 struct cl_object *obj;
1041 LASSERTF(ostid_id(&lsm->lsm_oi) != 0, DOSTID"\n", POSTID(&lsm->lsm_oi));
1042 LASSERTF(ostid_seq(&lsm->lsm_oi) == FID_SEQ_ECHO, DOSTID"\n",
1043 POSTID(&lsm->lsm_oi));
1045 /* Never return an object if the obd is to be freed. */
1046 if (echo_dev2cl(d)->cd_lu_dev.ld_obd->obd_stopping)
1047 RETURN(ERR_PTR(-ENODEV));
1049 env = cl_env_get(&refcheck);
1051 RETURN((void *)env);
1053 info = echo_env_info(env);
1054 conf = &info->eti_conf;
1056 if (!d->ed_next_islov) {
1057 struct lov_oinfo *oinfo = lsm->lsm_oinfo[0];
1058 LASSERT(oinfo != NULL);
1059 oinfo->loi_oi = lsm->lsm_oi;
1060 conf->eoc_cl.u.coc_oinfo = oinfo;
1062 struct lustre_md *md;
1064 memset(md, 0, sizeof *md);
1066 conf->eoc_cl.u.coc_md = md;
1069 conf->eoc_md = lsmp;
1071 fid = &info->eti_fid;
1072 rc = ostid_to_fid(fid, &lsm->lsm_oi, 0);
1074 GOTO(out, eco = ERR_PTR(rc));
1076 /* In the function below, .hs_keycmp resolves to
1077 * lu_obj_hop_keycmp() */
1078 /* coverity[overrun-buffer-val] */
1079 obj = cl_object_find(env, echo_dev2cl(d), fid, &conf->eoc_cl);
1081 GOTO(out, eco = (void*)obj);
1083 eco = cl2echo_obj(obj);
1084 if (eco->eo_deleted) {
1085 cl_object_put(env, obj);
1086 eco = ERR_PTR(-EAGAIN);
1090 cl_env_put(env, &refcheck);
1094 static int cl_echo_object_put(struct echo_object *eco)
1097 struct cl_object *obj = echo_obj2cl(eco);
1101 env = cl_env_get(&refcheck);
1103 RETURN(PTR_ERR(env));
1105 /* an external function to kill an object? */
1106 if (eco->eo_deleted) {
1107 struct lu_object_header *loh = obj->co_lu.lo_header;
1108 LASSERT(&eco->eo_hdr == luh2coh(loh));
1109 set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
1112 cl_object_put(env, obj);
1113 cl_env_put(env, &refcheck);
1117 static int cl_echo_enqueue0(struct lu_env *env, struct echo_object *eco,
1118 obd_off start, obd_off end, int mode,
1119 __u64 *cookie , __u32 enqflags)
1122 struct cl_lock *lck;
1123 struct cl_object *obj;
1124 struct cl_lock_descr *descr;
1125 struct echo_thread_info *info;
1129 info = echo_env_info(env);
1131 lck = &info->eti_lock;
1132 obj = echo_obj2cl(eco);
1134 memset(lck, 0, sizeof *lck);
1135 descr = &lck->cll_descr;
1136 descr->cld_obj = obj;
1137 descr->cld_start = cl_index(obj, start);
1138 descr->cld_end = cl_index(obj, end);
1139 descr->cld_mode = mode == LCK_PW ? CLM_WRITE : CLM_READ;
1140 descr->cld_enq_flags = enqflags;
1143 rc = cl_lock_request(env, io, lck);
1145 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
1146 struct echo_lock *el;
1148 el = cl2echo_lock(cl_lock_at(lck, &echo_device_type));
1149 spin_lock(&ec->ec_lock);
1150 if (list_empty(&el->el_chain)) {
1151 list_add(&el->el_chain, &ec->ec_locks);
1152 el->el_cookie = ++ec->ec_unique;
1154 atomic_inc(&el->el_refcount);
1155 *cookie = el->el_cookie;
1156 spin_unlock(&ec->ec_lock);
1161 static int cl_echo_cancel0(struct lu_env *env, struct echo_device *ed,
1164 struct echo_client_obd *ec = ed->ed_ec;
1165 struct echo_lock *ecl = NULL;
1166 struct list_head *el;
1167 int found = 0, still_used = 0;
1170 LASSERT(ec != NULL);
1171 spin_lock(&ec->ec_lock);
1172 list_for_each(el, &ec->ec_locks) {
1173 ecl = list_entry(el, struct echo_lock, el_chain);
1174 CDEBUG(D_INFO, "ecl: %p, cookie: "LPX64"\n", ecl, ecl->el_cookie);
1175 found = (ecl->el_cookie == cookie);
1177 if (atomic_dec_and_test(&ecl->el_refcount))
1178 list_del_init(&ecl->el_chain);
1184 spin_unlock(&ec->ec_lock);
1189 echo_lock_release(env, ecl, still_used);
1193 static void echo_commit_callback(const struct lu_env *env, struct cl_io *io,
1194 struct cl_page *page)
1196 struct echo_thread_info *info;
1197 struct cl_2queue *queue;
1199 info = echo_env_info(env);
1200 LASSERT(io == &info->eti_io);
1202 queue = &info->eti_queue;
1203 cl_page_list_add(&queue->c2_qout, page);
1206 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
1207 struct page **pages, int npages, int async)
1210 struct echo_thread_info *info;
1211 struct cl_object *obj = echo_obj2cl(eco);
1212 struct echo_device *ed = eco->eo_dev;
1213 struct cl_2queue *queue;
1215 struct cl_page *clp;
1216 struct lustre_handle lh = { 0 };
1217 int page_size = cl_page_size(obj);
1223 LASSERT((offset & ~CFS_PAGE_MASK) == 0);
1224 LASSERT(ed->ed_next != NULL);
1225 env = cl_env_get(&refcheck);
1227 RETURN(PTR_ERR(env));
1229 info = echo_env_info(env);
1231 queue = &info->eti_queue;
1233 cl_2queue_init(queue);
1235 io->ci_ignore_layout = 1;
1236 rc = cl_io_init(env, io, CIT_MISC, obj);
1242 rc = cl_echo_enqueue0(env, eco, offset,
1243 offset + npages * PAGE_CACHE_SIZE - 1,
1244 rw == READ ? LCK_PR : LCK_PW, &lh.cookie,
1247 GOTO(error_lock, rc);
1249 for (i = 0; i < npages; i++) {
1251 clp = cl_page_find(env, obj, cl_index(obj, offset),
1252 pages[i], CPT_TRANSIENT);
1257 LASSERT(clp->cp_type == CPT_TRANSIENT);
1259 rc = cl_page_own(env, io, clp);
1261 LASSERT(clp->cp_state == CPS_FREEING);
1262 cl_page_put(env, clp);
1266 cl_2queue_add(queue, clp);
1268 /* drop the reference count for cl_page_find, so that the page
1269 * will be freed in cl_2queue_fini. */
1270 cl_page_put(env, clp);
1271 cl_page_clip(env, clp, 0, page_size);
1273 offset += page_size;
1277 enum cl_req_type typ = rw == READ ? CRT_READ : CRT_WRITE;
1279 async = async && (typ == CRT_WRITE);
1281 rc = cl_io_commit_async(env, io, &queue->c2_qin,
1283 echo_commit_callback);
1285 rc = cl_io_submit_sync(env, io, typ, queue, 0);
1286 CDEBUG(D_INFO, "echo_client %s write returns %d\n",
1287 async ? "async" : "sync", rc);
1290 cl_echo_cancel0(env, ed, lh.cookie);
1293 cl_2queue_discard(env, io, queue);
1294 cl_2queue_disown(env, io, queue);
1295 cl_2queue_fini(env, queue);
1296 cl_io_fini(env, io);
1298 cl_env_put(env, &refcheck);
1301 /** @} echo_exports */
1304 static obd_id last_object_id;
1307 echo_copyin_lsm (struct echo_device *ed, struct lov_stripe_md *lsm,
1308 void *ulsm, int ulsm_nob)
1310 struct echo_client_obd *ec = ed->ed_ec;
1313 if (ulsm_nob < sizeof (*lsm))
1316 if (copy_from_user (lsm, ulsm, sizeof (*lsm)))
1319 if (lsm->lsm_stripe_count > ec->ec_nstripes ||
1320 lsm->lsm_magic != LOV_MAGIC ||
1321 (lsm->lsm_stripe_size & (~CFS_PAGE_MASK)) != 0 ||
1322 ((__u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count > ~0UL))
1326 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1327 if (copy_from_user(lsm->lsm_oinfo[i],
1328 ((struct lov_stripe_md *)ulsm)-> \
1330 sizeof(lsm->lsm_oinfo[0])))
1336 #ifdef HAVE_SERVER_SUPPORT
1337 static inline void echo_md_build_name(struct lu_name *lname, char *name,
1340 sprintf(name, LPU64, id);
1341 lname->ln_name = name;
1342 lname->ln_namelen = strlen(name);
1345 /* similar to mdt_attr_get_complex */
1346 static int echo_big_lmm_get(const struct lu_env *env, struct md_object *o,
1349 struct echo_thread_info *info = echo_env_info(env);
1354 LASSERT(ma->ma_lmm_size > 0);
1356 rc = mo_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LOV);
1360 /* big_lmm may need to be grown */
1361 if (info->eti_big_lmmsize < rc) {
1362 int size = size_roundup_power2(rc);
1364 if (info->eti_big_lmmsize > 0) {
1365 /* free old buffer */
1366 LASSERT(info->eti_big_lmm);
1367 OBD_FREE_LARGE(info->eti_big_lmm,
1368 info->eti_big_lmmsize);
1369 info->eti_big_lmm = NULL;
1370 info->eti_big_lmmsize = 0;
1373 OBD_ALLOC_LARGE(info->eti_big_lmm, size);
1374 if (info->eti_big_lmm == NULL)
1376 info->eti_big_lmmsize = size;
1378 LASSERT(info->eti_big_lmmsize >= rc);
1380 info->eti_buf.lb_buf = info->eti_big_lmm;
1381 info->eti_buf.lb_len = info->eti_big_lmmsize;
1382 rc = mo_xattr_get(env, o, &info->eti_buf, XATTR_NAME_LOV);
1386 ma->ma_valid |= MA_LOV;
1387 ma->ma_lmm = info->eti_big_lmm;
1388 ma->ma_lmm_size = rc;
1393 static int echo_attr_get_complex(const struct lu_env *env,
1394 struct md_object *next,
1397 struct echo_thread_info *info = echo_env_info(env);
1398 struct lu_buf *buf = &info->eti_buf;
1399 umode_t mode = lu_object_attr(&next->mo_lu);
1400 int need = ma->ma_need;
1407 if (need & MA_INODE) {
1408 ma->ma_need = MA_INODE;
1409 rc = mo_attr_get(env, next, ma);
1412 ma->ma_valid |= MA_INODE;
1415 if (need & MA_LOV) {
1416 if (S_ISREG(mode) || S_ISDIR(mode)) {
1417 LASSERT(ma->ma_lmm_size > 0);
1418 buf->lb_buf = ma->ma_lmm;
1419 buf->lb_len = ma->ma_lmm_size;
1420 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LOV);
1422 ma->ma_lmm_size = rc2;
1423 ma->ma_valid |= MA_LOV;
1424 } else if (rc2 == -ENODATA) {
1426 ma->ma_lmm_size = 0;
1427 } else if (rc2 == -ERANGE) {
1428 rc2 = echo_big_lmm_get(env, next, ma);
1430 GOTO(out, rc = rc2);
1432 GOTO(out, rc = rc2);
1437 #ifdef CONFIG_FS_POSIX_ACL
1438 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
1439 buf->lb_buf = ma->ma_acl;
1440 buf->lb_len = ma->ma_acl_size;
1441 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1443 ma->ma_acl_size = rc2;
1444 ma->ma_valid |= MA_ACL_DEF;
1445 } else if (rc2 == -ENODATA) {
1447 ma->ma_acl_size = 0;
1449 GOTO(out, rc = rc2);
1455 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
1456 rc, ma->ma_valid, ma->ma_lmm);
1461 echo_md_create_internal(const struct lu_env *env, struct echo_device *ed,
1462 struct md_object *parent, struct lu_fid *fid,
1463 struct lu_name *lname, struct md_op_spec *spec,
1466 struct lu_object *ec_child, *child;
1467 struct lu_device *ld = ed->ed_next;
1468 struct echo_thread_info *info = echo_env_info(env);
1469 struct lu_fid *fid2 = &info->eti_fid2;
1470 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
1475 rc = mdo_lookup(env, parent, lname, fid2, spec);
1478 else if (rc != -ENOENT)
1481 ec_child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev,
1483 if (IS_ERR(ec_child)) {
1484 CERROR("Can not find the child "DFID": rc = %ld\n", PFID(fid),
1486 RETURN(PTR_ERR(ec_child));
1489 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1490 if (child == NULL) {
1491 CERROR("Can not locate the child "DFID"\n", PFID(fid));
1492 GOTO(out_put, rc = -EINVAL);
1495 CDEBUG(D_RPCTRACE, "Start creating object "DFID" %s %p\n",
1496 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1499 * Do not perform lookup sanity check. We know that name does not exist.
1501 spec->sp_cr_lookup = 0;
1502 rc = mdo_create(env, parent, lname, lu2md(child), spec, ma);
1504 CERROR("Can not create child "DFID": rc = %d\n", PFID(fid), rc);
1507 CDEBUG(D_RPCTRACE, "End creating object "DFID" %s %p rc = %d\n",
1508 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent, rc);
1511 lu_object_put(env, ec_child);
1515 static int echo_set_lmm_size(const struct lu_env *env, struct lu_device *ld,
1518 struct echo_thread_info *info = echo_env_info(env);
1520 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
1521 ma->ma_lmm = (void *)&info->eti_lmm;
1522 ma->ma_lmm_size = sizeof(info->eti_lmm);
1524 LASSERT(info->eti_big_lmmsize);
1525 ma->ma_lmm = info->eti_big_lmm;
1526 ma->ma_lmm_size = info->eti_big_lmmsize;
1532 static int echo_create_md_object(const struct lu_env *env,
1533 struct echo_device *ed,
1534 struct lu_object *ec_parent,
1536 char *name, int namelen,
1537 __u64 id, __u32 mode, int count,
1538 int stripe_count, int stripe_offset)
1540 struct lu_object *parent;
1541 struct echo_thread_info *info = echo_env_info(env);
1542 struct lu_name *lname = &info->eti_lname;
1543 struct md_op_spec *spec = &info->eti_spec;
1544 struct md_attr *ma = &info->eti_ma;
1545 struct lu_device *ld = ed->ed_next;
1551 if (ec_parent == NULL)
1553 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1557 memset(ma, 0, sizeof(*ma));
1558 memset(spec, 0, sizeof(*spec));
1559 if (stripe_count != 0) {
1560 spec->sp_cr_flags |= FMODE_WRITE;
1561 echo_set_lmm_size(env, ld, ma);
1562 if (stripe_count != -1) {
1563 struct lov_user_md_v3 *lum = &info->eti_lum;
1565 lum->lmm_magic = LOV_USER_MAGIC_V3;
1566 lum->lmm_stripe_count = stripe_count;
1567 lum->lmm_stripe_offset = stripe_offset;
1568 lum->lmm_pattern = 0;
1569 spec->u.sp_ea.eadata = lum;
1570 spec->u.sp_ea.eadatalen = sizeof(*lum);
1571 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1575 ma->ma_attr.la_mode = mode;
1576 ma->ma_attr.la_valid = LA_CTIME | LA_MODE;
1577 ma->ma_attr.la_ctime = cfs_time_current_64();
1580 lname->ln_name = name;
1581 lname->ln_namelen = namelen;
1582 /* If name is specified, only create one object by name */
1583 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1588 /* Create multiple object sequenced by id */
1589 for (i = 0; i < count; i++) {
1590 char *tmp_name = info->eti_name;
1592 echo_md_build_name(lname, tmp_name, id);
1594 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1597 CERROR("Can not create child %s: rc = %d\n", tmp_name,
1608 static struct lu_object *echo_md_lookup(const struct lu_env *env,
1609 struct echo_device *ed,
1610 struct md_object *parent,
1611 struct lu_name *lname)
1613 struct echo_thread_info *info = echo_env_info(env);
1614 struct lu_fid *fid = &info->eti_fid;
1615 struct lu_object *child;
1619 CDEBUG(D_INFO, "lookup %s in parent "DFID" %p\n", lname->ln_name,
1621 rc = mdo_lookup(env, parent, lname, fid, NULL);
1623 CERROR("lookup %s: rc = %d\n", lname->ln_name, rc);
1624 RETURN(ERR_PTR(rc));
1627 /* In the function below, .hs_keycmp resolves to
1628 * lu_obj_hop_keycmp() */
1629 /* coverity[overrun-buffer-val] */
1630 child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1635 static int echo_setattr_object(const struct lu_env *env,
1636 struct echo_device *ed,
1637 struct lu_object *ec_parent,
1638 __u64 id, int count)
1640 struct lu_object *parent;
1641 struct echo_thread_info *info = echo_env_info(env);
1642 struct lu_name *lname = &info->eti_lname;
1643 char *name = info->eti_name;
1644 struct lu_device *ld = ed->ed_next;
1645 struct lu_buf *buf = &info->eti_buf;
1651 if (ec_parent == NULL)
1653 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1657 for (i = 0; i < count; i++) {
1658 struct lu_object *ec_child, *child;
1660 echo_md_build_name(lname, name, id);
1662 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1663 if (IS_ERR(ec_child)) {
1664 CERROR("Can't find child %s: rc = %ld\n",
1665 lname->ln_name, PTR_ERR(ec_child));
1666 RETURN(PTR_ERR(ec_child));
1669 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1670 if (child == NULL) {
1671 CERROR("Can not locate the child %s\n", lname->ln_name);
1672 lu_object_put(env, ec_child);
1677 CDEBUG(D_RPCTRACE, "Start setattr object "DFID"\n",
1678 PFID(lu_object_fid(child)));
1680 buf->lb_buf = info->eti_xattr_buf;
1681 buf->lb_len = sizeof(info->eti_xattr_buf);
1683 sprintf(name, "%s.test1", XATTR_USER_PREFIX);
1684 rc = mo_xattr_set(env, lu2md(child), buf, name,
1687 CERROR("Can not setattr child "DFID": rc = %d\n",
1688 PFID(lu_object_fid(child)), rc);
1689 lu_object_put(env, ec_child);
1692 CDEBUG(D_RPCTRACE, "End setattr object "DFID"\n",
1693 PFID(lu_object_fid(child)));
1695 lu_object_put(env, ec_child);
1700 static int echo_getattr_object(const struct lu_env *env,
1701 struct echo_device *ed,
1702 struct lu_object *ec_parent,
1703 __u64 id, int count)
1705 struct lu_object *parent;
1706 struct echo_thread_info *info = echo_env_info(env);
1707 struct lu_name *lname = &info->eti_lname;
1708 char *name = info->eti_name;
1709 struct md_attr *ma = &info->eti_ma;
1710 struct lu_device *ld = ed->ed_next;
1716 if (ec_parent == NULL)
1718 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1722 memset(ma, 0, sizeof(*ma));
1723 ma->ma_need |= MA_INODE | MA_LOV | MA_PFID | MA_HSM | MA_ACL_DEF;
1724 ma->ma_acl = info->eti_xattr_buf;
1725 ma->ma_acl_size = sizeof(info->eti_xattr_buf);
1727 for (i = 0; i < count; i++) {
1728 struct lu_object *ec_child, *child;
1731 echo_md_build_name(lname, name, id);
1732 echo_set_lmm_size(env, ld, ma);
1734 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1735 if (IS_ERR(ec_child)) {
1736 CERROR("Can't find child %s: rc = %ld\n",
1737 lname->ln_name, PTR_ERR(ec_child));
1738 RETURN(PTR_ERR(ec_child));
1741 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1742 if (child == NULL) {
1743 CERROR("Can not locate the child %s\n", lname->ln_name);
1744 lu_object_put(env, ec_child);
1748 CDEBUG(D_RPCTRACE, "Start getattr object "DFID"\n",
1749 PFID(lu_object_fid(child)));
1750 rc = echo_attr_get_complex(env, lu2md(child), ma);
1752 CERROR("Can not getattr child "DFID": rc = %d\n",
1753 PFID(lu_object_fid(child)), rc);
1754 lu_object_put(env, ec_child);
1757 CDEBUG(D_RPCTRACE, "End getattr object "DFID"\n",
1758 PFID(lu_object_fid(child)));
1760 lu_object_put(env, ec_child);
1766 static int echo_lookup_object(const struct lu_env *env,
1767 struct echo_device *ed,
1768 struct lu_object *ec_parent,
1769 __u64 id, int count)
1771 struct lu_object *parent;
1772 struct echo_thread_info *info = echo_env_info(env);
1773 struct lu_name *lname = &info->eti_lname;
1774 char *name = info->eti_name;
1775 struct lu_fid *fid = &info->eti_fid;
1776 struct lu_device *ld = ed->ed_next;
1780 if (ec_parent == NULL)
1782 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1786 /*prepare the requests*/
1787 for (i = 0; i < count; i++) {
1788 echo_md_build_name(lname, name, id);
1790 CDEBUG(D_RPCTRACE, "Start lookup object "DFID" %s %p\n",
1791 PFID(lu_object_fid(parent)), lname->ln_name, parent);
1793 rc = mdo_lookup(env, lu2md(parent), lname, fid, NULL);
1795 CERROR("Can not lookup child %s: rc = %d\n", name, rc);
1798 CDEBUG(D_RPCTRACE, "End lookup object "DFID" %s %p\n",
1799 PFID(lu_object_fid(parent)), lname->ln_name, parent);
1806 static int echo_md_destroy_internal(const struct lu_env *env,
1807 struct echo_device *ed,
1808 struct md_object *parent,
1809 struct lu_name *lname,
1812 struct lu_device *ld = ed->ed_next;
1813 struct lu_object *ec_child;
1814 struct lu_object *child;
1819 ec_child = echo_md_lookup(env, ed, parent, lname);
1820 if (IS_ERR(ec_child)) {
1821 CERROR("Can't find child %s: rc = %ld\n", lname->ln_name,
1823 RETURN(PTR_ERR(ec_child));
1826 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1827 if (child == NULL) {
1828 CERROR("Can not locate the child %s\n", lname->ln_name);
1829 GOTO(out_put, rc = -EINVAL);
1832 if (lu_object_remote(child)) {
1833 CERROR("Can not destroy remote object %s: rc = %d\n",
1834 lname->ln_name, -EPERM);
1835 GOTO(out_put, rc = -EPERM);
1837 CDEBUG(D_RPCTRACE, "Start destroy object "DFID" %s %p\n",
1838 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1840 rc = mdo_unlink(env, parent, lu2md(child), lname, ma, 0);
1842 CERROR("Can not unlink child %s: rc = %d\n",
1843 lname->ln_name, rc);
1846 CDEBUG(D_RPCTRACE, "End destroy object "DFID" %s %p\n",
1847 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1849 lu_object_put(env, ec_child);
1853 static int echo_destroy_object(const struct lu_env *env,
1854 struct echo_device *ed,
1855 struct lu_object *ec_parent,
1856 char *name, int namelen,
1857 __u64 id, __u32 mode,
1860 struct echo_thread_info *info = echo_env_info(env);
1861 struct lu_name *lname = &info->eti_lname;
1862 struct md_attr *ma = &info->eti_ma;
1863 struct lu_device *ld = ed->ed_next;
1864 struct lu_object *parent;
1869 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1873 memset(ma, 0, sizeof(*ma));
1874 ma->ma_attr.la_mode = mode;
1875 ma->ma_attr.la_valid = LA_CTIME;
1876 ma->ma_attr.la_ctime = cfs_time_current_64();
1877 ma->ma_need = MA_INODE;
1881 lname->ln_name = name;
1882 lname->ln_namelen = namelen;
1883 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
1888 /*prepare the requests*/
1889 for (i = 0; i < count; i++) {
1890 char *tmp_name = info->eti_name;
1893 echo_md_build_name(lname, tmp_name, id);
1895 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
1898 CERROR("Can not unlink child %s: rc = %d\n", name, rc);
1907 static struct lu_object *echo_resolve_path(const struct lu_env *env,
1908 struct echo_device *ed, char *path,
1911 struct lu_device *ld = ed->ed_next;
1912 struct md_device *md = lu2md_dev(ld);
1913 struct echo_thread_info *info = echo_env_info(env);
1914 struct lu_fid *fid = &info->eti_fid;
1915 struct lu_name *lname = &info->eti_lname;
1916 struct lu_object *parent = NULL;
1917 struct lu_object *child = NULL;
1921 /*Only support MDD layer right now*/
1922 rc = md->md_ops->mdo_root_get(env, md, fid);
1924 CERROR("get root error: rc = %d\n", rc);
1925 RETURN(ERR_PTR(rc));
1928 /* In the function below, .hs_keycmp resolves to
1929 * lu_obj_hop_keycmp() */
1930 /* coverity[overrun-buffer-val] */
1931 parent = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1932 if (IS_ERR(parent)) {
1933 CERROR("Can not find the parent "DFID": rc = %ld\n",
1934 PFID(fid), PTR_ERR(parent));
1939 struct lu_object *ld_parent;
1942 e = strsep(&path, "/");
1947 if (!path || path[0] == '\0')
1953 lname->ln_namelen = strlen(e);
1955 ld_parent = lu_object_locate(parent->lo_header, ld->ld_type);
1956 if (ld_parent == NULL) {
1957 lu_object_put(env, parent);
1962 child = echo_md_lookup(env, ed, lu2md(ld_parent), lname);
1963 lu_object_put(env, parent);
1964 if (IS_ERR(child)) {
1965 rc = (int)PTR_ERR(child);
1966 CERROR("lookup %s under parent "DFID": rc = %d\n",
1967 lname->ln_name, PFID(lu_object_fid(ld_parent)),
1974 RETURN(ERR_PTR(rc));
1979 static void echo_ucred_init(struct lu_env *env)
1981 struct lu_ucred *ucred = lu_ucred(env);
1983 ucred->uc_valid = UCRED_INVALID;
1985 ucred->uc_suppgids[0] = -1;
1986 ucred->uc_suppgids[1] = -1;
1988 ucred->uc_uid = ucred->uc_o_uid =
1989 from_kuid(&init_user_ns, current_uid());
1990 ucred->uc_gid = ucred->uc_o_gid =
1991 from_kgid(&init_user_ns, current_gid());
1992 ucred->uc_fsuid = ucred->uc_o_fsuid =
1993 from_kuid(&init_user_ns, current_fsuid());
1994 ucred->uc_fsgid = ucred->uc_o_fsgid =
1995 from_kgid(&init_user_ns, current_fsgid());
1996 ucred->uc_cap = cfs_curproc_cap_pack();
1998 /* remove fs privilege for non-root user. */
1999 if (ucred->uc_fsuid)
2000 ucred->uc_cap &= ~CFS_CAP_FS_MASK;
2001 ucred->uc_valid = UCRED_NEW;
2004 static void echo_ucred_fini(struct lu_env *env)
2006 struct lu_ucred *ucred = lu_ucred(env);
2007 ucred->uc_valid = UCRED_INIT;
2010 #define ECHO_MD_CTX_TAG (LCT_REMEMBER | LCT_MD_THREAD)
2011 #define ECHO_MD_SES_TAG (LCT_REMEMBER | LCT_SESSION | LCT_SERVER_SESSION)
2012 static int echo_md_handler(struct echo_device *ed, int command,
2013 char *path, int path_len, __u64 id, int count,
2014 struct obd_ioctl_data *data)
2016 struct echo_thread_info *info;
2017 struct lu_device *ld = ed->ed_next;
2020 struct lu_object *parent;
2022 int namelen = data->ioc_plen2;
2027 CERROR("MD echo client is not being initialized properly\n");
2031 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
2032 CERROR("Only support MDD layer right now!\n");
2036 env = cl_env_get(&refcheck);
2038 RETURN(PTR_ERR(env));
2040 rc = lu_env_refill_by_tags(env, ECHO_MD_CTX_TAG, ECHO_MD_SES_TAG);
2044 /* init big_lmm buffer */
2045 info = echo_env_info(env);
2046 LASSERT(info->eti_big_lmm == NULL);
2047 OBD_ALLOC_LARGE(info->eti_big_lmm, MIN_MD_SIZE);
2048 if (info->eti_big_lmm == NULL)
2049 GOTO(out_env, rc = -ENOMEM);
2050 info->eti_big_lmmsize = MIN_MD_SIZE;
2052 parent = echo_resolve_path(env, ed, path, path_len);
2053 if (IS_ERR(parent)) {
2054 CERROR("Can not resolve the path %s: rc = %ld\n", path,
2056 GOTO(out_free, rc = PTR_ERR(parent));
2060 OBD_ALLOC(name, namelen + 1);
2062 GOTO(out_put, rc = -ENOMEM);
2063 if (copy_from_user(name, data->ioc_pbuf2, namelen))
2064 GOTO(out_name, rc = -EFAULT);
2067 echo_ucred_init(env);
2070 case ECHO_MD_CREATE:
2071 case ECHO_MD_MKDIR: {
2072 struct echo_thread_info *info = echo_env_info(env);
2073 __u32 mode = data->ioc_obdo2.o_mode;
2074 struct lu_fid *fid = &info->eti_fid;
2075 int stripe_count = (int)data->ioc_obdo2.o_misc;
2076 int stripe_index = (int)data->ioc_obdo2.o_stripe_idx;
2078 rc = ostid_to_fid(fid, &data->ioc_obdo1.o_oi, 0);
2082 /* In the function below, .hs_keycmp resolves to
2083 * lu_obj_hop_keycmp() */
2084 /* coverity[overrun-buffer-val] */
2085 rc = echo_create_md_object(env, ed, parent, fid, name, namelen,
2086 id, mode, count, stripe_count,
2090 case ECHO_MD_DESTROY:
2091 case ECHO_MD_RMDIR: {
2092 __u32 mode = data->ioc_obdo2.o_mode;
2094 rc = echo_destroy_object(env, ed, parent, name, namelen,
2098 case ECHO_MD_LOOKUP:
2099 rc = echo_lookup_object(env, ed, parent, id, count);
2101 case ECHO_MD_GETATTR:
2102 rc = echo_getattr_object(env, ed, parent, id, count);
2104 case ECHO_MD_SETATTR:
2105 rc = echo_setattr_object(env, ed, parent, id, count);
2108 CERROR("unknown command %d\n", command);
2112 echo_ucred_fini(env);
2116 OBD_FREE(name, namelen + 1);
2118 lu_object_put(env, parent);
2120 LASSERT(info->eti_big_lmm);
2121 OBD_FREE_LARGE(info->eti_big_lmm, info->eti_big_lmmsize);
2122 info->eti_big_lmm = NULL;
2123 info->eti_big_lmmsize = 0;
2125 cl_env_put(env, &refcheck);
2128 #endif /* HAVE_SERVER_SUPPORT */
2130 static int echo_create_object(const struct lu_env *env, struct echo_device *ed,
2131 int on_target, struct obdo *oa, void *ulsm,
2132 int ulsm_nob, struct obd_trans_info *oti)
2134 struct echo_object *eco;
2135 struct echo_client_obd *ec = ed->ed_ec;
2136 struct lov_stripe_md *lsm = NULL;
2141 if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
2142 (on_target || /* set_stripe */
2143 ec->ec_nstripes != 0)) { /* LOV */
2144 CERROR ("No valid oid\n");
2148 rc = echo_alloc_memmd(ed, &lsm);
2150 CERROR("Cannot allocate md: rc = %d\n", rc);
2157 rc = echo_copyin_lsm (ed, lsm, ulsm, ulsm_nob);
2161 if (lsm->lsm_stripe_count == 0)
2162 lsm->lsm_stripe_count = ec->ec_nstripes;
2164 if (lsm->lsm_stripe_size == 0)
2165 lsm->lsm_stripe_size = PAGE_CACHE_SIZE;
2169 /* setup stripes: indices + default ids if required */
2170 for (i = 0; i < lsm->lsm_stripe_count; i++) {
2171 if (ostid_id(&lsm->lsm_oinfo[i]->loi_oi) == 0)
2172 lsm->lsm_oinfo[i]->loi_oi = lsm->lsm_oi;
2174 lsm->lsm_oinfo[i]->loi_ost_idx =
2175 (idx + i) % ec->ec_nstripes;
2179 /* setup object ID here for !on_target and LOV hint */
2180 if (oa->o_valid & OBD_MD_FLID) {
2181 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2182 lsm->lsm_oi = oa->o_oi;
2185 if (ostid_id(&lsm->lsm_oi) == 0)
2186 ostid_set_id(&lsm->lsm_oi, ++last_object_id);
2190 /* Only echo objects are allowed to be created */
2191 LASSERT((oa->o_valid & OBD_MD_FLGROUP) &&
2192 (ostid_seq(&oa->o_oi) == FID_SEQ_ECHO));
2193 rc = obd_create(env, ec->ec_exp, oa, &lsm, oti);
2195 CERROR("Cannot create objects: rc = %d\n", rc);
2201 /* See what object ID we were given */
2202 oa->o_oi = lsm->lsm_oi;
2203 oa->o_valid |= OBD_MD_FLID;
2205 eco = cl_echo_object_find(ed, &lsm);
2207 GOTO(failed, rc = PTR_ERR(eco));
2208 cl_echo_object_put(eco);
2210 CDEBUG(D_INFO, "oa oid "DOSTID"\n", POSTID(&oa->o_oi));
2215 obd_destroy(env, ec->ec_exp, oa, lsm, oti, NULL, NULL);
2217 echo_free_memmd(ed, &lsm);
2219 CERROR("create object failed with: rc = %d\n", rc);
2223 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
2226 struct lov_stripe_md *lsm = NULL;
2227 struct echo_object *eco;
2231 if ((oa->o_valid & OBD_MD_FLID) == 0 || ostid_id(&oa->o_oi) == 0) {
2232 /* disallow use of object id 0 */
2233 CERROR ("No valid oid\n");
2237 rc = echo_alloc_memmd(ed, &lsm);
2241 lsm->lsm_oi = oa->o_oi;
2242 if (!(oa->o_valid & OBD_MD_FLGROUP))
2243 ostid_set_seq_echo(&lsm->lsm_oi);
2246 eco = cl_echo_object_find(ed, &lsm);
2252 echo_free_memmd(ed, &lsm);
2256 static void echo_put_object(struct echo_object *eco)
2260 rc = cl_echo_object_put(eco);
2262 CERROR("%s: echo client drop an object failed: rc = %d\n",
2263 eco->eo_dev->ed_ec->ec_exp->exp_obd->obd_name, rc);
2267 echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
2269 unsigned long stripe_count;
2270 unsigned long stripe_size;
2271 unsigned long width;
2272 unsigned long woffset;
2276 if (lsm->lsm_stripe_count <= 1)
2280 stripe_size = lsm->lsm_stripe_size;
2281 stripe_count = lsm->lsm_stripe_count;
2283 /* width = # bytes in all stripes */
2284 width = stripe_size * stripe_count;
2286 /* woffset = offset within a width; offset = whole number of widths */
2287 woffset = do_div (offset, width);
2289 stripe_index = woffset / stripe_size;
2291 *idp = ostid_id(&lsm->lsm_oinfo[stripe_index]->loi_oi);
2292 *offp = offset * stripe_size + woffset % stripe_size;
2296 echo_client_page_debug_setup(struct lov_stripe_md *lsm,
2297 struct page *page, int rw, obd_id id,
2298 obd_off offset, obd_off count)
2305 /* no partial pages on the client */
2306 LASSERT(count == PAGE_CACHE_SIZE);
2310 for (delta = 0; delta < PAGE_CACHE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2311 if (rw == OBD_BRW_WRITE) {
2312 stripe_off = offset + delta;
2314 echo_get_stripe_off_id(lsm, &stripe_off, &stripe_id);
2316 stripe_off = 0xdeadbeef00c0ffeeULL;
2317 stripe_id = 0xdeadbeef00c0ffeeULL;
2319 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
2320 stripe_off, stripe_id);
2326 static int echo_client_page_debug_check(struct lov_stripe_md *lsm,
2327 struct page *page, obd_id id,
2328 obd_off offset, obd_off count)
2337 /* no partial pages on the client */
2338 LASSERT(count == PAGE_CACHE_SIZE);
2342 for (rc = delta = 0; delta < PAGE_CACHE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2343 stripe_off = offset + delta;
2345 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
2347 rc2 = block_debug_check("test_brw",
2348 addr + delta, OBD_ECHO_BLOCK_SIZE,
2349 stripe_off, stripe_id);
2351 CERROR ("Error in echo object "LPX64"\n", id);
2360 static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
2361 struct echo_object *eco, obd_off offset,
2362 obd_size count, int async,
2363 struct obd_trans_info *oti)
2365 struct lov_stripe_md *lsm = eco->eo_lsm;
2367 struct brw_page *pga;
2368 struct brw_page *pgp;
2369 struct page **pages;
2378 verify = (ostid_id(&oa->o_oi) != ECHO_PERSISTENT_OBJID &&
2379 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
2380 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
2382 gfp_mask = ((ostid_id(&oa->o_oi) & 2) == 0) ? GFP_IOFS : GFP_HIGHUSER;
2384 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
2385 LASSERT(lsm != NULL);
2386 LASSERT(ostid_id(&lsm->lsm_oi) == ostid_id(&oa->o_oi));
2389 (count & (~CFS_PAGE_MASK)) != 0)
2392 /* XXX think again with misaligned I/O */
2393 npages = count >> PAGE_CACHE_SHIFT;
2395 if (rw == OBD_BRW_WRITE)
2396 brw_flags = OBD_BRW_ASYNC;
2398 OBD_ALLOC(pga, npages * sizeof(*pga));
2402 OBD_ALLOC(pages, npages * sizeof(*pages));
2403 if (pages == NULL) {
2404 OBD_FREE(pga, npages * sizeof(*pga));
2408 for (i = 0, pgp = pga, off = offset;
2410 i++, pgp++, off += PAGE_CACHE_SIZE) {
2412 LASSERT (pgp->pg == NULL); /* for cleanup */
2415 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
2416 if (pgp->pg == NULL)
2420 pgp->count = PAGE_CACHE_SIZE;
2422 pgp->flag = brw_flags;
2425 echo_client_page_debug_setup(lsm, pgp->pg, rw,
2426 ostid_id(&oa->o_oi), off,
2430 /* brw mode can only be used at client */
2431 LASSERT(ed->ed_next != NULL);
2432 rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
2435 if (rc != 0 || rw != OBD_BRW_READ)
2438 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
2439 if (pgp->pg == NULL)
2444 vrc = echo_client_page_debug_check(lsm, pgp->pg,
2445 ostid_id(&oa->o_oi),
2446 pgp->off, pgp->count);
2447 if (vrc != 0 && rc == 0)
2450 OBD_PAGE_FREE(pgp->pg);
2452 OBD_FREE(pga, npages * sizeof(*pga));
2453 OBD_FREE(pages, npages * sizeof(*pages));
2457 static int echo_client_prep_commit(const struct lu_env *env,
2458 struct obd_export *exp, int rw,
2459 struct obdo *oa, struct echo_object *eco,
2460 obd_off offset, obd_size count,
2461 obd_size batch, struct obd_trans_info *oti,
2464 struct lov_stripe_md *lsm = eco->eo_lsm;
2465 struct obd_ioobj ioo;
2466 struct niobuf_local *lnb;
2467 struct niobuf_remote *rnb;
2469 obd_size npages, tot_pages;
2470 int i, ret = 0, brw_flags = 0;
2474 if (count <= 0 || (count & (~CFS_PAGE_MASK)) != 0 ||
2475 (lsm != NULL && ostid_id(&lsm->lsm_oi) != ostid_id(&oa->o_oi)))
2478 npages = batch >> PAGE_CACHE_SHIFT;
2479 tot_pages = count >> PAGE_CACHE_SHIFT;
2481 OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
2482 OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
2484 if (lnb == NULL || rnb == NULL)
2485 GOTO(out, ret = -ENOMEM);
2487 if (rw == OBD_BRW_WRITE && async)
2488 brw_flags |= OBD_BRW_ASYNC;
2490 obdo_to_ioobj(oa, &ioo);
2494 for(; tot_pages; tot_pages -= npages) {
2497 if (tot_pages < npages)
2500 for (i = 0; i < npages; i++, off += PAGE_CACHE_SIZE) {
2501 rnb[i].rnb_offset = off;
2502 rnb[i].rnb_len = PAGE_CACHE_SIZE;
2503 rnb[i].rnb_flags = brw_flags;
2506 ioo.ioo_bufcnt = npages;
2509 ret = obd_preprw(env, rw, exp, oa, 1, &ioo, rnb, &lpages,
2513 LASSERT(lpages == npages);
2515 for (i = 0; i < lpages; i++) {
2516 struct page *page = lnb[i].lnb_page;
2518 /* read past eof? */
2519 if (page == NULL && lnb[i].lnb_rc == 0)
2523 lnb[i].lnb_flags |= OBD_BRW_ASYNC;
2525 if (ostid_id(&oa->o_oi) == ECHO_PERSISTENT_OBJID ||
2526 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
2527 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
2530 if (rw == OBD_BRW_WRITE)
2531 echo_client_page_debug_setup(lsm, page, rw,
2532 ostid_id(&oa->o_oi),
2536 echo_client_page_debug_check(lsm, page,
2537 ostid_id(&oa->o_oi),
2542 ret = obd_commitrw(env, rw, exp, oa, 1, &ioo,
2543 rnb, npages, lnb, oti, ret);
2547 /* Reset oti otherwise it would confuse ldiskfs. */
2548 memset(oti, 0, sizeof(*oti));
2550 /* Reuse env context. */
2551 lu_context_exit((struct lu_context *)&env->le_ctx);
2552 lu_context_enter((struct lu_context *)&env->le_ctx);
2557 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
2559 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
2563 static int echo_client_brw_ioctl(const struct lu_env *env, int rw,
2564 struct obd_export *exp,
2565 struct obd_ioctl_data *data,
2566 struct obd_trans_info *dummy_oti)
2568 struct obd_device *obd = class_exp2obd(exp);
2569 struct echo_device *ed = obd2echo_dev(obd);
2570 struct echo_client_obd *ec = ed->ed_ec;
2571 struct obdo *oa = &data->ioc_obdo1;
2572 struct echo_object *eco;
2578 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2580 rc = echo_get_object(&eco, ed, oa);
2584 oa->o_valid &= ~OBD_MD_FLHANDLE;
2586 /* OFD/obdfilter works only via prep/commit */
2587 test_mode = (long)data->ioc_pbuf1;
2588 if (ed->ed_next == NULL && test_mode != 3) {
2590 data->ioc_plen1 = data->ioc_count;
2596 /* Truncate batch size to maximum */
2597 if (data->ioc_plen1 > PTLRPC_MAX_BRW_SIZE)
2598 data->ioc_plen1 = PTLRPC_MAX_BRW_SIZE;
2600 switch (test_mode) {
2604 rc = echo_client_kbrw(ed, rw, oa,
2605 eco, data->ioc_offset,
2606 data->ioc_count, async, dummy_oti);
2609 rc = echo_client_prep_commit(env, ec->ec_exp, rw, oa,
2610 eco, data->ioc_offset,
2611 data->ioc_count, data->ioc_plen1,
2617 echo_put_object(eco);
2622 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2623 void *karg, void *uarg)
2625 #ifdef HAVE_SERVER_SUPPORT
2626 struct tgt_session_info *tsi;
2628 struct obd_device *obd = exp->exp_obd;
2629 struct echo_device *ed = obd2echo_dev(obd);
2630 struct echo_client_obd *ec = ed->ed_ec;
2631 struct echo_object *eco;
2632 struct obd_ioctl_data *data = karg;
2633 struct obd_trans_info dummy_oti;
2635 struct oti_req_ack_lock *ack_lock;
2638 int rw = OBD_BRW_READ;
2641 #ifdef HAVE_SERVER_SUPPORT
2642 struct lu_context echo_session;
2646 memset(&dummy_oti, 0, sizeof(dummy_oti));
2648 oa = &data->ioc_obdo1;
2649 if (!(oa->o_valid & OBD_MD_FLGROUP)) {
2650 oa->o_valid |= OBD_MD_FLGROUP;
2651 ostid_set_seq_echo(&oa->o_oi);
2654 /* This FID is unpacked just for validation at this point */
2655 rc = ostid_to_fid(&fid, &oa->o_oi, 0);
2663 rc = lu_env_init(env, LCT_DT_THREAD);
2665 GOTO(out_alloc, rc = -ENOMEM);
2667 #ifdef HAVE_SERVER_SUPPORT
2668 env->le_ses = &echo_session;
2669 rc = lu_context_init(env->le_ses, LCT_SERVER_SESSION | LCT_NOREF);
2670 if (unlikely(rc < 0))
2672 lu_context_enter(env->le_ses);
2674 tsi = tgt_ses_info(env);
2675 tsi->tsi_exp = ec->ec_exp;
2676 tsi->tsi_jobid = NULL;
2679 case OBD_IOC_CREATE: /* may create echo object */
2680 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2681 GOTO (out, rc = -EPERM);
2683 rc = echo_create_object(env, ed, 1, oa, data->ioc_pbuf1,
2684 data->ioc_plen1, &dummy_oti);
2687 #ifdef HAVE_SERVER_SUPPORT
2688 case OBD_IOC_ECHO_MD: {
2695 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2696 GOTO(out, rc = -EPERM);
2698 count = data->ioc_count;
2699 cmd = data->ioc_command;
2701 id = data->ioc_obdo2.o_oi.oi.oi_id;
2702 dirlen = data->ioc_plen1;
2703 OBD_ALLOC(dir, dirlen + 1);
2705 GOTO(out, rc = -ENOMEM);
2707 if (copy_from_user(dir, data->ioc_pbuf1, dirlen)) {
2708 OBD_FREE(dir, data->ioc_plen1 + 1);
2709 GOTO(out, rc = -EFAULT);
2712 rc = echo_md_handler(ed, cmd, dir, dirlen, id, count, data);
2713 OBD_FREE(dir, dirlen + 1);
2716 case OBD_IOC_ECHO_ALLOC_SEQ: {
2717 struct lu_env *cl_env;
2722 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2723 GOTO(out, rc = -EPERM);
2725 cl_env = cl_env_get(&refcheck);
2727 GOTO(out, rc = PTR_ERR(cl_env));
2729 rc = lu_env_refill_by_tags(cl_env, ECHO_MD_CTX_TAG,
2732 cl_env_put(cl_env, &refcheck);
2736 rc = seq_client_get_seq(cl_env, ed->ed_cl_seq, &seq);
2737 cl_env_put(cl_env, &refcheck);
2739 CERROR("%s: Can not alloc seq: rc = %d\n",
2744 if (copy_to_user(data->ioc_pbuf1, &seq, data->ioc_plen1))
2747 max_count = LUSTRE_METADATA_SEQ_MAX_WIDTH;
2748 if (copy_to_user(data->ioc_pbuf2, &max_count,
2753 #endif /* HAVE_SERVER_SUPPORT */
2754 case OBD_IOC_DESTROY:
2755 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2756 GOTO (out, rc = -EPERM);
2758 rc = echo_get_object(&eco, ed, oa);
2760 rc = obd_destroy(env, ec->ec_exp, oa, eco->eo_lsm,
2761 &dummy_oti, NULL, NULL);
2763 eco->eo_deleted = 1;
2764 echo_put_object(eco);
2768 case OBD_IOC_GETATTR:
2769 rc = echo_get_object(&eco, ed, oa);
2771 struct obd_info oinfo = { { { 0 } } };
2772 oinfo.oi_md = eco->eo_lsm;
2774 rc = obd_getattr(env, ec->ec_exp, &oinfo);
2775 echo_put_object(eco);
2779 case OBD_IOC_SETATTR:
2780 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2781 GOTO (out, rc = -EPERM);
2783 rc = echo_get_object(&eco, ed, oa);
2785 struct obd_info oinfo = { { { 0 } } };
2787 oinfo.oi_md = eco->eo_lsm;
2789 rc = obd_setattr(env, ec->ec_exp, &oinfo, NULL);
2790 echo_put_object(eco);
2794 case OBD_IOC_BRW_WRITE:
2795 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2796 GOTO (out, rc = -EPERM);
2800 case OBD_IOC_BRW_READ:
2801 rc = echo_client_brw_ioctl(env, rw, exp, data, &dummy_oti);
2805 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
2806 GOTO (out, rc = -ENOTTY);
2811 #ifdef HAVE_SERVER_SUPPORT
2812 lu_context_exit(env->le_ses);
2813 lu_context_fini(env->le_ses);
2820 /* XXX this should be in a helper also called by target_send_reply */
2821 for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
2823 if (!ack_lock->mode)
2825 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
2831 static int echo_client_setup(const struct lu_env *env,
2832 struct obd_device *obddev, struct lustre_cfg *lcfg)
2834 struct echo_client_obd *ec = &obddev->u.echo_client;
2835 struct obd_device *tgt;
2836 struct obd_uuid echo_uuid = { "ECHO_UUID" };
2837 struct obd_connect_data *ocd = NULL;
2841 if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
2842 CERROR("requires a TARGET OBD name\n");
2846 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
2847 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
2848 CERROR("device not attached or not set up (%s)\n",
2849 lustre_cfg_string(lcfg, 1));
2853 spin_lock_init(&ec->ec_lock);
2854 INIT_LIST_HEAD(&ec->ec_objects);
2855 INIT_LIST_HEAD(&ec->ec_locks);
2857 ec->ec_nstripes = 0;
2859 if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
2860 #ifdef HAVE_SERVER_SUPPORT
2861 lu_context_tags_update(ECHO_MD_CTX_TAG);
2862 lu_session_tags_update(ECHO_MD_SES_TAG);
2864 CERROR("Local operations are NOT supported on client side. "
2865 "Only remote operations are supported. Metadata client "
2866 "must be run on server side.\n");
2871 OBD_ALLOC(ocd, sizeof(*ocd));
2873 CERROR("Can't alloc ocd connecting to %s\n",
2874 lustre_cfg_string(lcfg, 1));
2878 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
2879 OBD_CONNECT_BRW_SIZE |
2880 OBD_CONNECT_GRANT | OBD_CONNECT_FULL20 |
2881 OBD_CONNECT_64BITHASH | OBD_CONNECT_LVB_TYPE |
2883 ocd->ocd_brw_size = DT_MAX_BRW_SIZE;
2884 ocd->ocd_version = LUSTRE_VERSION_CODE;
2885 ocd->ocd_group = FID_SEQ_ECHO;
2887 rc = obd_connect(env, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
2889 /* Turn off pinger because it connects to tgt obd directly. */
2890 spin_lock(&tgt->obd_dev_lock);
2891 list_del_init(&ec->ec_exp->exp_obd_chain_timed);
2892 spin_unlock(&tgt->obd_dev_lock);
2895 OBD_FREE(ocd, sizeof(*ocd));
2898 CERROR("fail to connect to device %s\n",
2899 lustre_cfg_string(lcfg, 1));
2906 static int echo_client_cleanup(struct obd_device *obddev)
2908 struct echo_device *ed = obd2echo_dev(obddev);
2909 struct echo_client_obd *ec = &obddev->u.echo_client;
2913 /*Do nothing for Metadata echo client*/
2917 if (ed->ed_next_ismd) {
2918 #ifdef HAVE_SERVER_SUPPORT
2919 lu_context_tags_clear(ECHO_MD_CTX_TAG);
2920 lu_session_tags_clear(ECHO_MD_SES_TAG);
2922 CERROR("This is client-side only module, does not support "
2923 "metadata echo client.\n");
2928 if (!list_empty(&obddev->obd_exports)) {
2929 CERROR("still has clients!\n");
2933 LASSERT(atomic_read(&ec->ec_exp->exp_refcount) > 0);
2934 rc = obd_disconnect(ec->ec_exp);
2936 CERROR("fail to disconnect device: %d\n", rc);
2941 static int echo_client_connect(const struct lu_env *env,
2942 struct obd_export **exp,
2943 struct obd_device *src, struct obd_uuid *cluuid,
2944 struct obd_connect_data *data, void *localdata)
2947 struct lustre_handle conn = { 0 };
2950 rc = class_connect(&conn, src, cluuid);
2952 *exp = class_conn2export(&conn);
2958 static int echo_client_disconnect(struct obd_export *exp)
2964 GOTO(out, rc = -EINVAL);
2966 rc = class_disconnect(exp);
2972 static struct obd_ops echo_client_obd_ops = {
2973 .o_owner = THIS_MODULE,
2974 .o_iocontrol = echo_client_iocontrol,
2975 .o_connect = echo_client_connect,
2976 .o_disconnect = echo_client_disconnect
2979 int echo_client_init(void)
2983 rc = lu_kmem_init(echo_caches);
2985 rc = class_register_type(&echo_client_obd_ops, NULL, true, NULL,
2986 LUSTRE_ECHO_CLIENT_NAME,
2989 lu_kmem_fini(echo_caches);
2994 void echo_client_exit(void)
2996 class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
2997 lu_kmem_fini(echo_caches);
3000 static int __init obdecho_init(void)
3005 LCONSOLE_INFO("Echo OBD driver; http://www.lustre.org/\n");
3007 LASSERT(PAGE_CACHE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
3009 # ifdef HAVE_SERVER_SUPPORT
3010 rc = echo_persistent_pages_init();
3014 rc = class_register_type(&echo_obd_ops, NULL, true, NULL,
3015 LUSTRE_ECHO_NAME, NULL);
3020 rc = echo_client_init();
3022 # ifdef HAVE_SERVER_SUPPORT
3026 class_unregister_type(LUSTRE_ECHO_NAME);
3028 echo_persistent_pages_fini();
3034 static void /*__exit*/ obdecho_exit(void)
3038 # ifdef HAVE_SERVER_SUPPORT
3039 class_unregister_type(LUSTRE_ECHO_NAME);
3040 echo_persistent_pages_fini();
3044 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3045 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
3046 MODULE_LICENSE("GPL");
3048 cfs_module(obdecho, LUSTRE_VERSION_STRING, obdecho_init, obdecho_exit);
3050 /** @} echo_client */