xref: /openbmc/linux/fs/f2fs/checkpoint.c (revision 1f5f11a3c41e2b23288b2769435a00f74e02496b)
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:
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;
269ce4c638cSChao Yu 		err = f2fs_submit_page_bio(&fio);
270ce4c638cSChao Yu 		f2fs_put_page(page, err ? 1 : 0);
2718b83ac81SChao Yu 
2728b83ac81SChao Yu 		if (!err)
2738b83ac81SChao Yu 			f2fs_update_iostat(sbi, FS_META_READ_IO, F2FS_BLKSIZE);
274662befdaSChao Yu 	}
275662befdaSChao Yu out:
276e9f5b8b8SChao Yu 	blk_finish_plug(&plug);
277662befdaSChao Yu 	return blkno - start;
278662befdaSChao Yu }
279662befdaSChao Yu 
2804d57b86dSChao Yu void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index)
281635aee1fSChao Yu {
282635aee1fSChao Yu 	struct page *page;
283635aee1fSChao Yu 	bool readahead = false;
284635aee1fSChao Yu 
285635aee1fSChao Yu 	page = find_get_page(META_MAPPING(sbi), index);
2864da7bf5aSJaegeuk Kim 	if (!page || !PageUptodate(page))
287635aee1fSChao Yu 		readahead = true;
288635aee1fSChao Yu 	f2fs_put_page(page, 0);
289635aee1fSChao Yu 
290635aee1fSChao Yu 	if (readahead)
2914d57b86dSChao Yu 		f2fs_ra_meta_pages(sbi, index, BIO_MAX_PAGES, META_POR, true);
292635aee1fSChao Yu }
293635aee1fSChao Yu 
294b0af6d49SChao Yu static int __f2fs_write_meta_page(struct page *page,
295b0af6d49SChao Yu 				struct writeback_control *wbc,
296b0af6d49SChao Yu 				enum iostat_type io_type)
297127e670aSJaegeuk Kim {
2984081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_P_SB(page);
299577e3495SJaegeuk Kim 
300ecda0de3SChao Yu 	trace_f2fs_writepage(page, META);
301ecda0de3SChao Yu 
302af697c0fSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
303af697c0fSJaegeuk Kim 		goto redirty_out;
304caf0047eSChao Yu 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
305cfb271d4SChao Yu 		goto redirty_out;
306857dc4e0SJaegeuk Kim 	if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0))
307cfb271d4SChao Yu 		goto redirty_out;
308127e670aSJaegeuk Kim 
3094d57b86dSChao Yu 	f2fs_do_write_meta_page(sbi, page, io_type);
310127e670aSJaegeuk Kim 	dec_page_count(sbi, F2FS_DIRTY_META);
3110c3a5797SChao Yu 
3120c3a5797SChao Yu 	if (wbc->for_reclaim)
313bab475c5SChao Yu 		f2fs_submit_merged_write_cond(sbi, NULL, page, 0, META);
3140c3a5797SChao Yu 
315127e670aSJaegeuk Kim 	unlock_page(page);
316857dc4e0SJaegeuk Kim 
3170c3a5797SChao Yu 	if (unlikely(f2fs_cp_error(sbi)))
318b9109b0eSJaegeuk Kim 		f2fs_submit_merged_write(sbi, META);
3190c3a5797SChao Yu 
320577e3495SJaegeuk Kim 	return 0;
321cfb271d4SChao Yu 
322cfb271d4SChao Yu redirty_out:
32376f60268SJaegeuk Kim 	redirty_page_for_writepage(wbc, page);
324cfb271d4SChao Yu 	return AOP_WRITEPAGE_ACTIVATE;
325127e670aSJaegeuk Kim }
326127e670aSJaegeuk Kim 
327b0af6d49SChao Yu static int f2fs_write_meta_page(struct page *page,
328b0af6d49SChao Yu 				struct writeback_control *wbc)
329b0af6d49SChao Yu {
330b0af6d49SChao Yu 	return __f2fs_write_meta_page(page, wbc, FS_META_IO);
331b0af6d49SChao Yu }
332b0af6d49SChao Yu 
333127e670aSJaegeuk Kim static int f2fs_write_meta_pages(struct address_space *mapping,
334127e670aSJaegeuk Kim 				struct writeback_control *wbc)
335127e670aSJaegeuk Kim {
3364081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
33750c8cdb3SJaegeuk Kim 	long diff, written;
338127e670aSJaegeuk Kim 
3390771fcc7SChao Yu 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
3400771fcc7SChao Yu 		goto skip_write;
3410771fcc7SChao Yu 
3425459aa97SJaegeuk Kim 	/* collect a number of dirty meta pages and write together */
343812a9597SJaegeuk Kim 	if (wbc->sync_mode != WB_SYNC_ALL &&
344812a9597SJaegeuk Kim 			get_pages(sbi, F2FS_DIRTY_META) <
345812a9597SJaegeuk Kim 					nr_pages_to_skip(sbi, META))
346d3baf95dSJaegeuk Kim 		goto skip_write;
347127e670aSJaegeuk Kim 
348a29d0e0bSYunlei He 	/* if locked failed, cp will flush dirty pages instead */
349a29d0e0bSYunlei He 	if (!mutex_trylock(&sbi->cp_mutex))
350a29d0e0bSYunlei He 		goto skip_write;
351d31c7c3fSYunlei He 
352a29d0e0bSYunlei He 	trace_f2fs_writepages(mapping->host, wbc, META);
35350c8cdb3SJaegeuk Kim 	diff = nr_pages_to_write(sbi, META, wbc);
3544d57b86dSChao Yu 	written = f2fs_sync_meta_pages(sbi, META, wbc->nr_to_write, FS_META_IO);
355127e670aSJaegeuk Kim 	mutex_unlock(&sbi->cp_mutex);
35650c8cdb3SJaegeuk Kim 	wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff);
357127e670aSJaegeuk Kim 	return 0;
358d3baf95dSJaegeuk Kim 
359d3baf95dSJaegeuk Kim skip_write:
360d3baf95dSJaegeuk Kim 	wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META);
361d31c7c3fSYunlei He 	trace_f2fs_writepages(mapping->host, wbc, META);
362d3baf95dSJaegeuk Kim 	return 0;
363127e670aSJaegeuk Kim }
364127e670aSJaegeuk Kim 
3654d57b86dSChao Yu long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
366b0af6d49SChao Yu 				long nr_to_write, enum iostat_type io_type)
367127e670aSJaegeuk Kim {
3689df27d98SGu Zheng 	struct address_space *mapping = META_MAPPING(sbi);
369028a63a6SJan Kara 	pgoff_t index = 0, prev = ULONG_MAX;
370127e670aSJaegeuk Kim 	struct pagevec pvec;
371127e670aSJaegeuk Kim 	long nwritten = 0;
372028a63a6SJan Kara 	int nr_pages;
373127e670aSJaegeuk Kim 	struct writeback_control wbc = {
374127e670aSJaegeuk Kim 		.for_reclaim = 0,
375127e670aSJaegeuk Kim 	};
376e9f5b8b8SChao Yu 	struct blk_plug plug;
377127e670aSJaegeuk Kim 
37886679820SMel Gorman 	pagevec_init(&pvec);
379127e670aSJaegeuk Kim 
380e9f5b8b8SChao Yu 	blk_start_plug(&plug);
381e9f5b8b8SChao Yu 
382028a63a6SJan Kara 	while ((nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
38367fd707fSJan Kara 				PAGECACHE_TAG_DIRTY))) {
384028a63a6SJan Kara 		int i;
385127e670aSJaegeuk Kim 
386127e670aSJaegeuk Kim 		for (i = 0; i < nr_pages; i++) {
387127e670aSJaegeuk Kim 			struct page *page = pvec.pages[i];
388203681f6SJaegeuk Kim 
38980dd9c0eSChao Yu 			if (prev == ULONG_MAX)
3906066d8cdSJaegeuk Kim 				prev = page->index - 1;
3916066d8cdSJaegeuk Kim 			if (nr_to_write != LONG_MAX && page->index != prev + 1) {
3926066d8cdSJaegeuk Kim 				pagevec_release(&pvec);
3936066d8cdSJaegeuk Kim 				goto stop;
3946066d8cdSJaegeuk Kim 			}
3956066d8cdSJaegeuk Kim 
396127e670aSJaegeuk Kim 			lock_page(page);
397203681f6SJaegeuk Kim 
398203681f6SJaegeuk Kim 			if (unlikely(page->mapping != mapping)) {
399203681f6SJaegeuk Kim continue_unlock:
400203681f6SJaegeuk Kim 				unlock_page(page);
401203681f6SJaegeuk Kim 				continue;
402203681f6SJaegeuk Kim 			}
403203681f6SJaegeuk Kim 			if (!PageDirty(page)) {
404203681f6SJaegeuk Kim 				/* someone wrote it for us */
405203681f6SJaegeuk Kim 				goto continue_unlock;
406203681f6SJaegeuk Kim 			}
407203681f6SJaegeuk Kim 
408bae0ee7aSChao Yu 			f2fs_wait_on_page_writeback(page, META, true, true);
409fa3d2bdfSJaegeuk Kim 
410203681f6SJaegeuk Kim 			if (!clear_page_dirty_for_io(page))
411203681f6SJaegeuk Kim 				goto continue_unlock;
412203681f6SJaegeuk Kim 
413b0af6d49SChao Yu 			if (__f2fs_write_meta_page(page, &wbc, io_type)) {
414577e3495SJaegeuk Kim 				unlock_page(page);
415577e3495SJaegeuk Kim 				break;
416577e3495SJaegeuk Kim 			}
417cfb271d4SChao Yu 			nwritten++;
4186066d8cdSJaegeuk Kim 			prev = page->index;
419cfb271d4SChao Yu 			if (unlikely(nwritten >= nr_to_write))
420127e670aSJaegeuk Kim 				break;
421127e670aSJaegeuk Kim 		}
422127e670aSJaegeuk Kim 		pagevec_release(&pvec);
423127e670aSJaegeuk Kim 		cond_resched();
424127e670aSJaegeuk Kim 	}
4256066d8cdSJaegeuk Kim stop:
426127e670aSJaegeuk Kim 	if (nwritten)
427b9109b0eSJaegeuk Kim 		f2fs_submit_merged_write(sbi, type);
428127e670aSJaegeuk Kim 
429e9f5b8b8SChao Yu 	blk_finish_plug(&plug);
430e9f5b8b8SChao Yu 
431127e670aSJaegeuk Kim 	return nwritten;
432127e670aSJaegeuk Kim }
433127e670aSJaegeuk Kim 
434127e670aSJaegeuk Kim static int f2fs_set_meta_page_dirty(struct page *page)
435127e670aSJaegeuk Kim {
43626c6b887SJaegeuk Kim 	trace_f2fs_set_page_dirty(page, META);
43726c6b887SJaegeuk Kim 
438237c0790SJaegeuk Kim 	if (!PageUptodate(page))
439127e670aSJaegeuk Kim 		SetPageUptodate(page);
440127e670aSJaegeuk Kim 	if (!PageDirty(page)) {
441b87078adSJaegeuk Kim 		__set_page_dirty_nobuffers(page);
4424081363fSJaegeuk Kim 		inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_META);
443240a5915SChao Yu 		f2fs_set_page_private(page, 0);
4449e4ded3fSJaegeuk Kim 		f2fs_trace_pid(page);
445127e670aSJaegeuk Kim 		return 1;
446127e670aSJaegeuk Kim 	}
447127e670aSJaegeuk Kim 	return 0;
448127e670aSJaegeuk Kim }
449127e670aSJaegeuk Kim 
450127e670aSJaegeuk Kim const struct address_space_operations f2fs_meta_aops = {
451127e670aSJaegeuk Kim 	.writepage	= f2fs_write_meta_page,
452127e670aSJaegeuk Kim 	.writepages	= f2fs_write_meta_pages,
453127e670aSJaegeuk Kim 	.set_page_dirty	= f2fs_set_meta_page_dirty,
454487261f3SChao Yu 	.invalidatepage = f2fs_invalidate_page,
455487261f3SChao Yu 	.releasepage	= f2fs_release_page,
4565b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION
4575b7a487cSWeichao Guo 	.migratepage    = f2fs_migrate_page,
4585b7a487cSWeichao Guo #endif
459127e670aSJaegeuk Kim };
460127e670aSJaegeuk Kim 
46139d787beSChao Yu static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino,
46239d787beSChao Yu 						unsigned int devidx, int type)
463953e6cc6SJaegeuk Kim {
46467298804SChao Yu 	struct inode_management *im = &sbi->im[type];
46580c54505SJaegeuk Kim 	struct ino_entry *e, *tmp;
46680c54505SJaegeuk Kim 
46780c54505SJaegeuk Kim 	tmp = f2fs_kmem_cache_alloc(ino_entry_slab, GFP_NOFS);
46819526d74SChao Yu 
46980c54505SJaegeuk Kim 	radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
470769ec6e5SJaegeuk Kim 
47167298804SChao Yu 	spin_lock(&im->ino_lock);
47267298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
47339efac41SJaegeuk Kim 	if (!e) {
47480c54505SJaegeuk Kim 		e = tmp;
47519526d74SChao Yu 		if (unlikely(radix_tree_insert(&im->ino_root, ino, e)))
47619526d74SChao Yu 			f2fs_bug_on(sbi, 1);
47719526d74SChao Yu 
47839efac41SJaegeuk Kim 		memset(e, 0, sizeof(struct ino_entry));
47939efac41SJaegeuk Kim 		e->ino = ino;
48039efac41SJaegeuk Kim 
48167298804SChao Yu 		list_add_tail(&e->list, &im->ino_list);
4828c402946SJaegeuk Kim 		if (type != ORPHAN_INO)
48367298804SChao Yu 			im->ino_num++;
48439efac41SJaegeuk Kim 	}
48539d787beSChao Yu 
48639d787beSChao Yu 	if (type == FLUSH_INO)
48739d787beSChao Yu 		f2fs_set_bit(devidx, (char *)&e->dirty_device);
48839d787beSChao Yu 
48967298804SChao Yu 	spin_unlock(&im->ino_lock);
490769ec6e5SJaegeuk Kim 	radix_tree_preload_end();
49180c54505SJaegeuk Kim 
49280c54505SJaegeuk Kim 	if (e != tmp)
49380c54505SJaegeuk Kim 		kmem_cache_free(ino_entry_slab, tmp);
494953e6cc6SJaegeuk Kim }
495953e6cc6SJaegeuk Kim 
4966451e041SJaegeuk Kim static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
497953e6cc6SJaegeuk Kim {
49867298804SChao Yu 	struct inode_management *im = &sbi->im[type];
4996451e041SJaegeuk Kim 	struct ino_entry *e;
500953e6cc6SJaegeuk Kim 
50167298804SChao Yu 	spin_lock(&im->ino_lock);
50267298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
50339efac41SJaegeuk Kim 	if (e) {
504953e6cc6SJaegeuk Kim 		list_del(&e->list);
50567298804SChao Yu 		radix_tree_delete(&im->ino_root, ino);
50667298804SChao Yu 		im->ino_num--;
50767298804SChao Yu 		spin_unlock(&im->ino_lock);
5086451e041SJaegeuk Kim 		kmem_cache_free(ino_entry_slab, e);
509953e6cc6SJaegeuk Kim 		return;
510953e6cc6SJaegeuk Kim 	}
51167298804SChao Yu 	spin_unlock(&im->ino_lock);
512953e6cc6SJaegeuk Kim }
513953e6cc6SJaegeuk Kim 
5144d57b86dSChao Yu void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
515fff04f90SJaegeuk Kim {
516fff04f90SJaegeuk Kim 	/* add new dirty ino entry into list */
51739d787beSChao Yu 	__add_ino_entry(sbi, ino, 0, type);
518fff04f90SJaegeuk Kim }
519fff04f90SJaegeuk Kim 
5204d57b86dSChao Yu void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
521fff04f90SJaegeuk Kim {
522fff04f90SJaegeuk Kim 	/* remove dirty ino entry from list */
523fff04f90SJaegeuk Kim 	__remove_ino_entry(sbi, ino, type);
524fff04f90SJaegeuk Kim }
525fff04f90SJaegeuk Kim 
526fff04f90SJaegeuk Kim /* mode should be APPEND_INO or UPDATE_INO */
5274d57b86dSChao Yu bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode)
528fff04f90SJaegeuk Kim {
52967298804SChao Yu 	struct inode_management *im = &sbi->im[mode];
530fff04f90SJaegeuk Kim 	struct ino_entry *e;
53167298804SChao Yu 
53267298804SChao Yu 	spin_lock(&im->ino_lock);
53367298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
53467298804SChao Yu 	spin_unlock(&im->ino_lock);
535fff04f90SJaegeuk Kim 	return e ? true : false;
536fff04f90SJaegeuk Kim }
537fff04f90SJaegeuk Kim 
5384d57b86dSChao Yu void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all)
539fff04f90SJaegeuk Kim {
540fff04f90SJaegeuk Kim 	struct ino_entry *e, *tmp;
541fff04f90SJaegeuk Kim 	int i;
542fff04f90SJaegeuk Kim 
54339d787beSChao Yu 	for (i = all ? ORPHAN_INO : APPEND_INO; i < MAX_INO_ENTRY; i++) {
54467298804SChao Yu 		struct inode_management *im = &sbi->im[i];
54567298804SChao Yu 
54667298804SChao Yu 		spin_lock(&im->ino_lock);
54767298804SChao Yu 		list_for_each_entry_safe(e, tmp, &im->ino_list, list) {
548fff04f90SJaegeuk Kim 			list_del(&e->list);
54967298804SChao Yu 			radix_tree_delete(&im->ino_root, e->ino);
550fff04f90SJaegeuk Kim 			kmem_cache_free(ino_entry_slab, e);
55167298804SChao Yu 			im->ino_num--;
552fff04f90SJaegeuk Kim 		}
55367298804SChao Yu 		spin_unlock(&im->ino_lock);
554fff04f90SJaegeuk Kim 	}
555fff04f90SJaegeuk Kim }
556fff04f90SJaegeuk Kim 
5574d57b86dSChao Yu void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
55839d787beSChao Yu 					unsigned int devidx, int type)
55939d787beSChao Yu {
56039d787beSChao Yu 	__add_ino_entry(sbi, ino, devidx, type);
56139d787beSChao Yu }
56239d787beSChao Yu 
5634d57b86dSChao Yu bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
56439d787beSChao Yu 					unsigned int devidx, int type)
56539d787beSChao Yu {
56639d787beSChao Yu 	struct inode_management *im = &sbi->im[type];
56739d787beSChao Yu 	struct ino_entry *e;
56839d787beSChao Yu 	bool is_dirty = false;
56939d787beSChao Yu 
57039d787beSChao Yu 	spin_lock(&im->ino_lock);
57139d787beSChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
57239d787beSChao Yu 	if (e && f2fs_test_bit(devidx, (char *)&e->dirty_device))
57339d787beSChao Yu 		is_dirty = true;
57439d787beSChao Yu 	spin_unlock(&im->ino_lock);
57539d787beSChao Yu 	return is_dirty;
57639d787beSChao Yu }
57739d787beSChao Yu 
5784d57b86dSChao Yu int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi)
579127e670aSJaegeuk Kim {
58067298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
581127e670aSJaegeuk Kim 	int err = 0;
582127e670aSJaegeuk Kim 
58367298804SChao Yu 	spin_lock(&im->ino_lock);
584cb78942bSJaegeuk Kim 
5851ecc0c5cSChao Yu 	if (time_to_inject(sbi, FAULT_ORPHAN)) {
586cb78942bSJaegeuk Kim 		spin_unlock(&im->ino_lock);
587c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_ORPHAN);
588cb78942bSJaegeuk Kim 		return -ENOSPC;
589cb78942bSJaegeuk Kim 	}
5907fa750a1SArnd Bergmann 
59167298804SChao Yu 	if (unlikely(im->ino_num >= sbi->max_orphans))
592127e670aSJaegeuk Kim 		err = -ENOSPC;
593cbd56e7dSJaegeuk Kim 	else
59467298804SChao Yu 		im->ino_num++;
59567298804SChao Yu 	spin_unlock(&im->ino_lock);
5960d47c1adSGu Zheng 
597127e670aSJaegeuk Kim 	return err;
598127e670aSJaegeuk Kim }
599127e670aSJaegeuk Kim 
6004d57b86dSChao Yu void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi)
601cbd56e7dSJaegeuk Kim {
60267298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
60367298804SChao Yu 
60467298804SChao Yu 	spin_lock(&im->ino_lock);
60567298804SChao Yu 	f2fs_bug_on(sbi, im->ino_num == 0);
60667298804SChao Yu 	im->ino_num--;
60767298804SChao Yu 	spin_unlock(&im->ino_lock);
608cbd56e7dSJaegeuk Kim }
609cbd56e7dSJaegeuk Kim 
6104d57b86dSChao Yu void f2fs_add_orphan_inode(struct inode *inode)
611127e670aSJaegeuk Kim {
61239efac41SJaegeuk Kim 	/* add new orphan ino entry into list */
61339d787beSChao Yu 	__add_ino_entry(F2FS_I_SB(inode), inode->i_ino, 0, ORPHAN_INO);
6144d57b86dSChao Yu 	f2fs_update_inode_page(inode);
615127e670aSJaegeuk Kim }
616127e670aSJaegeuk Kim 
6174d57b86dSChao Yu void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
618127e670aSJaegeuk Kim {
619953e6cc6SJaegeuk Kim 	/* remove orphan entry from orphan list */
6206451e041SJaegeuk Kim 	__remove_ino_entry(sbi, ino, ORPHAN_INO);
621127e670aSJaegeuk Kim }
622127e670aSJaegeuk Kim 
6238c14bfadSChao Yu static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
624127e670aSJaegeuk Kim {
6258c14bfadSChao Yu 	struct inode *inode;
6265905f9afSJaegeuk Kim 	struct node_info ni;
62776a45e3cSChao Yu 	int err;
6288c14bfadSChao Yu 
6295905f9afSJaegeuk Kim 	inode = f2fs_iget_retry(sbi->sb, ino);
6308c14bfadSChao Yu 	if (IS_ERR(inode)) {
6318c14bfadSChao Yu 		/*
6328c14bfadSChao Yu 		 * there should be a bug that we can't find the entry
6338c14bfadSChao Yu 		 * to orphan inode.
6348c14bfadSChao Yu 		 */
6358c14bfadSChao Yu 		f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT);
6368c14bfadSChao Yu 		return PTR_ERR(inode);
6378c14bfadSChao Yu 	}
6388c14bfadSChao Yu 
6390f9ec2a8SJaegeuk Kim 	err = dquot_initialize(inode);
640a515d12fSSheng Yong 	if (err) {
641a515d12fSSheng Yong 		iput(inode);
6420f9ec2a8SJaegeuk Kim 		goto err_out;
643a515d12fSSheng Yong 	}
6440f9ec2a8SJaegeuk Kim 
645127e670aSJaegeuk Kim 	clear_nlink(inode);
646127e670aSJaegeuk Kim 
647127e670aSJaegeuk Kim 	/* truncate all the data during iput */
648127e670aSJaegeuk Kim 	iput(inode);
6495905f9afSJaegeuk Kim 
6507735730dSChao Yu 	err = f2fs_get_node_info(sbi, ino, &ni);
6517735730dSChao Yu 	if (err)
6527735730dSChao Yu 		goto err_out;
6535905f9afSJaegeuk Kim 
6545905f9afSJaegeuk Kim 	/* ENOMEM was fully retried in f2fs_evict_inode. */
6555905f9afSJaegeuk Kim 	if (ni.blk_addr != NULL_ADDR) {
6560f9ec2a8SJaegeuk Kim 		err = -EIO;
6570f9ec2a8SJaegeuk Kim 		goto err_out;
6585905f9afSJaegeuk Kim 	}
6598c14bfadSChao Yu 	return 0;
6600f9ec2a8SJaegeuk Kim 
6610f9ec2a8SJaegeuk Kim err_out:
6620f9ec2a8SJaegeuk Kim 	set_sbi_flag(sbi, SBI_NEED_FSCK);
663dcbb4c10SJoe Perches 	f2fs_warn(sbi, "%s: orphan failed (ino=%x), run fsck to fix.",
6640f9ec2a8SJaegeuk Kim 		  __func__, ino);
6650f9ec2a8SJaegeuk Kim 	return err;
666127e670aSJaegeuk Kim }
667127e670aSJaegeuk Kim 
6684d57b86dSChao Yu int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
669127e670aSJaegeuk Kim {
6703c642985SWanpeng Li 	block_t start_blk, orphan_blocks, i, j;
6714b2414d0SChao Yu 	unsigned int s_flags = sbi->sb->s_flags;
6724b2414d0SChao Yu 	int err = 0;
673ea676733SJaegeuk Kim #ifdef CONFIG_QUOTA
674ea676733SJaegeuk Kim 	int quota_enabled;
675ea676733SJaegeuk Kim #endif
676127e670aSJaegeuk Kim 
677aaec2b1dSChao Yu 	if (!is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG))
6788c14bfadSChao Yu 		return 0;
679127e670aSJaegeuk Kim 
680b61af314SChao Yu 	if (bdev_read_only(sbi->sb->s_bdev)) {
681dcbb4c10SJoe Perches 		f2fs_info(sbi, "write access unavailable, skipping orphan cleanup");
682b61af314SChao Yu 		return 0;
683b61af314SChao Yu 	}
684b61af314SChao Yu 
6851751e8a6SLinus Torvalds 	if (s_flags & SB_RDONLY) {
686dcbb4c10SJoe Perches 		f2fs_info(sbi, "orphan cleanup on readonly fs");
6871751e8a6SLinus Torvalds 		sbi->sb->s_flags &= ~SB_RDONLY;
6884b2414d0SChao Yu 	}
6894b2414d0SChao Yu 
6904b2414d0SChao Yu #ifdef CONFIG_QUOTA
6914b2414d0SChao Yu 	/* Needed for iput() to work correctly and not trash data */
6921751e8a6SLinus Torvalds 	sbi->sb->s_flags |= SB_ACTIVE;
693ea676733SJaegeuk Kim 
69476cf05d7SSheng Yong 	/*
69576cf05d7SSheng Yong 	 * Turn on quotas which were not enabled for read-only mounts if
69676cf05d7SSheng Yong 	 * filesystem has quota feature, so that they are updated correctly.
69776cf05d7SSheng Yong 	 */
6981751e8a6SLinus Torvalds 	quota_enabled = f2fs_enable_quota_files(sbi, s_flags & SB_RDONLY);
6994b2414d0SChao Yu #endif
7004b2414d0SChao Yu 
70155141486SWanpeng Li 	start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
7023c642985SWanpeng Li 	orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
703127e670aSJaegeuk Kim 
7044d57b86dSChao Yu 	f2fs_ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true);
705662befdaSChao Yu 
7063c642985SWanpeng Li 	for (i = 0; i < orphan_blocks; i++) {
7077735730dSChao Yu 		struct page *page;
708127e670aSJaegeuk Kim 		struct f2fs_orphan_block *orphan_blk;
709127e670aSJaegeuk Kim 
7107735730dSChao Yu 		page = f2fs_get_meta_page(sbi, start_blk + i);
7117735730dSChao Yu 		if (IS_ERR(page)) {
7127735730dSChao Yu 			err = PTR_ERR(page);
7137735730dSChao Yu 			goto out;
7147735730dSChao Yu 		}
7157735730dSChao Yu 
716127e670aSJaegeuk Kim 		orphan_blk = (struct f2fs_orphan_block *)page_address(page);
717127e670aSJaegeuk Kim 		for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
718127e670aSJaegeuk Kim 			nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
7198c14bfadSChao Yu 			err = recover_orphan_inode(sbi, ino);
7208c14bfadSChao Yu 			if (err) {
7218c14bfadSChao Yu 				f2fs_put_page(page, 1);
7224b2414d0SChao Yu 				goto out;
7238c14bfadSChao Yu 			}
724127e670aSJaegeuk Kim 		}
725127e670aSJaegeuk Kim 		f2fs_put_page(page, 1);
726127e670aSJaegeuk Kim 	}
727127e670aSJaegeuk Kim 	/* clear Orphan Flag */
728aaec2b1dSChao Yu 	clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
7294b2414d0SChao Yu out:
7301378752bSChao Yu 	set_sbi_flag(sbi, SBI_IS_RECOVERED);
7311378752bSChao Yu 
7324b2414d0SChao Yu #ifdef CONFIG_QUOTA
7334b2414d0SChao Yu 	/* Turn quotas off */
734ea676733SJaegeuk Kim 	if (quota_enabled)
7354b2414d0SChao Yu 		f2fs_quota_off_umount(sbi->sb);
7364b2414d0SChao Yu #endif
7371751e8a6SLinus Torvalds 	sbi->sb->s_flags = s_flags; /* Restore SB_RDONLY status */
7384b2414d0SChao Yu 
7394b2414d0SChao Yu 	return err;
740127e670aSJaegeuk Kim }
741127e670aSJaegeuk Kim 
742127e670aSJaegeuk Kim static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
743127e670aSJaegeuk Kim {
744502c6e0bSGu Zheng 	struct list_head *head;
745127e670aSJaegeuk Kim 	struct f2fs_orphan_block *orphan_blk = NULL;
746127e670aSJaegeuk Kim 	unsigned int nentries = 0;
747bd936f84SChao Yu 	unsigned short index = 1;
7488c402946SJaegeuk Kim 	unsigned short orphan_blocks;
7494531929eSGu Zheng 	struct page *page = NULL;
7506451e041SJaegeuk Kim 	struct ino_entry *orphan = NULL;
75167298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
752127e670aSJaegeuk Kim 
75367298804SChao Yu 	orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num);
7548c402946SJaegeuk Kim 
755d6c67a4fSJaegeuk Kim 	/*
756d6c67a4fSJaegeuk Kim 	 * we don't need to do spin_lock(&im->ino_lock) here, since all the
757d6c67a4fSJaegeuk Kim 	 * orphan inode operations are covered under f2fs_lock_op().
758d6c67a4fSJaegeuk Kim 	 * And, spin_lock should be avoided due to page operations below.
759d6c67a4fSJaegeuk Kim 	 */
76067298804SChao Yu 	head = &im->ino_list;
761127e670aSJaegeuk Kim 
762127e670aSJaegeuk Kim 	/* loop for each orphan inode entry and write them in Jornal block */
763502c6e0bSGu Zheng 	list_for_each_entry(orphan, head, list) {
764502c6e0bSGu Zheng 		if (!page) {
7654d57b86dSChao Yu 			page = f2fs_grab_meta_page(sbi, start_blk++);
766502c6e0bSGu Zheng 			orphan_blk =
767502c6e0bSGu Zheng 				(struct f2fs_orphan_block *)page_address(page);
768502c6e0bSGu Zheng 			memset(orphan_blk, 0, sizeof(*orphan_blk));
769502c6e0bSGu Zheng 		}
770127e670aSJaegeuk Kim 
77136795567SGu Zheng 		orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
772127e670aSJaegeuk Kim 
77336795567SGu Zheng 		if (nentries == F2FS_ORPHANS_PER_BLOCK) {
774127e670aSJaegeuk Kim 			/*
775127e670aSJaegeuk Kim 			 * an orphan block is full of 1020 entries,
776127e670aSJaegeuk Kim 			 * then we need to flush current orphan blocks
777127e670aSJaegeuk Kim 			 * and bring another one in memory
778127e670aSJaegeuk Kim 			 */
779127e670aSJaegeuk Kim 			orphan_blk->blk_addr = cpu_to_le16(index);
780127e670aSJaegeuk Kim 			orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
781127e670aSJaegeuk Kim 			orphan_blk->entry_count = cpu_to_le32(nentries);
782127e670aSJaegeuk Kim 			set_page_dirty(page);
783127e670aSJaegeuk Kim 			f2fs_put_page(page, 1);
784127e670aSJaegeuk Kim 			index++;
785127e670aSJaegeuk Kim 			nentries = 0;
786127e670aSJaegeuk Kim 			page = NULL;
787127e670aSJaegeuk Kim 		}
788127e670aSJaegeuk Kim 	}
789127e670aSJaegeuk Kim 
790502c6e0bSGu Zheng 	if (page) {
791127e670aSJaegeuk Kim 		orphan_blk->blk_addr = cpu_to_le16(index);
792127e670aSJaegeuk Kim 		orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
793127e670aSJaegeuk Kim 		orphan_blk->entry_count = cpu_to_le32(nentries);
794127e670aSJaegeuk Kim 		set_page_dirty(page);
795127e670aSJaegeuk Kim 		f2fs_put_page(page, 1);
796502c6e0bSGu Zheng 	}
797127e670aSJaegeuk Kim }
798127e670aSJaegeuk Kim 
799d7eb8f1cSChao Yu static __u32 f2fs_checkpoint_chksum(struct f2fs_sb_info *sbi,
800d7eb8f1cSChao Yu 						struct f2fs_checkpoint *ckpt)
801d7eb8f1cSChao Yu {
802d7eb8f1cSChao Yu 	unsigned int chksum_ofs = le32_to_cpu(ckpt->checksum_offset);
803d7eb8f1cSChao Yu 	__u32 chksum;
804d7eb8f1cSChao Yu 
805d7eb8f1cSChao Yu 	chksum = f2fs_crc32(sbi, ckpt, chksum_ofs);
806d7eb8f1cSChao Yu 	if (chksum_ofs < CP_CHKSUM_OFFSET) {
807d7eb8f1cSChao Yu 		chksum_ofs += sizeof(chksum);
808d7eb8f1cSChao Yu 		chksum = f2fs_chksum(sbi, chksum, (__u8 *)ckpt + chksum_ofs,
809d7eb8f1cSChao Yu 						F2FS_BLKSIZE - chksum_ofs);
810d7eb8f1cSChao Yu 	}
811d7eb8f1cSChao Yu 	return chksum;
812d7eb8f1cSChao Yu }
813d7eb8f1cSChao Yu 
814fc0065adSTiezhu Yang static int get_checkpoint_version(struct f2fs_sb_info *sbi, block_t cp_addr,
815fc0065adSTiezhu Yang 		struct f2fs_checkpoint **cp_block, struct page **cp_page,
816fc0065adSTiezhu Yang 		unsigned long long *version)
817fc0065adSTiezhu Yang {
818fc0065adSTiezhu Yang 	size_t crc_offset = 0;
819d7eb8f1cSChao Yu 	__u32 crc;
820fc0065adSTiezhu Yang 
8214d57b86dSChao Yu 	*cp_page = f2fs_get_meta_page(sbi, cp_addr);
8227735730dSChao Yu 	if (IS_ERR(*cp_page))
8237735730dSChao Yu 		return PTR_ERR(*cp_page);
8247735730dSChao Yu 
825fc0065adSTiezhu Yang 	*cp_block = (struct f2fs_checkpoint *)page_address(*cp_page);
826fc0065adSTiezhu Yang 
827fc0065adSTiezhu Yang 	crc_offset = le32_to_cpu((*cp_block)->checksum_offset);
828d7eb8f1cSChao Yu 	if (crc_offset < CP_MIN_CHKSUM_OFFSET ||
829d7eb8f1cSChao Yu 			crc_offset > CP_CHKSUM_OFFSET) {
830d3f07c04SChao Yu 		f2fs_put_page(*cp_page, 1);
831dcbb4c10SJoe Perches 		f2fs_warn(sbi, "invalid crc_offset: %zu", crc_offset);
832fc0065adSTiezhu Yang 		return -EINVAL;
833fc0065adSTiezhu Yang 	}
834fc0065adSTiezhu Yang 
835d7eb8f1cSChao Yu 	crc = f2fs_checkpoint_chksum(sbi, *cp_block);
836d7eb8f1cSChao Yu 	if (crc != cur_cp_crc(*cp_block)) {
837d3f07c04SChao Yu 		f2fs_put_page(*cp_page, 1);
838dcbb4c10SJoe Perches 		f2fs_warn(sbi, "invalid crc value");
839fc0065adSTiezhu Yang 		return -EINVAL;
840fc0065adSTiezhu Yang 	}
841fc0065adSTiezhu Yang 
842fc0065adSTiezhu Yang 	*version = cur_cp_version(*cp_block);
843fc0065adSTiezhu Yang 	return 0;
844fc0065adSTiezhu Yang }
845fc0065adSTiezhu Yang 
846127e670aSJaegeuk Kim static struct page *validate_checkpoint(struct f2fs_sb_info *sbi,
847127e670aSJaegeuk Kim 				block_t cp_addr, unsigned long long *version)
848127e670aSJaegeuk Kim {
849fc0065adSTiezhu Yang 	struct page *cp_page_1 = NULL, *cp_page_2 = NULL;
850fc0065adSTiezhu Yang 	struct f2fs_checkpoint *cp_block = NULL;
851127e670aSJaegeuk Kim 	unsigned long long cur_version = 0, pre_version = 0;
852fc0065adSTiezhu Yang 	int err;
853127e670aSJaegeuk Kim 
854fc0065adSTiezhu Yang 	err = get_checkpoint_version(sbi, cp_addr, &cp_block,
855fc0065adSTiezhu Yang 					&cp_page_1, version);
856fc0065adSTiezhu Yang 	if (err)
857d3f07c04SChao Yu 		return NULL;
858c9b60788SChao Yu 
859c9b60788SChao Yu 	if (le32_to_cpu(cp_block->cp_pack_total_block_count) >
860c9b60788SChao Yu 					sbi->blocks_per_seg) {
861dcbb4c10SJoe Perches 		f2fs_warn(sbi, "invalid cp_pack_total_block_count:%u",
862c9b60788SChao Yu 			  le32_to_cpu(cp_block->cp_pack_total_block_count));
863d3f07c04SChao Yu 		goto invalid_cp;
864c9b60788SChao Yu 	}
865fc0065adSTiezhu Yang 	pre_version = *version;
866127e670aSJaegeuk Kim 
86725ca923bSJaegeuk Kim 	cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1;
868fc0065adSTiezhu Yang 	err = get_checkpoint_version(sbi, cp_addr, &cp_block,
869fc0065adSTiezhu Yang 					&cp_page_2, version);
870fc0065adSTiezhu Yang 	if (err)
871d3f07c04SChao Yu 		goto invalid_cp;
872fc0065adSTiezhu Yang 	cur_version = *version;
873127e670aSJaegeuk Kim 
874127e670aSJaegeuk Kim 	if (cur_version == pre_version) {
875127e670aSJaegeuk Kim 		*version = cur_version;
876127e670aSJaegeuk Kim 		f2fs_put_page(cp_page_2, 1);
877127e670aSJaegeuk Kim 		return cp_page_1;
878127e670aSJaegeuk Kim 	}
879127e670aSJaegeuk Kim 	f2fs_put_page(cp_page_2, 1);
880d3f07c04SChao Yu invalid_cp:
881127e670aSJaegeuk Kim 	f2fs_put_page(cp_page_1, 1);
882127e670aSJaegeuk Kim 	return NULL;
883127e670aSJaegeuk Kim }
884127e670aSJaegeuk Kim 
8854d57b86dSChao Yu int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi)
886127e670aSJaegeuk Kim {
887127e670aSJaegeuk Kim 	struct f2fs_checkpoint *cp_block;
888127e670aSJaegeuk Kim 	struct f2fs_super_block *fsb = sbi->raw_super;
889127e670aSJaegeuk Kim 	struct page *cp1, *cp2, *cur_page;
890127e670aSJaegeuk Kim 	unsigned long blk_size = sbi->blocksize;
891127e670aSJaegeuk Kim 	unsigned long long cp1_version = 0, cp2_version = 0;
892127e670aSJaegeuk Kim 	unsigned long long cp_start_blk_no;
89355141486SWanpeng Li 	unsigned int cp_blks = 1 + __cp_payload(sbi);
8941dbe4152SChangman Lee 	block_t cp_blk_no;
8951dbe4152SChangman Lee 	int i;
89610f966bbSChao Yu 	int err;
897127e670aSJaegeuk Kim 
898026f0507SKees Cook 	sbi->ckpt = f2fs_kzalloc(sbi, array_size(blk_size, cp_blks),
899026f0507SKees Cook 				 GFP_KERNEL);
900127e670aSJaegeuk Kim 	if (!sbi->ckpt)
901127e670aSJaegeuk Kim 		return -ENOMEM;
902127e670aSJaegeuk Kim 	/*
903127e670aSJaegeuk Kim 	 * Finding out valid cp block involves read both
904127e670aSJaegeuk Kim 	 * sets( cp pack 1 and cp pack 2)
905127e670aSJaegeuk Kim 	 */
906127e670aSJaegeuk Kim 	cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr);
907127e670aSJaegeuk Kim 	cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
908127e670aSJaegeuk Kim 
909127e670aSJaegeuk Kim 	/* The second checkpoint pack should start at the next segment */
910f9a4e6dfSJaegeuk Kim 	cp_start_blk_no += ((unsigned long long)1) <<
911f9a4e6dfSJaegeuk Kim 				le32_to_cpu(fsb->log_blocks_per_seg);
912127e670aSJaegeuk Kim 	cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
913127e670aSJaegeuk Kim 
914127e670aSJaegeuk Kim 	if (cp1 && cp2) {
915127e670aSJaegeuk Kim 		if (ver_after(cp2_version, cp1_version))
916127e670aSJaegeuk Kim 			cur_page = cp2;
917127e670aSJaegeuk Kim 		else
918127e670aSJaegeuk Kim 			cur_page = cp1;
919127e670aSJaegeuk Kim 	} else if (cp1) {
920127e670aSJaegeuk Kim 		cur_page = cp1;
921127e670aSJaegeuk Kim 	} else if (cp2) {
922127e670aSJaegeuk Kim 		cur_page = cp2;
923127e670aSJaegeuk Kim 	} else {
92410f966bbSChao Yu 		err = -EFSCORRUPTED;
925127e670aSJaegeuk Kim 		goto fail_no_cp;
926127e670aSJaegeuk Kim 	}
927127e670aSJaegeuk Kim 
928127e670aSJaegeuk Kim 	cp_block = (struct f2fs_checkpoint *)page_address(cur_page);
929127e670aSJaegeuk Kim 	memcpy(sbi->ckpt, cp_block, blk_size);
930127e670aSJaegeuk Kim 
9318508e44aSJaegeuk Kim 	if (cur_page == cp1)
9328508e44aSJaegeuk Kim 		sbi->cur_cp_pack = 1;
9338508e44aSJaegeuk Kim 	else
9348508e44aSJaegeuk Kim 		sbi->cur_cp_pack = 2;
935984ec63cSShawn Lin 
936e494c2f9SChao Yu 	/* Sanity checking of checkpoint */
93710f966bbSChao Yu 	if (f2fs_sanity_check_ckpt(sbi)) {
93810f966bbSChao Yu 		err = -EFSCORRUPTED;
939e494c2f9SChao Yu 		goto free_fail_no_cp;
94010f966bbSChao Yu 	}
941e494c2f9SChao Yu 
9421dbe4152SChangman Lee 	if (cp_blks <= 1)
9431dbe4152SChangman Lee 		goto done;
9441dbe4152SChangman Lee 
9451dbe4152SChangman Lee 	cp_blk_no = le32_to_cpu(fsb->cp_blkaddr);
9461dbe4152SChangman Lee 	if (cur_page == cp2)
9471dbe4152SChangman Lee 		cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg);
9481dbe4152SChangman Lee 
9491dbe4152SChangman Lee 	for (i = 1; i < cp_blks; i++) {
9501dbe4152SChangman Lee 		void *sit_bitmap_ptr;
9511dbe4152SChangman Lee 		unsigned char *ckpt = (unsigned char *)sbi->ckpt;
9521dbe4152SChangman Lee 
9534d57b86dSChao Yu 		cur_page = f2fs_get_meta_page(sbi, cp_blk_no + i);
95410f966bbSChao Yu 		if (IS_ERR(cur_page)) {
95510f966bbSChao Yu 			err = PTR_ERR(cur_page);
9567735730dSChao Yu 			goto free_fail_no_cp;
95710f966bbSChao Yu 		}
9581dbe4152SChangman Lee 		sit_bitmap_ptr = page_address(cur_page);
9591dbe4152SChangman Lee 		memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size);
9601dbe4152SChangman Lee 		f2fs_put_page(cur_page, 1);
9611dbe4152SChangman Lee 	}
9621dbe4152SChangman Lee done:
963127e670aSJaegeuk Kim 	f2fs_put_page(cp1, 1);
964127e670aSJaegeuk Kim 	f2fs_put_page(cp2, 1);
965127e670aSJaegeuk Kim 	return 0;
966127e670aSJaegeuk Kim 
967a2125ff7SJaegeuk Kim free_fail_no_cp:
968a2125ff7SJaegeuk Kim 	f2fs_put_page(cp1, 1);
969a2125ff7SJaegeuk Kim 	f2fs_put_page(cp2, 1);
970127e670aSJaegeuk Kim fail_no_cp:
9715222595dSJaegeuk Kim 	kvfree(sbi->ckpt);
97210f966bbSChao Yu 	return err;
973127e670aSJaegeuk Kim }
974127e670aSJaegeuk Kim 
975c227f912SChao Yu static void __add_dirty_inode(struct inode *inode, enum inode_type type)
976127e670aSJaegeuk Kim {
9774081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
978c227f912SChao Yu 	int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
9792d7b822aSChao Yu 
98091942321SJaegeuk Kim 	if (is_inode_flag_set(inode, flag))
9812710fd7eSChao Yu 		return;
9822d7b822aSChao Yu 
98391942321SJaegeuk Kim 	set_inode_flag(inode, flag);
98499f4b917SChao Yu 	if (!f2fs_is_volatile_file(inode))
98599f4b917SChao Yu 		list_add_tail(&F2FS_I(inode)->dirty_list,
98699f4b917SChao Yu 						&sbi->inode_list[type]);
98733fbd510SChao Yu 	stat_inc_dirty_inode(sbi, type);
9885deb8267SJaegeuk Kim }
9895deb8267SJaegeuk Kim 
990c227f912SChao Yu static void __remove_dirty_inode(struct inode *inode, enum inode_type type)
9916ad7609aSChao Yu {
992c227f912SChao Yu 	int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
9936ad7609aSChao Yu 
99491942321SJaegeuk Kim 	if (get_dirty_pages(inode) || !is_inode_flag_set(inode, flag))
9956ad7609aSChao Yu 		return;
9966ad7609aSChao Yu 
99791942321SJaegeuk Kim 	list_del_init(&F2FS_I(inode)->dirty_list);
99891942321SJaegeuk Kim 	clear_inode_flag(inode, flag);
99933fbd510SChao Yu 	stat_dec_dirty_inode(F2FS_I_SB(inode), type);
10006ad7609aSChao Yu }
10016ad7609aSChao Yu 
10024d57b86dSChao Yu void f2fs_update_dirty_page(struct inode *inode, struct page *page)
10035deb8267SJaegeuk Kim {
10044081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1005c227f912SChao Yu 	enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
10065deb8267SJaegeuk Kim 
10075ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
10085ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
1009127e670aSJaegeuk Kim 		return;
10107bd59381SGu Zheng 
1011c227f912SChao Yu 	spin_lock(&sbi->inode_lock[type]);
10121c4bf763SJaegeuk Kim 	if (type != FILE_INODE || test_opt(sbi, DATA_FLUSH))
1013c227f912SChao Yu 		__add_dirty_inode(inode, type);
1014b951a4ecSYunlei He 	inode_inc_dirty_pages(inode);
10151c4bf763SJaegeuk Kim 	spin_unlock(&sbi->inode_lock[type]);
10161c4bf763SJaegeuk Kim 
1017240a5915SChao Yu 	f2fs_set_page_private(page, 0);
10189e4ded3fSJaegeuk Kim 	f2fs_trace_pid(page);
10195deb8267SJaegeuk Kim }
1020127e670aSJaegeuk Kim 
10214d57b86dSChao Yu void f2fs_remove_dirty_inode(struct inode *inode)
1022127e670aSJaegeuk Kim {
10234081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1024c227f912SChao Yu 	enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
1025127e670aSJaegeuk Kim 
1026c227f912SChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
1027c227f912SChao Yu 			!S_ISLNK(inode->i_mode))
1028127e670aSJaegeuk Kim 		return;
1029127e670aSJaegeuk Kim 
103010aa97c3SJaegeuk Kim 	if (type == FILE_INODE && !test_opt(sbi, DATA_FLUSH))
103110aa97c3SJaegeuk Kim 		return;
103210aa97c3SJaegeuk Kim 
1033c227f912SChao Yu 	spin_lock(&sbi->inode_lock[type]);
1034c227f912SChao Yu 	__remove_dirty_inode(inode, type);
1035c227f912SChao Yu 	spin_unlock(&sbi->inode_lock[type]);
1036afc3eda2SJaegeuk Kim }
103774d0b917SJaegeuk Kim 
10384d57b86dSChao Yu int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type)
1039127e670aSJaegeuk Kim {
1040ce3b7d80SGu Zheng 	struct list_head *head;
1041127e670aSJaegeuk Kim 	struct inode *inode;
10422710fd7eSChao Yu 	struct f2fs_inode_info *fi;
10434cf18537SChao Yu 	bool is_dir = (type == DIR_INODE);
10444db08d01SJaegeuk Kim 	unsigned long ino = 0;
10454cf18537SChao Yu 
10464cf18537SChao Yu 	trace_f2fs_sync_dirty_inodes_enter(sbi->sb, is_dir,
10474cf18537SChao Yu 				get_pages(sbi, is_dir ?
10484cf18537SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
1049127e670aSJaegeuk Kim retry:
1050af41d3eeSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
10516d5a1495SChao Yu 		return -EIO;
1052af41d3eeSJaegeuk Kim 
1053c227f912SChao Yu 	spin_lock(&sbi->inode_lock[type]);
1054ce3b7d80SGu Zheng 
1055c227f912SChao Yu 	head = &sbi->inode_list[type];
1056127e670aSJaegeuk Kim 	if (list_empty(head)) {
1057c227f912SChao Yu 		spin_unlock(&sbi->inode_lock[type]);
10584cf18537SChao Yu 		trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir,
10594cf18537SChao Yu 				get_pages(sbi, is_dir ?
10604cf18537SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
10616d5a1495SChao Yu 		return 0;
1062127e670aSJaegeuk Kim 	}
1063939afa94SChao Yu 	fi = list_first_entry(head, struct f2fs_inode_info, dirty_list);
10642710fd7eSChao Yu 	inode = igrab(&fi->vfs_inode);
1065c227f912SChao Yu 	spin_unlock(&sbi->inode_lock[type]);
1066127e670aSJaegeuk Kim 	if (inode) {
10674db08d01SJaegeuk Kim 		unsigned long cur_ino = inode->i_ino;
10684db08d01SJaegeuk Kim 
1069b0af6d49SChao Yu 		F2FS_I(inode)->cp_task = current;
1070b0af6d49SChao Yu 
107187d6f890SJaegeuk Kim 		filemap_fdatawrite(inode->i_mapping);
1072b0af6d49SChao Yu 
1073b0af6d49SChao Yu 		F2FS_I(inode)->cp_task = NULL;
1074b0af6d49SChao Yu 
1075127e670aSJaegeuk Kim 		iput(inode);
10764db08d01SJaegeuk Kim 		/* We need to give cpu to another writers. */
10772a63531aSYunlei He 		if (ino == cur_ino)
10784db08d01SJaegeuk Kim 			cond_resched();
10792a63531aSYunlei He 		else
10804db08d01SJaegeuk Kim 			ino = cur_ino;
1081127e670aSJaegeuk Kim 	} else {
1082127e670aSJaegeuk Kim 		/*
1083127e670aSJaegeuk Kim 		 * We should submit bio, since it exists several
1084127e670aSJaegeuk Kim 		 * wribacking dentry pages in the freeing inode.
1085127e670aSJaegeuk Kim 		 */
1086b9109b0eSJaegeuk Kim 		f2fs_submit_merged_write(sbi, DATA);
10877ecebe5eSSebastian Andrzej Siewior 		cond_resched();
1088127e670aSJaegeuk Kim 	}
1089127e670aSJaegeuk Kim 	goto retry;
1090127e670aSJaegeuk Kim }
1091127e670aSJaegeuk Kim 
10920f18b462SJaegeuk Kim int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi)
10930f18b462SJaegeuk Kim {
10940f18b462SJaegeuk Kim 	struct list_head *head = &sbi->inode_list[DIRTY_META];
10950f18b462SJaegeuk Kim 	struct inode *inode;
10960f18b462SJaegeuk Kim 	struct f2fs_inode_info *fi;
10970f18b462SJaegeuk Kim 	s64 total = get_pages(sbi, F2FS_DIRTY_IMETA);
10980f18b462SJaegeuk Kim 
10990f18b462SJaegeuk Kim 	while (total--) {
11000f18b462SJaegeuk Kim 		if (unlikely(f2fs_cp_error(sbi)))
11010f18b462SJaegeuk Kim 			return -EIO;
11020f18b462SJaegeuk Kim 
11030f18b462SJaegeuk Kim 		spin_lock(&sbi->inode_lock[DIRTY_META]);
11040f18b462SJaegeuk Kim 		if (list_empty(head)) {
11050f18b462SJaegeuk Kim 			spin_unlock(&sbi->inode_lock[DIRTY_META]);
11060f18b462SJaegeuk Kim 			return 0;
11070f18b462SJaegeuk Kim 		}
1108939afa94SChao Yu 		fi = list_first_entry(head, struct f2fs_inode_info,
11090f18b462SJaegeuk Kim 							gdirty_list);
11100f18b462SJaegeuk Kim 		inode = igrab(&fi->vfs_inode);
11110f18b462SJaegeuk Kim 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
11120f18b462SJaegeuk Kim 		if (inode) {
111318340edcSJaegeuk Kim 			sync_inode_metadata(inode, 0);
111418340edcSJaegeuk Kim 
111518340edcSJaegeuk Kim 			/* it's on eviction */
111618340edcSJaegeuk Kim 			if (is_inode_flag_set(inode, FI_DIRTY_INODE))
11174d57b86dSChao Yu 				f2fs_update_inode_page(inode);
11180f18b462SJaegeuk Kim 			iput(inode);
11190f18b462SJaegeuk Kim 		}
1120dee668c1SChao Yu 	}
11210f18b462SJaegeuk Kim 	return 0;
11220f18b462SJaegeuk Kim }
11230f18b462SJaegeuk Kim 
112459c9081bSYunlei He static void __prepare_cp_block(struct f2fs_sb_info *sbi)
112559c9081bSYunlei He {
112659c9081bSYunlei He 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
112759c9081bSYunlei He 	struct f2fs_nm_info *nm_i = NM_I(sbi);
112859c9081bSYunlei He 	nid_t last_nid = nm_i->next_scan_nid;
112959c9081bSYunlei He 
113059c9081bSYunlei He 	next_free_nid(sbi, &last_nid);
113159c9081bSYunlei He 	ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
113259c9081bSYunlei He 	ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi));
113359c9081bSYunlei He 	ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi));
113459c9081bSYunlei He 	ckpt->next_free_nid = cpu_to_le32(last_nid);
113559c9081bSYunlei He }
113659c9081bSYunlei He 
1137af033b2aSChao Yu static bool __need_flush_quota(struct f2fs_sb_info *sbi)
1138af033b2aSChao Yu {
1139db6ec53bSJaegeuk Kim 	bool ret = false;
1140db6ec53bSJaegeuk Kim 
1141af033b2aSChao Yu 	if (!is_journalled_quota(sbi))
1142af033b2aSChao Yu 		return false;
1143db6ec53bSJaegeuk Kim 
1144db6ec53bSJaegeuk Kim 	down_write(&sbi->quota_sem);
1145db6ec53bSJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH)) {
1146db6ec53bSJaegeuk Kim 		ret = false;
1147db6ec53bSJaegeuk Kim 	} else if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR)) {
1148db6ec53bSJaegeuk Kim 		ret = false;
1149db6ec53bSJaegeuk Kim 	} else if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_FLUSH)) {
1150db6ec53bSJaegeuk Kim 		clear_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH);
1151db6ec53bSJaegeuk Kim 		ret = true;
1152db6ec53bSJaegeuk Kim 	} else if (get_pages(sbi, F2FS_DIRTY_QDATA)) {
1153db6ec53bSJaegeuk Kim 		ret = true;
1154db6ec53bSJaegeuk Kim 	}
1155db6ec53bSJaegeuk Kim 	up_write(&sbi->quota_sem);
1156db6ec53bSJaegeuk Kim 	return ret;
1157af033b2aSChao Yu }
1158af033b2aSChao Yu 
11590a8165d7SJaegeuk Kim /*
1160127e670aSJaegeuk Kim  * Freeze all the FS-operations for checkpoint.
1161127e670aSJaegeuk Kim  */
1162cf779cabSJaegeuk Kim static int block_operations(struct f2fs_sb_info *sbi)
1163127e670aSJaegeuk Kim {
1164127e670aSJaegeuk Kim 	struct writeback_control wbc = {
1165127e670aSJaegeuk Kim 		.sync_mode = WB_SYNC_ALL,
1166127e670aSJaegeuk Kim 		.nr_to_write = LONG_MAX,
1167127e670aSJaegeuk Kim 		.for_reclaim = 0,
1168127e670aSJaegeuk Kim 	};
1169af033b2aSChao Yu 	int err = 0, cnt = 0;
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)
1198*1f5f11a3SJaegeuk Kim 			return err;
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)
1214*1f5f11a3SJaegeuk Kim 			return err;
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);
1230*1f5f11a3SJaegeuk Kim 			return err;
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 	return err;
1243127e670aSJaegeuk Kim }
1244127e670aSJaegeuk Kim 
1245127e670aSJaegeuk Kim static void unblock_operations(struct f2fs_sb_info *sbi)
1246127e670aSJaegeuk Kim {
1247b3582c68SChao Yu 	up_write(&sbi->node_write);
1248e479556bSGu Zheng 	f2fs_unlock_all(sbi);
1249127e670aSJaegeuk Kim }
1250127e670aSJaegeuk Kim 
1251bf22c3ccSSahitya Tummala void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type)
1252fb51b5efSChangman Lee {
1253fb51b5efSChangman Lee 	DEFINE_WAIT(wait);
1254fb51b5efSChangman Lee 
1255fb51b5efSChangman Lee 	for (;;) {
1256fb51b5efSChangman Lee 		prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
1257fb51b5efSChangman Lee 
1258bf22c3ccSSahitya Tummala 		if (!get_pages(sbi, type))
1259fb51b5efSChangman Lee 			break;
1260fb51b5efSChangman Lee 
1261af697c0fSJaegeuk Kim 		if (unlikely(f2fs_cp_error(sbi)))
1262af697c0fSJaegeuk Kim 			break;
1263af697c0fSJaegeuk Kim 
12645df7731fSChao Yu 		io_schedule_timeout(DEFAULT_IO_TIMEOUT);
1265fb51b5efSChangman Lee 	}
1266fb51b5efSChangman Lee 	finish_wait(&sbi->cp_wait, &wait);
1267fb51b5efSChangman Lee }
1268fb51b5efSChangman Lee 
1269e4c5d848SJaegeuk Kim static void update_ckpt_flags(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1270e4c5d848SJaegeuk Kim {
1271e4c5d848SJaegeuk Kim 	unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num;
1272e4c5d848SJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1273d1aa2453SChao Yu 	unsigned long flags;
1274e4c5d848SJaegeuk Kim 
1275d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1276e4c5d848SJaegeuk Kim 
1277c473f1a9SChao Yu 	if ((cpc->reason & CP_UMOUNT) &&
127810047f53SKinglong Mee 			le32_to_cpu(ckpt->cp_pack_total_block_count) >
127922ad0b6aSJaegeuk Kim 			sbi->blocks_per_seg - NM_I(sbi)->nat_bits_blocks)
128022ad0b6aSJaegeuk Kim 		disable_nat_bits(sbi, false);
128122ad0b6aSJaegeuk Kim 
12821f43e2adSChao Yu 	if (cpc->reason & CP_TRIMMED)
12831f43e2adSChao Yu 		__set_ckpt_flags(ckpt, CP_TRIMMED_FLAG);
1284cd36d7a1SChao Yu 	else
1285cd36d7a1SChao Yu 		__clear_ckpt_flags(ckpt, CP_TRIMMED_FLAG);
12861f43e2adSChao Yu 
1287c473f1a9SChao Yu 	if (cpc->reason & CP_UMOUNT)
1288e4c5d848SJaegeuk Kim 		__set_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1289e4c5d848SJaegeuk Kim 	else
1290e4c5d848SJaegeuk Kim 		__clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1291e4c5d848SJaegeuk Kim 
1292c473f1a9SChao Yu 	if (cpc->reason & CP_FASTBOOT)
1293e4c5d848SJaegeuk Kim 		__set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1294e4c5d848SJaegeuk Kim 	else
1295e4c5d848SJaegeuk Kim 		__clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1296e4c5d848SJaegeuk Kim 
1297e4c5d848SJaegeuk Kim 	if (orphan_num)
1298e4c5d848SJaegeuk Kim 		__set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1299e4c5d848SJaegeuk Kim 	else
1300e4c5d848SJaegeuk Kim 		__clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1301e4c5d848SJaegeuk Kim 
1302c84ef3c5SSahitya Tummala 	if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
1303e4c5d848SJaegeuk Kim 		__set_ckpt_flags(ckpt, CP_FSCK_FLAG);
1304e4c5d848SJaegeuk Kim 
1305c84ef3c5SSahitya Tummala 	if (is_sbi_flag_set(sbi, SBI_IS_RESIZEFS))
1306c84ef3c5SSahitya Tummala 		__set_ckpt_flags(ckpt, CP_RESIZEFS_FLAG);
1307c84ef3c5SSahitya Tummala 	else
1308c84ef3c5SSahitya Tummala 		__clear_ckpt_flags(ckpt, CP_RESIZEFS_FLAG);
1309c84ef3c5SSahitya Tummala 
13104354994fSDaniel Rosenberg 	if (is_sbi_flag_set(sbi, SBI_CP_DISABLED))
13114354994fSDaniel Rosenberg 		__set_ckpt_flags(ckpt, CP_DISABLED_FLAG);
13124354994fSDaniel Rosenberg 	else
13134354994fSDaniel Rosenberg 		__clear_ckpt_flags(ckpt, CP_DISABLED_FLAG);
13144354994fSDaniel Rosenberg 
1315db610a64SJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_CP_DISABLED_QUICK))
1316db610a64SJaegeuk Kim 		__set_ckpt_flags(ckpt, CP_DISABLED_QUICK_FLAG);
1317db610a64SJaegeuk Kim 	else
1318db610a64SJaegeuk Kim 		__clear_ckpt_flags(ckpt, CP_DISABLED_QUICK_FLAG);
1319db610a64SJaegeuk Kim 
1320af033b2aSChao Yu 	if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH))
1321af033b2aSChao Yu 		__set_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG);
1322bc88ac96SJaegeuk Kim 	else
1323bc88ac96SJaegeuk Kim 		__clear_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG);
1324af033b2aSChao Yu 
1325af033b2aSChao Yu 	if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR))
1326af033b2aSChao Yu 		__set_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG);
1327af033b2aSChao Yu 
1328e4c5d848SJaegeuk Kim 	/* set this flag to activate crc|cp_ver for recovery */
1329e4c5d848SJaegeuk Kim 	__set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG);
1330f2367923SJaegeuk Kim 	__clear_ckpt_flags(ckpt, CP_NOCRC_RECOVERY_FLAG);
1331e4c5d848SJaegeuk Kim 
1332d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1333e4c5d848SJaegeuk Kim }
1334e4c5d848SJaegeuk Kim 
133546706d59SGao Xiang static void commit_checkpoint(struct f2fs_sb_info *sbi,
133646706d59SGao Xiang 	void *src, block_t blk_addr)
133746706d59SGao Xiang {
133846706d59SGao Xiang 	struct writeback_control wbc = {
133946706d59SGao Xiang 		.for_reclaim = 0,
134046706d59SGao Xiang 	};
134146706d59SGao Xiang 
134246706d59SGao Xiang 	/*
134346706d59SGao Xiang 	 * pagevec_lookup_tag and lock_page again will take
13444d57b86dSChao Yu 	 * some extra time. Therefore, f2fs_update_meta_pages and
13454d57b86dSChao Yu 	 * f2fs_sync_meta_pages are combined in this function.
134646706d59SGao Xiang 	 */
13474d57b86dSChao Yu 	struct page *page = f2fs_grab_meta_page(sbi, blk_addr);
134846706d59SGao Xiang 	int err;
134946706d59SGao Xiang 
1350bae0ee7aSChao Yu 	f2fs_wait_on_page_writeback(page, META, true, true);
13518d64d365SChao Yu 
13528d64d365SChao Yu 	memcpy(page_address(page), src, PAGE_SIZE);
13538d64d365SChao Yu 
13548d64d365SChao Yu 	set_page_dirty(page);
135546706d59SGao Xiang 	if (unlikely(!clear_page_dirty_for_io(page)))
135646706d59SGao Xiang 		f2fs_bug_on(sbi, 1);
135746706d59SGao Xiang 
135846706d59SGao Xiang 	/* writeout cp pack 2 page */
135946706d59SGao Xiang 	err = __f2fs_write_meta_page(page, &wbc, FS_CP_META_IO);
1360af697c0fSJaegeuk Kim 	if (unlikely(err && f2fs_cp_error(sbi))) {
1361af697c0fSJaegeuk Kim 		f2fs_put_page(page, 1);
1362af697c0fSJaegeuk Kim 		return;
1363af697c0fSJaegeuk Kim 	}
136446706d59SGao Xiang 
1365af697c0fSJaegeuk Kim 	f2fs_bug_on(sbi, err);
136646706d59SGao Xiang 	f2fs_put_page(page, 0);
136746706d59SGao Xiang 
136846706d59SGao Xiang 	/* submit checkpoint (with barrier if NOBARRIER is not set) */
136946706d59SGao Xiang 	f2fs_submit_merged_write(sbi, META_FLUSH);
137046706d59SGao Xiang }
137146706d59SGao Xiang 
1372c34f42e2SChao Yu static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1373127e670aSJaegeuk Kim {
1374127e670aSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
137577041823SHuang Ying 	struct f2fs_nm_info *nm_i = NM_I(sbi);
1376d1aa2453SChao Yu 	unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num, flags;
1377127e670aSJaegeuk Kim 	block_t start_blk;
1378127e670aSJaegeuk Kim 	unsigned int data_sum_blocks, orphan_blocks;
13797e586fa0SJaegeuk Kim 	__u32 crc32 = 0;
1380127e670aSJaegeuk Kim 	int i;
138155141486SWanpeng Li 	int cp_payload_blks = __cp_payload(sbi);
13828f1dbbbbSShuoran Liu 	struct super_block *sb = sbi->sb;
13838f1dbbbbSShuoran Liu 	struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
13848f1dbbbbSShuoran Liu 	u64 kbytes_written;
13851228b482SChao Yu 	int err;
1386127e670aSJaegeuk Kim 
1387127e670aSJaegeuk Kim 	/* Flush all the NAT/SIT pages */
13884d57b86dSChao Yu 	f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
1389127e670aSJaegeuk Kim 
13907a88ddb5SChao Yu 	/* start to update checkpoint, cp ver is already updated previously */
1391a1f72ac2SChao Yu 	ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi, true));
1392127e670aSJaegeuk Kim 	ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
1393b5b82205SChao Yu 	for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
1394127e670aSJaegeuk Kim 		ckpt->cur_node_segno[i] =
1395127e670aSJaegeuk Kim 			cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE));
1396127e670aSJaegeuk Kim 		ckpt->cur_node_blkoff[i] =
1397127e670aSJaegeuk Kim 			cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE));
1398127e670aSJaegeuk Kim 		ckpt->alloc_type[i + CURSEG_HOT_NODE] =
1399127e670aSJaegeuk Kim 				curseg_alloc_type(sbi, i + CURSEG_HOT_NODE);
1400127e670aSJaegeuk Kim 	}
1401b5b82205SChao Yu 	for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
1402127e670aSJaegeuk Kim 		ckpt->cur_data_segno[i] =
1403127e670aSJaegeuk Kim 			cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA));
1404127e670aSJaegeuk Kim 		ckpt->cur_data_blkoff[i] =
1405127e670aSJaegeuk Kim 			cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA));
1406127e670aSJaegeuk Kim 		ckpt->alloc_type[i + CURSEG_HOT_DATA] =
1407127e670aSJaegeuk Kim 				curseg_alloc_type(sbi, i + CURSEG_HOT_DATA);
1408127e670aSJaegeuk Kim 	}
1409127e670aSJaegeuk Kim 
1410127e670aSJaegeuk Kim 	/* 2 cp  + n data seg summary + orphan inode blocks */
14114d57b86dSChao Yu 	data_sum_blocks = f2fs_npages_for_summary_flush(sbi, false);
1412d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1413b5b82205SChao Yu 	if (data_sum_blocks < NR_CURSEG_DATA_TYPE)
1414aaec2b1dSChao Yu 		__set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
1415127e670aSJaegeuk Kim 	else
1416aaec2b1dSChao Yu 		__clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
1417d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1418127e670aSJaegeuk Kim 
141967298804SChao Yu 	orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num);
14201dbe4152SChangman Lee 	ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks +
14211dbe4152SChangman Lee 			orphan_blocks);
1422127e670aSJaegeuk Kim 
1423119ee914SJaegeuk Kim 	if (__remain_node_summaries(cpc->reason))
1424b5b82205SChao Yu 		ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS+
14251dbe4152SChangman Lee 				cp_payload_blks + data_sum_blocks +
14261dbe4152SChangman Lee 				orphan_blocks + NR_CURSEG_NODE_TYPE);
1427119ee914SJaegeuk Kim 	else
1428b5b82205SChao Yu 		ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
14291dbe4152SChangman Lee 				cp_payload_blks + data_sum_blocks +
14301dbe4152SChangman Lee 				orphan_blocks);
1431119ee914SJaegeuk Kim 
1432e4c5d848SJaegeuk Kim 	/* update ckpt flag for checkpoint */
1433e4c5d848SJaegeuk Kim 	update_ckpt_flags(sbi, cpc);
1434a468f0efSJaegeuk Kim 
1435127e670aSJaegeuk Kim 	/* update SIT/NAT bitmap */
1436127e670aSJaegeuk Kim 	get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
1437127e670aSJaegeuk Kim 	get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
1438127e670aSJaegeuk Kim 
1439d7eb8f1cSChao Yu 	crc32 = f2fs_checkpoint_chksum(sbi, ckpt);
14407e586fa0SJaegeuk Kim 	*((__le32 *)((unsigned char *)ckpt +
14417e586fa0SJaegeuk Kim 				le32_to_cpu(ckpt->checksum_offset)))
1442127e670aSJaegeuk Kim 				= cpu_to_le32(crc32);
1443127e670aSJaegeuk Kim 
14448508e44aSJaegeuk Kim 	start_blk = __start_cp_next_addr(sbi);
1445127e670aSJaegeuk Kim 
144622ad0b6aSJaegeuk Kim 	/* write nat bits */
144722ad0b6aSJaegeuk Kim 	if (enabled_nat_bits(sbi, cpc)) {
144822ad0b6aSJaegeuk Kim 		__u64 cp_ver = cur_cp_version(ckpt);
144922ad0b6aSJaegeuk Kim 		block_t blk;
145022ad0b6aSJaegeuk Kim 
145122ad0b6aSJaegeuk Kim 		cp_ver |= ((__u64)crc32 << 32);
145222ad0b6aSJaegeuk Kim 		*(__le64 *)nm_i->nat_bits = cpu_to_le64(cp_ver);
145322ad0b6aSJaegeuk Kim 
145422ad0b6aSJaegeuk Kim 		blk = start_blk + sbi->blocks_per_seg - nm_i->nat_bits_blocks;
145522ad0b6aSJaegeuk Kim 		for (i = 0; i < nm_i->nat_bits_blocks; i++)
14564d57b86dSChao Yu 			f2fs_update_meta_page(sbi, nm_i->nat_bits +
145722ad0b6aSJaegeuk Kim 					(i << F2FS_BLKSIZE_BITS), blk + i);
145822ad0b6aSJaegeuk Kim 	}
145922ad0b6aSJaegeuk Kim 
1460127e670aSJaegeuk Kim 	/* write out checkpoint buffer at block 0 */
14614d57b86dSChao Yu 	f2fs_update_meta_page(sbi, ckpt, start_blk++);
1462127e670aSJaegeuk Kim 
1463381722d2SChao Yu 	for (i = 1; i < 1 + cp_payload_blks; i++)
14644d57b86dSChao Yu 		f2fs_update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE,
1465381722d2SChao Yu 							start_blk++);
14661dbe4152SChangman Lee 
146767298804SChao Yu 	if (orphan_num) {
1468127e670aSJaegeuk Kim 		write_orphan_inodes(sbi, start_blk);
1469127e670aSJaegeuk Kim 		start_blk += orphan_blocks;
1470127e670aSJaegeuk Kim 	}
1471127e670aSJaegeuk Kim 
14724d57b86dSChao Yu 	f2fs_write_data_summaries(sbi, start_blk);
1473127e670aSJaegeuk Kim 	start_blk += data_sum_blocks;
14748f1dbbbbSShuoran Liu 
14758f1dbbbbSShuoran Liu 	/* Record write statistics in the hot node summary */
14768f1dbbbbSShuoran Liu 	kbytes_written = sbi->kbytes_written;
14778f1dbbbbSShuoran Liu 	if (sb->s_bdev->bd_part)
14788f1dbbbbSShuoran Liu 		kbytes_written += BD_PART_WRITTEN(sbi);
14798f1dbbbbSShuoran Liu 
1480b7ad7512SChao Yu 	seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written);
14818f1dbbbbSShuoran Liu 
1482119ee914SJaegeuk Kim 	if (__remain_node_summaries(cpc->reason)) {
14834d57b86dSChao Yu 		f2fs_write_node_summaries(sbi, start_blk);
1484127e670aSJaegeuk Kim 		start_blk += NR_CURSEG_NODE_TYPE;
1485127e670aSJaegeuk Kim 	}
1486127e670aSJaegeuk Kim 
148746706d59SGao Xiang 	/* update user_block_counts */
148846706d59SGao Xiang 	sbi->last_valid_block_count = sbi->total_valid_block_count;
148946706d59SGao Xiang 	percpu_counter_set(&sbi->alloc_valid_block_count, 0);
1490127e670aSJaegeuk Kim 
149146706d59SGao Xiang 	/* Here, we have one bio having CP pack except cp pack 2 page */
14924d57b86dSChao Yu 	f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
1493bf22c3ccSSahitya Tummala 	/* Wait for all dirty meta pages to be submitted for IO */
1494bf22c3ccSSahitya Tummala 	f2fs_wait_on_all_pages(sbi, F2FS_DIRTY_META);
149546706d59SGao Xiang 
149646706d59SGao Xiang 	/* wait for previous submitted meta pages writeback */
1497bf22c3ccSSahitya Tummala 	f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
1498127e670aSJaegeuk Kim 
149946706d59SGao Xiang 	/* flush all device cache */
150046706d59SGao Xiang 	err = f2fs_flush_device_cache(sbi);
150146706d59SGao Xiang 	if (err)
150246706d59SGao Xiang 		return err;
1503127e670aSJaegeuk Kim 
150446706d59SGao Xiang 	/* barrier and flush checkpoint cp pack 2 page if it can */
150546706d59SGao Xiang 	commit_checkpoint(sbi, ckpt, start_blk);
1506bf22c3ccSSahitya Tummala 	f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
15076a8f8ca5SJaegeuk Kim 
150818767e62SChao Yu 	/*
1509542989b6SEric Biggers 	 * invalidate intermediate page cache borrowed from meta inode which are
1510542989b6SEric Biggers 	 * used for migration of encrypted or verity inode's blocks.
151118767e62SChao Yu 	 */
1512542989b6SEric Biggers 	if (f2fs_sb_has_encrypt(sbi) || f2fs_sb_has_verity(sbi))
151318767e62SChao Yu 		invalidate_mapping_pages(META_MAPPING(sbi),
151418767e62SChao Yu 				MAIN_BLKADDR(sbi), MAX_BLKADDR(sbi) - 1);
151518767e62SChao Yu 
15164d57b86dSChao Yu 	f2fs_release_ino_entry(sbi, false);
1517cf779cabSJaegeuk Kim 
151850fa53ecSChao Yu 	f2fs_reset_fsync_node_info(sbi);
151950fa53ecSChao Yu 
1520caf0047eSChao Yu 	clear_sbi_flag(sbi, SBI_IS_DIRTY);
1521bbf156f7SJaegeuk Kim 	clear_sbi_flag(sbi, SBI_NEED_CP);
1522af033b2aSChao Yu 	clear_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH);
1523c9c8ed50SChao Yu 
1524c9c8ed50SChao Yu 	spin_lock(&sbi->stat_lock);
15254354994fSDaniel Rosenberg 	sbi->unusable_block_count = 0;
1526c9c8ed50SChao Yu 	spin_unlock(&sbi->stat_lock);
1527c9c8ed50SChao Yu 
15288508e44aSJaegeuk Kim 	__set_cp_next_pack(sbi);
1529c34f42e2SChao Yu 
1530c2a080aeSChao Yu 	/*
1531c2a080aeSChao Yu 	 * redirty superblock if metadata like node page or inode cache is
1532c2a080aeSChao Yu 	 * updated during writing checkpoint.
1533c2a080aeSChao Yu 	 */
1534c2a080aeSChao Yu 	if (get_pages(sbi, F2FS_DIRTY_NODES) ||
1535c2a080aeSChao Yu 			get_pages(sbi, F2FS_DIRTY_IMETA))
1536c2a080aeSChao Yu 		set_sbi_flag(sbi, SBI_IS_DIRTY);
1537c2a080aeSChao Yu 
1538c2a080aeSChao Yu 	f2fs_bug_on(sbi, get_pages(sbi, F2FS_DIRTY_DENTS));
1539c2a080aeSChao Yu 
1540af697c0fSJaegeuk Kim 	return unlikely(f2fs_cp_error(sbi)) ? -EIO : 0;
1541127e670aSJaegeuk Kim }
1542127e670aSJaegeuk Kim 
15434d57b86dSChao Yu int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1544127e670aSJaegeuk Kim {
1545127e670aSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1546127e670aSJaegeuk Kim 	unsigned long long ckpt_ver;
1547c34f42e2SChao Yu 	int err = 0;
1548127e670aSJaegeuk Kim 
1549f5a131bbSChao Yu 	if (f2fs_readonly(sbi->sb) || f2fs_hw_is_readonly(sbi))
1550f5a131bbSChao Yu 		return -EROFS;
1551f5a131bbSChao Yu 
15524354994fSDaniel Rosenberg 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
15534354994fSDaniel Rosenberg 		if (cpc->reason != CP_PAUSE)
15544354994fSDaniel Rosenberg 			return 0;
1555dcbb4c10SJoe Perches 		f2fs_warn(sbi, "Start checkpoint disabled!");
15564354994fSDaniel Rosenberg 	}
1557127e670aSJaegeuk Kim 	mutex_lock(&sbi->cp_mutex);
15588501017eSJaegeuk Kim 
1559caf0047eSChao Yu 	if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) &&
1560c473f1a9SChao Yu 		((cpc->reason & CP_FASTBOOT) || (cpc->reason & CP_SYNC) ||
1561c473f1a9SChao Yu 		((cpc->reason & CP_DISCARD) && !sbi->discard_blks)))
15628501017eSJaegeuk Kim 		goto out;
1563c34f42e2SChao Yu 	if (unlikely(f2fs_cp_error(sbi))) {
1564c34f42e2SChao Yu 		err = -EIO;
1565cf779cabSJaegeuk Kim 		goto out;
1566c34f42e2SChao Yu 	}
15672bda542dSWanpeng Li 
15682bda542dSWanpeng Li 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops");
15692bda542dSWanpeng Li 
1570c34f42e2SChao Yu 	err = block_operations(sbi);
1571c34f42e2SChao Yu 	if (err)
1572cf779cabSJaegeuk Kim 		goto out;
1573127e670aSJaegeuk Kim 
157475ab4cb8SJaegeuk Kim 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops");
15752af4bd6cSNamjae Jeon 
1576b9109b0eSJaegeuk Kim 	f2fs_flush_merged_writes(sbi);
1577127e670aSJaegeuk Kim 
157858cce381SYunlei He 	/* this is the case of multiple fstrims without any changes */
1579c473f1a9SChao Yu 	if (cpc->reason & CP_DISCARD) {
15804d57b86dSChao Yu 		if (!f2fs_exist_trim_candidates(sbi, cpc)) {
158125290fa5SJaegeuk Kim 			unblock_operations(sbi);
158225290fa5SJaegeuk Kim 			goto out;
158325290fa5SJaegeuk Kim 		}
158425290fa5SJaegeuk Kim 
15850333ad4eSJaegeuk Kim 		if (NM_I(sbi)->dirty_nat_cnt == 0 &&
15860333ad4eSJaegeuk Kim 				SIT_I(sbi)->dirty_sentries == 0 &&
15870333ad4eSJaegeuk Kim 				prefree_segments(sbi) == 0) {
15884d57b86dSChao Yu 			f2fs_flush_sit_entries(sbi, cpc);
15894d57b86dSChao Yu 			f2fs_clear_prefree_segments(sbi, cpc);
159058cce381SYunlei He 			unblock_operations(sbi);
159158cce381SYunlei He 			goto out;
159258cce381SYunlei He 		}
15930333ad4eSJaegeuk Kim 	}
159458cce381SYunlei He 
1595127e670aSJaegeuk Kim 	/*
1596127e670aSJaegeuk Kim 	 * update checkpoint pack index
1597127e670aSJaegeuk Kim 	 * Increase the version number so that
1598127e670aSJaegeuk Kim 	 * SIT entries and seg summaries are written at correct place
1599127e670aSJaegeuk Kim 	 */
1600d71b5564SJaegeuk Kim 	ckpt_ver = cur_cp_version(ckpt);
1601127e670aSJaegeuk Kim 	ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
1602127e670aSJaegeuk Kim 
1603127e670aSJaegeuk Kim 	/* write cached NAT/SIT entries to NAT/SIT area */
1604edc55aafSJaegeuk Kim 	err = f2fs_flush_nat_entries(sbi, cpc);
1605edc55aafSJaegeuk Kim 	if (err)
1606edc55aafSJaegeuk Kim 		goto stop;
1607edc55aafSJaegeuk Kim 
16084d57b86dSChao Yu 	f2fs_flush_sit_entries(sbi, cpc);
1609127e670aSJaegeuk Kim 
1610c34f42e2SChao Yu 	err = do_checkpoint(sbi, cpc);
16114e6a8d9bSJaegeuk Kim 	if (err)
16124d57b86dSChao Yu 		f2fs_release_discard_addrs(sbi);
16134e6a8d9bSJaegeuk Kim 	else
16144d57b86dSChao Yu 		f2fs_clear_prefree_segments(sbi, cpc);
1615edc55aafSJaegeuk Kim stop:
1616127e670aSJaegeuk Kim 	unblock_operations(sbi);
1617942e0be6SChangman Lee 	stat_inc_cp_count(sbi->stat_info);
161810027551SJaegeuk Kim 
1619c473f1a9SChao Yu 	if (cpc->reason & CP_RECOVERY)
1620dcbb4c10SJoe Perches 		f2fs_notice(sbi, "checkpoint: version = %llx", ckpt_ver);
162160b99b48SJaegeuk Kim 
16227a88ddb5SChao Yu 	/* update CP_TIME to trigger checkpoint periodically */
16236beceb54SJaegeuk Kim 	f2fs_update_time(sbi, CP_TIME);
162455d1cdb2SJaegeuk Kim 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint");
16258501017eSJaegeuk Kim out:
16268501017eSJaegeuk Kim 	mutex_unlock(&sbi->cp_mutex);
1627c34f42e2SChao Yu 	return err;
1628127e670aSJaegeuk Kim }
1629127e670aSJaegeuk Kim 
16304d57b86dSChao Yu void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi)
1631127e670aSJaegeuk Kim {
16326451e041SJaegeuk Kim 	int i;
16336451e041SJaegeuk Kim 
16346451e041SJaegeuk Kim 	for (i = 0; i < MAX_INO_ENTRY; i++) {
163567298804SChao Yu 		struct inode_management *im = &sbi->im[i];
163667298804SChao Yu 
163767298804SChao Yu 		INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC);
163867298804SChao Yu 		spin_lock_init(&im->ino_lock);
163967298804SChao Yu 		INIT_LIST_HEAD(&im->ino_list);
164067298804SChao Yu 		im->ino_num = 0;
16416451e041SJaegeuk Kim 	}
16426451e041SJaegeuk Kim 
1643b5b82205SChao Yu 	sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS -
164414b42817SWanpeng Li 			NR_CURSEG_TYPE - __cp_payload(sbi)) *
164514b42817SWanpeng Li 				F2FS_ORPHANS_PER_BLOCK;
1646127e670aSJaegeuk Kim }
1647127e670aSJaegeuk Kim 
16484d57b86dSChao Yu int __init f2fs_create_checkpoint_caches(void)
1649127e670aSJaegeuk Kim {
16506451e041SJaegeuk Kim 	ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry",
16516451e041SJaegeuk Kim 			sizeof(struct ino_entry));
16526451e041SJaegeuk Kim 	if (!ino_entry_slab)
1653127e670aSJaegeuk Kim 		return -ENOMEM;
16544d57b86dSChao Yu 	f2fs_inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry",
165506292073SChao Yu 			sizeof(struct inode_entry));
16564d57b86dSChao Yu 	if (!f2fs_inode_entry_slab) {
16576451e041SJaegeuk Kim 		kmem_cache_destroy(ino_entry_slab);
1658127e670aSJaegeuk Kim 		return -ENOMEM;
1659127e670aSJaegeuk Kim 	}
1660127e670aSJaegeuk Kim 	return 0;
1661127e670aSJaegeuk Kim }
1662127e670aSJaegeuk Kim 
16634d57b86dSChao Yu void f2fs_destroy_checkpoint_caches(void)
1664127e670aSJaegeuk Kim {
16656451e041SJaegeuk Kim 	kmem_cache_destroy(ino_entry_slab);
16664d57b86dSChao Yu 	kmem_cache_destroy(f2fs_inode_entry_slab);
1667127e670aSJaegeuk Kim }
1668