diff mbox series

[v10,23/41] btrfs: split ordered extent when bio is sent

Message ID 4c6d82729e000c4552fceae4a64b2a869c93eb8c.1605007036.git.naohiro.aota@wdc.com (mailing list archive)
State New, archived
Headers show
Series btrfs: zoned block device support | expand

Commit Message

Naohiro Aota Nov. 10, 2020, 11:26 a.m. UTC
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.

Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
---
 fs/btrfs/inode.c        | 89 +++++++++++++++++++++++++++++++++++++++++
 fs/btrfs/ordered-data.c | 76 +++++++++++++++++++++++++++++++++++
 fs/btrfs/ordered-data.h |  2 +
 3 files changed, 167 insertions(+)

Comments

kernel test robot Nov. 11, 2020, 2:01 a.m. UTC | #1
Hi Naohiro,

I love your patch! Perhaps something to improve:

[auto build test WARNING on xfs-linux/for-next]
[also build test WARNING on v5.10-rc3]
[cannot apply to kdave/for-next block/for-next next-20201110]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Naohiro-Aota/btrfs-zoned-block-device-support/20201110-193227
base:   https://git.kernel.org/pub/scm/fs/xfs/xfs-linux.git for-next
config: arc-allyesconfig (attached as .config)
compiler: arceb-elf-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/0day-ci/linux/commit/c2b1e52b104fa60d0c731cc5016be18e98ec71d2
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Naohiro-Aota/btrfs-zoned-block-device-support/20201110-193227
        git checkout c2b1e52b104fa60d0c731cc5016be18e98ec71d2
        # save the attached .config to linux build tree
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=arc 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

>> fs/btrfs/inode.c:2161:5: warning: no previous prototype for 'extract_ordered_extent' [-Wmissing-prototypes]
    2161 | int extract_ordered_extent(struct inode *inode, struct bio *bio,
         |     ^~~~~~~~~~~~~~~~~~~~~~

vim +/extract_ordered_extent +2161 fs/btrfs/inode.c

  2160	
> 2161	int extract_ordered_extent(struct inode *inode, struct bio *bio,
  2162				   loff_t file_offset)
  2163	{
  2164		struct btrfs_ordered_extent *ordered;
  2165		struct extent_map *em = NULL, *em_new = NULL;
  2166		struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  2167		u64 start = (u64)bio->bi_iter.bi_sector << SECTOR_SHIFT;
  2168		u64 len = bio->bi_iter.bi_size;
  2169		u64 end = start + len;
  2170		u64 ordered_end;
  2171		u64 pre, post;
  2172		int ret = 0;
  2173	
  2174		ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), file_offset);
  2175		if (WARN_ON_ONCE(!ordered))
  2176			return -EIO;
  2177	
  2178		/* No need to split */
  2179		if (ordered->disk_num_bytes == len)
  2180			goto out;
  2181	
  2182		/* We cannot split once end_bio'd ordered extent */
  2183		if (WARN_ON_ONCE(ordered->bytes_left != ordered->disk_num_bytes)) {
  2184			ret = -EINVAL;
  2185			goto out;
  2186		}
  2187	
  2188		/* We cannot split a compressed ordered extent */
  2189		if (WARN_ON_ONCE(ordered->disk_num_bytes != ordered->num_bytes)) {
  2190			ret = -EINVAL;
  2191			goto out;
  2192		}
  2193	
  2194		/* We cannot split a waited ordered extent */
  2195		if (WARN_ON_ONCE(wq_has_sleeper(&ordered->wait))) {
  2196			ret = -EINVAL;
  2197			goto out;
  2198		}
  2199	
  2200		ordered_end = ordered->disk_bytenr + ordered->disk_num_bytes;
  2201		/* bio must be in one ordered extent */
  2202		if (WARN_ON_ONCE(start < ordered->disk_bytenr || end > ordered_end)) {
  2203			ret = -EINVAL;
  2204			goto out;
  2205		}
  2206	
  2207		/* Checksum list should be empty */
  2208		if (WARN_ON_ONCE(!list_empty(&ordered->list))) {
  2209			ret = -EINVAL;
  2210			goto out;
  2211		}
  2212	
  2213		pre = start - ordered->disk_bytenr;
  2214		post = ordered_end - end;
  2215	
  2216		btrfs_split_ordered_extent(ordered, pre, post);
  2217	
  2218		read_lock(&em_tree->lock);
  2219		em = lookup_extent_mapping(em_tree, ordered->file_offset, len);
  2220		if (!em) {
  2221			read_unlock(&em_tree->lock);
  2222			ret = -EIO;
  2223			goto out;
  2224		}
  2225		read_unlock(&em_tree->lock);
  2226	
  2227		ASSERT(!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags));
  2228		em_new = create_io_em(BTRFS_I(inode), em->start + pre, len,
  2229				      em->start + pre, em->block_start + pre, len,
  2230				      len, len, BTRFS_COMPRESS_NONE,
  2231				      BTRFS_ORDERED_REGULAR);
  2232		free_extent_map(em_new);
  2233	
  2234	out:
  2235		free_extent_map(em);
  2236		btrfs_put_ordered_extent(ordered);
  2237	
  2238		return ret;
  2239	}
  2240	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
kernel test robot Nov. 11, 2020, 2:26 a.m. UTC | #2
Hi Naohiro,

I love your patch! Perhaps something to improve:

[auto build test WARNING on xfs-linux/for-next]
[also build test WARNING on v5.10-rc3]
[cannot apply to kdave/for-next block/for-next next-20201110]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Naohiro-Aota/btrfs-zoned-block-device-support/20201110-193227
base:   https://git.kernel.org/pub/scm/fs/xfs/xfs-linux.git for-next
config: powerpc-randconfig-r022-20201110 (attached as .config)
compiler: clang version 12.0.0 (https://github.com/llvm/llvm-project 4d81c8adb6ed9840257f6cb6b93f60856d422a15)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # install powerpc cross compiling tool for clang build
        # apt-get install binutils-powerpc-linux-gnu
        # https://github.com/0day-ci/linux/commit/c2b1e52b104fa60d0c731cc5016be18e98ec71d2
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Naohiro-Aota/btrfs-zoned-block-device-support/20201110-193227
        git checkout c2b1e52b104fa60d0c731cc5016be18e98ec71d2
        # save the attached .config to linux build tree
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=powerpc 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

>> fs/btrfs/inode.c:2161:5: warning: no previous prototype for function 'extract_ordered_extent' [-Wmissing-prototypes]
   int extract_ordered_extent(struct inode *inode, struct bio *bio,
       ^
   fs/btrfs/inode.c:2161:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   int extract_ordered_extent(struct inode *inode, struct bio *bio,
   ^
   static 
   1 warning generated.

vim +/extract_ordered_extent +2161 fs/btrfs/inode.c

  2160	
> 2161	int extract_ordered_extent(struct inode *inode, struct bio *bio,
  2162				   loff_t file_offset)
  2163	{
  2164		struct btrfs_ordered_extent *ordered;
  2165		struct extent_map *em = NULL, *em_new = NULL;
  2166		struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  2167		u64 start = (u64)bio->bi_iter.bi_sector << SECTOR_SHIFT;
  2168		u64 len = bio->bi_iter.bi_size;
  2169		u64 end = start + len;
  2170		u64 ordered_end;
  2171		u64 pre, post;
  2172		int ret = 0;
  2173	
  2174		ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), file_offset);
  2175		if (WARN_ON_ONCE(!ordered))
  2176			return -EIO;
  2177	
  2178		/* No need to split */
  2179		if (ordered->disk_num_bytes == len)
  2180			goto out;
  2181	
  2182		/* We cannot split once end_bio'd ordered extent */
  2183		if (WARN_ON_ONCE(ordered->bytes_left != ordered->disk_num_bytes)) {
  2184			ret = -EINVAL;
  2185			goto out;
  2186		}
  2187	
  2188		/* We cannot split a compressed ordered extent */
  2189		if (WARN_ON_ONCE(ordered->disk_num_bytes != ordered->num_bytes)) {
  2190			ret = -EINVAL;
  2191			goto out;
  2192		}
  2193	
  2194		/* We cannot split a waited ordered extent */
  2195		if (WARN_ON_ONCE(wq_has_sleeper(&ordered->wait))) {
  2196			ret = -EINVAL;
  2197			goto out;
  2198		}
  2199	
  2200		ordered_end = ordered->disk_bytenr + ordered->disk_num_bytes;
  2201		/* bio must be in one ordered extent */
  2202		if (WARN_ON_ONCE(start < ordered->disk_bytenr || end > ordered_end)) {
  2203			ret = -EINVAL;
  2204			goto out;
  2205		}
  2206	
  2207		/* Checksum list should be empty */
  2208		if (WARN_ON_ONCE(!list_empty(&ordered->list))) {
  2209			ret = -EINVAL;
  2210			goto out;
  2211		}
  2212	
  2213		pre = start - ordered->disk_bytenr;
  2214		post = ordered_end - end;
  2215	
  2216		btrfs_split_ordered_extent(ordered, pre, post);
  2217	
  2218		read_lock(&em_tree->lock);
  2219		em = lookup_extent_mapping(em_tree, ordered->file_offset, len);
  2220		if (!em) {
  2221			read_unlock(&em_tree->lock);
  2222			ret = -EIO;
  2223			goto out;
  2224		}
  2225		read_unlock(&em_tree->lock);
  2226	
  2227		ASSERT(!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags));
  2228		em_new = create_io_em(BTRFS_I(inode), em->start + pre, len,
  2229				      em->start + pre, em->block_start + pre, len,
  2230				      len, len, BTRFS_COMPRESS_NONE,
  2231				      BTRFS_ORDERED_REGULAR);
  2232		free_extent_map(em_new);
  2233	
  2234	out:
  2235		free_extent_map(em);
  2236		btrfs_put_ordered_extent(ordered);
  2237	
  2238		return ret;
  2239	}
  2240	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
kernel test robot Nov. 11, 2020, 3:46 a.m. UTC | #3
Hi Naohiro,

I love your patch! Perhaps something to improve:

[auto build test WARNING on xfs-linux/for-next]
[also build test WARNING on v5.10-rc3]
[cannot apply to kdave/for-next block/for-next next-20201110]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Naohiro-Aota/btrfs-zoned-block-device-support/20201110-193227
base:   https://git.kernel.org/pub/scm/fs/xfs/xfs-linux.git for-next
config: x86_64-randconfig-s022-20201110 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-15) 9.3.0
reproduce:
        # apt-get install sparse
        # sparse version: v0.6.3-76-gf680124b-dirty
        # https://github.com/0day-ci/linux/commit/c2b1e52b104fa60d0c731cc5016be18e98ec71d2
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Naohiro-Aota/btrfs-zoned-block-device-support/20201110-193227
        git checkout c2b1e52b104fa60d0c731cc5016be18e98ec71d2
        # save the attached .config to linux build tree
        make W=1 C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' ARCH=x86_64 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>


"sparse warnings: (new ones prefixed by >>)"
>> fs/btrfs/inode.c:2161:5: sparse: sparse: symbol 'extract_ordered_extent' was not declared. Should it be static?
>> fs/btrfs/inode.c:2279:21: sparse: sparse: incorrect type in assignment (different base types) @@     expected restricted blk_status_t [usertype] ret @@     got int @@
>> fs/btrfs/inode.c:2279:21: sparse:     expected restricted blk_status_t [usertype] ret
>> fs/btrfs/inode.c:2279:21: sparse:     got int

Please review and possibly fold the followup patch.

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
diff mbox series

Patch

diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 591ca539e444..df85d8dea37c 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -2158,6 +2158,86 @@  static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
 	return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
 }
 
+int extract_ordered_extent(struct 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 = &BTRFS_I(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(BTRFS_I(inode), file_offset);
+	if (WARN_ON_ONCE(!ordered))
+		return -EIO;
+
+	/* 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;
+	}
+
+	/* We cannot split a waited ordered extent */
+	if (WARN_ON_ONCE(wq_has_sleeper(&ordered->wait))) {
+		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;
+
+	btrfs_split_ordered_extent(ordered, pre, post);
+
+	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));
+	em_new = create_io_em(BTRFS_I(inode), em->start + pre, len,
+			      em->start + pre, em->block_start + pre, len,
+			      len, len, BTRFS_COMPRESS_NONE,
+			      BTRFS_ORDERED_REGULAR);
+	free_extent_map(em_new);
+
+out:
+	free_extent_map(em);
+	btrfs_put_ordered_extent(ordered);
+
+	return 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.
@@ -2192,6 +2272,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(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 87bac9ecdf4c..35ef25e39561 100644
--- a/fs/btrfs/ordered-data.c
+++ b/fs/btrfs/ordered-data.c
@@ -943,6 +943,82 @@  void btrfs_lock_and_flush_ordered_range(struct btrfs_inode *inode, u64 start,
 	}
 }
 
+static void 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;
+
+	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);
+		btrfs_add_ordered_extent_compress(BTRFS_I(inode), file_offset,
+						  disk_bytenr, num_bytes,
+						  disk_num_bytes, type,
+						  compress_type);
+	} else if (test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
+		btrfs_add_ordered_extent_dio(BTRFS_I(inode), file_offset,
+					     disk_bytenr, num_bytes,
+					     disk_num_bytes, type);
+	} else {
+		btrfs_add_ordered_extent(BTRFS_I(inode), file_offset,
+					 disk_bytenr, num_bytes, disk_num_bytes,
+					 type);
+	}
+}
+
+void 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);
+
+	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)
+		clone_ordered_extent(ordered, 0, pre);
+	if (post)
+		clone_ordered_extent(ordered, pre + ordered->disk_num_bytes, post);
+}
+
 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 c3a2325e64a4..e346b03bd66a 100644
--- a/fs/btrfs/ordered-data.h
+++ b/fs/btrfs/ordered-data.h
@@ -193,6 +193,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);
+void 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);