Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2750018pxj; Mon, 10 May 2021 09:53:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzl1bu0p/rAfzD18Vb1S39A3RU7M6cRuHRwxOwpjpM/W6dUFi+gSL7LkKi353BzBAmxehmX X-Received: by 2002:a92:360e:: with SMTP id d14mr22086060ila.106.1620665617593; Mon, 10 May 2021 09:53:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620665617; cv=none; d=google.com; s=arc-20160816; b=EFLlecs0ShNTdCN77UMPzEUpepYujCwEnYFagP/2xv26KJtZO0CKvVZg6rees1fbxQ gwh1DIE94+RybnwJuKJiTsicamkaHSl0Opa7x8d/B7o15OTXB2MMyGfBFBgZzVss8fII 3953e0L1WM9sSPrAfKNp4r8okUrX9Oddk+tKSKA9qqpCA3moBpSruptT+d7CNp1z6r0l 4lGTYaoJtV6IwKeoC3yXRHxKkKWmo+FbnmXrMJI18msgTYh0Shishgs64E2+F3SIbuMS QvXdKWyuPOCqigQ5dX9qCZnuIHler3LsJcuFolev4jah39hMU5+cfR19Hsocqa+GCfUS qa1g== 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=NQA4Lc2zpDD/NZR0pEVvd3pNMk8TexvK3GjcTYx8UUk=; b=y1Nw2ALYofo+w6NOqoaCRlROBWkLCXBQne+GR0eWiYZ6YFs6GPhMxF1Kqw3ZcC641Q IsrDaqpz/VHKHFqtDgBzSHG2tdmGPNoNMLxnzPwkF9u+HkabhRWWsBzVu7Oso1A7trPh AWinLQpREEYuundRpTV393TgXafIaCrFiTk+lPJP6Xx97t2XGD98zxd11sWrVIBqYlnI hD5RxnBBRomvvvYwHmzt4uZhWMNAEmYxMEvDgWWxyj/7MoA4c9+1XZfeo2heQXDJIzyS hjKlRjWSheN5/DG/HuQfZagMjquHv7hsfx/ZlzCXP82Ul953x2AZOWljTFJEIPTzHtzs 6GGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=2JWs33WB; 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 r9si16979014ilt.62.2021.05.10.09.53.25; Mon, 10 May 2021 09:53:37 -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=2JWs33WB; 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 S240317AbhEJL2w (ORCPT + 99 others); Mon, 10 May 2021 07:28:52 -0400 Received: from mail.kernel.org ([198.145.29.99]:52738 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234706AbhEJK4x (ORCPT ); Mon, 10 May 2021 06:56:53 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 74B1161C2C; Mon, 10 May 2021 10:48:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1620643709; bh=UnwyQUOzjkf1WYv0tTxlbX5jGaXsEMHRaAJADGZFsyM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=2JWs33WBTQEl0vBYI56QqHAU5WGXiW8pD9vbKJ2dhOKrY0XNFuvAyG/EtlmmdpgEu v9nO6uPt2Dt41ltXtUSMTWopmZ8wjLwtCyFZ5Plw988HyGy1mjJnqE/yMloEvmYjHt 9OoeEM7luSydw1GmI2WRcR2tRxqEE+0XSpMeN9s8= 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.11 125/342] btrfs: do proper error handling in create_reloc_root Date: Mon, 10 May 2021 12:18:35 +0200 Message-Id: <20210510102014.210524106@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210510102010.096403571@linuxfoundation.org> References: <20210510102010.096403571@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 c01e0d7bef2c..a257a7d8d453 100644 --- a/fs/btrfs/relocation.c +++ b/fs/btrfs/relocation.c @@ -732,10 +732,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; @@ -747,7 +749,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 @@ -768,9 +772,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)); @@ -788,14 +799,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