Skip to content

Commit 9f0c4a4

Browse files
chaseyuJaegeuk Kim
authored andcommitted
f2fs: fix to truncate meta inode pages forcely
Below race case can cause data corruption: Thread A GC thread - gc_data_segment - ra_data_block - locked meta_inode page - f2fs_inplace_write_data - invalidate_mapping_pages : fail to invalidate meta_inode page due to lock failure or dirty|writeback status - f2fs_submit_page_bio : write last dirty data to old blkaddr - move_data_block - load old data from meta_inode page - f2fs_submit_page_write : write old data to new blkaddr Because invalidate_mapping_pages() will skip invalidating page which has unclear status including locked, dirty, writeback and so on, so we need to use truncate_inode_pages_range() instead of invalidate_mapping_pages() to make sure meta_inode page will be dropped. Fixes: 6aa58d8 ("f2fs: readahead encrypted block during GC") Fixes: e3b49ea ("f2fs: invalidate META_MAPPING before IPU/DIO write") Signed-off-by: Chao Yu <[email protected]> Signed-off-by: Jaegeuk Kim <[email protected]>
1 parent 2f6d721 commit 9f0c4a4

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

fs/f2fs/checkpoint.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,8 +1598,9 @@ static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
15981598
*/
15991599
if (f2fs_sb_has_encrypt(sbi) || f2fs_sb_has_verity(sbi) ||
16001600
f2fs_sb_has_compression(sbi))
1601-
invalidate_mapping_pages(META_MAPPING(sbi),
1602-
MAIN_BLKADDR(sbi), MAX_BLKADDR(sbi) - 1);
1601+
f2fs_bug_on(sbi,
1602+
invalidate_inode_pages2_range(META_MAPPING(sbi),
1603+
MAIN_BLKADDR(sbi), MAX_BLKADDR(sbi) - 1));
16031604

16041605
f2fs_release_ino_entry(sbi, false);
16051606

fs/f2fs/f2fs.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4655,10 +4655,36 @@ static inline bool f2fs_is_readonly(struct f2fs_sb_info *sbi)
46554655
return f2fs_sb_has_readonly(sbi) || f2fs_readonly(sbi->sb);
46564656
}
46574657

4658+
static inline void f2fs_truncate_meta_inode_pages(struct f2fs_sb_info *sbi,
4659+
block_t blkaddr, unsigned int cnt)
4660+
{
4661+
bool need_submit = false;
4662+
int i = 0;
4663+
4664+
do {
4665+
struct page *page;
4666+
4667+
page = find_get_page(META_MAPPING(sbi), blkaddr + i);
4668+
if (page) {
4669+
if (PageWriteback(page))
4670+
need_submit = true;
4671+
f2fs_put_page(page, 0);
4672+
}
4673+
} while (++i < cnt && !need_submit);
4674+
4675+
if (need_submit)
4676+
f2fs_submit_merged_write_cond(sbi, sbi->meta_inode,
4677+
NULL, 0, DATA);
4678+
4679+
truncate_inode_pages_range(META_MAPPING(sbi),
4680+
F2FS_BLK_TO_BYTES((loff_t)blkaddr),
4681+
F2FS_BLK_END_BYTES((loff_t)(blkaddr + cnt - 1)));
4682+
}
4683+
46584684
static inline void f2fs_invalidate_internal_cache(struct f2fs_sb_info *sbi,
46594685
block_t blkaddr)
46604686
{
4661-
invalidate_mapping_pages(META_MAPPING(sbi), blkaddr, blkaddr);
4687+
f2fs_truncate_meta_inode_pages(sbi, blkaddr, 1);
46624688
f2fs_invalidate_compress_page(sbi, blkaddr);
46634689
}
46644690

fs/f2fs/segment.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3741,8 +3741,7 @@ int f2fs_inplace_write_data(struct f2fs_io_info *fio)
37413741
}
37423742

37433743
if (fio->post_read)
3744-
invalidate_mapping_pages(META_MAPPING(sbi),
3745-
fio->new_blkaddr, fio->new_blkaddr);
3744+
f2fs_truncate_meta_inode_pages(sbi, fio->new_blkaddr, 1);
37463745

37473746
stat_inc_inplace_blocks(fio->sbi);
37483747

@@ -3932,7 +3931,7 @@ void f2fs_wait_on_block_writeback_range(struct inode *inode, block_t blkaddr,
39323931
for (i = 0; i < len; i++)
39333932
f2fs_wait_on_block_writeback(inode, blkaddr + i);
39343933

3935-
invalidate_mapping_pages(META_MAPPING(sbi), blkaddr, blkaddr + len - 1);
3934+
f2fs_truncate_meta_inode_pages(sbi, blkaddr, len);
39363935
}
39373936

39383937
static int read_compacted_summaries(struct f2fs_sb_info *sbi)

include/linux/f2fs_fs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#define F2FS_BYTES_TO_BLK(bytes) ((bytes) >> F2FS_BLKSIZE_BITS)
2929
#define F2FS_BLK_TO_BYTES(blk) ((blk) << F2FS_BLKSIZE_BITS)
30+
#define F2FS_BLK_END_BYTES(blk) (F2FS_BLK_TO_BYTES(blk + 1) - 1)
3031

3132
/* 0, 1(node nid), 2(meta nid) are reserved node id */
3233
#define F2FS_RESERVED_NODE_NUM 3

0 commit comments

Comments
 (0)