xref: /openbmc/linux/fs/f2fs/checkpoint.c (revision 6a257471fa42c8c9c04a875cd3a2a22db148e0f0)
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>
16127e670aSJaegeuk Kim 
17127e670aSJaegeuk Kim #include "f2fs.h"
18127e670aSJaegeuk Kim #include "node.h"
19127e670aSJaegeuk Kim #include "segment.h"
209e4ded3fSJaegeuk Kim #include "trace.h"
212af4bd6cSNamjae Jeon #include <trace/events/f2fs.h>
22127e670aSJaegeuk Kim 
236451e041SJaegeuk Kim static struct kmem_cache *ino_entry_slab;
244d57b86dSChao Yu struct kmem_cache *f2fs_inode_entry_slab;
25127e670aSJaegeuk Kim 
2638f91ca8SJaegeuk Kim void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io)
2738f91ca8SJaegeuk Kim {
28d494500aSChao Yu 	f2fs_build_fault_attr(sbi, 0, 0);
29aaec2b1dSChao Yu 	set_ckpt_flags(sbi, CP_ERROR_FLAG);
3038f91ca8SJaegeuk Kim 	if (!end_io)
31b9109b0eSJaegeuk Kim 		f2fs_flush_merged_writes(sbi);
3238f91ca8SJaegeuk Kim }
3338f91ca8SJaegeuk Kim 
340a8165d7SJaegeuk Kim /*
35127e670aSJaegeuk Kim  * We guarantee no failure on the returned page.
36127e670aSJaegeuk Kim  */
374d57b86dSChao Yu struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
38127e670aSJaegeuk Kim {
399df27d98SGu Zheng 	struct address_space *mapping = META_MAPPING(sbi);
40127e670aSJaegeuk Kim 	struct page *page = NULL;
41127e670aSJaegeuk Kim repeat:
42300e129cSJaegeuk Kim 	page = f2fs_grab_cache_page(mapping, index, false);
43127e670aSJaegeuk Kim 	if (!page) {
44127e670aSJaegeuk Kim 		cond_resched();
45127e670aSJaegeuk Kim 		goto repeat;
46127e670aSJaegeuk Kim 	}
47bae0ee7aSChao Yu 	f2fs_wait_on_page_writeback(page, META, true, true);
48237c0790SJaegeuk Kim 	if (!PageUptodate(page))
49127e670aSJaegeuk Kim 		SetPageUptodate(page);
50127e670aSJaegeuk Kim 	return page;
51127e670aSJaegeuk Kim }
52127e670aSJaegeuk Kim 
532b947003SChao Yu static struct page *__get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index,
542b947003SChao Yu 							bool is_meta)
55127e670aSJaegeuk Kim {
569df27d98SGu Zheng 	struct address_space *mapping = META_MAPPING(sbi);
57127e670aSJaegeuk Kim 	struct page *page;
58cf04e8ebSJaegeuk Kim 	struct f2fs_io_info fio = {
5905ca3632SJaegeuk Kim 		.sbi = sbi,
60cf04e8ebSJaegeuk Kim 		.type = META,
6104d328deSMike Christie 		.op = REQ_OP_READ,
6270fd7614SChristoph Hellwig 		.op_flags = REQ_META | REQ_PRIO,
637a9d7548SChao Yu 		.old_blkaddr = index,
647a9d7548SChao Yu 		.new_blkaddr = index,
654375a336SJaegeuk Kim 		.encrypted_page = NULL,
666dc3a126SChao Yu 		.is_por = !is_meta,
67cf04e8ebSJaegeuk Kim 	};
687735730dSChao Yu 	int err;
692b947003SChao Yu 
702b947003SChao Yu 	if (unlikely(!is_meta))
7104d328deSMike Christie 		fio.op_flags &= ~REQ_META;
72127e670aSJaegeuk Kim repeat:
73300e129cSJaegeuk Kim 	page = f2fs_grab_cache_page(mapping, index, false);
74127e670aSJaegeuk Kim 	if (!page) {
75127e670aSJaegeuk Kim 		cond_resched();
76127e670aSJaegeuk Kim 		goto repeat;
77127e670aSJaegeuk Kim 	}
78393ff91fSJaegeuk Kim 	if (PageUptodate(page))
79393ff91fSJaegeuk Kim 		goto out;
80127e670aSJaegeuk Kim 
8105ca3632SJaegeuk Kim 	fio.page = page;
8205ca3632SJaegeuk Kim 
837735730dSChao Yu 	err = f2fs_submit_page_bio(&fio);
847735730dSChao Yu 	if (err) {
857735730dSChao Yu 		f2fs_put_page(page, 1);
867735730dSChao Yu 		return ERR_PTR(err);
8786531d6bSJaegeuk Kim 	}
88393ff91fSJaegeuk Kim 
898b83ac81SChao Yu 	f2fs_update_iostat(sbi, FS_META_READ_IO, F2FS_BLKSIZE);
908b83ac81SChao Yu 
91393ff91fSJaegeuk Kim 	lock_page(page);
926bacf52fSJaegeuk Kim 	if (unlikely(page->mapping != mapping)) {
93afcb7ca0SJaegeuk Kim 		f2fs_put_page(page, 1);
94afcb7ca0SJaegeuk Kim 		goto repeat;
95afcb7ca0SJaegeuk Kim 	}
96f3f338caSChao Yu 
9781114baaSChao Yu 	if (unlikely(!PageUptodate(page))) {
987735730dSChao Yu 		f2fs_put_page(page, 1);
997735730dSChao Yu 		return ERR_PTR(-EIO);
10081114baaSChao Yu 	}
101393ff91fSJaegeuk Kim out:
102127e670aSJaegeuk Kim 	return page;
103127e670aSJaegeuk Kim }
104127e670aSJaegeuk Kim 
1054d57b86dSChao Yu struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
1062b947003SChao Yu {
1072b947003SChao Yu 	return __get_meta_page(sbi, index, true);
1082b947003SChao Yu }
1092b947003SChao Yu 
1107735730dSChao Yu struct page *f2fs_get_meta_page_nofail(struct f2fs_sb_info *sbi, pgoff_t index)
1117735730dSChao Yu {
1127735730dSChao Yu 	struct page *page;
1137735730dSChao Yu 	int count = 0;
1147735730dSChao Yu 
1157735730dSChao Yu retry:
1167735730dSChao Yu 	page = __get_meta_page(sbi, index, true);
1177735730dSChao Yu 	if (IS_ERR(page)) {
1187735730dSChao Yu 		if (PTR_ERR(page) == -EIO &&
1197735730dSChao Yu 				++count <= DEFAULT_RETRY_IO_COUNT)
1207735730dSChao Yu 			goto retry;
1217735730dSChao Yu 		f2fs_stop_checkpoint(sbi, false);
1227735730dSChao Yu 	}
1237735730dSChao Yu 	return page;
1247735730dSChao Yu }
1257735730dSChao Yu 
1262b947003SChao Yu /* for POR only */
1274d57b86dSChao Yu struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index)
1282b947003SChao Yu {
1292b947003SChao Yu 	return __get_meta_page(sbi, index, false);
1302b947003SChao Yu }
1312b947003SChao Yu 
13293770ab7SChao Yu static bool __is_bitmap_valid(struct f2fs_sb_info *sbi, block_t blkaddr,
13393770ab7SChao Yu 							int type)
13493770ab7SChao Yu {
13593770ab7SChao Yu 	struct seg_entry *se;
13693770ab7SChao Yu 	unsigned int segno, offset;
13793770ab7SChao Yu 	bool exist;
13893770ab7SChao Yu 
13993770ab7SChao Yu 	if (type != DATA_GENERIC_ENHANCE && type != DATA_GENERIC_ENHANCE_READ)
14093770ab7SChao Yu 		return true;
14193770ab7SChao Yu 
14293770ab7SChao Yu 	segno = GET_SEGNO(sbi, blkaddr);
14393770ab7SChao Yu 	offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
14493770ab7SChao Yu 	se = get_seg_entry(sbi, segno);
14593770ab7SChao Yu 
14693770ab7SChao Yu 	exist = f2fs_test_bit(offset, se->cur_valid_map);
14793770ab7SChao Yu 	if (!exist && type == DATA_GENERIC_ENHANCE) {
148dcbb4c10SJoe Perches 		f2fs_err(sbi, "Inconsistent error blkaddr:%u, sit bitmap:%d",
149dcbb4c10SJoe Perches 			 blkaddr, exist);
15093770ab7SChao Yu 		set_sbi_flag(sbi, SBI_NEED_FSCK);
15193770ab7SChao Yu 		WARN_ON(1);
15293770ab7SChao Yu 	}
15393770ab7SChao Yu 	return exist;
15493770ab7SChao Yu }
15593770ab7SChao Yu 
156e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
1574d57b86dSChao Yu 					block_t blkaddr, int type)
158662befdaSChao Yu {
159662befdaSChao Yu 	switch (type) {
160662befdaSChao Yu 	case META_NAT:
16166b00c18SChao Yu 		break;
162662befdaSChao Yu 	case META_SIT:
16366b00c18SChao Yu 		if (unlikely(blkaddr >= SIT_BLK_CNT(sbi)))
16466b00c18SChao Yu 			return false;
16566b00c18SChao Yu 		break;
16681c1a0f1SChao Yu 	case META_SSA:
16766b00c18SChao Yu 		if (unlikely(blkaddr >= MAIN_BLKADDR(sbi) ||
16866b00c18SChao Yu 			blkaddr < SM_I(sbi)->ssa_blkaddr))
16966b00c18SChao Yu 			return false;
17066b00c18SChao Yu 		break;
171662befdaSChao Yu 	case META_CP:
17266b00c18SChao Yu 		if (unlikely(blkaddr >= SIT_I(sbi)->sit_base_addr ||
17366b00c18SChao Yu 			blkaddr < __start_cp_addr(sbi)))
17466b00c18SChao Yu 			return false;
17566b00c18SChao Yu 		break;
1764c521f49SJaegeuk Kim 	case META_POR:
17793770ab7SChao Yu 		if (unlikely(blkaddr >= MAX_BLKADDR(sbi) ||
17893770ab7SChao Yu 			blkaddr < MAIN_BLKADDR(sbi)))
17993770ab7SChao Yu 			return false;
18093770ab7SChao Yu 		break;
181e1da7872SChao Yu 	case DATA_GENERIC:
18293770ab7SChao Yu 	case DATA_GENERIC_ENHANCE:
18393770ab7SChao Yu 	case DATA_GENERIC_ENHANCE_READ:
18466b00c18SChao Yu 		if (unlikely(blkaddr >= MAX_BLKADDR(sbi) ||
185c9b60788SChao Yu 				blkaddr < MAIN_BLKADDR(sbi))) {
186dcbb4c10SJoe Perches 			f2fs_warn(sbi, "access invalid blkaddr:%u",
187dcbb4c10SJoe Perches 				  blkaddr);
18893770ab7SChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);
189c9b60788SChao Yu 			WARN_ON(1);
19066b00c18SChao Yu 			return false;
19193770ab7SChao Yu 		} else {
19293770ab7SChao Yu 			return __is_bitmap_valid(sbi, blkaddr, type);
193c9b60788SChao Yu 		}
19466b00c18SChao Yu 		break;
195e1da7872SChao Yu 	case META_GENERIC:
196e1da7872SChao Yu 		if (unlikely(blkaddr < SEG0_BLKADDR(sbi) ||
197e1da7872SChao Yu 			blkaddr >= MAIN_BLKADDR(sbi)))
198e1da7872SChao Yu 			return false;
199e1da7872SChao Yu 		break;
200662befdaSChao Yu 	default:
201662befdaSChao Yu 		BUG();
202662befdaSChao Yu 	}
20366b00c18SChao Yu 
20466b00c18SChao Yu 	return true;
205662befdaSChao Yu }
206662befdaSChao Yu 
207662befdaSChao Yu /*
2087a88ddb5SChao Yu  * Readahead CP/NAT/SIT/SSA/POR pages
209662befdaSChao Yu  */
2104d57b86dSChao Yu int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
21126879fb1SChao Yu 							int type, bool sync)
212662befdaSChao Yu {
213662befdaSChao Yu 	struct page *page;
2144c521f49SJaegeuk Kim 	block_t blkno = start;
215662befdaSChao Yu 	struct f2fs_io_info fio = {
21605ca3632SJaegeuk Kim 		.sbi = sbi,
217662befdaSChao Yu 		.type = META,
21804d328deSMike Christie 		.op = REQ_OP_READ,
21970fd7614SChristoph Hellwig 		.op_flags = sync ? (REQ_META | REQ_PRIO) : REQ_RAHEAD,
2204375a336SJaegeuk Kim 		.encrypted_page = NULL,
221fb830fc5SChao Yu 		.in_list = false,
2226dc3a126SChao Yu 		.is_por = (type == META_POR),
223662befdaSChao Yu 	};
224e9f5b8b8SChao Yu 	struct blk_plug plug;
225ce4c638cSChao Yu 	int err;
226662befdaSChao Yu 
2272b947003SChao Yu 	if (unlikely(type == META_POR))
22804d328deSMike Christie 		fio.op_flags &= ~REQ_META;
2292b947003SChao Yu 
230e9f5b8b8SChao Yu 	blk_start_plug(&plug);
231662befdaSChao Yu 	for (; nrpages-- > 0; blkno++) {
232662befdaSChao Yu 
233e1da7872SChao Yu 		if (!f2fs_is_valid_blkaddr(sbi, blkno, type))
23466b00c18SChao Yu 			goto out;
23566b00c18SChao Yu 
236662befdaSChao Yu 		switch (type) {
237662befdaSChao Yu 		case META_NAT:
23866b00c18SChao Yu 			if (unlikely(blkno >=
23966b00c18SChao Yu 					NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid)))
240662befdaSChao Yu 				blkno = 0;
24166b00c18SChao Yu 			/* get nat block addr */
2427a9d7548SChao Yu 			fio.new_blkaddr = current_nat_addr(sbi,
243662befdaSChao Yu 					blkno * NAT_ENTRY_PER_BLOCK);
244662befdaSChao Yu 			break;
245662befdaSChao Yu 		case META_SIT:
246*6a257471SChao Yu 			if (unlikely(blkno >= TOTAL_SEGS(sbi)))
247*6a257471SChao Yu 				goto out;
248662befdaSChao Yu 			/* get sit block addr */
2497a9d7548SChao Yu 			fio.new_blkaddr = current_sit_addr(sbi,
250662befdaSChao Yu 					blkno * SIT_ENTRY_PER_BLOCK);
251662befdaSChao Yu 			break;
25281c1a0f1SChao Yu 		case META_SSA:
253662befdaSChao Yu 		case META_CP:
2544c521f49SJaegeuk Kim 		case META_POR:
2557a9d7548SChao Yu 			fio.new_blkaddr = blkno;
256662befdaSChao Yu 			break;
257662befdaSChao Yu 		default:
258662befdaSChao Yu 			BUG();
259662befdaSChao Yu 		}
260662befdaSChao Yu 
261300e129cSJaegeuk Kim 		page = f2fs_grab_cache_page(META_MAPPING(sbi),
262300e129cSJaegeuk Kim 						fio.new_blkaddr, false);
263662befdaSChao Yu 		if (!page)
264662befdaSChao Yu 			continue;
265662befdaSChao Yu 		if (PageUptodate(page)) {
266662befdaSChao Yu 			f2fs_put_page(page, 1);
267662befdaSChao Yu 			continue;
268662befdaSChao Yu 		}
269662befdaSChao Yu 
27005ca3632SJaegeuk Kim 		fio.page = page;
271ce4c638cSChao Yu 		err = f2fs_submit_page_bio(&fio);
272ce4c638cSChao Yu 		f2fs_put_page(page, err ? 1 : 0);
2738b83ac81SChao Yu 
2748b83ac81SChao Yu 		if (!err)
2758b83ac81SChao Yu 			f2fs_update_iostat(sbi, FS_META_READ_IO, F2FS_BLKSIZE);
276662befdaSChao Yu 	}
277662befdaSChao Yu out:
278e9f5b8b8SChao Yu 	blk_finish_plug(&plug);
279662befdaSChao Yu 	return blkno - start;
280662befdaSChao Yu }
281662befdaSChao Yu 
2824d57b86dSChao Yu void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index)
283635aee1fSChao Yu {
284635aee1fSChao Yu 	struct page *page;
285635aee1fSChao Yu 	bool readahead = false;
286635aee1fSChao Yu 
287635aee1fSChao Yu 	page = find_get_page(META_MAPPING(sbi), index);
2884da7bf5aSJaegeuk Kim 	if (!page || !PageUptodate(page))
289635aee1fSChao Yu 		readahead = true;
290635aee1fSChao Yu 	f2fs_put_page(page, 0);
291635aee1fSChao Yu 
292635aee1fSChao Yu 	if (readahead)
2934d57b86dSChao Yu 		f2fs_ra_meta_pages(sbi, index, BIO_MAX_PAGES, META_POR, true);
294635aee1fSChao Yu }
295635aee1fSChao Yu 
296b0af6d49SChao Yu static int __f2fs_write_meta_page(struct page *page,
297b0af6d49SChao Yu 				struct writeback_control *wbc,
298b0af6d49SChao Yu 				enum iostat_type io_type)
299127e670aSJaegeuk Kim {
3004081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_P_SB(page);
301577e3495SJaegeuk Kim 
302ecda0de3SChao Yu 	trace_f2fs_writepage(page, META);
303ecda0de3SChao Yu 
304af697c0fSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
305af697c0fSJaegeuk Kim 		goto redirty_out;
306caf0047eSChao Yu 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
307cfb271d4SChao Yu 		goto redirty_out;
308857dc4e0SJaegeuk Kim 	if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0))
309cfb271d4SChao Yu 		goto redirty_out;
310127e670aSJaegeuk Kim 
3114d57b86dSChao Yu 	f2fs_do_write_meta_page(sbi, page, io_type);
312127e670aSJaegeuk Kim 	dec_page_count(sbi, F2FS_DIRTY_META);
3130c3a5797SChao Yu 
3140c3a5797SChao Yu 	if (wbc->for_reclaim)
315bab475c5SChao Yu 		f2fs_submit_merged_write_cond(sbi, NULL, page, 0, META);
3160c3a5797SChao Yu 
317127e670aSJaegeuk Kim 	unlock_page(page);
318857dc4e0SJaegeuk Kim 
3190c3a5797SChao Yu 	if (unlikely(f2fs_cp_error(sbi)))
320b9109b0eSJaegeuk Kim 		f2fs_submit_merged_write(sbi, META);
3210c3a5797SChao Yu 
322577e3495SJaegeuk Kim 	return 0;
323cfb271d4SChao Yu 
324cfb271d4SChao Yu redirty_out:
32576f60268SJaegeuk Kim 	redirty_page_for_writepage(wbc, page);
326cfb271d4SChao Yu 	return AOP_WRITEPAGE_ACTIVATE;
327127e670aSJaegeuk Kim }
328127e670aSJaegeuk Kim 
329b0af6d49SChao Yu static int f2fs_write_meta_page(struct page *page,
330b0af6d49SChao Yu 				struct writeback_control *wbc)
331b0af6d49SChao Yu {
332b0af6d49SChao Yu 	return __f2fs_write_meta_page(page, wbc, FS_META_IO);
333b0af6d49SChao Yu }
334b0af6d49SChao Yu 
335127e670aSJaegeuk Kim static int f2fs_write_meta_pages(struct address_space *mapping,
336127e670aSJaegeuk Kim 				struct writeback_control *wbc)
337127e670aSJaegeuk Kim {
3384081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
33950c8cdb3SJaegeuk Kim 	long diff, written;
340127e670aSJaegeuk Kim 
3410771fcc7SChao Yu 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
3420771fcc7SChao Yu 		goto skip_write;
3430771fcc7SChao Yu 
3445459aa97SJaegeuk Kim 	/* collect a number of dirty meta pages and write together */
345812a9597SJaegeuk Kim 	if (wbc->sync_mode != WB_SYNC_ALL &&
346812a9597SJaegeuk Kim 			get_pages(sbi, F2FS_DIRTY_META) <
347812a9597SJaegeuk Kim 					nr_pages_to_skip(sbi, META))
348d3baf95dSJaegeuk Kim 		goto skip_write;
349127e670aSJaegeuk Kim 
350a29d0e0bSYunlei He 	/* if locked failed, cp will flush dirty pages instead */
351a29d0e0bSYunlei He 	if (!mutex_trylock(&sbi->cp_mutex))
352a29d0e0bSYunlei He 		goto skip_write;
353d31c7c3fSYunlei He 
354a29d0e0bSYunlei He 	trace_f2fs_writepages(mapping->host, wbc, META);
35550c8cdb3SJaegeuk Kim 	diff = nr_pages_to_write(sbi, META, wbc);
3564d57b86dSChao Yu 	written = f2fs_sync_meta_pages(sbi, META, wbc->nr_to_write, FS_META_IO);
357127e670aSJaegeuk Kim 	mutex_unlock(&sbi->cp_mutex);
35850c8cdb3SJaegeuk Kim 	wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff);
359127e670aSJaegeuk Kim 	return 0;
360d3baf95dSJaegeuk Kim 
361d3baf95dSJaegeuk Kim skip_write:
362d3baf95dSJaegeuk Kim 	wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META);
363d31c7c3fSYunlei He 	trace_f2fs_writepages(mapping->host, wbc, META);
364d3baf95dSJaegeuk Kim 	return 0;
365127e670aSJaegeuk Kim }
366127e670aSJaegeuk Kim 
3674d57b86dSChao Yu long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
368b0af6d49SChao Yu 				long nr_to_write, enum iostat_type io_type)
369127e670aSJaegeuk Kim {
3709df27d98SGu Zheng 	struct address_space *mapping = META_MAPPING(sbi);
371028a63a6SJan Kara 	pgoff_t index = 0, prev = ULONG_MAX;
372127e670aSJaegeuk Kim 	struct pagevec pvec;
373127e670aSJaegeuk Kim 	long nwritten = 0;
374028a63a6SJan Kara 	int nr_pages;
375127e670aSJaegeuk Kim 	struct writeback_control wbc = {
376127e670aSJaegeuk Kim 		.for_reclaim = 0,
377127e670aSJaegeuk Kim 	};
378e9f5b8b8SChao Yu 	struct blk_plug plug;
379127e670aSJaegeuk Kim 
38086679820SMel Gorman 	pagevec_init(&pvec);
381127e670aSJaegeuk Kim 
382e9f5b8b8SChao Yu 	blk_start_plug(&plug);
383e9f5b8b8SChao Yu 
384028a63a6SJan Kara 	while ((nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
38567fd707fSJan Kara 				PAGECACHE_TAG_DIRTY))) {
386028a63a6SJan Kara 		int i;
387127e670aSJaegeuk Kim 
388127e670aSJaegeuk Kim 		for (i = 0; i < nr_pages; i++) {
389127e670aSJaegeuk Kim 			struct page *page = pvec.pages[i];
390203681f6SJaegeuk Kim 
39180dd9c0eSChao Yu 			if (prev == ULONG_MAX)
3926066d8cdSJaegeuk Kim 				prev = page->index - 1;
3936066d8cdSJaegeuk Kim 			if (nr_to_write != LONG_MAX && page->index != prev + 1) {
3946066d8cdSJaegeuk Kim 				pagevec_release(&pvec);
3956066d8cdSJaegeuk Kim 				goto stop;
3966066d8cdSJaegeuk Kim 			}
3976066d8cdSJaegeuk Kim 
398127e670aSJaegeuk Kim 			lock_page(page);
399203681f6SJaegeuk Kim 
400203681f6SJaegeuk Kim 			if (unlikely(page->mapping != mapping)) {
401203681f6SJaegeuk Kim continue_unlock:
402203681f6SJaegeuk Kim 				unlock_page(page);
403203681f6SJaegeuk Kim 				continue;
404203681f6SJaegeuk Kim 			}
405203681f6SJaegeuk Kim 			if (!PageDirty(page)) {
406203681f6SJaegeuk Kim 				/* someone wrote it for us */
407203681f6SJaegeuk Kim 				goto continue_unlock;
408203681f6SJaegeuk Kim 			}
409203681f6SJaegeuk Kim 
410bae0ee7aSChao Yu 			f2fs_wait_on_page_writeback(page, META, true, true);
411fa3d2bdfSJaegeuk Kim 
412203681f6SJaegeuk Kim 			if (!clear_page_dirty_for_io(page))
413203681f6SJaegeuk Kim 				goto continue_unlock;
414203681f6SJaegeuk Kim 
415b0af6d49SChao Yu 			if (__f2fs_write_meta_page(page, &wbc, io_type)) {
416577e3495SJaegeuk Kim 				unlock_page(page);
417577e3495SJaegeuk Kim 				break;
418577e3495SJaegeuk Kim 			}
419cfb271d4SChao Yu 			nwritten++;
4206066d8cdSJaegeuk Kim 			prev = page->index;
421cfb271d4SChao Yu 			if (unlikely(nwritten >= nr_to_write))
422127e670aSJaegeuk Kim 				break;
423127e670aSJaegeuk Kim 		}
424127e670aSJaegeuk Kim 		pagevec_release(&pvec);
425127e670aSJaegeuk Kim 		cond_resched();
426127e670aSJaegeuk Kim 	}
4276066d8cdSJaegeuk Kim stop:
428127e670aSJaegeuk Kim 	if (nwritten)
429b9109b0eSJaegeuk Kim 		f2fs_submit_merged_write(sbi, type);
430127e670aSJaegeuk Kim 
431e9f5b8b8SChao Yu 	blk_finish_plug(&plug);
432e9f5b8b8SChao Yu 
433127e670aSJaegeuk Kim 	return nwritten;
434127e670aSJaegeuk Kim }
435127e670aSJaegeuk Kim 
436127e670aSJaegeuk Kim static int f2fs_set_meta_page_dirty(struct page *page)
437127e670aSJaegeuk Kim {
43826c6b887SJaegeuk Kim 	trace_f2fs_set_page_dirty(page, META);
43926c6b887SJaegeuk Kim 
440237c0790SJaegeuk Kim 	if (!PageUptodate(page))
441127e670aSJaegeuk Kim 		SetPageUptodate(page);
442127e670aSJaegeuk Kim 	if (!PageDirty(page)) {
443b87078adSJaegeuk Kim 		__set_page_dirty_nobuffers(page);
4444081363fSJaegeuk Kim 		inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_META);
445240a5915SChao Yu 		f2fs_set_page_private(page, 0);
4469e4ded3fSJaegeuk Kim 		f2fs_trace_pid(page);
447127e670aSJaegeuk Kim 		return 1;
448127e670aSJaegeuk Kim 	}
449127e670aSJaegeuk Kim 	return 0;
450127e670aSJaegeuk Kim }
451127e670aSJaegeuk Kim 
452127e670aSJaegeuk Kim const struct address_space_operations f2fs_meta_aops = {
453127e670aSJaegeuk Kim 	.writepage	= f2fs_write_meta_page,
454127e670aSJaegeuk Kim 	.writepages	= f2fs_write_meta_pages,
455127e670aSJaegeuk Kim 	.set_page_dirty	= f2fs_set_meta_page_dirty,
456487261f3SChao Yu 	.invalidatepage = f2fs_invalidate_page,
457487261f3SChao Yu 	.releasepage	= f2fs_release_page,
4585b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION
4595b7a487cSWeichao Guo 	.migratepage    = f2fs_migrate_page,
4605b7a487cSWeichao Guo #endif
461127e670aSJaegeuk Kim };
462127e670aSJaegeuk Kim 
46339d787beSChao Yu static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino,
46439d787beSChao Yu 						unsigned int devidx, int type)
465953e6cc6SJaegeuk Kim {
46667298804SChao Yu 	struct inode_management *im = &sbi->im[type];
46780c54505SJaegeuk Kim 	struct ino_entry *e, *tmp;
46880c54505SJaegeuk Kim 
46980c54505SJaegeuk Kim 	tmp = f2fs_kmem_cache_alloc(ino_entry_slab, GFP_NOFS);
47019526d74SChao Yu 
47180c54505SJaegeuk Kim 	radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
472769ec6e5SJaegeuk Kim 
47367298804SChao Yu 	spin_lock(&im->ino_lock);
47467298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
47539efac41SJaegeuk Kim 	if (!e) {
47680c54505SJaegeuk Kim 		e = tmp;
47719526d74SChao Yu 		if (unlikely(radix_tree_insert(&im->ino_root, ino, e)))
47819526d74SChao Yu 			f2fs_bug_on(sbi, 1);
47919526d74SChao Yu 
48039efac41SJaegeuk Kim 		memset(e, 0, sizeof(struct ino_entry));
48139efac41SJaegeuk Kim 		e->ino = ino;
48239efac41SJaegeuk Kim 
48367298804SChao Yu 		list_add_tail(&e->list, &im->ino_list);
4848c402946SJaegeuk Kim 		if (type != ORPHAN_INO)
48567298804SChao Yu 			im->ino_num++;
48639efac41SJaegeuk Kim 	}
48739d787beSChao Yu 
48839d787beSChao Yu 	if (type == FLUSH_INO)
48939d787beSChao Yu 		f2fs_set_bit(devidx, (char *)&e->dirty_device);
49039d787beSChao Yu 
49167298804SChao Yu 	spin_unlock(&im->ino_lock);
492769ec6e5SJaegeuk Kim 	radix_tree_preload_end();
49380c54505SJaegeuk Kim 
49480c54505SJaegeuk Kim 	if (e != tmp)
49580c54505SJaegeuk Kim 		kmem_cache_free(ino_entry_slab, tmp);
496953e6cc6SJaegeuk Kim }
497953e6cc6SJaegeuk Kim 
4986451e041SJaegeuk Kim static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
499953e6cc6SJaegeuk Kim {
50067298804SChao Yu 	struct inode_management *im = &sbi->im[type];
5016451e041SJaegeuk Kim 	struct ino_entry *e;
502953e6cc6SJaegeuk Kim 
50367298804SChao Yu 	spin_lock(&im->ino_lock);
50467298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
50539efac41SJaegeuk Kim 	if (e) {
506953e6cc6SJaegeuk Kim 		list_del(&e->list);
50767298804SChao Yu 		radix_tree_delete(&im->ino_root, ino);
50867298804SChao Yu 		im->ino_num--;
50967298804SChao Yu 		spin_unlock(&im->ino_lock);
5106451e041SJaegeuk Kim 		kmem_cache_free(ino_entry_slab, e);
511953e6cc6SJaegeuk Kim 		return;
512953e6cc6SJaegeuk Kim 	}
51367298804SChao Yu 	spin_unlock(&im->ino_lock);
514953e6cc6SJaegeuk Kim }
515953e6cc6SJaegeuk Kim 
5164d57b86dSChao Yu void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
517fff04f90SJaegeuk Kim {
518fff04f90SJaegeuk Kim 	/* add new dirty ino entry into list */
51939d787beSChao Yu 	__add_ino_entry(sbi, ino, 0, type);
520fff04f90SJaegeuk Kim }
521fff04f90SJaegeuk Kim 
5224d57b86dSChao Yu void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
523fff04f90SJaegeuk Kim {
524fff04f90SJaegeuk Kim 	/* remove dirty ino entry from list */
525fff04f90SJaegeuk Kim 	__remove_ino_entry(sbi, ino, type);
526fff04f90SJaegeuk Kim }
527fff04f90SJaegeuk Kim 
5281f07cc58SJack Qiu /* mode should be APPEND_INO, UPDATE_INO or TRANS_DIR_INO */
5294d57b86dSChao Yu bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode)
530fff04f90SJaegeuk Kim {
53167298804SChao Yu 	struct inode_management *im = &sbi->im[mode];
532fff04f90SJaegeuk Kim 	struct ino_entry *e;
53367298804SChao Yu 
53467298804SChao Yu 	spin_lock(&im->ino_lock);
53567298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
53667298804SChao Yu 	spin_unlock(&im->ino_lock);
537fff04f90SJaegeuk Kim 	return e ? true : false;
538fff04f90SJaegeuk Kim }
539fff04f90SJaegeuk Kim 
5404d57b86dSChao Yu void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all)
541fff04f90SJaegeuk Kim {
542fff04f90SJaegeuk Kim 	struct ino_entry *e, *tmp;
543fff04f90SJaegeuk Kim 	int i;
544fff04f90SJaegeuk Kim 
54539d787beSChao Yu 	for (i = all ? ORPHAN_INO : APPEND_INO; i < MAX_INO_ENTRY; i++) {
54667298804SChao Yu 		struct inode_management *im = &sbi->im[i];
54767298804SChao Yu 
54867298804SChao Yu 		spin_lock(&im->ino_lock);
54967298804SChao Yu 		list_for_each_entry_safe(e, tmp, &im->ino_list, list) {
550fff04f90SJaegeuk Kim 			list_del(&e->list);
55167298804SChao Yu 			radix_tree_delete(&im->ino_root, e->ino);
552fff04f90SJaegeuk Kim 			kmem_cache_free(ino_entry_slab, e);
55367298804SChao Yu 			im->ino_num--;
554fff04f90SJaegeuk Kim 		}
55567298804SChao Yu 		spin_unlock(&im->ino_lock);
556fff04f90SJaegeuk Kim 	}
557fff04f90SJaegeuk Kim }
558fff04f90SJaegeuk Kim 
5594d57b86dSChao Yu void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
56039d787beSChao Yu 					unsigned int devidx, int type)
56139d787beSChao Yu {
56239d787beSChao Yu 	__add_ino_entry(sbi, ino, devidx, type);
56339d787beSChao Yu }
56439d787beSChao Yu 
5654d57b86dSChao Yu bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
56639d787beSChao Yu 					unsigned int devidx, int type)
56739d787beSChao Yu {
56839d787beSChao Yu 	struct inode_management *im = &sbi->im[type];
56939d787beSChao Yu 	struct ino_entry *e;
57039d787beSChao Yu 	bool is_dirty = false;
57139d787beSChao Yu 
57239d787beSChao Yu 	spin_lock(&im->ino_lock);
57339d787beSChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
57439d787beSChao Yu 	if (e && f2fs_test_bit(devidx, (char *)&e->dirty_device))
57539d787beSChao Yu 		is_dirty = true;
57639d787beSChao Yu 	spin_unlock(&im->ino_lock);
57739d787beSChao Yu 	return is_dirty;
57839d787beSChao Yu }
57939d787beSChao Yu 
5804d57b86dSChao Yu int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi)
581127e670aSJaegeuk Kim {
58267298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
583127e670aSJaegeuk Kim 	int err = 0;
584127e670aSJaegeuk Kim 
58567298804SChao Yu 	spin_lock(&im->ino_lock);
586cb78942bSJaegeuk Kim 
5871ecc0c5cSChao Yu 	if (time_to_inject(sbi, FAULT_ORPHAN)) {
588cb78942bSJaegeuk Kim 		spin_unlock(&im->ino_lock);
589c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_ORPHAN);
590cb78942bSJaegeuk Kim 		return -ENOSPC;
591cb78942bSJaegeuk Kim 	}
5927fa750a1SArnd Bergmann 
59367298804SChao Yu 	if (unlikely(im->ino_num >= sbi->max_orphans))
594127e670aSJaegeuk Kim 		err = -ENOSPC;
595cbd56e7dSJaegeuk Kim 	else
59667298804SChao Yu 		im->ino_num++;
59767298804SChao Yu 	spin_unlock(&im->ino_lock);
5980d47c1adSGu Zheng 
599127e670aSJaegeuk Kim 	return err;
600127e670aSJaegeuk Kim }
601127e670aSJaegeuk Kim 
6024d57b86dSChao Yu void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi)
603cbd56e7dSJaegeuk Kim {
60467298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
60567298804SChao Yu 
60667298804SChao Yu 	spin_lock(&im->ino_lock);
60767298804SChao Yu 	f2fs_bug_on(sbi, im->ino_num == 0);
60867298804SChao Yu 	im->ino_num--;
60967298804SChao Yu 	spin_unlock(&im->ino_lock);
610cbd56e7dSJaegeuk Kim }
611cbd56e7dSJaegeuk Kim 
6124d57b86dSChao Yu void f2fs_add_orphan_inode(struct inode *inode)
613127e670aSJaegeuk Kim {
61439efac41SJaegeuk Kim 	/* add new orphan ino entry into list */
61539d787beSChao Yu 	__add_ino_entry(F2FS_I_SB(inode), inode->i_ino, 0, ORPHAN_INO);
6164d57b86dSChao Yu 	f2fs_update_inode_page(inode);
617127e670aSJaegeuk Kim }
618127e670aSJaegeuk Kim 
6194d57b86dSChao Yu void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
620127e670aSJaegeuk Kim {
621953e6cc6SJaegeuk Kim 	/* remove orphan entry from orphan list */
6226451e041SJaegeuk Kim 	__remove_ino_entry(sbi, ino, ORPHAN_INO);
623127e670aSJaegeuk Kim }
624127e670aSJaegeuk Kim 
6258c14bfadSChao Yu static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
626127e670aSJaegeuk Kim {
6278c14bfadSChao Yu 	struct inode *inode;
6285905f9afSJaegeuk Kim 	struct node_info ni;
62976a45e3cSChao Yu 	int err;
6308c14bfadSChao Yu 
6315905f9afSJaegeuk Kim 	inode = f2fs_iget_retry(sbi->sb, ino);
6328c14bfadSChao Yu 	if (IS_ERR(inode)) {
6338c14bfadSChao Yu 		/*
6348c14bfadSChao Yu 		 * there should be a bug that we can't find the entry
6358c14bfadSChao Yu 		 * to orphan inode.
6368c14bfadSChao Yu 		 */
6378c14bfadSChao Yu 		f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT);
6388c14bfadSChao Yu 		return PTR_ERR(inode);
6398c14bfadSChao Yu 	}
6408c14bfadSChao Yu 
6410f9ec2a8SJaegeuk Kim 	err = dquot_initialize(inode);
642a515d12fSSheng Yong 	if (err) {
643a515d12fSSheng Yong 		iput(inode);
6440f9ec2a8SJaegeuk Kim 		goto err_out;
645a515d12fSSheng Yong 	}
6460f9ec2a8SJaegeuk Kim 
647127e670aSJaegeuk Kim 	clear_nlink(inode);
648127e670aSJaegeuk Kim 
649127e670aSJaegeuk Kim 	/* truncate all the data during iput */
650127e670aSJaegeuk Kim 	iput(inode);
6515905f9afSJaegeuk Kim 
6527735730dSChao Yu 	err = f2fs_get_node_info(sbi, ino, &ni);
6537735730dSChao Yu 	if (err)
6547735730dSChao Yu 		goto err_out;
6555905f9afSJaegeuk Kim 
6565905f9afSJaegeuk Kim 	/* ENOMEM was fully retried in f2fs_evict_inode. */
6575905f9afSJaegeuk Kim 	if (ni.blk_addr != NULL_ADDR) {
6580f9ec2a8SJaegeuk Kim 		err = -EIO;
6590f9ec2a8SJaegeuk Kim 		goto err_out;
6605905f9afSJaegeuk Kim 	}
6618c14bfadSChao Yu 	return 0;
6620f9ec2a8SJaegeuk Kim 
6630f9ec2a8SJaegeuk Kim err_out:
6640f9ec2a8SJaegeuk Kim 	set_sbi_flag(sbi, SBI_NEED_FSCK);
665dcbb4c10SJoe Perches 	f2fs_warn(sbi, "%s: orphan failed (ino=%x), run fsck to fix.",
6660f9ec2a8SJaegeuk Kim 		  __func__, ino);
6670f9ec2a8SJaegeuk Kim 	return err;
668127e670aSJaegeuk Kim }
669127e670aSJaegeuk Kim 
6704d57b86dSChao Yu int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
671127e670aSJaegeuk Kim {
6723c642985SWanpeng Li 	block_t start_blk, orphan_blocks, i, j;
6734b2414d0SChao Yu 	unsigned int s_flags = sbi->sb->s_flags;
6744b2414d0SChao Yu 	int err = 0;
675ea676733SJaegeuk Kim #ifdef CONFIG_QUOTA
676ea676733SJaegeuk Kim 	int quota_enabled;
677ea676733SJaegeuk Kim #endif
678127e670aSJaegeuk Kim 
679aaec2b1dSChao Yu 	if (!is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG))
6808c14bfadSChao Yu 		return 0;
681127e670aSJaegeuk Kim 
682b61af314SChao Yu 	if (bdev_read_only(sbi->sb->s_bdev)) {
683dcbb4c10SJoe Perches 		f2fs_info(sbi, "write access unavailable, skipping orphan cleanup");
684b61af314SChao Yu 		return 0;
685b61af314SChao Yu 	}
686b61af314SChao Yu 
6871751e8a6SLinus Torvalds 	if (s_flags & SB_RDONLY) {
688dcbb4c10SJoe Perches 		f2fs_info(sbi, "orphan cleanup on readonly fs");
6891751e8a6SLinus Torvalds 		sbi->sb->s_flags &= ~SB_RDONLY;
6904b2414d0SChao Yu 	}
6914b2414d0SChao Yu 
6924b2414d0SChao Yu #ifdef CONFIG_QUOTA
6934b2414d0SChao Yu 	/* Needed for iput() to work correctly and not trash data */
6941751e8a6SLinus Torvalds 	sbi->sb->s_flags |= SB_ACTIVE;
695ea676733SJaegeuk Kim 
69676cf05d7SSheng Yong 	/*
69776cf05d7SSheng Yong 	 * Turn on quotas which were not enabled for read-only mounts if
69876cf05d7SSheng Yong 	 * filesystem has quota feature, so that they are updated correctly.
69976cf05d7SSheng Yong 	 */
7001751e8a6SLinus Torvalds 	quota_enabled = f2fs_enable_quota_files(sbi, s_flags & SB_RDONLY);
7014b2414d0SChao Yu #endif
7024b2414d0SChao Yu 
70355141486SWanpeng Li 	start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
7043c642985SWanpeng Li 	orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
705127e670aSJaegeuk Kim 
7064d57b86dSChao Yu 	f2fs_ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true);
707662befdaSChao Yu 
7083c642985SWanpeng Li 	for (i = 0; i < orphan_blocks; i++) {
7097735730dSChao Yu 		struct page *page;
710127e670aSJaegeuk Kim 		struct f2fs_orphan_block *orphan_blk;
711127e670aSJaegeuk Kim 
7127735730dSChao Yu 		page = f2fs_get_meta_page(sbi, start_blk + i);
7137735730dSChao Yu 		if (IS_ERR(page)) {
7147735730dSChao Yu 			err = PTR_ERR(page);
7157735730dSChao Yu 			goto out;
7167735730dSChao Yu 		}
7177735730dSChao Yu 
718127e670aSJaegeuk Kim 		orphan_blk = (struct f2fs_orphan_block *)page_address(page);
719127e670aSJaegeuk Kim 		for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
720127e670aSJaegeuk Kim 			nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
7218c14bfadSChao Yu 			err = recover_orphan_inode(sbi, ino);
7228c14bfadSChao Yu 			if (err) {
7238c14bfadSChao Yu 				f2fs_put_page(page, 1);
7244b2414d0SChao Yu 				goto out;
7258c14bfadSChao Yu 			}
726127e670aSJaegeuk Kim 		}
727127e670aSJaegeuk Kim 		f2fs_put_page(page, 1);
728127e670aSJaegeuk Kim 	}
729127e670aSJaegeuk Kim 	/* clear Orphan Flag */
730aaec2b1dSChao Yu 	clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
7314b2414d0SChao Yu out:
7321378752bSChao Yu 	set_sbi_flag(sbi, SBI_IS_RECOVERED);
7331378752bSChao Yu 
7344b2414d0SChao Yu #ifdef CONFIG_QUOTA
7354b2414d0SChao Yu 	/* Turn quotas off */
736ea676733SJaegeuk Kim 	if (quota_enabled)
7374b2414d0SChao Yu 		f2fs_quota_off_umount(sbi->sb);
7384b2414d0SChao Yu #endif
7391751e8a6SLinus Torvalds 	sbi->sb->s_flags = s_flags; /* Restore SB_RDONLY status */
7404b2414d0SChao Yu 
7414b2414d0SChao Yu 	return err;
742127e670aSJaegeuk Kim }
743127e670aSJaegeuk Kim 
744127e670aSJaegeuk Kim static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
745127e670aSJaegeuk Kim {
746502c6e0bSGu Zheng 	struct list_head *head;
747127e670aSJaegeuk Kim 	struct f2fs_orphan_block *orphan_blk = NULL;
748127e670aSJaegeuk Kim 	unsigned int nentries = 0;
749bd936f84SChao Yu 	unsigned short index = 1;
7508c402946SJaegeuk Kim 	unsigned short orphan_blocks;
7514531929eSGu Zheng 	struct page *page = NULL;
7526451e041SJaegeuk Kim 	struct ino_entry *orphan = NULL;
75367298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
754127e670aSJaegeuk Kim 
75567298804SChao Yu 	orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num);
7568c402946SJaegeuk Kim 
757d6c67a4fSJaegeuk Kim 	/*
758d6c67a4fSJaegeuk Kim 	 * we don't need to do spin_lock(&im->ino_lock) here, since all the
759d6c67a4fSJaegeuk Kim 	 * orphan inode operations are covered under f2fs_lock_op().
760d6c67a4fSJaegeuk Kim 	 * And, spin_lock should be avoided due to page operations below.
761d6c67a4fSJaegeuk Kim 	 */
76267298804SChao Yu 	head = &im->ino_list;
763127e670aSJaegeuk Kim 
764127e670aSJaegeuk Kim 	/* loop for each orphan inode entry and write them in Jornal block */
765502c6e0bSGu Zheng 	list_for_each_entry(orphan, head, list) {
766502c6e0bSGu Zheng 		if (!page) {
7674d57b86dSChao Yu 			page = f2fs_grab_meta_page(sbi, start_blk++);
768502c6e0bSGu Zheng 			orphan_blk =
769502c6e0bSGu Zheng 				(struct f2fs_orphan_block *)page_address(page);
770502c6e0bSGu Zheng 			memset(orphan_blk, 0, sizeof(*orphan_blk));
771502c6e0bSGu Zheng 		}
772127e670aSJaegeuk Kim 
77336795567SGu Zheng 		orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
774127e670aSJaegeuk Kim 
77536795567SGu Zheng 		if (nentries == F2FS_ORPHANS_PER_BLOCK) {
776127e670aSJaegeuk Kim 			/*
777127e670aSJaegeuk Kim 			 * an orphan block is full of 1020 entries,
778127e670aSJaegeuk Kim 			 * then we need to flush current orphan blocks
779127e670aSJaegeuk Kim 			 * and bring another one in memory
780127e670aSJaegeuk Kim 			 */
781127e670aSJaegeuk Kim 			orphan_blk->blk_addr = cpu_to_le16(index);
782127e670aSJaegeuk Kim 			orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
783127e670aSJaegeuk Kim 			orphan_blk->entry_count = cpu_to_le32(nentries);
784127e670aSJaegeuk Kim 			set_page_dirty(page);
785127e670aSJaegeuk Kim 			f2fs_put_page(page, 1);
786127e670aSJaegeuk Kim 			index++;
787127e670aSJaegeuk Kim 			nentries = 0;
788127e670aSJaegeuk Kim 			page = NULL;
789127e670aSJaegeuk Kim 		}
790127e670aSJaegeuk Kim 	}
791127e670aSJaegeuk Kim 
792502c6e0bSGu Zheng 	if (page) {
793127e670aSJaegeuk Kim 		orphan_blk->blk_addr = cpu_to_le16(index);
794127e670aSJaegeuk Kim 		orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
795127e670aSJaegeuk Kim 		orphan_blk->entry_count = cpu_to_le32(nentries);
796127e670aSJaegeuk Kim 		set_page_dirty(page);
797127e670aSJaegeuk Kim 		f2fs_put_page(page, 1);
798502c6e0bSGu Zheng 	}
799127e670aSJaegeuk Kim }
800127e670aSJaegeuk Kim 
801d7eb8f1cSChao Yu static __u32 f2fs_checkpoint_chksum(struct f2fs_sb_info *sbi,
802d7eb8f1cSChao Yu 						struct f2fs_checkpoint *ckpt)
803d7eb8f1cSChao Yu {
804d7eb8f1cSChao Yu 	unsigned int chksum_ofs = le32_to_cpu(ckpt->checksum_offset);
805d7eb8f1cSChao Yu 	__u32 chksum;
806d7eb8f1cSChao Yu 
807d7eb8f1cSChao Yu 	chksum = f2fs_crc32(sbi, ckpt, chksum_ofs);
808d7eb8f1cSChao Yu 	if (chksum_ofs < CP_CHKSUM_OFFSET) {
809d7eb8f1cSChao Yu 		chksum_ofs += sizeof(chksum);
810d7eb8f1cSChao Yu 		chksum = f2fs_chksum(sbi, chksum, (__u8 *)ckpt + chksum_ofs,
811d7eb8f1cSChao Yu 						F2FS_BLKSIZE - chksum_ofs);
812d7eb8f1cSChao Yu 	}
813d7eb8f1cSChao Yu 	return chksum;
814d7eb8f1cSChao Yu }
815d7eb8f1cSChao Yu 
816fc0065adSTiezhu Yang static int get_checkpoint_version(struct f2fs_sb_info *sbi, block_t cp_addr,
817fc0065adSTiezhu Yang 		struct f2fs_checkpoint **cp_block, struct page **cp_page,
818fc0065adSTiezhu Yang 		unsigned long long *version)
819fc0065adSTiezhu Yang {
820fc0065adSTiezhu Yang 	size_t crc_offset = 0;
821d7eb8f1cSChao Yu 	__u32 crc;
822fc0065adSTiezhu Yang 
8234d57b86dSChao Yu 	*cp_page = f2fs_get_meta_page(sbi, cp_addr);
8247735730dSChao Yu 	if (IS_ERR(*cp_page))
8257735730dSChao Yu 		return PTR_ERR(*cp_page);
8267735730dSChao Yu 
827fc0065adSTiezhu Yang 	*cp_block = (struct f2fs_checkpoint *)page_address(*cp_page);
828fc0065adSTiezhu Yang 
829fc0065adSTiezhu Yang 	crc_offset = le32_to_cpu((*cp_block)->checksum_offset);
830d7eb8f1cSChao Yu 	if (crc_offset < CP_MIN_CHKSUM_OFFSET ||
831d7eb8f1cSChao Yu 			crc_offset > CP_CHKSUM_OFFSET) {
832d3f07c04SChao Yu 		f2fs_put_page(*cp_page, 1);
833dcbb4c10SJoe Perches 		f2fs_warn(sbi, "invalid crc_offset: %zu", crc_offset);
834fc0065adSTiezhu Yang 		return -EINVAL;
835fc0065adSTiezhu Yang 	}
836fc0065adSTiezhu Yang 
837d7eb8f1cSChao Yu 	crc = f2fs_checkpoint_chksum(sbi, *cp_block);
838d7eb8f1cSChao Yu 	if (crc != cur_cp_crc(*cp_block)) {
839d3f07c04SChao Yu 		f2fs_put_page(*cp_page, 1);
840dcbb4c10SJoe Perches 		f2fs_warn(sbi, "invalid crc value");
841fc0065adSTiezhu Yang 		return -EINVAL;
842fc0065adSTiezhu Yang 	}
843fc0065adSTiezhu Yang 
844fc0065adSTiezhu Yang 	*version = cur_cp_version(*cp_block);
845fc0065adSTiezhu Yang 	return 0;
846fc0065adSTiezhu Yang }
847fc0065adSTiezhu Yang 
848127e670aSJaegeuk Kim static struct page *validate_checkpoint(struct f2fs_sb_info *sbi,
849127e670aSJaegeuk Kim 				block_t cp_addr, unsigned long long *version)
850127e670aSJaegeuk Kim {
851fc0065adSTiezhu Yang 	struct page *cp_page_1 = NULL, *cp_page_2 = NULL;
852fc0065adSTiezhu Yang 	struct f2fs_checkpoint *cp_block = NULL;
853127e670aSJaegeuk Kim 	unsigned long long cur_version = 0, pre_version = 0;
854fc0065adSTiezhu Yang 	int err;
855127e670aSJaegeuk Kim 
856fc0065adSTiezhu Yang 	err = get_checkpoint_version(sbi, cp_addr, &cp_block,
857fc0065adSTiezhu Yang 					&cp_page_1, version);
858fc0065adSTiezhu Yang 	if (err)
859d3f07c04SChao Yu 		return NULL;
860c9b60788SChao Yu 
861c9b60788SChao Yu 	if (le32_to_cpu(cp_block->cp_pack_total_block_count) >
862c9b60788SChao Yu 					sbi->blocks_per_seg) {
863dcbb4c10SJoe Perches 		f2fs_warn(sbi, "invalid cp_pack_total_block_count:%u",
864c9b60788SChao Yu 			  le32_to_cpu(cp_block->cp_pack_total_block_count));
865d3f07c04SChao Yu 		goto invalid_cp;
866c9b60788SChao Yu 	}
867fc0065adSTiezhu Yang 	pre_version = *version;
868127e670aSJaegeuk Kim 
86925ca923bSJaegeuk Kim 	cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1;
870fc0065adSTiezhu Yang 	err = get_checkpoint_version(sbi, cp_addr, &cp_block,
871fc0065adSTiezhu Yang 					&cp_page_2, version);
872fc0065adSTiezhu Yang 	if (err)
873d3f07c04SChao Yu 		goto invalid_cp;
874fc0065adSTiezhu Yang 	cur_version = *version;
875127e670aSJaegeuk Kim 
876127e670aSJaegeuk Kim 	if (cur_version == pre_version) {
877127e670aSJaegeuk Kim 		*version = cur_version;
878127e670aSJaegeuk Kim 		f2fs_put_page(cp_page_2, 1);
879127e670aSJaegeuk Kim 		return cp_page_1;
880127e670aSJaegeuk Kim 	}
881127e670aSJaegeuk Kim 	f2fs_put_page(cp_page_2, 1);
882d3f07c04SChao Yu invalid_cp:
883127e670aSJaegeuk Kim 	f2fs_put_page(cp_page_1, 1);
884127e670aSJaegeuk Kim 	return NULL;
885127e670aSJaegeuk Kim }
886127e670aSJaegeuk Kim 
8874d57b86dSChao Yu int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi)
888127e670aSJaegeuk Kim {
889127e670aSJaegeuk Kim 	struct f2fs_checkpoint *cp_block;
890127e670aSJaegeuk Kim 	struct f2fs_super_block *fsb = sbi->raw_super;
891127e670aSJaegeuk Kim 	struct page *cp1, *cp2, *cur_page;
892127e670aSJaegeuk Kim 	unsigned long blk_size = sbi->blocksize;
893127e670aSJaegeuk Kim 	unsigned long long cp1_version = 0, cp2_version = 0;
894127e670aSJaegeuk Kim 	unsigned long long cp_start_blk_no;
89555141486SWanpeng Li 	unsigned int cp_blks = 1 + __cp_payload(sbi);
8961dbe4152SChangman Lee 	block_t cp_blk_no;
8971dbe4152SChangman Lee 	int i;
89810f966bbSChao Yu 	int err;
899127e670aSJaegeuk Kim 
9000b6d4ca0SEric Biggers 	sbi->ckpt = f2fs_kvzalloc(sbi, array_size(blk_size, cp_blks),
901026f0507SKees Cook 				  GFP_KERNEL);
902127e670aSJaegeuk Kim 	if (!sbi->ckpt)
903127e670aSJaegeuk Kim 		return -ENOMEM;
904127e670aSJaegeuk Kim 	/*
905127e670aSJaegeuk Kim 	 * Finding out valid cp block involves read both
906127e670aSJaegeuk Kim 	 * sets( cp pack 1 and cp pack 2)
907127e670aSJaegeuk Kim 	 */
908127e670aSJaegeuk Kim 	cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr);
909127e670aSJaegeuk Kim 	cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
910127e670aSJaegeuk Kim 
911127e670aSJaegeuk Kim 	/* The second checkpoint pack should start at the next segment */
912f9a4e6dfSJaegeuk Kim 	cp_start_blk_no += ((unsigned long long)1) <<
913f9a4e6dfSJaegeuk Kim 				le32_to_cpu(fsb->log_blocks_per_seg);
914127e670aSJaegeuk Kim 	cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
915127e670aSJaegeuk Kim 
916127e670aSJaegeuk Kim 	if (cp1 && cp2) {
917127e670aSJaegeuk Kim 		if (ver_after(cp2_version, cp1_version))
918127e670aSJaegeuk Kim 			cur_page = cp2;
919127e670aSJaegeuk Kim 		else
920127e670aSJaegeuk Kim 			cur_page = cp1;
921127e670aSJaegeuk Kim 	} else if (cp1) {
922127e670aSJaegeuk Kim 		cur_page = cp1;
923127e670aSJaegeuk Kim 	} else if (cp2) {
924127e670aSJaegeuk Kim 		cur_page = cp2;
925127e670aSJaegeuk Kim 	} else {
92610f966bbSChao Yu 		err = -EFSCORRUPTED;
927127e670aSJaegeuk Kim 		goto fail_no_cp;
928127e670aSJaegeuk Kim 	}
929127e670aSJaegeuk Kim 
930127e670aSJaegeuk Kim 	cp_block = (struct f2fs_checkpoint *)page_address(cur_page);
931127e670aSJaegeuk Kim 	memcpy(sbi->ckpt, cp_block, blk_size);
932127e670aSJaegeuk Kim 
9338508e44aSJaegeuk Kim 	if (cur_page == cp1)
9348508e44aSJaegeuk Kim 		sbi->cur_cp_pack = 1;
9358508e44aSJaegeuk Kim 	else
9368508e44aSJaegeuk Kim 		sbi->cur_cp_pack = 2;
937984ec63cSShawn Lin 
938e494c2f9SChao Yu 	/* Sanity checking of checkpoint */
93910f966bbSChao Yu 	if (f2fs_sanity_check_ckpt(sbi)) {
94010f966bbSChao Yu 		err = -EFSCORRUPTED;
941e494c2f9SChao Yu 		goto free_fail_no_cp;
94210f966bbSChao Yu 	}
943e494c2f9SChao Yu 
9441dbe4152SChangman Lee 	if (cp_blks <= 1)
9451dbe4152SChangman Lee 		goto done;
9461dbe4152SChangman Lee 
9471dbe4152SChangman Lee 	cp_blk_no = le32_to_cpu(fsb->cp_blkaddr);
9481dbe4152SChangman Lee 	if (cur_page == cp2)
9491dbe4152SChangman Lee 		cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg);
9501dbe4152SChangman Lee 
9511dbe4152SChangman Lee 	for (i = 1; i < cp_blks; i++) {
9521dbe4152SChangman Lee 		void *sit_bitmap_ptr;
9531dbe4152SChangman Lee 		unsigned char *ckpt = (unsigned char *)sbi->ckpt;
9541dbe4152SChangman Lee 
9554d57b86dSChao Yu 		cur_page = f2fs_get_meta_page(sbi, cp_blk_no + i);
95610f966bbSChao Yu 		if (IS_ERR(cur_page)) {
95710f966bbSChao Yu 			err = PTR_ERR(cur_page);
9587735730dSChao Yu 			goto free_fail_no_cp;
95910f966bbSChao Yu 		}
9601dbe4152SChangman Lee 		sit_bitmap_ptr = page_address(cur_page);
9611dbe4152SChangman Lee 		memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size);
9621dbe4152SChangman Lee 		f2fs_put_page(cur_page, 1);
9631dbe4152SChangman Lee 	}
9641dbe4152SChangman Lee done:
965127e670aSJaegeuk Kim 	f2fs_put_page(cp1, 1);
966127e670aSJaegeuk Kim 	f2fs_put_page(cp2, 1);
967127e670aSJaegeuk Kim 	return 0;
968127e670aSJaegeuk Kim 
969a2125ff7SJaegeuk Kim free_fail_no_cp:
970a2125ff7SJaegeuk Kim 	f2fs_put_page(cp1, 1);
971a2125ff7SJaegeuk Kim 	f2fs_put_page(cp2, 1);
972127e670aSJaegeuk Kim fail_no_cp:
9735222595dSJaegeuk Kim 	kvfree(sbi->ckpt);
97410f966bbSChao Yu 	return err;
975127e670aSJaegeuk Kim }
976127e670aSJaegeuk Kim 
977c227f912SChao Yu static void __add_dirty_inode(struct inode *inode, enum inode_type type)
978127e670aSJaegeuk Kim {
9794081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
980c227f912SChao Yu 	int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
9812d7b822aSChao Yu 
98291942321SJaegeuk Kim 	if (is_inode_flag_set(inode, flag))
9832710fd7eSChao Yu 		return;
9842d7b822aSChao Yu 
98591942321SJaegeuk Kim 	set_inode_flag(inode, flag);
98699f4b917SChao Yu 	if (!f2fs_is_volatile_file(inode))
98799f4b917SChao Yu 		list_add_tail(&F2FS_I(inode)->dirty_list,
98899f4b917SChao Yu 						&sbi->inode_list[type]);
98933fbd510SChao Yu 	stat_inc_dirty_inode(sbi, type);
9905deb8267SJaegeuk Kim }
9915deb8267SJaegeuk Kim 
992c227f912SChao Yu static void __remove_dirty_inode(struct inode *inode, enum inode_type type)
9936ad7609aSChao Yu {
994c227f912SChao Yu 	int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
9956ad7609aSChao Yu 
99691942321SJaegeuk Kim 	if (get_dirty_pages(inode) || !is_inode_flag_set(inode, flag))
9976ad7609aSChao Yu 		return;
9986ad7609aSChao Yu 
99991942321SJaegeuk Kim 	list_del_init(&F2FS_I(inode)->dirty_list);
100091942321SJaegeuk Kim 	clear_inode_flag(inode, flag);
100133fbd510SChao Yu 	stat_dec_dirty_inode(F2FS_I_SB(inode), type);
10026ad7609aSChao Yu }
10036ad7609aSChao Yu 
10044d57b86dSChao Yu void f2fs_update_dirty_page(struct inode *inode, struct page *page)
10055deb8267SJaegeuk Kim {
10064081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1007c227f912SChao Yu 	enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
10085deb8267SJaegeuk Kim 
10095ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
10105ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
1011127e670aSJaegeuk Kim 		return;
10127bd59381SGu Zheng 
1013c227f912SChao Yu 	spin_lock(&sbi->inode_lock[type]);
10141c4bf763SJaegeuk Kim 	if (type != FILE_INODE || test_opt(sbi, DATA_FLUSH))
1015c227f912SChao Yu 		__add_dirty_inode(inode, type);
1016b951a4ecSYunlei He 	inode_inc_dirty_pages(inode);
10171c4bf763SJaegeuk Kim 	spin_unlock(&sbi->inode_lock[type]);
10181c4bf763SJaegeuk Kim 
1019240a5915SChao Yu 	f2fs_set_page_private(page, 0);
10209e4ded3fSJaegeuk Kim 	f2fs_trace_pid(page);
10215deb8267SJaegeuk Kim }
1022127e670aSJaegeuk Kim 
10234d57b86dSChao Yu void f2fs_remove_dirty_inode(struct inode *inode)
1024127e670aSJaegeuk Kim {
10254081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1026c227f912SChao Yu 	enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
1027127e670aSJaegeuk Kim 
1028c227f912SChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
1029c227f912SChao Yu 			!S_ISLNK(inode->i_mode))
1030127e670aSJaegeuk Kim 		return;
1031127e670aSJaegeuk Kim 
103210aa97c3SJaegeuk Kim 	if (type == FILE_INODE && !test_opt(sbi, DATA_FLUSH))
103310aa97c3SJaegeuk Kim 		return;
103410aa97c3SJaegeuk Kim 
1035c227f912SChao Yu 	spin_lock(&sbi->inode_lock[type]);
1036c227f912SChao Yu 	__remove_dirty_inode(inode, type);
1037c227f912SChao Yu 	spin_unlock(&sbi->inode_lock[type]);
1038afc3eda2SJaegeuk Kim }
103974d0b917SJaegeuk Kim 
10404d57b86dSChao Yu int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type)
1041127e670aSJaegeuk Kim {
1042ce3b7d80SGu Zheng 	struct list_head *head;
1043127e670aSJaegeuk Kim 	struct inode *inode;
10442710fd7eSChao Yu 	struct f2fs_inode_info *fi;
10454cf18537SChao Yu 	bool is_dir = (type == DIR_INODE);
10464db08d01SJaegeuk Kim 	unsigned long ino = 0;
10474cf18537SChao Yu 
10484cf18537SChao Yu 	trace_f2fs_sync_dirty_inodes_enter(sbi->sb, is_dir,
10494cf18537SChao Yu 				get_pages(sbi, is_dir ?
10504cf18537SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
1051127e670aSJaegeuk Kim retry:
10529b664822SZhang Qilong 	if (unlikely(f2fs_cp_error(sbi))) {
10539b664822SZhang Qilong 		trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir,
10549b664822SZhang Qilong 				get_pages(sbi, is_dir ?
10559b664822SZhang Qilong 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
10566d5a1495SChao Yu 		return -EIO;
10579b664822SZhang Qilong 	}
1058af41d3eeSJaegeuk Kim 
1059c227f912SChao Yu 	spin_lock(&sbi->inode_lock[type]);
1060ce3b7d80SGu Zheng 
1061c227f912SChao Yu 	head = &sbi->inode_list[type];
1062127e670aSJaegeuk Kim 	if (list_empty(head)) {
1063c227f912SChao Yu 		spin_unlock(&sbi->inode_lock[type]);
10644cf18537SChao Yu 		trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir,
10654cf18537SChao Yu 				get_pages(sbi, is_dir ?
10664cf18537SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
10676d5a1495SChao Yu 		return 0;
1068127e670aSJaegeuk Kim 	}
1069939afa94SChao Yu 	fi = list_first_entry(head, struct f2fs_inode_info, dirty_list);
10702710fd7eSChao Yu 	inode = igrab(&fi->vfs_inode);
1071c227f912SChao Yu 	spin_unlock(&sbi->inode_lock[type]);
1072127e670aSJaegeuk Kim 	if (inode) {
10734db08d01SJaegeuk Kim 		unsigned long cur_ino = inode->i_ino;
10744db08d01SJaegeuk Kim 
1075b0af6d49SChao Yu 		F2FS_I(inode)->cp_task = current;
1076b0af6d49SChao Yu 
107787d6f890SJaegeuk Kim 		filemap_fdatawrite(inode->i_mapping);
1078b0af6d49SChao Yu 
1079b0af6d49SChao Yu 		F2FS_I(inode)->cp_task = NULL;
1080b0af6d49SChao Yu 
1081127e670aSJaegeuk Kim 		iput(inode);
10824db08d01SJaegeuk Kim 		/* We need to give cpu to another writers. */
10832a63531aSYunlei He 		if (ino == cur_ino)
10844db08d01SJaegeuk Kim 			cond_resched();
10852a63531aSYunlei He 		else
10864db08d01SJaegeuk Kim 			ino = cur_ino;
1087127e670aSJaegeuk Kim 	} else {
1088127e670aSJaegeuk Kim 		/*
1089127e670aSJaegeuk Kim 		 * We should submit bio, since it exists several
1090127e670aSJaegeuk Kim 		 * wribacking dentry pages in the freeing inode.
1091127e670aSJaegeuk Kim 		 */
1092b9109b0eSJaegeuk Kim 		f2fs_submit_merged_write(sbi, DATA);
10937ecebe5eSSebastian Andrzej Siewior 		cond_resched();
1094127e670aSJaegeuk Kim 	}
1095127e670aSJaegeuk Kim 	goto retry;
1096127e670aSJaegeuk Kim }
1097127e670aSJaegeuk Kim 
10980f18b462SJaegeuk Kim int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi)
10990f18b462SJaegeuk Kim {
11000f18b462SJaegeuk Kim 	struct list_head *head = &sbi->inode_list[DIRTY_META];
11010f18b462SJaegeuk Kim 	struct inode *inode;
11020f18b462SJaegeuk Kim 	struct f2fs_inode_info *fi;
11030f18b462SJaegeuk Kim 	s64 total = get_pages(sbi, F2FS_DIRTY_IMETA);
11040f18b462SJaegeuk Kim 
11050f18b462SJaegeuk Kim 	while (total--) {
11060f18b462SJaegeuk Kim 		if (unlikely(f2fs_cp_error(sbi)))
11070f18b462SJaegeuk Kim 			return -EIO;
11080f18b462SJaegeuk Kim 
11090f18b462SJaegeuk Kim 		spin_lock(&sbi->inode_lock[DIRTY_META]);
11100f18b462SJaegeuk Kim 		if (list_empty(head)) {
11110f18b462SJaegeuk Kim 			spin_unlock(&sbi->inode_lock[DIRTY_META]);
11120f18b462SJaegeuk Kim 			return 0;
11130f18b462SJaegeuk Kim 		}
1114939afa94SChao Yu 		fi = list_first_entry(head, struct f2fs_inode_info,
11150f18b462SJaegeuk Kim 							gdirty_list);
11160f18b462SJaegeuk Kim 		inode = igrab(&fi->vfs_inode);
11170f18b462SJaegeuk Kim 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
11180f18b462SJaegeuk Kim 		if (inode) {
111918340edcSJaegeuk Kim 			sync_inode_metadata(inode, 0);
112018340edcSJaegeuk Kim 
112118340edcSJaegeuk Kim 			/* it's on eviction */
112218340edcSJaegeuk Kim 			if (is_inode_flag_set(inode, FI_DIRTY_INODE))
11234d57b86dSChao Yu 				f2fs_update_inode_page(inode);
11240f18b462SJaegeuk Kim 			iput(inode);
11250f18b462SJaegeuk Kim 		}
1126dee668c1SChao Yu 	}
11270f18b462SJaegeuk Kim 	return 0;
11280f18b462SJaegeuk Kim }
11290f18b462SJaegeuk Kim 
113059c9081bSYunlei He static void __prepare_cp_block(struct f2fs_sb_info *sbi)
113159c9081bSYunlei He {
113259c9081bSYunlei He 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
113359c9081bSYunlei He 	struct f2fs_nm_info *nm_i = NM_I(sbi);
113459c9081bSYunlei He 	nid_t last_nid = nm_i->next_scan_nid;
113559c9081bSYunlei He 
113659c9081bSYunlei He 	next_free_nid(sbi, &last_nid);
113759c9081bSYunlei He 	ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
113859c9081bSYunlei He 	ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi));
113959c9081bSYunlei He 	ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi));
114059c9081bSYunlei He 	ckpt->next_free_nid = cpu_to_le32(last_nid);
114159c9081bSYunlei He }
114259c9081bSYunlei He 
1143af033b2aSChao Yu static bool __need_flush_quota(struct f2fs_sb_info *sbi)
1144af033b2aSChao Yu {
1145db6ec53bSJaegeuk Kim 	bool ret = false;
1146db6ec53bSJaegeuk Kim 
1147af033b2aSChao Yu 	if (!is_journalled_quota(sbi))
1148af033b2aSChao Yu 		return false;
1149db6ec53bSJaegeuk Kim 
1150db6ec53bSJaegeuk Kim 	down_write(&sbi->quota_sem);
1151db6ec53bSJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH)) {
1152db6ec53bSJaegeuk Kim 		ret = false;
1153db6ec53bSJaegeuk Kim 	} else if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR)) {
1154db6ec53bSJaegeuk Kim 		ret = false;
1155db6ec53bSJaegeuk Kim 	} else if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_FLUSH)) {
1156db6ec53bSJaegeuk Kim 		clear_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH);
1157db6ec53bSJaegeuk Kim 		ret = true;
1158db6ec53bSJaegeuk Kim 	} else if (get_pages(sbi, F2FS_DIRTY_QDATA)) {
1159db6ec53bSJaegeuk Kim 		ret = true;
1160db6ec53bSJaegeuk Kim 	}
1161db6ec53bSJaegeuk Kim 	up_write(&sbi->quota_sem);
1162db6ec53bSJaegeuk Kim 	return ret;
1163af033b2aSChao Yu }
1164af033b2aSChao Yu 
11650a8165d7SJaegeuk Kim /*
1166127e670aSJaegeuk Kim  * Freeze all the FS-operations for checkpoint.
1167127e670aSJaegeuk Kim  */
1168cf779cabSJaegeuk Kim static int block_operations(struct f2fs_sb_info *sbi)
1169127e670aSJaegeuk Kim {
1170127e670aSJaegeuk Kim 	struct writeback_control wbc = {
1171127e670aSJaegeuk Kim 		.sync_mode = WB_SYNC_ALL,
1172127e670aSJaegeuk Kim 		.nr_to_write = LONG_MAX,
1173127e670aSJaegeuk Kim 		.for_reclaim = 0,
1174127e670aSJaegeuk Kim 	};
1175af033b2aSChao Yu 	int err = 0, cnt = 0;
1176c718379bSJaegeuk Kim 
117734c061adSSayali Lokhande 	/*
117834c061adSSayali Lokhande 	 * Let's flush inline_data in dirty node pages.
117934c061adSSayali Lokhande 	 */
118034c061adSSayali Lokhande 	f2fs_flush_inline_data(sbi);
118134c061adSSayali Lokhande 
1182af033b2aSChao Yu retry_flush_quotas:
1183db6ec53bSJaegeuk Kim 	f2fs_lock_all(sbi);
1184af033b2aSChao Yu 	if (__need_flush_quota(sbi)) {
1185af033b2aSChao Yu 		int locked;
1186af033b2aSChao Yu 
1187af033b2aSChao Yu 		if (++cnt > DEFAULT_RETRY_QUOTA_FLUSH_COUNT) {
1188af033b2aSChao Yu 			set_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH);
1189db6ec53bSJaegeuk Kim 			set_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH);
1190af033b2aSChao Yu 			goto retry_flush_dents;
1191af033b2aSChao Yu 		}
1192db6ec53bSJaegeuk Kim 		f2fs_unlock_all(sbi);
1193af033b2aSChao Yu 
1194af033b2aSChao Yu 		/* only failed during mount/umount/freeze/quotactl */
1195af033b2aSChao Yu 		locked = down_read_trylock(&sbi->sb->s_umount);
1196af033b2aSChao Yu 		f2fs_quota_sync(sbi->sb, -1);
1197af033b2aSChao Yu 		if (locked)
1198af033b2aSChao Yu 			up_read(&sbi->sb->s_umount);
1199af033b2aSChao Yu 		cond_resched();
1200af033b2aSChao Yu 		goto retry_flush_quotas;
1201af033b2aSChao Yu 	}
1202af033b2aSChao Yu 
1203af033b2aSChao Yu retry_flush_dents:
1204127e670aSJaegeuk Kim 	/* write all the dirty dentry pages */
1205127e670aSJaegeuk Kim 	if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
1206e479556bSGu Zheng 		f2fs_unlock_all(sbi);
12074d57b86dSChao Yu 		err = f2fs_sync_dirty_inodes(sbi, DIR_INODE);
12086d5a1495SChao Yu 		if (err)
12091f5f11a3SJaegeuk Kim 			return err;
121030973883SJaegeuk Kim 		cond_resched();
1211af033b2aSChao Yu 		goto retry_flush_quotas;
1212127e670aSJaegeuk Kim 	}
1213127e670aSJaegeuk Kim 
121459c9081bSYunlei He 	/*
121559c9081bSYunlei He 	 * POR: we should ensure that there are no dirty node pages
121659c9081bSYunlei He 	 * until finishing nat/sit flush. inode->i_blocks can be updated.
121759c9081bSYunlei He 	 */
121859c9081bSYunlei He 	down_write(&sbi->node_change);
121959c9081bSYunlei He 
12200f18b462SJaegeuk Kim 	if (get_pages(sbi, F2FS_DIRTY_IMETA)) {
122159c9081bSYunlei He 		up_write(&sbi->node_change);
12220f18b462SJaegeuk Kim 		f2fs_unlock_all(sbi);
12230f18b462SJaegeuk Kim 		err = f2fs_sync_inode_meta(sbi);
12240f18b462SJaegeuk Kim 		if (err)
12251f5f11a3SJaegeuk Kim 			return err;
122630973883SJaegeuk Kim 		cond_resched();
1227af033b2aSChao Yu 		goto retry_flush_quotas;
12280f18b462SJaegeuk Kim 	}
12290f18b462SJaegeuk Kim 
123039936837SJaegeuk Kim retry_flush_nodes:
1231b3582c68SChao Yu 	down_write(&sbi->node_write);
1232127e670aSJaegeuk Kim 
1233127e670aSJaegeuk Kim 	if (get_pages(sbi, F2FS_DIRTY_NODES)) {
1234b3582c68SChao Yu 		up_write(&sbi->node_write);
1235c29fd0c0SChao Yu 		atomic_inc(&sbi->wb_sync_req[NODE]);
12364d57b86dSChao Yu 		err = f2fs_sync_node_pages(sbi, &wbc, false, FS_CP_NODE_IO);
1237c29fd0c0SChao Yu 		atomic_dec(&sbi->wb_sync_req[NODE]);
12386d5a1495SChao Yu 		if (err) {
123959c9081bSYunlei He 			up_write(&sbi->node_change);
1240cf779cabSJaegeuk Kim 			f2fs_unlock_all(sbi);
12411f5f11a3SJaegeuk Kim 			return err;
1242cf779cabSJaegeuk Kim 		}
124330973883SJaegeuk Kim 		cond_resched();
124439936837SJaegeuk Kim 		goto retry_flush_nodes;
1245127e670aSJaegeuk Kim 	}
124659c9081bSYunlei He 
124759c9081bSYunlei He 	/*
124859c9081bSYunlei He 	 * sbi->node_change is used only for AIO write_begin path which produces
124959c9081bSYunlei He 	 * dirty node blocks and some checkpoint values by block allocation.
125059c9081bSYunlei He 	 */
125159c9081bSYunlei He 	__prepare_cp_block(sbi);
125259c9081bSYunlei He 	up_write(&sbi->node_change);
1253cf779cabSJaegeuk Kim 	return err;
1254127e670aSJaegeuk Kim }
1255127e670aSJaegeuk Kim 
1256127e670aSJaegeuk Kim static void unblock_operations(struct f2fs_sb_info *sbi)
1257127e670aSJaegeuk Kim {
1258b3582c68SChao Yu 	up_write(&sbi->node_write);
1259e479556bSGu Zheng 	f2fs_unlock_all(sbi);
1260127e670aSJaegeuk Kim }
1261127e670aSJaegeuk Kim 
1262bf22c3ccSSahitya Tummala void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type)
1263fb51b5efSChangman Lee {
1264fb51b5efSChangman Lee 	DEFINE_WAIT(wait);
1265fb51b5efSChangman Lee 
1266fb51b5efSChangman Lee 	for (;;) {
1267bf22c3ccSSahitya Tummala 		if (!get_pages(sbi, type))
1268fb51b5efSChangman Lee 			break;
1269fb51b5efSChangman Lee 
1270af697c0fSJaegeuk Kim 		if (unlikely(f2fs_cp_error(sbi)))
1271af697c0fSJaegeuk Kim 			break;
1272af697c0fSJaegeuk Kim 
12739c30df7cSJaegeuk Kim 		if (type == F2FS_DIRTY_META)
12749c30df7cSJaegeuk Kim 			f2fs_sync_meta_pages(sbi, META, LONG_MAX,
12759c30df7cSJaegeuk Kim 							FS_CP_META_IO);
12761fd28018SJaegeuk Kim 		else if (type == F2FS_WB_CP_DATA)
12771fd28018SJaegeuk Kim 			f2fs_submit_merged_write(sbi, DATA);
1278828add77SJaegeuk Kim 
1279828add77SJaegeuk Kim 		prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
12805df7731fSChao Yu 		io_schedule_timeout(DEFAULT_IO_TIMEOUT);
1281fb51b5efSChangman Lee 	}
1282fb51b5efSChangman Lee 	finish_wait(&sbi->cp_wait, &wait);
1283fb51b5efSChangman Lee }
1284fb51b5efSChangman Lee 
1285e4c5d848SJaegeuk Kim static void update_ckpt_flags(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1286e4c5d848SJaegeuk Kim {
1287e4c5d848SJaegeuk Kim 	unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num;
1288e4c5d848SJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1289d1aa2453SChao Yu 	unsigned long flags;
1290e4c5d848SJaegeuk Kim 
1291d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1292e4c5d848SJaegeuk Kim 
1293c473f1a9SChao Yu 	if ((cpc->reason & CP_UMOUNT) &&
129410047f53SKinglong Mee 			le32_to_cpu(ckpt->cp_pack_total_block_count) >
129522ad0b6aSJaegeuk Kim 			sbi->blocks_per_seg - NM_I(sbi)->nat_bits_blocks)
129622ad0b6aSJaegeuk Kim 		disable_nat_bits(sbi, false);
129722ad0b6aSJaegeuk Kim 
12981f43e2adSChao Yu 	if (cpc->reason & CP_TRIMMED)
12991f43e2adSChao Yu 		__set_ckpt_flags(ckpt, CP_TRIMMED_FLAG);
1300cd36d7a1SChao Yu 	else
1301cd36d7a1SChao Yu 		__clear_ckpt_flags(ckpt, CP_TRIMMED_FLAG);
13021f43e2adSChao Yu 
1303c473f1a9SChao Yu 	if (cpc->reason & CP_UMOUNT)
1304e4c5d848SJaegeuk Kim 		__set_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1305e4c5d848SJaegeuk Kim 	else
1306e4c5d848SJaegeuk Kim 		__clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1307e4c5d848SJaegeuk Kim 
1308c473f1a9SChao Yu 	if (cpc->reason & CP_FASTBOOT)
1309e4c5d848SJaegeuk Kim 		__set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1310e4c5d848SJaegeuk Kim 	else
1311e4c5d848SJaegeuk Kim 		__clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1312e4c5d848SJaegeuk Kim 
1313e4c5d848SJaegeuk Kim 	if (orphan_num)
1314e4c5d848SJaegeuk Kim 		__set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1315e4c5d848SJaegeuk Kim 	else
1316e4c5d848SJaegeuk Kim 		__clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1317e4c5d848SJaegeuk Kim 
1318c84ef3c5SSahitya Tummala 	if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
1319e4c5d848SJaegeuk Kim 		__set_ckpt_flags(ckpt, CP_FSCK_FLAG);
1320e4c5d848SJaegeuk Kim 
1321c84ef3c5SSahitya Tummala 	if (is_sbi_flag_set(sbi, SBI_IS_RESIZEFS))
1322c84ef3c5SSahitya Tummala 		__set_ckpt_flags(ckpt, CP_RESIZEFS_FLAG);
1323c84ef3c5SSahitya Tummala 	else
1324c84ef3c5SSahitya Tummala 		__clear_ckpt_flags(ckpt, CP_RESIZEFS_FLAG);
1325c84ef3c5SSahitya Tummala 
13264354994fSDaniel Rosenberg 	if (is_sbi_flag_set(sbi, SBI_CP_DISABLED))
13274354994fSDaniel Rosenberg 		__set_ckpt_flags(ckpt, CP_DISABLED_FLAG);
13284354994fSDaniel Rosenberg 	else
13294354994fSDaniel Rosenberg 		__clear_ckpt_flags(ckpt, CP_DISABLED_FLAG);
13304354994fSDaniel Rosenberg 
1331db610a64SJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_CP_DISABLED_QUICK))
1332db610a64SJaegeuk Kim 		__set_ckpt_flags(ckpt, CP_DISABLED_QUICK_FLAG);
1333db610a64SJaegeuk Kim 	else
1334db610a64SJaegeuk Kim 		__clear_ckpt_flags(ckpt, CP_DISABLED_QUICK_FLAG);
1335db610a64SJaegeuk Kim 
1336af033b2aSChao Yu 	if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH))
1337af033b2aSChao Yu 		__set_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG);
1338bc88ac96SJaegeuk Kim 	else
1339bc88ac96SJaegeuk Kim 		__clear_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG);
1340af033b2aSChao Yu 
1341af033b2aSChao Yu 	if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR))
1342af033b2aSChao Yu 		__set_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG);
1343af033b2aSChao Yu 
1344e4c5d848SJaegeuk Kim 	/* set this flag to activate crc|cp_ver for recovery */
1345e4c5d848SJaegeuk Kim 	__set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG);
1346f2367923SJaegeuk Kim 	__clear_ckpt_flags(ckpt, CP_NOCRC_RECOVERY_FLAG);
1347e4c5d848SJaegeuk Kim 
1348d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1349e4c5d848SJaegeuk Kim }
1350e4c5d848SJaegeuk Kim 
135146706d59SGao Xiang static void commit_checkpoint(struct f2fs_sb_info *sbi,
135246706d59SGao Xiang 	void *src, block_t blk_addr)
135346706d59SGao Xiang {
135446706d59SGao Xiang 	struct writeback_control wbc = {
135546706d59SGao Xiang 		.for_reclaim = 0,
135646706d59SGao Xiang 	};
135746706d59SGao Xiang 
135846706d59SGao Xiang 	/*
135946706d59SGao Xiang 	 * pagevec_lookup_tag and lock_page again will take
13604d57b86dSChao Yu 	 * some extra time. Therefore, f2fs_update_meta_pages and
13614d57b86dSChao Yu 	 * f2fs_sync_meta_pages are combined in this function.
136246706d59SGao Xiang 	 */
13634d57b86dSChao Yu 	struct page *page = f2fs_grab_meta_page(sbi, blk_addr);
136446706d59SGao Xiang 	int err;
136546706d59SGao Xiang 
1366bae0ee7aSChao Yu 	f2fs_wait_on_page_writeback(page, META, true, true);
13678d64d365SChao Yu 
13688d64d365SChao Yu 	memcpy(page_address(page), src, PAGE_SIZE);
13698d64d365SChao Yu 
13708d64d365SChao Yu 	set_page_dirty(page);
137146706d59SGao Xiang 	if (unlikely(!clear_page_dirty_for_io(page)))
137246706d59SGao Xiang 		f2fs_bug_on(sbi, 1);
137346706d59SGao Xiang 
137446706d59SGao Xiang 	/* writeout cp pack 2 page */
137546706d59SGao Xiang 	err = __f2fs_write_meta_page(page, &wbc, FS_CP_META_IO);
1376af697c0fSJaegeuk Kim 	if (unlikely(err && f2fs_cp_error(sbi))) {
1377af697c0fSJaegeuk Kim 		f2fs_put_page(page, 1);
1378af697c0fSJaegeuk Kim 		return;
1379af697c0fSJaegeuk Kim 	}
138046706d59SGao Xiang 
1381af697c0fSJaegeuk Kim 	f2fs_bug_on(sbi, err);
138246706d59SGao Xiang 	f2fs_put_page(page, 0);
138346706d59SGao Xiang 
138446706d59SGao Xiang 	/* submit checkpoint (with barrier if NOBARRIER is not set) */
138546706d59SGao Xiang 	f2fs_submit_merged_write(sbi, META_FLUSH);
138646706d59SGao Xiang }
138746706d59SGao Xiang 
1388c34f42e2SChao Yu static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1389127e670aSJaegeuk Kim {
1390127e670aSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
139177041823SHuang Ying 	struct f2fs_nm_info *nm_i = NM_I(sbi);
1392d1aa2453SChao Yu 	unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num, flags;
1393127e670aSJaegeuk Kim 	block_t start_blk;
1394127e670aSJaegeuk Kim 	unsigned int data_sum_blocks, orphan_blocks;
13957e586fa0SJaegeuk Kim 	__u32 crc32 = 0;
1396127e670aSJaegeuk Kim 	int i;
139755141486SWanpeng Li 	int cp_payload_blks = __cp_payload(sbi);
13988f1dbbbbSShuoran Liu 	struct super_block *sb = sbi->sb;
13998f1dbbbbSShuoran Liu 	struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
14008f1dbbbbSShuoran Liu 	u64 kbytes_written;
14011228b482SChao Yu 	int err;
1402127e670aSJaegeuk Kim 
1403127e670aSJaegeuk Kim 	/* Flush all the NAT/SIT pages */
14044d57b86dSChao Yu 	f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
1405127e670aSJaegeuk Kim 
14067a88ddb5SChao Yu 	/* start to update checkpoint, cp ver is already updated previously */
1407a1f72ac2SChao Yu 	ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi, true));
1408127e670aSJaegeuk Kim 	ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
1409b5b82205SChao Yu 	for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
1410127e670aSJaegeuk Kim 		ckpt->cur_node_segno[i] =
1411127e670aSJaegeuk Kim 			cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE));
1412127e670aSJaegeuk Kim 		ckpt->cur_node_blkoff[i] =
1413127e670aSJaegeuk Kim 			cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE));
1414127e670aSJaegeuk Kim 		ckpt->alloc_type[i + CURSEG_HOT_NODE] =
1415127e670aSJaegeuk Kim 				curseg_alloc_type(sbi, i + CURSEG_HOT_NODE);
1416127e670aSJaegeuk Kim 	}
1417b5b82205SChao Yu 	for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
1418127e670aSJaegeuk Kim 		ckpt->cur_data_segno[i] =
1419127e670aSJaegeuk Kim 			cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA));
1420127e670aSJaegeuk Kim 		ckpt->cur_data_blkoff[i] =
1421127e670aSJaegeuk Kim 			cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA));
1422127e670aSJaegeuk Kim 		ckpt->alloc_type[i + CURSEG_HOT_DATA] =
1423127e670aSJaegeuk Kim 				curseg_alloc_type(sbi, i + CURSEG_HOT_DATA);
1424127e670aSJaegeuk Kim 	}
1425127e670aSJaegeuk Kim 
1426127e670aSJaegeuk Kim 	/* 2 cp + n data seg summary + orphan inode blocks */
14274d57b86dSChao Yu 	data_sum_blocks = f2fs_npages_for_summary_flush(sbi, false);
1428d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1429b5b82205SChao Yu 	if (data_sum_blocks < NR_CURSEG_DATA_TYPE)
1430aaec2b1dSChao Yu 		__set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
1431127e670aSJaegeuk Kim 	else
1432aaec2b1dSChao Yu 		__clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
1433d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1434127e670aSJaegeuk Kim 
143567298804SChao Yu 	orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num);
14361dbe4152SChangman Lee 	ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks +
14371dbe4152SChangman Lee 			orphan_blocks);
1438127e670aSJaegeuk Kim 
1439119ee914SJaegeuk Kim 	if (__remain_node_summaries(cpc->reason))
1440b5b82205SChao Yu 		ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS+
14411dbe4152SChangman Lee 				cp_payload_blks + data_sum_blocks +
14421dbe4152SChangman Lee 				orphan_blocks + NR_CURSEG_NODE_TYPE);
1443119ee914SJaegeuk Kim 	else
1444b5b82205SChao Yu 		ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
14451dbe4152SChangman Lee 				cp_payload_blks + data_sum_blocks +
14461dbe4152SChangman Lee 				orphan_blocks);
1447119ee914SJaegeuk Kim 
1448e4c5d848SJaegeuk Kim 	/* update ckpt flag for checkpoint */
1449e4c5d848SJaegeuk Kim 	update_ckpt_flags(sbi, cpc);
1450a468f0efSJaegeuk Kim 
1451127e670aSJaegeuk Kim 	/* update SIT/NAT bitmap */
1452127e670aSJaegeuk Kim 	get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
1453127e670aSJaegeuk Kim 	get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
1454127e670aSJaegeuk Kim 
1455d7eb8f1cSChao Yu 	crc32 = f2fs_checkpoint_chksum(sbi, ckpt);
14567e586fa0SJaegeuk Kim 	*((__le32 *)((unsigned char *)ckpt +
14577e586fa0SJaegeuk Kim 				le32_to_cpu(ckpt->checksum_offset)))
1458127e670aSJaegeuk Kim 				= cpu_to_le32(crc32);
1459127e670aSJaegeuk Kim 
14608508e44aSJaegeuk Kim 	start_blk = __start_cp_next_addr(sbi);
1461127e670aSJaegeuk Kim 
146222ad0b6aSJaegeuk Kim 	/* write nat bits */
146322ad0b6aSJaegeuk Kim 	if (enabled_nat_bits(sbi, cpc)) {
146422ad0b6aSJaegeuk Kim 		__u64 cp_ver = cur_cp_version(ckpt);
146522ad0b6aSJaegeuk Kim 		block_t blk;
146622ad0b6aSJaegeuk Kim 
146722ad0b6aSJaegeuk Kim 		cp_ver |= ((__u64)crc32 << 32);
146822ad0b6aSJaegeuk Kim 		*(__le64 *)nm_i->nat_bits = cpu_to_le64(cp_ver);
146922ad0b6aSJaegeuk Kim 
147022ad0b6aSJaegeuk Kim 		blk = start_blk + sbi->blocks_per_seg - nm_i->nat_bits_blocks;
147122ad0b6aSJaegeuk Kim 		for (i = 0; i < nm_i->nat_bits_blocks; i++)
14724d57b86dSChao Yu 			f2fs_update_meta_page(sbi, nm_i->nat_bits +
147322ad0b6aSJaegeuk Kim 					(i << F2FS_BLKSIZE_BITS), blk + i);
147422ad0b6aSJaegeuk Kim 	}
147522ad0b6aSJaegeuk Kim 
1476127e670aSJaegeuk Kim 	/* write out checkpoint buffer at block 0 */
14774d57b86dSChao Yu 	f2fs_update_meta_page(sbi, ckpt, start_blk++);
1478127e670aSJaegeuk Kim 
1479381722d2SChao Yu 	for (i = 1; i < 1 + cp_payload_blks; i++)
14804d57b86dSChao Yu 		f2fs_update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE,
1481381722d2SChao Yu 							start_blk++);
14821dbe4152SChangman Lee 
148367298804SChao Yu 	if (orphan_num) {
1484127e670aSJaegeuk Kim 		write_orphan_inodes(sbi, start_blk);
1485127e670aSJaegeuk Kim 		start_blk += orphan_blocks;
1486127e670aSJaegeuk Kim 	}
1487127e670aSJaegeuk Kim 
14884d57b86dSChao Yu 	f2fs_write_data_summaries(sbi, start_blk);
1489127e670aSJaegeuk Kim 	start_blk += data_sum_blocks;
14908f1dbbbbSShuoran Liu 
14918f1dbbbbSShuoran Liu 	/* Record write statistics in the hot node summary */
14928f1dbbbbSShuoran Liu 	kbytes_written = sbi->kbytes_written;
14938f1dbbbbSShuoran Liu 	if (sb->s_bdev->bd_part)
14948f1dbbbbSShuoran Liu 		kbytes_written += BD_PART_WRITTEN(sbi);
14958f1dbbbbSShuoran Liu 
1496b7ad7512SChao Yu 	seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written);
14978f1dbbbbSShuoran Liu 
1498119ee914SJaegeuk Kim 	if (__remain_node_summaries(cpc->reason)) {
14994d57b86dSChao Yu 		f2fs_write_node_summaries(sbi, start_blk);
1500127e670aSJaegeuk Kim 		start_blk += NR_CURSEG_NODE_TYPE;
1501127e670aSJaegeuk Kim 	}
1502127e670aSJaegeuk Kim 
150346706d59SGao Xiang 	/* update user_block_counts */
150446706d59SGao Xiang 	sbi->last_valid_block_count = sbi->total_valid_block_count;
150546706d59SGao Xiang 	percpu_counter_set(&sbi->alloc_valid_block_count, 0);
1506127e670aSJaegeuk Kim 
150746706d59SGao Xiang 	/* Here, we have one bio having CP pack except cp pack 2 page */
15084d57b86dSChao Yu 	f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
1509bf22c3ccSSahitya Tummala 	/* Wait for all dirty meta pages to be submitted for IO */
1510bf22c3ccSSahitya Tummala 	f2fs_wait_on_all_pages(sbi, F2FS_DIRTY_META);
151146706d59SGao Xiang 
151246706d59SGao Xiang 	/* wait for previous submitted meta pages writeback */
1513bf22c3ccSSahitya Tummala 	f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
1514127e670aSJaegeuk Kim 
151546706d59SGao Xiang 	/* flush all device cache */
151646706d59SGao Xiang 	err = f2fs_flush_device_cache(sbi);
151746706d59SGao Xiang 	if (err)
151846706d59SGao Xiang 		return err;
1519127e670aSJaegeuk Kim 
152046706d59SGao Xiang 	/* barrier and flush checkpoint cp pack 2 page if it can */
152146706d59SGao Xiang 	commit_checkpoint(sbi, ckpt, start_blk);
1522bf22c3ccSSahitya Tummala 	f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
15236a8f8ca5SJaegeuk Kim 
152418767e62SChao Yu 	/*
1525542989b6SEric Biggers 	 * invalidate intermediate page cache borrowed from meta inode which are
1526aff6fbbeSChao Yu 	 * used for migration of encrypted, verity or compressed inode's blocks.
152718767e62SChao Yu 	 */
1528aff6fbbeSChao Yu 	if (f2fs_sb_has_encrypt(sbi) || f2fs_sb_has_verity(sbi) ||
1529aff6fbbeSChao Yu 		f2fs_sb_has_compression(sbi))
153018767e62SChao Yu 		invalidate_mapping_pages(META_MAPPING(sbi),
153118767e62SChao Yu 				MAIN_BLKADDR(sbi), MAX_BLKADDR(sbi) - 1);
153218767e62SChao Yu 
15334d57b86dSChao Yu 	f2fs_release_ino_entry(sbi, false);
1534cf779cabSJaegeuk Kim 
153550fa53ecSChao Yu 	f2fs_reset_fsync_node_info(sbi);
153650fa53ecSChao Yu 
1537caf0047eSChao Yu 	clear_sbi_flag(sbi, SBI_IS_DIRTY);
1538bbf156f7SJaegeuk Kim 	clear_sbi_flag(sbi, SBI_NEED_CP);
1539af033b2aSChao Yu 	clear_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH);
1540c9c8ed50SChao Yu 
1541c9c8ed50SChao Yu 	spin_lock(&sbi->stat_lock);
15424354994fSDaniel Rosenberg 	sbi->unusable_block_count = 0;
1543c9c8ed50SChao Yu 	spin_unlock(&sbi->stat_lock);
1544c9c8ed50SChao Yu 
15458508e44aSJaegeuk Kim 	__set_cp_next_pack(sbi);
1546c34f42e2SChao Yu 
1547c2a080aeSChao Yu 	/*
1548c2a080aeSChao Yu 	 * redirty superblock if metadata like node page or inode cache is
1549c2a080aeSChao Yu 	 * updated during writing checkpoint.
1550c2a080aeSChao Yu 	 */
1551c2a080aeSChao Yu 	if (get_pages(sbi, F2FS_DIRTY_NODES) ||
1552c2a080aeSChao Yu 			get_pages(sbi, F2FS_DIRTY_IMETA))
1553c2a080aeSChao Yu 		set_sbi_flag(sbi, SBI_IS_DIRTY);
1554c2a080aeSChao Yu 
1555c2a080aeSChao Yu 	f2fs_bug_on(sbi, get_pages(sbi, F2FS_DIRTY_DENTS));
1556c2a080aeSChao Yu 
1557af697c0fSJaegeuk Kim 	return unlikely(f2fs_cp_error(sbi)) ? -EIO : 0;
1558127e670aSJaegeuk Kim }
1559127e670aSJaegeuk Kim 
15604d57b86dSChao Yu int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1561127e670aSJaegeuk Kim {
1562127e670aSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1563127e670aSJaegeuk Kim 	unsigned long long ckpt_ver;
1564c34f42e2SChao Yu 	int err = 0;
1565127e670aSJaegeuk Kim 
1566f5a131bbSChao Yu 	if (f2fs_readonly(sbi->sb) || f2fs_hw_is_readonly(sbi))
1567f5a131bbSChao Yu 		return -EROFS;
1568f5a131bbSChao Yu 
15694354994fSDaniel Rosenberg 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
15704354994fSDaniel Rosenberg 		if (cpc->reason != CP_PAUSE)
15714354994fSDaniel Rosenberg 			return 0;
1572dcbb4c10SJoe Perches 		f2fs_warn(sbi, "Start checkpoint disabled!");
15734354994fSDaniel Rosenberg 	}
1574b4b10061SJaegeuk Kim 	if (cpc->reason != CP_RESIZE)
1575127e670aSJaegeuk Kim 		mutex_lock(&sbi->cp_mutex);
15768501017eSJaegeuk Kim 
1577caf0047eSChao Yu 	if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) &&
1578c473f1a9SChao Yu 		((cpc->reason & CP_FASTBOOT) || (cpc->reason & CP_SYNC) ||
1579c473f1a9SChao Yu 		((cpc->reason & CP_DISCARD) && !sbi->discard_blks)))
15808501017eSJaegeuk Kim 		goto out;
1581c34f42e2SChao Yu 	if (unlikely(f2fs_cp_error(sbi))) {
1582c34f42e2SChao Yu 		err = -EIO;
1583cf779cabSJaegeuk Kim 		goto out;
1584c34f42e2SChao Yu 	}
15852bda542dSWanpeng Li 
15862bda542dSWanpeng Li 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops");
15872bda542dSWanpeng Li 
1588c34f42e2SChao Yu 	err = block_operations(sbi);
1589c34f42e2SChao Yu 	if (err)
1590cf779cabSJaegeuk Kim 		goto out;
1591127e670aSJaegeuk Kim 
159275ab4cb8SJaegeuk Kim 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops");
15932af4bd6cSNamjae Jeon 
1594b9109b0eSJaegeuk Kim 	f2fs_flush_merged_writes(sbi);
1595127e670aSJaegeuk Kim 
159658cce381SYunlei He 	/* this is the case of multiple fstrims without any changes */
1597c473f1a9SChao Yu 	if (cpc->reason & CP_DISCARD) {
15984d57b86dSChao Yu 		if (!f2fs_exist_trim_candidates(sbi, cpc)) {
159925290fa5SJaegeuk Kim 			unblock_operations(sbi);
160025290fa5SJaegeuk Kim 			goto out;
160125290fa5SJaegeuk Kim 		}
160225290fa5SJaegeuk Kim 
16030333ad4eSJaegeuk Kim 		if (NM_I(sbi)->dirty_nat_cnt == 0 &&
16040333ad4eSJaegeuk Kim 				SIT_I(sbi)->dirty_sentries == 0 &&
16050333ad4eSJaegeuk Kim 				prefree_segments(sbi) == 0) {
16064d57b86dSChao Yu 			f2fs_flush_sit_entries(sbi, cpc);
16074d57b86dSChao Yu 			f2fs_clear_prefree_segments(sbi, cpc);
160858cce381SYunlei He 			unblock_operations(sbi);
160958cce381SYunlei He 			goto out;
161058cce381SYunlei He 		}
16110333ad4eSJaegeuk Kim 	}
161258cce381SYunlei He 
1613127e670aSJaegeuk Kim 	/*
1614127e670aSJaegeuk Kim 	 * update checkpoint pack index
1615127e670aSJaegeuk Kim 	 * Increase the version number so that
1616127e670aSJaegeuk Kim 	 * SIT entries and seg summaries are written at correct place
1617127e670aSJaegeuk Kim 	 */
1618d71b5564SJaegeuk Kim 	ckpt_ver = cur_cp_version(ckpt);
1619127e670aSJaegeuk Kim 	ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
1620127e670aSJaegeuk Kim 
1621127e670aSJaegeuk Kim 	/* write cached NAT/SIT entries to NAT/SIT area */
1622edc55aafSJaegeuk Kim 	err = f2fs_flush_nat_entries(sbi, cpc);
1623edc55aafSJaegeuk Kim 	if (err)
1624edc55aafSJaegeuk Kim 		goto stop;
1625edc55aafSJaegeuk Kim 
16264d57b86dSChao Yu 	f2fs_flush_sit_entries(sbi, cpc);
1627127e670aSJaegeuk Kim 
1628d0b9e42aSChao Yu 	/* save inmem log status */
1629093749e2SChao Yu 	f2fs_save_inmem_curseg(sbi);
1630d0b9e42aSChao Yu 
1631c34f42e2SChao Yu 	err = do_checkpoint(sbi, cpc);
16324e6a8d9bSJaegeuk Kim 	if (err)
16334d57b86dSChao Yu 		f2fs_release_discard_addrs(sbi);
16344e6a8d9bSJaegeuk Kim 	else
16354d57b86dSChao Yu 		f2fs_clear_prefree_segments(sbi, cpc);
1636d0b9e42aSChao Yu 
1637093749e2SChao Yu 	f2fs_restore_inmem_curseg(sbi);
1638edc55aafSJaegeuk Kim stop:
1639127e670aSJaegeuk Kim 	unblock_operations(sbi);
1640942e0be6SChangman Lee 	stat_inc_cp_count(sbi->stat_info);
164110027551SJaegeuk Kim 
1642c473f1a9SChao Yu 	if (cpc->reason & CP_RECOVERY)
1643dcbb4c10SJoe Perches 		f2fs_notice(sbi, "checkpoint: version = %llx", ckpt_ver);
164460b99b48SJaegeuk Kim 
16457a88ddb5SChao Yu 	/* update CP_TIME to trigger checkpoint periodically */
16466beceb54SJaegeuk Kim 	f2fs_update_time(sbi, CP_TIME);
164755d1cdb2SJaegeuk Kim 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint");
16488501017eSJaegeuk Kim out:
1649b4b10061SJaegeuk Kim 	if (cpc->reason != CP_RESIZE)
16508501017eSJaegeuk Kim 		mutex_unlock(&sbi->cp_mutex);
1651c34f42e2SChao Yu 	return err;
1652127e670aSJaegeuk Kim }
1653127e670aSJaegeuk Kim 
16544d57b86dSChao Yu void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi)
1655127e670aSJaegeuk Kim {
16566451e041SJaegeuk Kim 	int i;
16576451e041SJaegeuk Kim 
16586451e041SJaegeuk Kim 	for (i = 0; i < MAX_INO_ENTRY; i++) {
165967298804SChao Yu 		struct inode_management *im = &sbi->im[i];
166067298804SChao Yu 
166167298804SChao Yu 		INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC);
166267298804SChao Yu 		spin_lock_init(&im->ino_lock);
166367298804SChao Yu 		INIT_LIST_HEAD(&im->ino_list);
166467298804SChao Yu 		im->ino_num = 0;
16656451e041SJaegeuk Kim 	}
16666451e041SJaegeuk Kim 
1667b5b82205SChao Yu 	sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS -
1668d0b9e42aSChao Yu 			NR_CURSEG_PERSIST_TYPE - __cp_payload(sbi)) *
166914b42817SWanpeng Li 				F2FS_ORPHANS_PER_BLOCK;
1670127e670aSJaegeuk Kim }
1671127e670aSJaegeuk Kim 
16724d57b86dSChao Yu int __init f2fs_create_checkpoint_caches(void)
1673127e670aSJaegeuk Kim {
16746451e041SJaegeuk Kim 	ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry",
16756451e041SJaegeuk Kim 			sizeof(struct ino_entry));
16766451e041SJaegeuk Kim 	if (!ino_entry_slab)
1677127e670aSJaegeuk Kim 		return -ENOMEM;
16784d57b86dSChao Yu 	f2fs_inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry",
167906292073SChao Yu 			sizeof(struct inode_entry));
16804d57b86dSChao Yu 	if (!f2fs_inode_entry_slab) {
16816451e041SJaegeuk Kim 		kmem_cache_destroy(ino_entry_slab);
1682127e670aSJaegeuk Kim 		return -ENOMEM;
1683127e670aSJaegeuk Kim 	}
1684127e670aSJaegeuk Kim 	return 0;
1685127e670aSJaegeuk Kim }
1686127e670aSJaegeuk Kim 
16874d57b86dSChao Yu void f2fs_destroy_checkpoint_caches(void)
1688127e670aSJaegeuk Kim {
16896451e041SJaegeuk Kim 	kmem_cache_destroy(ino_entry_slab);
16904d57b86dSChao Yu 	kmem_cache_destroy(f2fs_inode_entry_slab);
1691127e670aSJaegeuk Kim }
1692