Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/sys/dev/dm dm: Add dm-flakey target
details: https://anonhg.NetBSD.org/src/rev/108571b753fb
branches: trunk
changeset: 968085:108571b753fb
user: tkusumi <tkusumi%NetBSD.org@localhost>
date: Thu Jan 02 06:22:23 2020 +0000
description:
dm: Add dm-flakey target
Ported from DragonFlyBSD, but this target had originally existed in
Linux kernel. See below for details.
https://www.kernel.org/doc/Documentation/device-mapper/dm-flakey.txt
Due to two technical issues, this hasn't been hooked to dm.kmod yet.
1) "tick" in hz(9) not working.
2) Unable to use ->b_private in nestiobuf callback when it's already
used for mbp (see HAS_BUF_PRIV2).
taken-from: DragonFlyBSD
diffstat:
sys/dev/dm/dm.h | 13 +-
sys/dev/dm/dm_target.c | 20 +-
sys/dev/dm/dm_target_flakey.c | 518 ++++++++++++++++++++++++++++++++++++++++++
3 files changed, 547 insertions(+), 4 deletions(-)
diffs (truncated from 589 to 300 lines):
diff -r 5e39ffc68ec5 -r 108571b753fb sys/dev/dm/dm.h
--- a/sys/dev/dm/dm.h Thu Jan 02 05:18:07 2020 +0000
+++ b/sys/dev/dm/dm.h Thu Jan 02 06:22:23 2020 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: dm.h,v 1.52 2019/12/23 16:17:35 tkusumi Exp $ */
+/* $NetBSD: dm.h,v 1.53 2020/01/02 06:22:23 tkusumi Exp $ */
/*
* Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -272,6 +272,17 @@
int dm_target_zero_destroy(dm_table_entry_t *);
int dm_target_zero_upcall(dm_table_entry_t *, struct buf *);
+#if 0
+/* dm_target_flakey.c */
+int dm_target_flakey_init(dm_table_entry_t *, int, char **);
+char *dm_target_flakey_table(void *);
+int dm_target_flakey_strategy(dm_table_entry_t *, struct buf *);
+int dm_target_flakey_sync(dm_table_entry_t *);
+int dm_target_flakey_destroy(dm_table_entry_t *);
+int dm_target_flakey_upcall(dm_table_entry_t *, struct buf *);
+int dm_target_flakey_secsize(dm_table_entry_t *, unsigned int *);
+#endif
+
/* dm_table.c */
#define DM_TABLE_ACTIVE 0
#define DM_TABLE_INACTIVE 1
diff -r 5e39ffc68ec5 -r 108571b753fb sys/dev/dm/dm_target.c
--- a/sys/dev/dm/dm_target.c Thu Jan 02 05:18:07 2020 +0000
+++ b/sys/dev/dm/dm_target.c Thu Jan 02 06:22:23 2020 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: dm_target.c,v 1.37 2019/12/23 16:17:35 tkusumi Exp $ */
+/* $NetBSD: dm_target.c,v 1.38 2020/01/02 06:22:23 tkusumi Exp $ */
/*
* Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -29,7 +29,7 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: dm_target.c,v 1.37 2019/12/23 16:17:35 tkusumi Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dm_target.c,v 1.38 2020/01/02 06:22:23 tkusumi Exp $");
#include <sys/types.h>
#include <sys/param.h>
@@ -358,6 +358,20 @@
dmt->upcall = &dm_target_zero_upcall;
if (dm_target_insert(dmt))
printf("Failed to insert zero\n");
-
+#if 0
+ dmt = dm_target_alloc("flakey");
+ dmt->version[0] = 1;
+ dmt->version[1] = 0;
+ dmt->version[2] = 0;
+ dmt->init = &dm_target_flakey_init;
+ dmt->table = &dm_target_flakey_table;
+ dmt->strategy = &dm_target_flakey_strategy;
+ dmt->sync = &dm_target_flakey_sync;
+ dmt->destroy = &dm_target_flakey_destroy;
+ dmt->upcall = &dm_target_flakey_upcall;
+ dmt->secsize = &dm_target_flakey_secsize;
+ if (dm_target_insert(dmt))
+ printf("Failed to insert flakey\n");
+#endif
return 0;
}
diff -r 5e39ffc68ec5 -r 108571b753fb sys/dev/dm/dm_target_flakey.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/dm/dm_target_flakey.c Thu Jan 02 06:22:23 2020 +0000
@@ -0,0 +1,518 @@
+/* $NetBSD: dm_target_flakey.c,v 1.1 2020/01/02 06:22:23 tkusumi Exp $ */
+
+/*
+ * Copyright (c) 2020 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Tomohiro Kusumi <tkusumi%netbsd.org@localhost>.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: dm_target_flakey.c,v 1.1 2020/01/02 06:22:23 tkusumi Exp $");
+
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/kernel.h>
+#include <sys/buf.h>
+#include <sys/kmem.h>
+
+#include "dm.h"
+
+//#define DEBUG_FLAKEY
+//#define HAS_BUF_PRIV2 /* XXX requires nonexistent buf::b_private2. */
+
+typedef struct target_flakey_config {
+ dm_pdev_t *pdev;
+ uint64_t offset;
+ int up_int;
+ int down_int;
+ int offset_time; /* XXX "tick" in hz(9) not working. */
+
+ /* drop_writes feature */
+ int drop_writes;
+
+ /* corrupt_bio_byte feature */
+ unsigned int corrupt_buf_byte;
+ unsigned int corrupt_buf_rw;
+ unsigned int corrupt_buf_value;
+ unsigned int corrupt_buf_flags; /* for B_XXX flags */
+} dm_target_flakey_config_t;
+
+#define BUF_CMD_READ 1
+#define BUF_CMD_WRITE 2
+
+#define FLAKEY_CORRUPT_DIR(tfc) \
+ ((tfc)->corrupt_buf_rw == BUF_CMD_READ ? 'r' : 'w')
+
+static int _init_features(dm_target_flakey_config_t*, int, char**);
+static __inline void _submit(dm_target_flakey_config_t*, struct buf*);
+static int _flakey_read(dm_target_flakey_config_t*, struct buf*);
+static int _flakey_write(dm_target_flakey_config_t*, struct buf*);
+static int _flakey_corrupt_buf(dm_target_flakey_config_t*, struct buf*);
+
+#ifdef DM_TARGET_MODULE
+/*
+ * Every target can be compiled directly to dm driver or as a
+ * separate module this part of target is used for loading targets
+ * to dm driver.
+ * Target can be unloaded from kernel only if there are no users of
+ * it e.g. there are no devices which uses that target.
+ */
+#include <sys/kernel.h>
+#include <sys/module.h>
+
+MODULE(MODULE_CLASS_MISC, dm_target_flakey, NULL);
+
+static int
+dm_target_flakey_modcmd(modcmd_t cmd, void *arg)
+{
+ dm_target_t *dmt;
+ int r;
+
+ switch (cmd) {
+ case MODULE_CMD_INIT:
+ if ((dmt = dm_target_lookup("flakey")) != NULL) {
+ dm_target_unbusy(dmt);
+ return EEXIST;
+ }
+ dmt = dm_target_alloc("flakey");
+
+ dmt->version[0] = 1;
+ dmt->version[1] = 0;
+ dmt->version[2] = 0;
+ dmt->init = &dm_target_flakey_init;
+ dmt->table = &dm_target_flakey_table;
+ dmt->strategy = &dm_target_flakey_strategy;
+ dmt->sync = &dm_target_flakey_sync;
+ dmt->destroy = &dm_target_flakey_destroy;
+ dmt->upcall = &dm_target_flakey_upcall;
+ dmt->secsize = &dm_target_flakey_secsize;
+
+ r = dm_target_insert(dmt);
+
+ break;
+
+ case MODULE_CMD_FINI:
+ r = dm_target_rem("flakey");
+ break;
+
+ case MODULE_CMD_STAT:
+ return ENOTTY;
+
+ default:
+ return ENOTTY;
+ }
+
+ return r;
+}
+#endif
+
+int
+dm_target_flakey_init(dm_table_entry_t *table_en, int argc, char **argv)
+{
+ dm_target_flakey_config_t *tfc;
+ dm_pdev_t *dmp;
+ int err;
+
+ if (argc < 4) {
+ printf("Flakey target takes at least 4 args, %d given\n", argc);
+ return EINVAL;
+ }
+
+ aprint_debug("Flakey target init function called: argc=%d\n", argc);
+
+ /* Insert dmp to global pdev list */
+ if ((dmp = dm_pdev_insert(argv[0])) == NULL)
+ return ENOENT;
+
+ tfc = kmem_alloc(sizeof(dm_target_flakey_config_t), KM_SLEEP);
+ tfc->pdev = dmp;
+ tfc->offset = atoi64(argv[1]);
+ tfc->up_int = atoi64(argv[2]);
+ tfc->down_int = atoi64(argv[3]);
+ tfc->offset_time = tick;
+
+ if ((tfc->up_int + tfc->down_int) == 0) {
+ printf("Sum of up/down interval is 0\n");
+ err = EINVAL;
+ goto fail;
+ }
+
+ if (tfc->up_int + tfc->down_int < tfc->up_int) {
+ printf("Interval time overflow\n");
+ err = EINVAL;
+ goto fail;
+ }
+
+ err = _init_features(tfc, argc - 4, argv + 4);
+ if (err)
+ goto fail;
+
+ dm_table_add_deps(table_en, dmp);
+ table_en->target_config = tfc;
+
+ return 0;
+fail:
+ kmem_free(tfc, sizeof(*tfc));
+ return err;
+}
+
+static int
+_init_features(dm_target_flakey_config_t *tfc, int argc, char **argv)
+{
+ char *arg;
+ unsigned int value;
+
+ if (argc == 0)
+ return 0;
+
+ argc = atoi64(*argv++); /* # of args for features */
+ if (argc > 6) {
+ printf("Invalid # of feature args %d\n", argc);
+ return EINVAL;
+ }
+
+ while (argc) {
+ argc--;
+ arg = *argv++;
+
+ /* drop_writes */
+ if (strcmp(arg, "drop_writes") == 0) {
+ tfc->drop_writes = 1;
+ continue;
+ }
+
+ /* corrupt_bio_byte <Nth_byte> <direction> <value> <flags> */
+ if (strcmp(arg, "corrupt_bio_byte") == 0) {
+ if (argc < 4) {
+ printf("Invalid # of feature args %d for "
+ "corrupt_bio_byte\n", argc);
+ return EINVAL;
+ }
+
+ /* <Nth_byte> */
+ argc--;
+ value = atoi64(*argv++);
+ if (value < 1) {
+ printf("Invalid corrupt_bio_byte "
+ "<Nth_byte> arg %u\n", value);
+ return EINVAL;
+ }
+ tfc->corrupt_buf_byte = value;
+
+ /* <direction> */
+ argc--;
+ arg = *argv++;
+ if (strcmp(arg, "r") == 0) {
+ tfc->corrupt_buf_rw = BUF_CMD_READ;
+ } else if (strcmp(arg, "w") == 0) {
Home |
Main Index |
Thread Index |
Old Index