From: Qu Wenruo <wqu@suse.com>
To: linux-btrfs@vger.kernel.org
Cc: dsterba@suse.cz
Subject: [PATCH v2 2/6] btrfs-progs: Introduce function to fix super block total bytes
Date: Tue, 17 Oct 2017 17:13:08 +0800 [thread overview]
Message-ID: <20171017091312.31045-3-wqu@suse.com> (raw)
In-Reply-To: <20171017091312.31045-1-wqu@suse.com>
Recent kernel (starting from v4.6) will refuse to mount if super block
total bytes is smaller than all devices' size.
This makes end user unable to do anything to their otherwise quite
healthy fs.
To fix such problem, introduce repair function in btrfs-progs to fix it
offline.
Reported-by: Asif Youssuff <yoasif@gmail.com>
Reported-by: Rich Rauenzahn <rrauenza@gmail.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
---
volumes.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
volumes.h | 1 +
2 files changed, 53 insertions(+)
diff --git a/volumes.c b/volumes.c
index 5462bf71c611..c86f8a931742 100644
--- a/volumes.c
+++ b/volumes.c
@@ -2403,3 +2403,55 @@ err:
btrfs_release_path(&path);
return ret;
}
+
+/*
+ * Return 0 if super block total bytes matches with all devices' total_bytes
+ * Return >0 if super block total bytes mismatch but fixed without problem
+ * Return <0 if we failed to fix super block total_bytes
+ */
+int btrfs_fix_super_size(struct btrfs_fs_info *fs_info)
+{
+ struct btrfs_trans_handle *trans;
+ struct btrfs_device *device;
+ struct list_head *dev_list = &fs_info->fs_devices->devices;
+ u64 total_bytes = 0;
+ u64 old_bytes = btrfs_super_total_bytes(fs_info->super_copy);
+ int ret;
+
+ list_for_each_entry(device, dev_list, dev_list) {
+ /*
+ * Caller should ensure this function is called after aligning
+ * all devices' total_bytes.
+ */
+ if (!IS_ALIGNED(device->total_bytes, fs_info->sectorsize)) {
+ error("device %llu total_bytes %llu not aligned to %u",
+ device->devid, device->total_bytes,
+ fs_info->sectorsize);
+ return -EUCLEAN;
+ }
+ total_bytes += device->total_bytes;
+ }
+
+ if (total_bytes == old_bytes)
+ return 0;
+
+ btrfs_set_super_total_bytes(fs_info->super_copy, total_bytes);
+
+ /* Commit transaction to update all super blocks */
+ trans = btrfs_start_transaction(fs_info->tree_root, 1);
+ if (IS_ERR(trans)) {
+ ret = PTR_ERR(trans);
+ error("error starting transaction: %d (%s)",
+ ret, strerror(-ret));
+ return ret;
+ }
+ ret = btrfs_commit_transaction(trans, fs_info->tree_root);
+ if (ret < 0) {
+ error("failed to commit current transaction: %d (%s)",
+ ret, strerror(-ret));
+ return ret;
+ }
+ printf("Fixed super total bytes, old size: %llu new size: %llu\n",
+ old_bytes, total_bytes);
+ return 1;
+}
diff --git a/volumes.h b/volumes.h
index 89bd4aa81ae0..d5bb5f8663a3 100644
--- a/volumes.h
+++ b/volumes.h
@@ -247,4 +247,5 @@ u64 btrfs_stripe_length(struct btrfs_fs_info *fs_info,
struct btrfs_chunk *chunk);
int btrfs_fix_device_size(struct btrfs_fs_info *fs_info,
struct btrfs_device *device);
+int btrfs_fix_super_size(struct btrfs_fs_info *fs_info);
#endif
--
2.14.2
next prev parent reply other threads:[~2017-10-17 9:13 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-10-17 9:13 [PATCH v2 0/6] Btrfs-progs: rescue: To fix device related Qu Wenruo
2017-10-17 9:13 ` [PATCH v2 1/6] btrfs-progs: Introduce function to fix unaligned device size Qu Wenruo
2017-10-17 9:13 ` Qu Wenruo [this message]
2017-10-17 9:13 ` [PATCH v2 3/6] btrfs-progs: rescue: Introduce fix-device-size Qu Wenruo
2017-10-17 9:13 ` [PATCH v2 4/6] btrfs-progs: check: Also check and repair unalignment/mismatch device and super size Qu Wenruo
2017-10-17 9:13 ` [PATCH v2 5/6] btrfs-progs: test/fsck: Add test case image for --fix-dev-size Qu Wenruo
2017-10-17 9:13 ` [PATCH v2 6/6] btrfs-progs: rescue: Fix zero-log mounted branch Qu Wenruo
2017-10-26 18:13 ` David Sterba
2017-10-17 13:05 ` [PATCH v2 0/6] Btrfs-progs: rescue: To fix device related Nikolay Borisov
2017-10-27 16:16 ` 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=20171017091312.31045-3-wqu@suse.com \
--to=wqu@suse.com \
--cc=dsterba@suse.cz \
--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).