17c1a000dSChao Yu // SPDX-License-Identifier: GPL-2.0
20a8165d7SJaegeuk Kim /*
3127e670aSJaegeuk Kim * fs/f2fs/checkpoint.c
4127e670aSJaegeuk Kim *
5127e670aSJaegeuk Kim * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6127e670aSJaegeuk Kim * http://www.samsung.com/
7127e670aSJaegeuk Kim */
8127e670aSJaegeuk Kim #include <linux/fs.h>
9127e670aSJaegeuk Kim #include <linux/bio.h>
10127e670aSJaegeuk Kim #include <linux/mpage.h>
11127e670aSJaegeuk Kim #include <linux/writeback.h>
12127e670aSJaegeuk Kim #include <linux/blkdev.h>
13127e670aSJaegeuk Kim #include <linux/f2fs_fs.h>
14127e670aSJaegeuk Kim #include <linux/pagevec.h>
15127e670aSJaegeuk Kim #include <linux/swap.h>
16261eeb9cSDaeho Jeong #include <linux/kthread.h>
17127e670aSJaegeuk Kim
18127e670aSJaegeuk Kim #include "f2fs.h"
19127e670aSJaegeuk Kim #include "node.h"
20127e670aSJaegeuk Kim #include "segment.h"
2152118743SDaeho Jeong #include "iostat.h"
222af4bd6cSNamjae Jeon #include <trace/events/f2fs.h>
23127e670aSJaegeuk Kim
24261eeb9cSDaeho Jeong #define DEFAULT_CHECKPOINT_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
25261eeb9cSDaeho Jeong
266451e041SJaegeuk Kim static struct kmem_cache *ino_entry_slab;
274d57b86dSChao Yu struct kmem_cache *f2fs_inode_entry_slab;
28127e670aSJaegeuk Kim
f2fs_stop_checkpoint(struct f2fs_sb_info * sbi,bool end_io,unsigned char reason)29a9cfee0eSChao Yu void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io,
30a9cfee0eSChao Yu unsigned char reason)
3138f91ca8SJaegeuk Kim {
32d494500aSChao Yu f2fs_build_fault_attr(sbi, 0, 0);
33b62e71beSChao Yu if (!end_io)
34b9109b0eSJaegeuk Kim f2fs_flush_merged_writes(sbi);
35*ecf4e678SChao Yu f2fs_handle_critical_error(sbi, reason);
3638f91ca8SJaegeuk Kim }
3738f91ca8SJaegeuk Kim
380a8165d7SJaegeuk Kim /*
39127e670aSJaegeuk Kim * We guarantee no failure on the returned page.
40127e670aSJaegeuk Kim */
f2fs_grab_meta_page(struct f2fs_sb_info * sbi,pgoff_t index)414d57b86dSChao Yu struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
42127e670aSJaegeuk Kim {
439df27d98SGu Zheng struct address_space *mapping = META_MAPPING(sbi);
44beb78181SZhang Qilong struct page *page;
45127e670aSJaegeuk Kim repeat:
46300e129cSJaegeuk Kim page = f2fs_grab_cache_page(mapping, index, false);
47127e670aSJaegeuk Kim if (!page) {
48127e670aSJaegeuk Kim cond_resched();
49127e670aSJaegeuk Kim goto repeat;
50127e670aSJaegeuk Kim }
51bae0ee7aSChao Yu f2fs_wait_on_page_writeback(page, META, true, true);
52237c0790SJaegeuk Kim if (!PageUptodate(page))
53127e670aSJaegeuk Kim SetPageUptodate(page);
54127e670aSJaegeuk Kim return page;
55127e670aSJaegeuk Kim }
56127e670aSJaegeuk Kim
__get_meta_page(struct f2fs_sb_info * sbi,pgoff_t index,bool is_meta)572b947003SChao Yu static struct page *__get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index,
582b947003SChao Yu bool is_meta)
59127e670aSJaegeuk Kim {
609df27d98SGu Zheng struct address_space *mapping = META_MAPPING(sbi);
61127e670aSJaegeuk Kim struct page *page;
62cf04e8ebSJaegeuk Kim struct f2fs_io_info fio = {
6305ca3632SJaegeuk Kim .sbi = sbi,
64cf04e8ebSJaegeuk Kim .type = META,
6504d328deSMike Christie .op = REQ_OP_READ,
6670fd7614SChristoph Hellwig .op_flags = REQ_META | REQ_PRIO,
677a9d7548SChao Yu .old_blkaddr = index,
687a9d7548SChao Yu .new_blkaddr = index,
694375a336SJaegeuk Kim .encrypted_page = NULL,
702eae077eSChao Yu .is_por = !is_meta ? 1 : 0,
71cf04e8ebSJaegeuk Kim };
727735730dSChao Yu int err;
732b947003SChao Yu
742b947003SChao Yu if (unlikely(!is_meta))
7504d328deSMike Christie fio.op_flags &= ~REQ_META;
76127e670aSJaegeuk Kim repeat:
77300e129cSJaegeuk Kim page = f2fs_grab_cache_page(mapping, index, false);
78127e670aSJaegeuk Kim if (!page) {
79127e670aSJaegeuk Kim cond_resched();
80127e670aSJaegeuk Kim goto repeat;
81127e670aSJaegeuk Kim }
82393ff91fSJaegeuk Kim if (PageUptodate(page))
83393ff91fSJaegeuk Kim goto out;
84127e670aSJaegeuk Kim
8505ca3632SJaegeuk Kim fio.page = page;
8605ca3632SJaegeuk Kim
877735730dSChao Yu err = f2fs_submit_page_bio(&fio);
887735730dSChao Yu if (err) {
897735730dSChao Yu f2fs_put_page(page, 1);
907735730dSChao Yu return ERR_PTR(err);
9186531d6bSJaegeuk Kim }
92393ff91fSJaegeuk Kim
9334a23525SChao Yu f2fs_update_iostat(sbi, NULL, FS_META_READ_IO, F2FS_BLKSIZE);
948b83ac81SChao Yu
95393ff91fSJaegeuk Kim lock_page(page);
966bacf52fSJaegeuk Kim if (unlikely(page->mapping != mapping)) {
97afcb7ca0SJaegeuk Kim f2fs_put_page(page, 1);
98afcb7ca0SJaegeuk Kim goto repeat;
99afcb7ca0SJaegeuk Kim }
100f3f338caSChao Yu
10181114baaSChao Yu if (unlikely(!PageUptodate(page))) {
102a7b8618aSJaegeuk Kim f2fs_handle_page_eio(sbi, page->index, META);
1037735730dSChao Yu f2fs_put_page(page, 1);
1047735730dSChao Yu return ERR_PTR(-EIO);
10581114baaSChao Yu }
106393ff91fSJaegeuk Kim out:
107127e670aSJaegeuk Kim return page;
108127e670aSJaegeuk Kim }
109127e670aSJaegeuk Kim
f2fs_get_meta_page(struct f2fs_sb_info * sbi,pgoff_t index)1104d57b86dSChao Yu struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
1112b947003SChao Yu {
1122b947003SChao Yu return __get_meta_page(sbi, index, true);
1132b947003SChao Yu }
1142b947003SChao Yu
f2fs_get_meta_page_retry(struct f2fs_sb_info * sbi,pgoff_t index)11586f33603SJaegeuk Kim struct page *f2fs_get_meta_page_retry(struct f2fs_sb_info *sbi, pgoff_t index)
1167735730dSChao Yu {
1177735730dSChao Yu struct page *page;
1187735730dSChao Yu int count = 0;
1197735730dSChao Yu
1207735730dSChao Yu retry:
1217735730dSChao Yu page = __get_meta_page(sbi, index, true);
1227735730dSChao Yu if (IS_ERR(page)) {
1237735730dSChao Yu if (PTR_ERR(page) == -EIO &&
1247735730dSChao Yu ++count <= DEFAULT_RETRY_IO_COUNT)
1257735730dSChao Yu goto retry;
126a9cfee0eSChao Yu f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_META_PAGE);
1277735730dSChao Yu }
1287735730dSChao Yu return page;
1297735730dSChao Yu }
1307735730dSChao Yu
1312b947003SChao Yu /* for POR only */
f2fs_get_tmp_page(struct f2fs_sb_info * sbi,pgoff_t index)1324d57b86dSChao Yu struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index)
1332b947003SChao Yu {
1342b947003SChao Yu return __get_meta_page(sbi, index, false);
1352b947003SChao Yu }
1362b947003SChao Yu
__is_bitmap_valid(struct f2fs_sb_info * sbi,block_t blkaddr,int type)13793770ab7SChao Yu static bool __is_bitmap_valid(struct f2fs_sb_info *sbi, block_t blkaddr,
13893770ab7SChao Yu int type)
13993770ab7SChao Yu {
14093770ab7SChao Yu struct seg_entry *se;
14193770ab7SChao Yu unsigned int segno, offset;
14293770ab7SChao Yu bool exist;
14393770ab7SChao Yu
1440ef4ca04SChao Yu if (type == DATA_GENERIC)
14593770ab7SChao Yu return true;
14693770ab7SChao Yu
14793770ab7SChao Yu segno = GET_SEGNO(sbi, blkaddr);
14893770ab7SChao Yu offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
14993770ab7SChao Yu se = get_seg_entry(sbi, segno);
15093770ab7SChao Yu
15193770ab7SChao Yu exist = f2fs_test_bit(offset, se->cur_valid_map);
152bd90c5cdSJaegeuk Kim
153bd90c5cdSJaegeuk Kim /* skip data, if we already have an error in checkpoint. */
154bd90c5cdSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi)))
155bd90c5cdSJaegeuk Kim return exist;
156bd90c5cdSJaegeuk Kim
1570ef4ca04SChao Yu if (exist && type == DATA_GENERIC_ENHANCE_UPDATE) {
1580ef4ca04SChao Yu f2fs_err(sbi, "Inconsistent error blkaddr:%u, sit bitmap:%d",
1590ef4ca04SChao Yu blkaddr, exist);
1600ef4ca04SChao Yu set_sbi_flag(sbi, SBI_NEED_FSCK);
1610ef4ca04SChao Yu return exist;
1620ef4ca04SChao Yu }
1630ef4ca04SChao Yu
16493770ab7SChao Yu if (!exist && type == DATA_GENERIC_ENHANCE) {
165dcbb4c10SJoe Perches f2fs_err(sbi, "Inconsistent error blkaddr:%u, sit bitmap:%d",
166dcbb4c10SJoe Perches blkaddr, exist);
16793770ab7SChao Yu set_sbi_flag(sbi, SBI_NEED_FSCK);
168dc2f78e2SDongliang Mu dump_stack();
16993770ab7SChao Yu }
17093770ab7SChao Yu return exist;
17193770ab7SChao Yu }
17293770ab7SChao Yu
f2fs_is_valid_blkaddr(struct f2fs_sb_info * sbi,block_t blkaddr,int type)173e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
1744d57b86dSChao Yu block_t blkaddr, int type)
175662befdaSChao Yu {
176c40e15a9SYangtao Li if (time_to_inject(sbi, FAULT_BLKADDR))
17718792e64SChao Yu return false;
17818792e64SChao Yu
179662befdaSChao Yu switch (type) {
180662befdaSChao Yu case META_NAT:
18166b00c18SChao Yu break;
182662befdaSChao Yu case META_SIT:
18366b00c18SChao Yu if (unlikely(blkaddr >= SIT_BLK_CNT(sbi)))
18466b00c18SChao Yu return false;
18566b00c18SChao Yu break;
18681c1a0f1SChao Yu case META_SSA:
18766b00c18SChao Yu if (unlikely(blkaddr >= MAIN_BLKADDR(sbi) ||
18866b00c18SChao Yu blkaddr < SM_I(sbi)->ssa_blkaddr))
18966b00c18SChao Yu return false;
19066b00c18SChao Yu break;
191662befdaSChao Yu case META_CP:
19266b00c18SChao Yu if (unlikely(blkaddr >= SIT_I(sbi)->sit_base_addr ||
19366b00c18SChao Yu blkaddr < __start_cp_addr(sbi)))
19466b00c18SChao Yu return false;
19566b00c18SChao Yu break;
1964c521f49SJaegeuk Kim case META_POR:
19793770ab7SChao Yu if (unlikely(blkaddr >= MAX_BLKADDR(sbi) ||
19893770ab7SChao Yu blkaddr < MAIN_BLKADDR(sbi)))
19993770ab7SChao Yu return false;
20093770ab7SChao Yu break;
201e1da7872SChao Yu case DATA_GENERIC:
20293770ab7SChao Yu case DATA_GENERIC_ENHANCE:
20393770ab7SChao Yu case DATA_GENERIC_ENHANCE_READ:
2040ef4ca04SChao Yu case DATA_GENERIC_ENHANCE_UPDATE:
20566b00c18SChao Yu if (unlikely(blkaddr >= MAX_BLKADDR(sbi) ||
206c9b60788SChao Yu blkaddr < MAIN_BLKADDR(sbi))) {
207bd90c5cdSJaegeuk Kim
208bd90c5cdSJaegeuk Kim /* Skip to emit an error message. */
209bd90c5cdSJaegeuk Kim if (unlikely(f2fs_cp_error(sbi)))
210bd90c5cdSJaegeuk Kim return false;
211bd90c5cdSJaegeuk Kim
212dcbb4c10SJoe Perches f2fs_warn(sbi, "access invalid blkaddr:%u",
213dcbb4c10SJoe Perches blkaddr);
21493770ab7SChao Yu set_sbi_flag(sbi, SBI_NEED_FSCK);
215dc2f78e2SDongliang Mu dump_stack();
21666b00c18SChao Yu return false;
21793770ab7SChao Yu } else {
21893770ab7SChao Yu return __is_bitmap_valid(sbi, blkaddr, type);
219c9b60788SChao Yu }
22066b00c18SChao Yu break;
221e1da7872SChao Yu case META_GENERIC:
222e1da7872SChao Yu if (unlikely(blkaddr < SEG0_BLKADDR(sbi) ||
223e1da7872SChao Yu blkaddr >= MAIN_BLKADDR(sbi)))
224e1da7872SChao Yu return false;
225e1da7872SChao Yu break;
226662befdaSChao Yu default:
227662befdaSChao Yu BUG();
228662befdaSChao Yu }
22966b00c18SChao Yu
23066b00c18SChao Yu return true;
231662befdaSChao Yu }
232662befdaSChao Yu
233662befdaSChao Yu /*
2347a88ddb5SChao Yu * Readahead CP/NAT/SIT/SSA/POR pages
235662befdaSChao Yu */
f2fs_ra_meta_pages(struct f2fs_sb_info * sbi,block_t start,int nrpages,int type,bool sync)2364d57b86dSChao Yu int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
23726879fb1SChao Yu int type, bool sync)
238662befdaSChao Yu {
239662befdaSChao Yu struct page *page;
2404c521f49SJaegeuk Kim block_t blkno = start;
241662befdaSChao Yu struct f2fs_io_info fio = {
24205ca3632SJaegeuk Kim .sbi = sbi,
243662befdaSChao Yu .type = META,
24404d328deSMike Christie .op = REQ_OP_READ,
24570fd7614SChristoph Hellwig .op_flags = sync ? (REQ_META | REQ_PRIO) : REQ_RAHEAD,
2464375a336SJaegeuk Kim .encrypted_page = NULL,
2472eae077eSChao Yu .in_list = 0,
2482eae077eSChao Yu .is_por = (type == META_POR) ? 1 : 0,
249662befdaSChao Yu };
250e9f5b8b8SChao Yu struct blk_plug plug;
251ce4c638cSChao Yu int err;
252662befdaSChao Yu
2532b947003SChao Yu if (unlikely(type == META_POR))
25404d328deSMike Christie fio.op_flags &= ~REQ_META;
2552b947003SChao Yu
256e9f5b8b8SChao Yu blk_start_plug(&plug);
257662befdaSChao Yu for (; nrpages-- > 0; blkno++) {
258662befdaSChao Yu
259e1da7872SChao Yu if (!f2fs_is_valid_blkaddr(sbi, blkno, type))
26066b00c18SChao Yu goto out;
26166b00c18SChao Yu
262662befdaSChao Yu switch (type) {
263662befdaSChao Yu case META_NAT:
26466b00c18SChao Yu if (unlikely(blkno >=
26566b00c18SChao Yu NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid)))
266662befdaSChao Yu blkno = 0;
26766b00c18SChao Yu /* get nat block addr */
2687a9d7548SChao Yu fio.new_blkaddr = current_nat_addr(sbi,
269662befdaSChao Yu blkno * NAT_ENTRY_PER_BLOCK);
270662befdaSChao Yu break;
271662befdaSChao Yu case META_SIT:
2726a257471SChao Yu if (unlikely(blkno >= TOTAL_SEGS(sbi)))
2736a257471SChao Yu goto out;
274662befdaSChao Yu /* get sit block addr */
2757a9d7548SChao Yu fio.new_blkaddr = current_sit_addr(sbi,
276662befdaSChao Yu blkno * SIT_ENTRY_PER_BLOCK);
277662befdaSChao Yu break;
27881c1a0f1SChao Yu case META_SSA:
279662befdaSChao Yu case META_CP:
2804c521f49SJaegeuk Kim case META_POR:
2817a9d7548SChao Yu fio.new_blkaddr = blkno;
282662befdaSChao Yu break;
283662befdaSChao Yu default:
284662befdaSChao Yu BUG();
285662befdaSChao Yu }
286662befdaSChao Yu
287300e129cSJaegeuk Kim page = f2fs_grab_cache_page(META_MAPPING(sbi),
288300e129cSJaegeuk Kim fio.new_blkaddr, false);
289662befdaSChao Yu if (!page)
290662befdaSChao Yu continue;
291662befdaSChao Yu if (PageUptodate(page)) {
292662befdaSChao Yu f2fs_put_page(page, 1);
293662befdaSChao Yu continue;
294662befdaSChao Yu }
295662befdaSChao Yu
29605ca3632SJaegeuk Kim fio.page = page;
297ce4c638cSChao Yu err = f2fs_submit_page_bio(&fio);
298ce4c638cSChao Yu f2fs_put_page(page, err ? 1 : 0);
2998b83ac81SChao Yu
3008b83ac81SChao Yu if (!err)
30134a23525SChao Yu f2fs_update_iostat(sbi, NULL, FS_META_READ_IO,
30234a23525SChao Yu F2FS_BLKSIZE);
303662befdaSChao Yu }
304662befdaSChao Yu out:
305e9f5b8b8SChao Yu blk_finish_plug(&plug);
306662befdaSChao Yu return blkno - start;
307662befdaSChao Yu }
308662befdaSChao Yu
f2fs_ra_meta_pages_cond(struct f2fs_sb_info * sbi,pgoff_t index,unsigned int ra_blocks)309430f163bSChao Yu void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index,
310430f163bSChao Yu unsigned int ra_blocks)
311635aee1fSChao Yu {
312635aee1fSChao Yu struct page *page;
313635aee1fSChao Yu bool readahead = false;
314635aee1fSChao Yu
315430f163bSChao Yu if (ra_blocks == RECOVERY_MIN_RA_BLOCKS)
316430f163bSChao Yu return;
317430f163bSChao Yu
318635aee1fSChao Yu page = find_get_page(META_MAPPING(sbi), index);
3194da7bf5aSJaegeuk Kim if (!page || !PageUptodate(page))
320635aee1fSChao Yu readahead = true;
321635aee1fSChao Yu f2fs_put_page(page, 0);
322635aee1fSChao Yu
323635aee1fSChao Yu if (readahead)
324430f163bSChao Yu f2fs_ra_meta_pages(sbi, index, ra_blocks, META_POR, true);
325635aee1fSChao Yu }
326635aee1fSChao Yu
__f2fs_write_meta_page(struct page * page,struct writeback_control * wbc,enum iostat_type io_type)327b0af6d49SChao Yu static int __f2fs_write_meta_page(struct page *page,
328b0af6d49SChao Yu struct writeback_control *wbc,
329b0af6d49SChao Yu enum iostat_type io_type)
330127e670aSJaegeuk Kim {
3314081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_P_SB(page);
332577e3495SJaegeuk Kim
333ecda0de3SChao Yu trace_f2fs_writepage(page, META);
334ecda0de3SChao Yu
335c9b3649aSChao Yu if (unlikely(f2fs_cp_error(sbi))) {
336c9b3649aSChao Yu if (is_sbi_flag_set(sbi, SBI_IS_CLOSE)) {
337c9b3649aSChao Yu ClearPageUptodate(page);
338c9b3649aSChao Yu dec_page_count(sbi, F2FS_DIRTY_META);
339c9b3649aSChao Yu unlock_page(page);
340c9b3649aSChao Yu return 0;
341c9b3649aSChao Yu }
342af697c0fSJaegeuk Kim goto redirty_out;
343c9b3649aSChao Yu }
344caf0047eSChao Yu if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
345cfb271d4SChao Yu goto redirty_out;
346857dc4e0SJaegeuk Kim if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0))
347cfb271d4SChao Yu goto redirty_out;
348127e670aSJaegeuk Kim
3494d57b86dSChao Yu f2fs_do_write_meta_page(sbi, page, io_type);
350127e670aSJaegeuk Kim dec_page_count(sbi, F2FS_DIRTY_META);
3510c3a5797SChao Yu
3520c3a5797SChao Yu if (wbc->for_reclaim)
353bab475c5SChao Yu f2fs_submit_merged_write_cond(sbi, NULL, page, 0, META);
3540c3a5797SChao Yu
355127e670aSJaegeuk Kim unlock_page(page);
356857dc4e0SJaegeuk Kim
3570c3a5797SChao Yu if (unlikely(f2fs_cp_error(sbi)))
358b9109b0eSJaegeuk Kim f2fs_submit_merged_write(sbi, META);
3590c3a5797SChao Yu
360577e3495SJaegeuk Kim return 0;
361cfb271d4SChao Yu
362cfb271d4SChao Yu redirty_out:
36376f60268SJaegeuk Kim redirty_page_for_writepage(wbc, page);
364cfb271d4SChao Yu return AOP_WRITEPAGE_ACTIVATE;
365127e670aSJaegeuk Kim }
366127e670aSJaegeuk Kim
f2fs_write_meta_page(struct page * page,struct writeback_control * wbc)367b0af6d49SChao Yu static int f2fs_write_meta_page(struct page *page,
368b0af6d49SChao Yu struct writeback_control *wbc)
369b0af6d49SChao Yu {
370b0af6d49SChao Yu return __f2fs_write_meta_page(page, wbc, FS_META_IO);
371b0af6d49SChao Yu }
372b0af6d49SChao Yu
f2fs_write_meta_pages(struct address_space * mapping,struct writeback_control * wbc)373127e670aSJaegeuk Kim static int f2fs_write_meta_pages(struct address_space *mapping,
374127e670aSJaegeuk Kim struct writeback_control *wbc)
375127e670aSJaegeuk Kim {
3764081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
37750c8cdb3SJaegeuk Kim long diff, written;
378127e670aSJaegeuk Kim
3790771fcc7SChao Yu if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
3800771fcc7SChao Yu goto skip_write;
3810771fcc7SChao Yu
3825459aa97SJaegeuk Kim /* collect a number of dirty meta pages and write together */
383812a9597SJaegeuk Kim if (wbc->sync_mode != WB_SYNC_ALL &&
384812a9597SJaegeuk Kim get_pages(sbi, F2FS_DIRTY_META) <
385812a9597SJaegeuk Kim nr_pages_to_skip(sbi, META))
386d3baf95dSJaegeuk Kim goto skip_write;
387127e670aSJaegeuk Kim
388a29d0e0bSYunlei He /* if locked failed, cp will flush dirty pages instead */
389e4544b63STim Murray if (!f2fs_down_write_trylock(&sbi->cp_global_sem))
390a29d0e0bSYunlei He goto skip_write;
391d31c7c3fSYunlei He
392a29d0e0bSYunlei He trace_f2fs_writepages(mapping->host, wbc, META);
39350c8cdb3SJaegeuk Kim diff = nr_pages_to_write(sbi, META, wbc);
3944d57b86dSChao Yu written = f2fs_sync_meta_pages(sbi, META, wbc->nr_to_write, FS_META_IO);
395e4544b63STim Murray f2fs_up_write(&sbi->cp_global_sem);
39650c8cdb3SJaegeuk Kim wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff);
397127e670aSJaegeuk Kim return 0;
398d3baf95dSJaegeuk Kim
399d3baf95dSJaegeuk Kim skip_write:
400d3baf95dSJaegeuk Kim wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META);
401d31c7c3fSYunlei He trace_f2fs_writepages(mapping->host, wbc, META);
402d3baf95dSJaegeuk Kim return 0;
403127e670aSJaegeuk Kim }
404127e670aSJaegeuk Kim
f2fs_sync_meta_pages(struct f2fs_sb_info * sbi,enum page_type type,long nr_to_write,enum iostat_type io_type)4054d57b86dSChao Yu long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
406b0af6d49SChao Yu long nr_to_write, enum iostat_type io_type)
407127e670aSJaegeuk Kim {
4089df27d98SGu Zheng struct address_space *mapping = META_MAPPING(sbi);
409028a63a6SJan Kara pgoff_t index = 0, prev = ULONG_MAX;
410580e7a49SVishal Moola (Oracle) struct folio_batch fbatch;
411127e670aSJaegeuk Kim long nwritten = 0;
412580e7a49SVishal Moola (Oracle) int nr_folios;
413127e670aSJaegeuk Kim struct writeback_control wbc = {
414127e670aSJaegeuk Kim .for_reclaim = 0,
415127e670aSJaegeuk Kim };
416e9f5b8b8SChao Yu struct blk_plug plug;
417127e670aSJaegeuk Kim
418580e7a49SVishal Moola (Oracle) folio_batch_init(&fbatch);
419127e670aSJaegeuk Kim
420e9f5b8b8SChao Yu blk_start_plug(&plug);
421e9f5b8b8SChao Yu
422580e7a49SVishal Moola (Oracle) while ((nr_folios = filemap_get_folios_tag(mapping, &index,
423580e7a49SVishal Moola (Oracle) (pgoff_t)-1,
424580e7a49SVishal Moola (Oracle) PAGECACHE_TAG_DIRTY, &fbatch))) {
425028a63a6SJan Kara int i;
426127e670aSJaegeuk Kim
427580e7a49SVishal Moola (Oracle) for (i = 0; i < nr_folios; i++) {
428580e7a49SVishal Moola (Oracle) struct folio *folio = fbatch.folios[i];
429203681f6SJaegeuk Kim
430580e7a49SVishal Moola (Oracle) if (nr_to_write != LONG_MAX && i != 0 &&
431580e7a49SVishal Moola (Oracle) folio->index != prev +
432580e7a49SVishal Moola (Oracle) folio_nr_pages(fbatch.folios[i-1])) {
433580e7a49SVishal Moola (Oracle) folio_batch_release(&fbatch);
4346066d8cdSJaegeuk Kim goto stop;
4356066d8cdSJaegeuk Kim }
4366066d8cdSJaegeuk Kim
437580e7a49SVishal Moola (Oracle) folio_lock(folio);
438203681f6SJaegeuk Kim
439580e7a49SVishal Moola (Oracle) if (unlikely(folio->mapping != mapping)) {
440203681f6SJaegeuk Kim continue_unlock:
441580e7a49SVishal Moola (Oracle) folio_unlock(folio);
442203681f6SJaegeuk Kim continue;
443203681f6SJaegeuk Kim }
444580e7a49SVishal Moola (Oracle) if (!folio_test_dirty(folio)) {
445203681f6SJaegeuk Kim /* someone wrote it for us */
446203681f6SJaegeuk Kim goto continue_unlock;
447203681f6SJaegeuk Kim }
448203681f6SJaegeuk Kim
449580e7a49SVishal Moola (Oracle) f2fs_wait_on_page_writeback(&folio->page, META,
450580e7a49SVishal Moola (Oracle) true, true);
451fa3d2bdfSJaegeuk Kim
452580e7a49SVishal Moola (Oracle) if (!folio_clear_dirty_for_io(folio))
453203681f6SJaegeuk Kim goto continue_unlock;
454203681f6SJaegeuk Kim
455580e7a49SVishal Moola (Oracle) if (__f2fs_write_meta_page(&folio->page, &wbc,
456580e7a49SVishal Moola (Oracle) io_type)) {
457580e7a49SVishal Moola (Oracle) folio_unlock(folio);
458577e3495SJaegeuk Kim break;
459577e3495SJaegeuk Kim }
460580e7a49SVishal Moola (Oracle) nwritten += folio_nr_pages(folio);
461580e7a49SVishal Moola (Oracle) prev = folio->index;
462cfb271d4SChao Yu if (unlikely(nwritten >= nr_to_write))
463127e670aSJaegeuk Kim break;
464127e670aSJaegeuk Kim }
465580e7a49SVishal Moola (Oracle) folio_batch_release(&fbatch);
466127e670aSJaegeuk Kim cond_resched();
467127e670aSJaegeuk Kim }
4686066d8cdSJaegeuk Kim stop:
469127e670aSJaegeuk Kim if (nwritten)
470b9109b0eSJaegeuk Kim f2fs_submit_merged_write(sbi, type);
471127e670aSJaegeuk Kim
472e9f5b8b8SChao Yu blk_finish_plug(&plug);
473e9f5b8b8SChao Yu
474127e670aSJaegeuk Kim return nwritten;
475127e670aSJaegeuk Kim }
476127e670aSJaegeuk Kim
f2fs_dirty_meta_folio(struct address_space * mapping,struct folio * folio)4771d9ac659SMatthew Wilcox (Oracle) static bool f2fs_dirty_meta_folio(struct address_space *mapping,
4781d9ac659SMatthew Wilcox (Oracle) struct folio *folio)
479127e670aSJaegeuk Kim {
4801d9ac659SMatthew Wilcox (Oracle) trace_f2fs_set_page_dirty(&folio->page, META);
48126c6b887SJaegeuk Kim
4821d9ac659SMatthew Wilcox (Oracle) if (!folio_test_uptodate(folio))
4831d9ac659SMatthew Wilcox (Oracle) folio_mark_uptodate(folio);
4849b7eadd9SShuqi Zhang if (filemap_dirty_folio(mapping, folio)) {
48529c87793SMatthew Wilcox (Oracle) inc_page_count(F2FS_M_SB(mapping), F2FS_DIRTY_META);
4861d9ac659SMatthew Wilcox (Oracle) set_page_private_reference(&folio->page);
4871d9ac659SMatthew Wilcox (Oracle) return true;
488127e670aSJaegeuk Kim }
4891d9ac659SMatthew Wilcox (Oracle) return false;
490127e670aSJaegeuk Kim }
491127e670aSJaegeuk Kim
492127e670aSJaegeuk Kim const struct address_space_operations f2fs_meta_aops = {
493127e670aSJaegeuk Kim .writepage = f2fs_write_meta_page,
494127e670aSJaegeuk Kim .writepages = f2fs_write_meta_pages,
4951d9ac659SMatthew Wilcox (Oracle) .dirty_folio = f2fs_dirty_meta_folio,
49691503996SMatthew Wilcox (Oracle) .invalidate_folio = f2fs_invalidate_folio,
497c26cd045SMatthew Wilcox (Oracle) .release_folio = f2fs_release_folio,
4981d5b9bd6SMatthew Wilcox (Oracle) .migrate_folio = filemap_migrate_folio,
499127e670aSJaegeuk Kim };
500127e670aSJaegeuk Kim
__add_ino_entry(struct f2fs_sb_info * sbi,nid_t ino,unsigned int devidx,int type)50139d787beSChao Yu static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino,
50239d787beSChao Yu unsigned int devidx, int type)
503953e6cc6SJaegeuk Kim {
50467298804SChao Yu struct inode_management *im = &sbi->im[type];
5054b106518SChao Yu struct ino_entry *e = NULL, *new = NULL;
50680c54505SJaegeuk Kim
5074b106518SChao Yu if (type == FLUSH_INO) {
5084b106518SChao Yu rcu_read_lock();
5094b106518SChao Yu e = radix_tree_lookup(&im->ino_root, ino);
5104b106518SChao Yu rcu_read_unlock();
5114b106518SChao Yu }
5124b106518SChao Yu
5134b106518SChao Yu retry:
5144b106518SChao Yu if (!e)
51532410577SChao Yu new = f2fs_kmem_cache_alloc(ino_entry_slab,
51632410577SChao Yu GFP_NOFS, true, NULL);
51719526d74SChao Yu
51880c54505SJaegeuk Kim radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
519769ec6e5SJaegeuk Kim
52067298804SChao Yu spin_lock(&im->ino_lock);
52167298804SChao Yu e = radix_tree_lookup(&im->ino_root, ino);
52239efac41SJaegeuk Kim if (!e) {
5234b106518SChao Yu if (!new) {
5244b106518SChao Yu spin_unlock(&im->ino_lock);
525d09bd853SYohan Joung radix_tree_preload_end();
5264b106518SChao Yu goto retry;
5274b106518SChao Yu }
5284b106518SChao Yu e = new;
52919526d74SChao Yu if (unlikely(radix_tree_insert(&im->ino_root, ino, e)))
53019526d74SChao Yu f2fs_bug_on(sbi, 1);
53119526d74SChao Yu
53239efac41SJaegeuk Kim memset(e, 0, sizeof(struct ino_entry));
53339efac41SJaegeuk Kim e->ino = ino;
53439efac41SJaegeuk Kim
53567298804SChao Yu list_add_tail(&e->list, &im->ino_list);
5368c402946SJaegeuk Kim if (type != ORPHAN_INO)
53767298804SChao Yu im->ino_num++;
53839efac41SJaegeuk Kim }
53939d787beSChao Yu
54039d787beSChao Yu if (type == FLUSH_INO)
54139d787beSChao Yu f2fs_set_bit(devidx, (char *)&e->dirty_device);
54239d787beSChao Yu
54367298804SChao Yu spin_unlock(&im->ino_lock);
544769ec6e5SJaegeuk Kim radix_tree_preload_end();
54580c54505SJaegeuk Kim
5464b106518SChao Yu if (new && e != new)
5474b106518SChao Yu kmem_cache_free(ino_entry_slab, new);
548953e6cc6SJaegeuk Kim }
549953e6cc6SJaegeuk Kim
__remove_ino_entry(struct f2fs_sb_info * sbi,nid_t ino,int type)5506451e041SJaegeuk Kim static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
551953e6cc6SJaegeuk Kim {
55267298804SChao Yu struct inode_management *im = &sbi->im[type];
5536451e041SJaegeuk Kim struct ino_entry *e;
554953e6cc6SJaegeuk Kim
55567298804SChao Yu spin_lock(&im->ino_lock);
55667298804SChao Yu e = radix_tree_lookup(&im->ino_root, ino);
55739efac41SJaegeuk Kim if (e) {
558953e6cc6SJaegeuk Kim list_del(&e->list);
55967298804SChao Yu radix_tree_delete(&im->ino_root, ino);
56067298804SChao Yu im->ino_num--;
56167298804SChao Yu spin_unlock(&im->ino_lock);
5626451e041SJaegeuk Kim kmem_cache_free(ino_entry_slab, e);
563953e6cc6SJaegeuk Kim return;
564953e6cc6SJaegeuk Kim }
56567298804SChao Yu spin_unlock(&im->ino_lock);
566953e6cc6SJaegeuk Kim }
567953e6cc6SJaegeuk Kim
f2fs_add_ino_entry(struct f2fs_sb_info * sbi,nid_t ino,int type)5684d57b86dSChao Yu void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
569fff04f90SJaegeuk Kim {
570fff04f90SJaegeuk Kim /* add new dirty ino entry into list */
57139d787beSChao Yu __add_ino_entry(sbi, ino, 0, type);
572fff04f90SJaegeuk Kim }
573fff04f90SJaegeuk Kim
f2fs_remove_ino_entry(struct f2fs_sb_info * sbi,nid_t ino,int type)5744d57b86dSChao Yu void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
575fff04f90SJaegeuk Kim {
576fff04f90SJaegeuk Kim /* remove dirty ino entry from list */
577fff04f90SJaegeuk Kim __remove_ino_entry(sbi, ino, type);
578fff04f90SJaegeuk Kim }
579fff04f90SJaegeuk Kim
5801f07cc58SJack Qiu /* mode should be APPEND_INO, UPDATE_INO or TRANS_DIR_INO */
f2fs_exist_written_data(struct f2fs_sb_info * sbi,nid_t ino,int mode)5814d57b86dSChao Yu bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode)
582fff04f90SJaegeuk Kim {
58367298804SChao Yu struct inode_management *im = &sbi->im[mode];
584fff04f90SJaegeuk Kim struct ino_entry *e;
58567298804SChao Yu
58667298804SChao Yu spin_lock(&im->ino_lock);
58767298804SChao Yu e = radix_tree_lookup(&im->ino_root, ino);
58867298804SChao Yu spin_unlock(&im->ino_lock);
589fff04f90SJaegeuk Kim return e ? true : false;
590fff04f90SJaegeuk Kim }
591fff04f90SJaegeuk Kim
f2fs_release_ino_entry(struct f2fs_sb_info * sbi,bool all)5924d57b86dSChao Yu void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all)
593fff04f90SJaegeuk Kim {
594fff04f90SJaegeuk Kim struct ino_entry *e, *tmp;
595fff04f90SJaegeuk Kim int i;
596fff04f90SJaegeuk Kim
59739d787beSChao Yu for (i = all ? ORPHAN_INO : APPEND_INO; i < MAX_INO_ENTRY; i++) {
59867298804SChao Yu struct inode_management *im = &sbi->im[i];
59967298804SChao Yu
60067298804SChao Yu spin_lock(&im->ino_lock);
60167298804SChao Yu list_for_each_entry_safe(e, tmp, &im->ino_list, list) {
602fff04f90SJaegeuk Kim list_del(&e->list);
60367298804SChao Yu radix_tree_delete(&im->ino_root, e->ino);
604fff04f90SJaegeuk Kim kmem_cache_free(ino_entry_slab, e);
60567298804SChao Yu im->ino_num--;
606fff04f90SJaegeuk Kim }
60767298804SChao Yu spin_unlock(&im->ino_lock);
608fff04f90SJaegeuk Kim }
609fff04f90SJaegeuk Kim }
610fff04f90SJaegeuk Kim
f2fs_set_dirty_device(struct f2fs_sb_info * sbi,nid_t ino,unsigned int devidx,int type)6114d57b86dSChao Yu void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
61239d787beSChao Yu unsigned int devidx, int type)
61339d787beSChao Yu {
61439d787beSChao Yu __add_ino_entry(sbi, ino, devidx, type);
61539d787beSChao Yu }
61639d787beSChao Yu
f2fs_is_dirty_device(struct f2fs_sb_info * sbi,nid_t ino,unsigned int devidx,int type)6174d57b86dSChao Yu bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
61839d787beSChao Yu unsigned int devidx, int type)
61939d787beSChao Yu {
62039d787beSChao Yu struct inode_management *im = &sbi->im[type];
62139d787beSChao Yu struct ino_entry *e;
62239d787beSChao Yu bool is_dirty = false;
62339d787beSChao Yu
62439d787beSChao Yu spin_lock(&im->ino_lock);
62539d787beSChao Yu e = radix_tree_lookup(&im->ino_root, ino);
62639d787beSChao Yu if (e && f2fs_test_bit(devidx, (char *)&e->dirty_device))
62739d787beSChao Yu is_dirty = true;
62839d787beSChao Yu spin_unlock(&im->ino_lock);
62939d787beSChao Yu return is_dirty;
63039d787beSChao Yu }
63139d787beSChao Yu
f2fs_acquire_orphan_inode(struct f2fs_sb_info * sbi)6324d57b86dSChao Yu int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi)
633127e670aSJaegeuk Kim {
63467298804SChao Yu struct inode_management *im = &sbi->im[ORPHAN_INO];
635127e670aSJaegeuk Kim int err = 0;
636127e670aSJaegeuk Kim
63767298804SChao Yu spin_lock(&im->ino_lock);
638cb78942bSJaegeuk Kim
6391ecc0c5cSChao Yu if (time_to_inject(sbi, FAULT_ORPHAN)) {
640cb78942bSJaegeuk Kim spin_unlock(&im->ino_lock);
641cb78942bSJaegeuk Kim return -ENOSPC;
642cb78942bSJaegeuk Kim }
6437fa750a1SArnd Bergmann
64467298804SChao Yu if (unlikely(im->ino_num >= sbi->max_orphans))
645127e670aSJaegeuk Kim err = -ENOSPC;
646cbd56e7dSJaegeuk Kim else
64767298804SChao Yu im->ino_num++;
64867298804SChao Yu spin_unlock(&im->ino_lock);
6490d47c1adSGu Zheng
650127e670aSJaegeuk Kim return err;
651127e670aSJaegeuk Kim }
652127e670aSJaegeuk Kim
f2fs_release_orphan_inode(struct f2fs_sb_info * sbi)6534d57b86dSChao Yu void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi)
654cbd56e7dSJaegeuk Kim {
65567298804SChao Yu struct inode_management *im = &sbi->im[ORPHAN_INO];
65667298804SChao Yu
65767298804SChao Yu spin_lock(&im->ino_lock);
65867298804SChao Yu f2fs_bug_on(sbi, im->ino_num == 0);
65967298804SChao Yu im->ino_num--;
66067298804SChao Yu spin_unlock(&im->ino_lock);
661cbd56e7dSJaegeuk Kim }
662cbd56e7dSJaegeuk Kim
f2fs_add_orphan_inode(struct inode * inode)6634d57b86dSChao Yu void f2fs_add_orphan_inode(struct inode *inode)
664127e670aSJaegeuk Kim {
66539efac41SJaegeuk Kim /* add new orphan ino entry into list */
66639d787beSChao Yu __add_ino_entry(F2FS_I_SB(inode), inode->i_ino, 0, ORPHAN_INO);
6674d57b86dSChao Yu f2fs_update_inode_page(inode);
668127e670aSJaegeuk Kim }
669127e670aSJaegeuk Kim
f2fs_remove_orphan_inode(struct f2fs_sb_info * sbi,nid_t ino)6704d57b86dSChao Yu void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
671127e670aSJaegeuk Kim {
672953e6cc6SJaegeuk Kim /* remove orphan entry from orphan list */
6736451e041SJaegeuk Kim __remove_ino_entry(sbi, ino, ORPHAN_INO);
674127e670aSJaegeuk Kim }
675127e670aSJaegeuk Kim
recover_orphan_inode(struct f2fs_sb_info * sbi,nid_t ino)6768c14bfadSChao Yu static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
677127e670aSJaegeuk Kim {
6788c14bfadSChao Yu struct inode *inode;
6795905f9afSJaegeuk Kim struct node_info ni;
68076a45e3cSChao Yu int err;
6818c14bfadSChao Yu
6825905f9afSJaegeuk Kim inode = f2fs_iget_retry(sbi->sb, ino);
6838c14bfadSChao Yu if (IS_ERR(inode)) {
6848c14bfadSChao Yu /*
6858c14bfadSChao Yu * there should be a bug that we can't find the entry
6868c14bfadSChao Yu * to orphan inode.
6878c14bfadSChao Yu */
6888c14bfadSChao Yu f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT);
6898c14bfadSChao Yu return PTR_ERR(inode);
6908c14bfadSChao Yu }
6918c14bfadSChao Yu
69210a26878SChao Yu err = f2fs_dquot_initialize(inode);
693a515d12fSSheng Yong if (err) {
694a515d12fSSheng Yong iput(inode);
6950f9ec2a8SJaegeuk Kim goto err_out;
696a515d12fSSheng Yong }
6970f9ec2a8SJaegeuk Kim
698127e670aSJaegeuk Kim clear_nlink(inode);
699127e670aSJaegeuk Kim
700127e670aSJaegeuk Kim /* truncate all the data during iput */
701127e670aSJaegeuk Kim iput(inode);
7025905f9afSJaegeuk Kim
703a9419b63SJaegeuk Kim err = f2fs_get_node_info(sbi, ino, &ni, false);
7047735730dSChao Yu if (err)
7057735730dSChao Yu goto err_out;
7065905f9afSJaegeuk Kim
7075905f9afSJaegeuk Kim /* ENOMEM was fully retried in f2fs_evict_inode. */
7085905f9afSJaegeuk Kim if (ni.blk_addr != NULL_ADDR) {
7090f9ec2a8SJaegeuk Kim err = -EIO;
7100f9ec2a8SJaegeuk Kim goto err_out;
7115905f9afSJaegeuk Kim }
7128c14bfadSChao Yu return 0;
7130f9ec2a8SJaegeuk Kim
7140f9ec2a8SJaegeuk Kim err_out:
7150f9ec2a8SJaegeuk Kim set_sbi_flag(sbi, SBI_NEED_FSCK);
716dcbb4c10SJoe Perches f2fs_warn(sbi, "%s: orphan failed (ino=%x), run fsck to fix.",
7170f9ec2a8SJaegeuk Kim __func__, ino);
7180f9ec2a8SJaegeuk Kim return err;
719127e670aSJaegeuk Kim }
720127e670aSJaegeuk Kim
f2fs_recover_orphan_inodes(struct f2fs_sb_info * sbi)7214d57b86dSChao Yu int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
722127e670aSJaegeuk Kim {
7233c642985SWanpeng Li block_t start_blk, orphan_blocks, i, j;
7244b2414d0SChao Yu int err = 0;
725127e670aSJaegeuk Kim
726aaec2b1dSChao Yu if (!is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG))
7278c14bfadSChao Yu return 0;
728127e670aSJaegeuk Kim
72968f0453dSChao Yu if (f2fs_hw_is_readonly(sbi)) {
730dcbb4c10SJoe Perches f2fs_info(sbi, "write access unavailable, skipping orphan cleanup");
731b61af314SChao Yu return 0;
732b61af314SChao Yu }
733b61af314SChao Yu
734e1bb7d3dSChao Yu if (is_sbi_flag_set(sbi, SBI_IS_WRITABLE))
735dcbb4c10SJoe Perches f2fs_info(sbi, "orphan cleanup on readonly fs");
7364b2414d0SChao Yu
73755141486SWanpeng Li start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
7383c642985SWanpeng Li orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
739127e670aSJaegeuk Kim
7404d57b86dSChao Yu f2fs_ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true);
741662befdaSChao Yu
7423c642985SWanpeng Li for (i = 0; i < orphan_blocks; i++) {
7437735730dSChao Yu struct page *page;
744127e670aSJaegeuk Kim struct f2fs_orphan_block *orphan_blk;
745127e670aSJaegeuk Kim
7467735730dSChao Yu page = f2fs_get_meta_page(sbi, start_blk + i);
7477735730dSChao Yu if (IS_ERR(page)) {
7487735730dSChao Yu err = PTR_ERR(page);
7497735730dSChao Yu goto out;
7507735730dSChao Yu }
7517735730dSChao Yu
752127e670aSJaegeuk Kim orphan_blk = (struct f2fs_orphan_block *)page_address(page);
753127e670aSJaegeuk Kim for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
754127e670aSJaegeuk Kim nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
7555f029c04SYi Zhuang
7568c14bfadSChao Yu err = recover_orphan_inode(sbi, ino);
7578c14bfadSChao Yu if (err) {
7588c14bfadSChao Yu f2fs_put_page(page, 1);
7594b2414d0SChao Yu goto out;
7608c14bfadSChao Yu }
761127e670aSJaegeuk Kim }
762127e670aSJaegeuk Kim f2fs_put_page(page, 1);
763127e670aSJaegeuk Kim }
764127e670aSJaegeuk Kim /* clear Orphan Flag */
765aaec2b1dSChao Yu clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
7664b2414d0SChao Yu out:
7671378752bSChao Yu set_sbi_flag(sbi, SBI_IS_RECOVERED);
7681378752bSChao Yu
7694b2414d0SChao Yu return err;
770127e670aSJaegeuk Kim }
771127e670aSJaegeuk Kim
write_orphan_inodes(struct f2fs_sb_info * sbi,block_t start_blk)772127e670aSJaegeuk Kim static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
773127e670aSJaegeuk Kim {
774502c6e0bSGu Zheng struct list_head *head;
775127e670aSJaegeuk Kim struct f2fs_orphan_block *orphan_blk = NULL;
776127e670aSJaegeuk Kim unsigned int nentries = 0;
777bd936f84SChao Yu unsigned short index = 1;
7788c402946SJaegeuk Kim unsigned short orphan_blocks;
7794531929eSGu Zheng struct page *page = NULL;
7806451e041SJaegeuk Kim struct ino_entry *orphan = NULL;
78167298804SChao Yu struct inode_management *im = &sbi->im[ORPHAN_INO];
782127e670aSJaegeuk Kim
78367298804SChao Yu orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num);
7848c402946SJaegeuk Kim
785d6c67a4fSJaegeuk Kim /*
786d6c67a4fSJaegeuk Kim * we don't need to do spin_lock(&im->ino_lock) here, since all the
787d6c67a4fSJaegeuk Kim * orphan inode operations are covered under f2fs_lock_op().
788d6c67a4fSJaegeuk Kim * And, spin_lock should be avoided due to page operations below.
789d6c67a4fSJaegeuk Kim */
79067298804SChao Yu head = &im->ino_list;
791127e670aSJaegeuk Kim
792146949deSJinyoung CHOI /* loop for each orphan inode entry and write them in journal block */
793502c6e0bSGu Zheng list_for_each_entry(orphan, head, list) {
794502c6e0bSGu Zheng if (!page) {
7954d57b86dSChao Yu page = f2fs_grab_meta_page(sbi, start_blk++);
796502c6e0bSGu Zheng orphan_blk =
797502c6e0bSGu Zheng (struct f2fs_orphan_block *)page_address(page);
798502c6e0bSGu Zheng memset(orphan_blk, 0, sizeof(*orphan_blk));
799502c6e0bSGu Zheng }
800127e670aSJaegeuk Kim
80136795567SGu Zheng orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
802127e670aSJaegeuk Kim
80336795567SGu Zheng if (nentries == F2FS_ORPHANS_PER_BLOCK) {
804127e670aSJaegeuk Kim /*
805127e670aSJaegeuk Kim * an orphan block is full of 1020 entries,
806127e670aSJaegeuk Kim * then we need to flush current orphan blocks
807127e670aSJaegeuk Kim * and bring another one in memory
808127e670aSJaegeuk Kim */
809127e670aSJaegeuk Kim orphan_blk->blk_addr = cpu_to_le16(index);
810127e670aSJaegeuk Kim orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
811127e670aSJaegeuk Kim orphan_blk->entry_count = cpu_to_le32(nentries);
812127e670aSJaegeuk Kim set_page_dirty(page);
813127e670aSJaegeuk Kim f2fs_put_page(page, 1);
814127e670aSJaegeuk Kim index++;
815127e670aSJaegeuk Kim nentries = 0;
816127e670aSJaegeuk Kim page = NULL;
817127e670aSJaegeuk Kim }
818127e670aSJaegeuk Kim }
819127e670aSJaegeuk Kim
820502c6e0bSGu Zheng if (page) {
821127e670aSJaegeuk Kim orphan_blk->blk_addr = cpu_to_le16(index);
822127e670aSJaegeuk Kim orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
823127e670aSJaegeuk Kim orphan_blk->entry_count = cpu_to_le32(nentries);
824127e670aSJaegeuk Kim set_page_dirty(page);
825127e670aSJaegeuk Kim f2fs_put_page(page, 1);
826502c6e0bSGu Zheng }
827127e670aSJaegeuk Kim }
828127e670aSJaegeuk Kim
f2fs_checkpoint_chksum(struct f2fs_sb_info * sbi,struct f2fs_checkpoint * ckpt)829d7eb8f1cSChao Yu static __u32 f2fs_checkpoint_chksum(struct f2fs_sb_info *sbi,
830d7eb8f1cSChao Yu struct f2fs_checkpoint *ckpt)
831d7eb8f1cSChao Yu {
832d7eb8f1cSChao Yu unsigned int chksum_ofs = le32_to_cpu(ckpt->checksum_offset);
833d7eb8f1cSChao Yu __u32 chksum;
834d7eb8f1cSChao Yu
835d7eb8f1cSChao Yu chksum = f2fs_crc32(sbi, ckpt, chksum_ofs);
836d7eb8f1cSChao Yu if (chksum_ofs < CP_CHKSUM_OFFSET) {
837d7eb8f1cSChao Yu chksum_ofs += sizeof(chksum);
838d7eb8f1cSChao Yu chksum = f2fs_chksum(sbi, chksum, (__u8 *)ckpt + chksum_ofs,
839d7eb8f1cSChao Yu F2FS_BLKSIZE - chksum_ofs);
840d7eb8f1cSChao Yu }
841d7eb8f1cSChao Yu return chksum;
842d7eb8f1cSChao Yu }
843d7eb8f1cSChao Yu
get_checkpoint_version(struct f2fs_sb_info * sbi,block_t cp_addr,struct f2fs_checkpoint ** cp_block,struct page ** cp_page,unsigned long long * version)844fc0065adSTiezhu Yang static int get_checkpoint_version(struct f2fs_sb_info *sbi, block_t cp_addr,
845fc0065adSTiezhu Yang struct f2fs_checkpoint **cp_block, struct page **cp_page,
846fc0065adSTiezhu Yang unsigned long long *version)
847fc0065adSTiezhu Yang {
848fc0065adSTiezhu Yang size_t crc_offset = 0;
849d7eb8f1cSChao Yu __u32 crc;
850fc0065adSTiezhu Yang
8514d57b86dSChao Yu *cp_page = f2fs_get_meta_page(sbi, cp_addr);
8527735730dSChao Yu if (IS_ERR(*cp_page))
8537735730dSChao Yu return PTR_ERR(*cp_page);
8547735730dSChao Yu
855fc0065adSTiezhu Yang *cp_block = (struct f2fs_checkpoint *)page_address(*cp_page);
856fc0065adSTiezhu Yang
857fc0065adSTiezhu Yang crc_offset = le32_to_cpu((*cp_block)->checksum_offset);
858d7eb8f1cSChao Yu if (crc_offset < CP_MIN_CHKSUM_OFFSET ||
859d7eb8f1cSChao Yu crc_offset > CP_CHKSUM_OFFSET) {
860d3f07c04SChao Yu f2fs_put_page(*cp_page, 1);
861dcbb4c10SJoe Perches f2fs_warn(sbi, "invalid crc_offset: %zu", crc_offset);
862fc0065adSTiezhu Yang return -EINVAL;
863fc0065adSTiezhu Yang }
864fc0065adSTiezhu Yang
865d7eb8f1cSChao Yu crc = f2fs_checkpoint_chksum(sbi, *cp_block);
866d7eb8f1cSChao Yu if (crc != cur_cp_crc(*cp_block)) {
867d3f07c04SChao Yu f2fs_put_page(*cp_page, 1);
868dcbb4c10SJoe Perches f2fs_warn(sbi, "invalid crc value");
869fc0065adSTiezhu Yang return -EINVAL;
870fc0065adSTiezhu Yang }
871fc0065adSTiezhu Yang
872fc0065adSTiezhu Yang *version = cur_cp_version(*cp_block);
873fc0065adSTiezhu Yang return 0;
874fc0065adSTiezhu Yang }
875fc0065adSTiezhu Yang
validate_checkpoint(struct f2fs_sb_info * sbi,block_t cp_addr,unsigned long long * version)876127e670aSJaegeuk Kim static struct page *validate_checkpoint(struct f2fs_sb_info *sbi,
877127e670aSJaegeuk Kim block_t cp_addr, unsigned long long *version)
878127e670aSJaegeuk Kim {
879fc0065adSTiezhu Yang struct page *cp_page_1 = NULL, *cp_page_2 = NULL;
880fc0065adSTiezhu Yang struct f2fs_checkpoint *cp_block = NULL;
881127e670aSJaegeuk Kim unsigned long long cur_version = 0, pre_version = 0;
8825b5b4f85SChao Yu unsigned int cp_blocks;
883fc0065adSTiezhu Yang int err;
884127e670aSJaegeuk Kim
885fc0065adSTiezhu Yang err = get_checkpoint_version(sbi, cp_addr, &cp_block,
886fc0065adSTiezhu Yang &cp_page_1, version);
887fc0065adSTiezhu Yang if (err)
888d3f07c04SChao Yu return NULL;
889c9b60788SChao Yu
8905b5b4f85SChao Yu cp_blocks = le32_to_cpu(cp_block->cp_pack_total_block_count);
8915b5b4f85SChao Yu
892f0248ba6SJaegeuk Kim if (cp_blocks > BLKS_PER_SEG(sbi) || cp_blocks <= F2FS_CP_PACKS) {
893dcbb4c10SJoe Perches f2fs_warn(sbi, "invalid cp_pack_total_block_count:%u",
894c9b60788SChao Yu le32_to_cpu(cp_block->cp_pack_total_block_count));
895d3f07c04SChao Yu goto invalid_cp;
896c9b60788SChao Yu }
897fc0065adSTiezhu Yang pre_version = *version;
898127e670aSJaegeuk Kim
8995b5b4f85SChao Yu cp_addr += cp_blocks - 1;
900fc0065adSTiezhu Yang err = get_checkpoint_version(sbi, cp_addr, &cp_block,
901fc0065adSTiezhu Yang &cp_page_2, version);
902fc0065adSTiezhu Yang if (err)
903d3f07c04SChao Yu goto invalid_cp;
904fc0065adSTiezhu Yang cur_version = *version;
905127e670aSJaegeuk Kim
906127e670aSJaegeuk Kim if (cur_version == pre_version) {
907127e670aSJaegeuk Kim *version = cur_version;
908127e670aSJaegeuk Kim f2fs_put_page(cp_page_2, 1);
909127e670aSJaegeuk Kim return cp_page_1;
910127e670aSJaegeuk Kim }
911127e670aSJaegeuk Kim f2fs_put_page(cp_page_2, 1);
912d3f07c04SChao Yu invalid_cp:
913127e670aSJaegeuk Kim f2fs_put_page(cp_page_1, 1);
914127e670aSJaegeuk Kim return NULL;
915127e670aSJaegeuk Kim }
916127e670aSJaegeuk Kim
f2fs_get_valid_checkpoint(struct f2fs_sb_info * sbi)9174d57b86dSChao Yu int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi)
918127e670aSJaegeuk Kim {
919127e670aSJaegeuk Kim struct f2fs_checkpoint *cp_block;
920127e670aSJaegeuk Kim struct f2fs_super_block *fsb = sbi->raw_super;
921127e670aSJaegeuk Kim struct page *cp1, *cp2, *cur_page;
922127e670aSJaegeuk Kim unsigned long blk_size = sbi->blocksize;
923127e670aSJaegeuk Kim unsigned long long cp1_version = 0, cp2_version = 0;
924127e670aSJaegeuk Kim unsigned long long cp_start_blk_no;
92555141486SWanpeng Li unsigned int cp_blks = 1 + __cp_payload(sbi);
9261dbe4152SChangman Lee block_t cp_blk_no;
9271dbe4152SChangman Lee int i;
92810f966bbSChao Yu int err;
929127e670aSJaegeuk Kim
9300b6d4ca0SEric Biggers sbi->ckpt = f2fs_kvzalloc(sbi, array_size(blk_size, cp_blks),
931026f0507SKees Cook GFP_KERNEL);
932127e670aSJaegeuk Kim if (!sbi->ckpt)
933127e670aSJaegeuk Kim return -ENOMEM;
934127e670aSJaegeuk Kim /*
935127e670aSJaegeuk Kim * Finding out valid cp block involves read both
936127e670aSJaegeuk Kim * sets( cp pack 1 and cp pack 2)
937127e670aSJaegeuk Kim */
938127e670aSJaegeuk Kim cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr);
939127e670aSJaegeuk Kim cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
940127e670aSJaegeuk Kim
941127e670aSJaegeuk Kim /* The second checkpoint pack should start at the next segment */
942f9a4e6dfSJaegeuk Kim cp_start_blk_no += ((unsigned long long)1) <<
943f9a4e6dfSJaegeuk Kim le32_to_cpu(fsb->log_blocks_per_seg);
944127e670aSJaegeuk Kim cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
945127e670aSJaegeuk Kim
946127e670aSJaegeuk Kim if (cp1 && cp2) {
947127e670aSJaegeuk Kim if (ver_after(cp2_version, cp1_version))
948127e670aSJaegeuk Kim cur_page = cp2;
949127e670aSJaegeuk Kim else
950127e670aSJaegeuk Kim cur_page = cp1;
951127e670aSJaegeuk Kim } else if (cp1) {
952127e670aSJaegeuk Kim cur_page = cp1;
953127e670aSJaegeuk Kim } else if (cp2) {
954127e670aSJaegeuk Kim cur_page = cp2;
955127e670aSJaegeuk Kim } else {
95610f966bbSChao Yu err = -EFSCORRUPTED;
957127e670aSJaegeuk Kim goto fail_no_cp;
958127e670aSJaegeuk Kim }
959127e670aSJaegeuk Kim
960127e670aSJaegeuk Kim cp_block = (struct f2fs_checkpoint *)page_address(cur_page);
961127e670aSJaegeuk Kim memcpy(sbi->ckpt, cp_block, blk_size);
962127e670aSJaegeuk Kim
9638508e44aSJaegeuk Kim if (cur_page == cp1)
9648508e44aSJaegeuk Kim sbi->cur_cp_pack = 1;
9658508e44aSJaegeuk Kim else
9668508e44aSJaegeuk Kim sbi->cur_cp_pack = 2;
967984ec63cSShawn Lin
968e494c2f9SChao Yu /* Sanity checking of checkpoint */
96910f966bbSChao Yu if (f2fs_sanity_check_ckpt(sbi)) {
97010f966bbSChao Yu err = -EFSCORRUPTED;
971e494c2f9SChao Yu goto free_fail_no_cp;
97210f966bbSChao Yu }
973e494c2f9SChao Yu
9741dbe4152SChangman Lee if (cp_blks <= 1)
9751dbe4152SChangman Lee goto done;
9761dbe4152SChangman Lee
9771dbe4152SChangman Lee cp_blk_no = le32_to_cpu(fsb->cp_blkaddr);
9781dbe4152SChangman Lee if (cur_page == cp2)
979447286ebSYangtao Li cp_blk_no += BIT(le32_to_cpu(fsb->log_blocks_per_seg));
9801dbe4152SChangman Lee
9811dbe4152SChangman Lee for (i = 1; i < cp_blks; i++) {
9821dbe4152SChangman Lee void *sit_bitmap_ptr;
9831dbe4152SChangman Lee unsigned char *ckpt = (unsigned char *)sbi->ckpt;
9841dbe4152SChangman Lee
9854d57b86dSChao Yu cur_page = f2fs_get_meta_page(sbi, cp_blk_no + i);
98610f966bbSChao Yu if (IS_ERR(cur_page)) {
98710f966bbSChao Yu err = PTR_ERR(cur_page);
9887735730dSChao Yu goto free_fail_no_cp;
98910f966bbSChao Yu }
9901dbe4152SChangman Lee sit_bitmap_ptr = page_address(cur_page);
9911dbe4152SChangman Lee memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size);
9921dbe4152SChangman Lee f2fs_put_page(cur_page, 1);
9931dbe4152SChangman Lee }
9941dbe4152SChangman Lee done:
995127e670aSJaegeuk Kim f2fs_put_page(cp1, 1);
996127e670aSJaegeuk Kim f2fs_put_page(cp2, 1);
997127e670aSJaegeuk Kim return 0;
998127e670aSJaegeuk Kim
999a2125ff7SJaegeuk Kim free_fail_no_cp:
1000a2125ff7SJaegeuk Kim f2fs_put_page(cp1, 1);
1001a2125ff7SJaegeuk Kim f2fs_put_page(cp2, 1);
1002127e670aSJaegeuk Kim fail_no_cp:
10035222595dSJaegeuk Kim kvfree(sbi->ckpt);
100410f966bbSChao Yu return err;
1005127e670aSJaegeuk Kim }
1006127e670aSJaegeuk Kim
__add_dirty_inode(struct inode * inode,enum inode_type type)1007c227f912SChao Yu static void __add_dirty_inode(struct inode *inode, enum inode_type type)
1008127e670aSJaegeuk Kim {
10094081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1010c227f912SChao Yu int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
10112d7b822aSChao Yu
101291942321SJaegeuk Kim if (is_inode_flag_set(inode, flag))
10132710fd7eSChao Yu return;
10142d7b822aSChao Yu
101591942321SJaegeuk Kim set_inode_flag(inode, flag);
10167bc155feSJaegeuk Kim list_add_tail(&F2FS_I(inode)->dirty_list, &sbi->inode_list[type]);
101733fbd510SChao Yu stat_inc_dirty_inode(sbi, type);
10185deb8267SJaegeuk Kim }
10195deb8267SJaegeuk Kim
__remove_dirty_inode(struct inode * inode,enum inode_type type)1020c227f912SChao Yu static void __remove_dirty_inode(struct inode *inode, enum inode_type type)
10216ad7609aSChao Yu {
1022c227f912SChao Yu int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
10236ad7609aSChao Yu
102491942321SJaegeuk Kim if (get_dirty_pages(inode) || !is_inode_flag_set(inode, flag))
10256ad7609aSChao Yu return;
10266ad7609aSChao Yu
102791942321SJaegeuk Kim list_del_init(&F2FS_I(inode)->dirty_list);
102891942321SJaegeuk Kim clear_inode_flag(inode, flag);
102933fbd510SChao Yu stat_dec_dirty_inode(F2FS_I_SB(inode), type);
10306ad7609aSChao Yu }
10316ad7609aSChao Yu
f2fs_update_dirty_folio(struct inode * inode,struct folio * folio)10324f5e34f7SMatthew Wilcox (Oracle) void f2fs_update_dirty_folio(struct inode *inode, struct folio *folio)
10335deb8267SJaegeuk Kim {
10344081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1035c227f912SChao Yu enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
10365deb8267SJaegeuk Kim
10375ac9f36fSChao Yu if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
10385ac9f36fSChao Yu !S_ISLNK(inode->i_mode))
1039127e670aSJaegeuk Kim return;
10407bd59381SGu Zheng
1041c227f912SChao Yu spin_lock(&sbi->inode_lock[type]);
10421c4bf763SJaegeuk Kim if (type != FILE_INODE || test_opt(sbi, DATA_FLUSH))
1043c227f912SChao Yu __add_dirty_inode(inode, type);
1044b951a4ecSYunlei He inode_inc_dirty_pages(inode);
10451c4bf763SJaegeuk Kim spin_unlock(&sbi->inode_lock[type]);
10461c4bf763SJaegeuk Kim
10474f5e34f7SMatthew Wilcox (Oracle) set_page_private_reference(&folio->page);
10485deb8267SJaegeuk Kim }
1049127e670aSJaegeuk Kim
f2fs_remove_dirty_inode(struct inode * inode)10504d57b86dSChao Yu void f2fs_remove_dirty_inode(struct inode *inode)
1051127e670aSJaegeuk Kim {
10524081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1053c227f912SChao Yu enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
1054127e670aSJaegeuk Kim
1055c227f912SChao Yu if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
1056c227f912SChao Yu !S_ISLNK(inode->i_mode))
1057127e670aSJaegeuk Kim return;
1058127e670aSJaegeuk Kim
105910aa97c3SJaegeuk Kim if (type == FILE_INODE && !test_opt(sbi, DATA_FLUSH))
106010aa97c3SJaegeuk Kim return;
106110aa97c3SJaegeuk Kim
1062c227f912SChao Yu spin_lock(&sbi->inode_lock[type]);
1063c227f912SChao Yu __remove_dirty_inode(inode, type);
1064c227f912SChao Yu spin_unlock(&sbi->inode_lock[type]);
1065afc3eda2SJaegeuk Kim }
106674d0b917SJaegeuk Kim
f2fs_sync_dirty_inodes(struct f2fs_sb_info * sbi,enum inode_type type,bool from_cp)1067d80afefbSChao Yu int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type,
1068d80afefbSChao Yu bool from_cp)
1069127e670aSJaegeuk Kim {
1070ce3b7d80SGu Zheng struct list_head *head;
1071127e670aSJaegeuk Kim struct inode *inode;
10722710fd7eSChao Yu struct f2fs_inode_info *fi;
10734cf18537SChao Yu bool is_dir = (type == DIR_INODE);
10744db08d01SJaegeuk Kim unsigned long ino = 0;
10754cf18537SChao Yu
10764cf18537SChao Yu trace_f2fs_sync_dirty_inodes_enter(sbi->sb, is_dir,
10774cf18537SChao Yu get_pages(sbi, is_dir ?
10784cf18537SChao Yu F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
1079127e670aSJaegeuk Kim retry:
10809b664822SZhang Qilong if (unlikely(f2fs_cp_error(sbi))) {
10819b664822SZhang Qilong trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir,
10829b664822SZhang Qilong get_pages(sbi, is_dir ?
10839b664822SZhang Qilong F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
10846d5a1495SChao Yu return -EIO;
10859b664822SZhang Qilong }
1086af41d3eeSJaegeuk Kim
1087c227f912SChao Yu spin_lock(&sbi->inode_lock[type]);
1088ce3b7d80SGu Zheng
1089c227f912SChao Yu head = &sbi->inode_list[type];
1090127e670aSJaegeuk Kim if (list_empty(head)) {
1091c227f912SChao Yu spin_unlock(&sbi->inode_lock[type]);
10924cf18537SChao Yu trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir,
10934cf18537SChao Yu get_pages(sbi, is_dir ?
10944cf18537SChao Yu F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
10956d5a1495SChao Yu return 0;
1096127e670aSJaegeuk Kim }
1097939afa94SChao Yu fi = list_first_entry(head, struct f2fs_inode_info, dirty_list);
10982710fd7eSChao Yu inode = igrab(&fi->vfs_inode);
1099c227f912SChao Yu spin_unlock(&sbi->inode_lock[type]);
1100127e670aSJaegeuk Kim if (inode) {
11014db08d01SJaegeuk Kim unsigned long cur_ino = inode->i_ino;
11024db08d01SJaegeuk Kim
1103d80afefbSChao Yu if (from_cp)
1104b0af6d49SChao Yu F2FS_I(inode)->cp_task = current;
1105d80afefbSChao Yu F2FS_I(inode)->wb_task = current;
1106b0af6d49SChao Yu
110787d6f890SJaegeuk Kim filemap_fdatawrite(inode->i_mapping);
1108b0af6d49SChao Yu
1109d80afefbSChao Yu F2FS_I(inode)->wb_task = NULL;
1110d80afefbSChao Yu if (from_cp)
1111b0af6d49SChao Yu F2FS_I(inode)->cp_task = NULL;
1112b0af6d49SChao Yu
1113127e670aSJaegeuk Kim iput(inode);
11144db08d01SJaegeuk Kim /* We need to give cpu to another writers. */
11152a63531aSYunlei He if (ino == cur_ino)
11164db08d01SJaegeuk Kim cond_resched();
11172a63531aSYunlei He else
11184db08d01SJaegeuk Kim ino = cur_ino;
1119127e670aSJaegeuk Kim } else {
1120127e670aSJaegeuk Kim /*
1121127e670aSJaegeuk Kim * We should submit bio, since it exists several
1122146949deSJinyoung CHOI * writebacking dentry pages in the freeing inode.
1123127e670aSJaegeuk Kim */
1124b9109b0eSJaegeuk Kim f2fs_submit_merged_write(sbi, DATA);
11257ecebe5eSSebastian Andrzej Siewior cond_resched();
1126127e670aSJaegeuk Kim }
1127127e670aSJaegeuk Kim goto retry;
1128127e670aSJaegeuk Kim }
1129127e670aSJaegeuk Kim
f2fs_sync_inode_meta(struct f2fs_sb_info * sbi)113060630375SLi Zetao static int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi)
11310f18b462SJaegeuk Kim {
11320f18b462SJaegeuk Kim struct list_head *head = &sbi->inode_list[DIRTY_META];
11330f18b462SJaegeuk Kim struct inode *inode;
11340f18b462SJaegeuk Kim struct f2fs_inode_info *fi;
11350f18b462SJaegeuk Kim s64 total = get_pages(sbi, F2FS_DIRTY_IMETA);
11360f18b462SJaegeuk Kim
11370f18b462SJaegeuk Kim while (total--) {
11380f18b462SJaegeuk Kim if (unlikely(f2fs_cp_error(sbi)))
11390f18b462SJaegeuk Kim return -EIO;
11400f18b462SJaegeuk Kim
11410f18b462SJaegeuk Kim spin_lock(&sbi->inode_lock[DIRTY_META]);
11420f18b462SJaegeuk Kim if (list_empty(head)) {
11430f18b462SJaegeuk Kim spin_unlock(&sbi->inode_lock[DIRTY_META]);
11440f18b462SJaegeuk Kim return 0;
11450f18b462SJaegeuk Kim }
1146939afa94SChao Yu fi = list_first_entry(head, struct f2fs_inode_info,
11470f18b462SJaegeuk Kim gdirty_list);
11480f18b462SJaegeuk Kim inode = igrab(&fi->vfs_inode);
11490f18b462SJaegeuk Kim spin_unlock(&sbi->inode_lock[DIRTY_META]);
11500f18b462SJaegeuk Kim if (inode) {
115118340edcSJaegeuk Kim sync_inode_metadata(inode, 0);
115218340edcSJaegeuk Kim
115318340edcSJaegeuk Kim /* it's on eviction */
115418340edcSJaegeuk Kim if (is_inode_flag_set(inode, FI_DIRTY_INODE))
11554d57b86dSChao Yu f2fs_update_inode_page(inode);
11560f18b462SJaegeuk Kim iput(inode);
11570f18b462SJaegeuk Kim }
1158dee668c1SChao Yu }
11590f18b462SJaegeuk Kim return 0;
11600f18b462SJaegeuk Kim }
11610f18b462SJaegeuk Kim
__prepare_cp_block(struct f2fs_sb_info * sbi)116259c9081bSYunlei He static void __prepare_cp_block(struct f2fs_sb_info *sbi)
116359c9081bSYunlei He {
116459c9081bSYunlei He struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
116559c9081bSYunlei He struct f2fs_nm_info *nm_i = NM_I(sbi);
116659c9081bSYunlei He nid_t last_nid = nm_i->next_scan_nid;
116759c9081bSYunlei He
116859c9081bSYunlei He next_free_nid(sbi, &last_nid);
116959c9081bSYunlei He ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
117059c9081bSYunlei He ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi));
117159c9081bSYunlei He ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi));
117259c9081bSYunlei He ckpt->next_free_nid = cpu_to_le32(last_nid);
1173fbcd51e0SChao Yu
1174fbcd51e0SChao Yu /* update user_block_counts */
1175fbcd51e0SChao Yu sbi->last_valid_block_count = sbi->total_valid_block_count;
1176fbcd51e0SChao Yu percpu_counter_set(&sbi->alloc_valid_block_count, 0);
1177fbcd51e0SChao Yu percpu_counter_set(&sbi->rf_node_block_count, 0);
117859c9081bSYunlei He }
117959c9081bSYunlei He
__need_flush_quota(struct f2fs_sb_info * sbi)1180af033b2aSChao Yu static bool __need_flush_quota(struct f2fs_sb_info *sbi)
1181af033b2aSChao Yu {
1182db6ec53bSJaegeuk Kim bool ret = false;
1183db6ec53bSJaegeuk Kim
1184af033b2aSChao Yu if (!is_journalled_quota(sbi))
1185af033b2aSChao Yu return false;
1186db6ec53bSJaegeuk Kim
1187e4544b63STim Murray if (!f2fs_down_write_trylock(&sbi->quota_sem))
1188a5c00422SChao Yu return true;
1189db6ec53bSJaegeuk Kim if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH)) {
1190db6ec53bSJaegeuk Kim ret = false;
1191db6ec53bSJaegeuk Kim } else if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR)) {
1192db6ec53bSJaegeuk Kim ret = false;
1193db6ec53bSJaegeuk Kim } else if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_FLUSH)) {
1194db6ec53bSJaegeuk Kim clear_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH);
1195db6ec53bSJaegeuk Kim ret = true;
1196db6ec53bSJaegeuk Kim } else if (get_pages(sbi, F2FS_DIRTY_QDATA)) {
1197db6ec53bSJaegeuk Kim ret = true;
1198db6ec53bSJaegeuk Kim }
1199e4544b63STim Murray f2fs_up_write(&sbi->quota_sem);
1200db6ec53bSJaegeuk Kim return ret;
1201af033b2aSChao Yu }
1202af033b2aSChao Yu
12030a8165d7SJaegeuk Kim /*
1204127e670aSJaegeuk Kim * Freeze all the FS-operations for checkpoint.
1205127e670aSJaegeuk Kim */
block_operations(struct f2fs_sb_info * sbi)1206cf779cabSJaegeuk Kim static int block_operations(struct f2fs_sb_info *sbi)
1207127e670aSJaegeuk Kim {
1208127e670aSJaegeuk Kim struct writeback_control wbc = {
1209127e670aSJaegeuk Kim .sync_mode = WB_SYNC_ALL,
1210127e670aSJaegeuk Kim .nr_to_write = LONG_MAX,
1211127e670aSJaegeuk Kim .for_reclaim = 0,
1212127e670aSJaegeuk Kim };
1213af033b2aSChao Yu int err = 0, cnt = 0;
1214c718379bSJaegeuk Kim
121534c061adSSayali Lokhande /*
121634c061adSSayali Lokhande * Let's flush inline_data in dirty node pages.
121734c061adSSayali Lokhande */
121834c061adSSayali Lokhande f2fs_flush_inline_data(sbi);
121934c061adSSayali Lokhande
1220af033b2aSChao Yu retry_flush_quotas:
1221db6ec53bSJaegeuk Kim f2fs_lock_all(sbi);
1222af033b2aSChao Yu if (__need_flush_quota(sbi)) {
1223af033b2aSChao Yu int locked;
1224af033b2aSChao Yu
1225af033b2aSChao Yu if (++cnt > DEFAULT_RETRY_QUOTA_FLUSH_COUNT) {
1226af033b2aSChao Yu set_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH);
1227db6ec53bSJaegeuk Kim set_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH);
1228af033b2aSChao Yu goto retry_flush_dents;
1229af033b2aSChao Yu }
1230db6ec53bSJaegeuk Kim f2fs_unlock_all(sbi);
1231af033b2aSChao Yu
1232af033b2aSChao Yu /* only failed during mount/umount/freeze/quotactl */
1233af033b2aSChao Yu locked = down_read_trylock(&sbi->sb->s_umount);
1234af033b2aSChao Yu f2fs_quota_sync(sbi->sb, -1);
1235af033b2aSChao Yu if (locked)
1236af033b2aSChao Yu up_read(&sbi->sb->s_umount);
1237af033b2aSChao Yu cond_resched();
1238af033b2aSChao Yu goto retry_flush_quotas;
1239af033b2aSChao Yu }
1240af033b2aSChao Yu
1241af033b2aSChao Yu retry_flush_dents:
1242127e670aSJaegeuk Kim /* write all the dirty dentry pages */
1243127e670aSJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
1244e479556bSGu Zheng f2fs_unlock_all(sbi);
1245d80afefbSChao Yu err = f2fs_sync_dirty_inodes(sbi, DIR_INODE, true);
12466d5a1495SChao Yu if (err)
12471f5f11a3SJaegeuk Kim return err;
124830973883SJaegeuk Kim cond_resched();
1249af033b2aSChao Yu goto retry_flush_quotas;
1250127e670aSJaegeuk Kim }
1251127e670aSJaegeuk Kim
125259c9081bSYunlei He /*
125359c9081bSYunlei He * POR: we should ensure that there are no dirty node pages
125459c9081bSYunlei He * until finishing nat/sit flush. inode->i_blocks can be updated.
125559c9081bSYunlei He */
1256e4544b63STim Murray f2fs_down_write(&sbi->node_change);
125759c9081bSYunlei He
12580f18b462SJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_IMETA)) {
1259e4544b63STim Murray f2fs_up_write(&sbi->node_change);
12600f18b462SJaegeuk Kim f2fs_unlock_all(sbi);
12610f18b462SJaegeuk Kim err = f2fs_sync_inode_meta(sbi);
12620f18b462SJaegeuk Kim if (err)
12631f5f11a3SJaegeuk Kim return err;
126430973883SJaegeuk Kim cond_resched();
1265af033b2aSChao Yu goto retry_flush_quotas;
12660f18b462SJaegeuk Kim }
12670f18b462SJaegeuk Kim
126839936837SJaegeuk Kim retry_flush_nodes:
1269e4544b63STim Murray f2fs_down_write(&sbi->node_write);
1270127e670aSJaegeuk Kim
1271127e670aSJaegeuk Kim if (get_pages(sbi, F2FS_DIRTY_NODES)) {
1272e4544b63STim Murray f2fs_up_write(&sbi->node_write);
1273c29fd0c0SChao Yu atomic_inc(&sbi->wb_sync_req[NODE]);
12744d57b86dSChao Yu err = f2fs_sync_node_pages(sbi, &wbc, false, FS_CP_NODE_IO);
1275c29fd0c0SChao Yu atomic_dec(&sbi->wb_sync_req[NODE]);
12766d5a1495SChao Yu if (err) {
1277e4544b63STim Murray f2fs_up_write(&sbi->node_change);
1278cf779cabSJaegeuk Kim f2fs_unlock_all(sbi);
12791f5f11a3SJaegeuk Kim return err;
1280cf779cabSJaegeuk Kim }
128130973883SJaegeuk Kim cond_resched();
128239936837SJaegeuk Kim goto retry_flush_nodes;
1283127e670aSJaegeuk Kim }
128459c9081bSYunlei He
128559c9081bSYunlei He /*
128659c9081bSYunlei He * sbi->node_change is used only for AIO write_begin path which produces
128759c9081bSYunlei He * dirty node blocks and some checkpoint values by block allocation.
128859c9081bSYunlei He */
128959c9081bSYunlei He __prepare_cp_block(sbi);
1290e4544b63STim Murray f2fs_up_write(&sbi->node_change);
1291cf779cabSJaegeuk Kim return err;
1292127e670aSJaegeuk Kim }
1293127e670aSJaegeuk Kim
unblock_operations(struct f2fs_sb_info * sbi)1294127e670aSJaegeuk Kim static void unblock_operations(struct f2fs_sb_info *sbi)
1295127e670aSJaegeuk Kim {
1296e4544b63STim Murray f2fs_up_write(&sbi->node_write);
1297e479556bSGu Zheng f2fs_unlock_all(sbi);
1298127e670aSJaegeuk Kim }
1299127e670aSJaegeuk Kim
f2fs_wait_on_all_pages(struct f2fs_sb_info * sbi,int type)1300bf22c3ccSSahitya Tummala void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type)
1301fb51b5efSChangman Lee {
1302fb51b5efSChangman Lee DEFINE_WAIT(wait);
1303fb51b5efSChangman Lee
1304fb51b5efSChangman Lee for (;;) {
1305bf22c3ccSSahitya Tummala if (!get_pages(sbi, type))
1306fb51b5efSChangman Lee break;
1307fb51b5efSChangman Lee
1308c9b3649aSChao Yu if (unlikely(f2fs_cp_error(sbi) &&
1309c9b3649aSChao Yu !is_sbi_flag_set(sbi, SBI_IS_CLOSE)))
1310af697c0fSJaegeuk Kim break;
1311af697c0fSJaegeuk Kim
13129c30df7cSJaegeuk Kim if (type == F2FS_DIRTY_META)
13139c30df7cSJaegeuk Kim f2fs_sync_meta_pages(sbi, META, LONG_MAX,
13149c30df7cSJaegeuk Kim FS_CP_META_IO);
13151fd28018SJaegeuk Kim else if (type == F2FS_WB_CP_DATA)
13161fd28018SJaegeuk Kim f2fs_submit_merged_write(sbi, DATA);
1317828add77SJaegeuk Kim
1318828add77SJaegeuk Kim prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
13195df7731fSChao Yu io_schedule_timeout(DEFAULT_IO_TIMEOUT);
1320fb51b5efSChangman Lee }
1321fb51b5efSChangman Lee finish_wait(&sbi->cp_wait, &wait);
1322fb51b5efSChangman Lee }
1323fb51b5efSChangman Lee
update_ckpt_flags(struct f2fs_sb_info * sbi,struct cp_control * cpc)1324e4c5d848SJaegeuk Kim static void update_ckpt_flags(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1325e4c5d848SJaegeuk Kim {
1326e4c5d848SJaegeuk Kim unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num;
1327e4c5d848SJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1328d1aa2453SChao Yu unsigned long flags;
1329e4c5d848SJaegeuk Kim
133094c821fbSChao Yu if (cpc->reason & CP_UMOUNT) {
1331b702c83eSChao Yu if (le32_to_cpu(ckpt->cp_pack_total_block_count) +
1332f0248ba6SJaegeuk Kim NM_I(sbi)->nat_bits_blocks > BLKS_PER_SEG(sbi)) {
133394c821fbSChao Yu clear_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
133494c821fbSChao Yu f2fs_notice(sbi, "Disable nat_bits due to no space");
133594c821fbSChao Yu } else if (!is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG) &&
133694c821fbSChao Yu f2fs_nat_bitmap_enabled(sbi)) {
133794c821fbSChao Yu f2fs_enable_nat_bits(sbi);
133894c821fbSChao Yu set_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
133994c821fbSChao Yu f2fs_notice(sbi, "Rebuild and enable nat_bits");
134094c821fbSChao Yu }
134194c821fbSChao Yu }
1342e4c5d848SJaegeuk Kim
134394c821fbSChao Yu spin_lock_irqsave(&sbi->cp_lock, flags);
134422ad0b6aSJaegeuk Kim
13451f43e2adSChao Yu if (cpc->reason & CP_TRIMMED)
13461f43e2adSChao Yu __set_ckpt_flags(ckpt, CP_TRIMMED_FLAG);
1347cd36d7a1SChao Yu else
1348cd36d7a1SChao Yu __clear_ckpt_flags(ckpt, CP_TRIMMED_FLAG);
13491f43e2adSChao Yu
1350c473f1a9SChao Yu if (cpc->reason & CP_UMOUNT)
1351e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1352e4c5d848SJaegeuk Kim else
1353e4c5d848SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1354e4c5d848SJaegeuk Kim
1355c473f1a9SChao Yu if (cpc->reason & CP_FASTBOOT)
1356e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1357e4c5d848SJaegeuk Kim else
1358e4c5d848SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1359e4c5d848SJaegeuk Kim
1360e4c5d848SJaegeuk Kim if (orphan_num)
1361e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1362e4c5d848SJaegeuk Kim else
1363e4c5d848SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1364e4c5d848SJaegeuk Kim
1365c84ef3c5SSahitya Tummala if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
1366e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_FSCK_FLAG);
1367e4c5d848SJaegeuk Kim
1368c84ef3c5SSahitya Tummala if (is_sbi_flag_set(sbi, SBI_IS_RESIZEFS))
1369c84ef3c5SSahitya Tummala __set_ckpt_flags(ckpt, CP_RESIZEFS_FLAG);
1370c84ef3c5SSahitya Tummala else
1371c84ef3c5SSahitya Tummala __clear_ckpt_flags(ckpt, CP_RESIZEFS_FLAG);
1372c84ef3c5SSahitya Tummala
13734354994fSDaniel Rosenberg if (is_sbi_flag_set(sbi, SBI_CP_DISABLED))
13744354994fSDaniel Rosenberg __set_ckpt_flags(ckpt, CP_DISABLED_FLAG);
13754354994fSDaniel Rosenberg else
13764354994fSDaniel Rosenberg __clear_ckpt_flags(ckpt, CP_DISABLED_FLAG);
13774354994fSDaniel Rosenberg
1378db610a64SJaegeuk Kim if (is_sbi_flag_set(sbi, SBI_CP_DISABLED_QUICK))
1379db610a64SJaegeuk Kim __set_ckpt_flags(ckpt, CP_DISABLED_QUICK_FLAG);
1380db610a64SJaegeuk Kim else
1381db610a64SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_DISABLED_QUICK_FLAG);
1382db610a64SJaegeuk Kim
1383af033b2aSChao Yu if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH))
1384af033b2aSChao Yu __set_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG);
1385bc88ac96SJaegeuk Kim else
1386bc88ac96SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG);
1387af033b2aSChao Yu
1388af033b2aSChao Yu if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR))
1389af033b2aSChao Yu __set_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG);
1390af033b2aSChao Yu
1391e4c5d848SJaegeuk Kim /* set this flag to activate crc|cp_ver for recovery */
1392e4c5d848SJaegeuk Kim __set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG);
1393f2367923SJaegeuk Kim __clear_ckpt_flags(ckpt, CP_NOCRC_RECOVERY_FLAG);
1394e4c5d848SJaegeuk Kim
1395d1aa2453SChao Yu spin_unlock_irqrestore(&sbi->cp_lock, flags);
1396e4c5d848SJaegeuk Kim }
1397e4c5d848SJaegeuk Kim
commit_checkpoint(struct f2fs_sb_info * sbi,void * src,block_t blk_addr)139846706d59SGao Xiang static void commit_checkpoint(struct f2fs_sb_info *sbi,
139946706d59SGao Xiang void *src, block_t blk_addr)
140046706d59SGao Xiang {
140146706d59SGao Xiang struct writeback_control wbc = {
140246706d59SGao Xiang .for_reclaim = 0,
140346706d59SGao Xiang };
140446706d59SGao Xiang
140546706d59SGao Xiang /*
1406580e7a49SVishal Moola (Oracle) * filemap_get_folios_tag and lock_page again will take
14074d57b86dSChao Yu * some extra time. Therefore, f2fs_update_meta_pages and
14084d57b86dSChao Yu * f2fs_sync_meta_pages are combined in this function.
140946706d59SGao Xiang */
14104d57b86dSChao Yu struct page *page = f2fs_grab_meta_page(sbi, blk_addr);
141146706d59SGao Xiang int err;
141246706d59SGao Xiang
1413bae0ee7aSChao Yu f2fs_wait_on_page_writeback(page, META, true, true);
14148d64d365SChao Yu
14158d64d365SChao Yu memcpy(page_address(page), src, PAGE_SIZE);
14168d64d365SChao Yu
14178d64d365SChao Yu set_page_dirty(page);
141846706d59SGao Xiang if (unlikely(!clear_page_dirty_for_io(page)))
141946706d59SGao Xiang f2fs_bug_on(sbi, 1);
142046706d59SGao Xiang
142146706d59SGao Xiang /* writeout cp pack 2 page */
142246706d59SGao Xiang err = __f2fs_write_meta_page(page, &wbc, FS_CP_META_IO);
1423af697c0fSJaegeuk Kim if (unlikely(err && f2fs_cp_error(sbi))) {
1424af697c0fSJaegeuk Kim f2fs_put_page(page, 1);
1425af697c0fSJaegeuk Kim return;
1426af697c0fSJaegeuk Kim }
142746706d59SGao Xiang
1428af697c0fSJaegeuk Kim f2fs_bug_on(sbi, err);
142946706d59SGao Xiang f2fs_put_page(page, 0);
143046706d59SGao Xiang
143146706d59SGao Xiang /* submit checkpoint (with barrier if NOBARRIER is not set) */
143246706d59SGao Xiang f2fs_submit_merged_write(sbi, META_FLUSH);
143346706d59SGao Xiang }
143446706d59SGao Xiang
get_sectors_written(struct block_device * bdev)14353a0a9cbcSChao Yu static inline u64 get_sectors_written(struct block_device *bdev)
14363a0a9cbcSChao Yu {
1437ff49c86fSLinus Torvalds return (u64)part_stat_read(bdev, sectors[STAT_WRITE]);
14383a0a9cbcSChao Yu }
14393a0a9cbcSChao Yu
f2fs_get_sectors_written(struct f2fs_sb_info * sbi)14403a0a9cbcSChao Yu u64 f2fs_get_sectors_written(struct f2fs_sb_info *sbi)
14413a0a9cbcSChao Yu {
14423a0a9cbcSChao Yu if (f2fs_is_multi_device(sbi)) {
14433a0a9cbcSChao Yu u64 sectors = 0;
14443a0a9cbcSChao Yu int i;
14453a0a9cbcSChao Yu
14463a0a9cbcSChao Yu for (i = 0; i < sbi->s_ndevs; i++)
14473a0a9cbcSChao Yu sectors += get_sectors_written(FDEV(i).bdev);
14483a0a9cbcSChao Yu
14493a0a9cbcSChao Yu return sectors;
14503a0a9cbcSChao Yu }
14513a0a9cbcSChao Yu
14523a0a9cbcSChao Yu return get_sectors_written(sbi->sb->s_bdev);
14533a0a9cbcSChao Yu }
14543a0a9cbcSChao Yu
do_checkpoint(struct f2fs_sb_info * sbi,struct cp_control * cpc)1455c34f42e2SChao Yu static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1456127e670aSJaegeuk Kim {
1457127e670aSJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
145877041823SHuang Ying struct f2fs_nm_info *nm_i = NM_I(sbi);
1459d1aa2453SChao Yu unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num, flags;
1460127e670aSJaegeuk Kim block_t start_blk;
1461127e670aSJaegeuk Kim unsigned int data_sum_blocks, orphan_blocks;
14627e586fa0SJaegeuk Kim __u32 crc32 = 0;
1463127e670aSJaegeuk Kim int i;
146455141486SWanpeng Li int cp_payload_blks = __cp_payload(sbi);
14658f1dbbbbSShuoran Liu struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
14668f1dbbbbSShuoran Liu u64 kbytes_written;
14671228b482SChao Yu int err;
1468127e670aSJaegeuk Kim
1469127e670aSJaegeuk Kim /* Flush all the NAT/SIT pages */
14704d57b86dSChao Yu f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
1471127e670aSJaegeuk Kim
14727a88ddb5SChao Yu /* start to update checkpoint, cp ver is already updated previously */
1473a1f72ac2SChao Yu ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi, true));
1474127e670aSJaegeuk Kim ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
1475b5b82205SChao Yu for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
14765a4fed7cSChristoph Hellwig struct curseg_info *curseg = CURSEG_I(sbi, i + CURSEG_HOT_NODE);
14775a4fed7cSChristoph Hellwig
14785a4fed7cSChristoph Hellwig ckpt->cur_node_segno[i] = cpu_to_le32(curseg->segno);
14795a4fed7cSChristoph Hellwig ckpt->cur_node_blkoff[i] = cpu_to_le16(curseg->next_blkoff);
14805a4fed7cSChristoph Hellwig ckpt->alloc_type[i + CURSEG_HOT_NODE] = curseg->alloc_type;
1481127e670aSJaegeuk Kim }
1482b5b82205SChao Yu for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
14835a4fed7cSChristoph Hellwig struct curseg_info *curseg = CURSEG_I(sbi, i + CURSEG_HOT_DATA);
14845a4fed7cSChristoph Hellwig
14855a4fed7cSChristoph Hellwig ckpt->cur_data_segno[i] = cpu_to_le32(curseg->segno);
14865a4fed7cSChristoph Hellwig ckpt->cur_data_blkoff[i] = cpu_to_le16(curseg->next_blkoff);
14875a4fed7cSChristoph Hellwig ckpt->alloc_type[i + CURSEG_HOT_DATA] = curseg->alloc_type;
1488127e670aSJaegeuk Kim }
1489127e670aSJaegeuk Kim
1490127e670aSJaegeuk Kim /* 2 cp + n data seg summary + orphan inode blocks */
14914d57b86dSChao Yu data_sum_blocks = f2fs_npages_for_summary_flush(sbi, false);
1492d1aa2453SChao Yu spin_lock_irqsave(&sbi->cp_lock, flags);
1493b5b82205SChao Yu if (data_sum_blocks < NR_CURSEG_DATA_TYPE)
1494aaec2b1dSChao Yu __set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
1495127e670aSJaegeuk Kim else
1496aaec2b1dSChao Yu __clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
1497d1aa2453SChao Yu spin_unlock_irqrestore(&sbi->cp_lock, flags);
1498127e670aSJaegeuk Kim
149967298804SChao Yu orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num);
15001dbe4152SChangman Lee ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks +
15011dbe4152SChangman Lee orphan_blocks);
1502127e670aSJaegeuk Kim
1503119ee914SJaegeuk Kim if (__remain_node_summaries(cpc->reason))
1504b5b82205SChao Yu ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
15051dbe4152SChangman Lee cp_payload_blks + data_sum_blocks +
15061dbe4152SChangman Lee orphan_blocks + NR_CURSEG_NODE_TYPE);
1507119ee914SJaegeuk Kim else
1508b5b82205SChao Yu ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
15091dbe4152SChangman Lee cp_payload_blks + data_sum_blocks +
15101dbe4152SChangman Lee orphan_blocks);
1511119ee914SJaegeuk Kim
1512e4c5d848SJaegeuk Kim /* update ckpt flag for checkpoint */
1513e4c5d848SJaegeuk Kim update_ckpt_flags(sbi, cpc);
1514a468f0efSJaegeuk Kim
1515127e670aSJaegeuk Kim /* update SIT/NAT bitmap */
1516127e670aSJaegeuk Kim get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
1517127e670aSJaegeuk Kim get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
1518127e670aSJaegeuk Kim
1519d7eb8f1cSChao Yu crc32 = f2fs_checkpoint_chksum(sbi, ckpt);
15207e586fa0SJaegeuk Kim *((__le32 *)((unsigned char *)ckpt +
15217e586fa0SJaegeuk Kim le32_to_cpu(ckpt->checksum_offset)))
1522127e670aSJaegeuk Kim = cpu_to_le32(crc32);
1523127e670aSJaegeuk Kim
15248508e44aSJaegeuk Kim start_blk = __start_cp_next_addr(sbi);
1525127e670aSJaegeuk Kim
152622ad0b6aSJaegeuk Kim /* write nat bits */
152794c821fbSChao Yu if ((cpc->reason & CP_UMOUNT) &&
152894c821fbSChao Yu is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG)) {
152922ad0b6aSJaegeuk Kim __u64 cp_ver = cur_cp_version(ckpt);
153022ad0b6aSJaegeuk Kim block_t blk;
153122ad0b6aSJaegeuk Kim
153222ad0b6aSJaegeuk Kim cp_ver |= ((__u64)crc32 << 32);
153322ad0b6aSJaegeuk Kim *(__le64 *)nm_i->nat_bits = cpu_to_le64(cp_ver);
153422ad0b6aSJaegeuk Kim
1535f0248ba6SJaegeuk Kim blk = start_blk + BLKS_PER_SEG(sbi) - nm_i->nat_bits_blocks;
153622ad0b6aSJaegeuk Kim for (i = 0; i < nm_i->nat_bits_blocks; i++)
15374d57b86dSChao Yu f2fs_update_meta_page(sbi, nm_i->nat_bits +
153822ad0b6aSJaegeuk Kim (i << F2FS_BLKSIZE_BITS), blk + i);
153922ad0b6aSJaegeuk Kim }
154022ad0b6aSJaegeuk Kim
1541127e670aSJaegeuk Kim /* write out checkpoint buffer at block 0 */
15424d57b86dSChao Yu f2fs_update_meta_page(sbi, ckpt, start_blk++);
1543127e670aSJaegeuk Kim
1544381722d2SChao Yu for (i = 1; i < 1 + cp_payload_blks; i++)
15454d57b86dSChao Yu f2fs_update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE,
1546381722d2SChao Yu start_blk++);
15471dbe4152SChangman Lee
154867298804SChao Yu if (orphan_num) {
1549127e670aSJaegeuk Kim write_orphan_inodes(sbi, start_blk);
1550127e670aSJaegeuk Kim start_blk += orphan_blocks;
1551127e670aSJaegeuk Kim }
1552127e670aSJaegeuk Kim
15534d57b86dSChao Yu f2fs_write_data_summaries(sbi, start_blk);
1554127e670aSJaegeuk Kim start_blk += data_sum_blocks;
15558f1dbbbbSShuoran Liu
15568f1dbbbbSShuoran Liu /* Record write statistics in the hot node summary */
15578f1dbbbbSShuoran Liu kbytes_written = sbi->kbytes_written;
15583a0a9cbcSChao Yu kbytes_written += (f2fs_get_sectors_written(sbi) -
15593a0a9cbcSChao Yu sbi->sectors_written_start) >> 1;
1560b7ad7512SChao Yu seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written);
15618f1dbbbbSShuoran Liu
1562119ee914SJaegeuk Kim if (__remain_node_summaries(cpc->reason)) {
15634d57b86dSChao Yu f2fs_write_node_summaries(sbi, start_blk);
1564127e670aSJaegeuk Kim start_blk += NR_CURSEG_NODE_TYPE;
1565127e670aSJaegeuk Kim }
1566127e670aSJaegeuk Kim
156746706d59SGao Xiang /* Here, we have one bio having CP pack except cp pack 2 page */
15684d57b86dSChao Yu f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
1569bf22c3ccSSahitya Tummala /* Wait for all dirty meta pages to be submitted for IO */
1570bf22c3ccSSahitya Tummala f2fs_wait_on_all_pages(sbi, F2FS_DIRTY_META);
157146706d59SGao Xiang
157246706d59SGao Xiang /* wait for previous submitted meta pages writeback */
1573bf22c3ccSSahitya Tummala f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
1574127e670aSJaegeuk Kim
157546706d59SGao Xiang /* flush all device cache */
157646706d59SGao Xiang err = f2fs_flush_device_cache(sbi);
157746706d59SGao Xiang if (err)
157846706d59SGao Xiang return err;
1579127e670aSJaegeuk Kim
158046706d59SGao Xiang /* barrier and flush checkpoint cp pack 2 page if it can */
158146706d59SGao Xiang commit_checkpoint(sbi, ckpt, start_blk);
1582bf22c3ccSSahitya Tummala f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
15836a8f8ca5SJaegeuk Kim
158418767e62SChao Yu /*
1585542989b6SEric Biggers * invalidate intermediate page cache borrowed from meta inode which are
1586aff6fbbeSChao Yu * used for migration of encrypted, verity or compressed inode's blocks.
158718767e62SChao Yu */
1588aff6fbbeSChao Yu if (f2fs_sb_has_encrypt(sbi) || f2fs_sb_has_verity(sbi) ||
1589aff6fbbeSChao Yu f2fs_sb_has_compression(sbi))
1590c92f2927SChao Yu f2fs_bug_on(sbi,
1591c92f2927SChao Yu invalidate_inode_pages2_range(META_MAPPING(sbi),
1592c92f2927SChao Yu MAIN_BLKADDR(sbi), MAX_BLKADDR(sbi) - 1));
159318767e62SChao Yu
15944d57b86dSChao Yu f2fs_release_ino_entry(sbi, false);
1595cf779cabSJaegeuk Kim
159650fa53ecSChao Yu f2fs_reset_fsync_node_info(sbi);
159750fa53ecSChao Yu
1598caf0047eSChao Yu clear_sbi_flag(sbi, SBI_IS_DIRTY);
1599bbf156f7SJaegeuk Kim clear_sbi_flag(sbi, SBI_NEED_CP);
1600af033b2aSChao Yu clear_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH);
1601c9c8ed50SChao Yu
1602c9c8ed50SChao Yu spin_lock(&sbi->stat_lock);
16034354994fSDaniel Rosenberg sbi->unusable_block_count = 0;
1604c9c8ed50SChao Yu spin_unlock(&sbi->stat_lock);
1605c9c8ed50SChao Yu
16068508e44aSJaegeuk Kim __set_cp_next_pack(sbi);
1607c34f42e2SChao Yu
1608c2a080aeSChao Yu /*
1609c2a080aeSChao Yu * redirty superblock if metadata like node page or inode cache is
1610c2a080aeSChao Yu * updated during writing checkpoint.
1611c2a080aeSChao Yu */
1612c2a080aeSChao Yu if (get_pages(sbi, F2FS_DIRTY_NODES) ||
1613c2a080aeSChao Yu get_pages(sbi, F2FS_DIRTY_IMETA))
1614c2a080aeSChao Yu set_sbi_flag(sbi, SBI_IS_DIRTY);
1615c2a080aeSChao Yu
1616c2a080aeSChao Yu f2fs_bug_on(sbi, get_pages(sbi, F2FS_DIRTY_DENTS));
1617c2a080aeSChao Yu
1618af697c0fSJaegeuk Kim return unlikely(f2fs_cp_error(sbi)) ? -EIO : 0;
1619127e670aSJaegeuk Kim }
1620127e670aSJaegeuk Kim
f2fs_write_checkpoint(struct f2fs_sb_info * sbi,struct cp_control * cpc)16214d57b86dSChao Yu int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1622127e670aSJaegeuk Kim {
1623127e670aSJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1624127e670aSJaegeuk Kim unsigned long long ckpt_ver;
1625c34f42e2SChao Yu int err = 0;
1626127e670aSJaegeuk Kim
1627f5a131bbSChao Yu if (f2fs_readonly(sbi->sb) || f2fs_hw_is_readonly(sbi))
1628f5a131bbSChao Yu return -EROFS;
1629f5a131bbSChao Yu
16304354994fSDaniel Rosenberg if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
16314354994fSDaniel Rosenberg if (cpc->reason != CP_PAUSE)
16324354994fSDaniel Rosenberg return 0;
1633dcbb4c10SJoe Perches f2fs_warn(sbi, "Start checkpoint disabled!");
16344354994fSDaniel Rosenberg }
1635b4b10061SJaegeuk Kim if (cpc->reason != CP_RESIZE)
1636e4544b63STim Murray f2fs_down_write(&sbi->cp_global_sem);
16378501017eSJaegeuk Kim
1638caf0047eSChao Yu if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) &&
1639c473f1a9SChao Yu ((cpc->reason & CP_FASTBOOT) || (cpc->reason & CP_SYNC) ||
1640c473f1a9SChao Yu ((cpc->reason & CP_DISCARD) && !sbi->discard_blks)))
16418501017eSJaegeuk Kim goto out;
1642c34f42e2SChao Yu if (unlikely(f2fs_cp_error(sbi))) {
1643c34f42e2SChao Yu err = -EIO;
1644cf779cabSJaegeuk Kim goto out;
1645c34f42e2SChao Yu }
16462bda542dSWanpeng Li
16472bda542dSWanpeng Li trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops");
16482bda542dSWanpeng Li
1649c34f42e2SChao Yu err = block_operations(sbi);
1650c34f42e2SChao Yu if (err)
1651cf779cabSJaegeuk Kim goto out;
1652127e670aSJaegeuk Kim
165375ab4cb8SJaegeuk Kim trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops");
16542af4bd6cSNamjae Jeon
1655b9109b0eSJaegeuk Kim f2fs_flush_merged_writes(sbi);
1656127e670aSJaegeuk Kim
165758cce381SYunlei He /* this is the case of multiple fstrims without any changes */
1658c473f1a9SChao Yu if (cpc->reason & CP_DISCARD) {
16594d57b86dSChao Yu if (!f2fs_exist_trim_candidates(sbi, cpc)) {
166025290fa5SJaegeuk Kim unblock_operations(sbi);
166125290fa5SJaegeuk Kim goto out;
166225290fa5SJaegeuk Kim }
166325290fa5SJaegeuk Kim
1664a95ba66aSJaegeuk Kim if (NM_I(sbi)->nat_cnt[DIRTY_NAT] == 0 &&
16650333ad4eSJaegeuk Kim SIT_I(sbi)->dirty_sentries == 0 &&
16660333ad4eSJaegeuk Kim prefree_segments(sbi) == 0) {
16674d57b86dSChao Yu f2fs_flush_sit_entries(sbi, cpc);
16684d57b86dSChao Yu f2fs_clear_prefree_segments(sbi, cpc);
166958cce381SYunlei He unblock_operations(sbi);
167058cce381SYunlei He goto out;
167158cce381SYunlei He }
16720333ad4eSJaegeuk Kim }
167358cce381SYunlei He
1674127e670aSJaegeuk Kim /*
1675127e670aSJaegeuk Kim * update checkpoint pack index
1676127e670aSJaegeuk Kim * Increase the version number so that
1677127e670aSJaegeuk Kim * SIT entries and seg summaries are written at correct place
1678127e670aSJaegeuk Kim */
1679d71b5564SJaegeuk Kim ckpt_ver = cur_cp_version(ckpt);
1680127e670aSJaegeuk Kim ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
1681127e670aSJaegeuk Kim
1682127e670aSJaegeuk Kim /* write cached NAT/SIT entries to NAT/SIT area */
1683edc55aafSJaegeuk Kim err = f2fs_flush_nat_entries(sbi, cpc);
168491803392SChao Yu if (err) {
168591803392SChao Yu f2fs_err(sbi, "f2fs_flush_nat_entries failed err:%d, stop checkpoint", err);
168691803392SChao Yu f2fs_bug_on(sbi, !f2fs_cp_error(sbi));
1687edc55aafSJaegeuk Kim goto stop;
168891803392SChao Yu }
1689edc55aafSJaegeuk Kim
16904d57b86dSChao Yu f2fs_flush_sit_entries(sbi, cpc);
1691127e670aSJaegeuk Kim
1692d0b9e42aSChao Yu /* save inmem log status */
1693093749e2SChao Yu f2fs_save_inmem_curseg(sbi);
1694d0b9e42aSChao Yu
1695c34f42e2SChao Yu err = do_checkpoint(sbi, cpc);
169691803392SChao Yu if (err) {
169791803392SChao Yu f2fs_err(sbi, "do_checkpoint failed err:%d, stop checkpoint", err);
169891803392SChao Yu f2fs_bug_on(sbi, !f2fs_cp_error(sbi));
16994d57b86dSChao Yu f2fs_release_discard_addrs(sbi);
170091803392SChao Yu } else {
17014d57b86dSChao Yu f2fs_clear_prefree_segments(sbi, cpc);
170291803392SChao Yu }
1703d0b9e42aSChao Yu
1704093749e2SChao Yu f2fs_restore_inmem_curseg(sbi);
1705eb61c2ccSChao Yu stat_inc_cp_count(sbi);
1706edc55aafSJaegeuk Kim stop:
1707127e670aSJaegeuk Kim unblock_operations(sbi);
170810027551SJaegeuk Kim
1709c473f1a9SChao Yu if (cpc->reason & CP_RECOVERY)
1710dcbb4c10SJoe Perches f2fs_notice(sbi, "checkpoint: version = %llx", ckpt_ver);
171160b99b48SJaegeuk Kim
17127a88ddb5SChao Yu /* update CP_TIME to trigger checkpoint periodically */
17136beceb54SJaegeuk Kim f2fs_update_time(sbi, CP_TIME);
171455d1cdb2SJaegeuk Kim trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint");
17158501017eSJaegeuk Kim out:
1716b4b10061SJaegeuk Kim if (cpc->reason != CP_RESIZE)
1717e4544b63STim Murray f2fs_up_write(&sbi->cp_global_sem);
1718c34f42e2SChao Yu return err;
1719127e670aSJaegeuk Kim }
1720127e670aSJaegeuk Kim
f2fs_init_ino_entry_info(struct f2fs_sb_info * sbi)17214d57b86dSChao Yu void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi)
1722127e670aSJaegeuk Kim {
17236451e041SJaegeuk Kim int i;
17246451e041SJaegeuk Kim
17256451e041SJaegeuk Kim for (i = 0; i < MAX_INO_ENTRY; i++) {
172667298804SChao Yu struct inode_management *im = &sbi->im[i];
172767298804SChao Yu
172867298804SChao Yu INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC);
172967298804SChao Yu spin_lock_init(&im->ino_lock);
173067298804SChao Yu INIT_LIST_HEAD(&im->ino_list);
173167298804SChao Yu im->ino_num = 0;
17326451e041SJaegeuk Kim }
17336451e041SJaegeuk Kim
1734f0248ba6SJaegeuk Kim sbi->max_orphans = (BLKS_PER_SEG(sbi) - F2FS_CP_PACKS -
1735d0b9e42aSChao Yu NR_CURSEG_PERSIST_TYPE - __cp_payload(sbi)) *
173614b42817SWanpeng Li F2FS_ORPHANS_PER_BLOCK;
1737127e670aSJaegeuk Kim }
1738127e670aSJaegeuk Kim
f2fs_create_checkpoint_caches(void)17394d57b86dSChao Yu int __init f2fs_create_checkpoint_caches(void)
1740127e670aSJaegeuk Kim {
17416451e041SJaegeuk Kim ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry",
17426451e041SJaegeuk Kim sizeof(struct ino_entry));
17436451e041SJaegeuk Kim if (!ino_entry_slab)
1744127e670aSJaegeuk Kim return -ENOMEM;
17454d57b86dSChao Yu f2fs_inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry",
174606292073SChao Yu sizeof(struct inode_entry));
17474d57b86dSChao Yu if (!f2fs_inode_entry_slab) {
17486451e041SJaegeuk Kim kmem_cache_destroy(ino_entry_slab);
1749127e670aSJaegeuk Kim return -ENOMEM;
1750127e670aSJaegeuk Kim }
1751127e670aSJaegeuk Kim return 0;
1752127e670aSJaegeuk Kim }
1753127e670aSJaegeuk Kim
f2fs_destroy_checkpoint_caches(void)17544d57b86dSChao Yu void f2fs_destroy_checkpoint_caches(void)
1755127e670aSJaegeuk Kim {
17566451e041SJaegeuk Kim kmem_cache_destroy(ino_entry_slab);
17574d57b86dSChao Yu kmem_cache_destroy(f2fs_inode_entry_slab);
1758127e670aSJaegeuk Kim }
1759261eeb9cSDaeho Jeong
__write_checkpoint_sync(struct f2fs_sb_info * sbi)1760261eeb9cSDaeho Jeong static int __write_checkpoint_sync(struct f2fs_sb_info *sbi)
1761261eeb9cSDaeho Jeong {
1762261eeb9cSDaeho Jeong struct cp_control cpc = { .reason = CP_SYNC, };
1763261eeb9cSDaeho Jeong int err;
1764261eeb9cSDaeho Jeong
1765e4544b63STim Murray f2fs_down_write(&sbi->gc_lock);
1766261eeb9cSDaeho Jeong err = f2fs_write_checkpoint(sbi, &cpc);
1767e4544b63STim Murray f2fs_up_write(&sbi->gc_lock);
1768261eeb9cSDaeho Jeong
1769261eeb9cSDaeho Jeong return err;
1770261eeb9cSDaeho Jeong }
1771261eeb9cSDaeho Jeong
__checkpoint_and_complete_reqs(struct f2fs_sb_info * sbi)1772261eeb9cSDaeho Jeong static void __checkpoint_and_complete_reqs(struct f2fs_sb_info *sbi)
1773261eeb9cSDaeho Jeong {
1774261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info;
1775261eeb9cSDaeho Jeong struct ckpt_req *req, *next;
1776261eeb9cSDaeho Jeong struct llist_node *dispatch_list;
1777261eeb9cSDaeho Jeong u64 sum_diff = 0, diff, count = 0;
1778261eeb9cSDaeho Jeong int ret;
1779261eeb9cSDaeho Jeong
1780261eeb9cSDaeho Jeong dispatch_list = llist_del_all(&cprc->issue_list);
1781261eeb9cSDaeho Jeong if (!dispatch_list)
1782261eeb9cSDaeho Jeong return;
1783261eeb9cSDaeho Jeong dispatch_list = llist_reverse_order(dispatch_list);
1784261eeb9cSDaeho Jeong
1785261eeb9cSDaeho Jeong ret = __write_checkpoint_sync(sbi);
1786261eeb9cSDaeho Jeong atomic_inc(&cprc->issued_ckpt);
1787261eeb9cSDaeho Jeong
1788261eeb9cSDaeho Jeong llist_for_each_entry_safe(req, next, dispatch_list, llnode) {
1789261eeb9cSDaeho Jeong diff = (u64)ktime_ms_delta(ktime_get(), req->queue_time);
1790261eeb9cSDaeho Jeong req->ret = ret;
1791261eeb9cSDaeho Jeong complete(&req->wait);
1792261eeb9cSDaeho Jeong
1793261eeb9cSDaeho Jeong sum_diff += diff;
1794261eeb9cSDaeho Jeong count++;
1795261eeb9cSDaeho Jeong }
1796261eeb9cSDaeho Jeong atomic_sub(count, &cprc->queued_ckpt);
1797261eeb9cSDaeho Jeong atomic_add(count, &cprc->total_ckpt);
1798261eeb9cSDaeho Jeong
1799261eeb9cSDaeho Jeong spin_lock(&cprc->stat_lock);
1800261eeb9cSDaeho Jeong cprc->cur_time = (unsigned int)div64_u64(sum_diff, count);
1801261eeb9cSDaeho Jeong if (cprc->peak_time < cprc->cur_time)
1802261eeb9cSDaeho Jeong cprc->peak_time = cprc->cur_time;
1803261eeb9cSDaeho Jeong spin_unlock(&cprc->stat_lock);
1804261eeb9cSDaeho Jeong }
1805261eeb9cSDaeho Jeong
issue_checkpoint_thread(void * data)1806261eeb9cSDaeho Jeong static int issue_checkpoint_thread(void *data)
1807261eeb9cSDaeho Jeong {
1808261eeb9cSDaeho Jeong struct f2fs_sb_info *sbi = data;
1809261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info;
1810261eeb9cSDaeho Jeong wait_queue_head_t *q = &cprc->ckpt_wait_queue;
1811261eeb9cSDaeho Jeong repeat:
1812261eeb9cSDaeho Jeong if (kthread_should_stop())
1813261eeb9cSDaeho Jeong return 0;
1814261eeb9cSDaeho Jeong
1815261eeb9cSDaeho Jeong if (!llist_empty(&cprc->issue_list))
1816261eeb9cSDaeho Jeong __checkpoint_and_complete_reqs(sbi);
1817261eeb9cSDaeho Jeong
1818261eeb9cSDaeho Jeong wait_event_interruptible(*q,
1819261eeb9cSDaeho Jeong kthread_should_stop() || !llist_empty(&cprc->issue_list));
1820261eeb9cSDaeho Jeong goto repeat;
1821261eeb9cSDaeho Jeong }
1822261eeb9cSDaeho Jeong
flush_remained_ckpt_reqs(struct f2fs_sb_info * sbi,struct ckpt_req * wait_req)1823261eeb9cSDaeho Jeong static void flush_remained_ckpt_reqs(struct f2fs_sb_info *sbi,
1824261eeb9cSDaeho Jeong struct ckpt_req *wait_req)
1825261eeb9cSDaeho Jeong {
1826261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info;
1827261eeb9cSDaeho Jeong
1828261eeb9cSDaeho Jeong if (!llist_empty(&cprc->issue_list)) {
1829261eeb9cSDaeho Jeong __checkpoint_and_complete_reqs(sbi);
1830261eeb9cSDaeho Jeong } else {
1831261eeb9cSDaeho Jeong /* already dispatched by issue_checkpoint_thread */
1832261eeb9cSDaeho Jeong if (wait_req)
1833261eeb9cSDaeho Jeong wait_for_completion(&wait_req->wait);
1834261eeb9cSDaeho Jeong }
1835261eeb9cSDaeho Jeong }
1836261eeb9cSDaeho Jeong
init_ckpt_req(struct ckpt_req * req)1837261eeb9cSDaeho Jeong static void init_ckpt_req(struct ckpt_req *req)
1838261eeb9cSDaeho Jeong {
1839261eeb9cSDaeho Jeong memset(req, 0, sizeof(struct ckpt_req));
1840261eeb9cSDaeho Jeong
1841261eeb9cSDaeho Jeong init_completion(&req->wait);
1842261eeb9cSDaeho Jeong req->queue_time = ktime_get();
1843261eeb9cSDaeho Jeong }
1844261eeb9cSDaeho Jeong
f2fs_issue_checkpoint(struct f2fs_sb_info * sbi)1845261eeb9cSDaeho Jeong int f2fs_issue_checkpoint(struct f2fs_sb_info *sbi)
1846261eeb9cSDaeho Jeong {
1847261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info;
1848261eeb9cSDaeho Jeong struct ckpt_req req;
1849261eeb9cSDaeho Jeong struct cp_control cpc;
1850261eeb9cSDaeho Jeong
1851261eeb9cSDaeho Jeong cpc.reason = __get_cp_reason(sbi);
1852261eeb9cSDaeho Jeong if (!test_opt(sbi, MERGE_CHECKPOINT) || cpc.reason != CP_SYNC) {
1853261eeb9cSDaeho Jeong int ret;
1854261eeb9cSDaeho Jeong
1855e4544b63STim Murray f2fs_down_write(&sbi->gc_lock);
1856261eeb9cSDaeho Jeong ret = f2fs_write_checkpoint(sbi, &cpc);
1857e4544b63STim Murray f2fs_up_write(&sbi->gc_lock);
1858261eeb9cSDaeho Jeong
1859261eeb9cSDaeho Jeong return ret;
1860261eeb9cSDaeho Jeong }
1861261eeb9cSDaeho Jeong
1862261eeb9cSDaeho Jeong if (!cprc->f2fs_issue_ckpt)
1863261eeb9cSDaeho Jeong return __write_checkpoint_sync(sbi);
1864261eeb9cSDaeho Jeong
1865261eeb9cSDaeho Jeong init_ckpt_req(&req);
1866261eeb9cSDaeho Jeong
1867261eeb9cSDaeho Jeong llist_add(&req.llnode, &cprc->issue_list);
1868261eeb9cSDaeho Jeong atomic_inc(&cprc->queued_ckpt);
1869261eeb9cSDaeho Jeong
18703b42c741SChao Yu /*
18713b42c741SChao Yu * update issue_list before we wake up issue_checkpoint thread,
18723b42c741SChao Yu * this smp_mb() pairs with another barrier in ___wait_event(),
18733b42c741SChao Yu * see more details in comments of waitqueue_active().
18743b42c741SChao Yu */
1875261eeb9cSDaeho Jeong smp_mb();
1876261eeb9cSDaeho Jeong
1877261eeb9cSDaeho Jeong if (waitqueue_active(&cprc->ckpt_wait_queue))
1878261eeb9cSDaeho Jeong wake_up(&cprc->ckpt_wait_queue);
1879261eeb9cSDaeho Jeong
1880261eeb9cSDaeho Jeong if (cprc->f2fs_issue_ckpt)
1881261eeb9cSDaeho Jeong wait_for_completion(&req.wait);
1882261eeb9cSDaeho Jeong else
1883261eeb9cSDaeho Jeong flush_remained_ckpt_reqs(sbi, &req);
1884261eeb9cSDaeho Jeong
1885261eeb9cSDaeho Jeong return req.ret;
1886261eeb9cSDaeho Jeong }
1887261eeb9cSDaeho Jeong
f2fs_start_ckpt_thread(struct f2fs_sb_info * sbi)1888261eeb9cSDaeho Jeong int f2fs_start_ckpt_thread(struct f2fs_sb_info *sbi)
1889261eeb9cSDaeho Jeong {
1890261eeb9cSDaeho Jeong dev_t dev = sbi->sb->s_bdev->bd_dev;
1891261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info;
1892261eeb9cSDaeho Jeong
1893261eeb9cSDaeho Jeong if (cprc->f2fs_issue_ckpt)
1894261eeb9cSDaeho Jeong return 0;
1895261eeb9cSDaeho Jeong
1896261eeb9cSDaeho Jeong cprc->f2fs_issue_ckpt = kthread_run(issue_checkpoint_thread, sbi,
1897261eeb9cSDaeho Jeong "f2fs_ckpt-%u:%u", MAJOR(dev), MINOR(dev));
1898261eeb9cSDaeho Jeong if (IS_ERR(cprc->f2fs_issue_ckpt)) {
1899146dbcbfSYangtao Li int err = PTR_ERR(cprc->f2fs_issue_ckpt);
1900146dbcbfSYangtao Li
1901261eeb9cSDaeho Jeong cprc->f2fs_issue_ckpt = NULL;
1902146dbcbfSYangtao Li return err;
1903261eeb9cSDaeho Jeong }
1904261eeb9cSDaeho Jeong
1905e6592066SDaeho Jeong set_task_ioprio(cprc->f2fs_issue_ckpt, cprc->ckpt_thread_ioprio);
1906261eeb9cSDaeho Jeong
1907261eeb9cSDaeho Jeong return 0;
1908261eeb9cSDaeho Jeong }
1909261eeb9cSDaeho Jeong
f2fs_stop_ckpt_thread(struct f2fs_sb_info * sbi)1910261eeb9cSDaeho Jeong void f2fs_stop_ckpt_thread(struct f2fs_sb_info *sbi)
1911261eeb9cSDaeho Jeong {
1912261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info;
1913c7b58576SJaegeuk Kim struct task_struct *ckpt_task;
1914261eeb9cSDaeho Jeong
1915c7b58576SJaegeuk Kim if (!cprc->f2fs_issue_ckpt)
1916c7b58576SJaegeuk Kim return;
1917261eeb9cSDaeho Jeong
1918c7b58576SJaegeuk Kim ckpt_task = cprc->f2fs_issue_ckpt;
1919261eeb9cSDaeho Jeong cprc->f2fs_issue_ckpt = NULL;
1920261eeb9cSDaeho Jeong kthread_stop(ckpt_task);
1921261eeb9cSDaeho Jeong
1922c7b58576SJaegeuk Kim f2fs_flush_ckpt_thread(sbi);
1923261eeb9cSDaeho Jeong }
1924c7b58576SJaegeuk Kim
f2fs_flush_ckpt_thread(struct f2fs_sb_info * sbi)1925c7b58576SJaegeuk Kim void f2fs_flush_ckpt_thread(struct f2fs_sb_info *sbi)
1926c7b58576SJaegeuk Kim {
1927c7b58576SJaegeuk Kim struct ckpt_req_control *cprc = &sbi->cprc_info;
1928c7b58576SJaegeuk Kim
1929c7b58576SJaegeuk Kim flush_remained_ckpt_reqs(sbi, NULL);
1930c7b58576SJaegeuk Kim
1931c7b58576SJaegeuk Kim /* Let's wait for the previous dispatched checkpoint. */
1932c7b58576SJaegeuk Kim while (atomic_read(&cprc->queued_ckpt))
1933c7b58576SJaegeuk Kim io_schedule_timeout(DEFAULT_IO_TIMEOUT);
1934261eeb9cSDaeho Jeong }
1935261eeb9cSDaeho Jeong
f2fs_init_ckpt_req_control(struct f2fs_sb_info * sbi)1936261eeb9cSDaeho Jeong void f2fs_init_ckpt_req_control(struct f2fs_sb_info *sbi)
1937261eeb9cSDaeho Jeong {
1938261eeb9cSDaeho Jeong struct ckpt_req_control *cprc = &sbi->cprc_info;
1939261eeb9cSDaeho Jeong
1940261eeb9cSDaeho Jeong atomic_set(&cprc->issued_ckpt, 0);
1941261eeb9cSDaeho Jeong atomic_set(&cprc->total_ckpt, 0);
1942261eeb9cSDaeho Jeong atomic_set(&cprc->queued_ckpt, 0);
1943e6592066SDaeho Jeong cprc->ckpt_thread_ioprio = DEFAULT_CHECKPOINT_IOPRIO;
1944261eeb9cSDaeho Jeong init_waitqueue_head(&cprc->ckpt_wait_queue);
1945261eeb9cSDaeho Jeong init_llist_head(&cprc->issue_list);
1946261eeb9cSDaeho Jeong spin_lock_init(&cprc->stat_lock);
1947261eeb9cSDaeho Jeong }
1948