hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/fs/f2fs/checkpoint.c
....@@ -13,22 +13,28 @@
1313 #include <linux/f2fs_fs.h>
1414 #include <linux/pagevec.h>
1515 #include <linux/swap.h>
16
+#include <linux/kthread.h>
1617
1718 #include "f2fs.h"
1819 #include "node.h"
1920 #include "segment.h"
20
-#include "trace.h"
2121 #include <trace/events/f2fs.h>
22
+
23
+#define DEFAULT_CHECKPOINT_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
2224
2325 static struct kmem_cache *ino_entry_slab;
2426 struct kmem_cache *f2fs_inode_entry_slab;
2527
26
-void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io)
28
+void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io,
29
+ unsigned char reason)
2730 {
2831 f2fs_build_fault_attr(sbi, 0, 0);
2932 set_ckpt_flags(sbi, CP_ERROR_FLAG);
30
- if (!end_io)
33
+ if (!end_io) {
3134 f2fs_flush_merged_writes(sbi);
35
+
36
+ f2fs_handle_stop(sbi, reason);
37
+ }
3238 }
3339
3440 /*
....@@ -37,7 +43,7 @@
3743 struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
3844 {
3945 struct address_space *mapping = META_MAPPING(sbi);
40
- struct page *page = NULL;
46
+ struct page *page;
4147 repeat:
4248 page = f2fs_grab_cache_page(mapping, index, false);
4349 if (!page) {
....@@ -107,7 +113,7 @@
107113 return __get_meta_page(sbi, index, true);
108114 }
109115
110
-struct page *f2fs_get_meta_page_nofail(struct f2fs_sb_info *sbi, pgoff_t index)
116
+struct page *f2fs_get_meta_page_retry(struct f2fs_sb_info *sbi, pgoff_t index)
111117 {
112118 struct page *page;
113119 int count = 0;
....@@ -118,7 +124,7 @@
118124 if (PTR_ERR(page) == -EIO &&
119125 ++count <= DEFAULT_RETRY_IO_COUNT)
120126 goto retry;
121
- f2fs_stop_checkpoint(sbi, false);
127
+ f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_META_PAGE);
122128 }
123129 return page;
124130 }
....@@ -136,7 +142,7 @@
136142 unsigned int segno, offset;
137143 bool exist;
138144
139
- if (type != DATA_GENERIC_ENHANCE && type != DATA_GENERIC_ENHANCE_READ)
145
+ if (type == DATA_GENERIC)
140146 return true;
141147
142148 segno = GET_SEGNO(sbi, blkaddr);
....@@ -144,11 +150,18 @@
144150 se = get_seg_entry(sbi, segno);
145151
146152 exist = f2fs_test_bit(offset, se->cur_valid_map);
153
+ if (exist && type == DATA_GENERIC_ENHANCE_UPDATE) {
154
+ f2fs_err(sbi, "Inconsistent error blkaddr:%u, sit bitmap:%d",
155
+ blkaddr, exist);
156
+ set_sbi_flag(sbi, SBI_NEED_FSCK);
157
+ return exist;
158
+ }
159
+
147160 if (!exist && type == DATA_GENERIC_ENHANCE) {
148161 f2fs_err(sbi, "Inconsistent error blkaddr:%u, sit bitmap:%d",
149162 blkaddr, exist);
150163 set_sbi_flag(sbi, SBI_NEED_FSCK);
151
- WARN_ON(1);
164
+ dump_stack();
152165 }
153166 return exist;
154167 }
....@@ -181,12 +194,13 @@
181194 case DATA_GENERIC:
182195 case DATA_GENERIC_ENHANCE:
183196 case DATA_GENERIC_ENHANCE_READ:
197
+ case DATA_GENERIC_ENHANCE_UPDATE:
184198 if (unlikely(blkaddr >= MAX_BLKADDR(sbi) ||
185199 blkaddr < MAIN_BLKADDR(sbi))) {
186200 f2fs_warn(sbi, "access invalid blkaddr:%u",
187201 blkaddr);
188202 set_sbi_flag(sbi, SBI_NEED_FSCK);
189
- WARN_ON(1);
203
+ dump_stack();
190204 return false;
191205 } else {
192206 return __is_bitmap_valid(sbi, blkaddr, type);
....@@ -301,8 +315,15 @@
301315
302316 trace_f2fs_writepage(page, META);
303317
304
- if (unlikely(f2fs_cp_error(sbi)))
318
+ if (unlikely(f2fs_cp_error(sbi))) {
319
+ if (is_sbi_flag_set(sbi, SBI_IS_CLOSE)) {
320
+ ClearPageUptodate(page);
321
+ dec_page_count(sbi, F2FS_DIRTY_META);
322
+ unlock_page(page);
323
+ return 0;
324
+ }
305325 goto redirty_out;
326
+ }
306327 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
307328 goto redirty_out;
308329 if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0))
....@@ -348,13 +369,13 @@
348369 goto skip_write;
349370
350371 /* if locked failed, cp will flush dirty pages instead */
351
- if (!mutex_trylock(&sbi->cp_mutex))
372
+ if (!f2fs_down_write_trylock(&sbi->cp_global_sem))
352373 goto skip_write;
353374
354375 trace_f2fs_writepages(mapping->host, wbc, META);
355376 diff = nr_pages_to_write(sbi, META, wbc);
356377 written = f2fs_sync_meta_pages(sbi, META, wbc->nr_to_write, FS_META_IO);
357
- mutex_unlock(&sbi->cp_mutex);
378
+ f2fs_up_write(&sbi->cp_global_sem);
358379 wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff);
359380 return 0;
360381
....@@ -442,8 +463,7 @@
442463 if (!PageDirty(page)) {
443464 __set_page_dirty_nobuffers(page);
444465 inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_META);
445
- f2fs_set_page_private(page, 0);
446
- f2fs_trace_pid(page);
466
+ set_page_private_reference(page);
447467 return 1;
448468 }
449469 return 0;
....@@ -525,7 +545,7 @@
525545 __remove_ino_entry(sbi, ino, type);
526546 }
527547
528
-/* mode should be APPEND_INO or UPDATE_INO */
548
+/* mode should be APPEND_INO, UPDATE_INO or TRANS_DIR_INO */
529549 bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode)
530550 {
531551 struct inode_management *im = &sbi->im[mode];
....@@ -649,7 +669,7 @@
649669 /* truncate all the data during iput */
650670 iput(inode);
651671
652
- err = f2fs_get_node_info(sbi, ino, &ni);
672
+ err = f2fs_get_node_info(sbi, ino, &ni, false);
653673 if (err)
654674 goto err_out;
655675
....@@ -718,6 +738,7 @@
718738 orphan_blk = (struct f2fs_orphan_block *)page_address(page);
719739 for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
720740 nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
741
+
721742 err = recover_orphan_inode(sbi, ino);
722743 if (err) {
723744 f2fs_put_page(page, 1);
....@@ -851,6 +872,7 @@
851872 struct page *cp_page_1 = NULL, *cp_page_2 = NULL;
852873 struct f2fs_checkpoint *cp_block = NULL;
853874 unsigned long long cur_version = 0, pre_version = 0;
875
+ unsigned int cp_blocks;
854876 int err;
855877
856878 err = get_checkpoint_version(sbi, cp_addr, &cp_block,
....@@ -858,15 +880,16 @@
858880 if (err)
859881 return NULL;
860882
861
- if (le32_to_cpu(cp_block->cp_pack_total_block_count) >
862
- sbi->blocks_per_seg) {
883
+ cp_blocks = le32_to_cpu(cp_block->cp_pack_total_block_count);
884
+
885
+ if (cp_blocks > sbi->blocks_per_seg || cp_blocks <= F2FS_CP_PACKS) {
863886 f2fs_warn(sbi, "invalid cp_pack_total_block_count:%u",
864887 le32_to_cpu(cp_block->cp_pack_total_block_count));
865888 goto invalid_cp;
866889 }
867890 pre_version = *version;
868891
869
- cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1;
892
+ cp_addr += cp_blocks - 1;
870893 err = get_checkpoint_version(sbi, cp_addr, &cp_block,
871894 &cp_page_2, version);
872895 if (err)
....@@ -1016,8 +1039,7 @@
10161039 inode_inc_dirty_pages(inode);
10171040 spin_unlock(&sbi->inode_lock[type]);
10181041
1019
- f2fs_set_page_private(page, 0);
1020
- f2fs_trace_pid(page);
1042
+ set_page_private_reference(page);
10211043 }
10221044
10231045 void f2fs_remove_dirty_inode(struct inode *inode)
....@@ -1037,7 +1059,8 @@
10371059 spin_unlock(&sbi->inode_lock[type]);
10381060 }
10391061
1040
-int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type)
1062
+int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type,
1063
+ bool from_cp)
10411064 {
10421065 struct list_head *head;
10431066 struct inode *inode;
....@@ -1072,11 +1095,15 @@
10721095 if (inode) {
10731096 unsigned long cur_ino = inode->i_ino;
10741097
1075
- F2FS_I(inode)->cp_task = current;
1098
+ if (from_cp)
1099
+ F2FS_I(inode)->cp_task = current;
1100
+ F2FS_I(inode)->wb_task = current;
10761101
10771102 filemap_fdatawrite(inode->i_mapping);
10781103
1079
- F2FS_I(inode)->cp_task = NULL;
1104
+ F2FS_I(inode)->wb_task = NULL;
1105
+ if (from_cp)
1106
+ F2FS_I(inode)->cp_task = NULL;
10801107
10811108 iput(inode);
10821109 /* We need to give cpu to another writers. */
....@@ -1147,7 +1174,8 @@
11471174 if (!is_journalled_quota(sbi))
11481175 return false;
11491176
1150
- down_write(&sbi->quota_sem);
1177
+ if (!f2fs_down_write_trylock(&sbi->quota_sem))
1178
+ return true;
11511179 if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH)) {
11521180 ret = false;
11531181 } else if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR)) {
....@@ -1158,7 +1186,7 @@
11581186 } else if (get_pages(sbi, F2FS_DIRTY_QDATA)) {
11591187 ret = true;
11601188 }
1161
- up_write(&sbi->quota_sem);
1189
+ f2fs_up_write(&sbi->quota_sem);
11621190 return ret;
11631191 }
11641192
....@@ -1204,7 +1232,7 @@
12041232 /* write all the dirty dentry pages */
12051233 if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
12061234 f2fs_unlock_all(sbi);
1207
- err = f2fs_sync_dirty_inodes(sbi, DIR_INODE);
1235
+ err = f2fs_sync_dirty_inodes(sbi, DIR_INODE, true);
12081236 if (err)
12091237 return err;
12101238 cond_resched();
....@@ -1215,10 +1243,10 @@
12151243 * POR: we should ensure that there are no dirty node pages
12161244 * until finishing nat/sit flush. inode->i_blocks can be updated.
12171245 */
1218
- down_write(&sbi->node_change);
1246
+ f2fs_down_write(&sbi->node_change);
12191247
12201248 if (get_pages(sbi, F2FS_DIRTY_IMETA)) {
1221
- up_write(&sbi->node_change);
1249
+ f2fs_up_write(&sbi->node_change);
12221250 f2fs_unlock_all(sbi);
12231251 err = f2fs_sync_inode_meta(sbi);
12241252 if (err)
....@@ -1228,15 +1256,15 @@
12281256 }
12291257
12301258 retry_flush_nodes:
1231
- down_write(&sbi->node_write);
1259
+ f2fs_down_write(&sbi->node_write);
12321260
12331261 if (get_pages(sbi, F2FS_DIRTY_NODES)) {
1234
- up_write(&sbi->node_write);
1262
+ f2fs_up_write(&sbi->node_write);
12351263 atomic_inc(&sbi->wb_sync_req[NODE]);
12361264 err = f2fs_sync_node_pages(sbi, &wbc, false, FS_CP_NODE_IO);
12371265 atomic_dec(&sbi->wb_sync_req[NODE]);
12381266 if (err) {
1239
- up_write(&sbi->node_change);
1267
+ f2fs_up_write(&sbi->node_change);
12401268 f2fs_unlock_all(sbi);
12411269 return err;
12421270 }
....@@ -1249,13 +1277,13 @@
12491277 * dirty node blocks and some checkpoint values by block allocation.
12501278 */
12511279 __prepare_cp_block(sbi);
1252
- up_write(&sbi->node_change);
1280
+ f2fs_up_write(&sbi->node_change);
12531281 return err;
12541282 }
12551283
12561284 static void unblock_operations(struct f2fs_sb_info *sbi)
12571285 {
1258
- up_write(&sbi->node_write);
1286
+ f2fs_up_write(&sbi->node_write);
12591287 f2fs_unlock_all(sbi);
12601288 }
12611289
....@@ -1264,17 +1292,20 @@
12641292 DEFINE_WAIT(wait);
12651293
12661294 for (;;) {
1267
- prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
1268
-
12691295 if (!get_pages(sbi, type))
12701296 break;
12711297
1272
- if (unlikely(f2fs_cp_error(sbi)))
1298
+ if (unlikely(f2fs_cp_error(sbi) &&
1299
+ !is_sbi_flag_set(sbi, SBI_IS_CLOSE)))
12731300 break;
12741301
12751302 if (type == F2FS_DIRTY_META)
12761303 f2fs_sync_meta_pages(sbi, META, LONG_MAX,
12771304 FS_CP_META_IO);
1305
+ else if (type == F2FS_WB_CP_DATA)
1306
+ f2fs_submit_merged_write(sbi, DATA);
1307
+
1308
+ prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
12781309 io_schedule_timeout(DEFAULT_IO_TIMEOUT);
12791310 }
12801311 finish_wait(&sbi->cp_wait, &wait);
....@@ -1383,6 +1414,26 @@
13831414 f2fs_submit_merged_write(sbi, META_FLUSH);
13841415 }
13851416
1417
+static inline u64 get_sectors_written(struct block_device *bdev)
1418
+{
1419
+ return (u64)part_stat_read(bdev->bd_part, sectors[STAT_WRITE]);
1420
+}
1421
+
1422
+u64 f2fs_get_sectors_written(struct f2fs_sb_info *sbi)
1423
+{
1424
+ if (f2fs_is_multi_device(sbi)) {
1425
+ u64 sectors = 0;
1426
+ int i;
1427
+
1428
+ for (i = 0; i < sbi->s_ndevs; i++)
1429
+ sectors += get_sectors_written(FDEV(i).bdev);
1430
+
1431
+ return sectors;
1432
+ }
1433
+
1434
+ return get_sectors_written(sbi->sb->s_bdev);
1435
+}
1436
+
13861437 static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
13871438 {
13881439 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
....@@ -1393,7 +1444,6 @@
13931444 __u32 crc32 = 0;
13941445 int i;
13951446 int cp_payload_blks = __cp_payload(sbi);
1396
- struct super_block *sb = sbi->sb;
13971447 struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
13981448 u64 kbytes_written;
13991449 int err;
....@@ -1421,7 +1471,7 @@
14211471 curseg_alloc_type(sbi, i + CURSEG_HOT_DATA);
14221472 }
14231473
1424
- /* 2 cp + n data seg summary + orphan inode blocks */
1474
+ /* 2 cp + n data seg summary + orphan inode blocks */
14251475 data_sum_blocks = f2fs_npages_for_summary_flush(sbi, false);
14261476 spin_lock_irqsave(&sbi->cp_lock, flags);
14271477 if (data_sum_blocks < NR_CURSEG_DATA_TYPE)
....@@ -1435,7 +1485,7 @@
14351485 orphan_blocks);
14361486
14371487 if (__remain_node_summaries(cpc->reason))
1438
- ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS+
1488
+ ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
14391489 cp_payload_blks + data_sum_blocks +
14401490 orphan_blocks + NR_CURSEG_NODE_TYPE);
14411491 else
....@@ -1488,9 +1538,8 @@
14881538
14891539 /* Record write statistics in the hot node summary */
14901540 kbytes_written = sbi->kbytes_written;
1491
- if (sb->s_bdev->bd_part)
1492
- kbytes_written += BD_PART_WRITTEN(sbi);
1493
-
1541
+ kbytes_written += (f2fs_get_sectors_written(sbi) -
1542
+ sbi->sectors_written_start) >> 1;
14941543 seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written);
14951544
14961545 if (__remain_node_summaries(cpc->reason)) {
....@@ -1521,9 +1570,10 @@
15211570
15221571 /*
15231572 * invalidate intermediate page cache borrowed from meta inode which are
1524
- * used for migration of encrypted or verity inode's blocks.
1573
+ * used for migration of encrypted, verity or compressed inode's blocks.
15251574 */
1526
- if (f2fs_sb_has_encrypt(sbi) || f2fs_sb_has_verity(sbi))
1575
+ if (f2fs_sb_has_encrypt(sbi) || f2fs_sb_has_verity(sbi) ||
1576
+ f2fs_sb_has_compression(sbi))
15271577 invalidate_mapping_pages(META_MAPPING(sbi),
15281578 MAIN_BLKADDR(sbi), MAX_BLKADDR(sbi) - 1);
15291579
....@@ -1569,7 +1619,7 @@
15691619 f2fs_warn(sbi, "Start checkpoint disabled!");
15701620 }
15711621 if (cpc->reason != CP_RESIZE)
1572
- mutex_lock(&sbi->cp_mutex);
1622
+ f2fs_down_write(&sbi->cp_global_sem);
15731623
15741624 if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) &&
15751625 ((cpc->reason & CP_FASTBOOT) || (cpc->reason & CP_SYNC) ||
....@@ -1597,7 +1647,7 @@
15971647 goto out;
15981648 }
15991649
1600
- if (NM_I(sbi)->dirty_nat_cnt == 0 &&
1650
+ if (NM_I(sbi)->nat_cnt[DIRTY_NAT] == 0 &&
16011651 SIT_I(sbi)->dirty_sentries == 0 &&
16021652 prefree_segments(sbi) == 0) {
16031653 f2fs_flush_sit_entries(sbi, cpc);
....@@ -1622,11 +1672,16 @@
16221672
16231673 f2fs_flush_sit_entries(sbi, cpc);
16241674
1675
+ /* save inmem log status */
1676
+ f2fs_save_inmem_curseg(sbi);
1677
+
16251678 err = do_checkpoint(sbi, cpc);
16261679 if (err)
16271680 f2fs_release_discard_addrs(sbi);
16281681 else
16291682 f2fs_clear_prefree_segments(sbi, cpc);
1683
+
1684
+ f2fs_restore_inmem_curseg(sbi);
16301685 stop:
16311686 unblock_operations(sbi);
16321687 stat_inc_cp_count(sbi->stat_info);
....@@ -1639,7 +1694,7 @@
16391694 trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint");
16401695 out:
16411696 if (cpc->reason != CP_RESIZE)
1642
- mutex_unlock(&sbi->cp_mutex);
1697
+ f2fs_up_write(&sbi->cp_global_sem);
16431698 return err;
16441699 }
16451700
....@@ -1657,7 +1712,7 @@
16571712 }
16581713
16591714 sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS -
1660
- NR_CURSEG_TYPE - __cp_payload(sbi)) *
1715
+ NR_CURSEG_PERSIST_TYPE - __cp_payload(sbi)) *
16611716 F2FS_ORPHANS_PER_BLOCK;
16621717 }
16631718
....@@ -1681,3 +1736,190 @@
16811736 kmem_cache_destroy(ino_entry_slab);
16821737 kmem_cache_destroy(f2fs_inode_entry_slab);
16831738 }
1739
+
1740
+static int __write_checkpoint_sync(struct f2fs_sb_info *sbi)
1741
+{
1742
+ struct cp_control cpc = { .reason = CP_SYNC, };
1743
+ int err;
1744
+
1745
+ f2fs_down_write(&sbi->gc_lock);
1746
+ err = f2fs_write_checkpoint(sbi, &cpc);
1747
+ f2fs_up_write(&sbi->gc_lock);
1748
+
1749
+ return err;
1750
+}
1751
+
1752
+static void __checkpoint_and_complete_reqs(struct f2fs_sb_info *sbi)
1753
+{
1754
+ struct ckpt_req_control *cprc = &sbi->cprc_info;
1755
+ struct ckpt_req *req, *next;
1756
+ struct llist_node *dispatch_list;
1757
+ u64 sum_diff = 0, diff, count = 0;
1758
+ int ret;
1759
+
1760
+ dispatch_list = llist_del_all(&cprc->issue_list);
1761
+ if (!dispatch_list)
1762
+ return;
1763
+ dispatch_list = llist_reverse_order(dispatch_list);
1764
+
1765
+ ret = __write_checkpoint_sync(sbi);
1766
+ atomic_inc(&cprc->issued_ckpt);
1767
+
1768
+ llist_for_each_entry_safe(req, next, dispatch_list, llnode) {
1769
+ diff = (u64)ktime_ms_delta(ktime_get(), req->queue_time);
1770
+ req->ret = ret;
1771
+ complete(&req->wait);
1772
+
1773
+ sum_diff += diff;
1774
+ count++;
1775
+ }
1776
+ atomic_sub(count, &cprc->queued_ckpt);
1777
+ atomic_add(count, &cprc->total_ckpt);
1778
+
1779
+ spin_lock(&cprc->stat_lock);
1780
+ cprc->cur_time = (unsigned int)div64_u64(sum_diff, count);
1781
+ if (cprc->peak_time < cprc->cur_time)
1782
+ cprc->peak_time = cprc->cur_time;
1783
+ spin_unlock(&cprc->stat_lock);
1784
+}
1785
+
1786
+static int issue_checkpoint_thread(void *data)
1787
+{
1788
+ struct f2fs_sb_info *sbi = data;
1789
+ struct ckpt_req_control *cprc = &sbi->cprc_info;
1790
+ wait_queue_head_t *q = &cprc->ckpt_wait_queue;
1791
+repeat:
1792
+ if (kthread_should_stop())
1793
+ return 0;
1794
+
1795
+ if (!llist_empty(&cprc->issue_list))
1796
+ __checkpoint_and_complete_reqs(sbi);
1797
+
1798
+ wait_event_interruptible(*q,
1799
+ kthread_should_stop() || !llist_empty(&cprc->issue_list));
1800
+ goto repeat;
1801
+}
1802
+
1803
+static void flush_remained_ckpt_reqs(struct f2fs_sb_info *sbi,
1804
+ struct ckpt_req *wait_req)
1805
+{
1806
+ struct ckpt_req_control *cprc = &sbi->cprc_info;
1807
+
1808
+ if (!llist_empty(&cprc->issue_list)) {
1809
+ __checkpoint_and_complete_reqs(sbi);
1810
+ } else {
1811
+ /* already dispatched by issue_checkpoint_thread */
1812
+ if (wait_req)
1813
+ wait_for_completion(&wait_req->wait);
1814
+ }
1815
+}
1816
+
1817
+static void init_ckpt_req(struct ckpt_req *req)
1818
+{
1819
+ memset(req, 0, sizeof(struct ckpt_req));
1820
+
1821
+ init_completion(&req->wait);
1822
+ req->queue_time = ktime_get();
1823
+}
1824
+
1825
+int f2fs_issue_checkpoint(struct f2fs_sb_info *sbi)
1826
+{
1827
+ struct ckpt_req_control *cprc = &sbi->cprc_info;
1828
+ struct ckpt_req req;
1829
+ struct cp_control cpc;
1830
+
1831
+ cpc.reason = __get_cp_reason(sbi);
1832
+ if (!test_opt(sbi, MERGE_CHECKPOINT) || cpc.reason != CP_SYNC) {
1833
+ int ret;
1834
+
1835
+ f2fs_down_write(&sbi->gc_lock);
1836
+ ret = f2fs_write_checkpoint(sbi, &cpc);
1837
+ f2fs_up_write(&sbi->gc_lock);
1838
+
1839
+ return ret;
1840
+ }
1841
+
1842
+ if (!cprc->f2fs_issue_ckpt)
1843
+ return __write_checkpoint_sync(sbi);
1844
+
1845
+ init_ckpt_req(&req);
1846
+
1847
+ llist_add(&req.llnode, &cprc->issue_list);
1848
+ atomic_inc(&cprc->queued_ckpt);
1849
+
1850
+ /*
1851
+ * update issue_list before we wake up issue_checkpoint thread,
1852
+ * this smp_mb() pairs with another barrier in ___wait_event(),
1853
+ * see more details in comments of waitqueue_active().
1854
+ */
1855
+ smp_mb();
1856
+
1857
+ if (waitqueue_active(&cprc->ckpt_wait_queue))
1858
+ wake_up(&cprc->ckpt_wait_queue);
1859
+
1860
+ if (cprc->f2fs_issue_ckpt)
1861
+ wait_for_completion(&req.wait);
1862
+ else
1863
+ flush_remained_ckpt_reqs(sbi, &req);
1864
+
1865
+ return req.ret;
1866
+}
1867
+
1868
+int f2fs_start_ckpt_thread(struct f2fs_sb_info *sbi)
1869
+{
1870
+ dev_t dev = sbi->sb->s_bdev->bd_dev;
1871
+ struct ckpt_req_control *cprc = &sbi->cprc_info;
1872
+
1873
+ if (cprc->f2fs_issue_ckpt)
1874
+ return 0;
1875
+
1876
+ cprc->f2fs_issue_ckpt = kthread_run(issue_checkpoint_thread, sbi,
1877
+ "f2fs_ckpt-%u:%u", MAJOR(dev), MINOR(dev));
1878
+ if (IS_ERR(cprc->f2fs_issue_ckpt)) {
1879
+ cprc->f2fs_issue_ckpt = NULL;
1880
+ return -ENOMEM;
1881
+ }
1882
+
1883
+ set_task_ioprio(cprc->f2fs_issue_ckpt, cprc->ckpt_thread_ioprio);
1884
+
1885
+ return 0;
1886
+}
1887
+
1888
+void f2fs_stop_ckpt_thread(struct f2fs_sb_info *sbi)
1889
+{
1890
+ struct ckpt_req_control *cprc = &sbi->cprc_info;
1891
+ struct task_struct *ckpt_task;
1892
+
1893
+ if (!cprc->f2fs_issue_ckpt)
1894
+ return;
1895
+
1896
+ ckpt_task = cprc->f2fs_issue_ckpt;
1897
+ cprc->f2fs_issue_ckpt = NULL;
1898
+ kthread_stop(ckpt_task);
1899
+
1900
+ f2fs_flush_ckpt_thread(sbi);
1901
+}
1902
+
1903
+void f2fs_flush_ckpt_thread(struct f2fs_sb_info *sbi)
1904
+{
1905
+ struct ckpt_req_control *cprc = &sbi->cprc_info;
1906
+
1907
+ flush_remained_ckpt_reqs(sbi, NULL);
1908
+
1909
+ /* Let's wait for the previous dispatched checkpoint. */
1910
+ while (atomic_read(&cprc->queued_ckpt))
1911
+ io_schedule_timeout(DEFAULT_IO_TIMEOUT);
1912
+}
1913
+
1914
+void f2fs_init_ckpt_req_control(struct f2fs_sb_info *sbi)
1915
+{
1916
+ struct ckpt_req_control *cprc = &sbi->cprc_info;
1917
+
1918
+ atomic_set(&cprc->issued_ckpt, 0);
1919
+ atomic_set(&cprc->total_ckpt, 0);
1920
+ atomic_set(&cprc->queued_ckpt, 0);
1921
+ cprc->ckpt_thread_ioprio = DEFAULT_CHECKPOINT_IOPRIO;
1922
+ init_waitqueue_head(&cprc->ckpt_wait_queue);
1923
+ init_llist_head(&cprc->issue_list);
1924
+ spin_lock_init(&cprc->stat_lock);
1925
+}