From: Qu Wenruo <wqu@suse.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH v3 1/6] btrfs-progs: image: Use correct device size when restoring
Date: Mon, 8 Oct 2018 20:30:39 +0800 [thread overview]
Message-ID: <20181008123044.13413-2-wqu@suse.com> (raw)
In-Reply-To: <20181008123044.13413-1-wqu@suse.com>
When restoring btrfs image, the total_bytes of device item is not
updated correctly. In fact total_bytes can be left 0 for restored image.
It doesn't trigger any error because btrfs check never checks
total_bytes of dev item.
However this is going to change.
Fix it by populating total_bytes of device item with the end position of
last dev extent to make later btrfs check happy.
Signed-off-by: Qu Wenruo <wqu@suse.com>
---
image/main.c | 48 +++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 45 insertions(+), 3 deletions(-)
diff --git a/image/main.c b/image/main.c
index 351c5a256938..d5b89bc3149f 100644
--- a/image/main.c
+++ b/image/main.c
@@ -2082,15 +2082,17 @@ static void remap_overlapping_chunks(struct mdrestore_struct *mdres)
}
static int fixup_devices(struct btrfs_fs_info *fs_info,
- struct mdrestore_struct *mdres, off_t dev_size)
+ struct mdrestore_struct *mdres, int out_fd)
{
struct btrfs_trans_handle *trans;
struct btrfs_dev_item *dev_item;
+ struct btrfs_dev_extent *dev_ext;
struct btrfs_path path;
struct extent_buffer *leaf;
struct btrfs_root *root = fs_info->chunk_root;
struct btrfs_key key;
u64 devid, cur_devid;
+ u64 dev_size; /* Get from last dev extents */
int ret;
trans = btrfs_start_transaction(fs_info->tree_root, 1);
@@ -2101,16 +2103,56 @@ static int fixup_devices(struct btrfs_fs_info *fs_info,
dev_item = &fs_info->super_copy->dev_item;
+ btrfs_init_path(&path);
devid = btrfs_stack_device_id(dev_item);
+ key.objectid = devid;
+ key.type = BTRFS_DEV_EXTENT_KEY;
+ key.offset = (u64)-1;
+
+ ret = btrfs_search_slot(NULL, fs_info->dev_root, &key, &path, 0, 0);
+ if (ret < 0) {
+ error("failed to locate last dev extent of devid %llu: %s",
+ devid, strerror(-ret));
+ btrfs_release_path(&path);
+ return ret;
+ }
+ if (ret == 0) {
+ error("found invalid dev extent devid %llu offset -1",
+ devid);
+ btrfs_release_path(&path);
+ return -EUCLEAN;
+ }
+ ret = btrfs_previous_item(fs_info->dev_root, &path, devid,
+ BTRFS_DEV_EXTENT_KEY);
+ if (ret > 0)
+ ret = -ENOENT;
+ if (ret < 0) {
+ error("failed to locate last dev extent of devid %llu: %s",
+ devid, strerror(-ret));
+ btrfs_release_path(&path);
+ return ret;
+ }
+
+ btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
+ dev_ext = btrfs_item_ptr(path.nodes[0], path.slots[0],
+ struct btrfs_dev_extent);
+ dev_size = key.offset + btrfs_dev_extent_length(path.nodes[0], dev_ext);
+ btrfs_release_path(&path);
+
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);
+ if (ret < 0) {
+ error("failed to enlarge result image: %s", strerror(errno));
+ return -errno;
+ }
key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
key.type = BTRFS_DEV_ITEM_KEY;
key.offset = 0;
- btrfs_init_path(&path);
again:
ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
@@ -2275,7 +2317,7 @@ static int restore_metadump(const char *input, FILE *out, int old_restore,
return 1;
}
- ret = fixup_devices(info, &mdrestore, st.st_size);
+ ret = fixup_devices(info, &mdrestore, fileno(out));
close_ctree(info->chunk_root);
if (ret)
goto out;
--
2.19.1
next prev parent reply other threads:[~2018-10-08 12:30 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-10-08 12:30 [PATCH v3 0/6] btrfs-progs: check: Detect invalid dev extents and device items Qu Wenruo
2018-10-08 12:30 ` Qu Wenruo [this message]
2018-10-11 12:07 ` [PATCH v3 1/6] btrfs-progs: image: Use correct device size when restoring Nikolay Borisov
2018-10-12 5:20 ` Qu Wenruo
2018-10-08 12:30 ` [PATCH v3 2/6] btrfs-progs: lowmem check: Add check for overlapping dev extents Qu Wenruo
2018-10-09 1:46 ` Su Yue
2018-10-08 12:30 ` [PATCH v3 3/6] btrfs-progs: original check: Add ability to detect bad " Qu Wenruo
2018-10-09 2:01 ` Su Yue
2018-10-08 12:30 ` [PATCH v3 4/6] btrfs-progs: lowmem check: Add dev_item check for used bytes and total bytes Qu Wenruo
2018-10-08 22:20 ` Hans van Kranenburg
2018-10-09 1:14 ` Qu Wenruo
2018-10-09 20:21 ` Hans van Kranenburg
2018-10-08 12:30 ` [PATCH v3 5/6] btrfs-progs: original " Qu Wenruo
2018-10-08 12:30 ` [PATCH v3 6/6] btrfs-progs: fsck-tests: Add test image for dev extents beyond device boundary Qu Wenruo
2018-12-17 18:55 ` [PATCH v3 0/6] btrfs-progs: check: Detect invalid dev extents and device items 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=20181008123044.13413-2-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).