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>,
	kernel test robot <lkp@intel.com>
Subject: [PATCH v14 22/42] btrfs: split ordered extent when bio is sent
Date: Tue, 26 Jan 2021 11:25:00 +0900	[thread overview]
Message-ID: <4293f37cdedd93b58df550eb0cdbea44e05e1280.1611627788.git.naohiro.aota@wdc.com> (raw)
In-Reply-To: <cover.1611627788.git.naohiro.aota@wdc.com>

For a zone append write, the device decides the location the data is
written to. Therefore we cannot ensure that two bios are written
consecutively on the device. In order to ensure that a ordered extent maps
to a contiguous region on disk, we need to maintain a "one bio == one
ordered extent" rule.

This commit implements the splitting of an ordered extent and extent map
on bio submission to adhere to the rule.

[testbot] made extract_ordered_extent static
Reported-by: kernel test robot <lkp@intel.com>
Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
---
 fs/btrfs/inode.c        | 95 +++++++++++++++++++++++++++++++++++++++++
 fs/btrfs/ordered-data.c | 85 ++++++++++++++++++++++++++++++++++++
 fs/btrfs/ordered-data.h |  2 +
 3 files changed, 182 insertions(+)

diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 04b9efe4ca5a..92fae7654a3a 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -2217,6 +2217,92 @@ static blk_status_t btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
 	return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
 }
 
+static blk_status_t extract_ordered_extent(struct btrfs_inode *inode,
+					   struct bio *bio, loff_t file_offset)
+{
+	struct btrfs_ordered_extent *ordered;
+	struct extent_map *em = NULL, *em_new = NULL;
+	struct extent_map_tree *em_tree = &inode->extent_tree;
+	u64 start = (u64)bio->bi_iter.bi_sector << SECTOR_SHIFT;
+	u64 len = bio->bi_iter.bi_size;
+	u64 end = start + len;
+	u64 ordered_end;
+	u64 pre, post;
+	int ret = 0;
+
+	ordered = btrfs_lookup_ordered_extent(inode, file_offset);
+	if (WARN_ON_ONCE(!ordered))
+		return BLK_STS_IOERR;
+
+	/* No need to split */
+	if (ordered->disk_num_bytes == len)
+		goto out;
+
+	/* We cannot split once end_bio'd ordered extent */
+	if (WARN_ON_ONCE(ordered->bytes_left != ordered->disk_num_bytes)) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	/* We cannot split a compressed ordered extent */
+	if (WARN_ON_ONCE(ordered->disk_num_bytes != ordered->num_bytes)) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	ordered_end = ordered->disk_bytenr + ordered->disk_num_bytes;
+	/* bio must be in one ordered extent */
+	if (WARN_ON_ONCE(start < ordered->disk_bytenr || end > ordered_end)) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	/* Checksum list should be empty */
+	if (WARN_ON_ONCE(!list_empty(&ordered->list))) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	pre = start - ordered->disk_bytenr;
+	post = ordered_end - end;
+
+	ret = btrfs_split_ordered_extent(ordered, pre, post);
+	if (ret)
+		goto out;
+
+	read_lock(&em_tree->lock);
+	em = lookup_extent_mapping(em_tree, ordered->file_offset, len);
+	if (!em) {
+		read_unlock(&em_tree->lock);
+		ret = -EIO;
+		goto out;
+	}
+	read_unlock(&em_tree->lock);
+
+	ASSERT(!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags));
+	/*
+	 * We canot re-use em_new here but have to create a new one, as
+	 * unpin_extent_cache() expects the start of the extent map to be the
+	 * logical offset of the file, which does not hold true anymore after
+	 * splitting.
+	 */
+	em_new = create_io_em(inode, em->start + pre, len,
+			      em->start + pre, em->block_start + pre, len,
+			      len, len, BTRFS_COMPRESS_NONE,
+			      BTRFS_ORDERED_REGULAR);
+	if (IS_ERR(em_new)) {
+		ret = PTR_ERR(em_new);
+		goto out;
+	}
+	free_extent_map(em_new);
+
+out:
+	free_extent_map(em);
+	btrfs_put_ordered_extent(ordered);
+
+	return errno_to_blk_status(ret);
+}
+
 /*
  * extent_io.c submission hook. This does the right thing for csum calculation
  * on write, or reading the csums from the tree before a read.
@@ -2252,6 +2338,15 @@ blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio,
 	if (btrfs_is_free_space_inode(BTRFS_I(inode)))
 		metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
 
+	if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
+		struct page *page = bio_first_bvec_all(bio)->bv_page;
+		loff_t file_offset = page_offset(page);
+
+		ret = extract_ordered_extent(BTRFS_I(inode), bio, file_offset);
+		if (ret)
+			goto out;
+	}
+
 	if (btrfs_op(bio) != BTRFS_MAP_WRITE) {
 		ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
 		if (ret)
diff --git a/fs/btrfs/ordered-data.c b/fs/btrfs/ordered-data.c
index b4e6500548a2..23aae67fe9e9 100644
--- a/fs/btrfs/ordered-data.c
+++ b/fs/btrfs/ordered-data.c
@@ -921,6 +921,91 @@ void btrfs_lock_and_flush_ordered_range(struct btrfs_inode *inode, u64 start,
 	}
 }
 
+static int clone_ordered_extent(struct btrfs_ordered_extent *ordered, u64 pos,
+				u64 len)
+{
+	struct inode *inode = ordered->inode;
+	u64 file_offset = ordered->file_offset + pos;
+	u64 disk_bytenr = ordered->disk_bytenr + pos;
+	u64 num_bytes = len;
+	u64 disk_num_bytes = len;
+	int type;
+	unsigned long flags_masked =
+		ordered->flags & ~(1 << BTRFS_ORDERED_DIRECT);
+	int compress_type = ordered->compress_type;
+	unsigned long weight;
+	int ret;
+
+	weight = hweight_long(flags_masked);
+	WARN_ON_ONCE(weight > 1);
+	if (!weight)
+		type = 0;
+	else
+		type = __ffs(flags_masked);
+
+	if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered->flags)) {
+		WARN_ON_ONCE(1);
+		ret = btrfs_add_ordered_extent_compress(BTRFS_I(inode),
+							file_offset,
+							disk_bytenr, num_bytes,
+							disk_num_bytes,
+							compress_type);
+	} else if (test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
+		ret = btrfs_add_ordered_extent_dio(BTRFS_I(inode), file_offset,
+						   disk_bytenr, num_bytes,
+						   disk_num_bytes, type);
+	} else {
+		ret = btrfs_add_ordered_extent(BTRFS_I(inode), file_offset,
+					       disk_bytenr, num_bytes,
+					       disk_num_bytes, type);
+	}
+
+	return ret;
+}
+
+int btrfs_split_ordered_extent(struct btrfs_ordered_extent *ordered, u64 pre,
+				u64 post)
+{
+	struct inode *inode = ordered->inode;
+	struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
+	struct rb_node *node;
+	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
+	int ret = 0;
+
+	spin_lock_irq(&tree->lock);
+	/* Remove from tree once */
+	node = &ordered->rb_node;
+	rb_erase(node, &tree->tree);
+	RB_CLEAR_NODE(node);
+	if (tree->last == node)
+		tree->last = NULL;
+
+	ordered->file_offset += pre;
+	ordered->disk_bytenr += pre;
+	ordered->num_bytes -= (pre + post);
+	ordered->disk_num_bytes -= (pre + post);
+	ordered->bytes_left -= (pre + post);
+
+	/* Re-insert the node */
+	node = tree_insert(&tree->tree, ordered->file_offset,
+			   &ordered->rb_node);
+	if (node)
+		btrfs_panic(fs_info, -EEXIST,
+				"zoned: inconsistency in ordered tree at offset %llu",
+				ordered->file_offset);
+
+	spin_unlock_irq(&tree->lock);
+
+	if (pre)
+		ret = clone_ordered_extent(ordered, 0, pre);
+	if (post)
+		ret = clone_ordered_extent(ordered,
+					   pre + ordered->disk_num_bytes,
+					   post);
+
+	return ret;
+}
+
 int __init ordered_data_init(void)
 {
 	btrfs_ordered_extent_cache = kmem_cache_create("btrfs_ordered_extent",
diff --git a/fs/btrfs/ordered-data.h b/fs/btrfs/ordered-data.h
index cca3307807e8..c400be75a3f1 100644
--- a/fs/btrfs/ordered-data.h
+++ b/fs/btrfs/ordered-data.h
@@ -201,6 +201,8 @@ void btrfs_wait_ordered_roots(struct btrfs_fs_info *fs_info, u64 nr,
 void btrfs_lock_and_flush_ordered_range(struct btrfs_inode *inode, u64 start,
 					u64 end,
 					struct extent_state **cached_state);
+int btrfs_split_ordered_extent(struct btrfs_ordered_extent *ordered, u64 pre,
+			       u64 post);
 int __init ordered_data_init(void);
 void __cold ordered_data_exit(void);
 
-- 
2.27.0


  parent reply	other threads:[~2021-01-26  5:46 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 ` Naohiro Aota [this message]
2021-01-27 19:00   ` [PATCH v14 22/42] btrfs: split ordered extent when bio is sent 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 ` [PATCH v14 40/42] btrfs: extend zoned allocator to use dedicated tree-log block group Naohiro Aota
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=4293f37cdedd93b58df550eb0cdbea44e05e1280.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=linux-btrfs@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=lkp@intel.com \
    /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.