Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp2038080imm; Thu, 24 May 2018 04:57:08 -0700 (PDT) X-Google-Smtp-Source: AB8JxZo+WE8SwSUU8vaCjWWZ1DKxTv+NE9DHNrPwnr8vrI77s11Dw4cU3g9Pcc+8SuDb5HOj4m6E X-Received: by 2002:a63:77c9:: with SMTP id s192-v6mr1234038pgc.140.1527163028175; Thu, 24 May 2018 04:57:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527163028; cv=none; d=google.com; s=arc-20160816; b=FtJC8koB/0DwMpALp2llt8zx8gO7wgh/HkWtNTiBZN1M4mMNfh893wOiy6uZvVVcgS C61K/gyimwHFHitfFTUsz/O7DeqcuZYok/6eihSKElERJCJvBV7V9XGfVITfFs+l1oUo dgg9yZBbKSbkwsy0uasKVqXRRle1MrPEcIZDQwTy2BsVQXHTefEWd5J4fGMfPOTeTy0F AsfiTHyDlTHtePmV8ekNEPN7sPw3i1vcNeP8mYdVSLiTt1DQokAZ2FAPGIS70GiKDkEx NAJZnsK/sVUjXQ6c40WLZ4DeYLkkoNOM5byMHV1gsHeBuUCoWvYDU5CRQT+sPR3SbP1H LmaQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=Wk+Jhwg//dMHFIzN6I397rHbq2eqowoXbK6SwFrD5FE=; b=TIq5fAldrOJafLd2fsNyMzWIb6XrBBYJ627UzuzltvlZF31b8D4cHSKLsUFnxO1XsH 01gZxcGEfzFCj7gsB2XHUSsOw+/iUMXinQ6kBQp/kCRjYsd9WQDhbbLQGB9XbZjwQYJF dAPod0Y5eX46XEQgzg+fAJizy0+KrChnaB6x96S5FZBtp/gttLx3DtgT/Uq30kKnH8Z9 rZv6Yz+un3nE6m0AnpNzbw4nme8Gbmg1YTCwVjg8XXUpHl30euQXEH5yPB2X5036kDXK +aVX7MiTNLMMl+bCw/re6/WvXCv+rioqf5BmD/+OoBwSc+hZhVcMTxNcxptbARdQ5CxU YGag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=01Lfoi5H; 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 r14-v6si20998738pfa.296.2018.05.24.04.56.53; Thu, 24 May 2018 04:57:08 -0700 (PDT) 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=01Lfoi5H; 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 S966838AbeEXJpr (ORCPT + 99 others); Thu, 24 May 2018 05:45:47 -0400 Received: from mail.kernel.org ([198.145.29.99]:56876 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S966818AbeEXJpn (ORCPT ); Thu, 24 May 2018 05:45:43 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) (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 2E5D4208C3; Thu, 24 May 2018 09:45:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1527155142; bh=tVT950vrMKAY7C96eDDlSgKt6ducQhodmTvk9RFdTwA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=01Lfoi5HRWfujlq/hMUjqWmZyh+2iz5kGachh4CPpGUrX3LBmG46jszOdqNtEzkQJ KOSwkQo6SrHLZnWGJZL/NO22+okiXKMgPTfn8bN/JJodNJ/Xcg3ZyfFi66EfcY0p94 mkkj2bxDqP5G+56dL5ad22bCMa1CaBrgGIVGi+A0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Liu Bo , Filipe Manana , Qu Wenruo , David Sterba , Nikolay Borisov Subject: [PATCH 4.4 70/92] btrfs: fix reading stale metadata blocks after degraded raid1 mounts Date: Thu, 24 May 2018 11:38:47 +0200 Message-Id: <20180524093206.062830692@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180524093159.286472249@linuxfoundation.org> References: <20180524093159.286472249@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.4-stable review patch. If anyone has any objections, please let me know. ------------------ From: Liu Bo commit 02a3307aa9c20b4f6626255b028f07f6cfa16feb upstream. If a btree block, aka. extent buffer, is not available in the extent buffer cache, it'll be read out from the disk instead, i.e. btrfs_search_slot() read_block_for_search() # hold parent and its lock, go to read child btrfs_release_path() read_tree_block() # read child Unfortunately, the parent lock got released before reading child, so commit 5bdd3536cbbe ("Btrfs: Fix block generation verification race") had used 0 as parent transid to read the child block. It forces read_tree_block() not to check if parent transid is different with the generation id of the child that it reads out from disk. A simple PoC is included in btrfs/124, 0. A two-disk raid1 btrfs, 1. Right after mkfs.btrfs, block A is allocated to be device tree's root. 2. Mount this filesystem and put it in use, after a while, device tree's root got COW but block A hasn't been allocated/overwritten yet. 3. Umount it and reload the btrfs module to remove both disks from the global @fs_devices list. 4. mount -odegraded dev1 and write some data, so now block A is allocated to be a leaf in checksum tree. Note that only dev1 has the latest metadata of this filesystem. 5. Umount it and mount it again normally (with both disks), since raid1 can pick up one disk by the writer task's pid, if btrfs_search_slot() needs to read block A, dev2 which does NOT have the latest metadata might be read for block A, then we got a stale block A. 6. As parent transid is not checked, block A is marked as uptodate and put into the extent buffer cache, so the future search won't bother to read disk again, which means it'll make changes on this stale one and make it dirty and flush it onto disk. To avoid the problem, parent transid needs to be passed to read_tree_block(). In order to get a valid parent transid, we need to hold the parent's lock until finishing reading child. This patch needs to be slightly adapted for stable kernels, the &first_key parameter added to read_tree_block() is from 4.16+ (581c1760415c4). The fix is to replace 0 by 'gen'. Fixes: 5bdd3536cbbe ("Btrfs: Fix block generation verification race") CC: stable@vger.kernel.org # 4.4+ Signed-off-by: Liu Bo Reviewed-by: Filipe Manana Reviewed-by: Qu Wenruo [ update changelog ] Signed-off-by: David Sterba Signed-off-by: Nikolay Borisov Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/ctree.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) --- a/fs/btrfs/ctree.c +++ b/fs/btrfs/ctree.c @@ -2497,10 +2497,8 @@ read_block_for_search(struct btrfs_trans if (p->reada) reada_for_search(root, p, level, slot, key->objectid); - btrfs_release_path(p); - ret = -EAGAIN; - tmp = read_tree_block(root, blocknr, 0); + tmp = read_tree_block(root, blocknr, gen); if (!IS_ERR(tmp)) { /* * If the read above didn't mark this buffer up to date, @@ -2512,6 +2510,8 @@ read_block_for_search(struct btrfs_trans ret = -EIO; free_extent_buffer(tmp); } + + btrfs_release_path(p); return ret; }