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_descr eti_descr;
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 void echo_lock_delete(const struct lu_env *env,
349 const struct cl_lock_slice *slice)
351 struct echo_lock *ecl = cl2echo_lock(slice);
353 LASSERT(list_empty(&ecl->el_chain));
356 static int echo_lock_fits_into(const struct lu_env *env,
357 const struct cl_lock_slice *slice,
358 const struct cl_lock_descr *need,
359 const struct cl_io *unused)
364 static struct cl_lock_operations echo_lock_ops = {
365 .clo_fini = echo_lock_fini,
366 .clo_delete = echo_lock_delete,
367 .clo_fits_into = echo_lock_fits_into
372 /** \defgroup echo_cl_ops cl_object operations
374 * operations for cl_object
378 static int echo_page_init(const struct lu_env *env, struct cl_object *obj,
379 struct cl_page *page, pgoff_t index)
381 struct echo_page *ep = cl_object_page_slice(obj, page);
382 struct echo_object *eco = cl2echo_obj(obj);
385 page_cache_get(page->cp_vmpage);
386 mutex_init(&ep->ep_lock);
387 cl_page_slice_add(page, &ep->ep_cl, obj, index, &echo_page_ops);
388 atomic_inc(&eco->eo_npages);
392 static int echo_io_init(const struct lu_env *env, struct cl_object *obj,
398 static int echo_lock_init(const struct lu_env *env,
399 struct cl_object *obj, struct cl_lock *lock,
400 const struct cl_io *unused)
402 struct echo_lock *el;
405 OBD_SLAB_ALLOC_PTR_GFP(el, echo_lock_kmem, GFP_NOFS);
407 cl_lock_slice_add(lock, &el->el_cl, obj, &echo_lock_ops);
408 el->el_object = cl2echo_obj(obj);
409 INIT_LIST_HEAD(&el->el_chain);
410 atomic_set(&el->el_refcount, 0);
412 RETURN(el == NULL ? -ENOMEM : 0);
415 static int echo_conf_set(const struct lu_env *env, struct cl_object *obj,
416 const struct cl_object_conf *conf)
421 static const struct cl_object_operations echo_cl_obj_ops = {
422 .coo_page_init = echo_page_init,
423 .coo_lock_init = echo_lock_init,
424 .coo_io_init = echo_io_init,
425 .coo_conf_set = echo_conf_set
427 /** @} echo_cl_ops */
429 /** \defgroup echo_lu_ops lu_object operations
431 * operations for echo lu object.
435 static int echo_object_init(const struct lu_env *env, struct lu_object *obj,
436 const struct lu_object_conf *conf)
438 struct echo_device *ed = cl2echo_dev(lu2cl_dev(obj->lo_dev));
439 struct echo_client_obd *ec = ed->ed_ec;
440 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
444 struct lu_object *below;
445 struct lu_device *under;
448 below = under->ld_ops->ldo_object_alloc(env, obj->lo_header,
452 lu_object_add(obj, below);
455 if (!ed->ed_next_ismd) {
456 const struct cl_object_conf *cconf = lu2cl_conf(conf);
457 struct echo_object_conf *econf = cl2echo_conf(cconf);
459 LASSERT(econf->eoc_md);
460 eco->eo_lsm = *econf->eoc_md;
461 /* clear the lsm pointer so that it won't get freed. */
462 *econf->eoc_md = NULL;
468 atomic_set(&eco->eo_npages, 0);
469 cl_object_page_init(lu2cl(obj), sizeof(struct echo_page));
471 spin_lock(&ec->ec_lock);
472 list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
473 spin_unlock(&ec->ec_lock);
478 /* taken from osc_unpackmd() */
479 static int echo_alloc_memmd(struct echo_device *ed,
480 struct lov_stripe_md **lsmp)
486 /* If export is lov/osc then use their obd method */
487 if (ed->ed_next != NULL)
488 return obd_alloc_memmd(ed->ed_ec->ec_exp, lsmp);
489 /* OFD has no unpackmd method, do everything here */
490 lsm_size = lov_stripe_md_size(1);
492 LASSERT(*lsmp == NULL);
493 OBD_ALLOC(*lsmp, lsm_size);
497 OBD_ALLOC((*lsmp)->lsm_oinfo[0], sizeof(struct lov_oinfo));
498 if ((*lsmp)->lsm_oinfo[0] == NULL) {
499 OBD_FREE(*lsmp, lsm_size);
503 loi_init((*lsmp)->lsm_oinfo[0]);
504 (*lsmp)->lsm_maxbytes = LUSTRE_EXT3_STRIPE_MAXBYTES;
505 ostid_set_seq_echo(&(*lsmp)->lsm_oi);
510 static int echo_free_memmd(struct echo_device *ed, struct lov_stripe_md **lsmp)
516 /* If export is lov/osc then use their obd method */
517 if (ed->ed_next != NULL)
518 return obd_free_memmd(ed->ed_ec->ec_exp, lsmp);
519 /* OFD has no unpackmd method, do everything here */
520 lsm_size = lov_stripe_md_size(1);
522 LASSERT(*lsmp != NULL);
523 OBD_FREE((*lsmp)->lsm_oinfo[0], sizeof(struct lov_oinfo));
524 OBD_FREE(*lsmp, lsm_size);
529 static void echo_object_free(const struct lu_env *env, struct lu_object *obj)
531 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
532 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
535 LASSERT(atomic_read(&eco->eo_npages) == 0);
537 spin_lock(&ec->ec_lock);
538 list_del_init(&eco->eo_obj_chain);
539 spin_unlock(&ec->ec_lock);
542 lu_object_header_fini(obj->lo_header);
545 echo_free_memmd(eco->eo_dev, &eco->eo_lsm);
546 OBD_SLAB_FREE_PTR(eco, echo_object_kmem);
550 static int echo_object_print(const struct lu_env *env, void *cookie,
551 lu_printer_t p, const struct lu_object *o)
553 struct echo_object *obj = cl2echo_obj(lu2cl(o));
555 return (*p)(env, cookie, "echoclient-object@%p", obj);
558 static const struct lu_object_operations echo_lu_obj_ops = {
559 .loo_object_init = echo_object_init,
560 .loo_object_delete = NULL,
561 .loo_object_release = NULL,
562 .loo_object_free = echo_object_free,
563 .loo_object_print = echo_object_print,
564 .loo_object_invariant = NULL
566 /** @} echo_lu_ops */
568 /** \defgroup echo_lu_dev_ops lu_device operations
570 * Operations for echo lu device.
574 static struct lu_object *echo_object_alloc(const struct lu_env *env,
575 const struct lu_object_header *hdr,
576 struct lu_device *dev)
578 struct echo_object *eco;
579 struct lu_object *obj = NULL;
582 /* we're the top dev. */
583 LASSERT(hdr == NULL);
584 OBD_SLAB_ALLOC_PTR_GFP(eco, echo_object_kmem, GFP_NOFS);
586 struct cl_object_header *hdr = &eco->eo_hdr;
588 obj = &echo_obj2cl(eco)->co_lu;
589 cl_object_header_init(hdr);
590 hdr->coh_page_bufsize = cfs_size_round(sizeof(struct cl_page));
592 lu_object_init(obj, &hdr->coh_lu, dev);
593 lu_object_add_top(&hdr->coh_lu, obj);
595 eco->eo_cl.co_ops = &echo_cl_obj_ops;
596 obj->lo_ops = &echo_lu_obj_ops;
601 static struct lu_device_operations echo_device_lu_ops = {
602 .ldo_object_alloc = echo_object_alloc,
605 /** @} echo_lu_dev_ops */
607 static struct cl_device_operations echo_device_cl_ops = {
610 /** \defgroup echo_init Setup and teardown
612 * Init and fini functions for echo client.
616 static int echo_site_init(const struct lu_env *env, struct echo_device *ed)
618 struct cl_site *site = &ed->ed_site_myself;
621 /* initialize site */
622 rc = cl_site_init(site, &ed->ed_cl);
624 CERROR("Cannot initilize site for echo client(%d)\n", rc);
628 rc = lu_site_init_finish(&site->cs_lu);
636 static void echo_site_fini(const struct lu_env *env, struct echo_device *ed)
639 if (!ed->ed_next_ismd)
640 cl_site_fini(ed->ed_site);
645 static void *echo_thread_key_init(const struct lu_context *ctx,
646 struct lu_context_key *key)
648 struct echo_thread_info *info;
650 OBD_SLAB_ALLOC_PTR_GFP(info, echo_thread_kmem, GFP_NOFS);
652 info = ERR_PTR(-ENOMEM);
656 static void echo_thread_key_fini(const struct lu_context *ctx,
657 struct lu_context_key *key, void *data)
659 struct echo_thread_info *info = data;
660 OBD_SLAB_FREE_PTR(info, echo_thread_kmem);
663 static void echo_thread_key_exit(const struct lu_context *ctx,
664 struct lu_context_key *key, void *data)
668 static struct lu_context_key echo_thread_key = {
669 .lct_tags = LCT_CL_THREAD,
670 .lct_init = echo_thread_key_init,
671 .lct_fini = echo_thread_key_fini,
672 .lct_exit = echo_thread_key_exit
675 static void *echo_session_key_init(const struct lu_context *ctx,
676 struct lu_context_key *key)
678 struct echo_session_info *session;
680 OBD_SLAB_ALLOC_PTR_GFP(session, echo_session_kmem, GFP_NOFS);
682 session = ERR_PTR(-ENOMEM);
686 static void echo_session_key_fini(const struct lu_context *ctx,
687 struct lu_context_key *key, void *data)
689 struct echo_session_info *session = data;
690 OBD_SLAB_FREE_PTR(session, echo_session_kmem);
693 static void echo_session_key_exit(const struct lu_context *ctx,
694 struct lu_context_key *key, void *data)
698 static struct lu_context_key echo_session_key = {
699 .lct_tags = LCT_SESSION,
700 .lct_init = echo_session_key_init,
701 .lct_fini = echo_session_key_fini,
702 .lct_exit = echo_session_key_exit
705 LU_TYPE_INIT_FINI(echo, &echo_thread_key, &echo_session_key);
707 #ifdef HAVE_SERVER_SUPPORT
708 # define ECHO_SEQ_WIDTH 0xffffffff
709 static int echo_fid_init(struct echo_device *ed, char *obd_name,
710 struct seq_server_site *ss)
716 OBD_ALLOC_PTR(ed->ed_cl_seq);
717 if (ed->ed_cl_seq == NULL)
720 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
722 GOTO(out_free_seq, rc = -ENOMEM);
724 snprintf(prefix, MAX_OBD_NAME + 5, "srv-%s", obd_name);
726 /* Init client side sequence-manager */
727 rc = seq_client_init(ed->ed_cl_seq, NULL,
729 prefix, ss->ss_server_seq);
730 ed->ed_cl_seq->lcs_width = ECHO_SEQ_WIDTH;
731 OBD_FREE(prefix, MAX_OBD_NAME + 5);
733 GOTO(out_free_seq, rc);
738 OBD_FREE_PTR(ed->ed_cl_seq);
739 ed->ed_cl_seq = NULL;
743 static int echo_fid_fini(struct obd_device *obddev)
745 struct echo_device *ed = obd2echo_dev(obddev);
748 if (ed->ed_cl_seq != NULL) {
749 seq_client_fini(ed->ed_cl_seq);
750 OBD_FREE_PTR(ed->ed_cl_seq);
751 ed->ed_cl_seq = NULL;
756 #endif /* HAVE_SERVER_SUPPORT */
758 static struct lu_device *echo_device_alloc(const struct lu_env *env,
759 struct lu_device_type *t,
760 struct lustre_cfg *cfg)
762 struct lu_device *next;
763 struct echo_device *ed;
764 struct cl_device *cd;
765 struct obd_device *obd = NULL; /* to keep compiler happy */
766 struct obd_device *tgt;
767 const char *tgt_type_name;
774 GOTO(out, rc = -ENOMEM);
778 rc = cl_device_init(cd, t);
782 cd->cd_lu_dev.ld_ops = &echo_device_lu_ops;
783 cd->cd_ops = &echo_device_cl_ops;
786 obd = class_name2obd(lustre_cfg_string(cfg, 0));
787 LASSERT(obd != NULL);
788 LASSERT(env != NULL);
790 tgt = class_name2obd(lustre_cfg_string(cfg, 1));
792 CERROR("Can not find tgt device %s\n",
793 lustre_cfg_string(cfg, 1));
794 GOTO(out, rc = -ENODEV);
797 next = tgt->obd_lu_dev;
798 if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
799 ed->ed_next_ismd = 1;
801 ed->ed_next_ismd = 0;
802 rc = echo_site_init(env, ed);
808 rc = echo_client_setup(env, obd, cfg);
812 ed->ed_ec = &obd->u.echo_client;
815 if (ed->ed_next_ismd) {
816 #ifdef HAVE_SERVER_SUPPORT
817 /* Suppose to connect to some Metadata layer */
819 struct lu_device *ld;
823 CERROR("%s is not lu device type!\n",
824 lustre_cfg_string(cfg, 1));
825 GOTO(out, rc = -EINVAL);
828 tgt_type_name = lustre_cfg_string(cfg, 2);
829 if (!tgt_type_name) {
830 CERROR("%s no type name for echo %s setup\n",
831 lustre_cfg_string(cfg, 1),
832 tgt->obd_type->typ_name);
833 GOTO(out, rc = -EINVAL);
838 spin_lock(&ls->ls_ld_lock);
839 list_for_each_entry(ld, &ls->ls_ld_linkage, ld_linkage) {
840 if (strcmp(ld->ld_type->ldt_name, tgt_type_name) == 0) {
845 spin_unlock(&ls->ls_ld_lock);
848 CERROR("%s is not lu device type!\n",
849 lustre_cfg_string(cfg, 1));
850 GOTO(out, rc = -EINVAL);
854 /* For MD echo client, it will use the site in MDS stack */
855 ed->ed_site_myself.cs_lu = *ls;
856 ed->ed_site = &ed->ed_site_myself;
857 ed->ed_cl.cd_lu_dev.ld_site = &ed->ed_site_myself.cs_lu;
858 rc = echo_fid_init(ed, obd->obd_name, lu_site2seq(ls));
860 CERROR("echo fid init error %d\n", rc);
863 #else /* !HAVE_SERVER_SUPPORT */
864 CERROR("Local operations are NOT supported on client side. "
865 "Only remote operations are supported. Metadata client "
866 "must be run on server side.\n");
867 GOTO(out, rc = -EOPNOTSUPP);
870 /* if echo client is to be stacked upon ost device, the next is
871 * NULL since ost is not a clio device so far */
872 if (next != NULL && !lu_device_is_cl(next))
875 tgt_type_name = tgt->obd_type->typ_name;
877 LASSERT(next != NULL);
878 if (next->ld_site != NULL)
879 GOTO(out, rc = -EBUSY);
881 next->ld_site = &ed->ed_site->cs_lu;
882 rc = next->ld_type->ldt_ops->ldto_device_init(env, next,
883 next->ld_type->ldt_name,
888 /* Tricky case, I have to determine the obd type since
889 * CLIO uses the different parameters to initialize
890 * objects for lov & osc. */
891 if (strcmp(tgt_type_name, LUSTRE_LOV_NAME) == 0)
892 ed->ed_next_islov = 1;
894 LASSERT(strcmp(tgt_type_name,
895 LUSTRE_OSC_NAME) == 0);
897 LASSERT(strcmp(tgt_type_name, LUSTRE_OST_NAME) == 0);
901 RETURN(&cd->cd_lu_dev);
906 rc2 = echo_client_cleanup(obd);
908 CERROR("Cleanup obd device %s error(%d)\n",
913 echo_site_fini(env, ed);
915 cl_device_fini(&ed->ed_cl);
925 static int echo_device_init(const struct lu_env *env, struct lu_device *d,
926 const char *name, struct lu_device *next)
932 static struct lu_device *echo_device_fini(const struct lu_env *env,
935 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
936 struct lu_device *next = ed->ed_next;
938 while (next && !ed->ed_next_ismd)
939 next = next->ld_type->ldt_ops->ldto_device_fini(env, next);
943 static void echo_lock_release(const struct lu_env *env,
944 struct echo_lock *ecl,
947 struct cl_lock *clk = echo_lock2cl(ecl);
951 cl_lock_release(env, clk, "ec enqueue", ecl->el_object);
953 cl_lock_mutex_get(env, clk);
954 cl_lock_cancel(env, clk);
955 cl_lock_delete(env, clk);
956 cl_lock_mutex_put(env, clk);
958 cl_lock_put(env, clk);
961 static struct lu_device *echo_device_free(const struct lu_env *env,
964 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
965 struct echo_client_obd *ec = ed->ed_ec;
966 struct echo_object *eco;
967 struct lu_device *next = ed->ed_next;
969 CDEBUG(D_INFO, "echo device:%p is going to be freed, next = %p\n",
972 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
974 /* check if there are objects still alive.
975 * It shouldn't have any object because lu_site_purge would cleanup
976 * all of cached objects. Anyway, probably the echo device is being
977 * parallelly accessed.
979 spin_lock(&ec->ec_lock);
980 list_for_each_entry(eco, &ec->ec_objects, eo_obj_chain)
982 spin_unlock(&ec->ec_lock);
985 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
988 "Waiting for the reference of echo object to be dropped\n");
990 /* Wait for the last reference to be dropped. */
991 spin_lock(&ec->ec_lock);
992 while (!list_empty(&ec->ec_objects)) {
993 spin_unlock(&ec->ec_lock);
994 CERROR("echo_client still has objects at cleanup time, "
995 "wait for 1 second\n");
996 schedule_timeout_and_set_state(TASK_UNINTERRUPTIBLE,
997 cfs_time_seconds(1));
998 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
999 spin_lock(&ec->ec_lock);
1001 spin_unlock(&ec->ec_lock);
1003 LASSERT(list_empty(&ec->ec_locks));
1005 CDEBUG(D_INFO, "No object exists, exiting...\n");
1007 echo_client_cleanup(d->ld_obd);
1008 #ifdef HAVE_SERVER_SUPPORT
1009 echo_fid_fini(d->ld_obd);
1011 while (next && !ed->ed_next_ismd)
1012 next = next->ld_type->ldt_ops->ldto_device_free(env, next);
1014 LASSERT(ed->ed_site == lu2cl_site(d->ld_site));
1015 echo_site_fini(env, ed);
1016 cl_device_fini(&ed->ed_cl);
1022 static const struct lu_device_type_operations echo_device_type_ops = {
1023 .ldto_init = echo_type_init,
1024 .ldto_fini = echo_type_fini,
1026 .ldto_start = echo_type_start,
1027 .ldto_stop = echo_type_stop,
1029 .ldto_device_alloc = echo_device_alloc,
1030 .ldto_device_free = echo_device_free,
1031 .ldto_device_init = echo_device_init,
1032 .ldto_device_fini = echo_device_fini
1035 static struct lu_device_type echo_device_type = {
1036 .ldt_tags = LU_DEVICE_CL,
1037 .ldt_name = LUSTRE_ECHO_CLIENT_NAME,
1038 .ldt_ops = &echo_device_type_ops,
1039 .ldt_ctx_tags = LCT_CL_THREAD | LCT_MD_THREAD | LCT_DT_THREAD,
1043 /** \defgroup echo_exports Exported operations
1045 * exporting functions to echo client
1050 /* Interfaces to echo client obd device */
1051 static struct echo_object *cl_echo_object_find(struct echo_device *d,
1052 struct lov_stripe_md **lsmp)
1055 struct echo_thread_info *info;
1056 struct echo_object_conf *conf;
1057 struct lov_stripe_md *lsm;
1058 struct echo_object *eco;
1059 struct cl_object *obj;
1068 LASSERTF(ostid_id(&lsm->lsm_oi) != 0, DOSTID"\n", POSTID(&lsm->lsm_oi));
1069 LASSERTF(ostid_seq(&lsm->lsm_oi) == FID_SEQ_ECHO, DOSTID"\n",
1070 POSTID(&lsm->lsm_oi));
1072 /* Never return an object if the obd is to be freed. */
1073 if (echo_dev2cl(d)->cd_lu_dev.ld_obd->obd_stopping)
1074 RETURN(ERR_PTR(-ENODEV));
1076 env = cl_env_get(&refcheck);
1078 RETURN((void *)env);
1080 info = echo_env_info(env);
1081 conf = &info->eti_conf;
1083 if (!d->ed_next_islov) {
1084 struct lov_oinfo *oinfo = lsm->lsm_oinfo[0];
1085 LASSERT(oinfo != NULL);
1086 oinfo->loi_oi = lsm->lsm_oi;
1087 conf->eoc_cl.u.coc_oinfo = oinfo;
1089 struct lustre_md *md;
1091 memset(md, 0, sizeof *md);
1093 conf->eoc_cl.u.coc_md = md;
1096 conf->eoc_md = lsmp;
1098 fid = &info->eti_fid;
1099 rc = ostid_to_fid(fid, &lsm->lsm_oi, 0);
1101 GOTO(out, eco = ERR_PTR(rc));
1103 /* In the function below, .hs_keycmp resolves to
1104 * lu_obj_hop_keycmp() */
1105 /* coverity[overrun-buffer-val] */
1106 obj = cl_object_find(env, echo_dev2cl(d), fid, &conf->eoc_cl);
1108 GOTO(out, eco = (void*)obj);
1110 eco = cl2echo_obj(obj);
1111 if (eco->eo_deleted) {
1112 cl_object_put(env, obj);
1113 eco = ERR_PTR(-EAGAIN);
1117 cl_env_put(env, &refcheck);
1121 static int cl_echo_object_put(struct echo_object *eco)
1124 struct cl_object *obj = echo_obj2cl(eco);
1128 env = cl_env_get(&refcheck);
1130 RETURN(PTR_ERR(env));
1132 /* an external function to kill an object? */
1133 if (eco->eo_deleted) {
1134 struct lu_object_header *loh = obj->co_lu.lo_header;
1135 LASSERT(&eco->eo_hdr == luh2coh(loh));
1136 set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
1139 cl_object_put(env, obj);
1140 cl_env_put(env, &refcheck);
1144 static int cl_echo_enqueue0(struct lu_env *env, struct echo_object *eco,
1145 obd_off start, obd_off end, int mode,
1146 __u64 *cookie , __u32 enqflags)
1149 struct cl_lock *lck;
1150 struct cl_object *obj;
1151 struct cl_lock_descr *descr;
1152 struct echo_thread_info *info;
1156 info = echo_env_info(env);
1158 descr = &info->eti_descr;
1159 obj = echo_obj2cl(eco);
1161 descr->cld_obj = obj;
1162 descr->cld_start = cl_index(obj, start);
1163 descr->cld_end = cl_index(obj, end);
1164 descr->cld_mode = mode == LCK_PW ? CLM_WRITE : CLM_READ;
1165 descr->cld_enq_flags = enqflags;
1168 lck = cl_lock_request(env, io, descr, "ec enqueue", eco);
1170 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
1171 struct echo_lock *el;
1173 rc = cl_wait(env, lck);
1175 el = cl2echo_lock(cl_lock_at(lck, &echo_device_type));
1176 spin_lock(&ec->ec_lock);
1177 if (list_empty(&el->el_chain)) {
1178 list_add(&el->el_chain, &ec->ec_locks);
1179 el->el_cookie = ++ec->ec_unique;
1181 atomic_inc(&el->el_refcount);
1182 *cookie = el->el_cookie;
1183 spin_unlock(&ec->ec_lock);
1185 cl_lock_release(env, lck, "ec enqueue", current);
1191 static int cl_echo_cancel0(struct lu_env *env, struct echo_device *ed,
1194 struct echo_client_obd *ec = ed->ed_ec;
1195 struct echo_lock *ecl = NULL;
1196 struct list_head *el;
1197 int found = 0, still_used = 0;
1200 LASSERT(ec != NULL);
1201 spin_lock(&ec->ec_lock);
1202 list_for_each(el, &ec->ec_locks) {
1203 ecl = list_entry(el, struct echo_lock, el_chain);
1204 CDEBUG(D_INFO, "ecl: %p, cookie: "LPX64"\n", ecl, ecl->el_cookie);
1205 found = (ecl->el_cookie == cookie);
1207 if (atomic_dec_and_test(&ecl->el_refcount))
1208 list_del_init(&ecl->el_chain);
1214 spin_unlock(&ec->ec_lock);
1219 echo_lock_release(env, ecl, still_used);
1223 static void echo_commit_callback(const struct lu_env *env, struct cl_io *io,
1224 struct cl_page *page)
1226 struct echo_thread_info *info;
1227 struct cl_2queue *queue;
1229 info = echo_env_info(env);
1230 LASSERT(io == &info->eti_io);
1232 queue = &info->eti_queue;
1233 cl_page_list_add(&queue->c2_qout, page);
1236 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
1237 struct page **pages, int npages, int async)
1240 struct echo_thread_info *info;
1241 struct cl_object *obj = echo_obj2cl(eco);
1242 struct echo_device *ed = eco->eo_dev;
1243 struct cl_2queue *queue;
1245 struct cl_page *clp;
1246 struct lustre_handle lh = { 0 };
1247 int page_size = cl_page_size(obj);
1253 LASSERT((offset & ~CFS_PAGE_MASK) == 0);
1254 LASSERT(ed->ed_next != NULL);
1255 env = cl_env_get(&refcheck);
1257 RETURN(PTR_ERR(env));
1259 info = echo_env_info(env);
1261 queue = &info->eti_queue;
1263 cl_2queue_init(queue);
1265 io->ci_ignore_layout = 1;
1266 rc = cl_io_init(env, io, CIT_MISC, obj);
1272 rc = cl_echo_enqueue0(env, eco, offset,
1273 offset + npages * PAGE_CACHE_SIZE - 1,
1274 rw == READ ? LCK_PR : LCK_PW, &lh.cookie,
1277 GOTO(error_lock, rc);
1279 for (i = 0; i < npages; i++) {
1281 clp = cl_page_find(env, obj, cl_index(obj, offset),
1282 pages[i], CPT_TRANSIENT);
1287 LASSERT(clp->cp_type == CPT_TRANSIENT);
1289 rc = cl_page_own(env, io, clp);
1291 LASSERT(clp->cp_state == CPS_FREEING);
1292 cl_page_put(env, clp);
1296 cl_2queue_add(queue, clp);
1298 /* drop the reference count for cl_page_find, so that the page
1299 * will be freed in cl_2queue_fini. */
1300 cl_page_put(env, clp);
1301 cl_page_clip(env, clp, 0, page_size);
1303 offset += page_size;
1307 enum cl_req_type typ = rw == READ ? CRT_READ : CRT_WRITE;
1309 async = async && (typ == CRT_WRITE);
1311 rc = cl_io_commit_async(env, io, &queue->c2_qin,
1313 echo_commit_callback);
1315 rc = cl_io_submit_sync(env, io, typ, queue, 0);
1316 CDEBUG(D_INFO, "echo_client %s write returns %d\n",
1317 async ? "async" : "sync", rc);
1320 cl_echo_cancel0(env, ed, lh.cookie);
1323 cl_2queue_discard(env, io, queue);
1324 cl_2queue_disown(env, io, queue);
1325 cl_2queue_fini(env, queue);
1326 cl_io_fini(env, io);
1328 cl_env_put(env, &refcheck);
1331 /** @} echo_exports */
1334 static obd_id last_object_id;
1337 echo_copyin_lsm (struct echo_device *ed, struct lov_stripe_md *lsm,
1338 void *ulsm, int ulsm_nob)
1340 struct echo_client_obd *ec = ed->ed_ec;
1343 if (ulsm_nob < sizeof (*lsm))
1346 if (copy_from_user (lsm, ulsm, sizeof (*lsm)))
1349 if (lsm->lsm_stripe_count > ec->ec_nstripes ||
1350 lsm->lsm_magic != LOV_MAGIC ||
1351 (lsm->lsm_stripe_size & (~CFS_PAGE_MASK)) != 0 ||
1352 ((__u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count > ~0UL))
1356 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1357 if (copy_from_user(lsm->lsm_oinfo[i],
1358 ((struct lov_stripe_md *)ulsm)-> \
1360 sizeof(lsm->lsm_oinfo[0])))
1366 #ifdef HAVE_SERVER_SUPPORT
1367 static inline void echo_md_build_name(struct lu_name *lname, char *name,
1370 sprintf(name, LPU64, id);
1371 lname->ln_name = name;
1372 lname->ln_namelen = strlen(name);
1375 /* similar to mdt_attr_get_complex */
1376 static int echo_big_lmm_get(const struct lu_env *env, struct md_object *o,
1379 struct echo_thread_info *info = echo_env_info(env);
1384 LASSERT(ma->ma_lmm_size > 0);
1386 rc = mo_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LOV);
1390 /* big_lmm may need to be grown */
1391 if (info->eti_big_lmmsize < rc) {
1392 int size = size_roundup_power2(rc);
1394 if (info->eti_big_lmmsize > 0) {
1395 /* free old buffer */
1396 LASSERT(info->eti_big_lmm);
1397 OBD_FREE_LARGE(info->eti_big_lmm,
1398 info->eti_big_lmmsize);
1399 info->eti_big_lmm = NULL;
1400 info->eti_big_lmmsize = 0;
1403 OBD_ALLOC_LARGE(info->eti_big_lmm, size);
1404 if (info->eti_big_lmm == NULL)
1406 info->eti_big_lmmsize = size;
1408 LASSERT(info->eti_big_lmmsize >= rc);
1410 info->eti_buf.lb_buf = info->eti_big_lmm;
1411 info->eti_buf.lb_len = info->eti_big_lmmsize;
1412 rc = mo_xattr_get(env, o, &info->eti_buf, XATTR_NAME_LOV);
1416 ma->ma_valid |= MA_LOV;
1417 ma->ma_lmm = info->eti_big_lmm;
1418 ma->ma_lmm_size = rc;
1423 static int echo_attr_get_complex(const struct lu_env *env,
1424 struct md_object *next,
1427 struct echo_thread_info *info = echo_env_info(env);
1428 struct lu_buf *buf = &info->eti_buf;
1429 umode_t mode = lu_object_attr(&next->mo_lu);
1430 int need = ma->ma_need;
1437 if (need & MA_INODE) {
1438 ma->ma_need = MA_INODE;
1439 rc = mo_attr_get(env, next, ma);
1442 ma->ma_valid |= MA_INODE;
1445 if (need & MA_LOV) {
1446 if (S_ISREG(mode) || S_ISDIR(mode)) {
1447 LASSERT(ma->ma_lmm_size > 0);
1448 buf->lb_buf = ma->ma_lmm;
1449 buf->lb_len = ma->ma_lmm_size;
1450 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LOV);
1452 ma->ma_lmm_size = rc2;
1453 ma->ma_valid |= MA_LOV;
1454 } else if (rc2 == -ENODATA) {
1456 ma->ma_lmm_size = 0;
1457 } else if (rc2 == -ERANGE) {
1458 rc2 = echo_big_lmm_get(env, next, ma);
1460 GOTO(out, rc = rc2);
1462 GOTO(out, rc = rc2);
1467 #ifdef CONFIG_FS_POSIX_ACL
1468 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
1469 buf->lb_buf = ma->ma_acl;
1470 buf->lb_len = ma->ma_acl_size;
1471 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1473 ma->ma_acl_size = rc2;
1474 ma->ma_valid |= MA_ACL_DEF;
1475 } else if (rc2 == -ENODATA) {
1477 ma->ma_acl_size = 0;
1479 GOTO(out, rc = rc2);
1485 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
1486 rc, ma->ma_valid, ma->ma_lmm);
1491 echo_md_create_internal(const struct lu_env *env, struct echo_device *ed,
1492 struct md_object *parent, struct lu_fid *fid,
1493 struct lu_name *lname, struct md_op_spec *spec,
1496 struct lu_object *ec_child, *child;
1497 struct lu_device *ld = ed->ed_next;
1498 struct echo_thread_info *info = echo_env_info(env);
1499 struct lu_fid *fid2 = &info->eti_fid2;
1500 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
1505 rc = mdo_lookup(env, parent, lname, fid2, spec);
1508 else if (rc != -ENOENT)
1511 ec_child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev,
1513 if (IS_ERR(ec_child)) {
1514 CERROR("Can not find the child "DFID": rc = %ld\n", PFID(fid),
1516 RETURN(PTR_ERR(ec_child));
1519 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1520 if (child == NULL) {
1521 CERROR("Can not locate the child "DFID"\n", PFID(fid));
1522 GOTO(out_put, rc = -EINVAL);
1525 CDEBUG(D_RPCTRACE, "Start creating object "DFID" %s %p\n",
1526 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1529 * Do not perform lookup sanity check. We know that name does not exist.
1531 spec->sp_cr_lookup = 0;
1532 rc = mdo_create(env, parent, lname, lu2md(child), spec, ma);
1534 CERROR("Can not create child "DFID": rc = %d\n", PFID(fid), rc);
1537 CDEBUG(D_RPCTRACE, "End creating object "DFID" %s %p rc = %d\n",
1538 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent, rc);
1541 lu_object_put(env, ec_child);
1545 static int echo_set_lmm_size(const struct lu_env *env, struct lu_device *ld,
1548 struct echo_thread_info *info = echo_env_info(env);
1550 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
1551 ma->ma_lmm = (void *)&info->eti_lmm;
1552 ma->ma_lmm_size = sizeof(info->eti_lmm);
1554 LASSERT(info->eti_big_lmmsize);
1555 ma->ma_lmm = info->eti_big_lmm;
1556 ma->ma_lmm_size = info->eti_big_lmmsize;
1562 static int echo_create_md_object(const struct lu_env *env,
1563 struct echo_device *ed,
1564 struct lu_object *ec_parent,
1566 char *name, int namelen,
1567 __u64 id, __u32 mode, int count,
1568 int stripe_count, int stripe_offset)
1570 struct lu_object *parent;
1571 struct echo_thread_info *info = echo_env_info(env);
1572 struct lu_name *lname = &info->eti_lname;
1573 struct md_op_spec *spec = &info->eti_spec;
1574 struct md_attr *ma = &info->eti_ma;
1575 struct lu_device *ld = ed->ed_next;
1581 if (ec_parent == NULL)
1583 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1587 memset(ma, 0, sizeof(*ma));
1588 memset(spec, 0, sizeof(*spec));
1589 if (stripe_count != 0) {
1590 spec->sp_cr_flags |= FMODE_WRITE;
1591 echo_set_lmm_size(env, ld, ma);
1592 if (stripe_count != -1) {
1593 struct lov_user_md_v3 *lum = &info->eti_lum;
1595 lum->lmm_magic = LOV_USER_MAGIC_V3;
1596 lum->lmm_stripe_count = stripe_count;
1597 lum->lmm_stripe_offset = stripe_offset;
1598 lum->lmm_pattern = 0;
1599 spec->u.sp_ea.eadata = lum;
1600 spec->u.sp_ea.eadatalen = sizeof(*lum);
1601 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1605 ma->ma_attr.la_mode = mode;
1606 ma->ma_attr.la_valid = LA_CTIME | LA_MODE;
1607 ma->ma_attr.la_ctime = cfs_time_current_64();
1610 lname->ln_name = name;
1611 lname->ln_namelen = namelen;
1612 /* If name is specified, only create one object by name */
1613 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1618 /* Create multiple object sequenced by id */
1619 for (i = 0; i < count; i++) {
1620 char *tmp_name = info->eti_name;
1622 echo_md_build_name(lname, tmp_name, id);
1624 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1627 CERROR("Can not create child %s: rc = %d\n", tmp_name,
1638 static struct lu_object *echo_md_lookup(const struct lu_env *env,
1639 struct echo_device *ed,
1640 struct md_object *parent,
1641 struct lu_name *lname)
1643 struct echo_thread_info *info = echo_env_info(env);
1644 struct lu_fid *fid = &info->eti_fid;
1645 struct lu_object *child;
1649 CDEBUG(D_INFO, "lookup %s in parent "DFID" %p\n", lname->ln_name,
1651 rc = mdo_lookup(env, parent, lname, fid, NULL);
1653 CERROR("lookup %s: rc = %d\n", lname->ln_name, rc);
1654 RETURN(ERR_PTR(rc));
1657 /* In the function below, .hs_keycmp resolves to
1658 * lu_obj_hop_keycmp() */
1659 /* coverity[overrun-buffer-val] */
1660 child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1665 static int echo_setattr_object(const struct lu_env *env,
1666 struct echo_device *ed,
1667 struct lu_object *ec_parent,
1668 __u64 id, int count)
1670 struct lu_object *parent;
1671 struct echo_thread_info *info = echo_env_info(env);
1672 struct lu_name *lname = &info->eti_lname;
1673 char *name = info->eti_name;
1674 struct lu_device *ld = ed->ed_next;
1675 struct lu_buf *buf = &info->eti_buf;
1681 if (ec_parent == NULL)
1683 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1687 for (i = 0; i < count; i++) {
1688 struct lu_object *ec_child, *child;
1690 echo_md_build_name(lname, name, id);
1692 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1693 if (IS_ERR(ec_child)) {
1694 CERROR("Can't find child %s: rc = %ld\n",
1695 lname->ln_name, PTR_ERR(ec_child));
1696 RETURN(PTR_ERR(ec_child));
1699 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1700 if (child == NULL) {
1701 CERROR("Can not locate the child %s\n", lname->ln_name);
1702 lu_object_put(env, ec_child);
1707 CDEBUG(D_RPCTRACE, "Start setattr object "DFID"\n",
1708 PFID(lu_object_fid(child)));
1710 buf->lb_buf = info->eti_xattr_buf;
1711 buf->lb_len = sizeof(info->eti_xattr_buf);
1713 sprintf(name, "%s.test1", XATTR_USER_PREFIX);
1714 rc = mo_xattr_set(env, lu2md(child), buf, name,
1717 CERROR("Can not setattr child "DFID": rc = %d\n",
1718 PFID(lu_object_fid(child)), rc);
1719 lu_object_put(env, ec_child);
1722 CDEBUG(D_RPCTRACE, "End setattr object "DFID"\n",
1723 PFID(lu_object_fid(child)));
1725 lu_object_put(env, ec_child);
1730 static int echo_getattr_object(const struct lu_env *env,
1731 struct echo_device *ed,
1732 struct lu_object *ec_parent,
1733 __u64 id, int count)
1735 struct lu_object *parent;
1736 struct echo_thread_info *info = echo_env_info(env);
1737 struct lu_name *lname = &info->eti_lname;
1738 char *name = info->eti_name;
1739 struct md_attr *ma = &info->eti_ma;
1740 struct lu_device *ld = ed->ed_next;
1746 if (ec_parent == NULL)
1748 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1752 memset(ma, 0, sizeof(*ma));
1753 ma->ma_need |= MA_INODE | MA_LOV | MA_PFID | MA_HSM | MA_ACL_DEF;
1754 ma->ma_acl = info->eti_xattr_buf;
1755 ma->ma_acl_size = sizeof(info->eti_xattr_buf);
1757 for (i = 0; i < count; i++) {
1758 struct lu_object *ec_child, *child;
1761 echo_md_build_name(lname, name, id);
1762 echo_set_lmm_size(env, ld, ma);
1764 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1765 if (IS_ERR(ec_child)) {
1766 CERROR("Can't find child %s: rc = %ld\n",
1767 lname->ln_name, PTR_ERR(ec_child));
1768 RETURN(PTR_ERR(ec_child));
1771 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1772 if (child == NULL) {
1773 CERROR("Can not locate the child %s\n", lname->ln_name);
1774 lu_object_put(env, ec_child);
1778 CDEBUG(D_RPCTRACE, "Start getattr object "DFID"\n",
1779 PFID(lu_object_fid(child)));
1780 rc = echo_attr_get_complex(env, lu2md(child), ma);
1782 CERROR("Can not getattr child "DFID": rc = %d\n",
1783 PFID(lu_object_fid(child)), rc);
1784 lu_object_put(env, ec_child);
1787 CDEBUG(D_RPCTRACE, "End getattr object "DFID"\n",
1788 PFID(lu_object_fid(child)));
1790 lu_object_put(env, ec_child);
1796 static int echo_lookup_object(const struct lu_env *env,
1797 struct echo_device *ed,
1798 struct lu_object *ec_parent,
1799 __u64 id, int count)
1801 struct lu_object *parent;
1802 struct echo_thread_info *info = echo_env_info(env);
1803 struct lu_name *lname = &info->eti_lname;
1804 char *name = info->eti_name;
1805 struct lu_fid *fid = &info->eti_fid;
1806 struct lu_device *ld = ed->ed_next;
1810 if (ec_parent == NULL)
1812 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1816 /*prepare the requests*/
1817 for (i = 0; i < count; i++) {
1818 echo_md_build_name(lname, name, id);
1820 CDEBUG(D_RPCTRACE, "Start lookup object "DFID" %s %p\n",
1821 PFID(lu_object_fid(parent)), lname->ln_name, parent);
1823 rc = mdo_lookup(env, lu2md(parent), lname, fid, NULL);
1825 CERROR("Can not lookup child %s: rc = %d\n", name, rc);
1828 CDEBUG(D_RPCTRACE, "End lookup object "DFID" %s %p\n",
1829 PFID(lu_object_fid(parent)), lname->ln_name, parent);
1836 static int echo_md_destroy_internal(const struct lu_env *env,
1837 struct echo_device *ed,
1838 struct md_object *parent,
1839 struct lu_name *lname,
1842 struct lu_device *ld = ed->ed_next;
1843 struct lu_object *ec_child;
1844 struct lu_object *child;
1849 ec_child = echo_md_lookup(env, ed, parent, lname);
1850 if (IS_ERR(ec_child)) {
1851 CERROR("Can't find child %s: rc = %ld\n", lname->ln_name,
1853 RETURN(PTR_ERR(ec_child));
1856 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1857 if (child == NULL) {
1858 CERROR("Can not locate the child %s\n", lname->ln_name);
1859 GOTO(out_put, rc = -EINVAL);
1862 if (lu_object_remote(child)) {
1863 CERROR("Can not destroy remote object %s: rc = %d\n",
1864 lname->ln_name, -EPERM);
1865 GOTO(out_put, rc = -EPERM);
1867 CDEBUG(D_RPCTRACE, "Start destroy object "DFID" %s %p\n",
1868 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1870 rc = mdo_unlink(env, parent, lu2md(child), lname, ma, 0);
1872 CERROR("Can not unlink child %s: rc = %d\n",
1873 lname->ln_name, rc);
1876 CDEBUG(D_RPCTRACE, "End destroy object "DFID" %s %p\n",
1877 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1879 lu_object_put(env, ec_child);
1883 static int echo_destroy_object(const struct lu_env *env,
1884 struct echo_device *ed,
1885 struct lu_object *ec_parent,
1886 char *name, int namelen,
1887 __u64 id, __u32 mode,
1890 struct echo_thread_info *info = echo_env_info(env);
1891 struct lu_name *lname = &info->eti_lname;
1892 struct md_attr *ma = &info->eti_ma;
1893 struct lu_device *ld = ed->ed_next;
1894 struct lu_object *parent;
1899 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1903 memset(ma, 0, sizeof(*ma));
1904 ma->ma_attr.la_mode = mode;
1905 ma->ma_attr.la_valid = LA_CTIME;
1906 ma->ma_attr.la_ctime = cfs_time_current_64();
1907 ma->ma_need = MA_INODE;
1911 lname->ln_name = name;
1912 lname->ln_namelen = namelen;
1913 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
1918 /*prepare the requests*/
1919 for (i = 0; i < count; i++) {
1920 char *tmp_name = info->eti_name;
1923 echo_md_build_name(lname, tmp_name, id);
1925 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
1928 CERROR("Can not unlink child %s: rc = %d\n", name, rc);
1937 static struct lu_object *echo_resolve_path(const struct lu_env *env,
1938 struct echo_device *ed, char *path,
1941 struct lu_device *ld = ed->ed_next;
1942 struct md_device *md = lu2md_dev(ld);
1943 struct echo_thread_info *info = echo_env_info(env);
1944 struct lu_fid *fid = &info->eti_fid;
1945 struct lu_name *lname = &info->eti_lname;
1946 struct lu_object *parent = NULL;
1947 struct lu_object *child = NULL;
1951 /*Only support MDD layer right now*/
1952 rc = md->md_ops->mdo_root_get(env, md, fid);
1954 CERROR("get root error: rc = %d\n", rc);
1955 RETURN(ERR_PTR(rc));
1958 /* In the function below, .hs_keycmp resolves to
1959 * lu_obj_hop_keycmp() */
1960 /* coverity[overrun-buffer-val] */
1961 parent = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1962 if (IS_ERR(parent)) {
1963 CERROR("Can not find the parent "DFID": rc = %ld\n",
1964 PFID(fid), PTR_ERR(parent));
1969 struct lu_object *ld_parent;
1972 e = strsep(&path, "/");
1977 if (!path || path[0] == '\0')
1983 lname->ln_namelen = strlen(e);
1985 ld_parent = lu_object_locate(parent->lo_header, ld->ld_type);
1986 if (ld_parent == NULL) {
1987 lu_object_put(env, parent);
1992 child = echo_md_lookup(env, ed, lu2md(ld_parent), lname);
1993 lu_object_put(env, parent);
1994 if (IS_ERR(child)) {
1995 rc = (int)PTR_ERR(child);
1996 CERROR("lookup %s under parent "DFID": rc = %d\n",
1997 lname->ln_name, PFID(lu_object_fid(ld_parent)),
2004 RETURN(ERR_PTR(rc));
2009 static void echo_ucred_init(struct lu_env *env)
2011 struct lu_ucred *ucred = lu_ucred(env);
2013 ucred->uc_valid = UCRED_INVALID;
2015 ucred->uc_suppgids[0] = -1;
2016 ucred->uc_suppgids[1] = -1;
2018 ucred->uc_uid = ucred->uc_o_uid =
2019 from_kuid(&init_user_ns, current_uid());
2020 ucred->uc_gid = ucred->uc_o_gid =
2021 from_kgid(&init_user_ns, current_gid());
2022 ucred->uc_fsuid = ucred->uc_o_fsuid =
2023 from_kuid(&init_user_ns, current_fsuid());
2024 ucred->uc_fsgid = ucred->uc_o_fsgid =
2025 from_kgid(&init_user_ns, current_fsgid());
2026 ucred->uc_cap = cfs_curproc_cap_pack();
2028 /* remove fs privilege for non-root user. */
2029 if (ucred->uc_fsuid)
2030 ucred->uc_cap &= ~CFS_CAP_FS_MASK;
2031 ucred->uc_valid = UCRED_NEW;
2034 static void echo_ucred_fini(struct lu_env *env)
2036 struct lu_ucred *ucred = lu_ucred(env);
2037 ucred->uc_valid = UCRED_INIT;
2040 #define ECHO_MD_CTX_TAG (LCT_REMEMBER | LCT_MD_THREAD)
2041 #define ECHO_MD_SES_TAG (LCT_REMEMBER | LCT_SESSION | LCT_SERVER_SESSION)
2042 static int echo_md_handler(struct echo_device *ed, int command,
2043 char *path, int path_len, __u64 id, int count,
2044 struct obd_ioctl_data *data)
2046 struct echo_thread_info *info;
2047 struct lu_device *ld = ed->ed_next;
2050 struct lu_object *parent;
2052 int namelen = data->ioc_plen2;
2057 CERROR("MD echo client is not being initialized properly\n");
2061 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
2062 CERROR("Only support MDD layer right now!\n");
2066 env = cl_env_get(&refcheck);
2068 RETURN(PTR_ERR(env));
2070 rc = lu_env_refill_by_tags(env, ECHO_MD_CTX_TAG, ECHO_MD_SES_TAG);
2074 /* init big_lmm buffer */
2075 info = echo_env_info(env);
2076 LASSERT(info->eti_big_lmm == NULL);
2077 OBD_ALLOC_LARGE(info->eti_big_lmm, MIN_MD_SIZE);
2078 if (info->eti_big_lmm == NULL)
2079 GOTO(out_env, rc = -ENOMEM);
2080 info->eti_big_lmmsize = MIN_MD_SIZE;
2082 parent = echo_resolve_path(env, ed, path, path_len);
2083 if (IS_ERR(parent)) {
2084 CERROR("Can not resolve the path %s: rc = %ld\n", path,
2086 GOTO(out_free, rc = PTR_ERR(parent));
2090 OBD_ALLOC(name, namelen + 1);
2092 GOTO(out_put, rc = -ENOMEM);
2093 if (copy_from_user(name, data->ioc_pbuf2, namelen))
2094 GOTO(out_name, rc = -EFAULT);
2097 echo_ucred_init(env);
2100 case ECHO_MD_CREATE:
2101 case ECHO_MD_MKDIR: {
2102 struct echo_thread_info *info = echo_env_info(env);
2103 __u32 mode = data->ioc_obdo2.o_mode;
2104 struct lu_fid *fid = &info->eti_fid;
2105 int stripe_count = (int)data->ioc_obdo2.o_misc;
2106 int stripe_index = (int)data->ioc_obdo2.o_stripe_idx;
2108 rc = ostid_to_fid(fid, &data->ioc_obdo1.o_oi, 0);
2112 /* In the function below, .hs_keycmp resolves to
2113 * lu_obj_hop_keycmp() */
2114 /* coverity[overrun-buffer-val] */
2115 rc = echo_create_md_object(env, ed, parent, fid, name, namelen,
2116 id, mode, count, stripe_count,
2120 case ECHO_MD_DESTROY:
2121 case ECHO_MD_RMDIR: {
2122 __u32 mode = data->ioc_obdo2.o_mode;
2124 rc = echo_destroy_object(env, ed, parent, name, namelen,
2128 case ECHO_MD_LOOKUP:
2129 rc = echo_lookup_object(env, ed, parent, id, count);
2131 case ECHO_MD_GETATTR:
2132 rc = echo_getattr_object(env, ed, parent, id, count);
2134 case ECHO_MD_SETATTR:
2135 rc = echo_setattr_object(env, ed, parent, id, count);
2138 CERROR("unknown command %d\n", command);
2142 echo_ucred_fini(env);
2146 OBD_FREE(name, namelen + 1);
2148 lu_object_put(env, parent);
2150 LASSERT(info->eti_big_lmm);
2151 OBD_FREE_LARGE(info->eti_big_lmm, info->eti_big_lmmsize);
2152 info->eti_big_lmm = NULL;
2153 info->eti_big_lmmsize = 0;
2155 cl_env_put(env, &refcheck);
2158 #endif /* HAVE_SERVER_SUPPORT */
2160 static int echo_create_object(const struct lu_env *env, struct echo_device *ed,
2161 int on_target, struct obdo *oa, void *ulsm,
2162 int ulsm_nob, struct obd_trans_info *oti)
2164 struct echo_object *eco;
2165 struct echo_client_obd *ec = ed->ed_ec;
2166 struct lov_stripe_md *lsm = NULL;
2171 if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
2172 (on_target || /* set_stripe */
2173 ec->ec_nstripes != 0)) { /* LOV */
2174 CERROR ("No valid oid\n");
2178 rc = echo_alloc_memmd(ed, &lsm);
2180 CERROR("Cannot allocate md: rc = %d\n", rc);
2187 rc = echo_copyin_lsm (ed, lsm, ulsm, ulsm_nob);
2191 if (lsm->lsm_stripe_count == 0)
2192 lsm->lsm_stripe_count = ec->ec_nstripes;
2194 if (lsm->lsm_stripe_size == 0)
2195 lsm->lsm_stripe_size = PAGE_CACHE_SIZE;
2199 /* setup stripes: indices + default ids if required */
2200 for (i = 0; i < lsm->lsm_stripe_count; i++) {
2201 if (ostid_id(&lsm->lsm_oinfo[i]->loi_oi) == 0)
2202 lsm->lsm_oinfo[i]->loi_oi = lsm->lsm_oi;
2204 lsm->lsm_oinfo[i]->loi_ost_idx =
2205 (idx + i) % ec->ec_nstripes;
2209 /* setup object ID here for !on_target and LOV hint */
2210 if (oa->o_valid & OBD_MD_FLID) {
2211 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2212 lsm->lsm_oi = oa->o_oi;
2215 if (ostid_id(&lsm->lsm_oi) == 0)
2216 ostid_set_id(&lsm->lsm_oi, ++last_object_id);
2220 /* Only echo objects are allowed to be created */
2221 LASSERT((oa->o_valid & OBD_MD_FLGROUP) &&
2222 (ostid_seq(&oa->o_oi) == FID_SEQ_ECHO));
2223 rc = obd_create(env, ec->ec_exp, oa, &lsm, oti);
2225 CERROR("Cannot create objects: rc = %d\n", rc);
2231 /* See what object ID we were given */
2232 oa->o_oi = lsm->lsm_oi;
2233 oa->o_valid |= OBD_MD_FLID;
2235 eco = cl_echo_object_find(ed, &lsm);
2237 GOTO(failed, rc = PTR_ERR(eco));
2238 cl_echo_object_put(eco);
2240 CDEBUG(D_INFO, "oa oid "DOSTID"\n", POSTID(&oa->o_oi));
2245 obd_destroy(env, ec->ec_exp, oa, lsm, oti, NULL, NULL);
2247 echo_free_memmd(ed, &lsm);
2249 CERROR("create object failed with: rc = %d\n", rc);
2253 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
2256 struct lov_stripe_md *lsm = NULL;
2257 struct echo_object *eco;
2261 if ((oa->o_valid & OBD_MD_FLID) == 0 || ostid_id(&oa->o_oi) == 0) {
2262 /* disallow use of object id 0 */
2263 CERROR ("No valid oid\n");
2267 rc = echo_alloc_memmd(ed, &lsm);
2271 lsm->lsm_oi = oa->o_oi;
2272 if (!(oa->o_valid & OBD_MD_FLGROUP))
2273 ostid_set_seq_echo(&lsm->lsm_oi);
2276 eco = cl_echo_object_find(ed, &lsm);
2282 echo_free_memmd(ed, &lsm);
2286 static void echo_put_object(struct echo_object *eco)
2290 rc = cl_echo_object_put(eco);
2292 CERROR("%s: echo client drop an object failed: rc = %d\n",
2293 eco->eo_dev->ed_ec->ec_exp->exp_obd->obd_name, rc);
2297 echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
2299 unsigned long stripe_count;
2300 unsigned long stripe_size;
2301 unsigned long width;
2302 unsigned long woffset;
2306 if (lsm->lsm_stripe_count <= 1)
2310 stripe_size = lsm->lsm_stripe_size;
2311 stripe_count = lsm->lsm_stripe_count;
2313 /* width = # bytes in all stripes */
2314 width = stripe_size * stripe_count;
2316 /* woffset = offset within a width; offset = whole number of widths */
2317 woffset = do_div (offset, width);
2319 stripe_index = woffset / stripe_size;
2321 *idp = ostid_id(&lsm->lsm_oinfo[stripe_index]->loi_oi);
2322 *offp = offset * stripe_size + woffset % stripe_size;
2326 echo_client_page_debug_setup(struct lov_stripe_md *lsm,
2327 struct page *page, int rw, obd_id id,
2328 obd_off offset, obd_off count)
2335 /* no partial pages on the client */
2336 LASSERT(count == PAGE_CACHE_SIZE);
2340 for (delta = 0; delta < PAGE_CACHE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2341 if (rw == OBD_BRW_WRITE) {
2342 stripe_off = offset + delta;
2344 echo_get_stripe_off_id(lsm, &stripe_off, &stripe_id);
2346 stripe_off = 0xdeadbeef00c0ffeeULL;
2347 stripe_id = 0xdeadbeef00c0ffeeULL;
2349 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
2350 stripe_off, stripe_id);
2356 static int echo_client_page_debug_check(struct lov_stripe_md *lsm,
2357 struct page *page, obd_id id,
2358 obd_off offset, obd_off count)
2367 /* no partial pages on the client */
2368 LASSERT(count == PAGE_CACHE_SIZE);
2372 for (rc = delta = 0; delta < PAGE_CACHE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2373 stripe_off = offset + delta;
2375 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
2377 rc2 = block_debug_check("test_brw",
2378 addr + delta, OBD_ECHO_BLOCK_SIZE,
2379 stripe_off, stripe_id);
2381 CERROR ("Error in echo object "LPX64"\n", id);
2390 static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
2391 struct echo_object *eco, obd_off offset,
2392 obd_size count, int async,
2393 struct obd_trans_info *oti)
2395 struct lov_stripe_md *lsm = eco->eo_lsm;
2397 struct brw_page *pga;
2398 struct brw_page *pgp;
2399 struct page **pages;
2408 verify = (ostid_id(&oa->o_oi) != ECHO_PERSISTENT_OBJID &&
2409 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
2410 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
2412 gfp_mask = ((ostid_id(&oa->o_oi) & 2) == 0) ? GFP_IOFS : GFP_HIGHUSER;
2414 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
2415 LASSERT(lsm != NULL);
2416 LASSERT(ostid_id(&lsm->lsm_oi) == ostid_id(&oa->o_oi));
2419 (count & (~CFS_PAGE_MASK)) != 0)
2422 /* XXX think again with misaligned I/O */
2423 npages = count >> PAGE_CACHE_SHIFT;
2425 if (rw == OBD_BRW_WRITE)
2426 brw_flags = OBD_BRW_ASYNC;
2428 OBD_ALLOC(pga, npages * sizeof(*pga));
2432 OBD_ALLOC(pages, npages * sizeof(*pages));
2433 if (pages == NULL) {
2434 OBD_FREE(pga, npages * sizeof(*pga));
2438 for (i = 0, pgp = pga, off = offset;
2440 i++, pgp++, off += PAGE_CACHE_SIZE) {
2442 LASSERT (pgp->pg == NULL); /* for cleanup */
2445 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
2446 if (pgp->pg == NULL)
2450 pgp->count = PAGE_CACHE_SIZE;
2452 pgp->flag = brw_flags;
2455 echo_client_page_debug_setup(lsm, pgp->pg, rw,
2456 ostid_id(&oa->o_oi), off,
2460 /* brw mode can only be used at client */
2461 LASSERT(ed->ed_next != NULL);
2462 rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
2465 if (rc != 0 || rw != OBD_BRW_READ)
2468 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
2469 if (pgp->pg == NULL)
2474 vrc = echo_client_page_debug_check(lsm, pgp->pg,
2475 ostid_id(&oa->o_oi),
2476 pgp->off, pgp->count);
2477 if (vrc != 0 && rc == 0)
2480 OBD_PAGE_FREE(pgp->pg);
2482 OBD_FREE(pga, npages * sizeof(*pga));
2483 OBD_FREE(pages, npages * sizeof(*pages));
2487 static int echo_client_prep_commit(const struct lu_env *env,
2488 struct obd_export *exp, int rw,
2489 struct obdo *oa, struct echo_object *eco,
2490 obd_off offset, obd_size count,
2491 obd_size batch, struct obd_trans_info *oti,
2494 struct lov_stripe_md *lsm = eco->eo_lsm;
2495 struct obd_ioobj ioo;
2496 struct niobuf_local *lnb;
2497 struct niobuf_remote *rnb;
2499 obd_size npages, tot_pages;
2500 int i, ret = 0, brw_flags = 0;
2504 if (count <= 0 || (count & (~CFS_PAGE_MASK)) != 0 ||
2505 (lsm != NULL && ostid_id(&lsm->lsm_oi) != ostid_id(&oa->o_oi)))
2508 npages = batch >> PAGE_CACHE_SHIFT;
2509 tot_pages = count >> PAGE_CACHE_SHIFT;
2511 OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
2512 OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
2514 if (lnb == NULL || rnb == NULL)
2515 GOTO(out, ret = -ENOMEM);
2517 if (rw == OBD_BRW_WRITE && async)
2518 brw_flags |= OBD_BRW_ASYNC;
2520 obdo_to_ioobj(oa, &ioo);
2524 for(; tot_pages; tot_pages -= npages) {
2527 if (tot_pages < npages)
2530 for (i = 0; i < npages; i++, off += PAGE_CACHE_SIZE) {
2531 rnb[i].rnb_offset = off;
2532 rnb[i].rnb_len = PAGE_CACHE_SIZE;
2533 rnb[i].rnb_flags = brw_flags;
2536 ioo.ioo_bufcnt = npages;
2539 ret = obd_preprw(env, rw, exp, oa, 1, &ioo, rnb, &lpages,
2543 LASSERT(lpages == npages);
2545 for (i = 0; i < lpages; i++) {
2546 struct page *page = lnb[i].lnb_page;
2548 /* read past eof? */
2549 if (page == NULL && lnb[i].lnb_rc == 0)
2553 lnb[i].lnb_flags |= OBD_BRW_ASYNC;
2555 if (ostid_id(&oa->o_oi) == ECHO_PERSISTENT_OBJID ||
2556 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
2557 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
2560 if (rw == OBD_BRW_WRITE)
2561 echo_client_page_debug_setup(lsm, page, rw,
2562 ostid_id(&oa->o_oi),
2566 echo_client_page_debug_check(lsm, page,
2567 ostid_id(&oa->o_oi),
2572 ret = obd_commitrw(env, rw, exp, oa, 1, &ioo,
2573 rnb, npages, lnb, oti, ret);
2577 /* Reset oti otherwise it would confuse ldiskfs. */
2578 memset(oti, 0, sizeof(*oti));
2580 /* Reuse env context. */
2581 lu_context_exit((struct lu_context *)&env->le_ctx);
2582 lu_context_enter((struct lu_context *)&env->le_ctx);
2587 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
2589 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
2593 static int echo_client_brw_ioctl(const struct lu_env *env, int rw,
2594 struct obd_export *exp,
2595 struct obd_ioctl_data *data,
2596 struct obd_trans_info *dummy_oti)
2598 struct obd_device *obd = class_exp2obd(exp);
2599 struct echo_device *ed = obd2echo_dev(obd);
2600 struct echo_client_obd *ec = ed->ed_ec;
2601 struct obdo *oa = &data->ioc_obdo1;
2602 struct echo_object *eco;
2608 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2610 rc = echo_get_object(&eco, ed, oa);
2614 oa->o_valid &= ~OBD_MD_FLHANDLE;
2616 /* OFD/obdfilter works only via prep/commit */
2617 test_mode = (long)data->ioc_pbuf1;
2618 if (ed->ed_next == NULL && test_mode != 3) {
2620 data->ioc_plen1 = data->ioc_count;
2626 /* Truncate batch size to maximum */
2627 if (data->ioc_plen1 > PTLRPC_MAX_BRW_SIZE)
2628 data->ioc_plen1 = PTLRPC_MAX_BRW_SIZE;
2630 switch (test_mode) {
2634 rc = echo_client_kbrw(ed, rw, oa,
2635 eco, data->ioc_offset,
2636 data->ioc_count, async, dummy_oti);
2639 rc = echo_client_prep_commit(env, ec->ec_exp, rw, oa,
2640 eco, data->ioc_offset,
2641 data->ioc_count, data->ioc_plen1,
2647 echo_put_object(eco);
2652 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2653 void *karg, void *uarg)
2655 #ifdef HAVE_SERVER_SUPPORT
2656 struct tgt_session_info *tsi;
2658 struct obd_device *obd = exp->exp_obd;
2659 struct echo_device *ed = obd2echo_dev(obd);
2660 struct echo_client_obd *ec = ed->ed_ec;
2661 struct echo_object *eco;
2662 struct obd_ioctl_data *data = karg;
2663 struct obd_trans_info dummy_oti;
2665 struct oti_req_ack_lock *ack_lock;
2668 int rw = OBD_BRW_READ;
2671 #ifdef HAVE_SERVER_SUPPORT
2672 struct lu_context echo_session;
2676 memset(&dummy_oti, 0, sizeof(dummy_oti));
2678 oa = &data->ioc_obdo1;
2679 if (!(oa->o_valid & OBD_MD_FLGROUP)) {
2680 oa->o_valid |= OBD_MD_FLGROUP;
2681 ostid_set_seq_echo(&oa->o_oi);
2684 /* This FID is unpacked just for validation at this point */
2685 rc = ostid_to_fid(&fid, &oa->o_oi, 0);
2693 rc = lu_env_init(env, LCT_DT_THREAD);
2695 GOTO(out_alloc, rc = -ENOMEM);
2697 #ifdef HAVE_SERVER_SUPPORT
2698 env->le_ses = &echo_session;
2699 rc = lu_context_init(env->le_ses, LCT_SERVER_SESSION | LCT_NOREF);
2700 if (unlikely(rc < 0))
2702 lu_context_enter(env->le_ses);
2704 tsi = tgt_ses_info(env);
2705 tsi->tsi_exp = ec->ec_exp;
2706 tsi->tsi_jobid = NULL;
2709 case OBD_IOC_CREATE: /* may create echo object */
2710 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2711 GOTO (out, rc = -EPERM);
2713 rc = echo_create_object(env, ed, 1, oa, data->ioc_pbuf1,
2714 data->ioc_plen1, &dummy_oti);
2717 #ifdef HAVE_SERVER_SUPPORT
2718 case OBD_IOC_ECHO_MD: {
2725 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2726 GOTO(out, rc = -EPERM);
2728 count = data->ioc_count;
2729 cmd = data->ioc_command;
2731 id = data->ioc_obdo2.o_oi.oi.oi_id;
2732 dirlen = data->ioc_plen1;
2733 OBD_ALLOC(dir, dirlen + 1);
2735 GOTO(out, rc = -ENOMEM);
2737 if (copy_from_user(dir, data->ioc_pbuf1, dirlen)) {
2738 OBD_FREE(dir, data->ioc_plen1 + 1);
2739 GOTO(out, rc = -EFAULT);
2742 rc = echo_md_handler(ed, cmd, dir, dirlen, id, count, data);
2743 OBD_FREE(dir, dirlen + 1);
2746 case OBD_IOC_ECHO_ALLOC_SEQ: {
2747 struct lu_env *cl_env;
2752 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2753 GOTO(out, rc = -EPERM);
2755 cl_env = cl_env_get(&refcheck);
2757 GOTO(out, rc = PTR_ERR(cl_env));
2759 rc = lu_env_refill_by_tags(cl_env, ECHO_MD_CTX_TAG,
2762 cl_env_put(cl_env, &refcheck);
2766 rc = seq_client_get_seq(cl_env, ed->ed_cl_seq, &seq);
2767 cl_env_put(cl_env, &refcheck);
2769 CERROR("%s: Can not alloc seq: rc = %d\n",
2774 if (copy_to_user(data->ioc_pbuf1, &seq, data->ioc_plen1))
2777 max_count = LUSTRE_METADATA_SEQ_MAX_WIDTH;
2778 if (copy_to_user(data->ioc_pbuf2, &max_count,
2783 #endif /* HAVE_SERVER_SUPPORT */
2784 case OBD_IOC_DESTROY:
2785 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2786 GOTO (out, rc = -EPERM);
2788 rc = echo_get_object(&eco, ed, oa);
2790 rc = obd_destroy(env, ec->ec_exp, oa, eco->eo_lsm,
2791 &dummy_oti, NULL, NULL);
2793 eco->eo_deleted = 1;
2794 echo_put_object(eco);
2798 case OBD_IOC_GETATTR:
2799 rc = echo_get_object(&eco, ed, oa);
2801 struct obd_info oinfo = { { { 0 } } };
2802 oinfo.oi_md = eco->eo_lsm;
2804 rc = obd_getattr(env, ec->ec_exp, &oinfo);
2805 echo_put_object(eco);
2809 case OBD_IOC_SETATTR:
2810 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2811 GOTO (out, rc = -EPERM);
2813 rc = echo_get_object(&eco, ed, oa);
2815 struct obd_info oinfo = { { { 0 } } };
2817 oinfo.oi_md = eco->eo_lsm;
2819 rc = obd_setattr(env, ec->ec_exp, &oinfo, NULL);
2820 echo_put_object(eco);
2824 case OBD_IOC_BRW_WRITE:
2825 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2826 GOTO (out, rc = -EPERM);
2830 case OBD_IOC_BRW_READ:
2831 rc = echo_client_brw_ioctl(env, rw, exp, data, &dummy_oti);
2835 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
2836 GOTO (out, rc = -ENOTTY);
2841 #ifdef HAVE_SERVER_SUPPORT
2842 lu_context_exit(env->le_ses);
2843 lu_context_fini(env->le_ses);
2850 /* XXX this should be in a helper also called by target_send_reply */
2851 for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
2853 if (!ack_lock->mode)
2855 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
2861 static int echo_client_setup(const struct lu_env *env,
2862 struct obd_device *obddev, struct lustre_cfg *lcfg)
2864 struct echo_client_obd *ec = &obddev->u.echo_client;
2865 struct obd_device *tgt;
2866 struct obd_uuid echo_uuid = { "ECHO_UUID" };
2867 struct obd_connect_data *ocd = NULL;
2871 if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
2872 CERROR("requires a TARGET OBD name\n");
2876 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
2877 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
2878 CERROR("device not attached or not set up (%s)\n",
2879 lustre_cfg_string(lcfg, 1));
2883 spin_lock_init(&ec->ec_lock);
2884 INIT_LIST_HEAD(&ec->ec_objects);
2885 INIT_LIST_HEAD(&ec->ec_locks);
2887 ec->ec_nstripes = 0;
2889 if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
2890 #ifdef HAVE_SERVER_SUPPORT
2891 lu_context_tags_update(ECHO_MD_CTX_TAG);
2892 lu_session_tags_update(ECHO_MD_SES_TAG);
2894 CERROR("Local operations are NOT supported on client side. "
2895 "Only remote operations are supported. Metadata client "
2896 "must be run on server side.\n");
2901 OBD_ALLOC(ocd, sizeof(*ocd));
2903 CERROR("Can't alloc ocd connecting to %s\n",
2904 lustre_cfg_string(lcfg, 1));
2908 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
2909 OBD_CONNECT_BRW_SIZE |
2910 OBD_CONNECT_GRANT | OBD_CONNECT_FULL20 |
2911 OBD_CONNECT_64BITHASH | OBD_CONNECT_LVB_TYPE |
2913 ocd->ocd_brw_size = DT_MAX_BRW_SIZE;
2914 ocd->ocd_version = LUSTRE_VERSION_CODE;
2915 ocd->ocd_group = FID_SEQ_ECHO;
2917 rc = obd_connect(env, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
2919 /* Turn off pinger because it connects to tgt obd directly. */
2920 spin_lock(&tgt->obd_dev_lock);
2921 list_del_init(&ec->ec_exp->exp_obd_chain_timed);
2922 spin_unlock(&tgt->obd_dev_lock);
2925 OBD_FREE(ocd, sizeof(*ocd));
2928 CERROR("fail to connect to device %s\n",
2929 lustre_cfg_string(lcfg, 1));
2936 static int echo_client_cleanup(struct obd_device *obddev)
2938 struct echo_device *ed = obd2echo_dev(obddev);
2939 struct echo_client_obd *ec = &obddev->u.echo_client;
2943 /*Do nothing for Metadata echo client*/
2947 if (ed->ed_next_ismd) {
2948 #ifdef HAVE_SERVER_SUPPORT
2949 lu_context_tags_clear(ECHO_MD_CTX_TAG);
2950 lu_session_tags_clear(ECHO_MD_SES_TAG);
2952 CERROR("This is client-side only module, does not support "
2953 "metadata echo client.\n");
2958 if (!list_empty(&obddev->obd_exports)) {
2959 CERROR("still has clients!\n");
2963 LASSERT(atomic_read(&ec->ec_exp->exp_refcount) > 0);
2964 rc = obd_disconnect(ec->ec_exp);
2966 CERROR("fail to disconnect device: %d\n", rc);
2971 static int echo_client_connect(const struct lu_env *env,
2972 struct obd_export **exp,
2973 struct obd_device *src, struct obd_uuid *cluuid,
2974 struct obd_connect_data *data, void *localdata)
2977 struct lustre_handle conn = { 0 };
2980 rc = class_connect(&conn, src, cluuid);
2982 *exp = class_conn2export(&conn);
2988 static int echo_client_disconnect(struct obd_export *exp)
2994 GOTO(out, rc = -EINVAL);
2996 rc = class_disconnect(exp);
3002 static struct obd_ops echo_client_obd_ops = {
3003 .o_owner = THIS_MODULE,
3004 .o_iocontrol = echo_client_iocontrol,
3005 .o_connect = echo_client_connect,
3006 .o_disconnect = echo_client_disconnect
3009 int echo_client_init(void)
3013 rc = lu_kmem_init(echo_caches);
3015 rc = class_register_type(&echo_client_obd_ops, NULL, true, NULL,
3016 LUSTRE_ECHO_CLIENT_NAME,
3019 lu_kmem_fini(echo_caches);
3024 void echo_client_exit(void)
3026 class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
3027 lu_kmem_fini(echo_caches);
3030 static int __init obdecho_init(void)
3035 LCONSOLE_INFO("Echo OBD driver; http://www.lustre.org/\n");
3037 LASSERT(PAGE_CACHE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
3039 # ifdef HAVE_SERVER_SUPPORT
3040 rc = echo_persistent_pages_init();
3044 rc = class_register_type(&echo_obd_ops, NULL, true, NULL,
3045 LUSTRE_ECHO_NAME, NULL);
3050 rc = echo_client_init();
3052 # ifdef HAVE_SERVER_SUPPORT
3056 class_unregister_type(LUSTRE_ECHO_NAME);
3058 echo_persistent_pages_fini();
3064 static void /*__exit*/ obdecho_exit(void)
3068 # ifdef HAVE_SERVER_SUPPORT
3069 class_unregister_type(LUSTRE_ECHO_NAME);
3070 echo_persistent_pages_fini();
3074 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3075 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
3076 MODULE_LICENSE("GPL");
3078 cfs_module(obdecho, LUSTRE_VERSION_STRING, obdecho_init, obdecho_exit);
3080 /** @} echo_client */