forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-09 95099d4622f8cb224d94e314c7a8e0df60b13f87
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);
....@@ -348,13 +362,13 @@
348362 goto skip_write;
349363
350364 /* if locked failed, cp will flush dirty pages instead */
351
- if (!mutex_trylock(&sbi->cp_mutex))
365
+ if (!f2fs_down_write_trylock(&sbi->cp_global_sem))
352366 goto skip_write;
353367
354368 trace_f2fs_writepages(mapping->host, wbc, META);
355369 diff = nr_pages_to_write(sbi, META, wbc);
356370 written = f2fs_sync_meta_pages(sbi, META, wbc->nr_to_write, FS_META_IO);
357
- mutex_unlock(&sbi->cp_mutex);
371
+ f2fs_up_write(&sbi->cp_global_sem);
358372 wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff);
359373 return 0;
360374
....@@ -442,8 +456,7 @@
442456 if (!PageDirty(page)) {
443457 __set_page_dirty_nobuffers(page);
444458 inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_META);
445
- f2fs_set_page_private(page, 0);
446
- f2fs_trace_pid(page);
459
+ set_page_private_reference(page);
447460 return 1;
448461 }
449462 return 0;
....@@ -525,7 +538,7 @@
525538 __remove_ino_entry(sbi, ino, type);
526539 }
527540
528
-/* mode should be APPEND_INO or UPDATE_INO */
541
+/* mode should be APPEND_INO, UPDATE_INO or TRANS_DIR_INO */
529542 bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode)
530543 {
531544 struct inode_management *im = &sbi->im[mode];
....@@ -649,7 +662,7 @@
649662 /* truncate all the data during iput */
650663 iput(inode);
651664
652
- err = f2fs_get_node_info(sbi, ino, &ni);
665
+ err = f2fs_get_node_info(sbi, ino, &ni, false);
653666 if (err)
654667 goto err_out;
655668
....@@ -718,6 +731,7 @@
718731 orphan_blk = (struct f2fs_orphan_block *)page_address(page);
719732 for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
720733 nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
734
+
721735 err = recover_orphan_inode(sbi, ino);
722736 if (err) {
723737 f2fs_put_page(page, 1);
....@@ -851,6 +865,7 @@
851865 struct page *cp_page_1 = NULL, *cp_page_2 = NULL;
852866 struct f2fs_checkpoint *cp_block = NULL;
853867 unsigned long long cur_version = 0, pre_version = 0;
868
+ unsigned int cp_blocks;
854869 int err;
855870
856871 err = get_checkpoint_version(sbi, cp_addr, &cp_block,
....@@ -858,15 +873,16 @@
858873 if (err)
859874 return NULL;
860875
861
- if (le32_to_cpu(cp_block->cp_pack_total_block_count) >
862
- sbi->blocks_per_seg) {
876
+ cp_blocks = le32_to_cpu(cp_block->cp_pack_total_block_count);
877
+
878
+ if (cp_blocks > sbi->blocks_per_seg || cp_blocks <= F2FS_CP_PACKS) {
863879 f2fs_warn(sbi, "invalid cp_pack_total_block_count:%u",
864880 le32_to_cpu(cp_block->cp_pack_total_block_count));
865881 goto invalid_cp;
866882 }
867883 pre_version = *version;
868884
869
- cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1;
885
+ cp_addr += cp_blocks - 1;
870886 err = get_checkpoint_version(sbi, cp_addr, &cp_block,
871887 &cp_page_2, version);
872888 if (err)
....@@ -1016,8 +1032,7 @@
10161032 inode_inc_dirty_pages(inode);
10171033 spin_unlock(&sbi->inode_lock[type]);
10181034
1019
- f2fs_set_page_private(page, 0);
1020
- f2fs_trace_pid(page);
1035
+ set_page_private_reference(page);
10211036 }
10221037
10231038 void f2fs_remove_dirty_inode(struct inode *inode)
....@@ -1037,7 +1052,8 @@
10371052 spin_unlock(&sbi->inode_lock[type]);
10381053 }
10391054
1040
-int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type)
1055
+int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type,
1056
+ bool from_cp)
10411057 {
10421058 struct list_head *head;
10431059 struct inode *inode;
....@@ -1072,11 +1088,15 @@
10721088 if (inode) {
10731089 unsigned long cur_ino = inode->i_ino;
10741090
1075
- F2FS_I(inode)->cp_task = current;
1091
+ if (from_cp)
1092
+ F2FS_I(inode)->cp_task = current;
1093
+ F2FS_I(inode)->wb_task = current;
10761094
10771095 filemap_fdatawrite(inode->i_mapping);
10781096
1079
- F2FS_I(inode)->cp_task = NULL;
1097
+ F2FS_I(inode)->wb_task = NULL;
1098
+ if (from_cp)
1099
+ F2FS_I(inode)->cp_task = NULL;
10801100
10811101 iput(inode);
10821102 /* We need to give cpu to another writers. */
....@@ -1147,7 +1167,8 @@
11471167 if (!is_journalled_quota(sbi))
11481168 return false;
11491169
1150
- down_write(&sbi->quota_sem);
1170
+ if (!f2fs_down_write_trylock(&sbi->quota_sem))
1171
+ return true;
11511172 if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH)) {
11521173 ret = false;
11531174 } else if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR)) {
....@@ -1158,7 +1179,7 @@
11581179 } else if (get_pages(sbi, F2FS_DIRTY_QDATA)) {
11591180 ret = true;
11601181 }
1161
- up_write(&sbi->quota_sem);
1182
+ f2fs_up_write(&sbi->quota_sem);
11621183 return ret;
11631184 }
11641185
....@@ -1204,7 +1225,7 @@
12041225 /* write all the dirty dentry pages */
12051226 if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
12061227 f2fs_unlock_all(sbi);
1207
- err = f2fs_sync_dirty_inodes(sbi, DIR_INODE);
1228
+ err = f2fs_sync_dirty_inodes(sbi, DIR_INODE, true);
12081229 if (err)
12091230 return err;
12101231 cond_resched();
....@@ -1215,10 +1236,10 @@
12151236 * POR: we should ensure that there are no dirty node pages
12161237 * until finishing nat/sit flush. inode->i_blocks can be updated.
12171238 */
1218
- down_write(&sbi->node_change);
1239
+ f2fs_down_write(&sbi->node_change);
12191240
12201241 if (get_pages(sbi, F2FS_DIRTY_IMETA)) {
1221
- up_write(&sbi->node_change);
1242
+ f2fs_up_write(&sbi->node_change);
12221243 f2fs_unlock_all(sbi);
12231244 err = f2fs_sync_inode_meta(sbi);
12241245 if (err)
....@@ -1228,15 +1249,15 @@
12281249 }
12291250
12301251 retry_flush_nodes:
1231
- down_write(&sbi->node_write);
1252
+ f2fs_down_write(&sbi->node_write);
12321253
12331254 if (get_pages(sbi, F2FS_DIRTY_NODES)) {
1234
- up_write(&sbi->node_write);
1255
+ f2fs_up_write(&sbi->node_write);
12351256 atomic_inc(&sbi->wb_sync_req[NODE]);
12361257 err = f2fs_sync_node_pages(sbi, &wbc, false, FS_CP_NODE_IO);
12371258 atomic_dec(&sbi->wb_sync_req[NODE]);
12381259 if (err) {
1239
- up_write(&sbi->node_change);
1260
+ f2fs_up_write(&sbi->node_change);
12401261 f2fs_unlock_all(sbi);
12411262 return err;
12421263 }
....@@ -1249,13 +1270,13 @@
12491270 * dirty node blocks and some checkpoint values by block allocation.
12501271 */
12511272 __prepare_cp_block(sbi);
1252
- up_write(&sbi->node_change);
1273
+ f2fs_up_write(&sbi->node_change);
12531274 return err;
12541275 }
12551276
12561277 static void unblock_operations(struct f2fs_sb_info *sbi)
12571278 {
1258
- up_write(&sbi->node_write);
1279
+ f2fs_up_write(&sbi->node_write);
12591280 f2fs_unlock_all(sbi);
12601281 }
12611282
....@@ -1264,8 +1285,6 @@
12641285 DEFINE_WAIT(wait);
12651286
12661287 for (;;) {
1267
- prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
1268
-
12691288 if (!get_pages(sbi, type))
12701289 break;
12711290
....@@ -1275,6 +1294,10 @@
12751294 if (type == F2FS_DIRTY_META)
12761295 f2fs_sync_meta_pages(sbi, META, LONG_MAX,
12771296 FS_CP_META_IO);
1297
+ else if (type == F2FS_WB_CP_DATA)
1298
+ f2fs_submit_merged_write(sbi, DATA);
1299
+
1300
+ prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
12781301 io_schedule_timeout(DEFAULT_IO_TIMEOUT);
12791302 }
12801303 finish_wait(&sbi->cp_wait, &wait);
....@@ -1383,6 +1406,26 @@
13831406 f2fs_submit_merged_write(sbi, META_FLUSH);
13841407 }
13851408
1409
+static inline u64 get_sectors_written(struct block_device *bdev)
1410
+{
1411
+ return (u64)part_stat_read(bdev->bd_part, sectors[STAT_WRITE]);
1412
+}
1413
+
1414
+u64 f2fs_get_sectors_written(struct f2fs_sb_info *sbi)
1415
+{
1416
+ if (f2fs_is_multi_device(sbi)) {
1417
+ u64 sectors = 0;
1418
+ int i;
1419
+
1420
+ for (i = 0; i < sbi->s_ndevs; i++)
1421
+ sectors += get_sectors_written(FDEV(i).bdev);
1422
+
1423
+ return sectors;
1424
+ }
1425
+
1426
+ return get_sectors_written(sbi->sb->s_bdev);
1427
+}
1428
+
13861429 static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
13871430 {
13881431 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
....@@ -1393,7 +1436,6 @@
13931436 __u32 crc32 = 0;
13941437 int i;
13951438 int cp_payload_blks = __cp_payload(sbi);
1396
- struct super_block *sb = sbi->sb;
13971439 struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
13981440 u64 kbytes_written;
13991441 int err;
....@@ -1421,7 +1463,7 @@
14211463 curseg_alloc_type(sbi, i + CURSEG_HOT_DATA);
14221464 }
14231465
1424
- /* 2 cp + n data seg summary + orphan inode blocks */
1466
+ /* 2 cp + n data seg summary + orphan inode blocks */
14251467 data_sum_blocks = f2fs_npages_for_summary_flush(sbi, false);
14261468 spin_lock_irqsave(&sbi->cp_lock, flags);
14271469 if (data_sum_blocks < NR_CURSEG_DATA_TYPE)
....@@ -1435,7 +1477,7 @@
14351477 orphan_blocks);
14361478
14371479 if (__remain_node_summaries(cpc->reason))
1438
- ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS+
1480
+ ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
14391481 cp_payload_blks + data_sum_blocks +
14401482 orphan_blocks + NR_CURSEG_NODE_TYPE);
14411483 else
....@@ -1488,9 +1530,8 @@
14881530
14891531 /* Record write statistics in the hot node summary */
14901532 kbytes_written = sbi->kbytes_written;
1491
- if (sb->s_bdev->bd_part)
1492
- kbytes_written += BD_PART_WRITTEN(sbi);
1493
-
1533
+ kbytes_written += (f2fs_get_sectors_written(sbi) -
1534
+ sbi->sectors_written_start) >> 1;
14941535 seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written);
14951536
14961537 if (__remain_node_summaries(cpc->reason)) {
....@@ -1521,9 +1562,10 @@
15211562
15221563 /*
15231564 * invalidate intermediate page cache borrowed from meta inode which are
1524
- * used for migration of encrypted or verity inode's blocks.
1565
+ * used for migration of encrypted, verity or compressed inode's blocks.
15251566 */
1526
- if (f2fs_sb_has_encrypt(sbi) || f2fs_sb_has_verity(sbi))
1567
+ if (f2fs_sb_has_encrypt(sbi) || f2fs_sb_has_verity(sbi) ||
1568
+ f2fs_sb_has_compression(sbi))
15271569 invalidate_mapping_pages(META_MAPPING(sbi),
15281570 MAIN_BLKADDR(sbi), MAX_BLKADDR(sbi) - 1);
15291571
....@@ -1569,7 +1611,7 @@
15691611 f2fs_warn(sbi, "Start checkpoint disabled!");
15701612 }
15711613 if (cpc->reason != CP_RESIZE)
1572
- mutex_lock(&sbi->cp_mutex);
1614
+ f2fs_down_write(&sbi->cp_global_sem);
15731615
15741616 if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) &&
15751617 ((cpc->reason & CP_FASTBOOT) || (cpc->reason & CP_SYNC) ||
....@@ -1597,7 +1639,7 @@
15971639 goto out;
15981640 }
15991641
1600
- if (NM_I(sbi)->dirty_nat_cnt == 0 &&
1642
+ if (NM_I(sbi)->nat_cnt[DIRTY_NAT] == 0 &&
16011643 SIT_I(sbi)->dirty_sentries == 0 &&
16021644 prefree_segments(sbi) == 0) {
16031645 f2fs_flush_sit_entries(sbi, cpc);
....@@ -1622,11 +1664,16 @@
16221664
16231665 f2fs_flush_sit_entries(sbi, cpc);
16241666
1667
+ /* save inmem log status */
1668
+ f2fs_save_inmem_curseg(sbi);
1669
+
16251670 err = do_checkpoint(sbi, cpc);
16261671 if (err)
16271672 f2fs_release_discard_addrs(sbi);
16281673 else
16291674 f2fs_clear_prefree_segments(sbi, cpc);
1675
+
1676
+ f2fs_restore_inmem_curseg(sbi);
16301677 stop:
16311678 unblock_operations(sbi);
16321679 stat_inc_cp_count(sbi->stat_info);
....@@ -1639,7 +1686,7 @@
16391686 trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint");
16401687 out:
16411688 if (cpc->reason != CP_RESIZE)
1642
- mutex_unlock(&sbi->cp_mutex);
1689
+ f2fs_up_write(&sbi->cp_global_sem);
16431690 return err;
16441691 }
16451692
....@@ -1657,7 +1704,7 @@
16571704 }
16581705
16591706 sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS -
1660
- NR_CURSEG_TYPE - __cp_payload(sbi)) *
1707
+ NR_CURSEG_PERSIST_TYPE - __cp_payload(sbi)) *
16611708 F2FS_ORPHANS_PER_BLOCK;
16621709 }
16631710
....@@ -1681,3 +1728,190 @@
16811728 kmem_cache_destroy(ino_entry_slab);
16821729 kmem_cache_destroy(f2fs_inode_entry_slab);
16831730 }
1731
+
1732
+static int __write_checkpoint_sync(struct f2fs_sb_info *sbi)
1733
+{
1734
+ struct cp_control cpc = { .reason = CP_SYNC, };
1735
+ int err;
1736
+
1737
+ f2fs_down_write(&sbi->gc_lock);
1738
+ err = f2fs_write_checkpoint(sbi, &cpc);
1739
+ f2fs_up_write(&sbi->gc_lock);
1740
+
1741
+ return err;
1742
+}
1743
+
1744
+static void __checkpoint_and_complete_reqs(struct f2fs_sb_info *sbi)
1745
+{
1746
+ struct ckpt_req_control *cprc = &sbi->cprc_info;
1747
+ struct ckpt_req *req, *next;
1748
+ struct llist_node *dispatch_list;
1749
+ u64 sum_diff = 0, diff, count = 0;
1750
+ int ret;
1751
+
1752
+ dispatch_list = llist_del_all(&cprc->issue_list);
1753
+ if (!dispatch_list)
1754
+ return;
1755
+ dispatch_list = llist_reverse_order(dispatch_list);
1756
+
1757
+ ret = __write_checkpoint_sync(sbi);
1758
+ atomic_inc(&cprc->issued_ckpt);
1759
+
1760
+ llist_for_each_entry_safe(req, next, dispatch_list, llnode) {
1761
+ diff = (u64)ktime_ms_delta(ktime_get(), req->queue_time);
1762
+ req->ret = ret;
1763
+ complete(&req->wait);
1764
+
1765
+ sum_diff += diff;
1766
+ count++;
1767
+ }
1768
+ atomic_sub(count, &cprc->queued_ckpt);
1769
+ atomic_add(count, &cprc->total_ckpt);
1770
+
1771
+ spin_lock(&cprc->stat_lock);
1772
+ cprc->cur_time = (unsigned int)div64_u64(sum_diff, count);
1773
+ if (cprc->peak_time < cprc->cur_time)
1774
+ cprc->peak_time = cprc->cur_time;
1775
+ spin_unlock(&cprc->stat_lock);
1776
+}
1777
+
1778
+static int issue_checkpoint_thread(void *data)
1779
+{
1780
+ struct f2fs_sb_info *sbi = data;
1781
+ struct ckpt_req_control *cprc = &sbi->cprc_info;
1782
+ wait_queue_head_t *q = &cprc->ckpt_wait_queue;
1783
+repeat:
1784
+ if (kthread_should_stop())
1785
+ return 0;
1786
+
1787
+ if (!llist_empty(&cprc->issue_list))
1788
+ __checkpoint_and_complete_reqs(sbi);
1789
+
1790
+ wait_event_interruptible(*q,
1791
+ kthread_should_stop() || !llist_empty(&cprc->issue_list));
1792
+ goto repeat;
1793
+}
1794
+
1795
+static void flush_remained_ckpt_reqs(struct f2fs_sb_info *sbi,
1796
+ struct ckpt_req *wait_req)
1797
+{
1798
+ struct ckpt_req_control *cprc = &sbi->cprc_info;
1799
+
1800
+ if (!llist_empty(&cprc->issue_list)) {
1801
+ __checkpoint_and_complete_reqs(sbi);
1802
+ } else {
1803
+ /* already dispatched by issue_checkpoint_thread */
1804
+ if (wait_req)
1805
+ wait_for_completion(&wait_req->wait);
1806
+ }
1807
+}
1808
+
1809
+static void init_ckpt_req(struct ckpt_req *req)
1810
+{
1811
+ memset(req, 0, sizeof(struct ckpt_req));
1812
+
1813
+ init_completion(&req->wait);
1814
+ req->queue_time = ktime_get();
1815
+}
1816
+
1817
+int f2fs_issue_checkpoint(struct f2fs_sb_info *sbi)
1818
+{
1819
+ struct ckpt_req_control *cprc = &sbi->cprc_info;
1820
+ struct ckpt_req req;
1821
+ struct cp_control cpc;
1822
+
1823
+ cpc.reason = __get_cp_reason(sbi);
1824
+ if (!test_opt(sbi, MERGE_CHECKPOINT) || cpc.reason != CP_SYNC) {
1825
+ int ret;
1826
+
1827
+ f2fs_down_write(&sbi->gc_lock);
1828
+ ret = f2fs_write_checkpoint(sbi, &cpc);
1829
+ f2fs_up_write(&sbi->gc_lock);
1830
+
1831
+ return ret;
1832
+ }
1833
+
1834
+ if (!cprc->f2fs_issue_ckpt)
1835
+ return __write_checkpoint_sync(sbi);
1836
+
1837
+ init_ckpt_req(&req);
1838
+
1839
+ llist_add(&req.llnode, &cprc->issue_list);
1840
+ atomic_inc(&cprc->queued_ckpt);
1841
+
1842
+ /*
1843
+ * update issue_list before we wake up issue_checkpoint thread,
1844
+ * this smp_mb() pairs with another barrier in ___wait_event(),
1845
+ * see more details in comments of waitqueue_active().
1846
+ */
1847
+ smp_mb();
1848
+
1849
+ if (waitqueue_active(&cprc->ckpt_wait_queue))
1850
+ wake_up(&cprc->ckpt_wait_queue);
1851
+
1852
+ if (cprc->f2fs_issue_ckpt)
1853
+ wait_for_completion(&req.wait);
1854
+ else
1855
+ flush_remained_ckpt_reqs(sbi, &req);
1856
+
1857
+ return req.ret;
1858
+}
1859
+
1860
+int f2fs_start_ckpt_thread(struct f2fs_sb_info *sbi)
1861
+{
1862
+ dev_t dev = sbi->sb->s_bdev->bd_dev;
1863
+ struct ckpt_req_control *cprc = &sbi->cprc_info;
1864
+
1865
+ if (cprc->f2fs_issue_ckpt)
1866
+ return 0;
1867
+
1868
+ cprc->f2fs_issue_ckpt = kthread_run(issue_checkpoint_thread, sbi,
1869
+ "f2fs_ckpt-%u:%u", MAJOR(dev), MINOR(dev));
1870
+ if (IS_ERR(cprc->f2fs_issue_ckpt)) {
1871
+ cprc->f2fs_issue_ckpt = NULL;
1872
+ return -ENOMEM;
1873
+ }
1874
+
1875
+ set_task_ioprio(cprc->f2fs_issue_ckpt, cprc->ckpt_thread_ioprio);
1876
+
1877
+ return 0;
1878
+}
1879
+
1880
+void f2fs_stop_ckpt_thread(struct f2fs_sb_info *sbi)
1881
+{
1882
+ struct ckpt_req_control *cprc = &sbi->cprc_info;
1883
+ struct task_struct *ckpt_task;
1884
+
1885
+ if (!cprc->f2fs_issue_ckpt)
1886
+ return;
1887
+
1888
+ ckpt_task = cprc->f2fs_issue_ckpt;
1889
+ cprc->f2fs_issue_ckpt = NULL;
1890
+ kthread_stop(ckpt_task);
1891
+
1892
+ f2fs_flush_ckpt_thread(sbi);
1893
+}
1894
+
1895
+void f2fs_flush_ckpt_thread(struct f2fs_sb_info *sbi)
1896
+{
1897
+ struct ckpt_req_control *cprc = &sbi->cprc_info;
1898
+
1899
+ flush_remained_ckpt_reqs(sbi, NULL);
1900
+
1901
+ /* Let's wait for the previous dispatched checkpoint. */
1902
+ while (atomic_read(&cprc->queued_ckpt))
1903
+ io_schedule_timeout(DEFAULT_IO_TIMEOUT);
1904
+}
1905
+
1906
+void f2fs_init_ckpt_req_control(struct f2fs_sb_info *sbi)
1907
+{
1908
+ struct ckpt_req_control *cprc = &sbi->cprc_info;
1909
+
1910
+ atomic_set(&cprc->issued_ckpt, 0);
1911
+ atomic_set(&cprc->total_ckpt, 0);
1912
+ atomic_set(&cprc->queued_ckpt, 0);
1913
+ cprc->ckpt_thread_ioprio = DEFAULT_CHECKPOINT_IOPRIO;
1914
+ init_waitqueue_head(&cprc->ckpt_wait_queue);
1915
+ init_llist_head(&cprc->issue_list);
1916
+ spin_lock_init(&cprc->stat_lock);
1917
+}