Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp823818pxb; Tue, 3 Nov 2020 13:34:31 -0800 (PST) X-Google-Smtp-Source: ABdhPJwHSulZWLOyFEIVJ5+SdR4ceiFRO05Lz6RtEpb06SzK7f0eM8i58rBlon+aIloui7PmQwyD X-Received: by 2002:a17:906:cede:: with SMTP id si30mr11090749ejb.16.1604439271450; Tue, 03 Nov 2020 13:34:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604439271; cv=none; d=google.com; s=arc-20160816; b=yyA6aYNkRBq9JdDHKZVKYJwuk6jjTz12v1ao4eEGyLFCCvYo3XgGUaQK2QZlJ2jjyQ Pa1LjnEP4Eu6+OTBhnto1vUv3PLCPZ7vc9HkQJys2E4h1BKtuxjTJTxfI21kjioRCNTH uUTwmjJ1GuUUr9hskGAxeFTDPbY9XHaML7wb9YpEmAuhaTiaISwuIlXvGctLhHLp80CB svLd+t1fQ+5uuISkJ6ktY8Ge/ANGX1Se/G6KmhDN/ukWoBp1DOJOTZKNBZeIyLh9PW/I P//jn5FxOSwI5VB486UEOP1BMdX3O17GRuihaZNqKqcEuc66cnA4hFCWnvQh/3KROtck y43A== 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=53hgYDdwsMLvuC2/0ekd0SsDcoTaKTxChA37LUdFkKU=; b=RvHarK1x9cU0guE6dJWbNH+OaCEvxtv1VViFbmdlCye2UqpD/2oHpIhjobgffImxav ummkqi1iBc6W5AVguoX7GV/dHwBZrIKD+LKbjuPEiS8zVaY3XaYiaptqlmDKMtLj98fJ BuPOGrHbuNntU0yyfzkkaPYvRNbHh0rW4PhIxM86rm1+HCmsIKQcufZyOY/T8hYBlTlU l4JkYBLX3bIuYM1TLvWpvtCbOmHOMb0dHO67n6LVXV8KTVGIU3TUflYClgXBgyvwZqfX quUx1ZP/qi0j5/t6lONEtYhdHsyq195tHOimUNp41h0FQCrDHBdkkXCh8nLSW3HxDpKs m39A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="QMVV/1R8"; 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 d10si4537152edo.243.2020.11.03.13.34.08; Tue, 03 Nov 2020 13:34:31 -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="QMVV/1R8"; 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 S1732863AbgKCVbi (ORCPT + 99 others); Tue, 3 Nov 2020 16:31:38 -0500 Received: from mail.kernel.org ([198.145.29.99]:59986 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732219AbgKCU5g (ORCPT ); Tue, 3 Nov 2020 15:57:36 -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 E77D22053B; Tue, 3 Nov 2020 20:57:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604437055; bh=6k1PCzL6AZQeW2dSgd0OaDlVHBN/yvSzAsbn/HrF0tA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QMVV/1R8jjofidXc44KfP/vxOSPHaiyJQ33T6IjCkkItBKHLlyvazBkW3w6o757JN qYfZjsDb1A+OFJ+WxLRQ7y5NkuO+Miz8qXjHFIbDbMZO3cSoAcWIENfbKc/7pDQ0aJ aFmVUqKbJRQF3mEnEZfTbzGrC+DGTfFPnYVsXSi0= 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.4 128/214] btrfs: tree-checker: fix false alert caused by legacy btrfs root item Date: Tue, 3 Nov 2020 21:36:16 +0100 Message-Id: <20201103203302.843409963@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201103203249.448706377@linuxfoundation.org> References: <20201103203249.448706377@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 @@ -869,7 +869,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; @@ -889,14 +889,21 @@ static int check_root_item(struct extent return -EUCLEAN; } - 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 @@ -651,6 +656,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 {