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 <[email protected]> --- btrfs-select-super.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) 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) { -- 1.8.1.4 -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
