Btrfs-progs: rescue: To fix device related
Subsystems (2)
v2 cover Btrfs-progs: rescue: To fix device related
The patchset can be fetched from github:
https://github.com/adam900710/btrfs-progs/tree/check_unaligned_dev
There are several reports in mail list for btrfs device size related problems.
1) kernel refuse to mount some fs, due to mismatched super total_bytes Unmountable if super total_bytes is smaller than total rw bytes of all devices.
Root cause seems to be that, at least for kernel v4.13, some device resize/add operation on unaligned device can lead to super size out of sync with device size.
And normally this will cause super size to be 1 page (4096) smaller than all devices' size.
This patchset provides the tool to fix it offline. (At least better than unmountable forever)
2) Harmless kernel warning for btrfs_update_device() v4.14 introduced restrict device size checker. This is causing harmless but annoying kernel warnning.
It can be fixed online with "btrfs filesystem resize".
This patchset also provide a fallback method to fix it.
Finally, fix a typo which makes "btrfs rescue zero-log" to continue execution even the fs is mounted and error message outputted.
Changelog:
v2:
Move the command line tool from "btrfs check" to "btrfs rescue", suggested by David. Although fsck can still repair/detect it. Function naming change for cmds-check.c part, suggested by Nikolay. Remove inaccurate kernel version, suggested by Nikolay. Fix a bug when coding in cmds-rescue.c.
Split the first patch to make review easier.
Qu Wenruo (6):
btrfs-progs: Introduce function to fix unaligned device size
btrfs-progs: Introduce function to fix super block total bytes
btrfs-progs: rescue: Introduce fix-device-size
btrfs-progs: check: Also check and repair unalignment/mismatch device
and super size
btrfs-progs: test/fsck: Add test case image for --fix-dev-size
btrfs-progs: rescue: Fix zero-log mounted branch
Documentation/btrfs-rescue.asciidoc | 29 ++++ cmds-check.c | 76 +++++++++ cmds-rescue.c | 49 ++++++ .../dev_and_super_mismatch_unaligned.raw.xz | Bin 0 -> 21536 bytes volumes.c | 178 +++++++++++++++++++++ volumes.h | 4 + 6 files changed, 336 insertions(+) create mode 100644 tests/fsck-tests/027-unaligned-super-dev-sizes/dev_and_super_mismatch_unaligned.raw.xz
Signature · 1 line
2.14.2
PATCH v2 1/6 btrfs-progs: Introduce function to fix unaligned device size
Recent kernel introduced alignment check for dev item, however older kernel doesn't align device size when adding new device or shrinking existing device.
This makes noisy kernel warning every time when any DEV_ITEM get updated.
Introduce function to fix device size in btrfs-progs to fix the problem offline.
--- volumes.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ volumes.h | 2 ++ 2 files changed, 73 insertions(+)
volumes.c +71 -0
@@ -2332,3 +2332,74 @@ u64 btrfs_stripe_length(struct btrfs_fs_info *fs_info, }return stripe_len; } + +/* +* Return 0 if size of @device is already good +* Return >0 if size of @device is not aligned but fixed without problem +* Return <0 if something wrong happened when aligning the size of @device +*/ +int btrfs_fix_device_size (struct btrfs_fs_info *fs_info, +struct btrfs_device *device) +{ +struct btrfs_trans_handle *trans; +struct btrfs_key key; +struct btrfs_path path; +struct btrfs_root *chunk_root = fs_info->chunk_root; +struct btrfs_dev_item *di; +u64 old_bytes = device->total_bytes; +int ret; + +if (IS_ALIGNED (old_bytes, fs_info->sectorsize)) +return 0 ; + +/* Align the in-memory total_bytes first, and use it as correct size */ + device->total_bytes =round_down (device->total_bytes, + fs_info->sectorsize); + + key.objectid =BTRFS_DEV_ITEMS_OBJECTID ; + key.type =BTRFS_DEV_ITEM_KEY ; + key.offset = device->devid; + + trans =btrfs_start_transaction (chunk_root,1 ); +if (IS_ERR (trans)) { + ret =PTR_ERR (trans); +error ("error starting transaction: %d (%s)" , + ret,strerror (-ret)); +return ret; + } + +btrfs_init_path (&path); + ret =btrfs_search_slot (trans, chunk_root, &key, &path,0 ,1 ); +if (ret >0 ) { +error ("failed to find DEV_ITEM for devid %llu" , + device->devid); + ret = -ENOENT ; + goto err; + } +if (ret <0 ) { +error ("failed to search chunk root: %d (%s)" , + ret,strerror (-ret)); + goto err; + } + di =btrfs_item_ptr (path.nodes[0 ], path.slots[0 ], + struct btrfs_dev_item); +btrfs_set_device_total_bytes (path.nodes[0 ], di, device->total_bytes); +btrfs_mark_buffer_dirty (path.nodes[0 ]); + ret =btrfs_commit_transaction (trans, chunk_root); +if (ret <0 ) { +error ("failed to commit current transaction: %d (%s)" , + ret,strerror (-ret)); +btrfs_release_path (&path); +return ret; + } +btrfs_release_path (&path); +printf ("Fixed device size for devid %llu, old size: %llu new size: %llu\n" , + device->devid, old_bytes, device->total_bytes); +return 1 ; + +err: +/* We haven't modified anything, it's OK to commit current trans */ +btrfs_commit_transaction (trans, chunk_root); +btrfs_release_path (&path); +return ret; +}
volumes.h +2 -0
@@ -245,4 +245,6 @@ int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info,u64 btrfs_stripe_length (struct btrfs_fs_info *fs_info,struct extent_buffer *leaf,struct btrfs_chunk *chunk); +int btrfs_fix_device_size (struct btrfs_fs_info *fs_info, +struct btrfs_device *device);#endif
Signature · 1 line
2.14.2
PATCH v2 2/6 btrfs-progs: Introduce function to fix super block total bytes
In reply to Qu WenruoRecent kernel (starting from v4.6) will refuse to mount if super block total bytes is smaller than all devices' size.
This makes end user unable to do anything to their otherwise quite healthy fs.
To fix such problem, introduce repair function in btrfs-progs to fix it offline.
--- volumes.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ volumes.h | 1 + 2 files changed, 53 insertions(+)
volumes.c +52 -0
@@ -2403,3 +2403,55 @@ err:btrfs_release_path (&path);return ret; } + +/* +* Return 0 if super block total bytes matches with all devices' total_bytes +* Return >0 if super block total bytes mismatch but fixed without problem +* Return <0 if we failed to fix super block total_bytes +*/ +int btrfs_fix_super_size (struct btrfs_fs_info *fs_info) +{ +struct btrfs_trans_handle *trans; +struct btrfs_device *device; +struct list_head *dev_list = &fs_info->fs_devices->devices; +u64 total_bytes =0 ; +u64 old_bytes =btrfs_super_total_bytes (fs_info->super_copy); +int ret; + +list_for_each_entry (device, dev_list, dev_list) { +/* +* Caller should ensure this function is called after aligning +* all devices' total_bytes. +*/ +if (!IS_ALIGNED (device->total_bytes, fs_info->sectorsize)) { +error ("device %llu total_bytes %llu not aligned to %u" , + device->devid, device->total_bytes, + fs_info->sectorsize); +return -EUCLEAN ; + } + total_bytes += device->total_bytes; + } + +if (total_bytes == old_bytes) +return 0 ; + +btrfs_set_super_total_bytes (fs_info->super_copy, total_bytes); + +/* Commit transaction to update all super blocks */ + trans =btrfs_start_transaction (fs_info->tree_root,1 ); +if (IS_ERR (trans)) { + ret =PTR_ERR (trans); +error ("error starting transaction: %d (%s)" , + ret,strerror (-ret)); +return ret; + } + ret =btrfs_commit_transaction (trans, fs_info->tree_root); +if (ret <0 ) { +error ("failed to commit current transaction: %d (%s)" , + ret,strerror (-ret)); +return ret; + } +printf ("Fixed super total bytes, old size: %llu new size: %llu\n" , + old_bytes, total_bytes); +return 1 ; +}
volumes.h +1 -0
@@ -247,4 +247,5 @@ u64 btrfs_stripe_length(struct btrfs_fs_info *fs_info, struct btrfs_chunk *chunk);int btrfs_fix_device_size (struct btrfs_fs_info *fs_info,struct btrfs_device *device); +int btrfs_fix_super_size (struct btrfs_fs_info *fs_info);#endif
Signature · 1 line
2.14.2
PATCH v2 3/6 btrfs-progs: rescue: Introduce fix-device-size
In reply to Qu WenruoIntroduce fix-device-size rescue subcommand to fix device size alignment related problems.
Especially for people unable to mount their fs with super total bytes mismatch, this tool should make their fs live again.
--- Documentation/btrfs-rescue.asciidoc | 29 +++++++++++++++++++ cmds-rescue.c | 48 ++++++++++++++++++++++++++++++++ volumes.c | 55 +++++++++++++++++++++++++++++++++++++ volumes.h | 1 + 4 files changed, 133 insertions(+)
Documentation/btrfs-rescue.asciidoc +29 -0
@@ -73,6 +73,35 @@ the log and the filesystem may be mounted normally again. The keywords to look for are 'open_ctree' which says that it's during mount and function names that contain 'replay', 'recover' or 'log_tree'. +*fix-device-size* <device>:: +fix device size and super block total bytes ++ +This command will fix the following problems, by re-aligning all devices' total +bytes and re-calculating super block total bytes. ++ +1. Newer kernel refuse to mount btrfs caused by mismatch super block total bytes ++ +---- +BTRFS error (device sdb): super_total_bytes 92017859088384 mismatch with fs_devices total_rw_bytes 92017859094528 +---- ++ +2. Noisy kernel warning for newer kernels ++ +---- +WARNING: CPU: 3 PID: 439 at fs/btrfs/ctree.h:1559 btrfs_update_device+0x1c5/0x1d0 [btrfs] +---- ++ +And the corresponding line is the `WARN_ON()` line below: ++ +---- +{ + BUILD_BUG_ON(sizeof(u64) != + sizeof(((struct btrfs_dev_item *)0))->total_bytes); + WARN_ON(!IS_ALIGNED(val, eb->fs_info->sectorsize)); + btrfs_set_64(eb, s, offsetof(struct btrfs_dev_item, total_bytes), val); +} +---- + EXIT STATUS ----------- *btrfs rescue* returns a zero exit status if it succeeds. Non zero is
cmds-rescue.c +48 -0
66 lines not on this page · raw message
volumes.c +55 -0
58 lines not on this page · raw message
volumes.h +1 -0
@@ -248,4 +248,5 @@ u64 btrfs_stripe_length(struct btrfs_fs_info *fs_info,int btrfs_fix_device_size (struct btrfs_fs_info *fs_info,struct btrfs_device *device);int btrfs_fix_super_size (struct btrfs_fs_info *fs_info); +int btrfs_fix_device_and_super_size (struct btrfs_fs_info *fs_info);#endif
Signature · 1 line
2.14.2
PATCH v2 4/6 btrfs-progs: check: Also check and repair unalignment/mismatch device and super size
In reply to Qu WenruoAlong with the rescue introduced, also introduce check and repair for them.
Unlike normal check functions, some of the check is optional, and even if the image failed to pass optional check, kernel can still run fine. (But may cause noisy kernel warning)
So some check, mainly for alignment, will not cause btrfs check to fail, but only to output warning and how to fix it.
For repair, it just calls the same repair in rescue, and is included in 'btrfs check --repair'.
But 'btrfs rescue' is still the preferred method, since it can bypass all the possible dangerous repair.
--- cmds-check.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+)
cmds-check.c +76 -0
@@ -9879,6 +9879,67 @@ static int check_device_used(struct device_record *dev_rec, } } +/* +* Extra (optional) check for dev_item size +* +* To avoid possible kernel warning for newer kernel. +* It's not a deadly problem, just to info newer kernel user. +* So it won't return any value, just info user directly. +*/ +static void check_dev_size_alignment (u64 devid,u64 total_bytes, +u32 sectorsize) +{ +if (!IS_ALIGNED (total_bytes, sectorsize)) { +warning ("unaligned total_bytes detected for devid %llu, have %llu should be aligned to %u" , + devid, total_bytes, sectorsize); +warning ("this is OK for older kernel, but may cause kernel warning for newer kernel" ); +warning ("this can be fixed by 'btrfs rescue fix-device-size'" ); + } +} + +/* +* Unlike device size alignment check above, some super total_bytes check +* failure can lead to mount failure for newer kernel. +* +* So this function will return the error for fatal super total_bytes problem. +*/ +static bool is_super_size_valid (struct btrfs_fs_info *fs_info) +{ +struct btrfs_device *dev; +struct list_head *dev_list = &fs_info->fs_devices->devices; +u64 total_bytes =0 ; +u64 super_bytes =btrfs_super_total_bytes (fs_info->super_copy); + +list_for_each_entry (dev, dev_list, dev_list) + total_bytes += dev->total_bytes; + +/* Important check, which can cause unmountable fs */ +if (super_bytes < total_bytes) { +error ("super total bytes %llu smaller than real device(s) size %llu" , + super_bytes, total_bytes); +error ("mounting this fs may fail for newer kernels" ); +error ("this can be fixed by 'btrfs rescue fix-device-size'" ); +return false; + } + +/* +* Optional check, just to make everything aligned and match with +* each other. +* +* For btrfs-image restored fs, we don't need to check it anyway. +*/ +if (btrfs_super_flags (fs_info->super_copy) & + (BTRFS_SUPER_FLAG_METADUMP |BTRFS_SUPER_FLAG_METADUMP_V2 )) +return true; +if (!IS_ALIGNED (super_bytes, fs_info->sectorsize) || + !IS_ALIGNED (total_bytes, fs_info->sectorsize) || + super_bytes != total_bytes) { +warning ("minor unaligned/mismatch device size detected" ); +warning ("recommended to use 'btrfs rescue fix-device-size' to fix it" ); + } +return true; +} +/* check btrfs_dev_item -> btrfs_dev_extent */ static int check_devices (struct rb_root *dev_cache,struct device_extent_tree *dev_extent_cache)
@@ -9896,6 +9957,8 @@ static int check_devices(struct rb_root *dev_cache,if (err) ret = err; +check_dev_size_alignment (dev_rec->devid, dev_rec->total_byte, + global_info->sectorsize); dev_node =rb_next (dev_node); } list_for_each_entry(dext_rec, &dev_extent_cache->no_device_orphans,
@@ -11141,6 +11204,7 @@ static int check_dev_item(struct btrfs_fs_info *fs_info,struct btrfs_path path;struct btrfs_key key;struct btrfs_dev_extent *ptr; +u64 total_bytes;u64 dev_id;u64 used;u64 total =0 ;
@@ -11149,6 +11213,7 @@ static int check_dev_item(struct btrfs_fs_info *fs_info, dev_item = btrfs_item_ptr(eb, slot, struct btrfs_dev_item); dev_id =btrfs_device_id (eb, dev_item); used =btrfs_device_bytes_used (eb, dev_item); + total_bytes =btrfs_device_total_bytes (eb, dev_item); key.objectid = dev_id; key.type =BTRFS_DEV_EXTENT_KEY ;
@@ -11193,6 +11258,8 @@ next: BTRFS_DEV_EXTENT_KEY, dev_id);return ACCOUNTING_MISMATCH ; } +check_dev_size_alignment (dev_id, total_bytes, fs_info->sectorsize); +return 0 ; }
@@ -11757,6 +11824,12 @@ static int do_check_chunks_and_extents(struct btrfs_fs_info *fs_info)else ret =check_chunks_and_extents (fs_info); +/* Also repair device size related problems */ +if (repair && !ret) { + ret =btrfs_fix_device_and_super_size (fs_info); +if (ret >0 ) + ret =0 ; + }return ret; }
@@ -13256,6 +13329,9 @@ int cmd_check(int argc, char **argv)error ("errors found in extent allocation tree or chunk allocation" ); +/* Only re-check super size after we checked and repaired the fs */ + err |= !is_super_size_valid (info); + ret =repair_root_items (info); err |= !!ret;if (ret <0 ) {
Signature · 1 line
2.14.2
PATCH v2 5/6 btrfs-progs: test/fsck: Add test case image for --fix-dev-size
In reply to Qu WenruoFrom: Qu Wenruo <[email protected]>
The image has 2 problems mixed:
1) Too small super total_bytes
This super total_bytes is manually modified to create such problem.
2) Unaligned dev item total_bytes
This is created by v4.12 kernel, with 128M + 2K device added, and original device removed.
Then we can create such image with unaligned dev item total_bytes.
--- .../dev_and_super_mismatch_unaligned.raw.xz | Bin 0 -> 21536 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 tests/fsck-tests/027-unaligned-super-dev-sizes/dev_and_super_mismatch_unaligned.raw.xz
tests/fsck-tests/027-unaligned-super-dev-sizes/dev_and_super_mismatch_unaligned.raw.xz new file binary +0 -0
Signature · 1 line
2.14.2
PATCH v2 6/6 btrfs-progs: rescue: Fix zero-log mounted branch
In reply to Qu WenruoSeems to be a typo that, in (ret > 0) branch of check_mounted(), zero-log set the return value but doesn't return.
Fix it by adding back the missing return.
--- cmds-rescue.c | 1 + 1 file changed, 1 insertion(+)
cmds-rescue.c +1 -0
@@ -178,6 +178,7 @@ static int cmd_rescue_zero_log(int argc, char **argv) }else if (ret) {error ("%s is currently mounted" , devname); ret = -EBUSY ; + goto out; } root =open_ctree (devname,0 ,OPEN_CTREE_WRITES |OPEN_CTREE_PARTIAL );
Signature · 1 line
2.14.2
David Sterba
AppliedRe: [PATCH v2 6/6] btrfs-progs: rescue: Fix zero-log mounted branch
Quoted 6 lines · Qu Wenruo
Seems to be a typo that, in (ret > 0) branch of
check_mounted(), zero-log set the return value but doesn't return.Fix it by adding back the missing return.
Signed-off-by Qu Wenruo <[email protected]>
Applied, thanks.
Quoted 29 lines · Qu Wenruo
The patchset can be fetched from github:
https://github.com/adam900710/btrfs-progs/tree/check_unaligned_devThere are several reports in mail list for btrfs device size related problems.
1) kernel refuse to mount some fs, due to mismatched super
total_bytesUnmountable if supertotal_bytesis smaller than total rw bytes of all devices.
Root cause seems to be that, at least for kernel v4.13, some device resize/add operation on unaligned device can lead to super size out of sync with device size.
And normally this will cause super size to be 1 page (4096) smaller than all devices' size.This patchset provides the tool to fix it offline. (At least better than unmountable forever)
2) Harmless kernel warning for
btrfs_update_device()v4.14 introduced restrict device size checker. This is causing harmless but annoying kernel warnning.It can be fixed online with "btrfs filesystem resize".
This patchset also provide a fallback method to fix it.
Finally, fix a typo which makes "btrfs rescue zero-log" to continue execution even the fs is mounted and error message outputted.
Quoted 9 lines
Changelog:
v2:
Move the command line tool from "btrfs check" to "btrfs rescue", suggested by David. Although fsck can still repair/detect it. Function naming change forcmds-check.cpart, suggested by Nikolay. Remove inaccurate kernel version, suggested by Nikolay. Fix a bug when coding incmds-rescue.c.
Split the first patch to make review easier.
For the whole series:
Quoted 18 lines
Qu Wenruo (6):
btrfs-progs: Introduce function to fix unaligned device size
btrfs-progs: Introduce function to fix super block total bytes
btrfs-progs: rescue: Introduce fix-device-size
btrfs-progs: check: Also check and repair unalignment/mismatch device
and super size
btrfs-progs: test/fsck: Add test case image for --fix-dev-size
btrfs-progs: rescue: Fix zero-log mounted branchDocumentation/btrfs-rescue.asciidoc | 29 ++++ cmds-check.c | 76 +++++++++ cmds-rescue.c | 49 ++++++ .../dev_and_super_mismatch_unaligned.raw.xz | Bin 0 -> 21536 bytes volumes.c | 178 +++++++++++++++++++++ volumes.h | 4 + 6 files changed, 336 insertions(+) create mode 100644 tests/fsck-tests/027-unaligned-super-dev-sizes/dev_and_super_mismatch_unaligned.raw.xz
David Sterba
AppliedQuoted 38 lines · Qu Wenruo
The patchset can be fetched from github:
https://github.com/adam900710/btrfs-progs/tree/check_unaligned_devThere are several reports in mail list for btrfs device size related problems.
1) kernel refuse to mount some fs, due to mismatched super
total_bytesUnmountable if supertotal_bytesis smaller than total rw bytes of all devices.
Root cause seems to be that, at least for kernel v4.13, some device resize/add operation on unaligned device can lead to super size out of sync with device size.
And normally this will cause super size to be 1 page (4096) smaller than all devices' size.This patchset provides the tool to fix it offline. (At least better than unmountable forever)
2) Harmless kernel warning for
btrfs_update_device()v4.14 introduced restrict device size checker. This is causing harmless but annoying kernel warnning.It can be fixed online with "btrfs filesystem resize".
This patchset also provide a fallback method to fix it.
Finally, fix a typo which makes "btrfs rescue zero-log" to continue execution even the fs is mounted and error message outputted.
Changelog:
v2:
Move the command line tool from "btrfs check" to "btrfs rescue", suggested by David. Although fsck can still repair/detect it. Function naming change forcmds-check.cpart, suggested by Nikolay. Remove inaccurate kernel version, suggested by Nikolay. Fix a bug when coding incmds-rescue.c.
Split the first patch to make review easier.
Quoted 8 lines
Qu Wenruo (6):
btrfs-progs: Introduce function to fix unaligned device size
btrfs-progs: Introduce function to fix super block total bytes
btrfs-progs: rescue: Introduce fix-device-size
btrfs-progs: check: Also check and repair unalignment/mismatch device
and super size
btrfs-progs: test/fsck: Add test case image for --fix-dev-size
btrfs-progs: rescue: Fix zero-log mounted branch
Applied, thanks.
Quoted 6 lines
Documentation/btrfs-rescue.asciidoc | 29 ++++ cmds-check.c | 76 +++++++++ cmds-rescue.c | 49 ++++++ .../dev_and_super_mismatch_unaligned.raw.xz | Bin 0 -> 21536 bytes volumes.c | 178 +++++++++++++++++++++ volumes.h | 4 +
The repair functions do not belong to volumes.c, but we need to move and split cmds-check.c first so I'm going to apply it as-is.
I've also extended the test to exercise fix-device-size.