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,
	Naohiro Aota <naohiro.aota@wdc.com>,
	Josef Bacik <josef@toxicpanda.com>,
	Anand Jain <anand.jain@oracle.com>
Subject: [PATCH v15 12/42] btrfs: zoned: calculate allocation offset for conventional zones
Date: Thu,  4 Feb 2021 19:21:51 +0900	[thread overview]
Message-ID: <7761072d41cc3be28575721df50647fb265a871f.1612434091.git.naohiro.aota@wdc.com> (raw)
In-Reply-To: <b36444df121d46c6d9638a8ae8eacecaa845fbe4.1612434091.git.naohiro.aota@wdc.com>

Conventional zones do not have a write pointer, so we cannot use it to
determine the allocation offset for sequential allocation if a block
group contains a conventional zone.

But instead, we can consider the end of the highest addressed extent in the
block group for the allocation offset.

For new block group, we cannot calculate the allocation offset by
consulting the extent tree, because it can cause deadlock by taking extent
buffer lock after chunk mutex, which is already taken in
btrfs_make_block_group(). Since it is a new block group anyways, we can
simply set the allocation offset to 0.

Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Reviewed-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
---
 fs/btrfs/block-group.c |  4 +-
 fs/btrfs/zoned.c       | 99 +++++++++++++++++++++++++++++++++++++++---
 fs/btrfs/zoned.h       |  4 +-
 3 files changed, 98 insertions(+), 9 deletions(-)

diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index e6bf728496eb..6d10874189df 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -1856,7 +1856,7 @@ static int read_one_block_group(struct btrfs_fs_info *info,
 			goto error;
 	}
 
-	ret = btrfs_load_block_group_zone_info(cache);
+	ret = btrfs_load_block_group_zone_info(cache, false);
 	if (ret) {
 		btrfs_err(info, "zoned: failed to load zone info of bg %llu",
 			  cache->start);
@@ -2150,7 +2150,7 @@ int btrfs_make_block_group(struct btrfs_trans_handle *trans, u64 bytes_used,
 	if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
 		cache->needs_free_space = 1;
 
-	ret = btrfs_load_block_group_zone_info(cache);
+	ret = btrfs_load_block_group_zone_info(cache, true);
 	if (ret) {
 		btrfs_put_block_group(cache);
 		return ret;
diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index 0a7cd00f405f..b892566a1c93 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -927,7 +927,68 @@ int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size)
 	return 0;
 }
 
-int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache)
+/*
+ * Calculate an allocation pointer from the extent allocation information
+ * for a block group consist of conventional zones. It is pointed to the
+ * end of the highest addressed extent in the block group as an allocation
+ * offset.
+ */
+static int calculate_alloc_pointer(struct btrfs_block_group *cache,
+				   u64 *offset_ret)
+{
+	struct btrfs_fs_info *fs_info = cache->fs_info;
+	struct btrfs_root *root = fs_info->extent_root;
+	struct btrfs_path *path;
+	struct btrfs_key key;
+	struct btrfs_key found_key;
+	int ret;
+	u64 length;
+
+	path = btrfs_alloc_path();
+	if (!path)
+		return -ENOMEM;
+
+	key.objectid = cache->start + cache->length;
+	key.type = 0;
+	key.offset = 0;
+
+	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
+	/* We should not find the exact match */
+	if (!ret)
+		ret = -EUCLEAN;
+	if (ret < 0)
+		goto out;
+
+	ret = btrfs_previous_extent_item(root, path, cache->start);
+	if (ret) {
+		if (ret == 1) {
+			ret = 0;
+			*offset_ret = 0;
+		}
+		goto out;
+	}
+
+	btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
+
+	if (found_key.type == BTRFS_EXTENT_ITEM_KEY)
+		length = found_key.offset;
+	else
+		length = fs_info->nodesize;
+
+	if (!(found_key.objectid >= cache->start &&
+	       found_key.objectid + length <= cache->start + cache->length)) {
+		ret = -EUCLEAN;
+		goto out;
+	}
+	*offset_ret = found_key.objectid + length - cache->start;
+	ret = 0;
+
+out:
+	btrfs_free_path(path);
+	return ret;
+}
+
+int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
 {
 	struct btrfs_fs_info *fs_info = cache->fs_info;
 	struct extent_map_tree *em_tree = &fs_info->mapping_tree;
@@ -941,6 +1002,7 @@ int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache)
 	int i;
 	unsigned int nofs_flag;
 	u64 *alloc_offsets = NULL;
+	u64 last_alloc = 0;
 	u32 num_sequential = 0, num_conventional = 0;
 
 	if (!btrfs_is_zoned(fs_info))
@@ -1040,11 +1102,30 @@ int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache)
 
 	if (num_conventional > 0) {
 		/*
-		 * Since conventional zones do not have a write pointer, we
-		 * cannot determine alloc_offset from the pointer
+		 * Avoid calling calculate_alloc_pointer() for new BG. It
+		 * is no use for new BG. It must be always 0.
+		 *
+		 * Also, we have a lock chain of extent buffer lock ->
+		 * chunk mutex.  For new BG, this function is called from
+		 * btrfs_make_block_group() which is already taking the
+		 * chunk mutex. Thus, we cannot call
+		 * calculate_alloc_pointer() which takes extent buffer
+		 * locks to avoid deadlock.
 		 */
-		ret = -EINVAL;
-		goto out;
+		if (new) {
+			cache->alloc_offset = 0;
+			goto out;
+		}
+		ret = calculate_alloc_pointer(cache, &last_alloc);
+		if (ret || map->num_stripes == num_conventional) {
+			if (!ret)
+				cache->alloc_offset = last_alloc;
+			else
+				btrfs_err(fs_info,
+			"zoned: failed to determine allocation offset of bg %llu",
+					  cache->start);
+			goto out;
+		}
 	}
 
 	switch (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
@@ -1066,6 +1147,14 @@ int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache)
 	}
 
 out:
+	/* An extent is allocated after the write pointer */
+	if (!ret && num_conventional && last_alloc > cache->alloc_offset) {
+		btrfs_err(fs_info,
+			  "zoned: got wrong write pointer in BG %llu: %llu > %llu",
+			  logical, last_alloc, cache->alloc_offset);
+		ret = -EIO;
+	}
+
 	kfree(alloc_offsets);
 	free_extent_map(em);
 
diff --git a/fs/btrfs/zoned.h b/fs/btrfs/zoned.h
index 4f3152d7b98f..d27db3993e51 100644
--- a/fs/btrfs/zoned.h
+++ b/fs/btrfs/zoned.h
@@ -41,7 +41,7 @@ u64 btrfs_find_allocatable_zones(struct btrfs_device *device, u64 hole_start,
 int btrfs_reset_device_zone(struct btrfs_device *device, u64 physical,
 			    u64 length, u64 *bytes);
 int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size);
-int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache);
+int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new);
 #else /* CONFIG_BLK_DEV_ZONED */
 static inline int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos,
 				     struct blk_zone *zone)
@@ -118,7 +118,7 @@ static inline int btrfs_ensure_empty_zones(struct btrfs_device *device,
 }
 
 static inline int btrfs_load_block_group_zone_info(
-		struct btrfs_block_group *cache)
+		struct btrfs_block_group *cache, bool new)
 {
 	return 0;
 }
-- 
2.30.0


  parent reply	other threads:[~2021-02-04 10:25 UTC|newest]

Thread overview: 72+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-04 10:21 [PATCH v15 00/42] btrfs: zoned block device support Naohiro Aota
2021-02-04 10:21 ` [PATCH v15 01/42] block: add bio_add_zone_append_page Naohiro Aota
2021-02-04 10:21   ` [PATCH v15 02/42] iomap: support REQ_OP_ZONE_APPEND Naohiro Aota
2021-02-04 10:21   ` [PATCH v15 03/42] btrfs: zoned: defer loading zone info after opening trees Naohiro Aota
2021-02-04 10:21   ` [PATCH v15 04/42] btrfs: zoned: use regular super block location on zone emulation Naohiro Aota
2021-02-04 10:21   ` [PATCH v15 05/42] btrfs: release path before calling to btrfs_load_block_group_zone_info Naohiro Aota
2021-02-04 10:21   ` [PATCH v15 06/42] btrfs: zoned: do not load fs_info::zoned from incompat flag Naohiro Aota
2021-02-04 10:21   ` [PATCH v15 07/42] btrfs: zoned: disallow fitrim on zoned filesystems Naohiro Aota
2021-02-04 10:21   ` [PATCH v15 08/42] btrfs: zoned: allow zoned filesystems on non-zoned block devices Naohiro Aota
2021-02-04 10:21   ` [PATCH v15 09/42] btrfs: zoned: implement zoned chunk allocator Naohiro Aota
2021-02-04 10:21   ` [PATCH v15 10/42] btrfs: zoned: verify device extent is aligned to zone Naohiro Aota
2021-02-04 10:21   ` [PATCH v15 11/42] btrfs: zoned: load zone's allocation offset Naohiro Aota
2021-02-04 10:21   ` Naohiro Aota [this message]
2021-02-04 10:21   ` [PATCH v15 13/42] btrfs: zoned: track unusable bytes for zones Naohiro Aota
2021-02-04 10:21   ` [PATCH v15 14/42] btrfs: zoned: implement sequential extent allocation Naohiro Aota
2021-02-04 10:21   ` [PATCH v15 15/42] btrfs: zoned: redirty released extent buffers Naohiro Aota
2021-02-04 10:21   ` [PATCH v15 16/42] btrfs: zoned: advance allocation pointer after tree log node Naohiro Aota
2021-02-04 10:21   ` [PATCH v15 17/42] btrfs: zoned: reset zones of unused block groups Naohiro Aota
2021-02-04 10:21   ` [PATCH v15 18/42] btrfs: factor out helper adding a page to bio Naohiro Aota
2021-02-04 10:21   ` [PATCH v15 19/42] btrfs: zoned: use bio_add_zone_append_page Naohiro Aota
2021-02-04 10:21   ` [PATCH v15 20/42] btrfs: zoned: handle REQ_OP_ZONE_APPEND as writing Naohiro Aota
2021-02-04 10:22   ` [PATCH v15 21/42] btrfs: zoned: split ordered extent when bio is sent Naohiro Aota
2021-02-04 10:22   ` [PATCH v15 22/42] btrfs: zoned: check if bio spans across an ordered extent Naohiro Aota
2021-02-04 10:22   ` [PATCH v15 23/42] btrfs: extend btrfs_rmap_block for specifying a device Naohiro Aota
2021-02-04 10:22   ` [PATCH v15 24/42] btrfs: zoned: cache if block-group is on a sequential zone Naohiro Aota
2021-02-04 10:22   ` [PATCH v15 25/42] btrfs: save irq flags when looking up an ordered extent Naohiro Aota
2021-02-04 10:22   ` [PATCH v15 26/42] btrfs: zoned: use ZONE_APPEND write for zoned btrfs Naohiro Aota
2021-02-04 10:22   ` [PATCH v15 27/42] btrfs: zoned: enable zone append writing for direct IO Naohiro Aota
2021-02-04 10:22   ` [PATCH v15 28/42] btrfs: zoned: introduce dedicated data write path for zoned filesystems Naohiro Aota
2021-02-04 10:22   ` [PATCH v15 29/42] btrfs: zoned: serialize metadata IO Naohiro Aota
2021-02-04 10:22   ` [PATCH v15 30/42] btrfs: zoned: wait for existing extents before truncating Naohiro Aota
2021-02-04 10:22   ` [PATCH v15 31/42] btrfs: zoned: do not use async metadata checksum on zoned filesystems Naohiro Aota
2021-02-04 10:22   ` [PATCH v15 32/42] btrfs: zoned: mark block groups to copy for device-replace Naohiro Aota
2021-02-04 10:22   ` [PATCH v15 33/42] btrfs: zoned: implement cloning for zoned device-replace Naohiro Aota
2021-02-04 10:22   ` [PATCH v15 34/42] btrfs: zoned: implement copying " Naohiro Aota
2021-02-04 10:22   ` [PATCH v15 35/42] btrfs: zoned: support dev-replace in zoned filesystems Naohiro Aota
2021-02-04 10:22   ` [PATCH v15 36/42] btrfs: zoned: enable relocation on a zoned filesystem Naohiro Aota
2021-02-04 10:22   ` [PATCH v15 37/42] btrfs: zoned: relocate block group to repair IO failure in zoned filesystems Naohiro Aota
2021-02-04 10:22   ` [PATCH v15 38/42] btrfs: split alloc_log_tree() Naohiro Aota
2021-02-04 10:22   ` [PATCH v15 39/42] btrfs: zoned: extend zoned allocator to use dedicated tree-log block group Naohiro Aota
2021-02-04 10:22   ` [PATCH v15 40/42] btrfs: zoned: serialize log transaction on zoned filesystems Naohiro Aota
2021-02-04 11:50     ` Filipe Manana
2021-02-05  7:21       ` Naohiro Aota
2021-02-05  9:15     ` Naohiro Aota
2021-02-05 11:21       ` Filipe Manana
2021-02-09  1:49       ` David Sterba
2021-02-04 10:22   ` [PATCH v15 41/42] btrfs: zoned: reorder log node allocation on zoned filesystem Naohiro Aota
2021-02-04 11:57     ` Filipe Manana
2021-02-04 14:54       ` Johannes Thumshirn
2021-02-04 15:48         ` David Sterba
2021-02-04 15:51           ` Johannes Thumshirn
2021-02-04 10:22   ` [PATCH v15 42/42] btrfs: zoned: enable to mount ZONED incompat flag Naohiro Aota
2021-02-05  9:26   ` [PATCH v15 43/43] btrfs: zoned: deal with holes writing out tree-log pages Naohiro Aota
2021-02-05 11:49     ` Filipe Manana
2021-02-05 12:55       ` Naohiro Aota
2021-02-05 13:07         ` Filipe Manana
2021-02-05 14:19       ` Filipe Manana
2021-02-05 14:46         ` Naohiro Aota
2021-02-05 14:58     ` [PATCH v15.1 " Naohiro Aota
2021-02-05 16:25       ` Filipe Manana
2021-02-09  1:55       ` David Sterba
2021-02-10 19:58 ` [PATCH v15 00/42] btrfs: zoned block device support David Sterba
2021-02-11  9:58   ` Johannes Thumshirn
2021-02-11 15:19     ` David Sterba
2021-02-11 15:26       ` Johannes Thumshirn
2021-02-11 15:46         ` David Sterba
2021-02-15 16:58           ` Johannes Thumshirn
2021-02-15 17:02             ` David Sterba
2021-02-16  4:33             ` Naohiro Aota
2021-02-16 11:46               ` David Sterba
2021-02-22  7:50                 ` Naohiro Aota
2021-02-22 16:00                   ` David Sterba

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=7761072d41cc3be28575721df50647fb265a871f.1612434091.git.naohiro.aota@wdc.com \
    --to=naohiro.aota@wdc.com \
    --cc=anand.jain@oracle.com \
    --cc=dsterba@suse.com \
    --cc=hare@suse.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.