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.
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>
51 #include "echo_internal.h"
53 /** \defgroup echo_client Echo Client
58 struct cl_device ed_cl;
59 struct echo_client_obd *ed_ec;
61 struct cl_site ed_site_myself;
62 struct cl_site *ed_site;
63 struct lu_device *ed_next;
68 struct cl_object eo_cl;
69 struct cl_object_header eo_hdr;
71 struct echo_device *eo_dev;
72 cfs_list_t eo_obj_chain;
73 struct lov_stripe_md *eo_lsm;
74 cfs_atomic_t eo_npages;
78 struct echo_object_conf {
79 struct cl_object_conf eoc_cl;
80 struct lov_stripe_md **eoc_md;
84 struct cl_page_slice ep_cl;
86 cfs_page_t *ep_vmpage;
90 struct cl_lock_slice el_cl;
92 struct echo_object *el_object;
94 cfs_atomic_t el_refcount;
98 struct cl_io_slice ei_cl;
103 struct cl_req_slice er_cl;
107 static int echo_client_setup(struct obd_device *obddev,
108 struct lustre_cfg *lcfg);
109 static int echo_client_cleanup(struct obd_device *obddev);
112 /** \defgroup echo_helpers Helper functions
115 static inline struct echo_device *cl2echo_dev(const struct cl_device *dev)
117 return container_of0(dev, struct echo_device, ed_cl);
120 static inline struct cl_device *echo_dev2cl(struct echo_device *d)
125 static inline struct echo_device *obd2echo_dev(const struct obd_device *obd)
127 return cl2echo_dev(lu2cl_dev(obd->obd_lu_dev));
130 static inline struct cl_object *echo_obj2cl(struct echo_object *eco)
135 static inline struct echo_object *cl2echo_obj(const struct cl_object *o)
137 return container_of(o, struct echo_object, eo_cl);
140 static inline struct echo_page *cl2echo_page(const struct cl_page_slice *s)
142 return container_of(s, struct echo_page, ep_cl);
145 static inline struct echo_lock *cl2echo_lock(const struct cl_lock_slice *s)
147 return container_of(s, struct echo_lock, el_cl);
150 static inline struct cl_lock *echo_lock2cl(const struct echo_lock *ecl)
152 return ecl->el_cl.cls_lock;
155 static struct lu_context_key echo_thread_key;
156 static inline struct echo_thread_info *echo_env_info(const struct lu_env *env)
158 struct echo_thread_info *info;
159 info = lu_context_key_get(&env->le_ctx, &echo_thread_key);
160 LASSERT(info != NULL);
165 struct echo_object_conf *cl2echo_conf(const struct cl_object_conf *c)
167 return container_of(c, struct echo_object_conf, eoc_cl);
170 static inline void lsm2fid(struct lov_stripe_md *lsm, struct lu_fid *fid)
173 fid->f_seq = FID_SEQ_ECHO;
174 /* truncated to 32 bits by assignment */
175 fid->f_oid = lsm->lsm_object_id;
176 fid->f_ver = lsm->lsm_object_id >> 32;
178 /** @} echo_helpers */
180 static struct echo_object *cl_echo_object_find(struct echo_device *d,
181 struct lov_stripe_md **lsm);
182 static int cl_echo_object_put(struct echo_object *eco);
183 static int cl_echo_enqueue (struct echo_object *eco, obd_off start,
184 obd_off end, int mode, __u64 *cookie);
185 static int cl_echo_cancel (struct echo_device *d, __u64 cookie);
186 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
187 cfs_page_t **pages, int npages, int async);
189 static struct echo_thread_info *echo_env_info(const struct lu_env *env);
191 struct echo_thread_info {
192 struct echo_object_conf eti_conf;
193 struct lustre_md eti_md;
195 struct cl_2queue eti_queue;
197 struct cl_lock_descr eti_descr;
198 struct lu_fid eti_fid;
201 /* No session used right now */
202 struct echo_session_info {
206 static cfs_mem_cache_t *echo_page_kmem;
207 static cfs_mem_cache_t *echo_lock_kmem;
208 static cfs_mem_cache_t *echo_object_kmem;
209 static cfs_mem_cache_t *echo_thread_kmem;
210 static cfs_mem_cache_t *echo_session_kmem;
211 //static cfs_mem_cache_t *echo_req_kmem;
213 static struct lu_kmem_descr echo_caches[] = {
215 .ckd_cache = &echo_page_kmem,
216 .ckd_name = "echo_page_kmem",
217 .ckd_size = sizeof (struct echo_page)
220 .ckd_cache = &echo_lock_kmem,
221 .ckd_name = "echo_lock_kmem",
222 .ckd_size = sizeof (struct echo_lock)
225 .ckd_cache = &echo_object_kmem,
226 .ckd_name = "echo_object_kmem",
227 .ckd_size = sizeof (struct echo_object)
230 .ckd_cache = &echo_thread_kmem,
231 .ckd_name = "echo_thread_kmem",
232 .ckd_size = sizeof (struct echo_thread_info)
235 .ckd_cache = &echo_session_kmem,
236 .ckd_name = "echo_session_kmem",
237 .ckd_size = sizeof (struct echo_session_info)
241 .ckd_cache = &echo_req_kmem,
242 .ckd_name = "echo_req_kmem",
243 .ckd_size = sizeof (struct echo_req)
251 /** \defgroup echo_page Page operations
253 * Echo page operations.
257 cfs_page_t *echo_page_vmpage(const struct lu_env *env,
258 const struct cl_page_slice *slice)
260 return cl2echo_page(slice)->ep_vmpage;
263 static int echo_page_own(const struct lu_env *env,
264 const struct cl_page_slice *slice,
265 struct cl_io *io, int nonblock)
267 struct echo_page *ep = cl2echo_page(slice);
270 cfs_mutex_lock(&ep->ep_lock);
271 else if (!cfs_mutex_trylock(&ep->ep_lock))
276 static void echo_page_disown(const struct lu_env *env,
277 const struct cl_page_slice *slice,
280 struct echo_page *ep = cl2echo_page(slice);
282 LASSERT(cfs_mutex_is_locked(&ep->ep_lock));
283 cfs_mutex_unlock(&ep->ep_lock);
286 static void echo_page_discard(const struct lu_env *env,
287 const struct cl_page_slice *slice,
288 struct cl_io *unused)
290 cl_page_delete(env, slice->cpl_page);
293 static int echo_page_is_vmlocked(const struct lu_env *env,
294 const struct cl_page_slice *slice)
296 return cfs_mutex_is_locked(&cl2echo_page(slice)->ep_lock);
299 static void echo_page_completion(const struct lu_env *env,
300 const struct cl_page_slice *slice,
303 LASSERT(slice->cpl_page->cp_sync_io != NULL);
306 static void echo_page_fini(const struct lu_env *env,
307 struct cl_page_slice *slice)
309 struct echo_page *ep = cl2echo_page(slice);
310 struct echo_object *eco = cl2echo_obj(slice->cpl_obj);
311 cfs_page_t *vmpage = ep->ep_vmpage;
314 cfs_atomic_dec(&eco->eo_npages);
315 page_cache_release(vmpage);
316 OBD_SLAB_FREE_PTR(ep, echo_page_kmem);
320 static int echo_page_prep(const struct lu_env *env,
321 const struct cl_page_slice *slice,
322 struct cl_io *unused)
327 static int echo_page_print(const struct lu_env *env,
328 const struct cl_page_slice *slice,
329 void *cookie, lu_printer_t printer)
331 struct echo_page *ep = cl2echo_page(slice);
333 (*printer)(env, cookie, LUSTRE_ECHO_CLIENT_NAME"-page@%p %d vm@%p\n",
334 ep, cfs_mutex_is_locked(&ep->ep_lock), ep->ep_vmpage);
338 static const struct cl_page_operations echo_page_ops = {
339 .cpo_own = echo_page_own,
340 .cpo_disown = echo_page_disown,
341 .cpo_discard = echo_page_discard,
342 .cpo_vmpage = echo_page_vmpage,
343 .cpo_fini = echo_page_fini,
344 .cpo_print = echo_page_print,
345 .cpo_is_vmlocked = echo_page_is_vmlocked,
348 .cpo_prep = echo_page_prep,
349 .cpo_completion = echo_page_completion,
352 .cpo_prep = echo_page_prep,
353 .cpo_completion = echo_page_completion,
359 /** \defgroup echo_lock Locking
361 * echo lock operations
365 static void echo_lock_fini(const struct lu_env *env,
366 struct cl_lock_slice *slice)
368 struct echo_lock *ecl = cl2echo_lock(slice);
370 LASSERT(cfs_list_empty(&ecl->el_chain));
371 OBD_SLAB_FREE_PTR(ecl, echo_lock_kmem);
374 static void echo_lock_delete(const struct lu_env *env,
375 const struct cl_lock_slice *slice)
377 struct echo_lock *ecl = cl2echo_lock(slice);
379 LASSERT(cfs_list_empty(&ecl->el_chain));
382 static int echo_lock_fits_into(const struct lu_env *env,
383 const struct cl_lock_slice *slice,
384 const struct cl_lock_descr *need,
385 const struct cl_io *unused)
390 static struct cl_lock_operations echo_lock_ops = {
391 .clo_fini = echo_lock_fini,
392 .clo_delete = echo_lock_delete,
393 .clo_fits_into = echo_lock_fits_into
398 /** \defgroup echo_cl_ops cl_object operations
400 * operations for cl_object
404 static struct cl_page *echo_page_init(const struct lu_env *env,
405 struct cl_object *obj,
406 struct cl_page *page, cfs_page_t *vmpage)
408 struct echo_page *ep;
411 OBD_SLAB_ALLOC_PTR_GFP(ep, echo_page_kmem, CFS_ALLOC_IO);
413 struct echo_object *eco = cl2echo_obj(obj);
414 ep->ep_vmpage = vmpage;
415 page_cache_get(vmpage);
416 cfs_mutex_init(&ep->ep_lock);
417 cl_page_slice_add(page, &ep->ep_cl, obj, &echo_page_ops);
418 cfs_atomic_inc(&eco->eo_npages);
420 RETURN(ERR_PTR(ep ? 0 : -ENOMEM));
423 static int echo_io_init(const struct lu_env *env, struct cl_object *obj,
429 static int echo_lock_init(const struct lu_env *env,
430 struct cl_object *obj, struct cl_lock *lock,
431 const struct cl_io *unused)
433 struct echo_lock *el;
436 OBD_SLAB_ALLOC_PTR_GFP(el, echo_lock_kmem, CFS_ALLOC_IO);
438 cl_lock_slice_add(lock, &el->el_cl, obj, &echo_lock_ops);
439 el->el_object = cl2echo_obj(obj);
440 CFS_INIT_LIST_HEAD(&el->el_chain);
441 cfs_atomic_set(&el->el_refcount, 0);
443 RETURN(el == NULL ? -ENOMEM : 0);
446 static int echo_conf_set(const struct lu_env *env, struct cl_object *obj,
447 const struct cl_object_conf *conf)
452 static const struct cl_object_operations echo_cl_obj_ops = {
453 .coo_page_init = echo_page_init,
454 .coo_lock_init = echo_lock_init,
455 .coo_io_init = echo_io_init,
456 .coo_conf_set = echo_conf_set
458 /** @} echo_cl_ops */
460 /** \defgroup echo_lu_ops lu_object operations
462 * operations for echo lu object.
466 static int echo_object_init(const struct lu_env *env, struct lu_object *obj,
467 const struct lu_object_conf *conf)
469 const struct cl_object_conf *cconf = lu2cl_conf(conf);
470 struct echo_object_conf *econf = cl2echo_conf(cconf);
471 struct echo_device *ed = cl2echo_dev(lu2cl_dev(obj->lo_dev));
472 struct echo_client_obd *ec = ed->ed_ec;
473 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
477 struct lu_object *below;
478 struct lu_device *under;
481 below = under->ld_ops->ldo_object_alloc(env, obj->lo_header,
485 lu_object_add(obj, below);
488 LASSERT(econf->eoc_md);
489 eco->eo_lsm = *econf->eoc_md;
491 cfs_atomic_set(&eco->eo_npages, 0);
493 /* clear the lsm pointer so that it won't get freed. */
494 *econf->eoc_md = NULL;
496 cfs_spin_lock(&ec->ec_lock);
497 cfs_list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
498 cfs_spin_unlock(&ec->ec_lock);
503 static void echo_object_free(const struct lu_env *env, struct lu_object *obj)
505 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
506 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
507 struct lov_stripe_md *lsm = eco->eo_lsm;
510 LASSERT(cfs_atomic_read(&eco->eo_npages) == 0);
512 cfs_spin_lock(&ec->ec_lock);
513 cfs_list_del_init(&eco->eo_obj_chain);
514 cfs_spin_unlock(&ec->ec_lock);
517 lu_object_header_fini(obj->lo_header);
520 obd_free_memmd(ec->ec_exp, &lsm);
521 OBD_SLAB_FREE_PTR(eco, echo_object_kmem);
525 static int echo_object_print(const struct lu_env *env, void *cookie,
526 lu_printer_t p, const struct lu_object *o)
528 struct echo_object *obj = cl2echo_obj(lu2cl(o));
530 return (*p)(env, cookie, "echoclient-object@%p", obj);
534 static const struct lu_object_operations echo_lu_obj_ops = {
535 .loo_object_init = echo_object_init,
536 .loo_object_delete = NULL,
537 .loo_object_release = NULL,
538 .loo_object_free = echo_object_free,
539 .loo_object_print = echo_object_print,
540 .loo_object_invariant = NULL
542 /** @} echo_lu_ops */
544 /** \defgroup echo_lu_dev_ops lu_device operations
546 * Operations for echo lu device.
550 static struct lu_object *echo_object_alloc(const struct lu_env *env,
551 const struct lu_object_header *hdr,
552 struct lu_device *dev)
554 struct echo_object *eco;
555 struct lu_object *obj = NULL;
558 /* we're the top dev. */
559 LASSERT(hdr == NULL);
560 OBD_SLAB_ALLOC_PTR_GFP(eco, echo_object_kmem, CFS_ALLOC_IO);
562 struct cl_object_header *hdr = &eco->eo_hdr;
564 obj = &echo_obj2cl(eco)->co_lu;
565 cl_object_header_init(hdr);
566 lu_object_init(obj, &hdr->coh_lu, dev);
567 lu_object_add_top(&hdr->coh_lu, obj);
569 eco->eo_cl.co_ops = &echo_cl_obj_ops;
570 obj->lo_ops = &echo_lu_obj_ops;
575 static struct lu_device_operations echo_device_lu_ops = {
576 .ldo_object_alloc = echo_object_alloc,
578 /** @} echo_lu_dev_ops */
580 static struct cl_device_operations echo_device_cl_ops = {
583 /** \defgroup echo_init Setup and teardown
585 * Init and fini functions for echo client.
589 static int echo_site_init(const struct lu_env *env, struct echo_device *ed)
591 struct cl_site *site = &ed->ed_site_myself;
594 /* initialize site */
595 rc = cl_site_init(site, &ed->ed_cl);
597 CERROR("Cannot initilize site for echo client(%d)\n", rc);
601 rc = lu_site_init_finish(&site->cs_lu);
609 static void echo_site_fini(const struct lu_env *env, struct echo_device *ed)
612 cl_site_fini(ed->ed_site);
617 static void *echo_thread_key_init(const struct lu_context *ctx,
618 struct lu_context_key *key)
620 struct echo_thread_info *info;
622 OBD_SLAB_ALLOC_PTR_GFP(info, echo_thread_kmem, CFS_ALLOC_IO);
624 info = ERR_PTR(-ENOMEM);
628 static void echo_thread_key_fini(const struct lu_context *ctx,
629 struct lu_context_key *key, void *data)
631 struct echo_thread_info *info = data;
632 OBD_SLAB_FREE_PTR(info, echo_thread_kmem);
635 static void echo_thread_key_exit(const struct lu_context *ctx,
636 struct lu_context_key *key, void *data)
640 static struct lu_context_key echo_thread_key = {
641 .lct_tags = LCT_CL_THREAD,
642 .lct_init = echo_thread_key_init,
643 .lct_fini = echo_thread_key_fini,
644 .lct_exit = echo_thread_key_exit
647 static void *echo_session_key_init(const struct lu_context *ctx,
648 struct lu_context_key *key)
650 struct echo_session_info *session;
652 OBD_SLAB_ALLOC_PTR_GFP(session, echo_session_kmem, CFS_ALLOC_IO);
654 session = ERR_PTR(-ENOMEM);
658 static void echo_session_key_fini(const struct lu_context *ctx,
659 struct lu_context_key *key, void *data)
661 struct echo_session_info *session = data;
662 OBD_SLAB_FREE_PTR(session, echo_session_kmem);
665 static void echo_session_key_exit(const struct lu_context *ctx,
666 struct lu_context_key *key, void *data)
670 static struct lu_context_key echo_session_key = {
671 .lct_tags = LCT_SESSION,
672 .lct_init = echo_session_key_init,
673 .lct_fini = echo_session_key_fini,
674 .lct_exit = echo_session_key_exit
677 LU_TYPE_INIT_FINI(echo, &echo_thread_key, &echo_session_key);
679 static struct lu_device *echo_device_alloc(const struct lu_env *env,
680 struct lu_device_type *t,
681 struct lustre_cfg *cfg)
683 struct lu_device *next;
684 struct echo_device *ed;
685 struct cl_device *cd;
686 struct obd_device *obd = NULL; /* to keep compiler happy */
687 struct obd_device *tgt;
688 const char *tgt_type_name;
695 GOTO(out, rc = -ENOMEM);
699 rc = cl_device_init(cd, t);
703 cd->cd_lu_dev.ld_ops = &echo_device_lu_ops;
704 cd->cd_ops = &echo_device_cl_ops;
707 rc = echo_site_init(env, ed);
712 obd = class_name2obd(lustre_cfg_string(cfg, 0));
713 LASSERT(obd != NULL);
714 rc = echo_client_setup(obd, cfg);
717 ed->ed_ec = &obd->u.echo_client;
720 tgt = class_name2obd(lustre_cfg_string(cfg, 1));
721 LASSERT(tgt != NULL);
722 next = tgt->obd_lu_dev;
723 if (next != NULL && !lu_device_is_cl(next))
727 * if echo client is to be stacked upon ost device, the next is NULL
728 * since ost is not a clio device so far
730 tgt_type_name = tgt->obd_type->typ_name;
732 LASSERT(next != NULL);
733 if (next->ld_site != NULL)
734 GOTO(out, rc = -EBUSY);
736 next->ld_site = &ed->ed_site->cs_lu;
737 rc = next->ld_type->ldt_ops->ldto_device_init(env, next,
738 next->ld_type->ldt_name, NULL);
742 /* Trikcy case, I have to determine the obd type since clio
743 * uses the different parameters to initialize objects for
746 if (strcmp(tgt_type_name, LUSTRE_LOV_NAME) == 0)
747 ed->ed_next_islov = 1;
749 LASSERT(strcmp(tgt_type_name, LUSTRE_OSC_NAME) == 0);
751 LASSERT(strcmp(tgt_type_name, LUSTRE_OST_NAME) == 0);
754 RETURN(&cd->cd_lu_dev);
760 rc2 = echo_client_cleanup(obd);
762 CERROR("Cleanup obd device %s error(%d)\n",
767 echo_site_fini(env, ed);
769 cl_device_fini(&ed->ed_cl);
779 static int echo_device_init(const struct lu_env *env, struct lu_device *d,
780 const char *name, struct lu_device *next)
786 static struct lu_device *echo_device_fini(const struct lu_env *env,
789 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
790 struct lu_device *next = ed->ed_next;
793 next = next->ld_type->ldt_ops->ldto_device_fini(env, next);
797 static void echo_lock_release(const struct lu_env *env,
798 struct echo_lock *ecl,
801 struct cl_lock *clk = echo_lock2cl(ecl);
805 cl_lock_release(env, clk, "ec enqueue", ecl->el_object);
807 cl_lock_mutex_get(env, clk);
808 cl_lock_cancel(env, clk);
809 cl_lock_delete(env, clk);
810 cl_lock_mutex_put(env, clk);
812 cl_lock_put(env, clk);
815 static struct lu_device *echo_device_free(const struct lu_env *env,
818 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
819 struct echo_client_obd *ec = ed->ed_ec;
820 struct echo_object *eco;
821 struct lu_device *next = ed->ed_next;
823 CDEBUG(D_INFO, "echo device:%p is going to be freed, next = %p\n",
826 LASSERT(ed->ed_site);
827 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
829 /* check if there are objects still alive.
830 * It shouldn't have any object because lu_site_purge would cleanup
831 * all of cached objects. Anyway, probably the echo device is being
832 * parallelly accessed.
834 cfs_spin_lock(&ec->ec_lock);
835 cfs_list_for_each_entry(eco, &ec->ec_objects, eo_obj_chain)
837 cfs_spin_unlock(&ec->ec_lock);
840 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
843 "Waiting for the reference of echo object to be dropped\n");
845 /* Wait for the last reference to be dropped. */
846 cfs_spin_lock(&ec->ec_lock);
847 while (!cfs_list_empty(&ec->ec_objects)) {
848 cfs_spin_unlock(&ec->ec_lock);
849 CERROR("echo_client still has objects at cleanup time, "
850 "wait for 1 second\n");
851 cfs_schedule_timeout_and_set_state(CFS_TASK_UNINT,
852 cfs_time_seconds(1));
853 cfs_spin_lock(&ec->ec_lock);
855 cfs_spin_unlock(&ec->ec_lock);
857 LASSERT(cfs_list_empty(&ec->ec_locks));
859 CDEBUG(D_INFO, "No object exists, exiting...\n");
861 echo_client_cleanup(d->ld_obd);
864 next = next->ld_type->ldt_ops->ldto_device_free(env, next);
866 LASSERT(ed->ed_site == lu2cl_site(d->ld_site));
867 echo_site_fini(env, ed);
868 cl_device_fini(&ed->ed_cl);
874 static const struct lu_device_type_operations echo_device_type_ops = {
875 .ldto_init = echo_type_init,
876 .ldto_fini = echo_type_fini,
878 .ldto_start = echo_type_start,
879 .ldto_stop = echo_type_stop,
881 .ldto_device_alloc = echo_device_alloc,
882 .ldto_device_free = echo_device_free,
883 .ldto_device_init = echo_device_init,
884 .ldto_device_fini = echo_device_fini
887 static struct lu_device_type echo_device_type = {
888 .ldt_tags = LU_DEVICE_CL,
889 .ldt_name = LUSTRE_ECHO_CLIENT_NAME,
890 .ldt_ops = &echo_device_type_ops,
891 .ldt_ctx_tags = LCT_CL_THREAD
895 /** \defgroup echo_exports Exported operations
897 * exporting functions to echo client
902 /* Interfaces to echo client obd device */
903 static struct echo_object *cl_echo_object_find(struct echo_device *d,
904 struct lov_stripe_md **lsmp)
907 struct echo_thread_info *info;
908 struct echo_object_conf *conf;
909 struct lov_stripe_md *lsm;
910 struct echo_object *eco;
911 struct cl_object *obj;
919 LASSERT(lsm->lsm_object_id);
921 /* Never return an object if the obd is to be freed. */
922 if (echo_dev2cl(d)->cd_lu_dev.ld_obd->obd_stopping)
923 RETURN(ERR_PTR(-ENODEV));
925 env = cl_env_get(&refcheck);
929 info = echo_env_info(env);
930 conf = &info->eti_conf;
932 if (!d->ed_next_islov) {
933 struct lov_oinfo *oinfo = lsm->lsm_oinfo[0];
934 LASSERT(oinfo != NULL);
935 oinfo->loi_id = lsm->lsm_object_id;
936 oinfo->loi_seq = lsm->lsm_object_seq;
937 conf->eoc_cl.u.coc_oinfo = oinfo;
939 struct lustre_md *md;
941 memset(md, 0, sizeof *md);
943 conf->eoc_cl.u.coc_md = md;
948 fid = &info->eti_fid;
951 obj = cl_object_find(env, echo_dev2cl(d), fid, &conf->eoc_cl);
953 GOTO(out, eco = (void*)obj);
955 eco = cl2echo_obj(obj);
956 if (eco->eo_deleted) {
957 cl_object_put(env, obj);
958 eco = ERR_PTR(-EAGAIN);
962 cl_env_put(env, &refcheck);
966 static int cl_echo_object_put(struct echo_object *eco)
969 struct cl_object *obj = echo_obj2cl(eco);
973 env = cl_env_get(&refcheck);
975 RETURN(PTR_ERR(env));
977 /* an external function to kill an object? */
978 if (eco->eo_deleted) {
979 struct lu_object_header *loh = obj->co_lu.lo_header;
980 LASSERT(&eco->eo_hdr == luh2coh(loh));
981 cfs_set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
984 cl_object_put(env, obj);
985 cl_env_put(env, &refcheck);
989 static int cl_echo_enqueue0(struct lu_env *env, struct echo_object *eco,
990 obd_off start, obd_off end, int mode,
991 __u64 *cookie , __u32 enqflags)
995 struct cl_object *obj;
996 struct cl_lock_descr *descr;
997 struct echo_thread_info *info;
1001 info = echo_env_info(env);
1003 descr = &info->eti_descr;
1004 obj = echo_obj2cl(eco);
1006 descr->cld_obj = obj;
1007 descr->cld_start = cl_index(obj, start);
1008 descr->cld_end = cl_index(obj, end);
1009 descr->cld_mode = mode == LCK_PW ? CLM_WRITE : CLM_READ;
1010 descr->cld_enq_flags = enqflags;
1013 lck = cl_lock_request(env, io, descr, "ec enqueue", eco);
1015 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
1016 struct echo_lock *el;
1018 rc = cl_wait(env, lck);
1020 el = cl2echo_lock(cl_lock_at(lck, &echo_device_type));
1021 cfs_spin_lock(&ec->ec_lock);
1022 if (cfs_list_empty(&el->el_chain)) {
1023 cfs_list_add(&el->el_chain, &ec->ec_locks);
1024 el->el_cookie = ++ec->ec_unique;
1026 cfs_atomic_inc(&el->el_refcount);
1027 *cookie = el->el_cookie;
1028 cfs_spin_unlock(&ec->ec_lock);
1030 cl_lock_release(env, lck, "ec enqueue", cfs_current());
1035 static int cl_echo_enqueue(struct echo_object *eco, obd_off start, obd_off end,
1036 int mode, __u64 *cookie)
1038 struct echo_thread_info *info;
1045 env = cl_env_get(&refcheck);
1047 RETURN(PTR_ERR(env));
1049 info = echo_env_info(env);
1052 result = cl_io_init(env, io, CIT_MISC, echo_obj2cl(eco));
1055 LASSERT(result == 0);
1057 result = cl_echo_enqueue0(env, eco, start, end, mode, cookie, 0);
1058 cl_io_fini(env, io);
1062 cl_env_put(env, &refcheck);
1066 static int cl_echo_cancel0(struct lu_env *env, struct echo_device *ed,
1069 struct echo_client_obd *ec = ed->ed_ec;
1070 struct echo_lock *ecl = NULL;
1072 int found = 0, still_used = 0;
1075 LASSERT(ec != NULL);
1076 cfs_spin_lock (&ec->ec_lock);
1077 cfs_list_for_each (el, &ec->ec_locks) {
1078 ecl = cfs_list_entry (el, struct echo_lock, el_chain);
1079 CDEBUG(D_INFO, "ecl: %p, cookie: "LPX64"\n", ecl, ecl->el_cookie);
1080 found = (ecl->el_cookie == cookie);
1082 if (cfs_atomic_dec_and_test(&ecl->el_refcount))
1083 cfs_list_del_init(&ecl->el_chain);
1089 cfs_spin_unlock (&ec->ec_lock);
1094 echo_lock_release(env, ecl, still_used);
1098 static int cl_echo_cancel(struct echo_device *ed, __u64 cookie)
1105 env = cl_env_get(&refcheck);
1107 RETURN(PTR_ERR(env));
1109 rc = cl_echo_cancel0(env, ed, cookie);
1111 cl_env_put(env, &refcheck);
1115 static int cl_echo_async_brw(const struct lu_env *env, struct cl_io *io,
1116 enum cl_req_type unused, struct cl_2queue *queue)
1118 struct cl_page *clp;
1119 struct cl_page *temp;
1123 cl_page_list_for_each_safe(clp, temp, &queue->c2_qin) {
1125 rc = cl_page_cache_add(env, io, clp, CRT_WRITE);
1128 result = result ?: rc;
1133 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
1134 cfs_page_t **pages, int npages, int async)
1137 struct echo_thread_info *info;
1138 struct cl_object *obj = echo_obj2cl(eco);
1139 struct echo_device *ed = eco->eo_dev;
1140 struct cl_2queue *queue;
1142 struct cl_page *clp;
1143 struct lustre_handle lh = { 0 };
1144 int page_size = cl_page_size(obj);
1150 LASSERT((offset & ~CFS_PAGE_MASK) == 0);
1151 LASSERT(ed->ed_next != NULL);
1152 env = cl_env_get(&refcheck);
1154 RETURN(PTR_ERR(env));
1156 info = echo_env_info(env);
1158 queue = &info->eti_queue;
1160 cl_2queue_init(queue);
1161 rc = cl_io_init(env, io, CIT_MISC, obj);
1167 rc = cl_echo_enqueue0(env, eco, offset,
1168 offset + npages * CFS_PAGE_SIZE - 1,
1169 rw == READ ? LCK_PR : LCK_PW, &lh.cookie,
1172 GOTO(error_lock, rc);
1174 for (i = 0; i < npages; i++) {
1176 clp = cl_page_find(env, obj, cl_index(obj, offset),
1177 pages[i], CPT_TRANSIENT);
1182 LASSERT(clp->cp_type == CPT_TRANSIENT);
1184 rc = cl_page_own(env, io, clp);
1186 LASSERT(clp->cp_state == CPS_FREEING);
1187 cl_page_put(env, clp);
1191 cl_2queue_add(queue, clp);
1193 /* drop the reference count for cl_page_find, so that the page
1194 * will be freed in cl_2queue_fini. */
1195 cl_page_put(env, clp);
1196 cl_page_clip(env, clp, 0, page_size);
1198 offset += page_size;
1202 enum cl_req_type typ = rw == READ ? CRT_READ : CRT_WRITE;
1204 async = async && (typ == CRT_WRITE);
1206 rc = cl_echo_async_brw(env, io, typ, queue);
1208 rc = cl_io_submit_sync(env, io, typ, queue,
1210 CDEBUG(D_INFO, "echo_client %s write returns %d\n",
1211 async ? "async" : "sync", rc);
1214 cl_echo_cancel0(env, ed, lh.cookie);
1217 cl_2queue_discard(env, io, queue);
1218 cl_2queue_disown(env, io, queue);
1219 cl_2queue_fini(env, queue);
1220 cl_io_fini(env, io);
1222 cl_env_put(env, &refcheck);
1225 /** @} echo_exports */
1228 static obd_id last_object_id;
1231 echo_copyout_lsm (struct lov_stripe_md *lsm, void *_ulsm, int ulsm_nob)
1233 struct lov_stripe_md *ulsm = _ulsm;
1236 nob = offsetof (struct lov_stripe_md, lsm_oinfo[lsm->lsm_stripe_count]);
1240 if (cfs_copy_to_user (ulsm, lsm, sizeof(ulsm)))
1243 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1244 if (cfs_copy_to_user (ulsm->lsm_oinfo[i], lsm->lsm_oinfo[i],
1245 sizeof(lsm->lsm_oinfo[0])))
1252 echo_copyin_lsm (struct echo_device *ed, struct lov_stripe_md *lsm,
1253 void *ulsm, int ulsm_nob)
1255 struct echo_client_obd *ec = ed->ed_ec;
1258 if (ulsm_nob < sizeof (*lsm))
1261 if (cfs_copy_from_user (lsm, ulsm, sizeof (*lsm)))
1264 if (lsm->lsm_stripe_count > ec->ec_nstripes ||
1265 lsm->lsm_magic != LOV_MAGIC ||
1266 (lsm->lsm_stripe_size & (~CFS_PAGE_MASK)) != 0 ||
1267 ((__u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count > ~0UL))
1271 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1272 if (cfs_copy_from_user(lsm->lsm_oinfo[i],
1273 ((struct lov_stripe_md *)ulsm)-> \
1275 sizeof(lsm->lsm_oinfo[0])))
1281 static int echo_create_object(struct echo_device *ed, int on_target,
1282 struct obdo *oa, void *ulsm, int ulsm_nob,
1283 struct obd_trans_info *oti)
1285 struct echo_object *eco;
1286 struct echo_client_obd *ec = ed->ed_ec;
1287 struct lov_stripe_md *lsm = NULL;
1292 if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
1293 (on_target || /* set_stripe */
1294 ec->ec_nstripes != 0)) { /* LOV */
1295 CERROR ("No valid oid\n");
1299 rc = obd_alloc_memmd(ec->ec_exp, &lsm);
1301 CERROR("Cannot allocate md, rc = %d\n", rc);
1308 rc = echo_copyin_lsm (ed, lsm, ulsm, ulsm_nob);
1312 if (lsm->lsm_stripe_count == 0)
1313 lsm->lsm_stripe_count = ec->ec_nstripes;
1315 if (lsm->lsm_stripe_size == 0)
1316 lsm->lsm_stripe_size = CFS_PAGE_SIZE;
1320 /* setup stripes: indices + default ids if required */
1321 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1322 if (lsm->lsm_oinfo[i]->loi_id == 0)
1323 lsm->lsm_oinfo[i]->loi_id = lsm->lsm_object_id;
1325 lsm->lsm_oinfo[i]->loi_ost_idx =
1326 (idx + i) % ec->ec_nstripes;
1330 /* setup object ID here for !on_target and LOV hint */
1331 if (oa->o_valid & OBD_MD_FLID)
1332 lsm->lsm_object_id = oa->o_id;
1334 if (lsm->lsm_object_id == 0)
1335 lsm->lsm_object_id = ++last_object_id;
1339 /* Only echo objects are allowed to be created */
1340 LASSERT((oa->o_valid & OBD_MD_FLGROUP) &&
1341 (oa->o_seq == FID_SEQ_ECHO));
1342 rc = obd_create(ec->ec_exp, oa, &lsm, oti);
1344 CERROR("Cannot create objects, rc = %d\n", rc);
1350 /* See what object ID we were given */
1351 oa->o_id = lsm->lsm_object_id;
1352 oa->o_valid |= OBD_MD_FLID;
1354 eco = cl_echo_object_find(ed, &lsm);
1356 GOTO(failed, rc = PTR_ERR(eco));
1357 cl_echo_object_put(eco);
1359 CDEBUG(D_INFO, "oa->o_id = %lx\n", (long)oa->o_id);
1364 obd_destroy(ec->ec_exp, oa, lsm, oti, NULL, NULL);
1366 obd_free_memmd(ec->ec_exp, &lsm);
1368 CERROR("create object failed with rc = %d\n", rc);
1372 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
1375 struct echo_client_obd *ec = ed->ed_ec;
1376 struct lov_stripe_md *lsm = NULL;
1377 struct echo_object *eco;
1381 if ((oa->o_valid & OBD_MD_FLID) == 0 ||
1382 oa->o_id == 0) /* disallow use of object id 0 */
1384 CERROR ("No valid oid\n");
1388 rc = obd_alloc_memmd(ec->ec_exp, &lsm);
1392 lsm->lsm_object_id = oa->o_id;
1393 if (oa->o_valid & OBD_MD_FLGROUP)
1394 lsm->lsm_object_seq = oa->o_seq;
1396 lsm->lsm_object_seq = FID_SEQ_ECHO;
1399 eco = cl_echo_object_find(ed, &lsm);
1405 obd_free_memmd(ec->ec_exp, &lsm);
1409 static void echo_put_object(struct echo_object *eco)
1411 if (cl_echo_object_put(eco))
1412 CERROR("echo client: drop an object failed");
1416 echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
1418 unsigned long stripe_count;
1419 unsigned long stripe_size;
1420 unsigned long width;
1421 unsigned long woffset;
1425 if (lsm->lsm_stripe_count <= 1)
1429 stripe_size = lsm->lsm_stripe_size;
1430 stripe_count = lsm->lsm_stripe_count;
1432 /* width = # bytes in all stripes */
1433 width = stripe_size * stripe_count;
1435 /* woffset = offset within a width; offset = whole number of widths */
1436 woffset = do_div (offset, width);
1438 stripe_index = woffset / stripe_size;
1440 *idp = lsm->lsm_oinfo[stripe_index]->loi_id;
1441 *offp = offset * stripe_size + woffset % stripe_size;
1445 echo_client_page_debug_setup(struct lov_stripe_md *lsm,
1446 cfs_page_t *page, int rw, obd_id id,
1447 obd_off offset, obd_off count)
1454 /* no partial pages on the client */
1455 LASSERT(count == CFS_PAGE_SIZE);
1457 addr = cfs_kmap(page);
1459 for (delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
1460 if (rw == OBD_BRW_WRITE) {
1461 stripe_off = offset + delta;
1463 echo_get_stripe_off_id(lsm, &stripe_off, &stripe_id);
1465 stripe_off = 0xdeadbeef00c0ffeeULL;
1466 stripe_id = 0xdeadbeef00c0ffeeULL;
1468 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
1469 stripe_off, stripe_id);
1475 static int echo_client_page_debug_check(struct lov_stripe_md *lsm,
1476 cfs_page_t *page, obd_id id,
1477 obd_off offset, obd_off count)
1486 /* no partial pages on the client */
1487 LASSERT(count == CFS_PAGE_SIZE);
1489 addr = cfs_kmap(page);
1491 for (rc = delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
1492 stripe_off = offset + delta;
1494 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
1496 rc2 = block_debug_check("test_brw",
1497 addr + delta, OBD_ECHO_BLOCK_SIZE,
1498 stripe_off, stripe_id);
1500 CERROR ("Error in echo object "LPX64"\n", id);
1509 static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
1510 struct echo_object *eco, obd_off offset,
1511 obd_size count, int async,
1512 struct obd_trans_info *oti)
1514 struct echo_client_obd *ec = ed->ed_ec;
1515 struct lov_stripe_md *lsm = eco->eo_lsm;
1517 struct brw_page *pga;
1518 struct brw_page *pgp;
1528 verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
1529 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
1530 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
1532 gfp_mask = ((oa->o_id & 2) == 0) ? CFS_ALLOC_STD : CFS_ALLOC_HIGHUSER;
1534 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
1535 LASSERT(lsm != NULL);
1536 LASSERT(lsm->lsm_object_id == oa->o_id);
1539 (count & (~CFS_PAGE_MASK)) != 0)
1542 /* XXX think again with misaligned I/O */
1543 npages = count >> CFS_PAGE_SHIFT;
1545 if (rw == OBD_BRW_WRITE)
1546 brw_flags = OBD_BRW_ASYNC;
1548 OBD_ALLOC(pga, npages * sizeof(*pga));
1552 OBD_ALLOC(pages, npages * sizeof(*pages));
1553 if (pages == NULL) {
1554 OBD_FREE(pga, npages * sizeof(*pga));
1558 for (i = 0, pgp = pga, off = offset;
1560 i++, pgp++, off += CFS_PAGE_SIZE) {
1562 LASSERT (pgp->pg == NULL); /* for cleanup */
1565 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
1566 if (pgp->pg == NULL)
1570 pgp->count = CFS_PAGE_SIZE;
1572 pgp->flag = brw_flags;
1575 echo_client_page_debug_setup(lsm, pgp->pg, rw,
1576 oa->o_id, off, pgp->count);
1579 if (ed->ed_next == NULL) {
1580 struct obd_info oinfo = { { { 0 } } };
1583 rc = obd_brw(rw, ec->ec_exp, &oinfo, npages, pga, oti);
1585 rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
1588 if (rc != 0 || rw != OBD_BRW_READ)
1591 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
1592 if (pgp->pg == NULL)
1597 vrc = echo_client_page_debug_check(lsm, pgp->pg, oa->o_id,
1598 pgp->off, pgp->count);
1599 if (vrc != 0 && rc == 0)
1602 OBD_PAGE_FREE(pgp->pg);
1604 OBD_FREE(pga, npages * sizeof(*pga));
1605 OBD_FREE(pages, npages * sizeof(*pages));
1609 static int echo_client_prep_commit(struct obd_export *exp, int rw,
1610 struct obdo *oa, struct echo_object *eco,
1611 obd_off offset, obd_size count,
1612 obd_size batch, struct obd_trans_info *oti)
1614 struct lov_stripe_md *lsm = eco->eo_lsm;
1615 struct obd_ioobj ioo;
1616 struct niobuf_local *lnb;
1617 struct niobuf_remote *rnb;
1619 obd_size npages, tot_pages;
1623 if (count <= 0 || (count & (~CFS_PAGE_MASK)) != 0 ||
1624 (lsm != NULL && lsm->lsm_object_id != oa->o_id))
1627 npages = batch >> CFS_PAGE_SHIFT;
1628 tot_pages = count >> CFS_PAGE_SHIFT;
1630 OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
1631 OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
1633 if (lnb == NULL || rnb == NULL)
1634 GOTO(out, ret = -ENOMEM);
1636 obdo_to_ioobj(oa, &ioo);
1640 for(; tot_pages; tot_pages -= npages) {
1643 if (tot_pages < npages)
1646 for (i = 0; i < npages; i++, off += CFS_PAGE_SIZE) {
1647 rnb[i].offset = off;
1648 rnb[i].len = CFS_PAGE_SIZE;
1651 ioo.ioo_bufcnt = npages;
1652 oti->oti_transno = 0;
1655 ret = obd_preprw(rw, exp, oa, 1, &ioo, rnb, &lpages, lnb, oti,
1659 LASSERT(lpages == npages);
1661 for (i = 0; i < lpages; i++) {
1662 cfs_page_t *page = lnb[i].page;
1664 /* read past eof? */
1665 if (page == NULL && lnb[i].rc == 0)
1668 if (oa->o_id == ECHO_PERSISTENT_OBJID ||
1669 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
1670 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
1673 if (rw == OBD_BRW_WRITE)
1674 echo_client_page_debug_setup(lsm, page, rw,
1679 echo_client_page_debug_check(lsm, page,
1685 ret = obd_commitrw(rw, exp, oa, 1,&ioo,rnb,npages,lnb,oti,ret);
1689 /* Reset oti otherwise it would confuse ldiskfs. */
1690 memset(oti, 0, sizeof(*oti));
1695 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
1697 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
1701 static int echo_client_brw_ioctl(int rw, struct obd_export *exp,
1702 struct obd_ioctl_data *data)
1704 struct obd_device *obd = class_exp2obd(exp);
1705 struct echo_device *ed = obd2echo_dev(obd);
1706 struct echo_client_obd *ec = ed->ed_ec;
1707 struct obd_trans_info dummy_oti = { 0 };
1708 struct obdo *oa = &data->ioc_obdo1;
1709 struct echo_object *eco;
1714 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
1716 rc = echo_get_object(&eco, ed, oa);
1720 oa->o_valid &= ~OBD_MD_FLHANDLE;
1722 switch((long)data->ioc_pbuf1) {
1727 rc = echo_client_kbrw(ed, rw, oa,
1728 eco, data->ioc_offset,
1729 data->ioc_count, async, &dummy_oti);
1732 rc = echo_client_prep_commit(ec->ec_exp, rw, oa,
1733 eco, data->ioc_offset,
1734 data->ioc_count, data->ioc_plen1,
1740 echo_put_object(eco);
1745 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
1746 int mode, obd_off offset, obd_size nob)
1748 struct echo_device *ed = obd2echo_dev(exp->exp_obd);
1749 struct lustre_handle *ulh = &oa->o_handle;
1750 struct echo_object *eco;
1755 if (ed->ed_next == NULL)
1756 RETURN(-EOPNOTSUPP);
1758 if (!(mode == LCK_PR || mode == LCK_PW))
1761 if ((offset & (~CFS_PAGE_MASK)) != 0 ||
1762 (nob & (~CFS_PAGE_MASK)) != 0)
1765 rc = echo_get_object (&eco, ed, oa);
1769 end = (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
1770 rc = cl_echo_enqueue(eco, offset, end, mode, &ulh->cookie);
1772 oa->o_valid |= OBD_MD_FLHANDLE;
1773 CDEBUG(D_INFO, "Cookie is "LPX64"\n", ulh->cookie);
1775 echo_put_object(eco);
1780 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
1782 struct echo_device *ed = obd2echo_dev(exp->exp_obd);
1783 __u64 cookie = oa->o_handle.cookie;
1785 if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
1788 CDEBUG(D_INFO, "Cookie is "LPX64"\n", cookie);
1789 return cl_echo_cancel(ed, cookie);
1793 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
1794 int len, void *karg, void *uarg)
1796 struct obd_device *obd = exp->exp_obd;
1797 struct echo_device *ed = obd2echo_dev(obd);
1798 struct echo_client_obd *ec = ed->ed_ec;
1799 struct echo_object *eco;
1800 struct obd_ioctl_data *data = karg;
1801 struct obd_trans_info dummy_oti;
1802 struct oti_req_ack_lock *ack_lock;
1805 int rw = OBD_BRW_READ;
1810 #ifndef HAVE_UNLOCKED_IOCTL
1811 cfs_unlock_kernel();
1814 memset(&dummy_oti, 0, sizeof(dummy_oti));
1816 oa = &data->ioc_obdo1;
1817 if (!(oa->o_valid & OBD_MD_FLGROUP)) {
1818 oa->o_valid |= OBD_MD_FLGROUP;
1819 oa->o_seq = FID_SEQ_ECHO;
1822 /* This FID is unpacked just for validation at this point */
1823 rc = fid_ostid_unpack(&fid, &oa->o_oi, 0);
1828 case OBD_IOC_CREATE: /* may create echo object */
1829 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1830 GOTO (out, rc = -EPERM);
1832 rc = echo_create_object (ed, 1, oa,
1833 data->ioc_pbuf1, data->ioc_plen1,
1837 case OBD_IOC_DESTROY:
1838 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1839 GOTO (out, rc = -EPERM);
1841 rc = echo_get_object (&eco, ed, oa);
1843 rc = obd_destroy(ec->ec_exp, oa, eco->eo_lsm,
1844 &dummy_oti, NULL, NULL);
1846 eco->eo_deleted = 1;
1847 echo_put_object(eco);
1851 case OBD_IOC_GETATTR:
1852 rc = echo_get_object (&eco, ed, oa);
1854 struct obd_info oinfo = { { { 0 } } };
1855 oinfo.oi_md = eco->eo_lsm;
1857 rc = obd_getattr(ec->ec_exp, &oinfo);
1858 echo_put_object(eco);
1862 case OBD_IOC_SETATTR:
1863 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1864 GOTO (out, rc = -EPERM);
1866 rc = echo_get_object (&eco, ed, oa);
1868 struct obd_info oinfo = { { { 0 } } };
1870 oinfo.oi_md = eco->eo_lsm;
1872 rc = obd_setattr(ec->ec_exp, &oinfo, NULL);
1873 echo_put_object(eco);
1877 case OBD_IOC_BRW_WRITE:
1878 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1879 GOTO (out, rc = -EPERM);
1883 case OBD_IOC_BRW_READ:
1884 rc = echo_client_brw_ioctl(rw, exp, data);
1887 case ECHO_IOC_GET_STRIPE:
1888 rc = echo_get_object(&eco, ed, oa);
1890 rc = echo_copyout_lsm(eco->eo_lsm, data->ioc_pbuf1,
1892 echo_put_object(eco);
1896 case ECHO_IOC_SET_STRIPE:
1897 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1898 GOTO (out, rc = -EPERM);
1900 if (data->ioc_pbuf1 == NULL) { /* unset */
1901 rc = echo_get_object(&eco, ed, oa);
1903 eco->eo_deleted = 1;
1904 echo_put_object(eco);
1907 rc = echo_create_object(ed, 0, oa,
1909 data->ioc_plen1, &dummy_oti);
1913 case ECHO_IOC_ENQUEUE:
1914 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1915 GOTO (out, rc = -EPERM);
1917 rc = echo_client_enqueue(exp, oa,
1918 data->ioc_conn1, /* lock mode */
1920 data->ioc_count);/*extent*/
1923 case ECHO_IOC_CANCEL:
1924 rc = echo_client_cancel(exp, oa);
1928 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
1929 GOTO (out, rc = -ENOTTY);
1935 /* XXX this should be in a helper also called by target_send_reply */
1936 for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
1938 if (!ack_lock->mode)
1940 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
1943 #ifndef HAVE_UNLOCKED_IOCTL
1950 static int echo_client_setup(struct obd_device *obddev, struct lustre_cfg *lcfg)
1952 struct echo_client_obd *ec = &obddev->u.echo_client;
1953 struct obd_device *tgt;
1954 struct obd_uuid echo_uuid = { "ECHO_UUID" };
1955 struct obd_connect_data *ocd = NULL;
1959 if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1960 CERROR("requires a TARGET OBD name\n");
1964 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
1965 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
1966 CERROR("device not attached or not set up (%s)\n",
1967 lustre_cfg_string(lcfg, 1));
1971 cfs_spin_lock_init (&ec->ec_lock);
1972 CFS_INIT_LIST_HEAD (&ec->ec_objects);
1973 CFS_INIT_LIST_HEAD (&ec->ec_locks);
1975 ec->ec_nstripes = 0;
1977 OBD_ALLOC(ocd, sizeof(*ocd));
1979 CERROR("Can't alloc ocd connecting to %s\n",
1980 lustre_cfg_string(lcfg, 1));
1984 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
1985 OBD_CONNECT_GRANT | OBD_CONNECT_FULL20;
1986 ocd->ocd_version = LUSTRE_VERSION_CODE;
1987 ocd->ocd_group = FID_SEQ_ECHO;
1989 rc = obd_connect(NULL, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
1991 /* Turn off pinger because it connects to tgt obd directly. */
1992 cfs_spin_lock(&tgt->obd_dev_lock);
1993 cfs_list_del_init(&ec->ec_exp->exp_obd_chain_timed);
1994 cfs_spin_unlock(&tgt->obd_dev_lock);
1997 OBD_FREE(ocd, sizeof(*ocd));
2000 CERROR("fail to connect to device %s\n",
2001 lustre_cfg_string(lcfg, 1));
2008 static int echo_client_cleanup(struct obd_device *obddev)
2010 struct echo_client_obd *ec = &obddev->u.echo_client;
2014 if (!cfs_list_empty(&obddev->obd_exports)) {
2015 CERROR("still has clients!\n");
2019 LASSERT(cfs_atomic_read(&ec->ec_exp->exp_refcount) > 0);
2020 rc = obd_disconnect(ec->ec_exp);
2022 CERROR("fail to disconnect device: %d\n", rc);
2027 static int echo_client_connect(const struct lu_env *env,
2028 struct obd_export **exp,
2029 struct obd_device *src, struct obd_uuid *cluuid,
2030 struct obd_connect_data *data, void *localdata)
2033 struct lustre_handle conn = { 0 };
2036 rc = class_connect(&conn, src, cluuid);
2038 *exp = class_conn2export(&conn);
2044 static int echo_client_disconnect(struct obd_export *exp)
2047 struct obd_device *obd;
2048 struct echo_client_obd *ec;
2049 struct ec_lock *ecl;
2055 GOTO(out, rc = -EINVAL);
2059 ec = &obd->u.echo_client;
2061 /* no more contention on export's lock list */
2062 while (!cfs_list_empty (&exp->exp_ec_data.eced_locks)) {
2063 ecl = cfs_list_entry (exp->exp_ec_data.eced_locks.next,
2064 struct ec_lock, ecl_exp_chain);
2065 cfs_list_del (&ecl->ecl_exp_chain);
2067 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm,
2068 ecl->ecl_mode, &ecl->ecl_lock_handle);
2070 CDEBUG (D_INFO, "Cancel lock on object "LPX64" on disconnect "
2071 "(%d)\n", ecl->ecl_object->eco_id, rc);
2073 echo_put_object (ecl->ecl_object);
2074 OBD_FREE (ecl, sizeof (*ecl));
2078 rc = class_disconnect(exp);
2084 static struct obd_ops echo_obd_ops = {
2085 .o_owner = THIS_MODULE,
2088 .o_setup = echo_client_setup,
2089 .o_cleanup = echo_client_cleanup,
2092 .o_iocontrol = echo_client_iocontrol,
2093 .o_connect = echo_client_connect,
2094 .o_disconnect = echo_client_disconnect
2097 int echo_client_init(void)
2099 struct lprocfs_static_vars lvars = { 0 };
2102 lprocfs_echo_init_vars(&lvars);
2103 rc = class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
2104 LUSTRE_ECHO_CLIENT_NAME, &echo_device_type);
2106 lu_kmem_init(echo_caches);
2110 void echo_client_exit(void)
2112 class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
2113 lu_kmem_fini(echo_caches);
2116 /** @} echo_client */