1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011 Whamcloud, Inc.
36 * This file is part of Lustre, http://www.lustre.org/
37 * Lustre is a trademark of Sun Microsystems, Inc.
40 #define DEBUG_SUBSYSTEM S_ECHO
42 #include <libcfs/libcfs.h>
44 #include <liblustre.h>
48 #include <obd_support.h>
49 #include <obd_class.h>
50 #include <lustre_debug.h>
51 #include <lprocfs_status.h>
52 #include <cl_object.h>
54 #include "echo_internal.h"
56 /** \defgroup echo_client Echo Client
61 struct cl_device ed_cl;
62 struct echo_client_obd *ed_ec;
64 struct cl_site ed_site_myself;
65 struct cl_site *ed_site;
66 struct lu_device *ed_next;
71 struct cl_object eo_cl;
72 struct cl_object_header eo_hdr;
74 struct echo_device *eo_dev;
75 cfs_list_t eo_obj_chain;
76 struct lov_stripe_md *eo_lsm;
77 cfs_atomic_t eo_npages;
81 struct echo_object_conf {
82 struct cl_object_conf eoc_cl;
83 struct lov_stripe_md **eoc_md;
87 struct cl_page_slice ep_cl;
89 cfs_page_t *ep_vmpage;
93 struct cl_lock_slice el_cl;
95 struct echo_object *el_object;
97 cfs_atomic_t el_refcount;
101 struct cl_io_slice ei_cl;
106 struct cl_req_slice er_cl;
110 static int echo_client_setup(struct obd_device *obddev,
111 struct lustre_cfg *lcfg);
112 static int echo_client_cleanup(struct obd_device *obddev);
115 /** \defgroup echo_helpers Helper functions
118 static inline struct echo_device *cl2echo_dev(const struct cl_device *dev)
120 return container_of0(dev, struct echo_device, ed_cl);
123 static inline struct cl_device *echo_dev2cl(struct echo_device *d)
128 static inline struct echo_device *obd2echo_dev(const struct obd_device *obd)
130 return cl2echo_dev(lu2cl_dev(obd->obd_lu_dev));
133 static inline struct cl_object *echo_obj2cl(struct echo_object *eco)
138 static inline struct echo_object *cl2echo_obj(const struct cl_object *o)
140 return container_of(o, struct echo_object, eo_cl);
143 static inline struct echo_page *cl2echo_page(const struct cl_page_slice *s)
145 return container_of(s, struct echo_page, ep_cl);
148 static inline struct echo_lock *cl2echo_lock(const struct cl_lock_slice *s)
150 return container_of(s, struct echo_lock, el_cl);
153 static inline struct cl_lock *echo_lock2cl(const struct echo_lock *ecl)
155 return ecl->el_cl.cls_lock;
158 static struct lu_context_key echo_thread_key;
159 static inline struct echo_thread_info *echo_env_info(const struct lu_env *env)
161 struct echo_thread_info *info;
162 info = lu_context_key_get(&env->le_ctx, &echo_thread_key);
163 LASSERT(info != NULL);
168 struct echo_object_conf *cl2echo_conf(const struct cl_object_conf *c)
170 return container_of(c, struct echo_object_conf, eoc_cl);
173 static inline void lsm2fid(struct lov_stripe_md *lsm, struct lu_fid *fid)
176 fid->f_seq = FID_SEQ_ECHO;
177 /* truncated to 32 bits by assignment */
178 fid->f_oid = lsm->lsm_object_id;
179 fid->f_ver = lsm->lsm_object_id >> 32;
181 /** @} echo_helpers */
183 static struct echo_object *cl_echo_object_find(struct echo_device *d,
184 struct lov_stripe_md **lsm);
185 static int cl_echo_object_put(struct echo_object *eco);
186 static int cl_echo_enqueue (struct echo_object *eco, obd_off start,
187 obd_off end, int mode, __u64 *cookie);
188 static int cl_echo_cancel (struct echo_device *d, __u64 cookie);
189 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
190 cfs_page_t **pages, int npages, int async);
192 static struct echo_thread_info *echo_env_info(const struct lu_env *env);
194 struct echo_thread_info {
195 struct echo_object_conf eti_conf;
196 struct lustre_md eti_md;
198 struct cl_2queue eti_queue;
200 struct cl_lock_descr eti_descr;
201 struct lu_fid eti_fid;
204 /* No session used right now */
205 struct echo_session_info {
209 static cfs_mem_cache_t *echo_page_kmem;
210 static cfs_mem_cache_t *echo_lock_kmem;
211 static cfs_mem_cache_t *echo_object_kmem;
212 static cfs_mem_cache_t *echo_thread_kmem;
213 static cfs_mem_cache_t *echo_session_kmem;
214 //static cfs_mem_cache_t *echo_req_kmem;
216 static struct lu_kmem_descr echo_caches[] = {
218 .ckd_cache = &echo_page_kmem,
219 .ckd_name = "echo_page_kmem",
220 .ckd_size = sizeof (struct echo_page)
223 .ckd_cache = &echo_lock_kmem,
224 .ckd_name = "echo_lock_kmem",
225 .ckd_size = sizeof (struct echo_lock)
228 .ckd_cache = &echo_object_kmem,
229 .ckd_name = "echo_object_kmem",
230 .ckd_size = sizeof (struct echo_object)
233 .ckd_cache = &echo_thread_kmem,
234 .ckd_name = "echo_thread_kmem",
235 .ckd_size = sizeof (struct echo_thread_info)
238 .ckd_cache = &echo_session_kmem,
239 .ckd_name = "echo_session_kmem",
240 .ckd_size = sizeof (struct echo_session_info)
244 .ckd_cache = &echo_req_kmem,
245 .ckd_name = "echo_req_kmem",
246 .ckd_size = sizeof (struct echo_req)
254 /** \defgroup echo_page Page operations
256 * Echo page operations.
260 cfs_page_t *echo_page_vmpage(const struct lu_env *env,
261 const struct cl_page_slice *slice)
263 return cl2echo_page(slice)->ep_vmpage;
266 static int echo_page_own(const struct lu_env *env,
267 const struct cl_page_slice *slice,
268 struct cl_io *io, int nonblock)
270 struct echo_page *ep = cl2echo_page(slice);
273 cfs_mutex_lock(&ep->ep_lock);
274 else if (!cfs_mutex_trylock(&ep->ep_lock))
279 static void echo_page_disown(const struct lu_env *env,
280 const struct cl_page_slice *slice,
283 struct echo_page *ep = cl2echo_page(slice);
285 LASSERT(cfs_mutex_is_locked(&ep->ep_lock));
286 cfs_mutex_unlock(&ep->ep_lock);
289 static void echo_page_discard(const struct lu_env *env,
290 const struct cl_page_slice *slice,
291 struct cl_io *unused)
293 cl_page_delete(env, slice->cpl_page);
296 static int echo_page_is_vmlocked(const struct lu_env *env,
297 const struct cl_page_slice *slice)
299 if (cfs_mutex_is_locked(&cl2echo_page(slice)->ep_lock))
304 static void echo_page_completion(const struct lu_env *env,
305 const struct cl_page_slice *slice,
308 LASSERT(slice->cpl_page->cp_sync_io != NULL);
311 static void echo_page_fini(const struct lu_env *env,
312 struct cl_page_slice *slice)
314 struct echo_page *ep = cl2echo_page(slice);
315 struct echo_object *eco = cl2echo_obj(slice->cpl_obj);
316 cfs_page_t *vmpage = ep->ep_vmpage;
319 cfs_atomic_dec(&eco->eo_npages);
320 page_cache_release(vmpage);
321 OBD_SLAB_FREE_PTR(ep, echo_page_kmem);
325 static int echo_page_prep(const struct lu_env *env,
326 const struct cl_page_slice *slice,
327 struct cl_io *unused)
332 static int echo_page_print(const struct lu_env *env,
333 const struct cl_page_slice *slice,
334 void *cookie, lu_printer_t printer)
336 struct echo_page *ep = cl2echo_page(slice);
338 (*printer)(env, cookie, LUSTRE_ECHO_CLIENT_NAME"-page@%p %d vm@%p\n",
339 ep, cfs_mutex_is_locked(&ep->ep_lock), ep->ep_vmpage);
343 static const struct cl_page_operations echo_page_ops = {
344 .cpo_own = echo_page_own,
345 .cpo_disown = echo_page_disown,
346 .cpo_discard = echo_page_discard,
347 .cpo_vmpage = echo_page_vmpage,
348 .cpo_fini = echo_page_fini,
349 .cpo_print = echo_page_print,
350 .cpo_is_vmlocked = echo_page_is_vmlocked,
353 .cpo_prep = echo_page_prep,
354 .cpo_completion = echo_page_completion,
357 .cpo_prep = echo_page_prep,
358 .cpo_completion = echo_page_completion,
364 /** \defgroup echo_lock Locking
366 * echo lock operations
370 static void echo_lock_fini(const struct lu_env *env,
371 struct cl_lock_slice *slice)
373 struct echo_lock *ecl = cl2echo_lock(slice);
375 LASSERT(cfs_list_empty(&ecl->el_chain));
376 OBD_SLAB_FREE_PTR(ecl, echo_lock_kmem);
379 static void echo_lock_delete(const struct lu_env *env,
380 const struct cl_lock_slice *slice)
382 struct echo_lock *ecl = cl2echo_lock(slice);
384 LASSERT(cfs_list_empty(&ecl->el_chain));
387 static int echo_lock_fits_into(const struct lu_env *env,
388 const struct cl_lock_slice *slice,
389 const struct cl_lock_descr *need,
390 const struct cl_io *unused)
395 static struct cl_lock_operations echo_lock_ops = {
396 .clo_fini = echo_lock_fini,
397 .clo_delete = echo_lock_delete,
398 .clo_fits_into = echo_lock_fits_into
403 /** \defgroup echo_cl_ops cl_object operations
405 * operations for cl_object
409 static struct cl_page *echo_page_init(const struct lu_env *env,
410 struct cl_object *obj,
411 struct cl_page *page, cfs_page_t *vmpage)
413 struct echo_page *ep;
416 OBD_SLAB_ALLOC_PTR_GFP(ep, echo_page_kmem, CFS_ALLOC_IO);
418 struct echo_object *eco = cl2echo_obj(obj);
419 ep->ep_vmpage = vmpage;
420 page_cache_get(vmpage);
421 cfs_mutex_init(&ep->ep_lock);
422 cl_page_slice_add(page, &ep->ep_cl, obj, &echo_page_ops);
423 cfs_atomic_inc(&eco->eo_npages);
425 RETURN(ERR_PTR(ep ? 0 : -ENOMEM));
428 static int echo_io_init(const struct lu_env *env, struct cl_object *obj,
434 static int echo_lock_init(const struct lu_env *env,
435 struct cl_object *obj, struct cl_lock *lock,
436 const struct cl_io *unused)
438 struct echo_lock *el;
441 OBD_SLAB_ALLOC_PTR_GFP(el, echo_lock_kmem, CFS_ALLOC_IO);
443 cl_lock_slice_add(lock, &el->el_cl, obj, &echo_lock_ops);
444 el->el_object = cl2echo_obj(obj);
445 CFS_INIT_LIST_HEAD(&el->el_chain);
446 cfs_atomic_set(&el->el_refcount, 0);
448 RETURN(el == NULL ? -ENOMEM : 0);
451 static int echo_conf_set(const struct lu_env *env, struct cl_object *obj,
452 const struct cl_object_conf *conf)
457 static const struct cl_object_operations echo_cl_obj_ops = {
458 .coo_page_init = echo_page_init,
459 .coo_lock_init = echo_lock_init,
460 .coo_io_init = echo_io_init,
461 .coo_conf_set = echo_conf_set
463 /** @} echo_cl_ops */
465 /** \defgroup echo_lu_ops lu_object operations
467 * operations for echo lu object.
471 static int echo_object_init(const struct lu_env *env, struct lu_object *obj,
472 const struct lu_object_conf *conf)
474 const struct cl_object_conf *cconf = lu2cl_conf(conf);
475 struct echo_object_conf *econf = cl2echo_conf(cconf);
476 struct echo_device *ed = cl2echo_dev(lu2cl_dev(obj->lo_dev));
477 struct echo_client_obd *ec = ed->ed_ec;
478 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
482 struct lu_object *below;
483 struct lu_device *under;
486 below = under->ld_ops->ldo_object_alloc(env, obj->lo_header,
490 lu_object_add(obj, below);
493 LASSERT(econf->eoc_md);
494 eco->eo_lsm = *econf->eoc_md;
496 cfs_atomic_set(&eco->eo_npages, 0);
498 /* clear the lsm pointer so that it won't get freed. */
499 *econf->eoc_md = NULL;
501 cfs_spin_lock(&ec->ec_lock);
502 cfs_list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
503 cfs_spin_unlock(&ec->ec_lock);
508 static void echo_object_free(const struct lu_env *env, struct lu_object *obj)
510 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
511 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
512 struct lov_stripe_md *lsm = eco->eo_lsm;
515 LASSERT(cfs_atomic_read(&eco->eo_npages) == 0);
517 cfs_spin_lock(&ec->ec_lock);
518 cfs_list_del_init(&eco->eo_obj_chain);
519 cfs_spin_unlock(&ec->ec_lock);
522 lu_object_header_fini(obj->lo_header);
525 obd_free_memmd(ec->ec_exp, &lsm);
526 OBD_SLAB_FREE_PTR(eco, echo_object_kmem);
530 static int echo_object_print(const struct lu_env *env, void *cookie,
531 lu_printer_t p, const struct lu_object *o)
533 struct echo_object *obj = cl2echo_obj(lu2cl(o));
535 return (*p)(env, cookie, "echoclient-object@%p", obj);
539 static const struct lu_object_operations echo_lu_obj_ops = {
540 .loo_object_init = echo_object_init,
541 .loo_object_delete = NULL,
542 .loo_object_release = NULL,
543 .loo_object_free = echo_object_free,
544 .loo_object_print = echo_object_print,
545 .loo_object_invariant = NULL
547 /** @} echo_lu_ops */
549 /** \defgroup echo_lu_dev_ops lu_device operations
551 * Operations for echo lu device.
555 static struct lu_object *echo_object_alloc(const struct lu_env *env,
556 const struct lu_object_header *hdr,
557 struct lu_device *dev)
559 struct echo_object *eco;
560 struct lu_object *obj = NULL;
563 /* we're the top dev. */
564 LASSERT(hdr == NULL);
565 OBD_SLAB_ALLOC_PTR_GFP(eco, echo_object_kmem, CFS_ALLOC_IO);
567 struct cl_object_header *hdr = &eco->eo_hdr;
569 obj = &echo_obj2cl(eco)->co_lu;
570 cl_object_header_init(hdr);
571 lu_object_init(obj, &hdr->coh_lu, dev);
572 lu_object_add_top(&hdr->coh_lu, obj);
574 eco->eo_cl.co_ops = &echo_cl_obj_ops;
575 obj->lo_ops = &echo_lu_obj_ops;
580 static struct lu_device_operations echo_device_lu_ops = {
581 .ldo_object_alloc = echo_object_alloc,
583 /** @} echo_lu_dev_ops */
585 static struct cl_device_operations echo_device_cl_ops = {
588 /** \defgroup echo_init Setup and teardown
590 * Init and fini functions for echo client.
594 static int echo_site_init(const struct lu_env *env, struct echo_device *ed)
596 struct cl_site *site = &ed->ed_site_myself;
599 /* initialize site */
600 rc = cl_site_init(site, &ed->ed_cl);
602 CERROR("Cannot initilize site for echo client(%d)\n", rc);
606 rc = lu_site_init_finish(&site->cs_lu);
614 static void echo_site_fini(const struct lu_env *env, struct echo_device *ed)
617 cl_site_fini(ed->ed_site);
622 static void *echo_thread_key_init(const struct lu_context *ctx,
623 struct lu_context_key *key)
625 struct echo_thread_info *info;
627 OBD_SLAB_ALLOC_PTR_GFP(info, echo_thread_kmem, CFS_ALLOC_IO);
629 info = ERR_PTR(-ENOMEM);
633 static void echo_thread_key_fini(const struct lu_context *ctx,
634 struct lu_context_key *key, void *data)
636 struct echo_thread_info *info = data;
637 OBD_SLAB_FREE_PTR(info, echo_thread_kmem);
640 static void echo_thread_key_exit(const struct lu_context *ctx,
641 struct lu_context_key *key, void *data)
645 static struct lu_context_key echo_thread_key = {
646 .lct_tags = LCT_CL_THREAD,
647 .lct_init = echo_thread_key_init,
648 .lct_fini = echo_thread_key_fini,
649 .lct_exit = echo_thread_key_exit
652 static void *echo_session_key_init(const struct lu_context *ctx,
653 struct lu_context_key *key)
655 struct echo_session_info *session;
657 OBD_SLAB_ALLOC_PTR_GFP(session, echo_session_kmem, CFS_ALLOC_IO);
659 session = ERR_PTR(-ENOMEM);
663 static void echo_session_key_fini(const struct lu_context *ctx,
664 struct lu_context_key *key, void *data)
666 struct echo_session_info *session = data;
667 OBD_SLAB_FREE_PTR(session, echo_session_kmem);
670 static void echo_session_key_exit(const struct lu_context *ctx,
671 struct lu_context_key *key, void *data)
675 static struct lu_context_key echo_session_key = {
676 .lct_tags = LCT_SESSION,
677 .lct_init = echo_session_key_init,
678 .lct_fini = echo_session_key_fini,
679 .lct_exit = echo_session_key_exit
682 LU_TYPE_INIT_FINI(echo, &echo_thread_key, &echo_session_key);
684 static struct lu_device *echo_device_alloc(const struct lu_env *env,
685 struct lu_device_type *t,
686 struct lustre_cfg *cfg)
688 struct lu_device *next;
689 struct echo_device *ed;
690 struct cl_device *cd;
691 struct obd_device *obd = NULL; /* to keep compiler happy */
692 struct obd_device *tgt;
693 const char *tgt_type_name;
700 GOTO(out, rc = -ENOMEM);
704 rc = cl_device_init(cd, t);
708 cd->cd_lu_dev.ld_ops = &echo_device_lu_ops;
709 cd->cd_ops = &echo_device_cl_ops;
712 rc = echo_site_init(env, ed);
717 obd = class_name2obd(lustre_cfg_string(cfg, 0));
718 LASSERT(obd != NULL);
719 rc = echo_client_setup(obd, cfg);
722 ed->ed_ec = &obd->u.echo_client;
725 tgt = class_name2obd(lustre_cfg_string(cfg, 1));
726 LASSERT(tgt != NULL);
727 next = tgt->obd_lu_dev;
728 if (next != NULL && !lu_device_is_cl(next))
732 * if echo client is to be stacked upon ost device, the next is NULL
733 * since ost is not a clio device so far
735 tgt_type_name = tgt->obd_type->typ_name;
737 LASSERT(next != NULL);
738 if (next->ld_site != NULL)
739 GOTO(out, rc = -EBUSY);
741 next->ld_site = &ed->ed_site->cs_lu;
742 rc = next->ld_type->ldt_ops->ldto_device_init(env, next,
743 next->ld_type->ldt_name, NULL);
747 /* Trikcy case, I have to determine the obd type since clio
748 * uses the different parameters to initialize objects for
751 if (strcmp(tgt_type_name, LUSTRE_LOV_NAME) == 0)
752 ed->ed_next_islov = 1;
754 LASSERT(strcmp(tgt_type_name, LUSTRE_OSC_NAME) == 0);
756 LASSERT(strcmp(tgt_type_name, LUSTRE_OST_NAME) == 0);
759 RETURN(&cd->cd_lu_dev);
765 rc2 = echo_client_cleanup(obd);
767 CERROR("Cleanup obd device %s error(%d)\n",
772 echo_site_fini(env, ed);
774 cl_device_fini(&ed->ed_cl);
784 static int echo_device_init(const struct lu_env *env, struct lu_device *d,
785 const char *name, struct lu_device *next)
791 static struct lu_device *echo_device_fini(const struct lu_env *env,
794 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
795 struct lu_device *next = ed->ed_next;
798 next = next->ld_type->ldt_ops->ldto_device_fini(env, next);
802 static void echo_lock_release(const struct lu_env *env,
803 struct echo_lock *ecl,
806 struct cl_lock *clk = echo_lock2cl(ecl);
810 cl_lock_release(env, clk, "ec enqueue", ecl->el_object);
812 cl_lock_mutex_get(env, clk);
813 cl_lock_cancel(env, clk);
814 cl_lock_delete(env, clk);
815 cl_lock_mutex_put(env, clk);
817 cl_lock_put(env, clk);
820 static struct lu_device *echo_device_free(const struct lu_env *env,
823 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
824 struct echo_client_obd *ec = ed->ed_ec;
825 struct echo_object *eco;
826 struct lu_device *next = ed->ed_next;
828 CDEBUG(D_INFO, "echo device:%p is going to be freed, next = %p\n",
831 LASSERT(ed->ed_site);
832 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
834 /* check if there are objects still alive.
835 * It shouldn't have any object because lu_site_purge would cleanup
836 * all of cached objects. Anyway, probably the echo device is being
837 * parallelly accessed.
839 cfs_spin_lock(&ec->ec_lock);
840 cfs_list_for_each_entry(eco, &ec->ec_objects, eo_obj_chain)
842 cfs_spin_unlock(&ec->ec_lock);
845 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
848 "Waiting for the reference of echo object to be dropped\n");
850 /* Wait for the last reference to be dropped. */
851 cfs_spin_lock(&ec->ec_lock);
852 while (!cfs_list_empty(&ec->ec_objects)) {
853 cfs_spin_unlock(&ec->ec_lock);
854 CERROR("echo_client still has objects at cleanup time, "
855 "wait for 1 second\n");
856 cfs_schedule_timeout_and_set_state(CFS_TASK_UNINT,
857 cfs_time_seconds(1));
858 cfs_spin_lock(&ec->ec_lock);
860 cfs_spin_unlock(&ec->ec_lock);
862 LASSERT(cfs_list_empty(&ec->ec_locks));
864 CDEBUG(D_INFO, "No object exists, exiting...\n");
866 echo_client_cleanup(d->ld_obd);
869 next = next->ld_type->ldt_ops->ldto_device_free(env, next);
871 LASSERT(ed->ed_site == lu2cl_site(d->ld_site));
872 echo_site_fini(env, ed);
873 cl_device_fini(&ed->ed_cl);
879 static const struct lu_device_type_operations echo_device_type_ops = {
880 .ldto_init = echo_type_init,
881 .ldto_fini = echo_type_fini,
883 .ldto_start = echo_type_start,
884 .ldto_stop = echo_type_stop,
886 .ldto_device_alloc = echo_device_alloc,
887 .ldto_device_free = echo_device_free,
888 .ldto_device_init = echo_device_init,
889 .ldto_device_fini = echo_device_fini
892 static struct lu_device_type echo_device_type = {
893 .ldt_tags = LU_DEVICE_CL,
894 .ldt_name = LUSTRE_ECHO_CLIENT_NAME,
895 .ldt_ops = &echo_device_type_ops,
896 .ldt_ctx_tags = LCT_CL_THREAD
900 /** \defgroup echo_exports Exported operations
902 * exporting functions to echo client
907 /* Interfaces to echo client obd device */
908 static struct echo_object *cl_echo_object_find(struct echo_device *d,
909 struct lov_stripe_md **lsmp)
912 struct echo_thread_info *info;
913 struct echo_object_conf *conf;
914 struct lov_stripe_md *lsm;
915 struct echo_object *eco;
916 struct cl_object *obj;
924 LASSERT(lsm->lsm_object_id);
926 /* Never return an object if the obd is to be freed. */
927 if (echo_dev2cl(d)->cd_lu_dev.ld_obd->obd_stopping)
928 RETURN(ERR_PTR(-ENODEV));
930 env = cl_env_get(&refcheck);
934 info = echo_env_info(env);
935 conf = &info->eti_conf;
937 if (!d->ed_next_islov) {
938 struct lov_oinfo *oinfo = lsm->lsm_oinfo[0];
939 LASSERT(oinfo != NULL);
940 oinfo->loi_id = lsm->lsm_object_id;
941 oinfo->loi_seq = lsm->lsm_object_seq;
942 conf->eoc_cl.u.coc_oinfo = oinfo;
944 struct lustre_md *md;
946 memset(md, 0, sizeof *md);
948 conf->eoc_cl.u.coc_md = md;
953 fid = &info->eti_fid;
956 obj = cl_object_find(env, echo_dev2cl(d), fid, &conf->eoc_cl);
958 GOTO(out, eco = (void*)obj);
960 eco = cl2echo_obj(obj);
961 if (eco->eo_deleted) {
962 cl_object_put(env, obj);
963 eco = ERR_PTR(-EAGAIN);
967 cl_env_put(env, &refcheck);
971 static int cl_echo_object_put(struct echo_object *eco)
974 struct cl_object *obj = echo_obj2cl(eco);
978 env = cl_env_get(&refcheck);
980 RETURN(PTR_ERR(env));
982 /* an external function to kill an object? */
983 if (eco->eo_deleted) {
984 struct lu_object_header *loh = obj->co_lu.lo_header;
985 LASSERT(&eco->eo_hdr == luh2coh(loh));
986 cfs_set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
989 cl_object_put(env, obj);
990 cl_env_put(env, &refcheck);
994 static int cl_echo_enqueue0(struct lu_env *env, struct echo_object *eco,
995 obd_off start, obd_off end, int mode,
996 __u64 *cookie , __u32 enqflags)
1000 struct cl_object *obj;
1001 struct cl_lock_descr *descr;
1002 struct echo_thread_info *info;
1006 info = echo_env_info(env);
1008 descr = &info->eti_descr;
1009 obj = echo_obj2cl(eco);
1011 descr->cld_obj = obj;
1012 descr->cld_start = cl_index(obj, start);
1013 descr->cld_end = cl_index(obj, end);
1014 descr->cld_mode = mode == LCK_PW ? CLM_WRITE : CLM_READ;
1015 descr->cld_enq_flags = enqflags;
1018 lck = cl_lock_request(env, io, descr, "ec enqueue", eco);
1020 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
1021 struct echo_lock *el;
1023 rc = cl_wait(env, lck);
1025 el = cl2echo_lock(cl_lock_at(lck, &echo_device_type));
1026 cfs_spin_lock(&ec->ec_lock);
1027 if (cfs_list_empty(&el->el_chain)) {
1028 cfs_list_add(&el->el_chain, &ec->ec_locks);
1029 el->el_cookie = ++ec->ec_unique;
1031 cfs_atomic_inc(&el->el_refcount);
1032 *cookie = el->el_cookie;
1033 cfs_spin_unlock(&ec->ec_lock);
1035 cl_lock_release(env, lck, "ec enqueue", cfs_current());
1040 static int cl_echo_enqueue(struct echo_object *eco, obd_off start, obd_off end,
1041 int mode, __u64 *cookie)
1043 struct echo_thread_info *info;
1050 env = cl_env_get(&refcheck);
1052 RETURN(PTR_ERR(env));
1054 info = echo_env_info(env);
1057 result = cl_io_init(env, io, CIT_MISC, echo_obj2cl(eco));
1060 LASSERT(result == 0);
1062 result = cl_echo_enqueue0(env, eco, start, end, mode, cookie, 0);
1063 cl_io_fini(env, io);
1067 cl_env_put(env, &refcheck);
1071 static int cl_echo_cancel0(struct lu_env *env, struct echo_device *ed,
1074 struct echo_client_obd *ec = ed->ed_ec;
1075 struct echo_lock *ecl = NULL;
1077 int found = 0, still_used = 0;
1080 LASSERT(ec != NULL);
1081 cfs_spin_lock (&ec->ec_lock);
1082 cfs_list_for_each (el, &ec->ec_locks) {
1083 ecl = cfs_list_entry (el, struct echo_lock, el_chain);
1084 CDEBUG(D_INFO, "ecl: %p, cookie: "LPX64"\n", ecl, ecl->el_cookie);
1085 found = (ecl->el_cookie == cookie);
1087 if (cfs_atomic_dec_and_test(&ecl->el_refcount))
1088 cfs_list_del_init(&ecl->el_chain);
1094 cfs_spin_unlock (&ec->ec_lock);
1099 echo_lock_release(env, ecl, still_used);
1103 static int cl_echo_cancel(struct echo_device *ed, __u64 cookie)
1110 env = cl_env_get(&refcheck);
1112 RETURN(PTR_ERR(env));
1114 rc = cl_echo_cancel0(env, ed, cookie);
1116 cl_env_put(env, &refcheck);
1120 static int cl_echo_async_brw(const struct lu_env *env, struct cl_io *io,
1121 enum cl_req_type unused, struct cl_2queue *queue)
1123 struct cl_page *clp;
1124 struct cl_page *temp;
1128 cl_page_list_for_each_safe(clp, temp, &queue->c2_qin) {
1130 rc = cl_page_cache_add(env, io, clp, CRT_WRITE);
1133 result = result ?: rc;
1138 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
1139 cfs_page_t **pages, int npages, int async)
1142 struct echo_thread_info *info;
1143 struct cl_object *obj = echo_obj2cl(eco);
1144 struct echo_device *ed = eco->eo_dev;
1145 struct cl_2queue *queue;
1147 struct cl_page *clp;
1148 struct lustre_handle lh = { 0 };
1149 int page_size = cl_page_size(obj);
1155 LASSERT((offset & ~CFS_PAGE_MASK) == 0);
1156 LASSERT(ed->ed_next != NULL);
1157 env = cl_env_get(&refcheck);
1159 RETURN(PTR_ERR(env));
1161 info = echo_env_info(env);
1163 queue = &info->eti_queue;
1165 cl_2queue_init(queue);
1166 rc = cl_io_init(env, io, CIT_MISC, obj);
1172 rc = cl_echo_enqueue0(env, eco, offset,
1173 offset + npages * CFS_PAGE_SIZE - 1,
1174 rw == READ ? LCK_PR : LCK_PW, &lh.cookie,
1177 GOTO(error_lock, rc);
1179 for (i = 0; i < npages; i++) {
1181 clp = cl_page_find(env, obj, cl_index(obj, offset),
1182 pages[i], CPT_TRANSIENT);
1187 LASSERT(clp->cp_type == CPT_TRANSIENT);
1189 rc = cl_page_own(env, io, clp);
1191 LASSERT(clp->cp_state == CPS_FREEING);
1192 cl_page_put(env, clp);
1196 cl_2queue_add(queue, clp);
1198 /* drop the reference count for cl_page_find, so that the page
1199 * will be freed in cl_2queue_fini. */
1200 cl_page_put(env, clp);
1201 cl_page_clip(env, clp, 0, page_size);
1203 offset += page_size;
1207 enum cl_req_type typ = rw == READ ? CRT_READ : CRT_WRITE;
1209 async = async && (typ == CRT_WRITE);
1211 rc = cl_echo_async_brw(env, io, typ, queue);
1213 rc = cl_io_submit_sync(env, io, typ, queue,
1215 CDEBUG(D_INFO, "echo_client %s write returns %d\n",
1216 async ? "async" : "sync", rc);
1219 cl_echo_cancel0(env, ed, lh.cookie);
1222 cl_2queue_discard(env, io, queue);
1223 cl_2queue_disown(env, io, queue);
1224 cl_2queue_fini(env, queue);
1225 cl_io_fini(env, io);
1227 cl_env_put(env, &refcheck);
1230 /** @} echo_exports */
1233 static obd_id last_object_id;
1236 echo_copyout_lsm (struct lov_stripe_md *lsm, void *_ulsm, int ulsm_nob)
1238 struct lov_stripe_md *ulsm = _ulsm;
1241 nob = offsetof (struct lov_stripe_md, lsm_oinfo[lsm->lsm_stripe_count]);
1245 if (cfs_copy_to_user (ulsm, lsm, sizeof(ulsm)))
1248 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1249 if (cfs_copy_to_user (ulsm->lsm_oinfo[i], lsm->lsm_oinfo[i],
1250 sizeof(lsm->lsm_oinfo[0])))
1257 echo_copyin_lsm (struct echo_device *ed, struct lov_stripe_md *lsm,
1258 void *ulsm, int ulsm_nob)
1260 struct echo_client_obd *ec = ed->ed_ec;
1263 if (ulsm_nob < sizeof (*lsm))
1266 if (cfs_copy_from_user (lsm, ulsm, sizeof (*lsm)))
1269 if (lsm->lsm_stripe_count > ec->ec_nstripes ||
1270 lsm->lsm_magic != LOV_MAGIC ||
1271 (lsm->lsm_stripe_size & (~CFS_PAGE_MASK)) != 0 ||
1272 ((__u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count > ~0UL))
1276 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1277 if (cfs_copy_from_user(lsm->lsm_oinfo[i],
1278 ((struct lov_stripe_md *)ulsm)-> \
1280 sizeof(lsm->lsm_oinfo[0])))
1286 static int echo_create_object(struct echo_device *ed, int on_target,
1287 struct obdo *oa, void *ulsm, int ulsm_nob,
1288 struct obd_trans_info *oti)
1290 struct echo_object *eco;
1291 struct echo_client_obd *ec = ed->ed_ec;
1292 struct lov_stripe_md *lsm = NULL;
1297 if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
1298 (on_target || /* set_stripe */
1299 ec->ec_nstripes != 0)) { /* LOV */
1300 CERROR ("No valid oid\n");
1304 rc = obd_alloc_memmd(ec->ec_exp, &lsm);
1306 CERROR("Cannot allocate md, rc = %d\n", rc);
1313 rc = echo_copyin_lsm (ed, lsm, ulsm, ulsm_nob);
1317 if (lsm->lsm_stripe_count == 0)
1318 lsm->lsm_stripe_count = ec->ec_nstripes;
1320 if (lsm->lsm_stripe_size == 0)
1321 lsm->lsm_stripe_size = CFS_PAGE_SIZE;
1325 /* setup stripes: indices + default ids if required */
1326 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1327 if (lsm->lsm_oinfo[i]->loi_id == 0)
1328 lsm->lsm_oinfo[i]->loi_id = lsm->lsm_object_id;
1330 lsm->lsm_oinfo[i]->loi_ost_idx =
1331 (idx + i) % ec->ec_nstripes;
1335 /* setup object ID here for !on_target and LOV hint */
1336 if (oa->o_valid & OBD_MD_FLID)
1337 lsm->lsm_object_id = oa->o_id;
1339 if (lsm->lsm_object_id == 0)
1340 lsm->lsm_object_id = ++last_object_id;
1344 /* Only echo objects are allowed to be created */
1345 LASSERT((oa->o_valid & OBD_MD_FLGROUP) &&
1346 (oa->o_seq == FID_SEQ_ECHO));
1347 rc = obd_create(ec->ec_exp, oa, &lsm, oti);
1349 CERROR("Cannot create objects, rc = %d\n", rc);
1355 /* See what object ID we were given */
1356 oa->o_id = lsm->lsm_object_id;
1357 oa->o_valid |= OBD_MD_FLID;
1359 eco = cl_echo_object_find(ed, &lsm);
1361 GOTO(failed, rc = PTR_ERR(eco));
1362 cl_echo_object_put(eco);
1364 CDEBUG(D_INFO, "oa->o_id = %lx\n", (long)oa->o_id);
1369 obd_destroy(ec->ec_exp, oa, lsm, oti, NULL, NULL);
1371 obd_free_memmd(ec->ec_exp, &lsm);
1373 CERROR("create object failed with rc = %d\n", rc);
1377 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
1380 struct echo_client_obd *ec = ed->ed_ec;
1381 struct lov_stripe_md *lsm = NULL;
1382 struct echo_object *eco;
1386 if ((oa->o_valid & OBD_MD_FLID) == 0 ||
1387 oa->o_id == 0) /* disallow use of object id 0 */
1389 CERROR ("No valid oid\n");
1393 rc = obd_alloc_memmd(ec->ec_exp, &lsm);
1397 lsm->lsm_object_id = oa->o_id;
1398 if (oa->o_valid & OBD_MD_FLGROUP)
1399 lsm->lsm_object_seq = oa->o_seq;
1401 lsm->lsm_object_seq = FID_SEQ_ECHO;
1404 eco = cl_echo_object_find(ed, &lsm);
1410 obd_free_memmd(ec->ec_exp, &lsm);
1414 static void echo_put_object(struct echo_object *eco)
1416 if (cl_echo_object_put(eco))
1417 CERROR("echo client: drop an object failed");
1421 echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
1423 unsigned long stripe_count;
1424 unsigned long stripe_size;
1425 unsigned long width;
1426 unsigned long woffset;
1430 if (lsm->lsm_stripe_count <= 1)
1434 stripe_size = lsm->lsm_stripe_size;
1435 stripe_count = lsm->lsm_stripe_count;
1437 /* width = # bytes in all stripes */
1438 width = stripe_size * stripe_count;
1440 /* woffset = offset within a width; offset = whole number of widths */
1441 woffset = do_div (offset, width);
1443 stripe_index = woffset / stripe_size;
1445 *idp = lsm->lsm_oinfo[stripe_index]->loi_id;
1446 *offp = offset * stripe_size + woffset % stripe_size;
1450 echo_client_page_debug_setup(struct lov_stripe_md *lsm,
1451 cfs_page_t *page, int rw, obd_id id,
1452 obd_off offset, obd_off count)
1459 /* no partial pages on the client */
1460 LASSERT(count == CFS_PAGE_SIZE);
1462 addr = cfs_kmap(page);
1464 for (delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
1465 if (rw == OBD_BRW_WRITE) {
1466 stripe_off = offset + delta;
1468 echo_get_stripe_off_id(lsm, &stripe_off, &stripe_id);
1470 stripe_off = 0xdeadbeef00c0ffeeULL;
1471 stripe_id = 0xdeadbeef00c0ffeeULL;
1473 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
1474 stripe_off, stripe_id);
1480 static int echo_client_page_debug_check(struct lov_stripe_md *lsm,
1481 cfs_page_t *page, obd_id id,
1482 obd_off offset, obd_off count)
1491 /* no partial pages on the client */
1492 LASSERT(count == CFS_PAGE_SIZE);
1494 addr = cfs_kmap(page);
1496 for (rc = delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
1497 stripe_off = offset + delta;
1499 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
1501 rc2 = block_debug_check("test_brw",
1502 addr + delta, OBD_ECHO_BLOCK_SIZE,
1503 stripe_off, stripe_id);
1505 CERROR ("Error in echo object "LPX64"\n", id);
1514 static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
1515 struct echo_object *eco, obd_off offset,
1516 obd_size count, int async,
1517 struct obd_trans_info *oti)
1519 struct echo_client_obd *ec = ed->ed_ec;
1520 struct lov_stripe_md *lsm = eco->eo_lsm;
1522 struct brw_page *pga;
1523 struct brw_page *pgp;
1533 verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
1534 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
1535 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
1537 gfp_mask = ((oa->o_id & 2) == 0) ? CFS_ALLOC_STD : CFS_ALLOC_HIGHUSER;
1539 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
1540 LASSERT(lsm != NULL);
1541 LASSERT(lsm->lsm_object_id == oa->o_id);
1544 (count & (~CFS_PAGE_MASK)) != 0)
1547 /* XXX think again with misaligned I/O */
1548 npages = count >> CFS_PAGE_SHIFT;
1550 if (rw == OBD_BRW_WRITE)
1551 brw_flags = OBD_BRW_ASYNC;
1553 OBD_ALLOC(pga, npages * sizeof(*pga));
1557 OBD_ALLOC(pages, npages * sizeof(*pages));
1558 if (pages == NULL) {
1559 OBD_FREE(pga, npages * sizeof(*pga));
1563 for (i = 0, pgp = pga, off = offset;
1565 i++, pgp++, off += CFS_PAGE_SIZE) {
1567 LASSERT (pgp->pg == NULL); /* for cleanup */
1570 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
1571 if (pgp->pg == NULL)
1575 pgp->count = CFS_PAGE_SIZE;
1577 pgp->flag = brw_flags;
1580 echo_client_page_debug_setup(lsm, pgp->pg, rw,
1581 oa->o_id, off, pgp->count);
1584 if (ed->ed_next == NULL) {
1585 struct obd_info oinfo = { { { 0 } } };
1588 rc = obd_brw(rw, ec->ec_exp, &oinfo, npages, pga, oti);
1590 rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
1593 if (rc != 0 || rw != OBD_BRW_READ)
1596 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
1597 if (pgp->pg == NULL)
1602 vrc = echo_client_page_debug_check(lsm, pgp->pg, oa->o_id,
1603 pgp->off, pgp->count);
1604 if (vrc != 0 && rc == 0)
1607 OBD_PAGE_FREE(pgp->pg);
1609 OBD_FREE(pga, npages * sizeof(*pga));
1610 OBD_FREE(pages, npages * sizeof(*pages));
1614 static int echo_client_prep_commit(struct obd_export *exp, int rw,
1615 struct obdo *oa, struct echo_object *eco,
1616 obd_off offset, obd_size count,
1617 obd_size batch, struct obd_trans_info *oti)
1619 struct lov_stripe_md *lsm = eco->eo_lsm;
1620 struct obd_ioobj ioo;
1621 struct niobuf_local *lnb;
1622 struct niobuf_remote *rnb;
1624 obd_size npages, tot_pages;
1628 if (count <= 0 || (count & (~CFS_PAGE_MASK)) != 0 ||
1629 (lsm != NULL && lsm->lsm_object_id != oa->o_id))
1632 npages = batch >> CFS_PAGE_SHIFT;
1633 tot_pages = count >> CFS_PAGE_SHIFT;
1635 OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
1636 OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
1638 if (lnb == NULL || rnb == NULL)
1639 GOTO(out, ret = -ENOMEM);
1641 obdo_to_ioobj(oa, &ioo);
1645 for(; tot_pages; tot_pages -= npages) {
1648 if (tot_pages < npages)
1651 for (i = 0; i < npages; i++, off += CFS_PAGE_SIZE) {
1652 rnb[i].offset = off;
1653 rnb[i].len = CFS_PAGE_SIZE;
1656 ioo.ioo_bufcnt = npages;
1657 oti->oti_transno = 0;
1660 ret = obd_preprw(rw, exp, oa, 1, &ioo, rnb, &lpages, lnb, oti,
1664 LASSERT(lpages == npages);
1666 for (i = 0; i < lpages; i++) {
1667 cfs_page_t *page = lnb[i].page;
1669 /* read past eof? */
1670 if (page == NULL && lnb[i].rc == 0)
1673 if (oa->o_id == ECHO_PERSISTENT_OBJID ||
1674 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
1675 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
1678 if (rw == OBD_BRW_WRITE)
1679 echo_client_page_debug_setup(lsm, page, rw,
1684 echo_client_page_debug_check(lsm, page,
1690 ret = obd_commitrw(rw, exp, oa, 1,&ioo,rnb,npages,lnb,oti,ret);
1694 /* Reset oti otherwise it would confuse ldiskfs. */
1695 memset(oti, 0, sizeof(*oti));
1700 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
1702 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
1706 static int echo_client_brw_ioctl(int rw, struct obd_export *exp,
1707 struct obd_ioctl_data *data)
1709 struct obd_device *obd = class_exp2obd(exp);
1710 struct echo_device *ed = obd2echo_dev(obd);
1711 struct echo_client_obd *ec = ed->ed_ec;
1712 struct obd_trans_info dummy_oti = { 0 };
1713 struct obdo *oa = &data->ioc_obdo1;
1714 struct echo_object *eco;
1719 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
1721 rc = echo_get_object(&eco, ed, oa);
1725 oa->o_valid &= ~OBD_MD_FLHANDLE;
1727 switch((long)data->ioc_pbuf1) {
1732 rc = echo_client_kbrw(ed, rw, oa,
1733 eco, data->ioc_offset,
1734 data->ioc_count, async, &dummy_oti);
1737 rc = echo_client_prep_commit(ec->ec_exp, rw, oa,
1738 eco, data->ioc_offset,
1739 data->ioc_count, data->ioc_plen1,
1745 echo_put_object(eco);
1750 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
1751 int mode, obd_off offset, obd_size nob)
1753 struct echo_device *ed = obd2echo_dev(exp->exp_obd);
1754 struct lustre_handle *ulh = &oa->o_handle;
1755 struct echo_object *eco;
1760 if (ed->ed_next == NULL)
1761 RETURN(-EOPNOTSUPP);
1763 if (!(mode == LCK_PR || mode == LCK_PW))
1766 if ((offset & (~CFS_PAGE_MASK)) != 0 ||
1767 (nob & (~CFS_PAGE_MASK)) != 0)
1770 rc = echo_get_object (&eco, ed, oa);
1774 end = (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
1775 rc = cl_echo_enqueue(eco, offset, end, mode, &ulh->cookie);
1777 oa->o_valid |= OBD_MD_FLHANDLE;
1778 CDEBUG(D_INFO, "Cookie is "LPX64"\n", ulh->cookie);
1780 echo_put_object(eco);
1785 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
1787 struct echo_device *ed = obd2echo_dev(exp->exp_obd);
1788 __u64 cookie = oa->o_handle.cookie;
1790 if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
1793 CDEBUG(D_INFO, "Cookie is "LPX64"\n", cookie);
1794 return cl_echo_cancel(ed, cookie);
1798 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
1799 int len, void *karg, void *uarg)
1801 struct obd_device *obd = exp->exp_obd;
1802 struct echo_device *ed = obd2echo_dev(obd);
1803 struct echo_client_obd *ec = ed->ed_ec;
1804 struct echo_object *eco;
1805 struct obd_ioctl_data *data = karg;
1806 struct obd_trans_info dummy_oti;
1807 struct oti_req_ack_lock *ack_lock;
1810 int rw = OBD_BRW_READ;
1815 #ifndef HAVE_UNLOCKED_IOCTL
1816 cfs_unlock_kernel();
1819 memset(&dummy_oti, 0, sizeof(dummy_oti));
1821 oa = &data->ioc_obdo1;
1822 if (!(oa->o_valid & OBD_MD_FLGROUP)) {
1823 oa->o_valid |= OBD_MD_FLGROUP;
1824 oa->o_seq = FID_SEQ_ECHO;
1827 /* This FID is unpacked just for validation at this point */
1828 rc = fid_ostid_unpack(&fid, &oa->o_oi, 0);
1833 case OBD_IOC_CREATE: /* may create echo object */
1834 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1835 GOTO (out, rc = -EPERM);
1837 rc = echo_create_object (ed, 1, oa,
1838 data->ioc_pbuf1, data->ioc_plen1,
1842 case OBD_IOC_DESTROY:
1843 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1844 GOTO (out, rc = -EPERM);
1846 rc = echo_get_object (&eco, ed, oa);
1848 rc = obd_destroy(ec->ec_exp, oa, eco->eo_lsm,
1849 &dummy_oti, NULL, NULL);
1851 eco->eo_deleted = 1;
1852 echo_put_object(eco);
1856 case OBD_IOC_GETATTR:
1857 rc = echo_get_object (&eco, ed, oa);
1859 struct obd_info oinfo = { { { 0 } } };
1860 oinfo.oi_md = eco->eo_lsm;
1862 rc = obd_getattr(ec->ec_exp, &oinfo);
1863 echo_put_object(eco);
1867 case OBD_IOC_SETATTR:
1868 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1869 GOTO (out, rc = -EPERM);
1871 rc = echo_get_object (&eco, ed, oa);
1873 struct obd_info oinfo = { { { 0 } } };
1875 oinfo.oi_md = eco->eo_lsm;
1877 rc = obd_setattr(ec->ec_exp, &oinfo, NULL);
1878 echo_put_object(eco);
1882 case OBD_IOC_BRW_WRITE:
1883 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1884 GOTO (out, rc = -EPERM);
1888 case OBD_IOC_BRW_READ:
1889 rc = echo_client_brw_ioctl(rw, exp, data);
1892 case ECHO_IOC_GET_STRIPE:
1893 rc = echo_get_object(&eco, ed, oa);
1895 rc = echo_copyout_lsm(eco->eo_lsm, data->ioc_pbuf1,
1897 echo_put_object(eco);
1901 case ECHO_IOC_SET_STRIPE:
1902 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1903 GOTO (out, rc = -EPERM);
1905 if (data->ioc_pbuf1 == NULL) { /* unset */
1906 rc = echo_get_object(&eco, ed, oa);
1908 eco->eo_deleted = 1;
1909 echo_put_object(eco);
1912 rc = echo_create_object(ed, 0, oa,
1914 data->ioc_plen1, &dummy_oti);
1918 case ECHO_IOC_ENQUEUE:
1919 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1920 GOTO (out, rc = -EPERM);
1922 rc = echo_client_enqueue(exp, oa,
1923 data->ioc_conn1, /* lock mode */
1925 data->ioc_count);/*extent*/
1928 case ECHO_IOC_CANCEL:
1929 rc = echo_client_cancel(exp, oa);
1933 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
1934 GOTO (out, rc = -ENOTTY);
1940 /* XXX this should be in a helper also called by target_send_reply */
1941 for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
1943 if (!ack_lock->mode)
1945 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
1948 #ifndef HAVE_UNLOCKED_IOCTL
1955 static int echo_client_setup(struct obd_device *obddev, struct lustre_cfg *lcfg)
1957 struct echo_client_obd *ec = &obddev->u.echo_client;
1958 struct obd_device *tgt;
1959 struct obd_uuid echo_uuid = { "ECHO_UUID" };
1960 struct obd_connect_data *ocd = NULL;
1964 if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1965 CERROR("requires a TARGET OBD name\n");
1969 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
1970 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
1971 CERROR("device not attached or not set up (%s)\n",
1972 lustre_cfg_string(lcfg, 1));
1976 cfs_spin_lock_init (&ec->ec_lock);
1977 CFS_INIT_LIST_HEAD (&ec->ec_objects);
1978 CFS_INIT_LIST_HEAD (&ec->ec_locks);
1980 ec->ec_nstripes = 0;
1982 OBD_ALLOC(ocd, sizeof(*ocd));
1984 CERROR("Can't alloc ocd connecting to %s\n",
1985 lustre_cfg_string(lcfg, 1));
1989 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
1990 OBD_CONNECT_GRANT | OBD_CONNECT_FULL20 |
1991 OBD_CONNECT_64BITHASH;
1992 ocd->ocd_version = LUSTRE_VERSION_CODE;
1993 ocd->ocd_group = FID_SEQ_ECHO;
1995 rc = obd_connect(NULL, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
1997 /* Turn off pinger because it connects to tgt obd directly. */
1998 cfs_spin_lock(&tgt->obd_dev_lock);
1999 cfs_list_del_init(&ec->ec_exp->exp_obd_chain_timed);
2000 cfs_spin_unlock(&tgt->obd_dev_lock);
2003 OBD_FREE(ocd, sizeof(*ocd));
2006 CERROR("fail to connect to device %s\n",
2007 lustre_cfg_string(lcfg, 1));
2014 static int echo_client_cleanup(struct obd_device *obddev)
2016 struct echo_client_obd *ec = &obddev->u.echo_client;
2020 if (!cfs_list_empty(&obddev->obd_exports)) {
2021 CERROR("still has clients!\n");
2025 LASSERT(cfs_atomic_read(&ec->ec_exp->exp_refcount) > 0);
2026 rc = obd_disconnect(ec->ec_exp);
2028 CERROR("fail to disconnect device: %d\n", rc);
2033 static int echo_client_connect(const struct lu_env *env,
2034 struct obd_export **exp,
2035 struct obd_device *src, struct obd_uuid *cluuid,
2036 struct obd_connect_data *data, void *localdata)
2039 struct lustre_handle conn = { 0 };
2042 rc = class_connect(&conn, src, cluuid);
2044 *exp = class_conn2export(&conn);
2050 static int echo_client_disconnect(struct obd_export *exp)
2053 struct obd_device *obd;
2054 struct echo_client_obd *ec;
2055 struct ec_lock *ecl;
2061 GOTO(out, rc = -EINVAL);
2065 ec = &obd->u.echo_client;
2067 /* no more contention on export's lock list */
2068 while (!cfs_list_empty (&exp->exp_ec_data.eced_locks)) {
2069 ecl = cfs_list_entry (exp->exp_ec_data.eced_locks.next,
2070 struct ec_lock, ecl_exp_chain);
2071 cfs_list_del (&ecl->ecl_exp_chain);
2073 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm,
2074 ecl->ecl_mode, &ecl->ecl_lock_handle);
2076 CDEBUG (D_INFO, "Cancel lock on object "LPX64" on disconnect "
2077 "(%d)\n", ecl->ecl_object->eco_id, rc);
2079 echo_put_object (ecl->ecl_object);
2080 OBD_FREE (ecl, sizeof (*ecl));
2084 rc = class_disconnect(exp);
2090 static struct obd_ops echo_obd_ops = {
2091 .o_owner = THIS_MODULE,
2094 .o_setup = echo_client_setup,
2095 .o_cleanup = echo_client_cleanup,
2098 .o_iocontrol = echo_client_iocontrol,
2099 .o_connect = echo_client_connect,
2100 .o_disconnect = echo_client_disconnect
2103 int echo_client_init(void)
2105 struct lprocfs_static_vars lvars = { 0 };
2108 lprocfs_echo_init_vars(&lvars);
2110 rc = lu_kmem_init(echo_caches);
2112 rc = class_register_type(&echo_obd_ops, NULL,
2114 LUSTRE_ECHO_CLIENT_NAME,
2117 lu_kmem_fini(echo_caches);
2122 void echo_client_exit(void)
2124 class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
2125 lu_kmem_fini(echo_caches);
2128 /** @} echo_client */