f2fs: compress: fix to update i_compr_blocks correctly
Chao Yu
maintainerPATCH 1/5 f2fs: compress: fix to update i_compr_blocks correctly
Previously, we account reserved blocks and compressed blocks into @compr_blocks, then, f2fs_i_compr_blocks_update(,compr_blocks) will update i_compr_blocks incorrectly, fix it.
Meanwhile, for the case all blocks in cluster were reserved, fix to update dn->ofs_in_node correctly.
--- fs/f2fs/file.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-)
fs/f2fs/file.c +14 -7
@@ -3641,7 +3641,8 @@ static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count,while (count) {int compr_blocks =0 ; -blkcnt_t reserved; +blkcnt_t reserved =0 ; +blkcnt_t to_reserved;int ret;for (i =0 ; i < cluster_size; i++) {
@@ -3661,20 +3662,26 @@ static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count,* fails in release_compress_blocks(), so NEW_ADDR * is a possible case. */ -if (blkaddr ==NEW_ADDR || -__is_valid_data_blkaddr (blkaddr)) { +if (blkaddr ==NEW_ADDR ) { + reserved++; +continue ; + } +if (__is_valid_data_blkaddr (blkaddr)) { compr_blocks++;continue ; } } - reserved = cluster_size - compr_blocks; + to_reserved = cluster_size - compr_blocks - reserved;/* for the case all blocks in cluster were reserved */ -if (reserved ==1 ) +if (to_reserved ==1 ) { + dn->ofs_in_node += cluster_size; goto next; + } - ret =inc_valid_block_count (sbi, dn->inode, &reserved, false); + ret =inc_valid_block_count (sbi, dn->inode, + &to_reserved, false);if (unlikely (ret))return ret;
@@ -3685,7 +3692,7 @@ static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count,f2fs_i_compr_blocks_update (dn->inode, compr_blocks, true); - *reserved_blocks += reserved; + *reserved_blocks += to_reserved; next: count -= cluster_size; }
Signature · 1 line
2.40.1
Chao Yu
maintainerPATCH 2/5 f2fs: compress: fix error path of inc_valid_block_count()
If inc_valid_block_count() can not allocate all requested blocks, it needs to release block count in .total_valid_block_count and resevation blocks in inode.
--- fs/f2fs/f2fs.h | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)
fs/f2fs/f2fs.h +8 -7
@@ -2309,7 +2309,7 @@ static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);static inline int inc_valid_block_count (struct f2fs_sb_info *sbi,struct inode *inode,blkcnt_t *count,bool partial) { -blkcnt_t diff =0 , release =0 ; +long long diff =0 , release =0 ;block_t avail_user_block_count;int ret;
@@ -2329,26 +2329,27 @@ static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,percpu_counter_add (&sbi->alloc_valid_block_count, (*count));spin_lock (&sbi->stat_lock); - sbi->total_valid_block_count += (block_t)(*count); - avail_user_block_count =get_available_block_count (sbi, inode, true); -if (unlikely (sbi->total_valid_block_count > avail_user_block_count)) { + avail_user_block_count =get_available_block_count (sbi, inode, true); + diff = (long long )sbi->total_valid_block_count + *count - + avail_user_block_count; +if (unlikely (diff >0 )) {if (!partial) {spin_unlock (&sbi->stat_lock); + release = *count; goto enospc; } - - diff = sbi->total_valid_block_count - avail_user_block_count;if (diff > *count) diff = *count; *count -= diff; release = diff; - sbi->total_valid_block_count -= diff;if (!*count) {spin_unlock (&sbi->stat_lock); goto enospc; } } + sbi->total_valid_block_count += (block_t)(*count); +spin_unlock (&sbi->stat_lock);if (unlikely (release)) {
Signature · 1 line
2.40.1
Chao Yu
maintainerPATCH 3/5 f2fs: compress: fix typo in f2fs_reserve_compress_blocks()
In reply to Chao Yus/released/reserved.
--- fs/f2fs/file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/f2fs/file.c +1 -1
@@ -3785,7 +3785,7 @@ static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg) }else if (reserved_blocks &&atomic_read (&F2FS_I (inode)->i_compr_blocks)) {set_sbi_flag (sbi,SBI_NEED_FSCK ); - f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx " + f2fs_warn(sbi, "%s: partial blocks were reserved i_ino=%lx " "iblocks=%llu, reserved=%u, compr_blocks=%u, " "run fsck to fix.", __func__, inode->i_ino, inode->i_blocks,
Signature · 1 line
2.40.1
Chao Yu
maintainerPATCH 4/5 f2fs: compress: fix to cover {reserve,release}_compress_blocks() w/ cp_rwsem lock
In reply to Chao YuIt needs to cover {reserve,release}_compress_blocks() w/ cp_rwsem lock to avoid racing with checkpoint, otherwise, filesystem metadata including blkaddr in dnode, inode fields and .total_valid_block_count may be corrupted after SPO case.
--- fs/f2fs/file.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
fs/f2fs/file.c +10 -0
@@ -3570,9 +3570,12 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)struct dnode_of_data dn;pgoff_t end_offset, count; +f2fs_lock_op (sbi); +set_new_dnode (&dn, inode,NULL ,NULL ,0 ); ret =f2fs_get_dnode_of_data (&dn, page_idx,LOOKUP_NODE );if (ret) { +f2fs_unlock_op (sbi);if (ret == -ENOENT ) { page_idx =f2fs_get_next_page_offset (&dn, page_idx);
@@ -3590,6 +3593,8 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)f2fs_put_dnode (&dn); +f2fs_unlock_op (sbi); +if (ret <0 )break ;
@@ -3742,9 +3747,12 @@ static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg)struct dnode_of_data dn;pgoff_t end_offset, count; +f2fs_lock_op (sbi); +set_new_dnode (&dn, inode,NULL ,NULL ,0 ); ret =f2fs_get_dnode_of_data (&dn, page_idx,LOOKUP_NODE );if (ret) { +f2fs_unlock_op (sbi);if (ret == -ENOENT ) { page_idx =f2fs_get_next_page_offset (&dn, page_idx);
@@ -3762,6 +3770,8 @@ static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg)f2fs_put_dnode (&dn); +f2fs_unlock_op (sbi); +if (ret <0 )break ;
Signature · 1 line
2.40.1
Chao Yu
maintainerPATCH 5/5 f2fs: compress: don't allow unaligned truncation on released compress inode
In reply to Chao Yuf2fs image may be corrupted after below testcase:
- mkfs.f2fs -O extra_attr,compression -f /dev/vdb
- mount /dev/vdb /mnt/f2fs
- touch /mnt/f2fs/file
- f2fs_io setflags compression /mnt/f2fs/file
- dd if=/dev/zero of=/mnt/f2fs/file bs=4k count=4
- f2fs_io release_cblocks /mnt/f2fs/file
- truncate -s 8192 /mnt/f2fs/file
- umount /mnt/f2fs
- fsck.f2fs /dev/vdb
[ASSERT] (fsck_chk_inode_blk:1256) --> ino: 0x5 has i_blocks: 0x00000002, but has 0x3 blocks
[FSCK] valid_block_count matching with CP [Fail] [0x4, 0x5]
[FSCK] other corrupted bugs [Fail]
The reason is: partial truncation assume compressed inode has reserved blocks, after partial truncation, valid block count may change w/ .i_blocks and .total_valid_block_count update, result in corruption.
This patch only allow cluster size aligned truncation on released compress inode for fixing.
--- fs/f2fs/file.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
fs/f2fs/file.c +8 -3
@@ -952,9 +952,14 @@ int f2fs_setattr(struct mnt_idmap *idmap, struct dentry *dentry, ATTR_GID | ATTR_TIMES_SET))))return -EPERM ; -if ((attr->ia_valid &ATTR_SIZE ) && - !f2fs_is_compress_backend_ready (inode)) -return -EOPNOTSUPP ; +if ((attr->ia_valid &ATTR_SIZE )) { +if (!f2fs_is_compress_backend_ready (inode)) +return -EOPNOTSUPP ; +if (is_inode_flag_set (inode,FI_COMPRESS_RELEASED ) && + (attr->ia_size % +F2FS_BLK_TO_BYTES (F2FS_I (inode)->i_cluster_size))) +return -EINVAL ; + } err =setattr_prepare (idmap, dentry, attr);if (err)
Signature · 1 line
2.40.1
This series was applied to jaegeuk/f2fs.git (dev) by Jaegeuk Kim <[email protected]>:
Hello:
This series was applied to jaegeuk/f2fs.git (dev)
by Jaegeuk Kim <[email protected]>:
Quoted 8 lines · On Mon, 6 May 2024 18:41:36 +0800 you wrote:
Previously, we account reserved blocks and compressed blocks into @compr_blocks, then,
f2fs_i_compr_blocks_update(,compr_blocks)will updatei_compr_blocksincorrectly, fix it.Meanwhile, for the case all blocks in cluster were reserved, fix to update
dn->ofs_in_nodecorrectly.[...]
Here is the summary with links:
- [f2fs-dev,1/5] f2fs: compress: fix to update i_compr_blocks correctly
https://git.kernel.org/jaegeuk/f2fs/c/186e7d71534d - [f2fs-dev,2/5] f2fs: compress: fix error path of inc_valid_block_count() https://git.kernel.org/jaegeuk/f2fs/c/043c832371cd - [f2fs-dev,3/5] f2fs: compress: fix typo in f2fs_reserve_compress_blocks() https://git.kernel.org/jaegeuk/f2fs/c/a3a0bc6c2239 - [f2fs-dev,4/5] f2fs: compress: fix to cover {reserve, release}_compress_blocks() w/ cp_rwsem lock https://git.kernel.org/jaegeuk/f2fs/c/0a4ed2d97cb6
- [f2fs-dev,5/5] f2fs: compress: don't allow unaligned truncation on released compress inode (no matching commit)
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html