From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 94FA5C433E0 for ; Wed, 24 Feb 2021 13:44:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 52D6864E85 for ; Wed, 24 Feb 2021 13:44:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234494AbhBXNnA (ORCPT ); Wed, 24 Feb 2021 08:43:00 -0500 Received: from mail.kernel.org ([198.145.29.99]:51028 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235385AbhBXMzP (ORCPT ); Wed, 24 Feb 2021 07:55:15 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 28F8864F29; Wed, 24 Feb 2021 12:51:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1614171097; bh=1knVN347gRRnZ6yIJhJPmpEmdhyyCJcBhL+w4F7Bel8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MGzwIJtgp9NunLhjJiKfdSV6h3BQmzoPMGeSmaaWAaJopPv7Q++veHU9j+JJKnYlA Zuz7PIzU1gt8XqPhOMR3CXixYvF4kJQpsqEIexJYgmLbbr4oZciw6Aw08b1BSmYz3W mBB/Pn9KtO0bdqXLH5W0Jy8VcOmGLPO+rLz9A17I5HoxmUxYLPkzKDH/cXGiIm5iCj Ui0jeTa3iaOs7YBf58NrJnr8xdh9UJ8jpW7RgTLejPHKxzpxkYb2ZKphySmuS/n3Ku 4rN/OZL86CIQFOy0R/2L3xwXVoncElFILUj7tY96vYXo66v6Sdqw5ZBehbLAm4kD0A Xqv8t6y/SDMvA== 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.11 53/67] btrfs: fix error handling in commit_fs_roots Date: Wed, 24 Feb 2021 07:50:11 -0500 Message-Id: <20210224125026.481804-53-sashal@kernel.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210224125026.481804-1-sashal@kernel.org> References: <20210224125026.481804-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-btrfs@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 6af7f2bf92de7..fbf93067642ac 100644 --- a/fs/btrfs/transaction.c +++ b/fs/btrfs/transaction.c @@ -1319,7 +1319,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) { @@ -1331,6 +1330,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); @@ -1350,17 +1351,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