@@ -1938,3 +1938,55 @@ static void disk_release_events(struct gendisk *disk)
WARN_ON_ONCE(disk->ev && disk->ev->block != 1);
kfree(disk->ev);
}
+
+/**
+ * bdev_interposer_attach - Attach an interposer block device to original
+ * @original: original block device
+ * @interposer: interposer block device
+ *
+ * Before attaching an interposer, it is necessary to lock the processing
+ * of bio requests of the original device by calling bdev_interposer_lock().
+ *
+ * The bdev_interposer_detach() function allows to detach the interposer
+ * from the original block device.
+ */
+int bdev_interposer_attach(struct block_device *original,
+ struct block_device *interposer)
+{
+ struct block_device *bdev;
+
+ WARN_ON(!original);
+ if (original->bd_interposer)
+ return -EBUSY;
+
+ bdev = bdgrab(interposer);
+ if (!bdev)
+ return -ENODEV;
+
+ original->bd_interposer = bdev;
+ return 0;
+}
+EXPORT_SYMBOL_GPL(bdev_interposer_attach);
+
+/**
+ * bdev_interposer_detach - Detach interposer from block device
+ * @original: original block device
+ *
+ * Before detaching an interposer, it is necessary to lock the processing
+ * of bio requests of the original device by calling bdev_interposer_lock().
+ *
+ * The interposer should be attached using the bdev_interposer_attach()
+ * function.
+ */
+void bdev_interposer_detach(struct block_device *original)
+{
+ if (WARN_ON(!original))
+ return;
+
+ if (!original->bd_interposer)
+ return;
+
+ bdput(original->bd_interposer);
+ original->bd_interposer = NULL;
+}
+EXPORT_SYMBOL_GPL(bdev_interposer_detach);
@@ -809,6 +809,7 @@ static void bdev_free_inode(struct inode *inode)
{
struct block_device *bdev = I_BDEV(inode);
+ percpu_free_rwsem(&bdev->bd_interposer_lock);
free_percpu(bdev->bd_stats);
kfree(bdev->bd_meta_info);
@@ -909,6 +910,8 @@ struct block_device *bdev_alloc(struct gendisk *disk, u8 partno)
iput(inode);
return NULL;
}
+ bdev->bd_interposer = NULL;
+ percpu_init_rwsem(&bdev->bd_interposer_lock);
return bdev;
}
@@ -46,6 +46,11 @@ struct block_device {
spinlock_t bd_size_lock; /* for bd_inode->i_size updates */
struct gendisk * bd_disk;
struct backing_dev_info *bd_bdi;
+ /* The interposer allows to redirect bio to another device */
+ struct block_device *bd_interposer;
+ /* Lock the queue of block device to attach or detach interposer.
+ * Allows to safely suspend and flush interposer. */
+ struct percpu_rw_semaphore bd_interposer_lock;
/* The counter of freeze processes */
int bd_fsfreeze_count;
@@ -304,6 +309,7 @@ enum {
BIO_CGROUP_ACCT, /* has been accounted to a cgroup */
BIO_TRACKED, /* set if bio goes through the rq_qos path */
BIO_REMAPPED,
+ BIO_INTERPOSED, /* bio was reassigned to another block device */
BIO_FLAG_LAST
};
@@ -2029,4 +2029,36 @@ int fsync_bdev(struct block_device *bdev);
int freeze_bdev(struct block_device *bdev);
int thaw_bdev(struct block_device *bdev);
+/**
+ * bdev_interposer_lock - Lock bio processing
+ * @bdev: locking block device
+ *
+ * Lock the bio processing in submit_bio_noacct() for the new requests in the
+ * original block device. Requests from the interposer will not be locked.
+ *
+ * To unlock, use the bdev_interposer_unlock() function.
+ *
+ * This lock should be used to attach/detach the interposer to the device.
+ */
+static inline void bdev_interposer_lock(struct block_device *bdev)
+{
+ percpu_down_write(&bdev->bd_interposer_lock);
+}
+
+/**
+ * bdev_interposer_unlock - Unlock bio processing
+ * @bdev: locked block device
+ *
+ * Unlock the bio processing that was locked by bdev_interposer_lock() function.
+ *
+ * This lock should be used to attach/detach the interposer to the device.
+ */
+static inline void bdev_interposer_unlock(struct block_device *bdev)
+{
+ percpu_up_write(&bdev->bd_interposer_lock);
+}
+
+int bdev_interposer_attach(struct block_device *original,
+ struct block_device *interposer);
+void bdev_interposer_detach(struct block_device *original);
#endif /* _LINUX_BLKDEV_H */
Additional fields were added in the block_device structure: bd_interposer and bd_interposer_lock. The bd_interposer field contains a pointer to an interposer block device. bd_interposer_lock is a lock which allows to safely attach and detach the interposer device. New functions bdev_interposer_attach() and bdev_interposer_detach() allow to attach and detach an interposer device. But first it is required to lock the processing of bio requests by the block device with bdev_interposer_lock() function. The BIO_INTERPOSED flag means that the bio request has been already interposed. This flag avoids recursive bio request interception. Signed-off-by: Sergei Shtepa <sergei.shtepa@veeam.com> --- block/genhd.c | 52 +++++++++++++++++++++++++++++++++++++++ fs/block_dev.c | 3 +++ include/linux/blk_types.h | 6 +++++ include/linux/blkdev.h | 32 ++++++++++++++++++++++++ 4 files changed, 93 insertions(+)