2021-02-10 05:12:13

by Sergei Shtepa

[permalink] [raw]
Subject: [PATCH v5 0/6] block-layer interposer

Hi all.

I'm joyful to suggest the block-layer interposer (blk_interposer) v5.
blk_interposer allows to intercept bio requests, remap bio to another
devices or add new bios.

This patch series adds support blk_interposer for dm-linear.

In the first patch, I suggest the remap_and_filter.rst file.
Yes, Mike, it's probably too early for documentation, but maybe it will be
interesting for someone. In the documentation I tried to explain
the purpose of blk_interposer and what prospects it opens up.

The second patch adds the function blk_mq_is_queue_frozen(). It allows to
assert a queue state.

The third patch is dedicated to blk_interposer itself, which provides
the ability to intercept bio.

The fourth one adds support for blk_interposer from the device mapper.
Added ioctl DM_DEV_REMAP_CMD.

In the fifth - added the 'noexcl' option for dm-linear, which allows
to open the underlying block-device without the FMODE_EXCL mode.
This allows to create a dm device to which can redirect bio requests
using DM_DEV_REMAP_CMD.

The latest patch changes linear.rst with the description of the 'noexcl'
option that is added for dm-linear.

A little history of changes:

v5 - current patch set
Changes:
* rebase for v5.11-rc7;
* patch set organization;
* fix defects in documentation;
* add some comments;
* change mutex names for better code readability;
* remove calling bd_unlink_disk_holder() for targets with non-exclusive
flag;
* change type for struct dm_remap_param from uint8_t to __u8.

v4 - https://patchwork.kernel.org/project/linux-block/cover/[email protected]/
Mostly changes were made, due to Damien's comments:
* on the design of the code;
* by the patch set organization;
* bug with passing a wrong parameter to dm_get_device();
* description of the 'noexcl' parameter in the linear.rst.
Also added remap_and_filter.rst.

v3 - https://patchwork.kernel.org/project/linux-block/cover/[email protected]/
In this version, I already suggested blk_interposer to apply to dm-linear.
Problems were solved:
* Interception of bio requests from a specific device on the disk, not
from the entire disk. To do this, we added the dm_interposed_dev
structure and an interval tree to store these structures.
* Implemented ioctl DM_DEV_REMAP_CMD. A patch with changes in the lvm2
project was sent to the team [email protected].
* Added the 'noexcl' option for dm-linear, which allows you to open
the underlying block-device without FMODE_EXCL mode.

v2 - https://patchwork.kernel.org/project/linux-block/cover/[email protected]/
I tried to suggest blk_interposer without using it in device mapper,
but with the addition of a sample of its use. It was then that I learned
about the maintainers' attitudes towards the samples directory :).

v1 - https://lwn.net/ml/linux-block/[email protected]/
This Hannes's patch can be considered as a starting point, since this is
where the interception mechanism and the term blk_interposer itself
appeared. It became clear that blk_interposer can be useful for
device mapper.

before v1 - https://patchwork.kernel.org/project/linux-block/cover/[email protected]/
I tried to offer a rather cumbersome blk-filter and a monster-like
blk-snap module for creating snapshots.

Thank you to everyone who was able to take the time to review
the previous versions.
I hope that this time I achieved the required quality.

Thanks,
Sergei.

Sergei Shtepa (6):
docs: device-mapper: add remap_and_filter
block: add blk_mq_is_queue_frozen()
block: add blk_interposer
dm: new ioctl DM_DEV_REMAP_CMD
dm: add 'noexcl' option for dm-linear
docs: device-mapper: 'noexcl' option for dm-linear

.../admin-guide/device-mapper/index.rst | 1 +
.../admin-guide/device-mapper/linear.rst | 26 +-
.../device-mapper/remap_and_filter.rst | 132 ++++++
block/bio.c | 2 +
block/blk-core.c | 35 ++
block/blk-mq.c | 13 +
block/genhd.c | 86 ++++
drivers/md/dm-core.h | 20 +
drivers/md/dm-ioctl.c | 35 ++
drivers/md/dm-linear.c | 14 +-
drivers/md/dm-table.c | 14 +-
drivers/md/dm.c | 401 +++++++++++++++++-
drivers/md/dm.h | 2 +-
include/linux/blk-mq.h | 1 +
include/linux/blk_types.h | 6 +-
include/linux/device-mapper.h | 7 +
include/linux/genhd.h | 18 +
include/uapi/linux/dm-ioctl.h | 15 +-
18 files changed, 796 insertions(+), 32 deletions(-)
create mode 100644 Documentation/admin-guide/device-mapper/remap_and_filter.rst

--
2.20.1


2021-02-10 05:12:33

by Sergei Shtepa

[permalink] [raw]
Subject: [PATCH v5 3/6] block: add blk_interposer

blk_interposer allows to intercept bio requests, remap bio
to another devices or add new bios.

Signed-off-by: Sergei Shtepa <[email protected]>
---
block/bio.c | 2 +
block/blk-core.c | 35 ++++++++++++++++
block/genhd.c | 86 +++++++++++++++++++++++++++++++++++++++
include/linux/blk_types.h | 6 ++-
include/linux/genhd.h | 18 ++++++++
5 files changed, 145 insertions(+), 2 deletions(-)

diff --git a/block/bio.c b/block/bio.c
index 1f2cc1fbe283..f6f135eb84b5 100644
--- a/block/bio.c
+++ b/block/bio.c
@@ -684,6 +684,8 @@ void __bio_clone_fast(struct bio *bio, struct bio *bio_src)
bio_set_flag(bio, BIO_CLONED);
if (bio_flagged(bio_src, BIO_THROTTLED))
bio_set_flag(bio, BIO_THROTTLED);
+ if (bio_flagged(bio_src, BIO_INTERPOSED))
+ bio_set_flag(bio, BIO_INTERPOSED);
bio->bi_opf = bio_src->bi_opf;
bio->bi_ioprio = bio_src->bi_ioprio;
bio->bi_write_hint = bio_src->bi_write_hint;
diff --git a/block/blk-core.c b/block/blk-core.c
index 7663a9b94b80..e830eb5ae7f0 100644
--- a/block/blk-core.c
+++ b/block/blk-core.c
@@ -1032,6 +1032,34 @@ static blk_qc_t __submit_bio_noacct_mq(struct bio *bio)
return ret;
}

+static blk_qc_t __submit_bio_interposed(struct bio *bio)
+{
+ struct bio_list bio_list[2] = { };
+ blk_qc_t ret = BLK_QC_T_NONE;
+
+ current->bio_list = bio_list;
+ if (likely(bio_queue_enter(bio) == 0)) {
+ struct gendisk *disk = bio->bi_disk;
+
+ if (likely(blk_has_interposer(disk))) {
+ bio_set_flag(bio, BIO_INTERPOSED);
+ disk->interposer->ip_submit_bio(bio);
+ } else {
+ /* interposer was removed */
+ bio_list_add(&current->bio_list[0], bio);
+ }
+
+ blk_queue_exit(disk->queue);
+ }
+ current->bio_list = NULL;
+
+ /* Resubmit remaining bios */
+ while ((bio = bio_list_pop(&bio_list[0])))
+ ret = submit_bio_noacct(bio);
+
+ return ret;
+}
+
/**
* submit_bio_noacct - re-submit a bio to the block device layer for I/O
* @bio: The bio describing the location in memory and on the device.
@@ -1057,6 +1085,13 @@ blk_qc_t submit_bio_noacct(struct bio *bio)
return BLK_QC_T_NONE;
}

+ /*
+ * Checking the BIO_INTERPOSED flag is necessary so that the bio
+ * created by the blk_interposer do not get to it for processing.
+ */
+ if (blk_has_interposer(bio->bi_disk) &&
+ !bio_flagged(bio, BIO_INTERPOSED))
+ return __submit_bio_interposed(bio);
if (!bio->bi_disk->fops->submit_bio)
return __submit_bio_noacct_mq(bio);
return __submit_bio_noacct(bio);
diff --git a/block/genhd.c b/block/genhd.c
index 9e741a4f351b..728f1e68bb2d 100644
--- a/block/genhd.c
+++ b/block/genhd.c
@@ -30,6 +30,11 @@
static struct kobject *block_depr;

DECLARE_RWSEM(bdev_lookup_sem);
+/*
+ * Prevents different block-layer interposers from attaching or detaching
+ * to the disk at the same time.
+ */
+DEFINE_MUTEX(blk_interposer_attach_lock);

/* for extended dynamic devt allocation, currently only one major is used */
#define NR_EXT_DEVT (1 << MINORBITS)
@@ -2149,3 +2154,84 @@ static void disk_release_events(struct gendisk *disk)
WARN_ON_ONCE(disk->ev && disk->ev->block != 1);
kfree(disk->ev);
}
+
+/**
+ * blk_interposer_attach - Attach interposer to disk
+ * @disk: target disk
+ * @interposer: block device interposer
+ * @ip_submit_bio: hook for submit_bio()
+ *
+ * Returns:
+ * -EINVAL if @interposer is NULL.
+ * -EPERM if queue is not frozen.
+ * -EBUSY if the block device already has @interposer.
+ * -EALREADY if the block device already has @interposer with same callback.
+ *
+ * Disk must be frozen by blk_mq_freeze_queue().
+ */
+int blk_interposer_attach(struct gendisk *disk, struct blk_interposer *interposer,
+ const ip_submit_bio_t ip_submit_bio)
+{
+ int ret = 0;
+
+ if (WARN_ON(!interposer))
+ return -EINVAL;
+
+ if (!blk_mq_is_queue_frozen(disk->queue))
+ return -EPERM;
+
+ mutex_lock(&blk_interposer_attach_lock);
+ if (blk_has_interposer(disk)) {
+ if (disk->interposer->ip_submit_bio == ip_submit_bio)
+ ret = -EALREADY;
+ else
+ ret = -EBUSY;
+ goto out;
+ }
+
+ interposer->ip_submit_bio = ip_submit_bio;
+ interposer->disk = disk;
+
+ disk->interposer = interposer;
+out:
+ mutex_unlock(&blk_interposer_attach_lock);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(blk_interposer_attach);
+
+/**
+ * blk_interposer_detach - Detach interposer from disk
+ * @interposer: block device interposer
+ * @ip_submit_bio: hook for submit_bio()
+ *
+ * Disk must be frozen by blk_mq_freeze_queue().
+ */
+void blk_interposer_detach(struct blk_interposer *interposer,
+ const ip_submit_bio_t ip_submit_bio)
+{
+ struct gendisk *disk;
+
+ if (WARN_ON(!interposer))
+ return;
+
+ mutex_lock(&blk_interposer_attach_lock);
+
+ /* Check if the interposer is still active. */
+ disk = interposer->disk;
+ if (WARN_ON(!disk))
+ goto out;
+
+ if (WARN_ON(!blk_mq_is_queue_frozen(disk->queue)))
+ goto out;
+
+ /* Check if it is really our interposer. */
+ if (WARN_ON(disk->interposer->ip_submit_bio != ip_submit_bio))
+ goto out;
+
+ disk->interposer = NULL;
+ interposer->disk = NULL;
+out:
+ mutex_unlock(&blk_interposer_attach_lock);
+}
+EXPORT_SYMBOL_GPL(blk_interposer_detach);
diff --git a/include/linux/blk_types.h b/include/linux/blk_types.h
index 866f74261b3b..6c1351d7b73f 100644
--- a/include/linux/blk_types.h
+++ b/include/linux/blk_types.h
@@ -227,7 +227,7 @@ struct bio {
* top bits REQ_OP. Use
* accessors.
*/
- unsigned short bi_flags; /* status, etc and bvec pool number */
+ unsigned int bi_flags; /* status, etc and bvec pool number */
unsigned short bi_ioprio;
unsigned short bi_write_hint;
blk_status_t bi_status;
@@ -304,6 +304,8 @@ enum {
* of this bio. */
BIO_CGROUP_ACCT, /* has been accounted to a cgroup */
BIO_TRACKED, /* set if bio goes through the rq_qos path */
+ BIO_INTERPOSED, /* bio has been interposed and can be moved to
+ * a different disk */
BIO_FLAG_LAST
};

@@ -322,7 +324,7 @@ enum {
* freed.
*/
#define BVEC_POOL_BITS (3)
-#define BVEC_POOL_OFFSET (16 - BVEC_POOL_BITS)
+#define BVEC_POOL_OFFSET (32 - BVEC_POOL_BITS)
#define BVEC_POOL_IDX(bio) ((bio)->bi_flags >> BVEC_POOL_OFFSET)
#if (1<< BVEC_POOL_BITS) < (BVEC_POOL_NR+1)
# error "BVEC_POOL_BITS is too small"
diff --git a/include/linux/genhd.h b/include/linux/genhd.h
index 809aaa32d53c..f68c8e83b4f1 100644
--- a/include/linux/genhd.h
+++ b/include/linux/genhd.h
@@ -134,6 +134,13 @@ struct blk_integrity {
unsigned char tag_size;
};

+typedef void (*ip_submit_bio_t) (struct bio *bio);
+
+struct blk_interposer {
+ ip_submit_bio_t ip_submit_bio;
+ struct gendisk *disk;
+};
+
struct gendisk {
/* major, first_minor and minors are input parameters only,
* don't use directly. Use disk_devt() and disk_max_parts().
@@ -158,6 +165,7 @@ struct gendisk {

const struct block_device_operations *fops;
struct request_queue *queue;
+ struct blk_interposer *interposer;
void *private_data;

int flags;
@@ -346,4 +354,14 @@ static inline void printk_all_partitions(void)
}
#endif /* CONFIG_BLOCK */

+/*
+ * block layer interposer
+ */
+#define blk_has_interposer(d) ((d)->interposer != NULL)
+
+int blk_interposer_attach(struct gendisk *disk, struct blk_interposer *interposer,
+ const ip_submit_bio_t ip_submit_bio);
+void blk_interposer_detach(struct blk_interposer *interposer,
+ const ip_submit_bio_t ip_submit_bio);
+
#endif /* _LINUX_GENHD_H */
--
2.20.1