Skip to content

Commit 57661f2

Browse files
zhangyi089tytso
authored andcommitted
ext4: replace ext4_writepage_trans_blocks()
After ext4 supports large folios, the semantics of reserving credits in pages is no longer applicable. In most scenarios, reserving credits in extents is sufficient. Therefore, introduce ext4_chunk_trans_extent() to replace ext4_writepage_trans_blocks(). move_extent_per_page() is the only remaining location where we are still processing extents in pages. Suggested-by: Jan Kara <jack@suse.cz> Signed-off-by: Zhang Yi <yi.zhang@huawei.com> Reviewed-by: Jan Kara <jack@suse.cz> Link: https://patch.msgid.link/20250707140814.542883-10-yi.zhang@huaweicloud.com Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent bbbf150 commit 57661f2

6 files changed

Lines changed: 25 additions & 27 deletions

File tree

fs/ext4/ext4.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3064,9 +3064,9 @@ extern int ext4_punch_hole(struct file *file, loff_t offset, loff_t length);
30643064
extern void ext4_set_inode_flags(struct inode *, bool init);
30653065
extern int ext4_alloc_da_blocks(struct inode *inode);
30663066
extern void ext4_set_aops(struct inode *inode);
3067-
extern int ext4_writepage_trans_blocks(struct inode *);
30683067
extern int ext4_normal_submit_inode_data_buffers(struct jbd2_inode *jinode);
30693068
extern int ext4_chunk_trans_blocks(struct inode *, int nrblocks);
3069+
extern int ext4_chunk_trans_extent(struct inode *inode, int nrblocks);
30703070
extern int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
30713071
int pextents);
30723072
extern int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode,

fs/ext4/extents.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5171,7 +5171,7 @@ ext4_ext_shift_path_extents(struct ext4_ext_path *path, ext4_lblk_t shift,
51715171
credits = depth + 2;
51725172
}
51735173

5174-
restart_credits = ext4_writepage_trans_blocks(inode);
5174+
restart_credits = ext4_chunk_trans_extent(inode, 0);
51755175
err = ext4_datasem_ensure_credits(handle, inode, credits,
51765176
restart_credits, 0);
51775177
if (err) {
@@ -5431,7 +5431,7 @@ static int ext4_collapse_range(struct file *file, loff_t offset, loff_t len)
54315431

54325432
truncate_pagecache(inode, start);
54335433

5434-
credits = ext4_writepage_trans_blocks(inode);
5434+
credits = ext4_chunk_trans_extent(inode, 0);
54355435
handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
54365436
if (IS_ERR(handle))
54375437
return PTR_ERR(handle);
@@ -5527,7 +5527,7 @@ static int ext4_insert_range(struct file *file, loff_t offset, loff_t len)
55275527

55285528
truncate_pagecache(inode, start);
55295529

5530-
credits = ext4_writepage_trans_blocks(inode);
5530+
credits = ext4_chunk_trans_extent(inode, 0);
55315531
handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
55325532
if (IS_ERR(handle))
55335533
return PTR_ERR(handle);

fs/ext4/inline.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ static int ext4_convert_inline_data_to_extent(struct address_space *mapping,
562562
return 0;
563563
}
564564

565-
needed_blocks = ext4_writepage_trans_blocks(inode);
565+
needed_blocks = ext4_chunk_trans_extent(inode, 1);
566566

567567
ret = ext4_get_inode_loc(inode, &iloc);
568568
if (ret)
@@ -1864,7 +1864,7 @@ int ext4_inline_data_truncate(struct inode *inode, int *has_inline)
18641864
};
18651865

18661866

1867-
needed_blocks = ext4_writepage_trans_blocks(inode);
1867+
needed_blocks = ext4_chunk_trans_extent(inode, 1);
18681868
handle = ext4_journal_start(inode, EXT4_HT_INODE, needed_blocks);
18691869
if (IS_ERR(handle))
18701870
return PTR_ERR(handle);
@@ -1979,7 +1979,7 @@ int ext4_convert_inline_data(struct inode *inode)
19791979
return 0;
19801980
}
19811981

1982-
needed_blocks = ext4_writepage_trans_blocks(inode);
1982+
needed_blocks = ext4_chunk_trans_extent(inode, 1);
19831983

19841984
iloc.bh = NULL;
19851985
error = ext4_get_inode_loc(inode, &iloc);

fs/ext4/inode.c

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,7 +1294,8 @@ static int ext4_write_begin(struct file *file, struct address_space *mapping,
12941294
* Reserve one block more for addition to orphan list in case
12951295
* we allocate blocks but write fails for some reason
12961296
*/
1297-
needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
1297+
needed_blocks = ext4_chunk_trans_extent(inode,
1298+
ext4_journal_blocks_per_folio(inode)) + 1;
12981299
index = pos >> PAGE_SHIFT;
12991300

13001301
if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
@@ -4461,7 +4462,7 @@ int ext4_punch_hole(struct file *file, loff_t offset, loff_t length)
44614462
return ret;
44624463

44634464
if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
4464-
credits = ext4_writepage_trans_blocks(inode);
4465+
credits = ext4_chunk_trans_extent(inode, 2);
44654466
else
44664467
credits = ext4_blocks_for_truncate(inode);
44674468
handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
@@ -4610,7 +4611,7 @@ int ext4_truncate(struct inode *inode)
46104611
}
46114612

46124613
if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
4613-
credits = ext4_writepage_trans_blocks(inode);
4614+
credits = ext4_chunk_trans_extent(inode, 1);
46144615
else
46154616
credits = ext4_blocks_for_truncate(inode);
46164617

@@ -6237,25 +6238,19 @@ int ext4_meta_trans_blocks(struct inode *inode, int lblocks, int pextents)
62376238
}
62386239

62396240
/*
6240-
* Calculate the total number of credits to reserve to fit
6241-
* the modification of a single pages into a single transaction,
6242-
* which may include multiple chunks of block allocations.
6243-
*
6244-
* This could be called via ext4_write_begin()
6245-
*
6246-
* We need to consider the worse case, when
6247-
* one new block per extent.
6241+
* Calculate the journal credits for modifying the number of blocks
6242+
* in a single extent within one transaction. 'nrblocks' is used only
6243+
* for non-extent inodes. For extent type inodes, 'nrblocks' can be
6244+
* zero if the exact number of blocks is unknown.
62486245
*/
6249-
int ext4_writepage_trans_blocks(struct inode *inode)
6246+
int ext4_chunk_trans_extent(struct inode *inode, int nrblocks)
62506247
{
6251-
int bpp = ext4_journal_blocks_per_folio(inode);
62526248
int ret;
62536249

6254-
ret = ext4_meta_trans_blocks(inode, bpp, bpp);
6255-
6250+
ret = ext4_meta_trans_blocks(inode, nrblocks, 1);
62566251
/* Account for data blocks for journalled mode */
62576252
if (ext4_should_journal_data(inode))
6258-
ret += bpp;
6253+
ret += nrblocks;
62596254
return ret;
62606255
}
62616256

@@ -6633,10 +6628,12 @@ static int ext4_block_page_mkwrite(struct inode *inode, struct folio *folio,
66336628
handle_t *handle;
66346629
loff_t size;
66356630
unsigned long len;
6631+
int credits;
66366632
int ret;
66376633

6638-
handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
6639-
ext4_writepage_trans_blocks(inode));
6634+
credits = ext4_chunk_trans_extent(inode,
6635+
ext4_journal_blocks_per_folio(inode));
6636+
handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, credits);
66406637
if (IS_ERR(handle))
66416638
return PTR_ERR(handle);
66426639

fs/ext4/move_extent.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ move_extent_per_page(struct file *o_filp, struct inode *donor_inode,
280280
*/
281281
again:
282282
*err = 0;
283-
jblocks = ext4_writepage_trans_blocks(orig_inode) * 2;
283+
jblocks = ext4_meta_trans_blocks(orig_inode, block_len_in_page,
284+
block_len_in_page) * 2;
284285
handle = ext4_journal_start(orig_inode, EXT4_HT_MOVE_EXTENTS, jblocks);
285286
if (IS_ERR(handle)) {
286287
*err = PTR_ERR(handle);

fs/ext4/xattr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,7 @@ int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
962962
* so we need to reserve credits for this eventuality
963963
*/
964964
if (inode && ext4_has_inline_data(inode))
965-
credits += ext4_writepage_trans_blocks(inode) + 1;
965+
credits += ext4_chunk_trans_extent(inode, 1) + 1;
966966

967967
/* We are done if ea_inode feature is not enabled. */
968968
if (!ext4_has_feature_ea_inode(sb))

0 commit comments

Comments
 (0)