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 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;
85 cfs_page_t *ep_vmpage;
89 struct cl_lock_slice el_cl;
91 struct echo_object *el_object;
93 cfs_atomic_t el_refcount;
97 struct cl_io_slice ei_cl;
102 struct cl_req_slice er_cl;
106 static int echo_client_setup(struct obd_device *obddev,
107 struct lustre_cfg *lcfg);
108 static int echo_client_cleanup(struct obd_device *obddev);
111 /** \defgroup echo_helpers Helper functions
114 static inline struct echo_device *cl2echo_dev(const struct cl_device *dev)
116 return container_of0(dev, struct echo_device, ed_cl);
119 static inline struct cl_device *echo_dev2cl(struct echo_device *d)
124 static inline struct echo_device *obd2echo_dev(const struct obd_device *obd)
126 return cl2echo_dev(lu2cl_dev(obd->obd_lu_dev));
129 static inline struct cl_object *echo_obj2cl(struct echo_object *eco)
134 static inline struct echo_object *cl2echo_obj(const struct cl_object *o)
136 return container_of(o, struct echo_object, eo_cl);
139 static inline struct echo_page *cl2echo_page(const struct cl_page_slice *s)
141 return container_of(s, struct echo_page, ep_cl);
144 static inline struct echo_lock *cl2echo_lock(const struct cl_lock_slice *s)
146 return container_of(s, struct echo_lock, el_cl);
149 static inline struct cl_lock *echo_lock2cl(const struct echo_lock *ecl)
151 return ecl->el_cl.cls_lock;
154 static struct lu_context_key echo_thread_key;
155 static inline struct echo_thread_info *echo_env_info(const struct lu_env *env)
157 struct echo_thread_info *info;
158 info = lu_context_key_get(&env->le_ctx, &echo_thread_key);
159 LASSERT(info != NULL);
164 struct echo_object_conf *cl2echo_conf(const struct cl_object_conf *c)
166 return container_of(c, struct echo_object_conf, eoc_cl);
169 static inline void lsm2fid(struct lov_stripe_md *lsm, struct lu_fid *fid)
172 fid->f_seq = lsm->lsm_object_gr << 16 | lsm->lsm_object_id >> 32;
173 fid->f_oid = lsm->lsm_object_id;
175 /** @} echo_helpers */
177 static struct echo_object *cl_echo_object_find(struct echo_device *d,
178 struct lov_stripe_md **lsm);
179 static int cl_echo_object_put(struct echo_object *eco);
180 static int cl_echo_enqueue (struct echo_object *eco, obd_off start,
181 obd_off end, int mode, __u64 *cookie);
182 static int cl_echo_cancel (struct echo_device *d, __u64 cookie);
183 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
184 cfs_page_t **pages, int npages, int async);
186 static struct echo_thread_info *echo_env_info(const struct lu_env *env);
188 struct echo_thread_info {
189 struct echo_object_conf eti_conf;
190 struct lustre_md eti_md;
192 struct cl_2queue eti_queue;
194 struct cl_lock_descr eti_descr;
195 struct lu_fid eti_fid;
198 /* No session used right now */
199 struct echo_session_info {
203 static cfs_mem_cache_t *echo_page_kmem;
204 static cfs_mem_cache_t *echo_lock_kmem;
205 static cfs_mem_cache_t *echo_object_kmem;
206 static cfs_mem_cache_t *echo_thread_kmem;
207 static cfs_mem_cache_t *echo_session_kmem;
208 //static cfs_mem_cache_t *echo_req_kmem;
210 static struct lu_kmem_descr echo_caches[] = {
212 .ckd_cache = &echo_page_kmem,
213 .ckd_name = "echo_page_kmem",
214 .ckd_size = sizeof (struct echo_page)
217 .ckd_cache = &echo_lock_kmem,
218 .ckd_name = "echo_lock_kmem",
219 .ckd_size = sizeof (struct echo_lock)
222 .ckd_cache = &echo_object_kmem,
223 .ckd_name = "echo_object_kmem",
224 .ckd_size = sizeof (struct echo_object)
227 .ckd_cache = &echo_thread_kmem,
228 .ckd_name = "echo_thread_kmem",
229 .ckd_size = sizeof (struct echo_thread_info)
232 .ckd_cache = &echo_session_kmem,
233 .ckd_name = "echo_session_kmem",
234 .ckd_size = sizeof (struct echo_session_info)
238 .ckd_cache = &echo_req_kmem,
239 .ckd_name = "echo_req_kmem",
240 .ckd_size = sizeof (struct echo_req)
248 /** \defgroup echo_page Page operations
250 * Echo page operations.
254 cfs_page_t *echo_page_vmpage(const struct lu_env *env,
255 const struct cl_page_slice *slice)
257 return cl2echo_page(slice)->ep_vmpage;
260 static void echo_page_discard(const struct lu_env *env,
261 const struct cl_page_slice *slice,
262 struct cl_io *unused)
264 cl_page_delete(env, slice->cpl_page);
267 static int echo_page_is_vmlocked(const struct lu_env *env,
268 const struct cl_page_slice *slice)
273 static void echo_page_completion(const struct lu_env *env,
274 const struct cl_page_slice *slice,
277 LASSERT(slice->cpl_page->cp_sync_io != NULL);
280 static void echo_page_fini(const struct lu_env *env,
281 struct cl_page_slice *slice)
283 struct echo_page *ep = cl2echo_page(slice);
284 struct echo_object *eco = cl2echo_obj(slice->cpl_obj);
285 cfs_page_t *vmpage = ep->ep_vmpage;
288 cfs_atomic_dec(&eco->eo_npages);
289 page_cache_release(vmpage);
290 OBD_SLAB_FREE_PTR(ep, echo_page_kmem);
294 static int echo_page_prep(const struct lu_env *env,
295 const struct cl_page_slice *slice,
296 struct cl_io *unused)
301 static int echo_page_print(const struct lu_env *env,
302 const struct cl_page_slice *slice,
303 void *cookie, lu_printer_t printer)
305 struct echo_page *ep = cl2echo_page(slice);
307 (*printer)(env, cookie, LUSTRE_ECHO_CLIENT_NAME"-page@%p vm@%p\n",
312 static const struct cl_page_operations echo_page_ops = {
313 .cpo_discard = echo_page_discard,
314 .cpo_vmpage = echo_page_vmpage,
315 .cpo_fini = echo_page_fini,
316 .cpo_print = echo_page_print,
317 .cpo_is_vmlocked = echo_page_is_vmlocked,
320 .cpo_prep = echo_page_prep,
321 .cpo_completion = echo_page_completion,
324 .cpo_prep = echo_page_prep,
325 .cpo_completion = echo_page_completion,
331 /** \defgroup echo_lock Locking
333 * echo lock operations
337 static void echo_lock_fini(const struct lu_env *env,
338 struct cl_lock_slice *slice)
340 struct echo_lock *ecl = cl2echo_lock(slice);
342 LASSERT(cfs_list_empty(&ecl->el_chain));
343 OBD_SLAB_FREE_PTR(ecl, echo_lock_kmem);
346 static void echo_lock_delete(const struct lu_env *env,
347 const struct cl_lock_slice *slice)
349 struct echo_lock *ecl = cl2echo_lock(slice);
351 LASSERT(cfs_list_empty(&ecl->el_chain));
354 static int echo_lock_fits_into(const struct lu_env *env,
355 const struct cl_lock_slice *slice,
356 const struct cl_lock_descr *need,
357 const struct cl_io *unused)
362 static struct cl_lock_operations echo_lock_ops = {
363 .clo_fini = echo_lock_fini,
364 .clo_delete = echo_lock_delete,
365 .clo_fits_into = echo_lock_fits_into
370 /** \defgroup echo_cl_ops cl_object operations
372 * operations for cl_object
376 static struct cl_page *echo_page_init(const struct lu_env *env,
377 struct cl_object *obj,
378 struct cl_page *page, cfs_page_t *vmpage)
380 struct echo_page *ep;
383 OBD_SLAB_ALLOC_PTR_GFP(ep, echo_page_kmem, CFS_ALLOC_IO);
385 struct echo_object *eco = cl2echo_obj(obj);
386 ep->ep_vmpage = vmpage;
387 page_cache_get(vmpage);
388 cl_page_slice_add(page, &ep->ep_cl, obj, &echo_page_ops);
389 cfs_atomic_inc(&eco->eo_npages);
391 RETURN(ERR_PTR(ep ? 0 : -ENOMEM));
394 static int echo_io_init(const struct lu_env *env, struct cl_object *obj,
400 static int echo_lock_init(const struct lu_env *env,
401 struct cl_object *obj, struct cl_lock *lock,
402 const struct cl_io *unused)
404 struct echo_lock *el;
407 OBD_SLAB_ALLOC_PTR_GFP(el, echo_lock_kmem, CFS_ALLOC_IO);
409 cl_lock_slice_add(lock, &el->el_cl, obj, &echo_lock_ops);
410 el->el_object = cl2echo_obj(obj);
411 CFS_INIT_LIST_HEAD(&el->el_chain);
412 cfs_atomic_set(&el->el_refcount, 0);
414 RETURN(el == NULL ? -ENOMEM : 0);
417 static int echo_conf_set(const struct lu_env *env, struct cl_object *obj,
418 const struct cl_object_conf *conf)
423 static const struct cl_object_operations echo_cl_obj_ops = {
424 .coo_page_init = echo_page_init,
425 .coo_lock_init = echo_lock_init,
426 .coo_io_init = echo_io_init,
427 .coo_conf_set = echo_conf_set
429 /** @} echo_cl_ops */
431 /** \defgroup echo_lu_ops lu_object operations
433 * operations for echo lu object.
437 static int echo_object_init(const struct lu_env *env, struct lu_object *obj,
438 const struct lu_object_conf *conf)
440 const struct cl_object_conf *cconf = lu2cl_conf(conf);
441 struct echo_object_conf *econf = cl2echo_conf(cconf);
442 struct echo_device *ed = cl2echo_dev(lu2cl_dev(obj->lo_dev));
443 struct echo_client_obd *ec = ed->ed_ec;
444 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
448 struct lu_object *below;
449 struct lu_device *under;
452 below = under->ld_ops->ldo_object_alloc(env, obj->lo_header,
456 lu_object_add(obj, below);
459 LASSERT(econf->eoc_md);
460 eco->eo_lsm = *econf->eoc_md;
462 cfs_atomic_set(&eco->eo_npages, 0);
464 /* clear the lsm pointer so that it won't get freed. */
465 *econf->eoc_md = NULL;
467 cfs_spin_lock(&ec->ec_lock);
468 cfs_list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
469 cfs_spin_unlock(&ec->ec_lock);
474 static void echo_object_free(const struct lu_env *env, struct lu_object *obj)
476 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
477 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
478 struct lov_stripe_md *lsm = eco->eo_lsm;
481 LASSERT(cfs_atomic_read(&eco->eo_npages) == 0);
483 cfs_spin_lock(&ec->ec_lock);
484 cfs_list_del_init(&eco->eo_obj_chain);
485 cfs_spin_unlock(&ec->ec_lock);
488 lu_object_header_fini(obj->lo_header);
491 obd_free_memmd(ec->ec_exp, &lsm);
492 OBD_SLAB_FREE_PTR(eco, echo_object_kmem);
496 static int echo_object_print(const struct lu_env *env, void *cookie,
497 lu_printer_t p, const struct lu_object *o)
499 struct echo_object *obj = cl2echo_obj(lu2cl(o));
501 return (*p)(env, cookie, "echoclient-object@%p", obj);
505 static const struct lu_object_operations echo_lu_obj_ops = {
506 .loo_object_init = echo_object_init,
507 .loo_object_delete = NULL,
508 .loo_object_release = NULL,
509 .loo_object_free = echo_object_free,
510 .loo_object_print = echo_object_print,
511 .loo_object_invariant = NULL
513 /** @} echo_lu_ops */
515 /** \defgroup echo_lu_dev_ops lu_device operations
517 * Operations for echo lu device.
521 static struct lu_object *echo_object_alloc(const struct lu_env *env,
522 const struct lu_object_header *hdr,
523 struct lu_device *dev)
525 struct echo_object *eco;
526 struct lu_object *obj = NULL;
529 /* we're the top dev. */
530 LASSERT(hdr == NULL);
531 OBD_SLAB_ALLOC_PTR_GFP(eco, echo_object_kmem, CFS_ALLOC_IO);
533 struct cl_object_header *hdr = &eco->eo_hdr;
535 obj = &echo_obj2cl(eco)->co_lu;
536 cl_object_header_init(hdr);
537 lu_object_init(obj, &hdr->coh_lu, dev);
538 lu_object_add_top(&hdr->coh_lu, obj);
540 eco->eo_cl.co_ops = &echo_cl_obj_ops;
541 obj->lo_ops = &echo_lu_obj_ops;
546 static struct lu_device_operations echo_device_lu_ops = {
547 .ldo_object_alloc = echo_object_alloc,
549 /** @} echo_lu_dev_ops */
551 static struct cl_device_operations echo_device_cl_ops = {
554 /** \defgroup echo_init Setup and teardown
556 * Init and fini functions for echo client.
560 static int echo_site_init(const struct lu_env *env, struct echo_device *ed)
562 struct cl_site *site = &ed->ed_site_myself;
565 /* initialize site */
566 rc = cl_site_init(site, &ed->ed_cl);
568 CERROR("Cannot initilize site for echo client(%d)\n", rc);
572 rc = lu_site_init_finish(&site->cs_lu);
580 static void echo_site_fini(const struct lu_env *env, struct echo_device *ed)
583 cl_site_fini(ed->ed_site);
588 static void *echo_thread_key_init(const struct lu_context *ctx,
589 struct lu_context_key *key)
591 struct echo_thread_info *info;
593 OBD_SLAB_ALLOC_PTR_GFP(info, echo_thread_kmem, CFS_ALLOC_IO);
595 info = ERR_PTR(-ENOMEM);
599 static void echo_thread_key_fini(const struct lu_context *ctx,
600 struct lu_context_key *key, void *data)
602 struct echo_thread_info *info = data;
603 OBD_SLAB_FREE_PTR(info, echo_thread_kmem);
606 static void echo_thread_key_exit(const struct lu_context *ctx,
607 struct lu_context_key *key, void *data)
611 static struct lu_context_key echo_thread_key = {
612 .lct_tags = LCT_CL_THREAD,
613 .lct_init = echo_thread_key_init,
614 .lct_fini = echo_thread_key_fini,
615 .lct_exit = echo_thread_key_exit
618 static void *echo_session_key_init(const struct lu_context *ctx,
619 struct lu_context_key *key)
621 struct echo_session_info *session;
623 OBD_SLAB_ALLOC_PTR_GFP(session, echo_session_kmem, CFS_ALLOC_IO);
625 session = ERR_PTR(-ENOMEM);
629 static void echo_session_key_fini(const struct lu_context *ctx,
630 struct lu_context_key *key, void *data)
632 struct echo_session_info *session = data;
633 OBD_SLAB_FREE_PTR(session, echo_session_kmem);
636 static void echo_session_key_exit(const struct lu_context *ctx,
637 struct lu_context_key *key, void *data)
641 static struct lu_context_key echo_session_key = {
642 .lct_tags = LCT_SESSION,
643 .lct_init = echo_session_key_init,
644 .lct_fini = echo_session_key_fini,
645 .lct_exit = echo_session_key_exit
648 LU_TYPE_INIT_FINI(echo, &echo_thread_key, &echo_session_key);
650 static struct lu_device *echo_device_alloc(const struct lu_env *env,
651 struct lu_device_type *t,
652 struct lustre_cfg *cfg)
654 struct lu_device *next;
655 struct echo_device *ed;
656 struct cl_device *cd;
657 struct obd_device *obd = NULL; /* to keep compiler happy */
658 struct obd_device *tgt;
659 const char *tgt_type_name;
666 GOTO(out, rc = -ENOMEM);
670 rc = cl_device_init(cd, t);
674 cd->cd_lu_dev.ld_ops = &echo_device_lu_ops;
675 cd->cd_ops = &echo_device_cl_ops;
678 rc = echo_site_init(env, ed);
683 obd = class_name2obd(lustre_cfg_string(cfg, 0));
684 LASSERT(obd != NULL);
685 rc = echo_client_setup(obd, cfg);
688 ed->ed_ec = &obd->u.echo_client;
691 tgt = class_name2obd(lustre_cfg_string(cfg, 1));
692 LASSERT(tgt != NULL);
693 next = tgt->obd_lu_dev;
694 if (next != NULL && !lu_device_is_cl(next))
698 * if echo client is to be stacked upon ost device, the next is NULL
699 * since ost is not a clio device so far
701 tgt_type_name = tgt->obd_type->typ_name;
703 LASSERT(next != NULL);
704 if (next->ld_site != NULL)
705 GOTO(out, rc = -EBUSY);
707 next->ld_site = &ed->ed_site->cs_lu;
708 rc = next->ld_type->ldt_ops->ldto_device_init(env, next,
709 next->ld_type->ldt_name, NULL);
713 /* Trikcy case, I have to determine the obd type since clio
714 * uses the different parameters to initialize objects for
717 if (strcmp(tgt_type_name, LUSTRE_LOV_NAME) == 0)
718 ed->ed_next_islov = 1;
720 LASSERT(strcmp(tgt_type_name, LUSTRE_OSC_NAME) == 0);
722 LASSERT(strcmp(tgt_type_name, LUSTRE_OST_NAME) == 0);
725 RETURN(&cd->cd_lu_dev);
731 rc2 = echo_client_cleanup(obd);
733 CERROR("Cleanup obd device %s error(%d)\n",
738 echo_site_fini(env, ed);
740 cl_device_fini(&ed->ed_cl);
750 static int echo_device_init(const struct lu_env *env, struct lu_device *d,
751 const char *name, struct lu_device *next)
757 static struct lu_device *echo_device_fini(const struct lu_env *env,
760 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
761 struct lu_device *next = ed->ed_next;
764 next = next->ld_type->ldt_ops->ldto_device_fini(env, next);
768 static void echo_lock_release(const struct lu_env *env,
769 struct echo_lock *ecl,
772 struct cl_lock *clk = echo_lock2cl(ecl);
776 cl_lock_release(env, clk, "ec enqueue", ecl->el_object);
778 cl_lock_mutex_get(env, clk);
779 cl_lock_cancel(env, clk);
780 cl_lock_delete(env, clk);
781 cl_lock_mutex_put(env, clk);
783 cl_lock_put(env, clk);
786 static struct lu_device *echo_device_free(const struct lu_env *env,
789 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
790 struct echo_client_obd *ec = ed->ed_ec;
791 struct echo_object *eco;
792 struct lu_device *next = ed->ed_next;
794 CDEBUG(D_INFO, "echo device:%p is going to be freed, next = %p\n", ed, next);
797 cfs_spin_lock(&ec->ec_lock);
798 while (!cfs_list_empty(&ec->ec_locks)) {
799 struct echo_lock *ecl = cfs_list_entry(ec->ec_locks.next,
804 if (cfs_atomic_dec_and_test(&ecl->el_refcount))
805 cfs_list_del_init(&ecl->el_chain);
808 cfs_spin_unlock(&ec->ec_lock);
810 CERROR("echo client: pending lock %p refs %d\n",
811 ecl, cfs_atomic_read(&ecl->el_refcount));
813 echo_lock_release(env, ecl, still_used);
814 cfs_spin_lock(&ec->ec_lock);
816 cfs_spin_unlock(&ec->ec_lock);
818 LASSERT(ed->ed_site);
819 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
821 /* check if there are objects still alive.
822 * It shouldn't have any object because lu_site_purge would cleanup
823 * all of cached objects. Anyway, probably the echo device is being
824 * parallelly accessed.
826 cfs_spin_lock(&ec->ec_lock);
827 cfs_list_for_each_entry(eco, &ec->ec_objects, eo_obj_chain)
829 cfs_spin_unlock(&ec->ec_lock);
832 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
835 "Waiting for the reference of echo object to be dropped\n");
837 /* Wait for the last reference to be dropped. */
838 cfs_spin_lock(&ec->ec_lock);
839 while (!cfs_list_empty(&ec->ec_objects)) {
840 cfs_spin_unlock(&ec->ec_lock);
841 CERROR("echo_client still has objects at cleanup time, "
842 "wait for 1 second\n");
843 cfs_schedule_timeout_and_set_state(CFS_TASK_UNINT,
844 cfs_time_seconds(1));
845 cfs_spin_lock(&ec->ec_lock);
847 cfs_spin_unlock(&ec->ec_lock);
849 CDEBUG(D_INFO, "No object exists, exiting...\n");
851 echo_client_cleanup(d->ld_obd);
854 next = next->ld_type->ldt_ops->ldto_device_free(env, next);
856 LASSERT(ed->ed_site == lu2cl_site(d->ld_site));
857 echo_site_fini(env, ed);
858 cl_device_fini(&ed->ed_cl);
864 static const struct lu_device_type_operations echo_device_type_ops = {
865 .ldto_init = echo_type_init,
866 .ldto_fini = echo_type_fini,
868 .ldto_start = echo_type_start,
869 .ldto_stop = echo_type_stop,
871 .ldto_device_alloc = echo_device_alloc,
872 .ldto_device_free = echo_device_free,
873 .ldto_device_init = echo_device_init,
874 .ldto_device_fini = echo_device_fini
877 static struct lu_device_type echo_device_type = {
878 .ldt_tags = LU_DEVICE_CL,
879 .ldt_name = LUSTRE_ECHO_CLIENT_NAME,
880 .ldt_ops = &echo_device_type_ops,
881 .ldt_ctx_tags = LCT_CL_THREAD
885 /** \defgroup echo_exports Exported operations
887 * exporting functions to echo client
892 /* Interfaces to echo client obd device */
893 static struct echo_object *cl_echo_object_find(struct echo_device *d,
894 struct lov_stripe_md **lsmp)
897 struct echo_thread_info *info;
898 struct echo_object_conf *conf;
899 struct lov_stripe_md *lsm;
900 struct echo_object *eco;
901 struct cl_object *obj;
909 LASSERT(lsm->lsm_object_id);
911 /* Never return an object if the obd is to be freed. */
912 if (echo_dev2cl(d)->cd_lu_dev.ld_obd->obd_stopping)
913 RETURN(ERR_PTR(-ENODEV));
915 env = cl_env_get(&refcheck);
919 info = echo_env_info(env);
920 conf = &info->eti_conf;
922 if (!d->ed_next_islov) {
923 struct lov_oinfo *oinfo = lsm->lsm_oinfo[0];
924 LASSERT(oinfo != NULL);
925 oinfo->loi_id = lsm->lsm_object_id;
926 oinfo->loi_gr = lsm->lsm_object_gr;
927 conf->eoc_cl.u.coc_oinfo = oinfo;
929 struct lustre_md *md;
931 memset(md, 0, sizeof *md);
933 conf->eoc_cl.u.coc_md = md;
938 fid = &info->eti_fid;
941 obj = cl_object_find(env, echo_dev2cl(d), fid, &conf->eoc_cl);
943 GOTO(out, eco = (void*)obj);
945 eco = cl2echo_obj(obj);
946 if (eco->eo_deleted) {
947 cl_object_put(env, obj);
948 eco = ERR_PTR(-EAGAIN);
952 cl_env_put(env, &refcheck);
956 static int cl_echo_object_put(struct echo_object *eco)
959 struct cl_object *obj = echo_obj2cl(eco);
963 env = cl_env_get(&refcheck);
965 RETURN(PTR_ERR(env));
967 /* an external function to kill an object? */
968 if (eco->eo_deleted) {
969 struct lu_object_header *loh = obj->co_lu.lo_header;
970 LASSERT(&eco->eo_hdr == luh2coh(loh));
971 cfs_set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
972 cl_object_prune(env, obj);
975 cl_object_put(env, obj);
976 cl_env_put(env, &refcheck);
980 static int cl_echo_enqueue0(struct lu_env *env, struct echo_object *eco,
981 obd_off start, obd_off end, int mode,
982 __u64 *cookie , __u32 enqflags)
986 struct cl_object *obj;
987 struct cl_lock_descr *descr;
988 struct echo_thread_info *info;
992 info = echo_env_info(env);
994 descr = &info->eti_descr;
995 obj = echo_obj2cl(eco);
997 descr->cld_obj = obj;
998 descr->cld_start = cl_index(obj, start);
999 descr->cld_end = cl_index(obj, end);
1000 descr->cld_mode = mode == LCK_PW ? CLM_WRITE : CLM_READ;
1001 descr->cld_enq_flags = enqflags;
1004 lck = cl_lock_request(env, io, descr, "ec enqueue", eco);
1006 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
1007 struct echo_lock *el;
1009 rc = cl_wait(env, lck);
1011 el = cl2echo_lock(cl_lock_at(lck, &echo_device_type));
1012 cfs_spin_lock(&ec->ec_lock);
1013 if (cfs_list_empty(&el->el_chain)) {
1014 cfs_list_add(&el->el_chain, &ec->ec_locks);
1015 el->el_cookie = ++ec->ec_unique;
1017 cfs_atomic_inc(&el->el_refcount);
1018 *cookie = el->el_cookie;
1019 cfs_spin_unlock(&ec->ec_lock);
1021 cl_lock_release(env, lck, "ec enqueue", cfs_current());
1026 static int cl_echo_enqueue(struct echo_object *eco, obd_off start, obd_off end,
1027 int mode, __u64 *cookie)
1029 struct echo_thread_info *info;
1036 env = cl_env_get(&refcheck);
1038 RETURN(PTR_ERR(env));
1040 info = echo_env_info(env);
1043 result = cl_io_init(env, io, CIT_MISC, echo_obj2cl(eco));
1046 LASSERT(result == 0);
1048 result = cl_echo_enqueue0(env, eco, start, end, mode, cookie, 0);
1049 cl_io_fini(env, io);
1053 cl_env_put(env, &refcheck);
1057 static int cl_echo_cancel0(struct lu_env *env, struct echo_device *ed,
1060 struct echo_client_obd *ec = ed->ed_ec;
1061 struct echo_lock *ecl = NULL;
1063 int found = 0, still_used = 0;
1066 LASSERT(ec != NULL);
1067 cfs_spin_lock (&ec->ec_lock);
1068 cfs_list_for_each (el, &ec->ec_locks) {
1069 ecl = cfs_list_entry (el, struct echo_lock, el_chain);
1070 CDEBUG(D_INFO, "ecl: %p, cookie: "LPX64"\n", ecl, ecl->el_cookie);
1071 found = (ecl->el_cookie == cookie);
1073 if (cfs_atomic_dec_and_test(&ecl->el_refcount))
1074 cfs_list_del_init(&ecl->el_chain);
1080 cfs_spin_unlock (&ec->ec_lock);
1085 echo_lock_release(env, ecl, still_used);
1089 static int cl_echo_cancel(struct echo_device *ed, __u64 cookie)
1096 env = cl_env_get(&refcheck);
1098 RETURN(PTR_ERR(env));
1100 rc = cl_echo_cancel0(env, ed, cookie);
1102 cl_env_put(env, &refcheck);
1106 static int cl_echo_async_brw(const struct lu_env *env, struct cl_io *io,
1107 enum cl_req_type unused, struct cl_2queue *queue)
1109 struct cl_page *clp;
1110 struct cl_page *temp;
1114 cl_page_list_for_each_safe(clp, temp, &queue->c2_qin) {
1116 rc = cl_page_cache_add(env, io, clp, CRT_WRITE);
1119 result = result ?: rc;
1124 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
1125 cfs_page_t **pages, int npages, int async)
1128 struct echo_thread_info *info;
1129 struct cl_object *obj = echo_obj2cl(eco);
1130 struct echo_device *ed = eco->eo_dev;
1131 struct cl_2queue *queue;
1133 struct cl_page *clp;
1134 struct lustre_handle lh = { 0 };
1135 int page_size = cl_page_size(obj);
1141 LASSERT((offset & ~CFS_PAGE_MASK) == 0);
1142 LASSERT(ed->ed_next != NULL);
1143 env = cl_env_get(&refcheck);
1145 RETURN(PTR_ERR(env));
1147 info = echo_env_info(env);
1149 queue = &info->eti_queue;
1151 cl_2queue_init(queue);
1152 rc = cl_io_init(env, io, CIT_MISC, obj);
1158 rc = cl_echo_enqueue0(env, eco, offset,
1159 offset + npages * CFS_PAGE_SIZE - 1,
1160 rw == READ ? LCK_PW : LCK_PW, &lh.cookie,
1163 GOTO(error_lock, rc);
1165 for (i = 0; i < npages; i++) {
1167 clp = cl_page_find(env, obj, cl_index(obj, offset),
1168 pages[i], CPT_TRANSIENT);
1173 LASSERT(clp->cp_type == CPT_TRANSIENT);
1175 rc = cl_page_own(env, io, clp);
1177 LASSERT(clp->cp_state == CPS_FREEING);
1178 cl_page_put(env, clp);
1182 cl_2queue_add(queue, clp);
1184 /* drop the reference count for cl_page_find, so that the page
1185 * will be freed in cl_2queue_fini. */
1186 cl_page_put(env, clp);
1187 cl_page_clip(env, clp, 0, page_size);
1189 offset += page_size;
1193 enum cl_req_type typ = rw == READ ? CRT_READ : CRT_WRITE;
1195 async = async && (typ == CRT_WRITE);
1197 rc = cl_echo_async_brw(env, io, typ, queue);
1199 rc = cl_io_submit_sync(env, io, typ, queue,
1201 CDEBUG(D_INFO, "echo_client %s write returns %d\n",
1202 async ? "async" : "sync", rc);
1205 cl_echo_cancel0(env, ed, lh.cookie);
1208 cl_2queue_discard(env, io, queue);
1209 cl_2queue_disown(env, io, queue);
1210 cl_2queue_fini(env, queue);
1211 cl_io_fini(env, io);
1213 cl_env_put(env, &refcheck);
1216 /** @} echo_exports */
1219 static obd_id last_object_id;
1222 echo_copyout_lsm (struct lov_stripe_md *lsm, void *_ulsm, int ulsm_nob)
1224 struct lov_stripe_md *ulsm = _ulsm;
1227 nob = offsetof (struct lov_stripe_md, lsm_oinfo[lsm->lsm_stripe_count]);
1231 if (cfs_copy_to_user (ulsm, lsm, sizeof(ulsm)))
1234 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1235 if (cfs_copy_to_user (ulsm->lsm_oinfo[i], lsm->lsm_oinfo[i],
1236 sizeof(lsm->lsm_oinfo[0])))
1243 echo_copyin_lsm (struct echo_device *ed, struct lov_stripe_md *lsm,
1244 void *ulsm, int ulsm_nob)
1246 struct echo_client_obd *ec = ed->ed_ec;
1249 if (ulsm_nob < sizeof (*lsm))
1252 if (cfs_copy_from_user (lsm, ulsm, sizeof (*lsm)))
1255 if (lsm->lsm_stripe_count > ec->ec_nstripes ||
1256 lsm->lsm_magic != LOV_MAGIC ||
1257 (lsm->lsm_stripe_size & (~CFS_PAGE_MASK)) != 0 ||
1258 ((__u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count > ~0UL))
1262 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1263 if (cfs_copy_from_user(lsm->lsm_oinfo[i],
1264 ((struct lov_stripe_md *)ulsm)-> \
1266 sizeof(lsm->lsm_oinfo[0])))
1272 static int echo_create_object(struct echo_device *ed, int on_target,
1273 struct obdo *oa, void *ulsm, int ulsm_nob,
1274 struct obd_trans_info *oti)
1276 struct echo_object *eco;
1277 struct echo_client_obd *ec = ed->ed_ec;
1278 struct lov_stripe_md *lsm = NULL;
1283 if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
1284 (on_target || /* set_stripe */
1285 ec->ec_nstripes != 0)) { /* LOV */
1286 CERROR ("No valid oid\n");
1290 rc = obd_alloc_memmd(ec->ec_exp, &lsm);
1292 CERROR("Cannot allocate md, rc = %d\n", rc);
1299 rc = echo_copyin_lsm (ed, lsm, ulsm, ulsm_nob);
1303 if (lsm->lsm_stripe_count == 0)
1304 lsm->lsm_stripe_count = ec->ec_nstripes;
1306 if (lsm->lsm_stripe_size == 0)
1307 lsm->lsm_stripe_size = CFS_PAGE_SIZE;
1311 /* setup stripes: indices + default ids if required */
1312 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1313 if (lsm->lsm_oinfo[i]->loi_id == 0)
1314 lsm->lsm_oinfo[i]->loi_id = lsm->lsm_object_id;
1316 lsm->lsm_oinfo[i]->loi_ost_idx =
1317 (idx + i) % ec->ec_nstripes;
1321 /* setup object ID here for !on_target and LOV hint */
1322 if (oa->o_valid & OBD_MD_FLID)
1323 lsm->lsm_object_id = oa->o_id;
1325 if (lsm->lsm_object_id == 0)
1326 lsm->lsm_object_id = ++last_object_id;
1330 /* Only echo objects are allowed to be created */
1331 LASSERT((oa->o_valid & OBD_MD_FLGROUP) &&
1332 (oa->o_gr == FILTER_GROUP_ECHO));
1333 rc = obd_create(ec->ec_exp, oa, &lsm, oti);
1335 CERROR("Cannot create objects, rc = %d\n", rc);
1341 /* See what object ID we were given */
1342 oa->o_id = lsm->lsm_object_id;
1343 oa->o_valid |= OBD_MD_FLID;
1345 eco = cl_echo_object_find(ed, &lsm);
1347 GOTO(failed, rc = PTR_ERR(eco));
1348 cl_echo_object_put(eco);
1350 CDEBUG(D_INFO, "oa->o_id = %lx\n", (long)oa->o_id);
1355 obd_destroy(ec->ec_exp, oa, lsm, oti, NULL, NULL);
1357 obd_free_memmd(ec->ec_exp, &lsm);
1359 CERROR("create object failed with rc = %d\n", rc);
1363 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
1366 struct echo_client_obd *ec = ed->ed_ec;
1367 struct lov_stripe_md *lsm = NULL;
1368 struct echo_object *eco;
1372 if ((oa->o_valid & OBD_MD_FLID) == 0 ||
1373 oa->o_id == 0) /* disallow use of object id 0 */
1375 CERROR ("No valid oid\n");
1379 rc = obd_alloc_memmd(ec->ec_exp, &lsm);
1383 lsm->lsm_object_id = oa->o_id;
1384 if (oa->o_valid & OBD_MD_FLGROUP)
1385 lsm->lsm_object_gr = oa->o_gr;
1387 lsm->lsm_object_gr = FILTER_GROUP_ECHO;
1390 eco = cl_echo_object_find(ed, &lsm);
1396 obd_free_memmd(ec->ec_exp, &lsm);
1400 static void echo_put_object(struct echo_object *eco)
1402 if (cl_echo_object_put(eco))
1403 CERROR("echo client: drop an object failed");
1407 echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
1409 unsigned long stripe_count;
1410 unsigned long stripe_size;
1411 unsigned long width;
1412 unsigned long woffset;
1416 if (lsm->lsm_stripe_count <= 1)
1420 stripe_size = lsm->lsm_stripe_size;
1421 stripe_count = lsm->lsm_stripe_count;
1423 /* width = # bytes in all stripes */
1424 width = stripe_size * stripe_count;
1426 /* woffset = offset within a width; offset = whole number of widths */
1427 woffset = do_div (offset, width);
1429 stripe_index = woffset / stripe_size;
1431 *idp = lsm->lsm_oinfo[stripe_index]->loi_id;
1432 *offp = offset * stripe_size + woffset % stripe_size;
1436 echo_client_page_debug_setup(struct lov_stripe_md *lsm,
1437 cfs_page_t *page, int rw, obd_id id,
1438 obd_off offset, obd_off count)
1445 /* no partial pages on the client */
1446 LASSERT(count == CFS_PAGE_SIZE);
1448 addr = cfs_kmap(page);
1450 for (delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
1451 if (rw == OBD_BRW_WRITE) {
1452 stripe_off = offset + delta;
1454 echo_get_stripe_off_id(lsm, &stripe_off, &stripe_id);
1456 stripe_off = 0xdeadbeef00c0ffeeULL;
1457 stripe_id = 0xdeadbeef00c0ffeeULL;
1459 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
1460 stripe_off, stripe_id);
1466 static int echo_client_page_debug_check(struct lov_stripe_md *lsm,
1467 cfs_page_t *page, obd_id id,
1468 obd_off offset, obd_off count)
1477 /* no partial pages on the client */
1478 LASSERT(count == CFS_PAGE_SIZE);
1480 addr = cfs_kmap(page);
1482 for (rc = delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
1483 stripe_off = offset + delta;
1485 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
1487 rc2 = block_debug_check("test_brw",
1488 addr + delta, OBD_ECHO_BLOCK_SIZE,
1489 stripe_off, stripe_id);
1491 CERROR ("Error in echo object "LPX64"\n", id);
1500 static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
1501 struct echo_object *eco, obd_off offset,
1502 obd_size count, int async,
1503 struct obd_trans_info *oti)
1505 struct echo_client_obd *ec = ed->ed_ec;
1506 struct lov_stripe_md *lsm = eco->eo_lsm;
1508 struct brw_page *pga;
1509 struct brw_page *pgp;
1518 verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
1519 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
1520 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
1522 gfp_mask = ((oa->o_id & 2) == 0) ? CFS_ALLOC_STD : CFS_ALLOC_HIGHUSER;
1524 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
1525 LASSERT(lsm != NULL);
1526 LASSERT(lsm->lsm_object_id == oa->o_id);
1529 (count & (~CFS_PAGE_MASK)) != 0)
1532 /* XXX think again with misaligned I/O */
1533 npages = count >> CFS_PAGE_SHIFT;
1535 OBD_ALLOC(pga, npages * sizeof(*pga));
1539 OBD_ALLOC(pages, npages * sizeof(*pages));
1540 if (pages == NULL) {
1541 OBD_FREE(pga, npages * sizeof(*pga));
1545 for (i = 0, pgp = pga, off = offset;
1547 i++, pgp++, off += CFS_PAGE_SIZE) {
1549 LASSERT (pgp->pg == NULL); /* for cleanup */
1552 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
1553 if (pgp->pg == NULL)
1557 pgp->count = CFS_PAGE_SIZE;
1562 echo_client_page_debug_setup(lsm, pgp->pg, rw,
1563 oa->o_id, off, pgp->count);
1566 if (ed->ed_next == NULL) {
1567 struct obd_info oinfo = { { { 0 } } };
1570 rc = obd_brw(rw, ec->ec_exp, &oinfo, npages, pga, oti);
1572 rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
1575 if (rc != 0 || rw != OBD_BRW_READ)
1578 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
1579 if (pgp->pg == NULL)
1584 vrc = echo_client_page_debug_check(lsm, pgp->pg, oa->o_id,
1585 pgp->off, pgp->count);
1586 if (vrc != 0 && rc == 0)
1589 OBD_PAGE_FREE(pgp->pg);
1591 OBD_FREE(pga, npages * sizeof(*pga));
1592 OBD_FREE(pages, npages * sizeof(*pages));
1596 static int echo_client_prep_commit(struct obd_export *exp, int rw,
1597 struct obdo *oa, struct echo_object *eco,
1598 obd_off offset, obd_size count,
1599 obd_size batch, struct obd_trans_info *oti)
1601 struct lov_stripe_md *lsm = eco->eo_lsm;
1602 struct obd_ioobj ioo;
1603 struct niobuf_local *lnb;
1604 struct niobuf_remote *rnb;
1606 obd_size npages, tot_pages;
1610 if (count <= 0 || (count & (~CFS_PAGE_MASK)) != 0 ||
1611 (lsm != NULL && lsm->lsm_object_id != oa->o_id))
1614 npages = batch >> CFS_PAGE_SHIFT;
1615 tot_pages = count >> CFS_PAGE_SHIFT;
1617 OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
1618 OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
1620 if (lnb == NULL || rnb == NULL)
1621 GOTO(out, ret = -ENOMEM);
1623 obdo_to_ioobj(oa, &ioo);
1627 for(; tot_pages; tot_pages -= npages) {
1630 if (tot_pages < npages)
1633 for (i = 0; i < npages; i++, off += CFS_PAGE_SIZE) {
1634 rnb[i].offset = off;
1635 rnb[i].len = CFS_PAGE_SIZE;
1638 ioo.ioo_bufcnt = npages;
1639 oti->oti_transno = 0;
1642 ret = obd_preprw(rw, exp, oa, 1, &ioo, rnb, &lpages, lnb, oti,
1646 LASSERT(lpages == npages);
1648 for (i = 0; i < lpages; i++) {
1649 cfs_page_t *page = lnb[i].page;
1651 /* read past eof? */
1652 if (page == NULL && lnb[i].rc == 0)
1655 if (oa->o_id == ECHO_PERSISTENT_OBJID ||
1656 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
1657 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
1660 if (rw == OBD_BRW_WRITE)
1661 echo_client_page_debug_setup(lsm, page, rw,
1666 echo_client_page_debug_check(lsm, page,
1672 ret = obd_commitrw(rw, exp, oa, 1,&ioo,rnb,npages,lnb,oti,ret);
1676 /* Reset oti otherwise it would confuse ldiskfs. */
1677 memset(oti, 0, sizeof(*oti));
1682 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
1684 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
1688 static int echo_client_brw_ioctl(int rw, struct obd_export *exp,
1689 struct obd_ioctl_data *data)
1691 struct obd_device *obd = class_exp2obd(exp);
1692 struct echo_device *ed = obd2echo_dev(obd);
1693 struct echo_client_obd *ec = ed->ed_ec;
1694 struct obd_trans_info dummy_oti = { .oti_thread = NULL };
1695 struct obdo *oa = &data->ioc_obdo1;
1696 struct echo_object *eco;
1701 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
1703 rc = echo_get_object(&eco, ed, oa);
1707 oa->o_valid &= ~OBD_MD_FLHANDLE;
1709 switch((long)data->ioc_pbuf1) {
1714 rc = echo_client_kbrw(ed, rw, oa,
1715 eco, data->ioc_offset,
1716 data->ioc_count, async, &dummy_oti);
1719 rc = echo_client_prep_commit(ec->ec_exp, rw, oa,
1720 eco, data->ioc_offset,
1721 data->ioc_count, data->ioc_plen1,
1727 echo_put_object(eco);
1732 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
1733 int mode, obd_off offset, obd_size nob)
1735 struct echo_device *ed = obd2echo_dev(exp->exp_obd);
1736 struct lustre_handle *ulh = &oa->o_handle;
1737 struct echo_object *eco;
1742 if (ed->ed_next == NULL)
1743 RETURN(-EOPNOTSUPP);
1745 if (!(mode == LCK_PR || mode == LCK_PW))
1748 if ((offset & (~CFS_PAGE_MASK)) != 0 ||
1749 (nob & (~CFS_PAGE_MASK)) != 0)
1752 rc = echo_get_object (&eco, ed, oa);
1756 end = (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
1757 rc = cl_echo_enqueue(eco, offset, end, mode, &ulh->cookie);
1759 oa->o_valid |= OBD_MD_FLHANDLE;
1760 CDEBUG(D_INFO, "Cookie is "LPX64"\n", ulh->cookie);
1762 echo_put_object(eco);
1767 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
1769 struct echo_device *ed = obd2echo_dev(exp->exp_obd);
1770 __u64 cookie = oa->o_handle.cookie;
1772 if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
1775 CDEBUG(D_INFO, "Cookie is "LPX64"\n", cookie);
1776 return cl_echo_cancel(ed, cookie);
1780 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
1781 int len, void *karg, void *uarg)
1783 struct obd_device *obd = exp->exp_obd;
1784 struct echo_device *ed = obd2echo_dev(obd);
1785 struct echo_client_obd *ec = ed->ed_ec;
1786 struct echo_object *eco;
1787 struct obd_ioctl_data *data = karg;
1788 struct obd_trans_info dummy_oti;
1789 struct oti_req_ack_lock *ack_lock;
1791 int rw = OBD_BRW_READ;
1796 cfs_unlock_kernel();
1798 memset(&dummy_oti, 0, sizeof(dummy_oti));
1800 oa = &data->ioc_obdo1;
1801 if (!(oa->o_valid & OBD_MD_FLGROUP)) {
1802 oa->o_valid |= OBD_MD_FLGROUP;
1803 oa->o_gr = FILTER_GROUP_ECHO;
1805 /* assume we can touch filter native objects with echo device. */
1806 /* LASSERT(oa->o_gr == FILTER_GROUP_ECHO); */
1809 case OBD_IOC_CREATE: /* may create echo object */
1810 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1811 GOTO (out, rc = -EPERM);
1813 rc = echo_create_object (ed, 1, oa,
1814 data->ioc_pbuf1, data->ioc_plen1,
1818 case OBD_IOC_DESTROY:
1819 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1820 GOTO (out, rc = -EPERM);
1822 rc = echo_get_object (&eco, ed, oa);
1824 rc = obd_destroy(ec->ec_exp, oa, eco->eo_lsm,
1825 &dummy_oti, NULL, NULL);
1827 eco->eo_deleted = 1;
1828 echo_put_object(eco);
1832 case OBD_IOC_GETATTR:
1833 rc = echo_get_object (&eco, ed, oa);
1835 struct obd_info oinfo = { { { 0 } } };
1836 oinfo.oi_md = eco->eo_lsm;
1838 rc = obd_getattr(ec->ec_exp, &oinfo);
1839 echo_put_object(eco);
1843 case OBD_IOC_SETATTR:
1844 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1845 GOTO (out, rc = -EPERM);
1847 rc = echo_get_object (&eco, ed, oa);
1849 struct obd_info oinfo = { { { 0 } } };
1851 oinfo.oi_md = eco->eo_lsm;
1853 rc = obd_setattr(ec->ec_exp, &oinfo, NULL);
1854 echo_put_object(eco);
1858 case OBD_IOC_BRW_WRITE:
1859 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1860 GOTO (out, rc = -EPERM);
1864 case OBD_IOC_BRW_READ:
1865 rc = echo_client_brw_ioctl(rw, exp, data);
1868 case ECHO_IOC_GET_STRIPE:
1869 rc = echo_get_object(&eco, ed, oa);
1871 rc = echo_copyout_lsm(eco->eo_lsm, data->ioc_pbuf1,
1873 echo_put_object(eco);
1877 case ECHO_IOC_SET_STRIPE:
1878 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1879 GOTO (out, rc = -EPERM);
1881 if (data->ioc_pbuf1 == NULL) { /* unset */
1882 rc = echo_get_object(&eco, ed, oa);
1884 eco->eo_deleted = 1;
1885 echo_put_object(eco);
1888 rc = echo_create_object(ed, 0, oa,
1890 data->ioc_plen1, &dummy_oti);
1894 case ECHO_IOC_ENQUEUE:
1895 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1896 GOTO (out, rc = -EPERM);
1898 rc = echo_client_enqueue(exp, oa,
1899 data->ioc_conn1, /* lock mode */
1901 data->ioc_count);/*extent*/
1904 case ECHO_IOC_CANCEL:
1905 rc = echo_client_cancel(exp, oa);
1909 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
1910 GOTO (out, rc = -ENOTTY);
1916 /* XXX this should be in a helper also called by target_send_reply */
1917 for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
1919 if (!ack_lock->mode)
1921 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
1929 static int echo_client_setup(struct obd_device *obddev, struct lustre_cfg *lcfg)
1931 struct echo_client_obd *ec = &obddev->u.echo_client;
1932 struct obd_device *tgt;
1933 struct obd_uuid echo_uuid = { "ECHO_UUID" };
1934 struct obd_connect_data *ocd = NULL;
1938 if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1939 CERROR("requires a TARGET OBD name\n");
1943 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
1944 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
1945 CERROR("device not attached or not set up (%s)\n",
1946 lustre_cfg_string(lcfg, 1));
1950 cfs_spin_lock_init (&ec->ec_lock);
1951 CFS_INIT_LIST_HEAD (&ec->ec_objects);
1952 CFS_INIT_LIST_HEAD (&ec->ec_locks);
1954 ec->ec_nstripes = 0;
1956 OBD_ALLOC(ocd, sizeof(*ocd));
1958 CERROR("Can't alloc ocd connecting to %s\n",
1959 lustre_cfg_string(lcfg, 1));
1963 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
1965 ocd->ocd_version = LUSTRE_VERSION_CODE;
1966 ocd->ocd_group = FILTER_GROUP_ECHO;
1968 rc = obd_connect(NULL, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
1970 /* Turn off pinger because it connects to tgt obd directly. */
1971 cfs_spin_lock(&tgt->obd_dev_lock);
1972 cfs_list_del_init(&ec->ec_exp->exp_obd_chain_timed);
1973 cfs_spin_unlock(&tgt->obd_dev_lock);
1976 OBD_FREE(ocd, sizeof(*ocd));
1979 CERROR("fail to connect to device %s\n",
1980 lustre_cfg_string(lcfg, 1));
1987 static int echo_client_cleanup(struct obd_device *obddev)
1989 struct echo_client_obd *ec = &obddev->u.echo_client;
1993 if (!cfs_list_empty(&obddev->obd_exports)) {
1994 CERROR("still has clients!\n");
1998 LASSERT(cfs_atomic_read(&ec->ec_exp->exp_refcount) > 0);
1999 rc = obd_disconnect(ec->ec_exp);
2001 CERROR("fail to disconnect device: %d\n", rc);
2006 static int echo_client_connect(const struct lu_env *env,
2007 struct obd_export **exp,
2008 struct obd_device *src, struct obd_uuid *cluuid,
2009 struct obd_connect_data *data, void *localdata)
2012 struct lustre_handle conn = { 0 };
2015 rc = class_connect(&conn, src, cluuid);
2017 *exp = class_conn2export(&conn);
2023 static int echo_client_disconnect(struct obd_export *exp)
2026 struct obd_device *obd;
2027 struct echo_client_obd *ec;
2028 struct ec_lock *ecl;
2034 GOTO(out, rc = -EINVAL);
2038 ec = &obd->u.echo_client;
2040 /* no more contention on export's lock list */
2041 while (!cfs_list_empty (&exp->exp_ec_data.eced_locks)) {
2042 ecl = cfs_list_entry (exp->exp_ec_data.eced_locks.next,
2043 struct ec_lock, ecl_exp_chain);
2044 cfs_list_del (&ecl->ecl_exp_chain);
2046 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm,
2047 ecl->ecl_mode, &ecl->ecl_lock_handle);
2049 CDEBUG (D_INFO, "Cancel lock on object "LPX64" on disconnect "
2050 "(%d)\n", ecl->ecl_object->eco_id, rc);
2052 echo_put_object (ecl->ecl_object);
2053 OBD_FREE (ecl, sizeof (*ecl));
2057 rc = class_disconnect(exp);
2063 static struct obd_ops echo_obd_ops = {
2064 .o_owner = THIS_MODULE,
2067 .o_setup = echo_client_setup,
2068 .o_cleanup = echo_client_cleanup,
2071 .o_iocontrol = echo_client_iocontrol,
2072 .o_connect = echo_client_connect,
2073 .o_disconnect = echo_client_disconnect
2076 int echo_client_init(void)
2078 struct lprocfs_static_vars lvars = { 0 };
2081 lprocfs_echo_init_vars(&lvars);
2082 rc = class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
2083 LUSTRE_ECHO_CLIENT_NAME, &echo_device_type);
2085 lu_kmem_init(echo_caches);
2089 void echo_client_exit(void)
2091 class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
2092 lu_kmem_fini(echo_caches);
2095 /** @} echo_client */