@@ -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
41874192int 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