Skip to content

Commit bd099a0

Browse files
zhangyi089tytso
authored andcommitted
ext4: rename and extend ext4_block_truncate_page()
Rename ext4_block_truncate_page() to ext4_block_zero_eof() and extend its signature to accept an explicit 'end' offset instead of calculating the block boundary. This helper function now can replace all cases requiring zeroing of the partial EOF block, including the append buffered write paths in ext4_*_write_end(). Signed-off-by: Zhang Yi <yi.zhang@huawei.com> Reviewed-by: Jan Kara <jack@suse.cz> Link: https://patch.msgid.link/20260327102939.1095257-3-yi.zhang@huaweicloud.com Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent 5447c8b commit bd099a0

3 files changed

Lines changed: 28 additions & 21 deletions

File tree

fs/ext4/ext4.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3097,6 +3097,8 @@ extern int ext4_chunk_trans_blocks(struct inode *, int nrblocks);
30973097
extern int ext4_chunk_trans_extent(struct inode *inode, int nrblocks);
30983098
extern int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
30993099
int pextents);
3100+
extern int ext4_block_zero_eof(handle_t *handle, struct inode *inode,
3101+
loff_t from, loff_t end);
31003102
extern int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode,
31013103
loff_t lstart, loff_t lend);
31023104
extern vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf);

fs/ext4/extents.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4654,8 +4654,8 @@ static int ext4_alloc_file_blocks(struct file *file, ext4_lblk_t offset,
46544654
inode_get_ctime(inode));
46554655
if (epos > old_size) {
46564656
pagecache_isize_extended(inode, old_size, epos);
4657-
ext4_zero_partial_blocks(handle, inode,
4658-
old_size, epos - old_size);
4657+
ext4_block_zero_eof(handle, inode, old_size,
4658+
epos);
46594659
}
46604660
}
46614661
ret2 = ext4_mark_inode_dirty(handle, inode);

fs/ext4/inode.c

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,7 +1468,7 @@ static int ext4_write_end(const struct kiocb *iocb,
14681468

14691469
if (old_size < pos && !verity) {
14701470
pagecache_isize_extended(inode, old_size, pos);
1471-
ext4_zero_partial_blocks(handle, inode, old_size, pos - old_size);
1471+
ext4_block_zero_eof(handle, inode, old_size, pos);
14721472
}
14731473
/*
14741474
* Don't mark the inode dirty under folio lock. First, it unnecessarily
@@ -1586,7 +1586,7 @@ static int ext4_journalled_write_end(const struct kiocb *iocb,
15861586

15871587
if (old_size < pos && !verity) {
15881588
pagecache_isize_extended(inode, old_size, pos);
1589-
ext4_zero_partial_blocks(handle, inode, old_size, pos - old_size);
1589+
ext4_block_zero_eof(handle, inode, old_size, pos);
15901590
}
15911591

15921592
if (size_changed) {
@@ -3282,7 +3282,7 @@ static int ext4_da_do_write_end(struct address_space *mapping,
32823282
if (IS_ERR(handle))
32833283
return PTR_ERR(handle);
32843284
if (zero_len)
3285-
ext4_zero_partial_blocks(handle, inode, old_size, zero_len);
3285+
ext4_block_zero_eof(handle, inode, old_size, pos);
32863286
ext4_mark_inode_dirty(handle, inode);
32873287
ext4_journal_stop(handle);
32883288

@@ -4162,26 +4162,31 @@ static int ext4_block_zero_page_range(handle_t *handle,
41624162
}
41634163

41644164
/*
4165-
* ext4_block_truncate_page() zeroes out a mapping from file offset `from'
4166-
* up to the end of the block which corresponds to `from'.
4167-
* This required during truncate. We need to physically zero the tail end
4168-
* of that block so it doesn't yield old data if the file is later grown.
4165+
* Zero out a mapping from file offset 'from' up to the end of the block
4166+
* which corresponds to 'from' or to the given 'end' inside this block.
4167+
* This required during truncate up and performing append writes. We need
4168+
* to physically zero the tail end of that block so it doesn't yield old
4169+
* data if the file is grown.
41694170
*/
4170-
static int ext4_block_truncate_page(handle_t *handle,
4171-
struct address_space *mapping, loff_t from)
4171+
int ext4_block_zero_eof(handle_t *handle, struct inode *inode,
4172+
loff_t from, loff_t end)
41724173
{
4173-
unsigned length;
4174-
unsigned blocksize;
4175-
struct inode *inode = mapping->host;
4174+
unsigned int blocksize = i_blocksize(inode);
4175+
unsigned int offset;
4176+
loff_t length = end - from;
41764177

4178+
offset = from & (blocksize - 1);
4179+
if (!offset || from >= end)
4180+
return 0;
41774181
/* If we are processing an encrypted inode during orphan list handling */
41784182
if (IS_ENCRYPTED(inode) && !fscrypt_has_encryption_key(inode))
41794183
return 0;
41804184

4181-
blocksize = i_blocksize(inode);
4182-
length = blocksize - (from & (blocksize - 1));
4185+
if (length > blocksize - offset)
4186+
length = blocksize - offset;
41834187

4184-
return ext4_block_zero_page_range(handle, mapping, from, length, NULL);
4188+
return ext4_block_zero_page_range(handle, inode->i_mapping, from,
4189+
length, NULL);
41854190
}
41864191

41874192
int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode,
@@ -4535,7 +4540,6 @@ int ext4_truncate(struct inode *inode)
45354540
unsigned int credits;
45364541
int err = 0, err2;
45374542
handle_t *handle;
4538-
struct address_space *mapping = inode->i_mapping;
45394543

45404544
/*
45414545
* There is a possibility that we're either freeing the inode
@@ -4578,8 +4582,9 @@ int ext4_truncate(struct inode *inode)
45784582
goto out_trace;
45794583
}
45804584

4585+
/* Zero to the end of the block containing i_size */
45814586
if (inode->i_size & (inode->i_sb->s_blocksize - 1))
4582-
ext4_block_truncate_page(handle, mapping, inode->i_size);
4587+
ext4_block_zero_eof(handle, inode, inode->i_size, LLONG_MAX);
45834588

45844589
/*
45854590
* We add the inode to the orphan list, so that if this
@@ -5968,8 +5973,8 @@ int ext4_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
59685973
inode_set_mtime_to_ts(inode,
59695974
inode_set_ctime_current(inode));
59705975
if (oldsize & (inode->i_sb->s_blocksize - 1))
5971-
ext4_block_truncate_page(handle,
5972-
inode->i_mapping, oldsize);
5976+
ext4_block_zero_eof(handle, inode,
5977+
oldsize, LLONG_MAX);
59735978
}
59745979

59755980
if (shrink)

0 commit comments

Comments
 (0)