From patchwork Thu Oct 19 05:41:33 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 10015999 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id AFD05600CC for ; Thu, 19 Oct 2017 05:41:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9F9EB28C51 for ; Thu, 19 Oct 2017 05:41:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 922B528CBA; Thu, 19 Oct 2017 05:41:53 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2165D28C51 for ; Thu, 19 Oct 2017 05:41:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751458AbdJSFlq (ORCPT ); Thu, 19 Oct 2017 01:41:46 -0400 Received: from prv3-mh.provo.novell.com ([137.65.250.26]:50917 "EHLO prv3-mh.provo.novell.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751153AbdJSFlp (ORCPT ); Thu, 19 Oct 2017 01:41:45 -0400 Received: from adam-pc.lan (prv-ext-foundry1int.gns.novell.com [137.65.251.240]) by prv3-mh.provo.novell.com with ESMTP (NOT encrypted); Wed, 18 Oct 2017 23:41:43 -0600 From: Qu Wenruo To: linux-btrfs@vger.kernel.org Cc: dsterba@suse.cz, nborisov@suse.com Subject: [PATCH v2 1/6] btrfs-progs: Avoid BUG_ON for chunk allocation when ENOSPC happens Date: Thu, 19 Oct 2017 13:41:33 +0800 Message-Id: <20171019054138.13965-2-wqu@suse.com> X-Mailer: git-send-email 2.14.2 In-Reply-To: <20171019054138.13965-1-wqu@suse.com> References: <20171019054138.13965-1-wqu@suse.com> Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When passing directory larger than block device using --rootdir parameter, we get the following backtrace: ------ extent-tree.c:2693: btrfs_reserve_extent: BUG_ON `ret` triggered, value -28 ./mkfs.btrfs(+0x1a05d)[0x557939e6b05d] ./mkfs.btrfs(btrfs_reserve_extent+0xb5a)[0x557939e710c8] ./mkfs.btrfs(+0xb0b6)[0x557939e5c0b6] ./mkfs.btrfs(main+0x15d5)[0x557939e5de04] /usr/lib/libc.so.6(__libc_start_main+0xea)[0x7f83b101af6a] ./mkfs.btrfs(_start+0x2a)[0x557939e5af5a] ------ Nothing special, just BUG_ON() abusing from ancient code. Fix them by using correct return. Signed-off-by: Qu Wenruo Reviewed-by: Nikolay Borisov --- extent-tree.c | 3 ++- volumes.c | 18 ++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/extent-tree.c b/extent-tree.c index 525a237e5923..055582c36da6 100644 --- a/extent-tree.c +++ b/extent-tree.c @@ -2690,7 +2690,8 @@ int btrfs_reserve_extent(struct btrfs_trans_handle *trans, search_start, search_end, hint_byte, ins, trans->alloc_exclude_start, trans->alloc_exclude_nr, data); - BUG_ON(ret); + if (ret < 0) + return ret; clear_extent_dirty(&info->free_space_cache, ins->objectid, ins->objectid + ins->offset - 1); return ret; diff --git a/volumes.c b/volumes.c index 2209e5a9100b..e1ee27d5f3ce 100644 --- a/volumes.c +++ b/volumes.c @@ -1032,11 +1032,13 @@ again: info->chunk_root->root_key.objectid, BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset, calc_size, &dev_offset, 0); - BUG_ON(ret); + if (ret < 0) + goto out_chunk_map; device->bytes_used += calc_size; ret = btrfs_update_device(trans, device); - BUG_ON(ret); + if (ret < 0) + goto out_chunk_map; map->stripes[index].dev = device; map->stripes[index].physical = dev_offset; @@ -1075,16 +1077,24 @@ again: map->ce.size = *num_bytes; ret = insert_cache_extent(&info->mapping_tree.cache_tree, &map->ce); - BUG_ON(ret); + if (ret < 0) + goto out_chunk_map; if (type & BTRFS_BLOCK_GROUP_SYSTEM) { ret = btrfs_add_system_chunk(info, &key, chunk, btrfs_chunk_item_size(num_stripes)); - BUG_ON(ret); + if (ret < 0) + goto out_chunk; } kfree(chunk); return ret; + +out_chunk_map: + kfree(map); +out_chunk: + kfree(chunk); + return ret; } /*