blksnap - block devices snapshots module
Subsystems (4)
v6 cover blksnap - block devices snapshots module
From: Sergei Shtepa <[email protected]>
Hi all.
I am happy to offer an improved version of the Block Devices Snapshots Module. It allows creating non-persistent snapshots of any block devices. The main purpose of such snapshots is to provide backups of block devices. See more in Documentation/block/blksnap.rst.
The Block Device Filtering Mechanism is added to the block layer. This allows attaching and detaching block device filters to the block layer. Filters allow extending the functionality of the block layer. See more in Documentation/block/blkfilter.rst.
The tool, library and tests for working with blksnap can be found on github.
From the documentation, it is possible to learn how to manage the module using the library and console tool.
In the new version, the method of saving snapshot difference has been changed. Why this should have been done, Dave Chinner <[email protected]> described in detail in the comments to the previous version.
The module is incompatible with features hardware inline encryption and data integrity. Thanks to Eric Biggers <[email protected]>.
Unfortunately, I didn't have a chance to check it, since I don't have such equipment.
And it is impossible to determine the presence of a blk-crypto-fallback at the block layer filter level. The filter receives already encrypted data. The original device continues to work without problems, but the data in the snapshot is encrypted. Perhaps they can be decrypted if specify the correct key when mounting the file system for snapshot image.
Tested on amd64 and ppc64le with a page size of 64KiB and a storage block size of 4KiB.
Based on LK v6.7-rc2.
Compatible with branch 'for-next'.
v6 changes:
- The difference storage has been changed.
In the previous version, the file was created only to reserve sector ranges on a block device. The data was stored directly to the block device in these sector ranges. Now saving and reading data is done using 'VFS' using vfs_iter_write() and vfs_iter_read() functions. This allows not to depend on the filesystem and use, for example, tmpfs. Using an unnamed temporary file allows hiding it from other processes and automatically release it when the snapshot is closed. However, now the module does not allow adding a block device to the snapshot on which the difference storage is located. There is no way to ensure the immutability of file metadata when writing data to a file. This means that the metadata of the filesystem may change, which may cause damage to the snapshot.
- _IOW and _IOR were mixed up - fixed.
- Protection against the use of the snapshots for block devices with hardware inline encryption and data integrity was implemented. Compatibility with them was not planned and has not been tested at the moment.
v5 changes:
- Rebase for "kernel/git/axboe/linux-block.git" branch "for-6.5/block".
v4 changes:
- Structures for describing the state of chunks are allocated dynamically. This reduces memory consumption, since the struct chunk is allocated only for those blocks for which the snapshot image state differs from the original block device.
- The algorithm for calculating the chunk size depending on the size of the block device has been changed. For large block devices, it is now possible to allocate a larger number of chunks, and their size is smaller.
- For block devices, a 'filter' file has been added to /sys/block/<device>. It displays the name of the filter that is attached to the block device.
- Fixed a problem with the lack of protection against re-adding a block device to a snapshot.
- Fixed a bug in the algorithm of allocating the next bio for a chunk. This problem was occurred on large disks, for which a chunk consists of at least two bio.
- The ownership mechanism of the diff_area structure has been changed. This fixed the error of prematurely releasing the diff_area structure when destroying the snapshot.
- Documentation corrected.
- The Sparse analyzer is passed.
- Use __u64 type instead pointers in UAPI.
v3 changes:
- New block device I/O controls BLKFILTER_ATTACH and BLKFILTER_DETACH allow to attach and detach filters.
- New block device I/O control BLKFILTER_CTL allow sending command to attached block device filter.
- The copy-on-write algorithm for processing I/O units has been optimized and has become asynchronous.
- The snapshot image reading algorithm has been optimized and has become asynchronous.
- Optimized the finite state machine for processing chunks.
- Fixed a tracking block size calculation bug.
v2 changes:
- Added documentation for Block Device Filtering Mechanism.
- Added documentation for Block Devices Snapshots Module (blksnap).
- The MAINTAINERS file has been updated.
- Optimized queue code for snapshot images.
- Fixed comments, log messages and code for better readability.
v1 changes:
- Forgotten "static" declarations have been added.
- The text of the comments has been corrected.
- It is possible to connect only one filter, since there are no others in upstream.
- Do not have additional locks for attach/detach filter.
- blksnap.h moved to include/uapi/.
- #pragma once and commented code removed.
- uuid_t removed from user API.
- Removed default values for module parameters from the configuration file.
- The debugging code for tracking memory leaks has been removed.
- Simplified Makefile.
- Optimized work with large memory buffers, CBT tables are now in virtual memory.
- The allocation code of minor numbers has been optimized.
- The implementation of the snapshot image block device has been simplified, now it is a bio-based block device.
- Removed initialization of global variables with null values.
- only one bio is used to copy one chunk.
- Checked on ppc64le.
Sergei Shtepa (11):
documentation: Block Device Filtering Mechanism
block: Block Device Filtering Mechanism
documentation: Block Devices Snapshots Module
blksnap: header file of the module interface
blksnap: module management interface functions
blksnap: handling and tracking I/O units
blksnap: difference storage and chunk
blksnap: event queue from the difference storage
blksnap: snapshot and snapshot image block device
blksnap: Kconfig and Makefile
blksnap: prevents using devices with data integrity or inline
encryption
Documentation/block/blkfilter.rst | 66 ++ Documentation/block/blksnap.rst | 352 +++++++++ Documentation/block/index.rst | 2 + .../userspace-api/ioctl/ioctl-number.rst | 1 + MAINTAINERS | 17 + block/Makefile | 3 +- block/bdev.c | 2 + block/blk-core.c | 35 +- block/blk-filter.c | 238 +++++++ block/blk.h | 11 + block/genhd.c | 10 + block/ioctl.c | 7 + block/partitions/core.c | 9 + drivers/block/Kconfig | 2 + drivers/block/Makefile | 2 + drivers/block/blksnap/Kconfig | 31 + drivers/block/blksnap/Makefile | 15 + drivers/block/blksnap/cbt_map.c | 228 ++++++ drivers/block/blksnap/cbt_map.h | 90 +++ drivers/block/blksnap/chunk.c | 667 ++++++++++++++++++ drivers/block/blksnap/chunk.h | 142 ++++ drivers/block/blksnap/diff_area.c | 601 ++++++++++++++++ drivers/block/blksnap/diff_area.h | 175 +++++ drivers/block/blksnap/diff_buffer.c | 115 +++ drivers/block/blksnap/diff_buffer.h | 37 + drivers/block/blksnap/diff_storage.c | 291 ++++++++ drivers/block/blksnap/diff_storage.h | 104 +++ drivers/block/blksnap/event_queue.c | 81 +++ drivers/block/blksnap/event_queue.h | 64 ++ drivers/block/blksnap/main.c | 475 +++++++++++++ drivers/block/blksnap/params.h | 16 + drivers/block/blksnap/snapimage.c | 134 ++++ drivers/block/blksnap/snapimage.h | 10 + drivers/block/blksnap/snapshot.c | 457 ++++++++++++ drivers/block/blksnap/snapshot.h | 64 ++ drivers/block/blksnap/tracker.c | 358 ++++++++++ drivers/block/blksnap/tracker.h | 78 ++ include/linux/blk-filter.h | 51 ++ include/linux/blk_types.h | 1 + include/linux/blkdev.h | 1 + include/linux/sched.h | 1 + include/uapi/linux/blk-filter.h | 35 + include/uapi/linux/blksnap.h | 388 ++++++++++ include/uapi/linux/fs.h | 3 + 44 files changed, 5468 insertions(+), 2 deletions(-) create mode 100644 Documentation/block/blkfilter.rst create mode 100644 Documentation/block/blksnap.rst create mode 100644 block/blk-filter.c create mode 100644 drivers/block/blksnap/Kconfig create mode 100644 drivers/block/blksnap/Makefile create mode 100644 drivers/block/blksnap/cbt_map.c create mode 100644 drivers/block/blksnap/cbt_map.h create mode 100644 drivers/block/blksnap/chunk.c create mode 100644 drivers/block/blksnap/chunk.h create mode 100644 drivers/block/blksnap/diff_area.c create mode 100644 drivers/block/blksnap/diff_area.h create mode 100644 drivers/block/blksnap/diff_buffer.c create mode 100644 drivers/block/blksnap/diff_buffer.h create mode 100644 drivers/block/blksnap/diff_storage.c create mode 100644 drivers/block/blksnap/diff_storage.h create mode 100644 drivers/block/blksnap/event_queue.c create mode 100644 drivers/block/blksnap/event_queue.h create mode 100644 drivers/block/blksnap/main.c create mode 100644 drivers/block/blksnap/params.h create mode 100644 drivers/block/blksnap/snapimage.c create mode 100644 drivers/block/blksnap/snapimage.h create mode 100644 drivers/block/blksnap/snapshot.c create mode 100644 drivers/block/blksnap/snapshot.h create mode 100644 drivers/block/blksnap/tracker.c create mode 100644 drivers/block/blksnap/tracker.h create mode 100644 include/linux/blk-filter.h create mode 100644 include/uapi/linux/blk-filter.h create mode 100644 include/uapi/linux/blksnap.h
Signature · 1 line
2.20.1
PATCH v6 1/11 documentation: Block Device Filtering Mechanism
From: Sergei Shtepa <[email protected]>
The document contains:
* Describes the purpose of the mechanism
* A little historical background on the capabilities of handling I/O units of the Linux kernel
* Brief description of the design
* Reference to interface description
--- Documentation/block/blkfilter.rst | 66 +++++++++++++++++++++++++++++++ Documentation/block/index.rst | 1 + MAINTAINERS | 6 +++ 3 files changed, 73 insertions(+) create mode 100644 Documentation/block/blkfilter.rst
Documentation/block/blkfilter.rst new file +66 -0
@@ -0,0 +1,66 @@ +.. SPDX-License-Identifier: GPL-2.0 + +================================ +Block Device Filtering Mechanism +================================ + +The block device filtering mechanism provides the ability to attach block +device filters. Block device filters allow performing additional processing +for I/O units. + +Introduction +============ + +The idea of handling I/O units on block devices is not new. Back in the +2.6 kernel, there was an undocumented possibility of handling I/O units +by substituting the make_request_fn() function, which belonged to the +request_queue structure. But none of the in-tree kernel modules used this +feature, and it was eliminated in the 5.10 kernel. + +The block device filtering mechanism returns the ability to handle I/O units. +It is possible to safely attach a filter to a block device "on the fly" without +changing the structure of the block device's stack. + +It supports attaching one filter to one block device, because there is only +one filter implementation in the kernel yet. +See Documentation/block/blksnap.rst. + +Design +====== + +The block device filtering mechanism provides registration and unregistration +for filter operations. The struct blkfilter_operations contains a pointer to +the callback functions for the filter. After registering the filter operations, +the filter can be managed using block device ioctls BLKFILTER_ATTACH, +BLKFILTER_DETACH and BLKFILTER_CTL. + +When the filter is attached, the callback function is called for each I/O unit +for a block device, providing I/O unit filtering. Depending on the result of +filtering the I/O unit, it can either be passed for subsequent processing by +the block layer, or skipped. + +The filter can be implemented as a loadable module. In this case, the filter +module cannot be unloaded while the filter is attached to at least one of the +block devices. + +Interface description +===================== + +The ioctl BLKFILTER_ATTACH allows user-space programs to attach a block device +filter to a block device. The ioctl BLKFILTER_DETACH allows user-space programs +to detach it. Both ioctls use &struct blkfilter_name. The ioctl BLKFILTER_CTL +allows user-space programs to send a filter-specific command. It use &struct +blkfilter_ctl. + +.. kernel-doc:: include/uapi/linux/blk-filter.h + +To register in the system, the filter uses the &struct blkfilter_operations, +which contains callback functions, unique filter name and module owner. When +attaching a filter to a block device, the filter creates a &struct blkfilter. +The pointer to the &struct blkfilter allows the filter to determine for which +block device the callback functions are being called. + +.. kernel-doc:: include/linux/blk-filter.h + +.. kernel-doc:: block/blk-filter.c + :export:
Documentation/block/index.rst +1 -0
@@ -10,6 +10,7 @@ Block bfq-iosched biovecs blk-mq + blkfilter cmdline-partition data-integrity deadline-iosched
MAINTAINERS +6 -0
@@ -3584,6 +3584,12 @@ M: Jan-Simon Moeller <[email protected]> S: Maintained F: drivers/leds/leds-blinkm.c +BLOCK DEVICE FILTERING MECHANISM +M: Sergei Shtepa <[email protected]> +L: [email protected] +S: Supported +F: Documentation/block/blkfilter.rst + BLOCK LAYER M: Jens Axboe <[email protected]> L: [email protected]
Signature · 1 line
2.20.1
PATCH v6 2/11 block: Block Device Filtering Mechanism
In reply to Sergei ShtepaFrom: Sergei Shtepa <[email protected]>
The block device filtering mechanism is an API that allows to attach block device filters. Block device filters allow perform additional processing for I/O units.
The idea of handling I/O units on block devices is not new. Back in the 2.6 kernel, there was an undocumented possibility of handling I/O units by substituting the make_request_fn() function, which belonged to the request_queue structure. But none of the in-tree kernel modules used this feature, and it was eliminated in the 5.10 kernel.
The block device filtering mechanism returns the ability to handle I/O units. It is possible to safely attach filter to a block device "on the fly" without changing the structure of block devices stack.
--- MAINTAINERS | 3 + block/Makefile | 3 +- block/bdev.c | 2 + block/blk-core.c | 35 ++++- block/blk-filter.c | 238 ++++++++++++++++++++++++++++++++ block/blk.h | 11 ++ block/genhd.c | 10 ++ block/ioctl.c | 7 + block/partitions/core.c | 9 ++ include/linux/blk-filter.h | 51 +++++++ include/linux/blk_types.h | 1 + include/linux/blkdev.h | 1 + include/linux/sched.h | 1 + include/uapi/linux/blk-filter.h | 35 +++++ include/uapi/linux/fs.h | 3 + 15 files changed, 408 insertions(+), 2 deletions(-) create mode 100644 block/blk-filter.c create mode 100644 include/linux/blk-filter.h create mode 100644 include/uapi/linux/blk-filter.h
MAINTAINERS +3 -0
@@ -3589,6 +3589,9 @@ M: Sergei Shtepa <[email protected]> L: [email protected] S: Supported F: Documentation/block/blkfilter.rst +F: block/blk-filter.c +F: include/linux/blk-filter.h +F: include/uapi/linux/blk-filter.h BLOCK LAYER M: Jens Axboe <[email protected]>
block/Makefile +2 -1
@@ -9,7 +9,8 @@ obj-y := bdev.o fops.o bio.o elevator.o blk-core.o blk-sysfs.o \ blk-lib.o blk-mq.o blk-mq-tag.o blk-stat.o \ blk-mq-sysfs.o blk-mq-cpumap.o blk-mq-sched.o ioctl.o \ genhd.o ioprio.o badblocks.o partitions/ blk-rq-qos.o \ - disk-events.o blk-ia-ranges.o early-lookup.o + disk-events.o blk-ia-ranges.o early-lookup.o \ + blk-filter.o obj-$(CONFIG_BOUNCE) += bounce.o obj-$(CONFIG_BLK_DEV_BSG_COMMON) += bsg.o
block/bdev.c +2 -0
@@ -412,6 +412,7 @@ struct block_device *bdev_alloc(struct gendisk *disk, u8 partno)return NULL ; } bdev->bd_disk = disk; + bdev->bd_filter =NULL ;return bdev; }
@@ -1018,6 +1019,7 @@ void bdev_mark_dead(struct block_device *bdev, bool surprise) }invalidate_bdev (bdev); +blkfilter_detach (bdev); }/* * New drivers should not use this directly. There are some drivers however
block/blk-core.c +34 -1
@@ -18,6 +18,7 @@#include <linux/blkdev.h> #include <linux/blk-pm.h> #include <linux/blk-integrity.h> +#include <linux/blk-filter.h> #include <linux/highmem.h> #include <linux/mm.h> #include <linux/pagemap.h>
@@ -592,12 +593,34 @@ static inline blk_status_t blk_check_zone_append(struct request_queue *q,static void __submit_bio (struct bio *bio) { +struct request_queue *q =bdev_get_queue (bio->bi_bdev); +bool skip_bio = false; + +if (unlikely (bio_queue_enter (bio))) +return ; + +if (bio->bi_bdev->bd_filter && + bio->bi_bdev->bd_filter != current->blk_filter) { +struct blkfilter *prev = current->blk_filter; + + current->blk_filter = bio->bi_bdev->bd_filter; + skip_bio = bio->bi_bdev->bd_filter->ops->submit_bio (bio); + current->blk_filter = prev; + } + +blk_queue_exit (q); +if (skip_bio) +return ; +if (unlikely (!blk_crypto_bio_prep (&bio)))return ;if (!bio->bi_bdev->bd_has_submit_bio) {blk_mq_submit_bio (bio); - }else if (likely (bio_queue_enter (bio) ==0 )) { +return ; + } + +if (likely (bio_queue_enter (bio) ==0 )) {struct gendisk *disk = bio->bi_bdev->bd_disk; disk->fops->submit_bio (bio);
@@ -681,6 +704,15 @@ static void __submit_bio_noacct_mq(struct bio *bio) current->bio_list =NULL ; } +/** +* submit_bio_noacct_nocheck - re-submit a bio to the block device layer for I/O +* from block device filter. +* @bio: The bio describing the location in memory and on the device. +* +* This is a version of submit_bio() that shall only be used for I/O that is +* resubmitted to lower level by block device filters. All file systems and +* other upper level users of the block layer should use submit_bio() instead. +*/ void submit_bio_noacct_nocheck (struct bio *bio) {blk_cgroup_bio_start (bio);
@@ -708,6 +740,7 @@ void submit_bio_noacct_nocheck(struct bio *bio)else __submit_bio_noacct (bio); } +EXPORT_SYMBOL_GPL (submit_bio_noacct_nocheck);/** * submit_bio_noacct - re-submit a bio to the block device layer for I/O
block/blk-filter.c new file +238 -0
238 lines not on this page · raw message
block/blk.h +11 -0
@@ -7,6 +7,8 @@#include <xen/xen.h> #include "blk-crypto-internal.h" +struct blkfilter_ctl ; +struct blkfilter_name ;struct elevator_type ;/* Max future timer expiry for timeouts */
@@ -474,6 +476,15 @@ long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg);extern const struct address_space_operations def_blk_aops; +int blkfilter_ioctl_attach (struct block_device *bdev, +struct blkfilter_name __user *argp); +int blkfilter_ioctl_detach (struct block_device *bdev, +struct blkfilter_name __user *argp); +int blkfilter_ioctl_ctl (struct block_device *bdev, +struct blkfilter_ctl __user *argp); +void blkfilter_detach (struct block_device *bdev); +ssize_t blkfilter_show (struct block_device *bdev,char *buf); +int disk_register_independent_access_ranges (struct gendisk *disk);void disk_unregister_independent_access_ranges (struct gendisk *disk);
block/genhd.c +10 -0
@@ -26,6 +26,7 @@#include <linux/badblocks.h> #include <linux/part_stat.h> #include <linux/blktrace_api.h> +#include <linux/blk-filter.h> #include "blk-throttle.h" #include "blk.h"
@@ -654,6 +655,7 @@ void del_gendisk(struct gendisk *disk)mutex_lock (&disk->open_mutex);xa_for_each (&disk->part_tbl, idx, part)remove_inode_hash (part->bd_inode); +blkfilter_detach (disk->part0);mutex_unlock (&disk->open_mutex);/*
@@ -1044,6 +1046,12 @@ static ssize_t diskseq_show(struct device *dev,return sprintf (buf,"%llu\n" , disk->diskseq); } +static ssize_t disk_filter_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +return blkfilter_show (dev_to_bdev (dev), buf); +} + static DEVICE_ATTR(range, 0444, disk_range_show, NULL); static DEVICE_ATTR(ext_range, 0444, disk_ext_range_show, NULL); static DEVICE_ATTR(removable, 0444, disk_removable_show, NULL);
@@ -1057,6 +1065,7 @@ static DEVICE_ATTR(stat, 0444, part_stat_show, NULL); static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL); static DEVICE_ATTR(badblocks, 0644, disk_badblocks_show, disk_badblocks_store); static DEVICE_ATTR(diskseq, 0444, diskseq_show, NULL); +static DEVICE_ATTR(filter, 0444, disk_filter_show, NULL);#ifdef CONFIG_FAIL_MAKE_REQUEST ssize_t part_fail_show(struct device *dev,
@@ -1103,6 +1112,7 @@ static struct attribute *disk_attrs[] = { &dev_attr_events_async.attr, &dev_attr_events_poll_msecs.attr, &dev_attr_diskseq.attr, + &dev_attr_filter.attr, #ifdef CONFIG_FAIL_MAKE_REQUEST &dev_attr_fail.attr, #endif
block/ioctl.c +7 -0
@@ -2,6 +2,7 @@#include <linux/capability.h> #include <linux/compat.h> #include <linux/blkdev.h> +#include <linux/blk-filter.h> #include <linux/export.h> #include <linux/gfp.h> #include <linux/blkpg.h>
@@ -572,6 +573,12 @@ static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode,return blkdev_pr_preempt (bdev, mode, argp, true);case IOC_PR_CLEAR :return blkdev_pr_clear (bdev, mode, argp); +case BLKFILTER_ATTACH : +return blkfilter_ioctl_attach (bdev, argp); +case BLKFILTER_DETACH : +return blkfilter_ioctl_detach (bdev, argp); +case BLKFILTER_CTL : +return blkfilter_ioctl_ctl (bdev, argp);default :return -ENOIOCTLCMD ; }
block/partitions/core.c +9 -0
@@ -10,6 +10,7 @@#include <linux/ctype.h> #include <linux/vmalloc.h> #include <linux/raid/detect.h> +#include <linux/blk-filter.h> #include "check.h" static int (*const check_part[])(struct parsed_partitions *) = {
@@ -200,6 +201,12 @@ static ssize_t part_discard_alignment_show(struct device *dev,return sprintf (buf,"%u\n" ,bdev_discard_alignment (dev_to_bdev (dev))); } +static ssize_t part_filter_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +return blkfilter_show (dev_to_bdev (dev), buf); +} + static DEVICE_ATTR(partition, 0444, part_partition_show, NULL); static DEVICE_ATTR(start, 0444, part_start_show, NULL); static DEVICE_ATTR(size, 0444, part_size_show, NULL);
@@ -208,6 +215,7 @@ static DEVICE_ATTR(alignment_offset, 0444, part_alignment_offset_show, NULL); static DEVICE_ATTR(discard_alignment, 0444, part_discard_alignment_show, NULL); static DEVICE_ATTR(stat, 0444, part_stat_show, NULL); static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL); +static DEVICE_ATTR(filter, 0444, part_filter_show, NULL);#ifdef CONFIG_FAIL_MAKE_REQUEST static struct device_attribute dev_attr_fail =__ATTR (make-it-fail,0644 , part_fail_show, part_fail_store);
@@ -222,6 +230,7 @@ static struct attribute *part_attrs[] = { &dev_attr_discard_alignment.attr, &dev_attr_stat.attr, &dev_attr_inflight.attr, + &dev_attr_filter.attr, #ifdef CONFIG_FAIL_MAKE_REQUEST &dev_attr_fail.attr, #endif
include/linux/blk-filter.h new file +51 -0
51 lines not on this page · raw message
include/linux/blk_types.h +1 -0
@@ -74,6 +74,7 @@ struct block_device {* path */ struct device bd_device; +struct blkfilter *bd_filter; } __randomize_layout;#define bdev_whole (_bdev) \
include/linux/blkdev.h +1 -0
@@ -834,6 +834,7 @@ void blk_request_module(dev_t devt);extern int blk_register_queue (struct gendisk *disk);extern void blk_unregister_queue (struct gendisk *disk); +void submit_bio_noacct_nocheck (struct bio *bio);void submit_bio_noacct (struct bio *bio);struct bio *bio_split_to_limits (struct bio *bio);
include/linux/sched.h +1 -0
@@ -1190,6 +1190,7 @@ struct task_struct {/* Stack plugging: */ struct blk_plug *plug; +struct blkfilter *blk_filter;/* VM state: */ struct reclaim_state *reclaim_state;
include/uapi/linux/blk-filter.h new file +35 -0
@@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +/* Copyright (C) 2023 Veeam Software Group GmbH */ +#ifndef _UAPI_LINUX_BLK_FILTER_H +#define _UAPI_LINUX_BLK_FILTER_H + +#include <linux/types.h> + +#define BLKFILTER_NAME_LENGTH 32 + +/** +* struct blkfilter_name - parameter for BLKFILTER_ATTACH and BLKFILTER_DETACH +* ioctl. +* +* @name: Name of block device filter. +*/ +struct blkfilter_name { +__u8 name[BLKFILTER_NAME_LENGTH ]; +}; + +/** +* struct blkfilter_ctl - parameter for BLKFILTER_CTL ioctl +* +* @name: Name of block device filter. +* @cmd: The filter-specific operation code of the command. +* @optlen: Size of data at @opt. +* @opt: Userspace buffer with options. +*/ +struct blkfilter_ctl { +__u8 name[BLKFILTER_NAME_LENGTH ]; +__u32 cmd; +__u32 optlen; +__u64 opt; +}; + +#endif /* _UAPI_LINUX_BLK_FILTER_H */
include/uapi/linux/fs.h +3 -0
@@ -189,6 +189,9 @@ struct fsxattr {* A jump here: 130-136 are reserved for zoned block devices * (see uapi/linux/blkzoned.h) */ +#define BLKFILTER_ATTACH _IOWR(0x12, 140, struct blkfilter_name) +#define BLKFILTER_DETACH _IOWR(0x12, 141, struct blkfilter_name) +#define BLKFILTER_CTL _IOWR(0x12, 142, struct blkfilter_ctl)#define BMAP_IOCTL 1/* obsolete - kept for compatibility */ #define FIBMAP _IO(0x00,1)/* bmap access */
Signature · 1 line
2.20.1
PATCH v6 3/11 documentation: Block Devices Snapshots Module
In reply to Sergei ShtepaFrom: Sergei Shtepa <[email protected]>
The document contains:
* Describes the purpose of the mechanism
* Description of features
* Description of algorithms
* Recommendations about using the module from the user-space side
* Reference to module interface description
--- Documentation/block/blksnap.rst | 352 ++++++++++++++++++++++++++++++++ Documentation/block/index.rst | 1 + MAINTAINERS | 6 + 3 files changed, 359 insertions(+) create mode 100644 Documentation/block/blksnap.rst
Documentation/block/blksnap.rst new file +352 -0
@@ -0,0 +1,352 @@ +.. SPDX-License-Identifier: GPL-2.0 + +======================================== +Block Devices Snapshots Module (blksnap) +======================================== + +Introduction +============ + +At first glance, there is no novelty in the idea of creating snapshots for +block devices. The Linux kernel already has mechanisms for creating snapshots. +Device Mapper includes dm-snap, which allows to create snapshots of block +devices. BTRFS supports snapshots at the filesystem level. However, both of +these options have flaws that do not allow to use them as a universal tool for +creating backups. + +The main properties that a backup tool should have are: + +- Simplicity and universality of use +- Reliability +- Minimal consumption of system resources during backup +- Minimal time required for recovery or replication of the entire system + +Taking above properties into account, blksnap module features: + +- Change tracker +- Snapshots at the block device level +- Dynamic allocation of space for storing differences +- Snapshot overflow resistance +- Coherent snapshot of multiple block devices + +Features +======== + +Change tracker +-------------- + +The change tracker allows to determine which blocks were changed during the +time between the last snapshot created and any of the previous snapshots. +With a map of changes, it is enough to copy only the changed blocks, and no +need to reread the entire block device completely. The change tracker allows +to implement the logic of both incremental and differential backups. +Incremental backup is critical for large file repositories whose size can be +hundreds of terabytes and whose full backup time can take more than a day. +On such servers, the use of backup tools without a change tracker becomes +practically impossible. + +Snapshot at the block device level +---------------------------------- + +A snapshot at the block device level allows to simplify the backup algorithm +and reduce consumption of system resources. It also allows to perform linear +reading of disk space directly, which allows to achieve maximum reading speed +with minimal use of processor time. At the same time, the universality of +creating snapshots for any block device is achieved, regardless of the file +system located on it. The exceptions are BTRFS, ZFS and cluster file systems. + +Dynamic allocation of storage space for differences +--------------------------------------------------- + +To store differences, the module does not require a pre-reserved space on +filesystem. The space for storing differences can be allocated in file in any +filesystem. In addition, the size of the difference storage can be increased +after the snapshot is created, but only for a filesystem that supports +fallocate. A shared difference storage for all images of snapshot block devices +allows to optimize the use of storage space. However, there is one limitation. +A snapshot cannot be taken from a block device on which the difference storage +is located. + +Snapshot overflow resistance +---------------------------- + +To create images of snapshots of block devices, the module stores blocks +of the original block device that have been changed since the snapshot +was taken. To do this, the module handles write requests and reads blocks +that need to be overwritten. This algorithm guarantees safety of the data +of the original block device in the event of an overflow of the snapshot, +and even in the case of unpredictable critical errors. If a problem occurs +during backup, the difference storage is released, the snapshot is closed, +no backup is created, but the server continues to work. + +Coherent snapshot of multiple block devices +------------------------------------------- + +A snapshot is created simultaneously for all block devices for which a backup +is being created, ensuring their coherent state. + + +Algorithms +========== + +Overview +-------- + +The blksnap module is a block-level filter. It handles all write I/O units. +The filter is attached to the block device when the snapshot is created +for the first time. The change tracker marks all overwritten blocks. +Information about the history of changes on the block device is available +while holding the snapshot. The module reads the blocks that need to be +overwritten and stores them in the difference storage. When reading from +a snapshot image, reading is performed either from the original device or +from the difference storage. + +Change tracking +--------------- + +A change tracker map is created for each block device. One byte of this map +corresponds to one block. The block size is set by the +``tracking_block_minimum_shift`` and ``tracking_block_maximum_count`` +module parameters. The ``tracking_block_minimum_shift`` parameter limits +the minimum block size for tracking, while ``tracking_block_maximum_count`` +defines the maximum allowed number of blocks. The size of the change tracker +block is determined depending on the size of the block device when adding +a tracking device, that is, when the snapshot is taken for the first time. +The block size must be a power of two. The ``tracking_block_maximum_shift`` +module parameter allows to limit the maximum block size for tracking. If the +block size reaches the allowable limit, the number of blocks will exceed the +``tracking_block_maximum_count`` parameter. + +The byte of the change map stores a number from 0 to 255. This is the +snapshot number, since the creation of which there have been changes in +the block. Each time a snapshot is created, the number of the current +snapshot is increased by one. This number is written to the cell of the +change map when writing to the block. Thus, knowing the number of one of +the previous snapshots and the number of the last snapshot, one can determine +from the change map which blocks have been changed. When the number of the +current change reaches the maximum allowed value for the map of 255, at the +time when the next snapshot is created, the map of changes is reset to zero, +and the number of the current snapshot is assigned the value 1. The change +tracker is reset, and a new UUID is generated - a unique identifier of the +snapshot generation. The snapshot generation identifier allows to identify +that a change tracking reset has been performed. + +The change map has two copies. One copy is active, it tracks the current +changes on the block device. The second copy is available for reading +while the snapshot is being held, and contains the history up to the moment +the snapshot is taken. Copies are synchronized at the moment of snapshot +creation. After the snapshot is released, a second copy of the map is not +needed, but it is not released, so as not to allocate memory for it again +the next time the snapshot is created. + +Copy on write +------------- + +Data is copied in blocks, or rather in chunks. The term "chunk" is used to +avoid confusion with change tracker blocks and I/O blocks. In addition, +the "chunk" in the blksnap module means about the same as the "chunk" in +the dm-snap module. + +The size of the chunk is determined by the ``chunk_minimum_shift`` and +``chunk_maximum_count`` module parameters. The ``chunk_minimum_shift`` +parameter limits the minimum size of the chunk, while ``chunk_maximum_count`` +defines the maximum allowed number of chunks. The size of the chunk is +determined depending on the size of the block device at the time of taking the +snapshot. The size of the chunk must be a power of two. The module parameter +``chunk_maximum_shift`` allows to limit the maximum chunk size. If the chunk +size reaches the allowable limit, the number of chunks will exceed the +``chunk_maximum_count`` parameter. + +One chunk is described by the ``struct chunk`` structure. A map of structures +is created for each block device. The structure contains all the necessary +information to copy the chunks data from the original block device to the +difference storage. This information allows to describe the snapshot image. +A semaphore is located in the structure, which allows synchronization of threads +accessing the chunk. + +The block level in Linux has a feature. If a read I/O unit was sent, and a +write I/O unit was sent after it, then a write can be performed first, and only +then a read. Therefore, the copy-on-write algorithm is executed synchronously. +If the write request is handled, the execution of this I/O unit will be delayed +until the overwritten chunks are read from the original device for later +storing to the difference store. But if, when handling a write I/O unit, it +turns out that the written range of sectors has already been prepared for +storing to the difference storage, then the I/O unit is simply passed. + +This algorithm makes it possible to efficiently perform backup even systems +with a Round-Robin databases. Such databases can be overwritten several times +during the system backup. Of course, the value of a backup of the RRD monitoring +system data can be questioned. However, it is often a task to make a backup +of the entire enterprise infrastructure in order to restore or replicate it +entirely in case of problems. + +There is also a flaw in the algorithm. When overwriting at least one sector, +an entire chunk is copied. Thus, a situation of rapid filling of the difference +storage when writing data to a block device in small portions in random order +is possible. This situation is possible in case of strong fragmentation of +data on the filesystem. But it must be borne in mind that with such data +fragmentation, performance of systems usually degrades greatly. So, this +problem does not occur on real servers, although it can easily be created +by artificial tests. + +Difference storage +------------------ + +The difference storage can be a block device or it can be a file on a +filesystem. Using a block device allows to achieve slightly higher performance, +but in this case, the block device is used by the kernel module exclusively. +Usually the disk space is marked up so that there is no available free space +for backup purposes. Using a file allows to place the difference storage on a +filesystem. + +The difference storage can be expanded already while the snapshot is being held, +but only if the filesystem supports fallocate(). If the free space in the +difference storage remains less than half of the value of the module parameter +``diff_storage_minimum``, then the kernel module can expand the difference +storage file within the specified limits. This limit is set when creating a +snapshot. + +If free space in the difference storage runs out, an event to user land is +generated about the overflow of the snapshot. Such a snapshot is considered +corrupted, and read I/O units to snapshot images will be terminated with an +error code. The difference storage stores outdated data required for snapshot +images, so when the snapshot is overflowed, the backup process is interrupted, +but the system maintains its operability without data loss. + +The difference storage has a limitation. The device cannot be added to the +snapshot where the difference storage is located. In this case, the difference +storage can be located in virtual memory, which consists of RAM and a swap +partition (or file). To do this, it is enough to use a file in /dev/shm, or a +new tmpfs filesystem can be created for this purpose. Obviously, this variant +can be useful if the system has a lot of RAM or a large swap. The good news is +that the modern Linux kernel allows to increase the size of the swap file "on +the fly" without changing the system configuration. + +A regular file or a block device file for the difference storage must be opened +with the O_EXCL flag. If an unnamed file with the O_TMPFILE flag is created, +then such a file will be automatically released when the snapshot is destroyed. +In addition, the use of an unnamed temporary file ensures that no one can open +this file and read its contents. + +Performing I/O for a snapshot image +----------------------------------- + +To read snapshot data, when taking a snapshot, block devices of snapshot images +are created. The snapshot image block devices support the write operation. +This allows to perform additional data preparation on the filesystem before +creating a backup. + +To process the I/O unit, clones of the I/O unit are created, which redirect +the I/O unit either to the original block device or to the difference storage. +When processing of cloned I/O units is completed, the original I/O unit is +marked as completed too. + +An I/O unit can be partially processed without accessing to block devices if +the I/O unit refers to a chunk that is in the queue for storing to the +difference storage. In this case, the data is read or written in a buffer in +memory. + +If, when processing the write I/O unit, it turns out that the data of the +referred chunk has not yet been stored to the difference storage or has not +even been read from the original device, then an I/O unit to read data from the +original device is initiated beforehand. After the reading from original device +is performed, their data from the I/O unit is partially overwritten directly in +the buffer of the chunk in memory, and the chunk is scheduled to be saved to the +difference storage. + +How to use +========== + +Depending on the needs and the selected license, you can choose different +options for managing the module: + +- Using ioctl directly +- Using a static C++ library +- Using the blksnap console tool + +Using a BLKFILTER_CTL for block device +-------------------------------------- + +BLKFILTER_CTL allows to send a filter-specific command to the filter on block +device and get the result of its execution. The module provides the +``include/uapi/blksnap.h`` header file with a description of the commands and +their data structures. + +1. ``blkfilter_ctl_blksnap_cbtinfo`` allows to get information from the + change tracker. +2. ``blkfilter_ctl_blksnap_cbtmap`` reads the change tracker table. If a write + operation was performed for the snapshot, then the change tracker takes this + into account. Therefore, it is necessary to receive tracker data after write + operations have been completed. +3. ``blkfilter_ctl_blksnap_cbtdirty`` mark blocks as changed in the change + tracker table. This is necessary if post-processing is performed after the + backup is created, which changes the backup blocks. +4. ``blkfilter_ctl_blksnap_snapshotadd`` adds a block device to the snapshot. +5. ``blkfilter_ctl_blksnap_snapshotinfo`` allows to get the name of the snapshot + image block device and the presence of an error. + +Using ioctl +----------- + +Using a BLKFILTER_CTL ioctl does not allow to fully implement the management of +the blksnap module. A control file ``blksnap-control`` is created to manage +snapshots. The control commands are also described in the file +``include/uapi/blksnap.h``. + +1. ``blksnap_ioctl_version`` get the version number. +2. ``blk_snap_ioctl_snapshot_create`` initiates the snapshot creation process. +3. ``blk_snap_ioctl_snapshot_append_storage`` add the range of blocks to + difference storage. +4. ``blk_snap_ioctl_snapshot_take`` creates block devices of block device + snapshot images. +5. ``blk_snap_ioctl_snapshot_collect`` collect all created snapshots. +6. ``blk_snap_ioctl_snapshot_wait_event`` allows to track the status of + snapshots and receive events about the requirement to expand the difference + storage or about snapshot overflow. +7. ``blk_snap_ioctl_snapshot_destroy`` releases the snapshot. + +Static C++ library +------------------ + +The [#userspace_libs]_ library was created primarily to simplify creation of +tests in C++, and it is also a good example of using the module interface. +When creating applications, direct use of control calls is preferable. +However, the library can be used in an application with a GPL-2+ license, +or a library with an LGPL-2+ license can be created, with which even a +proprietary application can be dynamically linked. + +blksnap console tool +-------------------- + +The blksnap [#userspace_tools]_ console tool allows to control the module from +the command line. The tool contains detailed built-in help. To get list of +commands with usage description, see ``blksnap --help`` command. The ``blksnap +<command name> --help`` command allows to get detailed information about the +parameters of each command call. This option may be convenient when creating +proprietary software, as it allows not to compile with the open source code. +At the same time, the blksnap tool can be used for creating backup scripts. +For example, rsync can be called to synchronize files on the filesystem of +the mounted snapshot image and files in the archive on a filesystem that +supports compression. + +Tests +----- + +A set of tests was created for regression testing [#userspace_tests]_. +Tests with simple algorithms that use the ``blksnap`` console tool to +control the module are written in Bash. More complex testing algorithms +are implemented in C++. + +References +========== + +.. [#userspace_libs] https://github.com/veeam/blksnap/tree/stable-v2.0/lib + +.. [#userspace_tools] https://github.com/veeam/blksnap/tree/stable-v2.0/tools + +.. [#userspace_tests] https://github.com/veeam/blksnap/tree/stable-v2.0/tests + +Module interface description +============================ + +.. kernel-doc:: include/uapi/linux/blksnap.h
Documentation/block/index.rst +1 -0
@@ -11,6 +11,7 @@ Block biovecs blk-mq blkfilter + blksnap cmdline-partition data-integrity deadline-iosched
MAINTAINERS +6 -0
@@ -3593,6 +3593,12 @@ F: block/blk-filter.c F: include/linux/blk-filter.h F: include/uapi/linux/blk-filter.h +BLOCK DEVICE SNAPSHOTS MODULE +M: Sergei Shtepa <[email protected]> +L: [email protected] +S: Supported +F: Documentation/block/blksnap.rst + BLOCK LAYER M: Jens Axboe <[email protected]> L: [email protected]
Signature · 1 line
2.20.1
PATCH v6 4/11 blksnap: header file of the module interface
In reply to Sergei ShtepaFrom: Sergei Shtepa <[email protected]>
The header file contains a set of declarations, structures and control requests (ioctl) that allows to manage the module from the user space.
--- .../userspace-api/ioctl/ioctl-number.rst | 1 + MAINTAINERS | 1 + include/uapi/linux/blksnap.h | 388 ++++++++++++++++++ 3 files changed, 390 insertions(+) create mode 100644 include/uapi/linux/blksnap.h
Documentation/userspace-api/ioctl/ioctl-number.rst +1 -0
@@ -203,6 +203,7 @@ Code Seq# Include File Comments 'V' C0 linux/ivtvfb.h conflict! 'V' C0 linux/ivtv.h conflict! 'V' C0 media/si4713.h conflict! +'V' 00-1F uapi/linux/blksnap.h conflict! 'W' 00-1F linux/watchdog.h conflict! 'W' 00-1F linux/wanrouter.h conflict! (pre 3.9) 'W' 00-3F sound/asound.h conflict!
MAINTAINERS +1 -0
@@ -3598,6 +3598,7 @@ M: Sergei Shtepa <[email protected]> L: [email protected] S: Supported F: Documentation/block/blksnap.rst +F: include/uapi/linux/blksnap.h BLOCK LAYER M: Jens Axboe <[email protected]>
include/uapi/linux/blksnap.h new file +388 -0
388 lines not on this page · raw message
Signature · 1 line
2.20.1