1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011, 2012, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
39 #define DEBUG_SUBSYSTEM S_ECHO
41 #include <libcfs/libcfs.h>
43 #include <liblustre.h>
47 #include <obd_support.h>
48 #include <obd_class.h>
49 #include <lustre_debug.h>
50 #include <lprocfs_status.h>
51 #include <cl_object.h>
52 #include <lustre_fid.h>
53 #include <lustre_acl.h>
54 #include <lustre_net.h>
56 #include "echo_internal.h"
58 /** \defgroup echo_client Echo Client
63 struct cl_device ed_cl;
64 struct echo_client_obd *ed_ec;
66 struct cl_site ed_site_myself;
67 struct cl_site *ed_site;
68 struct lu_device *ed_next;
71 struct lu_client_seq *ed_cl_seq;
75 struct cl_object eo_cl;
76 struct cl_object_header eo_hdr;
78 struct echo_device *eo_dev;
79 cfs_list_t eo_obj_chain;
80 struct lov_stripe_md *eo_lsm;
81 cfs_atomic_t eo_npages;
85 struct echo_object_conf {
86 struct cl_object_conf eoc_cl;
87 struct lov_stripe_md **eoc_md;
91 struct cl_page_slice ep_cl;
93 cfs_page_t *ep_vmpage;
97 struct cl_lock_slice el_cl;
99 struct echo_object *el_object;
101 cfs_atomic_t el_refcount;
105 struct cl_io_slice ei_cl;
110 struct cl_req_slice er_cl;
114 static int echo_client_setup(const struct lu_env *env,
115 struct obd_device *obddev,
116 struct lustre_cfg *lcfg);
117 static int echo_client_cleanup(struct obd_device *obddev);
120 /** \defgroup echo_helpers Helper functions
123 static inline struct echo_device *cl2echo_dev(const struct cl_device *dev)
125 return container_of0(dev, struct echo_device, ed_cl);
128 static inline struct cl_device *echo_dev2cl(struct echo_device *d)
133 static inline struct echo_device *obd2echo_dev(const struct obd_device *obd)
135 return cl2echo_dev(lu2cl_dev(obd->obd_lu_dev));
138 static inline struct cl_object *echo_obj2cl(struct echo_object *eco)
143 static inline struct echo_object *cl2echo_obj(const struct cl_object *o)
145 return container_of(o, struct echo_object, eo_cl);
148 static inline struct echo_page *cl2echo_page(const struct cl_page_slice *s)
150 return container_of(s, struct echo_page, ep_cl);
153 static inline struct echo_lock *cl2echo_lock(const struct cl_lock_slice *s)
155 return container_of(s, struct echo_lock, el_cl);
158 static inline struct cl_lock *echo_lock2cl(const struct echo_lock *ecl)
160 return ecl->el_cl.cls_lock;
163 static struct lu_context_key echo_thread_key;
164 static inline struct echo_thread_info *echo_env_info(const struct lu_env *env)
166 struct echo_thread_info *info;
167 info = lu_context_key_get(&env->le_ctx, &echo_thread_key);
168 LASSERT(info != NULL);
173 struct echo_object_conf *cl2echo_conf(const struct cl_object_conf *c)
175 return container_of(c, struct echo_object_conf, eoc_cl);
178 static inline void lsm2fid(struct lov_stripe_md *lsm, struct lu_fid *fid)
181 fid->f_seq = FID_SEQ_ECHO;
182 /* truncated to 32 bits by assignment */
183 fid->f_oid = lsm->lsm_object_id;
184 fid->f_ver = lsm->lsm_object_id >> 32;
186 /** @} echo_helpers */
188 static struct echo_object *cl_echo_object_find(struct echo_device *d,
189 struct lov_stripe_md **lsm);
190 static int cl_echo_object_put(struct echo_object *eco);
191 static int cl_echo_enqueue (struct echo_object *eco, obd_off start,
192 obd_off end, int mode, __u64 *cookie);
193 static int cl_echo_cancel (struct echo_device *d, __u64 cookie);
194 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
195 cfs_page_t **pages, int npages, int async);
197 static struct echo_thread_info *echo_env_info(const struct lu_env *env);
199 struct echo_thread_info {
200 struct echo_object_conf eti_conf;
201 struct lustre_md eti_md;
203 struct cl_2queue eti_queue;
205 struct cl_lock_descr eti_descr;
206 struct lu_fid eti_fid;
207 struct md_op_spec eti_spec;
208 struct lov_mds_md_v3 eti_lmm;
209 struct lov_user_md_v3 eti_lum;
210 struct md_attr eti_ma;
211 struct lu_name eti_lname;
213 struct lu_buf eti_buf;
214 char eti_xattr_buf[LUSTRE_POSIX_ACL_MAX_SIZE];
217 /* No session used right now */
218 struct echo_session_info {
222 static cfs_mem_cache_t *echo_page_kmem;
223 static cfs_mem_cache_t *echo_lock_kmem;
224 static cfs_mem_cache_t *echo_object_kmem;
225 static cfs_mem_cache_t *echo_thread_kmem;
226 static cfs_mem_cache_t *echo_session_kmem;
227 //static cfs_mem_cache_t *echo_req_kmem;
229 static struct lu_kmem_descr echo_caches[] = {
231 .ckd_cache = &echo_page_kmem,
232 .ckd_name = "echo_page_kmem",
233 .ckd_size = sizeof (struct echo_page)
236 .ckd_cache = &echo_lock_kmem,
237 .ckd_name = "echo_lock_kmem",
238 .ckd_size = sizeof (struct echo_lock)
241 .ckd_cache = &echo_object_kmem,
242 .ckd_name = "echo_object_kmem",
243 .ckd_size = sizeof (struct echo_object)
246 .ckd_cache = &echo_thread_kmem,
247 .ckd_name = "echo_thread_kmem",
248 .ckd_size = sizeof (struct echo_thread_info)
251 .ckd_cache = &echo_session_kmem,
252 .ckd_name = "echo_session_kmem",
253 .ckd_size = sizeof (struct echo_session_info)
257 .ckd_cache = &echo_req_kmem,
258 .ckd_name = "echo_req_kmem",
259 .ckd_size = sizeof (struct echo_req)
267 /** \defgroup echo_page Page operations
269 * Echo page operations.
273 cfs_page_t *echo_page_vmpage(const struct lu_env *env,
274 const struct cl_page_slice *slice)
276 return cl2echo_page(slice)->ep_vmpage;
279 static int echo_page_own(const struct lu_env *env,
280 const struct cl_page_slice *slice,
281 struct cl_io *io, int nonblock)
283 struct echo_page *ep = cl2echo_page(slice);
286 cfs_mutex_lock(&ep->ep_lock);
287 else if (!cfs_mutex_trylock(&ep->ep_lock))
292 static void echo_page_disown(const struct lu_env *env,
293 const struct cl_page_slice *slice,
296 struct echo_page *ep = cl2echo_page(slice);
298 LASSERT(cfs_mutex_is_locked(&ep->ep_lock));
299 cfs_mutex_unlock(&ep->ep_lock);
302 static void echo_page_discard(const struct lu_env *env,
303 const struct cl_page_slice *slice,
304 struct cl_io *unused)
306 cl_page_delete(env, slice->cpl_page);
309 static int echo_page_is_vmlocked(const struct lu_env *env,
310 const struct cl_page_slice *slice)
312 if (cfs_mutex_is_locked(&cl2echo_page(slice)->ep_lock))
317 static void echo_page_completion(const struct lu_env *env,
318 const struct cl_page_slice *slice,
321 LASSERT(slice->cpl_page->cp_sync_io != NULL);
324 static void echo_page_fini(const struct lu_env *env,
325 struct cl_page_slice *slice)
327 struct echo_page *ep = cl2echo_page(slice);
328 struct echo_object *eco = cl2echo_obj(slice->cpl_obj);
329 cfs_page_t *vmpage = ep->ep_vmpage;
332 cfs_atomic_dec(&eco->eo_npages);
333 page_cache_release(vmpage);
334 OBD_SLAB_FREE_PTR(ep, echo_page_kmem);
338 static int echo_page_prep(const struct lu_env *env,
339 const struct cl_page_slice *slice,
340 struct cl_io *unused)
345 static int echo_page_print(const struct lu_env *env,
346 const struct cl_page_slice *slice,
347 void *cookie, lu_printer_t printer)
349 struct echo_page *ep = cl2echo_page(slice);
351 (*printer)(env, cookie, LUSTRE_ECHO_CLIENT_NAME"-page@%p %d vm@%p\n",
352 ep, cfs_mutex_is_locked(&ep->ep_lock), ep->ep_vmpage);
356 static const struct cl_page_operations echo_page_ops = {
357 .cpo_own = echo_page_own,
358 .cpo_disown = echo_page_disown,
359 .cpo_discard = echo_page_discard,
360 .cpo_vmpage = echo_page_vmpage,
361 .cpo_fini = echo_page_fini,
362 .cpo_print = echo_page_print,
363 .cpo_is_vmlocked = echo_page_is_vmlocked,
366 .cpo_prep = echo_page_prep,
367 .cpo_completion = echo_page_completion,
370 .cpo_prep = echo_page_prep,
371 .cpo_completion = echo_page_completion,
377 /** \defgroup echo_lock Locking
379 * echo lock operations
383 static void echo_lock_fini(const struct lu_env *env,
384 struct cl_lock_slice *slice)
386 struct echo_lock *ecl = cl2echo_lock(slice);
388 LASSERT(cfs_list_empty(&ecl->el_chain));
389 OBD_SLAB_FREE_PTR(ecl, echo_lock_kmem);
392 static void echo_lock_delete(const struct lu_env *env,
393 const struct cl_lock_slice *slice)
395 struct echo_lock *ecl = cl2echo_lock(slice);
397 LASSERT(cfs_list_empty(&ecl->el_chain));
400 static int echo_lock_fits_into(const struct lu_env *env,
401 const struct cl_lock_slice *slice,
402 const struct cl_lock_descr *need,
403 const struct cl_io *unused)
408 static struct cl_lock_operations echo_lock_ops = {
409 .clo_fini = echo_lock_fini,
410 .clo_delete = echo_lock_delete,
411 .clo_fits_into = echo_lock_fits_into
416 /** \defgroup echo_cl_ops cl_object operations
418 * operations for cl_object
422 static struct cl_page *echo_page_init(const struct lu_env *env,
423 struct cl_object *obj,
424 struct cl_page *page, cfs_page_t *vmpage)
426 struct echo_page *ep;
429 OBD_SLAB_ALLOC_PTR_GFP(ep, echo_page_kmem, CFS_ALLOC_IO);
431 struct echo_object *eco = cl2echo_obj(obj);
432 ep->ep_vmpage = vmpage;
433 page_cache_get(vmpage);
434 cfs_mutex_init(&ep->ep_lock);
435 cl_page_slice_add(page, &ep->ep_cl, obj, &echo_page_ops);
436 cfs_atomic_inc(&eco->eo_npages);
438 RETURN(ERR_PTR(ep ? 0 : -ENOMEM));
441 static int echo_io_init(const struct lu_env *env, struct cl_object *obj,
447 static int echo_lock_init(const struct lu_env *env,
448 struct cl_object *obj, struct cl_lock *lock,
449 const struct cl_io *unused)
451 struct echo_lock *el;
454 OBD_SLAB_ALLOC_PTR_GFP(el, echo_lock_kmem, CFS_ALLOC_IO);
456 cl_lock_slice_add(lock, &el->el_cl, obj, &echo_lock_ops);
457 el->el_object = cl2echo_obj(obj);
458 CFS_INIT_LIST_HEAD(&el->el_chain);
459 cfs_atomic_set(&el->el_refcount, 0);
461 RETURN(el == NULL ? -ENOMEM : 0);
464 static int echo_conf_set(const struct lu_env *env, struct cl_object *obj,
465 const struct cl_object_conf *conf)
470 static const struct cl_object_operations echo_cl_obj_ops = {
471 .coo_page_init = echo_page_init,
472 .coo_lock_init = echo_lock_init,
473 .coo_io_init = echo_io_init,
474 .coo_conf_set = echo_conf_set
476 /** @} echo_cl_ops */
478 /** \defgroup echo_lu_ops lu_object operations
480 * operations for echo lu object.
484 static int echo_object_init(const struct lu_env *env, struct lu_object *obj,
485 const struct lu_object_conf *conf)
487 struct echo_device *ed = cl2echo_dev(lu2cl_dev(obj->lo_dev));
488 struct echo_client_obd *ec = ed->ed_ec;
489 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
493 struct lu_object *below;
494 struct lu_device *under;
497 below = under->ld_ops->ldo_object_alloc(env, obj->lo_header,
501 lu_object_add(obj, below);
504 if (!ed->ed_next_ismd) {
505 const struct cl_object_conf *cconf = lu2cl_conf(conf);
506 struct echo_object_conf *econf = cl2echo_conf(cconf);
508 LASSERT(econf->eoc_md);
509 eco->eo_lsm = *econf->eoc_md;
510 /* clear the lsm pointer so that it won't get freed. */
511 *econf->eoc_md = NULL;
517 cfs_atomic_set(&eco->eo_npages, 0);
519 cfs_spin_lock(&ec->ec_lock);
520 cfs_list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
521 cfs_spin_unlock(&ec->ec_lock);
526 static void echo_object_free(const struct lu_env *env, struct lu_object *obj)
528 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
529 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
530 struct lov_stripe_md *lsm = eco->eo_lsm;
533 LASSERT(cfs_atomic_read(&eco->eo_npages) == 0);
535 cfs_spin_lock(&ec->ec_lock);
536 cfs_list_del_init(&eco->eo_obj_chain);
537 cfs_spin_unlock(&ec->ec_lock);
540 lu_object_header_fini(obj->lo_header);
543 obd_free_memmd(ec->ec_exp, &lsm);
544 OBD_SLAB_FREE_PTR(eco, echo_object_kmem);
548 static int echo_object_print(const struct lu_env *env, void *cookie,
549 lu_printer_t p, const struct lu_object *o)
551 struct echo_object *obj = cl2echo_obj(lu2cl(o));
553 return (*p)(env, cookie, "echoclient-object@%p", obj);
556 static const struct lu_object_operations echo_lu_obj_ops = {
557 .loo_object_init = echo_object_init,
558 .loo_object_delete = NULL,
559 .loo_object_release = NULL,
560 .loo_object_free = echo_object_free,
561 .loo_object_print = echo_object_print,
562 .loo_object_invariant = NULL
564 /** @} echo_lu_ops */
566 /** \defgroup echo_lu_dev_ops lu_device operations
568 * Operations for echo lu device.
572 static struct lu_object *echo_object_alloc(const struct lu_env *env,
573 const struct lu_object_header *hdr,
574 struct lu_device *dev)
576 struct echo_object *eco;
577 struct lu_object *obj = NULL;
580 /* we're the top dev. */
581 LASSERT(hdr == NULL);
582 OBD_SLAB_ALLOC_PTR_GFP(eco, echo_object_kmem, CFS_ALLOC_IO);
584 struct cl_object_header *hdr = &eco->eo_hdr;
586 obj = &echo_obj2cl(eco)->co_lu;
587 cl_object_header_init(hdr);
588 lu_object_init(obj, &hdr->coh_lu, dev);
589 lu_object_add_top(&hdr->coh_lu, obj);
591 eco->eo_cl.co_ops = &echo_cl_obj_ops;
592 obj->lo_ops = &echo_lu_obj_ops;
597 static struct lu_device_operations echo_device_lu_ops = {
598 .ldo_object_alloc = echo_object_alloc,
601 /** @} echo_lu_dev_ops */
603 static struct cl_device_operations echo_device_cl_ops = {
606 /** \defgroup echo_init Setup and teardown
608 * Init and fini functions for echo client.
612 static int echo_site_init(const struct lu_env *env, struct echo_device *ed)
614 struct cl_site *site = &ed->ed_site_myself;
617 /* initialize site */
618 rc = cl_site_init(site, &ed->ed_cl);
620 CERROR("Cannot initilize site for echo client(%d)\n", rc);
624 rc = lu_site_init_finish(&site->cs_lu);
632 static void echo_site_fini(const struct lu_env *env, struct echo_device *ed)
635 if (!ed->ed_next_ismd)
636 cl_site_fini(ed->ed_site);
641 static void *echo_thread_key_init(const struct lu_context *ctx,
642 struct lu_context_key *key)
644 struct echo_thread_info *info;
646 OBD_SLAB_ALLOC_PTR_GFP(info, echo_thread_kmem, CFS_ALLOC_IO);
648 info = ERR_PTR(-ENOMEM);
652 static void echo_thread_key_fini(const struct lu_context *ctx,
653 struct lu_context_key *key, void *data)
655 struct echo_thread_info *info = data;
656 OBD_SLAB_FREE_PTR(info, echo_thread_kmem);
659 static void echo_thread_key_exit(const struct lu_context *ctx,
660 struct lu_context_key *key, void *data)
664 static struct lu_context_key echo_thread_key = {
665 .lct_tags = LCT_CL_THREAD,
666 .lct_init = echo_thread_key_init,
667 .lct_fini = echo_thread_key_fini,
668 .lct_exit = echo_thread_key_exit
671 static void *echo_session_key_init(const struct lu_context *ctx,
672 struct lu_context_key *key)
674 struct echo_session_info *session;
676 OBD_SLAB_ALLOC_PTR_GFP(session, echo_session_kmem, CFS_ALLOC_IO);
678 session = ERR_PTR(-ENOMEM);
682 static void echo_session_key_fini(const struct lu_context *ctx,
683 struct lu_context_key *key, void *data)
685 struct echo_session_info *session = data;
686 OBD_SLAB_FREE_PTR(session, echo_session_kmem);
689 static void echo_session_key_exit(const struct lu_context *ctx,
690 struct lu_context_key *key, void *data)
694 static struct lu_context_key echo_session_key = {
695 .lct_tags = LCT_SESSION,
696 .lct_init = echo_session_key_init,
697 .lct_fini = echo_session_key_fini,
698 .lct_exit = echo_session_key_exit
701 LU_TYPE_INIT_FINI(echo, &echo_thread_key, &echo_session_key);
703 #define ECHO_SEQ_WIDTH 0xffffffff
704 static int echo_fid_init(struct echo_device *ed, char *obd_name,
711 OBD_ALLOC_PTR(ed->ed_cl_seq);
712 if (ed->ed_cl_seq == NULL)
715 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
717 GOTO(out_free_seq, rc = -ENOMEM);
719 snprintf(prefix, MAX_OBD_NAME + 5, "srv-%s", obd_name);
721 /* Init client side sequence-manager */
722 rc = seq_client_init(ed->ed_cl_seq, NULL,
724 prefix, ms->ms_server_seq);
725 ed->ed_cl_seq->lcs_width = ECHO_SEQ_WIDTH;
726 OBD_FREE(prefix, MAX_OBD_NAME + 5);
728 GOTO(out_free_seq, rc);
733 OBD_FREE_PTR(ed->ed_cl_seq);
734 ed->ed_cl_seq = NULL;
738 static int echo_fid_fini(struct obd_device *obddev)
740 struct echo_device *ed = obd2echo_dev(obddev);
743 if (ed->ed_cl_seq != NULL) {
744 seq_client_fini(ed->ed_cl_seq);
745 OBD_FREE_PTR(ed->ed_cl_seq);
746 ed->ed_cl_seq = NULL;
752 static struct lu_device *echo_device_alloc(const struct lu_env *env,
753 struct lu_device_type *t,
754 struct lustre_cfg *cfg)
756 struct lu_device *next;
757 struct echo_device *ed;
758 struct cl_device *cd;
759 struct obd_device *obd = NULL; /* to keep compiler happy */
760 struct obd_device *tgt;
761 const char *tgt_type_name;
768 GOTO(out, rc = -ENOMEM);
772 rc = cl_device_init(cd, t);
776 cd->cd_lu_dev.ld_ops = &echo_device_lu_ops;
777 cd->cd_ops = &echo_device_cl_ops;
780 obd = class_name2obd(lustre_cfg_string(cfg, 0));
781 LASSERT(obd != NULL);
782 LASSERT(env != NULL);
784 tgt = class_name2obd(lustre_cfg_string(cfg, 1));
786 CERROR("Can not find tgt device %s\n",
787 lustre_cfg_string(cfg, 1));
788 GOTO(out, rc = -ENODEV);
791 next = tgt->obd_lu_dev;
792 if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
793 ed->ed_next_ismd = 1;
795 ed->ed_next_ismd = 0;
796 rc = echo_site_init(env, ed);
802 rc = echo_client_setup(env, obd, cfg);
806 ed->ed_ec = &obd->u.echo_client;
809 if (ed->ed_next_ismd) {
810 /* Suppose to connect to some Metadata layer */
812 struct lu_device *ld;
816 CERROR("%s is not lu device type!\n",
817 lustre_cfg_string(cfg, 1));
818 GOTO(out, rc = -EINVAL);
821 tgt_type_name = lustre_cfg_string(cfg, 2);
822 if (!tgt_type_name) {
823 CERROR("%s no type name for echo %s setup\n",
824 lustre_cfg_string(cfg, 1),
825 tgt->obd_type->typ_name);
826 GOTO(out, rc = -EINVAL);
831 cfs_spin_lock(&ls->ls_ld_lock);
832 cfs_list_for_each_entry(ld, &ls->ls_ld_linkage, ld_linkage) {
833 if (strcmp(ld->ld_type->ldt_name, tgt_type_name) == 0) {
838 cfs_spin_unlock(&ls->ls_ld_lock);
841 CERROR("%s is not lu device type!\n",
842 lustre_cfg_string(cfg, 1));
843 GOTO(out, rc = -EINVAL);
847 /* For MD echo client, it will use the site in MDS stack */
848 ed->ed_site_myself.cs_lu = *ls;
849 ed->ed_site = &ed->ed_site_myself;
850 ed->ed_cl.cd_lu_dev.ld_site = &ed->ed_site_myself.cs_lu;
851 rc = echo_fid_init(ed, obd->obd_name, lu_site2md(ls));
853 CERROR("echo fid init error %d\n", rc);
857 /* if echo client is to be stacked upon ost device, the next is
858 * NULL since ost is not a clio device so far */
859 if (next != NULL && !lu_device_is_cl(next))
862 tgt_type_name = tgt->obd_type->typ_name;
864 LASSERT(next != NULL);
865 if (next->ld_site != NULL)
866 GOTO(out, rc = -EBUSY);
868 next->ld_site = &ed->ed_site->cs_lu;
869 rc = next->ld_type->ldt_ops->ldto_device_init(env, next,
870 next->ld_type->ldt_name,
875 /* Tricky case, I have to determine the obd type since
876 * CLIO uses the different parameters to initialize
877 * objects for lov & osc. */
878 if (strcmp(tgt_type_name, LUSTRE_LOV_NAME) == 0)
879 ed->ed_next_islov = 1;
881 LASSERT(strcmp(tgt_type_name,
882 LUSTRE_OSC_NAME) == 0);
884 LASSERT(strcmp(tgt_type_name, LUSTRE_OST_NAME) == 0);
888 RETURN(&cd->cd_lu_dev);
893 rc2 = echo_client_cleanup(obd);
895 CERROR("Cleanup obd device %s error(%d)\n",
900 echo_site_fini(env, ed);
902 cl_device_fini(&ed->ed_cl);
912 static int echo_device_init(const struct lu_env *env, struct lu_device *d,
913 const char *name, struct lu_device *next)
919 static struct lu_device *echo_device_fini(const struct lu_env *env,
922 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
923 struct lu_device *next = ed->ed_next;
925 while (next && !ed->ed_next_ismd)
926 next = next->ld_type->ldt_ops->ldto_device_fini(env, next);
930 static void echo_lock_release(const struct lu_env *env,
931 struct echo_lock *ecl,
934 struct cl_lock *clk = echo_lock2cl(ecl);
938 cl_lock_release(env, clk, "ec enqueue", ecl->el_object);
940 cl_lock_mutex_get(env, clk);
941 cl_lock_cancel(env, clk);
942 cl_lock_delete(env, clk);
943 cl_lock_mutex_put(env, clk);
945 cl_lock_put(env, clk);
948 static struct lu_device *echo_device_free(const struct lu_env *env,
951 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
952 struct echo_client_obd *ec = ed->ed_ec;
953 struct echo_object *eco;
954 struct lu_device *next = ed->ed_next;
956 CDEBUG(D_INFO, "echo device:%p is going to be freed, next = %p\n",
959 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
961 /* check if there are objects still alive.
962 * It shouldn't have any object because lu_site_purge would cleanup
963 * all of cached objects. Anyway, probably the echo device is being
964 * parallelly accessed.
966 cfs_spin_lock(&ec->ec_lock);
967 cfs_list_for_each_entry(eco, &ec->ec_objects, eo_obj_chain)
969 cfs_spin_unlock(&ec->ec_lock);
972 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
975 "Waiting for the reference of echo object to be dropped\n");
977 /* Wait for the last reference to be dropped. */
978 cfs_spin_lock(&ec->ec_lock);
979 while (!cfs_list_empty(&ec->ec_objects)) {
980 cfs_spin_unlock(&ec->ec_lock);
981 CERROR("echo_client still has objects at cleanup time, "
982 "wait for 1 second\n");
983 cfs_schedule_timeout_and_set_state(CFS_TASK_UNINT,
984 cfs_time_seconds(1));
985 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
986 cfs_spin_lock(&ec->ec_lock);
988 cfs_spin_unlock(&ec->ec_lock);
990 LASSERT(cfs_list_empty(&ec->ec_locks));
992 CDEBUG(D_INFO, "No object exists, exiting...\n");
994 echo_client_cleanup(d->ld_obd);
995 echo_fid_fini(d->ld_obd);
996 while (next && !ed->ed_next_ismd)
997 next = next->ld_type->ldt_ops->ldto_device_free(env, next);
999 LASSERT(ed->ed_site == lu2cl_site(d->ld_site));
1000 echo_site_fini(env, ed);
1001 cl_device_fini(&ed->ed_cl);
1007 static const struct lu_device_type_operations echo_device_type_ops = {
1008 .ldto_init = echo_type_init,
1009 .ldto_fini = echo_type_fini,
1011 .ldto_start = echo_type_start,
1012 .ldto_stop = echo_type_stop,
1014 .ldto_device_alloc = echo_device_alloc,
1015 .ldto_device_free = echo_device_free,
1016 .ldto_device_init = echo_device_init,
1017 .ldto_device_fini = echo_device_fini
1020 static struct lu_device_type echo_device_type = {
1021 .ldt_tags = LU_DEVICE_CL,
1022 .ldt_name = LUSTRE_ECHO_CLIENT_NAME,
1023 .ldt_ops = &echo_device_type_ops,
1024 .ldt_ctx_tags = LCT_CL_THREAD | LCT_MD_THREAD | LCT_DT_THREAD,
1028 /** \defgroup echo_exports Exported operations
1030 * exporting functions to echo client
1035 /* Interfaces to echo client obd device */
1036 static struct echo_object *cl_echo_object_find(struct echo_device *d,
1037 struct lov_stripe_md **lsmp)
1040 struct echo_thread_info *info;
1041 struct echo_object_conf *conf;
1042 struct lov_stripe_md *lsm;
1043 struct echo_object *eco;
1044 struct cl_object *obj;
1052 LASSERT(lsm->lsm_object_id);
1054 /* Never return an object if the obd is to be freed. */
1055 if (echo_dev2cl(d)->cd_lu_dev.ld_obd->obd_stopping)
1056 RETURN(ERR_PTR(-ENODEV));
1058 env = cl_env_get(&refcheck);
1060 RETURN((void *)env);
1062 info = echo_env_info(env);
1063 conf = &info->eti_conf;
1065 if (!d->ed_next_islov) {
1066 struct lov_oinfo *oinfo = lsm->lsm_oinfo[0];
1067 LASSERT(oinfo != NULL);
1068 oinfo->loi_id = lsm->lsm_object_id;
1069 oinfo->loi_seq = lsm->lsm_object_seq;
1070 conf->eoc_cl.u.coc_oinfo = oinfo;
1072 struct lustre_md *md;
1074 memset(md, 0, sizeof *md);
1076 conf->eoc_cl.u.coc_md = md;
1079 conf->eoc_md = lsmp;
1081 fid = &info->eti_fid;
1084 obj = cl_object_find(env, echo_dev2cl(d), fid, &conf->eoc_cl);
1086 GOTO(out, eco = (void*)obj);
1088 eco = cl2echo_obj(obj);
1089 if (eco->eo_deleted) {
1090 cl_object_put(env, obj);
1091 eco = ERR_PTR(-EAGAIN);
1095 cl_env_put(env, &refcheck);
1099 static int cl_echo_object_put(struct echo_object *eco)
1102 struct cl_object *obj = echo_obj2cl(eco);
1106 env = cl_env_get(&refcheck);
1108 RETURN(PTR_ERR(env));
1110 /* an external function to kill an object? */
1111 if (eco->eo_deleted) {
1112 struct lu_object_header *loh = obj->co_lu.lo_header;
1113 LASSERT(&eco->eo_hdr == luh2coh(loh));
1114 cfs_set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
1117 cl_object_put(env, obj);
1118 cl_env_put(env, &refcheck);
1122 static int cl_echo_enqueue0(struct lu_env *env, struct echo_object *eco,
1123 obd_off start, obd_off end, int mode,
1124 __u64 *cookie , __u32 enqflags)
1127 struct cl_lock *lck;
1128 struct cl_object *obj;
1129 struct cl_lock_descr *descr;
1130 struct echo_thread_info *info;
1134 info = echo_env_info(env);
1136 descr = &info->eti_descr;
1137 obj = echo_obj2cl(eco);
1139 descr->cld_obj = obj;
1140 descr->cld_start = cl_index(obj, start);
1141 descr->cld_end = cl_index(obj, end);
1142 descr->cld_mode = mode == LCK_PW ? CLM_WRITE : CLM_READ;
1143 descr->cld_enq_flags = enqflags;
1146 lck = cl_lock_request(env, io, descr, "ec enqueue", eco);
1148 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
1149 struct echo_lock *el;
1151 rc = cl_wait(env, lck);
1153 el = cl2echo_lock(cl_lock_at(lck, &echo_device_type));
1154 cfs_spin_lock(&ec->ec_lock);
1155 if (cfs_list_empty(&el->el_chain)) {
1156 cfs_list_add(&el->el_chain, &ec->ec_locks);
1157 el->el_cookie = ++ec->ec_unique;
1159 cfs_atomic_inc(&el->el_refcount);
1160 *cookie = el->el_cookie;
1161 cfs_spin_unlock(&ec->ec_lock);
1163 cl_lock_release(env, lck, "ec enqueue", cfs_current());
1168 static int cl_echo_enqueue(struct echo_object *eco, obd_off start, obd_off end,
1169 int mode, __u64 *cookie)
1171 struct echo_thread_info *info;
1178 env = cl_env_get(&refcheck);
1180 RETURN(PTR_ERR(env));
1182 info = echo_env_info(env);
1185 result = cl_io_init(env, io, CIT_MISC, echo_obj2cl(eco));
1188 LASSERT(result == 0);
1190 result = cl_echo_enqueue0(env, eco, start, end, mode, cookie, 0);
1191 cl_io_fini(env, io);
1195 cl_env_put(env, &refcheck);
1199 static int cl_echo_cancel0(struct lu_env *env, struct echo_device *ed,
1202 struct echo_client_obd *ec = ed->ed_ec;
1203 struct echo_lock *ecl = NULL;
1205 int found = 0, still_used = 0;
1208 LASSERT(ec != NULL);
1209 cfs_spin_lock (&ec->ec_lock);
1210 cfs_list_for_each (el, &ec->ec_locks) {
1211 ecl = cfs_list_entry (el, struct echo_lock, el_chain);
1212 CDEBUG(D_INFO, "ecl: %p, cookie: "LPX64"\n", ecl, ecl->el_cookie);
1213 found = (ecl->el_cookie == cookie);
1215 if (cfs_atomic_dec_and_test(&ecl->el_refcount))
1216 cfs_list_del_init(&ecl->el_chain);
1222 cfs_spin_unlock (&ec->ec_lock);
1227 echo_lock_release(env, ecl, still_used);
1231 static int cl_echo_cancel(struct echo_device *ed, __u64 cookie)
1238 env = cl_env_get(&refcheck);
1240 RETURN(PTR_ERR(env));
1242 rc = cl_echo_cancel0(env, ed, cookie);
1244 cl_env_put(env, &refcheck);
1248 static int cl_echo_async_brw(const struct lu_env *env, struct cl_io *io,
1249 enum cl_req_type unused, struct cl_2queue *queue)
1251 struct cl_page *clp;
1252 struct cl_page *temp;
1256 cl_page_list_for_each_safe(clp, temp, &queue->c2_qin) {
1258 rc = cl_page_cache_add(env, io, clp, CRT_WRITE);
1261 result = result ?: rc;
1266 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
1267 cfs_page_t **pages, int npages, int async)
1270 struct echo_thread_info *info;
1271 struct cl_object *obj = echo_obj2cl(eco);
1272 struct echo_device *ed = eco->eo_dev;
1273 struct cl_2queue *queue;
1275 struct cl_page *clp;
1276 struct lustre_handle lh = { 0 };
1277 int page_size = cl_page_size(obj);
1283 LASSERT((offset & ~CFS_PAGE_MASK) == 0);
1284 LASSERT(ed->ed_next != NULL);
1285 env = cl_env_get(&refcheck);
1287 RETURN(PTR_ERR(env));
1289 info = echo_env_info(env);
1291 queue = &info->eti_queue;
1293 cl_2queue_init(queue);
1294 rc = cl_io_init(env, io, CIT_MISC, obj);
1300 rc = cl_echo_enqueue0(env, eco, offset,
1301 offset + npages * CFS_PAGE_SIZE - 1,
1302 rw == READ ? LCK_PR : LCK_PW, &lh.cookie,
1305 GOTO(error_lock, rc);
1307 for (i = 0; i < npages; i++) {
1309 clp = cl_page_find(env, obj, cl_index(obj, offset),
1310 pages[i], CPT_TRANSIENT);
1315 LASSERT(clp->cp_type == CPT_TRANSIENT);
1317 rc = cl_page_own(env, io, clp);
1319 LASSERT(clp->cp_state == CPS_FREEING);
1320 cl_page_put(env, clp);
1324 cl_2queue_add(queue, clp);
1326 /* drop the reference count for cl_page_find, so that the page
1327 * will be freed in cl_2queue_fini. */
1328 cl_page_put(env, clp);
1329 cl_page_clip(env, clp, 0, page_size);
1331 offset += page_size;
1335 enum cl_req_type typ = rw == READ ? CRT_READ : CRT_WRITE;
1337 async = async && (typ == CRT_WRITE);
1339 rc = cl_echo_async_brw(env, io, typ, queue);
1341 rc = cl_io_submit_sync(env, io, typ, queue,
1343 CDEBUG(D_INFO, "echo_client %s write returns %d\n",
1344 async ? "async" : "sync", rc);
1347 cl_echo_cancel0(env, ed, lh.cookie);
1350 cl_2queue_discard(env, io, queue);
1351 cl_2queue_disown(env, io, queue);
1352 cl_2queue_fini(env, queue);
1353 cl_io_fini(env, io);
1355 cl_env_put(env, &refcheck);
1358 /** @} echo_exports */
1361 static obd_id last_object_id;
1364 echo_copyout_lsm (struct lov_stripe_md *lsm, void *_ulsm, int ulsm_nob)
1366 struct lov_stripe_md *ulsm = _ulsm;
1369 nob = offsetof (struct lov_stripe_md, lsm_oinfo[lsm->lsm_stripe_count]);
1373 if (cfs_copy_to_user (ulsm, lsm, sizeof(ulsm)))
1376 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1377 if (cfs_copy_to_user (ulsm->lsm_oinfo[i], lsm->lsm_oinfo[i],
1378 sizeof(lsm->lsm_oinfo[0])))
1385 echo_copyin_lsm (struct echo_device *ed, struct lov_stripe_md *lsm,
1386 void *ulsm, int ulsm_nob)
1388 struct echo_client_obd *ec = ed->ed_ec;
1391 if (ulsm_nob < sizeof (*lsm))
1394 if (cfs_copy_from_user (lsm, ulsm, sizeof (*lsm)))
1397 if (lsm->lsm_stripe_count > ec->ec_nstripes ||
1398 lsm->lsm_magic != LOV_MAGIC ||
1399 (lsm->lsm_stripe_size & (~CFS_PAGE_MASK)) != 0 ||
1400 ((__u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count > ~0UL))
1404 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1405 if (cfs_copy_from_user(lsm->lsm_oinfo[i],
1406 ((struct lov_stripe_md *)ulsm)-> \
1408 sizeof(lsm->lsm_oinfo[0])))
1414 static inline void echo_md_build_name(struct lu_name *lname, char *name,
1417 sprintf(name, "%llu", id);
1418 lname->ln_name = name;
1419 lname->ln_namelen = strlen(name);
1422 static int echo_md_create_internal(const struct lu_env *env,
1423 struct echo_device *ed,
1424 struct md_object *parent,
1426 struct lu_name *lname,
1427 struct md_op_spec *spec,
1430 struct lu_object *ec_child, *child;
1431 struct lu_device *ld = ed->ed_next;
1434 ec_child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev,
1436 if (IS_ERR(ec_child)) {
1437 CERROR("Can not find the child "DFID": rc = %ld\n", PFID(fid),
1439 return PTR_ERR(ec_child);
1442 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1443 if (child == NULL) {
1444 CERROR("Can not locate the child "DFID"\n", PFID(fid));
1445 GOTO(out_put, rc = -EINVAL);
1448 CDEBUG(D_RPCTRACE, "Start creating object "DFID" %s %p\n",
1449 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1451 rc = mdo_create(env, parent, lname, lu2md(child), spec, ma);
1453 CERROR("Can not create child "DFID": rc = %d\n", PFID(fid), rc);
1456 CDEBUG(D_RPCTRACE, "End creating object "DFID" %s %p rc = %d\n",
1457 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent, rc);
1459 lu_object_put(env, ec_child);
1463 static int echo_set_lmm_size(const struct lu_env *env,
1464 struct lu_device *ld,
1467 struct md_device *md = lu2md_dev(ld);
1468 int lmm_size, cookie_size, rc;
1472 rc = md->md_ops->mdo_maxsize_get(env, md,
1473 &lmm_size, &cookie_size);
1477 ma->ma_lmm_size = lmm_size;
1479 OBD_ALLOC(ma->ma_lmm, lmm_size);
1480 if (ma->ma_lmm == NULL) {
1481 ma->ma_lmm_size = 0;
1486 ma->ma_cookie_size = cookie_size;
1487 if (cookie_size > 0) {
1488 OBD_ALLOC(ma->ma_cookie, cookie_size);
1489 if (ma->ma_cookie == NULL) {
1490 ma->ma_cookie_size = 0;
1498 static int echo_create_md_object(const struct lu_env *env,
1499 struct echo_device *ed,
1500 struct lu_object *ec_parent,
1502 char *name, int namelen,
1503 __u64 id, __u32 mode, int count,
1504 int stripe_count, int stripe_offset)
1506 struct lu_object *parent;
1507 struct echo_thread_info *info = echo_env_info(env);
1508 struct lu_name *lname = &info->eti_lname;
1509 struct md_op_spec *spec = &info->eti_spec;
1510 struct md_attr *ma = &info->eti_ma;
1511 struct lu_device *ld = ed->ed_next;
1515 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1516 if (ec_parent == NULL) {
1517 lu_object_put(env, ec_parent);
1518 RETURN(PTR_ERR(parent));
1521 memset(ma, 0, sizeof(*ma));
1522 memset(spec, 0, sizeof(*spec));
1523 if (stripe_count != 0) {
1524 spec->sp_cr_flags |= FMODE_WRITE;
1525 rc = echo_set_lmm_size(env, ld, ma);
1528 if (stripe_count != -1) {
1529 struct lov_user_md_v3 *lum = &info->eti_lum;
1530 lum->lmm_magic = LOV_USER_MAGIC_V3;
1531 lum->lmm_stripe_count = stripe_count;
1532 lum->lmm_stripe_offset = stripe_offset;
1533 lum->lmm_pattern = 0;
1534 spec->u.sp_ea.eadata = lum;
1535 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1539 ma->ma_attr.la_mode = mode;
1540 ma->ma_attr.la_valid = LA_CTIME;
1541 ma->ma_attr.la_ctime = cfs_time_current_64();
1544 lname->ln_name = name;
1545 lname->ln_namelen = namelen;
1546 /* If name is specified, only create one object by name */
1547 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1552 /* Create multiple object sequenced by id */
1553 for (i = 0; i < count; i++) {
1554 char *tmp_name = info->eti_name;
1556 echo_md_build_name(lname, tmp_name, id);
1558 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1561 CERROR("Can not create child %s: rc = %d\n", tmp_name,
1570 if (ma->ma_lmm_size > 0 && ma->ma_lmm != NULL)
1571 OBD_FREE(ma->ma_lmm, ma->ma_lmm_size);
1572 if (ma->ma_cookie_size > 0 && ma->ma_cookie != NULL)
1573 OBD_FREE(ma->ma_cookie, ma->ma_cookie_size);
1578 static struct lu_object *echo_md_lookup(const struct lu_env *env,
1579 struct echo_device *ed,
1580 struct md_object *parent,
1581 struct lu_name *lname)
1583 struct echo_thread_info *info = echo_env_info(env);
1584 struct lu_fid *fid = &info->eti_fid;
1585 struct lu_object *child;
1589 CDEBUG(D_INFO, "lookup %s in parent "DFID" %p\n", lname->ln_name,
1591 rc = mdo_lookup(env, parent, lname, fid, NULL);
1593 CERROR("lookup %s: rc = %d\n", lname->ln_name, rc);
1594 RETURN(ERR_PTR(rc));
1597 child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1602 static int echo_setattr_object(const struct lu_env *env,
1603 struct echo_device *ed,
1604 struct lu_object *ec_parent,
1605 __u64 id, int count)
1607 struct lu_object *parent;
1608 struct echo_thread_info *info = echo_env_info(env);
1609 struct lu_name *lname = &info->eti_lname;
1610 char *name = info->eti_name;
1611 struct lu_device *ld = ed->ed_next;
1612 struct lu_buf *buf = &info->eti_buf;
1616 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1617 if (ec_parent == NULL) {
1618 lu_object_put(env, ec_parent);
1619 return PTR_ERR(parent);
1622 buf->lb_buf = info->eti_xattr_buf;
1623 buf->lb_len = sizeof(info->eti_xattr_buf);
1624 for (i = 0; i < count; i++) {
1625 struct lu_object *ec_child, *child;
1627 echo_md_build_name(lname, name, id);
1629 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1630 if (IS_ERR(ec_child)) {
1631 CERROR("Can't find child %s: rc = %ld\n",
1632 lname->ln_name, PTR_ERR(ec_child));
1633 RETURN(PTR_ERR(ec_child));
1636 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1637 if (child == NULL) {
1638 CERROR("Can not locate the child %s\n", lname->ln_name);
1639 lu_object_put(env, ec_child);
1644 CDEBUG(D_RPCTRACE, "Start setattr object "DFID"\n",
1645 PFID(lu_object_fid(child)));
1647 sprintf(name, "%s.test1", XATTR_USER_PREFIX);
1648 rc = mo_xattr_set(env, lu2md(child), buf, name,
1651 CERROR("Can not setattr child "DFID": rc = %d\n",
1652 PFID(lu_object_fid(child)), rc);
1653 lu_object_put(env, ec_child);
1656 CDEBUG(D_RPCTRACE, "End setattr object "DFID"\n",
1657 PFID(lu_object_fid(child)));
1659 lu_object_put(env, ec_child);
1664 static int echo_getattr_object(const struct lu_env *env,
1665 struct echo_device *ed,
1666 struct lu_object *ec_parent,
1667 __u64 id, int count)
1669 struct lu_object *parent;
1670 struct echo_thread_info *info = echo_env_info(env);
1671 struct lu_name *lname = &info->eti_lname;
1672 char *name = info->eti_name;
1673 struct md_attr *ma = &info->eti_ma;
1674 struct lu_device *ld = ed->ed_next;
1678 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1679 if (ec_parent == NULL) {
1680 lu_object_put(env, ec_parent);
1681 return PTR_ERR(parent);
1684 memset(ma, 0, sizeof(*ma));
1685 rc = echo_set_lmm_size(env, ld, ma);
1689 ma->ma_need |= MA_INODE | MA_LOV | MA_PFID | MA_HSM | MA_ACL_DEF;
1690 ma->ma_acl = info->eti_xattr_buf;
1691 ma->ma_acl_size = sizeof(info->eti_xattr_buf);
1693 for (i = 0; i < count; i++) {
1694 struct lu_object *ec_child, *child;
1697 echo_md_build_name(lname, name, id);
1699 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1700 if (IS_ERR(ec_child)) {
1701 CERROR("Can't find child %s: rc = %ld\n",
1702 lname->ln_name, PTR_ERR(ec_child));
1703 RETURN(PTR_ERR(ec_child));
1706 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1707 if (child == NULL) {
1708 CERROR("Can not locate the child %s\n", lname->ln_name);
1709 lu_object_put(env, ec_child);
1710 GOTO(out_free, rc = -EINVAL);
1713 CDEBUG(D_RPCTRACE, "Start getattr object "DFID"\n",
1714 PFID(lu_object_fid(child)));
1715 rc = mo_attr_get(env, lu2md(child), ma);
1717 CERROR("Can not getattr child "DFID": rc = %d\n",
1718 PFID(lu_object_fid(child)), rc);
1719 lu_object_put(env, ec_child);
1722 CDEBUG(D_RPCTRACE, "End getattr object "DFID"\n",
1723 PFID(lu_object_fid(child)));
1725 lu_object_put(env, ec_child);
1729 if (ma->ma_lmm_size > 0 && ma->ma_lmm != NULL)
1730 OBD_FREE(ma->ma_lmm, ma->ma_lmm_size);
1731 if (ma->ma_cookie_size > 0 && ma->ma_cookie != NULL)
1732 OBD_FREE(ma->ma_cookie, ma->ma_cookie_size);
1736 static int echo_lookup_object(const struct lu_env *env,
1737 struct echo_device *ed,
1738 struct lu_object *ec_parent,
1739 __u64 id, int count)
1741 struct lu_object *parent;
1742 struct echo_thread_info *info = echo_env_info(env);
1743 struct lu_name *lname = &info->eti_lname;
1744 char *name = info->eti_name;
1745 struct lu_fid *fid = &info->eti_fid;
1746 struct lu_device *ld = ed->ed_next;
1750 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1751 if (ec_parent == NULL) {
1752 lu_object_put(env, ec_parent);
1753 return PTR_ERR(parent);
1756 /*prepare the requests*/
1757 for (i = 0; i < count; i++) {
1758 echo_md_build_name(lname, name, id);
1760 CDEBUG(D_RPCTRACE, "Start lookup object "DFID" %s %p\n",
1761 PFID(lu_object_fid(parent)), lname->ln_name, parent);
1763 rc = mdo_lookup(env, lu2md(parent), lname, fid, NULL);
1765 CERROR("Can not lookup child %s: rc = %d\n", name, rc);
1768 CDEBUG(D_RPCTRACE, "End lookup object "DFID" %s %p\n",
1769 PFID(lu_object_fid(parent)), lname->ln_name, parent);
1776 static int echo_md_destroy_internal(const struct lu_env *env,
1777 struct echo_device *ed,
1778 struct md_object *parent,
1779 struct lu_name *lname,
1782 struct lu_device *ld = ed->ed_next;
1783 struct lu_object *ec_child;
1784 struct lu_object *child;
1787 ec_child = echo_md_lookup(env, ed, parent, lname);
1788 if (IS_ERR(ec_child)) {
1789 CERROR("Can't find child %s: rc = %ld\n", lname->ln_name,
1791 RETURN(PTR_ERR(ec_child));
1794 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1795 if (child == NULL) {
1796 CERROR("Can not locate the child %s\n", lname->ln_name);
1797 GOTO(out_put, rc = -EINVAL);
1800 CDEBUG(D_RPCTRACE, "Start destroy object "DFID" %s %p\n",
1801 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1803 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2,3,50,0)
1804 /* After 2.4, MDT will send destroy RPC to OST directly, so no need
1806 ma->ma_valid |= MA_FLAGS;
1807 ma->ma_attr_flags |= MDS_UNLINK_DESTROY;
1809 #warning "Please remove this after 2.4 (LOD/OSP)"
1811 rc = mdo_unlink(env, parent, lu2md(child), lname, ma);
1813 CERROR("Can not unlink child %s: rc = %d\n",
1814 lname->ln_name, rc);
1817 CDEBUG(D_RPCTRACE, "End destroy object "DFID" %s %p\n",
1818 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1820 lu_object_put(env, ec_child);
1824 static int echo_destroy_object(const struct lu_env *env,
1825 struct echo_device *ed,
1826 struct lu_object *ec_parent,
1827 char *name, int namelen,
1828 __u64 id, __u32 mode,
1831 struct echo_thread_info *info = echo_env_info(env);
1832 struct lu_name *lname = &info->eti_lname;
1833 struct md_attr *ma = &info->eti_ma;
1834 struct lu_device *ld = ed->ed_next;
1835 struct lu_object *parent;
1840 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1844 memset(ma, 0, sizeof(*ma));
1845 ma->ma_attr.la_mode = mode;
1846 ma->ma_attr.la_valid = LA_CTIME;
1847 ma->ma_attr.la_ctime = cfs_time_current_64();
1848 ma->ma_need = MA_INODE;
1851 rc = echo_set_lmm_size(env, ld, ma);
1855 lname->ln_name = name;
1856 lname->ln_namelen = namelen;
1857 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
1862 /*prepare the requests*/
1863 for (i = 0; i < count; i++) {
1864 char *tmp_name = info->eti_name;
1866 ma->ma_need |= MA_LOV;
1868 echo_md_build_name(lname, tmp_name, id);
1870 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
1873 CERROR("Can not unlink child %s: rc = %d\n", name, rc);
1880 if (ma->ma_lmm_size > 0 && ma->ma_lmm != NULL)
1881 OBD_FREE(ma->ma_lmm, ma->ma_lmm_size);
1882 if (ma->ma_cookie_size > 0 && ma->ma_cookie != NULL)
1883 OBD_FREE(ma->ma_cookie, ma->ma_cookie_size);
1887 struct lu_object *echo_resolve_path(const struct lu_env *env,
1888 struct echo_device *ed, char *path,
1891 struct lu_device *ld = ed->ed_next;
1892 struct md_device *md = lu2md_dev(ld);
1893 struct echo_thread_info *info = echo_env_info(env);
1894 struct lu_fid *fid = &info->eti_fid;
1895 struct lu_name *lname = &info->eti_lname;
1896 struct lu_object *parent = NULL;
1897 struct lu_object *child = NULL;
1901 /*Only support MDD layer right now*/
1902 rc = md->md_ops->mdo_root_get(env, md, fid);
1904 CERROR("get root error: rc = %d\n", rc);
1905 RETURN(ERR_PTR(rc));
1908 parent = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1909 if (IS_ERR(parent)) {
1910 CERROR("Can not find the parent "DFID": rc = %ld\n",
1911 PFID(fid), PTR_ERR(parent));
1916 struct lu_object *ld_parent;
1919 e = strsep(&path, "/");
1924 if (!path || path[0] == '\0')
1930 lname->ln_namelen = strlen(e);
1932 ld_parent = lu_object_locate(parent->lo_header, ld->ld_type);
1933 if (ld_parent == NULL) {
1934 lu_object_put(env, parent);
1939 child = echo_md_lookup(env, ed, lu2md(ld_parent), lname);
1940 lu_object_put(env, parent);
1941 if (IS_ERR(child)) {
1942 rc = (int)PTR_ERR(child);
1943 CERROR("lookup %s under parent "DFID": rc = %d\n",
1944 lname->ln_name, PFID(lu_object_fid(ld_parent)),
1951 RETURN(ERR_PTR(rc));
1956 #define ECHO_MD_CTX_TAG (LCT_REMEMBER | LCT_NOREF | LCT_MD_THREAD)
1957 #define ECHO_MD_SES_TAG (LCT_SESSION | LCT_REMEMBER | LCT_NOREF)
1959 static int echo_md_handler(struct echo_device *ed, int command,
1960 char *path, int path_len, int id, int count,
1961 struct obd_ioctl_data *data)
1963 struct lu_device *ld = ed->ed_next;
1966 struct lu_object *parent;
1968 int namelen = data->ioc_plen2;
1973 CERROR("MD echo client is not being initialized properly\n");
1977 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
1978 CERROR("Only support MDD layer right now!\n");
1982 env = cl_env_get(&refcheck);
1984 RETURN(PTR_ERR(env));
1986 rc = lu_env_refill_by_tags(env, ECHO_MD_CTX_TAG, ECHO_MD_SES_TAG);
1988 cl_env_put(env, &refcheck);
1992 parent = echo_resolve_path(env, ed, path, path_len);
1993 if (IS_ERR(parent)) {
1994 CERROR("Can not resolve the path %s: rc = %ld\n", path,
1996 cl_env_put(env, &refcheck);
1997 RETURN(PTR_ERR(parent));
2001 OBD_ALLOC(name, namelen + 1);
2004 if (cfs_copy_from_user(name, data->ioc_pbuf2, namelen)) {
2005 OBD_FREE(name, namelen + 1);
2011 case ECHO_MD_CREATE:
2012 case ECHO_MD_MKDIR: {
2013 struct echo_thread_info *info = echo_env_info(env);
2014 __u32 mode = data->ioc_obdo2.o_mode;
2015 struct lu_fid *fid = &info->eti_fid;
2016 int stripe_count = (int)data->ioc_obdo2.o_misc;
2017 int stripe_index = (int)data->ioc_obdo2.o_stripe_idx;
2019 fid->f_seq = data->ioc_obdo1.o_seq;
2020 fid->f_oid = (__u32)data->ioc_obdo1.o_id;
2022 rc = echo_create_md_object(env, ed, parent, fid, name, namelen,
2023 id, mode, count, stripe_count,
2027 case ECHO_MD_DESTROY:
2028 case ECHO_MD_RMDIR: {
2029 __u32 mode = data->ioc_obdo2.o_mode;
2031 rc = echo_destroy_object(env, ed, parent, name, namelen,
2035 case ECHO_MD_LOOKUP:
2036 rc = echo_lookup_object(env, ed, parent, id, count);
2038 case ECHO_MD_GETATTR:
2039 rc = echo_getattr_object(env, ed, parent, id, count);
2041 case ECHO_MD_SETATTR:
2042 rc = echo_setattr_object(env, ed, parent, id, count);
2045 CERROR("unknown command %d\n", command);
2050 OBD_FREE(name, namelen + 1);
2051 lu_object_put(env, parent);
2052 cl_env_put(env, &refcheck);
2056 static int echo_create_object(struct echo_device *ed, int on_target,
2057 struct obdo *oa, void *ulsm, int ulsm_nob,
2058 struct obd_trans_info *oti)
2060 struct echo_object *eco;
2061 struct echo_client_obd *ec = ed->ed_ec;
2062 struct lov_stripe_md *lsm = NULL;
2067 if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
2068 (on_target || /* set_stripe */
2069 ec->ec_nstripes != 0)) { /* LOV */
2070 CERROR ("No valid oid\n");
2074 rc = obd_alloc_memmd(ec->ec_exp, &lsm);
2076 CERROR("Cannot allocate md: rc = %d\n", rc);
2083 rc = echo_copyin_lsm (ed, lsm, ulsm, ulsm_nob);
2087 if (lsm->lsm_stripe_count == 0)
2088 lsm->lsm_stripe_count = ec->ec_nstripes;
2090 if (lsm->lsm_stripe_size == 0)
2091 lsm->lsm_stripe_size = CFS_PAGE_SIZE;
2095 /* setup stripes: indices + default ids if required */
2096 for (i = 0; i < lsm->lsm_stripe_count; i++) {
2097 if (lsm->lsm_oinfo[i]->loi_id == 0)
2098 lsm->lsm_oinfo[i]->loi_id = lsm->lsm_object_id;
2100 lsm->lsm_oinfo[i]->loi_ost_idx =
2101 (idx + i) % ec->ec_nstripes;
2105 /* setup object ID here for !on_target and LOV hint */
2106 if (oa->o_valid & OBD_MD_FLID)
2107 lsm->lsm_object_id = oa->o_id;
2109 if (lsm->lsm_object_id == 0)
2110 lsm->lsm_object_id = ++last_object_id;
2114 /* Only echo objects are allowed to be created */
2115 LASSERT((oa->o_valid & OBD_MD_FLGROUP) &&
2116 (oa->o_seq == FID_SEQ_ECHO));
2117 rc = obd_create(ec->ec_exp, oa, &lsm, oti);
2119 CERROR("Cannot create objects: rc = %d\n", rc);
2125 /* See what object ID we were given */
2126 oa->o_id = lsm->lsm_object_id;
2127 oa->o_valid |= OBD_MD_FLID;
2129 eco = cl_echo_object_find(ed, &lsm);
2131 GOTO(failed, rc = PTR_ERR(eco));
2132 cl_echo_object_put(eco);
2134 CDEBUG(D_INFO, "oa->o_id = %lx\n", (long)oa->o_id);
2139 obd_destroy(ec->ec_exp, oa, lsm, oti, NULL, NULL);
2141 obd_free_memmd(ec->ec_exp, &lsm);
2143 CERROR("create object failed with: rc = %d\n", rc);
2147 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
2150 struct echo_client_obd *ec = ed->ed_ec;
2151 struct lov_stripe_md *lsm = NULL;
2152 struct echo_object *eco;
2156 if ((oa->o_valid & OBD_MD_FLID) == 0 ||
2157 oa->o_id == 0) /* disallow use of object id 0 */
2159 CERROR ("No valid oid\n");
2163 rc = obd_alloc_memmd(ec->ec_exp, &lsm);
2167 lsm->lsm_object_id = oa->o_id;
2168 if (oa->o_valid & OBD_MD_FLGROUP)
2169 lsm->lsm_object_seq = oa->o_seq;
2171 lsm->lsm_object_seq = FID_SEQ_ECHO;
2174 eco = cl_echo_object_find(ed, &lsm);
2180 obd_free_memmd(ec->ec_exp, &lsm);
2184 static void echo_put_object(struct echo_object *eco)
2186 if (cl_echo_object_put(eco))
2187 CERROR("echo client: drop an object failed");
2191 echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
2193 unsigned long stripe_count;
2194 unsigned long stripe_size;
2195 unsigned long width;
2196 unsigned long woffset;
2200 if (lsm->lsm_stripe_count <= 1)
2204 stripe_size = lsm->lsm_stripe_size;
2205 stripe_count = lsm->lsm_stripe_count;
2207 /* width = # bytes in all stripes */
2208 width = stripe_size * stripe_count;
2210 /* woffset = offset within a width; offset = whole number of widths */
2211 woffset = do_div (offset, width);
2213 stripe_index = woffset / stripe_size;
2215 *idp = lsm->lsm_oinfo[stripe_index]->loi_id;
2216 *offp = offset * stripe_size + woffset % stripe_size;
2220 echo_client_page_debug_setup(struct lov_stripe_md *lsm,
2221 cfs_page_t *page, int rw, obd_id id,
2222 obd_off offset, obd_off count)
2229 /* no partial pages on the client */
2230 LASSERT(count == CFS_PAGE_SIZE);
2232 addr = cfs_kmap(page);
2234 for (delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2235 if (rw == OBD_BRW_WRITE) {
2236 stripe_off = offset + delta;
2238 echo_get_stripe_off_id(lsm, &stripe_off, &stripe_id);
2240 stripe_off = 0xdeadbeef00c0ffeeULL;
2241 stripe_id = 0xdeadbeef00c0ffeeULL;
2243 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
2244 stripe_off, stripe_id);
2250 static int echo_client_page_debug_check(struct lov_stripe_md *lsm,
2251 cfs_page_t *page, obd_id id,
2252 obd_off offset, obd_off count)
2261 /* no partial pages on the client */
2262 LASSERT(count == CFS_PAGE_SIZE);
2264 addr = cfs_kmap(page);
2266 for (rc = delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2267 stripe_off = offset + delta;
2269 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
2271 rc2 = block_debug_check("test_brw",
2272 addr + delta, OBD_ECHO_BLOCK_SIZE,
2273 stripe_off, stripe_id);
2275 CERROR ("Error in echo object "LPX64"\n", id);
2284 static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
2285 struct echo_object *eco, obd_off offset,
2286 obd_size count, int async,
2287 struct obd_trans_info *oti)
2289 struct lov_stripe_md *lsm = eco->eo_lsm;
2291 struct brw_page *pga;
2292 struct brw_page *pgp;
2302 verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
2303 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
2304 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
2306 gfp_mask = ((oa->o_id & 2) == 0) ? CFS_ALLOC_STD : CFS_ALLOC_HIGHUSER;
2308 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
2309 LASSERT(lsm != NULL);
2310 LASSERT(lsm->lsm_object_id == oa->o_id);
2313 (count & (~CFS_PAGE_MASK)) != 0)
2316 /* XXX think again with misaligned I/O */
2317 npages = count >> CFS_PAGE_SHIFT;
2319 if (rw == OBD_BRW_WRITE)
2320 brw_flags = OBD_BRW_ASYNC;
2322 OBD_ALLOC(pga, npages * sizeof(*pga));
2326 OBD_ALLOC(pages, npages * sizeof(*pages));
2327 if (pages == NULL) {
2328 OBD_FREE(pga, npages * sizeof(*pga));
2332 for (i = 0, pgp = pga, off = offset;
2334 i++, pgp++, off += CFS_PAGE_SIZE) {
2336 LASSERT (pgp->pg == NULL); /* for cleanup */
2339 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
2340 if (pgp->pg == NULL)
2344 pgp->count = CFS_PAGE_SIZE;
2346 pgp->flag = brw_flags;
2349 echo_client_page_debug_setup(lsm, pgp->pg, rw,
2350 oa->o_id, off, pgp->count);
2353 /* brw mode can only be used at client */
2354 LASSERT(ed->ed_next != NULL);
2355 rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
2358 if (rc != 0 || rw != OBD_BRW_READ)
2361 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
2362 if (pgp->pg == NULL)
2367 vrc = echo_client_page_debug_check(lsm, pgp->pg, oa->o_id,
2368 pgp->off, pgp->count);
2369 if (vrc != 0 && rc == 0)
2372 OBD_PAGE_FREE(pgp->pg);
2374 OBD_FREE(pga, npages * sizeof(*pga));
2375 OBD_FREE(pages, npages * sizeof(*pages));
2379 static int echo_client_prep_commit(struct obd_export *exp, int rw,
2380 struct obdo *oa, struct echo_object *eco,
2381 obd_off offset, obd_size count,
2382 obd_size batch, struct obd_trans_info *oti,
2385 struct lov_stripe_md *lsm = eco->eo_lsm;
2386 struct obd_ioobj ioo;
2387 struct niobuf_local *lnb;
2388 struct niobuf_remote *rnb;
2390 obd_size npages, tot_pages;
2394 if (count <= 0 || (count & (~CFS_PAGE_MASK)) != 0 ||
2395 (lsm != NULL && lsm->lsm_object_id != oa->o_id))
2398 npages = batch >> CFS_PAGE_SHIFT;
2399 tot_pages = count >> CFS_PAGE_SHIFT;
2401 OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
2402 OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
2404 if (lnb == NULL || rnb == NULL)
2405 GOTO(out, ret = -ENOMEM);
2407 obdo_to_ioobj(oa, &ioo);
2411 for(; tot_pages; tot_pages -= npages) {
2414 if (tot_pages < npages)
2417 for (i = 0; i < npages; i++, off += CFS_PAGE_SIZE) {
2418 rnb[i].offset = off;
2419 rnb[i].len = CFS_PAGE_SIZE;
2422 ioo.ioo_bufcnt = npages;
2423 oti->oti_transno = 0;
2426 ret = obd_preprw(rw, exp, oa, 1, &ioo, rnb, &lpages, lnb, oti,
2430 LASSERT(lpages == npages);
2432 for (i = 0; i < lpages; i++) {
2433 cfs_page_t *page = lnb[i].page;
2435 /* read past eof? */
2436 if (page == NULL && lnb[i].rc == 0)
2440 lnb[i].flags |= OBD_BRW_ASYNC;
2442 if (oa->o_id == ECHO_PERSISTENT_OBJID ||
2443 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
2444 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
2447 if (rw == OBD_BRW_WRITE)
2448 echo_client_page_debug_setup(lsm, page, rw,
2453 echo_client_page_debug_check(lsm, page,
2459 ret = obd_commitrw(rw, exp, oa, 1,&ioo,rnb,npages,lnb,oti,ret);
2463 /* Reset oti otherwise it would confuse ldiskfs. */
2464 memset(oti, 0, sizeof(*oti));
2469 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
2471 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
2475 static int echo_client_brw_ioctl(int rw, struct obd_export *exp,
2476 struct obd_ioctl_data *data)
2478 struct obd_device *obd = class_exp2obd(exp);
2479 struct echo_device *ed = obd2echo_dev(obd);
2480 struct echo_client_obd *ec = ed->ed_ec;
2481 struct obd_trans_info dummy_oti = { 0 };
2482 struct obdo *oa = &data->ioc_obdo1;
2483 struct echo_object *eco;
2489 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2491 rc = echo_get_object(&eco, ed, oa);
2495 oa->o_valid &= ~OBD_MD_FLHANDLE;
2497 /* obdfilter doesn't support obd_brw now, simulate via prep + commit */
2498 test_mode = (long)data->ioc_pbuf1;
2502 if (ed->ed_next == NULL && test_mode != 3) {
2504 data->ioc_plen1 = data->ioc_count;
2507 /* Truncate batch size to maximum */
2508 if (data->ioc_plen1 > PTLRPC_MAX_BRW_SIZE)
2509 data->ioc_plen1 = PTLRPC_MAX_BRW_SIZE;
2511 switch (test_mode) {
2515 rc = echo_client_kbrw(ed, rw, oa,
2516 eco, data->ioc_offset,
2517 data->ioc_count, async, &dummy_oti);
2520 rc = echo_client_prep_commit(ec->ec_exp, rw, oa,
2521 eco, data->ioc_offset,
2522 data->ioc_count, data->ioc_plen1,
2528 echo_put_object(eco);
2533 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
2534 int mode, obd_off offset, obd_size nob)
2536 struct echo_device *ed = obd2echo_dev(exp->exp_obd);
2537 struct lustre_handle *ulh = &oa->o_handle;
2538 struct echo_object *eco;
2543 if (ed->ed_next == NULL)
2544 RETURN(-EOPNOTSUPP);
2546 if (!(mode == LCK_PR || mode == LCK_PW))
2549 if ((offset & (~CFS_PAGE_MASK)) != 0 ||
2550 (nob & (~CFS_PAGE_MASK)) != 0)
2553 rc = echo_get_object (&eco, ed, oa);
2557 end = (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
2558 rc = cl_echo_enqueue(eco, offset, end, mode, &ulh->cookie);
2560 oa->o_valid |= OBD_MD_FLHANDLE;
2561 CDEBUG(D_INFO, "Cookie is "LPX64"\n", ulh->cookie);
2563 echo_put_object(eco);
2568 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
2570 struct echo_device *ed = obd2echo_dev(exp->exp_obd);
2571 __u64 cookie = oa->o_handle.cookie;
2573 if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
2576 CDEBUG(D_INFO, "Cookie is "LPX64"\n", cookie);
2577 return cl_echo_cancel(ed, cookie);
2581 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
2582 int len, void *karg, void *uarg)
2584 struct obd_device *obd = exp->exp_obd;
2585 struct echo_device *ed = obd2echo_dev(obd);
2586 struct echo_client_obd *ec = ed->ed_ec;
2587 struct echo_object *eco;
2588 struct obd_ioctl_data *data = karg;
2589 struct obd_trans_info dummy_oti;
2590 struct oti_req_ack_lock *ack_lock;
2593 int rw = OBD_BRW_READ;
2598 #ifndef HAVE_UNLOCKED_IOCTL
2599 cfs_unlock_kernel();
2602 memset(&dummy_oti, 0, sizeof(dummy_oti));
2604 oa = &data->ioc_obdo1;
2605 if (!(oa->o_valid & OBD_MD_FLGROUP)) {
2606 oa->o_valid |= OBD_MD_FLGROUP;
2607 oa->o_seq = FID_SEQ_ECHO;
2610 /* This FID is unpacked just for validation at this point */
2611 rc = fid_ostid_unpack(&fid, &oa->o_oi, 0);
2616 case OBD_IOC_CREATE: /* may create echo object */
2617 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2618 GOTO (out, rc = -EPERM);
2620 rc = echo_create_object (ed, 1, oa,
2621 data->ioc_pbuf1, data->ioc_plen1,
2625 case OBD_IOC_ECHO_MD: {
2632 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2633 GOTO(out, rc = -EPERM);
2635 count = data->ioc_count;
2636 cmd = data->ioc_command;
2638 id = data->ioc_obdo2.o_id;
2640 dirlen = data->ioc_plen1;
2641 OBD_ALLOC(dir, dirlen + 1);
2643 GOTO(out, rc = -ENOMEM);
2645 if (cfs_copy_from_user(dir, data->ioc_pbuf1, dirlen)) {
2646 OBD_FREE(dir, data->ioc_plen1 + 1);
2647 GOTO(out, rc = -EFAULT);
2650 rc = echo_md_handler(ed, cmd, dir, dirlen, id, count, data);
2651 OBD_FREE(dir, dirlen + 1);
2654 case OBD_IOC_ECHO_ALLOC_SEQ: {
2660 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2661 GOTO(out, rc = -EPERM);
2663 env = cl_env_get(&refcheck);
2665 GOTO(out, rc = PTR_ERR(env));
2667 rc = lu_env_refill_by_tags(env, ECHO_MD_CTX_TAG,
2670 cl_env_put(env, &refcheck);
2674 rc = seq_client_get_seq(env, ed->ed_cl_seq, &seq);
2675 cl_env_put(env, &refcheck);
2677 CERROR("%s: Can not alloc seq: rc = %d\n",
2682 if (cfs_copy_to_user(data->ioc_pbuf1, &seq, data->ioc_plen1))
2685 max_count = LUSTRE_SEQ_MAX_WIDTH;
2686 if (cfs_copy_to_user(data->ioc_pbuf2, &max_count,
2691 case OBD_IOC_DESTROY:
2692 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2693 GOTO (out, rc = -EPERM);
2695 rc = echo_get_object (&eco, ed, oa);
2697 rc = obd_destroy(ec->ec_exp, oa, eco->eo_lsm,
2698 &dummy_oti, NULL, NULL);
2700 eco->eo_deleted = 1;
2701 echo_put_object(eco);
2705 case OBD_IOC_GETATTR:
2706 rc = echo_get_object (&eco, ed, oa);
2708 struct obd_info oinfo = { { { 0 } } };
2709 oinfo.oi_md = eco->eo_lsm;
2711 rc = obd_getattr(ec->ec_exp, &oinfo);
2712 echo_put_object(eco);
2716 case OBD_IOC_SETATTR:
2717 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2718 GOTO (out, rc = -EPERM);
2720 rc = echo_get_object (&eco, ed, oa);
2722 struct obd_info oinfo = { { { 0 } } };
2724 oinfo.oi_md = eco->eo_lsm;
2726 rc = obd_setattr(ec->ec_exp, &oinfo, NULL);
2727 echo_put_object(eco);
2731 case OBD_IOC_BRW_WRITE:
2732 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2733 GOTO (out, rc = -EPERM);
2737 case OBD_IOC_BRW_READ:
2738 rc = echo_client_brw_ioctl(rw, exp, data);
2741 case ECHO_IOC_GET_STRIPE:
2742 rc = echo_get_object(&eco, ed, oa);
2744 rc = echo_copyout_lsm(eco->eo_lsm, data->ioc_pbuf1,
2746 echo_put_object(eco);
2750 case ECHO_IOC_SET_STRIPE:
2751 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2752 GOTO (out, rc = -EPERM);
2754 if (data->ioc_pbuf1 == NULL) { /* unset */
2755 rc = echo_get_object(&eco, ed, oa);
2757 eco->eo_deleted = 1;
2758 echo_put_object(eco);
2761 rc = echo_create_object(ed, 0, oa,
2763 data->ioc_plen1, &dummy_oti);
2767 case ECHO_IOC_ENQUEUE:
2768 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2769 GOTO (out, rc = -EPERM);
2771 rc = echo_client_enqueue(exp, oa,
2772 data->ioc_conn1, /* lock mode */
2774 data->ioc_count);/*extent*/
2777 case ECHO_IOC_CANCEL:
2778 rc = echo_client_cancel(exp, oa);
2782 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
2783 GOTO (out, rc = -ENOTTY);
2789 /* XXX this should be in a helper also called by target_send_reply */
2790 for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
2792 if (!ack_lock->mode)
2794 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
2797 #ifndef HAVE_UNLOCKED_IOCTL
2804 static int echo_client_setup(const struct lu_env *env,
2805 struct obd_device *obddev, struct lustre_cfg *lcfg)
2807 struct echo_client_obd *ec = &obddev->u.echo_client;
2808 struct obd_device *tgt;
2809 struct obd_uuid echo_uuid = { "ECHO_UUID" };
2810 struct obd_connect_data *ocd = NULL;
2814 if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
2815 CERROR("requires a TARGET OBD name\n");
2819 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
2820 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
2821 CERROR("device not attached or not set up (%s)\n",
2822 lustre_cfg_string(lcfg, 1));
2826 cfs_spin_lock_init (&ec->ec_lock);
2827 CFS_INIT_LIST_HEAD (&ec->ec_objects);
2828 CFS_INIT_LIST_HEAD (&ec->ec_locks);
2830 ec->ec_nstripes = 0;
2832 if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
2833 lu_context_tags_update(ECHO_MD_CTX_TAG);
2834 lu_session_tags_update(ECHO_MD_SES_TAG);
2838 OBD_ALLOC(ocd, sizeof(*ocd));
2840 CERROR("Can't alloc ocd connecting to %s\n",
2841 lustre_cfg_string(lcfg, 1));
2845 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
2846 OBD_CONNECT_GRANT | OBD_CONNECT_FULL20 |
2847 OBD_CONNECT_64BITHASH;
2848 ocd->ocd_version = LUSTRE_VERSION_CODE;
2849 ocd->ocd_group = FID_SEQ_ECHO;
2851 rc = obd_connect(env, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
2853 /* Turn off pinger because it connects to tgt obd directly. */
2854 cfs_spin_lock(&tgt->obd_dev_lock);
2855 cfs_list_del_init(&ec->ec_exp->exp_obd_chain_timed);
2856 cfs_spin_unlock(&tgt->obd_dev_lock);
2859 OBD_FREE(ocd, sizeof(*ocd));
2862 CERROR("fail to connect to device %s\n",
2863 lustre_cfg_string(lcfg, 1));
2870 static int echo_client_cleanup(struct obd_device *obddev)
2872 struct echo_device *ed = obd2echo_dev(obddev);
2873 struct echo_client_obd *ec = &obddev->u.echo_client;
2877 /*Do nothing for Metadata echo client*/
2881 if (ed->ed_next_ismd) {
2882 lu_context_tags_clear(ECHO_MD_CTX_TAG);
2883 lu_session_tags_clear(ECHO_MD_SES_TAG);
2887 if (!cfs_list_empty(&obddev->obd_exports)) {
2888 CERROR("still has clients!\n");
2892 LASSERT(cfs_atomic_read(&ec->ec_exp->exp_refcount) > 0);
2893 rc = obd_disconnect(ec->ec_exp);
2895 CERROR("fail to disconnect device: %d\n", rc);
2900 static int echo_client_connect(const struct lu_env *env,
2901 struct obd_export **exp,
2902 struct obd_device *src, struct obd_uuid *cluuid,
2903 struct obd_connect_data *data, void *localdata)
2906 struct lustre_handle conn = { 0 };
2909 rc = class_connect(&conn, src, cluuid);
2911 *exp = class_conn2export(&conn);
2917 static int echo_client_disconnect(struct obd_export *exp)
2920 struct obd_device *obd;
2921 struct echo_client_obd *ec;
2922 struct ec_lock *ecl;
2928 GOTO(out, rc = -EINVAL);
2932 ec = &obd->u.echo_client;
2934 /* no more contention on export's lock list */
2935 while (!cfs_list_empty (&exp->exp_ec_data.eced_locks)) {
2936 ecl = cfs_list_entry (exp->exp_ec_data.eced_locks.next,
2937 struct ec_lock, ecl_exp_chain);
2938 cfs_list_del (&ecl->ecl_exp_chain);
2940 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm,
2941 ecl->ecl_mode, &ecl->ecl_lock_handle);
2943 CDEBUG (D_INFO, "Cancel lock on object "LPX64" on disconnect "
2944 "(%d)\n", ecl->ecl_object->eco_id, rc);
2946 echo_put_object (ecl->ecl_object);
2947 OBD_FREE (ecl, sizeof (*ecl));
2951 rc = class_disconnect(exp);
2957 static struct obd_ops echo_obd_ops = {
2958 .o_owner = THIS_MODULE,
2961 .o_setup = echo_client_setup,
2962 .o_cleanup = echo_client_cleanup,
2965 .o_iocontrol = echo_client_iocontrol,
2966 .o_connect = echo_client_connect,
2967 .o_disconnect = echo_client_disconnect
2970 int echo_client_init(void)
2972 struct lprocfs_static_vars lvars = { 0 };
2975 lprocfs_echo_init_vars(&lvars);
2977 rc = lu_kmem_init(echo_caches);
2979 rc = class_register_type(&echo_obd_ops, NULL,
2981 LUSTRE_ECHO_CLIENT_NAME,
2984 lu_kmem_fini(echo_caches);
2989 void echo_client_exit(void)
2991 class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
2992 lu_kmem_fini(echo_caches);
2995 /** @} echo_client */