Skip to contentSkip to conversation

Some cleanups and refactoring of the inline data code

PATCH Applied Julian Sun 3 participants 8 messages Jan 7, 2025 – Mar 18, 2025 ext4 file system

lore
Share & tools
lore

Julian Sun

author
#0 lore raw

v2 cover Some cleanups and refactoring of the inline data code

Here are some cleanups and refactoring of the inline data code. Please see patches for details.

Julian Sun (5):
ext4: Remove a redundant return statement
ext4: Don't set EXT4_STATE_MAY_INLINE_DATA for ea inodes
ext4: Introduce a new helper function ext4_generic_write_inline_data()
ext4: Replace ext4_da_write_inline_data_begin() with
ext4_generic_write_inline_data().
ext4: Refactor out ext4_try_to_write_inline_data()

 fs/ext4/ext4.h           |  10 +-
 fs/ext4/extents_status.c |   1 -
 fs/ext4/ialloc.c         |   2 +-
 fs/ext4/inline.c         | 199 +++++++++++++--------------------------
 fs/ext4/inode.c          |   4 +-
 5 files changed, 75 insertions(+), 141 deletions(-)
Signature · 1 line
2.39.5
author
#1 lore raw

PATCH v2 1/5 ext4: Remove a redundant return statement

Remove a redundant return statements in the
ext4_es_remove_extent() function.

---
 fs/ext4/extents_status.c | 1 -
 1 file changed, 1 deletion(-)
fs/ext4/extents_status.c +0 -1
index c786691dabd3..c56fb682a27e 100644
@@ -1551,7 +1551,6 @@ void ext4_es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
 
 	ext4_es_print_tree(inode);
 	ext4_da_release_space(inode, reserved);
-	return;
 }
 
 static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan,
Signature · 1 line
2.39.5
author
#2 lore raw

PATCH v2 2/5 ext4: Don't set EXT4_STATE_MAY_INLINE_DATA for ea inodes

In reply to Julian Sun

Setting the EXT4_STATE_MAY_INLINE_DATA flag for ea inodes is meaningless because ea inodes do not use functions like ext4_write_begin().

---
 fs/ext4/ialloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ext4/ialloc.c +1 -1
index 7f1a5f90dbbd..49b112bfbd93 100644
@@ -1297,7 +1297,7 @@ struct inode *__ext4_new_inode(struct mnt_idmap *idmap,
 	ei->i_extra_isize = sbi->s_want_extra_isize;
 	ei->i_inline_off = 0;
 	if (ext4_has_feature_inline_data(sb) &&
-	    (!(ei->i_flags & EXT4_DAX_FL) || S_ISDIR(mode)))
+	    (!(ei->i_flags & (EXT4_DAX_FL|EXT4_EA_INODE_FL)) || S_ISDIR(mode)))
 		ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
 	ret = inode;
 	err = dquot_alloc_inode(inode);
Signature · 1 line
2.39.5
author
#3 lore raw

PATCH v2 3/5 ext4: Introduce a new helper function ext4_generic_write_inline_data()

In reply to Julian Sun

A new function, ext4_generic_write_inline_data(), is introduced to provide a generic implementation of the common logic found in ext4_da_write_inline_data_begin() and ext4_try_to_write_inline_data().

This function will be utilized in the subsequent two patches.

---
 fs/ext4/inline.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 86 insertions(+)
fs/ext4/inline.c +86 -0
index 3536ca7e4fcc..3e103e003afb 100644
@@ -20,6 +20,11 @@
 #define EXT4_INLINE_DOTDOT_OFFSET	2
 #define EXT4_INLINE_DOTDOT_SIZE		4
 
+
+static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping,
+						 struct inode *inode,
+						 void **fsdata);
+
 static int ext4_get_inline_size(struct inode *inode)
 {
 	if (EXT4_I(inode)->i_inline_off)
@@ -652,6 +657,87 @@ static int ext4_convert_inline_data_to_extent(struct address_space *mapping,
 	return ret;
 }
 
+static int ext4_generic_write_inline_data(struct address_space *mapping,
+					  struct inode *inode,
+					  loff_t pos, unsigned len,
+					  struct folio **foliop,
+					  void **fsdata, bool da)
+{
+	int ret;
+	handle_t *handle;
+	struct folio *folio;
+	struct ext4_iloc iloc;
+	int retries = 0;
+
+	ret = ext4_get_inode_loc(inode, &iloc);
+	if (ret)
+		return ret;
+
+retry_journal:
+	handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
+	if (IS_ERR(handle)) {
+		ret = PTR_ERR(handle);
+		goto out_release_bh;
+	}
+
+	ret = ext4_prepare_inline_data(handle, inode, pos + len);
+	if (ret && ret != -ENOSPC)
+		goto out_stop_journal;
+
+	if (ret == -ENOSPC) {
+		ext4_journal_stop(handle);
+		if (!da) {
+			brelse(iloc.bh);
+			/* Retry inside */
+			return ext4_convert_inline_data_to_extent(mapping, inode);
+		}
+
+		ret = ext4_da_convert_inline_data_to_extent(mapping, inode, fsdata);
+		if (ret == -ENOSPC &&
+		    ext4_should_retry_alloc(inode->i_sb, &retries))
+			goto retry_journal;
+		goto out_release_bh;
+	}
+
+	folio = __filemap_get_folio(mapping, 0, FGP_WRITEBEGIN | FGP_NOFS,
+					mapping_gfp_mask(mapping));
+	if (IS_ERR(folio)) {
+		ret = PTR_ERR(folio);
+		goto out_stop_journal;
+	}
+
+	down_read(&EXT4_I(inode)->xattr_sem);
+	/* Someone else had converted it to extent */
+	if (!ext4_has_inline_data(inode)) {
+		ret = 0;
+		goto out_release_folio;
+	}
+
+	if (!folio_test_uptodate(folio)) {
+		ret = ext4_read_inline_folio(inode, folio);
+		if (ret < 0)
+			goto out_release_folio;
+	}
+
+	ret = ext4_journal_get_write_access(handle, inode->i_sb, iloc.bh, EXT4_JTR_NONE);
+	if (ret)
+		goto out_release_folio;
+	*foliop = folio;
+	up_read(&EXT4_I(inode)->xattr_sem);
+	brelse(iloc.bh);
+	return 1;
+
+out_release_folio:
+	up_read(&EXT4_I(inode)->xattr_sem);
+	folio_unlock(folio);
+	folio_put(folio);
+out_stop_journal:
+	ext4_journal_stop(handle);
+out_release_bh:
+	brelse(iloc.bh);
+	return ret;
+}
+
 /*
  * Try to write data in the inode.
  * If the inode has inline data, check whether the new write can be
Signature · 1 line
2.39.5
author
#4 lore raw

PATCH v2 4/5 ext4: Replace ext4_da_write_inline_data_begin() with ext4_generic_write_inline_data().

In reply to Julian Sun

Replace the call to ext4_da_write_inline_data_begin() with ext4_generic_write_inline_data(), and delete the ext4_da_write_inline_data_begin().

---
 fs/ext4/ext4.h   | 10 ++---
 fs/ext4/inline.c | 98 +++++-------------------------------------------
 fs/ext4/inode.c  |  4 +-
 3 files changed, 16 insertions(+), 96 deletions(-)
fs/ext4/ext4.h +5 -5
index 44b0d418143c..78dd3408ff39 100644
@@ -3569,11 +3569,11 @@ extern int ext4_try_to_write_inline_data(struct address_space *mapping,
 					 struct folio **foliop);
 int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len,
 			       unsigned copied, struct folio *folio);
-extern int ext4_da_write_inline_data_begin(struct address_space *mapping,
-					   struct inode *inode,
-					   loff_t pos, unsigned len,
-					   struct folio **foliop,
-					   void **fsdata);
+extern int ext4_generic_write_inline_data(struct address_space *mapping,
+					  struct inode *inode,
+					  loff_t pos, unsigned len,
+					  struct folio **foliop,
+					  void **fsdata, bool da);
 extern int ext4_try_add_inline_entry(handle_t *handle,
 				     struct ext4_filename *fname,
 				     struct inode *dir, struct inode *inode);
fs/ext4/inline.c +9 -89

110 lines not on this page · raw message

fs/ext4/inode.c +2 -2
index 54bdd4884fe6..24a3b0ff4c8a 100644
@@ -2918,8 +2918,8 @@ static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
 	trace_ext4_da_write_begin(inode, pos, len);
 
 	if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
-		ret = ext4_da_write_inline_data_begin(mapping, inode, pos, len,
-						      foliop, fsdata);
+		ret = ext4_generic_write_inline_data(mapping, inode, pos, len,
+						     foliop, fsdata, true);
 		if (ret < 0)
 			return ret;
 		if (ret == 1)
Signature · 1 line
2.39.5
reviewer Reviewed-by on v2 4/5
#5 lore raw

Re: [PATCH v2 4/5] ext4: Replace ext4_da_write_inline_data_begin() with ext4_generic_write_inline_data().

Quoted 5 lines · Julian Sun

Replace the call to ext4_da_write_inline_data_begin() with ext4_generic_write_inline_data(), and delete the ext4_da_write_inline_data_begin().

Looks good. Feel free to add:

Honza

Quoted 161 lines
---
 fs/ext4/ext4.h   | 10 ++---
 fs/ext4/inline.c | 98 +++++-------------------------------------------
 fs/ext4/inode.c  |  4 +-
 3 files changed, 16 insertions(+), 96 deletions(-)
fs/ext4/ext4.h +5 -5
index 44b0d418143c..78dd3408ff39 100644
@@ -3569,11 +3569,11 @@ extern int ext4_try_to_write_inline_data(struct address_space *mapping,
 					 struct folio **foliop);
 int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len,
 			       unsigned copied, struct folio *folio);
-extern int ext4_da_write_inline_data_begin(struct address_space *mapping,
-					   struct inode *inode,
-					   loff_t pos, unsigned len,
-					   struct folio **foliop,
-					   void **fsdata);
+extern int ext4_generic_write_inline_data(struct address_space *mapping,
+					  struct inode *inode,
+					  loff_t pos, unsigned len,
+					  struct folio **foliop,
+					  void **fsdata, bool da);
 extern int ext4_try_add_inline_entry(handle_t *handle,
 				     struct ext4_filename *fname,
 				     struct inode *dir, struct inode *inode);
fs/ext4/inline.c +9 -89
index 3e103e003afb..58d8fcfbecba 100644
@@ -657,7 +657,15 @@ static int ext4_convert_inline_data_to_extent(struct address_space *mapping,
 	return ret;
 }
 
-static int ext4_generic_write_inline_data(struct address_space *mapping,
+/*
+ * Prepare the write for the inline data.
+ * If the data can be written into the inode, we just read
+ * the page and make it uptodate, and start the journal.
+ * Otherwise read the page, makes it dirty so that it can be
+ * handle in writepages(the i_disksize update is left to the
+ * normal ext4_da_write_end).
+ */
+int ext4_generic_write_inline_data(struct address_space *mapping,
 					  struct inode *inode,
 					  loff_t pos, unsigned len,
 					  struct folio **foliop,
@@ -967,94 +975,6 @@ static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping,
 	return ret;
 }
 
-/*
- * Prepare the write for the inline data.
- * If the data can be written into the inode, we just read
- * the page and make it uptodate, and start the journal.
- * Otherwise read the page, makes it dirty so that it can be
- * handle in writepages(the i_disksize update is left to the
- * normal ext4_da_write_end).
- */
-int ext4_da_write_inline_data_begin(struct address_space *mapping,
-				    struct inode *inode,
-				    loff_t pos, unsigned len,
-				    struct folio **foliop,
-				    void **fsdata)
-{
-	int ret;
-	handle_t *handle;
-	struct folio *folio;
-	struct ext4_iloc iloc;
-	int retries = 0;
-
-	ret = ext4_get_inode_loc(inode, &iloc);
-	if (ret)
-		return ret;
-
-retry_journal:
-	handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
-	if (IS_ERR(handle)) {
-		ret = PTR_ERR(handle);
-		goto out;
-	}
-
-	ret = ext4_prepare_inline_data(handle, inode, pos + len);
-	if (ret && ret != -ENOSPC)
-		goto out_journal;
-
-	if (ret == -ENOSPC) {
-		ext4_journal_stop(handle);
-		ret = ext4_da_convert_inline_data_to_extent(mapping,
-							    inode,
-							    fsdata);
-		if (ret == -ENOSPC &&
-		    ext4_should_retry_alloc(inode->i_sb, &retries))
-			goto retry_journal;
-		goto out;
-	}
-
-	/*
-	 * We cannot recurse into the filesystem as the transaction
-	 * is already started.
-	 */
-	folio = __filemap_get_folio(mapping, 0, FGP_WRITEBEGIN | FGP_NOFS,
-					mapping_gfp_mask(mapping));
-	if (IS_ERR(folio)) {
-		ret = PTR_ERR(folio);
-		goto out_journal;
-	}
-
-	down_read(&EXT4_I(inode)->xattr_sem);
-	if (!ext4_has_inline_data(inode)) {
-		ret = 0;
-		goto out_release_page;
-	}
-
-	if (!folio_test_uptodate(folio)) {
-		ret = ext4_read_inline_folio(inode, folio);
-		if (ret < 0)
-			goto out_release_page;
-	}
-	ret = ext4_journal_get_write_access(handle, inode->i_sb, iloc.bh,
-					    EXT4_JTR_NONE);
-	if (ret)
-		goto out_release_page;
-
-	up_read(&EXT4_I(inode)->xattr_sem);
-	*foliop = folio;
-	brelse(iloc.bh);
-	return 1;
-out_release_page:
-	up_read(&EXT4_I(inode)->xattr_sem);
-	folio_unlock(folio);
-	folio_put(folio);
-out_journal:
-	ext4_journal_stop(handle);
-out:
-	brelse(iloc.bh);
-	return ret;
-}
-
 #ifdef INLINE_DIR_DEBUG
 void ext4_show_inline_dir(struct inode *dir, struct buffer_head *bh,
 			  void *inline_start, int inline_size)
fs/ext4/inode.c +2 -2
index 54bdd4884fe6..24a3b0ff4c8a 100644
@@ -2918,8 +2918,8 @@ static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
 	trace_ext4_da_write_begin(inode, pos, len);
 
 	if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
-		ret = ext4_da_write_inline_data_begin(mapping, inode, pos, len,
-						      foliop, fsdata);
+		ret = ext4_generic_write_inline_data(mapping, inode, pos, len,
+						     foliop, fsdata, true);
 		if (ret < 0)
 			return ret;
 		if (ret == 1)
Signature · 1 line
2.39.5
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author
#6 lore raw

PATCH v2 5/5 ext4: Refactor out ext4_try_to_write_inline_data()

In reply to Julian Sun

Refactor ext4_try_to_write_inline_data() to simplify its implementation by directly invoking ext4_generic_write_inline_data().

---
 fs/ext4/inline.c | 77 ++----------------------------------------------
 1 file changed, 3 insertions(+), 74 deletions(-)
fs/ext4/inline.c +3 -74
index 58d8fcfbecba..a651a033e518 100644
@@ -757,81 +757,10 @@ int ext4_try_to_write_inline_data(struct address_space *mapping,
 				  loff_t pos, unsigned len,
 				  struct folio **foliop)
 {
-	int ret;
-	handle_t *handle;
-	struct folio *folio;
-	struct ext4_iloc iloc;
-
 	if (pos + len > ext4_get_max_inline_size(inode))
-		goto convert;
-
-	ret = ext4_get_inode_loc(inode, &iloc);
-	if (ret)
-		return ret;
-
-	/*
-	 * The possible write could happen in the inode,
-	 * so try to reserve the space in inode first.
-	 */
-	handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
-	if (IS_ERR(handle)) {
-		ret = PTR_ERR(handle);
-		handle = NULL;
-		goto out;
-	}
-
-	ret = ext4_prepare_inline_data(handle, inode, pos + len);
-	if (ret && ret != -ENOSPC)
-		goto out;
-
-	/* We don't have space in inline inode, so convert it to extent. */
-	if (ret == -ENOSPC) {
-		ext4_journal_stop(handle);
-		brelse(iloc.bh);
-		goto convert;
-	}
-
-	ret = ext4_journal_get_write_access(handle, inode->i_sb, iloc.bh,
-					    EXT4_JTR_NONE);
-	if (ret)
-		goto out;
-
-	folio = __filemap_get_folio(mapping, 0, FGP_WRITEBEGIN | FGP_NOFS,
-					mapping_gfp_mask(mapping));
-	if (IS_ERR(folio)) {
-		ret = PTR_ERR(folio);
-		goto out;
-	}
-
-	*foliop = folio;
-	down_read(&EXT4_I(inode)->xattr_sem);
-	if (!ext4_has_inline_data(inode)) {
-		ret = 0;
-		folio_unlock(folio);
-		folio_put(folio);
-		goto out_up_read;
-	}
-
-	if (!folio_test_uptodate(folio)) {
-		ret = ext4_read_inline_folio(inode, folio);
-		if (ret < 0) {
-			folio_unlock(folio);
-			folio_put(folio);
-			goto out_up_read;
-		}
-	}
-
-	ret = 1;
-	handle = NULL;
-out_up_read:
-	up_read(&EXT4_I(inode)->xattr_sem);
-out:
-	if (handle && (ret != 1))
-		ext4_journal_stop(handle);
-	brelse(iloc.bh);
-	return ret;
-convert:
-	return ext4_convert_inline_data_to_extent(mapping, inode);
+		return ext4_convert_inline_data_to_extent(mapping, inode);
+	return ext4_generic_write_inline_data(mapping, inode, pos, len,
+					      foliop, NULL, false);
 }
 
 int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len,
Signature · 1 line
2.39.5
maintainer Applied
#7 lore raw
In reply to Julian Sun
Quoted 11 lines · Julian Sun

Here are some cleanups and refactoring of the inline data code. Please see patches for details.

Julian Sun (5):
ext4: Remove a redundant return statement
ext4: Don't set EXT4_STATE_MAY_INLINE_DATA for ea inodes
ext4: Introduce a new helper function ext4_generic_write_inline_data()
ext4: Replace ext4_da_write_inline_data_begin() with
ext4_generic_write_inline_data().
ext4: Refactor out ext4_try_to_write_inline_data()

Quoted 1 line

[...]

Applied, thanks!

[1/5] ext4: Remove a redundant return statement

      commit: f896776a7019ebc6403504262cd4239aaa9b99ba
[2/5] ext4: Don't set EXT4_STATE_MAY_INLINE_DATA for ea inodes
      commit: 90c764b4b7f683ca62dbeeceea0ea3a0c6831200
[3/5] ext4: Introduce a new helper function ext4_generic_write_inline_data()
      commit: 3db572f780e9dd7be33d5ac3fb3f0e70a9d4a82d
[4/5] ext4: Replace ext4_da_write_inline_data_begin() with ext4_generic_write_inline_data().
      commit: f9bdb042dfae981d34911a6184157a720fce5e3d
[5/5] ext4: Refactor out ext4_try_to_write_inline_data()
      commit: 30cbe84d48d7d704fdaaf5179a0f4e0764ca35ab

Best regards,

Signature · 1 line
Theodore Ts'o <[email protected]>