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 v11 25/40] btrfs: use ZONE_APPEND write for ZONED btrfs
Date: Tue, 22 Dec 2020 12:49:18 +0900	[thread overview]
Message-ID: <7f7891de68acb153cb8b56747e2f38362f5f4d7a.1608608848.git.naohiro.aota@wdc.com> (raw)
In-Reply-To: <06add214bc16ef08214de1594ecdfcc4cdcdbd78.1608608848.git.naohiro.aota@wdc.com>

This commit enables zone append writing for zoned btrfs. When using zone
append, a bio is issued to the start of a target zone and the device
decides to place it inside the zone. Upon completion the device reports
the actual written position back to the host.

Three parts are necessary to enable zone append in btrfs. First, modify
the bio to use REQ_OP_ZONE_APPEND in btrfs_submit_bio_hook() and adjust
the bi_sector to point the beginning of the zone.

Secondly, records the returned physical address (and disk/partno) to the
ordered extent in end_bio_extent_writepage() after the bio has been
completed. We cannot resolve the physical address to the logical address
because we can neither take locks nor allocate a buffer in this end_bio
context. So, we need to record the physical address to resolve it later in
btrfs_finish_ordered_io().

And finally, rewrites the logical addresses of the extent mapping and
checksum data according to the physical address (using __btrfs_rmap_block).
If the returned address matches the originally allocated address, we can
skip this rewriting process.

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/extent_io.c    | 11 ++++++-
 fs/btrfs/file.c         |  2 +-
 fs/btrfs/inode.c        |  4 +++
 fs/btrfs/ordered-data.c |  3 ++
 fs/btrfs/ordered-data.h |  8 +++++
 fs/btrfs/volumes.c      | 15 +++++++++
 fs/btrfs/zoned.c        | 68 +++++++++++++++++++++++++++++++++++++++++
 fs/btrfs/zoned.h        | 12 ++++++++
 8 files changed, 121 insertions(+), 2 deletions(-)

diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index d59b13f7ddcf..0cffb6901e58 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -2735,6 +2735,7 @@ static void end_bio_extent_writepage(struct bio *bio)
 	u64 start;
 	u64 end;
 	struct bvec_iter_all iter_all;
+	bool first_bvec = true;
 
 	ASSERT(!bio_flagged(bio, BIO_CLONED));
 	bio_for_each_segment_all(bvec, bio, iter_all) {
@@ -2761,6 +2762,11 @@ static void end_bio_extent_writepage(struct bio *bio)
 		start = page_offset(page);
 		end = start + bvec->bv_offset + bvec->bv_len - 1;
 
+		if (first_bvec) {
+			btrfs_record_physical_zoned(inode, start, bio);
+			first_bvec = false;
+		}
+
 		end_extent_writepage(page, error, start, end);
 		end_page_writeback(page);
 	}
@@ -3579,6 +3585,7 @@ static noinline_for_stack int __extent_writepage_io(struct btrfs_inode *inode,
 	size_t blocksize;
 	int ret = 0;
 	int nr = 0;
+	int opf = REQ_OP_WRITE;
 	const unsigned int write_flags = wbc_to_write_flags(wbc);
 	bool compressed;
 
@@ -3625,6 +3632,8 @@ static noinline_for_stack int __extent_writepage_io(struct btrfs_inode *inode,
 		offset = em->block_start + extent_offset;
 		block_start = em->block_start;
 		compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
+		if (btrfs_use_zone_append(inode, em))
+			opf = REQ_OP_ZONE_APPEND;
 		free_extent_map(em);
 		em = NULL;
 
@@ -3651,7 +3660,7 @@ static noinline_for_stack int __extent_writepage_io(struct btrfs_inode *inode,
 			       page->index, cur, end);
 		}
 
-		ret = submit_extent_page(REQ_OP_WRITE | write_flags, wbc,
+		ret = submit_extent_page(opf | write_flags, wbc,
 					 page, offset, iosize, pg_offset,
 					 &epd->bio,
 					 end_bio_extent_writepage,
diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
index e65223e3510d..5c120d8d060d 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -2176,7 +2176,7 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
 	 * the current transaction commits before the ordered extents complete
 	 * and a power failure happens right after that.
 	 */
-	if (full_sync) {
+	if (full_sync || btrfs_is_zoned(fs_info)) {
 		ret = btrfs_wait_ordered_range(inode, start, len);
 	} else {
 		/*
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 15e0c7714c7f..0ca5b6c9f0ef 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -50,6 +50,7 @@
 #include "delalloc-space.h"
 #include "block-group.h"
 #include "space-info.h"
+#include "zoned.h"
 
 struct btrfs_iget_args {
 	u64 ino;
@@ -2828,6 +2829,9 @@ static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
 	bool clear_reserved_extent = true;
 	unsigned int clear_bits = EXTENT_DEFRAG;
 
+	if (ordered_extent->disk)
+		btrfs_rewrite_logical_zoned(ordered_extent);
+
 	start = ordered_extent->file_offset;
 	end = start + ordered_extent->num_bytes - 1;
 
diff --git a/fs/btrfs/ordered-data.c b/fs/btrfs/ordered-data.c
index 4f8f48e7a482..db3797bc8bb5 100644
--- a/fs/btrfs/ordered-data.c
+++ b/fs/btrfs/ordered-data.c
@@ -199,6 +199,9 @@ static int __btrfs_add_ordered_extent(struct btrfs_inode *inode, u64 file_offset
 	entry->compress_type = compress_type;
 	entry->truncated_len = (u64)-1;
 	entry->qgroup_rsv = ret;
+	entry->physical = (u64)-1;
+	entry->disk = NULL;
+	entry->partno = (u8)-1;
 	if (type != BTRFS_ORDERED_IO_DONE && type != BTRFS_ORDERED_COMPLETE)
 		set_bit(type, &entry->flags);
 
diff --git a/fs/btrfs/ordered-data.h b/fs/btrfs/ordered-data.h
index f9964276f85f..6fdf44a2dea9 100644
--- a/fs/btrfs/ordered-data.h
+++ b/fs/btrfs/ordered-data.h
@@ -127,6 +127,14 @@ struct btrfs_ordered_extent {
 	struct completion completion;
 	struct btrfs_work flush_work;
 	struct list_head work_list;
+
+	/*
+	 * used to reverse-map physical address returned from ZONE_APPEND
+	 * write command in a workqueue context.
+	 */
+	u64 physical;
+	struct gendisk *disk;
+	u8 partno;
 };
 
 /*
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 322396ac3f8e..d2b86aa1fc72 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -6521,6 +6521,21 @@ static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio,
 	btrfs_io_bio(bio)->device = dev;
 	bio->bi_end_io = btrfs_end_bio;
 	bio->bi_iter.bi_sector = physical >> 9;
+	/*
+	 * For zone append writing, bi_sector must point the beginning of the
+	 * zone
+	 */
+	if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
+		if (btrfs_dev_is_sequential(dev, physical)) {
+			u64 zone_start = round_down(physical,
+						    fs_info->zone_size);
+
+			bio->bi_iter.bi_sector = zone_start >> SECTOR_SHIFT;
+		} else {
+			bio->bi_opf &= ~REQ_OP_ZONE_APPEND;
+			bio->bi_opf |= REQ_OP_WRITE;
+		}
+	}
 	btrfs_debug_in_rcu(fs_info,
 	"btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
 		bio_op(bio), bio->bi_opf, bio->bi_iter.bi_sector,
diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index 72735e948b6e..a4def29e7851 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -1217,3 +1217,71 @@ bool btrfs_use_zone_append(struct btrfs_inode *inode, struct extent_map *em)
 
 	return ret;
 }
+
+void btrfs_record_physical_zoned(struct inode *inode, u64 file_offset,
+				 struct bio *bio)
+{
+	struct btrfs_ordered_extent *ordered;
+	u64 physical = (u64)bio->bi_iter.bi_sector << SECTOR_SHIFT;
+
+	if (bio_op(bio) != REQ_OP_ZONE_APPEND)
+		return;
+
+	ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), file_offset);
+	if (WARN_ON(!ordered))
+		return;
+
+	ordered->physical = physical;
+	ordered->disk = bio->bi_disk;
+	ordered->partno = bio->bi_partno;
+
+	btrfs_put_ordered_extent(ordered);
+}
+
+void btrfs_rewrite_logical_zoned(struct btrfs_ordered_extent *ordered)
+{
+	struct extent_map_tree *em_tree;
+	struct extent_map *em;
+	struct inode *inode = ordered->inode;
+	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
+	struct btrfs_ordered_sum *sum;
+	struct block_device *bdev;
+	u64 orig_logical = ordered->disk_bytenr;
+	u64 *logical = NULL;
+	int nr, stripe_len;
+
+	bdev = bdget_disk(ordered->disk, ordered->partno);
+	if (WARN_ON(!bdev))
+		return;
+
+	if (WARN_ON(btrfs_rmap_block(fs_info, orig_logical, bdev,
+				     ordered->physical, &logical, &nr,
+				     &stripe_len)))
+		goto out;
+
+	WARN_ON(nr != 1);
+
+	if (orig_logical == *logical)
+		goto out;
+
+	ordered->disk_bytenr = *logical;
+
+	em_tree = &BTRFS_I(inode)->extent_tree;
+	write_lock(&em_tree->lock);
+	em = search_extent_mapping(em_tree, ordered->file_offset,
+				   ordered->num_bytes);
+	em->block_start = *logical;
+	free_extent_map(em);
+	write_unlock(&em_tree->lock);
+
+	list_for_each_entry(sum, &ordered->list, list) {
+		if (*logical < orig_logical)
+			sum->bytenr -= orig_logical - *logical;
+		else
+			sum->bytenr += *logical - orig_logical;
+	}
+
+out:
+	kfree(logical);
+	bdput(bdev);
+}
diff --git a/fs/btrfs/zoned.h b/fs/btrfs/zoned.h
index 92888eb86055..cf420964305f 100644
--- a/fs/btrfs/zoned.h
+++ b/fs/btrfs/zoned.h
@@ -47,6 +47,9 @@ void btrfs_redirty_list_add(struct btrfs_transaction *trans,
 			    struct extent_buffer *eb);
 void btrfs_free_redirty_list(struct btrfs_transaction *trans);
 bool btrfs_use_zone_append(struct btrfs_inode *inode, struct extent_map *em);
+void btrfs_record_physical_zoned(struct inode *inode, u64 file_offset,
+				 struct bio *bio);
+void btrfs_rewrite_logical_zoned(struct btrfs_ordered_extent *ordered);
 #else /* CONFIG_BLK_DEV_ZONED */
 static inline int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos,
 				     struct blk_zone *zone)
@@ -139,6 +142,15 @@ bool btrfs_use_zone_append(struct btrfs_inode *inode, struct extent_map *em)
 {
 	return false;
 }
+
+static inline void btrfs_record_physical_zoned(struct inode *inode,
+					       u64 file_offset, struct bio *bio)
+{
+}
+
+static inline void btrfs_rewrite_logical_zoned(
+				struct btrfs_ordered_extent *ordered) { }
+
 #endif
 
 static inline bool btrfs_dev_is_sequential(struct btrfs_device *device, u64 pos)
-- 
2.27.0


  parent reply	other threads:[~2020-12-22  3:54 UTC|newest]

Thread overview: 86+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-22  3:48 [PATCH v11 00/40] btrfs: zoned block device support Naohiro Aota
2020-12-22  3:48 ` [PATCH v11 01/40] block: add bio_add_zone_append_page Naohiro Aota
2020-12-22  3:48   ` [PATCH v11 02/40] iomap: support REQ_OP_ZONE_APPEND Naohiro Aota
2021-01-04 22:30     ` Darrick J. Wong
2021-01-13  9:34       ` Johannes Thumshirn
2020-12-22  3:48   ` [PATCH v11 03/40] btrfs: defer loading zone info after opening trees Naohiro Aota
2021-01-11 19:16     ` Josef Bacik
2020-12-22  3:48   ` [PATCH v11 04/40] btrfs: change superblock location on conventional zone Naohiro Aota
2021-01-11 19:47     ` Josef Bacik
2021-01-14 15:10       ` Naohiro Aota
2020-12-22  3:48   ` [PATCH v11 05/40] btrfs: release path before calling into btrfs_load_block_group_zone_info Naohiro Aota
2021-01-11 20:01     ` Josef Bacik
2021-01-12  8:05       ` Johannes Thumshirn
2020-12-22  3:48   ` [PATCH v11 06/40] btrfs: do not load fs_info->zoned from incompat flag Naohiro Aota
2021-01-11 20:08     ` Josef Bacik
2021-01-12  8:00       ` Johannes Thumshirn
2020-12-22  3:49   ` [PATCH v11 07/40] btrfs: disallow fitrim in ZONED mode Naohiro Aota
2021-01-11 20:12     ` Josef Bacik
2021-01-12 10:19       ` Johannes Thumshirn
2020-12-22  3:49   ` [PATCH v11 08/40] btrfs: emulated zoned mode on non-zoned devices Naohiro Aota
2021-01-11 20:54     ` Josef Bacik
2021-01-13 17:58     ` David Sterba
2021-01-13 18:23       ` Johannes Thumshirn
2020-12-22  3:49   ` [PATCH v11 09/40] btrfs: implement zoned chunk allocator Naohiro Aota
2021-01-11 21:24     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 10/40] btrfs: verify device extent is aligned to zone Naohiro Aota
2020-12-22  3:49   ` [PATCH v11 11/40] btrfs: load zone's allocation offset Naohiro Aota
2020-12-22  3:49   ` [PATCH v11 12/40] btrfs: calculate allocation offset for conventional zones Naohiro Aota
2021-01-12 15:12     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 13/40] btrfs: track unusable bytes for zones Naohiro Aota
2021-01-12 15:45     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 14/40] btrfs: do sequential extent allocation in ZONED mode Naohiro Aota
2020-12-22  3:49   ` [PATCH v11 15/40] btrfs: redirty released extent buffers " Naohiro Aota
2021-01-12 15:51     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 16/40] btrfs: advance allocation pointer after tree log node Naohiro Aota
2021-01-12 15:52     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 17/40] btrfs: enable to mount ZONED incompat flag Naohiro Aota
2020-12-22  3:49   ` [PATCH v11 18/40] btrfs: reset zones of unused block groups Naohiro Aota
2021-01-12 15:54     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 19/40] btrfs: extract page adding function Naohiro Aota
2021-01-11 16:22     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 20/40] btrfs: use bio_add_zone_append_page for zoned btrfs Naohiro Aota
2021-01-12 15:55     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 21/40] btrfs: handle REQ_OP_ZONE_APPEND as writing Naohiro Aota
2020-12-22  3:49   ` [PATCH v11 22/40] btrfs: split ordered extent when bio is sent Naohiro Aota
2021-01-12 15:59     ` Josef Bacik
2021-01-13 10:05       ` Johannes Thumshirn
2021-01-15  7:08     ` Su Yue
2020-12-22  3:49   ` [PATCH v11 23/40] btrfs: extend btrfs_rmap_block for specifying a device Naohiro Aota
2021-01-12 16:00     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 24/40] btrfs: cache if block-group is on a sequential zone Naohiro Aota
2021-01-12 16:01     ` Josef Bacik
2020-12-22  3:49   ` Naohiro Aota [this message]
2020-12-22  3:49   ` [PATCH v11 26/40] btrfs: enable zone append writing for direct IO Naohiro Aota
2020-12-22  3:49   ` [PATCH v11 27/40] btrfs: introduce dedicated data write path for ZONED mode Naohiro Aota
2021-01-12 19:24     ` Josef Bacik
2021-01-13 10:41       ` Naohiro Aota
2021-01-12 19:28     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 28/40] btrfs: serialize meta IOs on " Naohiro Aota
2020-12-22  3:49   ` [PATCH v11 29/40] btrfs: wait existing extents before truncating Naohiro Aota
2020-12-22  3:49   ` [PATCH v11 30/40] btrfs: avoid async metadata checksum on ZONED mode Naohiro Aota
2020-12-22  3:49   ` [PATCH v11 31/40] btrfs: mark block groups to copy for device-replace Naohiro Aota
2021-01-12 19:30     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 32/40] btrfs: implement cloning for ZONED device-replace Naohiro Aota
2021-01-12 19:36     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 33/40] btrfs: implement copying " Naohiro Aota
2021-01-12 19:37     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 34/40] btrfs: support dev-replace in ZONED mode Naohiro Aota
2021-01-12 19:37     ` Josef Bacik
2021-01-12 19:40     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 35/40] btrfs: enable relocation " Naohiro Aota
2021-01-12 19:43     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 36/40] btrfs: relocate block group to repair IO failure in ZONED Naohiro Aota
2020-12-22  3:49   ` [PATCH v11 37/40] btrfs: split alloc_log_tree() Naohiro Aota
2021-01-12 19:44     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 38/40] btrfs: extend zoned allocator to use dedicated tree-log block group Naohiro Aota
2021-01-12 19:48     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 39/40] btrfs: serialize log transaction on ZONED mode Naohiro Aota
2021-01-12 19:50     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 40/40] btrfs: reorder log node allocation Naohiro Aota
2020-12-22 13:35   ` [PATCH v11 01/40] block: add bio_add_zone_append_page Christoph Hellwig
2021-01-12 13:48     ` Johannes Thumshirn
2020-12-22 13:38 ` [PATCH v11 00/40] btrfs: zoned block device support Christoph Hellwig
2021-01-11 10:17   ` Johannes Thumshirn
2021-01-12 10:23     ` hch
2021-01-12 10:26       ` 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=7f7891de68acb153cb8b56747e2f38362f5f4d7a.1608608848.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.