Skip to contentSkip to conversation

btrfs: enhance function extent_range_clear_dirty_for_io()

PATCH Superseded Qu Wenruo 1 participant 5 messages May 22, 2024 – May 23, 2024 Btrfs file system

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

v2 cover btrfs: enhance function extent_range_clear_dirty_for_io()

[Changelog]
v2:
- Split the original patch into 3

- Return the error from filemap_get_folio() to be future-proof

- Enhance the comments for the new ASSERT() on extent_range_clear_dirty_for_io() error
In fact, even if some pages are missing, we do not need to handle the error at compress_file_range(), as btrfs_compress_folios() and each compression routine would handle the missing folio correctly.

Thus the new ASSERT() is only an early warning for developers.

This is a preparation for the (near) future support of sector perfect subpage compression support. (the current one requires full page alignment).

The function extent_range_clear_dirty_for_io() is just a simple start.

Qu Wenruo (3):
btrfs: move extent_range_clear_dirty_for_io() into inode.c
btrfs: make extent_range_clear_dirty_for_io() subpage compatible
btrfs: remove the BUG_ON() inside extent_range_clear_dirty_for_io()

 fs/btrfs/extent_io.c | 15 ---------------
 fs/btrfs/extent_io.h |  1 -
 fs/btrfs/inode.c     | 36 +++++++++++++++++++++++++++++++++++-
 3 files changed, 35 insertions(+), 17 deletions(-)
Signature · 1 line
2.45.1
author
#1 lore raw

PATCH v2 1/3 btrfs: move extent_range_clear_dirty_for_io() into inode.c

The function is only utilized inside inode.c by compress_file_range(), so move it to inode.c and unexport it.

---
 fs/btrfs/extent_io.c | 15 ---------------
 fs/btrfs/extent_io.h |  1 -
 fs/btrfs/inode.c     | 15 +++++++++++++++
 3 files changed, 15 insertions(+), 16 deletions(-)
fs/btrfs/extent_io.c +0 -15
index 7275bd919a3e..4af16c09dd88 100644
@@ -164,21 +164,6 @@ void __cold extent_buffer_free_cachep(void)
 	kmem_cache_destroy(extent_buffer_cache);
 }
 
-void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
-{
-	unsigned long index = start >> PAGE_SHIFT;
-	unsigned long end_index = end >> PAGE_SHIFT;
-	struct page *page;
-
-	while (index <= end_index) {
-		page = find_get_page(inode->i_mapping, index);
-		BUG_ON(!page); /* Pages should be in the extent_io_tree */
-		clear_page_dirty_for_io(page);
-		put_page(page);
-		index++;
-	}
-}
-
 static void process_one_page(struct btrfs_fs_info *fs_info,
 			     struct page *page, struct page *locked_page,
 			     unsigned long page_ops, u64 start, u64 end)
fs/btrfs/extent_io.h +0 -1
index dca6b12769ec..7c2f1bbc6b67 100644
@@ -350,7 +350,6 @@ void extent_buffer_bitmap_clear(const struct extent_buffer *eb,
 void set_extent_buffer_dirty(struct extent_buffer *eb);
 void set_extent_buffer_uptodate(struct extent_buffer *eb);
 void clear_extent_buffer_uptodate(struct extent_buffer *eb);
-void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end);
 void extent_clear_unlock_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
 				  struct page *locked_page,
 				  struct extent_state **cached,
fs/btrfs/inode.c +15 -0
index 000809e16aba..99be256f4f0e 100644
@@ -890,6 +890,21 @@ static inline void inode_should_defrag(struct btrfs_inode *inode,
 		btrfs_add_inode_defrag(NULL, inode, small_write);
 }
 
+static void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
+{
+	unsigned long index = start >> PAGE_SHIFT;
+	unsigned long end_index = end >> PAGE_SHIFT;
+	struct page *page;
+
+	while (index <= end_index) {
+		page = find_get_page(inode->i_mapping, index);
+		BUG_ON(!page); /* Pages should be in the extent_io_tree */
+		clear_page_dirty_for_io(page);
+		put_page(page);
+		index++;
+	}
+}
+
 /*
  * Work queue call back to started compression on a file and pages.
  *
Signature · 1 line
2.45.1
author
#2 lore raw

PATCH v2 2/3 btrfs: make extent_range_clear_dirty_for_io() subpage compatible

In reply to Qu Wenruo

Although the function is never called for subpage ranges, there is no harm to make it subpage compatible for the future sector perfect subpage compression support.

And since we're here, also change it to use folio APIs as the subpage helper is already folio based.

---
 fs/btrfs/inode.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
fs/btrfs/inode.c +10 -5
index 99be256f4f0e..dda47a273813 100644
@@ -892,15 +892,20 @@ static inline void inode_should_defrag(struct btrfs_inode *inode,
 
 static void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
 {
+	struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
+	const u64 len = end + 1 - start;
 	unsigned long index = start >> PAGE_SHIFT;
 	unsigned long end_index = end >> PAGE_SHIFT;
-	struct page *page;
 
+	/* We should not have such large range. */
+	ASSERT(len < U32_MAX);
 	while (index <= end_index) {
-		page = find_get_page(inode->i_mapping, index);
-		BUG_ON(!page); /* Pages should be in the extent_io_tree */
-		clear_page_dirty_for_io(page);
-		put_page(page);
+		struct folio *folio;
+
+		folio = filemap_get_folio(inode->i_mapping, index);
+		BUG_ON(IS_ERR(folio)); /* Pages should have been locked. */
+		btrfs_folio_clamp_clear_dirty(fs_info, folio, start, len);
+		folio_put(folio);
 		index++;
 	}
 }
Signature · 1 line
2.45.1
author
#4 lore raw

PATCH v2 3/3 btrfs: remove the BUG_ON() inside extent_range_clear_dirty_for_io()

In reply to Qu Wenruo

Previously we have BUG_ON() inside extent_range_clear_dirty_for_io(), as we expect all involved folios are still locked, thus no folio should be missing.

However for extent_range_clear_dirty_for_io() itself, we can skip the missing folio and handling the remaining ones, and return an error if there is anything wrong.

So this patch would remove the BUG_ON() and let the caller to handle the error.
In the caller we do not have a quick way to cleanup the error, but all the compression routines would handle the missing folio as an error and properly error out, so we only need to do an ASSERT() for developers, meanwhile for non-debug build the compression routine would handle the error correctly.

---
 fs/btrfs/inode.c | 26 ++++++++++++++++++++------
 1 file changed, 20 insertions(+), 6 deletions(-)
fs/btrfs/inode.c +20 -6
index dda47a273813..18b833e58d19 100644
@@ -890,24 +890,29 @@ static inline void inode_should_defrag(struct btrfs_inode *inode,
 		btrfs_add_inode_defrag(NULL, inode, small_write);
 }
 
-static void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
+static int extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
 {
 	struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
 	const u64 len = end + 1 - start;
-	unsigned long index = start >> PAGE_SHIFT;
 	unsigned long end_index = end >> PAGE_SHIFT;
+	int ret = 0;
 
 	/* We should not have such large range. */
 	ASSERT(len < U32_MAX);
-	while (index <= end_index) {
+	for (unsigned long index = start >> PAGE_SHIFT;
+	     index <= end_index; index++) {
 		struct folio *folio;
 
 		folio = filemap_get_folio(inode->i_mapping, index);
-		BUG_ON(IS_ERR(folio)); /* Pages should have been locked. */
+		if (IS_ERR(folio)) {
+			if (!ret)
+				ret = PTR_ERR(folio);
+			continue;
+		}
 		btrfs_folio_clamp_clear_dirty(fs_info, folio, start, len);
 		folio_put(folio);
-		index++;
 	}
+	return ret;
 }
 
 /*
@@ -951,7 +956,16 @@ static void compress_file_range(struct btrfs_work *work)
 	 * Otherwise applications with the file mmap'd can wander in and change
 	 * the page contents while we are compressing them.
 	 */
-	extent_range_clear_dirty_for_io(&inode->vfs_inode, start, end);
+	ret = extent_range_clear_dirty_for_io(&inode->vfs_inode, start, end);
+
+	/*
+	 * All the folios should have been locked thus no failure.
+	 *
+	 * And even some folios are missing, btrfs_compress_folios()
+	 * would handle them correctly, so here just do an ASSERT() check for
+	 * early logic errors.
+	 */
+	ASSERT(ret == 0);
 
 	/*
 	 * We need to save i_size before now because it could change in between
Signature · 1 line
2.45.1
author
#3 lore raw

Re: [PATCH v2 2/3] btrfs: make extent_range_clear_dirty_for_io() subpage compatible

In reply to Qu Wenruo
Quoted 8 lines · Qu Wenruo

Although the function is never called for subpage ranges, there is no harm to make it subpage compatible for the future sector perfect subpage compression support.

And since we're here, also change it to use folio APIs as the subpage helper is already folio based.

This patch is causing hangs for fstests with compression involved.

Please drop the series for now.

Thanks,
Qu

Quoted 34 lines
---
  fs/btrfs/inode.c | 15 ++++++++++-----
  1 file changed, 10 insertions(+), 5 deletions(-)
fs/btrfs/inode.c +10 -5
index 99be256f4f0e..dda47a273813 100644
@@ -892,15 +892,20 @@ static inline void inode_should_defrag(struct btrfs_inode *inode,
 
  static void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
  {
+	struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
+	const u64 len = end + 1 - start;
  	unsigned long index = start >> PAGE_SHIFT;
  	unsigned long end_index = end >> PAGE_SHIFT;
-	struct page *page;
 
+	/* We should not have such large range. */
+	ASSERT(len < U32_MAX);
  	while (index <= end_index) {
-		page = find_get_page(inode->i_mapping, index);
-		BUG_ON(!page); /* Pages should be in the extent_io_tree */
-		clear_page_dirty_for_io(page);
-		put_page(page);
+		struct folio *folio;
+
+		folio = filemap_get_folio(inode->i_mapping, index);
+		BUG_ON(IS_ERR(folio)); /* Pages should have been locked. */
+		btrfs_folio_clamp_clear_dirty(fs_info, folio, start, len);
+		folio_put(folio);
  		index++;
  	}
  }