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
On 2018/12/18 下午3:57, Qu Wenruo wrote:
> [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.
>
> Fixes: a7a44164c84e ("btrfs-progs: image: Use correct device size when restoring")
> Signed-off-by: Qu Wenruo <wqu@xxxxxxxx>
> ---
> image/main.c | 14 +++++++++++---
> 1 file changed, 11 insertions(+), 3 deletions(-)
>
> diff --git a/image/main.c b/image/main.c
> index cff5cc87662c..4fba8283cec7 100644
> --- a/image/main.c
> +++ b/image/main.c
> @@ -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;
>
Attachment:
signature.asc
Description: OpenPGP digital signature
