Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2475523pxj; Mon, 10 May 2021 04:01:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJycukwPBI0tzgzqbI5xB5Ml4BT5uyZdFeEzPjU66NYff8OKKyJ1DIG8YZvd+iSgBCGAolNE X-Received: by 2002:a05:6402:1a2f:: with SMTP id be15mr28476270edb.207.1620644519451; Mon, 10 May 2021 04:01:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620644519; cv=none; d=google.com; s=arc-20160816; b=rPTHtX8KG3yoBqbPduicgvUJECMfmdkXKoO+2RZBukIiFV0o84fa/tL5iNsf/gg0GN daQ5Bz5sbAnhxzY86kb0QtNbQsBLEyymiFt7OQW6WlP+jwuHJ7u2yl9bGsuHTyNM302Y +lKie2he9YPpH6PkxF6x/vhW/w/459LOFs6xSyO5+GrsNAd+ZfnMur5QxRw6ft9tlSmV hLxIcHnfuoXtYLAQB8QuY9Bu7MqIGSUq7H5D1arUTo/wRHPfKhjtVkdmzsrVVwQOQI9W s0fET6LUOIvFvUAvdjzthrYTAJ/56elQz4/VFB70q+Sk7Hokt6lZ6gZkDH230RLzRdrZ 6sbQ== 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=in7Rhk2/Kzx3kOuFrXp1S577CB1PfTvCJpqk7NTPCPA=; b=D5FprfT0rs5zyPMCPKagUjlT748canbO0AkIVTQqBr0voR9VqHzoEQqOpWqSJ9VlWH YjCXLEZ3/PT85NBaunPal/+elnTCPJlLc7pV09ujZ1o3X8c7IXPZ+P8zgfyHcC4vx1sL vW5WSrkngRMOcL4P43yed3UnS3FVNYtlz2ifGqkYXhGuLHTBsperhui3HlMT6qIttFsF oNu5kJHzsxKIsLCjwxE21uwYHpx3XRDED/JO1gWWP42tZxbYrChBaS1FOkCiD0/X83zM J5YU3Z91bDKQbQ2XuPQjKnwapEWJojtJdQuIqY0vEdCAXYJa96EXxTF4awYZSMfIyfdl zXaw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=L1LaJ8Ho; 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 a6si7943418edk.546.2021.05.10.04.01.33; Mon, 10 May 2021 04:01:59 -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=L1LaJ8Ho; 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 S233179AbhEJK6n (ORCPT + 99 others); Mon, 10 May 2021 06:58:43 -0400 Received: from mail.kernel.org ([198.145.29.99]:58338 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233244AbhEJKp2 (ORCPT ); Mon, 10 May 2021 06:45:28 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 581CA6191D; Mon, 10 May 2021 10:35:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1620642934; bh=/C23TMwhIzOsXf8MMgbFesmQbMu33pbW/bRTLTyp4ps=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=L1LaJ8HoAD2eYA5Ee8KZz52x8RQ8Yiu8c1hEk2Ijx8A2KLS9gmnqdG4/fO7OlxtwV tvFxYNKHMHOL16cCqCfvbTfDzogXvCA0EICBh65itpTOBeJtcaNVZO9xSDw4hci7Vm SHNh9L1itHsvOGLBYWZ0m+JIfgtd09YzM5Y0BElU= 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 110/299] btrfs: do proper error handling in create_reloc_root Date: Mon, 10 May 2021 12:18:27 +0200 Message-Id: <20210510102008.582029012@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210510102004.821838356@linuxfoundation.org> References: <20210510102004.821838356@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 6a44d8f5e12e..575604ebea44 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