btrfs-progs: image: Only enlarge result image if it's a 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.

Fixes: a7a44164c84e ("btrfs-progs: image: Use correct device size when restoring")
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
master
Qu Wenruo 2018-12-18 15:57:40 +08:00 committed by David Sterba
parent 9d5e88940a
commit 37cf7de3a1
1 changed files with 11 additions and 3 deletions

View File

@ -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;