xref: /openbmc/linux/fs/f2fs/checkpoint.c (revision 146dbcbf17a6d07169e75224d949cc2670de2e20)
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 
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);
33aaec2b1dSChao Yu 	set_ckpt_flags(sbi, CP_ERROR_FLAG);
34a9cfee0eSChao Yu 	if (!end_io) {
35b9109b0eSJaegeuk Kim 		f2fs_flush_merged_writes(sbi);
36a9cfee0eSChao Yu 
37a9cfee0eSChao Yu 		f2fs_handle_stop(sbi, reason);
38a9cfee0eSChao Yu 	}
3938f91ca8SJaegeuk Kim }
4038f91ca8SJaegeuk Kim 
410a8165d7SJaegeuk Kim /*
42127e670aSJaegeuk Kim  * We guarantee no failure on the returned page.
43127e670aSJaegeuk Kim  */
444d57b86dSChao Yu struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
45127e670aSJaegeuk Kim {
469df27d98SGu Zheng 	struct address_space *mapping = META_MAPPING(sbi);
47beb78181SZhang Qilong 	struct page *page;
48127e670aSJaegeuk Kim repeat:
49300e129cSJaegeuk Kim 	page = f2fs_grab_cache_page(mapping, index, false);
50127e670aSJaegeuk Kim 	if (!page) {
51127e670aSJaegeuk Kim 		cond_resched();
52127e670aSJaegeuk Kim 		goto repeat;
53127e670aSJaegeuk Kim 	}
54bae0ee7aSChao Yu 	f2fs_wait_on_page_writeback(page, META, true, true);
55237c0790SJaegeuk Kim 	if (!PageUptodate(page))
56127e670aSJaegeuk Kim 		SetPageUptodate(page);
57127e670aSJaegeuk Kim 	return page;
58127e670aSJaegeuk Kim }
59127e670aSJaegeuk Kim 
602b947003SChao Yu static struct page *__get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index,
612b947003SChao Yu 							bool is_meta)
62127e670aSJaegeuk Kim {
639df27d98SGu Zheng 	struct address_space *mapping = META_MAPPING(sbi);
64127e670aSJaegeuk Kim 	struct page *page;
65cf04e8ebSJaegeuk Kim 	struct f2fs_io_info fio = {
6605ca3632SJaegeuk Kim 		.sbi = sbi,
67cf04e8ebSJaegeuk Kim 		.type = META,
6804d328deSMike Christie 		.op = REQ_OP_READ,
6970fd7614SChristoph Hellwig 		.op_flags = REQ_META | REQ_PRIO,
707a9d7548SChao Yu 		.old_blkaddr = index,
717a9d7548SChao Yu 		.new_blkaddr = index,
724375a336SJaegeuk Kim 		.encrypted_page = NULL,
736dc3a126SChao Yu 		.is_por = !is_meta,
74cf04e8ebSJaegeuk Kim 	};
757735730dSChao Yu 	int err;
762b947003SChao Yu 
772b947003SChao Yu 	if (unlikely(!is_meta))
7804d328deSMike Christie 		fio.op_flags &= ~REQ_META;
79127e670aSJaegeuk Kim repeat:
80300e129cSJaegeuk Kim 	page = f2fs_grab_cache_page(mapping, index, false);
81127e670aSJaegeuk Kim 	if (!page) {
82127e670aSJaegeuk Kim 		cond_resched();
83127e670aSJaegeuk Kim 		goto repeat;
84127e670aSJaegeuk Kim 	}
85393ff91fSJaegeuk Kim 	if (PageUptodate(page))
86393ff91fSJaegeuk Kim 		goto out;
87127e670aSJaegeuk Kim 
8805ca3632SJaegeuk Kim 	fio.page = page;
8905ca3632SJaegeuk Kim 
907735730dSChao Yu 	err = f2fs_submit_page_bio(&fio);
917735730dSChao Yu 	if (err) {
927735730dSChao Yu 		f2fs_put_page(page, 1);
937735730dSChao Yu 		return ERR_PTR(err);
9486531d6bSJaegeuk Kim 	}
95393ff91fSJaegeuk Kim 
9634a23525SChao Yu 	f2fs_update_iostat(sbi, NULL, FS_META_READ_IO, F2FS_BLKSIZE);
978b83ac81SChao Yu 
98393ff91fSJaegeuk Kim 	lock_page(page);
996bacf52fSJaegeuk Kim 	if (unlikely(page->mapping != mapping)) {
100afcb7ca0SJaegeuk Kim 		f2fs_put_page(page, 1);
101afcb7ca0SJaegeuk Kim 		goto repeat;
102afcb7ca0SJaegeuk Kim 	}
103f3f338caSChao Yu 
10481114baaSChao Yu 	if (unlikely(!PageUptodate(page))) {
105a7b8618aSJaegeuk Kim 		f2fs_handle_page_eio(sbi, page->index, META);
1067735730dSChao Yu 		f2fs_put_page(page, 1);
1077735730dSChao Yu 		return ERR_PTR(-EIO);
10881114baaSChao Yu 	}
109393ff91fSJaegeuk Kim out:
110127e670aSJaegeuk Kim 	return page;
111127e670aSJaegeuk Kim }
112127e670aSJaegeuk Kim 
1134d57b86dSChao Yu struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
1142b947003SChao Yu {
1152b947003SChao Yu 	return __get_meta_page(sbi, index, true);
1162b947003SChao Yu }
1172b947003SChao Yu 
11886f33603SJaegeuk Kim struct page *f2fs_get_meta_page_retry(struct f2fs_sb_info *sbi, pgoff_t index)
1197735730dSChao Yu {
1207735730dSChao Yu 	struct page *page;
1217735730dSChao Yu 	int count = 0;
1227735730dSChao Yu 
1237735730dSChao Yu retry:
1247735730dSChao Yu 	page = __get_meta_page(sbi, index, true);
1257735730dSChao Yu 	if (IS_ERR(page)) {
1267735730dSChao Yu 		if (PTR_ERR(page) == -EIO &&
1277735730dSChao Yu 				++count <= DEFAULT_RETRY_IO_COUNT)
1287735730dSChao Yu 			goto retry;
129a9cfee0eSChao Yu 		f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_META_PAGE);
1307735730dSChao Yu 	}
1317735730dSChao Yu 	return page;
1327735730dSChao Yu }
1337735730dSChao Yu 
1342b947003SChao Yu /* for POR only */
1354d57b86dSChao Yu struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index)
1362b947003SChao Yu {
1372b947003SChao Yu 	return __get_meta_page(sbi, index, false);
1382b947003SChao Yu }
1392b947003SChao Yu 
14093770ab7SChao Yu static bool __is_bitmap_valid(struct f2fs_sb_info *sbi, block_t blkaddr,
14193770ab7SChao Yu 							int type)
14293770ab7SChao Yu {
14393770ab7SChao Yu 	struct seg_entry *se;
14493770ab7SChao Yu 	unsigned int segno, offset;
14593770ab7SChao Yu 	bool exist;
14693770ab7SChao Yu 
1470ef4ca04SChao Yu 	if (type == DATA_GENERIC)
14893770ab7SChao Yu 		return true;
14993770ab7SChao Yu 
15093770ab7SChao Yu 	segno = GET_SEGNO(sbi, blkaddr);
15193770ab7SChao Yu 	offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
15293770ab7SChao Yu 	se = get_seg_entry(sbi, segno);
15393770ab7SChao Yu 
15493770ab7SChao Yu 	exist = f2fs_test_bit(offset, se->cur_valid_map);
1550ef4ca04SChao Yu 	if (exist && type == DATA_GENERIC_ENHANCE_UPDATE) {
1560ef4ca04SChao Yu 		f2fs_err(sbi, "Inconsistent error blkaddr:%u, sit bitmap:%d",
1570ef4ca04SChao Yu 			 blkaddr, exist);
1580ef4ca04SChao Yu 		set_sbi_flag(sbi, SBI_NEED_FSCK);
1590ef4ca04SChao Yu 		return exist;
1600ef4ca04SChao Yu 	}
1610ef4ca04SChao Yu 
16293770ab7SChao Yu 	if (!exist && type == DATA_GENERIC_ENHANCE) {
163dcbb4c10SJoe Perches 		f2fs_err(sbi, "Inconsistent error blkaddr:%u, sit bitmap:%d",
164dcbb4c10SJoe Perches 			 blkaddr, exist);
16593770ab7SChao Yu 		set_sbi_flag(sbi, SBI_NEED_FSCK);
166dc2f78e2SDongliang Mu 		dump_stack();
16793770ab7SChao Yu 	}
16893770ab7SChao Yu 	return exist;
16993770ab7SChao Yu }
17093770ab7SChao Yu 
171e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
1724d57b86dSChao Yu 					block_t blkaddr, int type)
173662befdaSChao Yu {
17418792e64SChao Yu 	if (time_to_inject(sbi, FAULT_BLKADDR)) {
17518792e64SChao Yu 		f2fs_show_injection_info(sbi, FAULT_BLKADDR);
17618792e64SChao Yu 		return false;
17718792e64SChao Yu 	}
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))) {
207dcbb4c10SJoe Perches 			f2fs_warn(sbi, "access invalid blkaddr:%u",
208dcbb4c10SJoe Perches 				  blkaddr);
20993770ab7SChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);
210dc2f78e2SDongliang Mu 			dump_stack();
21166b00c18SChao Yu 			return false;
21293770ab7SChao Yu 		} else {
21393770ab7SChao Yu 			return __is_bitmap_valid(sbi, blkaddr, type);
214c9b60788SChao Yu 		}
21566b00c18SChao Yu 		break;
216e1da7872SChao Yu 	case META_GENERIC:
217e1da7872SChao Yu 		if (unlikely(blkaddr < SEG0_BLKADDR(sbi) ||
218e1da7872SChao Yu 			blkaddr >= MAIN_BLKADDR(sbi)))
219e1da7872SChao Yu 			return false;
220e1da7872SChao Yu 		break;
221662befdaSChao Yu 	default:
222662befdaSChao Yu 		BUG();
223662befdaSChao Yu 	}
22466b00c18SChao Yu 
22566b00c18SChao Yu 	return true;
226662befdaSChao Yu }
227662befdaSChao Yu 
228662befdaSChao Yu /*
2297a88ddb5SChao Yu  * Readahead CP/NAT/SIT/SSA/POR pages
230662befdaSChao Yu  */
2314d57b86dSChao Yu int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
23226879fb1SChao Yu 							int type, bool sync)
233662befdaSChao Yu {
234662befdaSChao Yu 	struct page *page;
2354c521f49SJaegeuk Kim 	block_t blkno = start;
236662befdaSChao Yu 	struct f2fs_io_info fio = {
23705ca3632SJaegeuk Kim 		.sbi = sbi,
238662befdaSChao Yu 		.type = META,
23904d328deSMike Christie 		.op = REQ_OP_READ,
24070fd7614SChristoph Hellwig 		.op_flags = sync ? (REQ_META | REQ_PRIO) : REQ_RAHEAD,
2414375a336SJaegeuk Kim 		.encrypted_page = NULL,
242fb830fc5SChao Yu 		.in_list = false,
2436dc3a126SChao Yu 		.is_por = (type == META_POR),
244662befdaSChao Yu 	};
245e9f5b8b8SChao Yu 	struct blk_plug plug;
246ce4c638cSChao Yu 	int err;
247662befdaSChao Yu 
2482b947003SChao Yu 	if (unlikely(type == META_POR))
24904d328deSMike Christie 		fio.op_flags &= ~REQ_META;
2502b947003SChao Yu 
251e9f5b8b8SChao Yu 	blk_start_plug(&plug);
252662befdaSChao Yu 	for (; nrpages-- > 0; blkno++) {
253662befdaSChao Yu 
254e1da7872SChao Yu 		if (!f2fs_is_valid_blkaddr(sbi, blkno, type))
25566b00c18SChao Yu 			goto out;
25666b00c18SChao Yu 
257662befdaSChao Yu 		switch (type) {
258662befdaSChao Yu 		case META_NAT:
25966b00c18SChao Yu 			if (unlikely(blkno >=
26066b00c18SChao Yu 					NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid)))
261662befdaSChao Yu 				blkno = 0;
26266b00c18SChao Yu 			/* get nat block addr */
2637a9d7548SChao Yu 			fio.new_blkaddr = current_nat_addr(sbi,
264662befdaSChao Yu 					blkno * NAT_ENTRY_PER_BLOCK);
265662befdaSChao Yu 			break;
266662befdaSChao Yu 		case META_SIT:
2676a257471SChao Yu 			if (unlikely(blkno >= TOTAL_SEGS(sbi)))
2686a257471SChao Yu 				goto out;
269662befdaSChao Yu 			/* get sit block addr */
2707a9d7548SChao Yu 			fio.new_blkaddr = current_sit_addr(sbi,
271662befdaSChao Yu 					blkno * SIT_ENTRY_PER_BLOCK);
272662befdaSChao Yu 			break;
27381c1a0f1SChao Yu 		case META_SSA:
274662befdaSChao Yu 		case META_CP:
2754c521f49SJaegeuk Kim 		case META_POR:
2767a9d7548SChao Yu 			fio.new_blkaddr = blkno;
277662befdaSChao Yu 			break;
278662befdaSChao Yu 		default:
279662befdaSChao Yu 			BUG();
280662befdaSChao Yu 		}
281662befdaSChao Yu 
282300e129cSJaegeuk Kim 		page = f2fs_grab_cache_page(META_MAPPING(sbi),
283300e129cSJaegeuk Kim 						fio.new_blkaddr, false);
284662befdaSChao Yu 		if (!page)
285662befdaSChao Yu 			continue;
286662befdaSChao Yu 		if (PageUptodate(page)) {
287662befdaSChao Yu 			f2fs_put_page(page, 1);
288662befdaSChao Yu 			continue;
289662befdaSChao Yu 		}
290662befdaSChao Yu 
29105ca3632SJaegeuk Kim 		fio.page = page;
292ce4c638cSChao Yu 		err = f2fs_submit_page_bio(&fio);
293ce4c638cSChao Yu 		f2fs_put_page(page, err ? 1 : 0);
2948b83ac81SChao Yu 
2958b83ac81SChao Yu 		if (!err)
29634a23525SChao Yu 			f2fs_update_iostat(sbi, NULL, FS_META_READ_IO,
29734a23525SChao Yu 							F2FS_BLKSIZE);
298662befdaSChao Yu 	}
299662befdaSChao Yu out:
300e9f5b8b8SChao Yu 	blk_finish_plug(&plug);
301662befdaSChao Yu 	return blkno - start;
302662befdaSChao Yu }
303662befdaSChao Yu 
304430f163bSChao Yu void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index,
305430f163bSChao Yu 							unsigned int ra_blocks)
306635aee1fSChao Yu {
307635aee1fSChao Yu 	struct page *page;
308635aee1fSChao Yu 	bool readahead = false;
309635aee1fSChao Yu 
310430f163bSChao Yu 	if (ra_blocks == RECOVERY_MIN_RA_BLOCKS)
311430f163bSChao Yu 		return;
312430f163bSChao Yu 
313635aee1fSChao Yu 	page = find_get_page(META_MAPPING(sbi), index);
3144da7bf5aSJaegeuk Kim 	if (!page || !PageUptodate(page))
315635aee1fSChao Yu 		readahead = true;
316635aee1fSChao Yu 	f2fs_put_page(page, 0);
317635aee1fSChao Yu 
318635aee1fSChao Yu 	if (readahead)
319430f163bSChao Yu 		f2fs_ra_meta_pages(sbi, index, ra_blocks, META_POR, true);
320635aee1fSChao Yu }
321635aee1fSChao Yu 
322b0af6d49SChao Yu static int __f2fs_write_meta_page(struct page *page,
323b0af6d49SChao Yu 				struct writeback_control *wbc,
324b0af6d49SChao Yu 				enum iostat_type io_type)
325127e670aSJaegeuk Kim {
3264081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_P_SB(page);
327577e3495SJaegeuk Kim 
328ecda0de3SChao Yu 	trace_f2fs_writepage(page, META);
329ecda0de3SChao Yu 
330af697c0fSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
331af697c0fSJaegeuk Kim 		goto redirty_out;
332caf0047eSChao Yu 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
333cfb271d4SChao Yu 		goto redirty_out;
334857dc4e0SJaegeuk Kim 	if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0))
335cfb271d4SChao Yu 		goto redirty_out;
336127e670aSJaegeuk Kim 
3374d57b86dSChao Yu 	f2fs_do_write_meta_page(sbi, page, io_type);
338127e670aSJaegeuk Kim 	dec_page_count(sbi, F2FS_DIRTY_META);
3390c3a5797SChao Yu 
3400c3a5797SChao Yu 	if (wbc->for_reclaim)
341bab475c5SChao Yu 		f2fs_submit_merged_write_cond(sbi, NULL, page, 0, META);
3420c3a5797SChao Yu 
343127e670aSJaegeuk Kim 	unlock_page(page);
344857dc4e0SJaegeuk Kim 
3450c3a5797SChao Yu 	if (unlikely(f2fs_cp_error(sbi)))
346b9109b0eSJaegeuk Kim 		f2fs_submit_merged_write(sbi, META);
3470c3a5797SChao Yu 
348577e3495SJaegeuk Kim 	return 0;
349cfb271d4SChao Yu 
350cfb271d4SChao Yu redirty_out:
35176f60268SJaegeuk Kim 	redirty_page_for_writepage(wbc, page);
352cfb271d4SChao Yu 	return AOP_WRITEPAGE_ACTIVATE;
353127e670aSJaegeuk Kim }
354127e670aSJaegeuk Kim 
355b0af6d49SChao Yu static int f2fs_write_meta_page(struct page *page,
356b0af6d49SChao Yu 				struct writeback_control *wbc)
357b0af6d49SChao Yu {
358b0af6d49SChao Yu 	return __f2fs_write_meta_page(page, wbc, FS_META_IO);
359b0af6d49SChao Yu }
360b0af6d49SChao Yu 
361127e670aSJaegeuk Kim static int f2fs_write_meta_pages(struct address_space *mapping,
362127e670aSJaegeuk Kim 				struct writeback_control *wbc)
363127e670aSJaegeuk Kim {
3644081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
36550c8cdb3SJaegeuk Kim 	long diff, written;
366127e670aSJaegeuk Kim 
3670771fcc7SChao Yu 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
3680771fcc7SChao Yu 		goto skip_write;
3690771fcc7SChao Yu 
3705459aa97SJaegeuk Kim 	/* collect a number of dirty meta pages and write together */
371812a9597SJaegeuk Kim 	if (wbc->sync_mode != WB_SYNC_ALL &&
372812a9597SJaegeuk Kim 			get_pages(sbi, F2FS_DIRTY_META) <
373812a9597SJaegeuk Kim 					nr_pages_to_skip(sbi, META))
374d3baf95dSJaegeuk Kim 		goto skip_write;
375127e670aSJaegeuk Kim 
376a29d0e0bSYunlei He 	/* if locked failed, cp will flush dirty pages instead */
377e4544b63STim Murray 	if (!f2fs_down_write_trylock(&sbi->cp_global_sem))
378a29d0e0bSYunlei He 		goto skip_write;
379d31c7c3fSYunlei He 
380a29d0e0bSYunlei He 	trace_f2fs_writepages(mapping->host, wbc, META);
38150c8cdb3SJaegeuk Kim 	diff = nr_pages_to_write(sbi, META, wbc);
3824d57b86dSChao Yu 	written = f2fs_sync_meta_pages(sbi, META, wbc->nr_to_write, FS_META_IO);
383e4544b63STim Murray 	f2fs_up_write(&sbi->cp_global_sem);
38450c8cdb3SJaegeuk Kim 	wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff);
385127e670aSJaegeuk Kim 	return 0;
386d3baf95dSJaegeuk Kim 
387d3baf95dSJaegeuk Kim skip_write:
388d3baf95dSJaegeuk Kim 	wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META);
389d31c7c3fSYunlei He 	trace_f2fs_writepages(mapping->host, wbc, META);
390d3baf95dSJaegeuk Kim 	return 0;
391127e670aSJaegeuk Kim }
392127e670aSJaegeuk Kim 
3934d57b86dSChao Yu long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
394b0af6d49SChao Yu 				long nr_to_write, enum iostat_type io_type)
395127e670aSJaegeuk Kim {
3969df27d98SGu Zheng 	struct address_space *mapping = META_MAPPING(sbi);
397028a63a6SJan Kara 	pgoff_t index = 0, prev = ULONG_MAX;
398127e670aSJaegeuk Kim 	struct pagevec pvec;
399127e670aSJaegeuk Kim 	long nwritten = 0;
400028a63a6SJan Kara 	int nr_pages;
401127e670aSJaegeuk Kim 	struct writeback_control wbc = {
402127e670aSJaegeuk Kim 		.for_reclaim = 0,
403127e670aSJaegeuk Kim 	};
404e9f5b8b8SChao Yu 	struct blk_plug plug;
405127e670aSJaegeuk Kim 
40686679820SMel Gorman 	pagevec_init(&pvec);
407127e670aSJaegeuk Kim 
408e9f5b8b8SChao Yu 	blk_start_plug(&plug);
409e9f5b8b8SChao Yu 
410028a63a6SJan Kara 	while ((nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
41167fd707fSJan Kara 				PAGECACHE_TAG_DIRTY))) {
412028a63a6SJan Kara 		int i;
413127e670aSJaegeuk Kim 
414127e670aSJaegeuk Kim 		for (i = 0; i < nr_pages; i++) {
415127e670aSJaegeuk Kim 			struct page *page = pvec.pages[i];
416203681f6SJaegeuk Kim 
41780dd9c0eSChao Yu 			if (prev == ULONG_MAX)
4186066d8cdSJaegeuk Kim 				prev = page->index - 1;
4196066d8cdSJaegeuk Kim 			if (nr_to_write != LONG_MAX && page->index != prev + 1) {
4206066d8cdSJaegeuk Kim 				pagevec_release(&pvec);
4216066d8cdSJaegeuk Kim 				goto stop;
4226066d8cdSJaegeuk Kim 			}
4236066d8cdSJaegeuk Kim 
424127e670aSJaegeuk Kim 			lock_page(page);
425203681f6SJaegeuk Kim 
426203681f6SJaegeuk Kim 			if (unlikely(page->mapping != mapping)) {
427203681f6SJaegeuk Kim continue_unlock:
428203681f6SJaegeuk Kim 				unlock_page(page);
429203681f6SJaegeuk Kim 				continue;
430203681f6SJaegeuk Kim 			}
431203681f6SJaegeuk Kim 			if (!PageDirty(page)) {
432203681f6SJaegeuk Kim 				/* someone wrote it for us */
433203681f6SJaegeuk Kim 				goto continue_unlock;
434203681f6SJaegeuk Kim 			}
435203681f6SJaegeuk Kim 
436bae0ee7aSChao Yu 			f2fs_wait_on_page_writeback(page, META, true, true);
437fa3d2bdfSJaegeuk Kim 
438203681f6SJaegeuk Kim 			if (!clear_page_dirty_for_io(page))
439203681f6SJaegeuk Kim 				goto continue_unlock;
440203681f6SJaegeuk Kim 
441b0af6d49SChao Yu 			if (__f2fs_write_meta_page(page, &wbc, io_type)) {
442577e3495SJaegeuk Kim 				unlock_page(page);
443577e3495SJaegeuk Kim 				break;
444577e3495SJaegeuk Kim 			}
445cfb271d4SChao Yu 			nwritten++;
4466066d8cdSJaegeuk Kim 			prev = page->index;
447cfb271d4SChao Yu 			if (unlikely(nwritten >= nr_to_write))
448127e670aSJaegeuk Kim 				break;
449127e670aSJaegeuk Kim 		}
450127e670aSJaegeuk Kim 		pagevec_release(&pvec);
451127e670aSJaegeuk Kim 		cond_resched();
452127e670aSJaegeuk Kim 	}
4536066d8cdSJaegeuk Kim stop:
454127e670aSJaegeuk Kim 	if (nwritten)
455b9109b0eSJaegeuk Kim 		f2fs_submit_merged_write(sbi, type);
456127e670aSJaegeuk Kim 
457e9f5b8b8SChao Yu 	blk_finish_plug(&plug);
458e9f5b8b8SChao Yu 
459127e670aSJaegeuk Kim 	return nwritten;
460127e670aSJaegeuk Kim }
461127e670aSJaegeuk Kim 
4621d9ac659SMatthew Wilcox (Oracle) static bool f2fs_dirty_meta_folio(struct address_space *mapping,
4631d9ac659SMatthew Wilcox (Oracle) 		struct folio *folio)
464127e670aSJaegeuk Kim {
4651d9ac659SMatthew Wilcox (Oracle) 	trace_f2fs_set_page_dirty(&folio->page, META);
46626c6b887SJaegeuk Kim 
4671d9ac659SMatthew Wilcox (Oracle) 	if (!folio_test_uptodate(folio))
4681d9ac659SMatthew Wilcox (Oracle) 		folio_mark_uptodate(folio);
4699b7eadd9SShuqi Zhang 	if (filemap_dirty_folio(mapping, folio)) {
47029c87793SMatthew Wilcox (Oracle) 		inc_page_count(F2FS_M_SB(mapping), F2FS_DIRTY_META);
4711d9ac659SMatthew Wilcox (Oracle) 		set_page_private_reference(&folio->page);
4721d9ac659SMatthew Wilcox (Oracle) 		return true;
473127e670aSJaegeuk Kim 	}
4741d9ac659SMatthew Wilcox (Oracle) 	return false;
475127e670aSJaegeuk Kim }
476127e670aSJaegeuk Kim 
477127e670aSJaegeuk Kim const struct address_space_operations f2fs_meta_aops = {
478127e670aSJaegeuk Kim 	.writepage	= f2fs_write_meta_page,
479127e670aSJaegeuk Kim 	.writepages	= f2fs_write_meta_pages,
4801d9ac659SMatthew Wilcox (Oracle) 	.dirty_folio	= f2fs_dirty_meta_folio,
48191503996SMatthew Wilcox (Oracle) 	.invalidate_folio = f2fs_invalidate_folio,
482c26cd045SMatthew Wilcox (Oracle) 	.release_folio	= f2fs_release_folio,
4831d5b9bd6SMatthew Wilcox (Oracle) 	.migrate_folio	= filemap_migrate_folio,
484127e670aSJaegeuk Kim };
485127e670aSJaegeuk Kim 
48639d787beSChao Yu static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino,
48739d787beSChao Yu 						unsigned int devidx, int type)
488953e6cc6SJaegeuk Kim {
48967298804SChao Yu 	struct inode_management *im = &sbi->im[type];
4904b106518SChao Yu 	struct ino_entry *e = NULL, *new = NULL;
49180c54505SJaegeuk Kim 
4924b106518SChao Yu 	if (type == FLUSH_INO) {
4934b106518SChao Yu 		rcu_read_lock();
4944b106518SChao Yu 		e = radix_tree_lookup(&im->ino_root, ino);
4954b106518SChao Yu 		rcu_read_unlock();
4964b106518SChao Yu 	}
4974b106518SChao Yu 
4984b106518SChao Yu retry:
4994b106518SChao Yu 	if (!e)
50032410577SChao Yu 		new = f2fs_kmem_cache_alloc(ino_entry_slab,
50132410577SChao Yu 						GFP_NOFS, true, NULL);
50219526d74SChao Yu 
50380c54505SJaegeuk Kim 	radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
504769ec6e5SJaegeuk Kim 
50567298804SChao Yu 	spin_lock(&im->ino_lock);
50667298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
50739efac41SJaegeuk Kim 	if (!e) {
5084b106518SChao Yu 		if (!new) {
5094b106518SChao Yu 			spin_unlock(&im->ino_lock);
5104b106518SChao Yu 			goto retry;
5114b106518SChao Yu 		}
5124b106518SChao Yu 		e = new;
51319526d74SChao Yu 		if (unlikely(radix_tree_insert(&im->ino_root, ino, e)))
51419526d74SChao Yu 			f2fs_bug_on(sbi, 1);
51519526d74SChao Yu 
51639efac41SJaegeuk Kim 		memset(e, 0, sizeof(struct ino_entry));
51739efac41SJaegeuk Kim 		e->ino = ino;
51839efac41SJaegeuk Kim 
51967298804SChao Yu 		list_add_tail(&e->list, &im->ino_list);
5208c402946SJaegeuk Kim 		if (type != ORPHAN_INO)
52167298804SChao Yu 			im->ino_num++;
52239efac41SJaegeuk Kim 	}
52339d787beSChao Yu 
52439d787beSChao Yu 	if (type == FLUSH_INO)
52539d787beSChao Yu 		f2fs_set_bit(devidx, (char *)&e->dirty_device);
52639d787beSChao Yu 
52767298804SChao Yu 	spin_unlock(&im->ino_lock);
528769ec6e5SJaegeuk Kim 	radix_tree_preload_end();
52980c54505SJaegeuk Kim 
5304b106518SChao Yu 	if (new && e != new)
5314b106518SChao Yu 		kmem_cache_free(ino_entry_slab, new);
532953e6cc6SJaegeuk Kim }
533953e6cc6SJaegeuk Kim 
5346451e041SJaegeuk Kim static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
535953e6cc6SJaegeuk Kim {
53667298804SChao Yu 	struct inode_management *im = &sbi->im[type];
5376451e041SJaegeuk Kim 	struct ino_entry *e;
538953e6cc6SJaegeuk Kim 
53967298804SChao Yu 	spin_lock(&im->ino_lock);
54067298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
54139efac41SJaegeuk Kim 	if (e) {
542953e6cc6SJaegeuk Kim 		list_del(&e->list);
54367298804SChao Yu 		radix_tree_delete(&im->ino_root, ino);
54467298804SChao Yu 		im->ino_num--;
54567298804SChao Yu 		spin_unlock(&im->ino_lock);
5466451e041SJaegeuk Kim 		kmem_cache_free(ino_entry_slab, e);
547953e6cc6SJaegeuk Kim 		return;
548953e6cc6SJaegeuk Kim 	}
54967298804SChao Yu 	spin_unlock(&im->ino_lock);
550953e6cc6SJaegeuk Kim }
551953e6cc6SJaegeuk Kim 
5524d57b86dSChao Yu void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
553fff04f90SJaegeuk Kim {
554fff04f90SJaegeuk Kim 	/* add new dirty ino entry into list */
55539d787beSChao Yu 	__add_ino_entry(sbi, ino, 0, type);
556fff04f90SJaegeuk Kim }
557fff04f90SJaegeuk Kim 
5584d57b86dSChao Yu void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
559fff04f90SJaegeuk Kim {
560fff04f90SJaegeuk Kim 	/* remove dirty ino entry from list */
561fff04f90SJaegeuk Kim 	__remove_ino_entry(sbi, ino, type);
562fff04f90SJaegeuk Kim }
563fff04f90SJaegeuk Kim 
5641f07cc58SJack Qiu /* mode should be APPEND_INO, UPDATE_INO or TRANS_DIR_INO */
5654d57b86dSChao Yu bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode)
566fff04f90SJaegeuk Kim {
56767298804SChao Yu 	struct inode_management *im = &sbi->im[mode];
568fff04f90SJaegeuk Kim 	struct ino_entry *e;
56967298804SChao Yu 
57067298804SChao Yu 	spin_lock(&im->ino_lock);
57167298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
57267298804SChao Yu 	spin_unlock(&im->ino_lock);
573fff04f90SJaegeuk Kim 	return e ? true : false;
574fff04f90SJaegeuk Kim }
575fff04f90SJaegeuk Kim 
5764d57b86dSChao Yu void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all)
577fff04f90SJaegeuk Kim {
578fff04f90SJaegeuk Kim 	struct ino_entry *e, *tmp;
579fff04f90SJaegeuk Kim 	int i;
580fff04f90SJaegeuk Kim 
58139d787beSChao Yu 	for (i = all ? ORPHAN_INO : APPEND_INO; i < MAX_INO_ENTRY; i++) {
58267298804SChao Yu 		struct inode_management *im = &sbi->im[i];
58367298804SChao Yu 
58467298804SChao Yu 		spin_lock(&im->ino_lock);
58567298804SChao Yu 		list_for_each_entry_safe(e, tmp, &im->ino_list, list) {
586fff04f90SJaegeuk Kim 			list_del(&e->list);
58767298804SChao Yu 			radix_tree_delete(&im->ino_root, e->ino);
588fff04f90SJaegeuk Kim 			kmem_cache_free(ino_entry_slab, e);
58967298804SChao Yu 			im->ino_num--;
590fff04f90SJaegeuk Kim 		}
59167298804SChao Yu 		spin_unlock(&im->ino_lock);
592fff04f90SJaegeuk Kim 	}
593fff04f90SJaegeuk Kim }
594fff04f90SJaegeuk Kim 
5954d57b86dSChao Yu void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
59639d787beSChao Yu 					unsigned int devidx, int type)
59739d787beSChao Yu {
59839d787beSChao Yu 	__add_ino_entry(sbi, ino, devidx, type);
59939d787beSChao Yu }
60039d787beSChao Yu 
6014d57b86dSChao Yu bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
60239d787beSChao Yu 					unsigned int devidx, int type)
60339d787beSChao Yu {
60439d787beSChao Yu 	struct inode_management *im = &sbi->im[type];
60539d787beSChao Yu 	struct ino_entry *e;
60639d787beSChao Yu 	bool is_dirty = false;
60739d787beSChao Yu 
60839d787beSChao Yu 	spin_lock(&im->ino_lock);
60939d787beSChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
61039d787beSChao Yu 	if (e && f2fs_test_bit(devidx, (char *)&e->dirty_device))
61139d787beSChao Yu 		is_dirty = true;
61239d787beSChao Yu 	spin_unlock(&im->ino_lock);
61339d787beSChao Yu 	return is_dirty;
61439d787beSChao Yu }
61539d787beSChao Yu 
6164d57b86dSChao Yu int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi)
617127e670aSJaegeuk Kim {
61867298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
619127e670aSJaegeuk Kim 	int err = 0;
620127e670aSJaegeuk Kim 
62167298804SChao Yu 	spin_lock(&im->ino_lock);
622cb78942bSJaegeuk Kim 
6231ecc0c5cSChao Yu 	if (time_to_inject(sbi, FAULT_ORPHAN)) {
624cb78942bSJaegeuk Kim 		spin_unlock(&im->ino_lock);
625c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_ORPHAN);
626cb78942bSJaegeuk Kim 		return -ENOSPC;
627cb78942bSJaegeuk Kim 	}
6287fa750a1SArnd Bergmann 
62967298804SChao Yu 	if (unlikely(im->ino_num >= sbi->max_orphans))
630127e670aSJaegeuk Kim 		err = -ENOSPC;
631cbd56e7dSJaegeuk Kim 	else
63267298804SChao Yu 		im->ino_num++;
63367298804SChao Yu 	spin_unlock(&im->ino_lock);
6340d47c1adSGu Zheng 
635127e670aSJaegeuk Kim 	return err;
636127e670aSJaegeuk Kim }
637127e670aSJaegeuk Kim 
6384d57b86dSChao Yu void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi)
639cbd56e7dSJaegeuk Kim {
64067298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
64167298804SChao Yu 
64267298804SChao Yu 	spin_lock(&im->ino_lock);
64367298804SChao Yu 	f2fs_bug_on(sbi, im->ino_num == 0);
64467298804SChao Yu 	im->ino_num--;
64567298804SChao Yu 	spin_unlock(&im->ino_lock);
646cbd56e7dSJaegeuk Kim }
647cbd56e7dSJaegeuk Kim 
6484d57b86dSChao Yu void f2fs_add_orphan_inode(struct inode *inode)
649127e670aSJaegeuk Kim {
65039efac41SJaegeuk Kim 	/* add new orphan ino entry into list */
65139d787beSChao Yu 	__add_ino_entry(F2FS_I_SB(inode), inode->i_ino, 0, ORPHAN_INO);
6524d57b86dSChao Yu 	f2fs_update_inode_page(inode);
653127e670aSJaegeuk Kim }
654127e670aSJaegeuk Kim 
6554d57b86dSChao Yu void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
656127e670aSJaegeuk Kim {
657953e6cc6SJaegeuk Kim 	/* remove orphan entry from orphan list */
6586451e041SJaegeuk Kim 	__remove_ino_entry(sbi, ino, ORPHAN_INO);
659127e670aSJaegeuk Kim }
660127e670aSJaegeuk Kim 
6618c14bfadSChao Yu static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
662127e670aSJaegeuk Kim {
6638c14bfadSChao Yu 	struct inode *inode;
6645905f9afSJaegeuk Kim 	struct node_info ni;
66576a45e3cSChao Yu 	int err;
6668c14bfadSChao Yu 
6675905f9afSJaegeuk Kim 	inode = f2fs_iget_retry(sbi->sb, ino);
6688c14bfadSChao Yu 	if (IS_ERR(inode)) {
6698c14bfadSChao Yu 		/*
6708c14bfadSChao Yu 		 * there should be a bug that we can't find the entry
6718c14bfadSChao Yu 		 * to orphan inode.
6728c14bfadSChao Yu 		 */
6738c14bfadSChao Yu 		f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT);
6748c14bfadSChao Yu 		return PTR_ERR(inode);
6758c14bfadSChao Yu 	}
6768c14bfadSChao Yu 
67710a26878SChao Yu 	err = f2fs_dquot_initialize(inode);
678a515d12fSSheng Yong 	if (err) {
679a515d12fSSheng Yong 		iput(inode);
6800f9ec2a8SJaegeuk Kim 		goto err_out;
681a515d12fSSheng Yong 	}
6820f9ec2a8SJaegeuk Kim 
683127e670aSJaegeuk Kim 	clear_nlink(inode);
684127e670aSJaegeuk Kim 
685127e670aSJaegeuk Kim 	/* truncate all the data during iput */
686127e670aSJaegeuk Kim 	iput(inode);
6875905f9afSJaegeuk Kim 
688a9419b63SJaegeuk Kim 	err = f2fs_get_node_info(sbi, ino, &ni, false);
6897735730dSChao Yu 	if (err)
6907735730dSChao Yu 		goto err_out;
6915905f9afSJaegeuk Kim 
6925905f9afSJaegeuk Kim 	/* ENOMEM was fully retried in f2fs_evict_inode. */
6935905f9afSJaegeuk Kim 	if (ni.blk_addr != NULL_ADDR) {
6940f9ec2a8SJaegeuk Kim 		err = -EIO;
6950f9ec2a8SJaegeuk Kim 		goto err_out;
6965905f9afSJaegeuk Kim 	}
6978c14bfadSChao Yu 	return 0;
6980f9ec2a8SJaegeuk Kim 
6990f9ec2a8SJaegeuk Kim err_out:
7000f9ec2a8SJaegeuk Kim 	set_sbi_flag(sbi, SBI_NEED_FSCK);
701dcbb4c10SJoe Perches 	f2fs_warn(sbi, "%s: orphan failed (ino=%x), run fsck to fix.",
7020f9ec2a8SJaegeuk Kim 		  __func__, ino);
7030f9ec2a8SJaegeuk Kim 	return err;
704127e670aSJaegeuk Kim }
705127e670aSJaegeuk Kim 
7064d57b86dSChao Yu int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
707127e670aSJaegeuk Kim {
7083c642985SWanpeng Li 	block_t start_blk, orphan_blocks, i, j;
7094b2414d0SChao Yu 	unsigned int s_flags = sbi->sb->s_flags;
7104b2414d0SChao Yu 	int err = 0;
711ea676733SJaegeuk Kim #ifdef CONFIG_QUOTA
712ea676733SJaegeuk Kim 	int quota_enabled;
713ea676733SJaegeuk Kim #endif
714127e670aSJaegeuk Kim 
715aaec2b1dSChao Yu 	if (!is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG))
7168c14bfadSChao Yu 		return 0;
717127e670aSJaegeuk Kim 
718b61af314SChao Yu 	if (bdev_read_only(sbi->sb->s_bdev)) {
719dcbb4c10SJoe Perches 		f2fs_info(sbi, "write access unavailable, skipping orphan cleanup");
720b61af314SChao Yu 		return 0;
721b61af314SChao Yu 	}
722b61af314SChao Yu 
7231751e8a6SLinus Torvalds 	if (s_flags & SB_RDONLY) {
724dcbb4c10SJoe Perches 		f2fs_info(sbi, "orphan cleanup on readonly fs");
7251751e8a6SLinus Torvalds 		sbi->sb->s_flags &= ~SB_RDONLY;
7264b2414d0SChao Yu 	}
7274b2414d0SChao Yu 
7284b2414d0SChao Yu #ifdef CONFIG_QUOTA
72976cf05d7SSheng Yong 	/*
73076cf05d7SSheng Yong 	 * Turn on quotas which were not enabled for read-only mounts if
73176cf05d7SSheng Yong 	 * filesystem has quota feature, so that they are updated correctly.
73276cf05d7SSheng Yong 	 */
7331751e8a6SLinus Torvalds 	quota_enabled = f2fs_enable_quota_files(sbi, s_flags & SB_RDONLY);
7344b2414d0SChao Yu #endif
7354b2414d0SChao Yu 
73655141486SWanpeng Li 	start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
7373c642985SWanpeng Li 	orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
738127e670aSJaegeuk Kim 
7394d57b86dSChao Yu 	f2fs_ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true);
740662befdaSChao Yu 
7413c642985SWanpeng Li 	for (i = 0; i < orphan_blocks; i++) {
7427735730dSChao Yu 		struct page *page;
743127e670aSJaegeuk Kim 		struct f2fs_orphan_block *orphan_blk;
744127e670aSJaegeuk Kim 
7457735730dSChao Yu 		page = f2fs_get_meta_page(sbi, start_blk + i);
7467735730dSChao Yu 		if (IS_ERR(page)) {
7477735730dSChao Yu 			err = PTR_ERR(page);
7487735730dSChao Yu 			goto out;
7497735730dSChao Yu 		}
7507735730dSChao Yu 
751127e670aSJaegeuk Kim 		orphan_blk = (struct f2fs_orphan_block *)page_address(page);
752127e670aSJaegeuk Kim 		for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
753127e670aSJaegeuk Kim 			nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
7545f029c04SYi Zhuang 
7558c14bfadSChao Yu 			err = recover_orphan_inode(sbi, ino);
7568c14bfadSChao Yu 			if (err) {
7578c14bfadSChao Yu 				f2fs_put_page(page, 1);
7584b2414d0SChao Yu 				goto out;
7598c14bfadSChao Yu 			}
760127e670aSJaegeuk Kim 		}
761127e670aSJaegeuk Kim 		f2fs_put_page(page, 1);
762127e670aSJaegeuk Kim 	}
763127e670aSJaegeuk Kim 	/* clear Orphan Flag */
764aaec2b1dSChao Yu 	clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
7654b2414d0SChao Yu out:
7661378752bSChao Yu 	set_sbi_flag(sbi, SBI_IS_RECOVERED);
7671378752bSChao Yu 
7684b2414d0SChao Yu #ifdef CONFIG_QUOTA
7694b2414d0SChao Yu 	/* Turn quotas off */
770ea676733SJaegeuk Kim 	if (quota_enabled)
7714b2414d0SChao Yu 		f2fs_quota_off_umount(sbi->sb);
7724b2414d0SChao Yu #endif
7731751e8a6SLinus Torvalds 	sbi->sb->s_flags = s_flags; /* Restore SB_RDONLY status */
7744b2414d0SChao Yu 
7754b2414d0SChao Yu 	return err;
776127e670aSJaegeuk Kim }
777127e670aSJaegeuk Kim 
778127e670aSJaegeuk Kim static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
779127e670aSJaegeuk Kim {
780502c6e0bSGu Zheng 	struct list_head *head;
781127e670aSJaegeuk Kim 	struct f2fs_orphan_block *orphan_blk = NULL;
782127e670aSJaegeuk Kim 	unsigned int nentries = 0;
783bd936f84SChao Yu 	unsigned short index = 1;
7848c402946SJaegeuk Kim 	unsigned short orphan_blocks;
7854531929eSGu Zheng 	struct page *page = NULL;
7866451e041SJaegeuk Kim 	struct ino_entry *orphan = NULL;
78767298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
788127e670aSJaegeuk Kim 
78967298804SChao Yu 	orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num);
7908c402946SJaegeuk Kim 
791d6c67a4fSJaegeuk Kim 	/*
792d6c67a4fSJaegeuk Kim 	 * we don't need to do spin_lock(&im->ino_lock) here, since all the
793d6c67a4fSJaegeuk Kim 	 * orphan inode operations are covered under f2fs_lock_op().
794d6c67a4fSJaegeuk Kim 	 * And, spin_lock should be avoided due to page operations below.
795d6c67a4fSJaegeuk Kim 	 */
79667298804SChao Yu 	head = &im->ino_list;
797127e670aSJaegeuk Kim 
798127e670aSJaegeuk Kim 	/* loop for each orphan inode entry and write them in Jornal block */
799502c6e0bSGu Zheng 	list_for_each_entry(orphan, head, list) {
800502c6e0bSGu Zheng 		if (!page) {
8014d57b86dSChao Yu 			page = f2fs_grab_meta_page(sbi, start_blk++);
802502c6e0bSGu Zheng 			orphan_blk =
803502c6e0bSGu Zheng 				(struct f2fs_orphan_block *)page_address(page);
804502c6e0bSGu Zheng 			memset(orphan_blk, 0, sizeof(*orphan_blk));
805502c6e0bSGu Zheng 		}
806127e670aSJaegeuk Kim 
80736795567SGu Zheng 		orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
808127e670aSJaegeuk Kim 
80936795567SGu Zheng 		if (nentries == F2FS_ORPHANS_PER_BLOCK) {
810127e670aSJaegeuk Kim 			/*
811127e670aSJaegeuk Kim 			 * an orphan block is full of 1020 entries,
812127e670aSJaegeuk Kim 			 * then we need to flush current orphan blocks
813127e670aSJaegeuk Kim 			 * and bring another one in memory
814127e670aSJaegeuk Kim 			 */
815127e670aSJaegeuk Kim 			orphan_blk->blk_addr = cpu_to_le16(index);
816127e670aSJaegeuk Kim 			orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
817127e670aSJaegeuk Kim 			orphan_blk->entry_count = cpu_to_le32(nentries);
818127e670aSJaegeuk Kim 			set_page_dirty(page);
819127e670aSJaegeuk Kim 			f2fs_put_page(page, 1);
820127e670aSJaegeuk Kim 			index++;
821127e670aSJaegeuk Kim 			nentries = 0;
822127e670aSJaegeuk Kim 			page = NULL;
823127e670aSJaegeuk Kim 		}
824127e670aSJaegeuk Kim 	}
825127e670aSJaegeuk Kim 
826502c6e0bSGu Zheng 	if (page) {
827127e670aSJaegeuk Kim 		orphan_blk->blk_addr = cpu_to_le16(index);
828127e670aSJaegeuk Kim 		orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
829127e670aSJaegeuk Kim 		orphan_blk->entry_count = cpu_to_le32(nentries);
830127e670aSJaegeuk Kim 		set_page_dirty(page);
831127e670aSJaegeuk Kim 		f2fs_put_page(page, 1);
832502c6e0bSGu Zheng 	}
833127e670aSJaegeuk Kim }
834127e670aSJaegeuk Kim 
835d7eb8f1cSChao Yu static __u32 f2fs_checkpoint_chksum(struct f2fs_sb_info *sbi,
836d7eb8f1cSChao Yu 						struct f2fs_checkpoint *ckpt)
837d7eb8f1cSChao Yu {
838d7eb8f1cSChao Yu 	unsigned int chksum_ofs = le32_to_cpu(ckpt->checksum_offset);
839d7eb8f1cSChao Yu 	__u32 chksum;
840d7eb8f1cSChao Yu 
841d7eb8f1cSChao Yu 	chksum = f2fs_crc32(sbi, ckpt, chksum_ofs);
842d7eb8f1cSChao Yu 	if (chksum_ofs < CP_CHKSUM_OFFSET) {
843d7eb8f1cSChao Yu 		chksum_ofs += sizeof(chksum);
844d7eb8f1cSChao Yu 		chksum = f2fs_chksum(sbi, chksum, (__u8 *)ckpt + chksum_ofs,
845d7eb8f1cSChao Yu 						F2FS_BLKSIZE - chksum_ofs);
846d7eb8f1cSChao Yu 	}
847d7eb8f1cSChao Yu 	return chksum;
848d7eb8f1cSChao Yu }
849d7eb8f1cSChao Yu 
850fc0065adSTiezhu Yang static int get_checkpoint_version(struct f2fs_sb_info *sbi, block_t cp_addr,
851fc0065adSTiezhu Yang 		struct f2fs_checkpoint **cp_block, struct page **cp_page,
852fc0065adSTiezhu Yang 		unsigned long long *version)
853fc0065adSTiezhu Yang {
854fc0065adSTiezhu Yang 	size_t crc_offset = 0;
855d7eb8f1cSChao Yu 	__u32 crc;
856fc0065adSTiezhu Yang 
8574d57b86dSChao Yu 	*cp_page = f2fs_get_meta_page(sbi, cp_addr);
8587735730dSChao Yu 	if (IS_ERR(*cp_page))
8597735730dSChao Yu 		return PTR_ERR(*cp_page);
8607735730dSChao Yu 
861fc0065adSTiezhu Yang 	*cp_block = (struct f2fs_checkpoint *)page_address(*cp_page);
862fc0065adSTiezhu Yang 
863fc0065adSTiezhu Yang 	crc_offset = le32_to_cpu((*cp_block)->checksum_offset);
864d7eb8f1cSChao Yu 	if (crc_offset < CP_MIN_CHKSUM_OFFSET ||
865d7eb8f1cSChao Yu 			crc_offset > CP_CHKSUM_OFFSET) {
866d3f07c04SChao Yu 		f2fs_put_page(*cp_page, 1);
867dcbb4c10SJoe Perches 		f2fs_warn(sbi, "invalid crc_offset: %zu", crc_offset);
868fc0065adSTiezhu Yang 		return -EINVAL;
869fc0065adSTiezhu Yang 	}
870fc0065adSTiezhu Yang 
871d7eb8f1cSChao Yu 	crc = f2fs_checkpoint_chksum(sbi, *cp_block);
872d7eb8f1cSChao Yu 	if (crc != cur_cp_crc(*cp_block)) {
873d3f07c04SChao Yu 		f2fs_put_page(*cp_page, 1);
874dcbb4c10SJoe Perches 		f2fs_warn(sbi, "invalid crc value");
875fc0065adSTiezhu Yang 		return -EINVAL;
876fc0065adSTiezhu Yang 	}
877fc0065adSTiezhu Yang 
878fc0065adSTiezhu Yang 	*version = cur_cp_version(*cp_block);
879fc0065adSTiezhu Yang 	return 0;
880fc0065adSTiezhu Yang }
881fc0065adSTiezhu Yang 
882127e670aSJaegeuk Kim static struct page *validate_checkpoint(struct f2fs_sb_info *sbi,
883127e670aSJaegeuk Kim 				block_t cp_addr, unsigned long long *version)
884127e670aSJaegeuk Kim {
885fc0065adSTiezhu Yang 	struct page *cp_page_1 = NULL, *cp_page_2 = NULL;
886fc0065adSTiezhu Yang 	struct f2fs_checkpoint *cp_block = NULL;
887127e670aSJaegeuk Kim 	unsigned long long cur_version = 0, pre_version = 0;
8885b5b4f85SChao Yu 	unsigned int cp_blocks;
889fc0065adSTiezhu Yang 	int err;
890127e670aSJaegeuk Kim 
891fc0065adSTiezhu Yang 	err = get_checkpoint_version(sbi, cp_addr, &cp_block,
892fc0065adSTiezhu Yang 					&cp_page_1, version);
893fc0065adSTiezhu Yang 	if (err)
894d3f07c04SChao Yu 		return NULL;
895c9b60788SChao Yu 
8965b5b4f85SChao Yu 	cp_blocks = le32_to_cpu(cp_block->cp_pack_total_block_count);
8975b5b4f85SChao Yu 
8985b5b4f85SChao Yu 	if (cp_blocks > sbi->blocks_per_seg || cp_blocks <= F2FS_CP_PACKS) {
899dcbb4c10SJoe Perches 		f2fs_warn(sbi, "invalid cp_pack_total_block_count:%u",
900c9b60788SChao Yu 			  le32_to_cpu(cp_block->cp_pack_total_block_count));
901d3f07c04SChao Yu 		goto invalid_cp;
902c9b60788SChao Yu 	}
903fc0065adSTiezhu Yang 	pre_version = *version;
904127e670aSJaegeuk Kim 
9055b5b4f85SChao Yu 	cp_addr += cp_blocks - 1;
906fc0065adSTiezhu Yang 	err = get_checkpoint_version(sbi, cp_addr, &cp_block,
907fc0065adSTiezhu Yang 					&cp_page_2, version);
908fc0065adSTiezhu Yang 	if (err)
909d3f07c04SChao Yu 		goto invalid_cp;
910fc0065adSTiezhu Yang 	cur_version = *version;
911127e670aSJaegeuk Kim 
912127e670aSJaegeuk Kim 	if (cur_version == pre_version) {
913127e670aSJaegeuk Kim 		*version = cur_version;
914127e670aSJaegeuk Kim 		f2fs_put_page(cp_page_2, 1);
915127e670aSJaegeuk Kim 		return cp_page_1;
916127e670aSJaegeuk Kim 	}
917127e670aSJaegeuk Kim 	f2fs_put_page(cp_page_2, 1);
918d3f07c04SChao Yu invalid_cp:
919127e670aSJaegeuk Kim 	f2fs_put_page(cp_page_1, 1);
920127e670aSJaegeuk Kim 	return NULL;
921127e670aSJaegeuk Kim }
922127e670aSJaegeuk Kim 
9234d57b86dSChao Yu int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi)
924127e670aSJaegeuk Kim {
925127e670aSJaegeuk Kim 	struct f2fs_checkpoint *cp_block;
926127e670aSJaegeuk Kim 	struct f2fs_super_block *fsb = sbi->raw_super;
927127e670aSJaegeuk Kim 	struct page *cp1, *cp2, *cur_page;
928127e670aSJaegeuk Kim 	unsigned long blk_size = sbi->blocksize;
929127e670aSJaegeuk Kim 	unsigned long long cp1_version = 0, cp2_version = 0;
930127e670aSJaegeuk Kim 	unsigned long long cp_start_blk_no;
93155141486SWanpeng Li 	unsigned int cp_blks = 1 + __cp_payload(sbi);
9321dbe4152SChangman Lee 	block_t cp_blk_no;
9331dbe4152SChangman Lee 	int i;
93410f966bbSChao Yu 	int err;
935127e670aSJaegeuk Kim 
9360b6d4ca0SEric Biggers 	sbi->ckpt = f2fs_kvzalloc(sbi, array_size(blk_size, cp_blks),
937026f0507SKees Cook 				  GFP_KERNEL);
938127e670aSJaegeuk Kim 	if (!sbi->ckpt)
939127e670aSJaegeuk Kim 		return -ENOMEM;
940127e670aSJaegeuk Kim 	/*
941127e670aSJaegeuk Kim 	 * Finding out valid cp block involves read both
942127e670aSJaegeuk Kim 	 * sets( cp pack 1 and cp pack 2)
943127e670aSJaegeuk Kim 	 */
944127e670aSJaegeuk Kim 	cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr);
945127e670aSJaegeuk Kim 	cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
946127e670aSJaegeuk Kim 
947127e670aSJaegeuk Kim 	/* The second checkpoint pack should start at the next segment */
948f9a4e6dfSJaegeuk Kim 	cp_start_blk_no += ((unsigned long long)1) <<
949f9a4e6dfSJaegeuk Kim 				le32_to_cpu(fsb->log_blocks_per_seg);
950127e670aSJaegeuk Kim 	cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
951127e670aSJaegeuk Kim 
952127e670aSJaegeuk Kim 	if (cp1 && cp2) {
953127e670aSJaegeuk Kim 		if (ver_after(cp2_version, cp1_version))
954127e670aSJaegeuk Kim 			cur_page = cp2;
955127e670aSJaegeuk Kim 		else
956127e670aSJaegeuk Kim 			cur_page = cp1;
957127e670aSJaegeuk Kim 	} else if (cp1) {
958127e670aSJaegeuk Kim 		cur_page = cp1;
959127e670aSJaegeuk Kim 	} else if (cp2) {
960127e670aSJaegeuk Kim 		cur_page = cp2;
961127e670aSJaegeuk Kim 	} else {
96210f966bbSChao Yu 		err = -EFSCORRUPTED;
963127e670aSJaegeuk Kim 		goto fail_no_cp;
964127e670aSJaegeuk Kim 	}
965127e670aSJaegeuk Kim 
966127e670aSJaegeuk Kim 	cp_block = (struct f2fs_checkpoint *)page_address(cur_page);
967127e670aSJaegeuk Kim 	memcpy(sbi->ckpt, cp_block, blk_size);
968127e670aSJaegeuk Kim 
9698508e44aSJaegeuk Kim 	if (cur_page == cp1)
9708508e44aSJaegeuk Kim 		sbi->cur_cp_pack = 1;
9718508e44aSJaegeuk Kim 	else
9728508e44aSJaegeuk Kim 		sbi->cur_cp_pack = 2;
973984ec63cSShawn Lin 
974e494c2f9SChao Yu 	/* Sanity checking of checkpoint */
97510f966bbSChao Yu 	if (f2fs_sanity_check_ckpt(sbi)) {
97610f966bbSChao Yu 		err = -EFSCORRUPTED;
977e494c2f9SChao Yu 		goto free_fail_no_cp;
97810f966bbSChao Yu 	}
979e494c2f9SChao Yu 
9801dbe4152SChangman Lee 	if (cp_blks <= 1)
9811dbe4152SChangman Lee 		goto done;
9821dbe4152SChangman Lee 
9831dbe4152SChangman Lee 	cp_blk_no = le32_to_cpu(fsb->cp_blkaddr);
9841dbe4152SChangman Lee 	if (cur_page == cp2)
9851dbe4152SChangman Lee 		cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg);
9861dbe4152SChangman Lee 
9871dbe4152SChangman Lee 	for (i = 1; i < cp_blks; i++) {
9881dbe4152SChangman Lee 		void *sit_bitmap_ptr;
9891dbe4152SChangman Lee 		unsigned char *ckpt = (unsigned char *)sbi->ckpt;
9901dbe4152SChangman Lee 
9914d57b86dSChao Yu 		cur_page = f2fs_get_meta_page(sbi, cp_blk_no + i);
99210f966bbSChao Yu 		if (IS_ERR(cur_page)) {
99310f966bbSChao Yu 			err = PTR_ERR(cur_page);
9947735730dSChao Yu 			goto free_fail_no_cp;
99510f966bbSChao Yu 		}
9961dbe4152SChangman Lee 		sit_bitmap_ptr = page_address(cur_page);
9971dbe4152SChangman Lee 		memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size);
9981dbe4152SChangman Lee 		f2fs_put_page(cur_page, 1);
9991dbe4152SChangman Lee 	}
10001dbe4152SChangman Lee done:
1001127e670aSJaegeuk Kim 	f2fs_put_page(cp1, 1);
1002127e670aSJaegeuk Kim 	f2fs_put_page(cp2, 1);
1003127e670aSJaegeuk Kim 	return 0;
1004127e670aSJaegeuk Kim 
1005a2125ff7SJaegeuk Kim free_fail_no_cp:
1006a2125ff7SJaegeuk Kim 	f2fs_put_page(cp1, 1);
1007a2125ff7SJaegeuk Kim 	f2fs_put_page(cp2, 1);
1008127e670aSJaegeuk Kim fail_no_cp:
10095222595dSJaegeuk Kim 	kvfree(sbi->ckpt);
101010f966bbSChao Yu 	return err;
1011127e670aSJaegeuk Kim }
1012127e670aSJaegeuk Kim 
1013c227f912SChao Yu static void __add_dirty_inode(struct inode *inode, enum inode_type type)
1014127e670aSJaegeuk Kim {
10154081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1016c227f912SChao Yu 	int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
10172d7b822aSChao Yu 
101891942321SJaegeuk Kim 	if (is_inode_flag_set(inode, flag))
10192710fd7eSChao Yu 		return;
10202d7b822aSChao Yu 
102191942321SJaegeuk Kim 	set_inode_flag(inode, flag);
10227bc155feSJaegeuk Kim 	list_add_tail(&F2FS_I(inode)->dirty_list, &sbi->inode_list[type]);
102333fbd510SChao Yu 	stat_inc_dirty_inode(sbi, type);
10245deb8267SJaegeuk Kim }
10255deb8267SJaegeuk Kim 
1026c227f912SChao Yu static void __remove_dirty_inode(struct inode *inode, enum inode_type type)
10276ad7609aSChao Yu {
1028c227f912SChao Yu 	int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
10296ad7609aSChao Yu 
103091942321SJaegeuk Kim 	if (get_dirty_pages(inode) || !is_inode_flag_set(inode, flag))
10316ad7609aSChao Yu 		return;
10326ad7609aSChao Yu 
103391942321SJaegeuk Kim 	list_del_init(&F2FS_I(inode)->dirty_list);
103491942321SJaegeuk Kim 	clear_inode_flag(inode, flag);
103533fbd510SChao Yu 	stat_dec_dirty_inode(F2FS_I_SB(inode), type);
10366ad7609aSChao Yu }
10376ad7609aSChao Yu 
10384f5e34f7SMatthew Wilcox (Oracle) void f2fs_update_dirty_folio(struct inode *inode, struct folio *folio)
10395deb8267SJaegeuk Kim {
10404081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1041c227f912SChao Yu 	enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
10425deb8267SJaegeuk Kim 
10435ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
10445ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
1045127e670aSJaegeuk Kim 		return;
10467bd59381SGu Zheng 
1047c227f912SChao Yu 	spin_lock(&sbi->inode_lock[type]);
10481c4bf763SJaegeuk Kim 	if (type != FILE_INODE || test_opt(sbi, DATA_FLUSH))
1049c227f912SChao Yu 		__add_dirty_inode(inode, type);
1050b951a4ecSYunlei He 	inode_inc_dirty_pages(inode);
10511c4bf763SJaegeuk Kim 	spin_unlock(&sbi->inode_lock[type]);
10521c4bf763SJaegeuk Kim 
10534f5e34f7SMatthew Wilcox (Oracle) 	set_page_private_reference(&folio->page);
10545deb8267SJaegeuk Kim }
1055127e670aSJaegeuk Kim 
10564d57b86dSChao Yu void f2fs_remove_dirty_inode(struct inode *inode)
1057127e670aSJaegeuk Kim {
10584081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1059c227f912SChao Yu 	enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
1060127e670aSJaegeuk Kim 
1061c227f912SChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
1062c227f912SChao Yu 			!S_ISLNK(inode->i_mode))
1063127e670aSJaegeuk Kim 		return;
1064127e670aSJaegeuk Kim 
106510aa97c3SJaegeuk Kim 	if (type == FILE_INODE && !test_opt(sbi, DATA_FLUSH))
106610aa97c3SJaegeuk Kim 		return;
106710aa97c3SJaegeuk Kim 
1068c227f912SChao Yu 	spin_lock(&sbi->inode_lock[type]);
1069c227f912SChao Yu 	__remove_dirty_inode(inode, type);
1070c227f912SChao Yu 	spin_unlock(&sbi->inode_lock[type]);
1071afc3eda2SJaegeuk Kim }
107274d0b917SJaegeuk Kim 
1073d80afefbSChao Yu int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type,
1074d80afefbSChao Yu 						bool from_cp)
1075127e670aSJaegeuk Kim {
1076ce3b7d80SGu Zheng 	struct list_head *head;
1077127e670aSJaegeuk Kim 	struct inode *inode;
10782710fd7eSChao Yu 	struct f2fs_inode_info *fi;
10794cf18537SChao Yu 	bool is_dir = (type == DIR_INODE);
10804db08d01SJaegeuk Kim 	unsigned long ino = 0;
10814cf18537SChao Yu 
10824cf18537SChao Yu 	trace_f2fs_sync_dirty_inodes_enter(sbi->sb, is_dir,
10834cf18537SChao Yu 				get_pages(sbi, is_dir ?
10844cf18537SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
1085127e670aSJaegeuk Kim retry:
10869b664822SZhang Qilong 	if (unlikely(f2fs_cp_error(sbi))) {
10879b664822SZhang Qilong 		trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir,
10889b664822SZhang Qilong 				get_pages(sbi, is_dir ?
10899b664822SZhang Qilong 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
10906d5a1495SChao Yu 		return -EIO;
10919b664822SZhang Qilong 	}
1092af41d3eeSJaegeuk Kim 
1093c227f912SChao Yu 	spin_lock(&sbi->inode_lock[type]);
1094ce3b7d80SGu Zheng 
1095c227f912SChao Yu 	head = &sbi->inode_list[type];
1096127e670aSJaegeuk Kim 	if (list_empty(head)) {
1097c227f912SChao Yu 		spin_unlock(&sbi->inode_lock[type]);
10984cf18537SChao Yu 		trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir,
10994cf18537SChao Yu 				get_pages(sbi, is_dir ?
11004cf18537SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
11016d5a1495SChao Yu 		return 0;
1102127e670aSJaegeuk Kim 	}
1103939afa94SChao Yu 	fi = list_first_entry(head, struct f2fs_inode_info, dirty_list);
11042710fd7eSChao Yu 	inode = igrab(&fi->vfs_inode);
1105c227f912SChao Yu 	spin_unlock(&sbi->inode_lock[type]);
1106127e670aSJaegeuk Kim 	if (inode) {
11074db08d01SJaegeuk Kim 		unsigned long cur_ino = inode->i_ino;
11084db08d01SJaegeuk Kim 
1109d80afefbSChao Yu 		if (from_cp)
1110b0af6d49SChao Yu 			F2FS_I(inode)->cp_task = current;
1111d80afefbSChao Yu 		F2FS_I(inode)->wb_task = current;
1112b0af6d49SChao Yu 
111387d6f890SJaegeuk Kim 		filemap_fdatawrite(inode->i_mapping);
1114b0af6d49SChao Yu 
1115d80afefbSChao Yu 		F2FS_I(inode)->wb_task = NULL;
1116d80afefbSChao Yu 		if (from_cp)
1117b0af6d49SChao Yu 			F2FS_I(inode)->cp_task = NULL;
1118b0af6d49SChao Yu 
1119127e670aSJaegeuk Kim 		iput(inode);
11204db08d01SJaegeuk Kim 		/* We need to give cpu to another writers. */
11212a63531aSYunlei He 		if (ino == cur_ino)
11224db08d01SJaegeuk Kim 			cond_resched();
11232a63531aSYunlei He 		else
11244db08d01SJaegeuk Kim 			ino = cur_ino;
1125127e670aSJaegeuk Kim 	} else {
1126127e670aSJaegeuk Kim 		/*
1127127e670aSJaegeuk Kim 		 * We should submit bio, since it exists several
1128127e670aSJaegeuk Kim 		 * wribacking dentry pages in the freeing inode.
1129127e670aSJaegeuk Kim 		 */
1130b9109b0eSJaegeuk Kim 		f2fs_submit_merged_write(sbi, DATA);
11317ecebe5eSSebastian Andrzej Siewior 		cond_resched();
1132127e670aSJaegeuk Kim 	}
1133127e670aSJaegeuk Kim 	goto retry;
1134127e670aSJaegeuk Kim }
1135127e670aSJaegeuk Kim 
11360f18b462SJaegeuk Kim int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi)
11370f18b462SJaegeuk Kim {
11380f18b462SJaegeuk Kim 	struct list_head *head = &sbi->inode_list[DIRTY_META];
11390f18b462SJaegeuk Kim 	struct inode *inode;
11400f18b462SJaegeuk Kim 	struct f2fs_inode_info *fi;
11410f18b462SJaegeuk Kim 	s64 total = get_pages(sbi, F2FS_DIRTY_IMETA);
11420f18b462SJaegeuk Kim 
11430f18b462SJaegeuk Kim 	while (total--) {
11440f18b462SJaegeuk Kim 		if (unlikely(f2fs_cp_error(sbi)))
11450f18b462SJaegeuk Kim 			return -EIO;
11460f18b462SJaegeuk Kim 
11470f18b462SJaegeuk Kim 		spin_lock(&sbi->inode_lock[DIRTY_META]);
11480f18b462SJaegeuk Kim 		if (list_empty(head)) {
11490f18b462SJaegeuk Kim 			spin_unlock(&sbi->inode_lock[DIRTY_META]);
11500f18b462SJaegeuk Kim 			return 0;
11510f18b462SJaegeuk Kim 		}
1152939afa94SChao Yu 		fi = list_first_entry(head, struct f2fs_inode_info,
11530f18b462SJaegeuk Kim 							gdirty_list);
11540f18b462SJaegeuk Kim 		inode = igrab(&fi->vfs_inode);
11550f18b462SJaegeuk Kim 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
11560f18b462SJaegeuk Kim 		if (inode) {
115718340edcSJaegeuk Kim 			sync_inode_metadata(inode, 0);
115818340edcSJaegeuk Kim 
115918340edcSJaegeuk Kim 			/* it's on eviction */
116018340edcSJaegeuk Kim 			if (is_inode_flag_set(inode, FI_DIRTY_INODE))
11614d57b86dSChao Yu 				f2fs_update_inode_page(inode);
11620f18b462SJaegeuk Kim 			iput(inode);
11630f18b462SJaegeuk Kim 		}
1164dee668c1SChao Yu 	}
11650f18b462SJaegeuk Kim 	return 0;
11660f18b462SJaegeuk Kim }
11670f18b462SJaegeuk Kim 
116859c9081bSYunlei He static void __prepare_cp_block(struct f2fs_sb_info *sbi)
116959c9081bSYunlei He {
117059c9081bSYunlei He 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
117159c9081bSYunlei He 	struct f2fs_nm_info *nm_i = NM_I(sbi);
117259c9081bSYunlei He 	nid_t last_nid = nm_i->next_scan_nid;
117359c9081bSYunlei He 
117459c9081bSYunlei He 	next_free_nid(sbi, &last_nid);
117559c9081bSYunlei He 	ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
117659c9081bSYunlei He 	ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi));
117759c9081bSYunlei He 	ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi));
117859c9081bSYunlei He 	ckpt->next_free_nid = cpu_to_le32(last_nid);
117959c9081bSYunlei He }
118059c9081bSYunlei He 
1181af033b2aSChao Yu static bool __need_flush_quota(struct f2fs_sb_info *sbi)
1182af033b2aSChao Yu {
1183db6ec53bSJaegeuk Kim 	bool ret = false;
1184db6ec53bSJaegeuk Kim 
1185af033b2aSChao Yu 	if (!is_journalled_quota(sbi))
1186af033b2aSChao Yu 		return false;
1187db6ec53bSJaegeuk Kim 
1188e4544b63STim Murray 	if (!f2fs_down_write_trylock(&sbi->quota_sem))
1189a5c00422SChao Yu 		return true;
1190db6ec53bSJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH)) {
1191db6ec53bSJaegeuk Kim 		ret = false;
1192db6ec53bSJaegeuk Kim 	} else if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR)) {
1193db6ec53bSJaegeuk Kim 		ret = false;
1194db6ec53bSJaegeuk Kim 	} else if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_FLUSH)) {
1195db6ec53bSJaegeuk Kim 		clear_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH);
1196db6ec53bSJaegeuk Kim 		ret = true;
1197db6ec53bSJaegeuk Kim 	} else if (get_pages(sbi, F2FS_DIRTY_QDATA)) {
1198db6ec53bSJaegeuk Kim 		ret = true;
1199db6ec53bSJaegeuk Kim 	}
1200e4544b63STim Murray 	f2fs_up_write(&sbi->quota_sem);
1201db6ec53bSJaegeuk Kim 	return ret;
1202af033b2aSChao Yu }
1203af033b2aSChao Yu 
12040a8165d7SJaegeuk Kim /*
1205127e670aSJaegeuk Kim  * Freeze all the FS-operations for checkpoint.
1206127e670aSJaegeuk Kim  */
1207cf779cabSJaegeuk Kim static int block_operations(struct f2fs_sb_info *sbi)
1208127e670aSJaegeuk Kim {
1209127e670aSJaegeuk Kim 	struct writeback_control wbc = {
1210127e670aSJaegeuk Kim 		.sync_mode = WB_SYNC_ALL,
1211127e670aSJaegeuk Kim 		.nr_to_write = LONG_MAX,
1212127e670aSJaegeuk Kim 		.for_reclaim = 0,
1213127e670aSJaegeuk Kim 	};
1214af033b2aSChao Yu 	int err = 0, cnt = 0;
1215c718379bSJaegeuk Kim 
121634c061adSSayali Lokhande 	/*
121734c061adSSayali Lokhande 	 * Let's flush inline_data in dirty node pages.
121834c061adSSayali Lokhande 	 */
121934c061adSSayali Lokhande 	f2fs_flush_inline_data(sbi);
122034c061adSSayali Lokhande 
1221af033b2aSChao Yu retry_flush_quotas:
1222db6ec53bSJaegeuk Kim 	f2fs_lock_all(sbi);
1223af033b2aSChao Yu 	if (__need_flush_quota(sbi)) {
1224af033b2aSChao Yu 		int locked;
1225af033b2aSChao Yu 
1226af033b2aSChao Yu 		if (++cnt > DEFAULT_RETRY_QUOTA_FLUSH_COUNT) {
1227af033b2aSChao Yu 			set_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH);
1228db6ec53bSJaegeuk Kim 			set_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH);
1229af033b2aSChao Yu 			goto retry_flush_dents;
1230af033b2aSChao Yu 		}
1231db6ec53bSJaegeuk Kim 		f2fs_unlock_all(sbi);
1232af033b2aSChao Yu 
1233af033b2aSChao Yu 		/* only failed during mount/umount/freeze/quotactl */
1234af033b2aSChao Yu 		locked = down_read_trylock(&sbi->sb->s_umount);
1235af033b2aSChao Yu 		f2fs_quota_sync(sbi->sb, -1);
1236af033b2aSChao Yu 		if (locked)
1237af033b2aSChao Yu 			up_read(&sbi->sb->s_umount);
1238af033b2aSChao Yu 		cond_resched();
1239af033b2aSChao Yu 		goto retry_flush_quotas;
1240af033b2aSChao Yu 	}
1241af033b2aSChao Yu 
1242af033b2aSChao Yu retry_flush_dents:
1243127e670aSJaegeuk Kim 	/* write all the dirty dentry pages */
1244127e670aSJaegeuk Kim 	if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
1245e479556bSGu Zheng 		f2fs_unlock_all(sbi);
1246d80afefbSChao Yu 		err = f2fs_sync_dirty_inodes(sbi, DIR_INODE, true);
12476d5a1495SChao Yu 		if (err)
12481f5f11a3SJaegeuk Kim 			return err;
124930973883SJaegeuk Kim 		cond_resched();
1250af033b2aSChao Yu 		goto retry_flush_quotas;
1251127e670aSJaegeuk Kim 	}
1252127e670aSJaegeuk Kim 
125359c9081bSYunlei He 	/*
125459c9081bSYunlei He 	 * POR: we should ensure that there are no dirty node pages
125559c9081bSYunlei He 	 * until finishing nat/sit flush. inode->i_blocks can be updated.
125659c9081bSYunlei He 	 */
1257e4544b63STim Murray 	f2fs_down_write(&sbi->node_change);
125859c9081bSYunlei He 
12590f18b462SJaegeuk Kim 	if (get_pages(sbi, F2FS_DIRTY_IMETA)) {
1260e4544b63STim Murray 		f2fs_up_write(&sbi->node_change);
12610f18b462SJaegeuk Kim 		f2fs_unlock_all(sbi);
12620f18b462SJaegeuk Kim 		err = f2fs_sync_inode_meta(sbi);
12630f18b462SJaegeuk Kim 		if (err)
12641f5f11a3SJaegeuk Kim 			return err;
126530973883SJaegeuk Kim 		cond_resched();
1266af033b2aSChao Yu 		goto retry_flush_quotas;
12670f18b462SJaegeuk Kim 	}
12680f18b462SJaegeuk Kim 
126939936837SJaegeuk Kim retry_flush_nodes:
1270e4544b63STim Murray 	f2fs_down_write(&sbi->node_write);
1271127e670aSJaegeuk Kim 
1272127e670aSJaegeuk Kim 	if (get_pages(sbi, F2FS_DIRTY_NODES)) {
1273e4544b63STim Murray 		f2fs_up_write(&sbi->node_write);
1274c29fd0c0SChao Yu 		atomic_inc(&sbi->wb_sync_req[NODE]);
12754d57b86dSChao Yu 		err = f2fs_sync_node_pages(sbi, &wbc, false, FS_CP_NODE_IO);
1276c29fd0c0SChao Yu 		atomic_dec(&sbi->wb_sync_req[NODE]);
12776d5a1495SChao Yu 		if (err) {
1278e4544b63STim Murray 			f2fs_up_write(&sbi->node_change);
1279cf779cabSJaegeuk Kim 			f2fs_unlock_all(sbi);
12801f5f11a3SJaegeuk Kim 			return err;
1281cf779cabSJaegeuk Kim 		}
128230973883SJaegeuk Kim 		cond_resched();
128339936837SJaegeuk Kim 		goto retry_flush_nodes;
1284127e670aSJaegeuk Kim 	}
128559c9081bSYunlei He 
128659c9081bSYunlei He 	/*
128759c9081bSYunlei He 	 * sbi->node_change is used only for AIO write_begin path which produces
128859c9081bSYunlei He 	 * dirty node blocks and some checkpoint values by block allocation.
128959c9081bSYunlei He 	 */
129059c9081bSYunlei He 	__prepare_cp_block(sbi);
1291e4544b63STim Murray 	f2fs_up_write(&sbi->node_change);
1292cf779cabSJaegeuk Kim 	return err;
1293127e670aSJaegeuk Kim }
1294127e670aSJaegeuk Kim 
1295127e670aSJaegeuk Kim static void unblock_operations(struct f2fs_sb_info *sbi)
1296127e670aSJaegeuk Kim {
1297e4544b63STim Murray 	f2fs_up_write(&sbi->node_write);
1298e479556bSGu Zheng 	f2fs_unlock_all(sbi);
1299127e670aSJaegeuk Kim }
1300127e670aSJaegeuk Kim 
1301bf22c3ccSSahitya Tummala void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type)
1302fb51b5efSChangman Lee {
1303fb51b5efSChangman Lee 	DEFINE_WAIT(wait);
1304fb51b5efSChangman Lee 
1305fb51b5efSChangman Lee 	for (;;) {
1306bf22c3ccSSahitya Tummala 		if (!get_pages(sbi, type))
1307fb51b5efSChangman Lee 			break;
1308fb51b5efSChangman Lee 
1309af697c0fSJaegeuk Kim 		if (unlikely(f2fs_cp_error(sbi)))
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 
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) +
1332b702c83eSChao Yu 			NM_I(sbi)->nat_bits_blocks > sbi->blocks_per_seg) {
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 
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 	/*
140646706d59SGao Xiang 	 * pagevec_lookup_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 
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 
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 
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++) {
1476127e670aSJaegeuk Kim 		ckpt->cur_node_segno[i] =
1477127e670aSJaegeuk Kim 			cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE));
1478127e670aSJaegeuk Kim 		ckpt->cur_node_blkoff[i] =
1479127e670aSJaegeuk Kim 			cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE));
1480127e670aSJaegeuk Kim 		ckpt->alloc_type[i + CURSEG_HOT_NODE] =
1481127e670aSJaegeuk Kim 				curseg_alloc_type(sbi, i + CURSEG_HOT_NODE);
1482127e670aSJaegeuk Kim 	}
1483b5b82205SChao Yu 	for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
1484127e670aSJaegeuk Kim 		ckpt->cur_data_segno[i] =
1485127e670aSJaegeuk Kim 			cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA));
1486127e670aSJaegeuk Kim 		ckpt->cur_data_blkoff[i] =
1487127e670aSJaegeuk Kim 			cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA));
1488127e670aSJaegeuk Kim 		ckpt->alloc_type[i + CURSEG_HOT_DATA] =
1489127e670aSJaegeuk Kim 				curseg_alloc_type(sbi, i + CURSEG_HOT_DATA);
1490127e670aSJaegeuk Kim 	}
1491127e670aSJaegeuk Kim 
1492127e670aSJaegeuk Kim 	/* 2 cp + n data seg summary + orphan inode blocks */
14934d57b86dSChao Yu 	data_sum_blocks = f2fs_npages_for_summary_flush(sbi, false);
1494d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1495b5b82205SChao Yu 	if (data_sum_blocks < NR_CURSEG_DATA_TYPE)
1496aaec2b1dSChao Yu 		__set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
1497127e670aSJaegeuk Kim 	else
1498aaec2b1dSChao Yu 		__clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
1499d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1500127e670aSJaegeuk Kim 
150167298804SChao Yu 	orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num);
15021dbe4152SChangman Lee 	ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks +
15031dbe4152SChangman Lee 			orphan_blocks);
1504127e670aSJaegeuk Kim 
1505119ee914SJaegeuk Kim 	if (__remain_node_summaries(cpc->reason))
1506b5b82205SChao Yu 		ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
15071dbe4152SChangman Lee 				cp_payload_blks + data_sum_blocks +
15081dbe4152SChangman Lee 				orphan_blocks + NR_CURSEG_NODE_TYPE);
1509119ee914SJaegeuk Kim 	else
1510b5b82205SChao Yu 		ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
15111dbe4152SChangman Lee 				cp_payload_blks + data_sum_blocks +
15121dbe4152SChangman Lee 				orphan_blocks);
1513119ee914SJaegeuk Kim 
1514e4c5d848SJaegeuk Kim 	/* update ckpt flag for checkpoint */
1515e4c5d848SJaegeuk Kim 	update_ckpt_flags(sbi, cpc);
1516a468f0efSJaegeuk Kim 
1517127e670aSJaegeuk Kim 	/* update SIT/NAT bitmap */
1518127e670aSJaegeuk Kim 	get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
1519127e670aSJaegeuk Kim 	get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
1520127e670aSJaegeuk Kim 
1521d7eb8f1cSChao Yu 	crc32 = f2fs_checkpoint_chksum(sbi, ckpt);
15227e586fa0SJaegeuk Kim 	*((__le32 *)((unsigned char *)ckpt +
15237e586fa0SJaegeuk Kim 				le32_to_cpu(ckpt->checksum_offset)))
1524127e670aSJaegeuk Kim 				= cpu_to_le32(crc32);
1525127e670aSJaegeuk Kim 
15268508e44aSJaegeuk Kim 	start_blk = __start_cp_next_addr(sbi);
1527127e670aSJaegeuk Kim 
152822ad0b6aSJaegeuk Kim 	/* write nat bits */
152994c821fbSChao Yu 	if ((cpc->reason & CP_UMOUNT) &&
153094c821fbSChao Yu 			is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG)) {
153122ad0b6aSJaegeuk Kim 		__u64 cp_ver = cur_cp_version(ckpt);
153222ad0b6aSJaegeuk Kim 		block_t blk;
153322ad0b6aSJaegeuk Kim 
153422ad0b6aSJaegeuk Kim 		cp_ver |= ((__u64)crc32 << 32);
153522ad0b6aSJaegeuk Kim 		*(__le64 *)nm_i->nat_bits = cpu_to_le64(cp_ver);
153622ad0b6aSJaegeuk Kim 
153722ad0b6aSJaegeuk Kim 		blk = start_blk + sbi->blocks_per_seg - nm_i->nat_bits_blocks;
153822ad0b6aSJaegeuk Kim 		for (i = 0; i < nm_i->nat_bits_blocks; i++)
15394d57b86dSChao Yu 			f2fs_update_meta_page(sbi, nm_i->nat_bits +
154022ad0b6aSJaegeuk Kim 					(i << F2FS_BLKSIZE_BITS), blk + i);
154122ad0b6aSJaegeuk Kim 	}
154222ad0b6aSJaegeuk Kim 
1543127e670aSJaegeuk Kim 	/* write out checkpoint buffer at block 0 */
15444d57b86dSChao Yu 	f2fs_update_meta_page(sbi, ckpt, start_blk++);
1545127e670aSJaegeuk Kim 
1546381722d2SChao Yu 	for (i = 1; i < 1 + cp_payload_blks; i++)
15474d57b86dSChao Yu 		f2fs_update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE,
1548381722d2SChao Yu 							start_blk++);
15491dbe4152SChangman Lee 
155067298804SChao Yu 	if (orphan_num) {
1551127e670aSJaegeuk Kim 		write_orphan_inodes(sbi, start_blk);
1552127e670aSJaegeuk Kim 		start_blk += orphan_blocks;
1553127e670aSJaegeuk Kim 	}
1554127e670aSJaegeuk Kim 
15554d57b86dSChao Yu 	f2fs_write_data_summaries(sbi, start_blk);
1556127e670aSJaegeuk Kim 	start_blk += data_sum_blocks;
15578f1dbbbbSShuoran Liu 
15588f1dbbbbSShuoran Liu 	/* Record write statistics in the hot node summary */
15598f1dbbbbSShuoran Liu 	kbytes_written = sbi->kbytes_written;
15603a0a9cbcSChao Yu 	kbytes_written += (f2fs_get_sectors_written(sbi) -
15613a0a9cbcSChao Yu 				sbi->sectors_written_start) >> 1;
1562b7ad7512SChao Yu 	seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written);
15638f1dbbbbSShuoran Liu 
1564119ee914SJaegeuk Kim 	if (__remain_node_summaries(cpc->reason)) {
15654d57b86dSChao Yu 		f2fs_write_node_summaries(sbi, start_blk);
1566127e670aSJaegeuk Kim 		start_blk += NR_CURSEG_NODE_TYPE;
1567127e670aSJaegeuk Kim 	}
1568127e670aSJaegeuk Kim 
156946706d59SGao Xiang 	/* update user_block_counts */
157046706d59SGao Xiang 	sbi->last_valid_block_count = sbi->total_valid_block_count;
157146706d59SGao Xiang 	percpu_counter_set(&sbi->alloc_valid_block_count, 0);
157247c8ebccSJaegeuk Kim 	percpu_counter_set(&sbi->rf_node_block_count, 0);
1573127e670aSJaegeuk Kim 
157446706d59SGao Xiang 	/* Here, we have one bio having CP pack except cp pack 2 page */
15754d57b86dSChao Yu 	f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
1576bf22c3ccSSahitya Tummala 	/* Wait for all dirty meta pages to be submitted for IO */
1577bf22c3ccSSahitya Tummala 	f2fs_wait_on_all_pages(sbi, F2FS_DIRTY_META);
157846706d59SGao Xiang 
157946706d59SGao Xiang 	/* wait for previous submitted meta pages writeback */
1580bf22c3ccSSahitya Tummala 	f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
1581127e670aSJaegeuk Kim 
158246706d59SGao Xiang 	/* flush all device cache */
158346706d59SGao Xiang 	err = f2fs_flush_device_cache(sbi);
158446706d59SGao Xiang 	if (err)
158546706d59SGao Xiang 		return err;
1586127e670aSJaegeuk Kim 
158746706d59SGao Xiang 	/* barrier and flush checkpoint cp pack 2 page if it can */
158846706d59SGao Xiang 	commit_checkpoint(sbi, ckpt, start_blk);
1589bf22c3ccSSahitya Tummala 	f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
15906a8f8ca5SJaegeuk Kim 
159118767e62SChao Yu 	/*
1592542989b6SEric Biggers 	 * invalidate intermediate page cache borrowed from meta inode which are
1593aff6fbbeSChao Yu 	 * used for migration of encrypted, verity or compressed inode's blocks.
159418767e62SChao Yu 	 */
1595aff6fbbeSChao Yu 	if (f2fs_sb_has_encrypt(sbi) || f2fs_sb_has_verity(sbi) ||
1596aff6fbbeSChao Yu 		f2fs_sb_has_compression(sbi))
159718767e62SChao Yu 		invalidate_mapping_pages(META_MAPPING(sbi),
159818767e62SChao Yu 				MAIN_BLKADDR(sbi), MAX_BLKADDR(sbi) - 1);
159918767e62SChao Yu 
16004d57b86dSChao Yu 	f2fs_release_ino_entry(sbi, false);
1601cf779cabSJaegeuk Kim 
160250fa53ecSChao Yu 	f2fs_reset_fsync_node_info(sbi);
160350fa53ecSChao Yu 
1604caf0047eSChao Yu 	clear_sbi_flag(sbi, SBI_IS_DIRTY);
1605bbf156f7SJaegeuk Kim 	clear_sbi_flag(sbi, SBI_NEED_CP);
1606af033b2aSChao Yu 	clear_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH);
1607c9c8ed50SChao Yu 
1608c9c8ed50SChao Yu 	spin_lock(&sbi->stat_lock);
16094354994fSDaniel Rosenberg 	sbi->unusable_block_count = 0;
1610c9c8ed50SChao Yu 	spin_unlock(&sbi->stat_lock);
1611c9c8ed50SChao Yu 
16128508e44aSJaegeuk Kim 	__set_cp_next_pack(sbi);
1613c34f42e2SChao Yu 
1614c2a080aeSChao Yu 	/*
1615c2a080aeSChao Yu 	 * redirty superblock if metadata like node page or inode cache is
1616c2a080aeSChao Yu 	 * updated during writing checkpoint.
1617c2a080aeSChao Yu 	 */
1618c2a080aeSChao Yu 	if (get_pages(sbi, F2FS_DIRTY_NODES) ||
1619c2a080aeSChao Yu 			get_pages(sbi, F2FS_DIRTY_IMETA))
1620c2a080aeSChao Yu 		set_sbi_flag(sbi, SBI_IS_DIRTY);
1621c2a080aeSChao Yu 
1622c2a080aeSChao Yu 	f2fs_bug_on(sbi, get_pages(sbi, F2FS_DIRTY_DENTS));
1623c2a080aeSChao Yu 
1624af697c0fSJaegeuk Kim 	return unlikely(f2fs_cp_error(sbi)) ? -EIO : 0;
1625127e670aSJaegeuk Kim }
1626127e670aSJaegeuk Kim 
16274d57b86dSChao Yu int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1628127e670aSJaegeuk Kim {
1629127e670aSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1630127e670aSJaegeuk Kim 	unsigned long long ckpt_ver;
1631c34f42e2SChao Yu 	int err = 0;
1632127e670aSJaegeuk Kim 
1633f5a131bbSChao Yu 	if (f2fs_readonly(sbi->sb) || f2fs_hw_is_readonly(sbi))
1634f5a131bbSChao Yu 		return -EROFS;
1635f5a131bbSChao Yu 
16364354994fSDaniel Rosenberg 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
16374354994fSDaniel Rosenberg 		if (cpc->reason != CP_PAUSE)
16384354994fSDaniel Rosenberg 			return 0;
1639dcbb4c10SJoe Perches 		f2fs_warn(sbi, "Start checkpoint disabled!");
16404354994fSDaniel Rosenberg 	}
1641b4b10061SJaegeuk Kim 	if (cpc->reason != CP_RESIZE)
1642e4544b63STim Murray 		f2fs_down_write(&sbi->cp_global_sem);
16438501017eSJaegeuk Kim 
1644caf0047eSChao Yu 	if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) &&
1645c473f1a9SChao Yu 		((cpc->reason & CP_FASTBOOT) || (cpc->reason & CP_SYNC) ||
1646c473f1a9SChao Yu 		((cpc->reason & CP_DISCARD) && !sbi->discard_blks)))
16478501017eSJaegeuk Kim 		goto out;
1648c34f42e2SChao Yu 	if (unlikely(f2fs_cp_error(sbi))) {
1649c34f42e2SChao Yu 		err = -EIO;
1650cf779cabSJaegeuk Kim 		goto out;
1651c34f42e2SChao Yu 	}
16522bda542dSWanpeng Li 
16532bda542dSWanpeng Li 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops");
16542bda542dSWanpeng Li 
1655c34f42e2SChao Yu 	err = block_operations(sbi);
1656c34f42e2SChao Yu 	if (err)
1657cf779cabSJaegeuk Kim 		goto out;
1658127e670aSJaegeuk Kim 
165975ab4cb8SJaegeuk Kim 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops");
16602af4bd6cSNamjae Jeon 
1661b9109b0eSJaegeuk Kim 	f2fs_flush_merged_writes(sbi);
1662127e670aSJaegeuk Kim 
166358cce381SYunlei He 	/* this is the case of multiple fstrims without any changes */
1664c473f1a9SChao Yu 	if (cpc->reason & CP_DISCARD) {
16654d57b86dSChao Yu 		if (!f2fs_exist_trim_candidates(sbi, cpc)) {
166625290fa5SJaegeuk Kim 			unblock_operations(sbi);
166725290fa5SJaegeuk Kim 			goto out;
166825290fa5SJaegeuk Kim 		}
166925290fa5SJaegeuk Kim 
1670a95ba66aSJaegeuk Kim 		if (NM_I(sbi)->nat_cnt[DIRTY_NAT] == 0 &&
16710333ad4eSJaegeuk Kim 				SIT_I(sbi)->dirty_sentries == 0 &&
16720333ad4eSJaegeuk Kim 				prefree_segments(sbi) == 0) {
16734d57b86dSChao Yu 			f2fs_flush_sit_entries(sbi, cpc);
16744d57b86dSChao Yu 			f2fs_clear_prefree_segments(sbi, cpc);
167558cce381SYunlei He 			unblock_operations(sbi);
167658cce381SYunlei He 			goto out;
167758cce381SYunlei He 		}
16780333ad4eSJaegeuk Kim 	}
167958cce381SYunlei He 
1680127e670aSJaegeuk Kim 	/*
1681127e670aSJaegeuk Kim 	 * update checkpoint pack index
1682127e670aSJaegeuk Kim 	 * Increase the version number so that
1683127e670aSJaegeuk Kim 	 * SIT entries and seg summaries are written at correct place
1684127e670aSJaegeuk Kim 	 */
1685d71b5564SJaegeuk Kim 	ckpt_ver = cur_cp_version(ckpt);
1686127e670aSJaegeuk Kim 	ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
1687127e670aSJaegeuk Kim 
1688127e670aSJaegeuk Kim 	/* write cached NAT/SIT entries to NAT/SIT area */
1689edc55aafSJaegeuk Kim 	err = f2fs_flush_nat_entries(sbi, cpc);
169091803392SChao Yu 	if (err) {
169191803392SChao Yu 		f2fs_err(sbi, "f2fs_flush_nat_entries failed err:%d, stop checkpoint", err);
169291803392SChao Yu 		f2fs_bug_on(sbi, !f2fs_cp_error(sbi));
1693edc55aafSJaegeuk Kim 		goto stop;
169491803392SChao Yu 	}
1695edc55aafSJaegeuk Kim 
16964d57b86dSChao Yu 	f2fs_flush_sit_entries(sbi, cpc);
1697127e670aSJaegeuk Kim 
1698d0b9e42aSChao Yu 	/* save inmem log status */
1699093749e2SChao Yu 	f2fs_save_inmem_curseg(sbi);
1700d0b9e42aSChao Yu 
1701c34f42e2SChao Yu 	err = do_checkpoint(sbi, cpc);
170291803392SChao Yu 	if (err) {
170391803392SChao Yu 		f2fs_err(sbi, "do_checkpoint failed err:%d, stop checkpoint", err);
170491803392SChao Yu 		f2fs_bug_on(sbi, !f2fs_cp_error(sbi));
17054d57b86dSChao Yu 		f2fs_release_discard_addrs(sbi);
170691803392SChao Yu 	} else {
17074d57b86dSChao Yu 		f2fs_clear_prefree_segments(sbi, cpc);
170891803392SChao Yu 	}
1709d0b9e42aSChao Yu 
1710093749e2SChao Yu 	f2fs_restore_inmem_curseg(sbi);
1711edc55aafSJaegeuk Kim stop:
1712127e670aSJaegeuk Kim 	unblock_operations(sbi);
1713942e0be6SChangman Lee 	stat_inc_cp_count(sbi->stat_info);
171410027551SJaegeuk Kim 
1715c473f1a9SChao Yu 	if (cpc->reason & CP_RECOVERY)
1716dcbb4c10SJoe Perches 		f2fs_notice(sbi, "checkpoint: version = %llx", ckpt_ver);
171760b99b48SJaegeuk Kim 
17187a88ddb5SChao Yu 	/* update CP_TIME to trigger checkpoint periodically */
17196beceb54SJaegeuk Kim 	f2fs_update_time(sbi, CP_TIME);
172055d1cdb2SJaegeuk Kim 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint");
17218501017eSJaegeuk Kim out:
1722b4b10061SJaegeuk Kim 	if (cpc->reason != CP_RESIZE)
1723e4544b63STim Murray 		f2fs_up_write(&sbi->cp_global_sem);
1724c34f42e2SChao Yu 	return err;
1725127e670aSJaegeuk Kim }
1726127e670aSJaegeuk Kim 
17274d57b86dSChao Yu void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi)
1728127e670aSJaegeuk Kim {
17296451e041SJaegeuk Kim 	int i;
17306451e041SJaegeuk Kim 
17316451e041SJaegeuk Kim 	for (i = 0; i < MAX_INO_ENTRY; i++) {
173267298804SChao Yu 		struct inode_management *im = &sbi->im[i];
173367298804SChao Yu 
173467298804SChao Yu 		INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC);
173567298804SChao Yu 		spin_lock_init(&im->ino_lock);
173667298804SChao Yu 		INIT_LIST_HEAD(&im->ino_list);
173767298804SChao Yu 		im->ino_num = 0;
17386451e041SJaegeuk Kim 	}
17396451e041SJaegeuk Kim 
1740b5b82205SChao Yu 	sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS -
1741d0b9e42aSChao Yu 			NR_CURSEG_PERSIST_TYPE - __cp_payload(sbi)) *
174214b42817SWanpeng Li 				F2FS_ORPHANS_PER_BLOCK;
1743127e670aSJaegeuk Kim }
1744127e670aSJaegeuk Kim 
17454d57b86dSChao Yu int __init f2fs_create_checkpoint_caches(void)
1746127e670aSJaegeuk Kim {
17476451e041SJaegeuk Kim 	ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry",
17486451e041SJaegeuk Kim 			sizeof(struct ino_entry));
17496451e041SJaegeuk Kim 	if (!ino_entry_slab)
1750127e670aSJaegeuk Kim 		return -ENOMEM;
17514d57b86dSChao Yu 	f2fs_inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry",
175206292073SChao Yu 			sizeof(struct inode_entry));
17534d57b86dSChao Yu 	if (!f2fs_inode_entry_slab) {
17546451e041SJaegeuk Kim 		kmem_cache_destroy(ino_entry_slab);
1755127e670aSJaegeuk Kim 		return -ENOMEM;
1756127e670aSJaegeuk Kim 	}
1757127e670aSJaegeuk Kim 	return 0;
1758127e670aSJaegeuk Kim }
1759127e670aSJaegeuk Kim 
17604d57b86dSChao Yu void f2fs_destroy_checkpoint_caches(void)
1761127e670aSJaegeuk Kim {
17626451e041SJaegeuk Kim 	kmem_cache_destroy(ino_entry_slab);
17634d57b86dSChao Yu 	kmem_cache_destroy(f2fs_inode_entry_slab);
1764127e670aSJaegeuk Kim }
1765261eeb9cSDaeho Jeong 
1766261eeb9cSDaeho Jeong static int __write_checkpoint_sync(struct f2fs_sb_info *sbi)
1767261eeb9cSDaeho Jeong {
1768261eeb9cSDaeho Jeong 	struct cp_control cpc = { .reason = CP_SYNC, };
1769261eeb9cSDaeho Jeong 	int err;
1770261eeb9cSDaeho Jeong 
1771e4544b63STim Murray 	f2fs_down_write(&sbi->gc_lock);
1772261eeb9cSDaeho Jeong 	err = f2fs_write_checkpoint(sbi, &cpc);
1773e4544b63STim Murray 	f2fs_up_write(&sbi->gc_lock);
1774261eeb9cSDaeho Jeong 
1775261eeb9cSDaeho Jeong 	return err;
1776261eeb9cSDaeho Jeong }
1777261eeb9cSDaeho Jeong 
1778261eeb9cSDaeho Jeong static void __checkpoint_and_complete_reqs(struct f2fs_sb_info *sbi)
1779261eeb9cSDaeho Jeong {
1780261eeb9cSDaeho Jeong 	struct ckpt_req_control *cprc = &sbi->cprc_info;
1781261eeb9cSDaeho Jeong 	struct ckpt_req *req, *next;
1782261eeb9cSDaeho Jeong 	struct llist_node *dispatch_list;
1783261eeb9cSDaeho Jeong 	u64 sum_diff = 0, diff, count = 0;
1784261eeb9cSDaeho Jeong 	int ret;
1785261eeb9cSDaeho Jeong 
1786261eeb9cSDaeho Jeong 	dispatch_list = llist_del_all(&cprc->issue_list);
1787261eeb9cSDaeho Jeong 	if (!dispatch_list)
1788261eeb9cSDaeho Jeong 		return;
1789261eeb9cSDaeho Jeong 	dispatch_list = llist_reverse_order(dispatch_list);
1790261eeb9cSDaeho Jeong 
1791261eeb9cSDaeho Jeong 	ret = __write_checkpoint_sync(sbi);
1792261eeb9cSDaeho Jeong 	atomic_inc(&cprc->issued_ckpt);
1793261eeb9cSDaeho Jeong 
1794261eeb9cSDaeho Jeong 	llist_for_each_entry_safe(req, next, dispatch_list, llnode) {
1795261eeb9cSDaeho Jeong 		diff = (u64)ktime_ms_delta(ktime_get(), req->queue_time);
1796261eeb9cSDaeho Jeong 		req->ret = ret;
1797261eeb9cSDaeho Jeong 		complete(&req->wait);
1798261eeb9cSDaeho Jeong 
1799261eeb9cSDaeho Jeong 		sum_diff += diff;
1800261eeb9cSDaeho Jeong 		count++;
1801261eeb9cSDaeho Jeong 	}
1802261eeb9cSDaeho Jeong 	atomic_sub(count, &cprc->queued_ckpt);
1803261eeb9cSDaeho Jeong 	atomic_add(count, &cprc->total_ckpt);
1804261eeb9cSDaeho Jeong 
1805261eeb9cSDaeho Jeong 	spin_lock(&cprc->stat_lock);
1806261eeb9cSDaeho Jeong 	cprc->cur_time = (unsigned int)div64_u64(sum_diff, count);
1807261eeb9cSDaeho Jeong 	if (cprc->peak_time < cprc->cur_time)
1808261eeb9cSDaeho Jeong 		cprc->peak_time = cprc->cur_time;
1809261eeb9cSDaeho Jeong 	spin_unlock(&cprc->stat_lock);
1810261eeb9cSDaeho Jeong }
1811261eeb9cSDaeho Jeong 
1812261eeb9cSDaeho Jeong static int issue_checkpoint_thread(void *data)
1813261eeb9cSDaeho Jeong {
1814261eeb9cSDaeho Jeong 	struct f2fs_sb_info *sbi = data;
1815261eeb9cSDaeho Jeong 	struct ckpt_req_control *cprc = &sbi->cprc_info;
1816261eeb9cSDaeho Jeong 	wait_queue_head_t *q = &cprc->ckpt_wait_queue;
1817261eeb9cSDaeho Jeong repeat:
1818261eeb9cSDaeho Jeong 	if (kthread_should_stop())
1819261eeb9cSDaeho Jeong 		return 0;
1820261eeb9cSDaeho Jeong 
1821261eeb9cSDaeho Jeong 	if (!llist_empty(&cprc->issue_list))
1822261eeb9cSDaeho Jeong 		__checkpoint_and_complete_reqs(sbi);
1823261eeb9cSDaeho Jeong 
1824261eeb9cSDaeho Jeong 	wait_event_interruptible(*q,
1825261eeb9cSDaeho Jeong 		kthread_should_stop() || !llist_empty(&cprc->issue_list));
1826261eeb9cSDaeho Jeong 	goto repeat;
1827261eeb9cSDaeho Jeong }
1828261eeb9cSDaeho Jeong 
1829261eeb9cSDaeho Jeong static void flush_remained_ckpt_reqs(struct f2fs_sb_info *sbi,
1830261eeb9cSDaeho Jeong 		struct ckpt_req *wait_req)
1831261eeb9cSDaeho Jeong {
1832261eeb9cSDaeho Jeong 	struct ckpt_req_control *cprc = &sbi->cprc_info;
1833261eeb9cSDaeho Jeong 
1834261eeb9cSDaeho Jeong 	if (!llist_empty(&cprc->issue_list)) {
1835261eeb9cSDaeho Jeong 		__checkpoint_and_complete_reqs(sbi);
1836261eeb9cSDaeho Jeong 	} else {
1837261eeb9cSDaeho Jeong 		/* already dispatched by issue_checkpoint_thread */
1838261eeb9cSDaeho Jeong 		if (wait_req)
1839261eeb9cSDaeho Jeong 			wait_for_completion(&wait_req->wait);
1840261eeb9cSDaeho Jeong 	}
1841261eeb9cSDaeho Jeong }
1842261eeb9cSDaeho Jeong 
1843261eeb9cSDaeho Jeong static void init_ckpt_req(struct ckpt_req *req)
1844261eeb9cSDaeho Jeong {
1845261eeb9cSDaeho Jeong 	memset(req, 0, sizeof(struct ckpt_req));
1846261eeb9cSDaeho Jeong 
1847261eeb9cSDaeho Jeong 	init_completion(&req->wait);
1848261eeb9cSDaeho Jeong 	req->queue_time = ktime_get();
1849261eeb9cSDaeho Jeong }
1850261eeb9cSDaeho Jeong 
1851261eeb9cSDaeho Jeong int f2fs_issue_checkpoint(struct f2fs_sb_info *sbi)
1852261eeb9cSDaeho Jeong {
1853261eeb9cSDaeho Jeong 	struct ckpt_req_control *cprc = &sbi->cprc_info;
1854261eeb9cSDaeho Jeong 	struct ckpt_req req;
1855261eeb9cSDaeho Jeong 	struct cp_control cpc;
1856261eeb9cSDaeho Jeong 
1857261eeb9cSDaeho Jeong 	cpc.reason = __get_cp_reason(sbi);
1858261eeb9cSDaeho Jeong 	if (!test_opt(sbi, MERGE_CHECKPOINT) || cpc.reason != CP_SYNC) {
1859261eeb9cSDaeho Jeong 		int ret;
1860261eeb9cSDaeho Jeong 
1861e4544b63STim Murray 		f2fs_down_write(&sbi->gc_lock);
1862261eeb9cSDaeho Jeong 		ret = f2fs_write_checkpoint(sbi, &cpc);
1863e4544b63STim Murray 		f2fs_up_write(&sbi->gc_lock);
1864261eeb9cSDaeho Jeong 
1865261eeb9cSDaeho Jeong 		return ret;
1866261eeb9cSDaeho Jeong 	}
1867261eeb9cSDaeho Jeong 
1868261eeb9cSDaeho Jeong 	if (!cprc->f2fs_issue_ckpt)
1869261eeb9cSDaeho Jeong 		return __write_checkpoint_sync(sbi);
1870261eeb9cSDaeho Jeong 
1871261eeb9cSDaeho Jeong 	init_ckpt_req(&req);
1872261eeb9cSDaeho Jeong 
1873261eeb9cSDaeho Jeong 	llist_add(&req.llnode, &cprc->issue_list);
1874261eeb9cSDaeho Jeong 	atomic_inc(&cprc->queued_ckpt);
1875261eeb9cSDaeho Jeong 
18763b42c741SChao Yu 	/*
18773b42c741SChao Yu 	 * update issue_list before we wake up issue_checkpoint thread,
18783b42c741SChao Yu 	 * this smp_mb() pairs with another barrier in ___wait_event(),
18793b42c741SChao Yu 	 * see more details in comments of waitqueue_active().
18803b42c741SChao Yu 	 */
1881261eeb9cSDaeho Jeong 	smp_mb();
1882261eeb9cSDaeho Jeong 
1883261eeb9cSDaeho Jeong 	if (waitqueue_active(&cprc->ckpt_wait_queue))
1884261eeb9cSDaeho Jeong 		wake_up(&cprc->ckpt_wait_queue);
1885261eeb9cSDaeho Jeong 
1886261eeb9cSDaeho Jeong 	if (cprc->f2fs_issue_ckpt)
1887261eeb9cSDaeho Jeong 		wait_for_completion(&req.wait);
1888261eeb9cSDaeho Jeong 	else
1889261eeb9cSDaeho Jeong 		flush_remained_ckpt_reqs(sbi, &req);
1890261eeb9cSDaeho Jeong 
1891261eeb9cSDaeho Jeong 	return req.ret;
1892261eeb9cSDaeho Jeong }
1893261eeb9cSDaeho Jeong 
1894261eeb9cSDaeho Jeong int f2fs_start_ckpt_thread(struct f2fs_sb_info *sbi)
1895261eeb9cSDaeho Jeong {
1896261eeb9cSDaeho Jeong 	dev_t dev = sbi->sb->s_bdev->bd_dev;
1897261eeb9cSDaeho Jeong 	struct ckpt_req_control *cprc = &sbi->cprc_info;
1898261eeb9cSDaeho Jeong 
1899261eeb9cSDaeho Jeong 	if (cprc->f2fs_issue_ckpt)
1900261eeb9cSDaeho Jeong 		return 0;
1901261eeb9cSDaeho Jeong 
1902261eeb9cSDaeho Jeong 	cprc->f2fs_issue_ckpt = kthread_run(issue_checkpoint_thread, sbi,
1903261eeb9cSDaeho Jeong 			"f2fs_ckpt-%u:%u", MAJOR(dev), MINOR(dev));
1904261eeb9cSDaeho Jeong 	if (IS_ERR(cprc->f2fs_issue_ckpt)) {
1905*146dbcbfSYangtao Li 		int err = PTR_ERR(cprc->f2fs_issue_ckpt);
1906*146dbcbfSYangtao Li 
1907261eeb9cSDaeho Jeong 		cprc->f2fs_issue_ckpt = NULL;
1908*146dbcbfSYangtao Li 		return err;
1909261eeb9cSDaeho Jeong 	}
1910261eeb9cSDaeho Jeong 
1911e6592066SDaeho Jeong 	set_task_ioprio(cprc->f2fs_issue_ckpt, cprc->ckpt_thread_ioprio);
1912261eeb9cSDaeho Jeong 
1913261eeb9cSDaeho Jeong 	return 0;
1914261eeb9cSDaeho Jeong }
1915261eeb9cSDaeho Jeong 
1916261eeb9cSDaeho Jeong void f2fs_stop_ckpt_thread(struct f2fs_sb_info *sbi)
1917261eeb9cSDaeho Jeong {
1918261eeb9cSDaeho Jeong 	struct ckpt_req_control *cprc = &sbi->cprc_info;
1919c7b58576SJaegeuk Kim 	struct task_struct *ckpt_task;
1920261eeb9cSDaeho Jeong 
1921c7b58576SJaegeuk Kim 	if (!cprc->f2fs_issue_ckpt)
1922c7b58576SJaegeuk Kim 		return;
1923261eeb9cSDaeho Jeong 
1924c7b58576SJaegeuk Kim 	ckpt_task = cprc->f2fs_issue_ckpt;
1925261eeb9cSDaeho Jeong 	cprc->f2fs_issue_ckpt = NULL;
1926261eeb9cSDaeho Jeong 	kthread_stop(ckpt_task);
1927261eeb9cSDaeho Jeong 
1928c7b58576SJaegeuk Kim 	f2fs_flush_ckpt_thread(sbi);
1929261eeb9cSDaeho Jeong }
1930c7b58576SJaegeuk Kim 
1931c7b58576SJaegeuk Kim void f2fs_flush_ckpt_thread(struct f2fs_sb_info *sbi)
1932c7b58576SJaegeuk Kim {
1933c7b58576SJaegeuk Kim 	struct ckpt_req_control *cprc = &sbi->cprc_info;
1934c7b58576SJaegeuk Kim 
1935c7b58576SJaegeuk Kim 	flush_remained_ckpt_reqs(sbi, NULL);
1936c7b58576SJaegeuk Kim 
1937c7b58576SJaegeuk Kim 	/* Let's wait for the previous dispatched checkpoint. */
1938c7b58576SJaegeuk Kim 	while (atomic_read(&cprc->queued_ckpt))
1939c7b58576SJaegeuk Kim 		io_schedule_timeout(DEFAULT_IO_TIMEOUT);
1940261eeb9cSDaeho Jeong }
1941261eeb9cSDaeho Jeong 
1942261eeb9cSDaeho Jeong void f2fs_init_ckpt_req_control(struct f2fs_sb_info *sbi)
1943261eeb9cSDaeho Jeong {
1944261eeb9cSDaeho Jeong 	struct ckpt_req_control *cprc = &sbi->cprc_info;
1945261eeb9cSDaeho Jeong 
1946261eeb9cSDaeho Jeong 	atomic_set(&cprc->issued_ckpt, 0);
1947261eeb9cSDaeho Jeong 	atomic_set(&cprc->total_ckpt, 0);
1948261eeb9cSDaeho Jeong 	atomic_set(&cprc->queued_ckpt, 0);
1949e6592066SDaeho Jeong 	cprc->ckpt_thread_ioprio = DEFAULT_CHECKPOINT_IOPRIO;
1950261eeb9cSDaeho Jeong 	init_waitqueue_head(&cprc->ckpt_wait_queue);
1951261eeb9cSDaeho Jeong 	init_llist_head(&cprc->issue_list);
1952261eeb9cSDaeho Jeong 	spin_lock_init(&cprc->stat_lock);
1953261eeb9cSDaeho Jeong }
1954