Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp13471994ybl; Sun, 29 Dec 2019 12:43:12 -0800 (PST) X-Google-Smtp-Source: APXvYqyfb9EQzYPMuk2Y5owLuDJMuUX9Izw0Vv1Qc7gFWZ4EZ1M2rn3TwXVrCIqM5ENcCgm0j3jl X-Received: by 2002:a9d:6745:: with SMTP id w5mr55412475otm.52.1577652192634; Sun, 29 Dec 2019 12:43:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1577652192; cv=none; d=google.com; s=arc-20160816; b=pvtc+HzqePzsJY1NQvUAAD2vYmZb3ESSB2MhgdoVhVBAoCBwSS8YeLqyx1qawhbQTW AuBNfklkUmNad1TFc32uFsKwBBwZbDXpS+/trLsTC/z/RAbL80rw3JJUZjV6OS+T279+ dbwd4kCleOIjJCQ0Y/PEiQpXq2hThZ82toGUe3KKmSU7mFCm1YozPDcqYS3hiTisj/nW nz3rg0zFOziWyYIMQG/R7xLr5qOm2yLvkEALZWOAzzkelnjMASnhHH+U3fjR0nbqyWke P/TkGOzwGPITsAuz4hInZ+D1/+EIL1YlXqh87HMRJgBPaNGaA76mItxYwT3vvrT3OwVG FXMw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=jQdZ7XgmUmpRFeuesaPJI9x/tFBEkCrN3K4PARSy5wI=; b=gHndwzIydEySmW4U6bS3qPkSebaURLF3JQFsAX86PafebbzNTG7oLtT8StyCYLLTSJ TdDOF0KPX9MhA7JHlPUCegGKDdP3WQrAgByFvfIi9xj9i0Coh1tLjkSAdDBUQ6gsePat HAnFfkcW7s7N/S5IXILLnG1CRP1XP80LM8OLGUK2RpOoXsogGjcwNjYLyylwGtogAbZx e10cX+iF77pN6dK+WWuK6IiquPv695J0nDNGrsXsak0sqIZzRk+5nBoyplY9H1LQO03w BSTrKUw2lkAlR6uZNQnKNG8a7BwrBVJ+0PpsmKrXlTaglAanfZBbN9SbJ55Kj8Sb8oau 4dPQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=fZaVAxK4; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r2si9497249oif.83.2019.12.29.12.43.01; Sun, 29 Dec 2019 12:43:12 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=fZaVAxK4; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730335AbfL2Rmk (ORCPT + 99 others); Sun, 29 Dec 2019 12:42:40 -0500 Received: from mail.kernel.org ([198.145.29.99]:48824 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730304AbfL2Rmb (ORCPT ); Sun, 29 Dec 2019 12:42:31 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (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 41DEF207FD; Sun, 29 Dec 2019 17:42:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1577641350; bh=aVGBNafn8ccvV1/KO7hDblzXPn28cCskNHJE9l2KE2o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fZaVAxK4qe8DUwYt57yNWinwY/Vej2xYbmL5BzG0inlfNW7fyDjIAfumQmpoulQ/4 ipYpHyvAvW2KDf3TIG0pd6/RFGcfQiCHTVoDx5s2nbRggBoFMvD+DHYvVFY7KZuWeb c1ZNIZ7gQmUBc6Vgh0QQ+Aed/ehvUf6Wh5aw7Cqc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Filipe Manana , David Sterba Subject: [PATCH 5.4 030/434] Btrfs: make tree checker detect checksum items with overlapping ranges Date: Sun, 29 Dec 2019 18:21:23 +0100 Message-Id: <20191229172704.048754998@linuxfoundation.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20191229172702.393141737@linuxfoundation.org> References: <20191229172702.393141737@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Filipe Manana commit ad1d8c439978ede77cbf73cbdd11bafe810421a5 upstream. Having checksum items, either on the checksums tree or in a log tree, that represent ranges that overlap each other is a sign of a corruption. Such case confuses the checksum lookup code and can result in not being able to find checksums or find stale checksums. So add a check for such case. This is motivated by a recent fix for a case where a log tree had checksum items covering ranges that overlap each other due to extent cloning, and resulted in missing checksums after replaying the log tree. It also helps detect past issues such as stale and outdated checksums due to overlapping, commit 27b9a8122ff71a ("Btrfs: fix csum tree corruption, duplicate and outdated checksums"). CC: stable@vger.kernel.org # 4.4+ Signed-off-by: Filipe Manana Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/tree-checker.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) --- a/fs/btrfs/tree-checker.c +++ b/fs/btrfs/tree-checker.c @@ -243,7 +243,7 @@ static int check_extent_data_item(struct } static int check_csum_item(struct extent_buffer *leaf, struct btrfs_key *key, - int slot) + int slot, struct btrfs_key *prev_key) { struct btrfs_fs_info *fs_info = leaf->fs_info; u32 sectorsize = fs_info->sectorsize; @@ -267,6 +267,20 @@ static int check_csum_item(struct extent btrfs_item_size_nr(leaf, slot), csumsize); return -EUCLEAN; } + if (slot > 0 && prev_key->type == BTRFS_EXTENT_CSUM_KEY) { + u64 prev_csum_end; + u32 prev_item_size; + + prev_item_size = btrfs_item_size_nr(leaf, slot - 1); + prev_csum_end = (prev_item_size / csumsize) * sectorsize; + prev_csum_end += prev_key->offset; + if (prev_csum_end > key->offset) { + generic_err(leaf, slot - 1, +"csum end range (%llu) goes beyond the start range (%llu) of the next csum item", + prev_csum_end, key->offset); + return -EUCLEAN; + } + } return 0; } @@ -1239,7 +1253,7 @@ static int check_leaf_item(struct extent ret = check_extent_data_item(leaf, key, slot, prev_key); break; case BTRFS_EXTENT_CSUM_KEY: - ret = check_csum_item(leaf, key, slot); + ret = check_csum_item(leaf, key, slot, prev_key); break; case BTRFS_DIR_ITEM_KEY: case BTRFS_DIR_INDEX_KEY: