xref: /openbmc/linux/fs/f2fs/checkpoint.c (revision c45d6002ff7a322022560e9b19ad867b01fec77f)
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 
530a8165d7SJaegeuk Kim /*
54127e670aSJaegeuk Kim  * We guarantee no failure on the returned page.
55127e670aSJaegeuk Kim  */
562b947003SChao Yu static struct page *__get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index,
572b947003SChao Yu 							bool is_meta)
58127e670aSJaegeuk Kim {
599df27d98SGu Zheng 	struct address_space *mapping = META_MAPPING(sbi);
60127e670aSJaegeuk Kim 	struct page *page;
61cf04e8ebSJaegeuk Kim 	struct f2fs_io_info fio = {
6205ca3632SJaegeuk Kim 		.sbi = sbi,
63cf04e8ebSJaegeuk Kim 		.type = META,
6404d328deSMike Christie 		.op = REQ_OP_READ,
6570fd7614SChristoph Hellwig 		.op_flags = REQ_META | REQ_PRIO,
667a9d7548SChao Yu 		.old_blkaddr = index,
677a9d7548SChao Yu 		.new_blkaddr = index,
684375a336SJaegeuk Kim 		.encrypted_page = NULL,
696dc3a126SChao Yu 		.is_por = !is_meta,
70cf04e8ebSJaegeuk Kim 	};
717735730dSChao Yu 	int err;
722b947003SChao Yu 
732b947003SChao Yu 	if (unlikely(!is_meta))
7404d328deSMike Christie 		fio.op_flags &= ~REQ_META;
75127e670aSJaegeuk Kim repeat:
76300e129cSJaegeuk Kim 	page = f2fs_grab_cache_page(mapping, index, false);
77127e670aSJaegeuk Kim 	if (!page) {
78127e670aSJaegeuk Kim 		cond_resched();
79127e670aSJaegeuk Kim 		goto repeat;
80127e670aSJaegeuk Kim 	}
81393ff91fSJaegeuk Kim 	if (PageUptodate(page))
82393ff91fSJaegeuk Kim 		goto out;
83127e670aSJaegeuk Kim 
8405ca3632SJaegeuk Kim 	fio.page = page;
8505ca3632SJaegeuk Kim 
867735730dSChao Yu 	err = f2fs_submit_page_bio(&fio);
877735730dSChao Yu 	if (err) {
887735730dSChao Yu 		f2fs_put_page(page, 1);
897735730dSChao Yu 		return ERR_PTR(err);
9086531d6bSJaegeuk Kim 	}
91393ff91fSJaegeuk Kim 
92393ff91fSJaegeuk Kim 	lock_page(page);
936bacf52fSJaegeuk Kim 	if (unlikely(page->mapping != mapping)) {
94afcb7ca0SJaegeuk Kim 		f2fs_put_page(page, 1);
95afcb7ca0SJaegeuk Kim 		goto repeat;
96afcb7ca0SJaegeuk Kim 	}
97f3f338caSChao Yu 
9881114baaSChao Yu 	if (unlikely(!PageUptodate(page))) {
997735730dSChao Yu 		f2fs_put_page(page, 1);
1007735730dSChao Yu 		return ERR_PTR(-EIO);
10181114baaSChao Yu 	}
102393ff91fSJaegeuk Kim out:
103127e670aSJaegeuk Kim 	return page;
104127e670aSJaegeuk Kim }
105127e670aSJaegeuk Kim 
1064d57b86dSChao Yu struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
1072b947003SChao Yu {
1082b947003SChao Yu 	return __get_meta_page(sbi, index, true);
1092b947003SChao Yu }
1102b947003SChao Yu 
1117735730dSChao Yu struct page *f2fs_get_meta_page_nofail(struct f2fs_sb_info *sbi, pgoff_t index)
1127735730dSChao Yu {
1137735730dSChao Yu 	struct page *page;
1147735730dSChao Yu 	int count = 0;
1157735730dSChao Yu 
1167735730dSChao Yu retry:
1177735730dSChao Yu 	page = __get_meta_page(sbi, index, true);
1187735730dSChao Yu 	if (IS_ERR(page)) {
1197735730dSChao Yu 		if (PTR_ERR(page) == -EIO &&
1207735730dSChao Yu 				++count <= DEFAULT_RETRY_IO_COUNT)
1217735730dSChao Yu 			goto retry;
1227735730dSChao Yu 		f2fs_stop_checkpoint(sbi, false);
1237735730dSChao Yu 	}
1247735730dSChao Yu 	return page;
1257735730dSChao Yu }
1267735730dSChao Yu 
1272b947003SChao Yu /* for POR only */
1284d57b86dSChao Yu struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index)
1292b947003SChao Yu {
1302b947003SChao Yu 	return __get_meta_page(sbi, index, false);
1312b947003SChao Yu }
1322b947003SChao Yu 
13393770ab7SChao Yu static bool __is_bitmap_valid(struct f2fs_sb_info *sbi, block_t blkaddr,
13493770ab7SChao Yu 							int type)
13593770ab7SChao Yu {
13693770ab7SChao Yu 	struct seg_entry *se;
13793770ab7SChao Yu 	unsigned int segno, offset;
13893770ab7SChao Yu 	bool exist;
13993770ab7SChao Yu 
14093770ab7SChao Yu 	if (type != DATA_GENERIC_ENHANCE && type != DATA_GENERIC_ENHANCE_READ)
14193770ab7SChao Yu 		return true;
14293770ab7SChao Yu 
14393770ab7SChao Yu 	segno = GET_SEGNO(sbi, blkaddr);
14493770ab7SChao Yu 	offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
14593770ab7SChao Yu 	se = get_seg_entry(sbi, segno);
14693770ab7SChao Yu 
14793770ab7SChao Yu 	exist = f2fs_test_bit(offset, se->cur_valid_map);
14893770ab7SChao Yu 	if (!exist && type == DATA_GENERIC_ENHANCE) {
149dcbb4c10SJoe Perches 		f2fs_err(sbi, "Inconsistent error blkaddr:%u, sit bitmap:%d",
150dcbb4c10SJoe Perches 			 blkaddr, exist);
15193770ab7SChao Yu 		set_sbi_flag(sbi, SBI_NEED_FSCK);
15293770ab7SChao Yu 		WARN_ON(1);
15393770ab7SChao Yu 	}
15493770ab7SChao Yu 	return exist;
15593770ab7SChao Yu }
15693770ab7SChao Yu 
157e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
1584d57b86dSChao Yu 					block_t blkaddr, int type)
159662befdaSChao Yu {
160662befdaSChao Yu 	switch (type) {
161662befdaSChao Yu 	case META_NAT:
16266b00c18SChao Yu 		break;
163662befdaSChao Yu 	case META_SIT:
16466b00c18SChao Yu 		if (unlikely(blkaddr >= SIT_BLK_CNT(sbi)))
16566b00c18SChao Yu 			return false;
16666b00c18SChao Yu 		break;
16781c1a0f1SChao Yu 	case META_SSA:
16866b00c18SChao Yu 		if (unlikely(blkaddr >= MAIN_BLKADDR(sbi) ||
16966b00c18SChao Yu 			blkaddr < SM_I(sbi)->ssa_blkaddr))
17066b00c18SChao Yu 			return false;
17166b00c18SChao Yu 		break;
172662befdaSChao Yu 	case META_CP:
17366b00c18SChao Yu 		if (unlikely(blkaddr >= SIT_I(sbi)->sit_base_addr ||
17466b00c18SChao Yu 			blkaddr < __start_cp_addr(sbi)))
17566b00c18SChao Yu 			return false;
17666b00c18SChao Yu 		break;
1774c521f49SJaegeuk Kim 	case META_POR:
17893770ab7SChao Yu 		if (unlikely(blkaddr >= MAX_BLKADDR(sbi) ||
17993770ab7SChao Yu 			blkaddr < MAIN_BLKADDR(sbi)))
18093770ab7SChao Yu 			return false;
18193770ab7SChao Yu 		break;
182e1da7872SChao Yu 	case DATA_GENERIC:
18393770ab7SChao Yu 	case DATA_GENERIC_ENHANCE:
18493770ab7SChao Yu 	case DATA_GENERIC_ENHANCE_READ:
18566b00c18SChao Yu 		if (unlikely(blkaddr >= MAX_BLKADDR(sbi) ||
186c9b60788SChao Yu 				blkaddr < MAIN_BLKADDR(sbi))) {
187dcbb4c10SJoe Perches 			f2fs_warn(sbi, "access invalid blkaddr:%u",
188dcbb4c10SJoe Perches 				  blkaddr);
18993770ab7SChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);
190c9b60788SChao Yu 			WARN_ON(1);
19166b00c18SChao Yu 			return false;
19293770ab7SChao Yu 		} else {
19393770ab7SChao Yu 			return __is_bitmap_valid(sbi, blkaddr, type);
194c9b60788SChao Yu 		}
19566b00c18SChao Yu 		break;
196e1da7872SChao Yu 	case META_GENERIC:
197e1da7872SChao Yu 		if (unlikely(blkaddr < SEG0_BLKADDR(sbi) ||
198e1da7872SChao Yu 			blkaddr >= MAIN_BLKADDR(sbi)))
199e1da7872SChao Yu 			return false;
200e1da7872SChao Yu 		break;
201662befdaSChao Yu 	default:
202662befdaSChao Yu 		BUG();
203662befdaSChao Yu 	}
20466b00c18SChao Yu 
20566b00c18SChao Yu 	return true;
206662befdaSChao Yu }
207662befdaSChao Yu 
208662befdaSChao Yu /*
20981c1a0f1SChao Yu  * Readahead CP/NAT/SIT/SSA pages
210662befdaSChao Yu  */
2114d57b86dSChao Yu int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
21226879fb1SChao Yu 							int type, bool sync)
213662befdaSChao Yu {
214662befdaSChao Yu 	struct page *page;
2154c521f49SJaegeuk Kim 	block_t blkno = start;
216662befdaSChao Yu 	struct f2fs_io_info fio = {
21705ca3632SJaegeuk Kim 		.sbi = sbi,
218662befdaSChao Yu 		.type = META,
21904d328deSMike Christie 		.op = REQ_OP_READ,
22070fd7614SChristoph Hellwig 		.op_flags = sync ? (REQ_META | REQ_PRIO) : REQ_RAHEAD,
2214375a336SJaegeuk Kim 		.encrypted_page = NULL,
222fb830fc5SChao Yu 		.in_list = false,
2236dc3a126SChao Yu 		.is_por = (type == META_POR),
224662befdaSChao Yu 	};
225e9f5b8b8SChao Yu 	struct blk_plug plug;
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:
246662befdaSChao Yu 			/* get sit block addr */
2477a9d7548SChao Yu 			fio.new_blkaddr = current_sit_addr(sbi,
248662befdaSChao Yu 					blkno * SIT_ENTRY_PER_BLOCK);
249662befdaSChao Yu 			break;
25081c1a0f1SChao Yu 		case META_SSA:
251662befdaSChao Yu 		case META_CP:
2524c521f49SJaegeuk Kim 		case META_POR:
2537a9d7548SChao Yu 			fio.new_blkaddr = blkno;
254662befdaSChao Yu 			break;
255662befdaSChao Yu 		default:
256662befdaSChao Yu 			BUG();
257662befdaSChao Yu 		}
258662befdaSChao Yu 
259300e129cSJaegeuk Kim 		page = f2fs_grab_cache_page(META_MAPPING(sbi),
260300e129cSJaegeuk Kim 						fio.new_blkaddr, false);
261662befdaSChao Yu 		if (!page)
262662befdaSChao Yu 			continue;
263662befdaSChao Yu 		if (PageUptodate(page)) {
264662befdaSChao Yu 			f2fs_put_page(page, 1);
265662befdaSChao Yu 			continue;
266662befdaSChao Yu 		}
267662befdaSChao Yu 
26805ca3632SJaegeuk Kim 		fio.page = page;
2691919ffc0SJaegeuk Kim 		f2fs_submit_page_bio(&fio);
270662befdaSChao Yu 		f2fs_put_page(page, 0);
271662befdaSChao Yu 	}
272662befdaSChao Yu out:
273e9f5b8b8SChao Yu 	blk_finish_plug(&plug);
274662befdaSChao Yu 	return blkno - start;
275662befdaSChao Yu }
276662befdaSChao Yu 
2774d57b86dSChao Yu void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index)
278635aee1fSChao Yu {
279635aee1fSChao Yu 	struct page *page;
280635aee1fSChao Yu 	bool readahead = false;
281635aee1fSChao Yu 
282635aee1fSChao Yu 	page = find_get_page(META_MAPPING(sbi), index);
2834da7bf5aSJaegeuk Kim 	if (!page || !PageUptodate(page))
284635aee1fSChao Yu 		readahead = true;
285635aee1fSChao Yu 	f2fs_put_page(page, 0);
286635aee1fSChao Yu 
287635aee1fSChao Yu 	if (readahead)
2884d57b86dSChao Yu 		f2fs_ra_meta_pages(sbi, index, BIO_MAX_PAGES, META_POR, true);
289635aee1fSChao Yu }
290635aee1fSChao Yu 
291b0af6d49SChao Yu static int __f2fs_write_meta_page(struct page *page,
292b0af6d49SChao Yu 				struct writeback_control *wbc,
293b0af6d49SChao Yu 				enum iostat_type io_type)
294127e670aSJaegeuk Kim {
2954081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_P_SB(page);
296577e3495SJaegeuk Kim 
297ecda0de3SChao Yu 	trace_f2fs_writepage(page, META);
298ecda0de3SChao Yu 
299af697c0fSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
300af697c0fSJaegeuk Kim 		goto redirty_out;
301caf0047eSChao Yu 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
302cfb271d4SChao Yu 		goto redirty_out;
303857dc4e0SJaegeuk Kim 	if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0))
304cfb271d4SChao Yu 		goto redirty_out;
305127e670aSJaegeuk Kim 
3064d57b86dSChao Yu 	f2fs_do_write_meta_page(sbi, page, io_type);
307127e670aSJaegeuk Kim 	dec_page_count(sbi, F2FS_DIRTY_META);
3080c3a5797SChao Yu 
3090c3a5797SChao Yu 	if (wbc->for_reclaim)
310bab475c5SChao Yu 		f2fs_submit_merged_write_cond(sbi, NULL, page, 0, META);
3110c3a5797SChao Yu 
312127e670aSJaegeuk Kim 	unlock_page(page);
313857dc4e0SJaegeuk Kim 
3140c3a5797SChao Yu 	if (unlikely(f2fs_cp_error(sbi)))
315b9109b0eSJaegeuk Kim 		f2fs_submit_merged_write(sbi, META);
3160c3a5797SChao Yu 
317577e3495SJaegeuk Kim 	return 0;
318cfb271d4SChao Yu 
319cfb271d4SChao Yu redirty_out:
32076f60268SJaegeuk Kim 	redirty_page_for_writepage(wbc, page);
321cfb271d4SChao Yu 	return AOP_WRITEPAGE_ACTIVATE;
322127e670aSJaegeuk Kim }
323127e670aSJaegeuk Kim 
324b0af6d49SChao Yu static int f2fs_write_meta_page(struct page *page,
325b0af6d49SChao Yu 				struct writeback_control *wbc)
326b0af6d49SChao Yu {
327b0af6d49SChao Yu 	return __f2fs_write_meta_page(page, wbc, FS_META_IO);
328b0af6d49SChao Yu }
329b0af6d49SChao Yu 
330127e670aSJaegeuk Kim static int f2fs_write_meta_pages(struct address_space *mapping,
331127e670aSJaegeuk Kim 				struct writeback_control *wbc)
332127e670aSJaegeuk Kim {
3334081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
33450c8cdb3SJaegeuk Kim 	long diff, written;
335127e670aSJaegeuk Kim 
3360771fcc7SChao Yu 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
3370771fcc7SChao Yu 		goto skip_write;
3380771fcc7SChao Yu 
3395459aa97SJaegeuk Kim 	/* collect a number of dirty meta pages and write together */
340812a9597SJaegeuk Kim 	if (wbc->sync_mode != WB_SYNC_ALL &&
341812a9597SJaegeuk Kim 			get_pages(sbi, F2FS_DIRTY_META) <
342812a9597SJaegeuk Kim 					nr_pages_to_skip(sbi, META))
343d3baf95dSJaegeuk Kim 		goto skip_write;
344127e670aSJaegeuk Kim 
345a29d0e0bSYunlei He 	/* if locked failed, cp will flush dirty pages instead */
346a29d0e0bSYunlei He 	if (!mutex_trylock(&sbi->cp_mutex))
347a29d0e0bSYunlei He 		goto skip_write;
348d31c7c3fSYunlei He 
349a29d0e0bSYunlei He 	trace_f2fs_writepages(mapping->host, wbc, META);
35050c8cdb3SJaegeuk Kim 	diff = nr_pages_to_write(sbi, META, wbc);
3514d57b86dSChao Yu 	written = f2fs_sync_meta_pages(sbi, META, wbc->nr_to_write, FS_META_IO);
352127e670aSJaegeuk Kim 	mutex_unlock(&sbi->cp_mutex);
35350c8cdb3SJaegeuk Kim 	wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff);
354127e670aSJaegeuk Kim 	return 0;
355d3baf95dSJaegeuk Kim 
356d3baf95dSJaegeuk Kim skip_write:
357d3baf95dSJaegeuk Kim 	wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META);
358d31c7c3fSYunlei He 	trace_f2fs_writepages(mapping->host, wbc, META);
359d3baf95dSJaegeuk Kim 	return 0;
360127e670aSJaegeuk Kim }
361127e670aSJaegeuk Kim 
3624d57b86dSChao Yu long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
363b0af6d49SChao Yu 				long nr_to_write, enum iostat_type io_type)
364127e670aSJaegeuk Kim {
3659df27d98SGu Zheng 	struct address_space *mapping = META_MAPPING(sbi);
366028a63a6SJan Kara 	pgoff_t index = 0, prev = ULONG_MAX;
367127e670aSJaegeuk Kim 	struct pagevec pvec;
368127e670aSJaegeuk Kim 	long nwritten = 0;
369028a63a6SJan Kara 	int nr_pages;
370127e670aSJaegeuk Kim 	struct writeback_control wbc = {
371127e670aSJaegeuk Kim 		.for_reclaim = 0,
372127e670aSJaegeuk Kim 	};
373e9f5b8b8SChao Yu 	struct blk_plug plug;
374127e670aSJaegeuk Kim 
37586679820SMel Gorman 	pagevec_init(&pvec);
376127e670aSJaegeuk Kim 
377e9f5b8b8SChao Yu 	blk_start_plug(&plug);
378e9f5b8b8SChao Yu 
379028a63a6SJan Kara 	while ((nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
38067fd707fSJan Kara 				PAGECACHE_TAG_DIRTY))) {
381028a63a6SJan Kara 		int i;
382127e670aSJaegeuk Kim 
383127e670aSJaegeuk Kim 		for (i = 0; i < nr_pages; i++) {
384127e670aSJaegeuk Kim 			struct page *page = pvec.pages[i];
385203681f6SJaegeuk Kim 
38680dd9c0eSChao Yu 			if (prev == ULONG_MAX)
3876066d8cdSJaegeuk Kim 				prev = page->index - 1;
3886066d8cdSJaegeuk Kim 			if (nr_to_write != LONG_MAX && page->index != prev + 1) {
3896066d8cdSJaegeuk Kim 				pagevec_release(&pvec);
3906066d8cdSJaegeuk Kim 				goto stop;
3916066d8cdSJaegeuk Kim 			}
3926066d8cdSJaegeuk Kim 
393127e670aSJaegeuk Kim 			lock_page(page);
394203681f6SJaegeuk Kim 
395203681f6SJaegeuk Kim 			if (unlikely(page->mapping != mapping)) {
396203681f6SJaegeuk Kim continue_unlock:
397203681f6SJaegeuk Kim 				unlock_page(page);
398203681f6SJaegeuk Kim 				continue;
399203681f6SJaegeuk Kim 			}
400203681f6SJaegeuk Kim 			if (!PageDirty(page)) {
401203681f6SJaegeuk Kim 				/* someone wrote it for us */
402203681f6SJaegeuk Kim 				goto continue_unlock;
403203681f6SJaegeuk Kim 			}
404203681f6SJaegeuk Kim 
405bae0ee7aSChao Yu 			f2fs_wait_on_page_writeback(page, META, true, true);
406fa3d2bdfSJaegeuk Kim 
407203681f6SJaegeuk Kim 			if (!clear_page_dirty_for_io(page))
408203681f6SJaegeuk Kim 				goto continue_unlock;
409203681f6SJaegeuk Kim 
410b0af6d49SChao Yu 			if (__f2fs_write_meta_page(page, &wbc, io_type)) {
411577e3495SJaegeuk Kim 				unlock_page(page);
412577e3495SJaegeuk Kim 				break;
413577e3495SJaegeuk Kim 			}
414cfb271d4SChao Yu 			nwritten++;
4156066d8cdSJaegeuk Kim 			prev = page->index;
416cfb271d4SChao Yu 			if (unlikely(nwritten >= nr_to_write))
417127e670aSJaegeuk Kim 				break;
418127e670aSJaegeuk Kim 		}
419127e670aSJaegeuk Kim 		pagevec_release(&pvec);
420127e670aSJaegeuk Kim 		cond_resched();
421127e670aSJaegeuk Kim 	}
4226066d8cdSJaegeuk Kim stop:
423127e670aSJaegeuk Kim 	if (nwritten)
424b9109b0eSJaegeuk Kim 		f2fs_submit_merged_write(sbi, type);
425127e670aSJaegeuk Kim 
426e9f5b8b8SChao Yu 	blk_finish_plug(&plug);
427e9f5b8b8SChao Yu 
428127e670aSJaegeuk Kim 	return nwritten;
429127e670aSJaegeuk Kim }
430127e670aSJaegeuk Kim 
431127e670aSJaegeuk Kim static int f2fs_set_meta_page_dirty(struct page *page)
432127e670aSJaegeuk Kim {
43326c6b887SJaegeuk Kim 	trace_f2fs_set_page_dirty(page, META);
43426c6b887SJaegeuk Kim 
435237c0790SJaegeuk Kim 	if (!PageUptodate(page))
436127e670aSJaegeuk Kim 		SetPageUptodate(page);
437127e670aSJaegeuk Kim 	if (!PageDirty(page)) {
438b87078adSJaegeuk Kim 		__set_page_dirty_nobuffers(page);
4394081363fSJaegeuk Kim 		inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_META);
440240a5915SChao Yu 		f2fs_set_page_private(page, 0);
4419e4ded3fSJaegeuk Kim 		f2fs_trace_pid(page);
442127e670aSJaegeuk Kim 		return 1;
443127e670aSJaegeuk Kim 	}
444127e670aSJaegeuk Kim 	return 0;
445127e670aSJaegeuk Kim }
446127e670aSJaegeuk Kim 
447127e670aSJaegeuk Kim const struct address_space_operations f2fs_meta_aops = {
448127e670aSJaegeuk Kim 	.writepage	= f2fs_write_meta_page,
449127e670aSJaegeuk Kim 	.writepages	= f2fs_write_meta_pages,
450127e670aSJaegeuk Kim 	.set_page_dirty	= f2fs_set_meta_page_dirty,
451487261f3SChao Yu 	.invalidatepage = f2fs_invalidate_page,
452487261f3SChao Yu 	.releasepage	= f2fs_release_page,
4535b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION
4545b7a487cSWeichao Guo 	.migratepage    = f2fs_migrate_page,
4555b7a487cSWeichao Guo #endif
456127e670aSJaegeuk Kim };
457127e670aSJaegeuk Kim 
45839d787beSChao Yu static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino,
45939d787beSChao Yu 						unsigned int devidx, int type)
460953e6cc6SJaegeuk Kim {
46167298804SChao Yu 	struct inode_management *im = &sbi->im[type];
46280c54505SJaegeuk Kim 	struct ino_entry *e, *tmp;
46380c54505SJaegeuk Kim 
46480c54505SJaegeuk Kim 	tmp = f2fs_kmem_cache_alloc(ino_entry_slab, GFP_NOFS);
46519526d74SChao Yu 
46680c54505SJaegeuk Kim 	radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
467769ec6e5SJaegeuk Kim 
46867298804SChao Yu 	spin_lock(&im->ino_lock);
46967298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
47039efac41SJaegeuk Kim 	if (!e) {
47180c54505SJaegeuk Kim 		e = tmp;
47219526d74SChao Yu 		if (unlikely(radix_tree_insert(&im->ino_root, ino, e)))
47319526d74SChao Yu 			f2fs_bug_on(sbi, 1);
47419526d74SChao Yu 
47539efac41SJaegeuk Kim 		memset(e, 0, sizeof(struct ino_entry));
47639efac41SJaegeuk Kim 		e->ino = ino;
47739efac41SJaegeuk Kim 
47867298804SChao Yu 		list_add_tail(&e->list, &im->ino_list);
4798c402946SJaegeuk Kim 		if (type != ORPHAN_INO)
48067298804SChao Yu 			im->ino_num++;
48139efac41SJaegeuk Kim 	}
48239d787beSChao Yu 
48339d787beSChao Yu 	if (type == FLUSH_INO)
48439d787beSChao Yu 		f2fs_set_bit(devidx, (char *)&e->dirty_device);
48539d787beSChao Yu 
48667298804SChao Yu 	spin_unlock(&im->ino_lock);
487769ec6e5SJaegeuk Kim 	radix_tree_preload_end();
48880c54505SJaegeuk Kim 
48980c54505SJaegeuk Kim 	if (e != tmp)
49080c54505SJaegeuk Kim 		kmem_cache_free(ino_entry_slab, tmp);
491953e6cc6SJaegeuk Kim }
492953e6cc6SJaegeuk Kim 
4936451e041SJaegeuk Kim static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
494953e6cc6SJaegeuk Kim {
49567298804SChao Yu 	struct inode_management *im = &sbi->im[type];
4966451e041SJaegeuk Kim 	struct ino_entry *e;
497953e6cc6SJaegeuk Kim 
49867298804SChao Yu 	spin_lock(&im->ino_lock);
49967298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
50039efac41SJaegeuk Kim 	if (e) {
501953e6cc6SJaegeuk Kim 		list_del(&e->list);
50267298804SChao Yu 		radix_tree_delete(&im->ino_root, ino);
50367298804SChao Yu 		im->ino_num--;
50467298804SChao Yu 		spin_unlock(&im->ino_lock);
5056451e041SJaegeuk Kim 		kmem_cache_free(ino_entry_slab, e);
506953e6cc6SJaegeuk Kim 		return;
507953e6cc6SJaegeuk Kim 	}
50867298804SChao Yu 	spin_unlock(&im->ino_lock);
509953e6cc6SJaegeuk Kim }
510953e6cc6SJaegeuk Kim 
5114d57b86dSChao Yu void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
512fff04f90SJaegeuk Kim {
513fff04f90SJaegeuk Kim 	/* add new dirty ino entry into list */
51439d787beSChao Yu 	__add_ino_entry(sbi, ino, 0, type);
515fff04f90SJaegeuk Kim }
516fff04f90SJaegeuk Kim 
5174d57b86dSChao Yu void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
518fff04f90SJaegeuk Kim {
519fff04f90SJaegeuk Kim 	/* remove dirty ino entry from list */
520fff04f90SJaegeuk Kim 	__remove_ino_entry(sbi, ino, type);
521fff04f90SJaegeuk Kim }
522fff04f90SJaegeuk Kim 
523fff04f90SJaegeuk Kim /* mode should be APPEND_INO or UPDATE_INO */
5244d57b86dSChao Yu bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode)
525fff04f90SJaegeuk Kim {
52667298804SChao Yu 	struct inode_management *im = &sbi->im[mode];
527fff04f90SJaegeuk Kim 	struct ino_entry *e;
52867298804SChao Yu 
52967298804SChao Yu 	spin_lock(&im->ino_lock);
53067298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
53167298804SChao Yu 	spin_unlock(&im->ino_lock);
532fff04f90SJaegeuk Kim 	return e ? true : false;
533fff04f90SJaegeuk Kim }
534fff04f90SJaegeuk Kim 
5354d57b86dSChao Yu void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all)
536fff04f90SJaegeuk Kim {
537fff04f90SJaegeuk Kim 	struct ino_entry *e, *tmp;
538fff04f90SJaegeuk Kim 	int i;
539fff04f90SJaegeuk Kim 
54039d787beSChao Yu 	for (i = all ? ORPHAN_INO : APPEND_INO; i < MAX_INO_ENTRY; i++) {
54167298804SChao Yu 		struct inode_management *im = &sbi->im[i];
54267298804SChao Yu 
54367298804SChao Yu 		spin_lock(&im->ino_lock);
54467298804SChao Yu 		list_for_each_entry_safe(e, tmp, &im->ino_list, list) {
545fff04f90SJaegeuk Kim 			list_del(&e->list);
54667298804SChao Yu 			radix_tree_delete(&im->ino_root, e->ino);
547fff04f90SJaegeuk Kim 			kmem_cache_free(ino_entry_slab, e);
54867298804SChao Yu 			im->ino_num--;
549fff04f90SJaegeuk Kim 		}
55067298804SChao Yu 		spin_unlock(&im->ino_lock);
551fff04f90SJaegeuk Kim 	}
552fff04f90SJaegeuk Kim }
553fff04f90SJaegeuk Kim 
5544d57b86dSChao Yu void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
55539d787beSChao Yu 					unsigned int devidx, int type)
55639d787beSChao Yu {
55739d787beSChao Yu 	__add_ino_entry(sbi, ino, devidx, type);
55839d787beSChao Yu }
55939d787beSChao Yu 
5604d57b86dSChao Yu bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
56139d787beSChao Yu 					unsigned int devidx, int type)
56239d787beSChao Yu {
56339d787beSChao Yu 	struct inode_management *im = &sbi->im[type];
56439d787beSChao Yu 	struct ino_entry *e;
56539d787beSChao Yu 	bool is_dirty = false;
56639d787beSChao Yu 
56739d787beSChao Yu 	spin_lock(&im->ino_lock);
56839d787beSChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
56939d787beSChao Yu 	if (e && f2fs_test_bit(devidx, (char *)&e->dirty_device))
57039d787beSChao Yu 		is_dirty = true;
57139d787beSChao Yu 	spin_unlock(&im->ino_lock);
57239d787beSChao Yu 	return is_dirty;
57339d787beSChao Yu }
57439d787beSChao Yu 
5754d57b86dSChao Yu int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi)
576127e670aSJaegeuk Kim {
57767298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
578127e670aSJaegeuk Kim 	int err = 0;
579127e670aSJaegeuk Kim 
58067298804SChao Yu 	spin_lock(&im->ino_lock);
581cb78942bSJaegeuk Kim 
5821ecc0c5cSChao Yu 	if (time_to_inject(sbi, FAULT_ORPHAN)) {
583cb78942bSJaegeuk Kim 		spin_unlock(&im->ino_lock);
584*c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_ORPHAN);
585cb78942bSJaegeuk Kim 		return -ENOSPC;
586cb78942bSJaegeuk Kim 	}
5877fa750a1SArnd Bergmann 
58867298804SChao Yu 	if (unlikely(im->ino_num >= sbi->max_orphans))
589127e670aSJaegeuk Kim 		err = -ENOSPC;
590cbd56e7dSJaegeuk Kim 	else
59167298804SChao Yu 		im->ino_num++;
59267298804SChao Yu 	spin_unlock(&im->ino_lock);
5930d47c1adSGu Zheng 
594127e670aSJaegeuk Kim 	return err;
595127e670aSJaegeuk Kim }
596127e670aSJaegeuk Kim 
5974d57b86dSChao Yu void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi)
598cbd56e7dSJaegeuk Kim {
59967298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
60067298804SChao Yu 
60167298804SChao Yu 	spin_lock(&im->ino_lock);
60267298804SChao Yu 	f2fs_bug_on(sbi, im->ino_num == 0);
60367298804SChao Yu 	im->ino_num--;
60467298804SChao Yu 	spin_unlock(&im->ino_lock);
605cbd56e7dSJaegeuk Kim }
606cbd56e7dSJaegeuk Kim 
6074d57b86dSChao Yu void f2fs_add_orphan_inode(struct inode *inode)
608127e670aSJaegeuk Kim {
60939efac41SJaegeuk Kim 	/* add new orphan ino entry into list */
61039d787beSChao Yu 	__add_ino_entry(F2FS_I_SB(inode), inode->i_ino, 0, ORPHAN_INO);
6114d57b86dSChao Yu 	f2fs_update_inode_page(inode);
612127e670aSJaegeuk Kim }
613127e670aSJaegeuk Kim 
6144d57b86dSChao Yu void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
615127e670aSJaegeuk Kim {
616953e6cc6SJaegeuk Kim 	/* remove orphan entry from orphan list */
6176451e041SJaegeuk Kim 	__remove_ino_entry(sbi, ino, ORPHAN_INO);
618127e670aSJaegeuk Kim }
619127e670aSJaegeuk Kim 
6208c14bfadSChao Yu static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
621127e670aSJaegeuk Kim {
6228c14bfadSChao Yu 	struct inode *inode;
6235905f9afSJaegeuk Kim 	struct node_info ni;
62476a45e3cSChao Yu 	int err;
6258c14bfadSChao Yu 
6265905f9afSJaegeuk Kim 	inode = f2fs_iget_retry(sbi->sb, ino);
6278c14bfadSChao Yu 	if (IS_ERR(inode)) {
6288c14bfadSChao Yu 		/*
6298c14bfadSChao Yu 		 * there should be a bug that we can't find the entry
6308c14bfadSChao Yu 		 * to orphan inode.
6318c14bfadSChao Yu 		 */
6328c14bfadSChao Yu 		f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT);
6338c14bfadSChao Yu 		return PTR_ERR(inode);
6348c14bfadSChao Yu 	}
6358c14bfadSChao Yu 
6360f9ec2a8SJaegeuk Kim 	err = dquot_initialize(inode);
637a515d12fSSheng Yong 	if (err) {
638a515d12fSSheng Yong 		iput(inode);
6390f9ec2a8SJaegeuk Kim 		goto err_out;
640a515d12fSSheng Yong 	}
6410f9ec2a8SJaegeuk Kim 
642127e670aSJaegeuk Kim 	clear_nlink(inode);
643127e670aSJaegeuk Kim 
644127e670aSJaegeuk Kim 	/* truncate all the data during iput */
645127e670aSJaegeuk Kim 	iput(inode);
6465905f9afSJaegeuk Kim 
6477735730dSChao Yu 	err = f2fs_get_node_info(sbi, ino, &ni);
6487735730dSChao Yu 	if (err)
6497735730dSChao Yu 		goto err_out;
6505905f9afSJaegeuk Kim 
6515905f9afSJaegeuk Kim 	/* ENOMEM was fully retried in f2fs_evict_inode. */
6525905f9afSJaegeuk Kim 	if (ni.blk_addr != NULL_ADDR) {
6530f9ec2a8SJaegeuk Kim 		err = -EIO;
6540f9ec2a8SJaegeuk Kim 		goto err_out;
6555905f9afSJaegeuk Kim 	}
6568c14bfadSChao Yu 	return 0;
6570f9ec2a8SJaegeuk Kim 
6580f9ec2a8SJaegeuk Kim err_out:
6590f9ec2a8SJaegeuk Kim 	set_sbi_flag(sbi, SBI_NEED_FSCK);
660dcbb4c10SJoe Perches 	f2fs_warn(sbi, "%s: orphan failed (ino=%x), run fsck to fix.",
6610f9ec2a8SJaegeuk Kim 		  __func__, ino);
6620f9ec2a8SJaegeuk Kim 	return err;
663127e670aSJaegeuk Kim }
664127e670aSJaegeuk Kim 
6654d57b86dSChao Yu int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
666127e670aSJaegeuk Kim {
6673c642985SWanpeng Li 	block_t start_blk, orphan_blocks, i, j;
6684b2414d0SChao Yu 	unsigned int s_flags = sbi->sb->s_flags;
6694b2414d0SChao Yu 	int err = 0;
670ea676733SJaegeuk Kim #ifdef CONFIG_QUOTA
671ea676733SJaegeuk Kim 	int quota_enabled;
672ea676733SJaegeuk Kim #endif
673127e670aSJaegeuk Kim 
674aaec2b1dSChao Yu 	if (!is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG))
6758c14bfadSChao Yu 		return 0;
676127e670aSJaegeuk Kim 
677b61af314SChao Yu 	if (bdev_read_only(sbi->sb->s_bdev)) {
678dcbb4c10SJoe Perches 		f2fs_info(sbi, "write access unavailable, skipping orphan cleanup");
679b61af314SChao Yu 		return 0;
680b61af314SChao Yu 	}
681b61af314SChao Yu 
6821751e8a6SLinus Torvalds 	if (s_flags & SB_RDONLY) {
683dcbb4c10SJoe Perches 		f2fs_info(sbi, "orphan cleanup on readonly fs");
6841751e8a6SLinus Torvalds 		sbi->sb->s_flags &= ~SB_RDONLY;
6854b2414d0SChao Yu 	}
6864b2414d0SChao Yu 
6874b2414d0SChao Yu #ifdef CONFIG_QUOTA
6884b2414d0SChao Yu 	/* Needed for iput() to work correctly and not trash data */
6891751e8a6SLinus Torvalds 	sbi->sb->s_flags |= SB_ACTIVE;
690ea676733SJaegeuk Kim 
69176cf05d7SSheng Yong 	/*
69276cf05d7SSheng Yong 	 * Turn on quotas which were not enabled for read-only mounts if
69376cf05d7SSheng Yong 	 * filesystem has quota feature, so that they are updated correctly.
69476cf05d7SSheng Yong 	 */
6951751e8a6SLinus Torvalds 	quota_enabled = f2fs_enable_quota_files(sbi, s_flags & SB_RDONLY);
6964b2414d0SChao Yu #endif
6974b2414d0SChao Yu 
69855141486SWanpeng Li 	start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
6993c642985SWanpeng Li 	orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
700127e670aSJaegeuk Kim 
7014d57b86dSChao Yu 	f2fs_ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true);
702662befdaSChao Yu 
7033c642985SWanpeng Li 	for (i = 0; i < orphan_blocks; i++) {
7047735730dSChao Yu 		struct page *page;
705127e670aSJaegeuk Kim 		struct f2fs_orphan_block *orphan_blk;
706127e670aSJaegeuk Kim 
7077735730dSChao Yu 		page = f2fs_get_meta_page(sbi, start_blk + i);
7087735730dSChao Yu 		if (IS_ERR(page)) {
7097735730dSChao Yu 			err = PTR_ERR(page);
7107735730dSChao Yu 			goto out;
7117735730dSChao Yu 		}
7127735730dSChao Yu 
713127e670aSJaegeuk Kim 		orphan_blk = (struct f2fs_orphan_block *)page_address(page);
714127e670aSJaegeuk Kim 		for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
715127e670aSJaegeuk Kim 			nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
7168c14bfadSChao Yu 			err = recover_orphan_inode(sbi, ino);
7178c14bfadSChao Yu 			if (err) {
7188c14bfadSChao Yu 				f2fs_put_page(page, 1);
7194b2414d0SChao Yu 				goto out;
7208c14bfadSChao Yu 			}
721127e670aSJaegeuk Kim 		}
722127e670aSJaegeuk Kim 		f2fs_put_page(page, 1);
723127e670aSJaegeuk Kim 	}
724127e670aSJaegeuk Kim 	/* clear Orphan Flag */
725aaec2b1dSChao Yu 	clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
7264b2414d0SChao Yu out:
7271378752bSChao Yu 	set_sbi_flag(sbi, SBI_IS_RECOVERED);
7281378752bSChao Yu 
7294b2414d0SChao Yu #ifdef CONFIG_QUOTA
7304b2414d0SChao Yu 	/* Turn quotas off */
731ea676733SJaegeuk Kim 	if (quota_enabled)
7324b2414d0SChao Yu 		f2fs_quota_off_umount(sbi->sb);
7334b2414d0SChao Yu #endif
7341751e8a6SLinus Torvalds 	sbi->sb->s_flags = s_flags; /* Restore SB_RDONLY status */
7354b2414d0SChao Yu 
7364b2414d0SChao Yu 	return err;
737127e670aSJaegeuk Kim }
738127e670aSJaegeuk Kim 
739127e670aSJaegeuk Kim static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
740127e670aSJaegeuk Kim {
741502c6e0bSGu Zheng 	struct list_head *head;
742127e670aSJaegeuk Kim 	struct f2fs_orphan_block *orphan_blk = NULL;
743127e670aSJaegeuk Kim 	unsigned int nentries = 0;
744bd936f84SChao Yu 	unsigned short index = 1;
7458c402946SJaegeuk Kim 	unsigned short orphan_blocks;
7464531929eSGu Zheng 	struct page *page = NULL;
7476451e041SJaegeuk Kim 	struct ino_entry *orphan = NULL;
74867298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
749127e670aSJaegeuk Kim 
75067298804SChao Yu 	orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num);
7518c402946SJaegeuk Kim 
752d6c67a4fSJaegeuk Kim 	/*
753d6c67a4fSJaegeuk Kim 	 * we don't need to do spin_lock(&im->ino_lock) here, since all the
754d6c67a4fSJaegeuk Kim 	 * orphan inode operations are covered under f2fs_lock_op().
755d6c67a4fSJaegeuk Kim 	 * And, spin_lock should be avoided due to page operations below.
756d6c67a4fSJaegeuk Kim 	 */
75767298804SChao Yu 	head = &im->ino_list;
758127e670aSJaegeuk Kim 
759127e670aSJaegeuk Kim 	/* loop for each orphan inode entry and write them in Jornal block */
760502c6e0bSGu Zheng 	list_for_each_entry(orphan, head, list) {
761502c6e0bSGu Zheng 		if (!page) {
7624d57b86dSChao Yu 			page = f2fs_grab_meta_page(sbi, start_blk++);
763502c6e0bSGu Zheng 			orphan_blk =
764502c6e0bSGu Zheng 				(struct f2fs_orphan_block *)page_address(page);
765502c6e0bSGu Zheng 			memset(orphan_blk, 0, sizeof(*orphan_blk));
766502c6e0bSGu Zheng 		}
767127e670aSJaegeuk Kim 
76836795567SGu Zheng 		orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
769127e670aSJaegeuk Kim 
77036795567SGu Zheng 		if (nentries == F2FS_ORPHANS_PER_BLOCK) {
771127e670aSJaegeuk Kim 			/*
772127e670aSJaegeuk Kim 			 * an orphan block is full of 1020 entries,
773127e670aSJaegeuk Kim 			 * then we need to flush current orphan blocks
774127e670aSJaegeuk Kim 			 * and bring another one in memory
775127e670aSJaegeuk Kim 			 */
776127e670aSJaegeuk Kim 			orphan_blk->blk_addr = cpu_to_le16(index);
777127e670aSJaegeuk Kim 			orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
778127e670aSJaegeuk Kim 			orphan_blk->entry_count = cpu_to_le32(nentries);
779127e670aSJaegeuk Kim 			set_page_dirty(page);
780127e670aSJaegeuk Kim 			f2fs_put_page(page, 1);
781127e670aSJaegeuk Kim 			index++;
782127e670aSJaegeuk Kim 			nentries = 0;
783127e670aSJaegeuk Kim 			page = NULL;
784127e670aSJaegeuk Kim 		}
785127e670aSJaegeuk Kim 	}
786127e670aSJaegeuk Kim 
787502c6e0bSGu Zheng 	if (page) {
788127e670aSJaegeuk Kim 		orphan_blk->blk_addr = cpu_to_le16(index);
789127e670aSJaegeuk Kim 		orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
790127e670aSJaegeuk Kim 		orphan_blk->entry_count = cpu_to_le32(nentries);
791127e670aSJaegeuk Kim 		set_page_dirty(page);
792127e670aSJaegeuk Kim 		f2fs_put_page(page, 1);
793502c6e0bSGu Zheng 	}
794127e670aSJaegeuk Kim }
795127e670aSJaegeuk Kim 
796d7eb8f1cSChao Yu static __u32 f2fs_checkpoint_chksum(struct f2fs_sb_info *sbi,
797d7eb8f1cSChao Yu 						struct f2fs_checkpoint *ckpt)
798d7eb8f1cSChao Yu {
799d7eb8f1cSChao Yu 	unsigned int chksum_ofs = le32_to_cpu(ckpt->checksum_offset);
800d7eb8f1cSChao Yu 	__u32 chksum;
801d7eb8f1cSChao Yu 
802d7eb8f1cSChao Yu 	chksum = f2fs_crc32(sbi, ckpt, chksum_ofs);
803d7eb8f1cSChao Yu 	if (chksum_ofs < CP_CHKSUM_OFFSET) {
804d7eb8f1cSChao Yu 		chksum_ofs += sizeof(chksum);
805d7eb8f1cSChao Yu 		chksum = f2fs_chksum(sbi, chksum, (__u8 *)ckpt + chksum_ofs,
806d7eb8f1cSChao Yu 						F2FS_BLKSIZE - chksum_ofs);
807d7eb8f1cSChao Yu 	}
808d7eb8f1cSChao Yu 	return chksum;
809d7eb8f1cSChao Yu }
810d7eb8f1cSChao Yu 
811fc0065adSTiezhu Yang static int get_checkpoint_version(struct f2fs_sb_info *sbi, block_t cp_addr,
812fc0065adSTiezhu Yang 		struct f2fs_checkpoint **cp_block, struct page **cp_page,
813fc0065adSTiezhu Yang 		unsigned long long *version)
814fc0065adSTiezhu Yang {
815fc0065adSTiezhu Yang 	size_t crc_offset = 0;
816d7eb8f1cSChao Yu 	__u32 crc;
817fc0065adSTiezhu Yang 
8184d57b86dSChao Yu 	*cp_page = f2fs_get_meta_page(sbi, cp_addr);
8197735730dSChao Yu 	if (IS_ERR(*cp_page))
8207735730dSChao Yu 		return PTR_ERR(*cp_page);
8217735730dSChao Yu 
822fc0065adSTiezhu Yang 	*cp_block = (struct f2fs_checkpoint *)page_address(*cp_page);
823fc0065adSTiezhu Yang 
824fc0065adSTiezhu Yang 	crc_offset = le32_to_cpu((*cp_block)->checksum_offset);
825d7eb8f1cSChao Yu 	if (crc_offset < CP_MIN_CHKSUM_OFFSET ||
826d7eb8f1cSChao Yu 			crc_offset > CP_CHKSUM_OFFSET) {
827d3f07c04SChao Yu 		f2fs_put_page(*cp_page, 1);
828dcbb4c10SJoe Perches 		f2fs_warn(sbi, "invalid crc_offset: %zu", crc_offset);
829fc0065adSTiezhu Yang 		return -EINVAL;
830fc0065adSTiezhu Yang 	}
831fc0065adSTiezhu Yang 
832d7eb8f1cSChao Yu 	crc = f2fs_checkpoint_chksum(sbi, *cp_block);
833d7eb8f1cSChao Yu 	if (crc != cur_cp_crc(*cp_block)) {
834d3f07c04SChao Yu 		f2fs_put_page(*cp_page, 1);
835dcbb4c10SJoe Perches 		f2fs_warn(sbi, "invalid crc value");
836fc0065adSTiezhu Yang 		return -EINVAL;
837fc0065adSTiezhu Yang 	}
838fc0065adSTiezhu Yang 
839fc0065adSTiezhu Yang 	*version = cur_cp_version(*cp_block);
840fc0065adSTiezhu Yang 	return 0;
841fc0065adSTiezhu Yang }
842fc0065adSTiezhu Yang 
843127e670aSJaegeuk Kim static struct page *validate_checkpoint(struct f2fs_sb_info *sbi,
844127e670aSJaegeuk Kim 				block_t cp_addr, unsigned long long *version)
845127e670aSJaegeuk Kim {
846fc0065adSTiezhu Yang 	struct page *cp_page_1 = NULL, *cp_page_2 = NULL;
847fc0065adSTiezhu Yang 	struct f2fs_checkpoint *cp_block = NULL;
848127e670aSJaegeuk Kim 	unsigned long long cur_version = 0, pre_version = 0;
849fc0065adSTiezhu Yang 	int err;
850127e670aSJaegeuk Kim 
851fc0065adSTiezhu Yang 	err = get_checkpoint_version(sbi, cp_addr, &cp_block,
852fc0065adSTiezhu Yang 					&cp_page_1, version);
853fc0065adSTiezhu Yang 	if (err)
854d3f07c04SChao Yu 		return NULL;
855c9b60788SChao Yu 
856c9b60788SChao Yu 	if (le32_to_cpu(cp_block->cp_pack_total_block_count) >
857c9b60788SChao Yu 					sbi->blocks_per_seg) {
858dcbb4c10SJoe Perches 		f2fs_warn(sbi, "invalid cp_pack_total_block_count:%u",
859c9b60788SChao Yu 			  le32_to_cpu(cp_block->cp_pack_total_block_count));
860d3f07c04SChao Yu 		goto invalid_cp;
861c9b60788SChao Yu 	}
862fc0065adSTiezhu Yang 	pre_version = *version;
863127e670aSJaegeuk Kim 
86425ca923bSJaegeuk Kim 	cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1;
865fc0065adSTiezhu Yang 	err = get_checkpoint_version(sbi, cp_addr, &cp_block,
866fc0065adSTiezhu Yang 					&cp_page_2, version);
867fc0065adSTiezhu Yang 	if (err)
868d3f07c04SChao Yu 		goto invalid_cp;
869fc0065adSTiezhu Yang 	cur_version = *version;
870127e670aSJaegeuk Kim 
871127e670aSJaegeuk Kim 	if (cur_version == pre_version) {
872127e670aSJaegeuk Kim 		*version = cur_version;
873127e670aSJaegeuk Kim 		f2fs_put_page(cp_page_2, 1);
874127e670aSJaegeuk Kim 		return cp_page_1;
875127e670aSJaegeuk Kim 	}
876127e670aSJaegeuk Kim 	f2fs_put_page(cp_page_2, 1);
877d3f07c04SChao Yu invalid_cp:
878127e670aSJaegeuk Kim 	f2fs_put_page(cp_page_1, 1);
879127e670aSJaegeuk Kim 	return NULL;
880127e670aSJaegeuk Kim }
881127e670aSJaegeuk Kim 
8824d57b86dSChao Yu int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi)
883127e670aSJaegeuk Kim {
884127e670aSJaegeuk Kim 	struct f2fs_checkpoint *cp_block;
885127e670aSJaegeuk Kim 	struct f2fs_super_block *fsb = sbi->raw_super;
886127e670aSJaegeuk Kim 	struct page *cp1, *cp2, *cur_page;
887127e670aSJaegeuk Kim 	unsigned long blk_size = sbi->blocksize;
888127e670aSJaegeuk Kim 	unsigned long long cp1_version = 0, cp2_version = 0;
889127e670aSJaegeuk Kim 	unsigned long long cp_start_blk_no;
89055141486SWanpeng Li 	unsigned int cp_blks = 1 + __cp_payload(sbi);
8911dbe4152SChangman Lee 	block_t cp_blk_no;
8921dbe4152SChangman Lee 	int i;
89310f966bbSChao Yu 	int err;
894127e670aSJaegeuk Kim 
895026f0507SKees Cook 	sbi->ckpt = f2fs_kzalloc(sbi, array_size(blk_size, cp_blks),
896026f0507SKees Cook 				 GFP_KERNEL);
897127e670aSJaegeuk Kim 	if (!sbi->ckpt)
898127e670aSJaegeuk Kim 		return -ENOMEM;
899127e670aSJaegeuk Kim 	/*
900127e670aSJaegeuk Kim 	 * Finding out valid cp block involves read both
901127e670aSJaegeuk Kim 	 * sets( cp pack1 and cp pack 2)
902127e670aSJaegeuk Kim 	 */
903127e670aSJaegeuk Kim 	cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr);
904127e670aSJaegeuk Kim 	cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
905127e670aSJaegeuk Kim 
906127e670aSJaegeuk Kim 	/* The second checkpoint pack should start at the next segment */
907f9a4e6dfSJaegeuk Kim 	cp_start_blk_no += ((unsigned long long)1) <<
908f9a4e6dfSJaegeuk Kim 				le32_to_cpu(fsb->log_blocks_per_seg);
909127e670aSJaegeuk Kim 	cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
910127e670aSJaegeuk Kim 
911127e670aSJaegeuk Kim 	if (cp1 && cp2) {
912127e670aSJaegeuk Kim 		if (ver_after(cp2_version, cp1_version))
913127e670aSJaegeuk Kim 			cur_page = cp2;
914127e670aSJaegeuk Kim 		else
915127e670aSJaegeuk Kim 			cur_page = cp1;
916127e670aSJaegeuk Kim 	} else if (cp1) {
917127e670aSJaegeuk Kim 		cur_page = cp1;
918127e670aSJaegeuk Kim 	} else if (cp2) {
919127e670aSJaegeuk Kim 		cur_page = cp2;
920127e670aSJaegeuk Kim 	} else {
92110f966bbSChao Yu 		err = -EFSCORRUPTED;
922127e670aSJaegeuk Kim 		goto fail_no_cp;
923127e670aSJaegeuk Kim 	}
924127e670aSJaegeuk Kim 
925127e670aSJaegeuk Kim 	cp_block = (struct f2fs_checkpoint *)page_address(cur_page);
926127e670aSJaegeuk Kim 	memcpy(sbi->ckpt, cp_block, blk_size);
927127e670aSJaegeuk Kim 
9288508e44aSJaegeuk Kim 	if (cur_page == cp1)
9298508e44aSJaegeuk Kim 		sbi->cur_cp_pack = 1;
9308508e44aSJaegeuk Kim 	else
9318508e44aSJaegeuk Kim 		sbi->cur_cp_pack = 2;
932984ec63cSShawn Lin 
933e494c2f9SChao Yu 	/* Sanity checking of checkpoint */
93410f966bbSChao Yu 	if (f2fs_sanity_check_ckpt(sbi)) {
93510f966bbSChao Yu 		err = -EFSCORRUPTED;
936e494c2f9SChao Yu 		goto free_fail_no_cp;
93710f966bbSChao Yu 	}
938e494c2f9SChao Yu 
9391dbe4152SChangman Lee 	if (cp_blks <= 1)
9401dbe4152SChangman Lee 		goto done;
9411dbe4152SChangman Lee 
9421dbe4152SChangman Lee 	cp_blk_no = le32_to_cpu(fsb->cp_blkaddr);
9431dbe4152SChangman Lee 	if (cur_page == cp2)
9441dbe4152SChangman Lee 		cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg);
9451dbe4152SChangman Lee 
9461dbe4152SChangman Lee 	for (i = 1; i < cp_blks; i++) {
9471dbe4152SChangman Lee 		void *sit_bitmap_ptr;
9481dbe4152SChangman Lee 		unsigned char *ckpt = (unsigned char *)sbi->ckpt;
9491dbe4152SChangman Lee 
9504d57b86dSChao Yu 		cur_page = f2fs_get_meta_page(sbi, cp_blk_no + i);
95110f966bbSChao Yu 		if (IS_ERR(cur_page)) {
95210f966bbSChao Yu 			err = PTR_ERR(cur_page);
9537735730dSChao Yu 			goto free_fail_no_cp;
95410f966bbSChao Yu 		}
9551dbe4152SChangman Lee 		sit_bitmap_ptr = page_address(cur_page);
9561dbe4152SChangman Lee 		memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size);
9571dbe4152SChangman Lee 		f2fs_put_page(cur_page, 1);
9581dbe4152SChangman Lee 	}
9591dbe4152SChangman Lee done:
960127e670aSJaegeuk Kim 	f2fs_put_page(cp1, 1);
961127e670aSJaegeuk Kim 	f2fs_put_page(cp2, 1);
962127e670aSJaegeuk Kim 	return 0;
963127e670aSJaegeuk Kim 
964a2125ff7SJaegeuk Kim free_fail_no_cp:
965a2125ff7SJaegeuk Kim 	f2fs_put_page(cp1, 1);
966a2125ff7SJaegeuk Kim 	f2fs_put_page(cp2, 1);
967127e670aSJaegeuk Kim fail_no_cp:
9685222595dSJaegeuk Kim 	kvfree(sbi->ckpt);
96910f966bbSChao Yu 	return err;
970127e670aSJaegeuk Kim }
971127e670aSJaegeuk Kim 
972c227f912SChao Yu static void __add_dirty_inode(struct inode *inode, enum inode_type type)
973127e670aSJaegeuk Kim {
9744081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
975c227f912SChao Yu 	int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
9762d7b822aSChao Yu 
97791942321SJaegeuk Kim 	if (is_inode_flag_set(inode, flag))
9782710fd7eSChao Yu 		return;
9792d7b822aSChao Yu 
98091942321SJaegeuk Kim 	set_inode_flag(inode, flag);
98199f4b917SChao Yu 	if (!f2fs_is_volatile_file(inode))
98299f4b917SChao Yu 		list_add_tail(&F2FS_I(inode)->dirty_list,
98399f4b917SChao Yu 						&sbi->inode_list[type]);
98433fbd510SChao Yu 	stat_inc_dirty_inode(sbi, type);
9855deb8267SJaegeuk Kim }
9865deb8267SJaegeuk Kim 
987c227f912SChao Yu static void __remove_dirty_inode(struct inode *inode, enum inode_type type)
9886ad7609aSChao Yu {
989c227f912SChao Yu 	int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
9906ad7609aSChao Yu 
99191942321SJaegeuk Kim 	if (get_dirty_pages(inode) || !is_inode_flag_set(inode, flag))
9926ad7609aSChao Yu 		return;
9936ad7609aSChao Yu 
99491942321SJaegeuk Kim 	list_del_init(&F2FS_I(inode)->dirty_list);
99591942321SJaegeuk Kim 	clear_inode_flag(inode, flag);
99633fbd510SChao Yu 	stat_dec_dirty_inode(F2FS_I_SB(inode), type);
9976ad7609aSChao Yu }
9986ad7609aSChao Yu 
9994d57b86dSChao Yu void f2fs_update_dirty_page(struct inode *inode, struct page *page)
10005deb8267SJaegeuk Kim {
10014081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1002c227f912SChao Yu 	enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
10035deb8267SJaegeuk Kim 
10045ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
10055ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
1006127e670aSJaegeuk Kim 		return;
10077bd59381SGu Zheng 
1008c227f912SChao Yu 	spin_lock(&sbi->inode_lock[type]);
10091c4bf763SJaegeuk Kim 	if (type != FILE_INODE || test_opt(sbi, DATA_FLUSH))
1010c227f912SChao Yu 		__add_dirty_inode(inode, type);
1011b951a4ecSYunlei He 	inode_inc_dirty_pages(inode);
10121c4bf763SJaegeuk Kim 	spin_unlock(&sbi->inode_lock[type]);
10131c4bf763SJaegeuk Kim 
1014240a5915SChao Yu 	f2fs_set_page_private(page, 0);
10159e4ded3fSJaegeuk Kim 	f2fs_trace_pid(page);
10165deb8267SJaegeuk Kim }
1017127e670aSJaegeuk Kim 
10184d57b86dSChao Yu void f2fs_remove_dirty_inode(struct inode *inode)
1019127e670aSJaegeuk Kim {
10204081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1021c227f912SChao Yu 	enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
1022127e670aSJaegeuk Kim 
1023c227f912SChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
1024c227f912SChao Yu 			!S_ISLNK(inode->i_mode))
1025127e670aSJaegeuk Kim 		return;
1026127e670aSJaegeuk Kim 
102710aa97c3SJaegeuk Kim 	if (type == FILE_INODE && !test_opt(sbi, DATA_FLUSH))
102810aa97c3SJaegeuk Kim 		return;
102910aa97c3SJaegeuk Kim 
1030c227f912SChao Yu 	spin_lock(&sbi->inode_lock[type]);
1031c227f912SChao Yu 	__remove_dirty_inode(inode, type);
1032c227f912SChao Yu 	spin_unlock(&sbi->inode_lock[type]);
1033afc3eda2SJaegeuk Kim }
103474d0b917SJaegeuk Kim 
10354d57b86dSChao Yu int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type)
1036127e670aSJaegeuk Kim {
1037ce3b7d80SGu Zheng 	struct list_head *head;
1038127e670aSJaegeuk Kim 	struct inode *inode;
10392710fd7eSChao Yu 	struct f2fs_inode_info *fi;
10404cf18537SChao Yu 	bool is_dir = (type == DIR_INODE);
10414db08d01SJaegeuk Kim 	unsigned long ino = 0;
10424cf18537SChao Yu 
10434cf18537SChao Yu 	trace_f2fs_sync_dirty_inodes_enter(sbi->sb, is_dir,
10444cf18537SChao Yu 				get_pages(sbi, is_dir ?
10454cf18537SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
1046127e670aSJaegeuk Kim retry:
1047af41d3eeSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
10486d5a1495SChao Yu 		return -EIO;
1049af41d3eeSJaegeuk Kim 
1050c227f912SChao Yu 	spin_lock(&sbi->inode_lock[type]);
1051ce3b7d80SGu Zheng 
1052c227f912SChao Yu 	head = &sbi->inode_list[type];
1053127e670aSJaegeuk Kim 	if (list_empty(head)) {
1054c227f912SChao Yu 		spin_unlock(&sbi->inode_lock[type]);
10554cf18537SChao Yu 		trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir,
10564cf18537SChao Yu 				get_pages(sbi, is_dir ?
10574cf18537SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
10586d5a1495SChao Yu 		return 0;
1059127e670aSJaegeuk Kim 	}
1060939afa94SChao Yu 	fi = list_first_entry(head, struct f2fs_inode_info, dirty_list);
10612710fd7eSChao Yu 	inode = igrab(&fi->vfs_inode);
1062c227f912SChao Yu 	spin_unlock(&sbi->inode_lock[type]);
1063127e670aSJaegeuk Kim 	if (inode) {
10644db08d01SJaegeuk Kim 		unsigned long cur_ino = inode->i_ino;
10654db08d01SJaegeuk Kim 
1066b0af6d49SChao Yu 		F2FS_I(inode)->cp_task = current;
1067b0af6d49SChao Yu 
106887d6f890SJaegeuk Kim 		filemap_fdatawrite(inode->i_mapping);
1069b0af6d49SChao Yu 
1070b0af6d49SChao Yu 		F2FS_I(inode)->cp_task = NULL;
1071b0af6d49SChao Yu 
1072127e670aSJaegeuk Kim 		iput(inode);
10734db08d01SJaegeuk Kim 		/* We need to give cpu to another writers. */
10742a63531aSYunlei He 		if (ino == cur_ino)
10754db08d01SJaegeuk Kim 			cond_resched();
10762a63531aSYunlei He 		else
10774db08d01SJaegeuk Kim 			ino = cur_ino;
1078127e670aSJaegeuk Kim 	} else {
1079127e670aSJaegeuk Kim 		/*
1080127e670aSJaegeuk Kim 		 * We should submit bio, since it exists several
1081127e670aSJaegeuk Kim 		 * wribacking dentry pages in the freeing inode.
1082127e670aSJaegeuk Kim 		 */
1083b9109b0eSJaegeuk Kim 		f2fs_submit_merged_write(sbi, DATA);
10847ecebe5eSSebastian Andrzej Siewior 		cond_resched();
1085127e670aSJaegeuk Kim 	}
1086127e670aSJaegeuk Kim 	goto retry;
1087127e670aSJaegeuk Kim }
1088127e670aSJaegeuk Kim 
10890f18b462SJaegeuk Kim int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi)
10900f18b462SJaegeuk Kim {
10910f18b462SJaegeuk Kim 	struct list_head *head = &sbi->inode_list[DIRTY_META];
10920f18b462SJaegeuk Kim 	struct inode *inode;
10930f18b462SJaegeuk Kim 	struct f2fs_inode_info *fi;
10940f18b462SJaegeuk Kim 	s64 total = get_pages(sbi, F2FS_DIRTY_IMETA);
10950f18b462SJaegeuk Kim 
10960f18b462SJaegeuk Kim 	while (total--) {
10970f18b462SJaegeuk Kim 		if (unlikely(f2fs_cp_error(sbi)))
10980f18b462SJaegeuk Kim 			return -EIO;
10990f18b462SJaegeuk Kim 
11000f18b462SJaegeuk Kim 		spin_lock(&sbi->inode_lock[DIRTY_META]);
11010f18b462SJaegeuk Kim 		if (list_empty(head)) {
11020f18b462SJaegeuk Kim 			spin_unlock(&sbi->inode_lock[DIRTY_META]);
11030f18b462SJaegeuk Kim 			return 0;
11040f18b462SJaegeuk Kim 		}
1105939afa94SChao Yu 		fi = list_first_entry(head, struct f2fs_inode_info,
11060f18b462SJaegeuk Kim 							gdirty_list);
11070f18b462SJaegeuk Kim 		inode = igrab(&fi->vfs_inode);
11080f18b462SJaegeuk Kim 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
11090f18b462SJaegeuk Kim 		if (inode) {
111018340edcSJaegeuk Kim 			sync_inode_metadata(inode, 0);
111118340edcSJaegeuk Kim 
111218340edcSJaegeuk Kim 			/* it's on eviction */
111318340edcSJaegeuk Kim 			if (is_inode_flag_set(inode, FI_DIRTY_INODE))
11144d57b86dSChao Yu 				f2fs_update_inode_page(inode);
11150f18b462SJaegeuk Kim 			iput(inode);
11160f18b462SJaegeuk Kim 		}
1117dee668c1SChao Yu 	}
11180f18b462SJaegeuk Kim 	return 0;
11190f18b462SJaegeuk Kim }
11200f18b462SJaegeuk Kim 
112159c9081bSYunlei He static void __prepare_cp_block(struct f2fs_sb_info *sbi)
112259c9081bSYunlei He {
112359c9081bSYunlei He 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
112459c9081bSYunlei He 	struct f2fs_nm_info *nm_i = NM_I(sbi);
112559c9081bSYunlei He 	nid_t last_nid = nm_i->next_scan_nid;
112659c9081bSYunlei He 
112759c9081bSYunlei He 	next_free_nid(sbi, &last_nid);
112859c9081bSYunlei He 	ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
112959c9081bSYunlei He 	ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi));
113059c9081bSYunlei He 	ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi));
113159c9081bSYunlei He 	ckpt->next_free_nid = cpu_to_le32(last_nid);
113259c9081bSYunlei He }
113359c9081bSYunlei He 
1134af033b2aSChao Yu static bool __need_flush_quota(struct f2fs_sb_info *sbi)
1135af033b2aSChao Yu {
1136db6ec53bSJaegeuk Kim 	bool ret = false;
1137db6ec53bSJaegeuk Kim 
1138af033b2aSChao Yu 	if (!is_journalled_quota(sbi))
1139af033b2aSChao Yu 		return false;
1140db6ec53bSJaegeuk Kim 
1141db6ec53bSJaegeuk Kim 	down_write(&sbi->quota_sem);
1142db6ec53bSJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH)) {
1143db6ec53bSJaegeuk Kim 		ret = false;
1144db6ec53bSJaegeuk Kim 	} else if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR)) {
1145db6ec53bSJaegeuk Kim 		ret = false;
1146db6ec53bSJaegeuk Kim 	} else if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_FLUSH)) {
1147db6ec53bSJaegeuk Kim 		clear_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH);
1148db6ec53bSJaegeuk Kim 		ret = true;
1149db6ec53bSJaegeuk Kim 	} else if (get_pages(sbi, F2FS_DIRTY_QDATA)) {
1150db6ec53bSJaegeuk Kim 		ret = true;
1151db6ec53bSJaegeuk Kim 	}
1152db6ec53bSJaegeuk Kim 	up_write(&sbi->quota_sem);
1153db6ec53bSJaegeuk Kim 	return ret;
1154af033b2aSChao Yu }
1155af033b2aSChao Yu 
11560a8165d7SJaegeuk Kim /*
1157127e670aSJaegeuk Kim  * Freeze all the FS-operations for checkpoint.
1158127e670aSJaegeuk Kim  */
1159cf779cabSJaegeuk Kim static int block_operations(struct f2fs_sb_info *sbi)
1160127e670aSJaegeuk Kim {
1161127e670aSJaegeuk Kim 	struct writeback_control wbc = {
1162127e670aSJaegeuk Kim 		.sync_mode = WB_SYNC_ALL,
1163127e670aSJaegeuk Kim 		.nr_to_write = LONG_MAX,
1164127e670aSJaegeuk Kim 		.for_reclaim = 0,
1165127e670aSJaegeuk Kim 	};
1166c718379bSJaegeuk Kim 	struct blk_plug plug;
1167af033b2aSChao Yu 	int err = 0, cnt = 0;
1168c718379bSJaegeuk Kim 
1169c718379bSJaegeuk Kim 	blk_start_plug(&plug);
1170c718379bSJaegeuk Kim 
1171af033b2aSChao Yu retry_flush_quotas:
1172db6ec53bSJaegeuk Kim 	f2fs_lock_all(sbi);
1173af033b2aSChao Yu 	if (__need_flush_quota(sbi)) {
1174af033b2aSChao Yu 		int locked;
1175af033b2aSChao Yu 
1176af033b2aSChao Yu 		if (++cnt > DEFAULT_RETRY_QUOTA_FLUSH_COUNT) {
1177af033b2aSChao Yu 			set_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH);
1178db6ec53bSJaegeuk Kim 			set_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH);
1179af033b2aSChao Yu 			goto retry_flush_dents;
1180af033b2aSChao Yu 		}
1181db6ec53bSJaegeuk Kim 		f2fs_unlock_all(sbi);
1182af033b2aSChao Yu 
1183af033b2aSChao Yu 		/* only failed during mount/umount/freeze/quotactl */
1184af033b2aSChao Yu 		locked = down_read_trylock(&sbi->sb->s_umount);
1185af033b2aSChao Yu 		f2fs_quota_sync(sbi->sb, -1);
1186af033b2aSChao Yu 		if (locked)
1187af033b2aSChao Yu 			up_read(&sbi->sb->s_umount);
1188af033b2aSChao Yu 		cond_resched();
1189af033b2aSChao Yu 		goto retry_flush_quotas;
1190af033b2aSChao Yu 	}
1191af033b2aSChao Yu 
1192af033b2aSChao Yu retry_flush_dents:
1193127e670aSJaegeuk Kim 	/* write all the dirty dentry pages */
1194127e670aSJaegeuk Kim 	if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
1195e479556bSGu Zheng 		f2fs_unlock_all(sbi);
11964d57b86dSChao Yu 		err = f2fs_sync_dirty_inodes(sbi, DIR_INODE);
11976d5a1495SChao Yu 		if (err)
1198cf779cabSJaegeuk Kim 			goto out;
119930973883SJaegeuk Kim 		cond_resched();
1200af033b2aSChao Yu 		goto retry_flush_quotas;
1201127e670aSJaegeuk Kim 	}
1202127e670aSJaegeuk Kim 
120359c9081bSYunlei He 	/*
120459c9081bSYunlei He 	 * POR: we should ensure that there are no dirty node pages
120559c9081bSYunlei He 	 * until finishing nat/sit flush. inode->i_blocks can be updated.
120659c9081bSYunlei He 	 */
120759c9081bSYunlei He 	down_write(&sbi->node_change);
120859c9081bSYunlei He 
12090f18b462SJaegeuk Kim 	if (get_pages(sbi, F2FS_DIRTY_IMETA)) {
121059c9081bSYunlei He 		up_write(&sbi->node_change);
12110f18b462SJaegeuk Kim 		f2fs_unlock_all(sbi);
12120f18b462SJaegeuk Kim 		err = f2fs_sync_inode_meta(sbi);
12130f18b462SJaegeuk Kim 		if (err)
12140f18b462SJaegeuk Kim 			goto out;
121530973883SJaegeuk Kim 		cond_resched();
1216af033b2aSChao Yu 		goto retry_flush_quotas;
12170f18b462SJaegeuk Kim 	}
12180f18b462SJaegeuk Kim 
121939936837SJaegeuk Kim retry_flush_nodes:
1220b3582c68SChao Yu 	down_write(&sbi->node_write);
1221127e670aSJaegeuk Kim 
1222127e670aSJaegeuk Kim 	if (get_pages(sbi, F2FS_DIRTY_NODES)) {
1223b3582c68SChao Yu 		up_write(&sbi->node_write);
1224c29fd0c0SChao Yu 		atomic_inc(&sbi->wb_sync_req[NODE]);
12254d57b86dSChao Yu 		err = f2fs_sync_node_pages(sbi, &wbc, false, FS_CP_NODE_IO);
1226c29fd0c0SChao Yu 		atomic_dec(&sbi->wb_sync_req[NODE]);
12276d5a1495SChao Yu 		if (err) {
122859c9081bSYunlei He 			up_write(&sbi->node_change);
1229cf779cabSJaegeuk Kim 			f2fs_unlock_all(sbi);
1230cf779cabSJaegeuk Kim 			goto out;
1231cf779cabSJaegeuk Kim 		}
123230973883SJaegeuk Kim 		cond_resched();
123339936837SJaegeuk Kim 		goto retry_flush_nodes;
1234127e670aSJaegeuk Kim 	}
123559c9081bSYunlei He 
123659c9081bSYunlei He 	/*
123759c9081bSYunlei He 	 * sbi->node_change is used only for AIO write_begin path which produces
123859c9081bSYunlei He 	 * dirty node blocks and some checkpoint values by block allocation.
123959c9081bSYunlei He 	 */
124059c9081bSYunlei He 	__prepare_cp_block(sbi);
124159c9081bSYunlei He 	up_write(&sbi->node_change);
1242cf779cabSJaegeuk Kim out:
1243c718379bSJaegeuk Kim 	blk_finish_plug(&plug);
1244cf779cabSJaegeuk Kim 	return err;
1245127e670aSJaegeuk Kim }
1246127e670aSJaegeuk Kim 
1247127e670aSJaegeuk Kim static void unblock_operations(struct f2fs_sb_info *sbi)
1248127e670aSJaegeuk Kim {
1249b3582c68SChao Yu 	up_write(&sbi->node_write);
1250e479556bSGu Zheng 	f2fs_unlock_all(sbi);
1251127e670aSJaegeuk Kim }
1252127e670aSJaegeuk Kim 
125350fa53ecSChao Yu void f2fs_wait_on_all_pages_writeback(struct f2fs_sb_info *sbi)
1254fb51b5efSChangman Lee {
1255fb51b5efSChangman Lee 	DEFINE_WAIT(wait);
1256fb51b5efSChangman Lee 
1257fb51b5efSChangman Lee 	for (;;) {
1258fb51b5efSChangman Lee 		prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
1259fb51b5efSChangman Lee 
126036951b38SChao Yu 		if (!get_pages(sbi, F2FS_WB_CP_DATA))
1261fb51b5efSChangman Lee 			break;
1262fb51b5efSChangman Lee 
1263af697c0fSJaegeuk Kim 		if (unlikely(f2fs_cp_error(sbi)))
1264af697c0fSJaegeuk Kim 			break;
1265af697c0fSJaegeuk Kim 
12660ff21646SYunlei He 		io_schedule_timeout(5*HZ);
1267fb51b5efSChangman Lee 	}
1268fb51b5efSChangman Lee 	finish_wait(&sbi->cp_wait, &wait);
1269fb51b5efSChangman Lee }
1270fb51b5efSChangman Lee 
1271e4c5d848SJaegeuk Kim static void update_ckpt_flags(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1272e4c5d848SJaegeuk Kim {
1273e4c5d848SJaegeuk Kim 	unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num;
1274e4c5d848SJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1275d1aa2453SChao Yu 	unsigned long flags;
1276e4c5d848SJaegeuk Kim 
1277d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1278e4c5d848SJaegeuk Kim 
1279c473f1a9SChao Yu 	if ((cpc->reason & CP_UMOUNT) &&
128010047f53SKinglong Mee 			le32_to_cpu(ckpt->cp_pack_total_block_count) >
128122ad0b6aSJaegeuk Kim 			sbi->blocks_per_seg - NM_I(sbi)->nat_bits_blocks)
128222ad0b6aSJaegeuk Kim 		disable_nat_bits(sbi, false);
128322ad0b6aSJaegeuk Kim 
12841f43e2adSChao Yu 	if (cpc->reason & CP_TRIMMED)
12851f43e2adSChao Yu 		__set_ckpt_flags(ckpt, CP_TRIMMED_FLAG);
1286cd36d7a1SChao Yu 	else
1287cd36d7a1SChao Yu 		__clear_ckpt_flags(ckpt, CP_TRIMMED_FLAG);
12881f43e2adSChao Yu 
1289c473f1a9SChao Yu 	if (cpc->reason & CP_UMOUNT)
1290e4c5d848SJaegeuk Kim 		__set_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1291e4c5d848SJaegeuk Kim 	else
1292e4c5d848SJaegeuk Kim 		__clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1293e4c5d848SJaegeuk Kim 
1294c473f1a9SChao Yu 	if (cpc->reason & CP_FASTBOOT)
1295e4c5d848SJaegeuk Kim 		__set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1296e4c5d848SJaegeuk Kim 	else
1297e4c5d848SJaegeuk Kim 		__clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1298e4c5d848SJaegeuk Kim 
1299e4c5d848SJaegeuk Kim 	if (orphan_num)
1300e4c5d848SJaegeuk Kim 		__set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1301e4c5d848SJaegeuk Kim 	else
1302e4c5d848SJaegeuk Kim 		__clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1303e4c5d848SJaegeuk Kim 
130404f0b2eaSQiuyang Sun 	if (is_sbi_flag_set(sbi, SBI_NEED_FSCK) ||
130504f0b2eaSQiuyang Sun 		is_sbi_flag_set(sbi, SBI_IS_RESIZEFS))
1306e4c5d848SJaegeuk Kim 		__set_ckpt_flags(ckpt, CP_FSCK_FLAG);
1307e4c5d848SJaegeuk Kim 
13084354994fSDaniel Rosenberg 	if (is_sbi_flag_set(sbi, SBI_CP_DISABLED))
13094354994fSDaniel Rosenberg 		__set_ckpt_flags(ckpt, CP_DISABLED_FLAG);
13104354994fSDaniel Rosenberg 	else
13114354994fSDaniel Rosenberg 		__clear_ckpt_flags(ckpt, CP_DISABLED_FLAG);
13124354994fSDaniel Rosenberg 
1313db610a64SJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_CP_DISABLED_QUICK))
1314db610a64SJaegeuk Kim 		__set_ckpt_flags(ckpt, CP_DISABLED_QUICK_FLAG);
1315db610a64SJaegeuk Kim 	else
1316db610a64SJaegeuk Kim 		__clear_ckpt_flags(ckpt, CP_DISABLED_QUICK_FLAG);
1317db610a64SJaegeuk Kim 
1318af033b2aSChao Yu 	if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH))
1319af033b2aSChao Yu 		__set_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG);
1320bc88ac96SJaegeuk Kim 	else
1321bc88ac96SJaegeuk Kim 		__clear_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG);
1322af033b2aSChao Yu 
1323af033b2aSChao Yu 	if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR))
1324af033b2aSChao Yu 		__set_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG);
1325af033b2aSChao Yu 
1326e4c5d848SJaegeuk Kim 	/* set this flag to activate crc|cp_ver for recovery */
1327e4c5d848SJaegeuk Kim 	__set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG);
1328f2367923SJaegeuk Kim 	__clear_ckpt_flags(ckpt, CP_NOCRC_RECOVERY_FLAG);
1329e4c5d848SJaegeuk Kim 
1330d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1331e4c5d848SJaegeuk Kim }
1332e4c5d848SJaegeuk Kim 
133346706d59SGao Xiang static void commit_checkpoint(struct f2fs_sb_info *sbi,
133446706d59SGao Xiang 	void *src, block_t blk_addr)
133546706d59SGao Xiang {
133646706d59SGao Xiang 	struct writeback_control wbc = {
133746706d59SGao Xiang 		.for_reclaim = 0,
133846706d59SGao Xiang 	};
133946706d59SGao Xiang 
134046706d59SGao Xiang 	/*
134146706d59SGao Xiang 	 * pagevec_lookup_tag and lock_page again will take
13424d57b86dSChao Yu 	 * some extra time. Therefore, f2fs_update_meta_pages and
13434d57b86dSChao Yu 	 * f2fs_sync_meta_pages are combined in this function.
134446706d59SGao Xiang 	 */
13454d57b86dSChao Yu 	struct page *page = f2fs_grab_meta_page(sbi, blk_addr);
134646706d59SGao Xiang 	int err;
134746706d59SGao Xiang 
1348bae0ee7aSChao Yu 	f2fs_wait_on_page_writeback(page, META, true, true);
13498d64d365SChao Yu 
13508d64d365SChao Yu 	memcpy(page_address(page), src, PAGE_SIZE);
13518d64d365SChao Yu 
13528d64d365SChao Yu 	set_page_dirty(page);
135346706d59SGao Xiang 	if (unlikely(!clear_page_dirty_for_io(page)))
135446706d59SGao Xiang 		f2fs_bug_on(sbi, 1);
135546706d59SGao Xiang 
135646706d59SGao Xiang 	/* writeout cp pack 2 page */
135746706d59SGao Xiang 	err = __f2fs_write_meta_page(page, &wbc, FS_CP_META_IO);
1358af697c0fSJaegeuk Kim 	if (unlikely(err && f2fs_cp_error(sbi))) {
1359af697c0fSJaegeuk Kim 		f2fs_put_page(page, 1);
1360af697c0fSJaegeuk Kim 		return;
1361af697c0fSJaegeuk Kim 	}
136246706d59SGao Xiang 
1363af697c0fSJaegeuk Kim 	f2fs_bug_on(sbi, err);
136446706d59SGao Xiang 	f2fs_put_page(page, 0);
136546706d59SGao Xiang 
136646706d59SGao Xiang 	/* submit checkpoint (with barrier if NOBARRIER is not set) */
136746706d59SGao Xiang 	f2fs_submit_merged_write(sbi, META_FLUSH);
136846706d59SGao Xiang }
136946706d59SGao Xiang 
1370c34f42e2SChao Yu static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1371127e670aSJaegeuk Kim {
1372127e670aSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
137377041823SHuang Ying 	struct f2fs_nm_info *nm_i = NM_I(sbi);
1374d1aa2453SChao Yu 	unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num, flags;
1375127e670aSJaegeuk Kim 	block_t start_blk;
1376127e670aSJaegeuk Kim 	unsigned int data_sum_blocks, orphan_blocks;
13777e586fa0SJaegeuk Kim 	__u32 crc32 = 0;
1378127e670aSJaegeuk Kim 	int i;
137955141486SWanpeng Li 	int cp_payload_blks = __cp_payload(sbi);
13808f1dbbbbSShuoran Liu 	struct super_block *sb = sbi->sb;
13818f1dbbbbSShuoran Liu 	struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
13828f1dbbbbSShuoran Liu 	u64 kbytes_written;
13831228b482SChao Yu 	int err;
1384127e670aSJaegeuk Kim 
1385127e670aSJaegeuk Kim 	/* Flush all the NAT/SIT pages */
13864d57b86dSChao Yu 	f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
13878ec18bffSChao Yu 	f2fs_bug_on(sbi, get_pages(sbi, F2FS_DIRTY_META) &&
13888ec18bffSChao Yu 					!f2fs_cp_error(sbi));
1389127e670aSJaegeuk Kim 
1390127e670aSJaegeuk Kim 	/*
1391127e670aSJaegeuk Kim 	 * modify checkpoint
1392127e670aSJaegeuk Kim 	 * version number is already updated
1393127e670aSJaegeuk Kim 	 */
1394a1f72ac2SChao Yu 	ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi, true));
1395127e670aSJaegeuk Kim 	ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
1396b5b82205SChao Yu 	for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
1397127e670aSJaegeuk Kim 		ckpt->cur_node_segno[i] =
1398127e670aSJaegeuk Kim 			cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE));
1399127e670aSJaegeuk Kim 		ckpt->cur_node_blkoff[i] =
1400127e670aSJaegeuk Kim 			cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE));
1401127e670aSJaegeuk Kim 		ckpt->alloc_type[i + CURSEG_HOT_NODE] =
1402127e670aSJaegeuk Kim 				curseg_alloc_type(sbi, i + CURSEG_HOT_NODE);
1403127e670aSJaegeuk Kim 	}
1404b5b82205SChao Yu 	for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
1405127e670aSJaegeuk Kim 		ckpt->cur_data_segno[i] =
1406127e670aSJaegeuk Kim 			cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA));
1407127e670aSJaegeuk Kim 		ckpt->cur_data_blkoff[i] =
1408127e670aSJaegeuk Kim 			cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA));
1409127e670aSJaegeuk Kim 		ckpt->alloc_type[i + CURSEG_HOT_DATA] =
1410127e670aSJaegeuk Kim 				curseg_alloc_type(sbi, i + CURSEG_HOT_DATA);
1411127e670aSJaegeuk Kim 	}
1412127e670aSJaegeuk Kim 
1413127e670aSJaegeuk Kim 	/* 2 cp  + n data seg summary + orphan inode blocks */
14144d57b86dSChao Yu 	data_sum_blocks = f2fs_npages_for_summary_flush(sbi, false);
1415d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1416b5b82205SChao Yu 	if (data_sum_blocks < NR_CURSEG_DATA_TYPE)
1417aaec2b1dSChao Yu 		__set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
1418127e670aSJaegeuk Kim 	else
1419aaec2b1dSChao Yu 		__clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
1420d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1421127e670aSJaegeuk Kim 
142267298804SChao Yu 	orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num);
14231dbe4152SChangman Lee 	ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks +
14241dbe4152SChangman Lee 			orphan_blocks);
1425127e670aSJaegeuk Kim 
1426119ee914SJaegeuk Kim 	if (__remain_node_summaries(cpc->reason))
1427b5b82205SChao Yu 		ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS+
14281dbe4152SChangman Lee 				cp_payload_blks + data_sum_blocks +
14291dbe4152SChangman Lee 				orphan_blocks + NR_CURSEG_NODE_TYPE);
1430119ee914SJaegeuk Kim 	else
1431b5b82205SChao Yu 		ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
14321dbe4152SChangman Lee 				cp_payload_blks + data_sum_blocks +
14331dbe4152SChangman Lee 				orphan_blocks);
1434119ee914SJaegeuk Kim 
1435e4c5d848SJaegeuk Kim 	/* update ckpt flag for checkpoint */
1436e4c5d848SJaegeuk Kim 	update_ckpt_flags(sbi, cpc);
1437a468f0efSJaegeuk Kim 
1438127e670aSJaegeuk Kim 	/* update SIT/NAT bitmap */
1439127e670aSJaegeuk Kim 	get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
1440127e670aSJaegeuk Kim 	get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
1441127e670aSJaegeuk Kim 
1442d7eb8f1cSChao Yu 	crc32 = f2fs_checkpoint_chksum(sbi, ckpt);
14437e586fa0SJaegeuk Kim 	*((__le32 *)((unsigned char *)ckpt +
14447e586fa0SJaegeuk Kim 				le32_to_cpu(ckpt->checksum_offset)))
1445127e670aSJaegeuk Kim 				= cpu_to_le32(crc32);
1446127e670aSJaegeuk Kim 
14478508e44aSJaegeuk Kim 	start_blk = __start_cp_next_addr(sbi);
1448127e670aSJaegeuk Kim 
144922ad0b6aSJaegeuk Kim 	/* write nat bits */
145022ad0b6aSJaegeuk Kim 	if (enabled_nat_bits(sbi, cpc)) {
145122ad0b6aSJaegeuk Kim 		__u64 cp_ver = cur_cp_version(ckpt);
145222ad0b6aSJaegeuk Kim 		block_t blk;
145322ad0b6aSJaegeuk Kim 
145422ad0b6aSJaegeuk Kim 		cp_ver |= ((__u64)crc32 << 32);
145522ad0b6aSJaegeuk Kim 		*(__le64 *)nm_i->nat_bits = cpu_to_le64(cp_ver);
145622ad0b6aSJaegeuk Kim 
145722ad0b6aSJaegeuk Kim 		blk = start_blk + sbi->blocks_per_seg - nm_i->nat_bits_blocks;
145822ad0b6aSJaegeuk Kim 		for (i = 0; i < nm_i->nat_bits_blocks; i++)
14594d57b86dSChao Yu 			f2fs_update_meta_page(sbi, nm_i->nat_bits +
146022ad0b6aSJaegeuk Kim 					(i << F2FS_BLKSIZE_BITS), blk + i);
146122ad0b6aSJaegeuk Kim 	}
146222ad0b6aSJaegeuk Kim 
1463127e670aSJaegeuk Kim 	/* write out checkpoint buffer at block 0 */
14644d57b86dSChao Yu 	f2fs_update_meta_page(sbi, ckpt, start_blk++);
1465127e670aSJaegeuk Kim 
1466381722d2SChao Yu 	for (i = 1; i < 1 + cp_payload_blks; i++)
14674d57b86dSChao Yu 		f2fs_update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE,
1468381722d2SChao Yu 							start_blk++);
14691dbe4152SChangman Lee 
147067298804SChao Yu 	if (orphan_num) {
1471127e670aSJaegeuk Kim 		write_orphan_inodes(sbi, start_blk);
1472127e670aSJaegeuk Kim 		start_blk += orphan_blocks;
1473127e670aSJaegeuk Kim 	}
1474127e670aSJaegeuk Kim 
14754d57b86dSChao Yu 	f2fs_write_data_summaries(sbi, start_blk);
1476127e670aSJaegeuk Kim 	start_blk += data_sum_blocks;
14778f1dbbbbSShuoran Liu 
14788f1dbbbbSShuoran Liu 	/* Record write statistics in the hot node summary */
14798f1dbbbbSShuoran Liu 	kbytes_written = sbi->kbytes_written;
14808f1dbbbbSShuoran Liu 	if (sb->s_bdev->bd_part)
14818f1dbbbbSShuoran Liu 		kbytes_written += BD_PART_WRITTEN(sbi);
14828f1dbbbbSShuoran Liu 
1483b7ad7512SChao Yu 	seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written);
14848f1dbbbbSShuoran Liu 
1485119ee914SJaegeuk Kim 	if (__remain_node_summaries(cpc->reason)) {
14864d57b86dSChao Yu 		f2fs_write_node_summaries(sbi, start_blk);
1487127e670aSJaegeuk Kim 		start_blk += NR_CURSEG_NODE_TYPE;
1488127e670aSJaegeuk Kim 	}
1489127e670aSJaegeuk Kim 
149046706d59SGao Xiang 	/* update user_block_counts */
149146706d59SGao Xiang 	sbi->last_valid_block_count = sbi->total_valid_block_count;
149246706d59SGao Xiang 	percpu_counter_set(&sbi->alloc_valid_block_count, 0);
1493127e670aSJaegeuk Kim 
149446706d59SGao Xiang 	/* Here, we have one bio having CP pack except cp pack 2 page */
14954d57b86dSChao Yu 	f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
14968ec18bffSChao Yu 	f2fs_bug_on(sbi, get_pages(sbi, F2FS_DIRTY_META) &&
14978ec18bffSChao Yu 					!f2fs_cp_error(sbi));
149846706d59SGao Xiang 
149946706d59SGao Xiang 	/* wait for previous submitted meta pages writeback */
150050fa53ecSChao Yu 	f2fs_wait_on_all_pages_writeback(sbi);
1501127e670aSJaegeuk Kim 
150246706d59SGao Xiang 	/* flush all device cache */
150346706d59SGao Xiang 	err = f2fs_flush_device_cache(sbi);
150446706d59SGao Xiang 	if (err)
150546706d59SGao Xiang 		return err;
1506127e670aSJaegeuk Kim 
150746706d59SGao Xiang 	/* barrier and flush checkpoint cp pack 2 page if it can */
150846706d59SGao Xiang 	commit_checkpoint(sbi, ckpt, start_blk);
150950fa53ecSChao Yu 	f2fs_wait_on_all_pages_writeback(sbi);
15106a8f8ca5SJaegeuk Kim 
151118767e62SChao Yu 	/*
151218767e62SChao Yu 	 * invalidate intermediate page cache borrowed from meta inode
151318767e62SChao Yu 	 * which are used for migration of encrypted inode's blocks.
151418767e62SChao Yu 	 */
15157beb01f7SChao Yu 	if (f2fs_sb_has_encrypt(sbi))
151618767e62SChao Yu 		invalidate_mapping_pages(META_MAPPING(sbi),
151718767e62SChao Yu 				MAIN_BLKADDR(sbi), MAX_BLKADDR(sbi) - 1);
151818767e62SChao Yu 
15194d57b86dSChao Yu 	f2fs_release_ino_entry(sbi, false);
1520cf779cabSJaegeuk Kim 
152150fa53ecSChao Yu 	f2fs_reset_fsync_node_info(sbi);
152250fa53ecSChao Yu 
1523caf0047eSChao Yu 	clear_sbi_flag(sbi, SBI_IS_DIRTY);
1524bbf156f7SJaegeuk Kim 	clear_sbi_flag(sbi, SBI_NEED_CP);
1525af033b2aSChao Yu 	clear_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH);
1526c9c8ed50SChao Yu 
1527c9c8ed50SChao Yu 	spin_lock(&sbi->stat_lock);
15284354994fSDaniel Rosenberg 	sbi->unusable_block_count = 0;
1529c9c8ed50SChao Yu 	spin_unlock(&sbi->stat_lock);
1530c9c8ed50SChao Yu 
15318508e44aSJaegeuk Kim 	__set_cp_next_pack(sbi);
1532c34f42e2SChao Yu 
1533c2a080aeSChao Yu 	/*
1534c2a080aeSChao Yu 	 * redirty superblock if metadata like node page or inode cache is
1535c2a080aeSChao Yu 	 * updated during writing checkpoint.
1536c2a080aeSChao Yu 	 */
1537c2a080aeSChao Yu 	if (get_pages(sbi, F2FS_DIRTY_NODES) ||
1538c2a080aeSChao Yu 			get_pages(sbi, F2FS_DIRTY_IMETA))
1539c2a080aeSChao Yu 		set_sbi_flag(sbi, SBI_IS_DIRTY);
1540c2a080aeSChao Yu 
1541c2a080aeSChao Yu 	f2fs_bug_on(sbi, get_pages(sbi, F2FS_DIRTY_DENTS));
1542c2a080aeSChao Yu 
1543af697c0fSJaegeuk Kim 	return unlikely(f2fs_cp_error(sbi)) ? -EIO : 0;
1544127e670aSJaegeuk Kim }
1545127e670aSJaegeuk Kim 
15460a8165d7SJaegeuk Kim /*
1547e1c42045Sarter97  * We guarantee that this checkpoint procedure will not fail.
1548127e670aSJaegeuk Kim  */
15494d57b86dSChao Yu int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1550127e670aSJaegeuk Kim {
1551127e670aSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1552127e670aSJaegeuk Kim 	unsigned long long ckpt_ver;
1553c34f42e2SChao Yu 	int err = 0;
1554127e670aSJaegeuk Kim 
1555f5a131bbSChao Yu 	if (f2fs_readonly(sbi->sb) || f2fs_hw_is_readonly(sbi))
1556f5a131bbSChao Yu 		return -EROFS;
1557f5a131bbSChao Yu 
15584354994fSDaniel Rosenberg 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
15594354994fSDaniel Rosenberg 		if (cpc->reason != CP_PAUSE)
15604354994fSDaniel Rosenberg 			return 0;
1561dcbb4c10SJoe Perches 		f2fs_warn(sbi, "Start checkpoint disabled!");
15624354994fSDaniel Rosenberg 	}
1563127e670aSJaegeuk Kim 	mutex_lock(&sbi->cp_mutex);
15648501017eSJaegeuk Kim 
1565caf0047eSChao Yu 	if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) &&
1566c473f1a9SChao Yu 		((cpc->reason & CP_FASTBOOT) || (cpc->reason & CP_SYNC) ||
1567c473f1a9SChao Yu 		((cpc->reason & CP_DISCARD) && !sbi->discard_blks)))
15688501017eSJaegeuk Kim 		goto out;
1569c34f42e2SChao Yu 	if (unlikely(f2fs_cp_error(sbi))) {
1570c34f42e2SChao Yu 		err = -EIO;
1571cf779cabSJaegeuk Kim 		goto out;
1572c34f42e2SChao Yu 	}
15732bda542dSWanpeng Li 
15742bda542dSWanpeng Li 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops");
15752bda542dSWanpeng Li 
1576c34f42e2SChao Yu 	err = block_operations(sbi);
1577c34f42e2SChao Yu 	if (err)
1578cf779cabSJaegeuk Kim 		goto out;
1579127e670aSJaegeuk Kim 
158075ab4cb8SJaegeuk Kim 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops");
15812af4bd6cSNamjae Jeon 
1582b9109b0eSJaegeuk Kim 	f2fs_flush_merged_writes(sbi);
1583127e670aSJaegeuk Kim 
158458cce381SYunlei He 	/* this is the case of multiple fstrims without any changes */
1585c473f1a9SChao Yu 	if (cpc->reason & CP_DISCARD) {
15864d57b86dSChao Yu 		if (!f2fs_exist_trim_candidates(sbi, cpc)) {
158725290fa5SJaegeuk Kim 			unblock_operations(sbi);
158825290fa5SJaegeuk Kim 			goto out;
158925290fa5SJaegeuk Kim 		}
159025290fa5SJaegeuk Kim 
15910333ad4eSJaegeuk Kim 		if (NM_I(sbi)->dirty_nat_cnt == 0 &&
15920333ad4eSJaegeuk Kim 				SIT_I(sbi)->dirty_sentries == 0 &&
15930333ad4eSJaegeuk Kim 				prefree_segments(sbi) == 0) {
15944d57b86dSChao Yu 			f2fs_flush_sit_entries(sbi, cpc);
15954d57b86dSChao Yu 			f2fs_clear_prefree_segments(sbi, cpc);
159658cce381SYunlei He 			unblock_operations(sbi);
159758cce381SYunlei He 			goto out;
159858cce381SYunlei He 		}
15990333ad4eSJaegeuk Kim 	}
160058cce381SYunlei He 
1601127e670aSJaegeuk Kim 	/*
1602127e670aSJaegeuk Kim 	 * update checkpoint pack index
1603127e670aSJaegeuk Kim 	 * Increase the version number so that
1604127e670aSJaegeuk Kim 	 * SIT entries and seg summaries are written at correct place
1605127e670aSJaegeuk Kim 	 */
1606d71b5564SJaegeuk Kim 	ckpt_ver = cur_cp_version(ckpt);
1607127e670aSJaegeuk Kim 	ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
1608127e670aSJaegeuk Kim 
1609127e670aSJaegeuk Kim 	/* write cached NAT/SIT entries to NAT/SIT area */
1610edc55aafSJaegeuk Kim 	err = f2fs_flush_nat_entries(sbi, cpc);
1611edc55aafSJaegeuk Kim 	if (err)
1612edc55aafSJaegeuk Kim 		goto stop;
1613edc55aafSJaegeuk Kim 
16144d57b86dSChao Yu 	f2fs_flush_sit_entries(sbi, cpc);
1615127e670aSJaegeuk Kim 
1616127e670aSJaegeuk Kim 	/* unlock all the fs_lock[] in do_checkpoint() */
1617c34f42e2SChao Yu 	err = do_checkpoint(sbi, cpc);
16184e6a8d9bSJaegeuk Kim 	if (err)
16194d57b86dSChao Yu 		f2fs_release_discard_addrs(sbi);
16204e6a8d9bSJaegeuk Kim 	else
16214d57b86dSChao Yu 		f2fs_clear_prefree_segments(sbi, cpc);
1622edc55aafSJaegeuk Kim stop:
1623127e670aSJaegeuk Kim 	unblock_operations(sbi);
1624942e0be6SChangman Lee 	stat_inc_cp_count(sbi->stat_info);
162510027551SJaegeuk Kim 
1626c473f1a9SChao Yu 	if (cpc->reason & CP_RECOVERY)
1627dcbb4c10SJoe Perches 		f2fs_notice(sbi, "checkpoint: version = %llx", ckpt_ver);
162860b99b48SJaegeuk Kim 
162960b99b48SJaegeuk Kim 	/* do checkpoint periodically */
16306beceb54SJaegeuk Kim 	f2fs_update_time(sbi, CP_TIME);
163155d1cdb2SJaegeuk Kim 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint");
16328501017eSJaegeuk Kim out:
16338501017eSJaegeuk Kim 	mutex_unlock(&sbi->cp_mutex);
1634c34f42e2SChao Yu 	return err;
1635127e670aSJaegeuk Kim }
1636127e670aSJaegeuk Kim 
16374d57b86dSChao Yu void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi)
1638127e670aSJaegeuk Kim {
16396451e041SJaegeuk Kim 	int i;
16406451e041SJaegeuk Kim 
16416451e041SJaegeuk Kim 	for (i = 0; i < MAX_INO_ENTRY; i++) {
164267298804SChao Yu 		struct inode_management *im = &sbi->im[i];
164367298804SChao Yu 
164467298804SChao Yu 		INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC);
164567298804SChao Yu 		spin_lock_init(&im->ino_lock);
164667298804SChao Yu 		INIT_LIST_HEAD(&im->ino_list);
164767298804SChao Yu 		im->ino_num = 0;
16486451e041SJaegeuk Kim 	}
16496451e041SJaegeuk Kim 
1650b5b82205SChao Yu 	sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS -
165114b42817SWanpeng Li 			NR_CURSEG_TYPE - __cp_payload(sbi)) *
165214b42817SWanpeng Li 				F2FS_ORPHANS_PER_BLOCK;
1653127e670aSJaegeuk Kim }
1654127e670aSJaegeuk Kim 
16554d57b86dSChao Yu int __init f2fs_create_checkpoint_caches(void)
1656127e670aSJaegeuk Kim {
16576451e041SJaegeuk Kim 	ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry",
16586451e041SJaegeuk Kim 			sizeof(struct ino_entry));
16596451e041SJaegeuk Kim 	if (!ino_entry_slab)
1660127e670aSJaegeuk Kim 		return -ENOMEM;
16614d57b86dSChao Yu 	f2fs_inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry",
166206292073SChao Yu 			sizeof(struct inode_entry));
16634d57b86dSChao Yu 	if (!f2fs_inode_entry_slab) {
16646451e041SJaegeuk Kim 		kmem_cache_destroy(ino_entry_slab);
1665127e670aSJaegeuk Kim 		return -ENOMEM;
1666127e670aSJaegeuk Kim 	}
1667127e670aSJaegeuk Kim 	return 0;
1668127e670aSJaegeuk Kim }
1669127e670aSJaegeuk Kim 
16704d57b86dSChao Yu void f2fs_destroy_checkpoint_caches(void)
1671127e670aSJaegeuk Kim {
16726451e041SJaegeuk Kim 	kmem_cache_destroy(ino_entry_slab);
16734d57b86dSChao Yu 	kmem_cache_destroy(f2fs_inode_entry_slab);
1674127e670aSJaegeuk Kim }
1675