4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_ECHO
39 #include <libcfs/libcfs.h>
41 #include <liblustre.h>
45 #include <obd_support.h>
46 #include <obd_class.h>
47 #include <lustre_debug.h>
48 #include <lprocfs_status.h>
49 #include <cl_object.h>
50 #include <md_object.h>
51 #include <lustre_fid.h>
52 #include <lustre_acl.h>
53 #include <lustre_net.h>
56 #include "echo_internal.h"
58 /** \defgroup echo_client Echo Client
63 struct cl_device ed_cl;
64 struct echo_client_obd *ed_ec;
66 struct cl_site ed_site_myself;
67 struct cl_site *ed_site;
68 struct lu_device *ed_next;
71 struct lu_client_seq *ed_cl_seq;
75 struct cl_object eo_cl;
76 struct cl_object_header eo_hdr;
78 struct echo_device *eo_dev;
79 cfs_list_t eo_obj_chain;
80 struct lov_stripe_md *eo_lsm;
85 struct echo_object_conf {
86 struct cl_object_conf eoc_cl;
87 struct lov_stripe_md **eoc_md;
91 struct cl_page_slice ep_cl;
96 struct cl_lock_slice el_cl;
98 struct echo_object *el_object;
100 atomic_t el_refcount;
103 static int echo_client_setup(const struct lu_env *env,
104 struct obd_device *obddev,
105 struct lustre_cfg *lcfg);
106 static int echo_client_cleanup(struct obd_device *obddev);
109 /** \defgroup echo_helpers Helper functions
112 static inline struct echo_device *cl2echo_dev(const struct cl_device *dev)
114 return container_of0(dev, struct echo_device, ed_cl);
117 static inline struct cl_device *echo_dev2cl(struct echo_device *d)
122 static inline struct echo_device *obd2echo_dev(const struct obd_device *obd)
124 return cl2echo_dev(lu2cl_dev(obd->obd_lu_dev));
127 static inline struct cl_object *echo_obj2cl(struct echo_object *eco)
132 static inline struct echo_object *cl2echo_obj(const struct cl_object *o)
134 return container_of(o, struct echo_object, eo_cl);
137 static inline struct echo_page *cl2echo_page(const struct cl_page_slice *s)
139 return container_of(s, struct echo_page, ep_cl);
142 static inline struct echo_lock *cl2echo_lock(const struct cl_lock_slice *s)
144 return container_of(s, struct echo_lock, el_cl);
147 static inline struct cl_lock *echo_lock2cl(const struct echo_lock *ecl)
149 return ecl->el_cl.cls_lock;
152 static struct lu_context_key echo_thread_key;
153 static inline struct echo_thread_info *echo_env_info(const struct lu_env *env)
155 struct echo_thread_info *info;
156 info = lu_context_key_get(&env->le_ctx, &echo_thread_key);
157 LASSERT(info != NULL);
162 struct echo_object_conf *cl2echo_conf(const struct cl_object_conf *c)
164 return container_of(c, struct echo_object_conf, eoc_cl);
167 /** @} echo_helpers */
169 static struct echo_object *cl_echo_object_find(struct echo_device *d,
170 struct lov_stripe_md **lsm);
171 static int cl_echo_object_put(struct echo_object *eco);
172 static int cl_echo_enqueue (struct echo_object *eco, obd_off start,
173 obd_off end, int mode, __u64 *cookie);
174 static int cl_echo_cancel (struct echo_device *d, __u64 cookie);
175 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
176 struct page **pages, int npages, int async);
178 struct echo_thread_info {
179 struct echo_object_conf eti_conf;
180 struct lustre_md eti_md;
182 struct cl_2queue eti_queue;
184 struct cl_lock_descr eti_descr;
185 struct lu_fid eti_fid;
186 struct lu_fid eti_fid2;
187 #ifdef HAVE_SERVER_SUPPORT
188 struct md_op_spec eti_spec;
189 struct lov_mds_md_v3 eti_lmm;
190 struct lov_user_md_v3 eti_lum;
191 struct md_attr eti_ma;
192 struct lu_name eti_lname;
193 /* per-thread values, can be re-used */
197 struct lu_buf eti_buf;
198 char eti_xattr_buf[LUSTRE_POSIX_ACL_MAX_SIZE];
202 /* No session used right now */
203 struct echo_session_info {
207 static struct kmem_cache *echo_lock_kmem;
208 static struct kmem_cache *echo_object_kmem;
209 static struct kmem_cache *echo_thread_kmem;
210 static struct kmem_cache *echo_session_kmem;
211 /* static struct kmem_cache *echo_req_kmem; */
213 static struct lu_kmem_descr echo_caches[] = {
215 .ckd_cache = &echo_lock_kmem,
216 .ckd_name = "echo_lock_kmem",
217 .ckd_size = sizeof (struct echo_lock)
220 .ckd_cache = &echo_object_kmem,
221 .ckd_name = "echo_object_kmem",
222 .ckd_size = sizeof (struct echo_object)
225 .ckd_cache = &echo_thread_kmem,
226 .ckd_name = "echo_thread_kmem",
227 .ckd_size = sizeof (struct echo_thread_info)
230 .ckd_cache = &echo_session_kmem,
231 .ckd_name = "echo_session_kmem",
232 .ckd_size = sizeof (struct echo_session_info)
239 /** \defgroup echo_page Page operations
241 * Echo page operations.
245 static int echo_page_own(const struct lu_env *env,
246 const struct cl_page_slice *slice,
247 struct cl_io *io, int nonblock)
249 struct echo_page *ep = cl2echo_page(slice);
252 mutex_lock(&ep->ep_lock);
253 else if (!mutex_trylock(&ep->ep_lock))
258 static void echo_page_disown(const struct lu_env *env,
259 const struct cl_page_slice *slice,
262 struct echo_page *ep = cl2echo_page(slice);
264 LASSERT(mutex_is_locked(&ep->ep_lock));
265 mutex_unlock(&ep->ep_lock);
268 static void echo_page_discard(const struct lu_env *env,
269 const struct cl_page_slice *slice,
270 struct cl_io *unused)
272 cl_page_delete(env, slice->cpl_page);
275 static int echo_page_is_vmlocked(const struct lu_env *env,
276 const struct cl_page_slice *slice)
278 if (mutex_is_locked(&cl2echo_page(slice)->ep_lock))
283 static void echo_page_completion(const struct lu_env *env,
284 const struct cl_page_slice *slice,
287 LASSERT(slice->cpl_page->cp_sync_io != NULL);
290 static void echo_page_fini(const struct lu_env *env,
291 struct cl_page_slice *slice)
293 struct echo_object *eco = cl2echo_obj(slice->cpl_obj);
296 atomic_dec(&eco->eo_npages);
297 page_cache_release(slice->cpl_page->cp_vmpage);
301 static int echo_page_prep(const struct lu_env *env,
302 const struct cl_page_slice *slice,
303 struct cl_io *unused)
308 static int echo_page_print(const struct lu_env *env,
309 const struct cl_page_slice *slice,
310 void *cookie, lu_printer_t printer)
312 struct echo_page *ep = cl2echo_page(slice);
314 (*printer)(env, cookie, LUSTRE_ECHO_CLIENT_NAME"-page@%p %d vm@%p\n",
315 ep, mutex_is_locked(&ep->ep_lock),
316 slice->cpl_page->cp_vmpage);
320 static const struct cl_page_operations echo_page_ops = {
321 .cpo_own = echo_page_own,
322 .cpo_disown = echo_page_disown,
323 .cpo_discard = echo_page_discard,
324 .cpo_fini = echo_page_fini,
325 .cpo_print = echo_page_print,
326 .cpo_is_vmlocked = echo_page_is_vmlocked,
329 .cpo_prep = echo_page_prep,
330 .cpo_completion = echo_page_completion,
333 .cpo_prep = echo_page_prep,
334 .cpo_completion = echo_page_completion,
340 /** \defgroup echo_lock Locking
342 * echo lock operations
346 static void echo_lock_fini(const struct lu_env *env,
347 struct cl_lock_slice *slice)
349 struct echo_lock *ecl = cl2echo_lock(slice);
351 LASSERT(cfs_list_empty(&ecl->el_chain));
352 OBD_SLAB_FREE_PTR(ecl, echo_lock_kmem);
355 static void echo_lock_delete(const struct lu_env *env,
356 const struct cl_lock_slice *slice)
358 struct echo_lock *ecl = cl2echo_lock(slice);
360 LASSERT(cfs_list_empty(&ecl->el_chain));
363 static int echo_lock_fits_into(const struct lu_env *env,
364 const struct cl_lock_slice *slice,
365 const struct cl_lock_descr *need,
366 const struct cl_io *unused)
371 static struct cl_lock_operations echo_lock_ops = {
372 .clo_fini = echo_lock_fini,
373 .clo_delete = echo_lock_delete,
374 .clo_fits_into = echo_lock_fits_into
379 /** \defgroup echo_cl_ops cl_object operations
381 * operations for cl_object
385 static int echo_page_init(const struct lu_env *env, struct cl_object *obj,
386 struct cl_page *page, pgoff_t index)
388 struct echo_page *ep = cl_object_page_slice(obj, page);
389 struct echo_object *eco = cl2echo_obj(obj);
392 page_cache_get(page->cp_vmpage);
393 mutex_init(&ep->ep_lock);
394 cl_page_slice_add(page, &ep->ep_cl, obj, &echo_page_ops);
395 atomic_inc(&eco->eo_npages);
399 static int echo_io_init(const struct lu_env *env, struct cl_object *obj,
405 static int echo_lock_init(const struct lu_env *env,
406 struct cl_object *obj, struct cl_lock *lock,
407 const struct cl_io *unused)
409 struct echo_lock *el;
412 OBD_SLAB_ALLOC_PTR_GFP(el, echo_lock_kmem, __GFP_IO);
414 cl_lock_slice_add(lock, &el->el_cl, obj, &echo_lock_ops);
415 el->el_object = cl2echo_obj(obj);
416 CFS_INIT_LIST_HEAD(&el->el_chain);
417 atomic_set(&el->el_refcount, 0);
419 RETURN(el == NULL ? -ENOMEM : 0);
422 static int echo_conf_set(const struct lu_env *env, struct cl_object *obj,
423 const struct cl_object_conf *conf)
428 static const struct cl_object_operations echo_cl_obj_ops = {
429 .coo_page_init = echo_page_init,
430 .coo_lock_init = echo_lock_init,
431 .coo_io_init = echo_io_init,
432 .coo_conf_set = echo_conf_set
434 /** @} echo_cl_ops */
436 /** \defgroup echo_lu_ops lu_object operations
438 * operations for echo lu object.
442 static int echo_object_init(const struct lu_env *env, struct lu_object *obj,
443 const struct lu_object_conf *conf)
445 struct echo_device *ed = cl2echo_dev(lu2cl_dev(obj->lo_dev));
446 struct echo_client_obd *ec = ed->ed_ec;
447 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
451 struct lu_object *below;
452 struct lu_device *under;
455 below = under->ld_ops->ldo_object_alloc(env, obj->lo_header,
459 lu_object_add(obj, below);
462 if (!ed->ed_next_ismd) {
463 const struct cl_object_conf *cconf = lu2cl_conf(conf);
464 struct echo_object_conf *econf = cl2echo_conf(cconf);
466 LASSERT(econf->eoc_md);
467 eco->eo_lsm = *econf->eoc_md;
468 /* clear the lsm pointer so that it won't get freed. */
469 *econf->eoc_md = NULL;
475 atomic_set(&eco->eo_npages, 0);
476 cl_object_page_init(lu2cl(obj), sizeof(struct echo_page));
478 spin_lock(&ec->ec_lock);
479 cfs_list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
480 spin_unlock(&ec->ec_lock);
485 /* taken from osc_unpackmd() */
486 static int echo_alloc_memmd(struct echo_device *ed,
487 struct lov_stripe_md **lsmp)
493 /* If export is lov/osc then use their obd method */
494 if (ed->ed_next != NULL)
495 return obd_alloc_memmd(ed->ed_ec->ec_exp, lsmp);
496 /* OFD has no unpackmd method, do everything here */
497 lsm_size = lov_stripe_md_size(1);
499 LASSERT(*lsmp == NULL);
500 OBD_ALLOC(*lsmp, lsm_size);
504 OBD_ALLOC((*lsmp)->lsm_oinfo[0], sizeof(struct lov_oinfo));
505 if ((*lsmp)->lsm_oinfo[0] == NULL) {
506 OBD_FREE(*lsmp, lsm_size);
510 loi_init((*lsmp)->lsm_oinfo[0]);
511 (*lsmp)->lsm_maxbytes = LUSTRE_STRIPE_MAXBYTES;
512 ostid_set_seq_echo(&(*lsmp)->lsm_oi);
517 static int echo_free_memmd(struct echo_device *ed, struct lov_stripe_md **lsmp)
523 /* If export is lov/osc then use their obd method */
524 if (ed->ed_next != NULL)
525 return obd_free_memmd(ed->ed_ec->ec_exp, lsmp);
526 /* OFD has no unpackmd method, do everything here */
527 lsm_size = lov_stripe_md_size(1);
529 LASSERT(*lsmp != NULL);
530 OBD_FREE((*lsmp)->lsm_oinfo[0], sizeof(struct lov_oinfo));
531 OBD_FREE(*lsmp, lsm_size);
536 static void echo_object_free(const struct lu_env *env, struct lu_object *obj)
538 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
539 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
542 LASSERT(atomic_read(&eco->eo_npages) == 0);
544 spin_lock(&ec->ec_lock);
545 cfs_list_del_init(&eco->eo_obj_chain);
546 spin_unlock(&ec->ec_lock);
549 lu_object_header_fini(obj->lo_header);
552 echo_free_memmd(eco->eo_dev, &eco->eo_lsm);
553 OBD_SLAB_FREE_PTR(eco, echo_object_kmem);
557 static int echo_object_print(const struct lu_env *env, void *cookie,
558 lu_printer_t p, const struct lu_object *o)
560 struct echo_object *obj = cl2echo_obj(lu2cl(o));
562 return (*p)(env, cookie, "echoclient-object@%p", obj);
565 static const struct lu_object_operations echo_lu_obj_ops = {
566 .loo_object_init = echo_object_init,
567 .loo_object_delete = NULL,
568 .loo_object_release = NULL,
569 .loo_object_free = echo_object_free,
570 .loo_object_print = echo_object_print,
571 .loo_object_invariant = NULL
573 /** @} echo_lu_ops */
575 /** \defgroup echo_lu_dev_ops lu_device operations
577 * Operations for echo lu device.
581 static struct lu_object *echo_object_alloc(const struct lu_env *env,
582 const struct lu_object_header *hdr,
583 struct lu_device *dev)
585 struct echo_object *eco;
586 struct lu_object *obj = NULL;
589 /* we're the top dev. */
590 LASSERT(hdr == NULL);
591 OBD_SLAB_ALLOC_PTR_GFP(eco, echo_object_kmem, __GFP_IO);
593 struct cl_object_header *hdr = &eco->eo_hdr;
595 obj = &echo_obj2cl(eco)->co_lu;
596 cl_object_header_init(hdr);
597 hdr->coh_page_bufsize = cfs_size_round(sizeof(struct cl_page));
599 lu_object_init(obj, &hdr->coh_lu, dev);
600 lu_object_add_top(&hdr->coh_lu, obj);
602 eco->eo_cl.co_ops = &echo_cl_obj_ops;
603 obj->lo_ops = &echo_lu_obj_ops;
608 static struct lu_device_operations echo_device_lu_ops = {
609 .ldo_object_alloc = echo_object_alloc,
612 /** @} echo_lu_dev_ops */
614 static struct cl_device_operations echo_device_cl_ops = {
617 /** \defgroup echo_init Setup and teardown
619 * Init and fini functions for echo client.
623 static int echo_site_init(const struct lu_env *env, struct echo_device *ed)
625 struct cl_site *site = &ed->ed_site_myself;
628 /* initialize site */
629 rc = cl_site_init(site, &ed->ed_cl);
631 CERROR("Cannot initilize site for echo client(%d)\n", rc);
635 rc = lu_site_init_finish(&site->cs_lu);
643 static void echo_site_fini(const struct lu_env *env, struct echo_device *ed)
646 if (!ed->ed_next_ismd)
647 cl_site_fini(ed->ed_site);
652 static void *echo_thread_key_init(const struct lu_context *ctx,
653 struct lu_context_key *key)
655 struct echo_thread_info *info;
657 OBD_SLAB_ALLOC_PTR_GFP(info, echo_thread_kmem, __GFP_IO);
659 info = ERR_PTR(-ENOMEM);
663 static void echo_thread_key_fini(const struct lu_context *ctx,
664 struct lu_context_key *key, void *data)
666 struct echo_thread_info *info = data;
667 OBD_SLAB_FREE_PTR(info, echo_thread_kmem);
670 static void echo_thread_key_exit(const struct lu_context *ctx,
671 struct lu_context_key *key, void *data)
675 static struct lu_context_key echo_thread_key = {
676 .lct_tags = LCT_CL_THREAD,
677 .lct_init = echo_thread_key_init,
678 .lct_fini = echo_thread_key_fini,
679 .lct_exit = echo_thread_key_exit
682 static void *echo_session_key_init(const struct lu_context *ctx,
683 struct lu_context_key *key)
685 struct echo_session_info *session;
687 OBD_SLAB_ALLOC_PTR_GFP(session, echo_session_kmem, __GFP_IO);
689 session = ERR_PTR(-ENOMEM);
693 static void echo_session_key_fini(const struct lu_context *ctx,
694 struct lu_context_key *key, void *data)
696 struct echo_session_info *session = data;
697 OBD_SLAB_FREE_PTR(session, echo_session_kmem);
700 static void echo_session_key_exit(const struct lu_context *ctx,
701 struct lu_context_key *key, void *data)
705 static struct lu_context_key echo_session_key = {
706 .lct_tags = LCT_SESSION,
707 .lct_init = echo_session_key_init,
708 .lct_fini = echo_session_key_fini,
709 .lct_exit = echo_session_key_exit
712 LU_TYPE_INIT_FINI(echo, &echo_thread_key, &echo_session_key);
714 #ifdef HAVE_SERVER_SUPPORT
715 # define ECHO_SEQ_WIDTH 0xffffffff
716 static int echo_fid_init(struct echo_device *ed, char *obd_name,
717 struct seq_server_site *ss)
723 OBD_ALLOC_PTR(ed->ed_cl_seq);
724 if (ed->ed_cl_seq == NULL)
727 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
729 GOTO(out_free_seq, rc = -ENOMEM);
731 snprintf(prefix, MAX_OBD_NAME + 5, "srv-%s", obd_name);
733 /* Init client side sequence-manager */
734 rc = seq_client_init(ed->ed_cl_seq, NULL,
736 prefix, ss->ss_server_seq);
737 ed->ed_cl_seq->lcs_width = ECHO_SEQ_WIDTH;
738 OBD_FREE(prefix, MAX_OBD_NAME + 5);
740 GOTO(out_free_seq, rc);
745 OBD_FREE_PTR(ed->ed_cl_seq);
746 ed->ed_cl_seq = NULL;
750 static int echo_fid_fini(struct obd_device *obddev)
752 struct echo_device *ed = obd2echo_dev(obddev);
755 if (ed->ed_cl_seq != NULL) {
756 seq_client_fini(ed->ed_cl_seq);
757 OBD_FREE_PTR(ed->ed_cl_seq);
758 ed->ed_cl_seq = NULL;
763 #endif /* HAVE_SERVER_SUPPORT */
765 static struct lu_device *echo_device_alloc(const struct lu_env *env,
766 struct lu_device_type *t,
767 struct lustre_cfg *cfg)
769 struct lu_device *next;
770 struct echo_device *ed;
771 struct cl_device *cd;
772 struct obd_device *obd = NULL; /* to keep compiler happy */
773 struct obd_device *tgt;
774 const char *tgt_type_name;
781 GOTO(out, rc = -ENOMEM);
785 rc = cl_device_init(cd, t);
789 cd->cd_lu_dev.ld_ops = &echo_device_lu_ops;
790 cd->cd_ops = &echo_device_cl_ops;
793 obd = class_name2obd(lustre_cfg_string(cfg, 0));
794 LASSERT(obd != NULL);
795 LASSERT(env != NULL);
797 tgt = class_name2obd(lustre_cfg_string(cfg, 1));
799 CERROR("Can not find tgt device %s\n",
800 lustre_cfg_string(cfg, 1));
801 GOTO(out, rc = -ENODEV);
804 next = tgt->obd_lu_dev;
805 if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
806 ed->ed_next_ismd = 1;
808 ed->ed_next_ismd = 0;
809 rc = echo_site_init(env, ed);
815 rc = echo_client_setup(env, obd, cfg);
819 ed->ed_ec = &obd->u.echo_client;
822 if (ed->ed_next_ismd) {
823 #ifdef HAVE_SERVER_SUPPORT
824 /* Suppose to connect to some Metadata layer */
826 struct lu_device *ld;
830 CERROR("%s is not lu device type!\n",
831 lustre_cfg_string(cfg, 1));
832 GOTO(out, rc = -EINVAL);
835 tgt_type_name = lustre_cfg_string(cfg, 2);
836 if (!tgt_type_name) {
837 CERROR("%s no type name for echo %s setup\n",
838 lustre_cfg_string(cfg, 1),
839 tgt->obd_type->typ_name);
840 GOTO(out, rc = -EINVAL);
845 spin_lock(&ls->ls_ld_lock);
846 cfs_list_for_each_entry(ld, &ls->ls_ld_linkage, ld_linkage) {
847 if (strcmp(ld->ld_type->ldt_name, tgt_type_name) == 0) {
852 spin_unlock(&ls->ls_ld_lock);
855 CERROR("%s is not lu device type!\n",
856 lustre_cfg_string(cfg, 1));
857 GOTO(out, rc = -EINVAL);
861 /* For MD echo client, it will use the site in MDS stack */
862 ed->ed_site_myself.cs_lu = *ls;
863 ed->ed_site = &ed->ed_site_myself;
864 ed->ed_cl.cd_lu_dev.ld_site = &ed->ed_site_myself.cs_lu;
865 rc = echo_fid_init(ed, obd->obd_name, lu_site2seq(ls));
867 CERROR("echo fid init error %d\n", rc);
870 #else /* !HAVE_SERVER_SUPPORT */
871 CERROR("Local operations are NOT supported on client side. "
872 "Only remote operations are supported. Metadata client "
873 "must be run on server side.\n");
874 GOTO(out, rc = -EOPNOTSUPP);
877 /* if echo client is to be stacked upon ost device, the next is
878 * NULL since ost is not a clio device so far */
879 if (next != NULL && !lu_device_is_cl(next))
882 tgt_type_name = tgt->obd_type->typ_name;
884 LASSERT(next != NULL);
885 if (next->ld_site != NULL)
886 GOTO(out, rc = -EBUSY);
888 next->ld_site = &ed->ed_site->cs_lu;
889 rc = next->ld_type->ldt_ops->ldto_device_init(env, next,
890 next->ld_type->ldt_name,
895 /* Tricky case, I have to determine the obd type since
896 * CLIO uses the different parameters to initialize
897 * objects for lov & osc. */
898 if (strcmp(tgt_type_name, LUSTRE_LOV_NAME) == 0)
899 ed->ed_next_islov = 1;
901 LASSERT(strcmp(tgt_type_name,
902 LUSTRE_OSC_NAME) == 0);
904 LASSERT(strcmp(tgt_type_name, LUSTRE_OST_NAME) == 0);
908 RETURN(&cd->cd_lu_dev);
913 rc2 = echo_client_cleanup(obd);
915 CERROR("Cleanup obd device %s error(%d)\n",
920 echo_site_fini(env, ed);
922 cl_device_fini(&ed->ed_cl);
932 static int echo_device_init(const struct lu_env *env, struct lu_device *d,
933 const char *name, struct lu_device *next)
939 static struct lu_device *echo_device_fini(const struct lu_env *env,
942 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
943 struct lu_device *next = ed->ed_next;
945 while (next && !ed->ed_next_ismd)
946 next = next->ld_type->ldt_ops->ldto_device_fini(env, next);
950 static void echo_lock_release(const struct lu_env *env,
951 struct echo_lock *ecl,
954 struct cl_lock *clk = echo_lock2cl(ecl);
958 cl_lock_release(env, clk, "ec enqueue", ecl->el_object);
960 cl_lock_mutex_get(env, clk);
961 cl_lock_cancel(env, clk);
962 cl_lock_delete(env, clk);
963 cl_lock_mutex_put(env, clk);
965 cl_lock_put(env, clk);
968 static struct lu_device *echo_device_free(const struct lu_env *env,
971 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
972 struct echo_client_obd *ec = ed->ed_ec;
973 struct echo_object *eco;
974 struct lu_device *next = ed->ed_next;
976 CDEBUG(D_INFO, "echo device:%p is going to be freed, next = %p\n",
979 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
981 /* check if there are objects still alive.
982 * It shouldn't have any object because lu_site_purge would cleanup
983 * all of cached objects. Anyway, probably the echo device is being
984 * parallelly accessed.
986 spin_lock(&ec->ec_lock);
987 cfs_list_for_each_entry(eco, &ec->ec_objects, eo_obj_chain)
989 spin_unlock(&ec->ec_lock);
992 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
995 "Waiting for the reference of echo object to be dropped\n");
997 /* Wait for the last reference to be dropped. */
998 spin_lock(&ec->ec_lock);
999 while (!cfs_list_empty(&ec->ec_objects)) {
1000 spin_unlock(&ec->ec_lock);
1001 CERROR("echo_client still has objects at cleanup time, "
1002 "wait for 1 second\n");
1003 schedule_timeout_and_set_state(TASK_UNINTERRUPTIBLE,
1004 cfs_time_seconds(1));
1005 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
1006 spin_lock(&ec->ec_lock);
1008 spin_unlock(&ec->ec_lock);
1010 LASSERT(cfs_list_empty(&ec->ec_locks));
1012 CDEBUG(D_INFO, "No object exists, exiting...\n");
1014 echo_client_cleanup(d->ld_obd);
1015 #ifdef HAVE_SERVER_SUPPORT
1016 echo_fid_fini(d->ld_obd);
1018 while (next && !ed->ed_next_ismd)
1019 next = next->ld_type->ldt_ops->ldto_device_free(env, next);
1021 LASSERT(ed->ed_site == lu2cl_site(d->ld_site));
1022 echo_site_fini(env, ed);
1023 cl_device_fini(&ed->ed_cl);
1029 static const struct lu_device_type_operations echo_device_type_ops = {
1030 .ldto_init = echo_type_init,
1031 .ldto_fini = echo_type_fini,
1033 .ldto_start = echo_type_start,
1034 .ldto_stop = echo_type_stop,
1036 .ldto_device_alloc = echo_device_alloc,
1037 .ldto_device_free = echo_device_free,
1038 .ldto_device_init = echo_device_init,
1039 .ldto_device_fini = echo_device_fini
1042 static struct lu_device_type echo_device_type = {
1043 .ldt_tags = LU_DEVICE_CL,
1044 .ldt_name = LUSTRE_ECHO_CLIENT_NAME,
1045 .ldt_ops = &echo_device_type_ops,
1046 .ldt_ctx_tags = LCT_CL_THREAD | LCT_MD_THREAD | LCT_DT_THREAD,
1050 /** \defgroup echo_exports Exported operations
1052 * exporting functions to echo client
1057 /* Interfaces to echo client obd device */
1058 static struct echo_object *cl_echo_object_find(struct echo_device *d,
1059 struct lov_stripe_md **lsmp)
1062 struct echo_thread_info *info;
1063 struct echo_object_conf *conf;
1064 struct lov_stripe_md *lsm;
1065 struct echo_object *eco;
1066 struct cl_object *obj;
1075 LASSERTF(ostid_id(&lsm->lsm_oi) != 0, DOSTID"\n", POSTID(&lsm->lsm_oi));
1076 LASSERTF(ostid_seq(&lsm->lsm_oi) == FID_SEQ_ECHO, DOSTID"\n",
1077 POSTID(&lsm->lsm_oi));
1079 /* Never return an object if the obd is to be freed. */
1080 if (echo_dev2cl(d)->cd_lu_dev.ld_obd->obd_stopping)
1081 RETURN(ERR_PTR(-ENODEV));
1083 env = cl_env_get(&refcheck);
1085 RETURN((void *)env);
1087 info = echo_env_info(env);
1088 conf = &info->eti_conf;
1090 if (!d->ed_next_islov) {
1091 struct lov_oinfo *oinfo = lsm->lsm_oinfo[0];
1092 LASSERT(oinfo != NULL);
1093 oinfo->loi_oi = lsm->lsm_oi;
1094 conf->eoc_cl.u.coc_oinfo = oinfo;
1096 struct lustre_md *md;
1098 memset(md, 0, sizeof *md);
1100 conf->eoc_cl.u.coc_md = md;
1103 conf->eoc_md = lsmp;
1105 fid = &info->eti_fid;
1106 rc = ostid_to_fid(fid, &lsm->lsm_oi, 0);
1108 GOTO(out, eco = ERR_PTR(rc));
1110 /* In the function below, .hs_keycmp resolves to
1111 * lu_obj_hop_keycmp() */
1112 /* coverity[overrun-buffer-val] */
1113 obj = cl_object_find(env, echo_dev2cl(d), fid, &conf->eoc_cl);
1115 GOTO(out, eco = (void*)obj);
1117 eco = cl2echo_obj(obj);
1118 if (eco->eo_deleted) {
1119 cl_object_put(env, obj);
1120 eco = ERR_PTR(-EAGAIN);
1124 cl_env_put(env, &refcheck);
1128 static int cl_echo_object_put(struct echo_object *eco)
1131 struct cl_object *obj = echo_obj2cl(eco);
1135 env = cl_env_get(&refcheck);
1137 RETURN(PTR_ERR(env));
1139 /* an external function to kill an object? */
1140 if (eco->eo_deleted) {
1141 struct lu_object_header *loh = obj->co_lu.lo_header;
1142 LASSERT(&eco->eo_hdr == luh2coh(loh));
1143 set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
1146 cl_object_put(env, obj);
1147 cl_env_put(env, &refcheck);
1151 static int cl_echo_enqueue0(struct lu_env *env, struct echo_object *eco,
1152 obd_off start, obd_off end, int mode,
1153 __u64 *cookie , __u32 enqflags)
1156 struct cl_lock *lck;
1157 struct cl_object *obj;
1158 struct cl_lock_descr *descr;
1159 struct echo_thread_info *info;
1163 info = echo_env_info(env);
1165 descr = &info->eti_descr;
1166 obj = echo_obj2cl(eco);
1168 descr->cld_obj = obj;
1169 descr->cld_start = cl_index(obj, start);
1170 descr->cld_end = cl_index(obj, end);
1171 descr->cld_mode = mode == LCK_PW ? CLM_WRITE : CLM_READ;
1172 descr->cld_enq_flags = enqflags;
1175 lck = cl_lock_request(env, io, descr, "ec enqueue", eco);
1177 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
1178 struct echo_lock *el;
1180 rc = cl_wait(env, lck);
1182 el = cl2echo_lock(cl_lock_at(lck, &echo_device_type));
1183 spin_lock(&ec->ec_lock);
1184 if (cfs_list_empty(&el->el_chain)) {
1185 cfs_list_add(&el->el_chain, &ec->ec_locks);
1186 el->el_cookie = ++ec->ec_unique;
1188 atomic_inc(&el->el_refcount);
1189 *cookie = el->el_cookie;
1190 spin_unlock(&ec->ec_lock);
1192 cl_lock_release(env, lck, "ec enqueue", current);
1198 static int cl_echo_enqueue(struct echo_object *eco, obd_off start, obd_off end,
1199 int mode, __u64 *cookie)
1201 struct echo_thread_info *info;
1208 env = cl_env_get(&refcheck);
1210 RETURN(PTR_ERR(env));
1212 info = echo_env_info(env);
1215 io->ci_ignore_layout = 1;
1216 result = cl_io_init(env, io, CIT_MISC, echo_obj2cl(eco));
1219 LASSERT(result == 0);
1221 result = cl_echo_enqueue0(env, eco, start, end, mode, cookie, 0);
1222 cl_io_fini(env, io);
1226 cl_env_put(env, &refcheck);
1230 static int cl_echo_cancel0(struct lu_env *env, struct echo_device *ed,
1233 struct echo_client_obd *ec = ed->ed_ec;
1234 struct echo_lock *ecl = NULL;
1236 int found = 0, still_used = 0;
1239 LASSERT(ec != NULL);
1240 spin_lock(&ec->ec_lock);
1241 cfs_list_for_each (el, &ec->ec_locks) {
1242 ecl = cfs_list_entry (el, struct echo_lock, el_chain);
1243 CDEBUG(D_INFO, "ecl: %p, cookie: "LPX64"\n", ecl, ecl->el_cookie);
1244 found = (ecl->el_cookie == cookie);
1246 if (atomic_dec_and_test(&ecl->el_refcount))
1247 cfs_list_del_init(&ecl->el_chain);
1253 spin_unlock(&ec->ec_lock);
1258 echo_lock_release(env, ecl, still_used);
1262 static int cl_echo_cancel(struct echo_device *ed, __u64 cookie)
1269 env = cl_env_get(&refcheck);
1271 RETURN(PTR_ERR(env));
1273 rc = cl_echo_cancel0(env, ed, cookie);
1275 cl_env_put(env, &refcheck);
1279 static void echo_commit_callback(const struct lu_env *env, struct cl_io *io,
1280 struct cl_page *page)
1282 struct echo_thread_info *info;
1283 struct cl_2queue *queue;
1285 info = echo_env_info(env);
1286 LASSERT(io == &info->eti_io);
1288 queue = &info->eti_queue;
1289 cl_page_list_add(&queue->c2_qout, page);
1292 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
1293 struct page **pages, int npages, int async)
1296 struct echo_thread_info *info;
1297 struct cl_object *obj = echo_obj2cl(eco);
1298 struct echo_device *ed = eco->eo_dev;
1299 struct cl_2queue *queue;
1301 struct cl_page *clp;
1302 struct lustre_handle lh = { 0 };
1303 int page_size = cl_page_size(obj);
1309 LASSERT((offset & ~CFS_PAGE_MASK) == 0);
1310 LASSERT(ed->ed_next != NULL);
1311 env = cl_env_get(&refcheck);
1313 RETURN(PTR_ERR(env));
1315 info = echo_env_info(env);
1317 queue = &info->eti_queue;
1319 cl_2queue_init(queue);
1321 io->ci_ignore_layout = 1;
1322 rc = cl_io_init(env, io, CIT_MISC, obj);
1328 rc = cl_echo_enqueue0(env, eco, offset,
1329 offset + npages * PAGE_CACHE_SIZE - 1,
1330 rw == READ ? LCK_PR : LCK_PW, &lh.cookie,
1333 GOTO(error_lock, rc);
1335 for (i = 0; i < npages; i++) {
1337 clp = cl_page_find(env, obj, cl_index(obj, offset),
1338 pages[i], CPT_TRANSIENT);
1343 LASSERT(clp->cp_type == CPT_TRANSIENT);
1345 rc = cl_page_own(env, io, clp);
1347 LASSERT(clp->cp_state == CPS_FREEING);
1348 cl_page_put(env, clp);
1352 cl_2queue_add(queue, clp);
1354 /* drop the reference count for cl_page_find, so that the page
1355 * will be freed in cl_2queue_fini. */
1356 cl_page_put(env, clp);
1357 cl_page_clip(env, clp, 0, page_size);
1359 offset += page_size;
1363 enum cl_req_type typ = rw == READ ? CRT_READ : CRT_WRITE;
1365 async = async && (typ == CRT_WRITE);
1367 rc = cl_io_commit_async(env, io, &queue->c2_qin,
1369 echo_commit_callback);
1371 rc = cl_io_submit_sync(env, io, typ, queue, 0);
1372 CDEBUG(D_INFO, "echo_client %s write returns %d\n",
1373 async ? "async" : "sync", rc);
1376 cl_echo_cancel0(env, ed, lh.cookie);
1379 cl_2queue_discard(env, io, queue);
1380 cl_2queue_disown(env, io, queue);
1381 cl_2queue_fini(env, queue);
1382 cl_io_fini(env, io);
1384 cl_env_put(env, &refcheck);
1387 /** @} echo_exports */
1390 static obd_id last_object_id;
1393 echo_copyout_lsm (struct lov_stripe_md *lsm, void *_ulsm, int ulsm_nob)
1395 struct lov_stripe_md *ulsm = _ulsm;
1398 nob = offsetof (struct lov_stripe_md, lsm_oinfo[lsm->lsm_stripe_count]);
1402 if (copy_to_user (ulsm, lsm, sizeof(*ulsm)))
1405 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1406 if (copy_to_user (ulsm->lsm_oinfo[i], lsm->lsm_oinfo[i],
1407 sizeof(lsm->lsm_oinfo[0])))
1414 echo_copyin_lsm (struct echo_device *ed, struct lov_stripe_md *lsm,
1415 void *ulsm, int ulsm_nob)
1417 struct echo_client_obd *ec = ed->ed_ec;
1420 if (ulsm_nob < sizeof (*lsm))
1423 if (copy_from_user (lsm, ulsm, sizeof (*lsm)))
1426 if (lsm->lsm_stripe_count > ec->ec_nstripes ||
1427 lsm->lsm_magic != LOV_MAGIC ||
1428 (lsm->lsm_stripe_size & (~CFS_PAGE_MASK)) != 0 ||
1429 ((__u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count > ~0UL))
1433 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1434 if (copy_from_user(lsm->lsm_oinfo[i],
1435 ((struct lov_stripe_md *)ulsm)-> \
1437 sizeof(lsm->lsm_oinfo[0])))
1443 #ifdef HAVE_SERVER_SUPPORT
1444 static inline void echo_md_build_name(struct lu_name *lname, char *name,
1447 sprintf(name, LPU64, id);
1448 lname->ln_name = name;
1449 lname->ln_namelen = strlen(name);
1452 /* similar to mdt_attr_get_complex */
1453 static int echo_big_lmm_get(const struct lu_env *env, struct md_object *o,
1456 struct echo_thread_info *info = echo_env_info(env);
1461 LASSERT(ma->ma_lmm_size > 0);
1463 rc = mo_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LOV);
1467 /* big_lmm may need to be grown */
1468 if (info->eti_big_lmmsize < rc) {
1469 int size = size_roundup_power2(rc);
1471 if (info->eti_big_lmmsize > 0) {
1472 /* free old buffer */
1473 LASSERT(info->eti_big_lmm);
1474 OBD_FREE_LARGE(info->eti_big_lmm,
1475 info->eti_big_lmmsize);
1476 info->eti_big_lmm = NULL;
1477 info->eti_big_lmmsize = 0;
1480 OBD_ALLOC_LARGE(info->eti_big_lmm, size);
1481 if (info->eti_big_lmm == NULL)
1483 info->eti_big_lmmsize = size;
1485 LASSERT(info->eti_big_lmmsize >= rc);
1487 info->eti_buf.lb_buf = info->eti_big_lmm;
1488 info->eti_buf.lb_len = info->eti_big_lmmsize;
1489 rc = mo_xattr_get(env, o, &info->eti_buf, XATTR_NAME_LOV);
1493 ma->ma_valid |= MA_LOV;
1494 ma->ma_lmm = info->eti_big_lmm;
1495 ma->ma_lmm_size = rc;
1500 static int echo_attr_get_complex(const struct lu_env *env,
1501 struct md_object *next,
1504 struct echo_thread_info *info = echo_env_info(env);
1505 struct lu_buf *buf = &info->eti_buf;
1506 umode_t mode = lu_object_attr(&next->mo_lu);
1507 int need = ma->ma_need;
1514 if (need & MA_INODE) {
1515 ma->ma_need = MA_INODE;
1516 rc = mo_attr_get(env, next, ma);
1519 ma->ma_valid |= MA_INODE;
1522 if (need & MA_LOV) {
1523 if (S_ISREG(mode) || S_ISDIR(mode)) {
1524 LASSERT(ma->ma_lmm_size > 0);
1525 buf->lb_buf = ma->ma_lmm;
1526 buf->lb_len = ma->ma_lmm_size;
1527 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LOV);
1529 ma->ma_lmm_size = rc2;
1530 ma->ma_valid |= MA_LOV;
1531 } else if (rc2 == -ENODATA) {
1533 ma->ma_lmm_size = 0;
1534 } else if (rc2 == -ERANGE) {
1535 rc2 = echo_big_lmm_get(env, next, ma);
1537 GOTO(out, rc = rc2);
1539 GOTO(out, rc = rc2);
1544 #ifdef CONFIG_FS_POSIX_ACL
1545 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
1546 buf->lb_buf = ma->ma_acl;
1547 buf->lb_len = ma->ma_acl_size;
1548 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1550 ma->ma_acl_size = rc2;
1551 ma->ma_valid |= MA_ACL_DEF;
1552 } else if (rc2 == -ENODATA) {
1554 ma->ma_acl_size = 0;
1556 GOTO(out, rc = rc2);
1562 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
1563 rc, ma->ma_valid, ma->ma_lmm);
1568 echo_md_create_internal(const struct lu_env *env, struct echo_device *ed,
1569 struct md_object *parent, struct lu_fid *fid,
1570 struct lu_name *lname, struct md_op_spec *spec,
1573 struct lu_object *ec_child, *child;
1574 struct lu_device *ld = ed->ed_next;
1575 struct echo_thread_info *info = echo_env_info(env);
1576 struct lu_fid *fid2 = &info->eti_fid2;
1577 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
1582 rc = mdo_lookup(env, parent, lname, fid2, spec);
1585 else if (rc != -ENOENT)
1588 ec_child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev,
1590 if (IS_ERR(ec_child)) {
1591 CERROR("Can not find the child "DFID": rc = %ld\n", PFID(fid),
1593 RETURN(PTR_ERR(ec_child));
1596 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1597 if (child == NULL) {
1598 CERROR("Can not locate the child "DFID"\n", PFID(fid));
1599 GOTO(out_put, rc = -EINVAL);
1602 CDEBUG(D_RPCTRACE, "Start creating object "DFID" %s %p\n",
1603 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1606 * Do not perform lookup sanity check. We know that name does not exist.
1608 spec->sp_cr_lookup = 0;
1609 rc = mdo_create(env, parent, lname, lu2md(child), spec, ma);
1611 CERROR("Can not create child "DFID": rc = %d\n", PFID(fid), rc);
1614 CDEBUG(D_RPCTRACE, "End creating object "DFID" %s %p rc = %d\n",
1615 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent, rc);
1618 lu_object_put(env, ec_child);
1622 static int echo_set_lmm_size(const struct lu_env *env, struct lu_device *ld,
1625 struct echo_thread_info *info = echo_env_info(env);
1627 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
1628 ma->ma_lmm = (void *)&info->eti_lmm;
1629 ma->ma_lmm_size = sizeof(info->eti_lmm);
1631 LASSERT(info->eti_big_lmmsize);
1632 ma->ma_lmm = info->eti_big_lmm;
1633 ma->ma_lmm_size = info->eti_big_lmmsize;
1639 static int echo_create_md_object(const struct lu_env *env,
1640 struct echo_device *ed,
1641 struct lu_object *ec_parent,
1643 char *name, int namelen,
1644 __u64 id, __u32 mode, int count,
1645 int stripe_count, int stripe_offset)
1647 struct lu_object *parent;
1648 struct echo_thread_info *info = echo_env_info(env);
1649 struct lu_name *lname = &info->eti_lname;
1650 struct md_op_spec *spec = &info->eti_spec;
1651 struct md_attr *ma = &info->eti_ma;
1652 struct lu_device *ld = ed->ed_next;
1658 if (ec_parent == NULL)
1660 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1664 memset(ma, 0, sizeof(*ma));
1665 memset(spec, 0, sizeof(*spec));
1666 if (stripe_count != 0) {
1667 spec->sp_cr_flags |= FMODE_WRITE;
1668 echo_set_lmm_size(env, ld, ma);
1669 if (stripe_count != -1) {
1670 struct lov_user_md_v3 *lum = &info->eti_lum;
1672 lum->lmm_magic = LOV_USER_MAGIC_V3;
1673 lum->lmm_stripe_count = stripe_count;
1674 lum->lmm_stripe_offset = stripe_offset;
1675 lum->lmm_pattern = 0;
1676 spec->u.sp_ea.eadata = lum;
1677 spec->u.sp_ea.eadatalen = sizeof(*lum);
1678 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1682 ma->ma_attr.la_mode = mode;
1683 ma->ma_attr.la_valid = LA_CTIME | LA_MODE;
1684 ma->ma_attr.la_ctime = cfs_time_current_64();
1687 lname->ln_name = name;
1688 lname->ln_namelen = namelen;
1689 /* If name is specified, only create one object by name */
1690 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1695 /* Create multiple object sequenced by id */
1696 for (i = 0; i < count; i++) {
1697 char *tmp_name = info->eti_name;
1699 echo_md_build_name(lname, tmp_name, id);
1701 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1704 CERROR("Can not create child %s: rc = %d\n", tmp_name,
1715 static struct lu_object *echo_md_lookup(const struct lu_env *env,
1716 struct echo_device *ed,
1717 struct md_object *parent,
1718 struct lu_name *lname)
1720 struct echo_thread_info *info = echo_env_info(env);
1721 struct lu_fid *fid = &info->eti_fid;
1722 struct lu_object *child;
1726 CDEBUG(D_INFO, "lookup %s in parent "DFID" %p\n", lname->ln_name,
1728 rc = mdo_lookup(env, parent, lname, fid, NULL);
1730 CERROR("lookup %s: rc = %d\n", lname->ln_name, rc);
1731 RETURN(ERR_PTR(rc));
1734 /* In the function below, .hs_keycmp resolves to
1735 * lu_obj_hop_keycmp() */
1736 /* coverity[overrun-buffer-val] */
1737 child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1742 static int echo_setattr_object(const struct lu_env *env,
1743 struct echo_device *ed,
1744 struct lu_object *ec_parent,
1745 __u64 id, int count)
1747 struct lu_object *parent;
1748 struct echo_thread_info *info = echo_env_info(env);
1749 struct lu_name *lname = &info->eti_lname;
1750 char *name = info->eti_name;
1751 struct lu_device *ld = ed->ed_next;
1752 struct lu_buf *buf = &info->eti_buf;
1758 if (ec_parent == NULL)
1760 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1764 for (i = 0; i < count; i++) {
1765 struct lu_object *ec_child, *child;
1767 echo_md_build_name(lname, name, id);
1769 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1770 if (IS_ERR(ec_child)) {
1771 CERROR("Can't find child %s: rc = %ld\n",
1772 lname->ln_name, PTR_ERR(ec_child));
1773 RETURN(PTR_ERR(ec_child));
1776 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1777 if (child == NULL) {
1778 CERROR("Can not locate the child %s\n", lname->ln_name);
1779 lu_object_put(env, ec_child);
1784 CDEBUG(D_RPCTRACE, "Start setattr object "DFID"\n",
1785 PFID(lu_object_fid(child)));
1787 buf->lb_buf = info->eti_xattr_buf;
1788 buf->lb_len = sizeof(info->eti_xattr_buf);
1790 sprintf(name, "%s.test1", XATTR_USER_PREFIX);
1791 rc = mo_xattr_set(env, lu2md(child), buf, name,
1794 CERROR("Can not setattr child "DFID": rc = %d\n",
1795 PFID(lu_object_fid(child)), rc);
1796 lu_object_put(env, ec_child);
1799 CDEBUG(D_RPCTRACE, "End setattr object "DFID"\n",
1800 PFID(lu_object_fid(child)));
1802 lu_object_put(env, ec_child);
1807 static int echo_getattr_object(const struct lu_env *env,
1808 struct echo_device *ed,
1809 struct lu_object *ec_parent,
1810 __u64 id, int count)
1812 struct lu_object *parent;
1813 struct echo_thread_info *info = echo_env_info(env);
1814 struct lu_name *lname = &info->eti_lname;
1815 char *name = info->eti_name;
1816 struct md_attr *ma = &info->eti_ma;
1817 struct lu_device *ld = ed->ed_next;
1823 if (ec_parent == NULL)
1825 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1829 memset(ma, 0, sizeof(*ma));
1830 ma->ma_need |= MA_INODE | MA_LOV | MA_PFID | MA_HSM | MA_ACL_DEF;
1831 ma->ma_acl = info->eti_xattr_buf;
1832 ma->ma_acl_size = sizeof(info->eti_xattr_buf);
1834 for (i = 0; i < count; i++) {
1835 struct lu_object *ec_child, *child;
1838 echo_md_build_name(lname, name, id);
1839 echo_set_lmm_size(env, ld, ma);
1841 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1842 if (IS_ERR(ec_child)) {
1843 CERROR("Can't find child %s: rc = %ld\n",
1844 lname->ln_name, PTR_ERR(ec_child));
1845 RETURN(PTR_ERR(ec_child));
1848 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1849 if (child == NULL) {
1850 CERROR("Can not locate the child %s\n", lname->ln_name);
1851 lu_object_put(env, ec_child);
1855 CDEBUG(D_RPCTRACE, "Start getattr object "DFID"\n",
1856 PFID(lu_object_fid(child)));
1857 rc = echo_attr_get_complex(env, lu2md(child), ma);
1859 CERROR("Can not getattr child "DFID": rc = %d\n",
1860 PFID(lu_object_fid(child)), rc);
1861 lu_object_put(env, ec_child);
1864 CDEBUG(D_RPCTRACE, "End getattr object "DFID"\n",
1865 PFID(lu_object_fid(child)));
1867 lu_object_put(env, ec_child);
1873 static int echo_lookup_object(const struct lu_env *env,
1874 struct echo_device *ed,
1875 struct lu_object *ec_parent,
1876 __u64 id, int count)
1878 struct lu_object *parent;
1879 struct echo_thread_info *info = echo_env_info(env);
1880 struct lu_name *lname = &info->eti_lname;
1881 char *name = info->eti_name;
1882 struct lu_fid *fid = &info->eti_fid;
1883 struct lu_device *ld = ed->ed_next;
1887 if (ec_parent == NULL)
1889 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1893 /*prepare the requests*/
1894 for (i = 0; i < count; i++) {
1895 echo_md_build_name(lname, name, id);
1897 CDEBUG(D_RPCTRACE, "Start lookup object "DFID" %s %p\n",
1898 PFID(lu_object_fid(parent)), lname->ln_name, parent);
1900 rc = mdo_lookup(env, lu2md(parent), lname, fid, NULL);
1902 CERROR("Can not lookup child %s: rc = %d\n", name, rc);
1905 CDEBUG(D_RPCTRACE, "End lookup object "DFID" %s %p\n",
1906 PFID(lu_object_fid(parent)), lname->ln_name, parent);
1913 static int echo_md_destroy_internal(const struct lu_env *env,
1914 struct echo_device *ed,
1915 struct md_object *parent,
1916 struct lu_name *lname,
1919 struct lu_device *ld = ed->ed_next;
1920 struct lu_object *ec_child;
1921 struct lu_object *child;
1926 ec_child = echo_md_lookup(env, ed, parent, lname);
1927 if (IS_ERR(ec_child)) {
1928 CERROR("Can't find child %s: rc = %ld\n", lname->ln_name,
1930 RETURN(PTR_ERR(ec_child));
1933 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1934 if (child == NULL) {
1935 CERROR("Can not locate the child %s\n", lname->ln_name);
1936 GOTO(out_put, rc = -EINVAL);
1939 if (lu_object_remote(child)) {
1940 CERROR("Can not destroy remote object %s: rc = %d\n",
1941 lname->ln_name, -EPERM);
1942 GOTO(out_put, rc = -EPERM);
1944 CDEBUG(D_RPCTRACE, "Start destroy object "DFID" %s %p\n",
1945 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1947 rc = mdo_unlink(env, parent, lu2md(child), lname, ma, 0);
1949 CERROR("Can not unlink child %s: rc = %d\n",
1950 lname->ln_name, rc);
1953 CDEBUG(D_RPCTRACE, "End destroy object "DFID" %s %p\n",
1954 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1956 lu_object_put(env, ec_child);
1960 static int echo_destroy_object(const struct lu_env *env,
1961 struct echo_device *ed,
1962 struct lu_object *ec_parent,
1963 char *name, int namelen,
1964 __u64 id, __u32 mode,
1967 struct echo_thread_info *info = echo_env_info(env);
1968 struct lu_name *lname = &info->eti_lname;
1969 struct md_attr *ma = &info->eti_ma;
1970 struct lu_device *ld = ed->ed_next;
1971 struct lu_object *parent;
1976 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1980 memset(ma, 0, sizeof(*ma));
1981 ma->ma_attr.la_mode = mode;
1982 ma->ma_attr.la_valid = LA_CTIME;
1983 ma->ma_attr.la_ctime = cfs_time_current_64();
1984 ma->ma_need = MA_INODE;
1988 lname->ln_name = name;
1989 lname->ln_namelen = namelen;
1990 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
1995 /*prepare the requests*/
1996 for (i = 0; i < count; i++) {
1997 char *tmp_name = info->eti_name;
2000 echo_md_build_name(lname, tmp_name, id);
2002 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
2005 CERROR("Can not unlink child %s: rc = %d\n", name, rc);
2014 static struct lu_object *echo_resolve_path(const struct lu_env *env,
2015 struct echo_device *ed, char *path,
2018 struct lu_device *ld = ed->ed_next;
2019 struct md_device *md = lu2md_dev(ld);
2020 struct echo_thread_info *info = echo_env_info(env);
2021 struct lu_fid *fid = &info->eti_fid;
2022 struct lu_name *lname = &info->eti_lname;
2023 struct lu_object *parent = NULL;
2024 struct lu_object *child = NULL;
2028 /*Only support MDD layer right now*/
2029 rc = md->md_ops->mdo_root_get(env, md, fid);
2031 CERROR("get root error: rc = %d\n", rc);
2032 RETURN(ERR_PTR(rc));
2035 /* In the function below, .hs_keycmp resolves to
2036 * lu_obj_hop_keycmp() */
2037 /* coverity[overrun-buffer-val] */
2038 parent = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
2039 if (IS_ERR(parent)) {
2040 CERROR("Can not find the parent "DFID": rc = %ld\n",
2041 PFID(fid), PTR_ERR(parent));
2046 struct lu_object *ld_parent;
2049 e = strsep(&path, "/");
2054 if (!path || path[0] == '\0')
2060 lname->ln_namelen = strlen(e);
2062 ld_parent = lu_object_locate(parent->lo_header, ld->ld_type);
2063 if (ld_parent == NULL) {
2064 lu_object_put(env, parent);
2069 child = echo_md_lookup(env, ed, lu2md(ld_parent), lname);
2070 lu_object_put(env, parent);
2071 if (IS_ERR(child)) {
2072 rc = (int)PTR_ERR(child);
2073 CERROR("lookup %s under parent "DFID": rc = %d\n",
2074 lname->ln_name, PFID(lu_object_fid(ld_parent)),
2081 RETURN(ERR_PTR(rc));
2086 static void echo_ucred_init(struct lu_env *env)
2088 struct lu_ucred *ucred = lu_ucred(env);
2090 ucred->uc_valid = UCRED_INVALID;
2092 ucred->uc_suppgids[0] = -1;
2093 ucred->uc_suppgids[1] = -1;
2095 ucred->uc_uid = ucred->uc_o_uid = current_uid();
2096 ucred->uc_gid = ucred->uc_o_gid = current_gid();
2097 ucred->uc_fsuid = ucred->uc_o_fsuid = current_fsuid();
2098 ucred->uc_fsgid = ucred->uc_o_fsgid = current_fsgid();
2099 ucred->uc_cap = cfs_curproc_cap_pack();
2101 /* remove fs privilege for non-root user. */
2102 if (ucred->uc_fsuid)
2103 ucred->uc_cap &= ~CFS_CAP_FS_MASK;
2104 ucred->uc_valid = UCRED_NEW;
2107 static void echo_ucred_fini(struct lu_env *env)
2109 struct lu_ucred *ucred = lu_ucred(env);
2110 ucred->uc_valid = UCRED_INIT;
2113 #define ECHO_MD_CTX_TAG (LCT_REMEMBER | LCT_MD_THREAD)
2114 #define ECHO_MD_SES_TAG (LCT_REMEMBER | LCT_SESSION | LCT_SERVER_SESSION)
2115 static int echo_md_handler(struct echo_device *ed, int command,
2116 char *path, int path_len, __u64 id, int count,
2117 struct obd_ioctl_data *data)
2119 struct echo_thread_info *info;
2120 struct lu_device *ld = ed->ed_next;
2123 struct lu_object *parent;
2125 int namelen = data->ioc_plen2;
2130 CERROR("MD echo client is not being initialized properly\n");
2134 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
2135 CERROR("Only support MDD layer right now!\n");
2139 env = cl_env_get(&refcheck);
2141 RETURN(PTR_ERR(env));
2143 rc = lu_env_refill_by_tags(env, ECHO_MD_CTX_TAG, ECHO_MD_SES_TAG);
2147 /* init big_lmm buffer */
2148 info = echo_env_info(env);
2149 LASSERT(info->eti_big_lmm == NULL);
2150 OBD_ALLOC_LARGE(info->eti_big_lmm, MIN_MD_SIZE);
2151 if (info->eti_big_lmm == NULL)
2152 GOTO(out_env, rc = -ENOMEM);
2153 info->eti_big_lmmsize = MIN_MD_SIZE;
2155 parent = echo_resolve_path(env, ed, path, path_len);
2156 if (IS_ERR(parent)) {
2157 CERROR("Can not resolve the path %s: rc = %ld\n", path,
2159 GOTO(out_free, rc = PTR_ERR(parent));
2163 OBD_ALLOC(name, namelen + 1);
2165 GOTO(out_put, rc = -ENOMEM);
2166 if (copy_from_user(name, data->ioc_pbuf2, namelen))
2167 GOTO(out_name, rc = -EFAULT);
2170 echo_ucred_init(env);
2173 case ECHO_MD_CREATE:
2174 case ECHO_MD_MKDIR: {
2175 struct echo_thread_info *info = echo_env_info(env);
2176 __u32 mode = data->ioc_obdo2.o_mode;
2177 struct lu_fid *fid = &info->eti_fid;
2178 int stripe_count = (int)data->ioc_obdo2.o_misc;
2179 int stripe_index = (int)data->ioc_obdo2.o_stripe_idx;
2181 rc = ostid_to_fid(fid, &data->ioc_obdo1.o_oi, 0);
2185 /* In the function below, .hs_keycmp resolves to
2186 * lu_obj_hop_keycmp() */
2187 /* coverity[overrun-buffer-val] */
2188 rc = echo_create_md_object(env, ed, parent, fid, name, namelen,
2189 id, mode, count, stripe_count,
2193 case ECHO_MD_DESTROY:
2194 case ECHO_MD_RMDIR: {
2195 __u32 mode = data->ioc_obdo2.o_mode;
2197 rc = echo_destroy_object(env, ed, parent, name, namelen,
2201 case ECHO_MD_LOOKUP:
2202 rc = echo_lookup_object(env, ed, parent, id, count);
2204 case ECHO_MD_GETATTR:
2205 rc = echo_getattr_object(env, ed, parent, id, count);
2207 case ECHO_MD_SETATTR:
2208 rc = echo_setattr_object(env, ed, parent, id, count);
2211 CERROR("unknown command %d\n", command);
2215 echo_ucred_fini(env);
2219 OBD_FREE(name, namelen + 1);
2221 lu_object_put(env, parent);
2223 LASSERT(info->eti_big_lmm);
2224 OBD_FREE_LARGE(info->eti_big_lmm, info->eti_big_lmmsize);
2225 info->eti_big_lmm = NULL;
2226 info->eti_big_lmmsize = 0;
2228 cl_env_put(env, &refcheck);
2231 #endif /* HAVE_SERVER_SUPPORT */
2233 static int echo_create_object(const struct lu_env *env, struct echo_device *ed,
2234 int on_target, struct obdo *oa, void *ulsm,
2235 int ulsm_nob, struct obd_trans_info *oti)
2237 struct echo_object *eco;
2238 struct echo_client_obd *ec = ed->ed_ec;
2239 struct lov_stripe_md *lsm = NULL;
2244 if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
2245 (on_target || /* set_stripe */
2246 ec->ec_nstripes != 0)) { /* LOV */
2247 CERROR ("No valid oid\n");
2251 rc = echo_alloc_memmd(ed, &lsm);
2253 CERROR("Cannot allocate md: rc = %d\n", rc);
2260 rc = echo_copyin_lsm (ed, lsm, ulsm, ulsm_nob);
2264 if (lsm->lsm_stripe_count == 0)
2265 lsm->lsm_stripe_count = ec->ec_nstripes;
2267 if (lsm->lsm_stripe_size == 0)
2268 lsm->lsm_stripe_size = PAGE_CACHE_SIZE;
2272 /* setup stripes: indices + default ids if required */
2273 for (i = 0; i < lsm->lsm_stripe_count; i++) {
2274 if (ostid_id(&lsm->lsm_oinfo[i]->loi_oi) == 0)
2275 lsm->lsm_oinfo[i]->loi_oi = lsm->lsm_oi;
2277 lsm->lsm_oinfo[i]->loi_ost_idx =
2278 (idx + i) % ec->ec_nstripes;
2282 /* setup object ID here for !on_target and LOV hint */
2283 if (oa->o_valid & OBD_MD_FLID) {
2284 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2285 lsm->lsm_oi = oa->o_oi;
2288 if (ostid_id(&lsm->lsm_oi) == 0)
2289 ostid_set_id(&lsm->lsm_oi, ++last_object_id);
2293 /* Only echo objects are allowed to be created */
2294 LASSERT((oa->o_valid & OBD_MD_FLGROUP) &&
2295 (ostid_seq(&oa->o_oi) == FID_SEQ_ECHO));
2296 rc = obd_create(env, ec->ec_exp, oa, &lsm, oti);
2298 CERROR("Cannot create objects: rc = %d\n", rc);
2304 /* See what object ID we were given */
2305 oa->o_oi = lsm->lsm_oi;
2306 oa->o_valid |= OBD_MD_FLID;
2308 eco = cl_echo_object_find(ed, &lsm);
2310 GOTO(failed, rc = PTR_ERR(eco));
2311 cl_echo_object_put(eco);
2313 CDEBUG(D_INFO, "oa oid "DOSTID"\n", POSTID(&oa->o_oi));
2318 obd_destroy(env, ec->ec_exp, oa, lsm, oti, NULL, NULL);
2320 echo_free_memmd(ed, &lsm);
2322 CERROR("create object failed with: rc = %d\n", rc);
2326 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
2329 struct lov_stripe_md *lsm = NULL;
2330 struct echo_object *eco;
2334 if ((oa->o_valid & OBD_MD_FLID) == 0 || ostid_id(&oa->o_oi) == 0) {
2335 /* disallow use of object id 0 */
2336 CERROR ("No valid oid\n");
2340 rc = echo_alloc_memmd(ed, &lsm);
2344 lsm->lsm_oi = oa->o_oi;
2345 if (!(oa->o_valid & OBD_MD_FLGROUP))
2346 ostid_set_seq_echo(&lsm->lsm_oi);
2349 eco = cl_echo_object_find(ed, &lsm);
2355 echo_free_memmd(ed, &lsm);
2359 static void echo_put_object(struct echo_object *eco)
2361 if (cl_echo_object_put(eco))
2362 CERROR("echo client: drop an object failed");
2366 echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
2368 unsigned long stripe_count;
2369 unsigned long stripe_size;
2370 unsigned long width;
2371 unsigned long woffset;
2375 if (lsm->lsm_stripe_count <= 1)
2379 stripe_size = lsm->lsm_stripe_size;
2380 stripe_count = lsm->lsm_stripe_count;
2382 /* width = # bytes in all stripes */
2383 width = stripe_size * stripe_count;
2385 /* woffset = offset within a width; offset = whole number of widths */
2386 woffset = do_div (offset, width);
2388 stripe_index = woffset / stripe_size;
2390 *idp = ostid_id(&lsm->lsm_oinfo[stripe_index]->loi_oi);
2391 *offp = offset * stripe_size + woffset % stripe_size;
2395 echo_client_page_debug_setup(struct lov_stripe_md *lsm,
2396 struct page *page, int rw, obd_id id,
2397 obd_off offset, obd_off count)
2404 /* no partial pages on the client */
2405 LASSERT(count == PAGE_CACHE_SIZE);
2409 for (delta = 0; delta < PAGE_CACHE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2410 if (rw == OBD_BRW_WRITE) {
2411 stripe_off = offset + delta;
2413 echo_get_stripe_off_id(lsm, &stripe_off, &stripe_id);
2415 stripe_off = 0xdeadbeef00c0ffeeULL;
2416 stripe_id = 0xdeadbeef00c0ffeeULL;
2418 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
2419 stripe_off, stripe_id);
2425 static int echo_client_page_debug_check(struct lov_stripe_md *lsm,
2426 struct page *page, obd_id id,
2427 obd_off offset, obd_off count)
2436 /* no partial pages on the client */
2437 LASSERT(count == PAGE_CACHE_SIZE);
2441 for (rc = delta = 0; delta < PAGE_CACHE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2442 stripe_off = offset + delta;
2444 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
2446 rc2 = block_debug_check("test_brw",
2447 addr + delta, OBD_ECHO_BLOCK_SIZE,
2448 stripe_off, stripe_id);
2450 CERROR ("Error in echo object "LPX64"\n", id);
2459 static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
2460 struct echo_object *eco, obd_off offset,
2461 obd_size count, int async,
2462 struct obd_trans_info *oti)
2464 struct lov_stripe_md *lsm = eco->eo_lsm;
2466 struct brw_page *pga;
2467 struct brw_page *pgp;
2468 struct page **pages;
2477 verify = (ostid_id(&oa->o_oi) != ECHO_PERSISTENT_OBJID &&
2478 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
2479 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
2481 gfp_mask = ((ostid_id(&oa->o_oi) & 2) == 0) ? GFP_IOFS : GFP_HIGHUSER;
2483 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
2484 LASSERT(lsm != NULL);
2485 LASSERT(ostid_id(&lsm->lsm_oi) == ostid_id(&oa->o_oi));
2488 (count & (~CFS_PAGE_MASK)) != 0)
2491 /* XXX think again with misaligned I/O */
2492 npages = count >> PAGE_CACHE_SHIFT;
2494 if (rw == OBD_BRW_WRITE)
2495 brw_flags = OBD_BRW_ASYNC;
2497 OBD_ALLOC(pga, npages * sizeof(*pga));
2501 OBD_ALLOC(pages, npages * sizeof(*pages));
2502 if (pages == NULL) {
2503 OBD_FREE(pga, npages * sizeof(*pga));
2507 for (i = 0, pgp = pga, off = offset;
2509 i++, pgp++, off += PAGE_CACHE_SIZE) {
2511 LASSERT (pgp->pg == NULL); /* for cleanup */
2514 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
2515 if (pgp->pg == NULL)
2519 pgp->count = PAGE_CACHE_SIZE;
2521 pgp->flag = brw_flags;
2524 echo_client_page_debug_setup(lsm, pgp->pg, rw,
2525 ostid_id(&oa->o_oi), off,
2529 /* brw mode can only be used at client */
2530 LASSERT(ed->ed_next != NULL);
2531 rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
2534 if (rc != 0 || rw != OBD_BRW_READ)
2537 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
2538 if (pgp->pg == NULL)
2543 vrc = echo_client_page_debug_check(lsm, pgp->pg,
2544 ostid_id(&oa->o_oi),
2545 pgp->off, pgp->count);
2546 if (vrc != 0 && rc == 0)
2549 OBD_PAGE_FREE(pgp->pg);
2551 OBD_FREE(pga, npages * sizeof(*pga));
2552 OBD_FREE(pages, npages * sizeof(*pages));
2556 static int echo_client_prep_commit(const struct lu_env *env,
2557 struct obd_export *exp, int rw,
2558 struct obdo *oa, struct echo_object *eco,
2559 obd_off offset, obd_size count,
2560 obd_size batch, struct obd_trans_info *oti,
2563 struct lov_stripe_md *lsm = eco->eo_lsm;
2564 struct obd_ioobj ioo;
2565 struct niobuf_local *lnb;
2566 struct niobuf_remote *rnb;
2568 obd_size npages, tot_pages;
2569 int i, ret = 0, brw_flags = 0;
2573 if (count <= 0 || (count & (~CFS_PAGE_MASK)) != 0 ||
2574 (lsm != NULL && ostid_id(&lsm->lsm_oi) != ostid_id(&oa->o_oi)))
2577 npages = batch >> PAGE_CACHE_SHIFT;
2578 tot_pages = count >> PAGE_CACHE_SHIFT;
2580 OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
2581 OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
2583 if (lnb == NULL || rnb == NULL)
2584 GOTO(out, ret = -ENOMEM);
2586 if (rw == OBD_BRW_WRITE && async)
2587 brw_flags |= OBD_BRW_ASYNC;
2589 obdo_to_ioobj(oa, &ioo);
2593 for(; tot_pages; tot_pages -= npages) {
2596 if (tot_pages < npages)
2599 for (i = 0; i < npages; i++, off += PAGE_CACHE_SIZE) {
2600 rnb[i].offset = off;
2601 rnb[i].len = PAGE_CACHE_SIZE;
2602 rnb[i].flags = brw_flags;
2605 ioo.ioo_bufcnt = npages;
2606 oti->oti_transno = 0;
2609 ret = obd_preprw(env, rw, exp, oa, 1, &ioo, rnb, &lpages,
2613 LASSERT(lpages == npages);
2615 for (i = 0; i < lpages; i++) {
2616 struct page *page = lnb[i].page;
2618 /* read past eof? */
2619 if (page == NULL && lnb[i].rc == 0)
2623 lnb[i].flags |= OBD_BRW_ASYNC;
2625 if (ostid_id(&oa->o_oi) == ECHO_PERSISTENT_OBJID ||
2626 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
2627 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
2630 if (rw == OBD_BRW_WRITE)
2631 echo_client_page_debug_setup(lsm, page, rw,
2632 ostid_id(&oa->o_oi),
2636 echo_client_page_debug_check(lsm, page,
2637 ostid_id(&oa->o_oi),
2642 ret = obd_commitrw(env, rw, exp, oa, 1, &ioo,
2643 rnb, npages, lnb, oti, ret);
2647 /* Reset oti otherwise it would confuse ldiskfs. */
2648 memset(oti, 0, sizeof(*oti));
2650 /* Reuse env context. */
2651 lu_context_exit((struct lu_context *)&env->le_ctx);
2652 lu_context_enter((struct lu_context *)&env->le_ctx);
2657 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
2659 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
2663 static int echo_client_brw_ioctl(const struct lu_env *env, int rw,
2664 struct obd_export *exp,
2665 struct obd_ioctl_data *data,
2666 struct obd_trans_info *dummy_oti)
2668 struct obd_device *obd = class_exp2obd(exp);
2669 struct echo_device *ed = obd2echo_dev(obd);
2670 struct echo_client_obd *ec = ed->ed_ec;
2671 struct obdo *oa = &data->ioc_obdo1;
2672 struct echo_object *eco;
2678 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2680 rc = echo_get_object(&eco, ed, oa);
2684 oa->o_valid &= ~OBD_MD_FLHANDLE;
2686 /* OFD/obdfilter works only via prep/commit */
2687 test_mode = (long)data->ioc_pbuf1;
2691 if (ed->ed_next == NULL && test_mode != 3) {
2693 data->ioc_plen1 = data->ioc_count;
2696 /* Truncate batch size to maximum */
2697 if (data->ioc_plen1 > PTLRPC_MAX_BRW_SIZE)
2698 data->ioc_plen1 = PTLRPC_MAX_BRW_SIZE;
2700 switch (test_mode) {
2704 rc = echo_client_kbrw(ed, rw, oa,
2705 eco, data->ioc_offset,
2706 data->ioc_count, async, dummy_oti);
2709 rc = echo_client_prep_commit(env, ec->ec_exp, rw, oa,
2710 eco, data->ioc_offset,
2711 data->ioc_count, data->ioc_plen1,
2717 echo_put_object(eco);
2722 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
2723 int mode, obd_off offset, obd_size nob)
2725 struct echo_device *ed = obd2echo_dev(exp->exp_obd);
2726 struct lustre_handle *ulh = &oa->o_handle;
2727 struct echo_object *eco;
2732 if (ed->ed_next == NULL)
2733 RETURN(-EOPNOTSUPP);
2735 if (!(mode == LCK_PR || mode == LCK_PW))
2738 if ((offset & (~CFS_PAGE_MASK)) != 0 ||
2739 (nob & (~CFS_PAGE_MASK)) != 0)
2742 rc = echo_get_object (&eco, ed, oa);
2746 end = (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
2747 rc = cl_echo_enqueue(eco, offset, end, mode, &ulh->cookie);
2749 oa->o_valid |= OBD_MD_FLHANDLE;
2750 CDEBUG(D_INFO, "Cookie is "LPX64"\n", ulh->cookie);
2752 echo_put_object(eco);
2757 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
2759 struct echo_device *ed = obd2echo_dev(exp->exp_obd);
2760 __u64 cookie = oa->o_handle.cookie;
2762 if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
2765 CDEBUG(D_INFO, "Cookie is "LPX64"\n", cookie);
2766 return cl_echo_cancel(ed, cookie);
2770 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2771 void *karg, void *uarg)
2773 #ifdef HAVE_SERVER_SUPPORT
2774 struct tgt_session_info *tsi;
2776 struct obd_device *obd = exp->exp_obd;
2777 struct echo_device *ed = obd2echo_dev(obd);
2778 struct echo_client_obd *ec = ed->ed_ec;
2779 struct echo_object *eco;
2780 struct obd_ioctl_data *data = karg;
2781 struct obd_trans_info dummy_oti;
2783 struct oti_req_ack_lock *ack_lock;
2786 int rw = OBD_BRW_READ;
2789 #ifdef HAVE_SERVER_SUPPORT
2790 struct lu_context echo_session;
2794 memset(&dummy_oti, 0, sizeof(dummy_oti));
2796 oa = &data->ioc_obdo1;
2797 if (!(oa->o_valid & OBD_MD_FLGROUP)) {
2798 oa->o_valid |= OBD_MD_FLGROUP;
2799 ostid_set_seq_echo(&oa->o_oi);
2802 /* This FID is unpacked just for validation at this point */
2803 rc = ostid_to_fid(&fid, &oa->o_oi, 0);
2811 rc = lu_env_init(env, LCT_DT_THREAD);
2813 GOTO(out_alloc, rc = -ENOMEM);
2815 #ifdef HAVE_SERVER_SUPPORT
2816 env->le_ses = &echo_session;
2817 rc = lu_context_init(env->le_ses, LCT_SERVER_SESSION | LCT_NOREF);
2818 if (unlikely(rc < 0))
2820 lu_context_enter(env->le_ses);
2822 tsi = tgt_ses_info(env);
2823 tsi->tsi_exp = ec->ec_exp;
2826 case OBD_IOC_CREATE: /* may create echo object */
2827 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2828 GOTO (out, rc = -EPERM);
2830 rc = echo_create_object(env, ed, 1, oa, data->ioc_pbuf1,
2831 data->ioc_plen1, &dummy_oti);
2834 #ifdef HAVE_SERVER_SUPPORT
2835 case OBD_IOC_ECHO_MD: {
2842 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2843 GOTO(out, rc = -EPERM);
2845 count = data->ioc_count;
2846 cmd = data->ioc_command;
2848 id = data->ioc_obdo2.o_oi.oi.oi_id;
2849 dirlen = data->ioc_plen1;
2850 OBD_ALLOC(dir, dirlen + 1);
2852 GOTO(out, rc = -ENOMEM);
2854 if (copy_from_user(dir, data->ioc_pbuf1, dirlen)) {
2855 OBD_FREE(dir, data->ioc_plen1 + 1);
2856 GOTO(out, rc = -EFAULT);
2859 rc = echo_md_handler(ed, cmd, dir, dirlen, id, count, data);
2860 OBD_FREE(dir, dirlen + 1);
2863 case OBD_IOC_ECHO_ALLOC_SEQ: {
2864 struct lu_env *cl_env;
2869 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2870 GOTO(out, rc = -EPERM);
2872 cl_env = cl_env_get(&refcheck);
2874 GOTO(out, rc = PTR_ERR(cl_env));
2876 rc = lu_env_refill_by_tags(cl_env, ECHO_MD_CTX_TAG,
2879 cl_env_put(cl_env, &refcheck);
2883 rc = seq_client_get_seq(cl_env, ed->ed_cl_seq, &seq);
2884 cl_env_put(cl_env, &refcheck);
2886 CERROR("%s: Can not alloc seq: rc = %d\n",
2891 if (copy_to_user(data->ioc_pbuf1, &seq, data->ioc_plen1))
2894 max_count = LUSTRE_METADATA_SEQ_MAX_WIDTH;
2895 if (copy_to_user(data->ioc_pbuf2, &max_count,
2900 #endif /* HAVE_SERVER_SUPPORT */
2901 case OBD_IOC_DESTROY:
2902 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2903 GOTO (out, rc = -EPERM);
2905 rc = echo_get_object(&eco, ed, oa);
2907 rc = obd_destroy(env, ec->ec_exp, oa, eco->eo_lsm,
2908 &dummy_oti, NULL, NULL);
2910 eco->eo_deleted = 1;
2911 echo_put_object(eco);
2915 case OBD_IOC_GETATTR:
2916 rc = echo_get_object(&eco, ed, oa);
2918 struct obd_info oinfo = { { { 0 } } };
2919 oinfo.oi_md = eco->eo_lsm;
2921 rc = obd_getattr(env, ec->ec_exp, &oinfo);
2922 echo_put_object(eco);
2926 case OBD_IOC_SETATTR:
2927 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2928 GOTO (out, rc = -EPERM);
2930 rc = echo_get_object(&eco, ed, oa);
2932 struct obd_info oinfo = { { { 0 } } };
2934 oinfo.oi_md = eco->eo_lsm;
2936 rc = obd_setattr(env, ec->ec_exp, &oinfo, NULL);
2937 echo_put_object(eco);
2941 case OBD_IOC_BRW_WRITE:
2942 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2943 GOTO (out, rc = -EPERM);
2947 case OBD_IOC_BRW_READ:
2948 rc = echo_client_brw_ioctl(env, rw, exp, data, &dummy_oti);
2951 case ECHO_IOC_GET_STRIPE:
2952 rc = echo_get_object(&eco, ed, oa);
2954 rc = echo_copyout_lsm(eco->eo_lsm, data->ioc_pbuf1,
2956 echo_put_object(eco);
2960 case ECHO_IOC_SET_STRIPE:
2961 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2962 GOTO (out, rc = -EPERM);
2964 if (data->ioc_pbuf1 == NULL) { /* unset */
2965 rc = echo_get_object(&eco, ed, oa);
2967 eco->eo_deleted = 1;
2968 echo_put_object(eco);
2971 rc = echo_create_object(env, ed, 0, oa,
2973 data->ioc_plen1, &dummy_oti);
2977 case ECHO_IOC_ENQUEUE:
2978 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2979 GOTO (out, rc = -EPERM);
2981 rc = echo_client_enqueue(exp, oa,
2982 data->ioc_conn1, /* lock mode */
2984 data->ioc_count);/*extent*/
2987 case ECHO_IOC_CANCEL:
2988 rc = echo_client_cancel(exp, oa);
2992 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
2993 GOTO (out, rc = -ENOTTY);
2998 #ifdef HAVE_SERVER_SUPPORT
2999 lu_context_exit(env->le_ses);
3000 lu_context_fini(env->le_ses);
3007 /* XXX this should be in a helper also called by target_send_reply */
3008 for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
3010 if (!ack_lock->mode)
3012 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
3018 static int echo_client_setup(const struct lu_env *env,
3019 struct obd_device *obddev, struct lustre_cfg *lcfg)
3021 struct echo_client_obd *ec = &obddev->u.echo_client;
3022 struct obd_device *tgt;
3023 struct obd_uuid echo_uuid = { "ECHO_UUID" };
3024 struct obd_connect_data *ocd = NULL;
3028 if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
3029 CERROR("requires a TARGET OBD name\n");
3033 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
3034 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
3035 CERROR("device not attached or not set up (%s)\n",
3036 lustre_cfg_string(lcfg, 1));
3040 spin_lock_init(&ec->ec_lock);
3041 CFS_INIT_LIST_HEAD (&ec->ec_objects);
3042 CFS_INIT_LIST_HEAD (&ec->ec_locks);
3044 ec->ec_nstripes = 0;
3046 if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
3047 #ifdef HAVE_SERVER_SUPPORT
3048 lu_context_tags_update(ECHO_MD_CTX_TAG);
3049 lu_session_tags_update(ECHO_MD_SES_TAG);
3051 CERROR("Local operations are NOT supported on client side. "
3052 "Only remote operations are supported. Metadata client "
3053 "must be run on server side.\n");
3058 OBD_ALLOC(ocd, sizeof(*ocd));
3060 CERROR("Can't alloc ocd connecting to %s\n",
3061 lustre_cfg_string(lcfg, 1));
3065 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
3066 OBD_CONNECT_BRW_SIZE |
3067 OBD_CONNECT_GRANT | OBD_CONNECT_FULL20 |
3068 OBD_CONNECT_64BITHASH | OBD_CONNECT_LVB_TYPE |
3070 ocd->ocd_brw_size = DT_MAX_BRW_SIZE;
3071 ocd->ocd_version = LUSTRE_VERSION_CODE;
3072 ocd->ocd_group = FID_SEQ_ECHO;
3074 rc = obd_connect(env, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
3076 /* Turn off pinger because it connects to tgt obd directly. */
3077 spin_lock(&tgt->obd_dev_lock);
3078 cfs_list_del_init(&ec->ec_exp->exp_obd_chain_timed);
3079 spin_unlock(&tgt->obd_dev_lock);
3082 OBD_FREE(ocd, sizeof(*ocd));
3085 CERROR("fail to connect to device %s\n",
3086 lustre_cfg_string(lcfg, 1));
3093 static int echo_client_cleanup(struct obd_device *obddev)
3095 struct echo_device *ed = obd2echo_dev(obddev);
3096 struct echo_client_obd *ec = &obddev->u.echo_client;
3100 /*Do nothing for Metadata echo client*/
3104 if (ed->ed_next_ismd) {
3105 #ifdef HAVE_SERVER_SUPPORT
3106 lu_context_tags_clear(ECHO_MD_CTX_TAG);
3107 lu_session_tags_clear(ECHO_MD_SES_TAG);
3109 CERROR("This is client-side only module, does not support "
3110 "metadata echo client.\n");
3115 if (!cfs_list_empty(&obddev->obd_exports)) {
3116 CERROR("still has clients!\n");
3120 LASSERT(atomic_read(&ec->ec_exp->exp_refcount) > 0);
3121 rc = obd_disconnect(ec->ec_exp);
3123 CERROR("fail to disconnect device: %d\n", rc);
3128 static int echo_client_connect(const struct lu_env *env,
3129 struct obd_export **exp,
3130 struct obd_device *src, struct obd_uuid *cluuid,
3131 struct obd_connect_data *data, void *localdata)
3134 struct lustre_handle conn = { 0 };
3137 rc = class_connect(&conn, src, cluuid);
3139 *exp = class_conn2export(&conn);
3145 static int echo_client_disconnect(struct obd_export *exp)
3151 GOTO(out, rc = -EINVAL);
3153 rc = class_disconnect(exp);
3159 static struct obd_ops echo_client_obd_ops = {
3160 .o_owner = THIS_MODULE,
3161 .o_iocontrol = echo_client_iocontrol,
3162 .o_connect = echo_client_connect,
3163 .o_disconnect = echo_client_disconnect
3166 int echo_client_init(void)
3170 rc = lu_kmem_init(echo_caches);
3172 rc = class_register_type(&echo_client_obd_ops, NULL, NULL,
3173 #ifndef HAVE_ONLY_PROCFS_SEQ
3176 LUSTRE_ECHO_CLIENT_NAME,
3179 lu_kmem_fini(echo_caches);
3184 void echo_client_exit(void)
3186 class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
3187 lu_kmem_fini(echo_caches);
3191 static int __init obdecho_init(void)
3196 LCONSOLE_INFO("Echo OBD driver; http://www.lustre.org/\n");
3198 LASSERT(PAGE_CACHE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
3200 # ifdef HAVE_SERVER_SUPPORT
3201 rc = echo_persistent_pages_init();
3205 rc = class_register_type(&echo_obd_ops, NULL, NULL,
3206 #ifndef HAVE_ONLY_PROCFS_SEQ
3209 LUSTRE_ECHO_NAME, NULL);
3214 rc = echo_client_init();
3216 # ifdef HAVE_SERVER_SUPPORT
3220 class_unregister_type(LUSTRE_ECHO_NAME);
3222 echo_persistent_pages_fini();
3228 static void /*__exit*/ obdecho_exit(void)
3232 # ifdef HAVE_SERVER_SUPPORT
3233 class_unregister_type(LUSTRE_ECHO_NAME);
3234 echo_persistent_pages_fini();
3238 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3239 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
3240 MODULE_LICENSE("GPL");
3242 cfs_module(obdecho, LUSTRE_VERSION_STRING, obdecho_init, obdecho_exit);
3243 #endif /* __KERNEL__ */
3245 /** @} echo_client */