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, 2012, 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 <lustre_fid.h>
51 #include <lustre_acl.h>
52 #include <lustre_net.h>
55 #include "echo_internal.h"
57 /** \defgroup echo_client Echo Client
62 struct cl_device ed_cl;
63 struct echo_client_obd *ed_ec;
65 struct cl_site ed_site_myself;
66 struct cl_site *ed_site;
67 struct lu_device *ed_next;
70 struct lu_client_seq *ed_cl_seq;
74 struct cl_object eo_cl;
75 struct cl_object_header eo_hdr;
77 struct echo_device *eo_dev;
78 cfs_list_t eo_obj_chain;
79 struct lov_stripe_md *eo_lsm;
80 cfs_atomic_t eo_npages;
84 struct echo_object_conf {
85 struct cl_object_conf eoc_cl;
86 struct lov_stripe_md **eoc_md;
90 struct cl_page_slice ep_cl;
92 cfs_page_t *ep_vmpage;
96 struct cl_lock_slice el_cl;
98 struct echo_object *el_object;
100 cfs_atomic_t el_refcount;
104 struct cl_io_slice ei_cl;
109 struct cl_req_slice er_cl;
113 static int echo_client_setup(const struct lu_env *env,
114 struct obd_device *obddev,
115 struct lustre_cfg *lcfg);
116 static int echo_client_cleanup(struct obd_device *obddev);
119 /** \defgroup echo_helpers Helper functions
122 static inline struct echo_device *cl2echo_dev(const struct cl_device *dev)
124 return container_of0(dev, struct echo_device, ed_cl);
127 static inline struct cl_device *echo_dev2cl(struct echo_device *d)
132 static inline struct echo_device *obd2echo_dev(const struct obd_device *obd)
134 return cl2echo_dev(lu2cl_dev(obd->obd_lu_dev));
137 static inline struct cl_object *echo_obj2cl(struct echo_object *eco)
142 static inline struct echo_object *cl2echo_obj(const struct cl_object *o)
144 return container_of(o, struct echo_object, eo_cl);
147 static inline struct echo_page *cl2echo_page(const struct cl_page_slice *s)
149 return container_of(s, struct echo_page, ep_cl);
152 static inline struct echo_lock *cl2echo_lock(const struct cl_lock_slice *s)
154 return container_of(s, struct echo_lock, el_cl);
157 static inline struct cl_lock *echo_lock2cl(const struct echo_lock *ecl)
159 return ecl->el_cl.cls_lock;
162 static struct lu_context_key echo_thread_key;
163 static inline struct echo_thread_info *echo_env_info(const struct lu_env *env)
165 struct echo_thread_info *info;
166 info = lu_context_key_get(&env->le_ctx, &echo_thread_key);
167 LASSERT(info != NULL);
172 struct echo_object_conf *cl2echo_conf(const struct cl_object_conf *c)
174 return container_of(c, struct echo_object_conf, eoc_cl);
177 /** @} echo_helpers */
179 static struct echo_object *cl_echo_object_find(struct echo_device *d,
180 struct lov_stripe_md **lsm);
181 static int cl_echo_object_put(struct echo_object *eco);
182 static int cl_echo_enqueue (struct echo_object *eco, obd_off start,
183 obd_off end, int mode, __u64 *cookie);
184 static int cl_echo_cancel (struct echo_device *d, __u64 cookie);
185 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
186 cfs_page_t **pages, int npages, int async);
188 static struct echo_thread_info *echo_env_info(const struct lu_env *env);
190 struct echo_thread_info {
191 struct echo_object_conf eti_conf;
192 struct lustre_md eti_md;
194 struct cl_2queue eti_queue;
196 struct cl_lock_descr eti_descr;
197 struct lu_fid eti_fid;
198 struct lu_fid eti_fid2;
199 struct md_op_spec eti_spec;
200 struct lov_mds_md_v3 eti_lmm;
201 struct lov_user_md_v3 eti_lum;
202 struct md_attr eti_ma;
203 struct lu_name eti_lname;
204 /* per-thread values, can be re-used */
208 struct lu_buf eti_buf;
209 char eti_xattr_buf[LUSTRE_POSIX_ACL_MAX_SIZE];
212 /* No session used right now */
213 struct echo_session_info {
217 static cfs_mem_cache_t *echo_lock_kmem;
218 static cfs_mem_cache_t *echo_object_kmem;
219 static cfs_mem_cache_t *echo_thread_kmem;
220 static cfs_mem_cache_t *echo_session_kmem;
221 //static cfs_mem_cache_t *echo_req_kmem;
223 static struct lu_kmem_descr echo_caches[] = {
225 .ckd_cache = &echo_lock_kmem,
226 .ckd_name = "echo_lock_kmem",
227 .ckd_size = sizeof (struct echo_lock)
230 .ckd_cache = &echo_object_kmem,
231 .ckd_name = "echo_object_kmem",
232 .ckd_size = sizeof (struct echo_object)
235 .ckd_cache = &echo_thread_kmem,
236 .ckd_name = "echo_thread_kmem",
237 .ckd_size = sizeof (struct echo_thread_info)
240 .ckd_cache = &echo_session_kmem,
241 .ckd_name = "echo_session_kmem",
242 .ckd_size = sizeof (struct echo_session_info)
246 .ckd_cache = &echo_req_kmem,
247 .ckd_name = "echo_req_kmem",
248 .ckd_size = sizeof (struct echo_req)
256 /** \defgroup echo_page Page operations
258 * Echo page operations.
262 static cfs_page_t *echo_page_vmpage(const struct lu_env *env,
263 const struct cl_page_slice *slice)
265 return cl2echo_page(slice)->ep_vmpage;
268 static int echo_page_own(const struct lu_env *env,
269 const struct cl_page_slice *slice,
270 struct cl_io *io, int nonblock)
272 struct echo_page *ep = cl2echo_page(slice);
275 mutex_lock(&ep->ep_lock);
276 else if (!mutex_trylock(&ep->ep_lock))
281 static void echo_page_disown(const struct lu_env *env,
282 const struct cl_page_slice *slice,
285 struct echo_page *ep = cl2echo_page(slice);
287 LASSERT(mutex_is_locked(&ep->ep_lock));
288 mutex_unlock(&ep->ep_lock);
291 static void echo_page_discard(const struct lu_env *env,
292 const struct cl_page_slice *slice,
293 struct cl_io *unused)
295 cl_page_delete(env, slice->cpl_page);
298 static int echo_page_is_vmlocked(const struct lu_env *env,
299 const struct cl_page_slice *slice)
301 if (mutex_is_locked(&cl2echo_page(slice)->ep_lock))
306 static void echo_page_completion(const struct lu_env *env,
307 const struct cl_page_slice *slice,
310 LASSERT(slice->cpl_page->cp_sync_io != NULL);
313 static void echo_page_fini(const struct lu_env *env,
314 struct cl_page_slice *slice)
316 struct echo_page *ep = cl2echo_page(slice);
317 struct echo_object *eco = cl2echo_obj(slice->cpl_obj);
318 cfs_page_t *vmpage = ep->ep_vmpage;
321 cfs_atomic_dec(&eco->eo_npages);
322 page_cache_release(vmpage);
326 static int echo_page_prep(const struct lu_env *env,
327 const struct cl_page_slice *slice,
328 struct cl_io *unused)
333 static int echo_page_print(const struct lu_env *env,
334 const struct cl_page_slice *slice,
335 void *cookie, lu_printer_t printer)
337 struct echo_page *ep = cl2echo_page(slice);
339 (*printer)(env, cookie, LUSTRE_ECHO_CLIENT_NAME"-page@%p %d vm@%p\n",
340 ep, mutex_is_locked(&ep->ep_lock), ep->ep_vmpage);
344 static const struct cl_page_operations echo_page_ops = {
345 .cpo_own = echo_page_own,
346 .cpo_disown = echo_page_disown,
347 .cpo_discard = echo_page_discard,
348 .cpo_vmpage = echo_page_vmpage,
349 .cpo_fini = echo_page_fini,
350 .cpo_print = echo_page_print,
351 .cpo_is_vmlocked = echo_page_is_vmlocked,
354 .cpo_prep = echo_page_prep,
355 .cpo_completion = echo_page_completion,
358 .cpo_prep = echo_page_prep,
359 .cpo_completion = echo_page_completion,
365 /** \defgroup echo_lock Locking
367 * echo lock operations
371 static void echo_lock_fini(const struct lu_env *env,
372 struct cl_lock_slice *slice)
374 struct echo_lock *ecl = cl2echo_lock(slice);
376 LASSERT(cfs_list_empty(&ecl->el_chain));
377 OBD_SLAB_FREE_PTR(ecl, echo_lock_kmem);
380 static void echo_lock_delete(const struct lu_env *env,
381 const struct cl_lock_slice *slice)
383 struct echo_lock *ecl = cl2echo_lock(slice);
385 LASSERT(cfs_list_empty(&ecl->el_chain));
388 static int echo_lock_fits_into(const struct lu_env *env,
389 const struct cl_lock_slice *slice,
390 const struct cl_lock_descr *need,
391 const struct cl_io *unused)
396 static struct cl_lock_operations echo_lock_ops = {
397 .clo_fini = echo_lock_fini,
398 .clo_delete = echo_lock_delete,
399 .clo_fits_into = echo_lock_fits_into
404 /** \defgroup echo_cl_ops cl_object operations
406 * operations for cl_object
410 static int echo_page_init(const struct lu_env *env, struct cl_object *obj,
411 struct cl_page *page, cfs_page_t *vmpage)
413 struct echo_page *ep = cl_object_page_slice(obj, page);
414 struct echo_object *eco = cl2echo_obj(obj);
417 ep->ep_vmpage = vmpage;
418 page_cache_get(vmpage);
419 mutex_init(&ep->ep_lock);
420 cl_page_slice_add(page, &ep->ep_cl, obj, &echo_page_ops);
421 cfs_atomic_inc(&eco->eo_npages);
425 static int echo_io_init(const struct lu_env *env, struct cl_object *obj,
431 static int echo_lock_init(const struct lu_env *env,
432 struct cl_object *obj, struct cl_lock *lock,
433 const struct cl_io *unused)
435 struct echo_lock *el;
438 OBD_SLAB_ALLOC_PTR_GFP(el, echo_lock_kmem, CFS_ALLOC_IO);
440 cl_lock_slice_add(lock, &el->el_cl, obj, &echo_lock_ops);
441 el->el_object = cl2echo_obj(obj);
442 CFS_INIT_LIST_HEAD(&el->el_chain);
443 cfs_atomic_set(&el->el_refcount, 0);
445 RETURN(el == NULL ? -ENOMEM : 0);
448 static int echo_conf_set(const struct lu_env *env, struct cl_object *obj,
449 const struct cl_object_conf *conf)
454 static const struct cl_object_operations echo_cl_obj_ops = {
455 .coo_page_init = echo_page_init,
456 .coo_lock_init = echo_lock_init,
457 .coo_io_init = echo_io_init,
458 .coo_conf_set = echo_conf_set
460 /** @} echo_cl_ops */
462 /** \defgroup echo_lu_ops lu_object operations
464 * operations for echo lu object.
468 static int echo_object_init(const struct lu_env *env, struct lu_object *obj,
469 const struct lu_object_conf *conf)
471 struct echo_device *ed = cl2echo_dev(lu2cl_dev(obj->lo_dev));
472 struct echo_client_obd *ec = ed->ed_ec;
473 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
477 struct lu_object *below;
478 struct lu_device *under;
481 below = under->ld_ops->ldo_object_alloc(env, obj->lo_header,
485 lu_object_add(obj, below);
488 if (!ed->ed_next_ismd) {
489 const struct cl_object_conf *cconf = lu2cl_conf(conf);
490 struct echo_object_conf *econf = cl2echo_conf(cconf);
492 LASSERT(econf->eoc_md);
493 eco->eo_lsm = *econf->eoc_md;
494 /* clear the lsm pointer so that it won't get freed. */
495 *econf->eoc_md = NULL;
501 cfs_atomic_set(&eco->eo_npages, 0);
502 cl_object_page_init(lu2cl(obj), sizeof(struct echo_page));
504 spin_lock(&ec->ec_lock);
505 cfs_list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
506 spin_unlock(&ec->ec_lock);
511 /* taken from osc_unpackmd() */
512 static int echo_alloc_memmd(struct echo_device *ed,
513 struct lov_stripe_md **lsmp)
519 /* If export is lov/osc then use their obd method */
520 if (ed->ed_next != NULL)
521 return obd_alloc_memmd(ed->ed_ec->ec_exp, lsmp);
522 /* OFD has no unpackmd method, do everything here */
523 lsm_size = lov_stripe_md_size(1);
525 LASSERT(*lsmp == NULL);
526 OBD_ALLOC(*lsmp, lsm_size);
530 OBD_ALLOC((*lsmp)->lsm_oinfo[0], sizeof(struct lov_oinfo));
531 if ((*lsmp)->lsm_oinfo[0] == NULL) {
532 OBD_FREE(*lsmp, lsm_size);
536 loi_init((*lsmp)->lsm_oinfo[0]);
537 (*lsmp)->lsm_maxbytes = LUSTRE_STRIPE_MAXBYTES;
538 ostid_set_seq_echo(&(*lsmp)->lsm_oi);
543 static int echo_free_memmd(struct echo_device *ed, struct lov_stripe_md **lsmp)
549 /* If export is lov/osc then use their obd method */
550 if (ed->ed_next != NULL)
551 return obd_free_memmd(ed->ed_ec->ec_exp, lsmp);
552 /* OFD has no unpackmd method, do everything here */
553 lsm_size = lov_stripe_md_size(1);
555 LASSERT(*lsmp != NULL);
556 OBD_FREE((*lsmp)->lsm_oinfo[0], sizeof(struct lov_oinfo));
557 OBD_FREE(*lsmp, lsm_size);
562 static void echo_object_free(const struct lu_env *env, struct lu_object *obj)
564 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
565 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
568 LASSERT(cfs_atomic_read(&eco->eo_npages) == 0);
570 spin_lock(&ec->ec_lock);
571 cfs_list_del_init(&eco->eo_obj_chain);
572 spin_unlock(&ec->ec_lock);
575 lu_object_header_fini(obj->lo_header);
578 echo_free_memmd(eco->eo_dev, &eco->eo_lsm);
579 OBD_SLAB_FREE_PTR(eco, echo_object_kmem);
583 static int echo_object_print(const struct lu_env *env, void *cookie,
584 lu_printer_t p, const struct lu_object *o)
586 struct echo_object *obj = cl2echo_obj(lu2cl(o));
588 return (*p)(env, cookie, "echoclient-object@%p", obj);
591 static const struct lu_object_operations echo_lu_obj_ops = {
592 .loo_object_init = echo_object_init,
593 .loo_object_delete = NULL,
594 .loo_object_release = NULL,
595 .loo_object_free = echo_object_free,
596 .loo_object_print = echo_object_print,
597 .loo_object_invariant = NULL
599 /** @} echo_lu_ops */
601 /** \defgroup echo_lu_dev_ops lu_device operations
603 * Operations for echo lu device.
607 static struct lu_object *echo_object_alloc(const struct lu_env *env,
608 const struct lu_object_header *hdr,
609 struct lu_device *dev)
611 struct echo_object *eco;
612 struct lu_object *obj = NULL;
615 /* we're the top dev. */
616 LASSERT(hdr == NULL);
617 OBD_SLAB_ALLOC_PTR_GFP(eco, echo_object_kmem, CFS_ALLOC_IO);
619 struct cl_object_header *hdr = &eco->eo_hdr;
621 obj = &echo_obj2cl(eco)->co_lu;
622 cl_object_header_init(hdr);
623 lu_object_init(obj, &hdr->coh_lu, dev);
624 lu_object_add_top(&hdr->coh_lu, obj);
626 eco->eo_cl.co_ops = &echo_cl_obj_ops;
627 obj->lo_ops = &echo_lu_obj_ops;
632 static struct lu_device_operations echo_device_lu_ops = {
633 .ldo_object_alloc = echo_object_alloc,
636 /** @} echo_lu_dev_ops */
638 static struct cl_device_operations echo_device_cl_ops = {
641 /** \defgroup echo_init Setup and teardown
643 * Init and fini functions for echo client.
647 static int echo_site_init(const struct lu_env *env, struct echo_device *ed)
649 struct cl_site *site = &ed->ed_site_myself;
652 /* initialize site */
653 rc = cl_site_init(site, &ed->ed_cl);
655 CERROR("Cannot initilize site for echo client(%d)\n", rc);
659 rc = lu_site_init_finish(&site->cs_lu);
667 static void echo_site_fini(const struct lu_env *env, struct echo_device *ed)
670 if (!ed->ed_next_ismd)
671 cl_site_fini(ed->ed_site);
676 static void *echo_thread_key_init(const struct lu_context *ctx,
677 struct lu_context_key *key)
679 struct echo_thread_info *info;
681 OBD_SLAB_ALLOC_PTR_GFP(info, echo_thread_kmem, CFS_ALLOC_IO);
683 info = ERR_PTR(-ENOMEM);
687 static void echo_thread_key_fini(const struct lu_context *ctx,
688 struct lu_context_key *key, void *data)
690 struct echo_thread_info *info = data;
691 OBD_SLAB_FREE_PTR(info, echo_thread_kmem);
694 static void echo_thread_key_exit(const struct lu_context *ctx,
695 struct lu_context_key *key, void *data)
699 static struct lu_context_key echo_thread_key = {
700 .lct_tags = LCT_CL_THREAD,
701 .lct_init = echo_thread_key_init,
702 .lct_fini = echo_thread_key_fini,
703 .lct_exit = echo_thread_key_exit
706 static void *echo_session_key_init(const struct lu_context *ctx,
707 struct lu_context_key *key)
709 struct echo_session_info *session;
711 OBD_SLAB_ALLOC_PTR_GFP(session, echo_session_kmem, CFS_ALLOC_IO);
713 session = ERR_PTR(-ENOMEM);
717 static void echo_session_key_fini(const struct lu_context *ctx,
718 struct lu_context_key *key, void *data)
720 struct echo_session_info *session = data;
721 OBD_SLAB_FREE_PTR(session, echo_session_kmem);
724 static void echo_session_key_exit(const struct lu_context *ctx,
725 struct lu_context_key *key, void *data)
729 static struct lu_context_key echo_session_key = {
730 .lct_tags = LCT_SESSION,
731 .lct_init = echo_session_key_init,
732 .lct_fini = echo_session_key_fini,
733 .lct_exit = echo_session_key_exit
736 LU_TYPE_INIT_FINI(echo, &echo_thread_key, &echo_session_key);
738 #define ECHO_SEQ_WIDTH 0xffffffff
739 static int echo_fid_init(struct echo_device *ed, char *obd_name,
740 struct seq_server_site *ss)
746 OBD_ALLOC_PTR(ed->ed_cl_seq);
747 if (ed->ed_cl_seq == NULL)
750 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
752 GOTO(out_free_seq, rc = -ENOMEM);
754 snprintf(prefix, MAX_OBD_NAME + 5, "srv-%s", obd_name);
756 /* Init client side sequence-manager */
757 rc = seq_client_init(ed->ed_cl_seq, NULL,
759 prefix, ss->ss_server_seq);
760 ed->ed_cl_seq->lcs_width = ECHO_SEQ_WIDTH;
761 OBD_FREE(prefix, MAX_OBD_NAME + 5);
763 GOTO(out_free_seq, rc);
768 OBD_FREE_PTR(ed->ed_cl_seq);
769 ed->ed_cl_seq = NULL;
773 static int echo_fid_fini(struct obd_device *obddev)
775 struct echo_device *ed = obd2echo_dev(obddev);
778 if (ed->ed_cl_seq != NULL) {
779 seq_client_fini(ed->ed_cl_seq);
780 OBD_FREE_PTR(ed->ed_cl_seq);
781 ed->ed_cl_seq = NULL;
787 static struct lu_device *echo_device_alloc(const struct lu_env *env,
788 struct lu_device_type *t,
789 struct lustre_cfg *cfg)
791 struct lu_device *next;
792 struct echo_device *ed;
793 struct cl_device *cd;
794 struct obd_device *obd = NULL; /* to keep compiler happy */
795 struct obd_device *tgt;
796 const char *tgt_type_name;
803 GOTO(out, rc = -ENOMEM);
807 rc = cl_device_init(cd, t);
811 cd->cd_lu_dev.ld_ops = &echo_device_lu_ops;
812 cd->cd_ops = &echo_device_cl_ops;
815 obd = class_name2obd(lustre_cfg_string(cfg, 0));
816 LASSERT(obd != NULL);
817 LASSERT(env != NULL);
819 tgt = class_name2obd(lustre_cfg_string(cfg, 1));
821 CERROR("Can not find tgt device %s\n",
822 lustre_cfg_string(cfg, 1));
823 GOTO(out, rc = -ENODEV);
826 next = tgt->obd_lu_dev;
827 if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
828 ed->ed_next_ismd = 1;
830 ed->ed_next_ismd = 0;
831 rc = echo_site_init(env, ed);
837 rc = echo_client_setup(env, obd, cfg);
841 ed->ed_ec = &obd->u.echo_client;
844 if (ed->ed_next_ismd) {
845 /* Suppose to connect to some Metadata layer */
847 struct lu_device *ld;
851 CERROR("%s is not lu device type!\n",
852 lustre_cfg_string(cfg, 1));
853 GOTO(out, rc = -EINVAL);
856 tgt_type_name = lustre_cfg_string(cfg, 2);
857 if (!tgt_type_name) {
858 CERROR("%s no type name for echo %s setup\n",
859 lustre_cfg_string(cfg, 1),
860 tgt->obd_type->typ_name);
861 GOTO(out, rc = -EINVAL);
866 spin_lock(&ls->ls_ld_lock);
867 cfs_list_for_each_entry(ld, &ls->ls_ld_linkage, ld_linkage) {
868 if (strcmp(ld->ld_type->ldt_name, tgt_type_name) == 0) {
873 spin_unlock(&ls->ls_ld_lock);
876 CERROR("%s is not lu device type!\n",
877 lustre_cfg_string(cfg, 1));
878 GOTO(out, rc = -EINVAL);
882 /* For MD echo client, it will use the site in MDS stack */
883 ed->ed_site_myself.cs_lu = *ls;
884 ed->ed_site = &ed->ed_site_myself;
885 ed->ed_cl.cd_lu_dev.ld_site = &ed->ed_site_myself.cs_lu;
886 rc = echo_fid_init(ed, obd->obd_name, lu_site2seq(ls));
888 CERROR("echo fid init error %d\n", rc);
892 /* if echo client is to be stacked upon ost device, the next is
893 * NULL since ost is not a clio device so far */
894 if (next != NULL && !lu_device_is_cl(next))
897 tgt_type_name = tgt->obd_type->typ_name;
899 LASSERT(next != NULL);
900 if (next->ld_site != NULL)
901 GOTO(out, rc = -EBUSY);
903 next->ld_site = &ed->ed_site->cs_lu;
904 rc = next->ld_type->ldt_ops->ldto_device_init(env, next,
905 next->ld_type->ldt_name,
910 /* Tricky case, I have to determine the obd type since
911 * CLIO uses the different parameters to initialize
912 * objects for lov & osc. */
913 if (strcmp(tgt_type_name, LUSTRE_LOV_NAME) == 0)
914 ed->ed_next_islov = 1;
916 LASSERT(strcmp(tgt_type_name,
917 LUSTRE_OSC_NAME) == 0);
919 LASSERT(strcmp(tgt_type_name, LUSTRE_OST_NAME) == 0);
923 RETURN(&cd->cd_lu_dev);
928 rc2 = echo_client_cleanup(obd);
930 CERROR("Cleanup obd device %s error(%d)\n",
935 echo_site_fini(env, ed);
937 cl_device_fini(&ed->ed_cl);
947 static int echo_device_init(const struct lu_env *env, struct lu_device *d,
948 const char *name, struct lu_device *next)
954 static struct lu_device *echo_device_fini(const struct lu_env *env,
957 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
958 struct lu_device *next = ed->ed_next;
960 while (next && !ed->ed_next_ismd)
961 next = next->ld_type->ldt_ops->ldto_device_fini(env, next);
965 static void echo_lock_release(const struct lu_env *env,
966 struct echo_lock *ecl,
969 struct cl_lock *clk = echo_lock2cl(ecl);
973 cl_lock_release(env, clk, "ec enqueue", ecl->el_object);
975 cl_lock_mutex_get(env, clk);
976 cl_lock_cancel(env, clk);
977 cl_lock_delete(env, clk);
978 cl_lock_mutex_put(env, clk);
980 cl_lock_put(env, clk);
983 static struct lu_device *echo_device_free(const struct lu_env *env,
986 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
987 struct echo_client_obd *ec = ed->ed_ec;
988 struct echo_object *eco;
989 struct lu_device *next = ed->ed_next;
991 CDEBUG(D_INFO, "echo device:%p is going to be freed, next = %p\n",
994 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
996 /* check if there are objects still alive.
997 * It shouldn't have any object because lu_site_purge would cleanup
998 * all of cached objects. Anyway, probably the echo device is being
999 * parallelly accessed.
1001 spin_lock(&ec->ec_lock);
1002 cfs_list_for_each_entry(eco, &ec->ec_objects, eo_obj_chain)
1003 eco->eo_deleted = 1;
1004 spin_unlock(&ec->ec_lock);
1007 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
1010 "Waiting for the reference of echo object to be dropped\n");
1012 /* Wait for the last reference to be dropped. */
1013 spin_lock(&ec->ec_lock);
1014 while (!cfs_list_empty(&ec->ec_objects)) {
1015 spin_unlock(&ec->ec_lock);
1016 CERROR("echo_client still has objects at cleanup time, "
1017 "wait for 1 second\n");
1018 cfs_schedule_timeout_and_set_state(CFS_TASK_UNINT,
1019 cfs_time_seconds(1));
1020 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
1021 spin_lock(&ec->ec_lock);
1023 spin_unlock(&ec->ec_lock);
1025 LASSERT(cfs_list_empty(&ec->ec_locks));
1027 CDEBUG(D_INFO, "No object exists, exiting...\n");
1029 echo_client_cleanup(d->ld_obd);
1030 echo_fid_fini(d->ld_obd);
1031 while (next && !ed->ed_next_ismd)
1032 next = next->ld_type->ldt_ops->ldto_device_free(env, next);
1034 LASSERT(ed->ed_site == lu2cl_site(d->ld_site));
1035 echo_site_fini(env, ed);
1036 cl_device_fini(&ed->ed_cl);
1042 static const struct lu_device_type_operations echo_device_type_ops = {
1043 .ldto_init = echo_type_init,
1044 .ldto_fini = echo_type_fini,
1046 .ldto_start = echo_type_start,
1047 .ldto_stop = echo_type_stop,
1049 .ldto_device_alloc = echo_device_alloc,
1050 .ldto_device_free = echo_device_free,
1051 .ldto_device_init = echo_device_init,
1052 .ldto_device_fini = echo_device_fini
1055 static struct lu_device_type echo_device_type = {
1056 .ldt_tags = LU_DEVICE_CL,
1057 .ldt_name = LUSTRE_ECHO_CLIENT_NAME,
1058 .ldt_ops = &echo_device_type_ops,
1059 .ldt_ctx_tags = LCT_CL_THREAD | LCT_MD_THREAD | LCT_DT_THREAD,
1063 /** \defgroup echo_exports Exported operations
1065 * exporting functions to echo client
1070 /* Interfaces to echo client obd device */
1071 static struct echo_object *cl_echo_object_find(struct echo_device *d,
1072 struct lov_stripe_md **lsmp)
1075 struct echo_thread_info *info;
1076 struct echo_object_conf *conf;
1077 struct lov_stripe_md *lsm;
1078 struct echo_object *eco;
1079 struct cl_object *obj;
1088 LASSERT(ostid_id(&lsm->lsm_oi) != 0);
1089 LASSERT(ostid_seq(&lsm->lsm_oi) == FID_SEQ_ECHO);
1091 /* Never return an object if the obd is to be freed. */
1092 if (echo_dev2cl(d)->cd_lu_dev.ld_obd->obd_stopping)
1093 RETURN(ERR_PTR(-ENODEV));
1095 env = cl_env_get(&refcheck);
1097 RETURN((void *)env);
1099 info = echo_env_info(env);
1100 conf = &info->eti_conf;
1102 if (!d->ed_next_islov) {
1103 struct lov_oinfo *oinfo = lsm->lsm_oinfo[0];
1104 LASSERT(oinfo != NULL);
1105 oinfo->loi_oi = lsm->lsm_oi;
1106 conf->eoc_cl.u.coc_oinfo = oinfo;
1108 struct lustre_md *md;
1110 memset(md, 0, sizeof *md);
1112 conf->eoc_cl.u.coc_md = md;
1115 conf->eoc_md = lsmp;
1117 fid = &info->eti_fid;
1118 rc = ostid_to_fid(fid, &lsm->lsm_oi, 0);
1120 GOTO(out, eco = ERR_PTR(rc));
1122 /* In the function below, .hs_keycmp resolves to
1123 * lu_obj_hop_keycmp() */
1124 /* coverity[overrun-buffer-val] */
1125 obj = cl_object_find(env, echo_dev2cl(d), fid, &conf->eoc_cl);
1127 GOTO(out, eco = (void*)obj);
1129 eco = cl2echo_obj(obj);
1130 if (eco->eo_deleted) {
1131 cl_object_put(env, obj);
1132 eco = ERR_PTR(-EAGAIN);
1136 cl_env_put(env, &refcheck);
1140 static int cl_echo_object_put(struct echo_object *eco)
1143 struct cl_object *obj = echo_obj2cl(eco);
1147 env = cl_env_get(&refcheck);
1149 RETURN(PTR_ERR(env));
1151 /* an external function to kill an object? */
1152 if (eco->eo_deleted) {
1153 struct lu_object_header *loh = obj->co_lu.lo_header;
1154 LASSERT(&eco->eo_hdr == luh2coh(loh));
1155 set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
1158 cl_object_put(env, obj);
1159 cl_env_put(env, &refcheck);
1163 static int cl_echo_enqueue0(struct lu_env *env, struct echo_object *eco,
1164 obd_off start, obd_off end, int mode,
1165 __u64 *cookie , __u32 enqflags)
1168 struct cl_lock *lck;
1169 struct cl_object *obj;
1170 struct cl_lock_descr *descr;
1171 struct echo_thread_info *info;
1175 info = echo_env_info(env);
1177 descr = &info->eti_descr;
1178 obj = echo_obj2cl(eco);
1180 descr->cld_obj = obj;
1181 descr->cld_start = cl_index(obj, start);
1182 descr->cld_end = cl_index(obj, end);
1183 descr->cld_mode = mode == LCK_PW ? CLM_WRITE : CLM_READ;
1184 descr->cld_enq_flags = enqflags;
1187 lck = cl_lock_request(env, io, descr, "ec enqueue", eco);
1189 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
1190 struct echo_lock *el;
1192 rc = cl_wait(env, lck);
1194 el = cl2echo_lock(cl_lock_at(lck, &echo_device_type));
1195 spin_lock(&ec->ec_lock);
1196 if (cfs_list_empty(&el->el_chain)) {
1197 cfs_list_add(&el->el_chain, &ec->ec_locks);
1198 el->el_cookie = ++ec->ec_unique;
1200 cfs_atomic_inc(&el->el_refcount);
1201 *cookie = el->el_cookie;
1202 spin_unlock(&ec->ec_lock);
1204 cl_lock_release(env, lck, "ec enqueue", cfs_current());
1210 static int cl_echo_enqueue(struct echo_object *eco, obd_off start, obd_off end,
1211 int mode, __u64 *cookie)
1213 struct echo_thread_info *info;
1220 env = cl_env_get(&refcheck);
1222 RETURN(PTR_ERR(env));
1224 info = echo_env_info(env);
1227 io->ci_ignore_layout = 1;
1228 result = cl_io_init(env, io, CIT_MISC, echo_obj2cl(eco));
1231 LASSERT(result == 0);
1233 result = cl_echo_enqueue0(env, eco, start, end, mode, cookie, 0);
1234 cl_io_fini(env, io);
1238 cl_env_put(env, &refcheck);
1242 static int cl_echo_cancel0(struct lu_env *env, struct echo_device *ed,
1245 struct echo_client_obd *ec = ed->ed_ec;
1246 struct echo_lock *ecl = NULL;
1248 int found = 0, still_used = 0;
1251 LASSERT(ec != NULL);
1252 spin_lock(&ec->ec_lock);
1253 cfs_list_for_each (el, &ec->ec_locks) {
1254 ecl = cfs_list_entry (el, struct echo_lock, el_chain);
1255 CDEBUG(D_INFO, "ecl: %p, cookie: "LPX64"\n", ecl, ecl->el_cookie);
1256 found = (ecl->el_cookie == cookie);
1258 if (cfs_atomic_dec_and_test(&ecl->el_refcount))
1259 cfs_list_del_init(&ecl->el_chain);
1265 spin_unlock(&ec->ec_lock);
1270 echo_lock_release(env, ecl, still_used);
1274 static int cl_echo_cancel(struct echo_device *ed, __u64 cookie)
1281 env = cl_env_get(&refcheck);
1283 RETURN(PTR_ERR(env));
1285 rc = cl_echo_cancel0(env, ed, cookie);
1287 cl_env_put(env, &refcheck);
1291 static int cl_echo_async_brw(const struct lu_env *env, struct cl_io *io,
1292 enum cl_req_type unused, struct cl_2queue *queue)
1294 struct cl_page *clp;
1295 struct cl_page *temp;
1299 cl_page_list_for_each_safe(clp, temp, &queue->c2_qin) {
1301 rc = cl_page_cache_add(env, io, clp, CRT_WRITE);
1304 result = result ?: rc;
1309 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
1310 cfs_page_t **pages, int npages, int async)
1313 struct echo_thread_info *info;
1314 struct cl_object *obj = echo_obj2cl(eco);
1315 struct echo_device *ed = eco->eo_dev;
1316 struct cl_2queue *queue;
1318 struct cl_page *clp;
1319 struct lustre_handle lh = { 0 };
1320 int page_size = cl_page_size(obj);
1326 LASSERT((offset & ~CFS_PAGE_MASK) == 0);
1327 LASSERT(ed->ed_next != NULL);
1328 env = cl_env_get(&refcheck);
1330 RETURN(PTR_ERR(env));
1332 info = echo_env_info(env);
1334 queue = &info->eti_queue;
1336 cl_2queue_init(queue);
1338 io->ci_ignore_layout = 1;
1339 rc = cl_io_init(env, io, CIT_MISC, obj);
1345 rc = cl_echo_enqueue0(env, eco, offset,
1346 offset + npages * CFS_PAGE_SIZE - 1,
1347 rw == READ ? LCK_PR : LCK_PW, &lh.cookie,
1350 GOTO(error_lock, rc);
1352 for (i = 0; i < npages; i++) {
1354 clp = cl_page_find(env, obj, cl_index(obj, offset),
1355 pages[i], CPT_TRANSIENT);
1360 LASSERT(clp->cp_type == CPT_TRANSIENT);
1362 rc = cl_page_own(env, io, clp);
1364 LASSERT(clp->cp_state == CPS_FREEING);
1365 cl_page_put(env, clp);
1369 cl_2queue_add(queue, clp);
1371 /* drop the reference count for cl_page_find, so that the page
1372 * will be freed in cl_2queue_fini. */
1373 cl_page_put(env, clp);
1374 cl_page_clip(env, clp, 0, page_size);
1376 offset += page_size;
1380 enum cl_req_type typ = rw == READ ? CRT_READ : CRT_WRITE;
1382 async = async && (typ == CRT_WRITE);
1384 rc = cl_echo_async_brw(env, io, typ, queue);
1386 rc = cl_io_submit_sync(env, io, typ, queue, 0);
1387 CDEBUG(D_INFO, "echo_client %s write returns %d\n",
1388 async ? "async" : "sync", rc);
1391 cl_echo_cancel0(env, ed, lh.cookie);
1394 cl_2queue_discard(env, io, queue);
1395 cl_2queue_disown(env, io, queue);
1396 cl_2queue_fini(env, queue);
1397 cl_io_fini(env, io);
1399 cl_env_put(env, &refcheck);
1402 /** @} echo_exports */
1405 static obd_id last_object_id;
1408 echo_copyout_lsm (struct lov_stripe_md *lsm, void *_ulsm, int ulsm_nob)
1410 struct lov_stripe_md *ulsm = _ulsm;
1413 nob = offsetof (struct lov_stripe_md, lsm_oinfo[lsm->lsm_stripe_count]);
1417 if (cfs_copy_to_user (ulsm, lsm, sizeof(ulsm)))
1420 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1421 if (cfs_copy_to_user (ulsm->lsm_oinfo[i], lsm->lsm_oinfo[i],
1422 sizeof(lsm->lsm_oinfo[0])))
1429 echo_copyin_lsm (struct echo_device *ed, struct lov_stripe_md *lsm,
1430 void *ulsm, int ulsm_nob)
1432 struct echo_client_obd *ec = ed->ed_ec;
1435 if (ulsm_nob < sizeof (*lsm))
1438 if (cfs_copy_from_user (lsm, ulsm, sizeof (*lsm)))
1441 if (lsm->lsm_stripe_count > ec->ec_nstripes ||
1442 lsm->lsm_magic != LOV_MAGIC ||
1443 (lsm->lsm_stripe_size & (~CFS_PAGE_MASK)) != 0 ||
1444 ((__u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count > ~0UL))
1448 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1449 if (cfs_copy_from_user(lsm->lsm_oinfo[i],
1450 ((struct lov_stripe_md *)ulsm)-> \
1452 sizeof(lsm->lsm_oinfo[0])))
1458 static inline void echo_md_build_name(struct lu_name *lname, char *name,
1461 sprintf(name, LPU64, id);
1462 lname->ln_name = name;
1463 lname->ln_namelen = strlen(name);
1466 /* similar to mdt_attr_get_complex */
1467 static int echo_big_lmm_get(const struct lu_env *env, struct md_object *o,
1470 struct echo_thread_info *info = echo_env_info(env);
1475 LASSERT(ma->ma_lmm_size > 0);
1477 rc = mo_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LOV);
1481 /* big_lmm may need to be grown */
1482 if (info->eti_big_lmmsize < rc) {
1483 int size = size_roundup_power2(rc);
1485 if (info->eti_big_lmmsize > 0) {
1486 /* free old buffer */
1487 LASSERT(info->eti_big_lmm);
1488 OBD_FREE_LARGE(info->eti_big_lmm,
1489 info->eti_big_lmmsize);
1490 info->eti_big_lmm = NULL;
1491 info->eti_big_lmmsize = 0;
1494 OBD_ALLOC_LARGE(info->eti_big_lmm, size);
1495 if (info->eti_big_lmm == NULL)
1497 info->eti_big_lmmsize = size;
1499 LASSERT(info->eti_big_lmmsize >= rc);
1501 info->eti_buf.lb_buf = info->eti_big_lmm;
1502 info->eti_buf.lb_len = info->eti_big_lmmsize;
1503 rc = mo_xattr_get(env, o, &info->eti_buf, XATTR_NAME_LOV);
1507 ma->ma_valid |= MA_LOV;
1508 ma->ma_lmm = info->eti_big_lmm;
1509 ma->ma_lmm_size = rc;
1514 int echo_attr_get_complex(const struct lu_env *env, struct md_object *next,
1517 struct echo_thread_info *info = echo_env_info(env);
1518 struct lu_buf *buf = &info->eti_buf;
1519 cfs_umode_t mode = lu_object_attr(&next->mo_lu);
1520 int need = ma->ma_need;
1527 if (need & MA_INODE) {
1528 ma->ma_need = MA_INODE;
1529 rc = mo_attr_get(env, next, ma);
1532 ma->ma_valid |= MA_INODE;
1535 if (need & MA_LOV) {
1536 if (S_ISREG(mode) || S_ISDIR(mode)) {
1537 LASSERT(ma->ma_lmm_size > 0);
1538 buf->lb_buf = ma->ma_lmm;
1539 buf->lb_len = ma->ma_lmm_size;
1540 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LOV);
1542 ma->ma_lmm_size = rc2;
1543 ma->ma_valid |= MA_LOV;
1544 } else if (rc2 == -ENODATA) {
1546 ma->ma_lmm_size = 0;
1547 } else if (rc2 == -ERANGE) {
1548 rc2 = echo_big_lmm_get(env, next, ma);
1550 GOTO(out, rc = rc2);
1552 GOTO(out, rc = rc2);
1557 #ifdef CONFIG_FS_POSIX_ACL
1558 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
1559 buf->lb_buf = ma->ma_acl;
1560 buf->lb_len = ma->ma_acl_size;
1561 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1563 ma->ma_acl_size = rc2;
1564 ma->ma_valid |= MA_ACL_DEF;
1565 } else if (rc2 == -ENODATA) {
1567 ma->ma_acl_size = 0;
1569 GOTO(out, rc = rc2);
1575 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
1576 rc, ma->ma_valid, ma->ma_lmm);
1581 echo_md_create_internal(const struct lu_env *env, struct echo_device *ed,
1582 struct md_object *parent, struct lu_fid *fid,
1583 struct lu_name *lname, struct md_op_spec *spec,
1586 struct lu_object *ec_child, *child;
1587 struct lu_device *ld = ed->ed_next;
1588 struct echo_thread_info *info = echo_env_info(env);
1589 struct lu_fid *fid2 = &info->eti_fid2;
1590 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
1595 rc = mdo_lookup(env, parent, lname, fid2, spec);
1598 else if (rc != -ENOENT)
1601 ec_child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev,
1603 if (IS_ERR(ec_child)) {
1604 CERROR("Can not find the child "DFID": rc = %ld\n", PFID(fid),
1606 RETURN(PTR_ERR(ec_child));
1609 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1610 if (child == NULL) {
1611 CERROR("Can not locate the child "DFID"\n", PFID(fid));
1612 GOTO(out_put, rc = -EINVAL);
1615 CDEBUG(D_RPCTRACE, "Start creating object "DFID" %s %p\n",
1616 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1619 * Do not perform lookup sanity check. We know that name does not exist.
1621 spec->sp_cr_lookup = 0;
1622 rc = mdo_create(env, parent, lname, lu2md(child), spec, ma);
1624 CERROR("Can not create child "DFID": rc = %d\n", PFID(fid), rc);
1627 CDEBUG(D_RPCTRACE, "End creating object "DFID" %s %p rc = %d\n",
1628 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent, rc);
1631 lu_object_put(env, ec_child);
1635 static int echo_set_lmm_size(const struct lu_env *env, struct lu_device *ld,
1638 struct echo_thread_info *info = echo_env_info(env);
1640 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
1641 ma->ma_lmm = (void *)&info->eti_lmm;
1642 ma->ma_lmm_size = sizeof(info->eti_lmm);
1644 LASSERT(info->eti_big_lmmsize);
1645 ma->ma_lmm = info->eti_big_lmm;
1646 ma->ma_lmm_size = info->eti_big_lmmsize;
1652 static int echo_create_md_object(const struct lu_env *env,
1653 struct echo_device *ed,
1654 struct lu_object *ec_parent,
1656 char *name, int namelen,
1657 __u64 id, __u32 mode, int count,
1658 int stripe_count, int stripe_offset)
1660 struct lu_object *parent;
1661 struct echo_thread_info *info = echo_env_info(env);
1662 struct lu_name *lname = &info->eti_lname;
1663 struct md_op_spec *spec = &info->eti_spec;
1664 struct md_attr *ma = &info->eti_ma;
1665 struct lu_device *ld = ed->ed_next;
1671 if (ec_parent == NULL)
1673 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1677 memset(ma, 0, sizeof(*ma));
1678 memset(spec, 0, sizeof(*spec));
1679 if (stripe_count != 0) {
1680 spec->sp_cr_flags |= FMODE_WRITE;
1681 echo_set_lmm_size(env, ld, ma);
1682 if (stripe_count != -1) {
1683 struct lov_user_md_v3 *lum = &info->eti_lum;
1685 lum->lmm_magic = LOV_USER_MAGIC_V3;
1686 lum->lmm_stripe_count = stripe_count;
1687 lum->lmm_stripe_offset = stripe_offset;
1688 lum->lmm_pattern = 0;
1689 spec->u.sp_ea.eadata = lum;
1690 spec->u.sp_ea.eadatalen = sizeof(*lum);
1691 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1695 ma->ma_attr.la_mode = mode;
1696 ma->ma_attr.la_valid = LA_CTIME | LA_MODE;
1697 ma->ma_attr.la_ctime = cfs_time_current_64();
1700 lname->ln_name = name;
1701 lname->ln_namelen = namelen;
1702 /* If name is specified, only create one object by name */
1703 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1708 /* Create multiple object sequenced by id */
1709 for (i = 0; i < count; i++) {
1710 char *tmp_name = info->eti_name;
1712 echo_md_build_name(lname, tmp_name, id);
1714 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1717 CERROR("Can not create child %s: rc = %d\n", tmp_name,
1728 static struct lu_object *echo_md_lookup(const struct lu_env *env,
1729 struct echo_device *ed,
1730 struct md_object *parent,
1731 struct lu_name *lname)
1733 struct echo_thread_info *info = echo_env_info(env);
1734 struct lu_fid *fid = &info->eti_fid;
1735 struct lu_object *child;
1739 CDEBUG(D_INFO, "lookup %s in parent "DFID" %p\n", lname->ln_name,
1741 rc = mdo_lookup(env, parent, lname, fid, NULL);
1743 CERROR("lookup %s: rc = %d\n", lname->ln_name, rc);
1744 RETURN(ERR_PTR(rc));
1747 /* In the function below, .hs_keycmp resolves to
1748 * lu_obj_hop_keycmp() */
1749 /* coverity[overrun-buffer-val] */
1750 child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1755 static int echo_setattr_object(const struct lu_env *env,
1756 struct echo_device *ed,
1757 struct lu_object *ec_parent,
1758 __u64 id, int count)
1760 struct lu_object *parent;
1761 struct echo_thread_info *info = echo_env_info(env);
1762 struct lu_name *lname = &info->eti_lname;
1763 char *name = info->eti_name;
1764 struct lu_device *ld = ed->ed_next;
1765 struct lu_buf *buf = &info->eti_buf;
1771 if (ec_parent == NULL)
1773 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1777 for (i = 0; i < count; i++) {
1778 struct lu_object *ec_child, *child;
1780 echo_md_build_name(lname, name, id);
1782 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1783 if (IS_ERR(ec_child)) {
1784 CERROR("Can't find child %s: rc = %ld\n",
1785 lname->ln_name, PTR_ERR(ec_child));
1786 RETURN(PTR_ERR(ec_child));
1789 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1790 if (child == NULL) {
1791 CERROR("Can not locate the child %s\n", lname->ln_name);
1792 lu_object_put(env, ec_child);
1797 CDEBUG(D_RPCTRACE, "Start setattr object "DFID"\n",
1798 PFID(lu_object_fid(child)));
1800 buf->lb_buf = info->eti_xattr_buf;
1801 buf->lb_len = sizeof(info->eti_xattr_buf);
1803 sprintf(name, "%s.test1", XATTR_USER_PREFIX);
1804 rc = mo_xattr_set(env, lu2md(child), buf, name,
1807 CERROR("Can not setattr child "DFID": rc = %d\n",
1808 PFID(lu_object_fid(child)), rc);
1809 lu_object_put(env, ec_child);
1812 CDEBUG(D_RPCTRACE, "End setattr object "DFID"\n",
1813 PFID(lu_object_fid(child)));
1815 lu_object_put(env, ec_child);
1820 static int echo_getattr_object(const struct lu_env *env,
1821 struct echo_device *ed,
1822 struct lu_object *ec_parent,
1823 __u64 id, int count)
1825 struct lu_object *parent;
1826 struct echo_thread_info *info = echo_env_info(env);
1827 struct lu_name *lname = &info->eti_lname;
1828 char *name = info->eti_name;
1829 struct md_attr *ma = &info->eti_ma;
1830 struct lu_device *ld = ed->ed_next;
1836 if (ec_parent == NULL)
1838 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1842 memset(ma, 0, sizeof(*ma));
1843 ma->ma_need |= MA_INODE | MA_LOV | MA_PFID | MA_HSM | MA_ACL_DEF;
1844 ma->ma_acl = info->eti_xattr_buf;
1845 ma->ma_acl_size = sizeof(info->eti_xattr_buf);
1847 for (i = 0; i < count; i++) {
1848 struct lu_object *ec_child, *child;
1851 echo_md_build_name(lname, name, id);
1852 echo_set_lmm_size(env, ld, ma);
1854 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1855 if (IS_ERR(ec_child)) {
1856 CERROR("Can't find child %s: rc = %ld\n",
1857 lname->ln_name, PTR_ERR(ec_child));
1858 RETURN(PTR_ERR(ec_child));
1861 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1862 if (child == NULL) {
1863 CERROR("Can not locate the child %s\n", lname->ln_name);
1864 lu_object_put(env, ec_child);
1868 CDEBUG(D_RPCTRACE, "Start getattr object "DFID"\n",
1869 PFID(lu_object_fid(child)));
1870 rc = echo_attr_get_complex(env, lu2md(child), ma);
1872 CERROR("Can not getattr child "DFID": rc = %d\n",
1873 PFID(lu_object_fid(child)), rc);
1874 lu_object_put(env, ec_child);
1877 CDEBUG(D_RPCTRACE, "End getattr object "DFID"\n",
1878 PFID(lu_object_fid(child)));
1880 lu_object_put(env, ec_child);
1886 static int echo_lookup_object(const struct lu_env *env,
1887 struct echo_device *ed,
1888 struct lu_object *ec_parent,
1889 __u64 id, int count)
1891 struct lu_object *parent;
1892 struct echo_thread_info *info = echo_env_info(env);
1893 struct lu_name *lname = &info->eti_lname;
1894 char *name = info->eti_name;
1895 struct lu_fid *fid = &info->eti_fid;
1896 struct lu_device *ld = ed->ed_next;
1900 if (ec_parent == NULL)
1902 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1906 /*prepare the requests*/
1907 for (i = 0; i < count; i++) {
1908 echo_md_build_name(lname, name, id);
1910 CDEBUG(D_RPCTRACE, "Start lookup object "DFID" %s %p\n",
1911 PFID(lu_object_fid(parent)), lname->ln_name, parent);
1913 rc = mdo_lookup(env, lu2md(parent), lname, fid, NULL);
1915 CERROR("Can not lookup child %s: rc = %d\n", name, rc);
1918 CDEBUG(D_RPCTRACE, "End lookup object "DFID" %s %p\n",
1919 PFID(lu_object_fid(parent)), lname->ln_name, parent);
1926 static int echo_md_destroy_internal(const struct lu_env *env,
1927 struct echo_device *ed,
1928 struct md_object *parent,
1929 struct lu_name *lname,
1932 struct lu_device *ld = ed->ed_next;
1933 struct lu_object *ec_child;
1934 struct lu_object *child;
1939 ec_child = echo_md_lookup(env, ed, parent, lname);
1940 if (IS_ERR(ec_child)) {
1941 CERROR("Can't find child %s: rc = %ld\n", lname->ln_name,
1943 RETURN(PTR_ERR(ec_child));
1946 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1947 if (child == NULL) {
1948 CERROR("Can not locate the child %s\n", lname->ln_name);
1949 GOTO(out_put, rc = -EINVAL);
1952 CDEBUG(D_RPCTRACE, "Start destroy object "DFID" %s %p\n",
1953 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1955 rc = mdo_unlink(env, parent, lu2md(child), lname, ma, 0);
1957 CERROR("Can not unlink child %s: rc = %d\n",
1958 lname->ln_name, rc);
1961 CDEBUG(D_RPCTRACE, "End destroy object "DFID" %s %p\n",
1962 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1964 lu_object_put(env, ec_child);
1968 static int echo_destroy_object(const struct lu_env *env,
1969 struct echo_device *ed,
1970 struct lu_object *ec_parent,
1971 char *name, int namelen,
1972 __u64 id, __u32 mode,
1975 struct echo_thread_info *info = echo_env_info(env);
1976 struct lu_name *lname = &info->eti_lname;
1977 struct md_attr *ma = &info->eti_ma;
1978 struct lu_device *ld = ed->ed_next;
1979 struct lu_object *parent;
1984 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1988 memset(ma, 0, sizeof(*ma));
1989 ma->ma_attr.la_mode = mode;
1990 ma->ma_attr.la_valid = LA_CTIME;
1991 ma->ma_attr.la_ctime = cfs_time_current_64();
1992 ma->ma_need = MA_INODE;
1996 lname->ln_name = name;
1997 lname->ln_namelen = namelen;
1998 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
2003 /*prepare the requests*/
2004 for (i = 0; i < count; i++) {
2005 char *tmp_name = info->eti_name;
2008 echo_md_build_name(lname, tmp_name, id);
2010 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
2013 CERROR("Can not unlink child %s: rc = %d\n", name, rc);
2022 static struct lu_object *echo_resolve_path(const struct lu_env *env,
2023 struct echo_device *ed, char *path,
2026 struct lu_device *ld = ed->ed_next;
2027 struct md_device *md = lu2md_dev(ld);
2028 struct echo_thread_info *info = echo_env_info(env);
2029 struct lu_fid *fid = &info->eti_fid;
2030 struct lu_name *lname = &info->eti_lname;
2031 struct lu_object *parent = NULL;
2032 struct lu_object *child = NULL;
2036 /*Only support MDD layer right now*/
2037 rc = md->md_ops->mdo_root_get(env, md, fid);
2039 CERROR("get root error: rc = %d\n", rc);
2040 RETURN(ERR_PTR(rc));
2043 /* In the function below, .hs_keycmp resolves to
2044 * lu_obj_hop_keycmp() */
2045 /* coverity[overrun-buffer-val] */
2046 parent = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
2047 if (IS_ERR(parent)) {
2048 CERROR("Can not find the parent "DFID": rc = %ld\n",
2049 PFID(fid), PTR_ERR(parent));
2054 struct lu_object *ld_parent;
2057 e = strsep(&path, "/");
2062 if (!path || path[0] == '\0')
2068 lname->ln_namelen = strlen(e);
2070 ld_parent = lu_object_locate(parent->lo_header, ld->ld_type);
2071 if (ld_parent == NULL) {
2072 lu_object_put(env, parent);
2077 child = echo_md_lookup(env, ed, lu2md(ld_parent), lname);
2078 lu_object_put(env, parent);
2079 if (IS_ERR(child)) {
2080 rc = (int)PTR_ERR(child);
2081 CERROR("lookup %s under parent "DFID": rc = %d\n",
2082 lname->ln_name, PFID(lu_object_fid(ld_parent)),
2089 RETURN(ERR_PTR(rc));
2094 static void echo_ucred_init(struct lu_env *env)
2096 struct lu_ucred *ucred = lu_ucred(env);
2098 ucred->uc_valid = UCRED_INVALID;
2100 ucred->uc_suppgids[0] = -1;
2101 ucred->uc_suppgids[1] = -1;
2103 ucred->uc_uid = ucred->uc_o_uid = cfs_curproc_uid();
2104 ucred->uc_gid = ucred->uc_o_gid = cfs_curproc_gid();
2105 ucred->uc_fsuid = ucred->uc_o_fsuid = cfs_curproc_fsuid();
2106 ucred->uc_fsgid = ucred->uc_o_fsgid = cfs_curproc_fsgid();
2107 ucred->uc_cap = cfs_curproc_cap_pack();
2109 /* remove fs privilege for non-root user. */
2110 if (ucred->uc_fsuid)
2111 ucred->uc_cap &= ~CFS_CAP_FS_MASK;
2112 ucred->uc_valid = UCRED_NEW;
2115 static void echo_ucred_fini(struct lu_env *env)
2117 struct lu_ucred *ucred = lu_ucred(env);
2118 ucred->uc_valid = UCRED_INIT;
2121 #define ECHO_MD_CTX_TAG (LCT_REMEMBER | LCT_MD_THREAD)
2122 #define ECHO_MD_SES_TAG (LCT_REMEMBER | LCT_SESSION)
2123 static int echo_md_handler(struct echo_device *ed, int command,
2124 char *path, int path_len, __u64 id, int count,
2125 struct obd_ioctl_data *data)
2127 struct echo_thread_info *info;
2128 struct lu_device *ld = ed->ed_next;
2131 struct lu_object *parent;
2133 int namelen = data->ioc_plen2;
2138 CERROR("MD echo client is not being initialized properly\n");
2142 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
2143 CERROR("Only support MDD layer right now!\n");
2147 env = cl_env_get(&refcheck);
2149 RETURN(PTR_ERR(env));
2151 rc = lu_env_refill_by_tags(env, ECHO_MD_CTX_TAG, ECHO_MD_SES_TAG);
2155 /* init big_lmm buffer */
2156 info = echo_env_info(env);
2157 LASSERT(info->eti_big_lmm == NULL);
2158 OBD_ALLOC_LARGE(info->eti_big_lmm, MIN_MD_SIZE);
2159 if (info->eti_big_lmm == NULL)
2160 GOTO(out_env, rc = -ENOMEM);
2161 info->eti_big_lmmsize = MIN_MD_SIZE;
2163 parent = echo_resolve_path(env, ed, path, path_len);
2164 if (IS_ERR(parent)) {
2165 CERROR("Can not resolve the path %s: rc = %ld\n", path,
2167 GOTO(out_free, rc = PTR_ERR(parent));
2171 OBD_ALLOC(name, namelen + 1);
2173 GOTO(out_put, rc = -ENOMEM);
2174 if (cfs_copy_from_user(name, data->ioc_pbuf2, namelen))
2175 GOTO(out_name, rc = -EFAULT);
2178 echo_ucred_init(env);
2181 case ECHO_MD_CREATE:
2182 case ECHO_MD_MKDIR: {
2183 struct echo_thread_info *info = echo_env_info(env);
2184 __u32 mode = data->ioc_obdo2.o_mode;
2185 struct lu_fid *fid = &info->eti_fid;
2186 int stripe_count = (int)data->ioc_obdo2.o_misc;
2187 int stripe_index = (int)data->ioc_obdo2.o_stripe_idx;
2189 rc = ostid_to_fid(fid, &data->ioc_obdo1.o_oi, 0);
2193 /* In the function below, .hs_keycmp resolves to
2194 * lu_obj_hop_keycmp() */
2195 /* coverity[overrun-buffer-val] */
2196 rc = echo_create_md_object(env, ed, parent, fid, name, namelen,
2197 id, mode, count, stripe_count,
2201 case ECHO_MD_DESTROY:
2202 case ECHO_MD_RMDIR: {
2203 __u32 mode = data->ioc_obdo2.o_mode;
2205 rc = echo_destroy_object(env, ed, parent, name, namelen,
2209 case ECHO_MD_LOOKUP:
2210 rc = echo_lookup_object(env, ed, parent, id, count);
2212 case ECHO_MD_GETATTR:
2213 rc = echo_getattr_object(env, ed, parent, id, count);
2215 case ECHO_MD_SETATTR:
2216 rc = echo_setattr_object(env, ed, parent, id, count);
2219 CERROR("unknown command %d\n", command);
2223 echo_ucred_fini(env);
2227 OBD_FREE(name, namelen + 1);
2229 lu_object_put(env, parent);
2231 LASSERT(info->eti_big_lmm);
2232 OBD_FREE_LARGE(info->eti_big_lmm, info->eti_big_lmmsize);
2233 info->eti_big_lmm = NULL;
2234 info->eti_big_lmmsize = 0;
2236 cl_env_put(env, &refcheck);
2240 static int echo_create_object(const struct lu_env *env, struct echo_device *ed,
2241 int on_target, struct obdo *oa, void *ulsm,
2242 int ulsm_nob, struct obd_trans_info *oti)
2244 struct echo_object *eco;
2245 struct echo_client_obd *ec = ed->ed_ec;
2246 struct lov_stripe_md *lsm = NULL;
2251 if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
2252 (on_target || /* set_stripe */
2253 ec->ec_nstripes != 0)) { /* LOV */
2254 CERROR ("No valid oid\n");
2258 rc = echo_alloc_memmd(ed, &lsm);
2260 CERROR("Cannot allocate md: rc = %d\n", rc);
2267 rc = echo_copyin_lsm (ed, lsm, ulsm, ulsm_nob);
2271 if (lsm->lsm_stripe_count == 0)
2272 lsm->lsm_stripe_count = ec->ec_nstripes;
2274 if (lsm->lsm_stripe_size == 0)
2275 lsm->lsm_stripe_size = CFS_PAGE_SIZE;
2279 /* setup stripes: indices + default ids if required */
2280 for (i = 0; i < lsm->lsm_stripe_count; i++) {
2281 if (ostid_id(&lsm->lsm_oinfo[i]->loi_oi) == 0)
2282 lsm->lsm_oinfo[i]->loi_oi = lsm->lsm_oi;
2284 lsm->lsm_oinfo[i]->loi_ost_idx =
2285 (idx + i) % ec->ec_nstripes;
2289 /* setup object ID here for !on_target and LOV hint */
2290 if (oa->o_valid & OBD_MD_FLID) {
2291 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2292 lsm->lsm_oi = oa->o_oi;
2295 if (ostid_id(&lsm->lsm_oi) == 0)
2296 ostid_set_id(&lsm->lsm_oi, ++last_object_id);
2300 /* Only echo objects are allowed to be created */
2301 LASSERT((oa->o_valid & OBD_MD_FLGROUP) &&
2302 (ostid_seq(&oa->o_oi) == FID_SEQ_ECHO));
2303 rc = obd_create(env, ec->ec_exp, oa, &lsm, oti);
2305 CERROR("Cannot create objects: rc = %d\n", rc);
2311 /* See what object ID we were given */
2312 oa->o_oi = lsm->lsm_oi;
2313 oa->o_valid |= OBD_MD_FLID;
2315 eco = cl_echo_object_find(ed, &lsm);
2317 GOTO(failed, rc = PTR_ERR(eco));
2318 cl_echo_object_put(eco);
2320 CDEBUG(D_INFO, "oa oid "DOSTID"\n", POSTID(&oa->o_oi));
2325 obd_destroy(env, ec->ec_exp, oa, lsm, oti, NULL, NULL);
2327 echo_free_memmd(ed, &lsm);
2329 CERROR("create object failed with: rc = %d\n", rc);
2333 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
2336 struct lov_stripe_md *lsm = NULL;
2337 struct echo_object *eco;
2341 if ((oa->o_valid & OBD_MD_FLID) == 0 || ostid_id(&oa->o_oi) == 0) {
2342 /* disallow use of object id 0 */
2343 CERROR ("No valid oid\n");
2347 rc = echo_alloc_memmd(ed, &lsm);
2351 lsm->lsm_oi = oa->o_oi;
2352 if (!(oa->o_valid & OBD_MD_FLGROUP))
2353 ostid_set_seq_echo(&lsm->lsm_oi);
2356 eco = cl_echo_object_find(ed, &lsm);
2362 echo_free_memmd(ed, &lsm);
2366 static void echo_put_object(struct echo_object *eco)
2368 if (cl_echo_object_put(eco))
2369 CERROR("echo client: drop an object failed");
2373 echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
2375 unsigned long stripe_count;
2376 unsigned long stripe_size;
2377 unsigned long width;
2378 unsigned long woffset;
2382 if (lsm->lsm_stripe_count <= 1)
2386 stripe_size = lsm->lsm_stripe_size;
2387 stripe_count = lsm->lsm_stripe_count;
2389 /* width = # bytes in all stripes */
2390 width = stripe_size * stripe_count;
2392 /* woffset = offset within a width; offset = whole number of widths */
2393 woffset = do_div (offset, width);
2395 stripe_index = woffset / stripe_size;
2397 *idp = ostid_id(&lsm->lsm_oinfo[stripe_index]->loi_oi);
2398 *offp = offset * stripe_size + woffset % stripe_size;
2402 echo_client_page_debug_setup(struct lov_stripe_md *lsm,
2403 cfs_page_t *page, int rw, obd_id id,
2404 obd_off offset, obd_off count)
2411 /* no partial pages on the client */
2412 LASSERT(count == CFS_PAGE_SIZE);
2414 addr = cfs_kmap(page);
2416 for (delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2417 if (rw == OBD_BRW_WRITE) {
2418 stripe_off = offset + delta;
2420 echo_get_stripe_off_id(lsm, &stripe_off, &stripe_id);
2422 stripe_off = 0xdeadbeef00c0ffeeULL;
2423 stripe_id = 0xdeadbeef00c0ffeeULL;
2425 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
2426 stripe_off, stripe_id);
2432 static int echo_client_page_debug_check(struct lov_stripe_md *lsm,
2433 cfs_page_t *page, obd_id id,
2434 obd_off offset, obd_off count)
2443 /* no partial pages on the client */
2444 LASSERT(count == CFS_PAGE_SIZE);
2446 addr = cfs_kmap(page);
2448 for (rc = delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2449 stripe_off = offset + delta;
2451 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
2453 rc2 = block_debug_check("test_brw",
2454 addr + delta, OBD_ECHO_BLOCK_SIZE,
2455 stripe_off, stripe_id);
2457 CERROR ("Error in echo object "LPX64"\n", id);
2466 static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
2467 struct echo_object *eco, obd_off offset,
2468 obd_size count, int async,
2469 struct obd_trans_info *oti)
2471 struct lov_stripe_md *lsm = eco->eo_lsm;
2473 struct brw_page *pga;
2474 struct brw_page *pgp;
2484 verify = (ostid_id(&oa->o_oi) != ECHO_PERSISTENT_OBJID &&
2485 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
2486 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
2488 gfp_mask = ((ostid_id(&oa->o_oi) & 2) == 0) ? CFS_ALLOC_STD : CFS_ALLOC_HIGHUSER;
2490 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
2491 LASSERT(lsm != NULL);
2492 LASSERT(ostid_id(&lsm->lsm_oi) == ostid_id(&oa->o_oi));
2495 (count & (~CFS_PAGE_MASK)) != 0)
2498 /* XXX think again with misaligned I/O */
2499 npages = count >> CFS_PAGE_SHIFT;
2501 if (rw == OBD_BRW_WRITE)
2502 brw_flags = OBD_BRW_ASYNC;
2504 OBD_ALLOC(pga, npages * sizeof(*pga));
2508 OBD_ALLOC(pages, npages * sizeof(*pages));
2509 if (pages == NULL) {
2510 OBD_FREE(pga, npages * sizeof(*pga));
2514 for (i = 0, pgp = pga, off = offset;
2516 i++, pgp++, off += CFS_PAGE_SIZE) {
2518 LASSERT (pgp->pg == NULL); /* for cleanup */
2521 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
2522 if (pgp->pg == NULL)
2526 pgp->count = CFS_PAGE_SIZE;
2528 pgp->flag = brw_flags;
2531 echo_client_page_debug_setup(lsm, pgp->pg, rw,
2532 ostid_id(&oa->o_oi), off,
2536 /* brw mode can only be used at client */
2537 LASSERT(ed->ed_next != NULL);
2538 rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
2541 if (rc != 0 || rw != OBD_BRW_READ)
2544 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
2545 if (pgp->pg == NULL)
2550 vrc = echo_client_page_debug_check(lsm, pgp->pg,
2551 ostid_id(&oa->o_oi),
2552 pgp->off, pgp->count);
2553 if (vrc != 0 && rc == 0)
2556 OBD_PAGE_FREE(pgp->pg);
2558 OBD_FREE(pga, npages * sizeof(*pga));
2559 OBD_FREE(pages, npages * sizeof(*pages));
2563 static int echo_client_prep_commit(const struct lu_env *env,
2564 struct obd_export *exp, int rw,
2565 struct obdo *oa, struct echo_object *eco,
2566 obd_off offset, obd_size count,
2567 obd_size batch, struct obd_trans_info *oti,
2570 struct lov_stripe_md *lsm = eco->eo_lsm;
2571 struct obd_ioobj ioo;
2572 struct niobuf_local *lnb;
2573 struct niobuf_remote *rnb;
2575 obd_size npages, tot_pages;
2576 int i, ret = 0, brw_flags = 0;
2580 if (count <= 0 || (count & (~CFS_PAGE_MASK)) != 0 ||
2581 (lsm != NULL && ostid_id(&lsm->lsm_oi) != ostid_id(&oa->o_oi)))
2584 npages = batch >> CFS_PAGE_SHIFT;
2585 tot_pages = count >> CFS_PAGE_SHIFT;
2587 OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
2588 OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
2590 if (lnb == NULL || rnb == NULL)
2591 GOTO(out, ret = -ENOMEM);
2593 if (rw == OBD_BRW_WRITE && async)
2594 brw_flags |= OBD_BRW_ASYNC;
2596 obdo_to_ioobj(oa, &ioo);
2600 for(; tot_pages; tot_pages -= npages) {
2603 if (tot_pages < npages)
2606 for (i = 0; i < npages; i++, off += CFS_PAGE_SIZE) {
2607 rnb[i].offset = off;
2608 rnb[i].len = CFS_PAGE_SIZE;
2609 rnb[i].flags = brw_flags;
2612 ioo.ioo_bufcnt = npages;
2613 oti->oti_transno = 0;
2616 ret = obd_preprw(env, rw, exp, oa, 1, &ioo, rnb, &lpages,
2620 LASSERT(lpages == npages);
2622 for (i = 0; i < lpages; i++) {
2623 cfs_page_t *page = lnb[i].page;
2625 /* read past eof? */
2626 if (page == NULL && lnb[i].rc == 0)
2630 lnb[i].flags |= OBD_BRW_ASYNC;
2632 if (ostid_id(&oa->o_oi) == ECHO_PERSISTENT_OBJID ||
2633 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
2634 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
2637 if (rw == OBD_BRW_WRITE)
2638 echo_client_page_debug_setup(lsm, page, rw,
2639 ostid_id(&oa->o_oi),
2643 echo_client_page_debug_check(lsm, page,
2644 ostid_id(&oa->o_oi),
2649 ret = obd_commitrw(env, rw, exp, oa, 1, &ioo,
2650 rnb, npages, lnb, oti, ret);
2654 /* Reset oti otherwise it would confuse ldiskfs. */
2655 memset(oti, 0, sizeof(*oti));
2660 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
2662 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
2666 static int echo_client_brw_ioctl(const struct lu_env *env, int rw,
2667 struct obd_export *exp,
2668 struct obd_ioctl_data *data,
2669 struct obd_trans_info *dummy_oti)
2671 struct obd_device *obd = class_exp2obd(exp);
2672 struct echo_device *ed = obd2echo_dev(obd);
2673 struct echo_client_obd *ec = ed->ed_ec;
2674 struct obdo *oa = &data->ioc_obdo1;
2675 struct echo_object *eco;
2681 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2683 rc = echo_get_object(&eco, ed, oa);
2687 oa->o_valid &= ~OBD_MD_FLHANDLE;
2689 /* OFD/obdfilter works only via prep/commit */
2690 test_mode = (long)data->ioc_pbuf1;
2694 if (ed->ed_next == NULL && test_mode != 3) {
2696 data->ioc_plen1 = data->ioc_count;
2699 /* Truncate batch size to maximum */
2700 if (data->ioc_plen1 > PTLRPC_MAX_BRW_SIZE)
2701 data->ioc_plen1 = PTLRPC_MAX_BRW_SIZE;
2703 switch (test_mode) {
2707 rc = echo_client_kbrw(ed, rw, oa,
2708 eco, data->ioc_offset,
2709 data->ioc_count, async, dummy_oti);
2712 rc = echo_client_prep_commit(env, ec->ec_exp, rw, oa,
2713 eco, data->ioc_offset,
2714 data->ioc_count, data->ioc_plen1,
2720 echo_put_object(eco);
2725 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
2726 int mode, obd_off offset, obd_size nob)
2728 struct echo_device *ed = obd2echo_dev(exp->exp_obd);
2729 struct lustre_handle *ulh = &oa->o_handle;
2730 struct echo_object *eco;
2735 if (ed->ed_next == NULL)
2736 RETURN(-EOPNOTSUPP);
2738 if (!(mode == LCK_PR || mode == LCK_PW))
2741 if ((offset & (~CFS_PAGE_MASK)) != 0 ||
2742 (nob & (~CFS_PAGE_MASK)) != 0)
2745 rc = echo_get_object (&eco, ed, oa);
2749 end = (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
2750 rc = cl_echo_enqueue(eco, offset, end, mode, &ulh->cookie);
2752 oa->o_valid |= OBD_MD_FLHANDLE;
2753 CDEBUG(D_INFO, "Cookie is "LPX64"\n", ulh->cookie);
2755 echo_put_object(eco);
2760 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
2762 struct echo_device *ed = obd2echo_dev(exp->exp_obd);
2763 __u64 cookie = oa->o_handle.cookie;
2765 if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
2768 CDEBUG(D_INFO, "Cookie is "LPX64"\n", cookie);
2769 return cl_echo_cancel(ed, cookie);
2773 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2774 void *karg, void *uarg)
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;
2791 memset(&dummy_oti, 0, sizeof(dummy_oti));
2793 oa = &data->ioc_obdo1;
2794 if (!(oa->o_valid & OBD_MD_FLGROUP)) {
2795 oa->o_valid |= OBD_MD_FLGROUP;
2796 ostid_set_seq_echo(&oa->o_oi);
2799 /* This FID is unpacked just for validation at this point */
2800 rc = ostid_to_fid(&fid, &oa->o_oi, 0);
2808 rc = lu_env_init(env, LCT_DT_THREAD);
2810 GOTO(out, rc = -ENOMEM);
2813 case OBD_IOC_CREATE: /* may create echo object */
2814 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2815 GOTO (out, rc = -EPERM);
2817 rc = echo_create_object(env, ed, 1, oa, data->ioc_pbuf1,
2818 data->ioc_plen1, &dummy_oti);
2821 case OBD_IOC_ECHO_MD: {
2828 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2829 GOTO(out, rc = -EPERM);
2831 count = data->ioc_count;
2832 cmd = data->ioc_command;
2834 id = ostid_id(&data->ioc_obdo2.o_oi);
2836 dirlen = data->ioc_plen1;
2837 OBD_ALLOC(dir, dirlen + 1);
2839 GOTO(out, rc = -ENOMEM);
2841 if (cfs_copy_from_user(dir, data->ioc_pbuf1, dirlen)) {
2842 OBD_FREE(dir, data->ioc_plen1 + 1);
2843 GOTO(out, rc = -EFAULT);
2846 rc = echo_md_handler(ed, cmd, dir, dirlen, id, count, data);
2847 OBD_FREE(dir, dirlen + 1);
2850 case OBD_IOC_ECHO_ALLOC_SEQ: {
2851 struct lu_env *cl_env;
2856 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2857 GOTO(out, rc = -EPERM);
2859 cl_env = cl_env_get(&refcheck);
2861 GOTO(out, rc = PTR_ERR(cl_env));
2863 rc = lu_env_refill_by_tags(cl_env, ECHO_MD_CTX_TAG,
2866 cl_env_put(cl_env, &refcheck);
2870 rc = seq_client_get_seq(cl_env, ed->ed_cl_seq, &seq);
2871 cl_env_put(cl_env, &refcheck);
2873 CERROR("%s: Can not alloc seq: rc = %d\n",
2878 if (cfs_copy_to_user(data->ioc_pbuf1, &seq, data->ioc_plen1))
2881 max_count = LUSTRE_METADATA_SEQ_MAX_WIDTH;
2882 if (cfs_copy_to_user(data->ioc_pbuf2, &max_count,
2887 case OBD_IOC_DESTROY:
2888 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2889 GOTO (out, rc = -EPERM);
2891 rc = echo_get_object(&eco, ed, oa);
2893 rc = obd_destroy(env, ec->ec_exp, oa, eco->eo_lsm,
2894 &dummy_oti, NULL, NULL);
2896 eco->eo_deleted = 1;
2897 echo_put_object(eco);
2901 case OBD_IOC_GETATTR:
2902 rc = echo_get_object(&eco, ed, oa);
2904 struct obd_info oinfo = { { { 0 } } };
2905 oinfo.oi_md = eco->eo_lsm;
2907 rc = obd_getattr(env, ec->ec_exp, &oinfo);
2908 echo_put_object(eco);
2912 case OBD_IOC_SETATTR:
2913 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2914 GOTO (out, rc = -EPERM);
2916 rc = echo_get_object(&eco, ed, oa);
2918 struct obd_info oinfo = { { { 0 } } };
2920 oinfo.oi_md = eco->eo_lsm;
2922 rc = obd_setattr(env, ec->ec_exp, &oinfo, NULL);
2923 echo_put_object(eco);
2927 case OBD_IOC_BRW_WRITE:
2928 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2929 GOTO (out, rc = -EPERM);
2933 case OBD_IOC_BRW_READ:
2934 rc = echo_client_brw_ioctl(env, rw, exp, data, &dummy_oti);
2937 case ECHO_IOC_GET_STRIPE:
2938 rc = echo_get_object(&eco, ed, oa);
2940 rc = echo_copyout_lsm(eco->eo_lsm, data->ioc_pbuf1,
2942 echo_put_object(eco);
2946 case ECHO_IOC_SET_STRIPE:
2947 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2948 GOTO (out, rc = -EPERM);
2950 if (data->ioc_pbuf1 == NULL) { /* unset */
2951 rc = echo_get_object(&eco, ed, oa);
2953 eco->eo_deleted = 1;
2954 echo_put_object(eco);
2957 rc = echo_create_object(env, ed, 0, oa,
2959 data->ioc_plen1, &dummy_oti);
2963 case ECHO_IOC_ENQUEUE:
2964 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2965 GOTO (out, rc = -EPERM);
2967 rc = echo_client_enqueue(exp, oa,
2968 data->ioc_conn1, /* lock mode */
2970 data->ioc_count);/*extent*/
2973 case ECHO_IOC_CANCEL:
2974 rc = echo_client_cancel(exp, oa);
2978 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
2979 GOTO (out, rc = -ENOTTY);
2987 /* XXX this should be in a helper also called by target_send_reply */
2988 for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
2990 if (!ack_lock->mode)
2992 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
2998 static int echo_client_setup(const struct lu_env *env,
2999 struct obd_device *obddev, struct lustre_cfg *lcfg)
3001 struct echo_client_obd *ec = &obddev->u.echo_client;
3002 struct obd_device *tgt;
3003 struct obd_uuid echo_uuid = { "ECHO_UUID" };
3004 struct obd_connect_data *ocd = NULL;
3008 if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
3009 CERROR("requires a TARGET OBD name\n");
3013 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
3014 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
3015 CERROR("device not attached or not set up (%s)\n",
3016 lustre_cfg_string(lcfg, 1));
3020 spin_lock_init(&ec->ec_lock);
3021 CFS_INIT_LIST_HEAD (&ec->ec_objects);
3022 CFS_INIT_LIST_HEAD (&ec->ec_locks);
3024 ec->ec_nstripes = 0;
3026 if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
3027 lu_context_tags_update(ECHO_MD_CTX_TAG);
3028 lu_session_tags_update(ECHO_MD_SES_TAG);
3032 OBD_ALLOC(ocd, sizeof(*ocd));
3034 CERROR("Can't alloc ocd connecting to %s\n",
3035 lustre_cfg_string(lcfg, 1));
3039 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
3040 OBD_CONNECT_BRW_SIZE |
3041 OBD_CONNECT_GRANT | OBD_CONNECT_FULL20 |
3042 OBD_CONNECT_64BITHASH | OBD_CONNECT_LVB_TYPE;
3043 ocd->ocd_brw_size = DT_MAX_BRW_SIZE;
3044 ocd->ocd_version = LUSTRE_VERSION_CODE;
3045 ocd->ocd_group = FID_SEQ_ECHO;
3047 rc = obd_connect(env, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
3049 /* Turn off pinger because it connects to tgt obd directly. */
3050 spin_lock(&tgt->obd_dev_lock);
3051 cfs_list_del_init(&ec->ec_exp->exp_obd_chain_timed);
3052 spin_unlock(&tgt->obd_dev_lock);
3055 OBD_FREE(ocd, sizeof(*ocd));
3058 CERROR("fail to connect to device %s\n",
3059 lustre_cfg_string(lcfg, 1));
3066 static int echo_client_cleanup(struct obd_device *obddev)
3068 struct echo_device *ed = obd2echo_dev(obddev);
3069 struct echo_client_obd *ec = &obddev->u.echo_client;
3073 /*Do nothing for Metadata echo client*/
3077 if (ed->ed_next_ismd) {
3078 lu_context_tags_clear(ECHO_MD_CTX_TAG);
3079 lu_session_tags_clear(ECHO_MD_SES_TAG);
3083 if (!cfs_list_empty(&obddev->obd_exports)) {
3084 CERROR("still has clients!\n");
3088 LASSERT(cfs_atomic_read(&ec->ec_exp->exp_refcount) > 0);
3089 rc = obd_disconnect(ec->ec_exp);
3091 CERROR("fail to disconnect device: %d\n", rc);
3096 static int echo_client_connect(const struct lu_env *env,
3097 struct obd_export **exp,
3098 struct obd_device *src, struct obd_uuid *cluuid,
3099 struct obd_connect_data *data, void *localdata)
3102 struct lustre_handle conn = { 0 };
3105 rc = class_connect(&conn, src, cluuid);
3107 *exp = class_conn2export(&conn);
3113 static int echo_client_disconnect(struct obd_export *exp)
3116 struct obd_device *obd;
3117 struct echo_client_obd *ec;
3118 struct ec_lock *ecl;
3124 GOTO(out, rc = -EINVAL);
3128 ec = &obd->u.echo_client;
3130 /* no more contention on export's lock list */
3131 while (!cfs_list_empty (&exp->exp_ec_data.eced_locks)) {
3132 ecl = cfs_list_entry (exp->exp_ec_data.eced_locks.next,
3133 struct ec_lock, ecl_exp_chain);
3134 cfs_list_del (&ecl->ecl_exp_chain);
3136 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm,
3137 ecl->ecl_mode, &ecl->ecl_lock_handle);
3139 CDEBUG (D_INFO, "Cancel lock on object "LPX64" on disconnect "
3140 "(%d)\n", ecl->ecl_object->eco_id, rc);
3142 echo_put_object (ecl->ecl_object);
3143 OBD_FREE (ecl, sizeof (*ecl));
3147 rc = class_disconnect(exp);
3153 static struct obd_ops echo_client_obd_ops = {
3154 .o_owner = THIS_MODULE,
3157 .o_setup = echo_client_setup,
3158 .o_cleanup = echo_client_cleanup,
3161 .o_iocontrol = echo_client_iocontrol,
3162 .o_connect = echo_client_connect,
3163 .o_disconnect = echo_client_disconnect
3166 int echo_client_init(void)
3168 struct lprocfs_static_vars lvars = { 0 };
3171 lprocfs_echo_init_vars(&lvars);
3173 rc = lu_kmem_init(echo_caches);
3175 rc = class_register_type(&echo_client_obd_ops, NULL,
3177 LUSTRE_ECHO_CLIENT_NAME,
3180 lu_kmem_fini(echo_caches);
3185 void echo_client_exit(void)
3187 class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
3188 lu_kmem_fini(echo_caches);
3192 static int __init obdecho_init(void)
3194 struct lprocfs_static_vars lvars;
3198 LCONSOLE_INFO("Echo OBD driver; http://www.lustre.org/\n");
3200 LASSERT(CFS_PAGE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
3202 lprocfs_echo_init_vars(&lvars);
3204 # ifdef HAVE_SERVER_SUPPORT
3205 rc = echo_persistent_pages_init();
3209 rc = class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
3210 LUSTRE_ECHO_NAME, NULL);
3215 rc = echo_client_init();
3217 # ifdef HAVE_SERVER_SUPPORT
3221 class_unregister_type(LUSTRE_ECHO_NAME);
3223 echo_persistent_pages_fini();
3229 static void /*__exit*/ obdecho_exit(void)
3233 # ifdef HAVE_SERVER_SUPPORT
3234 class_unregister_type(LUSTRE_ECHO_NAME);
3235 echo_persistent_pages_fini();
3239 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3240 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
3241 MODULE_LICENSE("GPL");
3243 cfs_module(obdecho, LUSTRE_VERSION_STRING, obdecho_init, obdecho_exit);
3244 #endif /* __KERNEL__ */
3246 /** @} echo_client */