Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2531194pxj; Mon, 10 May 2021 05:18:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyGZzjuggOZMzrhruMYMN24sVp+uswcIB6tHAcwyiF2vGxfUaSKNhD+1t8L0UadiGrJaBMF X-Received: by 2002:a17:907:209b:: with SMTP id pv27mr25571240ejb.475.1620649103215; Mon, 10 May 2021 05:18:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620649103; cv=none; d=google.com; s=arc-20160816; b=GzF6mJaH1VSaGEo8Z2GudAYYbJmPpA4A6rAsMJQwdukc/RnemugHHeEIGq8d/qR5CR IPZTp+bqUBsr7VZHDx7u/jFuIKEGeAHF18lGjwIFeOr/QQcvb5viQbk6wJWw2JGMDKBQ EKQgaU2sm0DcYTL5ovSJnASq25jFTJsSPms5rWRxHvpRI76qSV1qRfk54syJcwMLxaFX 3a5UIHrQQ4b1RnIwefh/iSyvN7fIDJvrJbYESYguWukDFZ8tTta/GYh3LZWwlrJG2soh DH/7G8ohiB5BAAQAfTRqkzVeUvDgX7UsOs3zUrYMUIqvQr3MXaC775VqOuc26b54+GtZ cq3Q== 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=dZ4nwKjH+Cj89pGCsZvNlzVaevg4blUt9Co5jE+iQC4=; b=MIGCX7FvjVTRfUW6uezEW9Fz3koTWKqCJ4sLgZigzASVi51PtBrCbxab0EncBDBiJe gm5wqGmG/9Q0bjIS9rDqC2lSsfpSewW/IJilt3mDCHb/T4yyrF2T1X0Ls6GBGHuHoP/+ /TXJoLXyqpfJK3lv3muUbo98WwOZvk/4xqnltQ6PZ3KDDj5K8pTjcZBfWgR06C9kK6bg VrmuIkgzgF5lqW6XUy9AB6n7bmUf8rAtnSETtgs9VY4fIoE/Hlb/ZtfrF1tz4Esr61wn 6jUwkjoGjPudpy0Qmm5YoeynNVP+eC3RZtgfvbZhxPxbJZIiPs+f4GXJQYW/tio1d4Zc O2QA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=sbPMF7Xc; 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 b23si13962933edv.199.2021.05.10.05.17.59; Mon, 10 May 2021 05:18:23 -0700 (PDT) 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=sbPMF7Xc; 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 S1343734AbhEJMOQ (ORCPT + 99 others); Mon, 10 May 2021 08:14:16 -0400 Received: from mail.kernel.org ([198.145.29.99]:46276 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236659AbhEJLIZ (ORCPT ); Mon, 10 May 2021 07:08:25 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 0AADE619B2; Mon, 10 May 2021 11:02:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1620644573; bh=Y7ekhyb+XSJ4OgJm7ILGcgfzw8TrCe8/USJ16LFGbGA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sbPMF7XcJhrQr3hOhuLT1eJZstS5uhw45IUpkx293UzQ+q6NBI3RKbdcIumJAnseG 6Wqxz7o9SCH1Hbg+6sWv6QV6MhOu+/3qm8WwsNKRTjEK0kkYhAx/SRysrhJfA0d52R 1bffjUSuUkGc0aWkAbyLH/DLCLQDezramo5KSwbo= 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.12 137/384] btrfs: do proper error handling in create_reloc_root Date: Mon, 10 May 2021 12:18:46 +0200 Message-Id: <20210510102019.410447094@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210510102014.849075526@linuxfoundation.org> References: <20210510102014.849075526@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 84c50ba5214c2f3c1be4a931d521ec19f55dfdc8 ] We do memory allocations here, read blocks from disk, all sorts of operations that could easily fail at any given point. Instead of panicing the box, simply return the error back up the chain, all callers at this point have proper error handling. Signed-off-by: Josef Bacik Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Sasha Levin --- fs/btrfs/relocation.c | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c index bf269ee17e68..b445b3073dea 100644 --- a/fs/btrfs/relocation.c +++ b/fs/btrfs/relocation.c @@ -733,10 +733,12 @@ static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans, struct extent_buffer *eb; struct btrfs_root_item *root_item; struct btrfs_key root_key; - int ret; + int ret = 0; + bool must_abort = false; root_item = kmalloc(sizeof(*root_item), GFP_NOFS); - BUG_ON(!root_item); + if (!root_item) + return ERR_PTR(-ENOMEM); root_key.objectid = BTRFS_TREE_RELOC_OBJECTID; root_key.type = BTRFS_ROOT_ITEM_KEY; @@ -748,7 +750,9 @@ static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans, /* called by btrfs_init_reloc_root */ ret = btrfs_copy_root(trans, root, root->commit_root, &eb, BTRFS_TREE_RELOC_OBJECTID); - BUG_ON(ret); + if (ret) + goto fail; + /* * Set the last_snapshot field to the generation of the commit * root - like this ctree.c:btrfs_block_can_be_shared() behaves @@ -769,9 +773,16 @@ static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans, */ ret = btrfs_copy_root(trans, root, root->node, &eb, BTRFS_TREE_RELOC_OBJECTID); - BUG_ON(ret); + if (ret) + goto fail; } + /* + * We have changed references at this point, we must abort the + * transaction if anything fails. + */ + must_abort = true; + memcpy(root_item, &root->root_item, sizeof(*root_item)); btrfs_set_root_bytenr(root_item, eb->start); btrfs_set_root_level(root_item, btrfs_header_level(eb)); @@ -789,14 +800,25 @@ static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans, ret = btrfs_insert_root(trans, fs_info->tree_root, &root_key, root_item); - BUG_ON(ret); + if (ret) + goto fail; + kfree(root_item); reloc_root = btrfs_read_tree_root(fs_info->tree_root, &root_key); - BUG_ON(IS_ERR(reloc_root)); + if (IS_ERR(reloc_root)) { + ret = PTR_ERR(reloc_root); + goto abort; + } set_bit(BTRFS_ROOT_SHAREABLE, &reloc_root->state); reloc_root->last_trans = trans->transid; return reloc_root; +fail: + kfree(root_item); +abort: + if (must_abort) + btrfs_abort_transaction(trans, ret); + return ERR_PTR(ret); } /* -- 2.30.2