Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp7237pxb; Wed, 24 Feb 2021 16:29:36 -0800 (PST) X-Google-Smtp-Source: ABdhPJwTuEp2z+FlO801hg+gZYjlG9c3C3Dn5+eHtxik61S7V9apKebUSkjy4GcWPS2SFf6cGDIS X-Received: by 2002:a17:906:dff4:: with SMTP id lc20mr225833ejc.290.1614212976351; Wed, 24 Feb 2021 16:29:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614212976; cv=none; d=google.com; s=arc-20160816; b=Ssf1QfkPhOYjE5ytW44WqQMqge4oUQKfDBi+9eQQiv2o1WwGEC8hnP3dg1/4368aoG msCcqvjrDC7Iv2JZM4/xu0Lqx+5rmznK4mUYmuYn9KZa1vPgwBWO3obG+qAvrg3wu8lr I0wDbecqDp04SD3WGnSEx9BSXlNgSARTtf8CRIhTrOd/QhgntwvvPwhjJS/sOU/HcDPk 9KJrjVoRWrwRKxo9/b2p594DK3/DIMId+DMIRhYpNWxunPpspSywIT654Shpd+4Zbvol UxXBfTiAtNO0ij9XDdbKjW2zfzAY3NCGrlgbPocxyJHnWrSqvC8ksLudWSLWafUM6rC4 WaZQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=mNvuYvpMNcTlwPZfzfvrr2YebW1+GQKaoW7XynVs3As=; b=hCWMAfuLxrFSNZeEL8Ws2QC87bb4jCDWXPsQrTWY+95a2HM8FOXTcfREzT1yf+SlIo IrgQKYe7FcYkt4kDv20bGE+ZyinI7wl+335Z7BIQVctWSqcp3fE9q8jmig9Hzo6tkGoA c58sD2k5bHErF196RrRuT0ewn4WmHx0gDYFHntlm5Gwnh7MxiV16EMMDdAeck8SgwuIN TZUKBoOtIHTBGwKYhsNL+1LwXy7HLg7Hmvi6SdBoVClFb9C6XAxR+E6MX/CvzW2q0Mnr J+cb1vtwTQ47JeRAYvTt8XG/ZTj0jhk0QPanM2zSiVsA65BO1jgFTtWwoV69xACb4vMd E5KA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=DQrgssfR; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c12si2106078edt.545.2021.02.24.16.29.06; Wed, 24 Feb 2021 16:29:36 -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=k20201202 header.b=DQrgssfR; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238142AbhBXN6x (ORCPT + 99 others); Wed, 24 Feb 2021 08:58:53 -0500 Received: from mail.kernel.org ([198.145.29.99]:58408 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235793AbhBXNG6 (ORCPT ); Wed, 24 Feb 2021 08:06:58 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 7187164F87; Wed, 24 Feb 2021 12:54:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1614171263; bh=YTCBPefOUoG89KtBq5r6rrPr10r8HCEbcZB1j4fUuLo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DQrgssfRQlBJBh7yORUs7Lx6rYYxwzaRTA+1WqXvZflZJh3+rZeLNDPDH9iCnA0yD su/9ClXWfNqufFqI4ppugiG4SUImQKtWpAi0odbZNfRf0PVMrXdxD1aM3bXjE86xtV USaTVYaa2GYskn2I9EcuGUSzIVAkvQjgQ6hI+nPj8JHINEnH+WgXgvXLpUC87c71cN BC78utZei6VhCTtYZuXOMGpJGUSMjevpohlmbt3AITG8c6wPA1Uk/qznq9hLxN9maG aag6PD2B+19EU2yuLg2/ou8WPY6idLmrLGhHMdJwf8O5zSPq1dIklkOMNAjkl27SkR kYlD/VaJO4hFA== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Josef Bacik , David Sterba , Sasha Levin , linux-btrfs@vger.kernel.org Subject: [PATCH AUTOSEL 5.4 32/40] btrfs: fix error handling in commit_fs_roots Date: Wed, 24 Feb 2021 07:53:32 -0500 Message-Id: <20210224125340.483162-32-sashal@kernel.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210224125340.483162-1-sashal@kernel.org> References: <20210224125340.483162-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Josef Bacik [ Upstream commit 4f4317c13a40194940acf4a71670179c4faca2b5 ] While doing error injection I would sometimes get a corrupt file system. This is because I was injecting errors at btrfs_search_slot, but would only do it one time per stack. This uncovered a problem in commit_fs_roots, where if we get an error we would just break. However we're in a nested loop, the first loop being a loop to find all the dirty fs roots, and then subsequent root updates would succeed clearing the error value. This isn't likely to happen in real scenarios, however we could potentially get a random ENOMEM once and then not again, and we'd end up with a corrupted file system. Fix this by moving the error checking around a bit to the main loop, as this is the only place where something will fail, and return the error as soon as it occurs. With this patch my reproducer no longer corrupts the file system. Signed-off-by: Josef Bacik Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Sasha Levin --- fs/btrfs/transaction.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c index c346ee7ec18d4..aca6c467d7761 100644 --- a/fs/btrfs/transaction.c +++ b/fs/btrfs/transaction.c @@ -1212,7 +1212,6 @@ static noinline int commit_fs_roots(struct btrfs_trans_handle *trans) struct btrfs_root *gang[8]; int i; int ret; - int err = 0; spin_lock(&fs_info->fs_roots_radix_lock); while (1) { @@ -1224,6 +1223,8 @@ static noinline int commit_fs_roots(struct btrfs_trans_handle *trans) break; for (i = 0; i < ret; i++) { struct btrfs_root *root = gang[i]; + int ret2; + radix_tree_tag_clear(&fs_info->fs_roots_radix, (unsigned long)root->root_key.objectid, BTRFS_ROOT_TRANS_TAG); @@ -1245,17 +1246,17 @@ static noinline int commit_fs_roots(struct btrfs_trans_handle *trans) root->node); } - err = btrfs_update_root(trans, fs_info->tree_root, + ret2 = btrfs_update_root(trans, fs_info->tree_root, &root->root_key, &root->root_item); + if (ret2) + return ret2; spin_lock(&fs_info->fs_roots_radix_lock); - if (err) - break; btrfs_qgroup_free_meta_all_pertrans(root); } } spin_unlock(&fs_info->fs_roots_radix_lock); - return err; + return 0; } /* -- 2.27.0