diff mbox

btrfs-progs: prevent select invalid dev super after dev replace

Message ID 1404353196-10914-3-git-send-email-guihc.fnst@cn.fujitsu.com (mailing list archive)
State Not Applicable
Headers show

Commit Message

Gui Hecheng July 3, 2014, 2:06 a.m. UTC
After dev replace, we should not select the superblock of the
replaced dev. Otherwise, all the superblokcs will be overwritten
by this invalid superblock.

To prevent this case, let btrfs-select-super check the first
superblock on the selected dev. If the magic doesn't match,
then the dev is a replaced dev and error message will show up.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
---
 btrfs-select-super.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

Comments

David Sterba July 3, 2014, 6:10 p.m. UTC | #1
On Thu, Jul 03, 2014 at 10:06:35AM +0800, Gui Hecheng wrote:
> After dev replace, we should not select the superblock of the
> replaced dev. Otherwise, all the superblokcs will be overwritten
> by this invalid superblock.
> 
> To prevent this case, let btrfs-select-super check the first
> superblock on the selected dev. If the magic doesn't match,
> then the dev is a replaced dev and error message will show up.

Is this patch needed if Qu's superblock checksum patches are applied?
Thanks.
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Gui Hecheng July 4, 2014, 1:09 a.m. UTC | #2
On Thu, 2014-07-03 at 20:10 +0200, David Sterba wrote:
> On Thu, Jul 03, 2014 at 10:06:35AM +0800, Gui Hecheng wrote:
> > After dev replace, we should not select the superblock of the
> > replaced dev. Otherwise, all the superblokcs will be overwritten
> > by this invalid superblock.
> > 
> > To prevent this case, let btrfs-select-super check the first
> > superblock on the selected dev. If the magic doesn't match,
> > then the dev is a replaced dev and error message will show up.
> 
> Is this patch needed if Qu's superblock checksum patches are applied?
> Thanks.
Hmm...I think it is not neccessary then, please *ignore* this one.
Thanks, David.

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/btrfs-select-super.c b/btrfs-select-super.c
index d7cd187..cba3414 100644
--- a/btrfs-select-super.c
+++ b/btrfs-select-super.c
@@ -45,6 +45,9 @@  int main(int ac, char **av)
 	int ret;
 	u64 num = 0;
 	u64 bytenr = 0;
+	int fd;
+	u8 buf[BTRFS_SUPER_INFO_SIZE];
+	struct btrfs_super_block *sb = (struct btrfs_super_block *)buf;
 
 	while(1) {
 		int c;
@@ -86,6 +89,26 @@  int main(int ac, char **av)
 		return -EBUSY;
 	}
 
+	/*
+	 * After dev replace, the first super block of replaced dev will be cleared,
+	 * don't select that dev.
+	 */
+	fd = open(av[optind], O_RDONLY, 0666);
+	if (fd < 0)
+		return -errno;
+
+	ret = pread64(fd, buf, sizeof(buf), BTRFS_SUPER_INFO_OFFSET);
+	if (ret < sizeof(buf)) {
+		close(fd);
+		return -errno;
+	}
+
+	if (sb->magic != cpu_to_le64(BTRFS_MAGIC)) {
+		fprintf(stderr, "Cannot select an invalid super block.\n");
+		close(fd);
+		return 1;
+	}
+
 	root = open_ctree(av[optind], bytenr, 1);
 
 	if (!root) {