Skip to contentSkip to conversation

nilfs2: simplify nilfs_mdt_writeback()

PATCH Applied Ryusuke Konishi 2 participants 2 messages Oct 4 – Oct 5 NILFS2 filesystem

lore
Share & tools
lore

Ryusuke Konishi

author maintainer
#0 lore raw

PATCH 1/1 nilfs2: simplify nilfs_mdt_writeback()

The writepages() callback for metadata files, nilfs_mdt_writeback(), calls nilfs_mdt_write_folio() for each dirty folio via writeback_iter(), where read-only superblock checks, folio dirty state clearing on error, and triggering of the log writer via nilfs_construct_segment() are performed. This helper function is used nowhere else.

Executing these operations per folio is inefficient:

- Checking if the filesystem has fallen back to read-only mode is repeated redundantly.

- Calling nilfs_construct_segment() inside the per-folio loop causes redundant log writer invocations for a single sync request, where only the first invocation performs actual log writing.

Unfold nilfs_mdt_write_folio() into nilfs_mdt_writeback() and consolidate these operations at the function level. The read-only check is moved to the top of nilfs_mdt_writeback(), replacing per-folio nilfs_clear_folio_dirty() calls with a single nilfs_clear_dirty_pages() call to discard dirty pages across the mapping at once. Additionally, nilfs_construct_segment() is invoked once when 'sync_mode' is WB_SYNC_ALL and the mapping is tagged dirty.

With these operations consolidated outside the loop, the remaining per-folio actions (calling folio_redirty_for_writepage() on already dirty folios and unlocking them) serve no purpose, so the folio iteration itself is removed.

---
Viacheslav,

Please add this to the queue for the next cycle.

This refactors the metadata file writeback callback function, restructuring an inefficient legacy function organization rooted in the era of aops->writepage() to align with aops->writepages().

Thanks,
Ryusuke Konishi

 fs/nilfs2/mdt.c | 40 +++++++++++-----------------------------
 1 file changed, 11 insertions(+), 29 deletions(-)
fs/nilfs2/mdt.c +11 -29
index b50c88b65183..77dc2be2ce65 100644
@@ -391,51 +391,33 @@ int nilfs_mdt_fetch_dirty(struct inode *inode)
 	return test_bit(NILFS_I_DIRTY, &ii->i_state);
 }
 
-static int nilfs_mdt_write_folio(struct folio *folio,
+static int nilfs_mdt_writeback(struct address_space *mapping,
 		struct writeback_control *wbc)
 {
-	struct inode *inode = folio->mapping->host;
-	struct super_block *sb;
+	struct inode *inode = mapping->host;
 	int err = 0;
 
-	if (inode && sb_rdonly(inode->i_sb)) {
+	if (!inode)
+		return 0;
+
+	if (sb_rdonly(inode->i_sb)) {
 		/*
 		 * It means that filesystem was remounted in read-only
 		 * mode because of error or metadata corruption. But we
 		 * have dirty folios that try to be flushed in background.
-		 * So, here we simply discard this dirty folio.
+		 * So, here we simply discard these dirty folios.
 		 */
-		nilfs_clear_folio_dirty(folio, false);
-		folio_unlock(folio);
+		nilfs_clear_dirty_pages(mapping, false);
 		return -EROFS;
 	}
 
-	folio_redirty_for_writepage(wbc, folio);
-	folio_unlock(folio);
-
-	if (!inode)
-		return 0;
-
-	sb = inode->i_sb;
-
-	if (wbc->sync_mode == WB_SYNC_ALL)
-		err = nilfs_construct_segment(sb);
+	if (wbc->sync_mode == WB_SYNC_ALL &&
+		mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
+		err = nilfs_construct_segment(inode->i_sb);
 
 	return err;
 }
 
-static int nilfs_mdt_writeback(struct address_space *mapping,
-		struct writeback_control *wbc)
-{
-	struct folio *folio = NULL;
-	int error;
-
-	while ((folio = writeback_iter(mapping, wbc, folio, &error)))
-		error = nilfs_mdt_write_folio(folio, wbc);
-
-	return error;
-}
-
 static const struct address_space_operations def_mdt_aops = {
 	.dirty_folio		= block_dirty_folio,
 	.invalidate_folio	= block_invalidate_folio,
Signature · 1 line
2.53.0
maintainer Applied
#1 lore raw
Quoted 121 lines · Ryusuke Konishi

The writepages() callback for metadata files, nilfs_mdt_writeback(), calls nilfs_mdt_write_folio() for each dirty folio via writeback_iter(),
where read-only superblock checks, folio dirty state clearing on error,
and triggering of the log writer via nilfs_construct_segment() are performed.  This helper function is used nowhere else.

Executing these operations per folio is inefficient:

- Checking if the filesystem has fallen back to read-only mode is repeated redundantly.

- Calling nilfs_construct_segment() inside the per-folio loop causes redundant log writer invocations for a single sync request, where only
  the first invocation performs actual log writing.

Unfold nilfs_mdt_write_folio() into nilfs_mdt_writeback() and consolidate
these operations at the function level.  The read-only check is moved to
the top of nilfs_mdt_writeback(), replacing per-folio nilfs_clear_folio_dirty() calls with a single nilfs_clear_dirty_pages()
call to discard dirty pages across the mapping at once. Additionally,
nilfs_construct_segment() is invoked once when 'sync_mode' is WB_SYNC_ALL
and the mapping is tagged dirty.

With these operations consolidated outside the loop, the remaining per-folio actions (calling folio_redirty_for_writepage() on already dirty folios and unlocking them) serve no purpose, so the folio iteration
itself is removed.

---
Viacheslav,

Please add this to the queue for the next cycle.

This refactors the metadata file writeback callback function, restructuring an inefficient legacy function organization rooted in the era of aops->writepage() to align with aops->writepages().

Thanks,
Ryusuke Konishi

 fs/nilfs2/mdt.c | 40 +++++++++++-----------------------------

 1 file changed, 11 insertions(+), 29 deletions(-)
fs/nilfs2/mdt.c +0 -0
index b50c88b65183..77dc2be2ce65 100644
@@ -391,51 +391,33 @@ int nilfs_mdt_fetch_dirty(struct inode *inode)
  	return test_bit(NILFS_I_DIRTY, &ii->i_state);
  }

fs/nilfs2/mdt.c +1 -1
-static int nilfs_mdt_write_folio(struct folio *folio,
+static int nilfs_mdt_writeback(struct address_space *mapping,

  struct writeback_control *wbc)
 {

fs/nilfs2/mdt.c +1 -2
-	struct inode *inode = folio->mapping->host;
-	struct super_block *sb;
+	struct inode *inode = mapping->host;

  int err = 0;

fs/nilfs2/mdt.c +4 -1
-	if (inode && sb_rdonly(inode->i_sb)) {
+	if (!inode)
+		return 0;
+
+	if (sb_rdonly(inode->i_sb)) {

  /*
  * It means that filesystem was remounted in read- only
  * mode because of error or metadata corruption. But we
  * have dirty folios that try to be flushed in background.

fs/nilfs2/mdt.c +1 -1
-		 * So, here we simply discard this dirty folio.
+		 * So, here we simply discard these dirty folios.

  */

fs/nilfs2/mdt.c +1 -2
-		nilfs_clear_folio_dirty(folio, false);
-		folio_unlock(folio);
+		nilfs_clear_dirty_pages(mapping, false);

  return -EROFS;
  }

fs/nilfs2/mdt.c +3 -10
-	folio_redirty_for_writepage(wbc, folio);
-	folio_unlock(folio);
-
-	if (!inode)
-		return 0;
-
-	sb = inode->i_sb;
-
-	if (wbc->sync_mode == WB_SYNC_ALL)
-		err = nilfs_construct_segment(sb);
+	if (wbc->sync_mode == WB_SYNC_ALL &&
+		mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
+		err = nilfs_construct_segment(inode->i_sb);


  return err;
 }

fs/nilfs2/mdt.c +0 -7
-static int nilfs_mdt_writeback(struct address_space *mapping,
-		struct writeback_control *wbc)
-{
-	struct folio *folio = NULL;
-	int error;
-
-	while ((folio = writeback_iter(mapping, wbc, folio,

&error)))

fs/nilfs2/mdt.c +0 -5
-		error = nilfs_mdt_write_folio(folio, wbc);
-
-	return error;
-}
-
Quoted 3 lines

 static const struct address_space_operations def_mdt_aops = {
  .dirty_folio = block_dirty_folio,
  .invalidate_folio = block_invalidate_folio,

Applied.

Thanks,
Slava.