From patchwork Mon May 15 08:27:39 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 9726365 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 5512F60387 for ; Mon, 15 May 2017 08:28:15 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 431702894F for ; Mon, 15 May 2017 08:28:15 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 382EE28972; Mon, 15 May 2017 08:28:15 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 98E1728970 for ; Mon, 15 May 2017 08:28:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1760008AbdEOI2N (ORCPT ); Mon, 15 May 2017 04:28:13 -0400 Received: from cn.fujitsu.com ([59.151.112.132]:33782 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1759996AbdEOI2M (ORCPT ); Mon, 15 May 2017 04:28:12 -0400 X-IronPort-AV: E=Sophos;i="5.22,518,1449504000"; d="scan'208";a="18851805" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 15 May 2017 16:27:47 +0800 Received: from G08CNEXCHPEKD02.g08.fujitsu.local (unknown [10.167.33.83]) by cn.fujitsu.com (Postfix) with ESMTP id 4DF6147C6770; Mon, 15 May 2017 16:27:45 +0800 (CST) Received: from localhost.localdomain (10.167.226.34) by G08CNEXCHPEKD02.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.319.2; Mon, 15 May 2017 16:27:48 +0800 From: Qu Wenruo To: , CC: Subject: [PATCH 2/5] btrfs-progs: Enhance chunk item validation check Date: Mon, 15 May 2017 16:27:39 +0800 Message-ID: <20170515082742.21363-2-quwenruo@cn.fujitsu.com> X-Mailer: git-send-email 2.12.2 In-Reply-To: <20170515082742.21363-1-quwenruo@cn.fujitsu.com> References: <20170515082742.21363-1-quwenruo@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.226.34] X-yoursite-MailScanner-ID: 4DF6147C6770.AEA40 X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: quwenruo@cn.fujitsu.com Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP btrfs_check_chunk_valid() doesn't check if 1) chunk flag has conflicting flags For example chunk type DATA|METADATA|RAID1|RAID10 is completely invalid, while current check_chunk_valid() can't detect it. 2) num_stripes is invalid for RAID10 Num_stripes 5 is not valid for RAID10. This patch will enhance btrfs_check_chunk_valid() to handle above cases. Signed-off-by: Qu Wenruo --- volumes.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/volumes.c b/volumes.c index 62e23aee..1e352869 100644 --- a/volumes.c +++ b/volumes.c @@ -1725,6 +1725,19 @@ int btrfs_check_chunk_valid(struct btrfs_root *root, BTRFS_BLOCK_GROUP_PROFILE_MASK) & type); return -EIO; } + if (!(type & BTRFS_BLOCK_GROUP_TYPE_MASK)) { + error("missing chunk type flag: %llu", type); + return -EIO; + } + if (!is_power_of_2(type & BTRFS_BLOCK_GROUP_PROFILE_MASK)) { + error("conflicting chunk type detected: %llu", type); + return -EIO; + } + if ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) && + !is_power_of_2(type & BTRFS_BLOCK_GROUP_PROFILE_MASK)) { + error("conflicting chunk profile detected: %llu", type); + return -EIO; + } chunk_ondisk_size = btrfs_chunk_item_size(num_stripes); /* @@ -1741,7 +1754,8 @@ int btrfs_check_chunk_valid(struct btrfs_root *root, /* * Device number check against profile */ - if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes == 0) || + if ((type & BTRFS_BLOCK_GROUP_RAID10 && (sub_stripes != 2 || + !IS_ALIGNED(num_stripes, sub_stripes))) || (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) || (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) || (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) ||