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 2008 Sun Microsystems, Inc. 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 struct list_head eo_obj_chain;
73 struct lov_stripe_md *eo_lsm;
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;
85 cfs_page_t *ep_vmpage;
89 struct cl_lock_slice el_cl;
90 struct list_head el_chain;
91 struct echo_object *el_object;
96 struct cl_io_slice ei_cl;
101 struct cl_req_slice er_cl;
105 static int echo_client_setup(struct obd_device *obddev,
106 struct lustre_cfg *lcfg);
107 static int echo_client_cleanup(struct obd_device *obddev);
110 /** \defgroup echo_helpers Helper functions
113 static inline struct echo_device *cl2echo_dev(const struct cl_device *dev)
115 return container_of0(dev, struct echo_device, ed_cl);
118 static inline struct cl_device *echo_dev2cl(struct echo_device *d)
123 static inline struct echo_device *obd2echo_dev(const struct obd_device *obd)
125 return cl2echo_dev(lu2cl_dev(obd->obd_lu_dev));
128 static inline struct cl_object *echo_obj2cl(struct echo_object *eco)
133 static inline struct echo_object *cl2echo_obj(const struct cl_object *o)
135 return container_of(o, struct echo_object, eo_cl);
138 static inline struct echo_page *cl2echo_page(const struct cl_page_slice *s)
140 return container_of(s, struct echo_page, ep_cl);
143 static inline struct echo_lock *cl2echo_lock(const struct cl_lock_slice *s)
145 return container_of(s, struct echo_lock, el_cl);
148 static inline struct cl_lock *echo_lock2cl(const struct echo_lock *ecl)
150 return ecl->el_cl.cls_lock;
153 static struct lu_context_key echo_thread_key;
154 static inline struct echo_thread_info *echo_env_info(const struct lu_env *env)
156 struct echo_thread_info *info;
157 info = lu_context_key_get(&env->le_ctx, &echo_thread_key);
158 LASSERT(info != NULL);
163 struct echo_object_conf *cl2echo_conf(const struct cl_object_conf *c)
165 return container_of(c, struct echo_object_conf, eoc_cl);
168 static inline void lsm2fid(struct lov_stripe_md *lsm, struct lu_fid *fid)
171 fid->f_seq = lsm->lsm_object_gr << 16 | lsm->lsm_object_id >> 32;
172 fid->f_oid = lsm->lsm_object_id;
174 /** @} echo_helpers */
176 static struct echo_object *cl_echo_object_find(struct echo_device *d,
177 struct lov_stripe_md **lsm);
178 static int cl_echo_object_put(struct echo_object *eco);
179 static int cl_echo_enqueue (struct echo_object *eco, obd_off start,
180 obd_off end, int mode, __u64 *cookie);
181 static int cl_echo_cancel (struct echo_device *d, __u64 cookie);
182 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
183 cfs_page_t **pages, int npages, int async);
185 static struct echo_thread_info *echo_env_info(const struct lu_env *env);
187 struct echo_thread_info {
188 struct echo_object_conf eti_conf;
189 struct lustre_md eti_md;
191 struct cl_2queue eti_queue;
193 struct cl_lock_descr eti_descr;
194 struct lu_fid eti_fid;
197 /* No session used right now */
198 struct echo_session_info {
202 static cfs_mem_cache_t *echo_page_kmem;
203 static cfs_mem_cache_t *echo_lock_kmem;
204 static cfs_mem_cache_t *echo_object_kmem;
205 static cfs_mem_cache_t *echo_thread_kmem;
206 static cfs_mem_cache_t *echo_session_kmem;
207 //static cfs_mem_cache_t *echo_req_kmem;
209 static struct lu_kmem_descr echo_caches[] = {
211 .ckd_cache = &echo_page_kmem,
212 .ckd_name = "echo_page_kmem",
213 .ckd_size = sizeof (struct echo_page)
216 .ckd_cache = &echo_lock_kmem,
217 .ckd_name = "echo_lock_kmem",
218 .ckd_size = sizeof (struct echo_lock)
221 .ckd_cache = &echo_object_kmem,
222 .ckd_name = "echo_object_kmem",
223 .ckd_size = sizeof (struct echo_object)
226 .ckd_cache = &echo_thread_kmem,
227 .ckd_name = "echo_thread_kmem",
228 .ckd_size = sizeof (struct echo_thread_info)
231 .ckd_cache = &echo_session_kmem,
232 .ckd_name = "echo_session_kmem",
233 .ckd_size = sizeof (struct echo_session_info)
237 .ckd_cache = &echo_req_kmem,
238 .ckd_name = "echo_req_kmem",
239 .ckd_size = sizeof (struct echo_req)
247 /** \defgroup echo_page Page operations
249 * Echo page operations.
253 cfs_page_t *echo_page_vmpage(const struct lu_env *env,
254 const struct cl_page_slice *slice)
256 return cl2echo_page(slice)->ep_vmpage;
259 static void echo_page_discard(const struct lu_env *env,
260 const struct cl_page_slice *slice,
261 struct cl_io *unused)
263 cl_page_delete(env, slice->cpl_page);
266 static int echo_page_is_vmlocked(const struct lu_env *env,
267 const struct cl_page_slice *slice)
272 static void echo_page_completion(const struct lu_env *env,
273 const struct cl_page_slice *slice,
276 LASSERT(slice->cpl_page->cp_sync_io != NULL);
279 static void echo_page_fini(const struct lu_env *env,
280 struct cl_page_slice *slice)
282 struct echo_page *ep = cl2echo_page(slice);
283 struct echo_object *eco = cl2echo_obj(slice->cpl_obj);
284 cfs_page_t *vmpage = ep->ep_vmpage;
287 atomic_dec(&eco->eo_npages);
288 page_cache_release(vmpage);
289 OBD_SLAB_FREE_PTR(ep, echo_page_kmem);
293 static int echo_page_prep(const struct lu_env *env,
294 const struct cl_page_slice *slice,
295 struct cl_io *unused)
300 static int echo_page_print(const struct lu_env *env,
301 const struct cl_page_slice *slice,
302 void *cookie, lu_printer_t printer)
304 struct echo_page *ep = cl2echo_page(slice);
306 (*printer)(env, cookie, LUSTRE_ECHO_CLIENT_NAME"-page@%p vm@%p\n",
311 static const struct cl_page_operations echo_page_ops = {
312 .cpo_discard = echo_page_discard,
313 .cpo_vmpage = echo_page_vmpage,
314 .cpo_fini = echo_page_fini,
315 .cpo_print = echo_page_print,
316 .cpo_is_vmlocked = echo_page_is_vmlocked,
319 .cpo_prep = echo_page_prep,
320 .cpo_completion = echo_page_completion,
323 .cpo_prep = echo_page_prep,
324 .cpo_completion = echo_page_completion,
330 /** \defgroup echo_lock Locking
332 * echo lock operations
336 static void echo_lock_fini(const struct lu_env *env,
337 struct cl_lock_slice *slice)
339 struct echo_lock *ecl = cl2echo_lock(slice);
341 LASSERT(list_empty(&ecl->el_chain));
342 OBD_SLAB_FREE_PTR(ecl, echo_lock_kmem);
345 static void echo_lock_delete(const struct lu_env *env,
346 const struct cl_lock_slice *slice)
348 struct echo_lock *ecl = cl2echo_lock(slice);
350 LASSERT(list_empty(&ecl->el_chain));
353 static int echo_lock_fits_into(const struct lu_env *env,
354 const struct cl_lock_slice *slice,
355 const struct cl_lock_descr *need,
356 const struct cl_io *unused)
361 static struct cl_lock_operations echo_lock_ops = {
362 .clo_fini = echo_lock_fini,
363 .clo_delete = echo_lock_delete,
364 .clo_fits_into = echo_lock_fits_into
369 /** \defgroup echo_cl_ops cl_object operations
371 * operations for cl_object
375 static struct cl_page *echo_page_init(const struct lu_env *env,
376 struct cl_object *obj,
377 struct cl_page *page, cfs_page_t *vmpage)
379 struct echo_page *ep;
382 OBD_SLAB_ALLOC_PTR_GFP(ep, echo_page_kmem, CFS_ALLOC_IO);
384 struct echo_object *eco = cl2echo_obj(obj);
385 ep->ep_vmpage = vmpage;
386 page_cache_get(vmpage);
387 cl_page_slice_add(page, &ep->ep_cl, obj, &echo_page_ops);
388 atomic_inc(&eco->eo_npages);
390 RETURN(ERR_PTR(ep ? 0 : -ENOMEM));
393 static int echo_io_init(const struct lu_env *env, struct cl_object *obj,
399 static int echo_lock_init(const struct lu_env *env,
400 struct cl_object *obj, struct cl_lock *lock,
401 const struct cl_io *unused)
403 struct echo_lock *el;
406 OBD_SLAB_ALLOC_PTR_GFP(el, echo_lock_kmem, CFS_ALLOC_IO);
408 cl_lock_slice_add(lock, &el->el_cl, obj, &echo_lock_ops);
409 el->el_object = cl2echo_obj(obj);
410 CFS_INIT_LIST_HEAD(&el->el_chain);
412 RETURN(el == NULL ? -ENOMEM : 0);
415 static int echo_conf_set(const struct lu_env *env, struct cl_object *obj,
416 const struct cl_object_conf *conf)
421 static const struct cl_object_operations echo_cl_obj_ops = {
422 .coo_page_init = echo_page_init,
423 .coo_lock_init = echo_lock_init,
424 .coo_io_init = echo_io_init,
425 .coo_conf_set = echo_conf_set
427 /** @} echo_cl_ops */
429 /** \defgroup echo_lu_ops lu_object operations
431 * operations for echo lu object.
435 static int echo_object_init(const struct lu_env *env, struct lu_object *obj,
436 const struct lu_object_conf *conf)
438 const struct cl_object_conf *cconf = lu2cl_conf(conf);
439 struct echo_object_conf *econf = cl2echo_conf(cconf);
440 struct echo_device *ed = cl2echo_dev(lu2cl_dev(obj->lo_dev));
441 struct echo_client_obd *ec = ed->ed_ec;
442 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
446 struct lu_object *below;
447 struct lu_device *under;
450 below = under->ld_ops->ldo_object_alloc(env, obj->lo_header,
454 lu_object_add(obj, below);
457 LASSERT(econf->eoc_md);
458 eco->eo_lsm = *econf->eoc_md;
460 atomic_set(&eco->eo_npages, 0);
462 /* clear the lsm pointer so that it won't get freed. */
463 *econf->eoc_md = NULL;
465 spin_lock(&ec->ec_lock);
466 list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
467 spin_unlock(&ec->ec_lock);
472 static void echo_object_free(const struct lu_env *env, struct lu_object *obj)
474 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
475 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
476 struct lov_stripe_md *lsm = eco->eo_lsm;
479 LASSERT(atomic_read(&eco->eo_npages) == 0);
481 spin_lock(&ec->ec_lock);
482 list_del_init(&eco->eo_obj_chain);
483 spin_unlock(&ec->ec_lock);
486 lu_object_header_fini(obj->lo_header);
489 obd_free_memmd(ec->ec_exp, &lsm);
490 OBD_SLAB_FREE_PTR(eco, echo_object_kmem);
494 static int echo_object_print(const struct lu_env *env, void *cookie,
495 lu_printer_t p, const struct lu_object *o)
497 struct echo_object *obj = cl2echo_obj(lu2cl(o));
499 return (*p)(env, cookie, "echoclient-object@%p", obj);
503 static const struct lu_object_operations echo_lu_obj_ops = {
504 .loo_object_init = echo_object_init,
505 .loo_object_delete = NULL,
506 .loo_object_release = NULL,
507 .loo_object_free = echo_object_free,
508 .loo_object_print = echo_object_print,
509 .loo_object_invariant = NULL
511 /** @} echo_lu_ops */
513 /** \defgroup echo_lu_dev_ops lu_device operations
515 * Operations for echo lu device.
519 static struct lu_object *echo_object_alloc(const struct lu_env *env,
520 const struct lu_object_header *hdr,
521 struct lu_device *dev)
523 struct echo_object *eco;
524 struct lu_object *obj = NULL;
527 /* we're the top dev. */
528 LASSERT(hdr == NULL);
529 OBD_SLAB_ALLOC_PTR_GFP(eco, echo_object_kmem, CFS_ALLOC_IO);
531 struct cl_object_header *hdr = &eco->eo_hdr;
533 obj = &echo_obj2cl(eco)->co_lu;
534 cl_object_header_init(hdr);
535 lu_object_init(obj, &hdr->coh_lu, dev);
536 lu_object_add_top(&hdr->coh_lu, obj);
538 eco->eo_cl.co_ops = &echo_cl_obj_ops;
539 obj->lo_ops = &echo_lu_obj_ops;
544 static struct lu_device_operations echo_device_lu_ops = {
545 .ldo_object_alloc = echo_object_alloc,
547 /** @} echo_lu_dev_ops */
549 static struct cl_device_operations echo_device_cl_ops = {
552 /** \defgroup echo_init Setup and teardown
554 * Init and fini functions for echo client.
558 static int echo_site_init(const struct lu_env *env, struct echo_device *ed)
560 struct cl_site *site = &ed->ed_site_myself;
563 /* initialize site */
564 rc = cl_site_init(site, &ed->ed_cl);
566 CERROR("Cannot initilize site for echo client(%d)\n", rc);
570 rc = lu_site_init_finish(&site->cs_lu);
578 static void echo_site_fini(const struct lu_env *env, struct echo_device *ed)
581 cl_site_fini(ed->ed_site);
586 static void *echo_thread_key_init(const struct lu_context *ctx,
587 struct lu_context_key *key)
589 struct echo_thread_info *info;
591 OBD_SLAB_ALLOC_PTR_GFP(info, echo_thread_kmem, CFS_ALLOC_IO);
593 info = ERR_PTR(-ENOMEM);
597 static void echo_thread_key_fini(const struct lu_context *ctx,
598 struct lu_context_key *key, void *data)
600 struct echo_thread_info *info = data;
601 OBD_SLAB_FREE_PTR(info, echo_thread_kmem);
604 static void echo_thread_key_exit(const struct lu_context *ctx,
605 struct lu_context_key *key, void *data)
609 static struct lu_context_key echo_thread_key = {
610 .lct_tags = LCT_CL_THREAD,
611 .lct_init = echo_thread_key_init,
612 .lct_fini = echo_thread_key_fini,
613 .lct_exit = echo_thread_key_exit
616 static void *echo_session_key_init(const struct lu_context *ctx,
617 struct lu_context_key *key)
619 struct echo_session_info *session;
621 OBD_SLAB_ALLOC_PTR_GFP(session, echo_session_kmem, CFS_ALLOC_IO);
623 session = ERR_PTR(-ENOMEM);
627 static void echo_session_key_fini(const struct lu_context *ctx,
628 struct lu_context_key *key, void *data)
630 struct echo_session_info *session = data;
631 OBD_SLAB_FREE_PTR(session, echo_session_kmem);
634 static void echo_session_key_exit(const struct lu_context *ctx,
635 struct lu_context_key *key, void *data)
639 static struct lu_context_key echo_session_key = {
640 .lct_tags = LCT_SESSION,
641 .lct_init = echo_session_key_init,
642 .lct_fini = echo_session_key_fini,
643 .lct_exit = echo_session_key_exit
646 LU_TYPE_INIT_FINI(echo, &echo_thread_key, &echo_session_key);
648 static struct lu_device *echo_device_alloc(const struct lu_env *env,
649 struct lu_device_type *t,
650 struct lustre_cfg *cfg)
652 struct lu_device *next;
653 struct echo_device *ed;
654 struct cl_device *cd;
655 struct obd_device *obd = NULL; /* to keep compiler happy */
656 struct obd_device *tgt;
657 const char *tgt_type_name;
664 GOTO(out, rc = -ENOMEM);
668 rc = cl_device_init(cd, t);
672 cd->cd_lu_dev.ld_ops = &echo_device_lu_ops;
673 cd->cd_ops = &echo_device_cl_ops;
676 rc = echo_site_init(env, ed);
681 obd = class_name2obd(lustre_cfg_string(cfg, 0));
682 LASSERT(obd != NULL);
683 rc = echo_client_setup(obd, cfg);
686 ed->ed_ec = &obd->u.echo_client;
689 tgt = class_name2obd(lustre_cfg_string(cfg, 1));
690 LASSERT(tgt != NULL);
691 next = tgt->obd_lu_dev;
692 if (next != NULL && !lu_device_is_cl(next))
696 * if echo client is to be stacked upon ost device, the next is NULL
697 * since ost is not a clio device so far
699 tgt_type_name = tgt->obd_type->typ_name;
701 LASSERT(next != NULL);
702 if (next->ld_site != NULL)
703 GOTO(out, rc = -EBUSY);
705 next->ld_site = &ed->ed_site->cs_lu;
706 rc = next->ld_type->ldt_ops->ldto_device_init(env, next,
707 next->ld_type->ldt_name, NULL);
711 /* Trikcy case, I have to determine the obd type since clio
712 * uses the different parameters to initialize objects for
715 if (strcmp(tgt_type_name, LUSTRE_LOV_NAME) == 0)
716 ed->ed_next_islov = 1;
718 LASSERT(strcmp(tgt_type_name, LUSTRE_OSC_NAME) == 0);
720 LASSERT(strcmp(tgt_type_name, LUSTRE_OST_NAME) == 0);
723 RETURN(&cd->cd_lu_dev);
729 rc2 = echo_client_cleanup(obd);
731 CERROR("Cleanup obd device %s error(%d)\n",
736 echo_site_fini(env, ed);
738 cl_device_fini(&ed->ed_cl);
748 static int echo_device_init(const struct lu_env *env, struct lu_device *d,
749 const char *name, struct lu_device *next)
755 static struct lu_device *echo_device_fini(const struct lu_env *env,
758 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
759 struct lu_device *next = ed->ed_next;
762 next = next->ld_type->ldt_ops->ldto_device_fini(env, next);
766 static struct lu_device *echo_device_free(const struct lu_env *env,
769 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
770 struct echo_client_obd *ec = ed->ed_ec;
771 struct echo_object *eco;
772 struct lu_device *next = ed->ed_next;
774 CDEBUG(D_INFO, "echo device:%p is going to be freed, next = %p\n", ed, next);
777 spin_lock(&ec->ec_lock);
778 while (!list_empty(&ec->ec_locks)) {
779 struct echo_lock *ecl = list_entry(ec->ec_locks.next,
780 struct echo_lock, el_chain);
781 struct cl_lock *lock = echo_lock2cl(ecl);
783 list_del_init(&ecl->el_chain);
784 spin_unlock(&ec->ec_lock);
786 CERROR("echo client: pending lock %p\n", ecl);
790 cl_lock_release(env, lock, "ec enqueue", ecl->el_object);
792 cl_lock_mutex_get(env, lock);
793 cl_lock_cancel(env, lock);
794 cl_lock_delete(env, lock);
795 cl_lock_mutex_put(env, lock);
796 cl_lock_put(env, lock);
798 spin_lock(&ec->ec_lock);
800 spin_unlock(&ec->ec_lock);
802 LASSERT(ed->ed_site);
803 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
805 /* check if there are objects still alive.
806 * It shouldn't have any object because lu_site_purge would cleanup
807 * all of cached objects. Anyway, probably the echo device is being
808 * parallelly accessed.
810 spin_lock(&ec->ec_lock);
811 list_for_each_entry(eco, &ec->ec_objects, eo_obj_chain)
813 spin_unlock(&ec->ec_lock);
816 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
818 CDEBUG(D_INFO, "Waiting for the reference of echo object to be dropped\n");
820 /* Wait for the last reference to be dropped. */
821 spin_lock(&ec->ec_lock);
822 while (!list_empty(&ec->ec_objects)) {
823 spin_unlock(&ec->ec_lock);
824 CERROR("echo_client still has objects at cleanup time, "
825 "wait for 1 second\n");
826 cfs_schedule_timeout(CFS_TASK_UNINT, cfs_time_seconds(1));
827 spin_lock(&ec->ec_lock);
829 spin_unlock(&ec->ec_lock);
831 CDEBUG(D_INFO, "No object exists, exiting...\n");
833 echo_client_cleanup(d->ld_obd);
836 next = next->ld_type->ldt_ops->ldto_device_free(env, next);
838 LASSERT(ed->ed_site == lu2cl_site(d->ld_site));
839 echo_site_fini(env, ed);
840 cl_device_fini(&ed->ed_cl);
846 static const struct lu_device_type_operations echo_device_type_ops = {
847 .ldto_init = echo_type_init,
848 .ldto_fini = echo_type_fini,
850 .ldto_start = echo_type_start,
851 .ldto_stop = echo_type_stop,
853 .ldto_device_alloc = echo_device_alloc,
854 .ldto_device_free = echo_device_free,
855 .ldto_device_init = echo_device_init,
856 .ldto_device_fini = echo_device_fini
859 static struct lu_device_type echo_device_type = {
860 .ldt_tags = LU_DEVICE_CL,
861 .ldt_name = LUSTRE_ECHO_CLIENT_NAME,
862 .ldt_ops = &echo_device_type_ops,
863 .ldt_ctx_tags = LCT_CL_THREAD
867 /** \defgroup echo_exports Exported operations
869 * exporting functions to echo client
874 /* Interfaces to echo client obd device */
875 static struct echo_object *cl_echo_object_find(struct echo_device *d,
876 struct lov_stripe_md **lsmp)
879 struct echo_thread_info *info;
880 struct echo_object_conf *conf;
881 struct lov_stripe_md *lsm;
882 struct echo_object *eco;
883 struct cl_object *obj;
891 LASSERT(lsm->lsm_object_id);
893 /* Never return an object if the obd is to be freed. */
894 if (echo_dev2cl(d)->cd_lu_dev.ld_obd->obd_stopping)
895 RETURN(ERR_PTR(-ENODEV));
897 env = cl_env_get(&refcheck);
901 info = echo_env_info(env);
902 conf = &info->eti_conf;
904 if (!d->ed_next_islov) {
905 struct lov_oinfo *oinfo = lsm->lsm_oinfo[0];
906 LASSERT(oinfo != NULL);
907 oinfo->loi_id = lsm->lsm_object_id;
908 oinfo->loi_gr = lsm->lsm_object_gr;
909 conf->eoc_cl.u.coc_oinfo = oinfo;
911 struct lustre_md *md;
913 memset(md, 0, sizeof *md);
915 conf->eoc_cl.u.coc_md = md;
920 fid = &info->eti_fid;
923 obj = cl_object_find(env, echo_dev2cl(d), fid, &conf->eoc_cl);
925 GOTO(out, eco = (void*)obj);
927 eco = cl2echo_obj(obj);
928 if (eco->eo_deleted) {
929 cl_object_put(env, obj);
930 eco = ERR_PTR(-EAGAIN);
934 cl_env_put(env, &refcheck);
938 static int cl_echo_object_put(struct echo_object *eco)
941 struct cl_object *obj = echo_obj2cl(eco);
945 env = cl_env_get(&refcheck);
947 RETURN(PTR_ERR(env));
949 /* an external function to kill an object? */
950 if (eco->eo_deleted) {
951 struct lu_object_header *loh = obj->co_lu.lo_header;
952 LASSERT(&eco->eo_hdr == luh2coh(loh));
953 set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
954 cl_object_prune(env, obj);
957 cl_object_put(env, obj);
958 cl_env_put(env, &refcheck);
962 static int cl_echo_enqueue(struct echo_object *eco, obd_off start, obd_off end,
963 int mode, __u64 *cookie)
967 struct echo_thread_info *info;
969 struct cl_lock_descr *descr;
970 struct cl_object *obj = echo_obj2cl(eco);
975 env = cl_env_get(&refcheck);
977 RETURN(PTR_ERR(env));
979 info = echo_env_info(env);
980 descr = &info->eti_descr;
981 descr->cld_obj = obj;
982 descr->cld_start = cl_index(obj, start);
983 descr->cld_end = cl_index(obj, end);
984 descr->cld_mode = mode == LCK_PW ? CLM_WRITE : CLM_READ;
988 result = cl_io_init(env, io, CIT_MISC, obj);
991 LASSERT(result == 0);
994 lck = cl_lock_request(env, io, descr, CEF_ASYNC, "ec enqueue", eco);
996 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
997 struct echo_lock *el;
999 result = cl_wait(env, lck);
1001 el = cl2echo_lock(cl_lock_at(lck, &echo_device_type));
1002 spin_lock(&ec->ec_lock);
1003 list_add(&el->el_chain, &ec->ec_locks);
1004 *cookie = el->el_cookie = ++ec->ec_unique;
1005 spin_unlock(&ec->ec_lock);
1007 cl_lock_release(env, lck, "ec enqueue", cfs_current());
1009 cl_io_fini(env, io);
1013 cl_env_put(env, &refcheck);
1017 static int cl_echo_cancel(struct echo_device *ed, __u64 cookie)
1019 struct echo_client_obd *ec = ed->ed_ec;
1020 struct echo_lock *ecl = NULL;
1021 struct list_head *el;
1029 env = cl_env_get(&refcheck);
1031 RETURN(PTR_ERR(env));
1033 spin_lock (&ec->ec_lock);
1034 list_for_each (el, &ec->ec_locks) {
1035 ecl = list_entry (el, struct echo_lock, el_chain);
1036 CDEBUG(D_INFO, "ecl: %p, cookie: %llx\n", ecl, ecl->el_cookie);
1037 found = (ecl->el_cookie == cookie);
1039 list_del_init(&ecl->el_chain);
1043 spin_unlock (&ec->ec_lock);
1047 struct cl_lock *clk = echo_lock2cl(ecl);
1051 cl_lock_release(env, clk, "ec enqueue", ecl->el_object);
1053 cl_lock_mutex_get(env, clk);
1054 cl_lock_cancel(env, clk);
1055 cl_lock_delete(env, clk);
1056 cl_lock_mutex_put(env, clk);
1057 cl_lock_put(env, clk);
1060 cl_env_put(env, &refcheck);
1064 static int cl_echo_async_brw(const struct lu_env *env, struct cl_io *io,
1065 enum cl_req_type unused, struct cl_2queue *queue)
1067 struct cl_page *clp;
1068 struct cl_page *temp;
1072 cl_page_list_for_each_safe(clp, temp, &queue->c2_qin) {
1074 rc = cl_page_cache_add(env, io, clp, CRT_WRITE);
1077 result = result ?: rc;
1082 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
1083 cfs_page_t **pages, int npages, int async)
1086 struct echo_thread_info *info;
1087 struct cl_object *obj = echo_obj2cl(eco);
1088 struct echo_device *ed = eco->eo_dev;
1089 struct cl_2queue *queue;
1091 struct cl_page *clp;
1093 int page_size = cl_page_size(obj);
1099 LASSERT(ed->ed_next != NULL);
1100 env = cl_env_get(&refcheck);
1102 RETURN(PTR_ERR(env));
1104 info = echo_env_info(env);
1106 queue = &info->eti_queue;
1108 cl_2queue_init(queue);
1109 rc = cl_io_init(env, io, CIT_MISC, obj);
1114 for (i = 0; i < npages; i++) {
1116 clp = cl_page_find(env, obj, cl_index(obj, offset),
1117 pages[i], CPT_TRANSIENT);
1122 LASSERT(clp->cp_type == CPT_TRANSIENT);
1124 rc = cl_page_own(env, io, clp);
1126 LASSERT(clp->cp_state == CPS_FREEING);
1127 cl_page_put(env, clp);
1131 cl_2queue_add(queue, clp);
1133 /* drop the reference count for cl_page_find, so that the page
1134 * will be freed in cl_2queue_fini. */
1135 cl_page_put(env, clp);
1136 offset += page_size;
1140 enum cl_req_type typ = rw == READ ? CRT_READ : CRT_WRITE;
1142 async = async && (typ == CRT_WRITE);
1144 rc = cl_echo_async_brw(env, io, typ, queue);
1146 rc = cl_io_submit_sync(env, io, typ, queue,
1148 CDEBUG(D_INFO, "echo_client %s write returns %d\n",
1149 async ? "async" : "sync", rc);
1152 cl_2queue_discard(env, io, queue);
1153 cl_2queue_disown(env, io, queue);
1154 cl_2queue_fini(env, queue);
1155 cl_io_fini(env, io);
1159 cl_env_put(env, &refcheck);
1162 /** @} echo_exports */
1165 static obd_id last_object_id;
1168 echo_copyout_lsm (struct lov_stripe_md *lsm, void *_ulsm, int ulsm_nob)
1170 struct lov_stripe_md *ulsm = _ulsm;
1173 nob = offsetof (struct lov_stripe_md, lsm_oinfo[lsm->lsm_stripe_count]);
1177 if (copy_to_user (ulsm, lsm, sizeof(ulsm)))
1180 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1181 if (copy_to_user (ulsm->lsm_oinfo[i], lsm->lsm_oinfo[i],
1182 sizeof(lsm->lsm_oinfo[0])))
1189 echo_copyin_lsm (struct echo_device *ed, struct lov_stripe_md *lsm,
1190 void *ulsm, int ulsm_nob)
1192 struct echo_client_obd *ec = ed->ed_ec;
1195 if (ulsm_nob < sizeof (*lsm))
1198 if (copy_from_user (lsm, ulsm, sizeof (*lsm)))
1201 if (lsm->lsm_stripe_count > ec->ec_nstripes ||
1202 lsm->lsm_magic != LOV_MAGIC ||
1203 (lsm->lsm_stripe_size & (~CFS_PAGE_MASK)) != 0 ||
1204 ((__u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count > ~0UL))
1208 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1209 if (copy_from_user(lsm->lsm_oinfo[i],
1210 ((struct lov_stripe_md *)ulsm)->lsm_oinfo[i],
1211 sizeof(lsm->lsm_oinfo[0])))
1217 static int echo_create_object(struct echo_device *ed, int on_target,
1218 struct obdo *oa, void *ulsm, int ulsm_nob,
1219 struct obd_trans_info *oti)
1221 struct echo_object *eco;
1222 struct echo_client_obd *ec = ed->ed_ec;
1223 struct lov_stripe_md *lsm = NULL;
1228 if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
1229 (on_target || /* set_stripe */
1230 ec->ec_nstripes != 0)) { /* LOV */
1231 CERROR ("No valid oid\n");
1235 rc = obd_alloc_memmd(ec->ec_exp, &lsm);
1237 CERROR("Cannot allocate md, rc = %d\n", rc);
1244 rc = echo_copyin_lsm (ed, lsm, ulsm, ulsm_nob);
1248 if (lsm->lsm_stripe_count == 0)
1249 lsm->lsm_stripe_count = ec->ec_nstripes;
1251 if (lsm->lsm_stripe_size == 0)
1252 lsm->lsm_stripe_size = CFS_PAGE_SIZE;
1256 /* setup stripes: indices + default ids if required */
1257 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1258 if (lsm->lsm_oinfo[i]->loi_id == 0)
1259 lsm->lsm_oinfo[i]->loi_id = lsm->lsm_object_id;
1261 lsm->lsm_oinfo[i]->loi_ost_idx =
1262 (idx + i) % ec->ec_nstripes;
1266 /* setup object ID here for !on_target and LOV hint */
1267 if (oa->o_valid & OBD_MD_FLID)
1268 lsm->lsm_object_id = oa->o_id;
1270 if (lsm->lsm_object_id == 0)
1271 lsm->lsm_object_id = ++last_object_id;
1275 /* Only echo objects are allowed to be created */
1276 LASSERT((oa->o_valid & OBD_MD_FLGROUP) &&
1277 (oa->o_gr == FILTER_GROUP_ECHO));
1278 rc = obd_create(ec->ec_exp, oa, &lsm, oti);
1280 CERROR("Cannot create objects, rc = %d\n", rc);
1286 /* See what object ID we were given */
1287 oa->o_id = lsm->lsm_object_id;
1288 oa->o_valid |= OBD_MD_FLID;
1290 eco = cl_echo_object_find(ed, &lsm);
1292 GOTO(failed, rc = PTR_ERR(eco));
1293 cl_echo_object_put(eco);
1295 CDEBUG(D_INFO, "oa->o_id = %lx\n", (long)oa->o_id);
1300 obd_destroy(ec->ec_exp, oa, lsm, oti, NULL, NULL);
1302 obd_free_memmd(ec->ec_exp, &lsm);
1304 CERROR("create object failed with rc = %d\n", rc);
1308 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
1311 struct echo_client_obd *ec = ed->ed_ec;
1312 struct lov_stripe_md *lsm = NULL;
1313 struct echo_object *eco;
1317 if ((oa->o_valid & OBD_MD_FLID) == 0 ||
1318 oa->o_id == 0) /* disallow use of object id 0 */
1320 CERROR ("No valid oid\n");
1324 rc = obd_alloc_memmd(ec->ec_exp, &lsm);
1328 lsm->lsm_object_id = oa->o_id;
1329 if (oa->o_valid & OBD_MD_FLGROUP)
1330 lsm->lsm_object_gr = oa->o_gr;
1332 lsm->lsm_object_gr = FILTER_GROUP_ECHO;
1335 eco = cl_echo_object_find(ed, &lsm);
1341 obd_free_memmd(ec->ec_exp, &lsm);
1345 static void echo_put_object(struct echo_object *eco)
1347 if (cl_echo_object_put(eco))
1348 CERROR("echo client: drop an object failed");
1352 echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
1354 unsigned long stripe_count;
1355 unsigned long stripe_size;
1356 unsigned long width;
1357 unsigned long woffset;
1361 if (lsm->lsm_stripe_count <= 1)
1365 stripe_size = lsm->lsm_stripe_size;
1366 stripe_count = lsm->lsm_stripe_count;
1368 /* width = # bytes in all stripes */
1369 width = stripe_size * stripe_count;
1371 /* woffset = offset within a width; offset = whole number of widths */
1372 woffset = do_div (offset, width);
1374 stripe_index = woffset / stripe_size;
1376 *idp = lsm->lsm_oinfo[stripe_index]->loi_id;
1377 *offp = offset * stripe_size + woffset % stripe_size;
1381 echo_client_page_debug_setup(struct lov_stripe_md *lsm,
1382 cfs_page_t *page, int rw, obd_id id,
1383 obd_off offset, obd_off count)
1390 /* no partial pages on the client */
1391 LASSERT(count == CFS_PAGE_SIZE);
1393 addr = cfs_kmap(page);
1395 for (delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
1396 if (rw == OBD_BRW_WRITE) {
1397 stripe_off = offset + delta;
1399 echo_get_stripe_off_id(lsm, &stripe_off, &stripe_id);
1401 stripe_off = 0xdeadbeef00c0ffeeULL;
1402 stripe_id = 0xdeadbeef00c0ffeeULL;
1404 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
1405 stripe_off, stripe_id);
1411 static int echo_client_page_debug_check(struct lov_stripe_md *lsm,
1412 cfs_page_t *page, obd_id id,
1413 obd_off offset, obd_off count)
1422 /* no partial pages on the client */
1423 LASSERT(count == CFS_PAGE_SIZE);
1425 addr = cfs_kmap(page);
1427 for (rc = delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
1428 stripe_off = offset + delta;
1430 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
1432 rc2 = block_debug_check("test_brw",
1433 addr + delta, OBD_ECHO_BLOCK_SIZE,
1434 stripe_off, stripe_id);
1436 CERROR ("Error in echo object "LPX64"\n", id);
1445 static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
1446 struct echo_object *eco, obd_off offset,
1447 obd_size count, int async,
1448 struct obd_trans_info *oti)
1450 struct echo_client_obd *ec = ed->ed_ec;
1451 struct lov_stripe_md *lsm = eco->eo_lsm;
1453 struct brw_page *pga;
1454 struct brw_page *pgp;
1463 verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
1464 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
1465 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
1467 gfp_mask = ((oa->o_id & 2) == 0) ? CFS_ALLOC_STD : CFS_ALLOC_HIGHUSER;
1469 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
1470 LASSERT(lsm != NULL);
1471 LASSERT(lsm->lsm_object_id == oa->o_id);
1474 (count & (~CFS_PAGE_MASK)) != 0)
1477 /* XXX think again with misaligned I/O */
1478 npages = count >> CFS_PAGE_SHIFT;
1480 OBD_ALLOC(pga, npages * sizeof(*pga));
1484 OBD_ALLOC(pages, npages * sizeof(*pages));
1485 if (pages == NULL) {
1486 OBD_FREE(pga, npages * sizeof(*pga));
1490 for (i = 0, pgp = pga, off = offset;
1492 i++, pgp++, off += CFS_PAGE_SIZE) {
1494 LASSERT (pgp->pg == NULL); /* for cleanup */
1497 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
1498 if (pgp->pg == NULL)
1502 pgp->count = CFS_PAGE_SIZE;
1507 echo_client_page_debug_setup(lsm, pgp->pg, rw,
1508 oa->o_id, off, pgp->count);
1511 if (ed->ed_next == NULL) {
1512 struct obd_info oinfo = { { { 0 } } };
1515 rc = obd_brw(rw, ec->ec_exp, &oinfo, npages, pga, oti);
1517 rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
1520 if (rc != 0 || rw != OBD_BRW_READ)
1523 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
1524 if (pgp->pg == NULL)
1529 vrc = echo_client_page_debug_check(lsm, pgp->pg, oa->o_id,
1530 pgp->off, pgp->count);
1531 if (vrc != 0 && rc == 0)
1534 OBD_PAGE_FREE(pgp->pg);
1536 OBD_FREE(pga, npages * sizeof(*pga));
1537 OBD_FREE(pages, npages * sizeof(*pages));
1541 static int echo_client_prep_commit(struct obd_export *exp, int rw,
1542 struct obdo *oa, struct echo_object *eco,
1543 obd_off offset, obd_size count,
1544 obd_size batch, struct obd_trans_info *oti)
1546 struct lov_stripe_md *lsm = eco->eo_lsm;
1547 struct obd_ioobj ioo;
1548 struct niobuf_local *lnb;
1549 struct niobuf_remote *rnb;
1551 obd_size npages, tot_pages;
1555 if (count <= 0 || (count & (~CFS_PAGE_MASK)) != 0 ||
1556 (lsm != NULL && lsm->lsm_object_id != oa->o_id))
1559 npages = batch >> CFS_PAGE_SHIFT;
1560 tot_pages = count >> CFS_PAGE_SHIFT;
1562 OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
1563 OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
1565 if (lnb == NULL || rnb == NULL)
1566 GOTO(out, ret = -ENOMEM);
1568 obdo_to_ioobj(oa, &ioo);
1572 for(; tot_pages; tot_pages -= npages) {
1575 if (tot_pages < npages)
1578 for (i = 0; i < npages; i++, off += CFS_PAGE_SIZE) {
1579 rnb[i].offset = off;
1580 rnb[i].len = CFS_PAGE_SIZE;
1583 ioo.ioo_bufcnt = npages;
1584 oti->oti_transno = 0;
1587 ret = obd_preprw(rw, exp, oa, 1, &ioo, rnb, &lpages, lnb, oti,
1591 LASSERT(lpages == npages);
1593 for (i = 0; i < lpages; i++) {
1594 cfs_page_t *page = lnb[i].page;
1596 /* read past eof? */
1597 if (page == NULL && lnb[i].rc == 0)
1600 if (oa->o_id == ECHO_PERSISTENT_OBJID ||
1601 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
1602 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
1605 if (rw == OBD_BRW_WRITE)
1606 echo_client_page_debug_setup(lsm, page, rw,
1611 echo_client_page_debug_check(lsm, page,
1617 ret = obd_commitrw(rw, exp, oa, 1,&ioo,rnb,npages,lnb,oti,ret);
1621 /* Reset oti otherwise it would confuse ldiskfs. */
1622 memset(oti, 0, sizeof(*oti));
1627 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
1629 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
1633 static int echo_client_brw_ioctl(int rw, struct obd_export *exp,
1634 struct obd_ioctl_data *data)
1636 struct obd_device *obd = class_exp2obd(exp);
1637 struct echo_device *ed = obd2echo_dev(obd);
1638 struct echo_client_obd *ec = ed->ed_ec;
1639 struct obd_trans_info dummy_oti = { .oti_thread = NULL };
1640 struct obdo *oa = &data->ioc_obdo1;
1641 struct echo_object *eco;
1646 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
1648 rc = echo_get_object(&eco, ed, oa);
1652 oa->o_valid &= ~OBD_MD_FLHANDLE;
1654 switch((long)data->ioc_pbuf1) {
1659 rc = echo_client_kbrw(ed, rw, oa,
1660 eco, data->ioc_offset,
1661 data->ioc_count, async, &dummy_oti);
1664 rc = echo_client_prep_commit(ec->ec_exp, rw, oa,
1665 eco, data->ioc_offset,
1666 data->ioc_count, data->ioc_plen1,
1672 echo_put_object(eco);
1677 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
1678 int mode, obd_off offset, obd_size nob)
1680 struct echo_device *ed = obd2echo_dev(exp->exp_obd);
1681 struct lustre_handle *ulh = &oa->o_handle;
1682 struct echo_object *eco;
1687 if (ed->ed_next == NULL)
1688 RETURN(-EOPNOTSUPP);
1690 if (!(mode == LCK_PR || mode == LCK_PW))
1693 if ((offset & (~CFS_PAGE_MASK)) != 0 ||
1694 (nob & (~CFS_PAGE_MASK)) != 0)
1697 rc = echo_get_object (&eco, ed, oa);
1701 end = (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
1702 rc = cl_echo_enqueue(eco, offset, end, mode, &ulh->cookie);
1704 oa->o_valid |= OBD_MD_FLHANDLE;
1705 CDEBUG(D_INFO, "Cookie is %llx\n", ulh->cookie);
1707 echo_put_object(eco);
1712 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
1714 struct echo_device *ed = obd2echo_dev(exp->exp_obd);
1715 __u64 cookie = oa->o_handle.cookie;
1717 if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
1720 CDEBUG(D_INFO, "Cookie is %llx\n", cookie);
1721 return cl_echo_cancel(ed, cookie);
1725 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
1726 int len, void *karg, void *uarg)
1728 struct obd_device *obd = exp->exp_obd;
1729 struct echo_device *ed = obd2echo_dev(obd);
1730 struct echo_client_obd *ec = ed->ed_ec;
1731 struct echo_object *eco;
1732 struct obd_ioctl_data *data = karg;
1733 struct obd_trans_info dummy_oti;
1734 struct oti_req_ack_lock *ack_lock;
1736 int rw = OBD_BRW_READ;
1743 memset(&dummy_oti, 0, sizeof(dummy_oti));
1745 oa = &data->ioc_obdo1;
1746 if (!(oa->o_valid & OBD_MD_FLGROUP)) {
1747 oa->o_valid |= OBD_MD_FLGROUP;
1748 oa->o_gr = FILTER_GROUP_ECHO;
1750 /* assume we can touch filter native objects with echo device. */
1751 /* LASSERT(oa->o_gr == FILTER_GROUP_ECHO); */
1754 case OBD_IOC_CREATE: /* may create echo object */
1755 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1756 GOTO (out, rc = -EPERM);
1758 rc = echo_create_object (ed, 1, oa,
1759 data->ioc_pbuf1, data->ioc_plen1,
1763 case OBD_IOC_DESTROY:
1764 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1765 GOTO (out, rc = -EPERM);
1767 rc = echo_get_object (&eco, ed, oa);
1769 rc = obd_destroy(ec->ec_exp, oa, eco->eo_lsm,
1770 &dummy_oti, NULL, NULL);
1772 eco->eo_deleted = 1;
1773 echo_put_object(eco);
1777 case OBD_IOC_GETATTR:
1778 rc = echo_get_object (&eco, ed, oa);
1780 struct obd_info oinfo = { { { 0 } } };
1781 oinfo.oi_md = eco->eo_lsm;
1783 rc = obd_getattr(ec->ec_exp, &oinfo);
1784 echo_put_object(eco);
1788 case OBD_IOC_SETATTR:
1789 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1790 GOTO (out, rc = -EPERM);
1792 rc = echo_get_object (&eco, ed, oa);
1794 struct obd_info oinfo = { { { 0 } } };
1796 oinfo.oi_md = eco->eo_lsm;
1798 rc = obd_setattr(ec->ec_exp, &oinfo, NULL);
1799 echo_put_object(eco);
1803 case OBD_IOC_BRW_WRITE:
1804 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1805 GOTO (out, rc = -EPERM);
1809 case OBD_IOC_BRW_READ:
1810 rc = echo_client_brw_ioctl(rw, exp, data);
1813 case ECHO_IOC_GET_STRIPE:
1814 rc = echo_get_object(&eco, ed, oa);
1816 rc = echo_copyout_lsm(eco->eo_lsm, data->ioc_pbuf1,
1818 echo_put_object(eco);
1822 case ECHO_IOC_SET_STRIPE:
1823 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1824 GOTO (out, rc = -EPERM);
1826 if (data->ioc_pbuf1 == NULL) { /* unset */
1827 rc = echo_get_object(&eco, ed, oa);
1829 eco->eo_deleted = 1;
1830 echo_put_object(eco);
1833 rc = echo_create_object(ed, 0, oa,
1835 data->ioc_plen1, &dummy_oti);
1839 case ECHO_IOC_ENQUEUE:
1840 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1841 GOTO (out, rc = -EPERM);
1843 rc = echo_client_enqueue(exp, oa,
1844 data->ioc_conn1, /* lock mode */
1846 data->ioc_count);/*extent*/
1849 case ECHO_IOC_CANCEL:
1850 rc = echo_client_cancel(exp, oa);
1854 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
1855 GOTO (out, rc = -ENOTTY);
1861 /* XXX this should be in a helper also called by target_send_reply */
1862 for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
1864 if (!ack_lock->mode)
1866 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
1874 static int echo_client_setup(struct obd_device *obddev, struct lustre_cfg *lcfg)
1876 struct echo_client_obd *ec = &obddev->u.echo_client;
1877 struct obd_device *tgt;
1878 struct obd_uuid echo_uuid = { "ECHO_UUID" };
1879 struct obd_connect_data *ocd = NULL;
1883 if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1884 CERROR("requires a TARGET OBD name\n");
1888 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
1889 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
1890 CERROR("device not attached or not set up (%s)\n",
1891 lustre_cfg_string(lcfg, 1));
1895 spin_lock_init (&ec->ec_lock);
1896 CFS_INIT_LIST_HEAD (&ec->ec_objects);
1897 CFS_INIT_LIST_HEAD (&ec->ec_locks);
1899 ec->ec_nstripes = 0;
1901 OBD_ALLOC(ocd, sizeof(*ocd));
1903 CERROR("Can't alloc ocd connecting to %s\n",
1904 lustre_cfg_string(lcfg, 1));
1908 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
1910 ocd->ocd_version = LUSTRE_VERSION_CODE;
1911 ocd->ocd_group = FILTER_GROUP_ECHO;
1913 rc = obd_connect(NULL, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
1915 /* Turn off pinger because it connects to tgt obd directly. */
1916 spin_lock(&tgt->obd_dev_lock);
1917 list_del_init(&ec->ec_exp->exp_obd_chain_timed);
1918 spin_unlock(&tgt->obd_dev_lock);
1921 OBD_FREE(ocd, sizeof(*ocd));
1924 CERROR("fail to connect to device %s\n",
1925 lustre_cfg_string(lcfg, 1));
1932 static int echo_client_cleanup(struct obd_device *obddev)
1934 struct echo_client_obd *ec = &obddev->u.echo_client;
1938 if (!list_empty(&obddev->obd_exports)) {
1939 CERROR("still has clients!\n");
1943 LASSERT(atomic_read(&ec->ec_exp->exp_refcount) > 0);
1944 rc = obd_disconnect(ec->ec_exp);
1946 CERROR("fail to disconnect device: %d\n", rc);
1951 static int echo_client_connect(const struct lu_env *env,
1952 struct obd_export **exp,
1953 struct obd_device *src, struct obd_uuid *cluuid,
1954 struct obd_connect_data *data, void *localdata)
1957 struct lustre_handle conn = { 0 };
1960 rc = class_connect(&conn, src, cluuid);
1962 *exp = class_conn2export(&conn);
1968 static int echo_client_disconnect(struct obd_export *exp)
1971 struct obd_device *obd;
1972 struct echo_client_obd *ec;
1973 struct ec_lock *ecl;
1979 GOTO(out, rc = -EINVAL);
1983 ec = &obd->u.echo_client;
1985 /* no more contention on export's lock list */
1986 while (!list_empty (&exp->exp_ec_data.eced_locks)) {
1987 ecl = list_entry (exp->exp_ec_data.eced_locks.next,
1988 struct ec_lock, ecl_exp_chain);
1989 list_del (&ecl->ecl_exp_chain);
1991 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm,
1992 ecl->ecl_mode, &ecl->ecl_lock_handle);
1994 CDEBUG (D_INFO, "Cancel lock on object "LPX64" on disconnect "
1995 "(%d)\n", ecl->ecl_object->eco_id, rc);
1997 echo_put_object (ecl->ecl_object);
1998 OBD_FREE (ecl, sizeof (*ecl));
2002 rc = class_disconnect(exp);
2008 static struct obd_ops echo_obd_ops = {
2009 .o_owner = THIS_MODULE,
2012 .o_setup = echo_client_setup,
2013 .o_cleanup = echo_client_cleanup,
2016 .o_iocontrol = echo_client_iocontrol,
2017 .o_connect = echo_client_connect,
2018 .o_disconnect = echo_client_disconnect
2021 int echo_client_init(void)
2023 struct lprocfs_static_vars lvars = { 0 };
2026 lprocfs_echo_init_vars(&lvars);
2027 rc = class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
2028 LUSTRE_ECHO_CLIENT_NAME, &echo_device_type);
2030 lu_kmem_init(echo_caches);
2034 void echo_client_exit(void)
2036 class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
2037 lu_kmem_fini(echo_caches);
2040 /** @} echo_client */