Skip to contentSkip to conversation

btrfs: btrfs_get_extent() cleanup

PATCH In review Qu Wenruo 3 participants 5 messages Sep 15, 2022 Btrfs file system

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

cover btrfs: btrfs_get_extent() cleanup

There are some small but weird behavior in btrfs_get_extent() for inline extent handling

- Resetting em members only for inline extent read

If btrfs_get_extent() is called with @page (aka, for file read path), it will reset @em members even it's already updated by btrfs_extent_item_to_extent_map()

The behavior itself is no longer needed as tree-checker has ensured inline extents are only valid if they have 0 file offset.

Thus this means, in that path, @extent_offset must be 0, and a lot of calculations can be simplified and the em member reset is unnecessary.

- Unnecessarily complex handling for inline extent read

The truth is, since inline extents can only exist at file offset 0, we don't need such complex calculation at all.

- Unnecessary argument for btrfs_extent_item_to_extent_map()

- Unnecessarily complex selftest for btrfs_get_extent()
It has an inline extent at file offset 5, which is no longer valid.

The root cause is, the old code just assumes we can have inline extents at non-zero file offset.

The patchset will replace those complex code, with just ASSERT()s, and use much cleaner code to implement the same behavior, and also to update the selftest to reflect the modern behavior.

Qu Wenruo (2):
btrfs: refactor the inline extent read code inside btrfs_get_extent()
btrfs: selftests: remove impossible inline extent at non-zero file
offset

 fs/btrfs/ctree.h             |  1 -
 fs/btrfs/extent_map.c        |  7 +++
 fs/btrfs/file-item.c         |  6 +--
 fs/btrfs/inode.c             | 93 +++++++++++++++++++++---------------
 fs/btrfs/ioctl.c             |  2 +-
 fs/btrfs/tests/inode-tests.c | 56 +++++++---------------
 6 files changed, 82 insertions(+), 83 deletions(-)
Signature · 1 line
2.37.3
author
#1 lore raw

PATCH 1/2 btrfs: refactor the inline extent read code inside btrfs_get_extent()

[PROBLEM]
In btrfs_get_extent() we can fill the inline extent directly.

But there are something not that straightforward for the functionality:

- It's behind two levels of indent

- It has a weird reset for extent map values
This includes resetting:
* em->start

This makes no sense, as our current tree-checker ensures that inline extent can only at file offset 0.

This means not only the @extent_start (key.offset) is always 0, but also @pg_offset and @extent_offset should also be 0.

* em->len

In btrfs_extent_item_to_extent_map(), we already initialize em->len to "btrfs_file_extent_end() - extent_start".
Since @extent_start can only be 0 for inline extents, and btrfs_file_extent_end() is already doing ALIGN() (which is rounding up).

So em->len is always sector_size for inlined extent now.

* em->orig_block_len/orig_start

They doesn't make much sense for inlined extent anyway.

- Extra complex calculation for inline extent read

This is mostly caused by the fact it's still assuming we can have inline file extents at non-zero file offset.

Such offset calculation is now a dead code which we will never reach, just damaging the readability.

- We have an extra bool for btrfs_extent_item_to_extent_map()

Which is making no difference for now.

[ENHANCEMENT]
This patch will enhance the behavior by:

- Extract the read code into a new helper, read_inline_extent()

- Much simpler calculation for inline extent read

- Don't touch extent map when reading inline extents

- Remove the bool argument from btrfs_extent_item_to_extent_map()

- New ASSERT()s to ensure inline extents only start at file offset 0

---
 fs/btrfs/ctree.h     |  1 -
 fs/btrfs/file-item.c |  6 +--
 fs/btrfs/inode.c     | 93 +++++++++++++++++++++++++-------------------
 fs/btrfs/ioctl.c     |  2 +-
 4 files changed, 57 insertions(+), 45 deletions(-)
fs/btrfs/ctree.h +0 -1
index 05df502c3c9d..e8ce86516ec8 100644
@@ -3356,7 +3356,6 @@ int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
 void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode,
 				     const struct btrfs_path *path,
 				     struct btrfs_file_extent_item *fi,
-				     const bool new_inline,
 				     struct extent_map *em);
 int btrfs_inode_clear_file_extent_range(struct btrfs_inode *inode, u64 start,
 					u64 len);
fs/btrfs/file-item.c +2 -4
index 29999686d234..d9c3b58b63bf 100644
@@ -1196,7 +1196,6 @@ int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
 void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode,
 				     const struct btrfs_path *path,
 				     struct btrfs_file_extent_item *fi,
-				     const bool new_inline,
 				     struct extent_map *em)
 {
 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
@@ -1248,10 +1247,9 @@ void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode,
 		 */
 		em->orig_start = EXTENT_MAP_HOLE;
 		em->block_len = (u64)-1;
-		if (!new_inline && compress_type != BTRFS_COMPRESS_NONE) {
+		em->compress_type = compress_type;
+		if (em->compress_type != BTRFS_COMPRESS_NONE)
 			set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
-			em->compress_type = compress_type;
-		}
 	} else {
 		btrfs_err(fs_info,
 			  "unknown file extent item type %d, inode %llu, offset %llu, "
fs/btrfs/inode.c +54 -39

111 lines not on this page · raw message

fs/btrfs/ioctl.c +1 -1
index fe0cc816b4eb..a623bd37d010 100644
@@ -1159,7 +1159,7 @@ static struct extent_map *defrag_get_extent(struct btrfs_inode *inode,
 			goto next;
 
 		/* Now this extent covers @start, convert it to em */
-		btrfs_extent_item_to_extent_map(inode, &path, fi, false, em);
+		btrfs_extent_item_to_extent_map(inode, &path, fi, em);
 		break;
 next:
 		ret = btrfs_next_item(root, &path);
Signature · 1 line
2.37.3
#2 lore raw

Re: [PATCH 1/2] btrfs: refactor the inline extent read code inside btrfs_get_extent()

Quoted 89 lines · Qu Wenruo

[PROBLEM]
In btrfs_get_extent() we can fill the inline extent directly.

But there are something not that straightforward for the functionality:

- It's behind two levels of indent

- It has a weird reset for extent map values
This includes resetting:
* em->start

This makes no sense, as our current tree-checker ensures that inline extent can only at file offset 0.

This means not only the @extent_start (key.offset) is always 0, but also @pg_offset and @extent_offset should also be 0.

* em->len

In btrfs_extent_item_to_extent_map(), we already initialize em->len to "btrfs_file_extent_end() - extent_start".
Since @extent_start can only be 0 for inline extents, and btrfs_file_extent_end() is already doing ALIGN() (which is rounding up).

So em->len is always sector_size for inlined extent now.

* em->orig_block_len/orig_start

They doesn't make much sense for inlined extent anyway.

- Extra complex calculation for inline extent read

This is mostly caused by the fact it's still assuming we can have inline file extents at non-zero file offset.

Such offset calculation is now a dead code which we will never reach, just damaging the readability.

- We have an extra bool for btrfs_extent_item_to_extent_map()

Which is making no difference for now.

[ENHANCEMENT]
This patch will enhance the behavior by:

- Extract the read code into a new helper, read_inline_extent()

- Much simpler calculation for inline extent read

- Don't touch extent map when reading inline extents

- Remove the bool argument from btrfs_extent_item_to_extent_map()

- New ASSERT()s to ensure inline extents only start at file offset 0

---
 fs/btrfs/ctree.h     |  1 -
 fs/btrfs/file-item.c |  6 +--
 fs/btrfs/inode.c     | 93 +++++++++++++++++++++++++-------------------
 fs/btrfs/ioctl.c     |  2 +-
 4 files changed, 57 insertions(+), 45 deletions(-)
fs/btrfs/ctree.h +0 -1
index 05df502c3c9d..e8ce86516ec8 100644
@@ -3356,7 +3356,6 @@ int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
 void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode,
 				     const struct btrfs_path *path,
 				     struct btrfs_file_extent_item *fi,
-				     const bool new_inline,
 				     struct extent_map *em);
 int btrfs_inode_clear_file_extent_range(struct btrfs_inode *inode, u64 start,
 					u64 len);
fs/btrfs/file-item.c +0 -1
index 29999686d234..d9c3b58b63bf 100644
@@ -1196,7 +1196,6 @@ int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
 void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode,
 				     const struct btrfs_path *path,
 				     struct btrfs_file_extent_item *fi,
-				     const bool new_inline,
 				     struct extent_map *em)
 {
 	struct btrfs_fs_info *fs_info = inode->root->fs_info;

fs/btrfs/file-item.c void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode, · line 1254

Quoted 9 lines
fs/btrfs/file-item.c +2 -3
@@ -1248,10 +1247,9 @@ void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode,
 		 */
 		em->orig_start = EXTENT_MAP_HOLE;
 		em->block_len = (u64)-1;
-		if (!new_inline && compress_type != BTRFS_COMPRESS_NONE) {
+		em->compress_type = compress_type;
+		if (em->compress_type != BTRFS_COMPRESS_NONE)
 			set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
-			em->compress_type = compress_type;
-		}

I spent most of my time reviewing this patch trying to decide if this mattered or not, why it was introduced in the first place, and if it was ok to change. Additionally it's not related to the bulk of the change which is making the btrfs_get_extent thing cleaner.

So break this out into it's own patch, with a detailed explanation of why this particular change is ok. In fact I'd prefer if you made all changes to how we mess with the em their own changes, since bugs here can be super subtle, I'd rather have more discreete areas to bisect to. Thanks,

Josef

#3 lore raw

Re: [PATCH 1/2] btrfs: refactor the inline extent read code inside btrfs_get_extent()

Quoted 105 lines · Josef Bacik
Quoted 89 lines · Qu Wenruo

[PROBLEM]
In btrfs_get_extent() we can fill the inline extent directly.

But there are something not that straightforward for the functionality:

- It's behind two levels of indent

- It has a weird reset for extent map values
This includes resetting:
* em->start

This makes no sense, as our current tree-checker ensures that inline extent can only at file offset 0.

This means not only the @extent_start (key.offset) is always 0, but also @pg_offset and @extent_offset should also be 0.

* em->len

In btrfs_extent_item_to_extent_map(), we already initialize em->len to "btrfs_file_extent_end() - extent_start".
Since @extent_start can only be 0 for inline extents, and btrfs_file_extent_end() is already doing ALIGN() (which is rounding up).

So em->len is always sector_size for inlined extent now.

* em->orig_block_len/orig_start

They doesn't make much sense for inlined extent anyway.

- Extra complex calculation for inline extent read

This is mostly caused by the fact it's still assuming we can have inline file extents at non-zero file offset.

Such offset calculation is now a dead code which we will never reach, just damaging the readability.

- We have an extra bool for btrfs_extent_item_to_extent_map()

Which is making no difference for now.

[ENHANCEMENT]
This patch will enhance the behavior by:

- Extract the read code into a new helper, read_inline_extent()

- Much simpler calculation for inline extent read

- Don't touch extent map when reading inline extents

- Remove the bool argument from btrfs_extent_item_to_extent_map()

- New ASSERT()s to ensure inline extents only start at file offset 0

---
  fs/btrfs/ctree.h     |  1 -
  fs/btrfs/file-item.c |  6 +--
  fs/btrfs/inode.c     | 93 +++++++++++++++++++++++++-------------------
  fs/btrfs/ioctl.c     |  2 +-
  4 files changed, 57 insertions(+), 45 deletions(-)
fs/btrfs/ctree.h +0 -1
index 05df502c3c9d..e8ce86516ec8 100644
@@ -3356,7 +3356,6 @@ int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
  void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode,
  				     const struct btrfs_path *path,
  				     struct btrfs_file_extent_item *fi,
-				     const bool new_inline,
  				     struct extent_map *em);
  int btrfs_inode_clear_file_extent_range(struct btrfs_inode *inode, u64 start,
  					u64 len);
fs/btrfs/file-item.c +0 -1
index 29999686d234..d9c3b58b63bf 100644
@@ -1196,7 +1196,6 @@ int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
  void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode,
  				     const struct btrfs_path *path,
  				     struct btrfs_file_extent_item *fi,
-				     const bool new_inline,
  				     struct extent_map *em)
  {
  	struct btrfs_fs_info *fs_info = inode->root->fs_info;

fs/btrfs/file-item.c void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode, · line 1254

Quoted 9 lines
fs/btrfs/file-item.c +2 -3
@@ -1248,10 +1247,9 @@ void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode,
  		 */
  		em->orig_start = EXTENT_MAP_HOLE;
  		em->block_len = (u64)-1;
-		if (!new_inline && compress_type != BTRFS_COMPRESS_NONE) {
+		em->compress_type = compress_type;
+		if (em->compress_type != BTRFS_COMPRESS_NONE)
  			set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
-			em->compress_type = compress_type;
-		}

I spent most of my time reviewing this patch trying to decide if this mattered or not, why it was introduced in the first place, and if it was ok to change. Additionally it's not related to the bulk of the change which is making the btrfs_get_extent thing cleaner.

Quoted 4 lines

So break this out into it's own patch, with a detailed explanation of why this particular change is ok. In fact I'd prefer if you made all changes to how we mess with the em their own changes, since bugs here can be super subtle, I'd rather have more discreete areas to bisect to. Thanks,

Sure, in fact I'm also a little unsure if I should put all these changes into one patch.

Will split all these changes into separate ones.

Thanks,
Qu

Quoted 2 lines

Josef

author
#4 lore raw

PATCH 2/2 btrfs: selftests: remove impossible inline extent at non-zero file offset

In reply to Qu Wenruo

In our inode-tests.c, we create an inline offset at file offset 5, which is no longer possible since the introduction of tree-checker.

Thus I don't think we should spend time maintaining some corner cases which are already ruled out by tree-checker.

So this patch will:

- Change the inline extent to start at file offset 0

Also change its length to 6 to cover the original length

- Add an extra ASSERT() for btrfs_add_extent_mapping()

This is to make sure tree-checker is working correctly.

- Update the inode selftest

---
 fs/btrfs/extent_map.c        |  7 +++++
 fs/btrfs/tests/inode-tests.c | 56 ++++++++++++------------------------
 2 files changed, 25 insertions(+), 38 deletions(-)
fs/btrfs/extent_map.c +7 -0
index 6fee14ce2e6b..59e5a035359b 100644
@@ -610,6 +610,13 @@ int btrfs_add_extent_mapping(struct btrfs_fs_info *fs_info,
 	int ret;
 	struct extent_map *em = *em_in;
 
+	/*
+	 * Tree-checker should have rejected any inline extent with non-zero
+	 * file offset. Here just do a sanity check.
+	 */
+	if (em->block_start == EXTENT_MAP_INLINE)
+		ASSERT(em->start == 0);
+
 	ret = add_extent_mapping(em_tree, em, 0);
 	/* it is possible that someone inserted the extent into the tree
 	 * while we had the lock dropped.  It is also possible that
fs/btrfs/tests/inode-tests.c +18 -38

82 lines not on this page · raw message

Signature · 1 line
2.37.3