Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1979422pxb; Fri, 5 Mar 2021 04:39:50 -0800 (PST) X-Google-Smtp-Source: ABdhPJyXRgjDwQup8TgVYsRVHVR/2lalH64hX2MiqG2BC+7yU3uZHxikInhzyygTp3UVVFTtoI9K X-Received: by 2002:a17:906:b80c:: with SMTP id dv12mr2095715ejb.110.1614947990261; Fri, 05 Mar 2021 04:39:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614947990; cv=none; d=google.com; s=arc-20160816; b=lroiOJwKd0TluYtl8FqzV599XzCYRYK6nYjr8bBBIgvWGZGaA9MrdermOM2fhjkLe4 7kML0emY/mLWOdVbm4fMd8ktzSxGGCJZgbCy90qrjek8U+W2FLDD6tM2PGrbJ96A6v0K iWTF8destNJTaJfIsXY+t+PFeSKMGf/fMgy7CDLGZw1294LODw/GmWmtcR1bZvKk0EeI Ku0F8PuzvpnMnPo+HV8Ef1h76nKPIh2X2zk6UnT8sQ4klZ4DzILfGtLGFLKInuxMTTRD TQIAcbbe1lYG1hRy0SGS8Gi3Lls3/D5nQEUH39X+wty0iqO/fSomMl7J6p3aBIiY8Qki OPpA== 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=v2OV7mvrzuVsHqGAhas48Om4FYuFQgwVXVMYr4KOOFQ=; b=Y5UEHZVS/aKJrqfUEx7lgn95BmrD9htrlfN9BEOQncVub676zuS9Hpxqrb45bHODTx Gsj2EhyH5kHmcPRnvrYRU1pgB6s396ACb0CS6rpzgLSIoh1OU9KFM0Ei4wMyi/yyZbyt g8Sd7nB3wmHydkuPdfpxrz1K56n80dxOvGIihmdxZcXPQRjlGNM3S+p2zHkuPqG/8rQQ kZwFnsLoWC9OOKFaEGJPLT8L1BYRG62mntR0SZQ5TESgig1q9DO/gRCmzZQ0rnJnVVi3 ZVTilQa5FHHTM71AzVwyJHuh68gBwl3gQmgs2sZYuR1XyUDULpRF5d8EPTbo/rpfbFBC 8Q0Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=NWZ67lWe; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d5si1423922edr.381.2021.03.05.04.39.27; Fri, 05 Mar 2021 04:39:50 -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=@linuxfoundation.org header.s=korg header.b=NWZ67lWe; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232836AbhCEMi1 (ORCPT + 99 others); Fri, 5 Mar 2021 07:38:27 -0500 Received: from mail.kernel.org ([198.145.29.99]:50444 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233346AbhCEMhg (ORCPT ); Fri, 5 Mar 2021 07:37:36 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 645696501B; Fri, 5 Mar 2021 12:37:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1614947856; bh=mH5lfZ8/5v9mgS6Y436aF+xZF4680Y0Wb2+gP2hbx34=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NWZ67lWeZvGlPwKqovcuHouMADQHL7nsWsoCzFxv662wLt1yHMQC60NfKxY478yjx qzXj8I1i4cq4kqm8wPtkU+78EeUMnVqEiUtI1RG43QzES+TE/sMaZH9dqhWKAZui9G mGeqns3SP+YmkZVapHQnHe+QXmSVlphojGSGDZVs= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Josef Bacik , David Sterba , Sasha Levin Subject: [PATCH 4.19 37/52] btrfs: fix error handling in commit_fs_roots Date: Fri, 5 Mar 2021 13:22:08 +0100 Message-Id: <20210305120855.483963642@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210305120853.659441428@linuxfoundation.org> References: <20210305120853.659441428@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: 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 8829d89eb4af..1b52c960682d 100644 --- a/fs/btrfs/transaction.c +++ b/fs/btrfs/transaction.c @@ -1249,7 +1249,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) { @@ -1261,6 +1260,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); @@ -1282,17 +1283,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.30.1