btrfs-progs: image: Only enlarge result image if it's regular file
PATCH 1/1 btrfs-progs: image: Only enlarge result image if it's regular file
[BUG]
misc-test/021 will fail with the following error message:
====== RUN CHECK root_helper btrfs-progs/btrfs-image -r btrfs-progs/tests//test.img /dev/loop2
ERROR: failed to enlarge result image: Invalid argument
ERROR: failed to fix chunks and devices mapping, the fs may not be mountable: Invalid argument extent buffer leak: start 30638080 len 16384
extent buffer leak: start 30408704 len 16384
WARNING: dirty eb leak (aborted trans): start 30408704 len 16384
ERROR: restore failed: Invalid argument
failed: root_helper btrfs-progs/btrfs-image -r btrfs-progs/tests//test.img /dev/loop2 test failed for case 021-image-multi-devices
[CAUSE]
For misc-test/021, we're using loopback device, which doesn't support ftruncate.
That's why it returns EINVAL.
[FIX]
Only call ftruncate64() if we're operating on a regluar file.
--- image/main.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)
image/main.c +11 -3
@@ -2101,6 +2101,7 @@ static int fixup_device_size(struct btrfs_trans_handle *trans,struct extent_buffer *leaf;struct btrfs_root *root = fs_info->chunk_root;struct btrfs_key key; +struct stat buf;u64 devid, cur_devid;u64 dev_size;/* Get from last dev extents */ int ret;
@@ -2147,12 +2148,19 @@ static int fixup_device_size(struct btrfs_trans_handle *trans,btrfs_set_stack_device_total_bytes (dev_item, dev_size);btrfs_set_stack_device_bytes_used (dev_item, mdres->alloced_chunks); -/* Don't forget to enlarge the real file */ - ret =ftruncate64 (out_fd, dev_size); + ret =fstat (out_fd, &buf);if (ret <0 ) { -error ("failed to enlarge result image: %m" ); +error ("failed to stat result image: %m" );return -errno; } +if (S_ISREG (buf.st_mode)) { +/* Don't forget to enlarge the real file */ + ret =ftruncate64 (out_fd, dev_size); +if (ret <0 ) { +error ("failed to enlarge result image: %m" ); +return -errno; + } + } key.objectid =BTRFS_DEV_ITEMS_OBJECTID ; key.type =BTRFS_DEV_ITEM_KEY ;
Signature · 1 line
2.20.0
Hi David,
This patch can also be folded into the original commit a7a44164c84e ("btrfs-progs: image: Use correct device size when restoring") in devel branch.
Thanks,
Qu
Quoted 63 lines · Qu Wenruo
[BUG]
misc-test/021 will fail with the following error message:====== RUN CHECK
root_helperbtrfs-progs/btrfs-image -r btrfs-progs/tests//test.img/dev/loop2
ERROR: failed to enlarge result image: Invalid argument
ERROR: failed to fix chunks and devices mapping, the fs may not be mountable: Invalid argument extent buffer leak: start 30638080 len 16384
extent buffer leak: start 30408704 len 16384
WARNING: dirty eb leak (aborted trans): start 30408704 len 16384
ERROR: restore failed: Invalid argument
failed:root_helperbtrfs-progs/btrfs-image -r btrfs-progs/tests//test.img/dev/loop2test failed for case 021-image-multi-devices[CAUSE]
For misc-test/021, we're using loopback device, which doesn't support ftruncate.
That's why it returns EINVAL.[FIX]
Only callftruncate64()if we're operating on a regluar file.Fixes a7a44164c84e ("btrfs-progs: image: Use correct device size when restoring")Signed-off-by Qu Wenruo <[email protected]>--- image/main.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)image/main.c +11
-3@@ -2101,6 +2101,7 @@ static int fixup_device_size(struct btrfs_trans_handle *trans,struct extent_buffer *leaf;struct btrfs_root *root = fs_info->chunk_root;struct btrfs_key key; +struct stat buf;u64 devid, cur_devid;u64 dev_size;/* Get from last dev extents */ int ret;@@ -2147,12 +2148,19 @@ static int fixup_device_size(struct btrfs_trans_handle *trans,btrfs_set_stack_device_total_bytes (dev_item, dev_size);btrfs_set_stack_device_bytes_used (dev_item, mdres->alloced_chunks); -/* Don't forget to enlarge the real file */ - ret =ftruncate64 (out_fd, dev_size); + ret =fstat (out_fd, &buf);if (ret <0 ) { -error ("failed to enlarge result image: %m" ); +error ("failed to stat result image: %m" );return -errno; } +if (S_ISREG (buf.st_mode)) { +/* Don't forget to enlarge the real file */ + ret =ftruncate64 (out_fd, dev_size); +if (ret <0 ) { +error ("failed to enlarge result image: %m" ); +return -errno; + } + } key.objectid =BTRFS_DEV_ITEMS_OBJECTID ; key.type =BTRFS_DEV_ITEM_KEY ;
Quoted 3 lines · Qu Wenruo
This patch can also be folded into the original commit
a7a44164c84e("btrfs-progs: image: Use correct device size when restoring") in devel branch.
I've applied this patch right after that one, as it's fixing a bit different problem. The temporary test failure is not a big deal, given that it was failing anyway. Thanks.