btrfs: introduce write-intent bitmaps for RAID56
cover btrfs: introduce write-intent bitmaps for RAID56
[BACKGROUND]
Unlike md-raid, btrfs RAID56 has nothing to sync its devices when power loss happens.
For pure mirror based profiles it's fine as btrfs can utilize its csums to find the correct mirror the repair the bad ones.
But for RAID56, the repair itself needs the data from other devices, thus any out-of-sync data can degrade the tolerance.
Even worse, incorrect RMW can use the stale data to generate P/Q, removing the possibility of recovery the data.
For md-raid, it goes with write-intent bitmap, to do faster resilver, and goes journal (partial parity log for RAID5) to ensure it can even stand a powerloss + device lose.
[OBJECTIVE]
This patchset will introduce a btrfs specific write-intent bitmap.
The bitmap will locate at physical offset 1MiB of each device, and the content is the same between all devices.
When there is a RAID56 write (currently all RAID56 write, including full stripe write), before submitting all the real bios to disks, write-intent bitmap will be updated and flushed to all writeable devices.
So even if a powerloss happened, at the next mount time we know which full stripes needs to check, and can start a scrub for those involved logical bytenr ranges.
[NO RECOVERY CODE YET]
Unfortunately, this patchset only implements the write-intent bitmap code, the recovery part is still a place holder, as we need some scrub refactor to make it only scrub a logical bytenr range.
[ADVANTAGE OF BTRFS SPECIFIC WRITE-INTENT BITMAPS]
Since btrfs can utilize csum for its metadata and CoWed data, unlike dm-bitmap which can only be used for faster re-silver, we can fully rebuild the full stripe, as long as:
1) There is no missing device
For missing device case, we still need to go full journal.
2) Untouched data stays untouched
This should be mostly sane for sane hardware.
And since the btrfs specific write-intent bitmaps are pretty small (4KiB in size), the overhead much lower than full journal.
In the future, we may allow users to choose between just bitmaps or full journal to meet their requirement.
[BITMAPS DESIGN]
The bitmaps on-disk format looks like this:
[ super ][ entry 1 ][ entry 2 ] ... [entry N]
|<--------- super::size (4K) ------------->|
Super block contains how many entires are in use.
Each entry is 128 bits (16 bytes) in size, containing one u64 for bytenr, and u64 for one bitmap.
And all utilized entries will be sorted in their bytenr order, and no bit can overlap.
The blocksize is now fixed to BTRFS_STRIPE_LEN (64KiB), so each entry can contain at most 4MiB, and the whole bitmaps can contain 224 entries.
For the worst case, it can contain 14MiB dirty ranges. (1 bits set per bitmap, also means 2 disks RAID5 or 3 disks RAID6).
For the best case, it can contain 896MiB dirty ranges. (all bits set per bitmap)
[WHY NOT BTRFS BTREE]
Current write-intent structure needs two features:
- Its data needs to survive cross stripe boundary Normally this means write-intent btree needs to acts like a proper tree root, has METADATA_ITEMs for all its tree blocks.
- Its data update must be outside of a transaction Currently only log tree can do such thing.
But unfortunately log tree can not survive across transaction boundary.
Thus write-intent btree can only meet one of the requirement, not a suitable solution here.
[TESTING AND BENCHMARK]
For performance benchmark, unfortunately I don't have 3 HDDs to test. Will do the benchmark after secured enough hardware.
For testing, it can survive volume/raid/dev-replace test groups, and no write-intent bitmap leakage.
Unfortunately there is still a warning triggered in btrfs/070, still under investigation, hopefully to be a false alert in bitmap clearing path.
[TODO]
- Scrub refactor to allow us to do proper recovery at mount time Need to change scrub interface to scrub based on logical bytenr.
This can be a super big work, thus currently we will focus only on RAID56 new scrub interface for write-intent recovery only.
- Extra optimizations
* Skip full stripe writes
* Enlarge the window between btrfs_write_intent_mark_dirty() and btrfs_write_intent_writeback()
- Bug hunts and more fstests runs
- Proper performance benchmark
Needs hardware/baremetal VMs, since I don't have any physical machine large enough to contian 3 3.5" HDDs.
Qu Wenruo (11):
btrfs: introduce new compat RO flag, EXTRA_SUPER_RESERVED
btrfs: introduce a new experimental compat RO flag,
WRITE_INTENT_BITMAP
btrfs: introduce the on-disk format of btrfs write intent bitmaps
btrfs: load/create write-intent bitmaps at mount time
btrfs: write-intent: write the newly created bitmaps to all disks
btrfs: write-intent: introduce an internal helper to set bits for a
range.
btrfs: write-intent: introduce an internal helper to clear bits for a
range.
btrfs: write back write intent bitmap after barrier_all_devices()
btrfs: update and writeback the write-intent bitmap for RAID56 write.
btrfs: raid56: clear write-intent bimaps when a full stripe finishes.
btrfs: warn and clear bitmaps if there is dirty bitmap at mount time
fs/btrfs/Makefile | 2 +- fs/btrfs/ctree.h | 24 +- fs/btrfs/disk-io.c | 54 +++ fs/btrfs/raid56.c | 16 + fs/btrfs/sysfs.c | 2 + fs/btrfs/volumes.c | 34 +- fs/btrfs/write-intent.c | 962 +++++++++++++++++++++++++++++++++++++ fs/btrfs/write-intent.h | 288 +++++++++++ fs/btrfs/zoned.c | 8 + include/uapi/linux/btrfs.h | 17 + 10 files changed, 1399 insertions(+), 8 deletions(-) create mode 100644 fs/btrfs/write-intent.c create mode 100644 fs/btrfs/write-intent.h
Signature · 1 line
2.36.1
RFC PATCH 1/11 btrfs: introduce new compat RO flag, EXTRA_SUPER_RESERVED
From the beginning of btrfs kernel module, kernel will avoid allocating dev extents into the first 1MiB of each device, not only for the superblock at 64KiB, but also for legacy bootloaders to store their data.
Here for later expansion, we introduce a new compat RO flag, EXTRA_SUPER_RESERVED, this allows btrfs to have extra reserved space beyond the default 1MiB.
The extra reserved space can be utilized by things like write-intent log.
Several new checks are introduced:
- No super_reserved_bytes should be smaller than the old 1MiB limit
- No zoned device support
Such reserved space will be utilized in a no COW way, thus it can not be supported by zoned device.
We still allow dev extents to exist in the new reserved_bytes range, this is to allow btrfstune to set reserved_bytes, then rely on kernel balance to reserve the new space.
But later, if there is any feature relying on the reserved_bytes, we will disable those features automatically.
There is a special catch, the new member @reserved_bytes is located at the end of the superblock, to utilize the padding bytes.
The reason is, due to some undetermined extent tree v2 on-disk format mess, we don't have reliable location inside the reserved8[] array. I just want a stable location undisturbed.
--- fs/btrfs/ctree.h | 14 +++++++++++--- fs/btrfs/disk-io.c | 9 +++++++++ fs/btrfs/sysfs.c | 2 ++ fs/btrfs/volumes.c | 22 ++++++++++++++++++---- fs/btrfs/zoned.c | 8 ++++++++ include/uapi/linux/btrfs.h | 10 ++++++++++ 6 files changed, 58 insertions(+), 7 deletions(-)
fs/btrfs/ctree.h +11 -3
@@ -293,14 +293,19 @@ struct btrfs_super_block {__le64 block_group_root_generation;u8 block_group_root_level; -/* future expansion */ u8 reserved8[7 ];__le64 reserved[25 ];u8 sys_chunk_array[BTRFS_SYSTEM_CHUNK_ARRAY_SIZE ];struct btrfs_root_backup super_roots[BTRFS_NUM_BACKUP_ROOTS ]; +/* +* How many bytes are reserved at the beginning of a device. +* Should be >= BTRFS_DEFAULT_RESERVED. +*/ +__le32 reserved_bytes; +/* Padded to 4096 bytes */ -u8 padding[565 ]; +u8 padding[561 ]; } __attribute__ ((__packed__));static_assert (sizeof (struct btrfs_super_block ) ==BTRFS_SUPER_INFO_SIZE );
@@ -315,7 +320,8 @@ static_assert(sizeof(struct btrfs_super_block) == BTRFS_SUPER_INFO_SIZE);#define BTRFS_FEATURE_COMPAT_RO_SUPP \ (BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE | \ BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE_VALID | \ - BTRFS_FEATURE_COMPAT_RO_VERITY) + BTRFS_FEATURE_COMPAT_RO_VERITY | \ + BTRFS_FEATURE_COMPAT_RO_EXTRA_SUPER_RESERVED)#define BTRFS_FEATURE_COMPAT_RO_SAFE_SET 0ULL#define BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR 0ULL
@@ -2539,6 +2545,8 @@ BTRFS_SETGET_STACK_FUNCS(super_block_group_root_generation, block_group_root_generation, 64); BTRFS_SETGET_STACK_FUNCS(super_block_group_root_level, struct btrfs_super_block, block_group_root_level,8 ); +BTRFS_SETGET_STACK_FUNCS(super_reserved_bytes, struct btrfs_super_block, + reserved_bytes,32 );int btrfs_super_csum_size (const struct btrfs_super_block *s);const char *btrfs_super_csum_name (u16 csum_type);
fs/btrfs/disk-io.c +9 -0
@@ -2822,6 +2822,15 @@ static int validate_super(struct btrfs_fs_info *fs_info, ret = -EINVAL ; } +if (btrfs_super_compat_ro_flags (sb) & +BTRFS_FEATURE_COMPAT_RO_EXTRA_SUPER_RESERVED && +btrfs_super_reserved_bytes (sb) <BTRFS_DEVICE_RANGE_RESERVED ) { +btrfs_err (fs_info, +"EXTRA_SUPER_RESERVED feature enabled, but reserved space is smaller than default (%u)" , +BTRFS_DEVICE_RANGE_RESERVED ); + ret = -EINVAL ; + } +/* * The generation is a global counter, we'll trust it more than the others * but it's still possible that it's the one that's wrong.
fs/btrfs/sysfs.c +2 -0
@@ -287,6 +287,7 @@ BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES);BTRFS_FEAT_ATTR_INCOMPAT (metadata_uuid,METADATA_UUID );BTRFS_FEAT_ATTR_COMPAT_RO (free_space_tree,FREE_SPACE_TREE );BTRFS_FEAT_ATTR_INCOMPAT (raid1c34,RAID1C34 ); +BTRFS_FEAT_ATTR_COMPAT_RO (extra_super_reserved,EXTRA_SUPER_RESERVED );#ifdef CONFIG_BLK_DEV_ZONED BTRFS_FEAT_ATTR_INCOMPAT (zoned,ZONED );#endif
@@ -317,6 +318,7 @@ static struct attribute *btrfs_supported_feature_attrs[] = {BTRFS_FEAT_ATTR_PTR (metadata_uuid),BTRFS_FEAT_ATTR_PTR (free_space_tree),BTRFS_FEAT_ATTR_PTR (raid1c34), +BTRFS_FEAT_ATTR_PTR (extra_super_reserved), #ifdef CONFIG_BLK_DEV_ZONED BTRFS_FEAT_ATTR_PTR(zoned), #endif
fs/btrfs/volumes.c +18 -4
@@ -1401,8 +1401,13 @@ static bool contains_pending_extent(struct btrfs_device *device, u64 *start,static u64 dev_extent_search_start (struct btrfs_device *device,u64 start) { +struct btrfs_fs_info *fs_info = device->fs_devices->fs_info; +switch (device->fs_devices->chunk_alloc_policy) {case BTRFS_CHUNK_ALLOC_REGULAR : +if (btrfs_fs_compat_ro (fs_info,EXTRA_SUPER_RESERVED )) +return max_t (u64, start, +btrfs_super_reserved_bytes (fs_info->super_copy));return max_t (u64, start,BTRFS_DEVICE_RANGE_RESERVED );case BTRFS_CHUNK_ALLOC_ZONED :/*
@@ -7969,11 +7974,14 @@ static int verify_one_dev_extent(struct btrfs_fs_info *fs_info,struct extent_map *em;struct map_lookup *map;struct btrfs_device *dev; +u32 super_reserved =BTRFS_DEVICE_RANGE_RESERVED ;u64 stripe_len;bool found = false;int ret =0 ;int i; +if (btrfs_fs_compat_ro (fs_info,EXTRA_SUPER_RESERVED )) + super_reserved =btrfs_super_reserved_bytes (fs_info->super_copy);read_lock (&em_tree->lock); em =lookup_extent_mapping (em_tree, chunk_offset,1 );read_unlock (&em_tree->lock);
@@ -7998,11 +8006,17 @@ static int verify_one_dev_extent(struct btrfs_fs_info *fs_info, }/* -* Very old mkfs.btrfs (before v4.1) will not respect the reserved -* space. Although kernel can handle it without problem, better to warn -* the users. +* This can be caused by two cases: +* - Very old mkfs.btrfs (before v4.1) and no balance at all +* This should be pretty rare now, as balance will relocate +* those dev extents in reserved range. +* +* - Newly set btrfs_super_block::reserved_bytes +* We are rely on this mount to relocate those dev extents. +* +* So here, we just give a warning and continue the mount. */ -if (physical_offset <BTRFS_DEVICE_RANGE_RESERVED ) +if (physical_offset < super_reserved)btrfs_warn (fs_info,"devid %llu physical %llu len %llu inside the reserved space" , devid, physical_offset, physical_len);
fs/btrfs/zoned.c +8 -0
@@ -703,6 +703,14 @@ int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info) goto out; } +if (incompat_zoned &&btrfs_fs_compat_ro (fs_info,EXTRA_SUPER_RESERVED )) { +btrfs_err (fs_info, +"zoned: incompatible optional feature detected: 0x%llx" , +BTRFS_FEATURE_COMPAT_RO_EXTRA_SUPER_RESERVED ); + ret = -EINVAL ; + goto out; + } +/* * stripe_size is always aligned to BTRFS_STRIPE_LEN in * btrfs_create_chunk(). Since we want stripe_len == zone_size,
include/uapi/linux/btrfs.h +10 -0
@@ -290,6 +290,16 @@ struct btrfs_ioctl_fs_info_args {#define BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE_VALID (1ULL << 1)#define BTRFS_FEATURE_COMPAT_RO_VERITY (1ULL << 2) +/* +* Allow btrfs to have extra reserved space (other than the default 1MiB) at +* the beginning of each device. +* +* This feature will enable the usage of btrfs_super_block::reserved_bytes. +* +* This feature would only be available for non-zoned filesystems. +*/ +#define BTRFS_FEATURE_COMPAT_RO_EXTRA_SUPER_RESERVED (1ULL << 3) +#define BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF (1ULL << 0)#define BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL (1ULL << 1)#define BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS (1ULL << 2)
Signature · 1 line
2.36.1
RFC PATCH 2/11 btrfs: introduce a new experimental compat RO flag, WRITE_INTENT_BITMAP
In reply to Qu WenruoThe new flag is for the incoming write intent bitmap, mostly to address the RAID56 write-hole, by doing a mandatory scrub for partial written stripes at mount time.
Currently the feature is still under development, this patch is mostly a placeholder for the extra reserved bytes for write intent bitmap.
We will utilize the newly introduce EXTRA_SUPER_RESERVED compat RO flags to enlarge the reserved bytes to at least (1MiB + 64KiB), and use that 64KiB (exact value is not yet fully determined) for write-intent bitmap.
Only one extra check is introduced, to ensure we have enough space to place the write-intent bitmap at 1MiB physical offset.
This patch is only a place holder for the incoming on-disk format change, no real write-intent functionality is implemented yet.
--- fs/btrfs/ctree.h | 9 +++++++++ fs/btrfs/disk-io.c | 20 ++++++++++++++++++++ fs/btrfs/volumes.c | 14 +++++++++++++- include/uapi/linux/btrfs.h | 7 +++++++ 4 files changed, 49 insertions(+), 1 deletion(-)
fs/btrfs/ctree.h +9 -0
@@ -317,11 +317,20 @@ static_assert(sizeof(struct btrfs_super_block) == BTRFS_SUPER_INFO_SIZE);#define BTRFS_FEATURE_COMPAT_SAFE_SET 0ULL#define BTRFS_FEATURE_COMPAT_SAFE_CLEAR 0ULL +#ifdef CONFIG_BTRFS_DEBUG +#define BTRFS_FEATURE_COMPAT_RO_SUPP \ + (BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE | \ + BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE_VALID | \ + BTRFS_FEATURE_COMPAT_RO_VERITY | \ + BTRFS_FEATURE_COMPAT_RO_EXTRA_SUPER_RESERVED | \ + BTRFS_FEATURE_COMPAT_RO_WRITE_INTENT_BITMAP) +#else #define BTRFS_FEATURE_COMPAT_RO_SUPP \ (BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE | \ BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE_VALID | \ BTRFS_FEATURE_COMPAT_RO_VERITY | \ BTRFS_FEATURE_COMPAT_RO_EXTRA_SUPER_RESERVED) +#endif #define BTRFS_FEATURE_COMPAT_RO_SAFE_SET 0ULL#define BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR 0ULL
fs/btrfs/disk-io.c +20 -0
@@ -2830,6 +2830,26 @@ static int validate_super(struct btrfs_fs_info *fs_info, BTRFS_DEVICE_RANGE_RESERVED); ret = -EINVAL ; } +if (btrfs_super_compat_ro_flags (sb) & +BTRFS_FEATURE_COMPAT_RO_WRITE_INTENT_BITMAP ) { +/* Write intent bitmap requires extra reserve. */ +if (!(btrfs_super_compat_ro_flags (sb) & +BTRFS_FEATURE_COMPAT_RO_EXTRA_SUPER_RESERVED )) { +btrfs_err (fs_info, +"WRITE_INTENT_BITMAP feature enabled, but missing EXTRA_SUPER_RESERVED feature" ); + ret = -EINVAL ; + } +/* +* Write intent bitmap is always located at 1MiB. +* Extra check like the length check against the reserved space +* will happen at bitmap load time. +*/ +if (btrfs_super_reserved_bytes (sb) <BTRFS_DEVICE_RANGE_RESERVED ) { +btrfs_err (fs_info, +"not enough reserved space for write intent bitmap" ); + ret = -EINVAL ; + } + }/* * The generation is a global counter, we'll trust it more than the others
fs/btrfs/volumes.c +13 -1
@@ -8016,11 +8016,23 @@ static int verify_one_dev_extent(struct btrfs_fs_info *fs_info,* * So here, we just give a warning and continue the mount. */ -if (physical_offset < super_reserved) +if (physical_offset < super_reserved) {btrfs_warn (fs_info,"devid %llu physical %llu len %llu inside the reserved space" , devid, physical_offset, physical_len); +/* Disable any feature relying on the new reserved_bytes. */ +if (btrfs_fs_compat_ro (fs_info,WRITE_INTENT_BITMAP )) { +struct btrfs_super_block *sb = fs_info->super_copy; + +btrfs_warn (fs_info, +"disabling write intent bitmap due to the lack of reserved space." ); +btrfs_set_super_compat_ro_flags (sb, +btrfs_super_compat_ro_flags (sb) | + ~BTRFS_FEATURE_COMPAT_RO_WRITE_INTENT_BITMAP ); + } + } +for (i =0 ; i < map->num_stripes; i++) {if (map->stripes[i].dev->devid == devid && map->stripes[i].physical == physical_offset) {
include/uapi/linux/btrfs.h +7 -0
@@ -300,6 +300,13 @@ struct btrfs_ioctl_fs_info_args {*/ #define BTRFS_FEATURE_COMPAT_RO_EXTRA_SUPER_RESERVED (1ULL << 3) +/* +* Allow btrfs to have per-device write-intent bitmap. +* Will be utilized to close the RAID56 write-hole (by forced scrub for dirty +* partial written stripes at mount time). +*/ +#define BTRFS_FEATURE_COMPAT_RO_WRITE_INTENT_BITMAP (1ULL << 4) +#define BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF (1ULL << 0)#define BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL (1ULL << 1)#define BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS (1ULL << 2)
Signature · 1 line
2.36.1
RFC PATCH 3/11 btrfs: introduce the on-disk format of btrfs write intent bitmaps
In reply to Qu WenruoWith extra comments explaining the on-disk format and the basic workflow.
--- fs/btrfs/disk-io.c | 4 +- fs/btrfs/raid56.c | 1 + fs/btrfs/write-intent.h | 199 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 fs/btrfs/write-intent.h
fs/btrfs/disk-io.c +3 -1
@@ -42,6 +42,7 @@#include "space-info.h" #include "zoned.h" #include "subpage.h" +#include "write-intent.h" #define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\ BTRFS_HEADER_FLAG_RELOC |\
@@ -2844,7 +2845,8 @@ static int validate_super(struct btrfs_fs_info *fs_info,* Extra check like the length check against the reserved space * will happen at bitmap load time. */ -if (btrfs_super_reserved_bytes (sb) <BTRFS_DEVICE_RANGE_RESERVED ) { +if (btrfs_super_reserved_bytes (sb) < +BTRFS_DEVICE_RANGE_RESERVED +WRITE_INTENT_BITMAPS_SIZE ) {btrfs_err (fs_info,"not enough reserved space for write intent bitmap" ); ret = -EINVAL ;
fs/btrfs/raid56.c +1 -0
@@ -19,6 +19,7 @@#include "volumes.h" #include "raid56.h" #include "async-thread.h" +#include "write-intent.h" /* set when additional merges to this rbio are not allowed */ #define RBIO_RMW_LOCKED_BIT 1
fs/btrfs/write-intent.h new file +199 -0
199 lines not on this page · raw message
Signature · 1 line
2.36.1
RFC PATCH 4/11 btrfs: load/create write-intent bitmaps at mount time
In reply to Qu WenruoThis patch will introduce btrfs_fs_info::wi_ctrl, which will have a non-highmem page for the write intent bitmaps.
Please note that, if we can't find a valid bitmaps, the newly create one will only be in memory for now, the bitmaps writeback functionality will be introduced in the next commit.
--- fs/btrfs/Makefile | 2 +- fs/btrfs/ctree.h | 1 + fs/btrfs/disk-io.c | 7 ++ fs/btrfs/write-intent.c | 174 ++++++++++++++++++++++++++++++++++++++++ fs/btrfs/write-intent.h | 15 ++++ 5 files changed, 198 insertions(+), 1 deletion(-) create mode 100644 fs/btrfs/write-intent.c
fs/btrfs/Makefile +1 -1
@@ -31,7 +31,7 @@ btrfs-y += super.o ctree.o extent-tree.o print-tree.o root-tree.o dir-item.o \ backref.o ulist.o qgroup.o send.o dev-replace.o raid56.o \ uuid-tree.o props.o free-space-tree.o tree-checker.o space-info.o \ block-rsv.o delalloc-space.o block-group.o discard.o reflink.o \ - subpage.o tree-mod-log.o + subpage.o tree-mod-log.o write-intent.o btrfs-$(CONFIG_BTRFS_FS_POSIX_ACL) += acl.o btrfs-$(CONFIG_BTRFS_FS_CHECK_INTEGRITY) += check-integrity.o
fs/btrfs/ctree.h +1 -0
@@ -989,6 +989,7 @@ struct btrfs_fs_info {struct workqueue_struct *scrub_wr_completion_workers;struct workqueue_struct *scrub_parity_workers;struct btrfs_subpage_info *subpage_info; +struct write_intent_ctrl *wi_ctrl;struct btrfs_discard_ctl discard_ctl;
fs/btrfs/disk-io.c +7 -0
@@ -3699,6 +3699,12 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device ret); goto fail_block_groups; } + ret =btrfs_write_intent_init (fs_info); +if (ret) { +btrfs_err (fs_info,"failed to init write-intent bitmaps: %d" , + ret); + goto fail_block_groups; + } ret =btrfs_recover_balance (fs_info);if (ret) {btrfs_err (fs_info,"failed to recover balance: %d" , ret);
@@ -4639,6 +4645,7 @@ void __cold close_ctree(struct btrfs_fs_info *fs_info) ret =btrfs_commit_super (fs_info);if (ret)btrfs_err (fs_info,"commit super ret %d" , ret); +btrfs_write_intent_free (fs_info); }if (BTRFS_FS_ERROR (fs_info))
fs/btrfs/write-intent.c new file +174 -0
174 lines not on this page · raw message
fs/btrfs/write-intent.h +15 -0
@@ -106,6 +106,18 @@ struct write_intent_entry {/* The number of bits we can have in one entry. */ #define WRITE_INTENT_BITS_PER_ENTRY (64) +/* In-memory write-intent control structure. */ +struct write_intent_ctrl { +/* For the write_intent super and entries. */ +struct page *page; + +/* Cached event counter.*/ +atomic64_t event; + +/* Cached blocksize from write intent super. */ +u32 blocksize; +}; +/* * ON-DISK FORMAT * ==============
@@ -196,4 +208,7 @@ static inline void wie_set_bitmap(struct write_intent_entry *entry, #endif } +int btrfs_write_intent_init (struct btrfs_fs_info *fs_info); +void btrfs_write_intent_free (struct btrfs_fs_info *fs_info); +#endif
Signature · 1 line
2.36.1
RFC PATCH 5/11 btrfs: write-intent: write the newly created bitmaps to all disks
In reply to Qu WenruoThis write back will happen even for RO mounts.
This will ensure we always have write-intent bitmaps for fses with that compat RO flags set.
--- fs/btrfs/write-intent.c | 197 +++++++++++++++++++++++++++++++++++++++- fs/btrfs/write-intent.h | 6 ++ 2 files changed, 198 insertions(+), 5 deletions(-)
fs/btrfs/write-intent.c +192 -5
@@ -1,8 +1,183 @@// SPDX-License-Identifier: GPL-2.0 +#include <crypto/hash.h> +#include <linux/bio.h> #include "ctree.h" #include "volumes.h" #include "write-intent.h" +#include "rcu-string.h" + +static void write_intent_end_write (struct bio *bio) +{ +struct btrfs_device *device = bio->bi_private; +struct bio_vec *bvec; +struct bvec_iter_all iter_all; +struct page *page; + +bio_for_each_segment_all (bvec, bio, iter_all) { + page = bvec->bv_page; + +if (bio->bi_status) { +btrfs_warn_rl_in_rcu (device->fs_info, +"write-intent bitmap update failed on %s (%d)" , +rcu_str_deref (device->name), +blk_status_to_errno (bio->bi_status)); +ClearPageUptodate (page); +btrfs_dev_stat_inc_and_print (device, +BTRFS_DEV_STAT_WRITE_ERRS ); + }else { +SetPageUptodate (page); + } + +unlock_page (page); +put_page (page); + } + +bio_put (bio); +} + +static int submit_one_device (struct btrfs_device *dev) +{ +struct btrfs_fs_info *fs_info = dev->fs_info; +struct write_intent_ctrl *ctrl = fs_info->wi_ctrl; +struct address_space *mapping; +struct page *page; +struct bio *bio; + +if (!dev->bdev) +return -EIO ; + +if (!test_bit (BTRFS_DEV_STATE_IN_FS_METADATA , &dev->dev_state) || + !test_bit (BTRFS_DEV_STATE_WRITEABLE , &dev->dev_state)) +return 0 ; + + mapping = dev->bdev->bd_inode->i_mapping; + + page =find_or_create_page (mapping, +BTRFS_DEVICE_RANGE_RESERVED >>PAGE_SHIFT , +GFP_NOFS ); +if (!page) { +btrfs_err (fs_info, +"couldn't get write intent bitmap page for devid %llu" , + dev->devid); +return -EIO ; + } + +/* Bump the refcount for later wait on this page */ +get_page (page); +memcpy_page (page,offset_in_page (BTRFS_DEVICE_RANGE_RESERVED ), + ctrl->commit_page,0 ,WRITE_INTENT_BITMAPS_SIZE ); + bio =bio_alloc (dev->bdev,1 ,REQ_OP_WRITE |REQ_SYNC | +REQ_META |REQ_PRIO |REQ_FUA ,GFP_NOFS ); + bio->bi_iter.bi_sector =BTRFS_DEVICE_RANGE_RESERVED >>SECTOR_SHIFT ; + bio->bi_private = dev; + bio->bi_end_io = write_intent_end_write; +__bio_add_page (bio, page,WRITE_INTENT_BITMAPS_SIZE , +offset_in_page (BTRFS_DEVICE_RANGE_RESERVED )); +submit_bio (bio); +return 0 ; +} + +static int wait_one_device (struct btrfs_device *dev) +{ +struct btrfs_fs_info *fs_info = dev->fs_info; +struct page *page; +int ret =0 ; + +/* +* This missing device has already been accounted in +* submit_one_device(), no need to report error again. +*/ +if (!dev->bdev) +return 0 ; + +if (!test_bit (BTRFS_DEV_STATE_IN_FS_METADATA , &dev->dev_state) || + !test_bit (BTRFS_DEV_STATE_WRITEABLE , &dev->dev_state)) +return 0 ; + + page =find_get_page (dev->bdev->bd_inode->i_mapping, +BTRFS_DEVICE_RANGE_RESERVED >>PAGE_SHIFT ); +if (!page) { +btrfs_err (fs_info, +"couldn't wait write intent bitmap page for devid %llu" , + dev->devid); +return -EIO ; + } + +/* The endio will unlock the page. */ +wait_on_page_locked (page); +if (!PageUptodate (page)) + ret = -EIO ; + +/* Drop our reference */ +put_page (page); + +/* Drop the reference bumped by submit_one_device() */ +put_page (page); + +return ret; +} + +/* Write back the page to all devices. */ +static int write_intent_writeback (struct btrfs_fs_info *fs_info) +{ +struct write_intent_ctrl *ctrl = fs_info->wi_ctrl; +struct write_intent_super *wis; +struct btrfs_device *dev; +SHASH_DESC_ON_STACK (shash, fs_info->csum_shash); +int total_errors =0 ; +int ret; + +ASSERT (ctrl); + + shash->tfm = fs_info->csum_shash; + +spin_lock (&ctrl->lock); + wis =page_address (ctrl->page); + +/* +* Bump up the event counter each time this bitmap is going to be +* written. +*/ +wi_set_super_events (wis,wi_super_events (wis) +1 ); +crypto_shash_digest (shash, (unsigned char *)wis +BTRFS_CSUM_SIZE , +WRITE_INTENT_BITMAPS_SIZE -BTRFS_CSUM_SIZE , + wis->csum); +atomic64_inc (&ctrl->event); +memcpy_page (ctrl->commit_page,0 , ctrl->page,0 , +WRITE_INTENT_BITMAPS_SIZE ); +spin_unlock (&ctrl->lock); + +mutex_lock (&fs_info->fs_devices->device_list_mutex); +/* +* Go through all the writeable devices, copy the bitmap page into the +* page cache, and submit them (without waiting). +* +* We will later check the page status to make sure they reached disk. +*/ +list_for_each_entry (dev, &fs_info->fs_devices->devices, dev_list) { + ret =submit_one_device (dev); +if (ret <0 ) + total_errors++; + } +/* +* Wait for the submitted page to finish on each device. +* By this we can submit all write intent bitmaps in one go, without +* waiting each one. +*/ +list_for_each_entry (dev, &fs_info->fs_devices->devices, dev_list) { + ret =wait_one_device (dev); +if (ret <0 ) + total_errors++; + } +mutex_unlock (&fs_info->fs_devices->device_list_mutex); + +if (total_errors >btrfs_super_num_devices (fs_info->super_copy) -1 ) { +btrfs_err (fs_info,"failed to writeback write-intent bitmaps" ); +return -EIO ; + } +return 0 ; +}/* * Return 0 if a valid write intent bitmap can be found.
@@ -53,10 +228,11 @@ static int write_intent_load(struct btrfs_device *device, struct page *dst)return ret; } -static void write_intent_init (struct btrfs_fs_info *fs_info) +static int write_intent_init (struct btrfs_fs_info *fs_info) {struct write_intent_ctrl *ctrl = fs_info->wi_ctrl;struct write_intent_super *wis; +int ret;ASSERT (ctrl);ASSERT (ctrl->page);
@@ -75,7 +251,12 @@ static void write_intent_init(struct btrfs_fs_info *fs_info)wi_set_super_size (wis,WRITE_INTENT_BITMAPS_SIZE );wi_set_super_blocksize (wis, ctrl->blocksize);wi_set_super_nr_entries (wis,0 ); -btrfs_info (fs_info,"creating new write intent bitmaps" ); + + ret =write_intent_writeback (fs_info); +if (ret <0 ) +return ret; +btrfs_info (fs_info,"new write intent bitmaps created" ); +return 0 ; }int btrfs_write_intent_init (struct btrfs_fs_info *fs_info)
@@ -95,11 +276,14 @@ int btrfs_write_intent_init(struct btrfs_fs_info *fs_info)return -ENOMEM ; fs_info->wi_ctrl->page =alloc_page (GFP_NOFS ); -if (!fs_info->wi_ctrl->page) { + fs_info->wi_ctrl->commit_page =alloc_page (GFP_NOFS ); +if (!fs_info->wi_ctrl->page || !fs_info->wi_ctrl->commit_page) { ret = -ENOMEM ; goto cleanup; } +spin_lock_init (&fs_info->wi_ctrl->lock); +/* * Go through every writeable device to find the highest event. *
@@ -149,12 +333,15 @@ int btrfs_write_intent_init(struct btrfs_fs_info *fs_info) }/* No valid bitmap found, create a new one. */ -write_intent_init (fs_info); -return 0 ; + ret =write_intent_init (fs_info); + +return ret; cleanup:if (fs_info->wi_ctrl) {if (fs_info->wi_ctrl->page)__free_page (fs_info->wi_ctrl->page); +if (fs_info->wi_ctrl->commit_page) +__free_page (fs_info->wi_ctrl->commit_page);kfree (fs_info->wi_ctrl); fs_info->wi_ctrl =NULL ; }
fs/btrfs/write-intent.h +6 -0
@@ -111,9 +111,15 @@ struct write_intent_ctrl {/* For the write_intent super and entries. */ struct page *page; +/* A copy for writeback. */ +struct page *commit_page; +/* Cached event counter.*/ atomic64_t event; +/* Lock for reading/writing above @page. */ +spinlock_t lock; +/* Cached blocksize from write intent super. */ u32 blocksize; };
Signature · 1 line
2.36.1
RFC PATCH 6/11 btrfs: write-intent: introduce an internal helper to set bits for a range.
In reply to Qu Wenruo--- fs/btrfs/write-intent.c | 251 ++++++++++++++++++++++++++++++++++++++++ fs/btrfs/write-intent.h | 16 +++ 2 files changed, 267 insertions(+)
fs/btrfs/write-intent.c +251 -0
@@ -259,6 +259,257 @@ static int write_intent_init(struct btrfs_fs_info *fs_info)return 0 ; } +static struct write_intent_entry *write_intent_entry_nr ( +struct write_intent_ctrl *ctrl,int nr) +{ + +ASSERT (nr <WRITE_INTENT_INTERNAL_BITMAPS_MAX_ENTRIES ); +return (page_address (ctrl->page) + +sizeof (struct write_intent_super ) + + nr *sizeof (struct write_intent_entry )); +} + +/* +* Return <0 if the bytenr is before the given entry. +* Return 0 if the bytenr is inside the given entry. +* Return >0 if the bytenr is after the given entry. +*/ +static int compare_bytenr_to_range (u64 bytenr,u64 start,u32 len) +{ +if (bytenr < start) +return -1 ; +if (start <= bytenr && bytenr < start + len) +return 0 ; +return 1 ; +} + +/* +* Move all non-empty entries starting from @nr, to the right, and make room +* for @nr_new entries. +* Those new entries will be all zero filled. +* +* Caller should ensure we have enough room for @nr_new new entries. +*/ +static void move_entries_right (struct write_intent_ctrl *ctrl,int nr, +int nr_new) +{ +struct write_intent_super *wis =page_address (ctrl->page); +int move_size; + +ASSERT (nr_new >0 ); +ASSERT (wi_super_nr_entries (wis) + nr_new <= +WRITE_INTENT_INTERNAL_BITMAPS_MAX_ENTRIES ); + + move_size = (wi_super_nr_entries (wis) - nr) * +sizeof (struct write_intent_entry ); + +memmove (write_intent_entry_nr (ctrl, nr + nr_new), +write_intent_entry_nr (ctrl, nr), move_size); +memset (write_intent_entry_nr (ctrl, nr),0 , + nr_new *sizeof (struct write_intent_entry )); +wi_set_super_nr_entries (wis,wi_super_nr_entries (wis) + nr_new); +} + +static void set_bits_in_one_entry (struct write_intent_ctrl *ctrl, +struct write_intent_entry *entry, +u64 bytenr,u32 len) +{ +const u64 entry_start =wi_entry_bytenr (entry); +const u32 entry_len =write_intent_entry_size (ctrl); +unsigned long bitmaps[WRITE_INTENT_BITS_PER_ENTRY /BITS_PER_LONG ]; + +wie_get_bitmap (entry, bitmaps); + +ASSERT (entry_start <= bytenr && bytenr + len <= entry_start + entry_len); +bitmap_set (bitmaps, (bytenr - entry_start) / ctrl->blocksize, + len / ctrl->blocksize); +wie_set_bitmap (entry, bitmaps); +} + +/* +* Insert new entries for the range [@bytenr, @bytenr + @len) at slot @nr +* and fill the new entries with proper bytenr and bitmaps. +*/ +static void insert_new_entries (struct write_intent_ctrl *ctrl,int nr, +u64 bytenr,u32 len) +{ +const u32 entry_size =write_intent_entry_size (ctrl); +u64 entry_start; +u64 new_start =round_down (bytenr, entry_size); +u64 new_end; +int nr_new_entries; +u64 cur; + +if (nr >=wi_super_nr_entries (page_address (ctrl->page)) || + nr >=WRITE_INTENT_INTERNAL_BITMAPS_MAX_ENTRIES ) + entry_start =U64_MAX ; +else + entry_start =wi_entry_bytenr (write_intent_entry_nr (ctrl, nr)); + +ASSERT (bytenr < entry_start); + + new_end =min (entry_start,round_up (bytenr + len, entry_size)); + nr_new_entries = (new_end - new_start) / entry_size; + +if (nr_new_entries ==0 ) +return ; + +move_entries_right (ctrl, nr, nr_new_entries); + +for (cur = new_start; cur < new_end; cur += entry_size, nr++) { +struct write_intent_entry *entry = +write_intent_entry_nr (ctrl, nr); +u64 range_start =max (cur, bytenr); +u64 range_len =min (cur + entry_size, bytenr + len) - + range_start; + +/* Fill the bytenr into the new empty entries.*/ +wi_set_entry_bytenr (entry, cur); + +/* And set the bitmap. */ +set_bits_in_one_entry (ctrl, entry, range_start, range_len); + } +} + +/* +* This should be only called when we have enough room in the bitmaps, and hold +* the wi_ctrl->lock. +*/ +static void write_intent_set_bits (struct write_intent_ctrl *ctrl,u64 bytenr, +u32 len) +{ +struct write_intent_super *wis =page_address (ctrl->page); +const u32 entry_size =write_intent_entry_size (ctrl); +int i; +u64 nr_entries =wi_super_nr_entries (wis); +u64 cur_bytenr; + +/* +* Currently we only accept full stripe length, which should be +* aligned to 64KiB. +*/ +ASSERT (IS_ALIGNED (len,BTRFS_STRIPE_LEN )); + +/* +* We should have room to contain the worst case scenario, in which we +* need to create one or more new entry. +*/ +ASSERT (nr_entries +bytes_to_entries (bytenr, len,BTRFS_STRIPE_LEN ) <= +WRITE_INTENT_INTERNAL_BITMAPS_MAX_ENTRIES ); + +/* Empty bitmap, just insert new ones. */ +if (wi_super_nr_entries (wis) ==0 ) +return insert_new_entries (ctrl,0 , bytenr, len); + +/* Go through entries to find the one covering our range. */ +for (i =0 , cur_bytenr = bytenr; + i <wi_super_nr_entries (wis) && cur_bytenr < bytenr + len; i++) { +struct write_intent_entry *entry =write_intent_entry_nr (ctrl, i); +u64 entry_start =wi_entry_bytenr (entry); +u64 entry_end = entry_start + entry_size; + +/* +* |<-- entry -->| +* |<-- bytenr/len -->| +* +* Or +* +* |<-- entry -->| +* |<-- bytenr/len -->| +* +* Or +* +* |<-- entry -->| +* |<-- bytenr/len -->| +* +* We need to insert one or more new entries for the range not +* covered by the existing entry. +*/ +if (compare_bytenr_to_range (cur_bytenr, entry_start, + entry_size) <0 ) { +u64 new_range_end; + + new_range_end =min (entry_start, bytenr + len); +insert_new_entries (ctrl, i, cur_bytenr, + new_range_end - cur_bytenr); + + cur_bytenr = new_range_end; +continue ; + } +/* +* |<-- entry -->| +* |<-- bytenr/len -->| +* +* Or +* +* |<-------- entry ------->| +* |<- bytenr/len ->| +* +* In this case, we just set the bitmap in the current entry, and +* advance @cur_bytenr to the end of the existing entry. +* By this, we either go check the range against the next entry, +* or we finish our current range. +*/ +if (compare_bytenr_to_range (cur_bytenr, entry_start, + entry_size) ==0 ) { +u64 range_end =min (entry_end, bytenr + len); + +set_bits_in_one_entry (ctrl, entry, cur_bytenr, + range_end - cur_bytenr); + cur_bytenr = range_end; +continue ; + } + +/* +* (A) +* |<-- entry -->| |<--- next -->| +* |<-- bytenr/len -->| +* +* OR +* +* (B) +* |<-- entry -->| |<--- next -->| +* |<-- bytenr/len -->| +* +* OR +* +* (C) +* |<-- entry -->|<--- next -->| +* |<-- bytenr/len -->| +*/ +if (i <wi_super_nr_entries (wis) -1 ) { +struct write_intent_entry *next = +write_intent_entry_nr (ctrl, i +1 ); +u64 next_start =wi_entry_bytenr (next); + + +/* Case (A) and (B), insert the new entries. */ +if (cur_bytenr >= entry_end && cur_bytenr < next_start) { +insert_new_entries (ctrl, i +1 , cur_bytenr, + bytenr + len - cur_bytenr); + cur_bytenr = next_start; +continue ; + } + +/* Case (C), just skip to next item */ +continue ; + } + +/* +* The remaining case is, @entry is already the last one. +* +* |<-- entry -->| +* |<-- bytenr/len -->| +* +* We're beyond the last entry. Need to insert new entries. +*/ +insert_new_entries (ctrl, i +1 , cur_bytenr, + bytenr + len - cur_bytenr); + + cur_bytenr = bytenr + len; + } +} +int btrfs_write_intent_init (struct btrfs_fs_info *fs_info) {struct btrfs_device *highest_dev =NULL ;
fs/btrfs/write-intent.h +16 -0
@@ -106,6 +106,15 @@ struct write_intent_entry {/* The number of bits we can have in one entry. */ #define WRITE_INTENT_BITS_PER_ENTRY (64) +static inline u32 bytes_to_entries (u64 start,u32 length,u32 blocksize) +{ +u32 entry_len = blocksize *WRITE_INTENT_BITS_PER_ENTRY ; +u64 entry_start =round_down (start, entry_len); +u64 entry_end =round_up (start + length, entry_len); + +return DIV_ROUND_UP ((u32)(entry_end - entry_start), entry_len); +} +/* In-memory write-intent control structure. */ struct write_intent_ctrl {/* For the write_intent super and entries. */
@@ -189,6 +198,13 @@ WRITE_INTENT_SETGET_FUNCS(super_csum_type, struct write_intent_super, csum_type, 16); WRITE_INTENT_SETGET_FUNCS(entry_bytenr, struct write_intent_entry, bytenr, 64); +static inline u32 write_intent_entry_size (struct write_intent_ctrl *ctrl) +{ +struct write_intent_super *wis =page_address (ctrl->page); + +return wi_super_blocksize (wis) *WRITE_INTENT_BITS_PER_ENTRY ; +} +static inline void wie_get_bitmap (struct write_intent_entry *entry,unsigned long *bitmap) {
Signature · 1 line
2.36.1
Re: [PATCH RFC 06/11] btrfs: write-intent: introduce an internal helper to set bits for a range.
Quoted 1 line · Qu Wenruo
Signed-off-by Qu Wenruo <[email protected]>
Unfortunately there seems to be corner case not handled properly.
If we have a start=37617664, len=196608, and we have an existing entry 0 , bitmap=0x0700000000000000.
Then after calling set_bits() with above range, we only got:
entry 0 bitmap=0xc700000000000000.
Note 0xc = 1100 binary, thus we didn't create a new entry for the remaining 1 bit, and triggered the later WARN_ON_ONCE() for clear_bits.
I'll fix the bug in the code and add a selftest case for it.
Thanks,
Qu
Quoted 301 lines
--- fs/btrfs/write-intent.c | 251 ++++++++++++++++++++++++++++++++++++++++ fs/btrfs/write-intent.h | 16 +++ 2 files changed, 267 insertions(+)fs/btrfs/write-intent.c +251
-0@@ -259,6 +259,257 @@ static int write_intent_init(struct btrfs_fs_info *fs_info)return 0 ; } +static struct write_intent_entry *write_intent_entry_nr ( +struct write_intent_ctrl *ctrl,int nr) +{ + +ASSERT (nr <WRITE_INTENT_INTERNAL_BITMAPS_MAX_ENTRIES ); +return (page_address (ctrl->page) + +sizeof (struct write_intent_super ) + + nr *sizeof (struct write_intent_entry )); +} + +/* +* Return <0 if the bytenr is before the given entry. +* Return 0 if the bytenr is inside the given entry. +* Return >0 if the bytenr is after the given entry. +*/ +static int compare_bytenr_to_range (u64 bytenr,u64 start,u32 len) +{ +if (bytenr < start) +return -1 ; +if (start <= bytenr && bytenr < start + len) +return 0 ; +return 1 ; +} + +/* +* Move all non-empty entries starting from @nr, to the right, and make room +* for @nr_new entries. +* Those new entries will be all zero filled. +* +* Caller should ensure we have enough room for @nr_new new entries. +*/ +static void move_entries_right (struct write_intent_ctrl *ctrl,int nr, +int nr_new) +{ +struct write_intent_super *wis =page_address (ctrl->page); +int move_size; + +ASSERT (nr_new >0 ); +ASSERT (wi_super_nr_entries (wis) + nr_new <= +WRITE_INTENT_INTERNAL_BITMAPS_MAX_ENTRIES ); + + move_size = (wi_super_nr_entries (wis) - nr) * +sizeof (struct write_intent_entry ); + +memmove (write_intent_entry_nr (ctrl, nr + nr_new), +write_intent_entry_nr (ctrl, nr), move_size); +memset (write_intent_entry_nr (ctrl, nr),0 , + nr_new *sizeof (struct write_intent_entry )); +wi_set_super_nr_entries (wis,wi_super_nr_entries (wis) + nr_new); +} + +static void set_bits_in_one_entry (struct write_intent_ctrl *ctrl, +struct write_intent_entry *entry, +u64 bytenr,u32 len) +{ +const u64 entry_start =wi_entry_bytenr (entry); +const u32 entry_len =write_intent_entry_size (ctrl); +unsigned long bitmaps[WRITE_INTENT_BITS_PER_ENTRY /BITS_PER_LONG ]; + +wie_get_bitmap (entry, bitmaps); + +ASSERT (entry_start <= bytenr && bytenr + len <= entry_start + entry_len); +bitmap_set (bitmaps, (bytenr - entry_start) / ctrl->blocksize, + len / ctrl->blocksize); +wie_set_bitmap (entry, bitmaps); +} + +/* +* Insert new entries for the range [@bytenr, @bytenr + @len) at slot @nr +* and fill the new entries with proper bytenr and bitmaps. +*/ +static void insert_new_entries (struct write_intent_ctrl *ctrl,int nr, +u64 bytenr,u32 len) +{ +const u32 entry_size =write_intent_entry_size (ctrl); +u64 entry_start; +u64 new_start =round_down (bytenr, entry_size); +u64 new_end; +int nr_new_entries; +u64 cur; + +if (nr >=wi_super_nr_entries (page_address (ctrl->page)) || + nr >=WRITE_INTENT_INTERNAL_BITMAPS_MAX_ENTRIES ) + entry_start =U64_MAX ; +else + entry_start =wi_entry_bytenr (write_intent_entry_nr (ctrl, nr)); + +ASSERT (bytenr < entry_start); + + new_end =min (entry_start,round_up (bytenr + len, entry_size)); + nr_new_entries = (new_end - new_start) / entry_size; + +if (nr_new_entries ==0 ) +return ; + +move_entries_right (ctrl, nr, nr_new_entries); + +for (cur = new_start; cur < new_end; cur += entry_size, nr++) { +struct write_intent_entry *entry = +write_intent_entry_nr (ctrl, nr); +u64 range_start =max (cur, bytenr); +u64 range_len =min (cur + entry_size, bytenr + len) - + range_start; + +/* Fill the bytenr into the new empty entries.*/ +wi_set_entry_bytenr (entry, cur); + +/* And set the bitmap. */ +set_bits_in_one_entry (ctrl, entry, range_start, range_len); + } +} + +/* +* This should be only called when we have enough room in the bitmaps, and hold +* the wi_ctrl->lock. +*/ +static void write_intent_set_bits (struct write_intent_ctrl *ctrl,u64 bytenr, +u32 len) +{ +struct write_intent_super *wis =page_address (ctrl->page); +const u32 entry_size =write_intent_entry_size (ctrl); +int i; +u64 nr_entries =wi_super_nr_entries (wis); +u64 cur_bytenr; + +/* +* Currently we only accept full stripe length, which should be +* aligned to 64KiB. +*/ +ASSERT (IS_ALIGNED (len,BTRFS_STRIPE_LEN )); + +/* +* We should have room to contain the worst case scenario, in which we +* need to create one or more new entry. +*/ +ASSERT (nr_entries +bytes_to_entries (bytenr, len,BTRFS_STRIPE_LEN ) <= +WRITE_INTENT_INTERNAL_BITMAPS_MAX_ENTRIES ); + +/* Empty bitmap, just insert new ones. */ +if (wi_super_nr_entries (wis) ==0 ) +return insert_new_entries (ctrl,0 , bytenr, len); + +/* Go through entries to find the one covering our range. */ +for (i =0 , cur_bytenr = bytenr; + i <wi_super_nr_entries (wis) && cur_bytenr < bytenr + len; i++) { +struct write_intent_entry *entry =write_intent_entry_nr (ctrl, i); +u64 entry_start =wi_entry_bytenr (entry); +u64 entry_end = entry_start + entry_size; + +/* +* |<-- entry -->| +* |<-- bytenr/len -->| +* +* Or +* +* |<-- entry -->| +* |<-- bytenr/len -->| +* +* Or +* +* |<-- entry -->| +* |<-- bytenr/len -->| +* +* We need to insert one or more new entries for the range not +* covered by the existing entry. +*/ +if (compare_bytenr_to_range (cur_bytenr, entry_start, + entry_size) <0 ) { +u64 new_range_end; + + new_range_end =min (entry_start, bytenr + len); +insert_new_entries (ctrl, i, cur_bytenr, + new_range_end - cur_bytenr); + + cur_bytenr = new_range_end; +continue ; + } +/* +* |<-- entry -->| +* |<-- bytenr/len -->| +* +* Or +* +* |<-------- entry ------->| +* |<- bytenr/len ->| +* +* In this case, we just set the bitmap in the current entry, and +* advance @cur_bytenr to the end of the existing entry. +* By this, we either go check the range against the next entry, +* or we finish our current range. +*/ +if (compare_bytenr_to_range (cur_bytenr, entry_start, + entry_size) ==0 ) { +u64 range_end =min (entry_end, bytenr + len); + +set_bits_in_one_entry (ctrl, entry, cur_bytenr, + range_end - cur_bytenr); + cur_bytenr = range_end; +continue ; + } + +/* +* (A) +* |<-- entry -->| |<--- next -->| +* |<-- bytenr/len -->| +* +* OR +* +* (B) +* |<-- entry -->| |<--- next -->| +* |<-- bytenr/len -->| +* +* OR +* +* (C) +* |<-- entry -->|<--- next -->| +* |<-- bytenr/len -->| +*/ +if (i <wi_super_nr_entries (wis) -1 ) { +struct write_intent_entry *next = +write_intent_entry_nr (ctrl, i +1 ); +u64 next_start =wi_entry_bytenr (next); + + +/* Case (A) and (B), insert the new entries. */ +if (cur_bytenr >= entry_end && cur_bytenr < next_start) { +insert_new_entries (ctrl, i +1 , cur_bytenr, + bytenr + len - cur_bytenr); + cur_bytenr = next_start; +continue ; + } + +/* Case (C), just skip to next item */ +continue ; + } + +/* +* The remaining case is, @entry is already the last one. +* +* |<-- entry -->| +* |<-- bytenr/len -->| +* +* We're beyond the last entry. Need to insert new entries. +*/ +insert_new_entries (ctrl, i +1 , cur_bytenr, + bytenr + len - cur_bytenr); + + cur_bytenr = bytenr + len; + } +} +int btrfs_write_intent_init (struct btrfs_fs_info *fs_info) {struct btrfs_device *highest_dev =NULL ;fs/btrfs/write-intent.h +16
-0@@ -106,6 +106,15 @@ struct write_intent_entry {/* The number of bits we can have in one entry. */ #define WRITE_INTENT_BITS_PER_ENTRY (64) +static inline u32 bytes_to_entries (u64 start,u32 length,u32 blocksize) +{ +u32 entry_len = blocksize *WRITE_INTENT_BITS_PER_ENTRY ; +u64 entry_start =round_down (start, entry_len); +u64 entry_end =round_up (start + length, entry_len); + +return DIV_ROUND_UP ((u32)(entry_end - entry_start), entry_len); +} +/* In-memory write-intent control structure. */ struct write_intent_ctrl {/* For the write_intent super and entries. */ @@ -189,6 +198,13 @@ WRITE_INTENT_SETGET_FUNCS(super_csum_type, struct write_intent_super, csum_type, 16); WRITE_INTENT_SETGET_FUNCS(entry_bytenr, struct write_intent_entry, bytenr, 64); +static inline u32 write_intent_entry_size (struct write_intent_ctrl *ctrl) +{ +struct write_intent_super *wis =page_address (ctrl->page); + +return wi_super_blocksize (wis) *WRITE_INTENT_BITS_PER_ENTRY ; +} +static inline void wie_get_bitmap (struct write_intent_entry *entry,unsigned long *bitmap) {
Re: [PATCH RFC 06/11] btrfs: write-intent: introduce an internal helper to set bits for a range.
Quoted 233 lines · Qu Wenruo
Quoted 1 line · Qu Wenruo
Signed-off-by Qu Wenruo <[email protected]>Unfortunately there seems to be corner case not handled properly.
If we have a start=37617664, len=196608, and we have an existing entry 0 , bitmap=0x0700000000000000.
Then after calling
set_bits()with above range, we only got:
entry 0 bitmap=0xc700000000000000.Note
0xc= 1100 binary, thus we didn't create a new entry for the remaining 1 bit, and triggered the laterWARN_ON_ONCE()forclear_bits.I'll fix the bug in the code and add a selftest case for it.
Thanks,
QuQuoted 214 lines
---
fs/btrfs/write-intent.c| 251 ++++++++++++++++++++++++++++++++++++++++fs/btrfs/write-intent.h| 16 +++2 files changed, 267 insertions(+)fs/btrfs/write-intent.c +26
-0@@ -259,6 +259,257 @@ static int write_intent_init(struct btrfs_fs_info *fs_info) return 0; } +static struct write_intent_entry *write_intent_entry_nr ( + struct write_intent_ctrl *ctrl, int nr) +{ + + ASSERT(nr < WRITE_INTENT_INTERNAL_BITMAPS_MAX_ENTRIES); + return (page_address(ctrl->page) + + sizeof(struct write_intent_super) + + nr * sizeof(struct write_intent_entry)); +} + +/* +* Return <0 if the bytenr is before the given entry. +* Return 0 if the bytenr is inside the given entry. +* Return >0 if the bytenr is after the given entry. +*/ +static int compare_bytenr_to_range (u64 bytenr,u64 start,u32 len) +{ + if (bytenr < start) + return -1; + if (start <= bytenr && bytenr < start + len) + return 0; + return 1; +} + +/* +* Move all non-empty entries starting from @nr, to the right, and make room
+ * for @nr_new entries.
+ * Those new entries will be all zero filled.
+ *
+ * Caller should ensure we have enough room for @nr_new new entries.
+ */fs/btrfs/write-intent.c +31
-0+static void move_entries_right (struct write_intent_ctrl *ctrl,int nr, + int nr_new) +{ + struct write_intent_super *wis = page_address(ctrl->page); + int move_size; + + ASSERT(nr_new > 0); + ASSERT(wi_super_nr_entries(wis) + nr_new <= + WRITE_INTENT_INTERNAL_BITMAPS_MAX_ENTRIES); + + move_size = (wi_super_nr_entries(wis) - nr) * + sizeof(struct write_intent_entry); + + memmove(write_intent_entry_nr(ctrl, nr + nr_new), + write_intent_entry_nr(ctrl, nr), move_size); + memset(write_intent_entry_nr(ctrl, nr), 0, + nr_new * sizeof(struct write_intent_entry)); + wi_set_super_nr_entries(wis, wi_super_nr_entries(wis) + nr_new); +} + +static void set_bits_in_one_entry(struct write_intent_ctrl *ctrl, + struct write_intent_entry *entry, + u64 bytenr, u32 len) +{ + const u64 entry_start = wi_entry_bytenr(entry); + const u32 entry_len = write_intent_entry_size(ctrl); + unsigned long bitmaps[WRITE_INTENT_BITS_PER_ENTRY / BITS_PER_LONG]; + + wie_get_bitmap(entry, bitmaps); + + ASSERT(entry_start <= bytenr && bytenr + len <= entry_start +
entry_len);
+bitmap_set(bitmaps, (bytenr - entry_start) / ctrl->blocksize, + len / ctrl->blocksize);
+wie_set_bitmap(entry, bitmaps);fs/btrfs/write-intent.c +4
-0+} + +/* +* Insert new entries for the range [@bytenr, @bytenr + @len) at slot @nr
+ * and fill the new entries with proper bytenr and bitmaps.
+ */fs/btrfs/write-intent.c +43
-0+static void insert_new_entries (struct write_intent_ctrl *ctrl,int nr, + u64 bytenr, u32 len) +{ + const u32 entry_size = write_intent_entry_size(ctrl); + u64 entry_start; + u64 new_start = round_down(bytenr, entry_size); + u64 new_end; + int nr_new_entries; + u64 cur; + + if (nr >= wi_super_nr_entries(page_address(ctrl->page)) || + nr >= WRITE_INTENT_INTERNAL_BITMAPS_MAX_ENTRIES) + entry_start = U64_MAX; + else + entry_start = wi_entry_bytenr(write_intent_entry_nr(ctrl, nr)); + + ASSERT(bytenr < entry_start); + + new_end = min(entry_start, round_up(bytenr + len, entry_size)); + nr_new_entries = (new_end - new_start) / entry_size; + + if (nr_new_entries == 0) + return; + + move_entries_right(ctrl, nr, nr_new_entries); + + for (cur = new_start; cur < new_end; cur += entry_size, nr++) { + struct write_intent_entry *entry = + write_intent_entry_nr(ctrl, nr); + u64 range_start = max(cur, bytenr); + u64 range_len = min(cur + entry_size, bytenr + len) - + range_start; + + /* Fill the bytenr into the new empty entries.*/ + wi_set_entry_bytenr(entry, cur); + + /* And set the bitmap. */ + set_bits_in_one_entry(ctrl, entry, range_start, range_len); + } +} + +/* +* This should be only called when we have enough room in the bitmaps, and hold
+ * thewi_ctrl->lock.
+ */
+static void write_intent_set_bits(structwrite_intent_ctrl*ctrl, u64 bytenr,
+ u32 len)fs/btrfs/write-intent.c +15
-0+{ + struct write_intent_super *wis = page_address(ctrl->page); + const u32 entry_size = write_intent_entry_size(ctrl); + int i; + u64 nr_entries = wi_super_nr_entries(wis); + u64 cur_bytenr; + + /* + * Currently we only accept full stripe length, which should be + * aligned to 64KiB. + */ + ASSERT(IS_ALIGNED(len, BTRFS_STRIPE_LEN)); + + /* +* We should have room to contain the worst case scenario, in which we
+ * need to create one or more new entry. + */
+ ASSERT(nr_entries +bytes_to_entries(bytenr, len, BTRFS_STRIPE_LEN)<=
+WRITE_INTENT_INTERNAL_BITMAPS_MAX_ENTRIES);fs/btrfs/write-intent.c +8
-0+ + /* Empty bitmap, just insert new ones. */ + if (wi_super_nr_entries(wis) == 0) + return insert_new_entries(ctrl, 0, bytenr, len); + + /* Go through entries to find the one covering our range. */ + for (i = 0, cur_bytenr = bytenr; + i < wi_super_nr_entries(wis) && cur_bytenr < bytenr + len;i++) {
+ structwrite_intent_entry*entry =write_intent_entry_nr(ctrl, i);
+ u64entry_start=wi_entry_bytenr(entry); + u64entry_end=entry_start+entry_size;fs/btrfs/write-intent.c +39
-0+ + /* + * |<-- entry -->| + * |<-- bytenr/len -->| + * + * Or + * + * |<-- entry -->| + * |<-- bytenr/len -->| + * + * Or + * + * |<-- entry -->| + * |<-- bytenr/len -->| + * + * We need to insert one or more new entries for the range not + * covered by the existing entry. + */ + if (compare_bytenr_to_range(cur_bytenr, entry_start, + entry_size) < 0) { + u64 new_range_end; + + new_range_end = min(entry_start, bytenr + len); + insert_new_entries(ctrl, i, cur_bytenr, + new_range_end - cur_bytenr); + + cur_bytenr = new_range_end; + continue; + } + /* +* |<-- entry -->| +* |<-- bytenr/len -->| +* +* Or +* +* |<-------- entry ------->| +* |<- bytenr/len ->| +* +* In this case, we just set the bitmap in the current entry, and
+ * advance @cur_bytenr to the end of the existing entry. + * By this, we either go check the range against the next entry, + * or we finish our current range.
+ */
Quoted 8 lines
Quoted 8 lines
+ if (
compare_bytenr_to_range(cur_bytenr, entry_start, + entry_size)== 0) {
+ u64range_end=min(entry_end, bytenr + len); +
+ set_bits_in_one_entry(ctrl, entry,cur_bytenr, +range_end-cur_bytenr); +cur_bytenr=range_end;
+ continue;
This is where the problem happens.
If this entry is also the last entry, and we still have something left, then we're screwed as we will no longer insert new entries for it, leaving some bits not properly set.
Will rework the big for loop to make the last entry insert consistent.
Thanks,
Qu
Quoted 89 lines
Quoted 89 lines
+ }
fs/btrfs/write-intent.c +51
-0+ + /* + * (A) + * |<-- entry -->| |<--- next -->| + * |<-- bytenr/len -->| + * + * OR + * + * (B) + * |<-- entry -->| |<--- next -->| + * |<-- bytenr/len -->| + * + * OR + * + * (C) + * |<-- entry -->|<--- next -->| + * |<-- bytenr/len -->| + */ + if (i < wi_super_nr_entries(wis) - 1) { + struct write_intent_entry *next = + write_intent_entry_nr(ctrl, i + 1); + u64 next_start = wi_entry_bytenr(next); + + + /* Case (A) and (B), insert the new entries. */ + if (cur_bytenr >= entry_end && cur_bytenr < next_start) { + insert_new_entries(ctrl, i + 1, cur_bytenr, + bytenr + len - cur_bytenr); + cur_bytenr = next_start; + continue; + } + + /* Case (C), just skip to next item */ + continue; + } + + /* + * The remaining case is, @entry is already the last one. + * + * |<-- entry -->| + * |<-- bytenr/len -->| + * + * We're beyond the last entry. Need to insert new entries. + */ + insert_new_entries(ctrl, i + 1, cur_bytenr, + bytenr + len - cur_bytenr); + + cur_bytenr = bytenr + len; + } +} +int
btrfs_write_intent_init(struct btrfs_fs_info *fs_info){
structbtrfs_device*highest_dev= NULL;fs/btrfs/write-intent.h +16
-0@@ -106,6 +106,15 @@ struct write_intent_entry { /* The number of bits we can have in one entry. */ #define WRITE_INTENT_BITS_PER_ENTRY (64) +static inline u32 bytes_to_entries (u64 start,u32 length,u32 blocksize) +{ + u32 entry_len = blocksize * WRITE_INTENT_BITS_PER_ENTRY; + u64 entry_start = round_down(start, entry_len); + u64 entry_end = round_up(start + length, entry_len); + + return DIV_ROUND_UP((u32)(entry_end - entry_start), entry_len); +} + /* In-memory write-intent control structure. */ struct write_intent_ctrl { /* For the write_intent super and entries. */@@ -189,6 +198,13 @@ WRITE_INTENT_SETGET_FUNCS(super_csum_type, struct write_intent_super, csum_type, 16); WRITE_INTENT_SETGET_FUNCS(entry_bytenr, struct write_intent_entry, bytenr, 64); +static inline u32 write_intent_entry_size (struct write_intent_ctrl *ctrl) +{ + struct write_intent_super *wis = page_address(ctrl->page); + + return wi_super_blocksize(wis) * WRITE_INTENT_BITS_PER_ENTRY; +} + static inline void wie_get_bitmap(struct write_intent_entry *entry, unsigned long *bitmap){
RFC PATCH 7/11 btrfs: write-intent: introduce an internal helper to clear bits for a range.
In reply to Qu WenruoThis new helper. write_intent_clear_bits(), is much simpler than the set bits counter part.
As if we can not find a entry for our target range, then it must be something wrong, and we only need to warn and skip to next entry.
Although there has one extra thing to do, if we have emptied one entry, we have to delete that empty entry.
--- fs/btrfs/write-intent.c | 175 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 175 insertions(+)
fs/btrfs/write-intent.c +175 -0
@@ -326,6 +326,36 @@ static void set_bits_in_one_entry(struct write_intent_ctrl *ctrl,wie_set_bitmap (entry, bitmaps); } +static bool is_entry_empty (struct write_intent_ctrl *ctrl, +struct write_intent_entry *entry) +{ +unsigned long bitmaps[2 ]; + +wie_get_bitmap (entry, bitmaps); + +return bitmap_empty (bitmaps,WRITE_INTENT_BITS_PER_ENTRY ); +} + +/* +* NOTE: This function may clear all bits of an entry, caller must check if +* that's the case, and delete the empty entry if needed. +*/ +static void clear_bits_in_one_entry (struct write_intent_ctrl *ctrl, +struct write_intent_entry *entry, +u64 bytenr,u32 len) +{ +const u64 entry_start =wi_entry_bytenr (entry); +const u32 entry_len =write_intent_entry_size (ctrl); +unsigned long bitmaps[WRITE_INTENT_BITS_PER_ENTRY /BITS_PER_LONG ]; + +wie_get_bitmap (entry, bitmaps); + +ASSERT (entry_start <= bytenr && bytenr + len <= entry_start + entry_len); +bitmap_clear (bitmaps, (bytenr - entry_start) / ctrl->blocksize, + len / ctrl->blocksize); +wie_set_bitmap (entry, bitmaps); +} +/* * Insert new entries for the range [@bytenr, @bytenr + @len) at slot @nr * and fill the new entries with proper bytenr and bitmaps.
@@ -371,6 +401,25 @@ static void insert_new_entries(struct write_intent_ctrl *ctrl, int nr, } } +static void delete_one_entry (struct write_intent_ctrl *ctrl,int nr) +{ +struct write_intent_super *wis =page_address (ctrl->page); +int cur_nr_entries =wi_super_nr_entries (wis); + +ASSERT (is_entry_empty (ctrl,write_intent_entry_nr (ctrl, nr))); +ASSERT (nr < cur_nr_entries); + +/* Move all the entries after slot @nr by one slot. */ +memmove (write_intent_entry_nr (ctrl, nr), +write_intent_entry_nr (ctrl, nr +1 ), + (cur_nr_entries - nr -1 ) *sizeof (struct write_intent_entry )); + +/* Memzero the right most entry. */ +memset (write_intent_entry_nr (ctrl, cur_nr_entries -1 ),0 , +sizeof (struct write_intent_entry )); +wi_set_super_nr_entries (wis, cur_nr_entries -1 ); +} +/* * This should be only called when we have enough room in the bitmaps, and hold * the wi_ctrl->lock.
@@ -510,6 +559,132 @@ static void write_intent_set_bits(struct write_intent_ctrl *ctrl, u64 bytenr, } } +/* This should be only called with wi_ctrl->lock hold. */ +static void write_intent_clear_bits (struct write_intent_ctrl *ctrl,u64 bytenr, +u32 len) +{ +struct write_intent_super *wis =page_address (ctrl->page); +const u32 entry_size =write_intent_entry_size (ctrl); +int i; +u64 cur_bytenr; + +/* +* Currently we only accept full stripe length, which should be +* aligned to 64KiB. +*/ +ASSERT (IS_ALIGNED (len,BTRFS_STRIPE_LEN )); + +for (i =0 , cur_bytenr = bytenr; + i <wi_super_nr_entries (wis) && cur_bytenr < bytenr + len; i++) { +struct write_intent_entry *entry =write_intent_entry_nr (ctrl, i); +u64 entry_start =wi_entry_bytenr (entry); +u64 entry_end = entry_start + entry_size; + +/* +* |<-- entry -->| +* |<-- bytenr/len -->| +* +* Or +* +* |<-- entry -->| +* |<-- bytenr/len -->| +* +* Or +* +* |<-- entry -->| +* |<-- bytenr/len -->| +* +* This case should not happen, it means we have some logged +* dirty range, but it's no longer there. +* Just warn and skip to the next covered range. +*/ +if (compare_bytenr_to_range (cur_bytenr, entry_start, entry_size) <0 ) { +WARN_ON_ONCE (1 ); + cur_bytenr =max (bytenr + len, entry_start); +continue ; + } + +/* +* |<-- entry -->| +* |<-- bytenr/len -->| +* +* Or +* +* |<-------- entry ------->| +* |<- bytenr/len ->| +* +* In this case, we just clear the bitmap in current entry, and +* advance @cur_bytenr. +* By this, we either go check the range against the next entry, +* or we finish our current range. +*/ +if (compare_bytenr_to_range (cur_bytenr, entry_start, entry_size) ==0 ) { +u64 range_end =min (entry_end, bytenr + len); + +clear_bits_in_one_entry (ctrl, entry, cur_bytenr, + range_end - cur_bytenr); + cur_bytenr = range_end; +/* +* If the current entry is empty, we need to delete the +* entry and decrease @nr. +*/ +if (is_entry_empty (ctrl, entry)) { +delete_one_entry (ctrl, i); + i--; + } +continue ; + } + +/* +* (A) +* |<-- entry -->| |<--- next -->| +* |<-- bytenr/len -->| +* +* OR +* +* (B) +* |<-- entry -->| |<--- next -->| +* |<-- bytenr/len -->| +* +* OR +* +* (C) +* |<-- entry -->|<--- next -->| +* |<-- bytenr/len -->| +*/ +if (i <wi_super_nr_entries (wis) -1 ) { +struct write_intent_entry *next = +write_intent_entry_nr (ctrl, i +1 ); +u64 next_start =wi_entry_bytenr (next); + +/* Case (A) and (B), insert the new entries. */ +if (cur_bytenr >= entry_end && cur_bytenr < next_start) { +/* +* This should not happen, as we should always +* have dirty bits in range. +* Just warn and skip to next entry. +*/ + cur_bytenr = next_start; +WARN_ON_ONCE (1 ); +continue ; + } + +/* Case (C), just skip to next item */ +continue ; + } + +/* +* The remaining case is, @entry is already the last one. +* +* |<-- entry -->| +* |<-- bytenr/len -->| +* +* Another impossible case, just warn and continue. +*/ +WARN_ON_ONCE (1 ); + } +} +int btrfs_write_intent_init (struct btrfs_fs_info *fs_info) {struct btrfs_device *highest_dev =NULL ;
Signature · 1 line
2.36.1
RFC PATCH 8/11 btrfs: write back write intent bitmap after barrier_all_devices()
In reply to Qu WenruoThe new function, btrfs_write_intent_writeback(), will also accept a u64 parameter, @event, to do extra fastpath check to avoid unnecessary writeback.
For now we just pass 0 to write the current bitmap to disk.
--- fs/btrfs/disk-io.c | 9 +++++ fs/btrfs/write-intent.c | 81 +++++++++++++++++++++++++++++++++++++++-- fs/btrfs/write-intent.h | 25 +++++++++++++ 3 files changed, 112 insertions(+), 3 deletions(-)
fs/btrfs/disk-io.c +9 -0
@@ -4357,6 +4357,15 @@ int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors) } } + ret =btrfs_write_intent_writeback (fs_info,0 ); +if (ret <0 ) { +mutex_unlock ( + &fs_info->fs_devices->device_list_mutex); +btrfs_handle_fs_error (fs_info, ret, +"errors while writing back write intent bitmaps." ); +return ret; + } +list_for_each_entry (dev, head, dev_list) {if (!dev->bdev) { total_errors++;
fs/btrfs/write-intent.c +78 -3
118 lines not on this page · raw message
fs/btrfs/write-intent.h +25 -0
@@ -123,12 +123,30 @@ struct write_intent_ctrl {/* A copy for writeback. */ struct page *commit_page; +/* +* For callers who has updated their bitmap, wait for the bitmap to be +* flushed to disk. +*/ +wait_queue_head_t write_wait; + +/* +* For callers whose bits can not be updated as no enough space left +* int the bitmap. +*/ +wait_queue_head_t overflow_wait; +/* Cached event counter.*/ atomic64_t event;/* Lock for reading/writing above @page. */ spinlock_t lock; +/* Event number for the bitmap being written. */ +u64 writing_event; + +/* Event number for the last committed bitmap. */ +u64 committed_event; +/* Cached blocksize from write intent super. */ u32 blocksize; };
@@ -233,4 +251,11 @@ static inline void wie_set_bitmap(struct write_intent_entry *entry,int btrfs_write_intent_init (struct btrfs_fs_info *fs_info);void btrfs_write_intent_free (struct btrfs_fs_info *fs_info); +/* +* Ensure the bitmap for @event is already written to disk. +* +* If @event is 0, it means to write current bitmap to disk. +*/ +int btrfs_write_intent_writeback (struct btrfs_fs_info *fs_info,u64 event); +#endif
Signature · 1 line
2.36.1
RFC PATCH 9/11 btrfs: update and writeback the write-intent bitmap for RAID56 write.
In reply to Qu WenruoThis allows us to mark the write-intent bitmaps dirty for later recovery usage.
For now, we only mark the bitmaps dirty but without really clearing them, this is going to cause problems (hang the fs if the bitmap is full), but this also allows us to debug the bitmap with the new dump-write-intent tool:
csum_type 0 (crc32c)
csum 0xad622029 [match]
magic _wIbSb_Q [match]
fsid 46bcd711-6c9b-400f-aaba-bf99aa3dd321
flags 0x7
( TARGET_RAID56 |
INTERNAL | BYTENR_LOGICAL )
events 10
size 4096
blocksize 65536
nr_entries 1
entry 0, bytenr 385875968, bitmap 0x000000000003fffc
This is doing 1MiB write for logical 388104192, which matches the above output bitmap.
--- fs/btrfs/raid56.c | 8 +++++++ fs/btrfs/write-intent.c | 46 +++++++++++++++++++++++++++++++++++++++++ fs/btrfs/write-intent.h | 9 ++++++++ 3 files changed, 63 insertions(+)
fs/btrfs/raid56.c +8 -0
@@ -1185,6 +1185,7 @@ static noinline void finish_rmw(struct btrfs_raid_bio *rbio)bool has_qstripe;struct bio_list bio_list;struct bio *bio; +u64 event;int ret;bio_list_init (&bio_list);
@@ -1338,6 +1339,13 @@ static noinline void finish_rmw(struct btrfs_raid_bio *rbio)atomic_set (&rbio->stripes_pending,bio_list_size (&bio_list));BUG_ON (atomic_read (&rbio->stripes_pending) ==0 ); +/* Update the write intent bitmap before we start submitting bios. */ +btrfs_write_intent_mark_dirty (bioc->fs_info, rbio->bioc->raid_map[0 ], + rbio->nr_data *BTRFS_STRIPE_LEN , &event); + ret =btrfs_write_intent_writeback (bioc->fs_info, event); + +if (ret <0 ) + goto cleanup;while ((bio =bio_list_pop (&bio_list))) { bio->bi_end_io = raid_write_end_io;
fs/btrfs/write-intent.c +46 -0
58 lines not on this page · raw message
fs/btrfs/write-intent.h +9 -0
@@ -258,4 +258,13 @@ void btrfs_write_intent_free(struct btrfs_fs_info *fs_info);*/ int btrfs_write_intent_writeback (struct btrfs_fs_info *fs_info,u64 event); +/* +* Mark the range dirty in write intent bitmaps. +* +* May (but unlikely) sleep if there is not enough free entries. +* In that case, we will wait for enough free entries to be released. +*/ +void btrfs_write_intent_mark_dirty (struct btrfs_fs_info *fs_info,u64 logical, +u32 len,u64 *event_ret); +#endif
Signature · 1 line
2.36.1
RFC PATCH 10/11 btrfs: raid56: clear write-intent bimaps when a full stripe finishes.
In reply to Qu Wenruo--- fs/btrfs/raid56.c | 7 +++++++ fs/btrfs/write-intent.c | 45 +++++++++++++++++++++++++++++------------ fs/btrfs/write-intent.h | 8 ++++++++ 3 files changed, 47 insertions(+), 13 deletions(-)
fs/btrfs/raid56.c +7 -0
@@ -817,6 +817,13 @@ static void rbio_orig_end_io(struct btrfs_raid_bio *rbio, blk_status_t err)if (rbio->generic_bio_cnt)btrfs_bio_counter_sub (rbio->bioc->fs_info, rbio->generic_bio_cnt); + +/* Clear the write-intent bitmap range for write operation. */ +if (rbio->operation ==BTRFS_RBIO_WRITE ) +btrfs_write_intent_clear_dirty (rbio->bioc->fs_info, + rbio->bioc->raid_map[0 ], + rbio->nr_data *BTRFS_STRIPE_LEN ); +/* * Clear the data bitmap, as the rbio may be cached for later usage. * do this before before unlock_stripe() so there will be no new bio
fs/btrfs/write-intent.c +32 -13
136 lines not on this page · raw message
fs/btrfs/write-intent.h +8 -0
@@ -267,4 +267,12 @@ int btrfs_write_intent_writeback(struct btrfs_fs_info *fs_info, u64 event);void btrfs_write_intent_mark_dirty (struct btrfs_fs_info *fs_info,u64 logical,u32 len,u64 *event_ret); +/* +* Clear the range dirty in write intent bitmaps. +* +* This function should not sleep, and no need to wait for the bitmap to be +* flushed. +*/ +void btrfs_write_intent_clear_dirty (struct btrfs_fs_info *fs_info,u64 logical, +u32 len);#endif
Signature · 1 line
2.36.1
RFC PATCH 11/11 btrfs: warn and clear bitmaps if there is dirty bitmap at mount time
In reply to Qu WenruoTo properly cleanup the bitmaps, we need to scrub the logical ranges in the bitmaps.
Unfortunately there is no such convient interface at all (scrub only works at device offset for now).
So just introduce a place holder to warn and clear the bitmap.
--- fs/btrfs/disk-io.c | 7 +++++++ fs/btrfs/write-intent.c | 35 +++++++++++++++++++++++++++++++++++ fs/btrfs/write-intent.h | 10 ++++++++++ 3 files changed, 52 insertions(+)
fs/btrfs/disk-io.c +7 -0
@@ -3705,6 +3705,13 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device ret); goto fail_block_groups; } + + ret =btrfs_write_intent_recover (fs_info); +if (ret <0 ) { +btrfs_err (fs_info,"failed to recover write-intent bitmap: %d" , + ret); + goto fail_block_groups; + } ret =btrfs_recover_balance (fs_info);if (ret) {btrfs_err (fs_info,"failed to recover balance: %d" , ret);
fs/btrfs/write-intent.c +35 -0
@@ -763,6 +763,41 @@ void btrfs_write_intent_clear_dirty(struct btrfs_fs_info *fs_info, u64 logical,spin_unlock_irqrestore (&ctrl->lock, flags); } +int btrfs_write_intent_recover (struct btrfs_fs_info *fs_info) +{ +struct write_intent_ctrl *ctrl = fs_info->wi_ctrl; +struct write_intent_super *wis; +int ret =0 ; + +if (!btrfs_fs_compat_ro (fs_info,WRITE_INTENT_BITMAP )) +return ret; + +ASSERT (ctrl); + wis =page_address (ctrl->page); + +if (wi_super_nr_entries (wis) !=0 ) { +int i; + +btrfs_warn (fs_info,"dirty write intent bitmap found:" ); +for (i =0 ; i <wi_super_nr_entries (wis); i++) { +struct write_intent_entry *entry = +write_intent_entry_nr (ctrl, i); + +btrfs_warn (fs_info, +" entry=%u bytenr=%llu bitmap=0x%016llx\n" , i, +wi_entry_bytenr (entry), +wi_entry_raw_bitmap (entry)); + } +/* For now, we just clear the whole bitmap. */ +memzero_page (ctrl->page,sizeof (struct write_intent_super ), +WRITE_INTENT_BITMAPS_SIZE - +sizeof (struct write_intent_super )); +wi_set_super_nr_entries (wis,0 ); + ret =write_intent_writeback (fs_info); + } +return ret; +} +int btrfs_write_intent_writeback (struct btrfs_fs_info *fs_info,u64 event) {struct write_intent_ctrl *ctrl = fs_info->wi_ctrl;
fs/btrfs/write-intent.h +10 -0
@@ -215,6 +215,8 @@ WRITE_INTENT_SETGET_FUNCS(super_blocksize, struct write_intent_super, WRITE_INTENT_SETGET_FUNCS(super_csum_type, struct write_intent_super, csum_type,16 ); WRITE_INTENT_SETGET_FUNCS(entry_bytenr, struct write_intent_entry, bytenr, 64); +WRITE_INTENT_SETGET_FUNCS(entry_raw_bitmap, struct write_intent_entry, + bitmap,64 );static inline u32 write_intent_entry_size (struct write_intent_ctrl *ctrl) {
@@ -275,4 +277,12 @@ void btrfs_write_intent_mark_dirty(struct btrfs_fs_info *fs_info, u64 logical,*/ void btrfs_write_intent_clear_dirty (struct btrfs_fs_info *fs_info,u64 logical,u32 len); + +/* +* Rebuild the range in the write-intent bitmaps. +* +* Currently not working, it will just output a warning and clear the bitmap. +*/ +int btrfs_write_intent_recover (struct btrfs_fs_info *fs_info); +#endif
Signature · 1 line
2.36.1
Hi,
Quoted 46 lines
[BACKGROUND]
Unlike md-raid, btrfs RAID56 has nothing to sync its devices when power loss happens.For pure mirror based profiles it's fine as btrfs can utilize its csums to find the correct mirror the repair the bad ones.
But for RAID56, the repair itself needs the data from other devices, thus any out-of-sync data can degrade the tolerance.
Even worse, incorrect RMW can use the stale data to generate P/Q, removing the possibility of recovery the data.
For md-raid, it goes with write-intent bitmap, to do faster resilver, and goes journal (partial parity log for RAID5) to ensure it can even stand a powerloss + device lose.
[OBJECTIVE]
This patchset will introduce a btrfs specific write-intent bitmap.
The bitmap will locate at physical offset 1MiB of each device, and the content is the same between all devices.
When there is a RAID56 write (currently all RAID56 write, including full stripe write), before submitting all the real bios to disks, write-intent bitmap will be updated and flushed to all writeable devices.
So even if a powerloss happened, at the next mount time we know which full stripes needs to check, and can start a scrub for those involved logical bytenr ranges.
[NO RECOVERY CODE YET]
Unfortunately, this patchset only implements the write-intent bitmap code, the recovery part is still a place holder, as we need some scrub refactor to make it only scrub a logical bytenr range.
[ADVANTAGE OF BTRFS SPECIFIC WRITE-INTENT BITMAPS]
Since btrfs can utilize csum for its metadata and CoWed data, unlike dm-bitmap which can only be used for faster re-silver, we can fully rebuild the full stripe, as long as:
Quoted 5 lines
1) There is no missing device
For missing device case, we still need to go full journal.2) Untouched data stays untouched
This should be mostly sane for sane hardware.
Is there a case that write-intent bitmap log is rotated?
For hardware raid of broadcom/LSI, once a disk is unpluged, the whole disk will be rebuild after this disk is plugged again.
Best Regards
Wang Yugui ([email protected])
2022/07/07
Quoted 109 lines
And since the btrfs specific write-intent bitmaps are pretty small (4KiB in size), the overhead much lower than full journal.
In the future, we may allow users to choose between just bitmaps or full journal to meet their requirement.
[BITMAPS DESIGN]
The bitmaps on-disk format looks like this:
[ super ][ entry 1 ][ entry 2 ] ... [entry N]
|<---------super::size(4K) ------------->|Super block contains how many entires are in use.
Each entry is 128 bits (16 bytes) in size, containing one u64 for bytenr, and u64 for one bitmap.
And all utilized entries will be sorted in their bytenr order, and no bit can overlap.
The blocksize is now fixed to
BTRFS_STRIPE_LEN(64KiB), so each entry can contain at most 4MiB, and the whole bitmaps can contain 224 entries.For the worst case, it can contain 14MiB dirty ranges. (1 bits set per bitmap, also means 2 disks RAID5 or 3 disks RAID6).
For the best case, it can contain 896MiB dirty ranges. (all bits set per bitmap)
[WHY NOT BTRFS BTREE]
Current write-intent structure needs two features:
- Its data needs to survive cross stripe boundary Normally this means write-intent btree needs to acts like a proper tree root, has
METADATA_ITEMsfor all its tree blocks.- Its data update must be outside of a transaction Currently only log tree can do such thing.
But unfortunately log tree can not survive across transaction boundary.Thus write-intent btree can only meet one of the requirement, not a suitable solution here.
[TESTING AND BENCHMARK]
For performance benchmark, unfortunately I don't have 3 HDDs to test. Will do the benchmark after secured enough hardware.
For testing, it can survive volume/raid/dev-replace test groups, and no write-intent bitmap leakage.
Unfortunately there is still a warning triggered in btrfs/070, still under investigation, hopefully to be a false alert in bitmap clearing path.
[TODO]
- Scrub refactor to allow us to do proper recovery at mount time Need to change scrub interface to scrub based on logical bytenr.This can be a super big work, thus currently we will focus only on RAID56 new scrub interface for write-intent recovery only.
- Extra optimizations
* Skip full stripe writes
* Enlarge the window betweenbtrfs_write_intent_mark_dirty()andbtrfs_write_intent_writeback()- Bug hunts and more fstests runs
- Proper performance benchmark
Needs hardware/baremetal VMs, since I don't have any physical machine large enough to contian 3 3.5" HDDs.Qu Wenruo (11):
btrfs: introduce new compat RO flag,EXTRA_SUPER_RESERVED
btrfs: introduce a new experimental compat RO flag,
WRITE_INTENT_BITMAP
btrfs: introduce the on-disk format of btrfs write intent bitmaps
btrfs: load/create write-intent bitmaps at mount time
btrfs: write-intent: write the newly created bitmaps to all disks
btrfs: write-intent: introduce an internal helper to set bits for a
range.
btrfs: write-intent: introduce an internal helper to clear bits for a
range.
btrfs: write back write intent bitmap afterbarrier_all_devices()
btrfs: update and writeback the write-intent bitmap for RAID56 write.
btrfs: raid56: clear write-intent bimaps when a full stripe finishes.
btrfs: warn and clear bitmaps if there is dirty bitmap at mount timefs/btrfs/Makefile | 2 +- fs/btrfs/ctree.h | 24 +- fs/btrfs/disk-io.c | 54 +++ fs/btrfs/raid56.c | 16 + fs/btrfs/sysfs.c | 2 + fs/btrfs/volumes.c | 34 +- fs/btrfs/write-intent.c | 962 +++++++++++++++++++++++++++++++++++++ fs/btrfs/write-intent.h | 288 +++++++++++ fs/btrfs/zoned.c | 8 + include/uapi/linux/btrfs.h | 17 + 10 files changed, 1399 insertions(+), 8 deletions(-) create mode 100644 fs/btrfs/write-intent.c create mode 100644 fs/btrfs/write-intent.hSignature · 1 line
2.36.1
Quoted 51 lines · Wang Yugui
Hi,
Quoted 49 lines
[BACKGROUND]
Unlike md-raid, btrfs RAID56 has nothing to sync its devices when power loss happens.For pure mirror based profiles it's fine as btrfs can utilize its csums to find the correct mirror the repair the bad ones.
But for RAID56, the repair itself needs the data from other devices, thus any out-of-sync data can degrade the tolerance.
Even worse, incorrect RMW can use the stale data to generate P/Q, removing the possibility of recovery the data.
For md-raid, it goes with write-intent bitmap, to do faster resilver, and goes journal (partial parity log for RAID5) to ensure it can even stand a powerloss + device lose.
[OBJECTIVE]
This patchset will introduce a btrfs specific write-intent bitmap.
The bitmap will locate at physical offset 1MiB of each device, and the content is the same between all devices.
When there is a RAID56 write (currently all RAID56 write, including full stripe write), before submitting all the real bios to disks, write-intent bitmap will be updated and flushed to all writeable devices.
So even if a powerloss happened, at the next mount time we know which full stripes needs to check, and can start a scrub for those involved logical bytenr ranges.
[NO RECOVERY CODE YET]
Unfortunately, this patchset only implements the write-intent bitmap code, the recovery part is still a place holder, as we need some scrub refactor to make it only scrub a logical bytenr range.
[ADVANTAGE OF BTRFS SPECIFIC WRITE-INTENT BITMAPS]
Since btrfs can utilize csum for its metadata and CoWed data, unlike dm-bitmap which can only be used for faster re-silver, we can fully rebuild the full stripe, as long as:
1) There is no missing device
For missing device case, we still need to go full journal.
Quoted 7 lines
Quoted 2 lines
2) Untouched data stays untouched
This should be mostly sane for sane hardware.Is there a case that write-intent bitmap log is rotated?
For hardware raid of broadcom/LSI, once a disk is unpluged, the whole disk will be rebuild after this disk is plugged again.
There is a plan in the future, adding a new flag, WRITE_INTENT_TARGET_DEGRADED, to record such new writes after a degraded rw operation.
But that would be something with lower priority, like after full journal.
Thanks,
Qu
Quoted 117 lines
Best Regards
Wang Yugui ([email protected])
2022/07/07Quoted 109 lines
And since the btrfs specific write-intent bitmaps are pretty small (4KiB in size), the overhead much lower than full journal.
In the future, we may allow users to choose between just bitmaps or full journal to meet their requirement.
[BITMAPS DESIGN]
The bitmaps on-disk format looks like this:
[ super ][ entry 1 ][ entry 2 ] ... [entry N]
|<---------super::size(4K) ------------->|Super block contains how many entires are in use.
Each entry is 128 bits (16 bytes) in size, containing one u64 for bytenr, and u64 for one bitmap.
And all utilized entries will be sorted in their bytenr order, and no bit can overlap.
The blocksize is now fixed to
BTRFS_STRIPE_LEN(64KiB), so each entry can contain at most 4MiB, and the whole bitmaps can contain 224 entries.For the worst case, it can contain 14MiB dirty ranges. (1 bits set per bitmap, also means 2 disks RAID5 or 3 disks RAID6).
For the best case, it can contain 896MiB dirty ranges. (all bits set per bitmap)
[WHY NOT BTRFS BTREE]
Current write-intent structure needs two features:
- Its data needs to survive cross stripe boundary
Normally this means write-intent btree needs to acts like a proper tree root, hasMETADATA_ITEMsfor all its tree blocks.- Its data update must be outside of a transaction Currently only log tree can do such thing.
But unfortunately log tree can not survive across transaction boundary.Thus write-intent btree can only meet one of the requirement, not a suitable solution here.
[TESTING AND BENCHMARK]
For performance benchmark, unfortunately I don't have 3 HDDs to test. Will do the benchmark after secured enough hardware.
For testing, it can survive volume/raid/dev-replace test groups, and no write-intent bitmap leakage.
Unfortunately there is still a warning triggered in btrfs/070, still under investigation, hopefully to be a false alert in bitmap clearing path.
[TODO]
- Scrub refactor to allow us to do proper recovery at mount time Need to change scrub interface to scrub based on logical bytenr.This can be a super big work, thus currently we will focus only on RAID56 new scrub interface for write-intent recovery only.
- Extra optimizations
* Skip full stripe writes
* Enlarge the window betweenbtrfs_write_intent_mark_dirty()andbtrfs_write_intent_writeback()- Bug hunts and more fstests runs
- Proper performance benchmark
Needs hardware/baremetal VMs, since I don't have any physical machine large enough to contian 3 3.5" HDDs.Qu Wenruo (11):
btrfs: introduce new compat RO flag,EXTRA_SUPER_RESERVED
btrfs: introduce a new experimental compat RO flag,
WRITE_INTENT_BITMAP
btrfs: introduce the on-disk format of btrfs write intent bitmaps
btrfs: load/create write-intent bitmaps at mount time
btrfs: write-intent: write the newly created bitmaps to all disks
btrfs: write-intent: introduce an internal helper to set bits for a
range.
btrfs: write-intent: introduce an internal helper to clear bits for a
range.
btrfs: write back write intent bitmap afterbarrier_all_devices()
btrfs: update and writeback the write-intent bitmap for RAID56 write.
btrfs: raid56: clear write-intent bimaps when a full stripe finishes.
btrfs: warn and clear bitmaps if there is dirty bitmap at mount timefs/btrfs/Makefile | 2 +- fs/btrfs/ctree.h | 24 +- fs/btrfs/disk-io.c | 54 +++ fs/btrfs/raid56.c | 16 + fs/btrfs/sysfs.c | 2 + fs/btrfs/volumes.c | 34 +- fs/btrfs/write-intent.c | 962 +++++++++++++++++++++++++++++++++++++ fs/btrfs/write-intent.h | 288 +++++++++++ fs/btrfs/zoned.c | 8 + include/uapi/linux/btrfs.h | 17 + 10 files changed, 1399 insertions(+), 8 deletions(-)create mode 100644
fs/btrfs/write-intent.c
create mode 100644fs/btrfs/write-intent.h--
2.36.1
Hi,
Quoted 67 lines
[BACKGROUND]
Unlike md-raid, btrfs RAID56 has nothing to sync its devices when power loss happens.For pure mirror based profiles it's fine as btrfs can utilize its csums to find the correct mirror the repair the bad ones.
But for RAID56, the repair itself needs the data from other devices, thus any out-of-sync data can degrade the tolerance.
Even worse, incorrect RMW can use the stale data to generate P/Q, removing the possibility of recovery the data.
For md-raid, it goes with write-intent bitmap, to do faster resilver, and goes journal (partial parity log for RAID5) to ensure it can even stand a powerloss + device lose.
[OBJECTIVE]
This patchset will introduce a btrfs specific write-intent bitmap.
The bitmap will locate at physical offset 1MiB of each device, and the content is the same between all devices.
When there is a RAID56 write (currently all RAID56 write, including full stripe write), before submitting all the real bios to disks, write-intent bitmap will be updated and flushed to all writeable devices.
So even if a powerloss happened, at the next mount time we know which full stripes needs to check, and can start a scrub for those involved logical bytenr ranges.
[NO RECOVERY CODE YET]
Unfortunately, this patchset only implements the write-intent bitmap code, the recovery part is still a place holder, as we need some scrub refactor to make it only scrub a logical bytenr range.
[ADVANTAGE OF BTRFS SPECIFIC WRITE-INTENT BITMAPS]
Since btrfs can utilize csum for its metadata and CoWed data, unlike dm-bitmap which can only be used for faster re-silver, we can fully rebuild the full stripe, as long as:
1) There is no missing device
For missing device case, we still need to go full journal.2) Untouched data stays untouched
This should be mostly sane for sane hardware.And since the btrfs specific write-intent bitmaps are pretty small (4KiB in size), the overhead much lower than full journal.
In the future, we may allow users to choose between just bitmaps or full journal to meet their requirement.
[BITMAPS DESIGN]
The bitmaps on-disk format looks like this:
[ super ][ entry 1 ][ entry 2 ] ... [entry N]
|<---------super::size(4K) ------------->|Super block contains how many entires are in use.
Quoted 8 lines
Each entry is 128 bits (16 bytes) in size, containing one u64 for bytenr, and u64 for one bitmap.
And all utilized entries will be sorted in their bytenr order, and no bit can overlap.
The blocksize is now fixed to
BTRFS_STRIPE_LEN(64KiB), so each entry can contain at most 4MiB, and the whole bitmaps can contain 224 entries.
Can we skip the write-intent bitmap log if we already log it in last N records (logrotate aware) to improve the write performance? because HDD sync IOPS is very small.
And a bigger blocksize help this case too.
Best Regards
Wang Yugui ([email protected])
2022/07/07
Quoted 85 lines
For the worst case, it can contain 14MiB dirty ranges. (1 bits set per bitmap, also means 2 disks RAID5 or 3 disks RAID6).
For the best case, it can contain 896MiB dirty ranges. (all bits set per bitmap)
[WHY NOT BTRFS BTREE]
Current write-intent structure needs two features:
- Its data needs to survive cross stripe boundary Normally this means write-intent btree needs to acts like a proper tree root, has
METADATA_ITEMsfor all its tree blocks.- Its data update must be outside of a transaction Currently only log tree can do such thing.
But unfortunately log tree can not survive across transaction boundary.Thus write-intent btree can only meet one of the requirement, not a suitable solution here.
[TESTING AND BENCHMARK]
For performance benchmark, unfortunately I don't have 3 HDDs to test. Will do the benchmark after secured enough hardware.
For testing, it can survive volume/raid/dev-replace test groups, and no write-intent bitmap leakage.
Unfortunately there is still a warning triggered in btrfs/070, still under investigation, hopefully to be a false alert in bitmap clearing path.
[TODO]
- Scrub refactor to allow us to do proper recovery at mount time Need to change scrub interface to scrub based on logical bytenr.This can be a super big work, thus currently we will focus only on RAID56 new scrub interface for write-intent recovery only.
- Extra optimizations
* Skip full stripe writes
* Enlarge the window betweenbtrfs_write_intent_mark_dirty()andbtrfs_write_intent_writeback()- Bug hunts and more fstests runs
- Proper performance benchmark
Needs hardware/baremetal VMs, since I don't have any physical machine large enough to contian 3 3.5" HDDs.Qu Wenruo (11):
btrfs: introduce new compat RO flag,EXTRA_SUPER_RESERVED
btrfs: introduce a new experimental compat RO flag,
WRITE_INTENT_BITMAP
btrfs: introduce the on-disk format of btrfs write intent bitmaps
btrfs: load/create write-intent bitmaps at mount time
btrfs: write-intent: write the newly created bitmaps to all disks
btrfs: write-intent: introduce an internal helper to set bits for a
range.
btrfs: write-intent: introduce an internal helper to clear bits for a
range.
btrfs: write back write intent bitmap afterbarrier_all_devices()
btrfs: update and writeback the write-intent bitmap for RAID56 write.
btrfs: raid56: clear write-intent bimaps when a full stripe finishes.
btrfs: warn and clear bitmaps if there is dirty bitmap at mount timefs/btrfs/Makefile | 2 +- fs/btrfs/ctree.h | 24 +- fs/btrfs/disk-io.c | 54 +++ fs/btrfs/raid56.c | 16 + fs/btrfs/sysfs.c | 2 + fs/btrfs/volumes.c | 34 +- fs/btrfs/write-intent.c | 962 +++++++++++++++++++++++++++++++++++++ fs/btrfs/write-intent.h | 288 +++++++++++ fs/btrfs/zoned.c | 8 + include/uapi/linux/btrfs.h | 17 + 10 files changed, 1399 insertions(+), 8 deletions(-) create mode 100644 fs/btrfs/write-intent.c create mode 100644 fs/btrfs/write-intent.hSignature · 1 line
2.36.1
Quoted 75 lines · Wang Yugui
Hi,
Quoted 73 lines
[BACKGROUND]
Unlike md-raid, btrfs RAID56 has nothing to sync its devices when power loss happens.For pure mirror based profiles it's fine as btrfs can utilize its csums to find the correct mirror the repair the bad ones.
But for RAID56, the repair itself needs the data from other devices, thus any out-of-sync data can degrade the tolerance.
Even worse, incorrect RMW can use the stale data to generate P/Q, removing the possibility of recovery the data.
For md-raid, it goes with write-intent bitmap, to do faster resilver, and goes journal (partial parity log for RAID5) to ensure it can even stand a powerloss + device lose.
[OBJECTIVE]
This patchset will introduce a btrfs specific write-intent bitmap.
The bitmap will locate at physical offset 1MiB of each device, and the content is the same between all devices.
When there is a RAID56 write (currently all RAID56 write, including full stripe write), before submitting all the real bios to disks, write-intent bitmap will be updated and flushed to all writeable devices.
So even if a powerloss happened, at the next mount time we know which full stripes needs to check, and can start a scrub for those involved logical bytenr ranges.
[NO RECOVERY CODE YET]
Unfortunately, this patchset only implements the write-intent bitmap code, the recovery part is still a place holder, as we need some scrub refactor to make it only scrub a logical bytenr range.
[ADVANTAGE OF BTRFS SPECIFIC WRITE-INTENT BITMAPS]
Since btrfs can utilize csum for its metadata and CoWed data, unlike dm-bitmap which can only be used for faster re-silver, we can fully rebuild the full stripe, as long as:
1) There is no missing device
For missing device case, we still need to go full journal.2) Untouched data stays untouched
This should be mostly sane for sane hardware.And since the btrfs specific write-intent bitmaps are pretty small (4KiB in size), the overhead much lower than full journal.
In the future, we may allow users to choose between just bitmaps or full journal to meet their requirement.
[BITMAPS DESIGN]
The bitmaps on-disk format looks like this:
[ super ][ entry 1 ][ entry 2 ] ... [entry N]
|<---------super::size(4K) ------------->|Super block contains how many entires are in use.
Each entry is 128 bits (16 bytes) in size, containing one u64 for bytenr, and u64 for one bitmap.
And all utilized entries will be sorted in their bytenr order, and no bit can overlap.
Quoted 6 lines
Quoted 2 lines
The blocksize is now fixed to
BTRFS_STRIPE_LEN(64KiB), so each entry can contain at most 4MiB, and the whole bitmaps can contain 224 entries.Can we skip the write-intent bitmap log if we already log it in last N records (logrotate aware) to improve the write performance? because HDD sync IOPS is very small.
I'm not aware about the logrotate idea you mentioned, mind to explain it more?
But the overall idea of journal/write-intent bitmaps are, always ensure there is something recording the full write or the write-intention before the real IO is submitted.
So I'm afraid such behavior can not be changed much.
Thanks,
Qu
Quoted 95 lines
And a bigger blocksize help this case too.
Best Regards
Wang Yugui ([email protected])
2022/07/07Quoted 85 lines
For the worst case, it can contain 14MiB dirty ranges. (1 bits set per bitmap, also means 2 disks RAID5 or 3 disks RAID6).
For the best case, it can contain 896MiB dirty ranges. (all bits set per bitmap)
[WHY NOT BTRFS BTREE]
Current write-intent structure needs two features:
- Its data needs to survive cross stripe boundary
Normally this means write-intent btree needs to acts like a proper tree root, hasMETADATA_ITEMsfor all its tree blocks.- Its data update must be outside of a transaction Currently only log tree can do such thing.
But unfortunately log tree can not survive across transaction boundary.Thus write-intent btree can only meet one of the requirement, not a suitable solution here.
[TESTING AND BENCHMARK]
For performance benchmark, unfortunately I don't have 3 HDDs to test. Will do the benchmark after secured enough hardware.
For testing, it can survive volume/raid/dev-replace test groups, and no write-intent bitmap leakage.
Unfortunately there is still a warning triggered in btrfs/070, still under investigation, hopefully to be a false alert in bitmap clearing path.
[TODO]
- Scrub refactor to allow us to do proper recovery at mount time Need to change scrub interface to scrub based on logical bytenr.This can be a super big work, thus currently we will focus only on RAID56 new scrub interface for write-intent recovery only.
- Extra optimizations
* Skip full stripe writes
* Enlarge the window betweenbtrfs_write_intent_mark_dirty()andbtrfs_write_intent_writeback()- Bug hunts and more fstests runs
- Proper performance benchmark
Needs hardware/baremetal VMs, since I don't have any physical machine large enough to contian 3 3.5" HDDs.Qu Wenruo (11):
btrfs: introduce new compat RO flag,EXTRA_SUPER_RESERVED
btrfs: introduce a new experimental compat RO flag,
WRITE_INTENT_BITMAP
btrfs: introduce the on-disk format of btrfs write intent bitmaps
btrfs: load/create write-intent bitmaps at mount time
btrfs: write-intent: write the newly created bitmaps to all disks
btrfs: write-intent: introduce an internal helper to set bits for a
range.
btrfs: write-intent: introduce an internal helper to clear bits for a
range.
btrfs: write back write intent bitmap afterbarrier_all_devices()
btrfs: update and writeback the write-intent bitmap for RAID56 write.
btrfs: raid56: clear write-intent bimaps when a full stripe finishes.
btrfs: warn and clear bitmaps if there is dirty bitmap at mount timefs/btrfs/Makefile | 2 +- fs/btrfs/ctree.h | 24 +- fs/btrfs/disk-io.c | 54 +++ fs/btrfs/raid56.c | 16 + fs/btrfs/sysfs.c | 2 + fs/btrfs/volumes.c | 34 +- fs/btrfs/write-intent.c | 962 +++++++++++++++++++++++++++++++++++++ fs/btrfs/write-intent.h | 288 +++++++++++ fs/btrfs/zoned.c | 8 + include/uapi/linux/btrfs.h | 17 + 10 files changed, 1399 insertions(+), 8 deletions(-)create mode 100644
fs/btrfs/write-intent.c
create mode 100644fs/btrfs/write-intent.h--
2.36.1
Hi,
Quoted 83 lines · Wang Yugui
Quoted 75 lines
Hi,
Quoted 73 lines
[BACKGROUND]
Unlike md-raid, btrfs RAID56 has nothing to sync its devices when power loss happens.For pure mirror based profiles it's fine as btrfs can utilize its csums to find the correct mirror the repair the bad ones.
But for RAID56, the repair itself needs the data from other devices, thus any out-of-sync data can degrade the tolerance.
Even worse, incorrect RMW can use the stale data to generate P/Q, removing the possibility of recovery the data.
For md-raid, it goes with write-intent bitmap, to do faster resilver, and goes journal (partial parity log for RAID5) to ensure it can even stand a powerloss + device lose.
[OBJECTIVE]
This patchset will introduce a btrfs specific write-intent bitmap.
The bitmap will locate at physical offset 1MiB of each device, and the content is the same between all devices.
When there is a RAID56 write (currently all RAID56 write, including full stripe write), before submitting all the real bios to disks, write-intent bitmap will be updated and flushed to all writeable devices.
So even if a powerloss happened, at the next mount time we know which full stripes needs to check, and can start a scrub for those involved logical bytenr ranges.
[NO RECOVERY CODE YET]
Unfortunately, this patchset only implements the write-intent bitmap code, the recovery part is still a place holder, as we need some scrub refactor to make it only scrub a logical bytenr range.
[ADVANTAGE OF BTRFS SPECIFIC WRITE-INTENT BITMAPS]
Since btrfs can utilize csum for its metadata and CoWed data, unlike dm-bitmap which can only be used for faster re-silver, we can fully rebuild the full stripe, as long as:
1) There is no missing device
For missing device case, we still need to go full journal.2) Untouched data stays untouched
This should be mostly sane for sane hardware.And since the btrfs specific write-intent bitmaps are pretty small (4KiB in size), the overhead much lower than full journal.
In the future, we may allow users to choose between just bitmaps or full journal to meet their requirement.
[BITMAPS DESIGN]
The bitmaps on-disk format looks like this:
[ super ][ entry 1 ][ entry 2 ] ... [entry N]
|<---------super::size(4K) ------------->|Super block contains how many entires are in use.
Each entry is 128 bits (16 bytes) in size, containing one u64 for bytenr, and u64 for one bitmap.
And all utilized entries will be sorted in their bytenr order, and no bit can overlap.
Quoted 6 lines
Quoted 2 lines
The blocksize is now fixed to
BTRFS_STRIPE_LEN(64KiB), so each entry can contain at most 4MiB, and the whole bitmaps can contain 224 entries.Can we skip the write-intent bitmap log if we already log it in last N records (logrotate aware) to improve the write performance? because HDD sync IOPS is very small.
Quoted 8 lines
I'm not aware about the logrotate idea you mentioned, mind to explain it more?
But the overall idea of journal/write-intent bitmaps are, always ensure there is something recording the full write or the write-intention before the real IO is submitted.
So I'm afraid such behavior can not be changed much.
The basic idea is that we recover the data by scrub, not full log, so log *1 is same to log *2, but with less IOPS?
log *1
write(0-64K) log wirte(64K-128K) log wirte(128K-192K) log wirte(192K-256K) log log *2 write(0-256K) log already write(0-256K), skip already write(0-256K), skip already write(0-256K), skip
we can search the entry we currently used, but can not search the prev entry, because it maybe be logrotated?
Best Regards
Wang Yugui ([email protected])
2022/07/07
Quoted 94 lines · Wang Yugui
Hi,
Quoted 83 lines · Wang Yugui
Quoted 75 lines
Hi,
Quoted 73 lines
[BACKGROUND]
Unlike md-raid, btrfs RAID56 has nothing to sync its devices when power loss happens.For pure mirror based profiles it's fine as btrfs can utilize its csums to find the correct mirror the repair the bad ones.
But for RAID56, the repair itself needs the data from other devices, thus any out-of-sync data can degrade the tolerance.
Even worse, incorrect RMW can use the stale data to generate P/Q, removing the possibility of recovery the data.
For md-raid, it goes with write-intent bitmap, to do faster resilver, and goes journal (partial parity log for RAID5) to ensure it can even stand a powerloss + device lose.
[OBJECTIVE]
This patchset will introduce a btrfs specific write-intent bitmap.
The bitmap will locate at physical offset 1MiB of each device, and the content is the same between all devices.
When there is a RAID56 write (currently all RAID56 write, including full stripe write), before submitting all the real bios to disks, write-intent bitmap will be updated and flushed to all writeable devices.
So even if a powerloss happened, at the next mount time we know which full stripes needs to check, and can start a scrub for those involved logical bytenr ranges.
[NO RECOVERY CODE YET]
Unfortunately, this patchset only implements the write-intent bitmap code, the recovery part is still a place holder, as we need some scrub refactor to make it only scrub a logical bytenr range.
[ADVANTAGE OF BTRFS SPECIFIC WRITE-INTENT BITMAPS]
Since btrfs can utilize csum for its metadata and CoWed data, unlike dm-bitmap which can only be used for faster re-silver, we can fully rebuild the full stripe, as long as:
1) There is no missing device
For missing device case, we still need to go full journal.2) Untouched data stays untouched
This should be mostly sane for sane hardware.And since the btrfs specific write-intent bitmaps are pretty small (4KiB in size), the overhead much lower than full journal.
In the future, we may allow users to choose between just bitmaps or full journal to meet their requirement.
[BITMAPS DESIGN]
The bitmaps on-disk format looks like this:
[ super ][ entry 1 ][ entry 2 ] ... [entry N] |<--------- super::size (4K) ------------->|Super block contains how many entires are in use.
Each entry is 128 bits (16 bytes) in size, containing one u64 for bytenr, and u64 for one bitmap.
And all utilized entries will be sorted in their bytenr order, and no bit can overlap.
Quoted 6 lines
Quoted 2 lines
The blocksize is now fixed to
BTRFS_STRIPE_LEN(64KiB), so each entry can contain at most 4MiB, and the whole bitmaps can contain 224 entries.Can we skip the write-intent bitmap log if we already log it in last N records (logrotate aware) to improve the write performance? because HDD sync IOPS is very small.
Quoted 8 lines
I'm not aware about the logrotate idea you mentioned, mind to explain it more?
But the overall idea of journal/write-intent bitmaps are, always ensure there is something recording the full write or the write-intention before the real IO is submitted.
So I'm afraid such behavior can not be changed much.
Quoted 8 lines
The basic idea is that we recover the data by scrub, not full log, so log *1 is same to log *2, but with less IOPS?
log *1
write(0-64K) log wirte(64K-128K) log wirte(128K-192K) log wirte(192K-256K) log
It's already done using the 64KiB blocksize.
In that case, a partial write for any range inside the full stripe (for 3 disk RAID5 it will be in 128KiB), the full stripe will be marked dirty.
So for above log *1 case, it will only be 4 bits set the full stripe 0-256K (if that's a full stripe) in one go.
Quoted 8 lines
log *2
write(0-256K) log already write(0-256K), skip already write(0-256K), skip already write(0-256K), skipwe can search the entry we currently used, but can not search the prev entry, because it maybe be logrotated?
Logrotation makes no sense. After one entry being written, the next write will still trigger a new write for the bitmap, the same amount of seek.
And the existing code already has the window to merge different bitmap writes into the same event count.
The requirement is only to ensure the bitmaps with corresponding bits reach disks before the IO has been submitted. It leaves a lot of room for us to merge the bits into one write.
Thanks,
Qu
Quoted 5 lines
Best Regards
Wang Yugui ([email protected])
2022/07/07