Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp835691pxb; Tue, 3 Nov 2020 13:57:25 -0800 (PST) X-Google-Smtp-Source: ABdhPJxMmm+C8TWfCneLomdFEDxphaHB4+IWiToqtZARu3lYYY16YQvXxgCdpGfJGlTCJhyKuQGt X-Received: by 2002:a17:906:519b:: with SMTP id y27mr20916957ejk.25.1604440645167; Tue, 03 Nov 2020 13:57:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604440645; cv=none; d=google.com; s=arc-20160816; b=A8UuiOqZoFjJkU+IVgtWfPXumiheBsZ2/vXecpNrB/W0p14xQofmlyG3eEyncttj97 /tcInwcN2Ra57AcIGUHpkEOe/yRdJsi8Bi6S9BG9GbXJLlD6fJ49i5O876o1zMUdsvDb y8IGAUnEDFA2VEGyM4migy9L7+PPjadw8wYHOyZv+b7vV43Dcd7QclrHbHfoDWewhCBC Bdeoozwq0TrpOkTtT5idtwuwMDEXr/Fg7cJMM+aMa23HNDHH1PXSMWznG2gogr04I9XV zEmqfvjvftDw2E10GvTpNYZQAW4yRdUQOCdJaBf0PyU/6uVk4zysd9emQkdcrheepdQu Q1Vw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=V712qtRl4VkO2oADzitmlH8Zs27VZaD3e86CIsPNREg=; b=Q7MOSsYjUZLOE7uZ7/3Y8hIQy/7sbT72Ek076moecc31NI6UPCUAxTFCphIKDTTho6 P8olzl1egBVmAua6gm8cGkOQzZpcyIGUGdfAVu+sOrZ2Ghwm8WELzI2K0m+aVAQcOCf6 0clN2H/eOI+ylNre9ShC419Nqpq3Sp4ylG65/31wjcPlyvmGpGpGsKz0Z54+qzujcMxO 2ocy/rvEW23u6wuKWe9OUTGRyx3pVNdDH92+IxfYl/ZtfOXFnvKCeYQys29y/APmR/aD pMJm/y/l+k7aO6bYR4syQJFvFglE0wVLR3/sA1VKWju9vZyOgZ320K+rxKzTZluXRRy9 7rpQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Ju+HHvdw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v4si11151814edl.447.2020.11.03.13.57.02; Tue, 03 Nov 2020 13:57:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Ju+HHvdw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731578AbgKCVxJ (ORCPT + 99 others); Tue, 3 Nov 2020 16:53:09 -0500 Received: from mail.kernel.org ([198.145.29.99]:35908 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730445AbgKCUqa (ORCPT ); Tue, 3 Nov 2020 15:46:30 -0500 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 309AD223FD; Tue, 3 Nov 2020 20:46:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604436389; bh=suf4uR6m2goLtvNXLbQiDTKtTkkFSuWPwkDjO2wOHZ0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ju+HHvdwmeB6kjitRETszpJtXdOZBgcZWzjVNsX/iSls1CrbyQkcaezt8iFcg+Z6O uuE2IsKEg95wDloJfJv+6tBF+pJi9qVrHFhsO85rFTIWOShSYICq78rvRA+osYd82j iZdH14vkv+P/zz/qK7pRZQAQ+kPOTcSvzAFWh2SY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Martin Steigerwald , Josef Bacik , Qu Wenruo , David Sterba Subject: [PATCH 5.9 230/391] btrfs: tree-checker: fix false alert caused by legacy btrfs root item Date: Tue, 3 Nov 2020 21:34:41 +0100 Message-Id: <20201103203402.503130926@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201103203348.153465465@linuxfoundation.org> References: <20201103203348.153465465@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Qu Wenruo commit 1465af12e254a68706e110846f59cf0f09683184 upstream. Commit 259ee7754b67 ("btrfs: tree-checker: Add ROOT_ITEM check") introduced btrfs root item size check, however btrfs root item has two versions, the legacy one which just ends before generation_v2 member, is smaller than current btrfs root item size. This caused btrfs kernel to reject valid but old tree root leaves. Fix this problem by also allowing legacy root item, since kernel can already handle them pretty well and upgrade to newer root item format when needed. Reported-by: Martin Steigerwald Fixes: 259ee7754b67 ("btrfs: tree-checker: Add ROOT_ITEM check") CC: stable@vger.kernel.org # 5.4+ Tested-By: Martin Steigerwald Reviewed-by: Josef Bacik Signed-off-by: Qu Wenruo Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/tree-checker.c | 17 ++++++++++++----- include/uapi/linux/btrfs_tree.h | 14 ++++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) --- a/fs/btrfs/tree-checker.c +++ b/fs/btrfs/tree-checker.c @@ -1035,7 +1035,7 @@ static int check_root_item(struct extent int slot) { struct btrfs_fs_info *fs_info = leaf->fs_info; - struct btrfs_root_item ri; + struct btrfs_root_item ri = { 0 }; const u64 valid_root_flags = BTRFS_ROOT_SUBVOL_RDONLY | BTRFS_ROOT_SUBVOL_DEAD; int ret; @@ -1044,14 +1044,21 @@ static int check_root_item(struct extent if (ret < 0) return ret; - if (btrfs_item_size_nr(leaf, slot) != sizeof(ri)) { + if (btrfs_item_size_nr(leaf, slot) != sizeof(ri) && + btrfs_item_size_nr(leaf, slot) != btrfs_legacy_root_item_size()) { generic_err(leaf, slot, - "invalid root item size, have %u expect %zu", - btrfs_item_size_nr(leaf, slot), sizeof(ri)); + "invalid root item size, have %u expect %zu or %u", + btrfs_item_size_nr(leaf, slot), sizeof(ri), + btrfs_legacy_root_item_size()); } + /* + * For legacy root item, the members starting at generation_v2 will be + * all filled with 0. + * And since we allow geneartion_v2 as 0, it will still pass the check. + */ read_extent_buffer(leaf, &ri, btrfs_item_ptr_offset(leaf, slot), - sizeof(ri)); + btrfs_item_size_nr(leaf, slot)); /* Generation related */ if (btrfs_root_generation(&ri) > --- a/include/uapi/linux/btrfs_tree.h +++ b/include/uapi/linux/btrfs_tree.h @@ -4,6 +4,11 @@ #include #include +#ifdef __KERNEL__ +#include +#else +#include +#endif /* * This header contains the structure definitions and constants used @@ -645,6 +650,15 @@ struct btrfs_root_item { } __attribute__ ((__packed__)); /* + * Btrfs root item used to be smaller than current size. The old format ends + * at where member generation_v2 is. + */ +static inline __u32 btrfs_legacy_root_item_size(void) +{ + return offsetof(struct btrfs_root_item, generation_v2); +} + +/* * this is used for both forward and backward root refs */ struct btrfs_root_ref {