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.
36 * lustre/obdecho/echo.c
38 * Author: Peter Braam <braam@clusterfs.com>
39 * Author: Andreas Dilger <adilger@clusterfs.com>
43 # define EXPORT_SYMTAB
46 #define DEBUG_SUBSYSTEM S_ECHO
48 #include <obd_support.h>
49 #include <obd_class.h>
51 #include <lustre_debug.h>
52 #include <lustre_dlm.h>
53 #include <lprocfs_status.h>
55 #define ECHO_INIT_OBJID 0x1000000000000000ULL
56 #define ECHO_HANDLE_MAGIC 0xabcd0123fedc9876ULL
58 #define ECHO_PERSISTENT_PAGES (ECHO_PERSISTENT_SIZE >> CFS_PAGE_SHIFT)
59 static cfs_page_t *echo_persistent_pages[ECHO_PERSISTENT_PAGES];
62 LPROC_ECHO_READ_BYTES = 1,
63 LPROC_ECHO_WRITE_BYTES = 2,
64 LPROC_ECHO_LAST = LPROC_ECHO_WRITE_BYTES +1
67 static int echo_connect(const struct lu_env *env,
68 struct lustre_handle *conn, struct obd_device *obd,
69 struct obd_uuid *cluuid, struct obd_connect_data *data,
72 data->ocd_connect_flags &= ECHO_CONNECT_SUPPORTED;
73 return class_connect(conn, obd, cluuid);
76 static int echo_disconnect(struct obd_export *exp)
78 LASSERT (exp != NULL);
80 ldlm_cancel_locks_for_export(exp);
82 /* complete all outstanding replies */
83 spin_lock(&exp->exp_lock);
84 while (!list_empty(&exp->exp_outstanding_replies)) {
85 struct ptlrpc_reply_state *rs =
86 list_entry(exp->exp_outstanding_replies.next,
87 struct ptlrpc_reply_state, rs_exp_list);
88 struct ptlrpc_service *svc = rs->rs_service;
90 spin_lock(&svc->srv_lock);
91 list_del_init(&rs->rs_exp_list);
92 ptlrpc_schedule_difficult_reply(rs);
93 spin_unlock(&svc->srv_lock);
95 spin_unlock(&exp->exp_lock);
97 return class_disconnect(exp);
100 static int echo_init_export(struct obd_export *exp)
102 return ldlm_init_export(exp);
105 static int echo_destroy_export(struct obd_export *exp)
109 target_destroy_export(exp);
110 ldlm_destroy_export(exp);
115 static __u64 echo_next_id(struct obd_device *obddev)
119 spin_lock(&obddev->u.echo.eo_lock);
120 id = ++obddev->u.echo.eo_lastino;
121 spin_unlock(&obddev->u.echo.eo_lock);
126 int echo_create(struct obd_export *exp, struct obdo *oa,
127 struct lov_stripe_md **ea, struct obd_trans_info *oti)
129 struct obd_device *obd = class_exp2obd(exp);
132 CERROR("invalid client cookie "LPX64"\n",
133 exp->exp_handle.h_cookie);
137 if (!(oa->o_mode && S_IFMT)) {
138 CERROR("echo obd: no type!\n");
142 if (!(oa->o_valid & OBD_MD_FLTYPE)) {
143 CERROR("invalid o_valid "LPX64"\n", oa->o_valid);
147 oa->o_id = echo_next_id(obd);
148 oa->o_valid = OBD_MD_FLID;
153 int echo_destroy(struct obd_export *exp, struct obdo *oa,
154 struct lov_stripe_md *ea, struct obd_trans_info *oti,
155 struct obd_export *md_exp)
157 struct obd_device *obd = class_exp2obd(exp);
161 CERROR("invalid client cookie "LPX64"\n",
162 exp->exp_handle.h_cookie);
166 if (!(oa->o_valid & OBD_MD_FLID)) {
167 CERROR("obdo missing FLID valid flag: "LPX64"\n", oa->o_valid);
171 if (oa->o_id > obd->u.echo.eo_lastino || oa->o_id < ECHO_INIT_OBJID) {
172 CERROR("bad destroy objid: "LPX64"\n", oa->o_id);
179 static int echo_getattr(struct obd_export *exp, struct obd_info *oinfo)
181 struct obd_device *obd = class_exp2obd(exp);
182 obd_id id = oinfo->oi_oa->o_id;
186 CERROR("invalid client cookie "LPX64"\n",
187 exp->exp_handle.h_cookie);
191 if (!(oinfo->oi_oa->o_valid & OBD_MD_FLID)) {
192 CERROR("obdo missing FLID valid flag: "LPX64"\n",
193 oinfo->oi_oa->o_valid);
197 obdo_cpy_md(oinfo->oi_oa, &obd->u.echo.eo_oa, oinfo->oi_oa->o_valid);
198 oinfo->oi_oa->o_id = id;
203 static int echo_setattr(struct obd_export *exp, struct obd_info *oinfo,
204 struct obd_trans_info *oti)
206 struct obd_device *obd = class_exp2obd(exp);
210 CERROR("invalid client cookie "LPX64"\n",
211 exp->exp_handle.h_cookie);
215 if (!(oinfo->oi_oa->o_valid & OBD_MD_FLID)) {
216 CERROR("obdo missing FLID valid flag: "LPX64"\n",
217 oinfo->oi_oa->o_valid);
221 memcpy(&obd->u.echo.eo_oa, oinfo->oi_oa, sizeof(*oinfo->oi_oa));
223 if (oinfo->oi_oa->o_id & 4) {
224 /* Save lock to force ACKed reply */
225 ldlm_lock_addref (&obd->u.echo.eo_nl_lock, LCK_NL);
226 oti->oti_ack_locks[0].mode = LCK_NL;
227 oti->oti_ack_locks[0].lock = obd->u.echo.eo_nl_lock;
234 echo_page_debug_setup(cfs_page_t *page, int rw, obd_id id,
235 __u64 offset, int len)
237 int page_offset = offset & ~CFS_PAGE_MASK;
238 char *addr = ((char *)cfs_kmap(page)) + page_offset;
240 if (len % OBD_ECHO_BLOCK_SIZE != 0)
241 CERROR("Unexpected block size %d\n", len);
244 if (rw & OBD_BRW_READ)
245 block_debug_setup(addr, OBD_ECHO_BLOCK_SIZE,
248 block_debug_setup(addr, OBD_ECHO_BLOCK_SIZE,
249 0xecc0ecc0ecc0ecc0ULL,
250 0xecc0ecc0ecc0ecc0ULL);
252 addr += OBD_ECHO_BLOCK_SIZE;
253 offset += OBD_ECHO_BLOCK_SIZE;
254 len -= OBD_ECHO_BLOCK_SIZE;
261 echo_page_debug_check(cfs_page_t *page, obd_id id,
262 __u64 offset, int len)
264 int page_offset = offset & ~CFS_PAGE_MASK;
265 char *addr = ((char *)cfs_kmap(page)) + page_offset;
269 if (len % OBD_ECHO_BLOCK_SIZE != 0)
270 CERROR("Unexpected block size %d\n", len);
273 rc2 = block_debug_check("echo", addr, OBD_ECHO_BLOCK_SIZE,
276 if (rc2 != 0 && rc == 0)
279 addr += OBD_ECHO_BLOCK_SIZE;
280 offset += OBD_ECHO_BLOCK_SIZE;
281 len -= OBD_ECHO_BLOCK_SIZE;
289 /* This allows us to verify that desc_private is passed unmolested */
290 #define DESC_PRIV 0x10293847
292 static int echo_map_nb_to_lb(struct obdo *oa, struct obd_ioobj *obj,
293 struct niobuf_remote *nb, int *pages,
294 struct niobuf_local *lb, int cmd, int *left)
296 int gfp_mask = (obj->ioo_id & 1) ? CFS_ALLOC_HIGHUSER : CFS_ALLOC_STD;
297 int ispersistent = obj->ioo_id == ECHO_PERSISTENT_OBJID;
298 int debug_setup = (!ispersistent &&
299 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
300 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
301 struct niobuf_local *res = lb;
302 obd_off offset = nb->offset;
306 int plen = CFS_PAGE_SIZE - (offset & (CFS_PAGE_SIZE-1));
310 /* check for local buf overflow */
314 res->offset = offset;
316 LASSERT((res->offset & ~CFS_PAGE_MASK) + res->len <= CFS_PAGE_SIZE);
320 (res->offset >> CFS_PAGE_SHIFT) < ECHO_PERSISTENT_PAGES) {
321 res->page = echo_persistent_pages[res->offset >>
323 /* Take extra ref so __free_pages() can be called OK */
324 cfs_get_page (res->page);
326 OBD_PAGE_ALLOC(res->page, gfp_mask);
327 if (res->page == NULL) {
328 CERROR("can't get page for id " LPU64"\n",
334 CDEBUG(D_PAGE, "$$$$ get page %p @ "LPU64" for %d\n",
335 res->page, res->offset, res->len);
337 if (cmd & OBD_BRW_READ)
341 echo_page_debug_setup(res->page, cmd, obj->ioo_id,
342 res->offset, res->len);
355 int echo_preprw(int cmd, struct obd_export *export, struct obdo *oa,
356 int objcount, struct obd_ioobj *obj, struct niobuf_remote *nb,
357 int *pages, struct niobuf_local *res,
358 struct obd_trans_info *oti, struct lustre_capa *unused)
360 struct obd_device *obd;
361 struct niobuf_local *r = res;
367 obd = export->exp_obd;
371 /* Temp fix to stop falling foul of osc_announce_cached() */
372 oa->o_valid &= ~(OBD_MD_FLBLOCKS | OBD_MD_FLGRANT);
374 memset(res, 0, sizeof(*res) * *pages);
376 CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
377 cmd == OBD_BRW_READ ? "reading" : "writing", objcount, *pages);
380 oti->oti_handle = (void *)DESC_PRIV;
385 for (i = 0; i < objcount; i++, obj++) {
388 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++, r++) {
390 rc = echo_map_nb_to_lb(oa, obj, nb, pages,
391 res + *pages, cmd, &left);
393 GOTO(preprw_cleanup, rc);
395 tot_bytes += nb->len;
399 atomic_add(*pages, &obd->u.echo.eo_prep);
401 if (cmd & OBD_BRW_READ)
402 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_READ_BYTES,
405 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
408 CDEBUG(D_PAGE, "%d pages allocated after prep\n",
409 atomic_read(&obd->u.echo.eo_prep));
414 /* It is possible that we would rather handle errors by allow
415 * any already-set-up pages to complete, rather than tearing them
416 * all down again. I believe that this is what the in-kernel
417 * prep/commit operations do.
419 CERROR("cleaning up %u pages (%d obdos)\n", *pages, objcount);
420 for (i = 0; i < *pages; i++) {
421 cfs_kunmap(res[i].page);
422 /* NB if this is a persistent page, __free_pages will just
423 * lose the extra ref gained above */
424 OBD_PAGE_FREE(res[i].page);
426 atomic_dec(&obd->u.echo.eo_prep);
432 int echo_commitrw(int cmd, struct obd_export *export, struct obdo *oa,
433 int objcount, struct obd_ioobj *obj,
434 struct niobuf_remote *rb, int niocount,
435 struct niobuf_local *res, struct obd_trans_info *oti, int rc)
437 struct obd_device *obd;
438 struct niobuf_local *r = res;
442 obd = export->exp_obd;
447 GOTO(commitrw_cleanup, rc);
449 if ((cmd & OBD_BRW_RWMASK) == OBD_BRW_READ) {
450 CDEBUG(D_PAGE, "reading %d obdos with %d IOs\n",
453 CDEBUG(D_PAGE, "writing %d obdos with %d IOs\n",
457 if (niocount && !r) {
458 CERROR("NULL res niobuf with niocount %d\n", niocount);
462 LASSERT(oti == NULL || oti->oti_handle == (void *)DESC_PRIV);
464 for (i = 0; i < objcount; i++, obj++) {
465 int verify = (rc == 0 &&
466 obj->ioo_id != ECHO_PERSISTENT_OBJID &&
467 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
468 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
471 for (j = 0 ; j < obj->ioo_bufcnt ; j++, r++) {
472 cfs_page_t *page = r->page;
476 CERROR("null page objid "LPU64":%p, buf %d/%d\n",
477 obj->ioo_id, page, j, obj->ioo_bufcnt);
478 GOTO(commitrw_cleanup, rc = -EFAULT);
481 addr = cfs_kmap(page);
483 CDEBUG(D_PAGE, "$$$$ use page %p, addr %p@"LPU64"\n",
484 r->page, addr, r->offset);
487 vrc = echo_page_debug_check(page, obj->ioo_id,
489 /* check all the pages always */
490 if (vrc != 0 && rc == 0)
495 /* NB see comment above regarding persistent pages */
497 atomic_dec(&obd->u.echo.eo_prep);
500 CDEBUG(D_PAGE, "%d pages remain after commit\n",
501 atomic_read(&obd->u.echo.eo_prep));
505 CERROR("cleaning up %ld pages (%d obdos)\n",
506 niocount - (long)(r - res) - 1, objcount);
507 while (++r < res + niocount) {
508 cfs_page_t *page = r->page;
510 /* NB see comment above regarding persistent pages */
512 atomic_dec(&obd->u.echo.eo_prep);
517 static int echo_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
519 struct lprocfs_static_vars lvars;
522 struct ldlm_res_id res_id = {.name = {1}};
525 spin_lock_init(&obd->u.echo.eo_lock);
526 obd->u.echo.eo_lastino = ECHO_INIT_OBJID;
528 obd->obd_namespace = ldlm_namespace_new(obd, "echo-tgt",
529 LDLM_NAMESPACE_SERVER,
530 LDLM_NAMESPACE_MODEST);
531 if (obd->obd_namespace == NULL) {
536 rc = ldlm_cli_enqueue_local(obd->obd_namespace, &res_id, LDLM_PLAIN,
537 NULL, LCK_NL, &lock_flags, NULL,
538 ldlm_completion_ast, NULL, NULL,
539 0, NULL, &obd->u.echo.eo_nl_lock);
540 LASSERT (rc == ELDLM_OK);
542 lprocfs_echo_init_vars(&lvars);
543 if (lprocfs_obd_setup(obd, lvars.obd_vars) == 0 &&
544 lprocfs_alloc_obd_stats(obd, LPROC_ECHO_LAST) == 0) {
545 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_READ_BYTES,
546 LPROCFS_CNTR_AVGMINMAX,
547 "read_bytes", "bytes");
548 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
549 LPROCFS_CNTR_AVGMINMAX,
550 "write_bytes", "bytes");
553 ptlrpc_init_client (LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
554 "echo_ldlm_cb_client", &obd->obd_ldlm_client);
558 static int echo_cleanup(struct obd_device *obd)
563 lprocfs_obd_cleanup(obd);
564 lprocfs_free_obd_stats(obd);
566 ldlm_lock_decref (&obd->u.echo.eo_nl_lock, LCK_NL);
568 /* XXX Bug 3413; wait for a bit to ensure the BL callback has
569 * happened before calling ldlm_namespace_free() */
570 cfs_schedule_timeout (CFS_TASK_UNINT, cfs_time_seconds(1));
572 ldlm_namespace_free(obd->obd_namespace, NULL, obd->obd_force);
573 obd->obd_namespace = NULL;
575 leaked = atomic_read(&obd->u.echo.eo_prep);
577 CERROR("%d prep/commitrw pages leaked\n", leaked);
582 static struct obd_ops echo_obd_ops = {
583 .o_owner = THIS_MODULE,
584 .o_connect = echo_connect,
585 .o_disconnect = echo_disconnect,
586 .o_init_export = echo_init_export,
587 .o_destroy_export = echo_destroy_export,
588 .o_create = echo_create,
589 .o_destroy = echo_destroy,
590 .o_getattr = echo_getattr,
591 .o_setattr = echo_setattr,
592 .o_preprw = echo_preprw,
593 .o_commitrw = echo_commitrw,
594 .o_setup = echo_setup,
595 .o_cleanup = echo_cleanup
598 extern int echo_client_init(void);
599 extern void echo_client_exit(void);
602 echo_persistent_pages_fini (void)
606 for (i = 0; i < ECHO_PERSISTENT_PAGES; i++)
607 if (echo_persistent_pages[i] != NULL) {
608 OBD_PAGE_FREE(echo_persistent_pages[i]);
609 echo_persistent_pages[i] = NULL;
614 echo_persistent_pages_init (void)
619 for (i = 0; i < ECHO_PERSISTENT_PAGES; i++) {
620 int gfp_mask = (i < ECHO_PERSISTENT_PAGES/2) ?
621 CFS_ALLOC_STD : CFS_ALLOC_HIGHUSER;
623 OBD_PAGE_ALLOC(pg, gfp_mask);
625 echo_persistent_pages_fini ();
629 memset (cfs_kmap (pg), 0, CFS_PAGE_SIZE);
632 echo_persistent_pages[i] = pg;
638 static int __init obdecho_init(void)
640 struct lprocfs_static_vars lvars;
644 LCONSOLE_INFO("Echo OBD driver; http://www.lustre.org/\n");
646 LASSERT(CFS_PAGE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
648 lprocfs_echo_init_vars(&lvars);
650 rc = echo_persistent_pages_init ();
654 rc = class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
655 LUSTRE_ECHO_NAME, NULL);
659 rc = echo_client_init();
663 class_unregister_type(LUSTRE_ECHO_NAME);
665 echo_persistent_pages_fini ();
670 static void /*__exit*/ obdecho_exit(void)
673 class_unregister_type(LUSTRE_ECHO_NAME);
674 echo_persistent_pages_fini ();
677 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
678 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
679 MODULE_LICENSE("GPL");
681 cfs_module(obdecho, "1.0.0", obdecho_init, obdecho_exit);