linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Naohiro Aota <naohiro.aota@wdc.com>
To: linux-btrfs@vger.kernel.org
Cc: linux-block@vger.kernel.org, Naohiro Aota <naohiro.aota@wdc.com>
Subject: [PATCH 10/13] btrfs: zoned: activate metadata BG on flush_space
Date: Sat,  9 Jul 2022 08:18:47 +0900	[thread overview]
Message-ID: <9356a688352bf220fba3dda1deff0486055d42ee.1657321126.git.naohiro.aota@wdc.com> (raw)
In-Reply-To: <cover.1657321126.git.naohiro.aota@wdc.com>

For metadata space on zoned btrfs, reaching ALLOC_CHUNK{,_FORCE} means we
don't have enough space left in the active_total_bytes. Before allocating a
new chunk, we can try to activate an existing block group in this case.

Also, allocating a chunk is not enough to grant a ticket for metadata space
on zoned btrfs. We need to activate the block group to increase the
active_total_bytes.

btrfs_zoned_activate_one_bg() implements the activation feature. It will
activate a block group by (maybe) finishing a block group. It will give up
activating a block group if it cannot finish any block group.

CC: stable@vger.kernel.org # 5.16+
Fixes: afba2bc036b0 ("btrfs: zoned: implement active zone tracking")
Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
---
 fs/btrfs/space-info.c | 30 ++++++++++++++++++++++++
 fs/btrfs/zoned.c      | 53 +++++++++++++++++++++++++++++++++++++++++++
 fs/btrfs/zoned.h      | 10 ++++++++
 3 files changed, 93 insertions(+)

diff --git a/fs/btrfs/space-info.c b/fs/btrfs/space-info.c
index 7183a8dc9b34..b99e3c32c07d 100644
--- a/fs/btrfs/space-info.c
+++ b/fs/btrfs/space-info.c
@@ -9,6 +9,7 @@
 #include "ordered-data.h"
 #include "transaction.h"
 #include "block-group.h"
+#include "zoned.h"
 
 /*
  * HOW DOES SPACE RESERVATION WORK
@@ -724,6 +725,18 @@ static void flush_space(struct btrfs_fs_info *fs_info,
 		break;
 	case ALLOC_CHUNK:
 	case ALLOC_CHUNK_FORCE:
+		/*
+		 * For metadata space on zoned btrfs, reaching here means we
+		 * don't have enough space left in active_total_bytes. Try to
+		 * activate a block group first, because we may have inactive
+		 * block group already allocated.
+		 */
+		ret = btrfs_zoned_activate_one_bg(fs_info, space_info, false);
+		if (ret < 0)
+			break;
+		else if (ret == 1)
+			break;
+
 		trans = btrfs_join_transaction(root);
 		if (IS_ERR(trans)) {
 			ret = PTR_ERR(trans);
@@ -734,6 +747,23 @@ static void flush_space(struct btrfs_fs_info *fs_info,
 				(state == ALLOC_CHUNK) ? CHUNK_ALLOC_NO_FORCE :
 					CHUNK_ALLOC_FORCE);
 		btrfs_end_transaction(trans);
+
+		/*
+		 * For metadata space on zoned btrfs, allocating a new chunk is
+		 * not enough. We still need to activate the block group. Active
+		 * the newly allocated block group by (maybe) finishing a block
+		 * group.
+		 */
+		if (ret == 1) {
+			ret = btrfs_zoned_activate_one_bg(fs_info, space_info, true);
+			/*
+			 * Revert to the original ret regardless we could finish
+			 * one block group or not.
+			 */
+			if (ret >= 0)
+				ret = 1;
+		}
+
 		if (ret > 0 || ret == -ENOSPC)
 			ret = 0;
 		break;
diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index 44a4b9e7dae9..67098f3fcd14 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -2225,3 +2225,56 @@ int btrfs_zone_finish_one_bg(struct btrfs_fs_info *fs_info)
 
 	return ret < 0 ? ret : 1;
 }
+
+int btrfs_zoned_activate_one_bg(struct btrfs_fs_info *fs_info,
+				struct btrfs_space_info *space_info,
+				bool do_finish)
+{
+	struct btrfs_block_group *bg;
+	bool need_finish;
+	int index;
+
+	if (!btrfs_is_zoned(fs_info) || (space_info->flags & BTRFS_BLOCK_GROUP_DATA))
+		return 0;
+
+	/* No more block group to activate */
+	if (space_info->active_total_bytes == space_info->total_bytes)
+		return 0;
+
+	for (;;) {
+		int ret;
+
+		need_finish = false;
+		down_read(&space_info->groups_sem);
+		for (index = 0; index < BTRFS_NR_RAID_TYPES; index++) {
+			list_for_each_entry(bg, &space_info->block_groups[index], list) {
+				if (!spin_trylock(&bg->lock))
+					continue;
+				if (btrfs_zoned_bg_is_full(bg) || bg->zone_is_active) {
+					spin_unlock(&bg->lock);
+					continue;
+				}
+				spin_unlock(&bg->lock);
+
+				if (btrfs_zone_activate(bg)) {
+					up_read(&space_info->groups_sem);
+					return 1;
+				}
+
+				need_finish = true;
+			}
+		}
+		up_read(&space_info->groups_sem);
+
+		if (!do_finish || !need_finish)
+			break;
+
+		ret = btrfs_zone_finish_one_bg(fs_info);
+		if (ret == 0)
+			break;
+		if (ret < 0)
+			return ret;
+	}
+
+	return 0;
+}
diff --git a/fs/btrfs/zoned.h b/fs/btrfs/zoned.h
index 329d28e2fd8d..f7b0b9035fd6 100644
--- a/fs/btrfs/zoned.h
+++ b/fs/btrfs/zoned.h
@@ -81,6 +81,8 @@ bool btrfs_zoned_should_reclaim(struct btrfs_fs_info *fs_info);
 void btrfs_zoned_release_data_reloc_bg(struct btrfs_fs_info *fs_info, u64 logical,
 				       u64 length);
 int btrfs_zone_finish_one_bg(struct btrfs_fs_info *fs_info);
+int btrfs_zoned_activate_one_bg(struct btrfs_fs_info *fs_info,
+				struct btrfs_space_info *space_info, bool do_finish);
 #else /* CONFIG_BLK_DEV_ZONED */
 static inline int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos,
 				     struct blk_zone *zone)
@@ -256,6 +258,14 @@ static inline int btrfs_zone_finish_one_bg(struct btrfs_fs_info *fs_info)
 	return 1;
 }
 
+static inline int btrfs_zoned_activate_one_bg(struct btrfs_fs_info *fs_info,
+					      struct btrfs_space_info *space_info,
+					      bool do_finish)
+{
+	/* Consider all the BGs are active */
+	return 0;
+}
+
 #endif
 
 static inline bool btrfs_dev_is_sequential(struct btrfs_device *device, u64 pos)
-- 
2.35.1


  parent reply	other threads:[~2022-07-08 23:19 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-08 23:18 [PATCH 00/13] btrfs: zoned: fix active zone tracking issues Naohiro Aota
2022-07-08 23:18 ` [PATCH 01/13] block: add bdev_max_segments() helper Naohiro Aota
2022-07-09 16:10   ` Jens Axboe
2022-07-11  7:00   ` Christoph Hellwig
2022-07-08 23:18 ` [PATCH 02/13] btrfs: zoned: revive max_zone_append_bytes Naohiro Aota
2022-07-09 11:34   ` Johannes Thumshirn
2022-07-08 23:18 ` [PATCH 03/13] btrfs: replace BTRFS_MAX_EXTENT_SIZE with fs_info->max_extent_size Naohiro Aota
2022-07-09 11:36   ` Johannes Thumshirn
2022-07-11 15:30     ` David Sterba
2022-07-08 23:18 ` [PATCH 04/13] btrfs: convert count_max_extents() to use fs_info->max_extent_size Naohiro Aota
2022-07-09 11:37   ` Johannes Thumshirn
2022-07-08 23:18 ` [PATCH 05/13] btrfs: use fs_info->max_extent_size in get_extent_max_capacity() Naohiro Aota
2022-07-08 23:18 ` [PATCH 06/13] btrfs: let can_allocate_chunk return int Naohiro Aota
2022-07-08 23:18 ` [PATCH 07/13] btrfs: zoned: finish least available block group on data BG allocation Naohiro Aota
2022-07-11  6:45   ` Johannes Thumshirn
2022-07-08 23:18 ` [PATCH 08/13] btrfs: zoned: introduce space_info->active_total_bytes Naohiro Aota
2022-07-08 23:18 ` [PATCH 09/13] btrfs: zoned: disable metadata overcommit for zoned Naohiro Aota
2022-07-08 23:18 ` Naohiro Aota [this message]
2022-07-08 23:18 ` [PATCH 11/13] btrfs: zoned: activate necessary block group Naohiro Aota
2022-07-08 23:18 ` [PATCH 12/13] btrfs: zoned: write out partially allocated region Naohiro Aota
2022-07-08 23:18 ` [PATCH 13/13] btrfs: zoned: wait until zone is finished when allocation didn't progress Naohiro Aota
2022-07-11 20:29 ` [PATCH v2 00/13] btrfs: zoned: fix active zone tracking issues David Sterba
2022-07-12 20:32 ` David Sterba
  -- strict thread matches above, loose matches on Subject: below --
2022-07-04  4:58 [PATCH " Naohiro Aota
2022-07-04  4:58 ` [PATCH 10/13] btrfs: zoned: activate metadata BG on flush_space Naohiro Aota
2022-07-07 14:51   ` Johannes Thumshirn

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9356a688352bf220fba3dda1deff0486055d42ee.1657321126.git.naohiro.aota@wdc.com \
    --to=naohiro.aota@wdc.com \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).