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"
54 struct cl_device ed_cl;
55 struct echo_client_obd *ed_ec;
57 struct cl_site ed_site_myself;
58 struct cl_site *ed_site;
59 struct lu_device *ed_next;
64 struct cl_object eo_cl;
65 struct cl_object_header eo_hdr;
67 struct echo_device *eo_dev;
68 struct list_head eo_obj_chain;
69 struct lov_stripe_md *eo_lsm;
74 struct echo_object_conf {
75 struct cl_object_conf eoc_cl;
76 struct lov_stripe_md **eoc_md;
80 struct cl_page_slice ep_cl;
81 cfs_page_t *ep_vmpage;
85 struct cl_lock_slice el_cl;
86 struct list_head el_chain;
87 struct echo_object *el_object;
92 struct cl_io_slice ei_cl;
97 struct cl_req_slice er_cl;
101 static int echo_client_setup(struct obd_device *obddev,
102 struct lustre_cfg *lcfg);
103 static int echo_client_cleanup(struct obd_device *obddev);
106 /** \defgroup echo_helpers
109 static inline struct echo_device *cl2echo_dev(const struct cl_device *dev)
111 return container_of0(dev, struct echo_device, ed_cl);
114 static inline struct cl_device *echo_dev2cl(struct echo_device *d)
119 static inline struct echo_device *obd2echo_dev(const struct obd_device *obd)
121 return cl2echo_dev(lu2cl_dev(obd->obd_lu_dev));
124 static inline struct cl_object *echo_obj2cl(struct echo_object *eco)
129 static inline struct echo_object *cl2echo_obj(const struct cl_object *o)
131 return container_of(o, struct echo_object, eo_cl);
134 static inline struct echo_page *cl2echo_page(const struct cl_page_slice *s)
136 return container_of(s, struct echo_page, ep_cl);
139 static inline struct echo_lock *cl2echo_lock(const struct cl_lock_slice *s)
141 return container_of(s, struct echo_lock, el_cl);
144 static inline struct cl_lock *echo_lock2cl(const struct echo_lock *ecl)
146 return ecl->el_cl.cls_lock;
149 static struct lu_context_key echo_thread_key;
150 static inline struct echo_thread_info *echo_env_info(const struct lu_env *env)
152 struct echo_thread_info *info;
153 info = lu_context_key_get(&env->le_ctx, &echo_thread_key);
154 LASSERT(info != NULL);
159 struct echo_object_conf *cl2echo_conf(const struct cl_object_conf *c)
161 return container_of(c, struct echo_object_conf, eoc_cl);
164 static inline void lsm2fid(struct lov_stripe_md *lsm, struct lu_fid *fid)
167 fid->f_seq = lsm->lsm_object_gr << 16 | lsm->lsm_object_id >> 32;
168 fid->f_oid = lsm->lsm_object_id;
170 /** @} echo_helpers */
172 static struct echo_object *cl_echo_object_find(struct echo_device *d,
173 struct lov_stripe_md **lsm);
174 static int cl_echo_object_put(struct echo_object *eco);
175 static int cl_echo_enqueue (struct echo_object *eco, obd_off start,
176 obd_off end, int mode, __u64 *cookie);
177 static int cl_echo_cancel (struct echo_device *d, __u64 cookie);
178 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
179 cfs_page_t **pages, int npages, int async);
181 static struct echo_thread_info *echo_env_info(const struct lu_env *env);
183 struct echo_thread_info {
184 struct echo_object_conf eti_conf;
185 struct lustre_md eti_md;
187 struct cl_2queue eti_queue;
189 struct cl_lock_descr eti_descr;
190 struct lu_fid eti_fid;
193 /* No session used right now */
194 struct echo_session_info {
198 static cfs_mem_cache_t *echo_page_kmem;
199 static cfs_mem_cache_t *echo_lock_kmem;
200 static cfs_mem_cache_t *echo_object_kmem;
201 static cfs_mem_cache_t *echo_thread_kmem;
202 static cfs_mem_cache_t *echo_session_kmem;
203 //static cfs_mem_cache_t *echo_req_kmem;
205 static struct lu_kmem_descr echo_caches[] = {
207 .ckd_cache = &echo_page_kmem,
208 .ckd_name = "echo_page_kmem",
209 .ckd_size = sizeof (struct echo_page)
212 .ckd_cache = &echo_lock_kmem,
213 .ckd_name = "echo_lock_kmem",
214 .ckd_size = sizeof (struct echo_lock)
217 .ckd_cache = &echo_object_kmem,
218 .ckd_name = "echo_object_kmem",
219 .ckd_size = sizeof (struct echo_object)
222 .ckd_cache = &echo_thread_kmem,
223 .ckd_name = "echo_thread_kmem",
224 .ckd_size = sizeof (struct echo_thread_info)
227 .ckd_cache = &echo_session_kmem,
228 .ckd_name = "echo_session_kmem",
229 .ckd_size = sizeof (struct echo_session_info)
233 .ckd_cache = &echo_req_kmem,
234 .ckd_name = "echo_req_kmem",
235 .ckd_size = sizeof (struct echo_req)
243 /** defgroup echo_page echo_page
245 * Echo page operations.
249 cfs_page_t *echo_page_vmpage(const struct lu_env *env,
250 const struct cl_page_slice *slice)
252 return cl2echo_page(slice)->ep_vmpage;
255 static void echo_page_discard(const struct lu_env *env,
256 const struct cl_page_slice *slice,
257 struct cl_io *unused)
259 cl_page_delete(env, slice->cpl_page);
262 static int echo_page_is_vmlocked(const struct lu_env *env,
263 const struct cl_page_slice *slice)
268 static void echo_page_completion(const struct lu_env *env,
269 const struct cl_page_slice *slice,
272 LASSERT(slice->cpl_page->cp_sync_io != NULL);
275 static void echo_page_fini(const struct lu_env *env,
276 struct cl_page_slice *slice)
278 struct echo_page *ep = cl2echo_page(slice);
279 struct echo_object *eco = cl2echo_obj(slice->cpl_obj);
280 cfs_page_t *vmpage = ep->ep_vmpage;
283 atomic_dec(&eco->eo_npages);
284 page_cache_release(vmpage);
285 OBD_SLAB_FREE_PTR(ep, echo_page_kmem);
289 static int echo_page_prep(const struct lu_env *env,
290 const struct cl_page_slice *slice,
291 struct cl_io *unused)
296 static int echo_page_print(const struct lu_env *env,
297 const struct cl_page_slice *slice,
298 void *cookie, lu_printer_t printer)
300 struct echo_page *ep = cl2echo_page(slice);
302 (*printer)(env, cookie, LUSTRE_ECHO_CLIENT_NAME"-page@%p vm@%p\n",
307 static const struct cl_page_operations echo_page_ops = {
308 .cpo_discard = echo_page_discard,
309 .cpo_vmpage = echo_page_vmpage,
310 .cpo_fini = echo_page_fini,
311 .cpo_print = echo_page_print,
312 .cpo_is_vmlocked = echo_page_is_vmlocked,
315 .cpo_prep = echo_page_prep,
316 .cpo_completion = echo_page_completion,
319 .cpo_prep = echo_page_prep,
320 .cpo_completion = echo_page_completion,
326 /** \defgroup echo_lock echo_lock
328 * echo lock operations
332 static void echo_lock_fini(const struct lu_env *env,
333 struct cl_lock_slice *slice)
335 struct echo_lock *ecl = cl2echo_lock(slice);
337 LASSERT(list_empty(&ecl->el_chain));
338 OBD_SLAB_FREE_PTR(ecl, echo_lock_kmem);
341 static void echo_lock_delete(const struct lu_env *env,
342 const struct cl_lock_slice *slice)
344 struct echo_lock *ecl = cl2echo_lock(slice);
346 LASSERT(list_empty(&ecl->el_chain));
349 static int echo_lock_fits_into(const struct lu_env *env,
350 const struct cl_lock_slice *slice,
351 const struct cl_lock_descr *need,
352 const struct cl_io *unused)
357 static struct cl_lock_operations echo_lock_ops = {
358 .clo_fini = echo_lock_fini,
359 .clo_delete = echo_lock_delete,
360 .clo_fits_into = echo_lock_fits_into
365 /** \defgroup echo_cl_ops echo_cl_ops
367 * operations for cl_object
371 static struct cl_page *echo_page_init(const struct lu_env *env,
372 struct cl_object *obj,
373 struct cl_page *page, cfs_page_t *vmpage)
375 struct echo_page *ep;
378 OBD_SLAB_ALLOC_PTR_GFP(ep, echo_page_kmem, CFS_ALLOC_IO);
380 struct echo_object *eco = cl2echo_obj(obj);
381 ep->ep_vmpage = vmpage;
382 page_cache_get(vmpage);
383 cl_page_slice_add(page, &ep->ep_cl, obj, &echo_page_ops);
384 atomic_inc(&eco->eo_npages);
386 RETURN(ERR_PTR(ep ? 0 : -ENOMEM));
389 static int echo_io_init(const struct lu_env *env, struct cl_object *obj,
395 static int echo_lock_init(const struct lu_env *env,
396 struct cl_object *obj, struct cl_lock *lock,
397 const struct cl_io *unused)
399 struct echo_lock *el;
402 OBD_SLAB_ALLOC_PTR_GFP(el, echo_lock_kmem, CFS_ALLOC_IO);
404 cl_lock_slice_add(lock, &el->el_cl, obj, &echo_lock_ops);
405 el->el_object = cl2echo_obj(obj);
406 CFS_INIT_LIST_HEAD(&el->el_chain);
408 RETURN(el == NULL ? -ENOMEM : 0);
411 static int echo_conf_set(const struct lu_env *env, struct cl_object *obj,
412 const struct cl_object_conf *conf)
417 static const struct cl_object_operations echo_cl_obj_ops = {
418 .coo_page_init = echo_page_init,
419 .coo_lock_init = echo_lock_init,
420 .coo_io_init = echo_io_init,
421 .coo_conf_set = echo_conf_set
423 /** @} echo_cl_ops */
425 /** \defgroup echo_lu_ops echo_lu_ops
427 * operations for echo lu object.
431 static int echo_object_init(const struct lu_env *env, struct lu_object *obj,
432 const struct lu_object_conf *conf)
434 const struct cl_object_conf *cconf = lu2cl_conf(conf);
435 struct echo_object_conf *econf = cl2echo_conf(cconf);
436 struct echo_device *ed = cl2echo_dev(lu2cl_dev(obj->lo_dev));
437 struct echo_client_obd *ec = ed->ed_ec;
438 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
442 struct lu_object *below;
443 struct lu_device *under;
446 below = under->ld_ops->ldo_object_alloc(env, obj->lo_header,
450 lu_object_add(obj, below);
453 LASSERT(econf->eoc_md);
454 eco->eo_lsm = *econf->eoc_md;
456 atomic_set(&eco->eo_npages, 0);
458 /* clear the lsm pointer so that it won't get freed. */
459 *econf->eoc_md = NULL;
461 spin_lock(&ec->ec_lock);
462 list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
463 spin_unlock(&ec->ec_lock);
468 static void echo_object_free(const struct lu_env *env, struct lu_object *obj)
470 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
471 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
472 struct lov_stripe_md *lsm = eco->eo_lsm;
475 LASSERT(atomic_read(&eco->eo_npages) == 0);
477 spin_lock(&ec->ec_lock);
478 list_del_init(&eco->eo_obj_chain);
479 spin_unlock(&ec->ec_lock);
482 lu_object_header_fini(obj->lo_header);
485 obd_free_memmd(ec->ec_exp, &lsm);
486 OBD_SLAB_FREE_PTR(eco, echo_object_kmem);
490 static int echo_object_print(const struct lu_env *env, void *cookie,
491 lu_printer_t p, const struct lu_object *o)
493 struct echo_object *obj = cl2echo_obj(lu2cl(o));
495 return (*p)(env, cookie, "echoclient-object@%p", obj);
499 static const struct lu_object_operations echo_lu_obj_ops = {
500 .loo_object_init = echo_object_init,
501 .loo_object_delete = NULL,
502 .loo_object_release = NULL,
503 .loo_object_free = echo_object_free,
504 .loo_object_print = echo_object_print,
505 .loo_object_invariant = NULL
507 /** @} echo_lu_ops */
509 /** \defgroup echo_lu_dev_ops
511 * Operations for echo lu device.
515 static struct lu_object *echo_object_alloc(const struct lu_env *env,
516 const struct lu_object_header *hdr,
517 struct lu_device *dev)
519 struct echo_object *eco;
520 struct lu_object *obj = NULL;
523 /* we're the top dev. */
524 LASSERT(hdr == NULL);
525 OBD_SLAB_ALLOC_PTR_GFP(eco, echo_object_kmem, CFS_ALLOC_IO);
527 struct cl_object_header *hdr = &eco->eo_hdr;
529 obj = &echo_obj2cl(eco)->co_lu;
530 cl_object_header_init(hdr);
531 lu_object_init(obj, &hdr->coh_lu, dev);
532 lu_object_add_top(&hdr->coh_lu, obj);
534 eco->eo_cl.co_ops = &echo_cl_obj_ops;
535 obj->lo_ops = &echo_lu_obj_ops;
540 static struct lu_device_operations echo_device_lu_ops = {
541 .ldo_object_alloc = echo_object_alloc,
543 /** @} echo_lu_dev_ops */
545 static struct cl_device_operations echo_device_cl_ops = {
548 /** \defgroup echo_init echo_init
550 * Init and fini functions for echo client.
554 static int echo_site_init(const struct lu_env *env, struct echo_device *ed)
556 struct cl_site *site = &ed->ed_site_myself;
559 /* initialize site */
560 rc = cl_site_init(site, &ed->ed_cl);
562 CERROR("Cannot initilize site for echo client(%d)\n", rc);
566 rc = lu_site_init_finish(&site->cs_lu);
574 static void echo_site_fini(const struct lu_env *env, struct echo_device *ed)
577 cl_site_fini(ed->ed_site);
582 static void *echo_thread_key_init(const struct lu_context *ctx,
583 struct lu_context_key *key)
585 struct echo_thread_info *info;
587 OBD_SLAB_ALLOC_PTR_GFP(info, echo_thread_kmem, CFS_ALLOC_IO);
589 info = ERR_PTR(-ENOMEM);
593 static void echo_thread_key_fini(const struct lu_context *ctx,
594 struct lu_context_key *key, void *data)
596 struct echo_thread_info *info = data;
597 OBD_SLAB_FREE_PTR(info, echo_thread_kmem);
600 static void echo_thread_key_exit(const struct lu_context *ctx,
601 struct lu_context_key *key, void *data)
605 static struct lu_context_key echo_thread_key = {
606 .lct_tags = LCT_CL_THREAD,
607 .lct_init = echo_thread_key_init,
608 .lct_fini = echo_thread_key_fini,
609 .lct_exit = echo_thread_key_exit
612 static void *echo_session_key_init(const struct lu_context *ctx,
613 struct lu_context_key *key)
615 struct echo_session_info *session;
617 OBD_SLAB_ALLOC_PTR_GFP(session, echo_session_kmem, CFS_ALLOC_IO);
619 session = ERR_PTR(-ENOMEM);
623 static void echo_session_key_fini(const struct lu_context *ctx,
624 struct lu_context_key *key, void *data)
626 struct echo_session_info *session = data;
627 OBD_SLAB_FREE_PTR(session, echo_session_kmem);
630 static void echo_session_key_exit(const struct lu_context *ctx,
631 struct lu_context_key *key, void *data)
635 static struct lu_context_key echo_session_key = {
636 .lct_tags = LCT_SESSION,
637 .lct_init = echo_session_key_init,
638 .lct_fini = echo_session_key_fini,
639 .lct_exit = echo_session_key_exit
642 LU_TYPE_INIT_FINI(echo, &echo_thread_key, &echo_session_key);
644 static struct lu_device *echo_device_alloc(const struct lu_env *env,
645 struct lu_device_type *t,
646 struct lustre_cfg *cfg)
648 struct lu_device *next;
649 struct echo_device *ed;
650 struct cl_device *cd;
651 struct obd_device *obd = NULL; /* to keep compiler happy */
652 struct obd_device *tgt;
653 const char *tgt_type_name;
660 GOTO(out, rc = -ENOMEM);
664 rc = cl_device_init(cd, t);
668 cd->cd_lu_dev.ld_ops = &echo_device_lu_ops;
669 cd->cd_ops = &echo_device_cl_ops;
672 rc = echo_site_init(env, ed);
677 obd = class_name2obd(lustre_cfg_string(cfg, 0));
678 LASSERT(obd != NULL);
679 rc = echo_client_setup(obd, cfg);
682 ed->ed_ec = &obd->u.echo_client;
685 tgt = class_name2obd(lustre_cfg_string(cfg, 1));
686 LASSERT(tgt != NULL);
687 next = tgt->obd_lu_dev;
688 if (next != NULL && !lu_device_is_cl(next))
692 * if echo client is to be stacked upon ost device, the next is NULL
693 * since ost is not a clio device so far
695 tgt_type_name = tgt->obd_type->typ_name;
697 LASSERT(next != NULL);
698 if (next->ld_site != NULL)
699 GOTO(out, rc = -EBUSY);
701 next->ld_site = &ed->ed_site->cs_lu;
702 rc = next->ld_type->ldt_ops->ldto_device_init(env, next,
703 next->ld_type->ldt_name, NULL);
707 /* Trikcy case, I have to determine the obd type since clio
708 * uses the different parameters to initialize objects for
711 if (strcmp(tgt_type_name, LUSTRE_LOV_NAME) == 0)
712 ed->ed_next_islov = 1;
714 LASSERT(strcmp(tgt_type_name, LUSTRE_OSC_NAME) == 0);
716 LASSERT(strcmp(tgt_type_name, LUSTRE_OST_NAME) == 0);
719 RETURN(&cd->cd_lu_dev);
725 rc2 = echo_client_cleanup(obd);
727 CERROR("Cleanup obd device %s error(%d)\n",
732 echo_site_fini(env, ed);
734 cl_device_fini(&ed->ed_cl);
744 static int echo_device_init(const struct lu_env *env, struct lu_device *d,
745 const char *name, struct lu_device *next)
751 static struct lu_device *echo_device_fini(const struct lu_env *env,
754 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
755 struct lu_device *next = ed->ed_next;
758 next = next->ld_type->ldt_ops->ldto_device_fini(env, next);
762 static struct lu_device *echo_device_free(const struct lu_env *env,
765 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
766 struct echo_client_obd *ec = ed->ed_ec;
767 struct echo_object *eco;
768 struct lu_device *next = ed->ed_next;
770 CDEBUG(D_INFO, "echo device:%p is going to be freed, next = %p\n", ed, next);
773 spin_lock(&ec->ec_lock);
774 while (!list_empty(&ec->ec_locks)) {
775 struct echo_lock *ecl = list_entry(ec->ec_locks.next,
776 struct echo_lock, el_chain);
777 struct cl_lock *lock = echo_lock2cl(ecl);
779 list_del_init(&ecl->el_chain);
780 spin_unlock(&ec->ec_lock);
782 CERROR("echo client: pending lock %p\n", ecl);
786 cl_lock_release(env, lock, "ec enqueue", ecl->el_object);
788 cl_lock_mutex_get(env, lock);
789 cl_lock_cancel(env, lock);
790 cl_lock_delete(env, lock);
791 cl_lock_mutex_put(env, lock);
792 cl_lock_put(env, lock);
794 spin_lock(&ec->ec_lock);
796 spin_unlock(&ec->ec_lock);
798 LASSERT(ed->ed_site);
799 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
801 /* check if there are objects still alive.
802 * It shouldn't have any object because lu_site_purge would cleanup
803 * all of cached objects. Anyway, probably the echo device is being
804 * parallelly accessed.
806 spin_lock(&ec->ec_lock);
807 list_for_each_entry(eco, &ec->ec_objects, eo_obj_chain)
809 spin_unlock(&ec->ec_lock);
812 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
814 CDEBUG(D_INFO, "Waiting for the reference of echo object to be dropped\n");
816 /* Wait for the last reference to be dropped. */
817 spin_lock(&ec->ec_lock);
818 while (!list_empty(&ec->ec_objects)) {
819 spin_unlock(&ec->ec_lock);
820 CERROR("echo_client still has objects at cleanup time, "
821 "wait for 1 second\n");
822 cfs_schedule_timeout(CFS_TASK_UNINT, cfs_time_seconds(1));
823 spin_lock(&ec->ec_lock);
825 spin_unlock(&ec->ec_lock);
827 CDEBUG(D_INFO, "No object exists, exiting...\n");
829 echo_client_cleanup(d->ld_obd);
832 next = next->ld_type->ldt_ops->ldto_device_free(env, next);
834 LASSERT(ed->ed_site == lu2cl_site(d->ld_site));
835 echo_site_fini(env, ed);
836 cl_device_fini(&ed->ed_cl);
842 static const struct lu_device_type_operations echo_device_type_ops = {
843 .ldto_init = echo_type_init,
844 .ldto_fini = echo_type_fini,
846 .ldto_start = echo_type_start,
847 .ldto_stop = echo_type_stop,
849 .ldto_device_alloc = echo_device_alloc,
850 .ldto_device_free = echo_device_free,
851 .ldto_device_init = echo_device_init,
852 .ldto_device_fini = echo_device_fini
855 static struct lu_device_type echo_device_type = {
856 .ldt_tags = LU_DEVICE_CL,
857 .ldt_name = LUSTRE_ECHO_CLIENT_NAME,
858 .ldt_ops = &echo_device_type_ops,
859 .ldt_ctx_tags = LCT_CL_THREAD
863 /** \defgroup echo_exports
865 * exporting functions to echo client
870 /* Interfaces to echo client obd device */
871 static struct echo_object *cl_echo_object_find(struct echo_device *d,
872 struct lov_stripe_md **lsmp)
875 struct echo_thread_info *info;
876 struct echo_object_conf *conf;
877 struct lov_stripe_md *lsm;
878 struct echo_object *eco;
879 struct cl_object *obj;
887 LASSERT(lsm->lsm_object_id);
889 /* Never return an object if the obd is to be freed. */
890 if (echo_dev2cl(d)->cd_lu_dev.ld_obd->obd_stopping)
891 RETURN(ERR_PTR(-ENODEV));
893 env = cl_env_get(&refcheck);
897 info = echo_env_info(env);
898 conf = &info->eti_conf;
900 if (!d->ed_next_islov) {
901 struct lov_oinfo *oinfo = lsm->lsm_oinfo[0];
902 LASSERT(oinfo != NULL);
903 oinfo->loi_id = lsm->lsm_object_id;
904 oinfo->loi_gr = lsm->lsm_object_gr;
905 conf->eoc_cl.u.coc_oinfo = oinfo;
907 struct lustre_md *md;
909 memset(md, 0, sizeof *md);
911 conf->eoc_cl.u.coc_md = md;
916 fid = &info->eti_fid;
919 obj = cl_object_find(env, echo_dev2cl(d), fid, &conf->eoc_cl);
921 GOTO(out, eco = (void*)obj);
923 eco = cl2echo_obj(obj);
924 if (eco->eo_deleted) {
925 cl_object_put(env, obj);
926 eco = ERR_PTR(-EAGAIN);
930 cl_env_put(env, &refcheck);
934 static int cl_echo_object_put(struct echo_object *eco)
937 struct cl_object *obj = echo_obj2cl(eco);
941 env = cl_env_get(&refcheck);
943 RETURN(PTR_ERR(env));
945 /* an external function to kill an object? */
946 if (eco->eo_deleted) {
947 struct lu_object_header *loh = obj->co_lu.lo_header;
948 LASSERT(&eco->eo_hdr == luh2coh(loh));
949 set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
950 cl_object_prune(env, obj);
953 cl_object_put(env, obj);
954 cl_env_put(env, &refcheck);
958 static int cl_echo_enqueue(struct echo_object *eco, obd_off start, obd_off end,
959 int mode, __u64 *cookie)
963 struct echo_thread_info *info;
965 struct cl_lock_descr *descr;
966 struct cl_object *obj = echo_obj2cl(eco);
971 env = cl_env_get(&refcheck);
973 RETURN(PTR_ERR(env));
975 info = echo_env_info(env);
976 descr = &info->eti_descr;
977 descr->cld_obj = obj;
978 descr->cld_start = cl_index(obj, start);
979 descr->cld_end = cl_index(obj, end);
980 descr->cld_mode = mode == LCK_PW ? CLM_WRITE : CLM_READ;
984 result = cl_io_init(env, io, CIT_MISC, obj);
987 LASSERT(result == 0);
990 lck = cl_lock_request(env, io, descr, CEF_ASYNC, "ec enqueue", eco);
992 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
993 struct echo_lock *el;
995 result = cl_wait(env, lck);
997 el = cl2echo_lock(cl_lock_at(lck, &echo_device_type));
998 spin_lock(&ec->ec_lock);
999 list_add(&el->el_chain, &ec->ec_locks);
1000 *cookie = el->el_cookie = ++ec->ec_unique;
1001 spin_unlock(&ec->ec_lock);
1003 cl_lock_release(env, lck, "ec enqueue", cfs_current());
1005 cl_io_fini(env, io);
1009 cl_env_put(env, &refcheck);
1013 static int cl_echo_cancel(struct echo_device *ed, __u64 cookie)
1015 struct echo_client_obd *ec = ed->ed_ec;
1016 struct echo_lock *ecl = NULL;
1017 struct list_head *el;
1025 env = cl_env_get(&refcheck);
1027 RETURN(PTR_ERR(env));
1029 spin_lock (&ec->ec_lock);
1030 list_for_each (el, &ec->ec_locks) {
1031 ecl = list_entry (el, struct echo_lock, el_chain);
1032 CDEBUG(D_INFO, "ecl: %p, cookie: %llx\n", ecl, ecl->el_cookie);
1033 found = (ecl->el_cookie == cookie);
1035 list_del_init(&ecl->el_chain);
1039 spin_unlock (&ec->ec_lock);
1043 struct cl_lock *clk = echo_lock2cl(ecl);
1047 cl_lock_release(env, clk, "ec enqueue", ecl->el_object);
1049 cl_lock_mutex_get(env, clk);
1050 cl_lock_cancel(env, clk);
1051 cl_lock_delete(env, clk);
1052 cl_lock_mutex_put(env, clk);
1053 cl_lock_put(env, clk);
1056 cl_env_put(env, &refcheck);
1060 static int cl_echo_async_brw(const struct lu_env *env, struct cl_io *io,
1061 enum cl_req_type unused, struct cl_2queue *queue)
1063 struct cl_page *clp;
1064 struct cl_page *temp;
1068 cl_page_list_for_each_safe(clp, temp, &queue->c2_qin) {
1070 rc = cl_page_cache_add(env, io, clp, CRT_WRITE);
1073 result = result ?: rc;
1078 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
1079 cfs_page_t **pages, int npages, int async)
1082 struct echo_thread_info *info;
1083 struct cl_object *obj = echo_obj2cl(eco);
1084 struct echo_device *ed = eco->eo_dev;
1085 struct cl_2queue *queue;
1087 struct cl_page *clp;
1089 int page_size = cl_page_size(obj);
1095 LASSERT(ed->ed_next != NULL);
1096 env = cl_env_get(&refcheck);
1098 RETURN(PTR_ERR(env));
1100 info = echo_env_info(env);
1102 queue = &info->eti_queue;
1104 cl_2queue_init(queue);
1105 rc = cl_io_init(env, io, CIT_MISC, obj);
1110 for (i = 0; i < npages; i++) {
1112 clp = cl_page_find(env, obj, cl_index(obj, offset),
1113 pages[i], CPT_TRANSIENT);
1118 LASSERT(clp->cp_type == CPT_TRANSIENT);
1120 rc = cl_page_own(env, io, clp);
1122 LASSERT(clp->cp_state == CPS_FREEING);
1123 cl_page_put(env, clp);
1127 cl_2queue_add(queue, clp);
1129 /* drop the reference count for cl_page_find, so that the page
1130 * will be freed in cl_2queue_fini. */
1131 cl_page_put(env, clp);
1132 offset += page_size;
1136 enum cl_req_type typ = rw == READ ? CRT_READ : CRT_WRITE;
1138 async = async && (typ == CRT_WRITE);
1140 rc = cl_echo_async_brw(env, io, typ, queue);
1142 rc = cl_io_submit_sync(env, io, typ, queue,
1144 CDEBUG(D_INFO, "echo_client %s write returns %d\n",
1145 async ? "async" : "sync", rc);
1148 cl_2queue_discard(env, io, queue);
1149 cl_2queue_disown(env, io, queue);
1150 cl_2queue_fini(env, queue);
1151 cl_io_fini(env, io);
1155 cl_env_put(env, &refcheck);
1158 /** @} echo_exports */
1161 static obd_id last_object_id;
1164 echo_copyout_lsm (struct lov_stripe_md *lsm, void *_ulsm, int ulsm_nob)
1166 struct lov_stripe_md *ulsm = _ulsm;
1169 nob = offsetof (struct lov_stripe_md, lsm_oinfo[lsm->lsm_stripe_count]);
1173 if (copy_to_user (ulsm, lsm, sizeof(ulsm)))
1176 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1177 if (copy_to_user (ulsm->lsm_oinfo[i], lsm->lsm_oinfo[i],
1178 sizeof(lsm->lsm_oinfo[0])))
1185 echo_copyin_lsm (struct echo_device *ed, struct lov_stripe_md *lsm,
1186 void *ulsm, int ulsm_nob)
1188 struct echo_client_obd *ec = ed->ed_ec;
1191 if (ulsm_nob < sizeof (*lsm))
1194 if (copy_from_user (lsm, ulsm, sizeof (*lsm)))
1197 if (lsm->lsm_stripe_count > ec->ec_nstripes ||
1198 lsm->lsm_magic != LOV_MAGIC ||
1199 (lsm->lsm_stripe_size & (~CFS_PAGE_MASK)) != 0 ||
1200 ((__u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count > ~0UL))
1204 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1205 if (copy_from_user(lsm->lsm_oinfo[i],
1206 ((struct lov_stripe_md *)ulsm)->lsm_oinfo[i],
1207 sizeof(lsm->lsm_oinfo[0])))
1213 static int echo_create_object(struct echo_device *ed, int on_target,
1214 struct obdo *oa, void *ulsm, int ulsm_nob,
1215 struct obd_trans_info *oti)
1217 struct echo_object *eco;
1218 struct echo_client_obd *ec = ed->ed_ec;
1219 struct lov_stripe_md *lsm = NULL;
1224 if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
1225 (on_target || /* set_stripe */
1226 ec->ec_nstripes != 0)) { /* LOV */
1227 CERROR ("No valid oid\n");
1231 rc = obd_alloc_memmd(ec->ec_exp, &lsm);
1233 CERROR("Cannot allocate md, rc = %d\n", rc);
1240 rc = echo_copyin_lsm (ed, lsm, ulsm, ulsm_nob);
1244 if (lsm->lsm_stripe_count == 0)
1245 lsm->lsm_stripe_count = ec->ec_nstripes;
1247 if (lsm->lsm_stripe_size == 0)
1248 lsm->lsm_stripe_size = CFS_PAGE_SIZE;
1252 /* setup stripes: indices + default ids if required */
1253 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1254 if (lsm->lsm_oinfo[i]->loi_id == 0)
1255 lsm->lsm_oinfo[i]->loi_id = lsm->lsm_object_id;
1257 lsm->lsm_oinfo[i]->loi_ost_idx =
1258 (idx + i) % ec->ec_nstripes;
1262 /* setup object ID here for !on_target and LOV hint */
1263 if (oa->o_valid & OBD_MD_FLID)
1264 lsm->lsm_object_id = oa->o_id;
1266 if (lsm->lsm_object_id == 0)
1267 lsm->lsm_object_id = ++last_object_id;
1271 /* Only echo objects are allowed to be created */
1272 LASSERT((oa->o_valid & OBD_MD_FLGROUP) &&
1273 (oa->o_gr == FILTER_GROUP_ECHO));
1274 rc = obd_create(ec->ec_exp, oa, &lsm, oti);
1276 CERROR("Cannot create objects, rc = %d\n", rc);
1282 /* See what object ID we were given */
1283 oa->o_id = lsm->lsm_object_id;
1284 oa->o_valid |= OBD_MD_FLID;
1286 eco = cl_echo_object_find(ed, &lsm);
1288 GOTO(failed, rc = PTR_ERR(eco));
1289 cl_echo_object_put(eco);
1291 CDEBUG(D_INFO, "oa->o_id = %lx\n", (long)oa->o_id);
1296 obd_destroy(ec->ec_exp, oa, lsm, oti, NULL, NULL);
1298 obd_free_memmd(ec->ec_exp, &lsm);
1300 CERROR("create object failed with rc = %d\n", rc);
1304 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
1307 struct echo_client_obd *ec = ed->ed_ec;
1308 struct lov_stripe_md *lsm = NULL;
1309 struct echo_object *eco;
1313 if ((oa->o_valid & OBD_MD_FLID) == 0 ||
1314 oa->o_id == 0) /* disallow use of object id 0 */
1316 CERROR ("No valid oid\n");
1320 rc = obd_alloc_memmd(ec->ec_exp, &lsm);
1324 lsm->lsm_object_id = oa->o_id;
1325 if (oa->o_valid & OBD_MD_FLGROUP)
1326 lsm->lsm_object_gr = oa->o_gr;
1328 lsm->lsm_object_gr = FILTER_GROUP_ECHO;
1331 eco = cl_echo_object_find(ed, &lsm);
1337 obd_free_memmd(ec->ec_exp, &lsm);
1341 static void echo_put_object(struct echo_object *eco)
1343 if (cl_echo_object_put(eco))
1344 CERROR("echo client: drop an object failed");
1348 echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
1350 unsigned long stripe_count;
1351 unsigned long stripe_size;
1352 unsigned long width;
1353 unsigned long woffset;
1357 if (lsm->lsm_stripe_count <= 1)
1361 stripe_size = lsm->lsm_stripe_size;
1362 stripe_count = lsm->lsm_stripe_count;
1364 /* width = # bytes in all stripes */
1365 width = stripe_size * stripe_count;
1367 /* woffset = offset within a width; offset = whole number of widths */
1368 woffset = do_div (offset, width);
1370 stripe_index = woffset / stripe_size;
1372 *idp = lsm->lsm_oinfo[stripe_index]->loi_id;
1373 *offp = offset * stripe_size + woffset % stripe_size;
1377 echo_client_page_debug_setup(struct lov_stripe_md *lsm,
1378 cfs_page_t *page, int rw, obd_id id,
1379 obd_off offset, obd_off count)
1386 /* no partial pages on the client */
1387 LASSERT(count == CFS_PAGE_SIZE);
1389 addr = cfs_kmap(page);
1391 for (delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
1392 if (rw == OBD_BRW_WRITE) {
1393 stripe_off = offset + delta;
1395 echo_get_stripe_off_id(lsm, &stripe_off, &stripe_id);
1397 stripe_off = 0xdeadbeef00c0ffeeULL;
1398 stripe_id = 0xdeadbeef00c0ffeeULL;
1400 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
1401 stripe_off, stripe_id);
1407 static int echo_client_page_debug_check(struct lov_stripe_md *lsm,
1408 cfs_page_t *page, obd_id id,
1409 obd_off offset, obd_off count)
1418 /* no partial pages on the client */
1419 LASSERT(count == CFS_PAGE_SIZE);
1421 addr = cfs_kmap(page);
1423 for (rc = delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
1424 stripe_off = offset + delta;
1426 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
1428 rc2 = block_debug_check("test_brw",
1429 addr + delta, OBD_ECHO_BLOCK_SIZE,
1430 stripe_off, stripe_id);
1432 CERROR ("Error in echo object "LPX64"\n", id);
1441 static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
1442 struct echo_object *eco, obd_off offset,
1443 obd_size count, int async,
1444 struct obd_trans_info *oti)
1446 struct echo_client_obd *ec = ed->ed_ec;
1447 struct lov_stripe_md *lsm = eco->eo_lsm;
1449 struct brw_page *pga;
1450 struct brw_page *pgp;
1459 verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
1460 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
1461 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
1463 gfp_mask = ((oa->o_id & 2) == 0) ? CFS_ALLOC_STD : CFS_ALLOC_HIGHUSER;
1465 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
1466 LASSERT(lsm != NULL);
1467 LASSERT(lsm->lsm_object_id == oa->o_id);
1470 (count & (~CFS_PAGE_MASK)) != 0)
1473 /* XXX think again with misaligned I/O */
1474 npages = count >> CFS_PAGE_SHIFT;
1476 OBD_ALLOC(pga, npages * sizeof(*pga));
1480 OBD_ALLOC(pages, npages * sizeof(*pages));
1481 if (pages == NULL) {
1482 OBD_FREE(pga, npages * sizeof(*pga));
1486 for (i = 0, pgp = pga, off = offset;
1488 i++, pgp++, off += CFS_PAGE_SIZE) {
1490 LASSERT (pgp->pg == NULL); /* for cleanup */
1493 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
1494 if (pgp->pg == NULL)
1498 pgp->count = CFS_PAGE_SIZE;
1503 echo_client_page_debug_setup(lsm, pgp->pg, rw,
1504 oa->o_id, off, pgp->count);
1507 if (ed->ed_next == NULL) {
1508 struct obd_info oinfo = { { { 0 } } };
1511 rc = obd_brw(rw, ec->ec_exp, &oinfo, npages, pga, oti);
1513 rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
1516 if (rc != 0 || rw != OBD_BRW_READ)
1519 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
1520 if (pgp->pg == NULL)
1525 vrc = echo_client_page_debug_check(lsm, pgp->pg, oa->o_id,
1526 pgp->off, pgp->count);
1527 if (vrc != 0 && rc == 0)
1530 OBD_PAGE_FREE(pgp->pg);
1532 OBD_FREE(pga, npages * sizeof(*pga));
1533 OBD_FREE(pages, npages * sizeof(*pages));
1537 static int echo_client_prep_commit(struct obd_export *exp, int rw,
1538 struct obdo *oa, struct echo_object *eco,
1539 obd_off offset, obd_size count,
1540 obd_size batch, struct obd_trans_info *oti)
1542 struct lov_stripe_md *lsm = eco->eo_lsm;
1543 struct obd_ioobj ioo;
1544 struct niobuf_local *lnb;
1545 struct niobuf_remote *rnb;
1547 obd_size npages, tot_pages;
1551 if (count <= 0 || (count & (~CFS_PAGE_MASK)) != 0 ||
1552 (lsm != NULL && lsm->lsm_object_id != oa->o_id))
1555 npages = batch >> CFS_PAGE_SHIFT;
1556 tot_pages = count >> CFS_PAGE_SHIFT;
1558 OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
1559 OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
1561 if (lnb == NULL || rnb == NULL)
1562 GOTO(out, ret = -ENOMEM);
1564 obdo_to_ioobj(oa, &ioo);
1568 for(; tot_pages; tot_pages -= npages) {
1571 if (tot_pages < npages)
1574 for (i = 0; i < npages; i++, off += CFS_PAGE_SIZE) {
1575 rnb[i].offset = off;
1576 rnb[i].len = CFS_PAGE_SIZE;
1579 ioo.ioo_bufcnt = npages;
1580 oti->oti_transno = 0;
1583 ret = obd_preprw(rw, exp, oa, 1, &ioo, rnb, &lpages, lnb, oti,
1587 LASSERT(lpages == npages);
1589 for (i = 0; i < lpages; i++) {
1590 cfs_page_t *page = lnb[i].page;
1592 /* read past eof? */
1593 if (page == NULL && lnb[i].rc == 0)
1596 if (oa->o_id == ECHO_PERSISTENT_OBJID ||
1597 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
1598 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
1601 if (rw == OBD_BRW_WRITE)
1602 echo_client_page_debug_setup(lsm, page, rw,
1607 echo_client_page_debug_check(lsm, page,
1613 ret = obd_commitrw(rw, exp, oa, 1,&ioo,rnb,npages,lnb,oti,ret);
1617 /* Reset oti otherwise it would confuse ldiskfs. */
1618 memset(oti, 0, sizeof(*oti));
1623 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
1625 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
1629 static int echo_client_brw_ioctl(int rw, struct obd_export *exp,
1630 struct obd_ioctl_data *data)
1632 struct obd_device *obd = class_exp2obd(exp);
1633 struct echo_device *ed = obd2echo_dev(obd);
1634 struct echo_client_obd *ec = ed->ed_ec;
1635 struct obd_trans_info dummy_oti = { .oti_thread = NULL };
1636 struct obdo *oa = &data->ioc_obdo1;
1637 struct echo_object *eco;
1642 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
1644 rc = echo_get_object(&eco, ed, oa);
1648 oa->o_valid &= ~OBD_MD_FLHANDLE;
1650 switch((long)data->ioc_pbuf1) {
1655 rc = echo_client_kbrw(ed, rw, oa,
1656 eco, data->ioc_offset,
1657 data->ioc_count, async, &dummy_oti);
1660 rc = echo_client_prep_commit(ec->ec_exp, rw, oa,
1661 eco, data->ioc_offset,
1662 data->ioc_count, data->ioc_plen1,
1668 echo_put_object(eco);
1673 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
1674 int mode, obd_off offset, obd_size nob)
1676 struct echo_device *ed = obd2echo_dev(exp->exp_obd);
1677 struct lustre_handle *ulh = &oa->o_handle;
1678 struct echo_object *eco;
1683 if (ed->ed_next == NULL)
1684 RETURN(-EOPNOTSUPP);
1686 if (!(mode == LCK_PR || mode == LCK_PW))
1689 if ((offset & (~CFS_PAGE_MASK)) != 0 ||
1690 (nob & (~CFS_PAGE_MASK)) != 0)
1693 rc = echo_get_object (&eco, ed, oa);
1697 end = (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
1698 rc = cl_echo_enqueue(eco, offset, end, mode, &ulh->cookie);
1700 oa->o_valid |= OBD_MD_FLHANDLE;
1701 CDEBUG(D_INFO, "Cookie is %llx\n", ulh->cookie);
1703 echo_put_object(eco);
1708 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
1710 struct echo_device *ed = obd2echo_dev(exp->exp_obd);
1711 __u64 cookie = oa->o_handle.cookie;
1713 if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
1716 CDEBUG(D_INFO, "Cookie is %llx\n", cookie);
1717 return cl_echo_cancel(ed, cookie);
1721 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
1722 int len, void *karg, void *uarg)
1724 struct obd_device *obd = exp->exp_obd;
1725 struct echo_device *ed = obd2echo_dev(obd);
1726 struct echo_client_obd *ec = ed->ed_ec;
1727 struct echo_object *eco;
1728 struct obd_ioctl_data *data = karg;
1729 struct obd_trans_info dummy_oti;
1730 struct oti_req_ack_lock *ack_lock;
1732 int rw = OBD_BRW_READ;
1739 memset(&dummy_oti, 0, sizeof(dummy_oti));
1741 oa = &data->ioc_obdo1;
1742 if (!(oa->o_valid & OBD_MD_FLGROUP)) {
1743 oa->o_valid |= OBD_MD_FLGROUP;
1744 oa->o_gr = FILTER_GROUP_ECHO;
1746 /* assume we can touch filter native objects with echo device. */
1747 /* LASSERT(oa->o_gr == FILTER_GROUP_ECHO); */
1750 case OBD_IOC_CREATE: /* may create echo object */
1751 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1752 GOTO (out, rc = -EPERM);
1754 rc = echo_create_object (ed, 1, oa,
1755 data->ioc_pbuf1, data->ioc_plen1,
1759 case OBD_IOC_DESTROY:
1760 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1761 GOTO (out, rc = -EPERM);
1763 rc = echo_get_object (&eco, ed, oa);
1765 rc = obd_destroy(ec->ec_exp, oa, eco->eo_lsm,
1766 &dummy_oti, NULL, NULL);
1768 eco->eo_deleted = 1;
1769 echo_put_object(eco);
1773 case OBD_IOC_GETATTR:
1774 rc = echo_get_object (&eco, ed, oa);
1776 struct obd_info oinfo = { { { 0 } } };
1777 oinfo.oi_md = eco->eo_lsm;
1779 rc = obd_getattr(ec->ec_exp, &oinfo);
1780 echo_put_object(eco);
1784 case OBD_IOC_SETATTR:
1785 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1786 GOTO (out, rc = -EPERM);
1788 rc = echo_get_object (&eco, ed, oa);
1790 struct obd_info oinfo = { { { 0 } } };
1792 oinfo.oi_md = eco->eo_lsm;
1794 rc = obd_setattr(ec->ec_exp, &oinfo, NULL);
1795 echo_put_object(eco);
1799 case OBD_IOC_BRW_WRITE:
1800 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1801 GOTO (out, rc = -EPERM);
1805 case OBD_IOC_BRW_READ:
1806 rc = echo_client_brw_ioctl(rw, exp, data);
1809 case ECHO_IOC_GET_STRIPE:
1810 rc = echo_get_object(&eco, ed, oa);
1812 rc = echo_copyout_lsm(eco->eo_lsm, data->ioc_pbuf1,
1814 echo_put_object(eco);
1818 case ECHO_IOC_SET_STRIPE:
1819 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1820 GOTO (out, rc = -EPERM);
1822 if (data->ioc_pbuf1 == NULL) { /* unset */
1823 rc = echo_get_object(&eco, ed, oa);
1825 eco->eo_deleted = 1;
1826 echo_put_object(eco);
1829 rc = echo_create_object(ed, 0, oa,
1831 data->ioc_plen1, &dummy_oti);
1835 case ECHO_IOC_ENQUEUE:
1836 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1837 GOTO (out, rc = -EPERM);
1839 rc = echo_client_enqueue(exp, oa,
1840 data->ioc_conn1, /* lock mode */
1842 data->ioc_count);/*extent*/
1845 case ECHO_IOC_CANCEL:
1846 rc = echo_client_cancel(exp, oa);
1850 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
1851 GOTO (out, rc = -ENOTTY);
1857 /* XXX this should be in a helper also called by target_send_reply */
1858 for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
1860 if (!ack_lock->mode)
1862 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
1870 static int echo_client_setup(struct obd_device *obddev, struct lustre_cfg *lcfg)
1872 struct echo_client_obd *ec = &obddev->u.echo_client;
1873 struct obd_device *tgt;
1874 struct obd_uuid echo_uuid = { "ECHO_UUID" };
1875 struct obd_connect_data *ocd = NULL;
1879 if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1880 CERROR("requires a TARGET OBD name\n");
1884 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
1885 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
1886 CERROR("device not attached or not set up (%s)\n",
1887 lustre_cfg_string(lcfg, 1));
1891 spin_lock_init (&ec->ec_lock);
1892 CFS_INIT_LIST_HEAD (&ec->ec_objects);
1893 CFS_INIT_LIST_HEAD (&ec->ec_locks);
1895 ec->ec_nstripes = 0;
1897 OBD_ALLOC(ocd, sizeof(*ocd));
1899 CERROR("Can't alloc ocd connecting to %s\n",
1900 lustre_cfg_string(lcfg, 1));
1904 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
1906 ocd->ocd_version = LUSTRE_VERSION_CODE;
1907 ocd->ocd_group = FILTER_GROUP_ECHO;
1909 rc = obd_connect(NULL, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
1911 /* Turn off pinger because it connects to tgt obd directly. */
1912 spin_lock(&tgt->obd_dev_lock);
1913 list_del_init(&ec->ec_exp->exp_obd_chain_timed);
1914 spin_unlock(&tgt->obd_dev_lock);
1917 OBD_FREE(ocd, sizeof(*ocd));
1920 CERROR("fail to connect to device %s\n",
1921 lustre_cfg_string(lcfg, 1));
1928 static int echo_client_cleanup(struct obd_device *obddev)
1930 struct echo_client_obd *ec = &obddev->u.echo_client;
1934 if (!list_empty(&obddev->obd_exports)) {
1935 CERROR("still has clients!\n");
1939 LASSERT(atomic_read(&ec->ec_exp->exp_refcount) > 0);
1940 rc = obd_disconnect(ec->ec_exp);
1942 CERROR("fail to disconnect device: %d\n", rc);
1947 static int echo_client_connect(const struct lu_env *env,
1948 struct obd_export **exp,
1949 struct obd_device *src, struct obd_uuid *cluuid,
1950 struct obd_connect_data *data, void *localdata)
1953 struct lustre_handle conn = { 0 };
1956 rc = class_connect(&conn, src, cluuid);
1958 *exp = class_conn2export(&conn);
1964 static int echo_client_disconnect(struct obd_export *exp)
1967 struct obd_device *obd;
1968 struct echo_client_obd *ec;
1969 struct ec_lock *ecl;
1975 GOTO(out, rc = -EINVAL);
1979 ec = &obd->u.echo_client;
1981 /* no more contention on export's lock list */
1982 while (!list_empty (&exp->exp_ec_data.eced_locks)) {
1983 ecl = list_entry (exp->exp_ec_data.eced_locks.next,
1984 struct ec_lock, ecl_exp_chain);
1985 list_del (&ecl->ecl_exp_chain);
1987 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm,
1988 ecl->ecl_mode, &ecl->ecl_lock_handle);
1990 CDEBUG (D_INFO, "Cancel lock on object "LPX64" on disconnect "
1991 "(%d)\n", ecl->ecl_object->eco_id, rc);
1993 echo_put_object (ecl->ecl_object);
1994 OBD_FREE (ecl, sizeof (*ecl));
1998 rc = class_disconnect(exp);
2004 static struct obd_ops echo_obd_ops = {
2005 .o_owner = THIS_MODULE,
2008 .o_setup = echo_client_setup,
2009 .o_cleanup = echo_client_cleanup,
2012 .o_iocontrol = echo_client_iocontrol,
2013 .o_connect = echo_client_connect,
2014 .o_disconnect = echo_client_disconnect
2017 int echo_client_init(void)
2019 struct lprocfs_static_vars lvars = { 0 };
2022 lprocfs_echo_init_vars(&lvars);
2023 rc = class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
2024 LUSTRE_ECHO_CLIENT_NAME, &echo_device_type);
2026 lu_kmem_init(echo_caches);
2030 void echo_client_exit(void)
2032 class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
2033 lu_kmem_fini(echo_caches);