Skip to content

Commit d889928

Browse files
chaseyugregkh
authored andcommitted
f2fs: atomic: fix to avoid racing w/ GC
[ Upstream commit 1a0bd28 ] 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> Signed-off-by: Chao Yu <chao@kernel.org> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 8edf3a4 commit d889928

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

fs/f2fs/data.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -2623,10 +2623,13 @@ int f2fs_do_write_data_page(struct f2fs_io_info *fio)
26232623
struct dnode_of_data dn;
26242624
struct node_info ni;
26252625
bool ipu_force = false;
2626+
bool atomic_commit;
26262627
int err = 0;
26272628

26282629
/* Use COW inode to make dnode_of_data for atomic write */
2629-
if (f2fs_is_atomic_file(inode))
2630+
atomic_commit = f2fs_is_atomic_file(inode) &&
2631+
page_private_atomic(fio->page);
2632+
if (atomic_commit)
26302633
set_new_dnode(&dn, F2FS_I(inode)->cow_inode, NULL, NULL, 0);
26312634
else
26322635
set_new_dnode(&dn, inode, NULL, NULL, 0);
@@ -2730,6 +2733,8 @@ int f2fs_do_write_data_page(struct f2fs_io_info *fio)
27302733
f2fs_outplace_write_data(&dn, fio);
27312734
trace_f2fs_do_write_data_page(page, OPU);
27322735
set_inode_flag(inode, FI_APPEND_WRITE);
2736+
if (atomic_commit)
2737+
clear_page_private_atomic(page);
27332738
out_writepage:
27342739
f2fs_put_dnode(&dn);
27352740
out:
@@ -3700,6 +3705,9 @@ static int f2fs_write_end(struct file *file,
37003705

37013706
set_page_dirty(page);
37023707

3708+
if (f2fs_is_atomic_file(inode))
3709+
set_page_private_atomic(page);
3710+
37033711
if (pos + copied > i_size_read(inode) &&
37043712
!f2fs_verity_in_progress(inode)) {
37053713
f2fs_i_size_write(inode, pos + copied);

fs/f2fs/f2fs.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,8 @@ static inline void f2fs_clear_bit(unsigned int nr, char *addr);
14111411
* bit 1 PAGE_PRIVATE_ONGOING_MIGRATION
14121412
* bit 2 PAGE_PRIVATE_INLINE_INODE
14131413
* bit 3 PAGE_PRIVATE_REF_RESOURCE
1414-
* bit 4- f2fs private data
1414+
* bit 4 PAGE_PRIVATE_ATOMIC_WRITE
1415+
* bit 5- f2fs private data
14151416
*
14161417
* Layout B: lowest bit should be 0
14171418
* page.private is a wrapped pointer.
@@ -1421,6 +1422,7 @@ enum {
14211422
PAGE_PRIVATE_ONGOING_MIGRATION, /* data page which is on-going migrating */
14221423
PAGE_PRIVATE_INLINE_INODE, /* inode page contains inline data */
14231424
PAGE_PRIVATE_REF_RESOURCE, /* dirty page has referenced resources */
1425+
PAGE_PRIVATE_ATOMIC_WRITE, /* data page from atomic write path */
14241426
PAGE_PRIVATE_MAX
14251427
};
14261428

@@ -2386,14 +2388,17 @@ static inline void clear_page_private_##name(struct page *page) \
23862388
PAGE_PRIVATE_GET_FUNC(nonpointer, NOT_POINTER);
23872389
PAGE_PRIVATE_GET_FUNC(inline, INLINE_INODE);
23882390
PAGE_PRIVATE_GET_FUNC(gcing, ONGOING_MIGRATION);
2391+
PAGE_PRIVATE_GET_FUNC(atomic, ATOMIC_WRITE);
23892392

23902393
PAGE_PRIVATE_SET_FUNC(reference, REF_RESOURCE);
23912394
PAGE_PRIVATE_SET_FUNC(inline, INLINE_INODE);
23922395
PAGE_PRIVATE_SET_FUNC(gcing, ONGOING_MIGRATION);
2396+
PAGE_PRIVATE_SET_FUNC(atomic, ATOMIC_WRITE);
23932397

23942398
PAGE_PRIVATE_CLEAR_FUNC(reference, REF_RESOURCE);
23952399
PAGE_PRIVATE_CLEAR_FUNC(inline, INLINE_INODE);
23962400
PAGE_PRIVATE_CLEAR_FUNC(gcing, ONGOING_MIGRATION);
2401+
PAGE_PRIVATE_CLEAR_FUNC(atomic, ATOMIC_WRITE);
23972402

23982403
static inline unsigned long get_page_private_data(struct page *page)
23992404
{
@@ -2425,6 +2430,7 @@ static inline void clear_page_private_all(struct page *page)
24252430
clear_page_private_reference(page);
24262431
clear_page_private_gcing(page);
24272432
clear_page_private_inline(page);
2433+
clear_page_private_atomic(page);
24282434

24292435
f2fs_bug_on(F2FS_P_SB(page), page_private(page));
24302436
}

0 commit comments

Comments
 (0)