[PATCH 2/2] btrfs: unify data and metadata subpage handling

Qu Wenruo <[email protected]>
Newsgroups org.kernel.vger.linux-btrfs
Message-ID <9ac2d271797c1baada373e4825728ceb08f08fc7.1786095309.git.wqu@suse.com>
Currently we have split data and metadata subpage handling, this is due
to the fact that data and metadata have very different conditions to
determine if a folio needs subpage handling.

But the condition different is not that huge, it's just checking
different values.
For data it's checking if the sectorsize is smaller than folio size, for
metadata it doesn't even need to bother folio size yet, as we have not
yet support large folios for metadata.

The idea is to merge btrfs_meta_is_subpage() into btrfs_is_subpage(),
so that btrfs_is_subpage() is the only entrance to check if a folio
needs subpage handling.

With that said, all the existing btrfs_meta_folio_*() helpers can be
updated to a very simple wrapper to the corresponding btrfs_folio_*()
helper.

Signed-off-by: Qu Wenruo <[email protected]>
---
 fs/btrfs/subpage.c | 23 +----------------------
 fs/btrfs/subpage.h | 45 ++++++++++++++++++++++++++++++++-------------
 2 files changed, 33 insertions(+), 35 deletions(-)

diff --git a/fs/btrfs/subpage.c b/fs/btrfs/subpage.c
index 72402ea156d3..7e760934cab8 100644
--- a/fs/btrfs/subpage.c
+++ b/fs/btrfs/subpage.c
@@ -742,29 +742,8 @@ bool btrfs_folio_clamp_test_##name(const struct btrfs_fs_info *fs_info,	\
 		return folio_test_func(folio);				\
 	btrfs_subpage_clamp_range(folio, &start, &len);			\
 	return btrfs_subpage_test_##name(fs_info, folio, start, len);	\
-}									\
-void btrfs_meta_folio_set_##name(struct folio *folio, const struct extent_buffer *eb) \
-{									\
-	if (!btrfs_meta_is_subpage(eb->fs_info)) {			\
-		folio_set_func(folio);					\
-		return;							\
-	}								\
-	btrfs_subpage_set_##name(eb->fs_info, folio, eb->start, eb->len); \
-}									\
-void btrfs_meta_folio_clear_##name(struct folio *folio, const struct extent_buffer *eb) \
-{									\
-	if (!btrfs_meta_is_subpage(eb->fs_info)) {			\
-		folio_clear_func(folio);				\
-		return;							\
-	}								\
-	btrfs_subpage_clear_##name(eb->fs_info, folio, eb->start, eb->len); \
-}									\
-bool btrfs_meta_folio_test_##name(struct folio *folio, const struct extent_buffer *eb) \
-{									\
-	if (!btrfs_meta_is_subpage(eb->fs_info))			\
-		return folio_test_func(folio);				\
-	return btrfs_subpage_test_##name(eb->fs_info, folio, eb->start, eb->len); \
 }
+
 IMPLEMENT_BTRFS_PAGE_OPS(uptodate, folio_mark_uptodate, folio_clear_uptodate,
 			 folio_test_uptodate);
 IMPLEMENT_BTRFS_PAGE_OPS(dirty, btrfs_folio_mark_dirty_reserved,
diff --git a/fs/btrfs/subpage.h b/fs/btrfs/subpage.h
index 9b106a73d682..7e25d8d1d3bb 100644
--- a/fs/btrfs/subpage.h
+++ b/fs/btrfs/subpage.h
@@ -95,11 +95,23 @@ static inline bool btrfs_meta_is_subpage(const struct btrfs_fs_info *fs_info)
 {
 	return fs_info->nodesize < PAGE_SIZE;
 }
-static inline bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info,
-				    struct folio *folio)
+
+static inline bool btrfs_folio_is_data(const struct folio *folio)
 {
-	if (folio->mapping && folio->mapping->host)
-		ASSERT(is_data_inode(BTRFS_I(folio->mapping->host)));
+	const struct address_space *mapping = folio_mapping(folio);
+
+	/* Only metadata folio can have no mapping for dummy ebs. */
+	if (!mapping || !mapping->host)
+		return false;
+	return is_data_inode(BTRFS_I(mapping->host));
+}
+
+/* This helper can be called on both data and metadata folios. */
+static inline bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info,
+				    const struct folio *folio)
+{
+	if (!btrfs_folio_is_data(folio))
+		return btrfs_meta_is_subpage(fs_info);
 	return fs_info->sectorsize < folio_size(folio);
 }
 
@@ -140,12 +152,7 @@ void btrfs_folio_end_lock_bitmap(const struct btrfs_fs_info *fs_info,
  * need to be inside the page. Those functions will truncate the range
  * automatically.
  *
- * Both btrfs_folio_*() and btrfs_folio_clamp_*() are for data folios.
- *
- * For metadata, one should use btrfs_meta_folio_*() helpers instead, and there
- * is no clamp version for metadata helpers, as we either go subpage
- * (nodesize < PAGE_SIZE) or go regular folio helpers (nodesize >= PAGE_SIZE,
- * and our folio is never larger than nodesize).
+ * All helpers can be called on both data and metadata folios.
  */
 #define DECLARE_BTRFS_SUBPAGE_OPS(name)					\
 void btrfs_subpage_set_##name(const struct btrfs_fs_info *fs_info,	\
@@ -166,9 +173,21 @@ void btrfs_folio_clamp_clear_##name(const struct btrfs_fs_info *fs_info,	\
 		struct folio *folio, u64 start, u32 len);			\
 bool btrfs_folio_clamp_test_##name(const struct btrfs_fs_info *fs_info,	\
 		struct folio *folio, u64 start, u32 len);		\
-void btrfs_meta_folio_set_##name(struct folio *folio, const struct extent_buffer *eb); \
-void btrfs_meta_folio_clear_##name(struct folio *folio, const struct extent_buffer *eb); \
-bool btrfs_meta_folio_test_##name(struct folio *folio, const struct extent_buffer *eb);
+static inline void btrfs_meta_folio_set_##name(struct folio *folio,		\
+					       const struct extent_buffer *eb)	\
+{										\
+	btrfs_folio_set_##name(eb->fs_info, folio, eb->start, eb->len);		\
+}										\
+static inline void btrfs_meta_folio_clear_##name(struct folio *folio,		\
+						 const struct extent_buffer *eb) \
+{										\
+	btrfs_folio_clear_##name(eb->fs_info, folio, eb->start, eb->len);	\
+}										\
+static inline bool btrfs_meta_folio_test_##name(struct folio *folio,		\
+				  const struct extent_buffer *eb)		\
+{										\
+	return btrfs_folio_test_##name(eb->fs_info, folio, eb->start, eb->len);	\
+}
 
 DECLARE_BTRFS_SUBPAGE_OPS(uptodate);
 DECLARE_BTRFS_SUBPAGE_OPS(dirty);
-- 
2.54.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.