From patchwork Thu Sep 17 18:13:38 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Boris Burkov X-Patchwork-Id: 11783243 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0E333746 for ; Thu, 17 Sep 2020 18:22:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8982422208 for ; Thu, 17 Sep 2020 18:22:51 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=bur.io header.i=@bur.io header.b="MprqB2RQ"; dkim=temperror (0-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="bXZYJYsm" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726584AbgIQSWq (ORCPT ); Thu, 17 Sep 2020 14:22:46 -0400 Received: from wout2-smtp.messagingengine.com ([64.147.123.25]:51367 "EHLO wout2-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726544AbgIQSV2 (ORCPT ); Thu, 17 Sep 2020 14:21:28 -0400 X-Greylist: delayed 433 seconds by postgrey-1.27 at vger.kernel.org; Thu, 17 Sep 2020 14:21:20 EDT Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id 1AA7994A; Thu, 17 Sep 2020 14:14:03 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Thu, 17 Sep 2020 14:14:03 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bur.io; h=from :to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; s=fm1; bh=IpEfRybLa8wi7 /AMXW1zdFQPhFNVcCq7L3X+9DWs09s=; b=MprqB2RQVkXt53c6GjjNP6wXPg5w6 KBfBej9DP6Dwl/+zqGKYWRNI3vrbkK/lF+DYbeehhT89HvKphjCXuVFUb5nhnYRy 86TxjC8BTRDWIY/QnPOGTfqoBraSDYOi7d6Q523q0sQuuHDdyyRNa6SMsrro0LlY IgRCSVBsnxx4uXDnAAxO+h/iihd5CGp+ArsysgKUu0JSQDnd5xtwUU3cqC+AeZKT cmEhNpuwNwNgNW4eu9OLgNCN/AdtjbU/Kg8mkh+A8Zre0lWivB1Dc7D0EDKourcY 9/ZiTEsgFuOsfwg46ach0XrBWYZfcagazmmK7hNHsJGbdnIUGrrMkQ+Gg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; bh=IpEfRybLa8wi7/AMXW1zdFQPhFNVcCq7L3X+9DWs09s=; b=bXZYJYsm 1ZBveSUSEUuN/uIdFAI86IyrFGaHqhZ4n/8Ie+yZzzzmYDWZF9yXJJl0i4DskpVt L9gTw7S1gyFq7BjFU2K2XQaHw2kp7YnsR+9Zy5O7w8qcdHY2dFsbFvXfqtRBTmNK 0DBVuAVQUXg1Q2czAbq6AC8v2D6q6TZQJnwTeXIJbj9/R/Bvl4fC9GC2nFX0yPBT pT1uGIlsLfygI/8hz5Zr3QS09ZAbab+BWw66bcHHvcHqv5eTzQyCnqfXvBleDBWi Aw8RCxVk6pxcQQ5sHpfaqML07O9bc3V248wqtd7fvFWwEmzzfYBQ3av6pKNfJ7Lb Yyb/nv5Qx+jRYQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrtdeggdduvdegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefhvffufffkofgjfhgggfestdekre dtredttdenucfhrhhomhepuehorhhishcuuehurhhkohhvuceosghorhhishessghurhdr ihhoqeenucggtffrrghtthgvrhhnpeetheehudfgleelhfehffettedtfeelvdfghfelke efgefgvdevgefffedvtdefgeenucffohhmrghinhepghhithhhuhgsrdgtohhmnecukfhp peduieefrdduudegrddufedvrdefnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrg hmpehmrghilhhfrhhomhepsghorhhishessghurhdrihho X-ME-Proxy: Received: from localhost (unknown [163.114.132.3]) by mail.messagingengine.com (Postfix) with ESMTPA id 00A123280064; Thu, 17 Sep 2020 14:14:02 -0400 (EDT) From: Boris Burkov To: linux-btrfs@vger.kernel.org, kernel-team@fb.com Cc: Boris Burkov Subject: [PATCH v3 1/4] btrfs: support remount of ro fs with free space tree Date: Thu, 17 Sep 2020 11:13:38 -0700 Message-Id: <1d0cca6ce1f67484c6b7ef591e264c04ca740c96.1600282812.git.boris@bur.io> X-Mailer: git-send-email 2.24.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org When a user attempts to remount a btrfs filesystem with 'mount -o remount,space_cache=v2', that operation silently succeeds. Unfortunately, this is misleading, because the remount does not create the free space tree. /proc/mounts will incorrectly show space_cache=v2, but on the next mount, the file system will revert to the old space_cache. For now, we handle only the easier case, where the existing mount is read-only and the new mount is read-write. In that case, we can create the free space tree without contending with the block groups changing as we go. If the remount is ro->ro, rw->ro, or rw->rw, we will not create the free space tree, and print a warning to dmesg so that this failure is more visible. References: https://github.com/btrfs/btrfs-todo/issues/5 Signed-off-by: Boris Burkov Reviewed-by: Josef Bacik --- v3: - change failure to warning in dmesg for consistency v2: - move creation down to ro->rw case - error on all other remount cases - add a comment to help future remount modifiers fs/btrfs/super.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c index 3ebe7240c63d..8dfd6089e31d 100644 --- a/fs/btrfs/super.c +++ b/fs/btrfs/super.c @@ -47,6 +47,7 @@ #include "tests/btrfs-tests.h" #include "block-group.h" #include "discard.h" +#include "free-space-tree.h" #include "qgroup.h" #define CREATE_TRACE_POINTS @@ -1838,6 +1839,7 @@ static int btrfs_remount(struct super_block *sb, int *flags, char *data) u64 old_max_inline = fs_info->max_inline; u32 old_thread_pool_size = fs_info->thread_pool_size; u32 old_metadata_ratio = fs_info->metadata_ratio; + bool create_fst = false; int ret; sync_filesystem(sb); @@ -1862,6 +1864,16 @@ static int btrfs_remount(struct super_block *sb, int *flags, char *data) btrfs_resize_thread_pool(fs_info, fs_info->thread_pool_size, old_thread_pool_size); + if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) && + !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { + create_fst = true; + if (!sb_rdonly(sb) || *flags & SB_RDONLY) { + btrfs_warn(fs_info, + "Remounting with free space tree only supported from read-only to read-write"); + create_fst = false; + } + } + if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb)) goto out; @@ -1924,6 +1936,21 @@ static int btrfs_remount(struct super_block *sb, int *flags, char *data) goto restore; } + /* + * NOTE: when remounting with a change that does writes, don't + * put it anywhere above this point, as we are not sure to be + * safe to write until we pass the above checks. + */ + if (create_fst) { + ret = btrfs_create_free_space_tree(fs_info); + if (ret) { + btrfs_warn(fs_info, + "failed to create free space tree: %d", ret); + goto restore; + } + } + + ret = btrfs_cleanup_fs_roots(fs_info); if (ret) goto restore; From patchwork Thu Sep 17 18:13:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Boris Burkov X-Patchwork-Id: 11783245 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 216E159D for ; Thu, 17 Sep 2020 18:24:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D701A2220F for ; Thu, 17 Sep 2020 18:24:12 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=bur.io header.i=@bur.io header.b="UZ6Qt8K9"; dkim=temperror (0-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="ekIkmKW/" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726424AbgIQSXe (ORCPT ); Thu, 17 Sep 2020 14:23:34 -0400 Received: from wout2-smtp.messagingengine.com ([64.147.123.25]:38933 "EHLO wout2-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726600AbgIQSV2 (ORCPT ); Thu, 17 Sep 2020 14:21:28 -0400 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id 23BAE537; Thu, 17 Sep 2020 14:14:08 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Thu, 17 Sep 2020 14:14:08 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bur.io; h=from :to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; s=fm1; bh=ljwPCr2wNgfaD roqYAJiE1zm/3m2I/BNEh3PKlQLkFs=; b=UZ6Qt8K91EdEppan48kagyYI/2XNM H/sCEHdBOr2SHa4cijC4q7biVQdM6J0HlXFqVz2i1t+AzHMcNjtAjfAZjk5e3QTx 9ihlANu8xXe+t8/a4gs0Yu3ahNSzp7wkpTfhYqt8pII1VcTY3qveomyYj8leI6X5 gVfJ7f6UGxD/uT6wn3wT+147HizoL+FMZ+/joi7Q91kBsNRwM9ypW3/FdApA5Njz r5qYhROb3gsABq8J1dXvyWKyYcfWVEBCxzhPwXgwzy3LF8gXujSeW9Rb5AWY0jOd W6ndX0TI3xAXAo3QlTNk/DtukH5SePx72RqnFAJDHxBXr47X2XFcWWfuA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; bh=ljwPCr2wNgfaDroqYAJiE1zm/3m2I/BNEh3PKlQLkFs=; b=ekIkmKW/ q88TtZDZ9s5hzjC0D4tJdEW63TcmkhRRWYkkIcMZYlDDKsXl2bQNwaQfccWDvM9r QETOhTMEUxacCfm1Al85ztzaviTwKs7EEcUkooMkIRIpGVYtmZJwFnkilUHqV63L KGITy+t98S4nGfvueNfqBSY9YYUNu4939KwUtTXybTSeIOk0jZmob3lUQoEdAyaK 7cZh8BwwwmC2mqRLEwkHnwVaiQ1rssATZzWO4bxbOTLz2ngTgV3Fczt/kyc/e8Zv XbdpgFJLTnnK+dVmEvpeOaRAlHgtJbtOM2T0NP4GfISf7xonpdZ5/b8ydxX2ZER5 hx9hbukweFEpyA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrtdeggdduvdegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefhvffufffkofgjfhgggfestdekre dtredttdenucfhrhhomhepuehorhhishcuuehurhhkohhvuceosghorhhishessghurhdr ihhoqeenucggtffrrghtthgvrhhnpeetheehudfgleelhfehffettedtfeelvdfghfelke efgefgvdevgefffedvtdefgeenucffohhmrghinhepghhithhhuhgsrdgtohhmnecukfhp peduieefrdduudegrddufedvrdefnecuvehluhhsthgvrhfuihiivgepudenucfrrghrrg hmpehmrghilhhfrhhomhepsghorhhishessghurhdrihho X-ME-Proxy: Received: from localhost (unknown [163.114.132.3]) by mail.messagingengine.com (Postfix) with ESMTPA id 1BA253064680; Thu, 17 Sep 2020 14:14:07 -0400 (EDT) From: Boris Burkov To: linux-btrfs@vger.kernel.org, kernel-team@fb.com Cc: Boris Burkov Subject: [PATCH 2/4] btrfs: use sb state to print space_cache mount option Date: Thu, 17 Sep 2020 11:13:39 -0700 Message-Id: X-Mailer: git-send-email 2.24.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org To make the contents of /proc/mounts better match the actual state of the file system, base the display of the space cache mount options off the contents of the super block rather than the last mount options passed in. Since there are many scenarios where the mount will ignore a space cache option, simply showing the passed in option is misleading. For example, if we mount with -o remount,space_cache=v2 on a read-write file system without an existing free space tree, we won't build a free space tree, but /proc/mounts will read space_cache=v2 (until we mount again and it goes away) There is already mount logic based on the super block's cache_generation and free space tree flag that helps decide a consistent setting for the space cache options, so we just bring those further to the fore. For free space tree, the flag is already consistent, so we just switch mount option display to use it. cache_generation is not always reliably set correctly, so we ensure that cache_generation > 0 iff the file system is using space_cache v1. This requires committing a transaction on any mount which changes whether we are using v1. (v1->nospace_cache, v1->v2, nospace_cache->v1, v2->v1). References: https://github.com/btrfs/btrfs-todo/issues/5 Signed-off-by: Boris Burkov Reviewed-by: Josef Bacik --- fs/btrfs/disk-io.c | 11 +++++++++++ fs/btrfs/free-space-cache.c | 20 ++++++++++++++++++++ fs/btrfs/free-space-cache.h | 2 ++ fs/btrfs/super.c | 15 ++++++++++----- fs/btrfs/transaction.c | 2 ++ 5 files changed, 45 insertions(+), 5 deletions(-) diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c index 71beb9493ab4..ade92e93e63f 100644 --- a/fs/btrfs/disk-io.c +++ b/fs/btrfs/disk-io.c @@ -3335,6 +3335,17 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device } } + if ((bool)btrfs_test_opt(fs_info, SPACE_CACHE) != + btrfs_free_space_cache_v1_active(fs_info)) { + ret = btrfs_update_free_space_cache_v1_active(fs_info); + if (ret) { + btrfs_warn(fs_info, + "failed to update free space cache status: %d", ret); + close_ctree(fs_info); + return ret; + } + } + down_read(&fs_info->cleanup_work_sem); if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) || (ret = btrfs_orphan_cleanup(fs_info->tree_root))) { diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c index 8759f5a1d6a0..25420d51039c 100644 --- a/fs/btrfs/free-space-cache.c +++ b/fs/btrfs/free-space-cache.c @@ -3995,6 +3995,26 @@ int btrfs_write_out_ino_cache(struct btrfs_root *root, return ret; } +bool btrfs_free_space_cache_v1_active(struct btrfs_fs_info *fs_info) +{ + return btrfs_super_cache_generation(fs_info->super_copy); +} + +int btrfs_update_free_space_cache_v1_active(struct btrfs_fs_info *fs_info) +{ + struct btrfs_trans_handle *trans; + + /* + * update_super_roots will appropriately set + * fs_info->super_copy->cache_generation based on the SPACE_CACHE + * option, so all we have to do is trigger a transaction commit. + */ + trans = btrfs_start_transaction(fs_info->tree_root, 0); + if (IS_ERR(trans)) + return PTR_ERR(trans); + return btrfs_commit_transaction(trans); +} + #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS /* * Use this if you need to make a bitmap or extent entry specifically, it diff --git a/fs/btrfs/free-space-cache.h b/fs/btrfs/free-space-cache.h index e3d5e0ad8f8e..5fbdbd2fe740 100644 --- a/fs/btrfs/free-space-cache.h +++ b/fs/btrfs/free-space-cache.h @@ -148,6 +148,8 @@ int btrfs_trim_block_group_bitmaps(struct btrfs_block_group *block_group, u64 *trimmed, u64 start, u64 end, u64 minlen, u64 maxlen, bool async); +bool btrfs_free_space_cache_v1_active(struct btrfs_fs_info *fs_info); +int btrfs_update_free_space_cache_v1_active(struct btrfs_fs_info *fs_info); /* Support functions for running our sanity tests */ #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS int test_add_free_space_entry(struct btrfs_block_group *cache, diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c index 8dfd6089e31d..3dcb676fc50c 100644 --- a/fs/btrfs/super.c +++ b/fs/btrfs/super.c @@ -512,7 +512,6 @@ int btrfs_parse_options(struct btrfs_fs_info *info, char *options, { substring_t args[MAX_OPT_ARGS]; char *p, *num; - u64 cache_gen; int intarg; int ret = 0; char *compress_type; @@ -522,10 +521,9 @@ int btrfs_parse_options(struct btrfs_fs_info *info, char *options, bool saved_compress_force; int no_compress = 0; - cache_gen = btrfs_super_cache_generation(info->super_copy); if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE)) btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE); - else if (cache_gen) + else if (btrfs_free_space_cache_v1_active(info)) btrfs_set_opt(info->mount_opt, SPACE_CACHE); /* @@ -1430,9 +1428,9 @@ static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry) seq_puts(seq, ",discard=async"); if (!(info->sb->s_flags & SB_POSIXACL)) seq_puts(seq, ",noacl"); - if (btrfs_test_opt(info, SPACE_CACHE)) + if (btrfs_free_space_cache_v1_active(info)) seq_puts(seq, ",space_cache"); - else if (btrfs_test_opt(info, FREE_SPACE_TREE)) + else if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE)) seq_puts(seq, ",space_cache=v2"); else seq_puts(seq, ",nospace_cache"); @@ -1870,6 +1868,13 @@ static int btrfs_remount(struct super_block *sb, int *flags, char *data) if (!sb_rdonly(sb) || *flags & SB_RDONLY) { btrfs_warn(fs_info, "Remounting with free space tree only supported from read-only to read-write"); + /* + * if we aren't building the free space tree, reset + * the space cache options to what they were before + */ + btrfs_clear_opt(fs_info->mount_opt, FREE_SPACE_TREE); + if (btrfs_free_space_cache_v1_active(fs_info)) + btrfs_set_opt(fs_info->mount_opt, SPACE_CACHE); create_fst = false; } } diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c index 52ada47aff50..7b4e5d031744 100644 --- a/fs/btrfs/transaction.c +++ b/fs/btrfs/transaction.c @@ -1761,6 +1761,8 @@ static void update_super_roots(struct btrfs_fs_info *fs_info) super->root_level = root_item->level; if (btrfs_test_opt(fs_info, SPACE_CACHE)) super->cache_generation = root_item->generation; + else + super->cache_generation = 0; if (test_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags)) super->uuid_tree_generation = root_item->generation; } From patchwork Thu Sep 17 18:13:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Boris Burkov X-Patchwork-Id: 11783249 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9B01C746 for ; Thu, 17 Sep 2020 18:31:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3E7B7221F1 for ; Thu, 17 Sep 2020 18:31:17 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=bur.io header.i=@bur.io header.b="W7x0amnZ"; dkim=temperror (0-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="NPgYkHAC" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726548AbgIQSbP (ORCPT ); Thu, 17 Sep 2020 14:31:15 -0400 Received: from wout2-smtp.messagingengine.com ([64.147.123.25]:35359 "EHLO wout2-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726489AbgIQSbF (ORCPT ); Thu, 17 Sep 2020 14:31:05 -0400 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id 1EA223CC; Thu, 17 Sep 2020 14:14:12 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Thu, 17 Sep 2020 14:14:12 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bur.io; h=from :to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; s=fm1; bh=c4o5GPfZS6ZG2 4fW4YueA0GGS4pFbyZh70UDaE1HvWM=; b=W7x0amnZRQhxe7uDIAgoyXLOo4ocS B5hYKLltvkd02ce8/lDydzteR18TKmZAQH1tjf345DDc1vdwkoeP0c44XmyJhRKW 4AzNds3IhDYbYtmggFNb9twMBn0xo88iVlr1etpxQsQqswjeeReH7yPNt9rRR0QX +K5lXrR9wfpENFaTCGDmGwadDV0Tznhip0TcaUEjRsvwPTfiM4grKLEAPzfgdm3D W53ECAev9sVR68WGLQRMEceqaynQ/ubZsqjOpwv0susyof3XNBfr4tVN4dbI7vFV Y8qkKQqLC+jGTVIMFo4+V+Lc9M4bhKx+mqLyqHZc1H3zTegbeAdUD9/pg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; bh=c4o5GPfZS6ZG24fW4YueA0GGS4pFbyZh70UDaE1HvWM=; b=NPgYkHAC /GMgdk80Gkk7BpsvCMbcgzk1HfnaUmbktKNrW0v82q18SkJYFJhRzHysJBs5yJpM m2hwA1RE1KUXFEcUZIINS3+21bD6F9kk/ZWJhbjDWPkMUcWvxYyskqEogcky9m6V IHov9hw/PSAwUPcIW60a4XUGtcLxtct9xLGJmZOHMuocEHYqteOhkbFGM805u+fh tcVSkrvGJXo0Hl2nbNFt43IZkhj6iBqPWNGSIEBIFMJZr0JYD9A8q+BWxuAxuW6i E8JLxIJMl/cpfBlaN6xhAZpxmVanytNrFmn/t1qv5IucNYgnujOSWWGLWPdsndOE Y9BqmYAEiWq0lw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrtdeggdduvdegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefhvffufffkofgjfhgggfestdekre dtredttdenucfhrhhomhepuehorhhishcuuehurhhkohhvuceosghorhhishessghurhdr ihhoqeenucggtffrrghtthgvrhhnpeetheehudfgleelhfehffettedtfeelvdfghfelke efgefgvdevgefffedvtdefgeenucffohhmrghinhepghhithhhuhgsrdgtohhmnecukfhp peduieefrdduudegrddufedvrdefnecuvehluhhsthgvrhfuihiivgepvdenucfrrghrrg hmpehmrghilhhfrhhomhepsghorhhishessghurhdrihho X-ME-Proxy: Received: from localhost (unknown [163.114.132.3]) by mail.messagingengine.com (Postfix) with ESMTPA id 157123064686; Thu, 17 Sep 2020 14:14:11 -0400 (EDT) From: Boris Burkov To: linux-btrfs@vger.kernel.org, kernel-team@fb.com Cc: Boris Burkov Subject: [PATCH v3 3/4] btrfs: remove free space items when creating free space tree Date: Thu, 17 Sep 2020 11:13:40 -0700 Message-Id: X-Mailer: git-send-email 2.24.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org When the file system transitions from space cache v1 to v2 it removes the old cached data, but does not remove the FREE_SPACE items nor the free space inodes they point to. This doesn't cause any issues besides being a bit inefficient, since these items no longer do anything useful. To fix it, as part of populating the free space tree, destroy each block group's free space item and free space inode. This code is lifted from the existing code for removing them when removing the block group. References: https://github.com/btrfs/btrfs-todo/issues/5 Signed-off-by: Boris Burkov Reviewed-by: Josef Bacik --- v3: - pass in optional inode to btrfs_remove_free_space_inode, which fixes the bug of not issuing an iput for it in the bg delete case. - fix bug where the orphan generated by fst creation could not be cleaned up, because delayed_iput had an outstanding reference v2: - remove_free_space_inode -> btrfs_remove_free_space_inode - undo sinful whitespace change fs/btrfs/block-group.c | 39 ++----------------------- fs/btrfs/disk-io.c | 9 ++++++ fs/btrfs/free-space-cache.c | 58 +++++++++++++++++++++++++++++++++++++ fs/btrfs/free-space-cache.h | 3 ++ fs/btrfs/free-space-tree.c | 3 ++ 5 files changed, 75 insertions(+), 37 deletions(-) diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c index 01e8ba1da1d3..717b3435c88e 100644 --- a/fs/btrfs/block-group.c +++ b/fs/btrfs/block-group.c @@ -892,8 +892,6 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans, struct btrfs_path *path; struct btrfs_block_group *block_group; struct btrfs_free_cluster *cluster; - struct btrfs_root *tree_root = fs_info->tree_root; - struct btrfs_key key; struct inode *inode; struct kobject *kobj = NULL; int ret; @@ -971,42 +969,9 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans, spin_unlock(&trans->transaction->dirty_bgs_lock); mutex_unlock(&trans->transaction->cache_write_mutex); - if (!IS_ERR(inode)) { - ret = btrfs_orphan_add(trans, BTRFS_I(inode)); - if (ret) { - btrfs_add_delayed_iput(inode); - goto out; - } - clear_nlink(inode); - /* One for the block groups ref */ - spin_lock(&block_group->lock); - if (block_group->iref) { - block_group->iref = 0; - block_group->inode = NULL; - spin_unlock(&block_group->lock); - iput(inode); - } else { - spin_unlock(&block_group->lock); - } - /* One for our lookup ref */ - btrfs_add_delayed_iput(inode); - } - - key.objectid = BTRFS_FREE_SPACE_OBJECTID; - key.type = 0; - key.offset = block_group->start; - - ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1); - if (ret < 0) + ret = btrfs_remove_free_space_inode(trans, inode, block_group); + if (ret) goto out; - if (ret > 0) - btrfs_release_path(path); - if (ret == 0) { - ret = btrfs_del_item(trans, tree_root, path); - if (ret) - goto out; - btrfs_release_path(path); - } spin_lock(&fs_info->block_group_cache_lock); rb_erase(&block_group->cache_node, diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c index ade92e93e63f..775d29565665 100644 --- a/fs/btrfs/disk-io.c +++ b/fs/btrfs/disk-io.c @@ -3333,6 +3333,15 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device close_ctree(fs_info); return ret; } + /* + * Creating the free space tree creates inode orphan items and + * delayed iputs when it deletes the free space inodes. Later in + * open_ctree, we run btrfs_orphan_cleanup which tries to clean + * up the orphan items. However, the outstanding references on + * the inodes from the delayed iputs causes the cleanup to fail. + * To fix it, force going through the delayed iputs here. + */ + btrfs_run_delayed_iputs(fs_info); } if ((bool)btrfs_test_opt(fs_info, SPACE_CACHE) != diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c index 25420d51039c..6e1bbe87d734 100644 --- a/fs/btrfs/free-space-cache.c +++ b/fs/btrfs/free-space-cache.c @@ -207,6 +207,64 @@ int create_free_space_inode(struct btrfs_trans_handle *trans, ino, block_group->start); } +/* + * inode is an optional sink: if it is NULL, btrfs_remove_free_space_inode + * handles lookup, otherwise it takes ownership and iputs the inode. + * Don't reuse an inode pointer after passing it into this function. + */ +int btrfs_remove_free_space_inode(struct btrfs_trans_handle *trans, + struct inode *inode, + struct btrfs_block_group *block_group) +{ + struct btrfs_path *path; + struct btrfs_key key; + int ret = 0; + path = btrfs_alloc_path(); + if (!path) + return -ENOMEM; + + if (!inode) { + inode = lookup_free_space_inode(block_group, path); + } + if (IS_ERR(inode)) { + if (PTR_ERR(inode) != -ENOENT) + ret = PTR_ERR(inode); + goto out; + } + ret = btrfs_orphan_add(trans, BTRFS_I(inode)); + if (ret) { + btrfs_add_delayed_iput(inode); + goto out; + } + clear_nlink(inode); + /* One for the block groups ref */ + spin_lock(&block_group->lock); + if (block_group->iref) { + block_group->iref = 0; + block_group->inode = NULL; + spin_unlock(&block_group->lock); + iput(inode); + } else { + spin_unlock(&block_group->lock); + } + /* One for the lookup ref */ + btrfs_add_delayed_iput(inode); + + key.objectid = BTRFS_FREE_SPACE_OBJECTID; + key.type = 0; + key.offset = block_group->start; + ret = btrfs_search_slot(trans, trans->fs_info->tree_root, &key, path, -1, 1); + if (ret) { + if (ret > 0) + ret = 0; + goto out; + } + ret = btrfs_del_item(trans, trans->fs_info->tree_root, path); +out: + btrfs_free_path(path); + return ret; +} + int btrfs_check_trunc_cache_free_space(struct btrfs_fs_info *fs_info, struct btrfs_block_rsv *rsv) { diff --git a/fs/btrfs/free-space-cache.h b/fs/btrfs/free-space-cache.h index 5fbdbd2fe740..0c01c53fce82 100644 --- a/fs/btrfs/free-space-cache.h +++ b/fs/btrfs/free-space-cache.h @@ -84,6 +84,9 @@ struct inode *lookup_free_space_inode(struct btrfs_block_group *block_group, int create_free_space_inode(struct btrfs_trans_handle *trans, struct btrfs_block_group *block_group, struct btrfs_path *path); +int btrfs_remove_free_space_inode(struct btrfs_trans_handle *trans, + struct inode *inode, + struct btrfs_block_group *block_group); int btrfs_check_trunc_cache_free_space(struct btrfs_fs_info *fs_info, struct btrfs_block_rsv *rsv); diff --git a/fs/btrfs/free-space-tree.c b/fs/btrfs/free-space-tree.c index 6b9faf3b0e96..d5926d36bf73 100644 --- a/fs/btrfs/free-space-tree.c +++ b/fs/btrfs/free-space-tree.c @@ -1165,6 +1165,9 @@ int btrfs_create_free_space_tree(struct btrfs_fs_info *fs_info) block_group = rb_entry(node, struct btrfs_block_group, cache_node); ret = populate_free_space_tree(trans, block_group); + if (ret) + goto abort; + ret = btrfs_remove_free_space_inode(trans, NULL, block_group); if (ret) goto abort; node = rb_next(node); From patchwork Thu Sep 17 18:13:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Boris Burkov X-Patchwork-Id: 11783251 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D0FC0746 for ; Thu, 17 Sep 2020 18:31:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AF591206CA for ; Thu, 17 Sep 2020 18:31:31 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=bur.io header.i=@bur.io header.b="aP3Li1Fq"; dkim=temperror (0-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="Th6aAXec" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726343AbgIQSbN (ORCPT ); Thu, 17 Sep 2020 14:31:13 -0400 Received: from wout2-smtp.messagingengine.com ([64.147.123.25]:55691 "EHLO wout2-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726496AbgIQSbI (ORCPT ); Thu, 17 Sep 2020 14:31:08 -0400 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id 4821F95D; Thu, 17 Sep 2020 14:14:18 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Thu, 17 Sep 2020 14:14:18 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bur.io; h=from :to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; s=fm1; bh=3TNmGDPNYcSrl uzWTGYLMRVlfFber1rMdlo8lV9Jf7M=; b=aP3Li1Fq6eF985/BFxXDiKnPeeLg5 LrSQMjBT2ZcO4R638T5Lz3OvgApuV1li73vUPmd3UOraNPlHUwK+d0G7LjkbLA+o k03XwoR+v6VswPEQuOSgcguPFV/WwO7ZnMENCLRp/J+y0F5Hu/OWJTeDHjuM32vX kKJl3KAwlEo3G/Mp26KLTftRWQfI7ykSpmCY66Lw+0BXa0HaCS8ZXvt+r6zBTtr8 NU1n9ND/5c+q7qiouD1eOIwqDqfRyRqax0k/krQYqa529Y1O+PbKS49YNHjqssap 3mvOLRBIExdu+6uNgS8sBW6lkjqB+pTS3YFqt2fU62Loi9VPcCm8Xzv1w== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; bh=3TNmGDPNYcSrluzWTGYLMRVlfFber1rMdlo8lV9Jf7M=; b=Th6aAXec HxUYh4zRjHu1HBNAm5ckY8D8h04hwYJMJZhe+FJH3T9ptu2fsZpc7uDq6m29zo6W 5WNQai8pUqXSBseaxl7yhqpXb5hFt6XkI/L1hkYQ+0UvmUGY5oXjlv7wJCK9znkV v17Kj1BSudJ54FgOZ4d+i2rubzkmL+lNwEgTv7JDEbFRu1ZIpQQc4C4KS32CZIR3 ioZZLAGhZqa0Fh9j2iH8z61eMVQcN13hQfoyU/5j25YSfDyr8U2zEXSxwyDpic2/ DXK8yLbnFjA/YjMZGtaSETZ+X7qZWnlc6y0AJzsiiIutehMDzOTIljRwzoCDLhI4 UkfxNkbw8r9tzQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrtdeggdduvdegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefhvffufffkofgjfhgggfestdekre dtredttdenucfhrhhomhepuehorhhishcuuehurhhkohhvuceosghorhhishessghurhdr ihhoqeenucggtffrrghtthgvrhhnpeeiueffuedvieeujefhheeigfekvedujeejjeffve dvhedtudefiefhkeegueehleenucfkphepudeifedruddugedrudefvddrfeenucevlhhu shhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegsohhrihhssegsuh hrrdhioh X-ME-Proxy: Received: from localhost (unknown [163.114.132.3]) by mail.messagingengine.com (Postfix) with ESMTPA id 553B93064674; Thu, 17 Sep 2020 14:14:17 -0400 (EDT) From: Boris Burkov To: linux-btrfs@vger.kernel.org, kernel-team@fb.com Cc: Boris Burkov Subject: [PATCH 4/4] btrfs: skip space_cache v1 setup when not using it Date: Thu, 17 Sep 2020 11:13:41 -0700 Message-Id: <2e63f1a18438150a8d4029c6b77ec8a661b6a3f6.1600282812.git.boris@bur.io> X-Mailer: git-send-email 2.24.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org If we are not using space cache v1, we should not create the free space object or free space inodes. This comes up when we delete the existing free space objects/inodes when migrating to v2, only to see them get recreated for every dirtied block group. Signed-off-by: Boris Burkov Reviewed-by: Josef Bacik --- fs/btrfs/block-group.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c index 717b3435c88e..002b3b3d9a30 100644 --- a/fs/btrfs/block-group.c +++ b/fs/btrfs/block-group.c @@ -2308,6 +2308,9 @@ static int cache_save_setup(struct btrfs_block_group *block_group, int retries = 0; int ret = 0; + if (!btrfs_test_opt(fs_info, SPACE_CACHE)) + return 0; + /* * If this block group is smaller than 100 megs don't bother caching the * block group.