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, Whamcloud, Inc.
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>
54 #include "echo_internal.h"
56 /** \defgroup echo_client Echo Client
61 struct cl_device ed_cl;
62 struct echo_client_obd *ed_ec;
64 struct cl_site ed_site_myself;
65 struct cl_site *ed_site;
66 struct lu_device *ed_next;
69 struct lu_client_seq *ed_cl_seq;
73 struct cl_object eo_cl;
74 struct cl_object_header eo_hdr;
76 struct echo_device *eo_dev;
77 cfs_list_t eo_obj_chain;
78 struct lov_stripe_md *eo_lsm;
79 cfs_atomic_t eo_npages;
83 struct echo_object_conf {
84 struct cl_object_conf eoc_cl;
85 struct lov_stripe_md **eoc_md;
89 struct cl_page_slice ep_cl;
91 cfs_page_t *ep_vmpage;
95 struct cl_lock_slice el_cl;
97 struct echo_object *el_object;
99 cfs_atomic_t el_refcount;
103 struct cl_io_slice ei_cl;
108 struct cl_req_slice er_cl;
112 static int echo_client_setup(const struct lu_env *env,
113 struct obd_device *obddev,
114 struct lustre_cfg *lcfg);
115 static int echo_client_cleanup(struct obd_device *obddev);
118 /** \defgroup echo_helpers Helper functions
121 static inline struct echo_device *cl2echo_dev(const struct cl_device *dev)
123 return container_of0(dev, struct echo_device, ed_cl);
126 static inline struct cl_device *echo_dev2cl(struct echo_device *d)
131 static inline struct echo_device *obd2echo_dev(const struct obd_device *obd)
133 return cl2echo_dev(lu2cl_dev(obd->obd_lu_dev));
136 static inline struct cl_object *echo_obj2cl(struct echo_object *eco)
141 static inline struct echo_object *cl2echo_obj(const struct cl_object *o)
143 return container_of(o, struct echo_object, eo_cl);
146 static inline struct echo_page *cl2echo_page(const struct cl_page_slice *s)
148 return container_of(s, struct echo_page, ep_cl);
151 static inline struct echo_lock *cl2echo_lock(const struct cl_lock_slice *s)
153 return container_of(s, struct echo_lock, el_cl);
156 static inline struct cl_lock *echo_lock2cl(const struct echo_lock *ecl)
158 return ecl->el_cl.cls_lock;
161 static struct lu_context_key echo_thread_key;
162 static inline struct echo_thread_info *echo_env_info(const struct lu_env *env)
164 struct echo_thread_info *info;
165 info = lu_context_key_get(&env->le_ctx, &echo_thread_key);
166 LASSERT(info != NULL);
171 struct echo_object_conf *cl2echo_conf(const struct cl_object_conf *c)
173 return container_of(c, struct echo_object_conf, eoc_cl);
176 static inline void lsm2fid(struct lov_stripe_md *lsm, struct lu_fid *fid)
179 fid->f_seq = FID_SEQ_ECHO;
180 /* truncated to 32 bits by assignment */
181 fid->f_oid = lsm->lsm_object_id;
182 fid->f_ver = lsm->lsm_object_id >> 32;
184 /** @} echo_helpers */
186 static struct echo_object *cl_echo_object_find(struct echo_device *d,
187 struct lov_stripe_md **lsm);
188 static int cl_echo_object_put(struct echo_object *eco);
189 static int cl_echo_enqueue (struct echo_object *eco, obd_off start,
190 obd_off end, int mode, __u64 *cookie);
191 static int cl_echo_cancel (struct echo_device *d, __u64 cookie);
192 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
193 cfs_page_t **pages, int npages, int async);
195 static struct echo_thread_info *echo_env_info(const struct lu_env *env);
197 struct echo_thread_info {
198 struct echo_object_conf eti_conf;
199 struct lustre_md eti_md;
201 struct cl_2queue eti_queue;
203 struct cl_lock_descr eti_descr;
204 struct lu_fid eti_fid;
205 struct md_op_spec eti_spec;
206 struct lov_mds_md_v3 eti_lmm;
207 struct lov_user_md_v3 eti_lum;
208 struct md_attr eti_ma;
209 struct lu_name eti_lname;
211 struct lu_buf eti_buf;
212 char eti_xattr_buf[LUSTRE_POSIX_ACL_MAX_SIZE];
215 /* No session used right now */
216 struct echo_session_info {
220 static cfs_mem_cache_t *echo_page_kmem;
221 static cfs_mem_cache_t *echo_lock_kmem;
222 static cfs_mem_cache_t *echo_object_kmem;
223 static cfs_mem_cache_t *echo_thread_kmem;
224 static cfs_mem_cache_t *echo_session_kmem;
225 //static cfs_mem_cache_t *echo_req_kmem;
227 static struct lu_kmem_descr echo_caches[] = {
229 .ckd_cache = &echo_page_kmem,
230 .ckd_name = "echo_page_kmem",
231 .ckd_size = sizeof (struct echo_page)
234 .ckd_cache = &echo_lock_kmem,
235 .ckd_name = "echo_lock_kmem",
236 .ckd_size = sizeof (struct echo_lock)
239 .ckd_cache = &echo_object_kmem,
240 .ckd_name = "echo_object_kmem",
241 .ckd_size = sizeof (struct echo_object)
244 .ckd_cache = &echo_thread_kmem,
245 .ckd_name = "echo_thread_kmem",
246 .ckd_size = sizeof (struct echo_thread_info)
249 .ckd_cache = &echo_session_kmem,
250 .ckd_name = "echo_session_kmem",
251 .ckd_size = sizeof (struct echo_session_info)
255 .ckd_cache = &echo_req_kmem,
256 .ckd_name = "echo_req_kmem",
257 .ckd_size = sizeof (struct echo_req)
265 /** \defgroup echo_page Page operations
267 * Echo page operations.
271 static cfs_page_t *echo_page_vmpage(const struct lu_env *env,
272 const struct cl_page_slice *slice)
274 return cl2echo_page(slice)->ep_vmpage;
277 static int echo_page_own(const struct lu_env *env,
278 const struct cl_page_slice *slice,
279 struct cl_io *io, int nonblock)
281 struct echo_page *ep = cl2echo_page(slice);
284 cfs_mutex_lock(&ep->ep_lock);
285 else if (!cfs_mutex_trylock(&ep->ep_lock))
290 static void echo_page_disown(const struct lu_env *env,
291 const struct cl_page_slice *slice,
294 struct echo_page *ep = cl2echo_page(slice);
296 LASSERT(cfs_mutex_is_locked(&ep->ep_lock));
297 cfs_mutex_unlock(&ep->ep_lock);
300 static void echo_page_discard(const struct lu_env *env,
301 const struct cl_page_slice *slice,
302 struct cl_io *unused)
304 cl_page_delete(env, slice->cpl_page);
307 static int echo_page_is_vmlocked(const struct lu_env *env,
308 const struct cl_page_slice *slice)
310 if (cfs_mutex_is_locked(&cl2echo_page(slice)->ep_lock))
315 static void echo_page_completion(const struct lu_env *env,
316 const struct cl_page_slice *slice,
319 LASSERT(slice->cpl_page->cp_sync_io != NULL);
322 static void echo_page_fini(const struct lu_env *env,
323 struct cl_page_slice *slice)
325 struct echo_page *ep = cl2echo_page(slice);
326 struct echo_object *eco = cl2echo_obj(slice->cpl_obj);
327 cfs_page_t *vmpage = ep->ep_vmpage;
330 cfs_atomic_dec(&eco->eo_npages);
331 page_cache_release(vmpage);
332 OBD_SLAB_FREE_PTR(ep, echo_page_kmem);
336 static int echo_page_prep(const struct lu_env *env,
337 const struct cl_page_slice *slice,
338 struct cl_io *unused)
343 static int echo_page_print(const struct lu_env *env,
344 const struct cl_page_slice *slice,
345 void *cookie, lu_printer_t printer)
347 struct echo_page *ep = cl2echo_page(slice);
349 (*printer)(env, cookie, LUSTRE_ECHO_CLIENT_NAME"-page@%p %d vm@%p\n",
350 ep, cfs_mutex_is_locked(&ep->ep_lock), ep->ep_vmpage);
354 static const struct cl_page_operations echo_page_ops = {
355 .cpo_own = echo_page_own,
356 .cpo_disown = echo_page_disown,
357 .cpo_discard = echo_page_discard,
358 .cpo_vmpage = echo_page_vmpage,
359 .cpo_fini = echo_page_fini,
360 .cpo_print = echo_page_print,
361 .cpo_is_vmlocked = echo_page_is_vmlocked,
364 .cpo_prep = echo_page_prep,
365 .cpo_completion = echo_page_completion,
368 .cpo_prep = echo_page_prep,
369 .cpo_completion = echo_page_completion,
375 /** \defgroup echo_lock Locking
377 * echo lock operations
381 static void echo_lock_fini(const struct lu_env *env,
382 struct cl_lock_slice *slice)
384 struct echo_lock *ecl = cl2echo_lock(slice);
386 LASSERT(cfs_list_empty(&ecl->el_chain));
387 OBD_SLAB_FREE_PTR(ecl, echo_lock_kmem);
390 static void echo_lock_delete(const struct lu_env *env,
391 const struct cl_lock_slice *slice)
393 struct echo_lock *ecl = cl2echo_lock(slice);
395 LASSERT(cfs_list_empty(&ecl->el_chain));
398 static int echo_lock_fits_into(const struct lu_env *env,
399 const struct cl_lock_slice *slice,
400 const struct cl_lock_descr *need,
401 const struct cl_io *unused)
406 static struct cl_lock_operations echo_lock_ops = {
407 .clo_fini = echo_lock_fini,
408 .clo_delete = echo_lock_delete,
409 .clo_fits_into = echo_lock_fits_into
414 /** \defgroup echo_cl_ops cl_object operations
416 * operations for cl_object
420 static struct cl_page *echo_page_init(const struct lu_env *env,
421 struct cl_object *obj,
422 struct cl_page *page, cfs_page_t *vmpage)
424 struct echo_page *ep;
427 OBD_SLAB_ALLOC_PTR_GFP(ep, echo_page_kmem, CFS_ALLOC_IO);
429 struct echo_object *eco = cl2echo_obj(obj);
430 ep->ep_vmpage = vmpage;
431 page_cache_get(vmpage);
432 cfs_mutex_init(&ep->ep_lock);
433 cl_page_slice_add(page, &ep->ep_cl, obj, &echo_page_ops);
434 cfs_atomic_inc(&eco->eo_npages);
436 RETURN(ERR_PTR(ep ? 0 : -ENOMEM));
439 static int echo_io_init(const struct lu_env *env, struct cl_object *obj,
445 static int echo_lock_init(const struct lu_env *env,
446 struct cl_object *obj, struct cl_lock *lock,
447 const struct cl_io *unused)
449 struct echo_lock *el;
452 OBD_SLAB_ALLOC_PTR_GFP(el, echo_lock_kmem, CFS_ALLOC_IO);
454 cl_lock_slice_add(lock, &el->el_cl, obj, &echo_lock_ops);
455 el->el_object = cl2echo_obj(obj);
456 CFS_INIT_LIST_HEAD(&el->el_chain);
457 cfs_atomic_set(&el->el_refcount, 0);
459 RETURN(el == NULL ? -ENOMEM : 0);
462 static int echo_conf_set(const struct lu_env *env, struct cl_object *obj,
463 const struct cl_object_conf *conf)
468 static const struct cl_object_operations echo_cl_obj_ops = {
469 .coo_page_init = echo_page_init,
470 .coo_lock_init = echo_lock_init,
471 .coo_io_init = echo_io_init,
472 .coo_conf_set = echo_conf_set
474 /** @} echo_cl_ops */
476 /** \defgroup echo_lu_ops lu_object operations
478 * operations for echo lu object.
482 static int echo_object_init(const struct lu_env *env, struct lu_object *obj,
483 const struct lu_object_conf *conf)
485 struct echo_device *ed = cl2echo_dev(lu2cl_dev(obj->lo_dev));
486 struct echo_client_obd *ec = ed->ed_ec;
487 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
491 struct lu_object *below;
492 struct lu_device *under;
495 below = under->ld_ops->ldo_object_alloc(env, obj->lo_header,
499 lu_object_add(obj, below);
502 if (!ed->ed_next_ismd) {
503 const struct cl_object_conf *cconf = lu2cl_conf(conf);
504 struct echo_object_conf *econf = cl2echo_conf(cconf);
506 LASSERT(econf->eoc_md);
507 eco->eo_lsm = *econf->eoc_md;
508 /* clear the lsm pointer so that it won't get freed. */
509 *econf->eoc_md = NULL;
515 cfs_atomic_set(&eco->eo_npages, 0);
517 cfs_spin_lock(&ec->ec_lock);
518 cfs_list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
519 cfs_spin_unlock(&ec->ec_lock);
524 static void echo_object_free(const struct lu_env *env, struct lu_object *obj)
526 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
527 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
528 struct lov_stripe_md *lsm = eco->eo_lsm;
531 LASSERT(cfs_atomic_read(&eco->eo_npages) == 0);
533 cfs_spin_lock(&ec->ec_lock);
534 cfs_list_del_init(&eco->eo_obj_chain);
535 cfs_spin_unlock(&ec->ec_lock);
538 lu_object_header_fini(obj->lo_header);
541 obd_free_memmd(ec->ec_exp, &lsm);
542 OBD_SLAB_FREE_PTR(eco, echo_object_kmem);
546 static int echo_object_print(const struct lu_env *env, void *cookie,
547 lu_printer_t p, const struct lu_object *o)
549 struct echo_object *obj = cl2echo_obj(lu2cl(o));
551 return (*p)(env, cookie, "echoclient-object@%p", obj);
554 static const struct lu_object_operations echo_lu_obj_ops = {
555 .loo_object_init = echo_object_init,
556 .loo_object_delete = NULL,
557 .loo_object_release = NULL,
558 .loo_object_free = echo_object_free,
559 .loo_object_print = echo_object_print,
560 .loo_object_invariant = NULL
562 /** @} echo_lu_ops */
564 /** \defgroup echo_lu_dev_ops lu_device operations
566 * Operations for echo lu device.
570 static struct lu_object *echo_object_alloc(const struct lu_env *env,
571 const struct lu_object_header *hdr,
572 struct lu_device *dev)
574 struct echo_object *eco;
575 struct lu_object *obj = NULL;
578 /* we're the top dev. */
579 LASSERT(hdr == NULL);
580 OBD_SLAB_ALLOC_PTR_GFP(eco, echo_object_kmem, CFS_ALLOC_IO);
582 struct cl_object_header *hdr = &eco->eo_hdr;
584 obj = &echo_obj2cl(eco)->co_lu;
585 cl_object_header_init(hdr);
586 lu_object_init(obj, &hdr->coh_lu, dev);
587 lu_object_add_top(&hdr->coh_lu, obj);
589 eco->eo_cl.co_ops = &echo_cl_obj_ops;
590 obj->lo_ops = &echo_lu_obj_ops;
595 static struct lu_device_operations echo_device_lu_ops = {
596 .ldo_object_alloc = echo_object_alloc,
599 /** @} echo_lu_dev_ops */
601 static struct cl_device_operations echo_device_cl_ops = {
604 /** \defgroup echo_init Setup and teardown
606 * Init and fini functions for echo client.
610 static int echo_site_init(const struct lu_env *env, struct echo_device *ed)
612 struct cl_site *site = &ed->ed_site_myself;
615 /* initialize site */
616 rc = cl_site_init(site, &ed->ed_cl);
618 CERROR("Cannot initilize site for echo client(%d)\n", rc);
622 rc = lu_site_init_finish(&site->cs_lu);
630 static void echo_site_fini(const struct lu_env *env, struct echo_device *ed)
633 if (!ed->ed_next_ismd)
634 cl_site_fini(ed->ed_site);
639 static void *echo_thread_key_init(const struct lu_context *ctx,
640 struct lu_context_key *key)
642 struct echo_thread_info *info;
644 OBD_SLAB_ALLOC_PTR_GFP(info, echo_thread_kmem, CFS_ALLOC_IO);
646 info = ERR_PTR(-ENOMEM);
650 static void echo_thread_key_fini(const struct lu_context *ctx,
651 struct lu_context_key *key, void *data)
653 struct echo_thread_info *info = data;
654 OBD_SLAB_FREE_PTR(info, echo_thread_kmem);
657 static void echo_thread_key_exit(const struct lu_context *ctx,
658 struct lu_context_key *key, void *data)
662 static struct lu_context_key echo_thread_key = {
663 .lct_tags = LCT_CL_THREAD,
664 .lct_init = echo_thread_key_init,
665 .lct_fini = echo_thread_key_fini,
666 .lct_exit = echo_thread_key_exit
669 static void *echo_session_key_init(const struct lu_context *ctx,
670 struct lu_context_key *key)
672 struct echo_session_info *session;
674 OBD_SLAB_ALLOC_PTR_GFP(session, echo_session_kmem, CFS_ALLOC_IO);
676 session = ERR_PTR(-ENOMEM);
680 static void echo_session_key_fini(const struct lu_context *ctx,
681 struct lu_context_key *key, void *data)
683 struct echo_session_info *session = data;
684 OBD_SLAB_FREE_PTR(session, echo_session_kmem);
687 static void echo_session_key_exit(const struct lu_context *ctx,
688 struct lu_context_key *key, void *data)
692 static struct lu_context_key echo_session_key = {
693 .lct_tags = LCT_SESSION,
694 .lct_init = echo_session_key_init,
695 .lct_fini = echo_session_key_fini,
696 .lct_exit = echo_session_key_exit
699 LU_TYPE_INIT_FINI(echo, &echo_thread_key, &echo_session_key);
701 #define ECHO_SEQ_WIDTH 0xffffffff
702 static int echo_fid_init(struct echo_device *ed, char *obd_name,
709 OBD_ALLOC_PTR(ed->ed_cl_seq);
710 if (ed->ed_cl_seq == NULL)
713 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
715 GOTO(out_free_seq, rc = -ENOMEM);
717 snprintf(prefix, MAX_OBD_NAME + 5, "srv-%s", obd_name);
719 /* Init client side sequence-manager */
720 rc = seq_client_init(ed->ed_cl_seq, NULL,
722 prefix, ms->ms_server_seq);
723 ed->ed_cl_seq->lcs_width = ECHO_SEQ_WIDTH;
724 OBD_FREE(prefix, MAX_OBD_NAME + 5);
726 GOTO(out_free_seq, rc);
731 OBD_FREE_PTR(ed->ed_cl_seq);
732 ed->ed_cl_seq = NULL;
736 static int echo_fid_fini(struct obd_device *obddev)
738 struct echo_device *ed = obd2echo_dev(obddev);
741 if (ed->ed_cl_seq != NULL) {
742 seq_client_fini(ed->ed_cl_seq);
743 OBD_FREE_PTR(ed->ed_cl_seq);
744 ed->ed_cl_seq = NULL;
750 static struct lu_device *echo_device_alloc(const struct lu_env *env,
751 struct lu_device_type *t,
752 struct lustre_cfg *cfg)
754 struct lu_device *next;
755 struct echo_device *ed;
756 struct cl_device *cd;
757 struct obd_device *obd = NULL; /* to keep compiler happy */
758 struct obd_device *tgt;
759 const char *tgt_type_name;
766 GOTO(out, rc = -ENOMEM);
770 rc = cl_device_init(cd, t);
774 cd->cd_lu_dev.ld_ops = &echo_device_lu_ops;
775 cd->cd_ops = &echo_device_cl_ops;
778 obd = class_name2obd(lustre_cfg_string(cfg, 0));
779 LASSERT(obd != NULL);
780 LASSERT(env != NULL);
782 tgt = class_name2obd(lustre_cfg_string(cfg, 1));
784 CERROR("Can not find tgt device %s\n",
785 lustre_cfg_string(cfg, 1));
786 GOTO(out, rc = -ENODEV);
789 next = tgt->obd_lu_dev;
790 if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
791 ed->ed_next_ismd = 1;
793 ed->ed_next_ismd = 0;
794 rc = echo_site_init(env, ed);
800 rc = echo_client_setup(env, obd, cfg);
804 ed->ed_ec = &obd->u.echo_client;
807 if (ed->ed_next_ismd) {
808 /* Suppose to connect to some Metadata layer */
810 struct lu_device *ld;
814 CERROR("%s is not lu device type!\n",
815 lustre_cfg_string(cfg, 1));
816 GOTO(out, rc = -EINVAL);
819 tgt_type_name = lustre_cfg_string(cfg, 2);
820 if (!tgt_type_name) {
821 CERROR("%s no type name for echo %s setup\n",
822 lustre_cfg_string(cfg, 1),
823 tgt->obd_type->typ_name);
824 GOTO(out, rc = -EINVAL);
829 cfs_spin_lock(&ls->ls_ld_lock);
830 cfs_list_for_each_entry(ld, &ls->ls_ld_linkage, ld_linkage) {
831 if (strcmp(ld->ld_type->ldt_name, tgt_type_name) == 0) {
836 cfs_spin_unlock(&ls->ls_ld_lock);
839 CERROR("%s is not lu device type!\n",
840 lustre_cfg_string(cfg, 1));
841 GOTO(out, rc = -EINVAL);
845 /* For MD echo client, it will use the site in MDS stack */
846 ed->ed_site_myself.cs_lu = *ls;
847 ed->ed_site = &ed->ed_site_myself;
848 ed->ed_cl.cd_lu_dev.ld_site = &ed->ed_site_myself.cs_lu;
849 rc = echo_fid_init(ed, obd->obd_name, lu_site2md(ls));
851 CERROR("echo fid init error %d\n", rc);
855 /* if echo client is to be stacked upon ost device, the next is
856 * NULL since ost is not a clio device so far */
857 if (next != NULL && !lu_device_is_cl(next))
860 tgt_type_name = tgt->obd_type->typ_name;
862 LASSERT(next != NULL);
863 if (next->ld_site != NULL)
864 GOTO(out, rc = -EBUSY);
866 next->ld_site = &ed->ed_site->cs_lu;
867 rc = next->ld_type->ldt_ops->ldto_device_init(env, next,
868 next->ld_type->ldt_name,
873 /* Tricky case, I have to determine the obd type since
874 * CLIO uses the different parameters to initialize
875 * objects for lov & osc. */
876 if (strcmp(tgt_type_name, LUSTRE_LOV_NAME) == 0)
877 ed->ed_next_islov = 1;
879 LASSERT(strcmp(tgt_type_name,
880 LUSTRE_OSC_NAME) == 0);
882 LASSERT(strcmp(tgt_type_name, LUSTRE_OST_NAME) == 0);
886 RETURN(&cd->cd_lu_dev);
891 rc2 = echo_client_cleanup(obd);
893 CERROR("Cleanup obd device %s error(%d)\n",
898 echo_site_fini(env, ed);
900 cl_device_fini(&ed->ed_cl);
910 static int echo_device_init(const struct lu_env *env, struct lu_device *d,
911 const char *name, struct lu_device *next)
917 static struct lu_device *echo_device_fini(const struct lu_env *env,
920 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
921 struct lu_device *next = ed->ed_next;
923 while (next && !ed->ed_next_ismd)
924 next = next->ld_type->ldt_ops->ldto_device_fini(env, next);
928 static void echo_lock_release(const struct lu_env *env,
929 struct echo_lock *ecl,
932 struct cl_lock *clk = echo_lock2cl(ecl);
936 cl_lock_release(env, clk, "ec enqueue", ecl->el_object);
938 cl_lock_mutex_get(env, clk);
939 cl_lock_cancel(env, clk);
940 cl_lock_delete(env, clk);
941 cl_lock_mutex_put(env, clk);
943 cl_lock_put(env, clk);
946 static struct lu_device *echo_device_free(const struct lu_env *env,
949 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
950 struct echo_client_obd *ec = ed->ed_ec;
951 struct echo_object *eco;
952 struct lu_device *next = ed->ed_next;
954 CDEBUG(D_INFO, "echo device:%p is going to be freed, next = %p\n",
957 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
959 /* check if there are objects still alive.
960 * It shouldn't have any object because lu_site_purge would cleanup
961 * all of cached objects. Anyway, probably the echo device is being
962 * parallelly accessed.
964 cfs_spin_lock(&ec->ec_lock);
965 cfs_list_for_each_entry(eco, &ec->ec_objects, eo_obj_chain)
967 cfs_spin_unlock(&ec->ec_lock);
970 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
973 "Waiting for the reference of echo object to be dropped\n");
975 /* Wait for the last reference to be dropped. */
976 cfs_spin_lock(&ec->ec_lock);
977 while (!cfs_list_empty(&ec->ec_objects)) {
978 cfs_spin_unlock(&ec->ec_lock);
979 CERROR("echo_client still has objects at cleanup time, "
980 "wait for 1 second\n");
981 cfs_schedule_timeout_and_set_state(CFS_TASK_UNINT,
982 cfs_time_seconds(1));
983 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
984 cfs_spin_lock(&ec->ec_lock);
986 cfs_spin_unlock(&ec->ec_lock);
988 LASSERT(cfs_list_empty(&ec->ec_locks));
990 CDEBUG(D_INFO, "No object exists, exiting...\n");
992 echo_client_cleanup(d->ld_obd);
993 echo_fid_fini(d->ld_obd);
994 while (next && !ed->ed_next_ismd)
995 next = next->ld_type->ldt_ops->ldto_device_free(env, next);
997 LASSERT(ed->ed_site == lu2cl_site(d->ld_site));
998 echo_site_fini(env, ed);
999 cl_device_fini(&ed->ed_cl);
1005 static const struct lu_device_type_operations echo_device_type_ops = {
1006 .ldto_init = echo_type_init,
1007 .ldto_fini = echo_type_fini,
1009 .ldto_start = echo_type_start,
1010 .ldto_stop = echo_type_stop,
1012 .ldto_device_alloc = echo_device_alloc,
1013 .ldto_device_free = echo_device_free,
1014 .ldto_device_init = echo_device_init,
1015 .ldto_device_fini = echo_device_fini
1018 static struct lu_device_type echo_device_type = {
1019 .ldt_tags = LU_DEVICE_CL,
1020 .ldt_name = LUSTRE_ECHO_CLIENT_NAME,
1021 .ldt_ops = &echo_device_type_ops,
1022 .ldt_ctx_tags = LCT_CL_THREAD | LCT_MD_THREAD | LCT_DT_THREAD,
1026 /** \defgroup echo_exports Exported operations
1028 * exporting functions to echo client
1033 /* Interfaces to echo client obd device */
1034 static struct echo_object *cl_echo_object_find(struct echo_device *d,
1035 struct lov_stripe_md **lsmp)
1038 struct echo_thread_info *info;
1039 struct echo_object_conf *conf;
1040 struct lov_stripe_md *lsm;
1041 struct echo_object *eco;
1042 struct cl_object *obj;
1050 LASSERT(lsm->lsm_object_id);
1052 /* Never return an object if the obd is to be freed. */
1053 if (echo_dev2cl(d)->cd_lu_dev.ld_obd->obd_stopping)
1054 RETURN(ERR_PTR(-ENODEV));
1056 env = cl_env_get(&refcheck);
1058 RETURN((void *)env);
1060 info = echo_env_info(env);
1061 conf = &info->eti_conf;
1063 if (!d->ed_next_islov) {
1064 struct lov_oinfo *oinfo = lsm->lsm_oinfo[0];
1065 LASSERT(oinfo != NULL);
1066 oinfo->loi_id = lsm->lsm_object_id;
1067 oinfo->loi_seq = lsm->lsm_object_seq;
1068 conf->eoc_cl.u.coc_oinfo = oinfo;
1070 struct lustre_md *md;
1072 memset(md, 0, sizeof *md);
1074 conf->eoc_cl.u.coc_md = md;
1077 conf->eoc_md = lsmp;
1079 fid = &info->eti_fid;
1082 obj = cl_object_find(env, echo_dev2cl(d), fid, &conf->eoc_cl);
1084 GOTO(out, eco = (void*)obj);
1086 eco = cl2echo_obj(obj);
1087 if (eco->eo_deleted) {
1088 cl_object_put(env, obj);
1089 eco = ERR_PTR(-EAGAIN);
1093 cl_env_put(env, &refcheck);
1097 static int cl_echo_object_put(struct echo_object *eco)
1100 struct cl_object *obj = echo_obj2cl(eco);
1104 env = cl_env_get(&refcheck);
1106 RETURN(PTR_ERR(env));
1108 /* an external function to kill an object? */
1109 if (eco->eo_deleted) {
1110 struct lu_object_header *loh = obj->co_lu.lo_header;
1111 LASSERT(&eco->eo_hdr == luh2coh(loh));
1112 cfs_set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
1115 cl_object_put(env, obj);
1116 cl_env_put(env, &refcheck);
1120 static int cl_echo_enqueue0(struct lu_env *env, struct echo_object *eco,
1121 obd_off start, obd_off end, int mode,
1122 __u64 *cookie , __u32 enqflags)
1125 struct cl_lock *lck;
1126 struct cl_object *obj;
1127 struct cl_lock_descr *descr;
1128 struct echo_thread_info *info;
1132 info = echo_env_info(env);
1134 descr = &info->eti_descr;
1135 obj = echo_obj2cl(eco);
1137 descr->cld_obj = obj;
1138 descr->cld_start = cl_index(obj, start);
1139 descr->cld_end = cl_index(obj, end);
1140 descr->cld_mode = mode == LCK_PW ? CLM_WRITE : CLM_READ;
1141 descr->cld_enq_flags = enqflags;
1144 lck = cl_lock_request(env, io, descr, "ec enqueue", eco);
1146 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
1147 struct echo_lock *el;
1149 rc = cl_wait(env, lck);
1151 el = cl2echo_lock(cl_lock_at(lck, &echo_device_type));
1152 cfs_spin_lock(&ec->ec_lock);
1153 if (cfs_list_empty(&el->el_chain)) {
1154 cfs_list_add(&el->el_chain, &ec->ec_locks);
1155 el->el_cookie = ++ec->ec_unique;
1157 cfs_atomic_inc(&el->el_refcount);
1158 *cookie = el->el_cookie;
1159 cfs_spin_unlock(&ec->ec_lock);
1161 cl_lock_release(env, lck, "ec enqueue", cfs_current());
1166 static int cl_echo_enqueue(struct echo_object *eco, obd_off start, obd_off end,
1167 int mode, __u64 *cookie)
1169 struct echo_thread_info *info;
1176 env = cl_env_get(&refcheck);
1178 RETURN(PTR_ERR(env));
1180 info = echo_env_info(env);
1183 result = cl_io_init(env, io, CIT_MISC, echo_obj2cl(eco));
1186 LASSERT(result == 0);
1188 result = cl_echo_enqueue0(env, eco, start, end, mode, cookie, 0);
1189 cl_io_fini(env, io);
1193 cl_env_put(env, &refcheck);
1197 static int cl_echo_cancel0(struct lu_env *env, struct echo_device *ed,
1200 struct echo_client_obd *ec = ed->ed_ec;
1201 struct echo_lock *ecl = NULL;
1203 int found = 0, still_used = 0;
1206 LASSERT(ec != NULL);
1207 cfs_spin_lock (&ec->ec_lock);
1208 cfs_list_for_each (el, &ec->ec_locks) {
1209 ecl = cfs_list_entry (el, struct echo_lock, el_chain);
1210 CDEBUG(D_INFO, "ecl: %p, cookie: "LPX64"\n", ecl, ecl->el_cookie);
1211 found = (ecl->el_cookie == cookie);
1213 if (cfs_atomic_dec_and_test(&ecl->el_refcount))
1214 cfs_list_del_init(&ecl->el_chain);
1220 cfs_spin_unlock (&ec->ec_lock);
1225 echo_lock_release(env, ecl, still_used);
1229 static int cl_echo_cancel(struct echo_device *ed, __u64 cookie)
1236 env = cl_env_get(&refcheck);
1238 RETURN(PTR_ERR(env));
1240 rc = cl_echo_cancel0(env, ed, cookie);
1242 cl_env_put(env, &refcheck);
1246 static int cl_echo_async_brw(const struct lu_env *env, struct cl_io *io,
1247 enum cl_req_type unused, struct cl_2queue *queue)
1249 struct cl_page *clp;
1250 struct cl_page *temp;
1254 cl_page_list_for_each_safe(clp, temp, &queue->c2_qin) {
1256 rc = cl_page_cache_add(env, io, clp, CRT_WRITE);
1259 result = result ?: rc;
1264 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
1265 cfs_page_t **pages, int npages, int async)
1268 struct echo_thread_info *info;
1269 struct cl_object *obj = echo_obj2cl(eco);
1270 struct echo_device *ed = eco->eo_dev;
1271 struct cl_2queue *queue;
1273 struct cl_page *clp;
1274 struct lustre_handle lh = { 0 };
1275 int page_size = cl_page_size(obj);
1281 LASSERT((offset & ~CFS_PAGE_MASK) == 0);
1282 LASSERT(ed->ed_next != NULL);
1283 env = cl_env_get(&refcheck);
1285 RETURN(PTR_ERR(env));
1287 info = echo_env_info(env);
1289 queue = &info->eti_queue;
1291 cl_2queue_init(queue);
1292 rc = cl_io_init(env, io, CIT_MISC, obj);
1298 rc = cl_echo_enqueue0(env, eco, offset,
1299 offset + npages * CFS_PAGE_SIZE - 1,
1300 rw == READ ? LCK_PR : LCK_PW, &lh.cookie,
1303 GOTO(error_lock, rc);
1305 for (i = 0; i < npages; i++) {
1307 clp = cl_page_find(env, obj, cl_index(obj, offset),
1308 pages[i], CPT_TRANSIENT);
1313 LASSERT(clp->cp_type == CPT_TRANSIENT);
1315 rc = cl_page_own(env, io, clp);
1317 LASSERT(clp->cp_state == CPS_FREEING);
1318 cl_page_put(env, clp);
1322 cl_2queue_add(queue, clp);
1324 /* drop the reference count for cl_page_find, so that the page
1325 * will be freed in cl_2queue_fini. */
1326 cl_page_put(env, clp);
1327 cl_page_clip(env, clp, 0, page_size);
1329 offset += page_size;
1333 enum cl_req_type typ = rw == READ ? CRT_READ : CRT_WRITE;
1335 async = async && (typ == CRT_WRITE);
1337 rc = cl_echo_async_brw(env, io, typ, queue);
1339 rc = cl_io_submit_sync(env, io, typ, queue, 0);
1340 CDEBUG(D_INFO, "echo_client %s write returns %d\n",
1341 async ? "async" : "sync", rc);
1344 cl_echo_cancel0(env, ed, lh.cookie);
1347 cl_2queue_discard(env, io, queue);
1348 cl_2queue_disown(env, io, queue);
1349 cl_2queue_fini(env, queue);
1350 cl_io_fini(env, io);
1352 cl_env_put(env, &refcheck);
1355 /** @} echo_exports */
1358 static obd_id last_object_id;
1361 echo_copyout_lsm (struct lov_stripe_md *lsm, void *_ulsm, int ulsm_nob)
1363 struct lov_stripe_md *ulsm = _ulsm;
1366 nob = offsetof (struct lov_stripe_md, lsm_oinfo[lsm->lsm_stripe_count]);
1370 if (cfs_copy_to_user (ulsm, lsm, sizeof(ulsm)))
1373 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1374 if (cfs_copy_to_user (ulsm->lsm_oinfo[i], lsm->lsm_oinfo[i],
1375 sizeof(lsm->lsm_oinfo[0])))
1382 echo_copyin_lsm (struct echo_device *ed, struct lov_stripe_md *lsm,
1383 void *ulsm, int ulsm_nob)
1385 struct echo_client_obd *ec = ed->ed_ec;
1388 if (ulsm_nob < sizeof (*lsm))
1391 if (cfs_copy_from_user (lsm, ulsm, sizeof (*lsm)))
1394 if (lsm->lsm_stripe_count > ec->ec_nstripes ||
1395 lsm->lsm_magic != LOV_MAGIC ||
1396 (lsm->lsm_stripe_size & (~CFS_PAGE_MASK)) != 0 ||
1397 ((__u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count > ~0UL))
1401 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1402 if (cfs_copy_from_user(lsm->lsm_oinfo[i],
1403 ((struct lov_stripe_md *)ulsm)-> \
1405 sizeof(lsm->lsm_oinfo[0])))
1411 static inline void echo_md_build_name(struct lu_name *lname, char *name,
1414 sprintf(name, LPU64, id);
1415 lname->ln_name = name;
1416 lname->ln_namelen = strlen(name);
1419 static int echo_md_create_internal(const struct lu_env *env,
1420 struct echo_device *ed,
1421 struct md_object *parent,
1423 struct lu_name *lname,
1424 struct md_op_spec *spec,
1427 struct lu_object *ec_child, *child;
1428 struct lu_device *ld = ed->ed_next;
1431 ec_child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev,
1433 if (IS_ERR(ec_child)) {
1434 CERROR("Can not find the child "DFID": rc = %ld\n", PFID(fid),
1436 return PTR_ERR(ec_child);
1439 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1440 if (child == NULL) {
1441 CERROR("Can not locate the child "DFID"\n", PFID(fid));
1442 GOTO(out_put, rc = -EINVAL);
1445 CDEBUG(D_RPCTRACE, "Start creating object "DFID" %s %p\n",
1446 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1448 rc = mdo_create(env, parent, lname, lu2md(child), spec, ma);
1450 CERROR("Can not create child "DFID": rc = %d\n", PFID(fid), rc);
1453 CDEBUG(D_RPCTRACE, "End creating object "DFID" %s %p rc = %d\n",
1454 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent, rc);
1456 lu_object_put(env, ec_child);
1460 static int echo_set_lmm_size(const struct lu_env *env,
1461 struct lu_device *ld,
1464 struct md_device *md = lu2md_dev(ld);
1465 int lmm_size, cookie_size, rc;
1469 rc = md->md_ops->mdo_maxsize_get(env, md,
1470 &lmm_size, &cookie_size);
1474 ma->ma_lmm_size = lmm_size;
1476 OBD_ALLOC(ma->ma_lmm, lmm_size);
1477 if (ma->ma_lmm == NULL) {
1478 ma->ma_lmm_size = 0;
1483 ma->ma_cookie_size = cookie_size;
1484 if (cookie_size > 0) {
1485 OBD_ALLOC(ma->ma_cookie, cookie_size);
1486 if (ma->ma_cookie == NULL) {
1487 ma->ma_cookie_size = 0;
1495 static int echo_create_md_object(const struct lu_env *env,
1496 struct echo_device *ed,
1497 struct lu_object *ec_parent,
1499 char *name, int namelen,
1500 __u64 id, __u32 mode, int count,
1501 int stripe_count, int stripe_offset)
1503 struct lu_object *parent;
1504 struct echo_thread_info *info = echo_env_info(env);
1505 struct lu_name *lname = &info->eti_lname;
1506 struct md_op_spec *spec = &info->eti_spec;
1507 struct md_attr *ma = &info->eti_ma;
1508 struct lu_device *ld = ed->ed_next;
1512 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1513 if (ec_parent == NULL) {
1514 lu_object_put(env, ec_parent);
1515 RETURN(PTR_ERR(parent));
1518 memset(ma, 0, sizeof(*ma));
1519 memset(spec, 0, sizeof(*spec));
1520 if (stripe_count != 0) {
1521 spec->sp_cr_flags |= FMODE_WRITE;
1522 rc = echo_set_lmm_size(env, ld, ma);
1525 if (stripe_count != -1) {
1526 struct lov_user_md_v3 *lum = &info->eti_lum;
1527 lum->lmm_magic = LOV_USER_MAGIC_V3;
1528 lum->lmm_stripe_count = stripe_count;
1529 lum->lmm_stripe_offset = stripe_offset;
1530 lum->lmm_pattern = 0;
1531 spec->u.sp_ea.eadata = lum;
1532 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1536 ma->ma_attr.la_mode = mode;
1537 ma->ma_attr.la_valid = LA_CTIME;
1538 ma->ma_attr.la_ctime = cfs_time_current_64();
1541 lname->ln_name = name;
1542 lname->ln_namelen = namelen;
1543 /* If name is specified, only create one object by name */
1544 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1549 /* Create multiple object sequenced by id */
1550 for (i = 0; i < count; i++) {
1551 char *tmp_name = info->eti_name;
1553 echo_md_build_name(lname, tmp_name, id);
1555 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1558 CERROR("Can not create child %s: rc = %d\n", tmp_name,
1567 if (ma->ma_lmm_size > 0 && ma->ma_lmm != NULL)
1568 OBD_FREE(ma->ma_lmm, ma->ma_lmm_size);
1569 if (ma->ma_cookie_size > 0 && ma->ma_cookie != NULL)
1570 OBD_FREE(ma->ma_cookie, ma->ma_cookie_size);
1575 static struct lu_object *echo_md_lookup(const struct lu_env *env,
1576 struct echo_device *ed,
1577 struct md_object *parent,
1578 struct lu_name *lname)
1580 struct echo_thread_info *info = echo_env_info(env);
1581 struct lu_fid *fid = &info->eti_fid;
1582 struct lu_object *child;
1586 CDEBUG(D_INFO, "lookup %s in parent "DFID" %p\n", lname->ln_name,
1588 rc = mdo_lookup(env, parent, lname, fid, NULL);
1590 CERROR("lookup %s: rc = %d\n", lname->ln_name, rc);
1591 RETURN(ERR_PTR(rc));
1594 child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1599 static int echo_setattr_object(const struct lu_env *env,
1600 struct echo_device *ed,
1601 struct lu_object *ec_parent,
1602 __u64 id, int count)
1604 struct lu_object *parent;
1605 struct echo_thread_info *info = echo_env_info(env);
1606 struct lu_name *lname = &info->eti_lname;
1607 char *name = info->eti_name;
1608 struct lu_device *ld = ed->ed_next;
1609 struct lu_buf *buf = &info->eti_buf;
1613 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1614 if (ec_parent == NULL) {
1615 lu_object_put(env, ec_parent);
1616 return PTR_ERR(parent);
1619 buf->lb_buf = info->eti_xattr_buf;
1620 buf->lb_len = sizeof(info->eti_xattr_buf);
1621 for (i = 0; i < count; i++) {
1622 struct lu_object *ec_child, *child;
1624 echo_md_build_name(lname, name, id);
1626 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1627 if (IS_ERR(ec_child)) {
1628 CERROR("Can't find child %s: rc = %ld\n",
1629 lname->ln_name, PTR_ERR(ec_child));
1630 RETURN(PTR_ERR(ec_child));
1633 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1634 if (child == NULL) {
1635 CERROR("Can not locate the child %s\n", lname->ln_name);
1636 lu_object_put(env, ec_child);
1641 CDEBUG(D_RPCTRACE, "Start setattr object "DFID"\n",
1642 PFID(lu_object_fid(child)));
1644 sprintf(name, "%s.test1", XATTR_USER_PREFIX);
1645 rc = mo_xattr_set(env, lu2md(child), buf, name,
1648 CERROR("Can not setattr child "DFID": rc = %d\n",
1649 PFID(lu_object_fid(child)), rc);
1650 lu_object_put(env, ec_child);
1653 CDEBUG(D_RPCTRACE, "End setattr object "DFID"\n",
1654 PFID(lu_object_fid(child)));
1656 lu_object_put(env, ec_child);
1661 static int echo_getattr_object(const struct lu_env *env,
1662 struct echo_device *ed,
1663 struct lu_object *ec_parent,
1664 __u64 id, int count)
1666 struct lu_object *parent;
1667 struct echo_thread_info *info = echo_env_info(env);
1668 struct lu_name *lname = &info->eti_lname;
1669 char *name = info->eti_name;
1670 struct md_attr *ma = &info->eti_ma;
1671 struct lu_device *ld = ed->ed_next;
1675 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1676 if (ec_parent == NULL) {
1677 lu_object_put(env, ec_parent);
1678 return PTR_ERR(parent);
1681 memset(ma, 0, sizeof(*ma));
1682 rc = echo_set_lmm_size(env, ld, ma);
1686 ma->ma_need |= MA_INODE | MA_LOV | MA_PFID | MA_HSM | MA_ACL_DEF;
1687 ma->ma_acl = info->eti_xattr_buf;
1688 ma->ma_acl_size = sizeof(info->eti_xattr_buf);
1690 for (i = 0; i < count; i++) {
1691 struct lu_object *ec_child, *child;
1694 echo_md_build_name(lname, name, id);
1696 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1697 if (IS_ERR(ec_child)) {
1698 CERROR("Can't find child %s: rc = %ld\n",
1699 lname->ln_name, PTR_ERR(ec_child));
1700 RETURN(PTR_ERR(ec_child));
1703 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1704 if (child == NULL) {
1705 CERROR("Can not locate the child %s\n", lname->ln_name);
1706 lu_object_put(env, ec_child);
1707 GOTO(out_free, rc = -EINVAL);
1710 CDEBUG(D_RPCTRACE, "Start getattr object "DFID"\n",
1711 PFID(lu_object_fid(child)));
1712 rc = mo_attr_get(env, lu2md(child), ma);
1714 CERROR("Can not getattr child "DFID": rc = %d\n",
1715 PFID(lu_object_fid(child)), rc);
1716 lu_object_put(env, ec_child);
1719 CDEBUG(D_RPCTRACE, "End getattr object "DFID"\n",
1720 PFID(lu_object_fid(child)));
1722 lu_object_put(env, ec_child);
1726 if (ma->ma_lmm_size > 0 && ma->ma_lmm != NULL)
1727 OBD_FREE(ma->ma_lmm, ma->ma_lmm_size);
1728 if (ma->ma_cookie_size > 0 && ma->ma_cookie != NULL)
1729 OBD_FREE(ma->ma_cookie, ma->ma_cookie_size);
1733 static int echo_lookup_object(const struct lu_env *env,
1734 struct echo_device *ed,
1735 struct lu_object *ec_parent,
1736 __u64 id, int count)
1738 struct lu_object *parent;
1739 struct echo_thread_info *info = echo_env_info(env);
1740 struct lu_name *lname = &info->eti_lname;
1741 char *name = info->eti_name;
1742 struct lu_fid *fid = &info->eti_fid;
1743 struct lu_device *ld = ed->ed_next;
1747 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1748 if (ec_parent == NULL) {
1749 lu_object_put(env, ec_parent);
1750 return PTR_ERR(parent);
1753 /*prepare the requests*/
1754 for (i = 0; i < count; i++) {
1755 echo_md_build_name(lname, name, id);
1757 CDEBUG(D_RPCTRACE, "Start lookup object "DFID" %s %p\n",
1758 PFID(lu_object_fid(parent)), lname->ln_name, parent);
1760 rc = mdo_lookup(env, lu2md(parent), lname, fid, NULL);
1762 CERROR("Can not lookup child %s: rc = %d\n", name, rc);
1765 CDEBUG(D_RPCTRACE, "End lookup object "DFID" %s %p\n",
1766 PFID(lu_object_fid(parent)), lname->ln_name, parent);
1773 static int echo_md_destroy_internal(const struct lu_env *env,
1774 struct echo_device *ed,
1775 struct md_object *parent,
1776 struct lu_name *lname,
1779 struct lu_device *ld = ed->ed_next;
1780 struct lu_object *ec_child;
1781 struct lu_object *child;
1784 ec_child = echo_md_lookup(env, ed, parent, lname);
1785 if (IS_ERR(ec_child)) {
1786 CERROR("Can't find child %s: rc = %ld\n", lname->ln_name,
1788 RETURN(PTR_ERR(ec_child));
1791 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1792 if (child == NULL) {
1793 CERROR("Can not locate the child %s\n", lname->ln_name);
1794 GOTO(out_put, rc = -EINVAL);
1797 CDEBUG(D_RPCTRACE, "Start destroy object "DFID" %s %p\n",
1798 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1800 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2,3,50,0)
1801 /* After 2.4, MDT will send destroy RPC to OST directly, so no need
1803 ma->ma_valid |= MA_FLAGS;
1804 ma->ma_attr_flags |= MDS_UNLINK_DESTROY;
1806 #warning "Please remove this after 2.4 (LOD/OSP)"
1808 rc = mdo_unlink(env, parent, lu2md(child), lname, ma);
1810 CERROR("Can not unlink child %s: rc = %d\n",
1811 lname->ln_name, rc);
1814 CDEBUG(D_RPCTRACE, "End destroy object "DFID" %s %p\n",
1815 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1817 lu_object_put(env, ec_child);
1821 static int echo_destroy_object(const struct lu_env *env,
1822 struct echo_device *ed,
1823 struct lu_object *ec_parent,
1824 char *name, int namelen,
1825 __u64 id, __u32 mode,
1828 struct echo_thread_info *info = echo_env_info(env);
1829 struct lu_name *lname = &info->eti_lname;
1830 struct md_attr *ma = &info->eti_ma;
1831 struct lu_device *ld = ed->ed_next;
1832 struct lu_object *parent;
1837 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1841 memset(ma, 0, sizeof(*ma));
1842 ma->ma_attr.la_mode = mode;
1843 ma->ma_attr.la_valid = LA_CTIME;
1844 ma->ma_attr.la_ctime = cfs_time_current_64();
1845 ma->ma_need = MA_INODE;
1848 rc = echo_set_lmm_size(env, ld, ma);
1852 lname->ln_name = name;
1853 lname->ln_namelen = namelen;
1854 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
1859 /*prepare the requests*/
1860 for (i = 0; i < count; i++) {
1861 char *tmp_name = info->eti_name;
1863 ma->ma_need |= MA_LOV;
1865 echo_md_build_name(lname, tmp_name, id);
1867 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
1870 CERROR("Can not unlink child %s: rc = %d\n", name, rc);
1877 if (ma->ma_lmm_size > 0 && ma->ma_lmm != NULL)
1878 OBD_FREE(ma->ma_lmm, ma->ma_lmm_size);
1879 if (ma->ma_cookie_size > 0 && ma->ma_cookie != NULL)
1880 OBD_FREE(ma->ma_cookie, ma->ma_cookie_size);
1884 static struct lu_object *echo_resolve_path(const struct lu_env *env,
1885 struct echo_device *ed, char *path,
1888 struct lu_device *ld = ed->ed_next;
1889 struct md_device *md = lu2md_dev(ld);
1890 struct echo_thread_info *info = echo_env_info(env);
1891 struct lu_fid *fid = &info->eti_fid;
1892 struct lu_name *lname = &info->eti_lname;
1893 struct lu_object *parent = NULL;
1894 struct lu_object *child = NULL;
1898 /*Only support MDD layer right now*/
1899 rc = md->md_ops->mdo_root_get(env, md, fid);
1901 CERROR("get root error: rc = %d\n", rc);
1902 RETURN(ERR_PTR(rc));
1905 parent = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1906 if (IS_ERR(parent)) {
1907 CERROR("Can not find the parent "DFID": rc = %ld\n",
1908 PFID(fid), PTR_ERR(parent));
1913 struct lu_object *ld_parent;
1916 e = strsep(&path, "/");
1921 if (!path || path[0] == '\0')
1927 lname->ln_namelen = strlen(e);
1929 ld_parent = lu_object_locate(parent->lo_header, ld->ld_type);
1930 if (ld_parent == NULL) {
1931 lu_object_put(env, parent);
1936 child = echo_md_lookup(env, ed, lu2md(ld_parent), lname);
1937 lu_object_put(env, parent);
1938 if (IS_ERR(child)) {
1939 rc = (int)PTR_ERR(child);
1940 CERROR("lookup %s under parent "DFID": rc = %d\n",
1941 lname->ln_name, PFID(lu_object_fid(ld_parent)),
1948 RETURN(ERR_PTR(rc));
1953 #define ECHO_MD_CTX_TAG (LCT_REMEMBER | LCT_NOREF | LCT_MD_THREAD)
1954 #define ECHO_MD_SES_TAG (LCT_SESSION | LCT_REMEMBER | LCT_NOREF)
1956 static int echo_md_handler(struct echo_device *ed, int command,
1957 char *path, int path_len, int id, int count,
1958 struct obd_ioctl_data *data)
1960 struct lu_device *ld = ed->ed_next;
1963 struct lu_object *parent;
1965 int namelen = data->ioc_plen2;
1970 CERROR("MD echo client is not being initialized properly\n");
1974 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
1975 CERROR("Only support MDD layer right now!\n");
1979 env = cl_env_get(&refcheck);
1981 RETURN(PTR_ERR(env));
1983 rc = lu_env_refill_by_tags(env, ECHO_MD_CTX_TAG, ECHO_MD_SES_TAG);
1985 cl_env_put(env, &refcheck);
1989 parent = echo_resolve_path(env, ed, path, path_len);
1990 if (IS_ERR(parent)) {
1991 CERROR("Can not resolve the path %s: rc = %ld\n", path,
1993 cl_env_put(env, &refcheck);
1994 RETURN(PTR_ERR(parent));
1998 OBD_ALLOC(name, namelen + 1);
2001 if (cfs_copy_from_user(name, data->ioc_pbuf2, namelen)) {
2002 OBD_FREE(name, namelen + 1);
2008 case ECHO_MD_CREATE:
2009 case ECHO_MD_MKDIR: {
2010 struct echo_thread_info *info = echo_env_info(env);
2011 __u32 mode = data->ioc_obdo2.o_mode;
2012 struct lu_fid *fid = &info->eti_fid;
2013 int stripe_count = (int)data->ioc_obdo2.o_misc;
2014 int stripe_index = (int)data->ioc_obdo2.o_stripe_idx;
2016 fid->f_seq = data->ioc_obdo1.o_seq;
2017 fid->f_oid = (__u32)data->ioc_obdo1.o_id;
2019 rc = echo_create_md_object(env, ed, parent, fid, name, namelen,
2020 id, mode, count, stripe_count,
2024 case ECHO_MD_DESTROY:
2025 case ECHO_MD_RMDIR: {
2026 __u32 mode = data->ioc_obdo2.o_mode;
2028 rc = echo_destroy_object(env, ed, parent, name, namelen,
2032 case ECHO_MD_LOOKUP:
2033 rc = echo_lookup_object(env, ed, parent, id, count);
2035 case ECHO_MD_GETATTR:
2036 rc = echo_getattr_object(env, ed, parent, id, count);
2038 case ECHO_MD_SETATTR:
2039 rc = echo_setattr_object(env, ed, parent, id, count);
2042 CERROR("unknown command %d\n", command);
2047 OBD_FREE(name, namelen + 1);
2048 lu_object_put(env, parent);
2049 cl_env_put(env, &refcheck);
2053 static int echo_create_object(const struct lu_env *env, struct echo_device *ed,
2054 int on_target, struct obdo *oa, void *ulsm,
2055 int ulsm_nob, struct obd_trans_info *oti)
2057 struct echo_object *eco;
2058 struct echo_client_obd *ec = ed->ed_ec;
2059 struct lov_stripe_md *lsm = NULL;
2064 if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
2065 (on_target || /* set_stripe */
2066 ec->ec_nstripes != 0)) { /* LOV */
2067 CERROR ("No valid oid\n");
2071 rc = obd_alloc_memmd(ec->ec_exp, &lsm);
2073 CERROR("Cannot allocate md: rc = %d\n", rc);
2080 rc = echo_copyin_lsm (ed, lsm, ulsm, ulsm_nob);
2084 if (lsm->lsm_stripe_count == 0)
2085 lsm->lsm_stripe_count = ec->ec_nstripes;
2087 if (lsm->lsm_stripe_size == 0)
2088 lsm->lsm_stripe_size = CFS_PAGE_SIZE;
2092 /* setup stripes: indices + default ids if required */
2093 for (i = 0; i < lsm->lsm_stripe_count; i++) {
2094 if (lsm->lsm_oinfo[i]->loi_id == 0)
2095 lsm->lsm_oinfo[i]->loi_id = lsm->lsm_object_id;
2097 lsm->lsm_oinfo[i]->loi_ost_idx =
2098 (idx + i) % ec->ec_nstripes;
2102 /* setup object ID here for !on_target and LOV hint */
2103 if (oa->o_valid & OBD_MD_FLID)
2104 lsm->lsm_object_id = oa->o_id;
2106 if (lsm->lsm_object_id == 0)
2107 lsm->lsm_object_id = ++last_object_id;
2111 /* Only echo objects are allowed to be created */
2112 LASSERT((oa->o_valid & OBD_MD_FLGROUP) &&
2113 (oa->o_seq == FID_SEQ_ECHO));
2114 rc = obd_create(env, ec->ec_exp, oa, &lsm, oti);
2116 CERROR("Cannot create objects: rc = %d\n", rc);
2122 /* See what object ID we were given */
2123 oa->o_id = lsm->lsm_object_id;
2124 oa->o_valid |= OBD_MD_FLID;
2126 eco = cl_echo_object_find(ed, &lsm);
2128 GOTO(failed, rc = PTR_ERR(eco));
2129 cl_echo_object_put(eco);
2131 CDEBUG(D_INFO, "oa->o_id = %lx\n", (long)oa->o_id);
2136 obd_destroy(env, ec->ec_exp, oa, lsm, oti, NULL, NULL);
2138 obd_free_memmd(ec->ec_exp, &lsm);
2140 CERROR("create object failed with: rc = %d\n", rc);
2144 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
2147 struct echo_client_obd *ec = ed->ed_ec;
2148 struct lov_stripe_md *lsm = NULL;
2149 struct echo_object *eco;
2153 if ((oa->o_valid & OBD_MD_FLID) == 0 ||
2154 oa->o_id == 0) /* disallow use of object id 0 */
2156 CERROR ("No valid oid\n");
2160 rc = obd_alloc_memmd(ec->ec_exp, &lsm);
2164 lsm->lsm_object_id = oa->o_id;
2165 if (oa->o_valid & OBD_MD_FLGROUP)
2166 lsm->lsm_object_seq = oa->o_seq;
2168 lsm->lsm_object_seq = FID_SEQ_ECHO;
2171 eco = cl_echo_object_find(ed, &lsm);
2177 obd_free_memmd(ec->ec_exp, &lsm);
2181 static void echo_put_object(struct echo_object *eco)
2183 if (cl_echo_object_put(eco))
2184 CERROR("echo client: drop an object failed");
2188 echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
2190 unsigned long stripe_count;
2191 unsigned long stripe_size;
2192 unsigned long width;
2193 unsigned long woffset;
2197 if (lsm->lsm_stripe_count <= 1)
2201 stripe_size = lsm->lsm_stripe_size;
2202 stripe_count = lsm->lsm_stripe_count;
2204 /* width = # bytes in all stripes */
2205 width = stripe_size * stripe_count;
2207 /* woffset = offset within a width; offset = whole number of widths */
2208 woffset = do_div (offset, width);
2210 stripe_index = woffset / stripe_size;
2212 *idp = lsm->lsm_oinfo[stripe_index]->loi_id;
2213 *offp = offset * stripe_size + woffset % stripe_size;
2217 echo_client_page_debug_setup(struct lov_stripe_md *lsm,
2218 cfs_page_t *page, int rw, obd_id id,
2219 obd_off offset, obd_off count)
2226 /* no partial pages on the client */
2227 LASSERT(count == CFS_PAGE_SIZE);
2229 addr = cfs_kmap(page);
2231 for (delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2232 if (rw == OBD_BRW_WRITE) {
2233 stripe_off = offset + delta;
2235 echo_get_stripe_off_id(lsm, &stripe_off, &stripe_id);
2237 stripe_off = 0xdeadbeef00c0ffeeULL;
2238 stripe_id = 0xdeadbeef00c0ffeeULL;
2240 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
2241 stripe_off, stripe_id);
2247 static int echo_client_page_debug_check(struct lov_stripe_md *lsm,
2248 cfs_page_t *page, obd_id id,
2249 obd_off offset, obd_off count)
2258 /* no partial pages on the client */
2259 LASSERT(count == CFS_PAGE_SIZE);
2261 addr = cfs_kmap(page);
2263 for (rc = delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2264 stripe_off = offset + delta;
2266 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
2268 rc2 = block_debug_check("test_brw",
2269 addr + delta, OBD_ECHO_BLOCK_SIZE,
2270 stripe_off, stripe_id);
2272 CERROR ("Error in echo object "LPX64"\n", id);
2281 static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
2282 struct echo_object *eco, obd_off offset,
2283 obd_size count, int async,
2284 struct obd_trans_info *oti)
2286 struct lov_stripe_md *lsm = eco->eo_lsm;
2288 struct brw_page *pga;
2289 struct brw_page *pgp;
2299 verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
2300 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
2301 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
2303 gfp_mask = ((oa->o_id & 2) == 0) ? CFS_ALLOC_STD : CFS_ALLOC_HIGHUSER;
2305 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
2306 LASSERT(lsm != NULL);
2307 LASSERT(lsm->lsm_object_id == oa->o_id);
2310 (count & (~CFS_PAGE_MASK)) != 0)
2313 /* XXX think again with misaligned I/O */
2314 npages = count >> CFS_PAGE_SHIFT;
2316 if (rw == OBD_BRW_WRITE)
2317 brw_flags = OBD_BRW_ASYNC;
2319 OBD_ALLOC(pga, npages * sizeof(*pga));
2323 OBD_ALLOC(pages, npages * sizeof(*pages));
2324 if (pages == NULL) {
2325 OBD_FREE(pga, npages * sizeof(*pga));
2329 for (i = 0, pgp = pga, off = offset;
2331 i++, pgp++, off += CFS_PAGE_SIZE) {
2333 LASSERT (pgp->pg == NULL); /* for cleanup */
2336 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
2337 if (pgp->pg == NULL)
2341 pgp->count = CFS_PAGE_SIZE;
2343 pgp->flag = brw_flags;
2346 echo_client_page_debug_setup(lsm, pgp->pg, rw,
2347 oa->o_id, off, pgp->count);
2350 /* brw mode can only be used at client */
2351 LASSERT(ed->ed_next != NULL);
2352 rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
2355 if (rc != 0 || rw != OBD_BRW_READ)
2358 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
2359 if (pgp->pg == NULL)
2364 vrc = echo_client_page_debug_check(lsm, pgp->pg, oa->o_id,
2365 pgp->off, pgp->count);
2366 if (vrc != 0 && rc == 0)
2369 OBD_PAGE_FREE(pgp->pg);
2371 OBD_FREE(pga, npages * sizeof(*pga));
2372 OBD_FREE(pages, npages * sizeof(*pages));
2376 static int echo_client_prep_commit(struct obd_export *exp, int rw,
2377 struct obdo *oa, struct echo_object *eco,
2378 obd_off offset, obd_size count,
2379 obd_size batch, struct obd_trans_info *oti,
2382 struct lov_stripe_md *lsm = eco->eo_lsm;
2383 struct obd_ioobj ioo;
2384 struct niobuf_local *lnb;
2385 struct niobuf_remote *rnb;
2387 obd_size npages, tot_pages;
2391 if (count <= 0 || (count & (~CFS_PAGE_MASK)) != 0 ||
2392 (lsm != NULL && lsm->lsm_object_id != oa->o_id))
2395 npages = batch >> CFS_PAGE_SHIFT;
2396 tot_pages = count >> CFS_PAGE_SHIFT;
2398 OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
2399 OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
2401 if (lnb == NULL || rnb == NULL)
2402 GOTO(out, ret = -ENOMEM);
2404 obdo_to_ioobj(oa, &ioo);
2408 for(; tot_pages; tot_pages -= npages) {
2411 if (tot_pages < npages)
2414 for (i = 0; i < npages; i++, off += CFS_PAGE_SIZE) {
2415 rnb[i].offset = off;
2416 rnb[i].len = CFS_PAGE_SIZE;
2419 ioo.ioo_bufcnt = npages;
2420 oti->oti_transno = 0;
2423 ret = obd_preprw(NULL, rw, exp, oa, 1, &ioo, rnb, &lpages,
2427 LASSERT(lpages == npages);
2429 for (i = 0; i < lpages; i++) {
2430 cfs_page_t *page = lnb[i].page;
2432 /* read past eof? */
2433 if (page == NULL && lnb[i].rc == 0)
2437 lnb[i].flags |= OBD_BRW_ASYNC;
2439 if (oa->o_id == ECHO_PERSISTENT_OBJID ||
2440 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
2441 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
2444 if (rw == OBD_BRW_WRITE)
2445 echo_client_page_debug_setup(lsm, page, rw,
2450 echo_client_page_debug_check(lsm, page,
2456 ret = obd_commitrw(NULL, rw, exp, oa, 1, &ioo,
2457 rnb, npages, lnb, oti, ret);
2461 /* Reset oti otherwise it would confuse ldiskfs. */
2462 memset(oti, 0, sizeof(*oti));
2467 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
2469 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
2473 static int echo_client_brw_ioctl(int rw, struct obd_export *exp,
2474 struct obd_ioctl_data *data)
2476 struct obd_device *obd = class_exp2obd(exp);
2477 struct echo_device *ed = obd2echo_dev(obd);
2478 struct echo_client_obd *ec = ed->ed_ec;
2479 struct obd_trans_info dummy_oti = { 0 };
2480 struct obdo *oa = &data->ioc_obdo1;
2481 struct echo_object *eco;
2487 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2489 rc = echo_get_object(&eco, ed, oa);
2493 oa->o_valid &= ~OBD_MD_FLHANDLE;
2495 /* obdfilter doesn't support obd_brw now, simulate via prep + commit */
2496 test_mode = (long)data->ioc_pbuf1;
2500 if (ed->ed_next == NULL && test_mode != 3) {
2502 data->ioc_plen1 = data->ioc_count;
2505 /* Truncate batch size to maximum */
2506 if (data->ioc_plen1 > PTLRPC_MAX_BRW_SIZE)
2507 data->ioc_plen1 = PTLRPC_MAX_BRW_SIZE;
2509 switch (test_mode) {
2513 rc = echo_client_kbrw(ed, rw, oa,
2514 eco, data->ioc_offset,
2515 data->ioc_count, async, &dummy_oti);
2518 rc = echo_client_prep_commit(ec->ec_exp, rw, oa,
2519 eco, data->ioc_offset,
2520 data->ioc_count, data->ioc_plen1,
2526 echo_put_object(eco);
2531 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
2532 int mode, obd_off offset, obd_size nob)
2534 struct echo_device *ed = obd2echo_dev(exp->exp_obd);
2535 struct lustre_handle *ulh = &oa->o_handle;
2536 struct echo_object *eco;
2541 if (ed->ed_next == NULL)
2542 RETURN(-EOPNOTSUPP);
2544 if (!(mode == LCK_PR || mode == LCK_PW))
2547 if ((offset & (~CFS_PAGE_MASK)) != 0 ||
2548 (nob & (~CFS_PAGE_MASK)) != 0)
2551 rc = echo_get_object (&eco, ed, oa);
2555 end = (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
2556 rc = cl_echo_enqueue(eco, offset, end, mode, &ulh->cookie);
2558 oa->o_valid |= OBD_MD_FLHANDLE;
2559 CDEBUG(D_INFO, "Cookie is "LPX64"\n", ulh->cookie);
2561 echo_put_object(eco);
2566 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
2568 struct echo_device *ed = obd2echo_dev(exp->exp_obd);
2569 __u64 cookie = oa->o_handle.cookie;
2571 if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
2574 CDEBUG(D_INFO, "Cookie is "LPX64"\n", cookie);
2575 return cl_echo_cancel(ed, cookie);
2579 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2580 void *karg, void *uarg)
2582 struct obd_device *obd = exp->exp_obd;
2583 struct echo_device *ed = obd2echo_dev(obd);
2584 struct echo_client_obd *ec = ed->ed_ec;
2585 struct echo_object *eco;
2586 struct obd_ioctl_data *data = karg;
2587 struct obd_trans_info dummy_oti;
2589 struct oti_req_ack_lock *ack_lock;
2592 int rw = OBD_BRW_READ;
2597 memset(&dummy_oti, 0, sizeof(dummy_oti));
2599 oa = &data->ioc_obdo1;
2600 if (!(oa->o_valid & OBD_MD_FLGROUP)) {
2601 oa->o_valid |= OBD_MD_FLGROUP;
2602 oa->o_seq = FID_SEQ_ECHO;
2605 /* This FID is unpacked just for validation at this point */
2606 rc = fid_ostid_unpack(&fid, &oa->o_oi, 0);
2614 rc = lu_env_init(env, LCT_DT_THREAD);
2616 GOTO(out, rc = -ENOMEM);
2619 case OBD_IOC_CREATE: /* may create echo object */
2620 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2621 GOTO (out, rc = -EPERM);
2623 rc = echo_create_object(env, ed, 1, oa, data->ioc_pbuf1,
2624 data->ioc_plen1, &dummy_oti);
2627 case OBD_IOC_ECHO_MD: {
2634 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2635 GOTO(out, rc = -EPERM);
2637 count = data->ioc_count;
2638 cmd = data->ioc_command;
2640 id = data->ioc_obdo2.o_id;
2642 dirlen = data->ioc_plen1;
2643 OBD_ALLOC(dir, dirlen + 1);
2645 GOTO(out, rc = -ENOMEM);
2647 if (cfs_copy_from_user(dir, data->ioc_pbuf1, dirlen)) {
2648 OBD_FREE(dir, data->ioc_plen1 + 1);
2649 GOTO(out, rc = -EFAULT);
2652 rc = echo_md_handler(ed, cmd, dir, dirlen, id, count, data);
2653 OBD_FREE(dir, dirlen + 1);
2656 case OBD_IOC_ECHO_ALLOC_SEQ: {
2657 struct lu_env *cl_env;
2662 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2663 GOTO(out, rc = -EPERM);
2665 cl_env = cl_env_get(&refcheck);
2667 GOTO(out, rc = PTR_ERR(cl_env));
2669 rc = lu_env_refill_by_tags(cl_env, ECHO_MD_CTX_TAG,
2672 cl_env_put(cl_env, &refcheck);
2676 rc = seq_client_get_seq(cl_env, ed->ed_cl_seq, &seq);
2677 cl_env_put(cl_env, &refcheck);
2679 CERROR("%s: Can not alloc seq: rc = %d\n",
2684 if (cfs_copy_to_user(data->ioc_pbuf1, &seq, data->ioc_plen1))
2687 max_count = LUSTRE_SEQ_MAX_WIDTH;
2688 if (cfs_copy_to_user(data->ioc_pbuf2, &max_count,
2693 case OBD_IOC_DESTROY:
2694 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2695 GOTO (out, rc = -EPERM);
2697 rc = echo_get_object(&eco, ed, oa);
2699 rc = obd_destroy(env, ec->ec_exp, oa, eco->eo_lsm,
2700 &dummy_oti, NULL, NULL);
2702 eco->eo_deleted = 1;
2703 echo_put_object(eco);
2707 case OBD_IOC_GETATTR:
2708 rc = echo_get_object(&eco, ed, oa);
2710 struct obd_info oinfo = { { { 0 } } };
2711 oinfo.oi_md = eco->eo_lsm;
2713 rc = obd_getattr(env, ec->ec_exp, &oinfo);
2714 echo_put_object(eco);
2718 case OBD_IOC_SETATTR:
2719 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2720 GOTO (out, rc = -EPERM);
2722 rc = echo_get_object(&eco, ed, oa);
2724 struct obd_info oinfo = { { { 0 } } };
2726 oinfo.oi_md = eco->eo_lsm;
2728 rc = obd_setattr(env, ec->ec_exp, &oinfo, NULL);
2729 echo_put_object(eco);
2733 case OBD_IOC_BRW_WRITE:
2734 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2735 GOTO (out, rc = -EPERM);
2739 case OBD_IOC_BRW_READ:
2740 rc = echo_client_brw_ioctl(rw, exp, data);
2743 case ECHO_IOC_GET_STRIPE:
2744 rc = echo_get_object(&eco, ed, oa);
2746 rc = echo_copyout_lsm(eco->eo_lsm, data->ioc_pbuf1,
2748 echo_put_object(eco);
2752 case ECHO_IOC_SET_STRIPE:
2753 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2754 GOTO (out, rc = -EPERM);
2756 if (data->ioc_pbuf1 == NULL) { /* unset */
2757 rc = echo_get_object(&eco, ed, oa);
2759 eco->eo_deleted = 1;
2760 echo_put_object(eco);
2763 rc = echo_create_object(env, ed, 0, oa,
2765 data->ioc_plen1, &dummy_oti);
2769 case ECHO_IOC_ENQUEUE:
2770 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2771 GOTO (out, rc = -EPERM);
2773 rc = echo_client_enqueue(exp, oa,
2774 data->ioc_conn1, /* lock mode */
2776 data->ioc_count);/*extent*/
2779 case ECHO_IOC_CANCEL:
2780 rc = echo_client_cancel(exp, oa);
2784 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
2785 GOTO (out, rc = -ENOTTY);
2793 /* XXX this should be in a helper also called by target_send_reply */
2794 for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
2796 if (!ack_lock->mode)
2798 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
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_client_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_client_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);
2996 static int __init obdecho_init(void)
2998 struct lprocfs_static_vars lvars;
3002 LCONSOLE_INFO("Echo OBD driver; http://www.lustre.org/\n");
3004 LASSERT(CFS_PAGE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
3006 lprocfs_echo_init_vars(&lvars);
3008 # ifdef HAVE_SERVER_SUPPORT
3009 rc = echo_persistent_pages_init();
3013 rc = class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
3014 LUSTRE_ECHO_NAME, NULL);
3019 rc = echo_client_init();
3021 # ifdef HAVE_SERVER_SUPPORT
3025 class_unregister_type(LUSTRE_ECHO_NAME);
3027 echo_persistent_pages_fini();
3033 static void /*__exit*/ obdecho_exit(void)
3037 # ifdef HAVE_SERVER_SUPPORT
3038 class_unregister_type(LUSTRE_ECHO_NAME);
3039 echo_persistent_pages_fini();
3043 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3044 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
3045 MODULE_LICENSE("GPL");
3047 cfs_module(obdecho, LUSTRE_VERSION_STRING, obdecho_init, obdecho_exit);
3048 #endif /* __KERNEL__ */
3050 /** @} echo_client */