1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2010, 2011, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * lustre/obdecho/echo.c
40 * Author: Peter Braam <braam@clusterfs.com>
41 * Author: Andreas Dilger <adilger@clusterfs.com>
45 # define EXPORT_SYMTAB
48 #define DEBUG_SUBSYSTEM S_ECHO
50 #include <obd_support.h>
51 #include <obd_class.h>
52 #include <lustre_debug.h>
53 #include <lustre_dlm.h>
54 #include <lprocfs_status.h>
56 #include "echo_internal.h"
58 /* The echo objid needs to be below 2^32, because regular FID numbers are
59 * limited to 2^32 objects in f_oid for the FID_SEQ_ECHO range. b=23335 */
60 #define ECHO_INIT_OID 0x10000000ULL
61 #define ECHO_HANDLE_MAGIC 0xabcd0123fedc9876ULL
63 #define ECHO_PERSISTENT_PAGES (ECHO_PERSISTENT_SIZE >> CFS_PAGE_SHIFT)
64 static cfs_page_t *echo_persistent_pages[ECHO_PERSISTENT_PAGES];
67 LPROC_ECHO_READ_BYTES = 1,
68 LPROC_ECHO_WRITE_BYTES = 2,
69 LPROC_ECHO_LAST = LPROC_ECHO_WRITE_BYTES +1
72 static int echo_connect(const struct lu_env *env,
73 struct obd_export **exp, struct obd_device *obd,
74 struct obd_uuid *cluuid, struct obd_connect_data *data,
77 struct lustre_handle conn = { 0 };
80 data->ocd_connect_flags &= ECHO_CONNECT_SUPPORTED;
81 rc = class_connect(&conn, obd, cluuid);
83 CERROR("can't connect %d\n", rc);
86 *exp = class_conn2export(&conn);
91 static int echo_disconnect(struct obd_export *exp)
93 LASSERT (exp != NULL);
95 return server_disconnect_export(exp);
98 static int echo_init_export(struct obd_export *exp)
100 return ldlm_init_export(exp);
103 static int echo_destroy_export(struct obd_export *exp)
107 target_destroy_export(exp);
108 ldlm_destroy_export(exp);
113 static __u64 echo_next_id(struct obd_device *obddev)
117 cfs_spin_lock(&obddev->u.echo.eo_lock);
118 id = ++obddev->u.echo.eo_lastino;
119 cfs_spin_unlock(&obddev->u.echo.eo_lock);
124 int echo_create(struct obd_export *exp, struct obdo *oa,
125 struct lov_stripe_md **ea, struct obd_trans_info *oti)
127 struct obd_device *obd = class_exp2obd(exp);
130 CERROR("invalid client cookie "LPX64"\n",
131 exp->exp_handle.h_cookie);
135 if (!(oa->o_mode && S_IFMT)) {
136 CERROR("echo obd: no type!\n");
140 if (!(oa->o_valid & OBD_MD_FLTYPE)) {
141 CERROR("invalid o_valid "LPX64"\n", oa->o_valid);
145 oa->o_id = echo_next_id(obd);
146 oa->o_valid = OBD_MD_FLID;
151 int echo_destroy(struct obd_export *exp, struct obdo *oa,
152 struct lov_stripe_md *ea, struct obd_trans_info *oti,
153 struct obd_export *md_exp, void *capa)
155 struct obd_device *obd = class_exp2obd(exp);
159 CERROR("invalid client cookie "LPX64"\n",
160 exp->exp_handle.h_cookie);
164 if (!(oa->o_valid & OBD_MD_FLID)) {
165 CERROR("obdo missing FLID valid flag: "LPX64"\n", oa->o_valid);
169 if (oa->o_id > obd->u.echo.eo_lastino || oa->o_id < ECHO_INIT_OID) {
170 CERROR("bad destroy objid: "LPX64"\n", oa->o_id);
177 static int echo_getattr(struct obd_export *exp, struct obd_info *oinfo)
179 struct obd_device *obd = class_exp2obd(exp);
180 obd_id id = oinfo->oi_oa->o_id;
184 CERROR("invalid client cookie "LPX64"\n",
185 exp->exp_handle.h_cookie);
189 if (!(oinfo->oi_oa->o_valid & OBD_MD_FLID)) {
190 CERROR("obdo missing FLID valid flag: "LPX64"\n",
191 oinfo->oi_oa->o_valid);
195 obdo_cpy_md(oinfo->oi_oa, &obd->u.echo.eo_oa, oinfo->oi_oa->o_valid);
196 oinfo->oi_oa->o_id = id;
201 static int echo_setattr(struct obd_export *exp, struct obd_info *oinfo,
202 struct obd_trans_info *oti)
204 struct obd_device *obd = class_exp2obd(exp);
208 CERROR("invalid client cookie "LPX64"\n",
209 exp->exp_handle.h_cookie);
213 if (!(oinfo->oi_oa->o_valid & OBD_MD_FLID)) {
214 CERROR("obdo missing FLID valid flag: "LPX64"\n",
215 oinfo->oi_oa->o_valid);
219 memcpy(&obd->u.echo.eo_oa, oinfo->oi_oa, sizeof(*oinfo->oi_oa));
221 if (oinfo->oi_oa->o_id & 4) {
222 /* Save lock to force ACKed reply */
223 ldlm_lock_addref (&obd->u.echo.eo_nl_lock, LCK_NL);
224 oti->oti_ack_locks[0].mode = LCK_NL;
225 oti->oti_ack_locks[0].lock = obd->u.echo.eo_nl_lock;
232 echo_page_debug_setup(cfs_page_t *page, int rw, obd_id id,
233 __u64 offset, int len)
235 int page_offset = offset & ~CFS_PAGE_MASK;
236 char *addr = ((char *)cfs_kmap(page)) + page_offset;
238 if (len % OBD_ECHO_BLOCK_SIZE != 0)
239 CERROR("Unexpected block size %d\n", len);
242 if (rw & OBD_BRW_READ)
243 block_debug_setup(addr, OBD_ECHO_BLOCK_SIZE,
246 block_debug_setup(addr, OBD_ECHO_BLOCK_SIZE,
247 0xecc0ecc0ecc0ecc0ULL,
248 0xecc0ecc0ecc0ecc0ULL);
250 addr += OBD_ECHO_BLOCK_SIZE;
251 offset += OBD_ECHO_BLOCK_SIZE;
252 len -= OBD_ECHO_BLOCK_SIZE;
259 echo_page_debug_check(cfs_page_t *page, obd_id id,
260 __u64 offset, int len)
262 int page_offset = offset & ~CFS_PAGE_MASK;
263 char *addr = ((char *)cfs_kmap(page)) + page_offset;
267 if (len % OBD_ECHO_BLOCK_SIZE != 0)
268 CERROR("Unexpected block size %d\n", len);
271 rc2 = block_debug_check("echo", addr, OBD_ECHO_BLOCK_SIZE,
274 if (rc2 != 0 && rc == 0)
277 addr += OBD_ECHO_BLOCK_SIZE;
278 offset += OBD_ECHO_BLOCK_SIZE;
279 len -= OBD_ECHO_BLOCK_SIZE;
287 /* This allows us to verify that desc_private is passed unmolested */
288 #define DESC_PRIV 0x10293847
290 static int echo_map_nb_to_lb(struct obdo *oa, struct obd_ioobj *obj,
291 struct niobuf_remote *nb, int *pages,
292 struct niobuf_local *lb, int cmd, int *left)
294 int gfp_mask = (obj->ioo_id & 1) ? CFS_ALLOC_HIGHUSER : CFS_ALLOC_STD;
295 int ispersistent = obj->ioo_id == ECHO_PERSISTENT_OBJID;
296 int debug_setup = (!ispersistent &&
297 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
298 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
299 struct niobuf_local *res = lb;
300 obd_off offset = nb->offset;
304 int plen = CFS_PAGE_SIZE - (offset & (CFS_PAGE_SIZE-1));
308 /* check for local buf overflow */
312 res->offset = offset;
314 LASSERT((res->offset & ~CFS_PAGE_MASK) + res->len <= CFS_PAGE_SIZE);
318 (res->offset >> CFS_PAGE_SHIFT) < ECHO_PERSISTENT_PAGES) {
319 res->page = echo_persistent_pages[res->offset >>
321 /* Take extra ref so __free_pages() can be called OK */
322 cfs_get_page (res->page);
324 OBD_PAGE_ALLOC(res->page, gfp_mask);
325 if (res->page == NULL) {
326 CERROR("can't get page for id " LPU64"\n",
332 CDEBUG(D_PAGE, "$$$$ get page %p @ "LPU64" for %d\n",
333 res->page, res->offset, res->len);
335 if (cmd & OBD_BRW_READ)
339 echo_page_debug_setup(res->page, cmd, obj->ioo_id,
340 res->offset, res->len);
353 static int echo_finalize_lb(struct obdo *oa, struct obd_ioobj *obj,
354 struct niobuf_remote *rb, int *pgs,
355 struct niobuf_local *lb, int verify)
357 struct niobuf_local *res = lb;
358 obd_off start = rb->offset >> CFS_PAGE_SHIFT;
359 obd_off end = (rb->offset + rb->len + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
360 int count = (int)(end - start);
364 for (i = 0; i < count; i++, (*pgs) ++, res++) {
365 cfs_page_t *page = res->page;
369 CERROR("null page objid "LPU64":%p, buf %d/%d\n",
370 obj->ioo_id, page, i, obj->ioo_bufcnt);
374 addr = cfs_kmap(page);
376 CDEBUG(D_PAGE, "$$$$ use page %p, addr %p@"LPU64"\n",
377 res->page, addr, res->offset);
380 int vrc = echo_page_debug_check(page, obj->ioo_id,
381 res->offset, res->len);
382 /* check all the pages always */
383 if (vrc != 0 && rc == 0)
388 /* NB see comment above regarding persistent pages */
395 int echo_preprw(int cmd, struct obd_export *export, struct obdo *oa,
396 int objcount, struct obd_ioobj *obj, struct niobuf_remote *nb,
397 int *pages, struct niobuf_local *res,
398 struct obd_trans_info *oti, struct lustre_capa *unused)
400 struct obd_device *obd;
406 obd = export->exp_obd;
410 /* Temp fix to stop falling foul of osc_announce_cached() */
411 oa->o_valid &= ~(OBD_MD_FLBLOCKS | OBD_MD_FLGRANT);
413 memset(res, 0, sizeof(*res) * *pages);
415 CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
416 cmd == OBD_BRW_READ ? "reading" : "writing", objcount, *pages);
419 oti->oti_handle = (void *)DESC_PRIV;
424 for (i = 0; i < objcount; i++, obj++) {
427 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++) {
429 rc = echo_map_nb_to_lb(oa, obj, nb, pages,
430 res + *pages, cmd, &left);
432 GOTO(preprw_cleanup, rc);
434 tot_bytes += nb->len;
438 cfs_atomic_add(*pages, &obd->u.echo.eo_prep);
440 if (cmd & OBD_BRW_READ)
441 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_READ_BYTES,
444 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
447 CDEBUG(D_PAGE, "%d pages allocated after prep\n",
448 cfs_atomic_read(&obd->u.echo.eo_prep));
453 /* It is possible that we would rather handle errors by allow
454 * any already-set-up pages to complete, rather than tearing them
455 * all down again. I believe that this is what the in-kernel
456 * prep/commit operations do.
458 CERROR("cleaning up %u pages (%d obdos)\n", *pages, objcount);
459 for (i = 0; i < *pages; i++) {
460 cfs_kunmap(res[i].page);
461 /* NB if this is a persistent page, __free_pages will just
462 * lose the extra ref gained above */
463 OBD_PAGE_FREE(res[i].page);
465 cfs_atomic_dec(&obd->u.echo.eo_prep);
471 int echo_commitrw(int cmd, struct obd_export *export, struct obdo *oa,
472 int objcount, struct obd_ioobj *obj,
473 struct niobuf_remote *rb, int niocount,
474 struct niobuf_local *res, struct obd_trans_info *oti, int rc)
476 struct obd_device *obd;
481 obd = export->exp_obd;
486 GOTO(commitrw_cleanup, rc);
488 if ((cmd & OBD_BRW_RWMASK) == OBD_BRW_READ) {
489 CDEBUG(D_PAGE, "reading %d obdos with %d IOs\n",
492 CDEBUG(D_PAGE, "writing %d obdos with %d IOs\n",
496 if (niocount && res == NULL) {
497 CERROR("NULL res niobuf with niocount %d\n", niocount);
501 LASSERT(oti == NULL || oti->oti_handle == (void *)DESC_PRIV);
503 for (i = 0; i < objcount; i++, obj++) {
504 int verify = (rc == 0 &&
505 obj->ioo_id != ECHO_PERSISTENT_OBJID &&
506 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
507 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
510 for (j = 0 ; j < obj->ioo_bufcnt ; j++, rb++) {
511 int vrc = echo_finalize_lb(oa, obj, rb, &pgs, &res[pgs], verify);
517 GOTO(commitrw_cleanup, rc = vrc);
525 cfs_atomic_sub(pgs, &obd->u.echo.eo_prep);
527 CDEBUG(D_PAGE, "%d pages remain after commit\n",
528 cfs_atomic_read(&obd->u.echo.eo_prep));
532 cfs_atomic_sub(pgs, &obd->u.echo.eo_prep);
534 CERROR("cleaning up %d pages (%d obdos)\n",
535 niocount - pgs - 1, objcount);
537 while (pgs < niocount) {
538 cfs_page_t *page = res[pgs++].page;
543 /* NB see comment above regarding persistent pages */
545 cfs_atomic_dec(&obd->u.echo.eo_prep);
550 static int echo_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
552 struct lprocfs_static_vars lvars;
555 struct ldlm_res_id res_id = {.name = {1}};
559 obd->u.echo.eo_obt.obt_magic = OBT_MAGIC;
560 cfs_spin_lock_init(&obd->u.echo.eo_lock);
561 obd->u.echo.eo_lastino = ECHO_INIT_OID;
563 sprintf(ns_name, "echotgt-%s", obd->obd_uuid.uuid);
564 obd->obd_namespace = ldlm_namespace_new(obd, ns_name,
565 LDLM_NAMESPACE_SERVER,
566 LDLM_NAMESPACE_MODEST,
568 if (obd->obd_namespace == NULL) {
573 rc = ldlm_cli_enqueue_local(obd->obd_namespace, &res_id, LDLM_PLAIN,
574 NULL, LCK_NL, &lock_flags, NULL,
575 ldlm_completion_ast, NULL, NULL, 0, NULL,
576 &obd->u.echo.eo_nl_lock);
577 LASSERT (rc == ELDLM_OK);
579 lprocfs_echo_init_vars(&lvars);
580 if (lprocfs_obd_setup(obd, lvars.obd_vars) == 0 &&
581 lprocfs_alloc_obd_stats(obd, LPROC_ECHO_LAST) == 0) {
582 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_READ_BYTES,
583 LPROCFS_CNTR_AVGMINMAX,
584 "read_bytes", "bytes");
585 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
586 LPROCFS_CNTR_AVGMINMAX,
587 "write_bytes", "bytes");
590 ptlrpc_init_client (LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
591 "echo_ldlm_cb_client", &obd->obd_ldlm_client);
595 static int echo_cleanup(struct obd_device *obd)
600 lprocfs_obd_cleanup(obd);
601 lprocfs_free_obd_stats(obd);
603 ldlm_lock_decref(&obd->u.echo.eo_nl_lock, LCK_NL);
605 /* XXX Bug 3413; wait for a bit to ensure the BL callback has
606 * happened before calling ldlm_namespace_free() */
607 cfs_schedule_timeout_and_set_state(CFS_TASK_UNINT, cfs_time_seconds(1));
609 ldlm_namespace_free(obd->obd_namespace, NULL, obd->obd_force);
610 obd->obd_namespace = NULL;
612 leaked = cfs_atomic_read(&obd->u.echo.eo_prep);
614 CERROR("%d prep/commitrw pages leaked\n", leaked);
619 static struct obd_ops echo_obd_ops = {
620 .o_owner = THIS_MODULE,
621 .o_connect = echo_connect,
622 .o_disconnect = echo_disconnect,
623 .o_init_export = echo_init_export,
624 .o_destroy_export = echo_destroy_export,
625 .o_create = echo_create,
626 .o_destroy = echo_destroy,
627 .o_getattr = echo_getattr,
628 .o_setattr = echo_setattr,
629 .o_preprw = echo_preprw,
630 .o_commitrw = echo_commitrw,
631 .o_setup = echo_setup,
632 .o_cleanup = echo_cleanup
635 extern int echo_client_init(void);
636 extern void echo_client_exit(void);
639 echo_persistent_pages_fini (void)
643 for (i = 0; i < ECHO_PERSISTENT_PAGES; i++)
644 if (echo_persistent_pages[i] != NULL) {
645 OBD_PAGE_FREE(echo_persistent_pages[i]);
646 echo_persistent_pages[i] = NULL;
651 echo_persistent_pages_init (void)
656 for (i = 0; i < ECHO_PERSISTENT_PAGES; i++) {
657 int gfp_mask = (i < ECHO_PERSISTENT_PAGES/2) ?
658 CFS_ALLOC_STD : CFS_ALLOC_HIGHUSER;
660 OBD_PAGE_ALLOC(pg, gfp_mask);
662 echo_persistent_pages_fini ();
666 memset (cfs_kmap (pg), 0, CFS_PAGE_SIZE);
669 echo_persistent_pages[i] = pg;
675 static int __init obdecho_init(void)
677 struct lprocfs_static_vars lvars;
681 LCONSOLE_INFO("Echo OBD driver; http://www.lustre.org/\n");
683 LASSERT(CFS_PAGE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
685 lprocfs_echo_init_vars(&lvars);
687 rc = echo_persistent_pages_init ();
691 rc = class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
692 LUSTRE_ECHO_NAME, NULL);
696 rc = echo_client_init();
700 class_unregister_type(LUSTRE_ECHO_NAME);
702 echo_persistent_pages_fini ();
707 static void /*__exit*/ obdecho_exit(void)
710 class_unregister_type(LUSTRE_ECHO_NAME);
711 echo_persistent_pages_fini ();
714 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
715 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
716 MODULE_LICENSE("GPL");
718 cfs_module(obdecho, "1.0.0", obdecho_init, obdecho_exit);