Skip to contentSkip to conversation

btrfs: zoned block device support

PATCH Superseded Naohiro Aota 7 participants 119 messages Oct 30, 2020 – Nov 10, 2020 Page 2 of 2

Subsystems (4)

v9 of 15 Superseded Latest is v15 →

lore
Share & tools
lore
#107 lore raw

PATCH v9 38/41 btrfs: extend zoned allocator to use dedicated tree-log block group

In reply to Naohiro Aota

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.

---
 fs/btrfs/block-group.c |  7 +++++
 fs/btrfs/ctree.h       |  2 ++
 fs/btrfs/extent-tree.c | 63 +++++++++++++++++++++++++++++++++++++++---
 3 files changed, 68 insertions(+), 4 deletions(-)
fs/btrfs/block-group.c +7 -0
index 21e40046dce1..ac4fb954db28 100644
@@ -939,6 +939,13 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
 	btrfs_return_cluster_to_free_space(block_group, cluster);
 	spin_unlock(&cluster->refill_lock);
 
+	if (btrfs_is_zoned(fs_info)) {
+		spin_lock(&fs_info->treelog_bg_lock);
+		if (fs_info->treelog_bg == block_group->start)
+			fs_info->treelog_bg = 0;
+		spin_unlock(&fs_info->treelog_bg_lock);
+	}
+
 	path = btrfs_alloc_path();
 	if (!path) {
 		ret = -ENOMEM;
fs/btrfs/ctree.h +2 -0
index 736f679f1310..026d34c8f2ee 100644
@@ -956,6 +956,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;
fs/btrfs/extent-tree.c +59 -4

141 lines not on this page · raw message

Signature · 1 line
2.27.0
#108 lore raw

Re: [PATCH v9 38/41] btrfs: extend zoned allocator to use dedicated tree-log block group

Quoted 15 lines · Naohiro Aota

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.

Quoted 4 lines

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

If you're going to remove an entire block group from being allowed to be used for metadata you are going to need to account for it in the space_info, otherwise we're going to end up with nasty ENOSPC corners here.

But this begs the question, do we want the tree log for zoned? We could just commit the transaction and call it good enough. We lose performance, but zoned isn't necessarily about performance.

If we do then at a minimum we're going to need to remove this block group from the space info counters for metadata. Thanks,

Josef

#109 lore raw

Re: [PATCH v9 38/41] btrfs: extend zoned allocator to use dedicated tree-log block group

Quoted 16 lines · Josef Bacik
Quoted 15 lines · Naohiro Aota

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.

Quoted 6 lines
Quoted 4 lines

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

If you're going to remove an entire block group from being allowed to be used for metadata you are going to need to account for it in the space_info, otherwise we're going to end up with nasty ENOSPC corners here.

Indeed. I'll add a dedicated space_info for treelog or, at least, separate the block group from other metadata space_info. But, I'll address this later in v11.

Quoted 2 lines

But this begs the question, do we want the tree log for zoned? We could just commit the transaction and call it good enough. We lose performance, but zoned isn't necessarily about performance.

We have a large performance drop without tree-log (-o notreelog). Here is a dbench (32 clients) result on SMR HDD.

With treelog:    153.509  MB/s	
Without treelog:  21.9651 MB/s

So, there is 85% drop of the throughput. I think this degradation is too large.

Quoted 4 lines

If we do then at a minimum we're going to need to remove this block group from the space info counters for metadata. Thanks,

Josef

#110 lore raw

PATCH v9 39/41 btrfs: serialize log transaction on ZONED mode

In reply to Naohiro Aota

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

Since we can start more than one log transactions per subvolume simultaneously, nodes from multiple transactions can be allocated interleaved. Such mixed allocation results in non-sequential writes at the time of log transaction commit. The nodes of the global log root tree (fs_info->log_root_tree), also have the same mixed allocation problem.

This patch serializes log transactions by waiting for a committing transaction when someone tries to start a new transaction, to avoid the mixed allocation problem. We must also wait for running log transactions from another subvolume, but there is no easy way to detect which subvolume root is running a log transaction. So, this patch forbids starting a new log transaction when other subvolumes already allocated the global log root tree.

---
 fs/btrfs/tree-log.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)
fs/btrfs/tree-log.c +21 -1
index 5f585cf57383..826af2ff4740 100644
@@ -106,6 +106,7 @@ static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
 				       struct btrfs_root *log,
 				       struct btrfs_path *path,
 				       u64 dirid, int del_all);
+static void wait_log_commit(struct btrfs_root *root, int transid);
 
 /*
  * tree logging is a special write ahead log used to make sure that
@@ -140,16 +141,25 @@ static int start_log_trans(struct btrfs_trans_handle *trans,
 			   struct btrfs_log_ctx *ctx)
 {
 	struct btrfs_fs_info *fs_info = root->fs_info;
+	bool zoned = btrfs_is_zoned(fs_info);
 	int ret = 0;
 
 	mutex_lock(&root->log_mutex);
 
+again:
 	if (root->log_root) {
+		int index = (root->log_transid + 1) % 2;
+
 		if (btrfs_need_log_full_commit(trans)) {
 			ret = -EAGAIN;
 			goto out;
 		}
 
+		if (zoned && atomic_read(&root->log_commit[index])) {
+			wait_log_commit(root, root->log_transid - 1);
+			goto again;
+		}
+
 		if (!root->log_start_pid) {
 			clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
 			root->log_start_pid = current->pid;
@@ -158,7 +168,9 @@ static int start_log_trans(struct btrfs_trans_handle *trans,
 		}
 	} else {
 		mutex_lock(&fs_info->tree_log_mutex);
-		if (!fs_info->log_root_tree)
+		if (zoned && fs_info->log_root_tree)
+			ret = -EAGAIN;
+		else if (!fs_info->log_root_tree)
 			ret = btrfs_init_log_root_tree(trans, fs_info);
 		mutex_unlock(&fs_info->tree_log_mutex);
 		if (ret)
@@ -193,14 +205,22 @@ static int start_log_trans(struct btrfs_trans_handle *trans,
  */
 static int join_running_log_trans(struct btrfs_root *root)
 {
+	bool zoned = btrfs_is_zoned(root->fs_info);
 	int ret = -ENOENT;
 
 	if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state))
 		return ret;
 
 	mutex_lock(&root->log_mutex);
+again:
 	if (root->log_root) {
+		int index = (root->log_transid + 1) % 2;
+
 		ret = 0;
+		if (zoned && atomic_read(&root->log_commit[index])) {
+			wait_log_commit(root, root->log_transid - 1);
+			goto again;
+		}
 		atomic_inc(&root->log_writers);
 	}
 	mutex_unlock(&root->log_mutex);
Signature · 1 line
2.27.0
#111 lore raw

Re: [PATCH v9 39/41] btrfs: serialize log transaction on ZONED mode

Quoted 16 lines · Naohiro Aota

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

Since we can start more than one log transactions per subvolume simultaneously, nodes from multiple transactions can be allocated interleaved. Such mixed allocation results in non-sequential writes at the time of log transaction commit. The nodes of the global log root tree (fs_info->log_root_tree), also have the same mixed allocation problem.

This patch serializes log transactions by waiting for a committing transaction when someone tries to start a new transaction, to avoid the mixed allocation problem. We must also wait for running log transactions from another subvolume, but there is no easy way to detect which subvolume root is running a log transaction. So, this patch forbids starting a new log transaction when other subvolumes already allocated the global log root tree.

Quoted 1 line

This restriction makes the tree log really, really seem not worth it for zoned.
I'd rig up a fio test that does fsyncs into multiple subvolumes and tests this
vs just committing the transaction. If committing the transaction is better, just skip the log altogether for zoned. Thanks,

Josef

#112 lore raw

PATCH v9 40/41 btrfs: reorder log node allocation

In reply to Naohiro Aota

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

The allocation order of nodes of "fs_info->log_root_tree" and nodes of "root->log_root" is not the same as the writing order of them. So, the writing causes unaligned write errors.

This patch reorders the allocation of them by delaying allocation of the root node of "fs_info->log_root_tree," so that the node buffers can go out sequentially to devices.

---
 fs/btrfs/disk-io.c  |  6 ------
 fs/btrfs/tree-log.c | 24 ++++++++++++++++++------
 2 files changed, 18 insertions(+), 12 deletions(-)
fs/btrfs/disk-io.c +0 -6
index 70885f3d3321..2c2fa5ebfef1 100644
@@ -1255,16 +1255,10 @@ int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans,
 			     struct btrfs_fs_info *fs_info)
 {
 	struct btrfs_root *log_root;
-	int ret;
 
 	log_root = alloc_log_tree(trans, fs_info);
 	if (IS_ERR(log_root))
 		return PTR_ERR(log_root);
-	ret = btrfs_alloc_log_tree_node(trans, log_root);
-	if (ret) {
-		kfree(log_root);
-		return ret;
-	}
 	WARN_ON(fs_info->log_root_tree);
 	fs_info->log_root_tree = log_root;
 	return 0;
fs/btrfs/tree-log.c +18 -6
index 826af2ff4740..46ff92a18474 100644
@@ -3140,6 +3140,16 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
 	list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
 	root_log_ctx.log_transid = log_root_tree->log_transid;
 
+	mutex_lock(&fs_info->tree_log_mutex);
+	if (!log_root_tree->node) {
+		ret = btrfs_alloc_log_tree_node(trans, log_root_tree);
+		if (ret) {
+			mutex_unlock(&fs_info->tree_log_mutex);
+			goto out;
+		}
+	}
+	mutex_unlock(&fs_info->tree_log_mutex);
+
 	/*
 	 * Now we are safe to update the log_root_tree because we're under the
 	 * log_mutex, and we're a current writer so we're holding the commit
@@ -3289,12 +3299,14 @@ static void free_log_tree(struct btrfs_trans_handle *trans,
 		.process_func = process_one_buffer
 	};
 
-	ret = walk_log_tree(trans, log, &wc);
-	if (ret) {
-		if (trans)
-			btrfs_abort_transaction(trans, ret);
-		else
-			btrfs_handle_fs_error(log->fs_info, ret, NULL);
+	if (log->node) {
+		ret = walk_log_tree(trans, log, &wc);
+		if (ret) {
+			if (trans)
+				btrfs_abort_transaction(trans, ret);
+			else
+				btrfs_handle_fs_error(log->fs_info, ret, NULL);
+		}
 	}
 
 	clear_extent_bits(&log->dirty_log_pages, 0, (u64)-1,
Signature · 1 line
2.27.0
Reviewed-by on v9 40/41
#113 lore raw

Re: [PATCH v9 40/41] btrfs: reorder log node allocation

Quoted 6 lines · Naohiro Aota

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

The allocation order of nodes of "fs_info->log_root_tree" and nodes of "root->log_root" is not the same as the writing order of them. So, the writing causes unaligned write errors.

Quoted 6 lines

This patch reorders the allocation of them by delaying allocation of the root node of "fs_info->log_root_tree," so that the node buffers can go out sequentially to devices.

If you keep the tree log stuff, you can add

Thanks,

Josef

#114 lore raw

PATCH v9 41/41 btrfs: enable to mount ZONED incompat flag

In reply to Naohiro Aota

This final patch adds the ZONED incompat flag to BTRFS_FEATURE_INCOMPAT_SUPP and enables btrfs to mount ZONED flagged file system.

---
 fs/btrfs/ctree.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/btrfs/ctree.h +2 -1
index 026d34c8f2ee..c15f73c21f69 100644
@@ -302,7 +302,8 @@ struct btrfs_super_block {
 	 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA |	\
 	 BTRFS_FEATURE_INCOMPAT_NO_HOLES	|	\
 	 BTRFS_FEATURE_INCOMPAT_METADATA_UUID	|	\
-	 BTRFS_FEATURE_INCOMPAT_RAID1C34)
+	 BTRFS_FEATURE_INCOMPAT_RAID1C34	|	\
+	 BTRFS_FEATURE_INCOMPAT_ZONED)
 
 #define BTRFS_FEATURE_INCOMPAT_SAFE_SET			\
 	(BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF)
Signature · 1 line
2.27.0
maintainer
#115 lore raw

Re: [PATCH v9 01/41] block: add bio_add_zone_append_page

In reply to Naohiro Aota
Quoted 5 lines · Naohiro Aota

From: Johannes Thumshirn <[email protected]>

Add bio_add_zone_append_page(), a wrapper around bio_add_hw_page() which is intended to be used by file systems that directly add pages to a bio instead of using bio_iov_iter_get_pages().

Not sure what this is for, since I'm only on one patch in the series...

Quoted 6 lines

+/**
+ * bio_add_zone_append_page - attempt to add page to zone-append bio
+ * @bio: destination bio
+ * @page: page to add
+ * @len: vec entry length
+ * @offset: vec entry offset

Quoted 8 lines

+ *
+ * Attempt to add a page to the bio_vec maplist of a bio that will be submitted
+ * for a zone-append request. This can fail for a number of reasons, such as the
+ * bio being full or the target block device is not a zoned block device or
+ * other limitations of the target block device. The target block device must
+ * allow bio's up to PAGE_SIZE, so it is always possible to add a single page
+ * to an empty bio.
+ */

This should include a

Return value:

section, explaining how it returns number of bytes added (and why 0 is thus a failure case).

block/bio.c int bio_add_pc_page(struct request_queue *q, struct bio *bio, · line 871

Quoted 2 lines
block/bio.c +2 -0
+int bio_add_zone_append_page(struct bio *bio, struct page *page,
+			     unsigned int len, unsigned int offset)

Should this return unsigned int? If not, how would it work if someone asked for INT_MAX + 4k.

Signature · 1 line
Jens Axboe
#116 lore raw

Re: [PATCH v9 01/41] block: add bio_add_zone_append_page

Quoted 8 lines · Jens Axboe
Quoted 5 lines · Naohiro Aota

From: Johannes Thumshirn <[email protected]>

Add bio_add_zone_append_page(), a wrapper around bio_add_hw_page() which is intended to be used by file systems that directly add pages to a bio instead of using bio_iov_iter_get_pages().

Not sure what this is for, since I'm only on one patch in the series...

I'm sorry for the missing context. This patch is for this series.

https://lore.kernel.org/linux-btrfs/[email protected]/T/

This patch uses bio_add_zone_append_page in place of bio_add_page for zoned device.

https://lore.kernel.org/linux-btrfs/[email protected]/T/#m88184d5dd11ac30c0878582898cd9d6f7cbc21fc

Quoted 32 lines
Quoted 15 lines

+/**
+ * bio_add_zone_append_page - attempt to add page to zone-append bio
+ * @bio: destination bio
+ * @page: page to add
+ * @len: vec entry length
+ * @offset: vec entry offset
+ *
+ * Attempt to add a page to the bio_vec maplist of a bio that will be submitted
+ * for a zone-append request. This can fail for a number of reasons, such as the
+ * bio being full or the target block device is not a zoned block device or
+ * other limitations of the target block device. The target block device must
+ * allow bio's up to PAGE_SIZE, so it is always possible to add a single page
+ * to an empty bio.
+ */

This should include a

Return value:

section, explaining how it returns number of bytes added (and why 0 is thus a failure case).

block/bio.c int bio_add_pc_page(struct request_queue *q, struct bio *bio, · line 871

Quoted 2 lines
block/bio.c +2 -0
+int bio_add_zone_append_page(struct bio *bio, struct page *page,
+			     unsigned int len, unsigned int offset)

Should this return unsigned int? If not, how would it work if someone asked for INT_MAX + 4k.

Signature · 1 line
Jens Axboe
#117 lore raw

Re: [PATCH v9 01/41] block: add bio_add_zone_append_page

In reply to Jens Axboe
Quoted 8 lines · Jens Axboe
Quoted 5 lines · Naohiro Aota

From: Johannes Thumshirn <[email protected]>

Add bio_add_zone_append_page(), a wrapper around bio_add_hw_page() which is intended to be used by file systems that directly add pages to a bio instead of using bio_iov_iter_get_pages().

Not sure what this is for, since I'm only on one patch in the series...

Sorry, we'll Cc you on the whole series.

Quoted 16 lines
Quoted 15 lines

+/**
+ * bio_add_zone_append_page - attempt to add page to zone-append bio
+ * @bio: destination bio
+ * @page: page to add
+ * @len: vec entry length
+ * @offset: vec entry offset
+ *
+ * Attempt to add a page to the bio_vec maplist of a bio that will be submitted
+ * for a zone-append request. This can fail for a number of reasons, such as the
+ * bio being full or the target block device is not a zoned block device or
+ * other limitations of the target block device. The target block device must
+ * allow bio's up to PAGE_SIZE, so it is always possible to add a single page
+ * to an empty bio.
+ */

Quoted 6 lines

This should include a

Return value:

section, explaining how it returns number of bytes added (and why 0 is thus a failure case).

I'll update the comment to include the return value. It was just a copy and paste of bio_add_page() and it didn't have the return value documented, so this is why I missed it here.

I'll probably should document the existing functions as well.

Quoted 6 lines

block/bio.c int bio_add_pc_page(struct request_queue *q, struct bio *bio, · line 871

Quoted 2 lines
block/bio.c +2 -0
+int bio_add_zone_append_page(struct bio *bio, struct page *page,
+			     unsigned int len, unsigned int offset)

Should this return unsigned int? If not, how would it work if someone asked for INT_MAX + 4k.

I don't think this is needed as we can't go over 2G anyways, can't we? bio_add_page() also returns an int and I wanted to have a consistent interface here.

#118 lore raw
In reply to Naohiro Aota
Quoted 15 lines · Naohiro Aota

This series adds zoned block device support to btrfs.

Changes from v8:
- Direct-IO path now follow several hardware restrictions (other than max_zone_append_size) by using ZONE_APPEND support of iomap
- introduces union of fs_info->zone_size and fs_info->zoned [Johannes]
- and use btrfs_is_zoned(fs_info) in place of btrfs_fs_incompat(fs_info, ZONED)
- print if zoned is enabled or not when printing module info [Johannes]
- drop patch of disabling inode_cache on ZONED
- moved for_teelog flag to a proper location [Johannes]
- Code style fixes [Johannes]
- Add comment about adding physical layer things to ordered extent structure
- Pass file_offset explicitly to extract_ordered_extent() instead of

Quoted 8 lines

determining it from bio
- Bug fixes
- write out fsync region so that the logical address of ordered extents and checksums are properly finalized
- free zone_info at umount time
- fix superblock log handling when entering zones[1] in the first time
- fixes double free of log-tree roots [Johannes]
- Drop erroneous ASSERT in do_allocation_zoned()

As discussed, patches 1-10 are preparatory and can be added to misc-next but there are still issues regarding coding style and messages. I'll reply to individual patches as well, list below. They're not the most useful comments regarding functionality but we won't have much chance to fix them later and we want to focus on the hard parts.

* all comments start with uppercase, unless it's an identifier
* error messages
* don't start with uppercase (because there's a prefix already)
* wording and style needs to be unified
* new locks that are nested in existing locks need to documented
* somewhere (treelog_bg_lock)
* missing { } in if/else if/else blocks
* newlines between function definitions