1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_ECHO
39 #include <libcfs/libcfs.h>
41 #include <liblustre.h>
45 #include <obd_support.h>
46 #include <obd_class.h>
47 #include <lustre_debug.h>
48 #include <lprocfs_status.h>
49 #include <cl_object.h>
51 #include "echo_internal.h"
53 /** \defgroup echo_client Echo Client
58 struct cl_device ed_cl;
59 struct echo_client_obd *ed_ec;
61 struct cl_site ed_site_myself;
62 struct cl_site *ed_site;
63 struct lu_device *ed_next;
68 struct cl_object eo_cl;
69 struct cl_object_header eo_hdr;
71 struct echo_device *eo_dev;
72 struct list_head eo_obj_chain;
73 struct lov_stripe_md *eo_lsm;
78 struct echo_object_conf {
79 struct cl_object_conf eoc_cl;
80 struct lov_stripe_md **eoc_md;
84 struct cl_page_slice ep_cl;
85 cfs_page_t *ep_vmpage;
89 struct cl_lock_slice el_cl;
90 struct list_head el_chain;
91 struct echo_object *el_object;
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 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(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(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 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 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 atomic_set(&eco->eo_npages, 0);
464 /* clear the lsm pointer so that it won't get freed. */
465 *econf->eoc_md = NULL;
467 spin_lock(&ec->ec_lock);
468 list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
469 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(atomic_read(&eco->eo_npages) == 0);
483 spin_lock(&ec->ec_lock);
484 list_del_init(&eco->eo_obj_chain);
485 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 spin_lock(&ec->ec_lock);
798 while (!list_empty(&ec->ec_locks)) {
799 struct echo_lock *ecl = list_entry(ec->ec_locks.next,
800 struct echo_lock, el_chain);
803 if (atomic_dec_and_test(&ecl->el_refcount))
804 list_del_init(&ecl->el_chain);
807 spin_unlock(&ec->ec_lock);
809 CERROR("echo client: pending lock %p refs %d\n",
810 ecl, atomic_read(&ecl->el_refcount));
812 echo_lock_release(env, ecl, still_used);
813 spin_lock(&ec->ec_lock);
815 spin_unlock(&ec->ec_lock);
817 LASSERT(ed->ed_site);
818 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
820 /* check if there are objects still alive.
821 * It shouldn't have any object because lu_site_purge would cleanup
822 * all of cached objects. Anyway, probably the echo device is being
823 * parallelly accessed.
825 spin_lock(&ec->ec_lock);
826 list_for_each_entry(eco, &ec->ec_objects, eo_obj_chain)
828 spin_unlock(&ec->ec_lock);
831 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
833 CDEBUG(D_INFO, "Waiting for the reference of echo object to be dropped\n");
835 /* Wait for the last reference to be dropped. */
836 spin_lock(&ec->ec_lock);
837 while (!list_empty(&ec->ec_objects)) {
838 spin_unlock(&ec->ec_lock);
839 CERROR("echo_client still has objects at cleanup time, "
840 "wait for 1 second\n");
841 cfs_schedule_timeout(CFS_TASK_UNINT, cfs_time_seconds(1));
842 spin_lock(&ec->ec_lock);
844 spin_unlock(&ec->ec_lock);
846 CDEBUG(D_INFO, "No object exists, exiting...\n");
848 echo_client_cleanup(d->ld_obd);
851 next = next->ld_type->ldt_ops->ldto_device_free(env, next);
853 LASSERT(ed->ed_site == lu2cl_site(d->ld_site));
854 echo_site_fini(env, ed);
855 cl_device_fini(&ed->ed_cl);
861 static const struct lu_device_type_operations echo_device_type_ops = {
862 .ldto_init = echo_type_init,
863 .ldto_fini = echo_type_fini,
865 .ldto_start = echo_type_start,
866 .ldto_stop = echo_type_stop,
868 .ldto_device_alloc = echo_device_alloc,
869 .ldto_device_free = echo_device_free,
870 .ldto_device_init = echo_device_init,
871 .ldto_device_fini = echo_device_fini
874 static struct lu_device_type echo_device_type = {
875 .ldt_tags = LU_DEVICE_CL,
876 .ldt_name = LUSTRE_ECHO_CLIENT_NAME,
877 .ldt_ops = &echo_device_type_ops,
878 .ldt_ctx_tags = LCT_CL_THREAD
882 /** \defgroup echo_exports Exported operations
884 * exporting functions to echo client
889 /* Interfaces to echo client obd device */
890 static struct echo_object *cl_echo_object_find(struct echo_device *d,
891 struct lov_stripe_md **lsmp)
894 struct echo_thread_info *info;
895 struct echo_object_conf *conf;
896 struct lov_stripe_md *lsm;
897 struct echo_object *eco;
898 struct cl_object *obj;
906 LASSERT(lsm->lsm_object_id);
908 /* Never return an object if the obd is to be freed. */
909 if (echo_dev2cl(d)->cd_lu_dev.ld_obd->obd_stopping)
910 RETURN(ERR_PTR(-ENODEV));
912 env = cl_env_get(&refcheck);
916 info = echo_env_info(env);
917 conf = &info->eti_conf;
919 if (!d->ed_next_islov) {
920 struct lov_oinfo *oinfo = lsm->lsm_oinfo[0];
921 LASSERT(oinfo != NULL);
922 oinfo->loi_id = lsm->lsm_object_id;
923 oinfo->loi_gr = lsm->lsm_object_gr;
924 conf->eoc_cl.u.coc_oinfo = oinfo;
926 struct lustre_md *md;
928 memset(md, 0, sizeof *md);
930 conf->eoc_cl.u.coc_md = md;
935 fid = &info->eti_fid;
938 obj = cl_object_find(env, echo_dev2cl(d), fid, &conf->eoc_cl);
940 GOTO(out, eco = (void*)obj);
942 eco = cl2echo_obj(obj);
943 if (eco->eo_deleted) {
944 cl_object_put(env, obj);
945 eco = ERR_PTR(-EAGAIN);
949 cl_env_put(env, &refcheck);
953 static int cl_echo_object_put(struct echo_object *eco)
956 struct cl_object *obj = echo_obj2cl(eco);
960 env = cl_env_get(&refcheck);
962 RETURN(PTR_ERR(env));
964 /* an external function to kill an object? */
965 if (eco->eo_deleted) {
966 struct lu_object_header *loh = obj->co_lu.lo_header;
967 LASSERT(&eco->eo_hdr == luh2coh(loh));
968 set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
969 cl_object_prune(env, obj);
972 cl_object_put(env, obj);
973 cl_env_put(env, &refcheck);
977 static int cl_echo_enqueue0(struct lu_env *env, struct echo_object *eco,
978 obd_off start, obd_off end, int mode,
979 __u64 *cookie , __u32 enqflags)
983 struct cl_object *obj;
984 struct cl_lock_descr *descr;
985 struct echo_thread_info *info;
989 info = echo_env_info(env);
991 descr = &info->eti_descr;
992 obj = echo_obj2cl(eco);
994 descr->cld_obj = obj;
995 descr->cld_start = cl_index(obj, start);
996 descr->cld_end = cl_index(obj, end);
997 descr->cld_mode = mode == LCK_PW ? CLM_WRITE : CLM_READ;
998 descr->cld_enq_flags = CEF_ASYNC | enqflags;
1001 lck = cl_lock_request(env, io, descr, "ec enqueue", eco);
1003 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
1004 struct echo_lock *el;
1006 rc = cl_wait(env, lck);
1008 el = cl2echo_lock(cl_lock_at(lck, &echo_device_type));
1009 spin_lock(&ec->ec_lock);
1010 if (list_empty(&el->el_chain)) {
1011 list_add(&el->el_chain, &ec->ec_locks);
1012 el->el_cookie = ++ec->ec_unique;
1014 atomic_inc(&el->el_refcount);
1015 *cookie = el->el_cookie;
1016 spin_unlock(&ec->ec_lock);
1018 cl_lock_release(env, lck, "ec enqueue", cfs_current());
1023 static int cl_echo_enqueue(struct echo_object *eco, obd_off start, obd_off end,
1024 int mode, __u64 *cookie)
1026 struct echo_thread_info *info;
1033 env = cl_env_get(&refcheck);
1035 RETURN(PTR_ERR(env));
1037 info = echo_env_info(env);
1040 result = cl_io_init(env, io, CIT_MISC, echo_obj2cl(eco));
1043 LASSERT(result == 0);
1045 result = cl_echo_enqueue0(env, eco, start, end, mode, cookie, 0);
1046 cl_io_fini(env, io);
1050 cl_env_put(env, &refcheck);
1054 static int cl_echo_cancel0(struct lu_env *env, struct echo_device *ed,
1057 struct echo_client_obd *ec = ed->ed_ec;
1058 struct echo_lock *ecl = NULL;
1059 struct list_head *el;
1060 int found = 0, still_used = 0;
1063 LASSERT(ec != NULL);
1064 spin_lock (&ec->ec_lock);
1065 list_for_each (el, &ec->ec_locks) {
1066 ecl = list_entry (el, struct echo_lock, el_chain);
1067 CDEBUG(D_INFO, "ecl: %p, cookie: %llx\n", ecl, ecl->el_cookie);
1068 found = (ecl->el_cookie == cookie);
1070 if (atomic_dec_and_test(&ecl->el_refcount))
1071 list_del_init(&ecl->el_chain);
1077 spin_unlock (&ec->ec_lock);
1082 echo_lock_release(env, ecl, still_used);
1086 static int cl_echo_cancel(struct echo_device *ed, __u64 cookie)
1093 env = cl_env_get(&refcheck);
1095 RETURN(PTR_ERR(env));
1097 rc = cl_echo_cancel0(env, ed, cookie);
1099 cl_env_put(env, &refcheck);
1103 static int cl_echo_async_brw(const struct lu_env *env, struct cl_io *io,
1104 enum cl_req_type unused, struct cl_2queue *queue)
1106 struct cl_page *clp;
1107 struct cl_page *temp;
1111 cl_page_list_for_each_safe(clp, temp, &queue->c2_qin) {
1113 rc = cl_page_cache_add(env, io, clp, CRT_WRITE);
1116 result = result ?: rc;
1121 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
1122 cfs_page_t **pages, int npages, int async)
1125 struct echo_thread_info *info;
1126 struct cl_object *obj = echo_obj2cl(eco);
1127 struct echo_device *ed = eco->eo_dev;
1128 struct cl_2queue *queue;
1130 struct cl_page *clp;
1131 struct lustre_handle lh = { 0 };
1132 int page_size = cl_page_size(obj);
1138 LASSERT((offset & ~CFS_PAGE_MASK) == 0);
1139 LASSERT(ed->ed_next != NULL);
1140 env = cl_env_get(&refcheck);
1142 RETURN(PTR_ERR(env));
1144 info = echo_env_info(env);
1146 queue = &info->eti_queue;
1148 cl_2queue_init(queue);
1149 rc = cl_io_init(env, io, CIT_MISC, obj);
1155 rc = cl_echo_enqueue0(env, eco, offset,
1156 offset + npages * CFS_PAGE_SIZE - 1,
1157 rw == READ ? LCK_PW : LCK_PW, &lh.cookie,
1160 GOTO(error_lock, rc);
1162 for (i = 0; i < npages; i++) {
1164 clp = cl_page_find(env, obj, cl_index(obj, offset),
1165 pages[i], CPT_TRANSIENT);
1170 LASSERT(clp->cp_type == CPT_TRANSIENT);
1172 rc = cl_page_own(env, io, clp);
1174 LASSERT(clp->cp_state == CPS_FREEING);
1175 cl_page_put(env, clp);
1179 cl_2queue_add(queue, clp);
1181 /* drop the reference count for cl_page_find, so that the page
1182 * will be freed in cl_2queue_fini. */
1183 cl_page_put(env, clp);
1184 cl_page_clip(env, clp, 0, page_size);
1186 offset += page_size;
1190 enum cl_req_type typ = rw == READ ? CRT_READ : CRT_WRITE;
1192 async = async && (typ == CRT_WRITE);
1194 rc = cl_echo_async_brw(env, io, typ, queue);
1196 rc = cl_io_submit_sync(env, io, typ, queue,
1198 CDEBUG(D_INFO, "echo_client %s write returns %d\n",
1199 async ? "async" : "sync", rc);
1202 cl_echo_cancel0(env, ed, lh.cookie);
1205 cl_2queue_discard(env, io, queue);
1206 cl_2queue_disown(env, io, queue);
1207 cl_2queue_fini(env, queue);
1208 cl_io_fini(env, io);
1210 cl_env_put(env, &refcheck);
1213 /** @} echo_exports */
1216 static obd_id last_object_id;
1219 echo_copyout_lsm (struct lov_stripe_md *lsm, void *_ulsm, int ulsm_nob)
1221 struct lov_stripe_md *ulsm = _ulsm;
1224 nob = offsetof (struct lov_stripe_md, lsm_oinfo[lsm->lsm_stripe_count]);
1228 if (copy_to_user (ulsm, lsm, sizeof(ulsm)))
1231 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1232 if (copy_to_user (ulsm->lsm_oinfo[i], lsm->lsm_oinfo[i],
1233 sizeof(lsm->lsm_oinfo[0])))
1240 echo_copyin_lsm (struct echo_device *ed, struct lov_stripe_md *lsm,
1241 void *ulsm, int ulsm_nob)
1243 struct echo_client_obd *ec = ed->ed_ec;
1246 if (ulsm_nob < sizeof (*lsm))
1249 if (copy_from_user (lsm, ulsm, sizeof (*lsm)))
1252 if (lsm->lsm_stripe_count > ec->ec_nstripes ||
1253 lsm->lsm_magic != LOV_MAGIC ||
1254 (lsm->lsm_stripe_size & (~CFS_PAGE_MASK)) != 0 ||
1255 ((__u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count > ~0UL))
1259 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1260 if (copy_from_user(lsm->lsm_oinfo[i],
1261 ((struct lov_stripe_md *)ulsm)->lsm_oinfo[i],
1262 sizeof(lsm->lsm_oinfo[0])))
1268 static int echo_create_object(struct echo_device *ed, int on_target,
1269 struct obdo *oa, void *ulsm, int ulsm_nob,
1270 struct obd_trans_info *oti)
1272 struct echo_object *eco;
1273 struct echo_client_obd *ec = ed->ed_ec;
1274 struct lov_stripe_md *lsm = NULL;
1279 if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
1280 (on_target || /* set_stripe */
1281 ec->ec_nstripes != 0)) { /* LOV */
1282 CERROR ("No valid oid\n");
1286 rc = obd_alloc_memmd(ec->ec_exp, &lsm);
1288 CERROR("Cannot allocate md, rc = %d\n", rc);
1295 rc = echo_copyin_lsm (ed, lsm, ulsm, ulsm_nob);
1299 if (lsm->lsm_stripe_count == 0)
1300 lsm->lsm_stripe_count = ec->ec_nstripes;
1302 if (lsm->lsm_stripe_size == 0)
1303 lsm->lsm_stripe_size = CFS_PAGE_SIZE;
1307 /* setup stripes: indices + default ids if required */
1308 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1309 if (lsm->lsm_oinfo[i]->loi_id == 0)
1310 lsm->lsm_oinfo[i]->loi_id = lsm->lsm_object_id;
1312 lsm->lsm_oinfo[i]->loi_ost_idx =
1313 (idx + i) % ec->ec_nstripes;
1317 /* setup object ID here for !on_target and LOV hint */
1318 if (oa->o_valid & OBD_MD_FLID)
1319 lsm->lsm_object_id = oa->o_id;
1321 if (lsm->lsm_object_id == 0)
1322 lsm->lsm_object_id = ++last_object_id;
1326 /* Only echo objects are allowed to be created */
1327 LASSERT((oa->o_valid & OBD_MD_FLGROUP) &&
1328 (oa->o_gr == FILTER_GROUP_ECHO));
1329 rc = obd_create(ec->ec_exp, oa, &lsm, oti);
1331 CERROR("Cannot create objects, rc = %d\n", rc);
1337 /* See what object ID we were given */
1338 oa->o_id = lsm->lsm_object_id;
1339 oa->o_valid |= OBD_MD_FLID;
1341 eco = cl_echo_object_find(ed, &lsm);
1343 GOTO(failed, rc = PTR_ERR(eco));
1344 cl_echo_object_put(eco);
1346 CDEBUG(D_INFO, "oa->o_id = %lx\n", (long)oa->o_id);
1351 obd_destroy(ec->ec_exp, oa, lsm, oti, NULL, NULL);
1353 obd_free_memmd(ec->ec_exp, &lsm);
1355 CERROR("create object failed with rc = %d\n", rc);
1359 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
1362 struct echo_client_obd *ec = ed->ed_ec;
1363 struct lov_stripe_md *lsm = NULL;
1364 struct echo_object *eco;
1368 if ((oa->o_valid & OBD_MD_FLID) == 0 ||
1369 oa->o_id == 0) /* disallow use of object id 0 */
1371 CERROR ("No valid oid\n");
1375 rc = obd_alloc_memmd(ec->ec_exp, &lsm);
1379 lsm->lsm_object_id = oa->o_id;
1380 if (oa->o_valid & OBD_MD_FLGROUP)
1381 lsm->lsm_object_gr = oa->o_gr;
1383 lsm->lsm_object_gr = FILTER_GROUP_ECHO;
1386 eco = cl_echo_object_find(ed, &lsm);
1392 obd_free_memmd(ec->ec_exp, &lsm);
1396 static void echo_put_object(struct echo_object *eco)
1398 if (cl_echo_object_put(eco))
1399 CERROR("echo client: drop an object failed");
1403 echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
1405 unsigned long stripe_count;
1406 unsigned long stripe_size;
1407 unsigned long width;
1408 unsigned long woffset;
1412 if (lsm->lsm_stripe_count <= 1)
1416 stripe_size = lsm->lsm_stripe_size;
1417 stripe_count = lsm->lsm_stripe_count;
1419 /* width = # bytes in all stripes */
1420 width = stripe_size * stripe_count;
1422 /* woffset = offset within a width; offset = whole number of widths */
1423 woffset = do_div (offset, width);
1425 stripe_index = woffset / stripe_size;
1427 *idp = lsm->lsm_oinfo[stripe_index]->loi_id;
1428 *offp = offset * stripe_size + woffset % stripe_size;
1432 echo_client_page_debug_setup(struct lov_stripe_md *lsm,
1433 cfs_page_t *page, int rw, obd_id id,
1434 obd_off offset, obd_off count)
1441 /* no partial pages on the client */
1442 LASSERT(count == CFS_PAGE_SIZE);
1444 addr = cfs_kmap(page);
1446 for (delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
1447 if (rw == OBD_BRW_WRITE) {
1448 stripe_off = offset + delta;
1450 echo_get_stripe_off_id(lsm, &stripe_off, &stripe_id);
1452 stripe_off = 0xdeadbeef00c0ffeeULL;
1453 stripe_id = 0xdeadbeef00c0ffeeULL;
1455 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
1456 stripe_off, stripe_id);
1462 static int echo_client_page_debug_check(struct lov_stripe_md *lsm,
1463 cfs_page_t *page, obd_id id,
1464 obd_off offset, obd_off count)
1473 /* no partial pages on the client */
1474 LASSERT(count == CFS_PAGE_SIZE);
1476 addr = cfs_kmap(page);
1478 for (rc = delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
1479 stripe_off = offset + delta;
1481 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
1483 rc2 = block_debug_check("test_brw",
1484 addr + delta, OBD_ECHO_BLOCK_SIZE,
1485 stripe_off, stripe_id);
1487 CERROR ("Error in echo object "LPX64"\n", id);
1496 static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
1497 struct echo_object *eco, obd_off offset,
1498 obd_size count, int async,
1499 struct obd_trans_info *oti)
1501 struct echo_client_obd *ec = ed->ed_ec;
1502 struct lov_stripe_md *lsm = eco->eo_lsm;
1504 struct brw_page *pga;
1505 struct brw_page *pgp;
1514 verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
1515 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
1516 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
1518 gfp_mask = ((oa->o_id & 2) == 0) ? CFS_ALLOC_STD : CFS_ALLOC_HIGHUSER;
1520 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
1521 LASSERT(lsm != NULL);
1522 LASSERT(lsm->lsm_object_id == oa->o_id);
1525 (count & (~CFS_PAGE_MASK)) != 0)
1528 /* XXX think again with misaligned I/O */
1529 npages = count >> CFS_PAGE_SHIFT;
1531 OBD_ALLOC(pga, npages * sizeof(*pga));
1535 OBD_ALLOC(pages, npages * sizeof(*pages));
1536 if (pages == NULL) {
1537 OBD_FREE(pga, npages * sizeof(*pga));
1541 for (i = 0, pgp = pga, off = offset;
1543 i++, pgp++, off += CFS_PAGE_SIZE) {
1545 LASSERT (pgp->pg == NULL); /* for cleanup */
1548 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
1549 if (pgp->pg == NULL)
1553 pgp->count = CFS_PAGE_SIZE;
1558 echo_client_page_debug_setup(lsm, pgp->pg, rw,
1559 oa->o_id, off, pgp->count);
1562 if (ed->ed_next == NULL) {
1563 struct obd_info oinfo = { { { 0 } } };
1566 rc = obd_brw(rw, ec->ec_exp, &oinfo, npages, pga, oti);
1568 rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
1571 if (rc != 0 || rw != OBD_BRW_READ)
1574 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
1575 if (pgp->pg == NULL)
1580 vrc = echo_client_page_debug_check(lsm, pgp->pg, oa->o_id,
1581 pgp->off, pgp->count);
1582 if (vrc != 0 && rc == 0)
1585 OBD_PAGE_FREE(pgp->pg);
1587 OBD_FREE(pga, npages * sizeof(*pga));
1588 OBD_FREE(pages, npages * sizeof(*pages));
1592 static int echo_client_prep_commit(struct obd_export *exp, int rw,
1593 struct obdo *oa, struct echo_object *eco,
1594 obd_off offset, obd_size count,
1595 obd_size batch, struct obd_trans_info *oti)
1597 struct lov_stripe_md *lsm = eco->eo_lsm;
1598 struct obd_ioobj ioo;
1599 struct niobuf_local *lnb;
1600 struct niobuf_remote *rnb;
1602 obd_size npages, tot_pages;
1606 if (count <= 0 || (count & (~CFS_PAGE_MASK)) != 0 ||
1607 (lsm != NULL && lsm->lsm_object_id != oa->o_id))
1610 npages = batch >> CFS_PAGE_SHIFT;
1611 tot_pages = count >> CFS_PAGE_SHIFT;
1613 OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
1614 OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
1616 if (lnb == NULL || rnb == NULL)
1617 GOTO(out, ret = -ENOMEM);
1619 obdo_to_ioobj(oa, &ioo);
1623 for(; tot_pages; tot_pages -= npages) {
1626 if (tot_pages < npages)
1629 for (i = 0; i < npages; i++, off += CFS_PAGE_SIZE) {
1630 rnb[i].offset = off;
1631 rnb[i].len = CFS_PAGE_SIZE;
1634 ioo.ioo_bufcnt = npages;
1635 oti->oti_transno = 0;
1638 ret = obd_preprw(rw, exp, oa, 1, &ioo, rnb, &lpages, lnb, oti,
1642 LASSERT(lpages == npages);
1644 for (i = 0; i < lpages; i++) {
1645 cfs_page_t *page = lnb[i].page;
1647 /* read past eof? */
1648 if (page == NULL && lnb[i].rc == 0)
1651 if (oa->o_id == ECHO_PERSISTENT_OBJID ||
1652 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
1653 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
1656 if (rw == OBD_BRW_WRITE)
1657 echo_client_page_debug_setup(lsm, page, rw,
1662 echo_client_page_debug_check(lsm, page,
1668 ret = obd_commitrw(rw, exp, oa, 1,&ioo,rnb,npages,lnb,oti,ret);
1672 /* Reset oti otherwise it would confuse ldiskfs. */
1673 memset(oti, 0, sizeof(*oti));
1678 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
1680 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
1684 static int echo_client_brw_ioctl(int rw, struct obd_export *exp,
1685 struct obd_ioctl_data *data)
1687 struct obd_device *obd = class_exp2obd(exp);
1688 struct echo_device *ed = obd2echo_dev(obd);
1689 struct echo_client_obd *ec = ed->ed_ec;
1690 struct obd_trans_info dummy_oti = { .oti_thread = NULL };
1691 struct obdo *oa = &data->ioc_obdo1;
1692 struct echo_object *eco;
1697 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
1699 rc = echo_get_object(&eco, ed, oa);
1703 oa->o_valid &= ~OBD_MD_FLHANDLE;
1705 switch((long)data->ioc_pbuf1) {
1710 rc = echo_client_kbrw(ed, rw, oa,
1711 eco, data->ioc_offset,
1712 data->ioc_count, async, &dummy_oti);
1715 rc = echo_client_prep_commit(ec->ec_exp, rw, oa,
1716 eco, data->ioc_offset,
1717 data->ioc_count, data->ioc_plen1,
1723 echo_put_object(eco);
1728 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
1729 int mode, obd_off offset, obd_size nob)
1731 struct echo_device *ed = obd2echo_dev(exp->exp_obd);
1732 struct lustre_handle *ulh = &oa->o_handle;
1733 struct echo_object *eco;
1738 if (ed->ed_next == NULL)
1739 RETURN(-EOPNOTSUPP);
1741 if (!(mode == LCK_PR || mode == LCK_PW))
1744 if ((offset & (~CFS_PAGE_MASK)) != 0 ||
1745 (nob & (~CFS_PAGE_MASK)) != 0)
1748 rc = echo_get_object (&eco, ed, oa);
1752 end = (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
1753 rc = cl_echo_enqueue(eco, offset, end, mode, &ulh->cookie);
1755 oa->o_valid |= OBD_MD_FLHANDLE;
1756 CDEBUG(D_INFO, "Cookie is %llx\n", ulh->cookie);
1758 echo_put_object(eco);
1763 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
1765 struct echo_device *ed = obd2echo_dev(exp->exp_obd);
1766 __u64 cookie = oa->o_handle.cookie;
1768 if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
1771 CDEBUG(D_INFO, "Cookie is %llx\n", cookie);
1772 return cl_echo_cancel(ed, cookie);
1776 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
1777 int len, void *karg, void *uarg)
1779 struct obd_device *obd = exp->exp_obd;
1780 struct echo_device *ed = obd2echo_dev(obd);
1781 struct echo_client_obd *ec = ed->ed_ec;
1782 struct echo_object *eco;
1783 struct obd_ioctl_data *data = karg;
1784 struct obd_trans_info dummy_oti;
1785 struct oti_req_ack_lock *ack_lock;
1787 int rw = OBD_BRW_READ;
1794 memset(&dummy_oti, 0, sizeof(dummy_oti));
1796 oa = &data->ioc_obdo1;
1797 if (!(oa->o_valid & OBD_MD_FLGROUP)) {
1798 oa->o_valid |= OBD_MD_FLGROUP;
1799 oa->o_gr = FILTER_GROUP_ECHO;
1801 /* assume we can touch filter native objects with echo device. */
1802 /* LASSERT(oa->o_gr == FILTER_GROUP_ECHO); */
1805 case OBD_IOC_CREATE: /* may create echo object */
1806 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1807 GOTO (out, rc = -EPERM);
1809 rc = echo_create_object (ed, 1, oa,
1810 data->ioc_pbuf1, data->ioc_plen1,
1814 case OBD_IOC_DESTROY:
1815 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1816 GOTO (out, rc = -EPERM);
1818 rc = echo_get_object (&eco, ed, oa);
1820 rc = obd_destroy(ec->ec_exp, oa, eco->eo_lsm,
1821 &dummy_oti, NULL, NULL);
1823 eco->eo_deleted = 1;
1824 echo_put_object(eco);
1828 case OBD_IOC_GETATTR:
1829 rc = echo_get_object (&eco, ed, oa);
1831 struct obd_info oinfo = { { { 0 } } };
1832 oinfo.oi_md = eco->eo_lsm;
1834 rc = obd_getattr(ec->ec_exp, &oinfo);
1835 echo_put_object(eco);
1839 case OBD_IOC_SETATTR:
1840 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1841 GOTO (out, rc = -EPERM);
1843 rc = echo_get_object (&eco, ed, oa);
1845 struct obd_info oinfo = { { { 0 } } };
1847 oinfo.oi_md = eco->eo_lsm;
1849 rc = obd_setattr(ec->ec_exp, &oinfo, NULL);
1850 echo_put_object(eco);
1854 case OBD_IOC_BRW_WRITE:
1855 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1856 GOTO (out, rc = -EPERM);
1860 case OBD_IOC_BRW_READ:
1861 rc = echo_client_brw_ioctl(rw, exp, data);
1864 case ECHO_IOC_GET_STRIPE:
1865 rc = echo_get_object(&eco, ed, oa);
1867 rc = echo_copyout_lsm(eco->eo_lsm, data->ioc_pbuf1,
1869 echo_put_object(eco);
1873 case ECHO_IOC_SET_STRIPE:
1874 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1875 GOTO (out, rc = -EPERM);
1877 if (data->ioc_pbuf1 == NULL) { /* unset */
1878 rc = echo_get_object(&eco, ed, oa);
1880 eco->eo_deleted = 1;
1881 echo_put_object(eco);
1884 rc = echo_create_object(ed, 0, oa,
1886 data->ioc_plen1, &dummy_oti);
1890 case ECHO_IOC_ENQUEUE:
1891 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1892 GOTO (out, rc = -EPERM);
1894 rc = echo_client_enqueue(exp, oa,
1895 data->ioc_conn1, /* lock mode */
1897 data->ioc_count);/*extent*/
1900 case ECHO_IOC_CANCEL:
1901 rc = echo_client_cancel(exp, oa);
1905 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
1906 GOTO (out, rc = -ENOTTY);
1912 /* XXX this should be in a helper also called by target_send_reply */
1913 for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
1915 if (!ack_lock->mode)
1917 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
1925 static int echo_client_setup(struct obd_device *obddev, struct lustre_cfg *lcfg)
1927 struct echo_client_obd *ec = &obddev->u.echo_client;
1928 struct obd_device *tgt;
1929 struct obd_uuid echo_uuid = { "ECHO_UUID" };
1930 struct obd_connect_data *ocd = NULL;
1934 if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1935 CERROR("requires a TARGET OBD name\n");
1939 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
1940 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
1941 CERROR("device not attached or not set up (%s)\n",
1942 lustre_cfg_string(lcfg, 1));
1946 spin_lock_init (&ec->ec_lock);
1947 CFS_INIT_LIST_HEAD (&ec->ec_objects);
1948 CFS_INIT_LIST_HEAD (&ec->ec_locks);
1950 ec->ec_nstripes = 0;
1952 OBD_ALLOC(ocd, sizeof(*ocd));
1954 CERROR("Can't alloc ocd connecting to %s\n",
1955 lustre_cfg_string(lcfg, 1));
1959 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
1961 ocd->ocd_version = LUSTRE_VERSION_CODE;
1962 ocd->ocd_group = FILTER_GROUP_ECHO;
1964 rc = obd_connect(NULL, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
1966 /* Turn off pinger because it connects to tgt obd directly. */
1967 spin_lock(&tgt->obd_dev_lock);
1968 list_del_init(&ec->ec_exp->exp_obd_chain_timed);
1969 spin_unlock(&tgt->obd_dev_lock);
1972 OBD_FREE(ocd, sizeof(*ocd));
1975 CERROR("fail to connect to device %s\n",
1976 lustre_cfg_string(lcfg, 1));
1983 static int echo_client_cleanup(struct obd_device *obddev)
1985 struct echo_client_obd *ec = &obddev->u.echo_client;
1989 if (!list_empty(&obddev->obd_exports)) {
1990 CERROR("still has clients!\n");
1994 LASSERT(atomic_read(&ec->ec_exp->exp_refcount) > 0);
1995 rc = obd_disconnect(ec->ec_exp);
1997 CERROR("fail to disconnect device: %d\n", rc);
2002 static int echo_client_connect(const struct lu_env *env,
2003 struct obd_export **exp,
2004 struct obd_device *src, struct obd_uuid *cluuid,
2005 struct obd_connect_data *data, void *localdata)
2008 struct lustre_handle conn = { 0 };
2011 rc = class_connect(&conn, src, cluuid);
2013 *exp = class_conn2export(&conn);
2019 static int echo_client_disconnect(struct obd_export *exp)
2022 struct obd_device *obd;
2023 struct echo_client_obd *ec;
2024 struct ec_lock *ecl;
2030 GOTO(out, rc = -EINVAL);
2034 ec = &obd->u.echo_client;
2036 /* no more contention on export's lock list */
2037 while (!list_empty (&exp->exp_ec_data.eced_locks)) {
2038 ecl = list_entry (exp->exp_ec_data.eced_locks.next,
2039 struct ec_lock, ecl_exp_chain);
2040 list_del (&ecl->ecl_exp_chain);
2042 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm,
2043 ecl->ecl_mode, &ecl->ecl_lock_handle);
2045 CDEBUG (D_INFO, "Cancel lock on object "LPX64" on disconnect "
2046 "(%d)\n", ecl->ecl_object->eco_id, rc);
2048 echo_put_object (ecl->ecl_object);
2049 OBD_FREE (ecl, sizeof (*ecl));
2053 rc = class_disconnect(exp);
2059 static struct obd_ops echo_obd_ops = {
2060 .o_owner = THIS_MODULE,
2063 .o_setup = echo_client_setup,
2064 .o_cleanup = echo_client_cleanup,
2067 .o_iocontrol = echo_client_iocontrol,
2068 .o_connect = echo_client_connect,
2069 .o_disconnect = echo_client_disconnect
2072 int echo_client_init(void)
2074 struct lprocfs_static_vars lvars = { 0 };
2077 lprocfs_echo_init_vars(&lvars);
2078 rc = class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
2079 LUSTRE_ECHO_CLIENT_NAME, &echo_device_type);
2081 lu_kmem_init(echo_caches);
2085 void echo_client_exit(void)
2087 class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
2088 lu_kmem_fini(echo_caches);
2091 /** @} echo_client */