linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Qu Wenruo <wqu@suse.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH] btrfs-progs: image: Only enlarge result image if it's regular file
Date: Tue, 18 Dec 2018 15:57:40 +0800	[thread overview]
Message-ID: <20181218075740.23723-1-wqu@suse.com> (raw)

[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>
---
 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;
-- 
2.20.0


             reply	other threads:[~2018-12-18  7:57 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-18  7:57 Qu Wenruo [this message]
2018-12-18  7:58 ` [PATCH] btrfs-progs: image: Only enlarge result image if it's regular file Qu Wenruo
2019-01-15 17:37   ` David Sterba

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181218075740.23723-1-wqu@suse.com \
    --to=wqu@suse.com \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).