Skip to content

Commit

Permalink
f2fs: fix to do sanity check on blocks for inline_data inode
Browse files Browse the repository at this point in the history
[ Upstream commit c240c87 ]

inode can be fuzzed, so it can has F2FS_INLINE_DATA flag and valid
i_blocks/i_nid value, this patch supports to do extra sanity check
to detect such corrupted state.

Signed-off-by: Chao Yu <[email protected]>
Signed-off-by: Jaegeuk Kim <[email protected]>
Signed-off-by: Sasha Levin <[email protected]>
  • Loading branch information
chaseyu authored and gregkh committed Sep 8, 2024
1 parent 4540181 commit f909236
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion fs/f2fs/f2fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -4154,7 +4154,7 @@ extern struct kmem_cache *f2fs_inode_entry_slab;
* inline.c
*/
bool f2fs_may_inline_data(struct inode *inode);
bool f2fs_sanity_check_inline_data(struct inode *inode);
bool f2fs_sanity_check_inline_data(struct inode *inode, struct page *ipage);
bool f2fs_may_inline_dentry(struct inode *inode);
void f2fs_do_read_inline_data(struct folio *folio, struct page *ipage);
void f2fs_truncate_inline_inode(struct inode *inode,
Expand Down
20 changes: 19 additions & 1 deletion fs/f2fs/inline.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,29 @@ bool f2fs_may_inline_data(struct inode *inode)
return !f2fs_post_read_required(inode);
}

bool f2fs_sanity_check_inline_data(struct inode *inode)
static bool inode_has_blocks(struct inode *inode, struct page *ipage)
{
struct f2fs_inode *ri = F2FS_INODE(ipage);
int i;

if (F2FS_HAS_BLOCKS(inode))
return true;

for (i = 0; i < DEF_NIDS_PER_INODE; i++) {
if (ri->i_nid[i])
return true;
}
return false;
}

bool f2fs_sanity_check_inline_data(struct inode *inode, struct page *ipage)
{
if (!f2fs_has_inline_data(inode))
return false;

if (inode_has_blocks(inode, ipage))
return false;

if (!support_inline_data(inode))
return true;

Expand Down
2 changes: 1 addition & 1 deletion fs/f2fs/inode.c
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ static bool sanity_check_inode(struct inode *inode, struct page *node_page)
}
}

if (f2fs_sanity_check_inline_data(inode)) {
if (f2fs_sanity_check_inline_data(inode, node_page)) {
f2fs_warn(sbi, "%s: inode (ino=%lx, mode=%u) should not have inline_data, run fsck to fix",
__func__, inode->i_ino, inode->i_mode);
return false;
Expand Down

0 comments on commit f909236

Please sign in to comment.