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 = FID_SEQ_ECHO;
173 /* truncated to 32 bits by assignment */
174 fid->f_oid = lsm->lsm_object_id;
175 fid->f_ver = lsm->lsm_object_id >> 32;
177 /** @} echo_helpers */
179 static struct echo_object *cl_echo_object_find(struct echo_device *d,
180 struct lov_stripe_md **lsm);
181 static int cl_echo_object_put(struct echo_object *eco);
182 static int cl_echo_enqueue (struct echo_object *eco, obd_off start,
183 obd_off end, int mode, __u64 *cookie);
184 static int cl_echo_cancel (struct echo_device *d, __u64 cookie);
185 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
186 cfs_page_t **pages, int npages, int async);
188 static struct echo_thread_info *echo_env_info(const struct lu_env *env);
190 struct echo_thread_info {
191 struct echo_object_conf eti_conf;
192 struct lustre_md eti_md;
194 struct cl_2queue eti_queue;
196 struct cl_lock_descr eti_descr;
197 struct lu_fid eti_fid;
200 /* No session used right now */
201 struct echo_session_info {
205 static cfs_mem_cache_t *echo_page_kmem;
206 static cfs_mem_cache_t *echo_lock_kmem;
207 static cfs_mem_cache_t *echo_object_kmem;
208 static cfs_mem_cache_t *echo_thread_kmem;
209 static cfs_mem_cache_t *echo_session_kmem;
210 //static cfs_mem_cache_t *echo_req_kmem;
212 static struct lu_kmem_descr echo_caches[] = {
214 .ckd_cache = &echo_page_kmem,
215 .ckd_name = "echo_page_kmem",
216 .ckd_size = sizeof (struct echo_page)
219 .ckd_cache = &echo_lock_kmem,
220 .ckd_name = "echo_lock_kmem",
221 .ckd_size = sizeof (struct echo_lock)
224 .ckd_cache = &echo_object_kmem,
225 .ckd_name = "echo_object_kmem",
226 .ckd_size = sizeof (struct echo_object)
229 .ckd_cache = &echo_thread_kmem,
230 .ckd_name = "echo_thread_kmem",
231 .ckd_size = sizeof (struct echo_thread_info)
234 .ckd_cache = &echo_session_kmem,
235 .ckd_name = "echo_session_kmem",
236 .ckd_size = sizeof (struct echo_session_info)
240 .ckd_cache = &echo_req_kmem,
241 .ckd_name = "echo_req_kmem",
242 .ckd_size = sizeof (struct echo_req)
250 /** \defgroup echo_page Page operations
252 * Echo page operations.
256 cfs_page_t *echo_page_vmpage(const struct lu_env *env,
257 const struct cl_page_slice *slice)
259 return cl2echo_page(slice)->ep_vmpage;
262 static void echo_page_discard(const struct lu_env *env,
263 const struct cl_page_slice *slice,
264 struct cl_io *unused)
266 cl_page_delete(env, slice->cpl_page);
269 static int echo_page_is_vmlocked(const struct lu_env *env,
270 const struct cl_page_slice *slice)
275 static void echo_page_completion(const struct lu_env *env,
276 const struct cl_page_slice *slice,
279 LASSERT(slice->cpl_page->cp_sync_io != NULL);
282 static void echo_page_fini(const struct lu_env *env,
283 struct cl_page_slice *slice)
285 struct echo_page *ep = cl2echo_page(slice);
286 struct echo_object *eco = cl2echo_obj(slice->cpl_obj);
287 cfs_page_t *vmpage = ep->ep_vmpage;
290 cfs_atomic_dec(&eco->eo_npages);
291 page_cache_release(vmpage);
292 OBD_SLAB_FREE_PTR(ep, echo_page_kmem);
296 static int echo_page_prep(const struct lu_env *env,
297 const struct cl_page_slice *slice,
298 struct cl_io *unused)
303 static int echo_page_print(const struct lu_env *env,
304 const struct cl_page_slice *slice,
305 void *cookie, lu_printer_t printer)
307 struct echo_page *ep = cl2echo_page(slice);
309 (*printer)(env, cookie, LUSTRE_ECHO_CLIENT_NAME"-page@%p vm@%p\n",
314 static const struct cl_page_operations echo_page_ops = {
315 .cpo_discard = echo_page_discard,
316 .cpo_vmpage = echo_page_vmpage,
317 .cpo_fini = echo_page_fini,
318 .cpo_print = echo_page_print,
319 .cpo_is_vmlocked = echo_page_is_vmlocked,
322 .cpo_prep = echo_page_prep,
323 .cpo_completion = echo_page_completion,
326 .cpo_prep = echo_page_prep,
327 .cpo_completion = echo_page_completion,
333 /** \defgroup echo_lock Locking
335 * echo lock operations
339 static void echo_lock_fini(const struct lu_env *env,
340 struct cl_lock_slice *slice)
342 struct echo_lock *ecl = cl2echo_lock(slice);
344 LASSERT(cfs_list_empty(&ecl->el_chain));
345 OBD_SLAB_FREE_PTR(ecl, echo_lock_kmem);
348 static void echo_lock_delete(const struct lu_env *env,
349 const struct cl_lock_slice *slice)
351 struct echo_lock *ecl = cl2echo_lock(slice);
353 LASSERT(cfs_list_empty(&ecl->el_chain));
356 static int echo_lock_fits_into(const struct lu_env *env,
357 const struct cl_lock_slice *slice,
358 const struct cl_lock_descr *need,
359 const struct cl_io *unused)
364 static struct cl_lock_operations echo_lock_ops = {
365 .clo_fini = echo_lock_fini,
366 .clo_delete = echo_lock_delete,
367 .clo_fits_into = echo_lock_fits_into
372 /** \defgroup echo_cl_ops cl_object operations
374 * operations for cl_object
378 static struct cl_page *echo_page_init(const struct lu_env *env,
379 struct cl_object *obj,
380 struct cl_page *page, cfs_page_t *vmpage)
382 struct echo_page *ep;
385 OBD_SLAB_ALLOC_PTR_GFP(ep, echo_page_kmem, CFS_ALLOC_IO);
387 struct echo_object *eco = cl2echo_obj(obj);
388 ep->ep_vmpage = vmpage;
389 page_cache_get(vmpage);
390 cl_page_slice_add(page, &ep->ep_cl, obj, &echo_page_ops);
391 cfs_atomic_inc(&eco->eo_npages);
393 RETURN(ERR_PTR(ep ? 0 : -ENOMEM));
396 static int echo_io_init(const struct lu_env *env, struct cl_object *obj,
402 static int echo_lock_init(const struct lu_env *env,
403 struct cl_object *obj, struct cl_lock *lock,
404 const struct cl_io *unused)
406 struct echo_lock *el;
409 OBD_SLAB_ALLOC_PTR_GFP(el, echo_lock_kmem, CFS_ALLOC_IO);
411 cl_lock_slice_add(lock, &el->el_cl, obj, &echo_lock_ops);
412 el->el_object = cl2echo_obj(obj);
413 CFS_INIT_LIST_HEAD(&el->el_chain);
414 cfs_atomic_set(&el->el_refcount, 0);
416 RETURN(el == NULL ? -ENOMEM : 0);
419 static int echo_conf_set(const struct lu_env *env, struct cl_object *obj,
420 const struct cl_object_conf *conf)
425 static const struct cl_object_operations echo_cl_obj_ops = {
426 .coo_page_init = echo_page_init,
427 .coo_lock_init = echo_lock_init,
428 .coo_io_init = echo_io_init,
429 .coo_conf_set = echo_conf_set
431 /** @} echo_cl_ops */
433 /** \defgroup echo_lu_ops lu_object operations
435 * operations for echo lu object.
439 static int echo_object_init(const struct lu_env *env, struct lu_object *obj,
440 const struct lu_object_conf *conf)
442 const struct cl_object_conf *cconf = lu2cl_conf(conf);
443 struct echo_object_conf *econf = cl2echo_conf(cconf);
444 struct echo_device *ed = cl2echo_dev(lu2cl_dev(obj->lo_dev));
445 struct echo_client_obd *ec = ed->ed_ec;
446 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
450 struct lu_object *below;
451 struct lu_device *under;
454 below = under->ld_ops->ldo_object_alloc(env, obj->lo_header,
458 lu_object_add(obj, below);
461 LASSERT(econf->eoc_md);
462 eco->eo_lsm = *econf->eoc_md;
464 cfs_atomic_set(&eco->eo_npages, 0);
466 /* clear the lsm pointer so that it won't get freed. */
467 *econf->eoc_md = NULL;
469 cfs_spin_lock(&ec->ec_lock);
470 cfs_list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
471 cfs_spin_unlock(&ec->ec_lock);
476 static void echo_object_free(const struct lu_env *env, struct lu_object *obj)
478 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
479 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
480 struct lov_stripe_md *lsm = eco->eo_lsm;
483 LASSERT(cfs_atomic_read(&eco->eo_npages) == 0);
485 cfs_spin_lock(&ec->ec_lock);
486 cfs_list_del_init(&eco->eo_obj_chain);
487 cfs_spin_unlock(&ec->ec_lock);
490 lu_object_header_fini(obj->lo_header);
493 obd_free_memmd(ec->ec_exp, &lsm);
494 OBD_SLAB_FREE_PTR(eco, echo_object_kmem);
498 static int echo_object_print(const struct lu_env *env, void *cookie,
499 lu_printer_t p, const struct lu_object *o)
501 struct echo_object *obj = cl2echo_obj(lu2cl(o));
503 return (*p)(env, cookie, "echoclient-object@%p", obj);
507 static const struct lu_object_operations echo_lu_obj_ops = {
508 .loo_object_init = echo_object_init,
509 .loo_object_delete = NULL,
510 .loo_object_release = NULL,
511 .loo_object_free = echo_object_free,
512 .loo_object_print = echo_object_print,
513 .loo_object_invariant = NULL
515 /** @} echo_lu_ops */
517 /** \defgroup echo_lu_dev_ops lu_device operations
519 * Operations for echo lu device.
523 static struct lu_object *echo_object_alloc(const struct lu_env *env,
524 const struct lu_object_header *hdr,
525 struct lu_device *dev)
527 struct echo_object *eco;
528 struct lu_object *obj = NULL;
531 /* we're the top dev. */
532 LASSERT(hdr == NULL);
533 OBD_SLAB_ALLOC_PTR_GFP(eco, echo_object_kmem, CFS_ALLOC_IO);
535 struct cl_object_header *hdr = &eco->eo_hdr;
537 obj = &echo_obj2cl(eco)->co_lu;
538 cl_object_header_init(hdr);
539 lu_object_init(obj, &hdr->coh_lu, dev);
540 lu_object_add_top(&hdr->coh_lu, obj);
542 eco->eo_cl.co_ops = &echo_cl_obj_ops;
543 obj->lo_ops = &echo_lu_obj_ops;
548 static struct lu_device_operations echo_device_lu_ops = {
549 .ldo_object_alloc = echo_object_alloc,
551 /** @} echo_lu_dev_ops */
553 static struct cl_device_operations echo_device_cl_ops = {
556 /** \defgroup echo_init Setup and teardown
558 * Init and fini functions for echo client.
562 static int echo_site_init(const struct lu_env *env, struct echo_device *ed)
564 struct cl_site *site = &ed->ed_site_myself;
567 /* initialize site */
568 rc = cl_site_init(site, &ed->ed_cl);
570 CERROR("Cannot initilize site for echo client(%d)\n", rc);
574 rc = lu_site_init_finish(&site->cs_lu);
582 static void echo_site_fini(const struct lu_env *env, struct echo_device *ed)
585 cl_site_fini(ed->ed_site);
590 static void *echo_thread_key_init(const struct lu_context *ctx,
591 struct lu_context_key *key)
593 struct echo_thread_info *info;
595 OBD_SLAB_ALLOC_PTR_GFP(info, echo_thread_kmem, CFS_ALLOC_IO);
597 info = ERR_PTR(-ENOMEM);
601 static void echo_thread_key_fini(const struct lu_context *ctx,
602 struct lu_context_key *key, void *data)
604 struct echo_thread_info *info = data;
605 OBD_SLAB_FREE_PTR(info, echo_thread_kmem);
608 static void echo_thread_key_exit(const struct lu_context *ctx,
609 struct lu_context_key *key, void *data)
613 static struct lu_context_key echo_thread_key = {
614 .lct_tags = LCT_CL_THREAD,
615 .lct_init = echo_thread_key_init,
616 .lct_fini = echo_thread_key_fini,
617 .lct_exit = echo_thread_key_exit
620 static void *echo_session_key_init(const struct lu_context *ctx,
621 struct lu_context_key *key)
623 struct echo_session_info *session;
625 OBD_SLAB_ALLOC_PTR_GFP(session, echo_session_kmem, CFS_ALLOC_IO);
627 session = ERR_PTR(-ENOMEM);
631 static void echo_session_key_fini(const struct lu_context *ctx,
632 struct lu_context_key *key, void *data)
634 struct echo_session_info *session = data;
635 OBD_SLAB_FREE_PTR(session, echo_session_kmem);
638 static void echo_session_key_exit(const struct lu_context *ctx,
639 struct lu_context_key *key, void *data)
643 static struct lu_context_key echo_session_key = {
644 .lct_tags = LCT_SESSION,
645 .lct_init = echo_session_key_init,
646 .lct_fini = echo_session_key_fini,
647 .lct_exit = echo_session_key_exit
650 LU_TYPE_INIT_FINI(echo, &echo_thread_key, &echo_session_key);
652 static struct lu_device *echo_device_alloc(const struct lu_env *env,
653 struct lu_device_type *t,
654 struct lustre_cfg *cfg)
656 struct lu_device *next;
657 struct echo_device *ed;
658 struct cl_device *cd;
659 struct obd_device *obd = NULL; /* to keep compiler happy */
660 struct obd_device *tgt;
661 const char *tgt_type_name;
668 GOTO(out, rc = -ENOMEM);
672 rc = cl_device_init(cd, t);
676 cd->cd_lu_dev.ld_ops = &echo_device_lu_ops;
677 cd->cd_ops = &echo_device_cl_ops;
680 rc = echo_site_init(env, ed);
685 obd = class_name2obd(lustre_cfg_string(cfg, 0));
686 LASSERT(obd != NULL);
687 rc = echo_client_setup(obd, cfg);
690 ed->ed_ec = &obd->u.echo_client;
693 tgt = class_name2obd(lustre_cfg_string(cfg, 1));
694 LASSERT(tgt != NULL);
695 next = tgt->obd_lu_dev;
696 if (next != NULL && !lu_device_is_cl(next))
700 * if echo client is to be stacked upon ost device, the next is NULL
701 * since ost is not a clio device so far
703 tgt_type_name = tgt->obd_type->typ_name;
705 LASSERT(next != NULL);
706 if (next->ld_site != NULL)
707 GOTO(out, rc = -EBUSY);
709 next->ld_site = &ed->ed_site->cs_lu;
710 rc = next->ld_type->ldt_ops->ldto_device_init(env, next,
711 next->ld_type->ldt_name, NULL);
715 /* Trikcy case, I have to determine the obd type since clio
716 * uses the different parameters to initialize objects for
719 if (strcmp(tgt_type_name, LUSTRE_LOV_NAME) == 0)
720 ed->ed_next_islov = 1;
722 LASSERT(strcmp(tgt_type_name, LUSTRE_OSC_NAME) == 0);
724 LASSERT(strcmp(tgt_type_name, LUSTRE_OST_NAME) == 0);
727 RETURN(&cd->cd_lu_dev);
733 rc2 = echo_client_cleanup(obd);
735 CERROR("Cleanup obd device %s error(%d)\n",
740 echo_site_fini(env, ed);
742 cl_device_fini(&ed->ed_cl);
752 static int echo_device_init(const struct lu_env *env, struct lu_device *d,
753 const char *name, struct lu_device *next)
759 static struct lu_device *echo_device_fini(const struct lu_env *env,
762 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
763 struct lu_device *next = ed->ed_next;
766 next = next->ld_type->ldt_ops->ldto_device_fini(env, next);
770 static void echo_lock_release(const struct lu_env *env,
771 struct echo_lock *ecl,
774 struct cl_lock *clk = echo_lock2cl(ecl);
778 cl_lock_release(env, clk, "ec enqueue", ecl->el_object);
780 cl_lock_mutex_get(env, clk);
781 cl_lock_cancel(env, clk);
782 cl_lock_delete(env, clk);
783 cl_lock_mutex_put(env, clk);
785 cl_lock_put(env, clk);
788 static struct lu_device *echo_device_free(const struct lu_env *env,
791 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
792 struct echo_client_obd *ec = ed->ed_ec;
793 struct echo_object *eco;
794 struct lu_device *next = ed->ed_next;
796 CDEBUG(D_INFO, "echo device:%p is going to be freed, next = %p\n", ed, next);
799 cfs_spin_lock(&ec->ec_lock);
800 while (!cfs_list_empty(&ec->ec_locks)) {
801 struct echo_lock *ecl = cfs_list_entry(ec->ec_locks.next,
806 if (cfs_atomic_dec_and_test(&ecl->el_refcount))
807 cfs_list_del_init(&ecl->el_chain);
810 cfs_spin_unlock(&ec->ec_lock);
812 CERROR("echo client: pending lock %p refs %d\n",
813 ecl, cfs_atomic_read(&ecl->el_refcount));
815 echo_lock_release(env, ecl, still_used);
816 cfs_spin_lock(&ec->ec_lock);
818 cfs_spin_unlock(&ec->ec_lock);
820 LASSERT(ed->ed_site);
821 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
823 /* check if there are objects still alive.
824 * It shouldn't have any object because lu_site_purge would cleanup
825 * all of cached objects. Anyway, probably the echo device is being
826 * parallelly accessed.
828 cfs_spin_lock(&ec->ec_lock);
829 cfs_list_for_each_entry(eco, &ec->ec_objects, eo_obj_chain)
831 cfs_spin_unlock(&ec->ec_lock);
834 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
837 "Waiting for the reference of echo object to be dropped\n");
839 /* Wait for the last reference to be dropped. */
840 cfs_spin_lock(&ec->ec_lock);
841 while (!cfs_list_empty(&ec->ec_objects)) {
842 cfs_spin_unlock(&ec->ec_lock);
843 CERROR("echo_client still has objects at cleanup time, "
844 "wait for 1 second\n");
845 cfs_schedule_timeout_and_set_state(CFS_TASK_UNINT,
846 cfs_time_seconds(1));
847 cfs_spin_lock(&ec->ec_lock);
849 cfs_spin_unlock(&ec->ec_lock);
851 CDEBUG(D_INFO, "No object exists, exiting...\n");
853 echo_client_cleanup(d->ld_obd);
856 next = next->ld_type->ldt_ops->ldto_device_free(env, next);
858 LASSERT(ed->ed_site == lu2cl_site(d->ld_site));
859 echo_site_fini(env, ed);
860 cl_device_fini(&ed->ed_cl);
866 static const struct lu_device_type_operations echo_device_type_ops = {
867 .ldto_init = echo_type_init,
868 .ldto_fini = echo_type_fini,
870 .ldto_start = echo_type_start,
871 .ldto_stop = echo_type_stop,
873 .ldto_device_alloc = echo_device_alloc,
874 .ldto_device_free = echo_device_free,
875 .ldto_device_init = echo_device_init,
876 .ldto_device_fini = echo_device_fini
879 static struct lu_device_type echo_device_type = {
880 .ldt_tags = LU_DEVICE_CL,
881 .ldt_name = LUSTRE_ECHO_CLIENT_NAME,
882 .ldt_ops = &echo_device_type_ops,
883 .ldt_ctx_tags = LCT_CL_THREAD
887 /** \defgroup echo_exports Exported operations
889 * exporting functions to echo client
894 /* Interfaces to echo client obd device */
895 static struct echo_object *cl_echo_object_find(struct echo_device *d,
896 struct lov_stripe_md **lsmp)
899 struct echo_thread_info *info;
900 struct echo_object_conf *conf;
901 struct lov_stripe_md *lsm;
902 struct echo_object *eco;
903 struct cl_object *obj;
911 LASSERT(lsm->lsm_object_id);
913 /* Never return an object if the obd is to be freed. */
914 if (echo_dev2cl(d)->cd_lu_dev.ld_obd->obd_stopping)
915 RETURN(ERR_PTR(-ENODEV));
917 env = cl_env_get(&refcheck);
921 info = echo_env_info(env);
922 conf = &info->eti_conf;
924 if (!d->ed_next_islov) {
925 struct lov_oinfo *oinfo = lsm->lsm_oinfo[0];
926 LASSERT(oinfo != NULL);
927 oinfo->loi_id = lsm->lsm_object_id;
928 oinfo->loi_seq = lsm->lsm_object_seq;
929 conf->eoc_cl.u.coc_oinfo = oinfo;
931 struct lustre_md *md;
933 memset(md, 0, sizeof *md);
935 conf->eoc_cl.u.coc_md = md;
940 fid = &info->eti_fid;
943 obj = cl_object_find(env, echo_dev2cl(d), fid, &conf->eoc_cl);
945 GOTO(out, eco = (void*)obj);
947 eco = cl2echo_obj(obj);
948 if (eco->eo_deleted) {
949 cl_object_put(env, obj);
950 eco = ERR_PTR(-EAGAIN);
954 cl_env_put(env, &refcheck);
958 static int cl_echo_object_put(struct echo_object *eco)
961 struct cl_object *obj = echo_obj2cl(eco);
965 env = cl_env_get(&refcheck);
967 RETURN(PTR_ERR(env));
969 /* an external function to kill an object? */
970 if (eco->eo_deleted) {
971 struct lu_object_header *loh = obj->co_lu.lo_header;
972 LASSERT(&eco->eo_hdr == luh2coh(loh));
973 cfs_set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
974 cl_object_prune(env, obj);
977 cl_object_put(env, obj);
978 cl_env_put(env, &refcheck);
982 static int cl_echo_enqueue0(struct lu_env *env, struct echo_object *eco,
983 obd_off start, obd_off end, int mode,
984 __u64 *cookie , __u32 enqflags)
988 struct cl_object *obj;
989 struct cl_lock_descr *descr;
990 struct echo_thread_info *info;
994 info = echo_env_info(env);
996 descr = &info->eti_descr;
997 obj = echo_obj2cl(eco);
999 descr->cld_obj = obj;
1000 descr->cld_start = cl_index(obj, start);
1001 descr->cld_end = cl_index(obj, end);
1002 descr->cld_mode = mode == LCK_PW ? CLM_WRITE : CLM_READ;
1003 descr->cld_enq_flags = enqflags;
1006 lck = cl_lock_request(env, io, descr, "ec enqueue", eco);
1008 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
1009 struct echo_lock *el;
1011 rc = cl_wait(env, lck);
1013 el = cl2echo_lock(cl_lock_at(lck, &echo_device_type));
1014 cfs_spin_lock(&ec->ec_lock);
1015 if (cfs_list_empty(&el->el_chain)) {
1016 cfs_list_add(&el->el_chain, &ec->ec_locks);
1017 el->el_cookie = ++ec->ec_unique;
1019 cfs_atomic_inc(&el->el_refcount);
1020 *cookie = el->el_cookie;
1021 cfs_spin_unlock(&ec->ec_lock);
1023 cl_lock_release(env, lck, "ec enqueue", cfs_current());
1028 static int cl_echo_enqueue(struct echo_object *eco, obd_off start, obd_off end,
1029 int mode, __u64 *cookie)
1031 struct echo_thread_info *info;
1038 env = cl_env_get(&refcheck);
1040 RETURN(PTR_ERR(env));
1042 info = echo_env_info(env);
1045 result = cl_io_init(env, io, CIT_MISC, echo_obj2cl(eco));
1048 LASSERT(result == 0);
1050 result = cl_echo_enqueue0(env, eco, start, end, mode, cookie, 0);
1051 cl_io_fini(env, io);
1055 cl_env_put(env, &refcheck);
1059 static int cl_echo_cancel0(struct lu_env *env, struct echo_device *ed,
1062 struct echo_client_obd *ec = ed->ed_ec;
1063 struct echo_lock *ecl = NULL;
1065 int found = 0, still_used = 0;
1068 LASSERT(ec != NULL);
1069 cfs_spin_lock (&ec->ec_lock);
1070 cfs_list_for_each (el, &ec->ec_locks) {
1071 ecl = cfs_list_entry (el, struct echo_lock, el_chain);
1072 CDEBUG(D_INFO, "ecl: %p, cookie: "LPX64"\n", ecl, ecl->el_cookie);
1073 found = (ecl->el_cookie == cookie);
1075 if (cfs_atomic_dec_and_test(&ecl->el_refcount))
1076 cfs_list_del_init(&ecl->el_chain);
1082 cfs_spin_unlock (&ec->ec_lock);
1087 echo_lock_release(env, ecl, still_used);
1091 static int cl_echo_cancel(struct echo_device *ed, __u64 cookie)
1098 env = cl_env_get(&refcheck);
1100 RETURN(PTR_ERR(env));
1102 rc = cl_echo_cancel0(env, ed, cookie);
1104 cl_env_put(env, &refcheck);
1108 static int cl_echo_async_brw(const struct lu_env *env, struct cl_io *io,
1109 enum cl_req_type unused, struct cl_2queue *queue)
1111 struct cl_page *clp;
1112 struct cl_page *temp;
1116 cl_page_list_for_each_safe(clp, temp, &queue->c2_qin) {
1118 rc = cl_page_cache_add(env, io, clp, CRT_WRITE);
1121 result = result ?: rc;
1126 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
1127 cfs_page_t **pages, int npages, int async)
1130 struct echo_thread_info *info;
1131 struct cl_object *obj = echo_obj2cl(eco);
1132 struct echo_device *ed = eco->eo_dev;
1133 struct cl_2queue *queue;
1135 struct cl_page *clp;
1136 struct lustre_handle lh = { 0 };
1137 int page_size = cl_page_size(obj);
1143 LASSERT((offset & ~CFS_PAGE_MASK) == 0);
1144 LASSERT(ed->ed_next != NULL);
1145 env = cl_env_get(&refcheck);
1147 RETURN(PTR_ERR(env));
1149 info = echo_env_info(env);
1151 queue = &info->eti_queue;
1153 cl_2queue_init(queue);
1154 rc = cl_io_init(env, io, CIT_MISC, obj);
1160 rc = cl_echo_enqueue0(env, eco, offset,
1161 offset + npages * CFS_PAGE_SIZE - 1,
1162 rw == READ ? LCK_PR : LCK_PW, &lh.cookie,
1165 GOTO(error_lock, rc);
1167 for (i = 0; i < npages; i++) {
1169 clp = cl_page_find(env, obj, cl_index(obj, offset),
1170 pages[i], CPT_TRANSIENT);
1175 LASSERT(clp->cp_type == CPT_TRANSIENT);
1177 rc = cl_page_own(env, io, clp);
1179 LASSERT(clp->cp_state == CPS_FREEING);
1180 cl_page_put(env, clp);
1184 cl_2queue_add(queue, clp);
1186 /* drop the reference count for cl_page_find, so that the page
1187 * will be freed in cl_2queue_fini. */
1188 cl_page_put(env, clp);
1189 cl_page_clip(env, clp, 0, page_size);
1191 offset += page_size;
1195 enum cl_req_type typ = rw == READ ? CRT_READ : CRT_WRITE;
1197 async = async && (typ == CRT_WRITE);
1199 rc = cl_echo_async_brw(env, io, typ, queue);
1201 rc = cl_io_submit_sync(env, io, typ, queue,
1203 CDEBUG(D_INFO, "echo_client %s write returns %d\n",
1204 async ? "async" : "sync", rc);
1207 cl_echo_cancel0(env, ed, lh.cookie);
1210 cl_2queue_discard(env, io, queue);
1211 cl_2queue_disown(env, io, queue);
1212 cl_2queue_fini(env, queue);
1213 cl_io_fini(env, io);
1215 cl_env_put(env, &refcheck);
1218 /** @} echo_exports */
1221 static obd_id last_object_id;
1224 echo_copyout_lsm (struct lov_stripe_md *lsm, void *_ulsm, int ulsm_nob)
1226 struct lov_stripe_md *ulsm = _ulsm;
1229 nob = offsetof (struct lov_stripe_md, lsm_oinfo[lsm->lsm_stripe_count]);
1233 if (cfs_copy_to_user (ulsm, lsm, sizeof(ulsm)))
1236 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1237 if (cfs_copy_to_user (ulsm->lsm_oinfo[i], lsm->lsm_oinfo[i],
1238 sizeof(lsm->lsm_oinfo[0])))
1245 echo_copyin_lsm (struct echo_device *ed, struct lov_stripe_md *lsm,
1246 void *ulsm, int ulsm_nob)
1248 struct echo_client_obd *ec = ed->ed_ec;
1251 if (ulsm_nob < sizeof (*lsm))
1254 if (cfs_copy_from_user (lsm, ulsm, sizeof (*lsm)))
1257 if (lsm->lsm_stripe_count > ec->ec_nstripes ||
1258 lsm->lsm_magic != LOV_MAGIC ||
1259 (lsm->lsm_stripe_size & (~CFS_PAGE_MASK)) != 0 ||
1260 ((__u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count > ~0UL))
1264 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1265 if (cfs_copy_from_user(lsm->lsm_oinfo[i],
1266 ((struct lov_stripe_md *)ulsm)-> \
1268 sizeof(lsm->lsm_oinfo[0])))
1274 static int echo_create_object(struct echo_device *ed, int on_target,
1275 struct obdo *oa, void *ulsm, int ulsm_nob,
1276 struct obd_trans_info *oti)
1278 struct echo_object *eco;
1279 struct echo_client_obd *ec = ed->ed_ec;
1280 struct lov_stripe_md *lsm = NULL;
1285 if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
1286 (on_target || /* set_stripe */
1287 ec->ec_nstripes != 0)) { /* LOV */
1288 CERROR ("No valid oid\n");
1292 rc = obd_alloc_memmd(ec->ec_exp, &lsm);
1294 CERROR("Cannot allocate md, rc = %d\n", rc);
1301 rc = echo_copyin_lsm (ed, lsm, ulsm, ulsm_nob);
1305 if (lsm->lsm_stripe_count == 0)
1306 lsm->lsm_stripe_count = ec->ec_nstripes;
1308 if (lsm->lsm_stripe_size == 0)
1309 lsm->lsm_stripe_size = CFS_PAGE_SIZE;
1313 /* setup stripes: indices + default ids if required */
1314 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1315 if (lsm->lsm_oinfo[i]->loi_id == 0)
1316 lsm->lsm_oinfo[i]->loi_id = lsm->lsm_object_id;
1318 lsm->lsm_oinfo[i]->loi_ost_idx =
1319 (idx + i) % ec->ec_nstripes;
1323 /* setup object ID here for !on_target and LOV hint */
1324 if (oa->o_valid & OBD_MD_FLID)
1325 lsm->lsm_object_id = oa->o_id;
1327 if (lsm->lsm_object_id == 0)
1328 lsm->lsm_object_id = ++last_object_id;
1332 /* Only echo objects are allowed to be created */
1333 LASSERT((oa->o_valid & OBD_MD_FLGROUP) &&
1334 (oa->o_seq == FID_SEQ_ECHO));
1335 rc = obd_create(ec->ec_exp, oa, &lsm, oti);
1337 CERROR("Cannot create objects, rc = %d\n", rc);
1343 /* See what object ID we were given */
1344 oa->o_id = lsm->lsm_object_id;
1345 oa->o_valid |= OBD_MD_FLID;
1347 eco = cl_echo_object_find(ed, &lsm);
1349 GOTO(failed, rc = PTR_ERR(eco));
1350 cl_echo_object_put(eco);
1352 CDEBUG(D_INFO, "oa->o_id = %lx\n", (long)oa->o_id);
1357 obd_destroy(ec->ec_exp, oa, lsm, oti, NULL, NULL);
1359 obd_free_memmd(ec->ec_exp, &lsm);
1361 CERROR("create object failed with rc = %d\n", rc);
1365 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
1368 struct echo_client_obd *ec = ed->ed_ec;
1369 struct lov_stripe_md *lsm = NULL;
1370 struct echo_object *eco;
1374 if ((oa->o_valid & OBD_MD_FLID) == 0 ||
1375 oa->o_id == 0) /* disallow use of object id 0 */
1377 CERROR ("No valid oid\n");
1381 rc = obd_alloc_memmd(ec->ec_exp, &lsm);
1385 lsm->lsm_object_id = oa->o_id;
1386 if (oa->o_valid & OBD_MD_FLGROUP)
1387 lsm->lsm_object_seq = oa->o_seq;
1389 lsm->lsm_object_seq = FID_SEQ_ECHO;
1392 eco = cl_echo_object_find(ed, &lsm);
1398 obd_free_memmd(ec->ec_exp, &lsm);
1402 static void echo_put_object(struct echo_object *eco)
1404 if (cl_echo_object_put(eco))
1405 CERROR("echo client: drop an object failed");
1409 echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
1411 unsigned long stripe_count;
1412 unsigned long stripe_size;
1413 unsigned long width;
1414 unsigned long woffset;
1418 if (lsm->lsm_stripe_count <= 1)
1422 stripe_size = lsm->lsm_stripe_size;
1423 stripe_count = lsm->lsm_stripe_count;
1425 /* width = # bytes in all stripes */
1426 width = stripe_size * stripe_count;
1428 /* woffset = offset within a width; offset = whole number of widths */
1429 woffset = do_div (offset, width);
1431 stripe_index = woffset / stripe_size;
1433 *idp = lsm->lsm_oinfo[stripe_index]->loi_id;
1434 *offp = offset * stripe_size + woffset % stripe_size;
1438 echo_client_page_debug_setup(struct lov_stripe_md *lsm,
1439 cfs_page_t *page, int rw, obd_id id,
1440 obd_off offset, obd_off count)
1447 /* no partial pages on the client */
1448 LASSERT(count == CFS_PAGE_SIZE);
1450 addr = cfs_kmap(page);
1452 for (delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
1453 if (rw == OBD_BRW_WRITE) {
1454 stripe_off = offset + delta;
1456 echo_get_stripe_off_id(lsm, &stripe_off, &stripe_id);
1458 stripe_off = 0xdeadbeef00c0ffeeULL;
1459 stripe_id = 0xdeadbeef00c0ffeeULL;
1461 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
1462 stripe_off, stripe_id);
1468 static int echo_client_page_debug_check(struct lov_stripe_md *lsm,
1469 cfs_page_t *page, obd_id id,
1470 obd_off offset, obd_off count)
1479 /* no partial pages on the client */
1480 LASSERT(count == CFS_PAGE_SIZE);
1482 addr = cfs_kmap(page);
1484 for (rc = delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
1485 stripe_off = offset + delta;
1487 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
1489 rc2 = block_debug_check("test_brw",
1490 addr + delta, OBD_ECHO_BLOCK_SIZE,
1491 stripe_off, stripe_id);
1493 CERROR ("Error in echo object "LPX64"\n", id);
1502 static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
1503 struct echo_object *eco, obd_off offset,
1504 obd_size count, int async,
1505 struct obd_trans_info *oti)
1507 struct echo_client_obd *ec = ed->ed_ec;
1508 struct lov_stripe_md *lsm = eco->eo_lsm;
1510 struct brw_page *pga;
1511 struct brw_page *pgp;
1520 verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
1521 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
1522 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
1524 gfp_mask = ((oa->o_id & 2) == 0) ? CFS_ALLOC_STD : CFS_ALLOC_HIGHUSER;
1526 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
1527 LASSERT(lsm != NULL);
1528 LASSERT(lsm->lsm_object_id == oa->o_id);
1531 (count & (~CFS_PAGE_MASK)) != 0)
1534 /* XXX think again with misaligned I/O */
1535 npages = count >> CFS_PAGE_SHIFT;
1537 OBD_ALLOC(pga, npages * sizeof(*pga));
1541 OBD_ALLOC(pages, npages * sizeof(*pages));
1542 if (pages == NULL) {
1543 OBD_FREE(pga, npages * sizeof(*pga));
1547 for (i = 0, pgp = pga, off = offset;
1549 i++, pgp++, off += CFS_PAGE_SIZE) {
1551 LASSERT (pgp->pg == NULL); /* for cleanup */
1554 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
1555 if (pgp->pg == NULL)
1559 pgp->count = CFS_PAGE_SIZE;
1564 echo_client_page_debug_setup(lsm, pgp->pg, rw,
1565 oa->o_id, off, pgp->count);
1568 if (ed->ed_next == NULL) {
1569 struct obd_info oinfo = { { { 0 } } };
1572 rc = obd_brw(rw, ec->ec_exp, &oinfo, npages, pga, oti);
1574 rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
1577 if (rc != 0 || rw != OBD_BRW_READ)
1580 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
1581 if (pgp->pg == NULL)
1586 vrc = echo_client_page_debug_check(lsm, pgp->pg, oa->o_id,
1587 pgp->off, pgp->count);
1588 if (vrc != 0 && rc == 0)
1591 OBD_PAGE_FREE(pgp->pg);
1593 OBD_FREE(pga, npages * sizeof(*pga));
1594 OBD_FREE(pages, npages * sizeof(*pages));
1598 static int echo_client_prep_commit(struct obd_export *exp, int rw,
1599 struct obdo *oa, struct echo_object *eco,
1600 obd_off offset, obd_size count,
1601 obd_size batch, struct obd_trans_info *oti)
1603 struct lov_stripe_md *lsm = eco->eo_lsm;
1604 struct obd_ioobj ioo;
1605 struct niobuf_local *lnb;
1606 struct niobuf_remote *rnb;
1608 obd_size npages, tot_pages;
1612 if (count <= 0 || (count & (~CFS_PAGE_MASK)) != 0 ||
1613 (lsm != NULL && lsm->lsm_object_id != oa->o_id))
1616 npages = batch >> CFS_PAGE_SHIFT;
1617 tot_pages = count >> CFS_PAGE_SHIFT;
1619 OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
1620 OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
1622 if (lnb == NULL || rnb == NULL)
1623 GOTO(out, ret = -ENOMEM);
1625 obdo_to_ioobj(oa, &ioo);
1629 for(; tot_pages; tot_pages -= npages) {
1632 if (tot_pages < npages)
1635 for (i = 0; i < npages; i++, off += CFS_PAGE_SIZE) {
1636 rnb[i].offset = off;
1637 rnb[i].len = CFS_PAGE_SIZE;
1640 ioo.ioo_bufcnt = npages;
1641 oti->oti_transno = 0;
1644 ret = obd_preprw(rw, exp, oa, 1, &ioo, rnb, &lpages, lnb, oti,
1648 LASSERT(lpages == npages);
1650 for (i = 0; i < lpages; i++) {
1651 cfs_page_t *page = lnb[i].page;
1653 /* read past eof? */
1654 if (page == NULL && lnb[i].rc == 0)
1657 if (oa->o_id == ECHO_PERSISTENT_OBJID ||
1658 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
1659 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
1662 if (rw == OBD_BRW_WRITE)
1663 echo_client_page_debug_setup(lsm, page, rw,
1668 echo_client_page_debug_check(lsm, page,
1674 ret = obd_commitrw(rw, exp, oa, 1,&ioo,rnb,npages,lnb,oti,ret);
1678 /* Reset oti otherwise it would confuse ldiskfs. */
1679 memset(oti, 0, sizeof(*oti));
1684 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
1686 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
1690 static int echo_client_brw_ioctl(int rw, struct obd_export *exp,
1691 struct obd_ioctl_data *data)
1693 struct obd_device *obd = class_exp2obd(exp);
1694 struct echo_device *ed = obd2echo_dev(obd);
1695 struct echo_client_obd *ec = ed->ed_ec;
1696 struct obd_trans_info dummy_oti = { .oti_thread = NULL };
1697 struct obdo *oa = &data->ioc_obdo1;
1698 struct echo_object *eco;
1703 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
1705 rc = echo_get_object(&eco, ed, oa);
1709 oa->o_valid &= ~OBD_MD_FLHANDLE;
1711 switch((long)data->ioc_pbuf1) {
1716 rc = echo_client_kbrw(ed, rw, oa,
1717 eco, data->ioc_offset,
1718 data->ioc_count, async, &dummy_oti);
1721 rc = echo_client_prep_commit(ec->ec_exp, rw, oa,
1722 eco, data->ioc_offset,
1723 data->ioc_count, data->ioc_plen1,
1729 echo_put_object(eco);
1734 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
1735 int mode, obd_off offset, obd_size nob)
1737 struct echo_device *ed = obd2echo_dev(exp->exp_obd);
1738 struct lustre_handle *ulh = &oa->o_handle;
1739 struct echo_object *eco;
1744 if (ed->ed_next == NULL)
1745 RETURN(-EOPNOTSUPP);
1747 if (!(mode == LCK_PR || mode == LCK_PW))
1750 if ((offset & (~CFS_PAGE_MASK)) != 0 ||
1751 (nob & (~CFS_PAGE_MASK)) != 0)
1754 rc = echo_get_object (&eco, ed, oa);
1758 end = (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
1759 rc = cl_echo_enqueue(eco, offset, end, mode, &ulh->cookie);
1761 oa->o_valid |= OBD_MD_FLHANDLE;
1762 CDEBUG(D_INFO, "Cookie is "LPX64"\n", ulh->cookie);
1764 echo_put_object(eco);
1769 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
1771 struct echo_device *ed = obd2echo_dev(exp->exp_obd);
1772 __u64 cookie = oa->o_handle.cookie;
1774 if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
1777 CDEBUG(D_INFO, "Cookie is "LPX64"\n", cookie);
1778 return cl_echo_cancel(ed, cookie);
1782 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
1783 int len, void *karg, void *uarg)
1785 struct obd_device *obd = exp->exp_obd;
1786 struct echo_device *ed = obd2echo_dev(obd);
1787 struct echo_client_obd *ec = ed->ed_ec;
1788 struct echo_object *eco;
1789 struct obd_ioctl_data *data = karg;
1790 struct obd_trans_info dummy_oti;
1791 struct oti_req_ack_lock *ack_lock;
1793 int rw = OBD_BRW_READ;
1798 cfs_unlock_kernel();
1800 memset(&dummy_oti, 0, sizeof(dummy_oti));
1802 oa = &data->ioc_obdo1;
1803 if (!(oa->o_valid & OBD_MD_FLGROUP)) {
1804 oa->o_valid |= OBD_MD_FLGROUP;
1805 oa->o_seq = FID_SEQ_ECHO;
1807 /* assume we can touch filter native objects with echo device. */
1808 /* LASSERT(oa->o_seq == FID_SEQ_ECHO); */
1811 case OBD_IOC_CREATE: /* may create echo object */
1812 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1813 GOTO (out, rc = -EPERM);
1815 rc = echo_create_object (ed, 1, oa,
1816 data->ioc_pbuf1, data->ioc_plen1,
1820 case OBD_IOC_DESTROY:
1821 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1822 GOTO (out, rc = -EPERM);
1824 rc = echo_get_object (&eco, ed, oa);
1826 rc = obd_destroy(ec->ec_exp, oa, eco->eo_lsm,
1827 &dummy_oti, NULL, NULL);
1829 eco->eo_deleted = 1;
1830 echo_put_object(eco);
1834 case OBD_IOC_GETATTR:
1835 rc = echo_get_object (&eco, ed, oa);
1837 struct obd_info oinfo = { { { 0 } } };
1838 oinfo.oi_md = eco->eo_lsm;
1840 rc = obd_getattr(ec->ec_exp, &oinfo);
1841 echo_put_object(eco);
1845 case OBD_IOC_SETATTR:
1846 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1847 GOTO (out, rc = -EPERM);
1849 rc = echo_get_object (&eco, ed, oa);
1851 struct obd_info oinfo = { { { 0 } } };
1853 oinfo.oi_md = eco->eo_lsm;
1855 rc = obd_setattr(ec->ec_exp, &oinfo, NULL);
1856 echo_put_object(eco);
1860 case OBD_IOC_BRW_WRITE:
1861 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1862 GOTO (out, rc = -EPERM);
1866 case OBD_IOC_BRW_READ:
1867 rc = echo_client_brw_ioctl(rw, exp, data);
1870 case ECHO_IOC_GET_STRIPE:
1871 rc = echo_get_object(&eco, ed, oa);
1873 rc = echo_copyout_lsm(eco->eo_lsm, data->ioc_pbuf1,
1875 echo_put_object(eco);
1879 case ECHO_IOC_SET_STRIPE:
1880 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1881 GOTO (out, rc = -EPERM);
1883 if (data->ioc_pbuf1 == NULL) { /* unset */
1884 rc = echo_get_object(&eco, ed, oa);
1886 eco->eo_deleted = 1;
1887 echo_put_object(eco);
1890 rc = echo_create_object(ed, 0, oa,
1892 data->ioc_plen1, &dummy_oti);
1896 case ECHO_IOC_ENQUEUE:
1897 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1898 GOTO (out, rc = -EPERM);
1900 rc = echo_client_enqueue(exp, oa,
1901 data->ioc_conn1, /* lock mode */
1903 data->ioc_count);/*extent*/
1906 case ECHO_IOC_CANCEL:
1907 rc = echo_client_cancel(exp, oa);
1911 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
1912 GOTO (out, rc = -ENOTTY);
1918 /* XXX this should be in a helper also called by target_send_reply */
1919 for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
1921 if (!ack_lock->mode)
1923 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
1931 static int echo_client_setup(struct obd_device *obddev, struct lustre_cfg *lcfg)
1933 struct echo_client_obd *ec = &obddev->u.echo_client;
1934 struct obd_device *tgt;
1935 struct obd_uuid echo_uuid = { "ECHO_UUID" };
1936 struct obd_connect_data *ocd = NULL;
1940 if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1941 CERROR("requires a TARGET OBD name\n");
1945 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
1946 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
1947 CERROR("device not attached or not set up (%s)\n",
1948 lustre_cfg_string(lcfg, 1));
1952 cfs_spin_lock_init (&ec->ec_lock);
1953 CFS_INIT_LIST_HEAD (&ec->ec_objects);
1954 CFS_INIT_LIST_HEAD (&ec->ec_locks);
1956 ec->ec_nstripes = 0;
1958 OBD_ALLOC(ocd, sizeof(*ocd));
1960 CERROR("Can't alloc ocd connecting to %s\n",
1961 lustre_cfg_string(lcfg, 1));
1965 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
1966 OBD_CONNECT_GRANT | OBD_CONNECT_FULL20;
1967 ocd->ocd_version = LUSTRE_VERSION_CODE;
1968 ocd->ocd_group = FID_SEQ_ECHO;
1970 rc = obd_connect(NULL, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
1972 /* Turn off pinger because it connects to tgt obd directly. */
1973 cfs_spin_lock(&tgt->obd_dev_lock);
1974 cfs_list_del_init(&ec->ec_exp->exp_obd_chain_timed);
1975 cfs_spin_unlock(&tgt->obd_dev_lock);
1978 OBD_FREE(ocd, sizeof(*ocd));
1981 CERROR("fail to connect to device %s\n",
1982 lustre_cfg_string(lcfg, 1));
1989 static int echo_client_cleanup(struct obd_device *obddev)
1991 struct echo_client_obd *ec = &obddev->u.echo_client;
1995 if (!cfs_list_empty(&obddev->obd_exports)) {
1996 CERROR("still has clients!\n");
2000 LASSERT(cfs_atomic_read(&ec->ec_exp->exp_refcount) > 0);
2001 rc = obd_disconnect(ec->ec_exp);
2003 CERROR("fail to disconnect device: %d\n", rc);
2008 static int echo_client_connect(const struct lu_env *env,
2009 struct obd_export **exp,
2010 struct obd_device *src, struct obd_uuid *cluuid,
2011 struct obd_connect_data *data, void *localdata)
2014 struct lustre_handle conn = { 0 };
2017 rc = class_connect(&conn, src, cluuid);
2019 *exp = class_conn2export(&conn);
2025 static int echo_client_disconnect(struct obd_export *exp)
2028 struct obd_device *obd;
2029 struct echo_client_obd *ec;
2030 struct ec_lock *ecl;
2036 GOTO(out, rc = -EINVAL);
2040 ec = &obd->u.echo_client;
2042 /* no more contention on export's lock list */
2043 while (!cfs_list_empty (&exp->exp_ec_data.eced_locks)) {
2044 ecl = cfs_list_entry (exp->exp_ec_data.eced_locks.next,
2045 struct ec_lock, ecl_exp_chain);
2046 cfs_list_del (&ecl->ecl_exp_chain);
2048 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm,
2049 ecl->ecl_mode, &ecl->ecl_lock_handle);
2051 CDEBUG (D_INFO, "Cancel lock on object "LPX64" on disconnect "
2052 "(%d)\n", ecl->ecl_object->eco_id, rc);
2054 echo_put_object (ecl->ecl_object);
2055 OBD_FREE (ecl, sizeof (*ecl));
2059 rc = class_disconnect(exp);
2065 static struct obd_ops echo_obd_ops = {
2066 .o_owner = THIS_MODULE,
2069 .o_setup = echo_client_setup,
2070 .o_cleanup = echo_client_cleanup,
2073 .o_iocontrol = echo_client_iocontrol,
2074 .o_connect = echo_client_connect,
2075 .o_disconnect = echo_client_disconnect
2078 int echo_client_init(void)
2080 struct lprocfs_static_vars lvars = { 0 };
2083 lprocfs_echo_init_vars(&lvars);
2084 rc = class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
2085 LUSTRE_ECHO_CLIENT_NAME, &echo_device_type);
2087 lu_kmem_init(echo_caches);
2091 void echo_client_exit(void)
2093 class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
2094 lu_kmem_fini(echo_caches);
2097 /** @} echo_client */