From patchwork Thu Jul 3 02:06:35 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gui Hecheng X-Patchwork-Id: 4468951 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 64B21BEEAA for ; Thu, 3 Jul 2014 02:12:20 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 920482037F for ; Thu, 3 Jul 2014 02:12:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id C0B8F20304 for ; Thu, 3 Jul 2014 02:12:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754642AbaGCCMK (ORCPT ); Wed, 2 Jul 2014 22:12:10 -0400 Received: from cn.fujitsu.com ([59.151.112.132]:53247 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1754578AbaGCCMI (ORCPT ); Wed, 2 Jul 2014 22:12:08 -0400 X-IronPort-AV: E=Sophos;i="5.00,823,1396972800"; d="scan'208";a="32766363" Received: from unknown (HELO edo.cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 03 Jul 2014 10:09:24 +0800 Received: from G08CNEXCHPEKD01.g08.fujitsu.local (localhost.localdomain [127.0.0.1]) by edo.cn.fujitsu.com (8.14.3/8.13.1) with ESMTP id s632C5e6013412 for ; Thu, 3 Jul 2014 10:12:05 +0800 Received: from localhost.localdomain (10.167.226.111) by G08CNEXCHPEKD01.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.181.6; Thu, 3 Jul 2014 10:12:16 +0800 From: Gui Hecheng To: CC: Gui Hecheng Subject: [PATCH] btrfs-progs: prevent select invalid dev super after dev replace Date: Thu, 3 Jul 2014 10:06:35 +0800 Message-ID: <1404353196-10914-3-git-send-email-guihc.fnst@cn.fujitsu.com> X-Mailer: git-send-email 1.8.1.4 In-Reply-To: <1404353196-10914-1-git-send-email-guihc.fnst@cn.fujitsu.com> References: <1404353196-10914-1-git-send-email-guihc.fnst@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.226.111] Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 --- 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) {