4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
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>
50 #include <lustre_fid.h>
51 #include <lustre_acl.h>
52 #include <lustre_net.h>
55 #include "echo_internal.h"
57 /** \defgroup echo_client Echo Client
62 struct cl_device ed_cl;
63 struct echo_client_obd *ed_ec;
65 struct cl_site ed_site_myself;
66 struct cl_site *ed_site;
67 struct lu_device *ed_next;
70 struct lu_client_seq *ed_cl_seq;
74 struct cl_object eo_cl;
75 struct cl_object_header eo_hdr;
77 struct echo_device *eo_dev;
78 cfs_list_t eo_obj_chain;
79 struct lov_stripe_md *eo_lsm;
80 cfs_atomic_t eo_npages;
84 struct echo_object_conf {
85 struct cl_object_conf eoc_cl;
86 struct lov_stripe_md **eoc_md;
90 struct cl_page_slice ep_cl;
92 cfs_page_t *ep_vmpage;
96 struct cl_lock_slice el_cl;
98 struct echo_object *el_object;
100 cfs_atomic_t el_refcount;
104 struct cl_io_slice ei_cl;
109 struct cl_req_slice er_cl;
113 static int echo_client_setup(const struct lu_env *env,
114 struct obd_device *obddev,
115 struct lustre_cfg *lcfg);
116 static int echo_client_cleanup(struct obd_device *obddev);
119 /** \defgroup echo_helpers Helper functions
122 static inline struct echo_device *cl2echo_dev(const struct cl_device *dev)
124 return container_of0(dev, struct echo_device, ed_cl);
127 static inline struct cl_device *echo_dev2cl(struct echo_device *d)
132 static inline struct echo_device *obd2echo_dev(const struct obd_device *obd)
134 return cl2echo_dev(lu2cl_dev(obd->obd_lu_dev));
137 static inline struct cl_object *echo_obj2cl(struct echo_object *eco)
142 static inline struct echo_object *cl2echo_obj(const struct cl_object *o)
144 return container_of(o, struct echo_object, eo_cl);
147 static inline struct echo_page *cl2echo_page(const struct cl_page_slice *s)
149 return container_of(s, struct echo_page, ep_cl);
152 static inline struct echo_lock *cl2echo_lock(const struct cl_lock_slice *s)
154 return container_of(s, struct echo_lock, el_cl);
157 static inline struct cl_lock *echo_lock2cl(const struct echo_lock *ecl)
159 return ecl->el_cl.cls_lock;
162 static struct lu_context_key echo_thread_key;
163 static inline struct echo_thread_info *echo_env_info(const struct lu_env *env)
165 struct echo_thread_info *info;
166 info = lu_context_key_get(&env->le_ctx, &echo_thread_key);
167 LASSERT(info != NULL);
172 struct echo_object_conf *cl2echo_conf(const struct cl_object_conf *c)
174 return container_of(c, struct echo_object_conf, eoc_cl);
177 static inline void lsm2fid(struct lov_stripe_md *lsm, struct lu_fid *fid)
180 fid->f_seq = FID_SEQ_ECHO;
181 /* truncated to 32 bits by assignment */
182 fid->f_oid = lsm->lsm_object_id;
183 fid->f_ver = lsm->lsm_object_id >> 32;
185 /** @} echo_helpers */
187 static struct echo_object *cl_echo_object_find(struct echo_device *d,
188 struct lov_stripe_md **lsm);
189 static int cl_echo_object_put(struct echo_object *eco);
190 static int cl_echo_enqueue (struct echo_object *eco, obd_off start,
191 obd_off end, int mode, __u64 *cookie);
192 static int cl_echo_cancel (struct echo_device *d, __u64 cookie);
193 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
194 cfs_page_t **pages, int npages, int async);
196 static struct echo_thread_info *echo_env_info(const struct lu_env *env);
198 struct echo_thread_info {
199 struct echo_object_conf eti_conf;
200 struct lustre_md eti_md;
202 struct cl_2queue eti_queue;
204 struct cl_lock_descr eti_descr;
205 struct lu_fid eti_fid;
206 struct lu_fid eti_fid2;
207 struct md_op_spec eti_spec;
208 struct lov_mds_md_v3 eti_lmm;
209 struct lov_user_md_v3 eti_lum;
210 struct md_attr eti_ma;
211 struct lu_name eti_lname;
212 /* per-thread values, can be re-used */
216 struct lu_buf eti_buf;
217 char eti_xattr_buf[LUSTRE_POSIX_ACL_MAX_SIZE];
220 /* No session used right now */
221 struct echo_session_info {
225 static cfs_mem_cache_t *echo_page_kmem;
226 static cfs_mem_cache_t *echo_lock_kmem;
227 static cfs_mem_cache_t *echo_object_kmem;
228 static cfs_mem_cache_t *echo_thread_kmem;
229 static cfs_mem_cache_t *echo_session_kmem;
230 //static cfs_mem_cache_t *echo_req_kmem;
232 static struct lu_kmem_descr echo_caches[] = {
234 .ckd_cache = &echo_page_kmem,
235 .ckd_name = "echo_page_kmem",
236 .ckd_size = sizeof (struct echo_page)
239 .ckd_cache = &echo_lock_kmem,
240 .ckd_name = "echo_lock_kmem",
241 .ckd_size = sizeof (struct echo_lock)
244 .ckd_cache = &echo_object_kmem,
245 .ckd_name = "echo_object_kmem",
246 .ckd_size = sizeof (struct echo_object)
249 .ckd_cache = &echo_thread_kmem,
250 .ckd_name = "echo_thread_kmem",
251 .ckd_size = sizeof (struct echo_thread_info)
254 .ckd_cache = &echo_session_kmem,
255 .ckd_name = "echo_session_kmem",
256 .ckd_size = sizeof (struct echo_session_info)
260 .ckd_cache = &echo_req_kmem,
261 .ckd_name = "echo_req_kmem",
262 .ckd_size = sizeof (struct echo_req)
270 /** \defgroup echo_page Page operations
272 * Echo page operations.
276 static cfs_page_t *echo_page_vmpage(const struct lu_env *env,
277 const struct cl_page_slice *slice)
279 return cl2echo_page(slice)->ep_vmpage;
282 static int echo_page_own(const struct lu_env *env,
283 const struct cl_page_slice *slice,
284 struct cl_io *io, int nonblock)
286 struct echo_page *ep = cl2echo_page(slice);
289 mutex_lock(&ep->ep_lock);
290 else if (!mutex_trylock(&ep->ep_lock))
295 static void echo_page_disown(const struct lu_env *env,
296 const struct cl_page_slice *slice,
299 struct echo_page *ep = cl2echo_page(slice);
301 LASSERT(mutex_is_locked(&ep->ep_lock));
302 mutex_unlock(&ep->ep_lock);
305 static void echo_page_discard(const struct lu_env *env,
306 const struct cl_page_slice *slice,
307 struct cl_io *unused)
309 cl_page_delete(env, slice->cpl_page);
312 static int echo_page_is_vmlocked(const struct lu_env *env,
313 const struct cl_page_slice *slice)
315 if (mutex_is_locked(&cl2echo_page(slice)->ep_lock))
320 static void echo_page_completion(const struct lu_env *env,
321 const struct cl_page_slice *slice,
324 LASSERT(slice->cpl_page->cp_sync_io != NULL);
327 static void echo_page_fini(const struct lu_env *env,
328 struct cl_page_slice *slice)
330 struct echo_page *ep = cl2echo_page(slice);
331 struct echo_object *eco = cl2echo_obj(slice->cpl_obj);
332 cfs_page_t *vmpage = ep->ep_vmpage;
335 cfs_atomic_dec(&eco->eo_npages);
336 page_cache_release(vmpage);
337 OBD_SLAB_FREE_PTR(ep, echo_page_kmem);
341 static int echo_page_prep(const struct lu_env *env,
342 const struct cl_page_slice *slice,
343 struct cl_io *unused)
348 static int echo_page_print(const struct lu_env *env,
349 const struct cl_page_slice *slice,
350 void *cookie, lu_printer_t printer)
352 struct echo_page *ep = cl2echo_page(slice);
354 (*printer)(env, cookie, LUSTRE_ECHO_CLIENT_NAME"-page@%p %d vm@%p\n",
355 ep, mutex_is_locked(&ep->ep_lock), ep->ep_vmpage);
359 static const struct cl_page_operations echo_page_ops = {
360 .cpo_own = echo_page_own,
361 .cpo_disown = echo_page_disown,
362 .cpo_discard = echo_page_discard,
363 .cpo_vmpage = echo_page_vmpage,
364 .cpo_fini = echo_page_fini,
365 .cpo_print = echo_page_print,
366 .cpo_is_vmlocked = echo_page_is_vmlocked,
369 .cpo_prep = echo_page_prep,
370 .cpo_completion = echo_page_completion,
373 .cpo_prep = echo_page_prep,
374 .cpo_completion = echo_page_completion,
380 /** \defgroup echo_lock Locking
382 * echo lock operations
386 static void echo_lock_fini(const struct lu_env *env,
387 struct cl_lock_slice *slice)
389 struct echo_lock *ecl = cl2echo_lock(slice);
391 LASSERT(cfs_list_empty(&ecl->el_chain));
392 OBD_SLAB_FREE_PTR(ecl, echo_lock_kmem);
395 static void echo_lock_delete(const struct lu_env *env,
396 const struct cl_lock_slice *slice)
398 struct echo_lock *ecl = cl2echo_lock(slice);
400 LASSERT(cfs_list_empty(&ecl->el_chain));
403 static int echo_lock_fits_into(const struct lu_env *env,
404 const struct cl_lock_slice *slice,
405 const struct cl_lock_descr *need,
406 const struct cl_io *unused)
411 static struct cl_lock_operations echo_lock_ops = {
412 .clo_fini = echo_lock_fini,
413 .clo_delete = echo_lock_delete,
414 .clo_fits_into = echo_lock_fits_into
419 /** \defgroup echo_cl_ops cl_object operations
421 * operations for cl_object
425 static struct cl_page *echo_page_init(const struct lu_env *env,
426 struct cl_object *obj,
427 struct cl_page *page, cfs_page_t *vmpage)
429 struct echo_page *ep;
432 OBD_SLAB_ALLOC_PTR_GFP(ep, echo_page_kmem, CFS_ALLOC_IO);
434 struct echo_object *eco = cl2echo_obj(obj);
435 ep->ep_vmpage = vmpage;
436 page_cache_get(vmpage);
437 mutex_init(&ep->ep_lock);
438 cl_page_slice_add(page, &ep->ep_cl, obj, &echo_page_ops);
439 cfs_atomic_inc(&eco->eo_npages);
441 RETURN(ERR_PTR(ep ? 0 : -ENOMEM));
444 static int echo_io_init(const struct lu_env *env, struct cl_object *obj,
450 static int echo_lock_init(const struct lu_env *env,
451 struct cl_object *obj, struct cl_lock *lock,
452 const struct cl_io *unused)
454 struct echo_lock *el;
457 OBD_SLAB_ALLOC_PTR_GFP(el, echo_lock_kmem, CFS_ALLOC_IO);
459 cl_lock_slice_add(lock, &el->el_cl, obj, &echo_lock_ops);
460 el->el_object = cl2echo_obj(obj);
461 CFS_INIT_LIST_HEAD(&el->el_chain);
462 cfs_atomic_set(&el->el_refcount, 0);
464 RETURN(el == NULL ? -ENOMEM : 0);
467 static int echo_conf_set(const struct lu_env *env, struct cl_object *obj,
468 const struct cl_object_conf *conf)
473 static const struct cl_object_operations echo_cl_obj_ops = {
474 .coo_page_init = echo_page_init,
475 .coo_lock_init = echo_lock_init,
476 .coo_io_init = echo_io_init,
477 .coo_conf_set = echo_conf_set
479 /** @} echo_cl_ops */
481 /** \defgroup echo_lu_ops lu_object operations
483 * operations for echo lu object.
487 static int echo_object_init(const struct lu_env *env, struct lu_object *obj,
488 const struct lu_object_conf *conf)
490 struct echo_device *ed = cl2echo_dev(lu2cl_dev(obj->lo_dev));
491 struct echo_client_obd *ec = ed->ed_ec;
492 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
496 struct lu_object *below;
497 struct lu_device *under;
500 below = under->ld_ops->ldo_object_alloc(env, obj->lo_header,
504 lu_object_add(obj, below);
507 if (!ed->ed_next_ismd) {
508 const struct cl_object_conf *cconf = lu2cl_conf(conf);
509 struct echo_object_conf *econf = cl2echo_conf(cconf);
511 LASSERT(econf->eoc_md);
512 eco->eo_lsm = *econf->eoc_md;
513 /* clear the lsm pointer so that it won't get freed. */
514 *econf->eoc_md = NULL;
520 cfs_atomic_set(&eco->eo_npages, 0);
522 spin_lock(&ec->ec_lock);
523 cfs_list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
524 spin_unlock(&ec->ec_lock);
529 /* taken from osc_unpackmd() */
530 static int echo_alloc_memmd(struct echo_device *ed,
531 struct lov_stripe_md **lsmp)
537 /* If export is lov/osc then use their obd method */
538 if (ed->ed_next != NULL)
539 return obd_alloc_memmd(ed->ed_ec->ec_exp, lsmp);
540 /* OFD has no unpackmd method, do everything here */
541 lsm_size = lov_stripe_md_size(1);
543 LASSERT(*lsmp == NULL);
544 OBD_ALLOC(*lsmp, lsm_size);
548 OBD_ALLOC((*lsmp)->lsm_oinfo[0], sizeof(struct lov_oinfo));
549 if ((*lsmp)->lsm_oinfo[0] == NULL) {
550 OBD_FREE(*lsmp, lsm_size);
554 loi_init((*lsmp)->lsm_oinfo[0]);
555 (*lsmp)->lsm_maxbytes = LUSTRE_STRIPE_MAXBYTES;
560 static int echo_free_memmd(struct echo_device *ed, struct lov_stripe_md **lsmp)
566 /* If export is lov/osc then use their obd method */
567 if (ed->ed_next != NULL)
568 return obd_free_memmd(ed->ed_ec->ec_exp, lsmp);
569 /* OFD has no unpackmd method, do everything here */
570 lsm_size = lov_stripe_md_size(1);
572 LASSERT(*lsmp != NULL);
573 OBD_FREE((*lsmp)->lsm_oinfo[0], sizeof(struct lov_oinfo));
574 OBD_FREE(*lsmp, lsm_size);
579 static void echo_object_free(const struct lu_env *env, struct lu_object *obj)
581 struct echo_object *eco = cl2echo_obj(lu2cl(obj));
582 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
585 LASSERT(cfs_atomic_read(&eco->eo_npages) == 0);
587 spin_lock(&ec->ec_lock);
588 cfs_list_del_init(&eco->eo_obj_chain);
589 spin_unlock(&ec->ec_lock);
592 lu_object_header_fini(obj->lo_header);
595 echo_free_memmd(eco->eo_dev, &eco->eo_lsm);
596 OBD_SLAB_FREE_PTR(eco, echo_object_kmem);
600 static int echo_object_print(const struct lu_env *env, void *cookie,
601 lu_printer_t p, const struct lu_object *o)
603 struct echo_object *obj = cl2echo_obj(lu2cl(o));
605 return (*p)(env, cookie, "echoclient-object@%p", obj);
608 static const struct lu_object_operations echo_lu_obj_ops = {
609 .loo_object_init = echo_object_init,
610 .loo_object_delete = NULL,
611 .loo_object_release = NULL,
612 .loo_object_free = echo_object_free,
613 .loo_object_print = echo_object_print,
614 .loo_object_invariant = NULL
616 /** @} echo_lu_ops */
618 /** \defgroup echo_lu_dev_ops lu_device operations
620 * Operations for echo lu device.
624 static struct lu_object *echo_object_alloc(const struct lu_env *env,
625 const struct lu_object_header *hdr,
626 struct lu_device *dev)
628 struct echo_object *eco;
629 struct lu_object *obj = NULL;
632 /* we're the top dev. */
633 LASSERT(hdr == NULL);
634 OBD_SLAB_ALLOC_PTR_GFP(eco, echo_object_kmem, CFS_ALLOC_IO);
636 struct cl_object_header *hdr = &eco->eo_hdr;
638 obj = &echo_obj2cl(eco)->co_lu;
639 cl_object_header_init(hdr);
640 lu_object_init(obj, &hdr->coh_lu, dev);
641 lu_object_add_top(&hdr->coh_lu, obj);
643 eco->eo_cl.co_ops = &echo_cl_obj_ops;
644 obj->lo_ops = &echo_lu_obj_ops;
649 static struct lu_device_operations echo_device_lu_ops = {
650 .ldo_object_alloc = echo_object_alloc,
653 /** @} echo_lu_dev_ops */
655 static struct cl_device_operations echo_device_cl_ops = {
658 /** \defgroup echo_init Setup and teardown
660 * Init and fini functions for echo client.
664 static int echo_site_init(const struct lu_env *env, struct echo_device *ed)
666 struct cl_site *site = &ed->ed_site_myself;
669 /* initialize site */
670 rc = cl_site_init(site, &ed->ed_cl);
672 CERROR("Cannot initilize site for echo client(%d)\n", rc);
676 rc = lu_site_init_finish(&site->cs_lu);
684 static void echo_site_fini(const struct lu_env *env, struct echo_device *ed)
687 if (!ed->ed_next_ismd)
688 cl_site_fini(ed->ed_site);
693 static void *echo_thread_key_init(const struct lu_context *ctx,
694 struct lu_context_key *key)
696 struct echo_thread_info *info;
698 OBD_SLAB_ALLOC_PTR_GFP(info, echo_thread_kmem, CFS_ALLOC_IO);
700 info = ERR_PTR(-ENOMEM);
704 static void echo_thread_key_fini(const struct lu_context *ctx,
705 struct lu_context_key *key, void *data)
707 struct echo_thread_info *info = data;
708 OBD_SLAB_FREE_PTR(info, echo_thread_kmem);
711 static void echo_thread_key_exit(const struct lu_context *ctx,
712 struct lu_context_key *key, void *data)
716 static struct lu_context_key echo_thread_key = {
717 .lct_tags = LCT_CL_THREAD,
718 .lct_init = echo_thread_key_init,
719 .lct_fini = echo_thread_key_fini,
720 .lct_exit = echo_thread_key_exit
723 static void *echo_session_key_init(const struct lu_context *ctx,
724 struct lu_context_key *key)
726 struct echo_session_info *session;
728 OBD_SLAB_ALLOC_PTR_GFP(session, echo_session_kmem, CFS_ALLOC_IO);
730 session = ERR_PTR(-ENOMEM);
734 static void echo_session_key_fini(const struct lu_context *ctx,
735 struct lu_context_key *key, void *data)
737 struct echo_session_info *session = data;
738 OBD_SLAB_FREE_PTR(session, echo_session_kmem);
741 static void echo_session_key_exit(const struct lu_context *ctx,
742 struct lu_context_key *key, void *data)
746 static struct lu_context_key echo_session_key = {
747 .lct_tags = LCT_SESSION,
748 .lct_init = echo_session_key_init,
749 .lct_fini = echo_session_key_fini,
750 .lct_exit = echo_session_key_exit
753 LU_TYPE_INIT_FINI(echo, &echo_thread_key, &echo_session_key);
755 #define ECHO_SEQ_WIDTH 0xffffffff
756 static int echo_fid_init(struct echo_device *ed, char *obd_name,
763 OBD_ALLOC_PTR(ed->ed_cl_seq);
764 if (ed->ed_cl_seq == NULL)
767 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
769 GOTO(out_free_seq, rc = -ENOMEM);
771 snprintf(prefix, MAX_OBD_NAME + 5, "srv-%s", obd_name);
773 /* Init client side sequence-manager */
774 rc = seq_client_init(ed->ed_cl_seq, NULL,
776 prefix, ms->ms_server_seq);
777 ed->ed_cl_seq->lcs_width = ECHO_SEQ_WIDTH;
778 OBD_FREE(prefix, MAX_OBD_NAME + 5);
780 GOTO(out_free_seq, rc);
785 OBD_FREE_PTR(ed->ed_cl_seq);
786 ed->ed_cl_seq = NULL;
790 static int echo_fid_fini(struct obd_device *obddev)
792 struct echo_device *ed = obd2echo_dev(obddev);
795 if (ed->ed_cl_seq != NULL) {
796 seq_client_fini(ed->ed_cl_seq);
797 OBD_FREE_PTR(ed->ed_cl_seq);
798 ed->ed_cl_seq = NULL;
804 static struct lu_device *echo_device_alloc(const struct lu_env *env,
805 struct lu_device_type *t,
806 struct lustre_cfg *cfg)
808 struct lu_device *next;
809 struct echo_device *ed;
810 struct cl_device *cd;
811 struct obd_device *obd = NULL; /* to keep compiler happy */
812 struct obd_device *tgt;
813 const char *tgt_type_name;
820 GOTO(out, rc = -ENOMEM);
824 rc = cl_device_init(cd, t);
828 cd->cd_lu_dev.ld_ops = &echo_device_lu_ops;
829 cd->cd_ops = &echo_device_cl_ops;
832 obd = class_name2obd(lustre_cfg_string(cfg, 0));
833 LASSERT(obd != NULL);
834 LASSERT(env != NULL);
836 tgt = class_name2obd(lustre_cfg_string(cfg, 1));
838 CERROR("Can not find tgt device %s\n",
839 lustre_cfg_string(cfg, 1));
840 GOTO(out, rc = -ENODEV);
843 next = tgt->obd_lu_dev;
844 if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
845 ed->ed_next_ismd = 1;
847 ed->ed_next_ismd = 0;
848 rc = echo_site_init(env, ed);
854 rc = echo_client_setup(env, obd, cfg);
858 ed->ed_ec = &obd->u.echo_client;
861 if (ed->ed_next_ismd) {
862 /* Suppose to connect to some Metadata layer */
864 struct lu_device *ld;
868 CERROR("%s is not lu device type!\n",
869 lustre_cfg_string(cfg, 1));
870 GOTO(out, rc = -EINVAL);
873 tgt_type_name = lustre_cfg_string(cfg, 2);
874 if (!tgt_type_name) {
875 CERROR("%s no type name for echo %s setup\n",
876 lustre_cfg_string(cfg, 1),
877 tgt->obd_type->typ_name);
878 GOTO(out, rc = -EINVAL);
883 spin_lock(&ls->ls_ld_lock);
884 cfs_list_for_each_entry(ld, &ls->ls_ld_linkage, ld_linkage) {
885 if (strcmp(ld->ld_type->ldt_name, tgt_type_name) == 0) {
890 spin_unlock(&ls->ls_ld_lock);
893 CERROR("%s is not lu device type!\n",
894 lustre_cfg_string(cfg, 1));
895 GOTO(out, rc = -EINVAL);
899 /* For MD echo client, it will use the site in MDS stack */
900 ed->ed_site_myself.cs_lu = *ls;
901 ed->ed_site = &ed->ed_site_myself;
902 ed->ed_cl.cd_lu_dev.ld_site = &ed->ed_site_myself.cs_lu;
903 rc = echo_fid_init(ed, obd->obd_name, lu_site2md(ls));
905 CERROR("echo fid init error %d\n", rc);
909 /* if echo client is to be stacked upon ost device, the next is
910 * NULL since ost is not a clio device so far */
911 if (next != NULL && !lu_device_is_cl(next))
914 tgt_type_name = tgt->obd_type->typ_name;
916 LASSERT(next != NULL);
917 if (next->ld_site != NULL)
918 GOTO(out, rc = -EBUSY);
920 next->ld_site = &ed->ed_site->cs_lu;
921 rc = next->ld_type->ldt_ops->ldto_device_init(env, next,
922 next->ld_type->ldt_name,
927 /* Tricky case, I have to determine the obd type since
928 * CLIO uses the different parameters to initialize
929 * objects for lov & osc. */
930 if (strcmp(tgt_type_name, LUSTRE_LOV_NAME) == 0)
931 ed->ed_next_islov = 1;
933 LASSERT(strcmp(tgt_type_name,
934 LUSTRE_OSC_NAME) == 0);
936 LASSERT(strcmp(tgt_type_name, LUSTRE_OST_NAME) == 0);
940 RETURN(&cd->cd_lu_dev);
945 rc2 = echo_client_cleanup(obd);
947 CERROR("Cleanup obd device %s error(%d)\n",
952 echo_site_fini(env, ed);
954 cl_device_fini(&ed->ed_cl);
964 static int echo_device_init(const struct lu_env *env, struct lu_device *d,
965 const char *name, struct lu_device *next)
971 static struct lu_device *echo_device_fini(const struct lu_env *env,
974 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
975 struct lu_device *next = ed->ed_next;
977 while (next && !ed->ed_next_ismd)
978 next = next->ld_type->ldt_ops->ldto_device_fini(env, next);
982 static void echo_lock_release(const struct lu_env *env,
983 struct echo_lock *ecl,
986 struct cl_lock *clk = echo_lock2cl(ecl);
990 cl_lock_release(env, clk, "ec enqueue", ecl->el_object);
992 cl_lock_mutex_get(env, clk);
993 cl_lock_cancel(env, clk);
994 cl_lock_delete(env, clk);
995 cl_lock_mutex_put(env, clk);
997 cl_lock_put(env, clk);
1000 static struct lu_device *echo_device_free(const struct lu_env *env,
1001 struct lu_device *d)
1003 struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
1004 struct echo_client_obd *ec = ed->ed_ec;
1005 struct echo_object *eco;
1006 struct lu_device *next = ed->ed_next;
1008 CDEBUG(D_INFO, "echo device:%p is going to be freed, next = %p\n",
1011 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
1013 /* check if there are objects still alive.
1014 * It shouldn't have any object because lu_site_purge would cleanup
1015 * all of cached objects. Anyway, probably the echo device is being
1016 * parallelly accessed.
1018 spin_lock(&ec->ec_lock);
1019 cfs_list_for_each_entry(eco, &ec->ec_objects, eo_obj_chain)
1020 eco->eo_deleted = 1;
1021 spin_unlock(&ec->ec_lock);
1024 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
1027 "Waiting for the reference of echo object to be dropped\n");
1029 /* Wait for the last reference to be dropped. */
1030 spin_lock(&ec->ec_lock);
1031 while (!cfs_list_empty(&ec->ec_objects)) {
1032 spin_unlock(&ec->ec_lock);
1033 CERROR("echo_client still has objects at cleanup time, "
1034 "wait for 1 second\n");
1035 cfs_schedule_timeout_and_set_state(CFS_TASK_UNINT,
1036 cfs_time_seconds(1));
1037 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
1038 spin_lock(&ec->ec_lock);
1040 spin_unlock(&ec->ec_lock);
1042 LASSERT(cfs_list_empty(&ec->ec_locks));
1044 CDEBUG(D_INFO, "No object exists, exiting...\n");
1046 echo_client_cleanup(d->ld_obd);
1047 echo_fid_fini(d->ld_obd);
1048 while (next && !ed->ed_next_ismd)
1049 next = next->ld_type->ldt_ops->ldto_device_free(env, next);
1051 LASSERT(ed->ed_site == lu2cl_site(d->ld_site));
1052 echo_site_fini(env, ed);
1053 cl_device_fini(&ed->ed_cl);
1059 static const struct lu_device_type_operations echo_device_type_ops = {
1060 .ldto_init = echo_type_init,
1061 .ldto_fini = echo_type_fini,
1063 .ldto_start = echo_type_start,
1064 .ldto_stop = echo_type_stop,
1066 .ldto_device_alloc = echo_device_alloc,
1067 .ldto_device_free = echo_device_free,
1068 .ldto_device_init = echo_device_init,
1069 .ldto_device_fini = echo_device_fini
1072 static struct lu_device_type echo_device_type = {
1073 .ldt_tags = LU_DEVICE_CL,
1074 .ldt_name = LUSTRE_ECHO_CLIENT_NAME,
1075 .ldt_ops = &echo_device_type_ops,
1076 .ldt_ctx_tags = LCT_CL_THREAD | LCT_MD_THREAD | LCT_DT_THREAD,
1080 /** \defgroup echo_exports Exported operations
1082 * exporting functions to echo client
1087 /* Interfaces to echo client obd device */
1088 static struct echo_object *cl_echo_object_find(struct echo_device *d,
1089 struct lov_stripe_md **lsmp)
1092 struct echo_thread_info *info;
1093 struct echo_object_conf *conf;
1094 struct lov_stripe_md *lsm;
1095 struct echo_object *eco;
1096 struct cl_object *obj;
1104 LASSERT(lsm->lsm_object_id);
1106 /* Never return an object if the obd is to be freed. */
1107 if (echo_dev2cl(d)->cd_lu_dev.ld_obd->obd_stopping)
1108 RETURN(ERR_PTR(-ENODEV));
1110 env = cl_env_get(&refcheck);
1112 RETURN((void *)env);
1114 info = echo_env_info(env);
1115 conf = &info->eti_conf;
1117 if (!d->ed_next_islov) {
1118 struct lov_oinfo *oinfo = lsm->lsm_oinfo[0];
1119 LASSERT(oinfo != NULL);
1120 oinfo->loi_id = lsm->lsm_object_id;
1121 oinfo->loi_seq = lsm->lsm_object_seq;
1122 conf->eoc_cl.u.coc_oinfo = oinfo;
1124 struct lustre_md *md;
1126 memset(md, 0, sizeof *md);
1128 conf->eoc_cl.u.coc_md = md;
1131 conf->eoc_md = lsmp;
1133 fid = &info->eti_fid;
1136 /* In the function below, .hs_keycmp resolves to
1137 * lu_obj_hop_keycmp() */
1138 /* coverity[overrun-buffer-val] */
1139 obj = cl_object_find(env, echo_dev2cl(d), fid, &conf->eoc_cl);
1141 GOTO(out, eco = (void*)obj);
1143 eco = cl2echo_obj(obj);
1144 if (eco->eo_deleted) {
1145 cl_object_put(env, obj);
1146 eco = ERR_PTR(-EAGAIN);
1150 cl_env_put(env, &refcheck);
1154 static int cl_echo_object_put(struct echo_object *eco)
1157 struct cl_object *obj = echo_obj2cl(eco);
1161 env = cl_env_get(&refcheck);
1163 RETURN(PTR_ERR(env));
1165 /* an external function to kill an object? */
1166 if (eco->eo_deleted) {
1167 struct lu_object_header *loh = obj->co_lu.lo_header;
1168 LASSERT(&eco->eo_hdr == luh2coh(loh));
1169 set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
1172 cl_object_put(env, obj);
1173 cl_env_put(env, &refcheck);
1177 static int cl_echo_enqueue0(struct lu_env *env, struct echo_object *eco,
1178 obd_off start, obd_off end, int mode,
1179 __u64 *cookie , __u32 enqflags)
1182 struct cl_lock *lck;
1183 struct cl_object *obj;
1184 struct cl_lock_descr *descr;
1185 struct echo_thread_info *info;
1189 info = echo_env_info(env);
1191 descr = &info->eti_descr;
1192 obj = echo_obj2cl(eco);
1194 descr->cld_obj = obj;
1195 descr->cld_start = cl_index(obj, start);
1196 descr->cld_end = cl_index(obj, end);
1197 descr->cld_mode = mode == LCK_PW ? CLM_WRITE : CLM_READ;
1198 descr->cld_enq_flags = enqflags;
1201 lck = cl_lock_request(env, io, descr, "ec enqueue", eco);
1203 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
1204 struct echo_lock *el;
1206 rc = cl_wait(env, lck);
1208 el = cl2echo_lock(cl_lock_at(lck, &echo_device_type));
1209 spin_lock(&ec->ec_lock);
1210 if (cfs_list_empty(&el->el_chain)) {
1211 cfs_list_add(&el->el_chain, &ec->ec_locks);
1212 el->el_cookie = ++ec->ec_unique;
1214 cfs_atomic_inc(&el->el_refcount);
1215 *cookie = el->el_cookie;
1216 spin_unlock(&ec->ec_lock);
1218 cl_lock_release(env, lck, "ec enqueue", cfs_current());
1224 static int cl_echo_enqueue(struct echo_object *eco, obd_off start, obd_off end,
1225 int mode, __u64 *cookie)
1227 struct echo_thread_info *info;
1234 env = cl_env_get(&refcheck);
1236 RETURN(PTR_ERR(env));
1238 info = echo_env_info(env);
1241 io->ci_ignore_layout = 1;
1242 result = cl_io_init(env, io, CIT_MISC, echo_obj2cl(eco));
1245 LASSERT(result == 0);
1247 result = cl_echo_enqueue0(env, eco, start, end, mode, cookie, 0);
1248 cl_io_fini(env, io);
1252 cl_env_put(env, &refcheck);
1256 static int cl_echo_cancel0(struct lu_env *env, struct echo_device *ed,
1259 struct echo_client_obd *ec = ed->ed_ec;
1260 struct echo_lock *ecl = NULL;
1262 int found = 0, still_used = 0;
1265 LASSERT(ec != NULL);
1266 spin_lock(&ec->ec_lock);
1267 cfs_list_for_each (el, &ec->ec_locks) {
1268 ecl = cfs_list_entry (el, struct echo_lock, el_chain);
1269 CDEBUG(D_INFO, "ecl: %p, cookie: "LPX64"\n", ecl, ecl->el_cookie);
1270 found = (ecl->el_cookie == cookie);
1272 if (cfs_atomic_dec_and_test(&ecl->el_refcount))
1273 cfs_list_del_init(&ecl->el_chain);
1279 spin_unlock(&ec->ec_lock);
1284 echo_lock_release(env, ecl, still_used);
1288 static int cl_echo_cancel(struct echo_device *ed, __u64 cookie)
1295 env = cl_env_get(&refcheck);
1297 RETURN(PTR_ERR(env));
1299 rc = cl_echo_cancel0(env, ed, cookie);
1301 cl_env_put(env, &refcheck);
1305 static int cl_echo_async_brw(const struct lu_env *env, struct cl_io *io,
1306 enum cl_req_type unused, struct cl_2queue *queue)
1308 struct cl_page *clp;
1309 struct cl_page *temp;
1313 cl_page_list_for_each_safe(clp, temp, &queue->c2_qin) {
1315 rc = cl_page_cache_add(env, io, clp, CRT_WRITE);
1318 result = result ?: rc;
1323 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
1324 cfs_page_t **pages, int npages, int async)
1327 struct echo_thread_info *info;
1328 struct cl_object *obj = echo_obj2cl(eco);
1329 struct echo_device *ed = eco->eo_dev;
1330 struct cl_2queue *queue;
1332 struct cl_page *clp;
1333 struct lustre_handle lh = { 0 };
1334 int page_size = cl_page_size(obj);
1340 LASSERT((offset & ~CFS_PAGE_MASK) == 0);
1341 LASSERT(ed->ed_next != NULL);
1342 env = cl_env_get(&refcheck);
1344 RETURN(PTR_ERR(env));
1346 info = echo_env_info(env);
1348 queue = &info->eti_queue;
1350 cl_2queue_init(queue);
1352 io->ci_ignore_layout = 1;
1353 rc = cl_io_init(env, io, CIT_MISC, obj);
1359 rc = cl_echo_enqueue0(env, eco, offset,
1360 offset + npages * CFS_PAGE_SIZE - 1,
1361 rw == READ ? LCK_PR : LCK_PW, &lh.cookie,
1364 GOTO(error_lock, rc);
1366 for (i = 0; i < npages; i++) {
1368 clp = cl_page_find(env, obj, cl_index(obj, offset),
1369 pages[i], CPT_TRANSIENT);
1374 LASSERT(clp->cp_type == CPT_TRANSIENT);
1376 rc = cl_page_own(env, io, clp);
1378 LASSERT(clp->cp_state == CPS_FREEING);
1379 cl_page_put(env, clp);
1383 cl_2queue_add(queue, clp);
1385 /* drop the reference count for cl_page_find, so that the page
1386 * will be freed in cl_2queue_fini. */
1387 cl_page_put(env, clp);
1388 cl_page_clip(env, clp, 0, page_size);
1390 offset += page_size;
1394 enum cl_req_type typ = rw == READ ? CRT_READ : CRT_WRITE;
1396 async = async && (typ == CRT_WRITE);
1398 rc = cl_echo_async_brw(env, io, typ, queue);
1400 rc = cl_io_submit_sync(env, io, typ, queue, 0);
1401 CDEBUG(D_INFO, "echo_client %s write returns %d\n",
1402 async ? "async" : "sync", rc);
1405 cl_echo_cancel0(env, ed, lh.cookie);
1408 cl_2queue_discard(env, io, queue);
1409 cl_2queue_disown(env, io, queue);
1410 cl_2queue_fini(env, queue);
1411 cl_io_fini(env, io);
1413 cl_env_put(env, &refcheck);
1416 /** @} echo_exports */
1419 static obd_id last_object_id;
1422 echo_copyout_lsm (struct lov_stripe_md *lsm, void *_ulsm, int ulsm_nob)
1424 struct lov_stripe_md *ulsm = _ulsm;
1427 nob = offsetof (struct lov_stripe_md, lsm_oinfo[lsm->lsm_stripe_count]);
1431 if (cfs_copy_to_user (ulsm, lsm, sizeof(ulsm)))
1434 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1435 if (cfs_copy_to_user (ulsm->lsm_oinfo[i], lsm->lsm_oinfo[i],
1436 sizeof(lsm->lsm_oinfo[0])))
1443 echo_copyin_lsm (struct echo_device *ed, struct lov_stripe_md *lsm,
1444 void *ulsm, int ulsm_nob)
1446 struct echo_client_obd *ec = ed->ed_ec;
1449 if (ulsm_nob < sizeof (*lsm))
1452 if (cfs_copy_from_user (lsm, ulsm, sizeof (*lsm)))
1455 if (lsm->lsm_stripe_count > ec->ec_nstripes ||
1456 lsm->lsm_magic != LOV_MAGIC ||
1457 (lsm->lsm_stripe_size & (~CFS_PAGE_MASK)) != 0 ||
1458 ((__u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count > ~0UL))
1462 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1463 if (cfs_copy_from_user(lsm->lsm_oinfo[i],
1464 ((struct lov_stripe_md *)ulsm)-> \
1466 sizeof(lsm->lsm_oinfo[0])))
1472 static inline void echo_md_build_name(struct lu_name *lname, char *name,
1475 sprintf(name, LPU64, id);
1476 lname->ln_name = name;
1477 lname->ln_namelen = strlen(name);
1480 /* similar to mdt_attr_get_complex */
1481 static int echo_big_lmm_get(const struct lu_env *env, struct md_object *o,
1484 struct echo_thread_info *info = echo_env_info(env);
1489 LASSERT(ma->ma_lmm_size > 0);
1491 rc = mo_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LOV);
1495 /* big_lmm may need to be grown */
1496 if (info->eti_big_lmmsize < rc) {
1497 int size = size_roundup_power2(rc);
1499 if (info->eti_big_lmmsize > 0) {
1500 /* free old buffer */
1501 LASSERT(info->eti_big_lmm);
1502 OBD_FREE_LARGE(info->eti_big_lmm,
1503 info->eti_big_lmmsize);
1504 info->eti_big_lmm = NULL;
1505 info->eti_big_lmmsize = 0;
1508 OBD_ALLOC_LARGE(info->eti_big_lmm, size);
1509 if (info->eti_big_lmm == NULL)
1511 info->eti_big_lmmsize = size;
1513 LASSERT(info->eti_big_lmmsize >= rc);
1515 info->eti_buf.lb_buf = info->eti_big_lmm;
1516 info->eti_buf.lb_len = info->eti_big_lmmsize;
1517 rc = mo_xattr_get(env, o, &info->eti_buf, XATTR_NAME_LOV);
1521 ma->ma_valid |= MA_LOV;
1522 ma->ma_lmm = info->eti_big_lmm;
1523 ma->ma_lmm_size = rc;
1528 int echo_attr_get_complex(const struct lu_env *env, struct md_object *next,
1531 struct echo_thread_info *info = echo_env_info(env);
1532 struct lu_buf *buf = &info->eti_buf;
1533 cfs_umode_t mode = lu_object_attr(&next->mo_lu);
1534 int need = ma->ma_need;
1541 if (need & MA_INODE) {
1542 ma->ma_need = MA_INODE;
1543 rc = mo_attr_get(env, next, ma);
1546 ma->ma_valid |= MA_INODE;
1549 if (need & MA_LOV) {
1550 if (S_ISREG(mode) || S_ISDIR(mode)) {
1551 LASSERT(ma->ma_lmm_size > 0);
1552 buf->lb_buf = ma->ma_lmm;
1553 buf->lb_len = ma->ma_lmm_size;
1554 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LOV);
1556 ma->ma_lmm_size = rc2;
1557 ma->ma_valid |= MA_LOV;
1558 } else if (rc2 == -ENODATA) {
1560 ma->ma_lmm_size = 0;
1561 } else if (rc2 == -ERANGE) {
1562 rc2 = echo_big_lmm_get(env, next, ma);
1564 GOTO(out, rc = rc2);
1566 GOTO(out, rc = rc2);
1571 #ifdef CONFIG_FS_POSIX_ACL
1572 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
1573 buf->lb_buf = ma->ma_acl;
1574 buf->lb_len = ma->ma_acl_size;
1575 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1577 ma->ma_acl_size = rc2;
1578 ma->ma_valid |= MA_ACL_DEF;
1579 } else if (rc2 == -ENODATA) {
1581 ma->ma_acl_size = 0;
1583 GOTO(out, rc = rc2);
1589 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
1590 rc, ma->ma_valid, ma->ma_lmm);
1595 echo_md_create_internal(const struct lu_env *env, struct echo_device *ed,
1596 struct md_object *parent, struct lu_fid *fid,
1597 struct lu_name *lname, struct md_op_spec *spec,
1600 struct lu_object *ec_child, *child;
1601 struct lu_device *ld = ed->ed_next;
1602 struct echo_thread_info *info = echo_env_info(env);
1603 struct lu_fid *fid2 = &info->eti_fid2;
1604 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
1609 rc = mdo_lookup(env, parent, lname, fid2, spec);
1612 else if (rc != -ENOENT)
1615 ec_child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev,
1617 if (IS_ERR(ec_child)) {
1618 CERROR("Can not find the child "DFID": rc = %ld\n", PFID(fid),
1620 RETURN(PTR_ERR(ec_child));
1623 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1624 if (child == NULL) {
1625 CERROR("Can not locate the child "DFID"\n", PFID(fid));
1626 GOTO(out_put, rc = -EINVAL);
1629 CDEBUG(D_RPCTRACE, "Start creating object "DFID" %s %p\n",
1630 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1633 * Do not perform lookup sanity check. We know that name does not exist.
1635 spec->sp_cr_lookup = 0;
1636 rc = mdo_create(env, parent, lname, lu2md(child), spec, ma);
1638 CERROR("Can not create child "DFID": rc = %d\n", PFID(fid), rc);
1641 CDEBUG(D_RPCTRACE, "End creating object "DFID" %s %p rc = %d\n",
1642 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent, rc);
1645 lu_object_put(env, ec_child);
1649 static int echo_set_lmm_size(const struct lu_env *env, struct lu_device *ld,
1652 struct echo_thread_info *info = echo_env_info(env);
1654 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
1655 ma->ma_lmm = (void *)&info->eti_lmm;
1656 ma->ma_lmm_size = sizeof(info->eti_lmm);
1658 LASSERT(info->eti_big_lmmsize);
1659 ma->ma_lmm = info->eti_big_lmm;
1660 ma->ma_lmm_size = info->eti_big_lmmsize;
1666 static int echo_create_md_object(const struct lu_env *env,
1667 struct echo_device *ed,
1668 struct lu_object *ec_parent,
1670 char *name, int namelen,
1671 __u64 id, __u32 mode, int count,
1672 int stripe_count, int stripe_offset)
1674 struct lu_object *parent;
1675 struct echo_thread_info *info = echo_env_info(env);
1676 struct lu_name *lname = &info->eti_lname;
1677 struct md_op_spec *spec = &info->eti_spec;
1678 struct md_attr *ma = &info->eti_ma;
1679 struct lu_device *ld = ed->ed_next;
1685 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1689 memset(ma, 0, sizeof(*ma));
1690 memset(spec, 0, sizeof(*spec));
1691 if (stripe_count != 0) {
1692 spec->sp_cr_flags |= FMODE_WRITE;
1693 echo_set_lmm_size(env, ld, ma);
1694 if (stripe_count != -1) {
1695 struct lov_user_md_v3 *lum = &info->eti_lum;
1697 lum->lmm_magic = LOV_USER_MAGIC_V3;
1698 lum->lmm_stripe_count = stripe_count;
1699 lum->lmm_stripe_offset = stripe_offset;
1700 lum->lmm_pattern = 0;
1701 spec->u.sp_ea.eadata = lum;
1702 spec->u.sp_ea.eadatalen = sizeof(*lum);
1703 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1707 ma->ma_attr.la_mode = mode;
1708 ma->ma_attr.la_valid = LA_CTIME | LA_MODE;
1709 ma->ma_attr.la_ctime = cfs_time_current_64();
1712 lname->ln_name = name;
1713 lname->ln_namelen = namelen;
1714 /* If name is specified, only create one object by name */
1715 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1720 /* Create multiple object sequenced by id */
1721 for (i = 0; i < count; i++) {
1722 char *tmp_name = info->eti_name;
1724 echo_md_build_name(lname, tmp_name, id);
1726 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1729 CERROR("Can not create child %s: rc = %d\n", tmp_name,
1740 static struct lu_object *echo_md_lookup(const struct lu_env *env,
1741 struct echo_device *ed,
1742 struct md_object *parent,
1743 struct lu_name *lname)
1745 struct echo_thread_info *info = echo_env_info(env);
1746 struct lu_fid *fid = &info->eti_fid;
1747 struct lu_object *child;
1751 CDEBUG(D_INFO, "lookup %s in parent "DFID" %p\n", lname->ln_name,
1753 rc = mdo_lookup(env, parent, lname, fid, NULL);
1755 CERROR("lookup %s: rc = %d\n", lname->ln_name, rc);
1756 RETURN(ERR_PTR(rc));
1759 child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1764 static int echo_setattr_object(const struct lu_env *env,
1765 struct echo_device *ed,
1766 struct lu_object *ec_parent,
1767 __u64 id, int count)
1769 struct lu_object *parent;
1770 struct echo_thread_info *info = echo_env_info(env);
1771 struct lu_name *lname = &info->eti_lname;
1772 char *name = info->eti_name;
1773 struct lu_device *ld = ed->ed_next;
1774 struct lu_buf *buf = &info->eti_buf;
1780 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1784 for (i = 0; i < count; i++) {
1785 struct lu_object *ec_child, *child;
1787 echo_md_build_name(lname, name, id);
1789 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1790 if (IS_ERR(ec_child)) {
1791 CERROR("Can't find child %s: rc = %ld\n",
1792 lname->ln_name, PTR_ERR(ec_child));
1793 RETURN(PTR_ERR(ec_child));
1796 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1797 if (child == NULL) {
1798 CERROR("Can not locate the child %s\n", lname->ln_name);
1799 lu_object_put(env, ec_child);
1804 CDEBUG(D_RPCTRACE, "Start setattr object "DFID"\n",
1805 PFID(lu_object_fid(child)));
1807 buf->lb_buf = info->eti_xattr_buf;
1808 buf->lb_len = sizeof(info->eti_xattr_buf);
1810 sprintf(name, "%s.test1", XATTR_USER_PREFIX);
1811 rc = mo_xattr_set(env, lu2md(child), buf, name,
1814 CERROR("Can not setattr child "DFID": rc = %d\n",
1815 PFID(lu_object_fid(child)), rc);
1816 lu_object_put(env, ec_child);
1819 CDEBUG(D_RPCTRACE, "End setattr object "DFID"\n",
1820 PFID(lu_object_fid(child)));
1822 lu_object_put(env, ec_child);
1827 static int echo_getattr_object(const struct lu_env *env,
1828 struct echo_device *ed,
1829 struct lu_object *ec_parent,
1830 __u64 id, int count)
1832 struct lu_object *parent;
1833 struct echo_thread_info *info = echo_env_info(env);
1834 struct lu_name *lname = &info->eti_lname;
1835 char *name = info->eti_name;
1836 struct md_attr *ma = &info->eti_ma;
1837 struct lu_device *ld = ed->ed_next;
1843 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1847 memset(ma, 0, sizeof(*ma));
1848 ma->ma_need |= MA_INODE | MA_LOV | MA_PFID | MA_HSM | MA_ACL_DEF;
1849 ma->ma_acl = info->eti_xattr_buf;
1850 ma->ma_acl_size = sizeof(info->eti_xattr_buf);
1852 for (i = 0; i < count; i++) {
1853 struct lu_object *ec_child, *child;
1856 echo_md_build_name(lname, name, id);
1857 echo_set_lmm_size(env, ld, ma);
1859 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1860 if (IS_ERR(ec_child)) {
1861 CERROR("Can't find child %s: rc = %ld\n",
1862 lname->ln_name, PTR_ERR(ec_child));
1863 RETURN(PTR_ERR(ec_child));
1866 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1867 if (child == NULL) {
1868 CERROR("Can not locate the child %s\n", lname->ln_name);
1869 lu_object_put(env, ec_child);
1873 CDEBUG(D_RPCTRACE, "Start getattr object "DFID"\n",
1874 PFID(lu_object_fid(child)));
1875 rc = echo_attr_get_complex(env, lu2md(child), ma);
1877 CERROR("Can not getattr child "DFID": rc = %d\n",
1878 PFID(lu_object_fid(child)), rc);
1879 lu_object_put(env, ec_child);
1882 CDEBUG(D_RPCTRACE, "End getattr object "DFID"\n",
1883 PFID(lu_object_fid(child)));
1885 lu_object_put(env, ec_child);
1891 static int echo_lookup_object(const struct lu_env *env,
1892 struct echo_device *ed,
1893 struct lu_object *ec_parent,
1894 __u64 id, int count)
1896 struct lu_object *parent;
1897 struct echo_thread_info *info = echo_env_info(env);
1898 struct lu_name *lname = &info->eti_lname;
1899 char *name = info->eti_name;
1900 struct lu_fid *fid = &info->eti_fid;
1901 struct lu_device *ld = ed->ed_next;
1905 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1909 /*prepare the requests*/
1910 for (i = 0; i < count; i++) {
1911 echo_md_build_name(lname, name, id);
1913 CDEBUG(D_RPCTRACE, "Start lookup object "DFID" %s %p\n",
1914 PFID(lu_object_fid(parent)), lname->ln_name, parent);
1916 rc = mdo_lookup(env, lu2md(parent), lname, fid, NULL);
1918 CERROR("Can not lookup child %s: rc = %d\n", name, rc);
1921 CDEBUG(D_RPCTRACE, "End lookup object "DFID" %s %p\n",
1922 PFID(lu_object_fid(parent)), lname->ln_name, parent);
1929 static int echo_md_destroy_internal(const struct lu_env *env,
1930 struct echo_device *ed,
1931 struct md_object *parent,
1932 struct lu_name *lname,
1935 struct lu_device *ld = ed->ed_next;
1936 struct lu_object *ec_child;
1937 struct lu_object *child;
1942 ec_child = echo_md_lookup(env, ed, parent, lname);
1943 if (IS_ERR(ec_child)) {
1944 CERROR("Can't find child %s: rc = %ld\n", lname->ln_name,
1946 RETURN(PTR_ERR(ec_child));
1949 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1950 if (child == NULL) {
1951 CERROR("Can not locate the child %s\n", lname->ln_name);
1952 GOTO(out_put, rc = -EINVAL);
1955 CDEBUG(D_RPCTRACE, "Start destroy object "DFID" %s %p\n",
1956 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1958 rc = mdo_unlink(env, parent, lu2md(child), lname, ma);
1960 CERROR("Can not unlink child %s: rc = %d\n",
1961 lname->ln_name, rc);
1964 CDEBUG(D_RPCTRACE, "End destroy object "DFID" %s %p\n",
1965 PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1967 lu_object_put(env, ec_child);
1971 static int echo_destroy_object(const struct lu_env *env,
1972 struct echo_device *ed,
1973 struct lu_object *ec_parent,
1974 char *name, int namelen,
1975 __u64 id, __u32 mode,
1978 struct echo_thread_info *info = echo_env_info(env);
1979 struct lu_name *lname = &info->eti_lname;
1980 struct md_attr *ma = &info->eti_ma;
1981 struct lu_device *ld = ed->ed_next;
1982 struct lu_object *parent;
1987 parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1991 memset(ma, 0, sizeof(*ma));
1992 ma->ma_attr.la_mode = mode;
1993 ma->ma_attr.la_valid = LA_CTIME;
1994 ma->ma_attr.la_ctime = cfs_time_current_64();
1995 ma->ma_need = MA_INODE;
1999 lname->ln_name = name;
2000 lname->ln_namelen = namelen;
2001 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
2006 /*prepare the requests*/
2007 for (i = 0; i < count; i++) {
2008 char *tmp_name = info->eti_name;
2011 echo_md_build_name(lname, tmp_name, id);
2013 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
2016 CERROR("Can not unlink child %s: rc = %d\n", name, rc);
2025 static struct lu_object *echo_resolve_path(const struct lu_env *env,
2026 struct echo_device *ed, char *path,
2029 struct lu_device *ld = ed->ed_next;
2030 struct md_device *md = lu2md_dev(ld);
2031 struct echo_thread_info *info = echo_env_info(env);
2032 struct lu_fid *fid = &info->eti_fid;
2033 struct lu_name *lname = &info->eti_lname;
2034 struct lu_object *parent = NULL;
2035 struct lu_object *child = NULL;
2039 /*Only support MDD layer right now*/
2040 rc = md->md_ops->mdo_root_get(env, md, fid);
2042 CERROR("get root error: rc = %d\n", rc);
2043 RETURN(ERR_PTR(rc));
2046 parent = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
2047 if (IS_ERR(parent)) {
2048 CERROR("Can not find the parent "DFID": rc = %ld\n",
2049 PFID(fid), PTR_ERR(parent));
2054 struct lu_object *ld_parent;
2057 e = strsep(&path, "/");
2062 if (!path || path[0] == '\0')
2068 lname->ln_namelen = strlen(e);
2070 ld_parent = lu_object_locate(parent->lo_header, ld->ld_type);
2071 if (ld_parent == NULL) {
2072 lu_object_put(env, parent);
2077 child = echo_md_lookup(env, ed, lu2md(ld_parent), lname);
2078 lu_object_put(env, parent);
2079 if (IS_ERR(child)) {
2080 rc = (int)PTR_ERR(child);
2081 CERROR("lookup %s under parent "DFID": rc = %d\n",
2082 lname->ln_name, PFID(lu_object_fid(ld_parent)),
2089 RETURN(ERR_PTR(rc));
2094 #define ECHO_MD_CTX_TAG (LCT_REMEMBER | LCT_MD_THREAD)
2095 #define ECHO_MD_SES_TAG (LCT_REMEMBER | LCT_SESSION)
2096 static int echo_md_handler(struct echo_device *ed, int command,
2097 char *path, int path_len, int id, int count,
2098 struct obd_ioctl_data *data)
2100 struct echo_thread_info *info;
2101 struct lu_device *ld = ed->ed_next;
2104 struct lu_object *parent;
2106 int namelen = data->ioc_plen2;
2111 CERROR("MD echo client is not being initialized properly\n");
2115 if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
2116 CERROR("Only support MDD layer right now!\n");
2120 env = cl_env_get(&refcheck);
2122 RETURN(PTR_ERR(env));
2124 rc = lu_env_refill_by_tags(env, ECHO_MD_CTX_TAG, ECHO_MD_SES_TAG);
2128 /* init big_lmm buffer */
2129 info = echo_env_info(env);
2130 LASSERT(info->eti_big_lmm == NULL);
2131 OBD_ALLOC_LARGE(info->eti_big_lmm, MIN_MD_SIZE);
2132 if (info->eti_big_lmm == NULL)
2133 GOTO(out_env, rc = -ENOMEM);
2134 info->eti_big_lmmsize = MIN_MD_SIZE;
2136 parent = echo_resolve_path(env, ed, path, path_len);
2137 if (IS_ERR(parent)) {
2138 CERROR("Can not resolve the path %s: rc = %ld\n", path,
2140 GOTO(out_free, rc = PTR_ERR(parent));
2144 OBD_ALLOC(name, namelen + 1);
2146 GOTO(out_put, rc = -ENOMEM);
2147 if (cfs_copy_from_user(name, data->ioc_pbuf2, namelen))
2148 GOTO(out_name, rc = -EFAULT);
2152 case ECHO_MD_CREATE:
2153 case ECHO_MD_MKDIR: {
2154 struct echo_thread_info *info = echo_env_info(env);
2155 __u32 mode = data->ioc_obdo2.o_mode;
2156 struct lu_fid *fid = &info->eti_fid;
2157 int stripe_count = (int)data->ioc_obdo2.o_misc;
2158 int stripe_index = (int)data->ioc_obdo2.o_stripe_idx;
2160 fid->f_seq = data->ioc_obdo1.o_seq;
2161 fid->f_oid = (__u32)data->ioc_obdo1.o_id;
2163 /* In the function below, .hs_keycmp resolves to
2164 * lu_obj_hop_keycmp() */
2165 /* coverity[overrun-buffer-val] */
2166 rc = echo_create_md_object(env, ed, parent, fid, name, namelen,
2167 id, mode, count, stripe_count,
2171 case ECHO_MD_DESTROY:
2172 case ECHO_MD_RMDIR: {
2173 __u32 mode = data->ioc_obdo2.o_mode;
2175 rc = echo_destroy_object(env, ed, parent, name, namelen,
2179 case ECHO_MD_LOOKUP:
2180 rc = echo_lookup_object(env, ed, parent, id, count);
2182 case ECHO_MD_GETATTR:
2183 rc = echo_getattr_object(env, ed, parent, id, count);
2185 case ECHO_MD_SETATTR:
2186 rc = echo_setattr_object(env, ed, parent, id, count);
2189 CERROR("unknown command %d\n", command);
2195 OBD_FREE(name, namelen + 1);
2197 lu_object_put(env, parent);
2199 LASSERT(info->eti_big_lmm);
2200 OBD_FREE_LARGE(info->eti_big_lmm, info->eti_big_lmmsize);
2201 info->eti_big_lmm = NULL;
2202 info->eti_big_lmmsize = 0;
2204 cl_env_put(env, &refcheck);
2208 static int echo_create_object(const struct lu_env *env, struct echo_device *ed,
2209 int on_target, struct obdo *oa, void *ulsm,
2210 int ulsm_nob, struct obd_trans_info *oti)
2212 struct echo_object *eco;
2213 struct echo_client_obd *ec = ed->ed_ec;
2214 struct lov_stripe_md *lsm = NULL;
2219 if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
2220 (on_target || /* set_stripe */
2221 ec->ec_nstripes != 0)) { /* LOV */
2222 CERROR ("No valid oid\n");
2226 rc = echo_alloc_memmd(ed, &lsm);
2228 CERROR("Cannot allocate md: rc = %d\n", rc);
2235 rc = echo_copyin_lsm (ed, lsm, ulsm, ulsm_nob);
2239 if (lsm->lsm_stripe_count == 0)
2240 lsm->lsm_stripe_count = ec->ec_nstripes;
2242 if (lsm->lsm_stripe_size == 0)
2243 lsm->lsm_stripe_size = CFS_PAGE_SIZE;
2247 /* setup stripes: indices + default ids if required */
2248 for (i = 0; i < lsm->lsm_stripe_count; i++) {
2249 if (lsm->lsm_oinfo[i]->loi_id == 0)
2250 lsm->lsm_oinfo[i]->loi_id = lsm->lsm_object_id;
2252 lsm->lsm_oinfo[i]->loi_ost_idx =
2253 (idx + i) % ec->ec_nstripes;
2257 /* setup object ID here for !on_target and LOV hint */
2258 if (oa->o_valid & OBD_MD_FLID)
2259 lsm->lsm_object_id = oa->o_id;
2261 if (lsm->lsm_object_id == 0)
2262 lsm->lsm_object_id = ++last_object_id;
2266 /* Only echo objects are allowed to be created */
2267 LASSERT((oa->o_valid & OBD_MD_FLGROUP) &&
2268 (oa->o_seq == FID_SEQ_ECHO));
2269 rc = obd_create(env, ec->ec_exp, oa, &lsm, oti);
2271 CERROR("Cannot create objects: rc = %d\n", rc);
2277 /* See what object ID we were given */
2278 oa->o_id = lsm->lsm_object_id;
2279 oa->o_valid |= OBD_MD_FLID;
2281 eco = cl_echo_object_find(ed, &lsm);
2283 GOTO(failed, rc = PTR_ERR(eco));
2284 cl_echo_object_put(eco);
2286 CDEBUG(D_INFO, "oa->o_id = %lx\n", (long)oa->o_id);
2291 obd_destroy(env, ec->ec_exp, oa, lsm, oti, NULL, NULL);
2293 echo_free_memmd(ed, &lsm);
2295 CERROR("create object failed with: rc = %d\n", rc);
2299 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
2302 struct lov_stripe_md *lsm = NULL;
2303 struct echo_object *eco;
2307 if ((oa->o_valid & OBD_MD_FLID) == 0 ||
2308 oa->o_id == 0) /* disallow use of object id 0 */
2310 CERROR ("No valid oid\n");
2314 rc = echo_alloc_memmd(ed, &lsm);
2318 lsm->lsm_object_id = oa->o_id;
2319 if (oa->o_valid & OBD_MD_FLGROUP)
2320 lsm->lsm_object_seq = oa->o_seq;
2322 lsm->lsm_object_seq = FID_SEQ_ECHO;
2325 eco = cl_echo_object_find(ed, &lsm);
2331 echo_free_memmd(ed, &lsm);
2335 static void echo_put_object(struct echo_object *eco)
2337 if (cl_echo_object_put(eco))
2338 CERROR("echo client: drop an object failed");
2342 echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
2344 unsigned long stripe_count;
2345 unsigned long stripe_size;
2346 unsigned long width;
2347 unsigned long woffset;
2351 if (lsm->lsm_stripe_count <= 1)
2355 stripe_size = lsm->lsm_stripe_size;
2356 stripe_count = lsm->lsm_stripe_count;
2358 /* width = # bytes in all stripes */
2359 width = stripe_size * stripe_count;
2361 /* woffset = offset within a width; offset = whole number of widths */
2362 woffset = do_div (offset, width);
2364 stripe_index = woffset / stripe_size;
2366 *idp = lsm->lsm_oinfo[stripe_index]->loi_id;
2367 *offp = offset * stripe_size + woffset % stripe_size;
2371 echo_client_page_debug_setup(struct lov_stripe_md *lsm,
2372 cfs_page_t *page, int rw, obd_id id,
2373 obd_off offset, obd_off count)
2380 /* no partial pages on the client */
2381 LASSERT(count == CFS_PAGE_SIZE);
2383 addr = cfs_kmap(page);
2385 for (delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2386 if (rw == OBD_BRW_WRITE) {
2387 stripe_off = offset + delta;
2389 echo_get_stripe_off_id(lsm, &stripe_off, &stripe_id);
2391 stripe_off = 0xdeadbeef00c0ffeeULL;
2392 stripe_id = 0xdeadbeef00c0ffeeULL;
2394 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
2395 stripe_off, stripe_id);
2401 static int echo_client_page_debug_check(struct lov_stripe_md *lsm,
2402 cfs_page_t *page, obd_id id,
2403 obd_off offset, obd_off count)
2412 /* no partial pages on the client */
2413 LASSERT(count == CFS_PAGE_SIZE);
2415 addr = cfs_kmap(page);
2417 for (rc = delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2418 stripe_off = offset + delta;
2420 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
2422 rc2 = block_debug_check("test_brw",
2423 addr + delta, OBD_ECHO_BLOCK_SIZE,
2424 stripe_off, stripe_id);
2426 CERROR ("Error in echo object "LPX64"\n", id);
2435 static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
2436 struct echo_object *eco, obd_off offset,
2437 obd_size count, int async,
2438 struct obd_trans_info *oti)
2440 struct lov_stripe_md *lsm = eco->eo_lsm;
2442 struct brw_page *pga;
2443 struct brw_page *pgp;
2453 verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
2454 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
2455 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
2457 gfp_mask = ((oa->o_id & 2) == 0) ? CFS_ALLOC_STD : CFS_ALLOC_HIGHUSER;
2459 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
2460 LASSERT(lsm != NULL);
2461 LASSERT(lsm->lsm_object_id == oa->o_id);
2464 (count & (~CFS_PAGE_MASK)) != 0)
2467 /* XXX think again with misaligned I/O */
2468 npages = count >> CFS_PAGE_SHIFT;
2470 if (rw == OBD_BRW_WRITE)
2471 brw_flags = OBD_BRW_ASYNC;
2473 OBD_ALLOC(pga, npages * sizeof(*pga));
2477 OBD_ALLOC(pages, npages * sizeof(*pages));
2478 if (pages == NULL) {
2479 OBD_FREE(pga, npages * sizeof(*pga));
2483 for (i = 0, pgp = pga, off = offset;
2485 i++, pgp++, off += CFS_PAGE_SIZE) {
2487 LASSERT (pgp->pg == NULL); /* for cleanup */
2490 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
2491 if (pgp->pg == NULL)
2495 pgp->count = CFS_PAGE_SIZE;
2497 pgp->flag = brw_flags;
2500 echo_client_page_debug_setup(lsm, pgp->pg, rw,
2501 oa->o_id, off, pgp->count);
2504 /* brw mode can only be used at client */
2505 LASSERT(ed->ed_next != NULL);
2506 rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
2509 if (rc != 0 || rw != OBD_BRW_READ)
2512 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
2513 if (pgp->pg == NULL)
2518 vrc = echo_client_page_debug_check(lsm, pgp->pg, oa->o_id,
2519 pgp->off, pgp->count);
2520 if (vrc != 0 && rc == 0)
2523 OBD_PAGE_FREE(pgp->pg);
2525 OBD_FREE(pga, npages * sizeof(*pga));
2526 OBD_FREE(pages, npages * sizeof(*pages));
2530 static int echo_client_prep_commit(const struct lu_env *env,
2531 struct obd_export *exp, int rw,
2532 struct obdo *oa, struct echo_object *eco,
2533 obd_off offset, obd_size count,
2534 obd_size batch, struct obd_trans_info *oti,
2537 struct lov_stripe_md *lsm = eco->eo_lsm;
2538 struct obd_ioobj ioo;
2539 struct niobuf_local *lnb;
2540 struct niobuf_remote *rnb;
2542 obd_size npages, tot_pages;
2543 int i, ret = 0, brw_flags = 0;
2547 if (count <= 0 || (count & (~CFS_PAGE_MASK)) != 0 ||
2548 (lsm != NULL && lsm->lsm_object_id != oa->o_id))
2551 npages = batch >> CFS_PAGE_SHIFT;
2552 tot_pages = count >> CFS_PAGE_SHIFT;
2554 OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
2555 OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
2557 if (lnb == NULL || rnb == NULL)
2558 GOTO(out, ret = -ENOMEM);
2560 if (rw == OBD_BRW_WRITE && async)
2561 brw_flags |= OBD_BRW_ASYNC;
2563 obdo_to_ioobj(oa, &ioo);
2567 for(; tot_pages; tot_pages -= npages) {
2570 if (tot_pages < npages)
2573 for (i = 0; i < npages; i++, off += CFS_PAGE_SIZE) {
2574 rnb[i].offset = off;
2575 rnb[i].len = CFS_PAGE_SIZE;
2576 rnb[i].flags = brw_flags;
2579 ioo.ioo_bufcnt = npages;
2580 oti->oti_transno = 0;
2583 ret = obd_preprw(env, rw, exp, oa, 1, &ioo, rnb, &lpages,
2587 LASSERT(lpages == npages);
2589 for (i = 0; i < lpages; i++) {
2590 cfs_page_t *page = lnb[i].page;
2592 /* read past eof? */
2593 if (page == NULL && lnb[i].rc == 0)
2597 lnb[i].flags |= OBD_BRW_ASYNC;
2599 if (oa->o_id == ECHO_PERSISTENT_OBJID ||
2600 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
2601 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
2604 if (rw == OBD_BRW_WRITE)
2605 echo_client_page_debug_setup(lsm, page, rw,
2610 echo_client_page_debug_check(lsm, page,
2616 ret = obd_commitrw(env, rw, exp, oa, 1, &ioo,
2617 rnb, npages, lnb, oti, ret);
2621 /* Reset oti otherwise it would confuse ldiskfs. */
2622 memset(oti, 0, sizeof(*oti));
2627 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
2629 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
2633 static int echo_client_brw_ioctl(const struct lu_env *env, int rw,
2634 struct obd_export *exp,
2635 struct obd_ioctl_data *data,
2636 struct obd_trans_info *dummy_oti)
2638 struct obd_device *obd = class_exp2obd(exp);
2639 struct echo_device *ed = obd2echo_dev(obd);
2640 struct echo_client_obd *ec = ed->ed_ec;
2641 struct obdo *oa = &data->ioc_obdo1;
2642 struct echo_object *eco;
2648 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2650 rc = echo_get_object(&eco, ed, oa);
2654 oa->o_valid &= ~OBD_MD_FLHANDLE;
2656 /* OFD/obdfilter works only via prep/commit */
2657 test_mode = (long)data->ioc_pbuf1;
2661 if (ed->ed_next == NULL && test_mode != 3) {
2663 data->ioc_plen1 = data->ioc_count;
2666 /* Truncate batch size to maximum */
2667 if (data->ioc_plen1 > PTLRPC_MAX_BRW_SIZE)
2668 data->ioc_plen1 = PTLRPC_MAX_BRW_SIZE;
2670 switch (test_mode) {
2674 rc = echo_client_kbrw(ed, rw, oa,
2675 eco, data->ioc_offset,
2676 data->ioc_count, async, dummy_oti);
2679 rc = echo_client_prep_commit(env, ec->ec_exp, rw, oa,
2680 eco, data->ioc_offset,
2681 data->ioc_count, data->ioc_plen1,
2687 echo_put_object(eco);
2692 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
2693 int mode, obd_off offset, obd_size nob)
2695 struct echo_device *ed = obd2echo_dev(exp->exp_obd);
2696 struct lustre_handle *ulh = &oa->o_handle;
2697 struct echo_object *eco;
2702 if (ed->ed_next == NULL)
2703 RETURN(-EOPNOTSUPP);
2705 if (!(mode == LCK_PR || mode == LCK_PW))
2708 if ((offset & (~CFS_PAGE_MASK)) != 0 ||
2709 (nob & (~CFS_PAGE_MASK)) != 0)
2712 rc = echo_get_object (&eco, ed, oa);
2716 end = (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
2717 rc = cl_echo_enqueue(eco, offset, end, mode, &ulh->cookie);
2719 oa->o_valid |= OBD_MD_FLHANDLE;
2720 CDEBUG(D_INFO, "Cookie is "LPX64"\n", ulh->cookie);
2722 echo_put_object(eco);
2727 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
2729 struct echo_device *ed = obd2echo_dev(exp->exp_obd);
2730 __u64 cookie = oa->o_handle.cookie;
2732 if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
2735 CDEBUG(D_INFO, "Cookie is "LPX64"\n", cookie);
2736 return cl_echo_cancel(ed, cookie);
2740 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2741 void *karg, void *uarg)
2743 struct obd_device *obd = exp->exp_obd;
2744 struct echo_device *ed = obd2echo_dev(obd);
2745 struct echo_client_obd *ec = ed->ed_ec;
2746 struct echo_object *eco;
2747 struct obd_ioctl_data *data = karg;
2748 struct obd_trans_info dummy_oti;
2750 struct oti_req_ack_lock *ack_lock;
2753 int rw = OBD_BRW_READ;
2758 memset(&dummy_oti, 0, sizeof(dummy_oti));
2760 oa = &data->ioc_obdo1;
2761 if (!(oa->o_valid & OBD_MD_FLGROUP)) {
2762 oa->o_valid |= OBD_MD_FLGROUP;
2763 oa->o_seq = FID_SEQ_ECHO;
2766 /* This FID is unpacked just for validation at this point */
2767 rc = fid_ostid_unpack(&fid, &oa->o_oi, 0);
2775 rc = lu_env_init(env, LCT_DT_THREAD);
2777 GOTO(out, rc = -ENOMEM);
2780 case OBD_IOC_CREATE: /* may create echo object */
2781 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2782 GOTO (out, rc = -EPERM);
2784 rc = echo_create_object(env, ed, 1, oa, data->ioc_pbuf1,
2785 data->ioc_plen1, &dummy_oti);
2788 case OBD_IOC_ECHO_MD: {
2795 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2796 GOTO(out, rc = -EPERM);
2798 count = data->ioc_count;
2799 cmd = data->ioc_command;
2801 id = data->ioc_obdo2.o_id;
2803 dirlen = data->ioc_plen1;
2804 OBD_ALLOC(dir, dirlen + 1);
2806 GOTO(out, rc = -ENOMEM);
2808 if (cfs_copy_from_user(dir, data->ioc_pbuf1, dirlen)) {
2809 OBD_FREE(dir, data->ioc_plen1 + 1);
2810 GOTO(out, rc = -EFAULT);
2813 rc = echo_md_handler(ed, cmd, dir, dirlen, id, count, data);
2814 OBD_FREE(dir, dirlen + 1);
2817 case OBD_IOC_ECHO_ALLOC_SEQ: {
2818 struct lu_env *cl_env;
2823 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2824 GOTO(out, rc = -EPERM);
2826 cl_env = cl_env_get(&refcheck);
2828 GOTO(out, rc = PTR_ERR(cl_env));
2830 rc = lu_env_refill_by_tags(cl_env, ECHO_MD_CTX_TAG,
2833 cl_env_put(cl_env, &refcheck);
2837 rc = seq_client_get_seq(cl_env, ed->ed_cl_seq, &seq);
2838 cl_env_put(cl_env, &refcheck);
2840 CERROR("%s: Can not alloc seq: rc = %d\n",
2845 if (cfs_copy_to_user(data->ioc_pbuf1, &seq, data->ioc_plen1))
2848 max_count = LUSTRE_SEQ_MAX_WIDTH;
2849 if (cfs_copy_to_user(data->ioc_pbuf2, &max_count,
2854 case OBD_IOC_DESTROY:
2855 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2856 GOTO (out, rc = -EPERM);
2858 rc = echo_get_object(&eco, ed, oa);
2860 rc = obd_destroy(env, ec->ec_exp, oa, eco->eo_lsm,
2861 &dummy_oti, NULL, NULL);
2863 eco->eo_deleted = 1;
2864 echo_put_object(eco);
2868 case OBD_IOC_GETATTR:
2869 rc = echo_get_object(&eco, ed, oa);
2871 struct obd_info oinfo = { { { 0 } } };
2872 oinfo.oi_md = eco->eo_lsm;
2874 rc = obd_getattr(env, ec->ec_exp, &oinfo);
2875 echo_put_object(eco);
2879 case OBD_IOC_SETATTR:
2880 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2881 GOTO (out, rc = -EPERM);
2883 rc = echo_get_object(&eco, ed, oa);
2885 struct obd_info oinfo = { { { 0 } } };
2887 oinfo.oi_md = eco->eo_lsm;
2889 rc = obd_setattr(env, ec->ec_exp, &oinfo, NULL);
2890 echo_put_object(eco);
2894 case OBD_IOC_BRW_WRITE:
2895 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2896 GOTO (out, rc = -EPERM);
2900 case OBD_IOC_BRW_READ:
2901 rc = echo_client_brw_ioctl(env, rw, exp, data, &dummy_oti);
2904 case ECHO_IOC_GET_STRIPE:
2905 rc = echo_get_object(&eco, ed, oa);
2907 rc = echo_copyout_lsm(eco->eo_lsm, data->ioc_pbuf1,
2909 echo_put_object(eco);
2913 case ECHO_IOC_SET_STRIPE:
2914 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2915 GOTO (out, rc = -EPERM);
2917 if (data->ioc_pbuf1 == NULL) { /* unset */
2918 rc = echo_get_object(&eco, ed, oa);
2920 eco->eo_deleted = 1;
2921 echo_put_object(eco);
2924 rc = echo_create_object(env, ed, 0, oa,
2926 data->ioc_plen1, &dummy_oti);
2930 case ECHO_IOC_ENQUEUE:
2931 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2932 GOTO (out, rc = -EPERM);
2934 rc = echo_client_enqueue(exp, oa,
2935 data->ioc_conn1, /* lock mode */
2937 data->ioc_count);/*extent*/
2940 case ECHO_IOC_CANCEL:
2941 rc = echo_client_cancel(exp, oa);
2945 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
2946 GOTO (out, rc = -ENOTTY);
2954 /* XXX this should be in a helper also called by target_send_reply */
2955 for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
2957 if (!ack_lock->mode)
2959 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
2965 static int echo_client_setup(const struct lu_env *env,
2966 struct obd_device *obddev, struct lustre_cfg *lcfg)
2968 struct echo_client_obd *ec = &obddev->u.echo_client;
2969 struct obd_device *tgt;
2970 struct obd_uuid echo_uuid = { "ECHO_UUID" };
2971 struct obd_connect_data *ocd = NULL;
2975 if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
2976 CERROR("requires a TARGET OBD name\n");
2980 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
2981 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
2982 CERROR("device not attached or not set up (%s)\n",
2983 lustre_cfg_string(lcfg, 1));
2987 spin_lock_init(&ec->ec_lock);
2988 CFS_INIT_LIST_HEAD (&ec->ec_objects);
2989 CFS_INIT_LIST_HEAD (&ec->ec_locks);
2991 ec->ec_nstripes = 0;
2993 if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
2994 lu_context_tags_update(ECHO_MD_CTX_TAG);
2995 lu_session_tags_update(ECHO_MD_SES_TAG);
2999 OBD_ALLOC(ocd, sizeof(*ocd));
3001 CERROR("Can't alloc ocd connecting to %s\n",
3002 lustre_cfg_string(lcfg, 1));
3006 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
3007 OBD_CONNECT_GRANT | OBD_CONNECT_FULL20 |
3008 OBD_CONNECT_64BITHASH | OBD_CONNECT_LVB_TYPE;
3009 ocd->ocd_version = LUSTRE_VERSION_CODE;
3010 ocd->ocd_group = FID_SEQ_ECHO;
3012 rc = obd_connect(env, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
3014 /* Turn off pinger because it connects to tgt obd directly. */
3015 spin_lock(&tgt->obd_dev_lock);
3016 cfs_list_del_init(&ec->ec_exp->exp_obd_chain_timed);
3017 spin_unlock(&tgt->obd_dev_lock);
3020 OBD_FREE(ocd, sizeof(*ocd));
3023 CERROR("fail to connect to device %s\n",
3024 lustre_cfg_string(lcfg, 1));
3031 static int echo_client_cleanup(struct obd_device *obddev)
3033 struct echo_device *ed = obd2echo_dev(obddev);
3034 struct echo_client_obd *ec = &obddev->u.echo_client;
3038 /*Do nothing for Metadata echo client*/
3042 if (ed->ed_next_ismd) {
3043 lu_context_tags_clear(ECHO_MD_CTX_TAG);
3044 lu_session_tags_clear(ECHO_MD_SES_TAG);
3048 if (!cfs_list_empty(&obddev->obd_exports)) {
3049 CERROR("still has clients!\n");
3053 LASSERT(cfs_atomic_read(&ec->ec_exp->exp_refcount) > 0);
3054 rc = obd_disconnect(ec->ec_exp);
3056 CERROR("fail to disconnect device: %d\n", rc);
3061 static int echo_client_connect(const struct lu_env *env,
3062 struct obd_export **exp,
3063 struct obd_device *src, struct obd_uuid *cluuid,
3064 struct obd_connect_data *data, void *localdata)
3067 struct lustre_handle conn = { 0 };
3070 rc = class_connect(&conn, src, cluuid);
3072 *exp = class_conn2export(&conn);
3078 static int echo_client_disconnect(struct obd_export *exp)
3081 struct obd_device *obd;
3082 struct echo_client_obd *ec;
3083 struct ec_lock *ecl;
3089 GOTO(out, rc = -EINVAL);
3093 ec = &obd->u.echo_client;
3095 /* no more contention on export's lock list */
3096 while (!cfs_list_empty (&exp->exp_ec_data.eced_locks)) {
3097 ecl = cfs_list_entry (exp->exp_ec_data.eced_locks.next,
3098 struct ec_lock, ecl_exp_chain);
3099 cfs_list_del (&ecl->ecl_exp_chain);
3101 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm,
3102 ecl->ecl_mode, &ecl->ecl_lock_handle);
3104 CDEBUG (D_INFO, "Cancel lock on object "LPX64" on disconnect "
3105 "(%d)\n", ecl->ecl_object->eco_id, rc);
3107 echo_put_object (ecl->ecl_object);
3108 OBD_FREE (ecl, sizeof (*ecl));
3112 rc = class_disconnect(exp);
3118 static struct obd_ops echo_client_obd_ops = {
3119 .o_owner = THIS_MODULE,
3122 .o_setup = echo_client_setup,
3123 .o_cleanup = echo_client_cleanup,
3126 .o_iocontrol = echo_client_iocontrol,
3127 .o_connect = echo_client_connect,
3128 .o_disconnect = echo_client_disconnect
3131 int echo_client_init(void)
3133 struct lprocfs_static_vars lvars = { 0 };
3136 lprocfs_echo_init_vars(&lvars);
3138 rc = lu_kmem_init(echo_caches);
3140 rc = class_register_type(&echo_client_obd_ops, NULL,
3142 LUSTRE_ECHO_CLIENT_NAME,
3145 lu_kmem_fini(echo_caches);
3150 void echo_client_exit(void)
3152 class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
3153 lu_kmem_fini(echo_caches);
3157 static int __init obdecho_init(void)
3159 struct lprocfs_static_vars lvars;
3163 LCONSOLE_INFO("Echo OBD driver; http://www.lustre.org/\n");
3165 LASSERT(CFS_PAGE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
3167 lprocfs_echo_init_vars(&lvars);
3169 # ifdef HAVE_SERVER_SUPPORT
3170 rc = echo_persistent_pages_init();
3174 rc = class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
3175 LUSTRE_ECHO_NAME, NULL);
3180 rc = echo_client_init();
3182 # ifdef HAVE_SERVER_SUPPORT
3186 class_unregister_type(LUSTRE_ECHO_NAME);
3188 echo_persistent_pages_fini();
3194 static void /*__exit*/ obdecho_exit(void)
3198 # ifdef HAVE_SERVER_SUPPORT
3199 class_unregister_type(LUSTRE_ECHO_NAME);
3200 echo_persistent_pages_fini();
3204 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3205 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
3206 MODULE_LICENSE("GPL");
3208 cfs_module(obdecho, LUSTRE_VERSION_STRING, obdecho_init, obdecho_exit);
3209 #endif /* __KERNEL__ */
3211 /** @} echo_client */