All of lore.kernel.org
 help / color / mirror / Atom feed
From: Naohiro Aota <naohiro.aota@wdc.com>
To: linux-btrfs@vger.kernel.org, dsterba@suse.com
Cc: hare@suse.com, linux-fsdevel@vger.kernel.org,
	Jens Axboe <axboe@kernel.dk>,
	Christoph Hellwig <hch@infradead.org>,
	"Darrick J. Wong" <darrick.wong@oracle.com>,
	Naohiro Aota <naohiro.aota@wdc.com>,
	Josef Bacik <josef@toxicpanda.com>,
	Johannes Thumshirn <johannes.thumshirn@wdc.com>
Subject: [PATCH v14 40/42] btrfs: extend zoned allocator to use dedicated tree-log block group
Date: Tue, 26 Jan 2021 11:25:18 +0900	[thread overview]
Message-ID: <932487e490aed6797414f34f225b190668020db7.1611627788.git.naohiro.aota@wdc.com> (raw)
In-Reply-To: <cover.1611627788.git.naohiro.aota@wdc.com>

This is the 1/3 patch to enable tree log on ZONED mode.

The tree-log feature does not work on ZONED mode as is. Blocks for a
tree-log tree are allocated mixed with other metadata blocks, and btrfs
writes and syncs the tree-log blocks to devices at the time of fsync(),
which is different timing from a global transaction commit. As a result,
both writing tree-log blocks and writing other metadata blocks become
non-sequential writes that ZONED mode must avoid.

We can introduce a dedicated block group for tree-log blocks so that
tree-log blocks and other metadata blocks can be separated write streams.
As a result, each write stream can now be written to devices separately.
"fs_info->treelog_bg" tracks the dedicated block group and btrfs assign
"treelog_bg" on-demand on tree-log block allocation time.

This commit extends the zoned block allocator to use the block group.

Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
---
 fs/btrfs/block-group.c |  2 ++
 fs/btrfs/ctree.h       |  2 ++
 fs/btrfs/disk-io.c     |  1 +
 fs/btrfs/extent-tree.c | 75 +++++++++++++++++++++++++++++++++++++++---
 fs/btrfs/zoned.h       | 14 ++++++++
 5 files changed, 90 insertions(+), 4 deletions(-)

diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index 7facc4439116..7f90e2fd39a5 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -901,6 +901,8 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
 	btrfs_return_cluster_to_free_space(block_group, cluster);
 	spin_unlock(&cluster->refill_lock);
 
+	btrfs_clear_treelog_bg(block_group);
+
 	path = btrfs_alloc_path();
 	if (!path) {
 		ret = -ENOMEM;
diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 37afe3f49045..7c04f33b3e44 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -976,6 +976,8 @@ struct btrfs_fs_info {
 	/* Max size to emit ZONE_APPEND write command */
 	u64 max_zone_append_size;
 	struct mutex zoned_meta_io_lock;
+	spinlock_t treelog_bg_lock;
+	u64 treelog_bg;
 
 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
 	spinlock_t ref_verify_lock;
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 2e2f09a46f45..c3b5cfe4d928 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -2722,6 +2722,7 @@ void btrfs_init_fs_info(struct btrfs_fs_info *fs_info)
 	spin_lock_init(&fs_info->super_lock);
 	spin_lock_init(&fs_info->buffer_lock);
 	spin_lock_init(&fs_info->unused_bgs_lock);
+	spin_lock_init(&fs_info->treelog_bg_lock);
 	rwlock_init(&fs_info->tree_mod_log_lock);
 	mutex_init(&fs_info->unused_bg_unpin_mutex);
 	mutex_init(&fs_info->delete_unused_bgs_mutex);
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index f73f39bd68c0..7ba91b175450 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -3522,6 +3522,9 @@ struct find_free_extent_ctl {
 	bool have_caching_bg;
 	bool orig_have_caching_bg;
 
+	/* Allocation is called for tree-log */
+	bool for_treelog;
+
 	/* RAID index, converted from flags */
 	int index;
 
@@ -3750,6 +3753,22 @@ static int do_allocation_clustered(struct btrfs_block_group *block_group,
 	return find_free_extent_unclustered(block_group, ffe_ctl);
 }
 
+/*
+ * Tree-log Block Group Locking
+ * ============================
+ *
+ * fs_info::treelog_bg_lock protects the fs_info::treelog_bg which
+ * indicates the starting address of a block group, which is reserved only
+ * for tree-log metadata.
+ *
+ * Lock nesting
+ * ============
+ *
+ * space_info::lock
+ *   block_group::lock
+ *     fs_info::treelog_bg_lock
+ */
+
 /*
  * Simple allocator for sequential only block group. It only allows
  * sequential allocation. No need to play with trees. This function
@@ -3759,23 +3778,54 @@ static int do_allocation_zoned(struct btrfs_block_group *block_group,
 			       struct find_free_extent_ctl *ffe_ctl,
 			       struct btrfs_block_group **bg_ret)
 {
+	struct btrfs_fs_info *fs_info = block_group->fs_info;
 	struct btrfs_space_info *space_info = block_group->space_info;
 	struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
 	u64 start = block_group->start;
 	u64 num_bytes = ffe_ctl->num_bytes;
 	u64 avail;
+	u64 bytenr = block_group->start;
+	u64 log_bytenr;
 	int ret = 0;
+	bool skip;
 
 	ASSERT(btrfs_is_zoned(block_group->fs_info));
 
+	/*
+	 * Do not allow non-tree-log blocks in the dedicated tree-log block
+	 * group, and vice versa.
+	 */
+	spin_lock(&fs_info->treelog_bg_lock);
+	log_bytenr = fs_info->treelog_bg;
+	skip = log_bytenr && ((ffe_ctl->for_treelog && bytenr != log_bytenr) ||
+			      (!ffe_ctl->for_treelog && bytenr == log_bytenr));
+	spin_unlock(&fs_info->treelog_bg_lock);
+	if (skip)
+		return 1;
+
 	spin_lock(&space_info->lock);
 	spin_lock(&block_group->lock);
+	spin_lock(&fs_info->treelog_bg_lock);
+
+	ASSERT(!ffe_ctl->for_treelog ||
+	       block_group->start == fs_info->treelog_bg ||
+	       fs_info->treelog_bg == 0);
 
 	if (block_group->ro) {
 		ret = 1;
 		goto out;
 	}
 
+	/*
+	 * Do not allow currently using block group to be tree-log dedicated
+	 * block group.
+	 */
+	if (ffe_ctl->for_treelog && !fs_info->treelog_bg &&
+	    (block_group->used || block_group->reserved)) {
+		ret = 1;
+		goto out;
+	}
+
 	avail = block_group->length - block_group->alloc_offset;
 	if (avail < num_bytes) {
 		if (ffe_ctl->max_extent_size < avail) {
@@ -3790,6 +3840,9 @@ static int do_allocation_zoned(struct btrfs_block_group *block_group,
 		goto out;
 	}
 
+	if (ffe_ctl->for_treelog && !fs_info->treelog_bg)
+		fs_info->treelog_bg = block_group->start;
+
 	ffe_ctl->found_offset = start + block_group->alloc_offset;
 	block_group->alloc_offset += num_bytes;
 	spin_lock(&ctl->tree_lock);
@@ -3804,6 +3857,9 @@ static int do_allocation_zoned(struct btrfs_block_group *block_group,
 	ffe_ctl->search_start = ffe_ctl->found_offset;
 
 out:
+	if (ret && ffe_ctl->for_treelog)
+		fs_info->treelog_bg = 0;
+	spin_unlock(&fs_info->treelog_bg_lock);
 	spin_unlock(&block_group->lock);
 	spin_unlock(&space_info->lock);
 	return ret;
@@ -4053,7 +4109,12 @@ static int prepare_allocation(struct btrfs_fs_info *fs_info,
 		return prepare_allocation_clustered(fs_info, ffe_ctl,
 						    space_info, ins);
 	case BTRFS_EXTENT_ALLOC_ZONED:
-		/* nothing to do */
+		if (ffe_ctl->for_treelog) {
+			spin_lock(&fs_info->treelog_bg_lock);
+			if (fs_info->treelog_bg)
+				ffe_ctl->hint_byte = fs_info->treelog_bg;
+			spin_unlock(&fs_info->treelog_bg_lock);
+		}
 		return 0;
 	default:
 		BUG();
@@ -4097,6 +4158,7 @@ static noinline int find_free_extent(struct btrfs_root *root,
 	struct find_free_extent_ctl ffe_ctl = {0};
 	struct btrfs_space_info *space_info;
 	bool full_search = false;
+	bool for_treelog = root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID;
 
 	WARN_ON(num_bytes < fs_info->sectorsize);
 
@@ -4110,6 +4172,7 @@ static noinline int find_free_extent(struct btrfs_root *root,
 	ffe_ctl.orig_have_caching_bg = false;
 	ffe_ctl.found_offset = 0;
 	ffe_ctl.hint_byte = hint_byte_orig;
+	ffe_ctl.for_treelog = for_treelog;
 	ffe_ctl.policy = BTRFS_EXTENT_ALLOC_CLUSTERED;
 
 	/* For clustered allocation */
@@ -4184,8 +4247,11 @@ static noinline int find_free_extent(struct btrfs_root *root,
 		struct btrfs_block_group *bg_ret;
 
 		/* If the block group is read-only, we can skip it entirely. */
-		if (unlikely(block_group->ro))
+		if (unlikely(block_group->ro)) {
+			if (for_treelog)
+				btrfs_clear_treelog_bg(block_group);
 			continue;
+		}
 
 		btrfs_grab_block_group(block_group, delalloc);
 		ffe_ctl.search_start = block_group->start;
@@ -4373,6 +4439,7 @@ int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes,
 	bool final_tried = num_bytes == min_alloc_size;
 	u64 flags;
 	int ret;
+	bool for_treelog = root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID;
 
 	flags = get_alloc_profile_by_root(root, is_data);
 again:
@@ -4396,8 +4463,8 @@ int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes,
 
 			sinfo = btrfs_find_space_info(fs_info, flags);
 			btrfs_err(fs_info,
-				  "allocation failed flags %llu, wanted %llu",
-				  flags, num_bytes);
+			"allocation failed flags %llu, wanted %llu treelog %d",
+				  flags, num_bytes, for_treelog);
 			if (sinfo)
 				btrfs_dump_space_info(fs_info, sinfo,
 						      num_bytes, 1);
diff --git a/fs/btrfs/zoned.h b/fs/btrfs/zoned.h
index 8c203c0425e0..52789da61fa3 100644
--- a/fs/btrfs/zoned.h
+++ b/fs/btrfs/zoned.h
@@ -7,6 +7,7 @@
 #include <linux/blkdev.h>
 #include "volumes.h"
 #include "disk-io.h"
+#include "block-group.h"
 
 struct btrfs_zoned_device_info {
 	/*
@@ -292,4 +293,17 @@ static inline void btrfs_zoned_meta_io_unlock(struct btrfs_fs_info *fs_info)
 	mutex_unlock(&fs_info->zoned_meta_io_lock);
 }
 
+static inline void btrfs_clear_treelog_bg(struct btrfs_block_group *bg)
+{
+	struct btrfs_fs_info *fs_info = bg->fs_info;
+
+	if (!btrfs_is_zoned(fs_info))
+		return;
+
+	spin_lock(&fs_info->treelog_bg_lock);
+	if (fs_info->treelog_bg == bg->start)
+		fs_info->treelog_bg = 0;
+	spin_unlock(&fs_info->treelog_bg_lock);
+}
+
 #endif
-- 
2.27.0


  parent reply	other threads:[~2021-01-26  5:50 UTC|newest]

Thread overview: 72+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-26  2:24 [PATCH v14 00/42] btrfs: zoned block device support Naohiro Aota
2021-01-26  2:24 ` [PATCH v14 01/42] block: add bio_add_zone_append_page Naohiro Aota
2021-01-26 16:08   ` Jens Axboe
2021-01-26  2:24 ` [PATCH v14 02/42] iomap: support REQ_OP_ZONE_APPEND Naohiro Aota
2021-01-26  2:24 ` [PATCH v14 03/42] btrfs: defer loading zone info after opening trees Naohiro Aota
2021-01-30 22:09   ` Anand Jain
2021-01-26  2:24 ` [PATCH v14 04/42] btrfs: use regular SB location on emulated zoned mode Naohiro Aota
2021-01-30 22:28   ` Anand Jain
2021-01-26  2:24 ` [PATCH v14 05/42] btrfs: release path before calling into btrfs_load_block_group_zone_info Naohiro Aota
2021-01-30 23:21   ` Anand Jain
2021-01-26  2:24 ` [PATCH v14 06/42] btrfs: do not load fs_info->zoned from incompat flag Naohiro Aota
2021-01-30 23:40   ` Anand Jain
2021-01-26  2:24 ` [PATCH v14 07/42] btrfs: disallow fitrim in ZONED mode Naohiro Aota
2021-01-30 23:44   ` Anand Jain
2021-01-26  2:24 ` [PATCH v14 08/42] btrfs: allow zoned mode on non-zoned block devices Naohiro Aota
2021-01-31  1:17   ` Anand Jain
2021-02-01 11:06     ` Johannes Thumshirn
2021-02-02  1:49   ` Anand Jain
2021-01-26  2:24 ` [PATCH v14 09/42] btrfs: implement zoned chunk allocator Naohiro Aota
2021-01-26  2:24 ` [PATCH v14 10/42] btrfs: verify device extent is aligned to zone Naohiro Aota
2021-01-26  2:24 ` [PATCH v14 11/42] btrfs: load zone's allocation offset Naohiro Aota
2021-01-26  2:24 ` [PATCH v14 12/42] btrfs: calculate allocation offset for conventional zones Naohiro Aota
2021-01-27 18:03   ` Josef Bacik
2021-02-03  5:19   ` Anand Jain
2021-02-03  6:10     ` Damien Le Moal
2021-02-03  6:56       ` Anand Jain
2021-02-03  7:10         ` Damien Le Moal
2021-01-26  2:24 ` [PATCH v14 13/42] btrfs: track unusable bytes for zones Naohiro Aota
2021-01-27 18:06   ` Josef Bacik
2021-01-26  2:24 ` [PATCH v14 14/42] btrfs: do sequential extent allocation in ZONED mode Naohiro Aota
2021-01-26  2:24 ` [PATCH v14 15/42] btrfs: redirty released extent buffers " Naohiro Aota
2021-01-26  2:24 ` [PATCH v14 16/42] btrfs: advance allocation pointer after tree log node Naohiro Aota
2021-01-26  2:24 ` [PATCH v14 17/42] btrfs: enable to mount ZONED incompat flag Naohiro Aota
2021-01-31 12:21   ` Anand Jain
2021-01-26  2:24 ` [PATCH v14 18/42] btrfs: reset zones of unused block groups Naohiro Aota
2021-01-26  2:24 ` [PATCH v14 19/42] btrfs: extract page adding function Naohiro Aota
2021-01-26  2:24 ` [PATCH v14 20/42] btrfs: use bio_add_zone_append_page for zoned btrfs Naohiro Aota
2021-01-26  2:24 ` [PATCH v14 21/42] btrfs: handle REQ_OP_ZONE_APPEND as writing Naohiro Aota
2021-01-26  2:25 ` [PATCH v14 22/42] btrfs: split ordered extent when bio is sent Naohiro Aota
2021-01-27 19:00   ` Josef Bacik
2021-01-26  2:25 ` [PATCH v14 23/42] btrfs: check if bio spans across an ordered extent Naohiro Aota
2021-01-26  2:25 ` [PATCH v14 24/42] btrfs: extend btrfs_rmap_block for specifying a device Naohiro Aota
2021-01-26  2:25 ` [PATCH v14 25/42] btrfs: cache if block-group is on a sequential zone Naohiro Aota
2021-01-26  2:25 ` [PATCH v14 26/42] btrfs: save irq flags when looking up an ordered extent Naohiro Aota
2021-01-26  2:25 ` [PATCH v14 27/42] btrfs: use ZONE_APPEND write for ZONED btrfs Naohiro Aota
2021-01-26  2:25 ` [PATCH v14 28/42] btrfs: enable zone append writing for direct IO Naohiro Aota
2021-01-26  2:25 ` [PATCH v14 29/42] btrfs: introduce dedicated data write path for ZONED mode Naohiro Aota
2021-02-02 15:00   ` David Sterba
2021-02-04  8:25     ` Naohiro Aota
2021-01-26  2:25 ` [PATCH v14 30/42] btrfs: serialize meta IOs on " Naohiro Aota
2021-01-26  2:25 ` [PATCH v14 31/42] btrfs: wait existing extents before truncating Naohiro Aota
2021-01-26  2:25 ` [PATCH v14 32/42] btrfs: avoid async metadata checksum on ZONED mode Naohiro Aota
2021-02-02 14:54   ` David Sterba
2021-02-02 16:50     ` Johannes Thumshirn
2021-02-02 19:28       ` David Sterba
2021-01-26  2:25 ` [PATCH v14 33/42] btrfs: mark block groups to copy for device-replace Naohiro Aota
2021-01-26  2:25 ` [PATCH v14 34/42] btrfs: implement cloning for ZONED device-replace Naohiro Aota
2021-01-26  2:25 ` [PATCH v14 35/42] btrfs: implement copying " Naohiro Aota
2021-01-26  2:25 ` [PATCH v14 36/42] btrfs: support dev-replace in ZONED mode Naohiro Aota
2021-01-26  2:25 ` [PATCH v14 37/42] btrfs: enable relocation " Naohiro Aota
2021-01-26  2:25 ` [PATCH v14 38/42] btrfs: relocate block group to repair IO failure in ZONED Naohiro Aota
2021-01-26  2:25 ` [PATCH v14 39/42] btrfs: split alloc_log_tree() Naohiro Aota
2021-01-26  2:25 ` Naohiro Aota [this message]
2021-01-26  2:25 ` [PATCH v14 41/42] btrfs: serialize log transaction on ZONED mode Naohiro Aota
2021-01-27 19:01   ` Josef Bacik
2021-02-01 15:48   ` Filipe Manana
2021-01-26  2:25 ` [PATCH v14 42/42] btrfs: reorder log node allocation Naohiro Aota
2021-02-01 15:48   ` Filipe Manana
2021-02-01 15:54     ` Johannes Thumshirn
2021-01-29  7:56 ` [PATCH v14 00/42] btrfs: zoned block device support Johannes Thumshirn
2021-01-29 20:44   ` David Sterba
2021-01-30 11:30     ` 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=932487e490aed6797414f34f225b190668020db7.1611627788.git.naohiro.aota@wdc.com \
    --to=naohiro.aota@wdc.com \
    --cc=axboe@kernel.dk \
    --cc=darrick.wong@oracle.com \
    --cc=dsterba@suse.com \
    --cc=hare@suse.com \
    --cc=hch@infradead.org \
    --cc=johannes.thumshirn@wdc.com \
    --cc=josef@toxicpanda.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-fsdevel@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.