xref: /openbmc/linux/fs/f2fs/checkpoint.c (revision 68f0453dabdb99e76f75ce52ce40bc4bc71389a8)
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,
732eae077eSChao Yu 		.is_por = !is_meta ? 1 : 0,
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 {
174c40e15a9SYangtao Li 	if (time_to_inject(sbi, FAULT_BLKADDR))
17518792e64SChao Yu 		return false;
17618792e64SChao Yu 
177662befdaSChao Yu 	switch (type) {
178662befdaSChao Yu 	case META_NAT:
17966b00c18SChao Yu 		break;
180662befdaSChao Yu 	case META_SIT:
18166b00c18SChao Yu 		if (unlikely(blkaddr >= SIT_BLK_CNT(sbi)))
18266b00c18SChao Yu 			return false;
18366b00c18SChao Yu 		break;
18481c1a0f1SChao Yu 	case META_SSA:
18566b00c18SChao Yu 		if (unlikely(blkaddr >= MAIN_BLKADDR(sbi) ||
18666b00c18SChao Yu 			blkaddr < SM_I(sbi)->ssa_blkaddr))
18766b00c18SChao Yu 			return false;
18866b00c18SChao Yu 		break;
189662befdaSChao Yu 	case META_CP:
19066b00c18SChao Yu 		if (unlikely(blkaddr >= SIT_I(sbi)->sit_base_addr ||
19166b00c18SChao Yu 			blkaddr < __start_cp_addr(sbi)))
19266b00c18SChao Yu 			return false;
19366b00c18SChao Yu 		break;
1944c521f49SJaegeuk Kim 	case META_POR:
19593770ab7SChao Yu 		if (unlikely(blkaddr >= MAX_BLKADDR(sbi) ||
19693770ab7SChao Yu 			blkaddr < MAIN_BLKADDR(sbi)))
19793770ab7SChao Yu 			return false;
19893770ab7SChao Yu 		break;
199e1da7872SChao Yu 	case DATA_GENERIC:
20093770ab7SChao Yu 	case DATA_GENERIC_ENHANCE:
20193770ab7SChao Yu 	case DATA_GENERIC_ENHANCE_READ:
2020ef4ca04SChao Yu 	case DATA_GENERIC_ENHANCE_UPDATE:
20366b00c18SChao Yu 		if (unlikely(blkaddr >= MAX_BLKADDR(sbi) ||
204c9b60788SChao Yu 				blkaddr < MAIN_BLKADDR(sbi))) {
205dcbb4c10SJoe Perches 			f2fs_warn(sbi, "access invalid blkaddr:%u",
206dcbb4c10SJoe Perches 				  blkaddr);
20793770ab7SChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);
208dc2f78e2SDongliang Mu 			dump_stack();
20966b00c18SChao Yu 			return false;
21093770ab7SChao Yu 		} else {
21193770ab7SChao Yu 			return __is_bitmap_valid(sbi, blkaddr, type);
212c9b60788SChao Yu 		}
21366b00c18SChao Yu 		break;
214e1da7872SChao Yu 	case META_GENERIC:
215e1da7872SChao Yu 		if (unlikely(blkaddr < SEG0_BLKADDR(sbi) ||
216e1da7872SChao Yu 			blkaddr >= MAIN_BLKADDR(sbi)))
217e1da7872SChao Yu 			return false;
218e1da7872SChao Yu 		break;
219662befdaSChao Yu 	default:
220662befdaSChao Yu 		BUG();
221662befdaSChao Yu 	}
22266b00c18SChao Yu 
22366b00c18SChao Yu 	return true;
224662befdaSChao Yu }
225662befdaSChao Yu 
226662befdaSChao Yu /*
2277a88ddb5SChao Yu  * Readahead CP/NAT/SIT/SSA/POR pages
228662befdaSChao Yu  */
2294d57b86dSChao Yu int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
23026879fb1SChao Yu 							int type, bool sync)
231662befdaSChao Yu {
232662befdaSChao Yu 	struct page *page;
2334c521f49SJaegeuk Kim 	block_t blkno = start;
234662befdaSChao Yu 	struct f2fs_io_info fio = {
23505ca3632SJaegeuk Kim 		.sbi = sbi,
236662befdaSChao Yu 		.type = META,
23704d328deSMike Christie 		.op = REQ_OP_READ,
23870fd7614SChristoph Hellwig 		.op_flags = sync ? (REQ_META | REQ_PRIO) : REQ_RAHEAD,
2394375a336SJaegeuk Kim 		.encrypted_page = NULL,
2402eae077eSChao Yu 		.in_list = 0,
2412eae077eSChao Yu 		.is_por = (type == META_POR) ? 1 : 0,
242662befdaSChao Yu 	};
243e9f5b8b8SChao Yu 	struct blk_plug plug;
244ce4c638cSChao Yu 	int err;
245662befdaSChao Yu 
2462b947003SChao Yu 	if (unlikely(type == META_POR))
24704d328deSMike Christie 		fio.op_flags &= ~REQ_META;
2482b947003SChao Yu 
249e9f5b8b8SChao Yu 	blk_start_plug(&plug);
250662befdaSChao Yu 	for (; nrpages-- > 0; blkno++) {
251662befdaSChao Yu 
252e1da7872SChao Yu 		if (!f2fs_is_valid_blkaddr(sbi, blkno, type))
25366b00c18SChao Yu 			goto out;
25466b00c18SChao Yu 
255662befdaSChao Yu 		switch (type) {
256662befdaSChao Yu 		case META_NAT:
25766b00c18SChao Yu 			if (unlikely(blkno >=
25866b00c18SChao Yu 					NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid)))
259662befdaSChao Yu 				blkno = 0;
26066b00c18SChao Yu 			/* get nat block addr */
2617a9d7548SChao Yu 			fio.new_blkaddr = current_nat_addr(sbi,
262662befdaSChao Yu 					blkno * NAT_ENTRY_PER_BLOCK);
263662befdaSChao Yu 			break;
264662befdaSChao Yu 		case META_SIT:
2656a257471SChao Yu 			if (unlikely(blkno >= TOTAL_SEGS(sbi)))
2666a257471SChao Yu 				goto out;
267662befdaSChao Yu 			/* get sit block addr */
2687a9d7548SChao Yu 			fio.new_blkaddr = current_sit_addr(sbi,
269662befdaSChao Yu 					blkno * SIT_ENTRY_PER_BLOCK);
270662befdaSChao Yu 			break;
27181c1a0f1SChao Yu 		case META_SSA:
272662befdaSChao Yu 		case META_CP:
2734c521f49SJaegeuk Kim 		case META_POR:
2747a9d7548SChao Yu 			fio.new_blkaddr = blkno;
275662befdaSChao Yu 			break;
276662befdaSChao Yu 		default:
277662befdaSChao Yu 			BUG();
278662befdaSChao Yu 		}
279662befdaSChao Yu 
280300e129cSJaegeuk Kim 		page = f2fs_grab_cache_page(META_MAPPING(sbi),
281300e129cSJaegeuk Kim 						fio.new_blkaddr, false);
282662befdaSChao Yu 		if (!page)
283662befdaSChao Yu 			continue;
284662befdaSChao Yu 		if (PageUptodate(page)) {
285662befdaSChao Yu 			f2fs_put_page(page, 1);
286662befdaSChao Yu 			continue;
287662befdaSChao Yu 		}
288662befdaSChao Yu 
28905ca3632SJaegeuk Kim 		fio.page = page;
290ce4c638cSChao Yu 		err = f2fs_submit_page_bio(&fio);
291ce4c638cSChao Yu 		f2fs_put_page(page, err ? 1 : 0);
2928b83ac81SChao Yu 
2938b83ac81SChao Yu 		if (!err)
29434a23525SChao Yu 			f2fs_update_iostat(sbi, NULL, FS_META_READ_IO,
29534a23525SChao Yu 							F2FS_BLKSIZE);
296662befdaSChao Yu 	}
297662befdaSChao Yu out:
298e9f5b8b8SChao Yu 	blk_finish_plug(&plug);
299662befdaSChao Yu 	return blkno - start;
300662befdaSChao Yu }
301662befdaSChao Yu 
302430f163bSChao Yu void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index,
303430f163bSChao Yu 							unsigned int ra_blocks)
304635aee1fSChao Yu {
305635aee1fSChao Yu 	struct page *page;
306635aee1fSChao Yu 	bool readahead = false;
307635aee1fSChao Yu 
308430f163bSChao Yu 	if (ra_blocks == RECOVERY_MIN_RA_BLOCKS)
309430f163bSChao Yu 		return;
310430f163bSChao Yu 
311635aee1fSChao Yu 	page = find_get_page(META_MAPPING(sbi), index);
3124da7bf5aSJaegeuk Kim 	if (!page || !PageUptodate(page))
313635aee1fSChao Yu 		readahead = true;
314635aee1fSChao Yu 	f2fs_put_page(page, 0);
315635aee1fSChao Yu 
316635aee1fSChao Yu 	if (readahead)
317430f163bSChao Yu 		f2fs_ra_meta_pages(sbi, index, ra_blocks, META_POR, true);
318635aee1fSChao Yu }
319635aee1fSChao Yu 
320b0af6d49SChao Yu static int __f2fs_write_meta_page(struct page *page,
321b0af6d49SChao Yu 				struct writeback_control *wbc,
322b0af6d49SChao Yu 				enum iostat_type io_type)
323127e670aSJaegeuk Kim {
3244081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_P_SB(page);
325577e3495SJaegeuk Kim 
326ecda0de3SChao Yu 	trace_f2fs_writepage(page, META);
327ecda0de3SChao Yu 
328af697c0fSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
329af697c0fSJaegeuk Kim 		goto redirty_out;
330caf0047eSChao Yu 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
331cfb271d4SChao Yu 		goto redirty_out;
332857dc4e0SJaegeuk Kim 	if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0))
333cfb271d4SChao Yu 		goto redirty_out;
334127e670aSJaegeuk Kim 
3354d57b86dSChao Yu 	f2fs_do_write_meta_page(sbi, page, io_type);
336127e670aSJaegeuk Kim 	dec_page_count(sbi, F2FS_DIRTY_META);
3370c3a5797SChao Yu 
3380c3a5797SChao Yu 	if (wbc->for_reclaim)
339bab475c5SChao Yu 		f2fs_submit_merged_write_cond(sbi, NULL, page, 0, META);
3400c3a5797SChao Yu 
341127e670aSJaegeuk Kim 	unlock_page(page);
342857dc4e0SJaegeuk Kim 
3430c3a5797SChao Yu 	if (unlikely(f2fs_cp_error(sbi)))
344b9109b0eSJaegeuk Kim 		f2fs_submit_merged_write(sbi, META);
3450c3a5797SChao Yu 
346577e3495SJaegeuk Kim 	return 0;
347cfb271d4SChao Yu 
348cfb271d4SChao Yu redirty_out:
34976f60268SJaegeuk Kim 	redirty_page_for_writepage(wbc, page);
350cfb271d4SChao Yu 	return AOP_WRITEPAGE_ACTIVATE;
351127e670aSJaegeuk Kim }
352127e670aSJaegeuk Kim 
353b0af6d49SChao Yu static int f2fs_write_meta_page(struct page *page,
354b0af6d49SChao Yu 				struct writeback_control *wbc)
355b0af6d49SChao Yu {
356b0af6d49SChao Yu 	return __f2fs_write_meta_page(page, wbc, FS_META_IO);
357b0af6d49SChao Yu }
358b0af6d49SChao Yu 
359127e670aSJaegeuk Kim static int f2fs_write_meta_pages(struct address_space *mapping,
360127e670aSJaegeuk Kim 				struct writeback_control *wbc)
361127e670aSJaegeuk Kim {
3624081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
36350c8cdb3SJaegeuk Kim 	long diff, written;
364127e670aSJaegeuk Kim 
3650771fcc7SChao Yu 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
3660771fcc7SChao Yu 		goto skip_write;
3670771fcc7SChao Yu 
3685459aa97SJaegeuk Kim 	/* collect a number of dirty meta pages and write together */
369812a9597SJaegeuk Kim 	if (wbc->sync_mode != WB_SYNC_ALL &&
370812a9597SJaegeuk Kim 			get_pages(sbi, F2FS_DIRTY_META) <
371812a9597SJaegeuk Kim 					nr_pages_to_skip(sbi, META))
372d3baf95dSJaegeuk Kim 		goto skip_write;
373127e670aSJaegeuk Kim 
374a29d0e0bSYunlei He 	/* if locked failed, cp will flush dirty pages instead */
375e4544b63STim Murray 	if (!f2fs_down_write_trylock(&sbi->cp_global_sem))
376a29d0e0bSYunlei He 		goto skip_write;
377d31c7c3fSYunlei He 
378a29d0e0bSYunlei He 	trace_f2fs_writepages(mapping->host, wbc, META);
37950c8cdb3SJaegeuk Kim 	diff = nr_pages_to_write(sbi, META, wbc);
3804d57b86dSChao Yu 	written = f2fs_sync_meta_pages(sbi, META, wbc->nr_to_write, FS_META_IO);
381e4544b63STim Murray 	f2fs_up_write(&sbi->cp_global_sem);
38250c8cdb3SJaegeuk Kim 	wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff);
383127e670aSJaegeuk Kim 	return 0;
384d3baf95dSJaegeuk Kim 
385d3baf95dSJaegeuk Kim skip_write:
386d3baf95dSJaegeuk Kim 	wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META);
387d31c7c3fSYunlei He 	trace_f2fs_writepages(mapping->host, wbc, META);
388d3baf95dSJaegeuk Kim 	return 0;
389127e670aSJaegeuk Kim }
390127e670aSJaegeuk Kim 
3914d57b86dSChao Yu long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
392b0af6d49SChao Yu 				long nr_to_write, enum iostat_type io_type)
393127e670aSJaegeuk Kim {
3949df27d98SGu Zheng 	struct address_space *mapping = META_MAPPING(sbi);
395028a63a6SJan Kara 	pgoff_t index = 0, prev = ULONG_MAX;
396580e7a49SVishal Moola (Oracle) 	struct folio_batch fbatch;
397127e670aSJaegeuk Kim 	long nwritten = 0;
398580e7a49SVishal Moola (Oracle) 	int nr_folios;
399127e670aSJaegeuk Kim 	struct writeback_control wbc = {
400127e670aSJaegeuk Kim 		.for_reclaim = 0,
401127e670aSJaegeuk Kim 	};
402e9f5b8b8SChao Yu 	struct blk_plug plug;
403127e670aSJaegeuk Kim 
404580e7a49SVishal Moola (Oracle) 	folio_batch_init(&fbatch);
405127e670aSJaegeuk Kim 
406e9f5b8b8SChao Yu 	blk_start_plug(&plug);
407e9f5b8b8SChao Yu 
408580e7a49SVishal Moola (Oracle) 	while ((nr_folios = filemap_get_folios_tag(mapping, &index,
409580e7a49SVishal Moola (Oracle) 					(pgoff_t)-1,
410580e7a49SVishal Moola (Oracle) 					PAGECACHE_TAG_DIRTY, &fbatch))) {
411028a63a6SJan Kara 		int i;
412127e670aSJaegeuk Kim 
413580e7a49SVishal Moola (Oracle) 		for (i = 0; i < nr_folios; i++) {
414580e7a49SVishal Moola (Oracle) 			struct folio *folio = fbatch.folios[i];
415203681f6SJaegeuk Kim 
416580e7a49SVishal Moola (Oracle) 			if (nr_to_write != LONG_MAX && i != 0 &&
417580e7a49SVishal Moola (Oracle) 					folio->index != prev +
418580e7a49SVishal Moola (Oracle) 					folio_nr_pages(fbatch.folios[i-1])) {
419580e7a49SVishal Moola (Oracle) 				folio_batch_release(&fbatch);
4206066d8cdSJaegeuk Kim 				goto stop;
4216066d8cdSJaegeuk Kim 			}
4226066d8cdSJaegeuk Kim 
423580e7a49SVishal Moola (Oracle) 			folio_lock(folio);
424203681f6SJaegeuk Kim 
425580e7a49SVishal Moola (Oracle) 			if (unlikely(folio->mapping != mapping)) {
426203681f6SJaegeuk Kim continue_unlock:
427580e7a49SVishal Moola (Oracle) 				folio_unlock(folio);
428203681f6SJaegeuk Kim 				continue;
429203681f6SJaegeuk Kim 			}
430580e7a49SVishal Moola (Oracle) 			if (!folio_test_dirty(folio)) {
431203681f6SJaegeuk Kim 				/* someone wrote it for us */
432203681f6SJaegeuk Kim 				goto continue_unlock;
433203681f6SJaegeuk Kim 			}
434203681f6SJaegeuk Kim 
435580e7a49SVishal Moola (Oracle) 			f2fs_wait_on_page_writeback(&folio->page, META,
436580e7a49SVishal Moola (Oracle) 					true, true);
437fa3d2bdfSJaegeuk Kim 
438580e7a49SVishal Moola (Oracle) 			if (!folio_clear_dirty_for_io(folio))
439203681f6SJaegeuk Kim 				goto continue_unlock;
440203681f6SJaegeuk Kim 
441580e7a49SVishal Moola (Oracle) 			if (__f2fs_write_meta_page(&folio->page, &wbc,
442580e7a49SVishal Moola (Oracle) 						io_type)) {
443580e7a49SVishal Moola (Oracle) 				folio_unlock(folio);
444577e3495SJaegeuk Kim 				break;
445577e3495SJaegeuk Kim 			}
446580e7a49SVishal Moola (Oracle) 			nwritten += folio_nr_pages(folio);
447580e7a49SVishal Moola (Oracle) 			prev = folio->index;
448cfb271d4SChao Yu 			if (unlikely(nwritten >= nr_to_write))
449127e670aSJaegeuk Kim 				break;
450127e670aSJaegeuk Kim 		}
451580e7a49SVishal Moola (Oracle) 		folio_batch_release(&fbatch);
452127e670aSJaegeuk Kim 		cond_resched();
453127e670aSJaegeuk Kim 	}
4546066d8cdSJaegeuk Kim stop:
455127e670aSJaegeuk Kim 	if (nwritten)
456b9109b0eSJaegeuk Kim 		f2fs_submit_merged_write(sbi, type);
457127e670aSJaegeuk Kim 
458e9f5b8b8SChao Yu 	blk_finish_plug(&plug);
459e9f5b8b8SChao Yu 
460127e670aSJaegeuk Kim 	return nwritten;
461127e670aSJaegeuk Kim }
462127e670aSJaegeuk Kim 
4631d9ac659SMatthew Wilcox (Oracle) static bool f2fs_dirty_meta_folio(struct address_space *mapping,
4641d9ac659SMatthew Wilcox (Oracle) 		struct folio *folio)
465127e670aSJaegeuk Kim {
4661d9ac659SMatthew Wilcox (Oracle) 	trace_f2fs_set_page_dirty(&folio->page, META);
46726c6b887SJaegeuk Kim 
4681d9ac659SMatthew Wilcox (Oracle) 	if (!folio_test_uptodate(folio))
4691d9ac659SMatthew Wilcox (Oracle) 		folio_mark_uptodate(folio);
4709b7eadd9SShuqi Zhang 	if (filemap_dirty_folio(mapping, folio)) {
47129c87793SMatthew Wilcox (Oracle) 		inc_page_count(F2FS_M_SB(mapping), F2FS_DIRTY_META);
4721d9ac659SMatthew Wilcox (Oracle) 		set_page_private_reference(&folio->page);
4731d9ac659SMatthew Wilcox (Oracle) 		return true;
474127e670aSJaegeuk Kim 	}
4751d9ac659SMatthew Wilcox (Oracle) 	return false;
476127e670aSJaegeuk Kim }
477127e670aSJaegeuk Kim 
478127e670aSJaegeuk Kim const struct address_space_operations f2fs_meta_aops = {
479127e670aSJaegeuk Kim 	.writepage	= f2fs_write_meta_page,
480127e670aSJaegeuk Kim 	.writepages	= f2fs_write_meta_pages,
4811d9ac659SMatthew Wilcox (Oracle) 	.dirty_folio	= f2fs_dirty_meta_folio,
48291503996SMatthew Wilcox (Oracle) 	.invalidate_folio = f2fs_invalidate_folio,
483c26cd045SMatthew Wilcox (Oracle) 	.release_folio	= f2fs_release_folio,
4841d5b9bd6SMatthew Wilcox (Oracle) 	.migrate_folio	= filemap_migrate_folio,
485127e670aSJaegeuk Kim };
486127e670aSJaegeuk Kim 
48739d787beSChao Yu static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino,
48839d787beSChao Yu 						unsigned int devidx, int type)
489953e6cc6SJaegeuk Kim {
49067298804SChao Yu 	struct inode_management *im = &sbi->im[type];
4914b106518SChao Yu 	struct ino_entry *e = NULL, *new = NULL;
49280c54505SJaegeuk Kim 
4934b106518SChao Yu 	if (type == FLUSH_INO) {
4944b106518SChao Yu 		rcu_read_lock();
4954b106518SChao Yu 		e = radix_tree_lookup(&im->ino_root, ino);
4964b106518SChao Yu 		rcu_read_unlock();
4974b106518SChao Yu 	}
4984b106518SChao Yu 
4994b106518SChao Yu retry:
5004b106518SChao Yu 	if (!e)
50132410577SChao Yu 		new = f2fs_kmem_cache_alloc(ino_entry_slab,
50232410577SChao Yu 						GFP_NOFS, true, NULL);
50319526d74SChao Yu 
50480c54505SJaegeuk Kim 	radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
505769ec6e5SJaegeuk Kim 
50667298804SChao Yu 	spin_lock(&im->ino_lock);
50767298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
50839efac41SJaegeuk Kim 	if (!e) {
5094b106518SChao Yu 		if (!new) {
5104b106518SChao Yu 			spin_unlock(&im->ino_lock);
5114b106518SChao Yu 			goto retry;
5124b106518SChao Yu 		}
5134b106518SChao Yu 		e = new;
51419526d74SChao Yu 		if (unlikely(radix_tree_insert(&im->ino_root, ino, e)))
51519526d74SChao Yu 			f2fs_bug_on(sbi, 1);
51619526d74SChao Yu 
51739efac41SJaegeuk Kim 		memset(e, 0, sizeof(struct ino_entry));
51839efac41SJaegeuk Kim 		e->ino = ino;
51939efac41SJaegeuk Kim 
52067298804SChao Yu 		list_add_tail(&e->list, &im->ino_list);
5218c402946SJaegeuk Kim 		if (type != ORPHAN_INO)
52267298804SChao Yu 			im->ino_num++;
52339efac41SJaegeuk Kim 	}
52439d787beSChao Yu 
52539d787beSChao Yu 	if (type == FLUSH_INO)
52639d787beSChao Yu 		f2fs_set_bit(devidx, (char *)&e->dirty_device);
52739d787beSChao Yu 
52867298804SChao Yu 	spin_unlock(&im->ino_lock);
529769ec6e5SJaegeuk Kim 	radix_tree_preload_end();
53080c54505SJaegeuk Kim 
5314b106518SChao Yu 	if (new && e != new)
5324b106518SChao Yu 		kmem_cache_free(ino_entry_slab, new);
533953e6cc6SJaegeuk Kim }
534953e6cc6SJaegeuk Kim 
5356451e041SJaegeuk Kim static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
536953e6cc6SJaegeuk Kim {
53767298804SChao Yu 	struct inode_management *im = &sbi->im[type];
5386451e041SJaegeuk Kim 	struct ino_entry *e;
539953e6cc6SJaegeuk Kim 
54067298804SChao Yu 	spin_lock(&im->ino_lock);
54167298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
54239efac41SJaegeuk Kim 	if (e) {
543953e6cc6SJaegeuk Kim 		list_del(&e->list);
54467298804SChao Yu 		radix_tree_delete(&im->ino_root, ino);
54567298804SChao Yu 		im->ino_num--;
54667298804SChao Yu 		spin_unlock(&im->ino_lock);
5476451e041SJaegeuk Kim 		kmem_cache_free(ino_entry_slab, e);
548953e6cc6SJaegeuk Kim 		return;
549953e6cc6SJaegeuk Kim 	}
55067298804SChao Yu 	spin_unlock(&im->ino_lock);
551953e6cc6SJaegeuk Kim }
552953e6cc6SJaegeuk Kim 
5534d57b86dSChao Yu void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
554fff04f90SJaegeuk Kim {
555fff04f90SJaegeuk Kim 	/* add new dirty ino entry into list */
55639d787beSChao Yu 	__add_ino_entry(sbi, ino, 0, type);
557fff04f90SJaegeuk Kim }
558fff04f90SJaegeuk Kim 
5594d57b86dSChao Yu void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
560fff04f90SJaegeuk Kim {
561fff04f90SJaegeuk Kim 	/* remove dirty ino entry from list */
562fff04f90SJaegeuk Kim 	__remove_ino_entry(sbi, ino, type);
563fff04f90SJaegeuk Kim }
564fff04f90SJaegeuk Kim 
5651f07cc58SJack Qiu /* mode should be APPEND_INO, UPDATE_INO or TRANS_DIR_INO */
5664d57b86dSChao Yu bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode)
567fff04f90SJaegeuk Kim {
56867298804SChao Yu 	struct inode_management *im = &sbi->im[mode];
569fff04f90SJaegeuk Kim 	struct ino_entry *e;
57067298804SChao Yu 
57167298804SChao Yu 	spin_lock(&im->ino_lock);
57267298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
57367298804SChao Yu 	spin_unlock(&im->ino_lock);
574fff04f90SJaegeuk Kim 	return e ? true : false;
575fff04f90SJaegeuk Kim }
576fff04f90SJaegeuk Kim 
5774d57b86dSChao Yu void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all)
578fff04f90SJaegeuk Kim {
579fff04f90SJaegeuk Kim 	struct ino_entry *e, *tmp;
580fff04f90SJaegeuk Kim 	int i;
581fff04f90SJaegeuk Kim 
58239d787beSChao Yu 	for (i = all ? ORPHAN_INO : APPEND_INO; i < MAX_INO_ENTRY; i++) {
58367298804SChao Yu 		struct inode_management *im = &sbi->im[i];
58467298804SChao Yu 
58567298804SChao Yu 		spin_lock(&im->ino_lock);
58667298804SChao Yu 		list_for_each_entry_safe(e, tmp, &im->ino_list, list) {
587fff04f90SJaegeuk Kim 			list_del(&e->list);
58867298804SChao Yu 			radix_tree_delete(&im->ino_root, e->ino);
589fff04f90SJaegeuk Kim 			kmem_cache_free(ino_entry_slab, e);
59067298804SChao Yu 			im->ino_num--;
591fff04f90SJaegeuk Kim 		}
59267298804SChao Yu 		spin_unlock(&im->ino_lock);
593fff04f90SJaegeuk Kim 	}
594fff04f90SJaegeuk Kim }
595fff04f90SJaegeuk Kim 
5964d57b86dSChao Yu void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
59739d787beSChao Yu 					unsigned int devidx, int type)
59839d787beSChao Yu {
59939d787beSChao Yu 	__add_ino_entry(sbi, ino, devidx, type);
60039d787beSChao Yu }
60139d787beSChao Yu 
6024d57b86dSChao Yu bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
60339d787beSChao Yu 					unsigned int devidx, int type)
60439d787beSChao Yu {
60539d787beSChao Yu 	struct inode_management *im = &sbi->im[type];
60639d787beSChao Yu 	struct ino_entry *e;
60739d787beSChao Yu 	bool is_dirty = false;
60839d787beSChao Yu 
60939d787beSChao Yu 	spin_lock(&im->ino_lock);
61039d787beSChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
61139d787beSChao Yu 	if (e && f2fs_test_bit(devidx, (char *)&e->dirty_device))
61239d787beSChao Yu 		is_dirty = true;
61339d787beSChao Yu 	spin_unlock(&im->ino_lock);
61439d787beSChao Yu 	return is_dirty;
61539d787beSChao Yu }
61639d787beSChao Yu 
6174d57b86dSChao Yu int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi)
618127e670aSJaegeuk Kim {
61967298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
620127e670aSJaegeuk Kim 	int err = 0;
621127e670aSJaegeuk Kim 
62267298804SChao Yu 	spin_lock(&im->ino_lock);
623cb78942bSJaegeuk Kim 
6241ecc0c5cSChao Yu 	if (time_to_inject(sbi, FAULT_ORPHAN)) {
625cb78942bSJaegeuk Kim 		spin_unlock(&im->ino_lock);
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 
718*68f0453dSChao Yu 	if (f2fs_hw_is_readonly(sbi)) {
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 
798146949deSJinyoung CHOI 	/* loop for each orphan inode entry and write them in journal 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)
985447286ebSYangtao Li 		cp_blk_no += BIT(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
1128146949deSJinyoung CHOI 		 * writebacking 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 
113660630375SLi Zetao static 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 	/*
1406580e7a49SVishal Moola (Oracle) 	 * filemap_get_folios_tag and lock_page again will take
14074d57b86dSChao Yu 	 * some extra time. Therefore, f2fs_update_meta_pages and
14084d57b86dSChao Yu 	 * f2fs_sync_meta_pages are combined in this function.
140946706d59SGao Xiang 	 */
14104d57b86dSChao Yu 	struct page *page = f2fs_grab_meta_page(sbi, blk_addr);
141146706d59SGao Xiang 	int err;
141246706d59SGao Xiang 
1413bae0ee7aSChao Yu 	f2fs_wait_on_page_writeback(page, META, true, true);
14148d64d365SChao Yu 
14158d64d365SChao Yu 	memcpy(page_address(page), src, PAGE_SIZE);
14168d64d365SChao Yu 
14178d64d365SChao Yu 	set_page_dirty(page);
141846706d59SGao Xiang 	if (unlikely(!clear_page_dirty_for_io(page)))
141946706d59SGao Xiang 		f2fs_bug_on(sbi, 1);
142046706d59SGao Xiang 
142146706d59SGao Xiang 	/* writeout cp pack 2 page */
142246706d59SGao Xiang 	err = __f2fs_write_meta_page(page, &wbc, FS_CP_META_IO);
1423af697c0fSJaegeuk Kim 	if (unlikely(err && f2fs_cp_error(sbi))) {
1424af697c0fSJaegeuk Kim 		f2fs_put_page(page, 1);
1425af697c0fSJaegeuk Kim 		return;
1426af697c0fSJaegeuk Kim 	}
142746706d59SGao Xiang 
1428af697c0fSJaegeuk Kim 	f2fs_bug_on(sbi, err);
142946706d59SGao Xiang 	f2fs_put_page(page, 0);
143046706d59SGao Xiang 
143146706d59SGao Xiang 	/* submit checkpoint (with barrier if NOBARRIER is not set) */
143246706d59SGao Xiang 	f2fs_submit_merged_write(sbi, META_FLUSH);
143346706d59SGao Xiang }
143446706d59SGao Xiang 
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++) {
14765a4fed7cSChristoph Hellwig 		struct curseg_info *curseg = CURSEG_I(sbi, i + CURSEG_HOT_NODE);
14775a4fed7cSChristoph Hellwig 
14785a4fed7cSChristoph Hellwig 		ckpt->cur_node_segno[i] = cpu_to_le32(curseg->segno);
14795a4fed7cSChristoph Hellwig 		ckpt->cur_node_blkoff[i] = cpu_to_le16(curseg->next_blkoff);
14805a4fed7cSChristoph Hellwig 		ckpt->alloc_type[i + CURSEG_HOT_NODE] = curseg->alloc_type;
1481127e670aSJaegeuk Kim 	}
1482b5b82205SChao Yu 	for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
14835a4fed7cSChristoph Hellwig 		struct curseg_info *curseg = CURSEG_I(sbi, i + CURSEG_HOT_DATA);
14845a4fed7cSChristoph Hellwig 
14855a4fed7cSChristoph Hellwig 		ckpt->cur_data_segno[i] = cpu_to_le32(curseg->segno);
14865a4fed7cSChristoph Hellwig 		ckpt->cur_data_blkoff[i] = cpu_to_le16(curseg->next_blkoff);
14875a4fed7cSChristoph Hellwig 		ckpt->alloc_type[i + CURSEG_HOT_DATA] = curseg->alloc_type;
1488127e670aSJaegeuk Kim 	}
1489127e670aSJaegeuk Kim 
1490127e670aSJaegeuk Kim 	/* 2 cp + n data seg summary + orphan inode blocks */
14914d57b86dSChao Yu 	data_sum_blocks = f2fs_npages_for_summary_flush(sbi, false);
1492d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1493b5b82205SChao Yu 	if (data_sum_blocks < NR_CURSEG_DATA_TYPE)
1494aaec2b1dSChao Yu 		__set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
1495127e670aSJaegeuk Kim 	else
1496aaec2b1dSChao Yu 		__clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
1497d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1498127e670aSJaegeuk Kim 
149967298804SChao Yu 	orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num);
15001dbe4152SChangman Lee 	ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks +
15011dbe4152SChangman Lee 			orphan_blocks);
1502127e670aSJaegeuk Kim 
1503119ee914SJaegeuk Kim 	if (__remain_node_summaries(cpc->reason))
1504b5b82205SChao Yu 		ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
15051dbe4152SChangman Lee 				cp_payload_blks + data_sum_blocks +
15061dbe4152SChangman Lee 				orphan_blocks + NR_CURSEG_NODE_TYPE);
1507119ee914SJaegeuk Kim 	else
1508b5b82205SChao Yu 		ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
15091dbe4152SChangman Lee 				cp_payload_blks + data_sum_blocks +
15101dbe4152SChangman Lee 				orphan_blocks);
1511119ee914SJaegeuk Kim 
1512e4c5d848SJaegeuk Kim 	/* update ckpt flag for checkpoint */
1513e4c5d848SJaegeuk Kim 	update_ckpt_flags(sbi, cpc);
1514a468f0efSJaegeuk Kim 
1515127e670aSJaegeuk Kim 	/* update SIT/NAT bitmap */
1516127e670aSJaegeuk Kim 	get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
1517127e670aSJaegeuk Kim 	get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
1518127e670aSJaegeuk Kim 
1519d7eb8f1cSChao Yu 	crc32 = f2fs_checkpoint_chksum(sbi, ckpt);
15207e586fa0SJaegeuk Kim 	*((__le32 *)((unsigned char *)ckpt +
15217e586fa0SJaegeuk Kim 				le32_to_cpu(ckpt->checksum_offset)))
1522127e670aSJaegeuk Kim 				= cpu_to_le32(crc32);
1523127e670aSJaegeuk Kim 
15248508e44aSJaegeuk Kim 	start_blk = __start_cp_next_addr(sbi);
1525127e670aSJaegeuk Kim 
152622ad0b6aSJaegeuk Kim 	/* write nat bits */
152794c821fbSChao Yu 	if ((cpc->reason & CP_UMOUNT) &&
152894c821fbSChao Yu 			is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG)) {
152922ad0b6aSJaegeuk Kim 		__u64 cp_ver = cur_cp_version(ckpt);
153022ad0b6aSJaegeuk Kim 		block_t blk;
153122ad0b6aSJaegeuk Kim 
153222ad0b6aSJaegeuk Kim 		cp_ver |= ((__u64)crc32 << 32);
153322ad0b6aSJaegeuk Kim 		*(__le64 *)nm_i->nat_bits = cpu_to_le64(cp_ver);
153422ad0b6aSJaegeuk Kim 
153522ad0b6aSJaegeuk Kim 		blk = start_blk + sbi->blocks_per_seg - nm_i->nat_bits_blocks;
153622ad0b6aSJaegeuk Kim 		for (i = 0; i < nm_i->nat_bits_blocks; i++)
15374d57b86dSChao Yu 			f2fs_update_meta_page(sbi, nm_i->nat_bits +
153822ad0b6aSJaegeuk Kim 					(i << F2FS_BLKSIZE_BITS), blk + i);
153922ad0b6aSJaegeuk Kim 	}
154022ad0b6aSJaegeuk Kim 
1541127e670aSJaegeuk Kim 	/* write out checkpoint buffer at block 0 */
15424d57b86dSChao Yu 	f2fs_update_meta_page(sbi, ckpt, start_blk++);
1543127e670aSJaegeuk Kim 
1544381722d2SChao Yu 	for (i = 1; i < 1 + cp_payload_blks; i++)
15454d57b86dSChao Yu 		f2fs_update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE,
1546381722d2SChao Yu 							start_blk++);
15471dbe4152SChangman Lee 
154867298804SChao Yu 	if (orphan_num) {
1549127e670aSJaegeuk Kim 		write_orphan_inodes(sbi, start_blk);
1550127e670aSJaegeuk Kim 		start_blk += orphan_blocks;
1551127e670aSJaegeuk Kim 	}
1552127e670aSJaegeuk Kim 
15534d57b86dSChao Yu 	f2fs_write_data_summaries(sbi, start_blk);
1554127e670aSJaegeuk Kim 	start_blk += data_sum_blocks;
15558f1dbbbbSShuoran Liu 
15568f1dbbbbSShuoran Liu 	/* Record write statistics in the hot node summary */
15578f1dbbbbSShuoran Liu 	kbytes_written = sbi->kbytes_written;
15583a0a9cbcSChao Yu 	kbytes_written += (f2fs_get_sectors_written(sbi) -
15593a0a9cbcSChao Yu 				sbi->sectors_written_start) >> 1;
1560b7ad7512SChao Yu 	seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written);
15618f1dbbbbSShuoran Liu 
1562119ee914SJaegeuk Kim 	if (__remain_node_summaries(cpc->reason)) {
15634d57b86dSChao Yu 		f2fs_write_node_summaries(sbi, start_blk);
1564127e670aSJaegeuk Kim 		start_blk += NR_CURSEG_NODE_TYPE;
1565127e670aSJaegeuk Kim 	}
1566127e670aSJaegeuk Kim 
156746706d59SGao Xiang 	/* update user_block_counts */
156846706d59SGao Xiang 	sbi->last_valid_block_count = sbi->total_valid_block_count;
156946706d59SGao Xiang 	percpu_counter_set(&sbi->alloc_valid_block_count, 0);
157047c8ebccSJaegeuk Kim 	percpu_counter_set(&sbi->rf_node_block_count, 0);
1571127e670aSJaegeuk Kim 
157246706d59SGao Xiang 	/* Here, we have one bio having CP pack except cp pack 2 page */
15734d57b86dSChao Yu 	f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
1574bf22c3ccSSahitya Tummala 	/* Wait for all dirty meta pages to be submitted for IO */
1575bf22c3ccSSahitya Tummala 	f2fs_wait_on_all_pages(sbi, F2FS_DIRTY_META);
157646706d59SGao Xiang 
157746706d59SGao Xiang 	/* wait for previous submitted meta pages writeback */
1578bf22c3ccSSahitya Tummala 	f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
1579127e670aSJaegeuk Kim 
158046706d59SGao Xiang 	/* flush all device cache */
158146706d59SGao Xiang 	err = f2fs_flush_device_cache(sbi);
158246706d59SGao Xiang 	if (err)
158346706d59SGao Xiang 		return err;
1584127e670aSJaegeuk Kim 
158546706d59SGao Xiang 	/* barrier and flush checkpoint cp pack 2 page if it can */
158646706d59SGao Xiang 	commit_checkpoint(sbi, ckpt, start_blk);
1587bf22c3ccSSahitya Tummala 	f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
15886a8f8ca5SJaegeuk Kim 
158918767e62SChao Yu 	/*
1590542989b6SEric Biggers 	 * invalidate intermediate page cache borrowed from meta inode which are
1591aff6fbbeSChao Yu 	 * used for migration of encrypted, verity or compressed inode's blocks.
159218767e62SChao Yu 	 */
1593aff6fbbeSChao Yu 	if (f2fs_sb_has_encrypt(sbi) || f2fs_sb_has_verity(sbi) ||
1594aff6fbbeSChao Yu 		f2fs_sb_has_compression(sbi))
159518767e62SChao Yu 		invalidate_mapping_pages(META_MAPPING(sbi),
159618767e62SChao Yu 				MAIN_BLKADDR(sbi), MAX_BLKADDR(sbi) - 1);
159718767e62SChao Yu 
15984d57b86dSChao Yu 	f2fs_release_ino_entry(sbi, false);
1599cf779cabSJaegeuk Kim 
160050fa53ecSChao Yu 	f2fs_reset_fsync_node_info(sbi);
160150fa53ecSChao Yu 
1602caf0047eSChao Yu 	clear_sbi_flag(sbi, SBI_IS_DIRTY);
1603bbf156f7SJaegeuk Kim 	clear_sbi_flag(sbi, SBI_NEED_CP);
1604af033b2aSChao Yu 	clear_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH);
1605c9c8ed50SChao Yu 
1606c9c8ed50SChao Yu 	spin_lock(&sbi->stat_lock);
16074354994fSDaniel Rosenberg 	sbi->unusable_block_count = 0;
1608c9c8ed50SChao Yu 	spin_unlock(&sbi->stat_lock);
1609c9c8ed50SChao Yu 
16108508e44aSJaegeuk Kim 	__set_cp_next_pack(sbi);
1611c34f42e2SChao Yu 
1612c2a080aeSChao Yu 	/*
1613c2a080aeSChao Yu 	 * redirty superblock if metadata like node page or inode cache is
1614c2a080aeSChao Yu 	 * updated during writing checkpoint.
1615c2a080aeSChao Yu 	 */
1616c2a080aeSChao Yu 	if (get_pages(sbi, F2FS_DIRTY_NODES) ||
1617c2a080aeSChao Yu 			get_pages(sbi, F2FS_DIRTY_IMETA))
1618c2a080aeSChao Yu 		set_sbi_flag(sbi, SBI_IS_DIRTY);
1619c2a080aeSChao Yu 
1620c2a080aeSChao Yu 	f2fs_bug_on(sbi, get_pages(sbi, F2FS_DIRTY_DENTS));
1621c2a080aeSChao Yu 
1622af697c0fSJaegeuk Kim 	return unlikely(f2fs_cp_error(sbi)) ? -EIO : 0;
1623127e670aSJaegeuk Kim }
1624127e670aSJaegeuk Kim 
16254d57b86dSChao Yu int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1626127e670aSJaegeuk Kim {
1627127e670aSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1628127e670aSJaegeuk Kim 	unsigned long long ckpt_ver;
1629c34f42e2SChao Yu 	int err = 0;
1630127e670aSJaegeuk Kim 
1631f5a131bbSChao Yu 	if (f2fs_readonly(sbi->sb) || f2fs_hw_is_readonly(sbi))
1632f5a131bbSChao Yu 		return -EROFS;
1633f5a131bbSChao Yu 
16344354994fSDaniel Rosenberg 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
16354354994fSDaniel Rosenberg 		if (cpc->reason != CP_PAUSE)
16364354994fSDaniel Rosenberg 			return 0;
1637dcbb4c10SJoe Perches 		f2fs_warn(sbi, "Start checkpoint disabled!");
16384354994fSDaniel Rosenberg 	}
1639b4b10061SJaegeuk Kim 	if (cpc->reason != CP_RESIZE)
1640e4544b63STim Murray 		f2fs_down_write(&sbi->cp_global_sem);
16418501017eSJaegeuk Kim 
1642caf0047eSChao Yu 	if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) &&
1643c473f1a9SChao Yu 		((cpc->reason & CP_FASTBOOT) || (cpc->reason & CP_SYNC) ||
1644c473f1a9SChao Yu 		((cpc->reason & CP_DISCARD) && !sbi->discard_blks)))
16458501017eSJaegeuk Kim 		goto out;
1646c34f42e2SChao Yu 	if (unlikely(f2fs_cp_error(sbi))) {
1647c34f42e2SChao Yu 		err = -EIO;
1648cf779cabSJaegeuk Kim 		goto out;
1649c34f42e2SChao Yu 	}
16502bda542dSWanpeng Li 
16512bda542dSWanpeng Li 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops");
16522bda542dSWanpeng Li 
1653c34f42e2SChao Yu 	err = block_operations(sbi);
1654c34f42e2SChao Yu 	if (err)
1655cf779cabSJaegeuk Kim 		goto out;
1656127e670aSJaegeuk Kim 
165775ab4cb8SJaegeuk Kim 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops");
16582af4bd6cSNamjae Jeon 
1659b9109b0eSJaegeuk Kim 	f2fs_flush_merged_writes(sbi);
1660127e670aSJaegeuk Kim 
166158cce381SYunlei He 	/* this is the case of multiple fstrims without any changes */
1662c473f1a9SChao Yu 	if (cpc->reason & CP_DISCARD) {
16634d57b86dSChao Yu 		if (!f2fs_exist_trim_candidates(sbi, cpc)) {
166425290fa5SJaegeuk Kim 			unblock_operations(sbi);
166525290fa5SJaegeuk Kim 			goto out;
166625290fa5SJaegeuk Kim 		}
166725290fa5SJaegeuk Kim 
1668a95ba66aSJaegeuk Kim 		if (NM_I(sbi)->nat_cnt[DIRTY_NAT] == 0 &&
16690333ad4eSJaegeuk Kim 				SIT_I(sbi)->dirty_sentries == 0 &&
16700333ad4eSJaegeuk Kim 				prefree_segments(sbi) == 0) {
16714d57b86dSChao Yu 			f2fs_flush_sit_entries(sbi, cpc);
16724d57b86dSChao Yu 			f2fs_clear_prefree_segments(sbi, cpc);
167358cce381SYunlei He 			unblock_operations(sbi);
167458cce381SYunlei He 			goto out;
167558cce381SYunlei He 		}
16760333ad4eSJaegeuk Kim 	}
167758cce381SYunlei He 
1678127e670aSJaegeuk Kim 	/*
1679127e670aSJaegeuk Kim 	 * update checkpoint pack index
1680127e670aSJaegeuk Kim 	 * Increase the version number so that
1681127e670aSJaegeuk Kim 	 * SIT entries and seg summaries are written at correct place
1682127e670aSJaegeuk Kim 	 */
1683d71b5564SJaegeuk Kim 	ckpt_ver = cur_cp_version(ckpt);
1684127e670aSJaegeuk Kim 	ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
1685127e670aSJaegeuk Kim 
1686127e670aSJaegeuk Kim 	/* write cached NAT/SIT entries to NAT/SIT area */
1687edc55aafSJaegeuk Kim 	err = f2fs_flush_nat_entries(sbi, cpc);
168891803392SChao Yu 	if (err) {
168991803392SChao Yu 		f2fs_err(sbi, "f2fs_flush_nat_entries failed err:%d, stop checkpoint", err);
169091803392SChao Yu 		f2fs_bug_on(sbi, !f2fs_cp_error(sbi));
1691edc55aafSJaegeuk Kim 		goto stop;
169291803392SChao Yu 	}
1693edc55aafSJaegeuk Kim 
16944d57b86dSChao Yu 	f2fs_flush_sit_entries(sbi, cpc);
1695127e670aSJaegeuk Kim 
1696d0b9e42aSChao Yu 	/* save inmem log status */
1697093749e2SChao Yu 	f2fs_save_inmem_curseg(sbi);
1698d0b9e42aSChao Yu 
1699c34f42e2SChao Yu 	err = do_checkpoint(sbi, cpc);
170091803392SChao Yu 	if (err) {
170191803392SChao Yu 		f2fs_err(sbi, "do_checkpoint failed err:%d, stop checkpoint", err);
170291803392SChao Yu 		f2fs_bug_on(sbi, !f2fs_cp_error(sbi));
17034d57b86dSChao Yu 		f2fs_release_discard_addrs(sbi);
170491803392SChao Yu 	} else {
17054d57b86dSChao Yu 		f2fs_clear_prefree_segments(sbi, cpc);
170691803392SChao Yu 	}
1707d0b9e42aSChao Yu 
1708093749e2SChao Yu 	f2fs_restore_inmem_curseg(sbi);
1709edc55aafSJaegeuk Kim stop:
1710127e670aSJaegeuk Kim 	unblock_operations(sbi);
1711942e0be6SChangman Lee 	stat_inc_cp_count(sbi->stat_info);
171210027551SJaegeuk Kim 
1713c473f1a9SChao Yu 	if (cpc->reason & CP_RECOVERY)
1714dcbb4c10SJoe Perches 		f2fs_notice(sbi, "checkpoint: version = %llx", ckpt_ver);
171560b99b48SJaegeuk Kim 
17167a88ddb5SChao Yu 	/* update CP_TIME to trigger checkpoint periodically */
17176beceb54SJaegeuk Kim 	f2fs_update_time(sbi, CP_TIME);
171855d1cdb2SJaegeuk Kim 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint");
17198501017eSJaegeuk Kim out:
1720b4b10061SJaegeuk Kim 	if (cpc->reason != CP_RESIZE)
1721e4544b63STim Murray 		f2fs_up_write(&sbi->cp_global_sem);
1722c34f42e2SChao Yu 	return err;
1723127e670aSJaegeuk Kim }
1724127e670aSJaegeuk Kim 
17254d57b86dSChao Yu void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi)
1726127e670aSJaegeuk Kim {
17276451e041SJaegeuk Kim 	int i;
17286451e041SJaegeuk Kim 
17296451e041SJaegeuk Kim 	for (i = 0; i < MAX_INO_ENTRY; i++) {
173067298804SChao Yu 		struct inode_management *im = &sbi->im[i];
173167298804SChao Yu 
173267298804SChao Yu 		INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC);
173367298804SChao Yu 		spin_lock_init(&im->ino_lock);
173467298804SChao Yu 		INIT_LIST_HEAD(&im->ino_list);
173567298804SChao Yu 		im->ino_num = 0;
17366451e041SJaegeuk Kim 	}
17376451e041SJaegeuk Kim 
1738b5b82205SChao Yu 	sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS -
1739d0b9e42aSChao Yu 			NR_CURSEG_PERSIST_TYPE - __cp_payload(sbi)) *
174014b42817SWanpeng Li 				F2FS_ORPHANS_PER_BLOCK;
1741127e670aSJaegeuk Kim }
1742127e670aSJaegeuk Kim 
17434d57b86dSChao Yu int __init f2fs_create_checkpoint_caches(void)
1744127e670aSJaegeuk Kim {
17456451e041SJaegeuk Kim 	ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry",
17466451e041SJaegeuk Kim 			sizeof(struct ino_entry));
17476451e041SJaegeuk Kim 	if (!ino_entry_slab)
1748127e670aSJaegeuk Kim 		return -ENOMEM;
17494d57b86dSChao Yu 	f2fs_inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry",
175006292073SChao Yu 			sizeof(struct inode_entry));
17514d57b86dSChao Yu 	if (!f2fs_inode_entry_slab) {
17526451e041SJaegeuk Kim 		kmem_cache_destroy(ino_entry_slab);
1753127e670aSJaegeuk Kim 		return -ENOMEM;
1754127e670aSJaegeuk Kim 	}
1755127e670aSJaegeuk Kim 	return 0;
1756127e670aSJaegeuk Kim }
1757127e670aSJaegeuk Kim 
17584d57b86dSChao Yu void f2fs_destroy_checkpoint_caches(void)
1759127e670aSJaegeuk Kim {
17606451e041SJaegeuk Kim 	kmem_cache_destroy(ino_entry_slab);
17614d57b86dSChao Yu 	kmem_cache_destroy(f2fs_inode_entry_slab);
1762127e670aSJaegeuk Kim }
1763261eeb9cSDaeho Jeong 
1764261eeb9cSDaeho Jeong static int __write_checkpoint_sync(struct f2fs_sb_info *sbi)
1765261eeb9cSDaeho Jeong {
1766261eeb9cSDaeho Jeong 	struct cp_control cpc = { .reason = CP_SYNC, };
1767261eeb9cSDaeho Jeong 	int err;
1768261eeb9cSDaeho Jeong 
1769e4544b63STim Murray 	f2fs_down_write(&sbi->gc_lock);
1770261eeb9cSDaeho Jeong 	err = f2fs_write_checkpoint(sbi, &cpc);
1771e4544b63STim Murray 	f2fs_up_write(&sbi->gc_lock);
1772261eeb9cSDaeho Jeong 
1773261eeb9cSDaeho Jeong 	return err;
1774261eeb9cSDaeho Jeong }
1775261eeb9cSDaeho Jeong 
1776261eeb9cSDaeho Jeong static void __checkpoint_and_complete_reqs(struct f2fs_sb_info *sbi)
1777261eeb9cSDaeho Jeong {
1778261eeb9cSDaeho Jeong 	struct ckpt_req_control *cprc = &sbi->cprc_info;
1779261eeb9cSDaeho Jeong 	struct ckpt_req *req, *next;
1780261eeb9cSDaeho Jeong 	struct llist_node *dispatch_list;
1781261eeb9cSDaeho Jeong 	u64 sum_diff = 0, diff, count = 0;
1782261eeb9cSDaeho Jeong 	int ret;
1783261eeb9cSDaeho Jeong 
1784261eeb9cSDaeho Jeong 	dispatch_list = llist_del_all(&cprc->issue_list);
1785261eeb9cSDaeho Jeong 	if (!dispatch_list)
1786261eeb9cSDaeho Jeong 		return;
1787261eeb9cSDaeho Jeong 	dispatch_list = llist_reverse_order(dispatch_list);
1788261eeb9cSDaeho Jeong 
1789261eeb9cSDaeho Jeong 	ret = __write_checkpoint_sync(sbi);
1790261eeb9cSDaeho Jeong 	atomic_inc(&cprc->issued_ckpt);
1791261eeb9cSDaeho Jeong 
1792261eeb9cSDaeho Jeong 	llist_for_each_entry_safe(req, next, dispatch_list, llnode) {
1793261eeb9cSDaeho Jeong 		diff = (u64)ktime_ms_delta(ktime_get(), req->queue_time);
1794261eeb9cSDaeho Jeong 		req->ret = ret;
1795261eeb9cSDaeho Jeong 		complete(&req->wait);
1796261eeb9cSDaeho Jeong 
1797261eeb9cSDaeho Jeong 		sum_diff += diff;
1798261eeb9cSDaeho Jeong 		count++;
1799261eeb9cSDaeho Jeong 	}
1800261eeb9cSDaeho Jeong 	atomic_sub(count, &cprc->queued_ckpt);
1801261eeb9cSDaeho Jeong 	atomic_add(count, &cprc->total_ckpt);
1802261eeb9cSDaeho Jeong 
1803261eeb9cSDaeho Jeong 	spin_lock(&cprc->stat_lock);
1804261eeb9cSDaeho Jeong 	cprc->cur_time = (unsigned int)div64_u64(sum_diff, count);
1805261eeb9cSDaeho Jeong 	if (cprc->peak_time < cprc->cur_time)
1806261eeb9cSDaeho Jeong 		cprc->peak_time = cprc->cur_time;
1807261eeb9cSDaeho Jeong 	spin_unlock(&cprc->stat_lock);
1808261eeb9cSDaeho Jeong }
1809261eeb9cSDaeho Jeong 
1810261eeb9cSDaeho Jeong static int issue_checkpoint_thread(void *data)
1811261eeb9cSDaeho Jeong {
1812261eeb9cSDaeho Jeong 	struct f2fs_sb_info *sbi = data;
1813261eeb9cSDaeho Jeong 	struct ckpt_req_control *cprc = &sbi->cprc_info;
1814261eeb9cSDaeho Jeong 	wait_queue_head_t *q = &cprc->ckpt_wait_queue;
1815261eeb9cSDaeho Jeong repeat:
1816261eeb9cSDaeho Jeong 	if (kthread_should_stop())
1817261eeb9cSDaeho Jeong 		return 0;
1818261eeb9cSDaeho Jeong 
1819261eeb9cSDaeho Jeong 	if (!llist_empty(&cprc->issue_list))
1820261eeb9cSDaeho Jeong 		__checkpoint_and_complete_reqs(sbi);
1821261eeb9cSDaeho Jeong 
1822261eeb9cSDaeho Jeong 	wait_event_interruptible(*q,
1823261eeb9cSDaeho Jeong 		kthread_should_stop() || !llist_empty(&cprc->issue_list));
1824261eeb9cSDaeho Jeong 	goto repeat;
1825261eeb9cSDaeho Jeong }
1826261eeb9cSDaeho Jeong 
1827261eeb9cSDaeho Jeong static void flush_remained_ckpt_reqs(struct f2fs_sb_info *sbi,
1828261eeb9cSDaeho Jeong 		struct ckpt_req *wait_req)
1829261eeb9cSDaeho Jeong {
1830261eeb9cSDaeho Jeong 	struct ckpt_req_control *cprc = &sbi->cprc_info;
1831261eeb9cSDaeho Jeong 
1832261eeb9cSDaeho Jeong 	if (!llist_empty(&cprc->issue_list)) {
1833261eeb9cSDaeho Jeong 		__checkpoint_and_complete_reqs(sbi);
1834261eeb9cSDaeho Jeong 	} else {
1835261eeb9cSDaeho Jeong 		/* already dispatched by issue_checkpoint_thread */
1836261eeb9cSDaeho Jeong 		if (wait_req)
1837261eeb9cSDaeho Jeong 			wait_for_completion(&wait_req->wait);
1838261eeb9cSDaeho Jeong 	}
1839261eeb9cSDaeho Jeong }
1840261eeb9cSDaeho Jeong 
1841261eeb9cSDaeho Jeong static void init_ckpt_req(struct ckpt_req *req)
1842261eeb9cSDaeho Jeong {
1843261eeb9cSDaeho Jeong 	memset(req, 0, sizeof(struct ckpt_req));
1844261eeb9cSDaeho Jeong 
1845261eeb9cSDaeho Jeong 	init_completion(&req->wait);
1846261eeb9cSDaeho Jeong 	req->queue_time = ktime_get();
1847261eeb9cSDaeho Jeong }
1848261eeb9cSDaeho Jeong 
1849261eeb9cSDaeho Jeong int f2fs_issue_checkpoint(struct f2fs_sb_info *sbi)
1850261eeb9cSDaeho Jeong {
1851261eeb9cSDaeho Jeong 	struct ckpt_req_control *cprc = &sbi->cprc_info;
1852261eeb9cSDaeho Jeong 	struct ckpt_req req;
1853261eeb9cSDaeho Jeong 	struct cp_control cpc;
1854261eeb9cSDaeho Jeong 
1855261eeb9cSDaeho Jeong 	cpc.reason = __get_cp_reason(sbi);
1856261eeb9cSDaeho Jeong 	if (!test_opt(sbi, MERGE_CHECKPOINT) || cpc.reason != CP_SYNC) {
1857261eeb9cSDaeho Jeong 		int ret;
1858261eeb9cSDaeho Jeong 
1859e4544b63STim Murray 		f2fs_down_write(&sbi->gc_lock);
1860261eeb9cSDaeho Jeong 		ret = f2fs_write_checkpoint(sbi, &cpc);
1861e4544b63STim Murray 		f2fs_up_write(&sbi->gc_lock);
1862261eeb9cSDaeho Jeong 
1863261eeb9cSDaeho Jeong 		return ret;
1864261eeb9cSDaeho Jeong 	}
1865261eeb9cSDaeho Jeong 
1866261eeb9cSDaeho Jeong 	if (!cprc->f2fs_issue_ckpt)
1867261eeb9cSDaeho Jeong 		return __write_checkpoint_sync(sbi);
1868261eeb9cSDaeho Jeong 
1869261eeb9cSDaeho Jeong 	init_ckpt_req(&req);
1870261eeb9cSDaeho Jeong 
1871261eeb9cSDaeho Jeong 	llist_add(&req.llnode, &cprc->issue_list);
1872261eeb9cSDaeho Jeong 	atomic_inc(&cprc->queued_ckpt);
1873261eeb9cSDaeho Jeong 
18743b42c741SChao Yu 	/*
18753b42c741SChao Yu 	 * update issue_list before we wake up issue_checkpoint thread,
18763b42c741SChao Yu 	 * this smp_mb() pairs with another barrier in ___wait_event(),
18773b42c741SChao Yu 	 * see more details in comments of waitqueue_active().
18783b42c741SChao Yu 	 */
1879261eeb9cSDaeho Jeong 	smp_mb();
1880261eeb9cSDaeho Jeong 
1881261eeb9cSDaeho Jeong 	if (waitqueue_active(&cprc->ckpt_wait_queue))
1882261eeb9cSDaeho Jeong 		wake_up(&cprc->ckpt_wait_queue);
1883261eeb9cSDaeho Jeong 
1884261eeb9cSDaeho Jeong 	if (cprc->f2fs_issue_ckpt)
1885261eeb9cSDaeho Jeong 		wait_for_completion(&req.wait);
1886261eeb9cSDaeho Jeong 	else
1887261eeb9cSDaeho Jeong 		flush_remained_ckpt_reqs(sbi, &req);
1888261eeb9cSDaeho Jeong 
1889261eeb9cSDaeho Jeong 	return req.ret;
1890261eeb9cSDaeho Jeong }
1891261eeb9cSDaeho Jeong 
1892261eeb9cSDaeho Jeong int f2fs_start_ckpt_thread(struct f2fs_sb_info *sbi)
1893261eeb9cSDaeho Jeong {
1894261eeb9cSDaeho Jeong 	dev_t dev = sbi->sb->s_bdev->bd_dev;
1895261eeb9cSDaeho Jeong 	struct ckpt_req_control *cprc = &sbi->cprc_info;
1896261eeb9cSDaeho Jeong 
1897261eeb9cSDaeho Jeong 	if (cprc->f2fs_issue_ckpt)
1898261eeb9cSDaeho Jeong 		return 0;
1899261eeb9cSDaeho Jeong 
1900261eeb9cSDaeho Jeong 	cprc->f2fs_issue_ckpt = kthread_run(issue_checkpoint_thread, sbi,
1901261eeb9cSDaeho Jeong 			"f2fs_ckpt-%u:%u", MAJOR(dev), MINOR(dev));
1902261eeb9cSDaeho Jeong 	if (IS_ERR(cprc->f2fs_issue_ckpt)) {
1903146dbcbfSYangtao Li 		int err = PTR_ERR(cprc->f2fs_issue_ckpt);
1904146dbcbfSYangtao Li 
1905261eeb9cSDaeho Jeong 		cprc->f2fs_issue_ckpt = NULL;
1906146dbcbfSYangtao Li 		return err;
1907261eeb9cSDaeho Jeong 	}
1908261eeb9cSDaeho Jeong 
1909e6592066SDaeho Jeong 	set_task_ioprio(cprc->f2fs_issue_ckpt, cprc->ckpt_thread_ioprio);
1910261eeb9cSDaeho Jeong 
1911261eeb9cSDaeho Jeong 	return 0;
1912261eeb9cSDaeho Jeong }
1913261eeb9cSDaeho Jeong 
1914261eeb9cSDaeho Jeong void f2fs_stop_ckpt_thread(struct f2fs_sb_info *sbi)
1915261eeb9cSDaeho Jeong {
1916261eeb9cSDaeho Jeong 	struct ckpt_req_control *cprc = &sbi->cprc_info;
1917c7b58576SJaegeuk Kim 	struct task_struct *ckpt_task;
1918261eeb9cSDaeho Jeong 
1919c7b58576SJaegeuk Kim 	if (!cprc->f2fs_issue_ckpt)
1920c7b58576SJaegeuk Kim 		return;
1921261eeb9cSDaeho Jeong 
1922c7b58576SJaegeuk Kim 	ckpt_task = cprc->f2fs_issue_ckpt;
1923261eeb9cSDaeho Jeong 	cprc->f2fs_issue_ckpt = NULL;
1924261eeb9cSDaeho Jeong 	kthread_stop(ckpt_task);
1925261eeb9cSDaeho Jeong 
1926c7b58576SJaegeuk Kim 	f2fs_flush_ckpt_thread(sbi);
1927261eeb9cSDaeho Jeong }
1928c7b58576SJaegeuk Kim 
1929c7b58576SJaegeuk Kim void f2fs_flush_ckpt_thread(struct f2fs_sb_info *sbi)
1930c7b58576SJaegeuk Kim {
1931c7b58576SJaegeuk Kim 	struct ckpt_req_control *cprc = &sbi->cprc_info;
1932c7b58576SJaegeuk Kim 
1933c7b58576SJaegeuk Kim 	flush_remained_ckpt_reqs(sbi, NULL);
1934c7b58576SJaegeuk Kim 
1935c7b58576SJaegeuk Kim 	/* Let's wait for the previous dispatched checkpoint. */
1936c7b58576SJaegeuk Kim 	while (atomic_read(&cprc->queued_ckpt))
1937c7b58576SJaegeuk Kim 		io_schedule_timeout(DEFAULT_IO_TIMEOUT);
1938261eeb9cSDaeho Jeong }
1939261eeb9cSDaeho Jeong 
1940261eeb9cSDaeho Jeong void f2fs_init_ckpt_req_control(struct f2fs_sb_info *sbi)
1941261eeb9cSDaeho Jeong {
1942261eeb9cSDaeho Jeong 	struct ckpt_req_control *cprc = &sbi->cprc_info;
1943261eeb9cSDaeho Jeong 
1944261eeb9cSDaeho Jeong 	atomic_set(&cprc->issued_ckpt, 0);
1945261eeb9cSDaeho Jeong 	atomic_set(&cprc->total_ckpt, 0);
1946261eeb9cSDaeho Jeong 	atomic_set(&cprc->queued_ckpt, 0);
1947e6592066SDaeho Jeong 	cprc->ckpt_thread_ioprio = DEFAULT_CHECKPOINT_IOPRIO;
1948261eeb9cSDaeho Jeong 	init_waitqueue_head(&cprc->ckpt_wait_queue);
1949261eeb9cSDaeho Jeong 	init_llist_head(&cprc->issue_list);
1950261eeb9cSDaeho Jeong 	spin_lock_init(&cprc->stat_lock);
1951261eeb9cSDaeho Jeong }
1952