Skip to content

Commit 60f02a0

Browse files
chaseyumetti
authored andcommitted
UPSTREAM: f2fs: atomic: fix to avoid racing w/ GC
Case #1: SQLite App GC Thread Kworker Shrinker - f2fs_ioc_start_atomic_write - f2fs_ioc_commit_atomic_write - f2fs_commit_atomic_write - filemap_write_and_wait_range : write atomic_file's data to cow_inode echo 3 > drop_caches to drop atomic_file's cache. - f2fs_gc - gc_data_segment - move_data_page - set_page_dirty - writepages - f2fs_do_write_data_page : overwrite atomic_file's data to cow_inode - f2fs_down_write(&fi->i_gc_rwsem[WRITE]) - __f2fs_commit_atomic_write - f2fs_up_write(&fi->i_gc_rwsem[WRITE]) Case #2: SQLite App GC Thread Kworker - f2fs_ioc_start_atomic_write - __writeback_single_inode - do_writepages - f2fs_write_cache_pages - f2fs_write_single_data_page - f2fs_do_write_data_page : write atomic_file's data to cow_inode - f2fs_gc - gc_data_segment - move_data_page - set_page_dirty - writepages - f2fs_do_write_data_page : overwrite atomic_file's data to cow_inode - f2fs_ioc_commit_atomic_write In above cases racing in between atomic_write and GC, previous data in atomic_file may be overwrited to cow_file, result in data corruption. This patch introduces PAGE_PRIVATE_ATOMIC_WRITE bit flag in page.private, and use it to indicate that there is last dirty data in atomic file, and the data should be writebacked into cow_file, if the flag is not tagged in page, we should never write data across files. Fixes: 3db1de0 ("f2fs: change the current atomic write way") Cc: Daeho Jeong <daehojeong@google.com> Change-Id: I7e6c2a790b7f57c41d3a955cd9ba519d4dba12f9 Signed-off-by: Chao Yu <chao@kernel.org> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org> (cherry picked from commit 1a0bd28)
1 parent aa2602f commit 60f02a0

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

fs/f2fs/data.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2672,10 +2672,13 @@ int f2fs_do_write_data_page(struct f2fs_io_info *fio)
26722672
struct dnode_of_data dn;
26732673
struct node_info ni;
26742674
bool ipu_force = false;
2675+
bool atomic_commit;
26752676
int err = 0;
26762677

26772678
/* Use COW inode to make dnode_of_data for atomic write */
2678-
if (f2fs_is_atomic_file(inode))
2679+
atomic_commit = f2fs_is_atomic_file(inode) &&
2680+
page_private_atomic(fio->page);
2681+
if (atomic_commit)
26792682
set_new_dnode(&dn, F2FS_I(inode)->cow_inode, NULL, NULL, 0);
26802683
else
26812684
set_new_dnode(&dn, inode, NULL, NULL, 0);
@@ -2774,6 +2777,8 @@ int f2fs_do_write_data_page(struct f2fs_io_info *fio)
27742777
f2fs_outplace_write_data(&dn, fio);
27752778
trace_f2fs_do_write_data_page(page_folio(page), OPU);
27762779
set_inode_flag(inode, FI_APPEND_WRITE);
2780+
if (atomic_commit)
2781+
clear_page_private_atomic(page);
27772782
out_writepage:
27782783
f2fs_put_dnode(&dn);
27792784
out:
@@ -3743,6 +3748,9 @@ static int f2fs_write_end(struct file *file,
37433748

37443749
set_page_dirty(page);
37453750

3751+
if (f2fs_is_atomic_file(inode))
3752+
set_page_private_atomic(page);
3753+
37463754
if (pos + copied > i_size_read(inode) &&
37473755
!f2fs_verity_in_progress(inode)) {
37483756
f2fs_i_size_write(inode, pos + copied);

fs/f2fs/f2fs.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1423,7 +1423,8 @@ static inline void f2fs_clear_bit(unsigned int nr, char *addr);
14231423
* bit 1 PAGE_PRIVATE_ONGOING_MIGRATION
14241424
* bit 2 PAGE_PRIVATE_INLINE_INODE
14251425
* bit 3 PAGE_PRIVATE_REF_RESOURCE
1426-
* bit 4- f2fs private data
1426+
* bit 4 PAGE_PRIVATE_ATOMIC_WRITE
1427+
* bit 5- f2fs private data
14271428
*
14281429
* Layout B: lowest bit should be 0
14291430
* page.private is a wrapped pointer.
@@ -1433,6 +1434,7 @@ enum {
14331434
PAGE_PRIVATE_ONGOING_MIGRATION, /* data page which is on-going migrating */
14341435
PAGE_PRIVATE_INLINE_INODE, /* inode page contains inline data */
14351436
PAGE_PRIVATE_REF_RESOURCE, /* dirty page has referenced resources */
1437+
PAGE_PRIVATE_ATOMIC_WRITE, /* data page from atomic write path */
14361438
PAGE_PRIVATE_MAX
14371439
};
14381440

@@ -2403,14 +2405,17 @@ static inline void clear_page_private_##name(struct page *page) \
24032405
PAGE_PRIVATE_GET_FUNC(nonpointer, NOT_POINTER);
24042406
PAGE_PRIVATE_GET_FUNC(inline, INLINE_INODE);
24052407
PAGE_PRIVATE_GET_FUNC(gcing, ONGOING_MIGRATION);
2408+
PAGE_PRIVATE_GET_FUNC(atomic, ATOMIC_WRITE);
24062409

24072410
PAGE_PRIVATE_SET_FUNC(reference, REF_RESOURCE);
24082411
PAGE_PRIVATE_SET_FUNC(inline, INLINE_INODE);
24092412
PAGE_PRIVATE_SET_FUNC(gcing, ONGOING_MIGRATION);
2413+
PAGE_PRIVATE_SET_FUNC(atomic, ATOMIC_WRITE);
24102414

24112415
PAGE_PRIVATE_CLEAR_FUNC(reference, REF_RESOURCE);
24122416
PAGE_PRIVATE_CLEAR_FUNC(inline, INLINE_INODE);
24132417
PAGE_PRIVATE_CLEAR_FUNC(gcing, ONGOING_MIGRATION);
2418+
PAGE_PRIVATE_CLEAR_FUNC(atomic, ATOMIC_WRITE);
24142419

24152420
static inline unsigned long get_page_private_data(struct page *page)
24162421
{
@@ -2442,6 +2447,7 @@ static inline void clear_page_private_all(struct page *page)
24422447
clear_page_private_reference(page);
24432448
clear_page_private_gcing(page);
24442449
clear_page_private_inline(page);
2450+
clear_page_private_atomic(page);
24452451

24462452
f2fs_bug_on(F2FS_P_SB(page), page_private(page));
24472453
}

0 commit comments

Comments
 (0)