Skip to content

Commit 50fa53e

Browse files
chaseyuJaegeuk Kim
authored andcommitted
f2fs: fix to avoid broken of dnode block list
f2fs recovery flow is relying on dnode block link list, it means fsynced file recovery depends on previous dnode's persistence in the list, so during fsync() we should wait on all regular inode's dnode writebacked before issuing flush. By this way, we can avoid dnode block list being broken by out-of-order IO submission due to IO scheduler or driver. Sheng Yong helps to do the test with this patch: Target:/data (f2fs, -) 64MB / 32768KB / 4KB / 8 1 / PERSIST / Index Base: SEQ-RD(MB/s) SEQ-WR(MB/s) RND-RD(IOPS) RND-WR(IOPS) Insert(TPS) Update(TPS) Delete(TPS) 1 867.82 204.15 41440.03 41370.54 680.8 1025.94 1031.08 2 871.87 205.87 41370.3 40275.2 791.14 1065.84 1101.7 3 866.52 205.69 41795.67 40596.16 694.69 1037.16 1031.48 Avg 868.7366667 205.2366667 41535.33333 40747.3 722.21 1042.98 1054.753333 After: SEQ-RD(MB/s) SEQ-WR(MB/s) RND-RD(IOPS) RND-WR(IOPS) Insert(TPS) Update(TPS) Delete(TPS) 1 798.81 202.5 41143 40613.87 602.71 838.08 913.83 2 805.79 206.47 40297.2 41291.46 604.44 840.75 924.27 3 814.83 206.17 41209.57 40453.62 602.85 834.66 927.91 Avg 806.4766667 205.0466667 40883.25667 40786.31667 603.3333333 837.83 922.0033333 Patched/Original: 0.928332713 0.999074239 0.984300676 1.000957528 0.835398753 0.803303994 0.874141189 It looks like atomic write will suffer performance regression. I suspect that the criminal is that we forcing to wait all dnode being in storage cache before we issue PREFLUSH+FUA. BTW, will commit ("f2fs: don't need to wait for node writes for atomic write") cause the problem: we will lose data of last transaction after SPO, even if atomic write return no error: - atomic_open(); - write() P1, P2, P3; - atomic_commit(); - writeback data: P1, P2, P3; - writeback node: N1, N2, N3; <--- If N1, N2 is not writebacked, N3 with fsync_mark is writebacked, In SPOR, we won't find N3 since node chain is broken, turns out that losing last transaction. - preflush + fua; - power-cut If we don't wait dnode writeback for atomic_write: SEQ-RD(MB/s) SEQ-WR(MB/s) RND-RD(IOPS) RND-WR(IOPS) Insert(TPS) Update(TPS) Delete(TPS) 1 779.91 206.03 41621.5 40333.16 716.9 1038.21 1034.85 2 848.51 204.35 40082.44 39486.17 791.83 1119.96 1083.77 3 772.12 206.27 41335.25 41599.65 723.29 1055.07 971.92 Avg 800.18 205.55 41013.06333 40472.99333 744.0066667 1071.08 1030.18 Patched/Original: 0.92108464 1.001526693 0.987425886 0.993268102 1.030180511 1.026942031 0.976702294 SQLite's performance recovers. Jaegeuk: "Practically, I don't see db corruption becase of this. We can excuse to lose the last transaction." Finally, we decide to keep original implementation of atomic write interface sematics that we don't wait all dnode writeback before preflush+fua submission. Signed-off-by: Chao Yu <yuchao0@huawei.com> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
1 parent 6e45f2a commit 50fa53e

6 files changed

Lines changed: 156 additions & 31 deletions

File tree

‎fs/f2fs/checkpoint.c‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,7 @@ static void unblock_operations(struct f2fs_sb_info *sbi)
11611161
f2fs_unlock_all(sbi);
11621162
}
11631163

1164-
static void wait_on_all_pages_writeback(struct f2fs_sb_info *sbi)
1164+
void f2fs_wait_on_all_pages_writeback(struct f2fs_sb_info *sbi)
11651165
{
11661166
DEFINE_WAIT(wait);
11671167

@@ -1397,7 +1397,7 @@ static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
13971397
f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
13981398

13991399
/* wait for previous submitted meta pages writeback */
1400-
wait_on_all_pages_writeback(sbi);
1400+
f2fs_wait_on_all_pages_writeback(sbi);
14011401

14021402
/* flush all device cache */
14031403
err = f2fs_flush_device_cache(sbi);
@@ -1406,7 +1406,7 @@ static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
14061406

14071407
/* barrier and flush checkpoint cp pack 2 page if it can */
14081408
commit_checkpoint(sbi, ckpt, start_blk);
1409-
wait_on_all_pages_writeback(sbi);
1409+
f2fs_wait_on_all_pages_writeback(sbi);
14101410

14111411
/*
14121412
* invalidate intermediate page cache borrowed from meta inode
@@ -1418,6 +1418,8 @@ static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
14181418

14191419
f2fs_release_ino_entry(sbi, false);
14201420

1421+
f2fs_reset_fsync_node_info(sbi);
1422+
14211423
clear_sbi_flag(sbi, SBI_IS_DIRTY);
14221424
clear_sbi_flag(sbi, SBI_NEED_CP);
14231425
__set_cp_next_pack(sbi);

‎fs/f2fs/data.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ static void f2fs_write_end_io(struct bio *bio)
177177
page->index != nid_of_node(page));
178178

179179
dec_page_count(sbi, type);
180+
if (f2fs_in_warm_node_list(sbi, page))
181+
f2fs_del_fsync_node_entry(sbi, page);
180182
clear_cold_data(page);
181183
end_page_writeback(page);
182184
}

‎fs/f2fs/f2fs.h‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,12 @@ struct inode_entry {
228228
struct inode *inode; /* vfs inode pointer */
229229
};
230230

231+
struct fsync_node_entry {
232+
struct list_head list; /* list head */
233+
struct page *page; /* warm node page pointer */
234+
unsigned int seq_id; /* sequence id */
235+
};
236+
231237
/* for the bitmap indicate blocks to be discarded */
232238
struct discard_entry {
233239
struct list_head list; /* list head */
@@ -1156,6 +1162,11 @@ struct f2fs_sb_info {
11561162

11571163
struct inode_management im[MAX_INO_ENTRY]; /* manage inode cache */
11581164

1165+
spinlock_t fsync_node_lock; /* for node entry lock */
1166+
struct list_head fsync_node_list; /* node list head */
1167+
unsigned int fsync_seg_id; /* sequence id */
1168+
unsigned int fsync_node_num; /* number of node entries */
1169+
11591170
/* for orphan inode, use 0'th array */
11601171
unsigned int max_orphans; /* max orphan inodes */
11611172

@@ -2827,6 +2838,10 @@ struct node_info;
28272838

28282839
int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid);
28292840
bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type);
2841+
bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct page *page);
2842+
void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi);
2843+
void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct page *page);
2844+
void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi);
28302845
int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
28312846
bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
28322847
bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
@@ -2836,7 +2851,8 @@ pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
28362851
int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
28372852
int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from);
28382853
int f2fs_truncate_xattr_node(struct inode *inode);
2839-
int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino);
2854+
int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
2855+
unsigned int seq_id);
28402856
int f2fs_remove_inode_page(struct inode *inode);
28412857
struct page *f2fs_new_inode_page(struct inode *inode);
28422858
struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs);
@@ -2845,7 +2861,8 @@ struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
28452861
struct page *f2fs_get_node_page_ra(struct page *parent, int start);
28462862
void f2fs_move_node_page(struct page *node_page, int gc_type);
28472863
int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
2848-
struct writeback_control *wbc, bool atomic);
2864+
struct writeback_control *wbc, bool atomic,
2865+
unsigned int *seq_id);
28492866
int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
28502867
struct writeback_control *wbc,
28512868
bool do_balance, enum iostat_type io_type);
@@ -2962,6 +2979,7 @@ int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi);
29622979
void f2fs_update_dirty_page(struct inode *inode, struct page *page);
29632980
void f2fs_remove_dirty_inode(struct inode *inode);
29642981
int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
2982+
void f2fs_wait_on_all_pages_writeback(struct f2fs_sb_info *sbi);
29652983
int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
29662984
void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi);
29672985
int __init f2fs_create_checkpoint_caches(void);

‎fs/f2fs/file.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
213213
.nr_to_write = LONG_MAX,
214214
.for_reclaim = 0,
215215
};
216+
unsigned int seq_id = 0;
216217

217218
if (unlikely(f2fs_readonly(inode->i_sb)))
218219
return 0;
@@ -275,7 +276,7 @@ static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
275276
}
276277
sync_nodes:
277278
atomic_inc(&sbi->wb_sync_req[NODE]);
278-
ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic);
279+
ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic, &seq_id);
279280
atomic_dec(&sbi->wb_sync_req[NODE]);
280281
if (ret)
281282
goto out;
@@ -301,7 +302,7 @@ static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
301302
* given fsync mark.
302303
*/
303304
if (!atomic) {
304-
ret = f2fs_wait_on_node_pages_writeback(sbi, ino);
305+
ret = f2fs_wait_on_node_pages_writeback(sbi, seq_id);
305306
if (ret)
306307
goto out;
307308
}

‎fs/f2fs/node.c‎

Lines changed: 120 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
static struct kmem_cache *nat_entry_slab;
2929
static struct kmem_cache *free_nid_slab;
3030
static struct kmem_cache *nat_entry_set_slab;
31+
static struct kmem_cache *fsync_node_entry_slab;
3132

3233
/*
3334
* Check whether the given nid is within node id range.
@@ -264,6 +265,72 @@ static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info *nm_i,
264265
start, nr);
265266
}
266267

268+
bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct page *page)
269+
{
270+
return NODE_MAPPING(sbi) == page->mapping &&
271+
IS_DNODE(page) && is_cold_node(page);
272+
}
273+
274+
void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi)
275+
{
276+
spin_lock_init(&sbi->fsync_node_lock);
277+
INIT_LIST_HEAD(&sbi->fsync_node_list);
278+
sbi->fsync_seg_id = 0;
279+
sbi->fsync_node_num = 0;
280+
}
281+
282+
static unsigned int f2fs_add_fsync_node_entry(struct f2fs_sb_info *sbi,
283+
struct page *page)
284+
{
285+
struct fsync_node_entry *fn;
286+
unsigned long flags;
287+
unsigned int seq_id;
288+
289+
fn = f2fs_kmem_cache_alloc(fsync_node_entry_slab, GFP_NOFS);
290+
291+
get_page(page);
292+
fn->page = page;
293+
INIT_LIST_HEAD(&fn->list);
294+
295+
spin_lock_irqsave(&sbi->fsync_node_lock, flags);
296+
list_add_tail(&fn->list, &sbi->fsync_node_list);
297+
fn->seq_id = sbi->fsync_seg_id++;
298+
seq_id = fn->seq_id;
299+
sbi->fsync_node_num++;
300+
spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
301+
302+
return seq_id;
303+
}
304+
305+
void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct page *page)
306+
{
307+
struct fsync_node_entry *fn;
308+
unsigned long flags;
309+
310+
spin_lock_irqsave(&sbi->fsync_node_lock, flags);
311+
list_for_each_entry(fn, &sbi->fsync_node_list, list) {
312+
if (fn->page == page) {
313+
list_del(&fn->list);
314+
sbi->fsync_node_num--;
315+
spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
316+
kmem_cache_free(fsync_node_entry_slab, fn);
317+
put_page(page);
318+
return;
319+
}
320+
}
321+
spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
322+
f2fs_bug_on(sbi, 1);
323+
}
324+
325+
void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi)
326+
{
327+
unsigned long flags;
328+
329+
spin_lock_irqsave(&sbi->fsync_node_lock, flags);
330+
sbi->fsync_seg_id = 0;
331+
spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
332+
}
333+
267334
int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid)
268335
{
269336
struct f2fs_nm_info *nm_i = NM_I(sbi);
@@ -1388,7 +1455,7 @@ static struct page *last_fsync_dnode(struct f2fs_sb_info *sbi, nid_t ino)
13881455

13891456
static int __write_node_page(struct page *page, bool atomic, bool *submitted,
13901457
struct writeback_control *wbc, bool do_balance,
1391-
enum iostat_type io_type)
1458+
enum iostat_type io_type, unsigned int *seq_id)
13921459
{
13931460
struct f2fs_sb_info *sbi = F2FS_P_SB(page);
13941461
nid_t nid;
@@ -1405,6 +1472,7 @@ static int __write_node_page(struct page *page, bool atomic, bool *submitted,
14051472
.io_type = io_type,
14061473
.io_wbc = wbc,
14071474
};
1475+
unsigned int seq;
14081476

14091477
trace_f2fs_writepage(page, NODE);
14101478

@@ -1450,6 +1518,13 @@ static int __write_node_page(struct page *page, bool atomic, bool *submitted,
14501518

14511519
set_page_writeback(page);
14521520
ClearPageError(page);
1521+
1522+
if (f2fs_in_warm_node_list(sbi, page)) {
1523+
seq = f2fs_add_fsync_node_entry(sbi, page);
1524+
if (seq_id)
1525+
*seq_id = seq;
1526+
}
1527+
14531528
fio.old_blkaddr = ni.blk_addr;
14541529
f2fs_do_write_node_page(nid, &fio);
14551530
set_node_addr(sbi, &ni, fio.new_blkaddr, is_fsync_dnode(page));
@@ -1497,7 +1572,7 @@ void f2fs_move_node_page(struct page *node_page, int gc_type)
14971572
goto out_page;
14981573

14991574
if (__write_node_page(node_page, false, NULL,
1500-
&wbc, false, FS_GC_NODE_IO))
1575+
&wbc, false, FS_GC_NODE_IO, NULL))
15011576
unlock_page(node_page);
15021577
goto release_page;
15031578
} else {
@@ -1514,11 +1589,13 @@ void f2fs_move_node_page(struct page *node_page, int gc_type)
15141589
static int f2fs_write_node_page(struct page *page,
15151590
struct writeback_control *wbc)
15161591
{
1517-
return __write_node_page(page, false, NULL, wbc, false, FS_NODE_IO);
1592+
return __write_node_page(page, false, NULL, wbc, false,
1593+
FS_NODE_IO, NULL);
15181594
}
15191595

15201596
int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
1521-
struct writeback_control *wbc, bool atomic)
1597+
struct writeback_control *wbc, bool atomic,
1598+
unsigned int *seq_id)
15221599
{
15231600
pgoff_t index;
15241601
pgoff_t last_idx = ULONG_MAX;
@@ -1599,7 +1676,7 @@ int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
15991676
ret = __write_node_page(page, atomic &&
16001677
page == last_page,
16011678
&submitted, wbc, true,
1602-
FS_NODE_IO);
1679+
FS_NODE_IO, seq_id);
16031680
if (ret) {
16041681
unlock_page(page);
16051682
f2fs_put_page(last_page, 0);
@@ -1716,7 +1793,7 @@ int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
17161793
set_dentry_mark(page, 0);
17171794

17181795
ret = __write_node_page(page, false, &submitted,
1719-
wbc, do_balance, io_type);
1796+
wbc, do_balance, io_type, NULL);
17201797
if (ret)
17211798
unlock_page(page);
17221799
else if (submitted)
@@ -1749,35 +1826,46 @@ int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
17491826
return ret;
17501827
}
17511828

1752-
int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino)
1829+
int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
1830+
unsigned int seq_id)
17531831
{
1754-
pgoff_t index = 0;
1755-
struct pagevec pvec;
1832+
struct fsync_node_entry *fn;
1833+
struct page *page;
1834+
struct list_head *head = &sbi->fsync_node_list;
1835+
unsigned long flags;
1836+
unsigned int cur_seq_id = 0;
17561837
int ret2, ret = 0;
1757-
int nr_pages;
17581838

1759-
pagevec_init(&pvec);
1839+
while (seq_id && cur_seq_id < seq_id) {
1840+
spin_lock_irqsave(&sbi->fsync_node_lock, flags);
1841+
if (list_empty(head)) {
1842+
spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
1843+
break;
1844+
}
1845+
fn = list_first_entry(head, struct fsync_node_entry, list);
1846+
if (fn->seq_id > seq_id) {
1847+
spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
1848+
break;
1849+
}
1850+
cur_seq_id = fn->seq_id;
1851+
page = fn->page;
1852+
get_page(page);
1853+
spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
17601854

1761-
while ((nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
1762-
PAGECACHE_TAG_WRITEBACK))) {
1763-
int i;
1855+
f2fs_wait_on_page_writeback(page, NODE, true);
1856+
if (TestClearPageError(page))
1857+
ret = -EIO;
17641858

1765-
for (i = 0; i < nr_pages; i++) {
1766-
struct page *page = pvec.pages[i];
1859+
put_page(page);
17671860

1768-
if (ino && ino_of_node(page) == ino) {
1769-
f2fs_wait_on_page_writeback(page, NODE, true);
1770-
if (TestClearPageError(page))
1771-
ret = -EIO;
1772-
}
1773-
}
1774-
pagevec_release(&pvec);
1775-
cond_resched();
1861+
if (ret)
1862+
break;
17761863
}
17771864

17781865
ret2 = filemap_check_errors(NODE_MAPPING(sbi));
17791866
if (!ret)
17801867
ret = ret2;
1868+
17811869
return ret;
17821870
}
17831871

@@ -2992,8 +3080,15 @@ int __init f2fs_create_node_manager_caches(void)
29923080
sizeof(struct nat_entry_set));
29933081
if (!nat_entry_set_slab)
29943082
goto destroy_free_nid;
3083+
3084+
fsync_node_entry_slab = f2fs_kmem_cache_create("fsync_node_entry",
3085+
sizeof(struct fsync_node_entry));
3086+
if (!fsync_node_entry_slab)
3087+
goto destroy_nat_entry_set;
29953088
return 0;
29963089

3090+
destroy_nat_entry_set:
3091+
kmem_cache_destroy(nat_entry_set_slab);
29973092
destroy_free_nid:
29983093
kmem_cache_destroy(free_nid_slab);
29993094
destroy_nat_entry:
@@ -3004,6 +3099,7 @@ int __init f2fs_create_node_manager_caches(void)
30043099

30053100
void f2fs_destroy_node_manager_caches(void)
30063101
{
3102+
kmem_cache_destroy(fsync_node_entry_slab);
30073103
kmem_cache_destroy(nat_entry_set_slab);
30083104
kmem_cache_destroy(free_nid_slab);
30093105
kmem_cache_destroy(nat_entry_slab);

‎fs/f2fs/super.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,10 @@ static void f2fs_put_super(struct super_block *sb)
10361036
/* our cp_error case, we can wait for any writeback page */
10371037
f2fs_flush_merged_writes(sbi);
10381038

1039+
f2fs_wait_on_all_pages_writeback(sbi);
1040+
1041+
f2fs_bug_on(sbi, sbi->fsync_node_num);
1042+
10391043
iput(sbi->node_inode);
10401044
iput(sbi->meta_inode);
10411045

@@ -2911,6 +2915,8 @@ static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
29112915

29122916
f2fs_init_ino_entry_info(sbi);
29132917

2918+
f2fs_init_fsync_node_info(sbi);
2919+
29142920
/* setup f2fs internal modules */
29152921
err = f2fs_build_segment_manager(sbi);
29162922
if (err) {

0 commit comments

Comments
 (0)