[Devel] [PATCH rh9] dm-zero-req: Introduce zero request based target

Konstantin Khorenko khorenko at virtuozzo.com
Fri Apr 1 13:03:29 MSK 2022


This driver is like "dm-zero", but request based rather than bio based
like original "dm-zero".

This driver will be used on a block device for Container configuration
stage: we need to construct a block device which honors CBT mask (stored
in ploop image), for that first we need to create a dummy block device
(pure technical issue, otherwise CBT mask is dropped).

dm-ploop/dm-qcow2 are request based, thus we need zero target
also to be request based.

https://jira.sw.ru/browse/PSBM-127989

Signed-off-by: Konstantin Khorenko <khorenko at virtuozzo.com>
Feature: cbt: changed block tracking (for backup)
---
 drivers/md/Kconfig                            |  8 ++
 drivers/md/Makefile                           |  3 +-
 drivers/md/dm-zero-req.c                      | 95 +++++++++++++++++++
 .../generic/CONFIG_DM_ZERO_REQ                |  1 +
 4 files changed, 106 insertions(+), 1 deletion(-)
 create mode 100644 drivers/md/dm-zero-req.c
 create mode 100644 redhat/configs/custom-overrides/generic/CONFIG_DM_ZERO_REQ

diff --git a/drivers/md/Kconfig b/drivers/md/Kconfig
index 259e426a93a4..fd075f1243d7 100644
--- a/drivers/md/Kconfig
+++ b/drivers/md/Kconfig
@@ -425,6 +425,14 @@ config DM_ZERO
 	  A target that discards writes, and returns all zeroes for
 	  reads.  Useful in some recovery situations.
 
+config DM_ZERO_REQ
+	tristate "Zero request based target"
+	depends on BLK_DEV_DM
+	help
+	  A target that discards writes, and returns all zeroes for
+	  reads. Useful in some recovery situations. Also used for block device
+	  configutation used by CBT based backups.
+
 config DM_MULTIPATH
 	tristate "Multipath target"
 	depends on BLK_DEV_DM
diff --git a/drivers/md/Makefile b/drivers/md/Makefile
index 94134440cf70..8271d1517264 100644
--- a/drivers/md/Makefile
+++ b/drivers/md/Makefile
@@ -75,7 +75,8 @@ obj-$(CONFIG_DM_SNAPSHOT)	+= dm-snapshot.o
 obj-$(CONFIG_DM_PERSISTENT_DATA) += persistent-data/
 obj-$(CONFIG_DM_MIRROR)		+= dm-mirror.o dm-log.o dm-region-hash.o
 obj-$(CONFIG_DM_LOG_USERSPACE)	+= dm-log-userspace.o
-obj-$(CONFIG_DM_ZERO)		+= dm-zero.o
+obj-$(CONFIG_DM_ZERO)		+= dm-zero.o dm-zero-req.o
+obj-$(CONFIG_DM_ZERO_REQ)	+= dm-zero-req.o
 obj-$(CONFIG_DM_RAID)		+= dm-raid.o
 obj-$(CONFIG_DM_THIN_PROVISIONING) += dm-thin-pool.o
 obj-$(CONFIG_DM_VERITY)		+= dm-verity.o
diff --git a/drivers/md/dm-zero-req.c b/drivers/md/dm-zero-req.c
new file mode 100644
index 000000000000..9582564a5db6
--- /dev/null
+++ b/drivers/md/dm-zero-req.c
@@ -0,0 +1,95 @@
+/*
+ * Copyright (C) 2003 Jana Saout <jana at saout.de>
+ *
+ * This file is released under the GPL.
+ */
+
+#include <linux/device-mapper.h>
+
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/bio.h>
+#include <linux/blk-mq.h>
+#include "dm-rq.h"
+
+#define DM_MSG_PREFIX "zero"
+
+/*
+ * Construct a dummy mapping that only returns zeros
+ */
+static int zero_ctr(struct dm_target *ti, unsigned int argc, char **argv)
+{
+	if (argc != 0) {
+		ti->error = "No arguments required";
+		return -EINVAL;
+	}
+
+	/*
+	 * Silently drop discards, avoiding -EOPNOTSUPP.
+	 */
+	ti->num_discard_bios = 1;
+
+	return 0;
+}
+
+static int zero_clone_and_map_rq(struct dm_target *ti, struct request *rq,
+				 union map_info *map_context,
+				 struct request **clone)
+{
+	struct bio *bio = rq->bio;
+
+	switch (bio_op(bio)) {
+	case REQ_OP_READ:
+		if (bio->bi_opf & REQ_RAHEAD) {
+			/* readahead of null bytes only wastes buffer cache */
+			return DM_MAPIO_KILL;
+		}
+		while (bio) {
+			zero_fill_bio(bio);
+			bio = bio->bi_next;
+		}
+
+		break;
+	case REQ_OP_WRITE:
+		/* writes get silently dropped */
+		break;
+	default:
+		return DM_MAPIO_KILL;
+	}
+
+	dm_complete_request(rq, BLK_STS_OK);
+
+	/* accepted rq, don't make new request */
+	return DM_MAPIO_SUBMITTED;
+}
+
+static struct target_type zero_target = {
+	.name   = "zero-rq",
+	.version = {1, 1, 0},
+	.features = DM_TARGET_NOWAIT,
+	.module = THIS_MODULE,
+	.ctr    = zero_ctr,
+	.clone_and_map_rq = zero_clone_and_map_rq,
+};
+
+static int __init dm_zero_init(void)
+{
+	int r = dm_register_target(&zero_target);
+
+	if (r < 0)
+		DMERR("register failed %d", r);
+
+	return r;
+}
+
+static void __exit dm_zero_exit(void)
+{
+	dm_unregister_target(&zero_target);
+}
+
+module_init(dm_zero_init)
+module_exit(dm_zero_exit)
+
+MODULE_AUTHOR("Jana Saout <jana at saout.de>");
+MODULE_DESCRIPTION(DM_NAME " dummy request based target returning zeros");
+MODULE_LICENSE("GPL");
diff --git a/redhat/configs/custom-overrides/generic/CONFIG_DM_ZERO_REQ b/redhat/configs/custom-overrides/generic/CONFIG_DM_ZERO_REQ
new file mode 100644
index 000000000000..1c5c184f9478
--- /dev/null
+++ b/redhat/configs/custom-overrides/generic/CONFIG_DM_ZERO_REQ
@@ -0,0 +1 @@
+CONFIG_DM_ZERO_REQ=m
-- 
2.31.1



More information about the Devel mailing list