Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1974814pxb; Fri, 5 Mar 2021 04:33:06 -0800 (PST) X-Google-Smtp-Source: ABdhPJy/KFSlMnOXTyeJCmCdq3x/iD82usXMMh7KK0N+rEyU/rq2d2hgClOhIY6qCqlxFSHfMDez X-Received: by 2002:a50:fd15:: with SMTP id i21mr7446535eds.384.1614947585846; Fri, 05 Mar 2021 04:33:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614947585; cv=none; d=google.com; s=arc-20160816; b=gs9pcmhpGZGcoFk4OU5JMv+/mJF3RZlGz7lfVyZLGAnwrX3o9gGi0iM8X+1i7b1Vzz njSDF3JRAUKTO/ysu4DSUtZy3JhnxyUo5uzDers7RUT3egNCtGB1SoGT6IlVM30mr3C7 MyXWEJWWNI2y7VgW5OlmYNxosw8rtGIp2kGUprCMynwLVlvZy/qGx1oaODJCrUC9+Z3F ohYeRBnjoLxgJl/b+T7ldGe6cPkHbA83oYw4BIfoEtcjzQd4ye+cve0lxdB9bU4mw60t 5QSdeZ//POIOHyTzKf6ykFS20Mz9dMgOwf2hBoJYZxvesnOinpfsbNLHfb9Q91j11avE G/Pg== 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=c1AuWnfiRTzfpSFQwx+SzEijO5fswzqnWUVUA70Bo6E=; b=zn9nOmewuO4EPyuNrTlczBpcUedBgk+4oOFs9IFohj6+dIZbeTGBJz+masRPd3ouzO rMp8c1ejzoi7ATOi4EgHj2Zsxas3LNnHahmO3HtVh7C0meJRRT70rjoEDpWOX8iwJ+Xk f8/GwBaO7PRiuMMPkW5oC+NQgvRjEPYSPIiJSYetVTvxCIJWquqA9axerKjWytMtd9vX T5ZF/WTV7f1+gwr63d9dKTiiD155vzBUmWupLKPcq15WkyZrQ4HyRtcfpgBPPcrd07pD un4pqtvVYXyhBHS/a9Dy3NRr7cpexOyivlXRK5W6PtahBAxSTdDLpq8LnjCrW52O47m+ u5Cg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=sJnUqEBo; 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 l13si1616333edj.219.2021.03.05.04.32.43; Fri, 05 Mar 2021 04:33:05 -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=sJnUqEBo; 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 S231699AbhCEMbr (ORCPT + 99 others); Fri, 5 Mar 2021 07:31:47 -0500 Received: from mail.kernel.org ([198.145.29.99]:40968 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232234AbhCEMa7 (ORCPT ); Fri, 5 Mar 2021 07:30:59 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id EB3AF65004; Fri, 5 Mar 2021 12:30:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1614947459; bh=opyeyE8L2bI76gjJP2Uo5XEhvfPNOd9HtR+7guSSwz0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sJnUqEBoIP/mDYfrrK8ZvnMDkohBZHj7gzf+cSt9xd/Pfq6X/j7eRxMaPFm1dH7+b RgEJmtpeoxuKcDnpOgdwjqpBELhuqBLsSlVchFXyzdNhPwQaJwEc7WtVcd3T3nio/I 4PX5ukI5qmTW/RzUX7VzYP08bmrlVvVA9awUGfRg= 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 5.10 072/102] btrfs: fix error handling in commit_fs_roots Date: Fri, 5 Mar 2021 13:21:31 +0100 Message-Id: <20210305120906.823576586@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210305120903.276489876@linuxfoundation.org> References: <20210305120903.276489876@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 96dbfc011f45..261a50708cb8 100644 --- a/fs/btrfs/transaction.c +++ b/fs/btrfs/transaction.c @@ -1320,7 +1320,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) { @@ -1332,6 +1331,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); @@ -1353,17 +1354,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