xref: /openbmc/linux/fs/f2fs/checkpoint.c (revision 91942321e4c9f8460f260cdfcf0a7a48a73a84a4)
10a8165d7SJaegeuk Kim /*
2127e670aSJaegeuk Kim  * fs/f2fs/checkpoint.c
3127e670aSJaegeuk Kim  *
4127e670aSJaegeuk Kim  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5127e670aSJaegeuk Kim  *             http://www.samsung.com/
6127e670aSJaegeuk Kim  *
7127e670aSJaegeuk Kim  * This program is free software; you can redistribute it and/or modify
8127e670aSJaegeuk Kim  * it under the terms of the GNU General Public License version 2 as
9127e670aSJaegeuk Kim  * published by the Free Software Foundation.
10127e670aSJaegeuk Kim  */
11127e670aSJaegeuk Kim #include <linux/fs.h>
12127e670aSJaegeuk Kim #include <linux/bio.h>
13127e670aSJaegeuk Kim #include <linux/mpage.h>
14127e670aSJaegeuk Kim #include <linux/writeback.h>
15127e670aSJaegeuk Kim #include <linux/blkdev.h>
16127e670aSJaegeuk Kim #include <linux/f2fs_fs.h>
17127e670aSJaegeuk Kim #include <linux/pagevec.h>
18127e670aSJaegeuk Kim #include <linux/swap.h>
19127e670aSJaegeuk Kim 
20127e670aSJaegeuk Kim #include "f2fs.h"
21127e670aSJaegeuk Kim #include "node.h"
22127e670aSJaegeuk Kim #include "segment.h"
239e4ded3fSJaegeuk Kim #include "trace.h"
242af4bd6cSNamjae Jeon #include <trace/events/f2fs.h>
25127e670aSJaegeuk Kim 
266451e041SJaegeuk Kim static struct kmem_cache *ino_entry_slab;
2706292073SChao Yu struct kmem_cache *inode_entry_slab;
28127e670aSJaegeuk Kim 
2938f91ca8SJaegeuk Kim void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io)
3038f91ca8SJaegeuk Kim {
3138f91ca8SJaegeuk Kim 	set_ckpt_flags(sbi->ckpt, CP_ERROR_FLAG);
3238f91ca8SJaegeuk Kim 	sbi->sb->s_flags |= MS_RDONLY;
3338f91ca8SJaegeuk Kim 	if (!end_io)
3438f91ca8SJaegeuk Kim 		f2fs_flush_merged_bios(sbi);
3538f91ca8SJaegeuk Kim }
3638f91ca8SJaegeuk Kim 
370a8165d7SJaegeuk Kim /*
38127e670aSJaegeuk Kim  * We guarantee no failure on the returned page.
39127e670aSJaegeuk Kim  */
40127e670aSJaegeuk Kim struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
41127e670aSJaegeuk Kim {
429df27d98SGu Zheng 	struct address_space *mapping = META_MAPPING(sbi);
43127e670aSJaegeuk Kim 	struct page *page = NULL;
44127e670aSJaegeuk Kim repeat:
45300e129cSJaegeuk Kim 	page = f2fs_grab_cache_page(mapping, index, false);
46127e670aSJaegeuk Kim 	if (!page) {
47127e670aSJaegeuk Kim 		cond_resched();
48127e670aSJaegeuk Kim 		goto repeat;
49127e670aSJaegeuk Kim 	}
50fec1d657SJaegeuk Kim 	f2fs_wait_on_page_writeback(page, META, true);
51127e670aSJaegeuk Kim 	SetPageUptodate(page);
52127e670aSJaegeuk Kim 	return page;
53127e670aSJaegeuk Kim }
54127e670aSJaegeuk Kim 
550a8165d7SJaegeuk Kim /*
56127e670aSJaegeuk Kim  * We guarantee no failure on the returned page.
57127e670aSJaegeuk Kim  */
582b947003SChao Yu static struct page *__get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index,
592b947003SChao Yu 							bool is_meta)
60127e670aSJaegeuk Kim {
619df27d98SGu Zheng 	struct address_space *mapping = META_MAPPING(sbi);
62127e670aSJaegeuk Kim 	struct page *page;
63cf04e8ebSJaegeuk Kim 	struct f2fs_io_info fio = {
6405ca3632SJaegeuk Kim 		.sbi = sbi,
65cf04e8ebSJaegeuk Kim 		.type = META,
66cf04e8ebSJaegeuk Kim 		.rw = READ_SYNC | REQ_META | REQ_PRIO,
677a9d7548SChao Yu 		.old_blkaddr = index,
687a9d7548SChao Yu 		.new_blkaddr = index,
694375a336SJaegeuk Kim 		.encrypted_page = NULL,
70cf04e8ebSJaegeuk Kim 	};
712b947003SChao Yu 
722b947003SChao Yu 	if (unlikely(!is_meta))
732b947003SChao Yu 		fio.rw &= ~REQ_META;
74127e670aSJaegeuk Kim repeat:
75300e129cSJaegeuk Kim 	page = f2fs_grab_cache_page(mapping, index, false);
76127e670aSJaegeuk Kim 	if (!page) {
77127e670aSJaegeuk Kim 		cond_resched();
78127e670aSJaegeuk Kim 		goto repeat;
79127e670aSJaegeuk Kim 	}
80393ff91fSJaegeuk Kim 	if (PageUptodate(page))
81393ff91fSJaegeuk Kim 		goto out;
82127e670aSJaegeuk Kim 
8305ca3632SJaegeuk Kim 	fio.page = page;
8405ca3632SJaegeuk Kim 
8586531d6bSJaegeuk Kim 	if (f2fs_submit_page_bio(&fio)) {
8686531d6bSJaegeuk Kim 		f2fs_put_page(page, 1);
87393ff91fSJaegeuk Kim 		goto repeat;
8886531d6bSJaegeuk Kim 	}
89393ff91fSJaegeuk Kim 
90393ff91fSJaegeuk Kim 	lock_page(page);
916bacf52fSJaegeuk Kim 	if (unlikely(page->mapping != mapping)) {
92afcb7ca0SJaegeuk Kim 		f2fs_put_page(page, 1);
93afcb7ca0SJaegeuk Kim 		goto repeat;
94afcb7ca0SJaegeuk Kim 	}
95f3f338caSChao Yu 
96f3f338caSChao Yu 	/*
97f3f338caSChao Yu 	 * if there is any IO error when accessing device, make our filesystem
98f3f338caSChao Yu 	 * readonly and make sure do not write checkpoint with non-uptodate
99f3f338caSChao Yu 	 * meta page.
100f3f338caSChao Yu 	 */
101f3f338caSChao Yu 	if (unlikely(!PageUptodate(page)))
10238f91ca8SJaegeuk Kim 		f2fs_stop_checkpoint(sbi, false);
103393ff91fSJaegeuk Kim out:
104127e670aSJaegeuk Kim 	return page;
105127e670aSJaegeuk Kim }
106127e670aSJaegeuk Kim 
1072b947003SChao Yu struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
1082b947003SChao Yu {
1092b947003SChao Yu 	return __get_meta_page(sbi, index, true);
1102b947003SChao Yu }
1112b947003SChao Yu 
1122b947003SChao Yu /* for POR only */
1132b947003SChao Yu struct page *get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index)
1142b947003SChao Yu {
1152b947003SChao Yu 	return __get_meta_page(sbi, index, false);
1162b947003SChao Yu }
1172b947003SChao Yu 
118f0c9cadaSChao Yu bool is_valid_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr, int type)
119662befdaSChao Yu {
120662befdaSChao Yu 	switch (type) {
121662befdaSChao Yu 	case META_NAT:
12266b00c18SChao Yu 		break;
123662befdaSChao Yu 	case META_SIT:
12466b00c18SChao Yu 		if (unlikely(blkaddr >= SIT_BLK_CNT(sbi)))
12566b00c18SChao Yu 			return false;
12666b00c18SChao Yu 		break;
12781c1a0f1SChao Yu 	case META_SSA:
12866b00c18SChao Yu 		if (unlikely(blkaddr >= MAIN_BLKADDR(sbi) ||
12966b00c18SChao Yu 			blkaddr < SM_I(sbi)->ssa_blkaddr))
13066b00c18SChao Yu 			return false;
13166b00c18SChao Yu 		break;
132662befdaSChao Yu 	case META_CP:
13366b00c18SChao Yu 		if (unlikely(blkaddr >= SIT_I(sbi)->sit_base_addr ||
13466b00c18SChao Yu 			blkaddr < __start_cp_addr(sbi)))
13566b00c18SChao Yu 			return false;
13666b00c18SChao Yu 		break;
1374c521f49SJaegeuk Kim 	case META_POR:
13866b00c18SChao Yu 		if (unlikely(blkaddr >= MAX_BLKADDR(sbi) ||
13966b00c18SChao Yu 			blkaddr < MAIN_BLKADDR(sbi)))
14066b00c18SChao Yu 			return false;
14166b00c18SChao Yu 		break;
142662befdaSChao Yu 	default:
143662befdaSChao Yu 		BUG();
144662befdaSChao Yu 	}
14566b00c18SChao Yu 
14666b00c18SChao Yu 	return true;
147662befdaSChao Yu }
148662befdaSChao Yu 
149662befdaSChao Yu /*
15081c1a0f1SChao Yu  * Readahead CP/NAT/SIT/SSA pages
151662befdaSChao Yu  */
15226879fb1SChao Yu int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
15326879fb1SChao Yu 							int type, bool sync)
154662befdaSChao Yu {
155662befdaSChao Yu 	struct page *page;
1564c521f49SJaegeuk Kim 	block_t blkno = start;
157662befdaSChao Yu 	struct f2fs_io_info fio = {
15805ca3632SJaegeuk Kim 		.sbi = sbi,
159662befdaSChao Yu 		.type = META,
16026879fb1SChao Yu 		.rw = sync ? (READ_SYNC | REQ_META | REQ_PRIO) : READA,
1614375a336SJaegeuk Kim 		.encrypted_page = NULL,
162662befdaSChao Yu 	};
163e9f5b8b8SChao Yu 	struct blk_plug plug;
164662befdaSChao Yu 
1652b947003SChao Yu 	if (unlikely(type == META_POR))
1662b947003SChao Yu 		fio.rw &= ~REQ_META;
1672b947003SChao Yu 
168e9f5b8b8SChao Yu 	blk_start_plug(&plug);
169662befdaSChao Yu 	for (; nrpages-- > 0; blkno++) {
170662befdaSChao Yu 
17166b00c18SChao Yu 		if (!is_valid_blkaddr(sbi, blkno, type))
17266b00c18SChao Yu 			goto out;
17366b00c18SChao Yu 
174662befdaSChao Yu 		switch (type) {
175662befdaSChao Yu 		case META_NAT:
17666b00c18SChao Yu 			if (unlikely(blkno >=
17766b00c18SChao Yu 					NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid)))
178662befdaSChao Yu 				blkno = 0;
17966b00c18SChao Yu 			/* get nat block addr */
1807a9d7548SChao Yu 			fio.new_blkaddr = current_nat_addr(sbi,
181662befdaSChao Yu 					blkno * NAT_ENTRY_PER_BLOCK);
182662befdaSChao Yu 			break;
183662befdaSChao Yu 		case META_SIT:
184662befdaSChao Yu 			/* get sit block addr */
1857a9d7548SChao Yu 			fio.new_blkaddr = current_sit_addr(sbi,
186662befdaSChao Yu 					blkno * SIT_ENTRY_PER_BLOCK);
187662befdaSChao Yu 			break;
18881c1a0f1SChao Yu 		case META_SSA:
189662befdaSChao Yu 		case META_CP:
1904c521f49SJaegeuk Kim 		case META_POR:
1917a9d7548SChao Yu 			fio.new_blkaddr = blkno;
192662befdaSChao Yu 			break;
193662befdaSChao Yu 		default:
194662befdaSChao Yu 			BUG();
195662befdaSChao Yu 		}
196662befdaSChao Yu 
197300e129cSJaegeuk Kim 		page = f2fs_grab_cache_page(META_MAPPING(sbi),
198300e129cSJaegeuk Kim 						fio.new_blkaddr, false);
199662befdaSChao Yu 		if (!page)
200662befdaSChao Yu 			continue;
201662befdaSChao Yu 		if (PageUptodate(page)) {
202662befdaSChao Yu 			f2fs_put_page(page, 1);
203662befdaSChao Yu 			continue;
204662befdaSChao Yu 		}
205662befdaSChao Yu 
20605ca3632SJaegeuk Kim 		fio.page = page;
2077a9d7548SChao Yu 		fio.old_blkaddr = fio.new_blkaddr;
20805ca3632SJaegeuk Kim 		f2fs_submit_page_mbio(&fio);
209662befdaSChao Yu 		f2fs_put_page(page, 0);
210662befdaSChao Yu 	}
211662befdaSChao Yu out:
212662befdaSChao Yu 	f2fs_submit_merged_bio(sbi, META, READ);
213e9f5b8b8SChao Yu 	blk_finish_plug(&plug);
214662befdaSChao Yu 	return blkno - start;
215662befdaSChao Yu }
216662befdaSChao Yu 
217635aee1fSChao Yu void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index)
218635aee1fSChao Yu {
219635aee1fSChao Yu 	struct page *page;
220635aee1fSChao Yu 	bool readahead = false;
221635aee1fSChao Yu 
222635aee1fSChao Yu 	page = find_get_page(META_MAPPING(sbi), index);
2234da7bf5aSJaegeuk Kim 	if (!page || !PageUptodate(page))
224635aee1fSChao Yu 		readahead = true;
225635aee1fSChao Yu 	f2fs_put_page(page, 0);
226635aee1fSChao Yu 
227635aee1fSChao Yu 	if (readahead)
22826879fb1SChao Yu 		ra_meta_pages(sbi, index, MAX_BIO_BLOCKS(sbi), META_POR, true);
229635aee1fSChao Yu }
230635aee1fSChao Yu 
231127e670aSJaegeuk Kim static int f2fs_write_meta_page(struct page *page,
232127e670aSJaegeuk Kim 				struct writeback_control *wbc)
233127e670aSJaegeuk Kim {
2344081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_P_SB(page);
235577e3495SJaegeuk Kim 
236ecda0de3SChao Yu 	trace_f2fs_writepage(page, META);
237ecda0de3SChao Yu 
238caf0047eSChao Yu 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
239cfb271d4SChao Yu 		goto redirty_out;
240857dc4e0SJaegeuk Kim 	if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0))
241cfb271d4SChao Yu 		goto redirty_out;
2421e968fdfSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
243cf779cabSJaegeuk Kim 		goto redirty_out;
244127e670aSJaegeuk Kim 
245577e3495SJaegeuk Kim 	write_meta_page(sbi, page);
246127e670aSJaegeuk Kim 	dec_page_count(sbi, F2FS_DIRTY_META);
2470c3a5797SChao Yu 
2480c3a5797SChao Yu 	if (wbc->for_reclaim)
2490c3a5797SChao Yu 		f2fs_submit_merged_bio_cond(sbi, NULL, page, 0, META, WRITE);
2500c3a5797SChao Yu 
251127e670aSJaegeuk Kim 	unlock_page(page);
252857dc4e0SJaegeuk Kim 
2530c3a5797SChao Yu 	if (unlikely(f2fs_cp_error(sbi)))
254857dc4e0SJaegeuk Kim 		f2fs_submit_merged_bio(sbi, META, WRITE);
2550c3a5797SChao Yu 
256577e3495SJaegeuk Kim 	return 0;
257cfb271d4SChao Yu 
258cfb271d4SChao Yu redirty_out:
25976f60268SJaegeuk Kim 	redirty_page_for_writepage(wbc, page);
260cfb271d4SChao Yu 	return AOP_WRITEPAGE_ACTIVATE;
261127e670aSJaegeuk Kim }
262127e670aSJaegeuk Kim 
263127e670aSJaegeuk Kim static int f2fs_write_meta_pages(struct address_space *mapping,
264127e670aSJaegeuk Kim 				struct writeback_control *wbc)
265127e670aSJaegeuk Kim {
2664081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
26750c8cdb3SJaegeuk Kim 	long diff, written;
268127e670aSJaegeuk Kim 
2695459aa97SJaegeuk Kim 	/* collect a number of dirty meta pages and write together */
27050c8cdb3SJaegeuk Kim 	if (wbc->for_kupdate ||
27150c8cdb3SJaegeuk Kim 		get_pages(sbi, F2FS_DIRTY_META) < nr_pages_to_skip(sbi, META))
272d3baf95dSJaegeuk Kim 		goto skip_write;
273127e670aSJaegeuk Kim 
274d31c7c3fSYunlei He 	trace_f2fs_writepages(mapping->host, wbc, META);
275d31c7c3fSYunlei He 
276127e670aSJaegeuk Kim 	/* if mounting is failed, skip writing node pages */
277127e670aSJaegeuk Kim 	mutex_lock(&sbi->cp_mutex);
27850c8cdb3SJaegeuk Kim 	diff = nr_pages_to_write(sbi, META, wbc);
27950c8cdb3SJaegeuk Kim 	written = sync_meta_pages(sbi, META, wbc->nr_to_write);
280127e670aSJaegeuk Kim 	mutex_unlock(&sbi->cp_mutex);
28150c8cdb3SJaegeuk Kim 	wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff);
282127e670aSJaegeuk Kim 	return 0;
283d3baf95dSJaegeuk Kim 
284d3baf95dSJaegeuk Kim skip_write:
285d3baf95dSJaegeuk Kim 	wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META);
286d31c7c3fSYunlei He 	trace_f2fs_writepages(mapping->host, wbc, META);
287d3baf95dSJaegeuk Kim 	return 0;
288127e670aSJaegeuk Kim }
289127e670aSJaegeuk Kim 
290127e670aSJaegeuk Kim long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
291127e670aSJaegeuk Kim 						long nr_to_write)
292127e670aSJaegeuk Kim {
2939df27d98SGu Zheng 	struct address_space *mapping = META_MAPPING(sbi);
29480dd9c0eSChao Yu 	pgoff_t index = 0, end = ULONG_MAX, prev = ULONG_MAX;
295127e670aSJaegeuk Kim 	struct pagevec pvec;
296127e670aSJaegeuk Kim 	long nwritten = 0;
297127e670aSJaegeuk Kim 	struct writeback_control wbc = {
298127e670aSJaegeuk Kim 		.for_reclaim = 0,
299127e670aSJaegeuk Kim 	};
300e9f5b8b8SChao Yu 	struct blk_plug plug;
301127e670aSJaegeuk Kim 
302127e670aSJaegeuk Kim 	pagevec_init(&pvec, 0);
303127e670aSJaegeuk Kim 
304e9f5b8b8SChao Yu 	blk_start_plug(&plug);
305e9f5b8b8SChao Yu 
306127e670aSJaegeuk Kim 	while (index <= end) {
307127e670aSJaegeuk Kim 		int i, nr_pages;
308127e670aSJaegeuk Kim 		nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
309127e670aSJaegeuk Kim 				PAGECACHE_TAG_DIRTY,
310127e670aSJaegeuk Kim 				min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
311cfb271d4SChao Yu 		if (unlikely(nr_pages == 0))
312127e670aSJaegeuk Kim 			break;
313127e670aSJaegeuk Kim 
314127e670aSJaegeuk Kim 		for (i = 0; i < nr_pages; i++) {
315127e670aSJaegeuk Kim 			struct page *page = pvec.pages[i];
316203681f6SJaegeuk Kim 
31780dd9c0eSChao Yu 			if (prev == ULONG_MAX)
3186066d8cdSJaegeuk Kim 				prev = page->index - 1;
3196066d8cdSJaegeuk Kim 			if (nr_to_write != LONG_MAX && page->index != prev + 1) {
3206066d8cdSJaegeuk Kim 				pagevec_release(&pvec);
3216066d8cdSJaegeuk Kim 				goto stop;
3226066d8cdSJaegeuk Kim 			}
3236066d8cdSJaegeuk Kim 
324127e670aSJaegeuk Kim 			lock_page(page);
325203681f6SJaegeuk Kim 
326203681f6SJaegeuk Kim 			if (unlikely(page->mapping != mapping)) {
327203681f6SJaegeuk Kim continue_unlock:
328203681f6SJaegeuk Kim 				unlock_page(page);
329203681f6SJaegeuk Kim 				continue;
330203681f6SJaegeuk Kim 			}
331203681f6SJaegeuk Kim 			if (!PageDirty(page)) {
332203681f6SJaegeuk Kim 				/* someone wrote it for us */
333203681f6SJaegeuk Kim 				goto continue_unlock;
334203681f6SJaegeuk Kim 			}
335203681f6SJaegeuk Kim 
336fa3d2bdfSJaegeuk Kim 			f2fs_wait_on_page_writeback(page, META, true);
337fa3d2bdfSJaegeuk Kim 
338fa3d2bdfSJaegeuk Kim 			BUG_ON(PageWriteback(page));
339203681f6SJaegeuk Kim 			if (!clear_page_dirty_for_io(page))
340203681f6SJaegeuk Kim 				goto continue_unlock;
341203681f6SJaegeuk Kim 
34297dc3fd2SChao Yu 			if (mapping->a_ops->writepage(page, &wbc)) {
343577e3495SJaegeuk Kim 				unlock_page(page);
344577e3495SJaegeuk Kim 				break;
345577e3495SJaegeuk Kim 			}
346cfb271d4SChao Yu 			nwritten++;
3476066d8cdSJaegeuk Kim 			prev = page->index;
348cfb271d4SChao Yu 			if (unlikely(nwritten >= nr_to_write))
349127e670aSJaegeuk Kim 				break;
350127e670aSJaegeuk Kim 		}
351127e670aSJaegeuk Kim 		pagevec_release(&pvec);
352127e670aSJaegeuk Kim 		cond_resched();
353127e670aSJaegeuk Kim 	}
3546066d8cdSJaegeuk Kim stop:
355127e670aSJaegeuk Kim 	if (nwritten)
356458e6197SJaegeuk Kim 		f2fs_submit_merged_bio(sbi, type, WRITE);
357127e670aSJaegeuk Kim 
358e9f5b8b8SChao Yu 	blk_finish_plug(&plug);
359e9f5b8b8SChao Yu 
360127e670aSJaegeuk Kim 	return nwritten;
361127e670aSJaegeuk Kim }
362127e670aSJaegeuk Kim 
363127e670aSJaegeuk Kim static int f2fs_set_meta_page_dirty(struct page *page)
364127e670aSJaegeuk Kim {
36526c6b887SJaegeuk Kim 	trace_f2fs_set_page_dirty(page, META);
36626c6b887SJaegeuk Kim 
367127e670aSJaegeuk Kim 	SetPageUptodate(page);
368127e670aSJaegeuk Kim 	if (!PageDirty(page)) {
369127e670aSJaegeuk Kim 		__set_page_dirty_nobuffers(page);
3704081363fSJaegeuk Kim 		inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_META);
3711601839eSChao Yu 		SetPagePrivate(page);
3729e4ded3fSJaegeuk Kim 		f2fs_trace_pid(page);
373127e670aSJaegeuk Kim 		return 1;
374127e670aSJaegeuk Kim 	}
375127e670aSJaegeuk Kim 	return 0;
376127e670aSJaegeuk Kim }
377127e670aSJaegeuk Kim 
378127e670aSJaegeuk Kim const struct address_space_operations f2fs_meta_aops = {
379127e670aSJaegeuk Kim 	.writepage	= f2fs_write_meta_page,
380127e670aSJaegeuk Kim 	.writepages	= f2fs_write_meta_pages,
381127e670aSJaegeuk Kim 	.set_page_dirty	= f2fs_set_meta_page_dirty,
382487261f3SChao Yu 	.invalidatepage = f2fs_invalidate_page,
383487261f3SChao Yu 	.releasepage	= f2fs_release_page,
384127e670aSJaegeuk Kim };
385127e670aSJaegeuk Kim 
3866451e041SJaegeuk Kim static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
387953e6cc6SJaegeuk Kim {
38867298804SChao Yu 	struct inode_management *im = &sbi->im[type];
38980c54505SJaegeuk Kim 	struct ino_entry *e, *tmp;
39080c54505SJaegeuk Kim 
39180c54505SJaegeuk Kim 	tmp = f2fs_kmem_cache_alloc(ino_entry_slab, GFP_NOFS);
39239efac41SJaegeuk Kim retry:
39380c54505SJaegeuk Kim 	radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
394769ec6e5SJaegeuk Kim 
39567298804SChao Yu 	spin_lock(&im->ino_lock);
39667298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
39739efac41SJaegeuk Kim 	if (!e) {
39880c54505SJaegeuk Kim 		e = tmp;
39967298804SChao Yu 		if (radix_tree_insert(&im->ino_root, ino, e)) {
40067298804SChao Yu 			spin_unlock(&im->ino_lock);
401769ec6e5SJaegeuk Kim 			radix_tree_preload_end();
40239efac41SJaegeuk Kim 			goto retry;
40339efac41SJaegeuk Kim 		}
40439efac41SJaegeuk Kim 		memset(e, 0, sizeof(struct ino_entry));
40539efac41SJaegeuk Kim 		e->ino = ino;
40639efac41SJaegeuk Kim 
40767298804SChao Yu 		list_add_tail(&e->list, &im->ino_list);
4088c402946SJaegeuk Kim 		if (type != ORPHAN_INO)
40967298804SChao Yu 			im->ino_num++;
41039efac41SJaegeuk Kim 	}
41167298804SChao Yu 	spin_unlock(&im->ino_lock);
412769ec6e5SJaegeuk Kim 	radix_tree_preload_end();
41380c54505SJaegeuk Kim 
41480c54505SJaegeuk Kim 	if (e != tmp)
41580c54505SJaegeuk Kim 		kmem_cache_free(ino_entry_slab, tmp);
416953e6cc6SJaegeuk Kim }
417953e6cc6SJaegeuk Kim 
4186451e041SJaegeuk Kim static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
419953e6cc6SJaegeuk Kim {
42067298804SChao Yu 	struct inode_management *im = &sbi->im[type];
4216451e041SJaegeuk Kim 	struct ino_entry *e;
422953e6cc6SJaegeuk Kim 
42367298804SChao Yu 	spin_lock(&im->ino_lock);
42467298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
42539efac41SJaegeuk Kim 	if (e) {
426953e6cc6SJaegeuk Kim 		list_del(&e->list);
42767298804SChao Yu 		radix_tree_delete(&im->ino_root, ino);
42867298804SChao Yu 		im->ino_num--;
42967298804SChao Yu 		spin_unlock(&im->ino_lock);
4306451e041SJaegeuk Kim 		kmem_cache_free(ino_entry_slab, e);
431953e6cc6SJaegeuk Kim 		return;
432953e6cc6SJaegeuk Kim 	}
43367298804SChao Yu 	spin_unlock(&im->ino_lock);
434953e6cc6SJaegeuk Kim }
435953e6cc6SJaegeuk Kim 
436a49324f1SChao Yu void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
437fff04f90SJaegeuk Kim {
438fff04f90SJaegeuk Kim 	/* add new dirty ino entry into list */
439fff04f90SJaegeuk Kim 	__add_ino_entry(sbi, ino, type);
440fff04f90SJaegeuk Kim }
441fff04f90SJaegeuk Kim 
442a49324f1SChao Yu void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
443fff04f90SJaegeuk Kim {
444fff04f90SJaegeuk Kim 	/* remove dirty ino entry from list */
445fff04f90SJaegeuk Kim 	__remove_ino_entry(sbi, ino, type);
446fff04f90SJaegeuk Kim }
447fff04f90SJaegeuk Kim 
448fff04f90SJaegeuk Kim /* mode should be APPEND_INO or UPDATE_INO */
449fff04f90SJaegeuk Kim bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode)
450fff04f90SJaegeuk Kim {
45167298804SChao Yu 	struct inode_management *im = &sbi->im[mode];
452fff04f90SJaegeuk Kim 	struct ino_entry *e;
45367298804SChao Yu 
45467298804SChao Yu 	spin_lock(&im->ino_lock);
45567298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
45667298804SChao Yu 	spin_unlock(&im->ino_lock);
457fff04f90SJaegeuk Kim 	return e ? true : false;
458fff04f90SJaegeuk Kim }
459fff04f90SJaegeuk Kim 
46074ef9241SJaegeuk Kim void release_ino_entry(struct f2fs_sb_info *sbi, bool all)
461fff04f90SJaegeuk Kim {
462fff04f90SJaegeuk Kim 	struct ino_entry *e, *tmp;
463fff04f90SJaegeuk Kim 	int i;
464fff04f90SJaegeuk Kim 
46574ef9241SJaegeuk Kim 	for (i = all ? ORPHAN_INO: APPEND_INO; i <= UPDATE_INO; i++) {
46667298804SChao Yu 		struct inode_management *im = &sbi->im[i];
46767298804SChao Yu 
46867298804SChao Yu 		spin_lock(&im->ino_lock);
46967298804SChao Yu 		list_for_each_entry_safe(e, tmp, &im->ino_list, list) {
470fff04f90SJaegeuk Kim 			list_del(&e->list);
47167298804SChao Yu 			radix_tree_delete(&im->ino_root, e->ino);
472fff04f90SJaegeuk Kim 			kmem_cache_free(ino_entry_slab, e);
47367298804SChao Yu 			im->ino_num--;
474fff04f90SJaegeuk Kim 		}
47567298804SChao Yu 		spin_unlock(&im->ino_lock);
476fff04f90SJaegeuk Kim 	}
477fff04f90SJaegeuk Kim }
478fff04f90SJaegeuk Kim 
479cbd56e7dSJaegeuk Kim int acquire_orphan_inode(struct f2fs_sb_info *sbi)
480127e670aSJaegeuk Kim {
48167298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
482127e670aSJaegeuk Kim 	int err = 0;
483127e670aSJaegeuk Kim 
48467298804SChao Yu 	spin_lock(&im->ino_lock);
485cb78942bSJaegeuk Kim 
486cb78942bSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
487cb78942bSJaegeuk Kim 	if (time_to_inject(FAULT_ORPHAN)) {
488cb78942bSJaegeuk Kim 		spin_unlock(&im->ino_lock);
489cb78942bSJaegeuk Kim 		return -ENOSPC;
490cb78942bSJaegeuk Kim 	}
491cb78942bSJaegeuk Kim #endif
49267298804SChao Yu 	if (unlikely(im->ino_num >= sbi->max_orphans))
493127e670aSJaegeuk Kim 		err = -ENOSPC;
494cbd56e7dSJaegeuk Kim 	else
49567298804SChao Yu 		im->ino_num++;
49667298804SChao Yu 	spin_unlock(&im->ino_lock);
4970d47c1adSGu Zheng 
498127e670aSJaegeuk Kim 	return err;
499127e670aSJaegeuk Kim }
500127e670aSJaegeuk Kim 
501cbd56e7dSJaegeuk Kim void release_orphan_inode(struct f2fs_sb_info *sbi)
502cbd56e7dSJaegeuk Kim {
50367298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
50467298804SChao Yu 
50567298804SChao Yu 	spin_lock(&im->ino_lock);
50667298804SChao Yu 	f2fs_bug_on(sbi, im->ino_num == 0);
50767298804SChao Yu 	im->ino_num--;
50867298804SChao Yu 	spin_unlock(&im->ino_lock);
509cbd56e7dSJaegeuk Kim }
510cbd56e7dSJaegeuk Kim 
511127e670aSJaegeuk Kim void add_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
512127e670aSJaegeuk Kim {
51339efac41SJaegeuk Kim 	/* add new orphan ino entry into list */
5146451e041SJaegeuk Kim 	__add_ino_entry(sbi, ino, ORPHAN_INO);
515127e670aSJaegeuk Kim }
516127e670aSJaegeuk Kim 
517127e670aSJaegeuk Kim void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
518127e670aSJaegeuk Kim {
519953e6cc6SJaegeuk Kim 	/* remove orphan entry from orphan list */
5206451e041SJaegeuk Kim 	__remove_ino_entry(sbi, ino, ORPHAN_INO);
521127e670aSJaegeuk Kim }
522127e670aSJaegeuk Kim 
5238c14bfadSChao Yu static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
524127e670aSJaegeuk Kim {
5258c14bfadSChao Yu 	struct inode *inode;
5268c14bfadSChao Yu 
5278c14bfadSChao Yu 	inode = f2fs_iget(sbi->sb, ino);
5288c14bfadSChao Yu 	if (IS_ERR(inode)) {
5298c14bfadSChao Yu 		/*
5308c14bfadSChao Yu 		 * there should be a bug that we can't find the entry
5318c14bfadSChao Yu 		 * to orphan inode.
5328c14bfadSChao Yu 		 */
5338c14bfadSChao Yu 		f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT);
5348c14bfadSChao Yu 		return PTR_ERR(inode);
5358c14bfadSChao Yu 	}
5368c14bfadSChao Yu 
537127e670aSJaegeuk Kim 	clear_nlink(inode);
538127e670aSJaegeuk Kim 
539127e670aSJaegeuk Kim 	/* truncate all the data during iput */
540127e670aSJaegeuk Kim 	iput(inode);
5418c14bfadSChao Yu 	return 0;
542127e670aSJaegeuk Kim }
543127e670aSJaegeuk Kim 
5448c14bfadSChao Yu int recover_orphan_inodes(struct f2fs_sb_info *sbi)
545127e670aSJaegeuk Kim {
5463c642985SWanpeng Li 	block_t start_blk, orphan_blocks, i, j;
5478c14bfadSChao Yu 	int err;
548127e670aSJaegeuk Kim 
54925ca923bSJaegeuk Kim 	if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG))
5508c14bfadSChao Yu 		return 0;
551127e670aSJaegeuk Kim 
55255141486SWanpeng Li 	start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
5533c642985SWanpeng Li 	orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
554127e670aSJaegeuk Kim 
55526879fb1SChao Yu 	ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true);
556662befdaSChao Yu 
5573c642985SWanpeng Li 	for (i = 0; i < orphan_blocks; i++) {
558127e670aSJaegeuk Kim 		struct page *page = get_meta_page(sbi, start_blk + i);
559127e670aSJaegeuk Kim 		struct f2fs_orphan_block *orphan_blk;
560127e670aSJaegeuk Kim 
561127e670aSJaegeuk Kim 		orphan_blk = (struct f2fs_orphan_block *)page_address(page);
562127e670aSJaegeuk Kim 		for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
563127e670aSJaegeuk Kim 			nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
5648c14bfadSChao Yu 			err = recover_orphan_inode(sbi, ino);
5658c14bfadSChao Yu 			if (err) {
5668c14bfadSChao Yu 				f2fs_put_page(page, 1);
5678c14bfadSChao Yu 				return err;
5688c14bfadSChao Yu 			}
569127e670aSJaegeuk Kim 		}
570127e670aSJaegeuk Kim 		f2fs_put_page(page, 1);
571127e670aSJaegeuk Kim 	}
572127e670aSJaegeuk Kim 	/* clear Orphan Flag */
57325ca923bSJaegeuk Kim 	clear_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG);
5748c14bfadSChao Yu 	return 0;
575127e670aSJaegeuk Kim }
576127e670aSJaegeuk Kim 
577127e670aSJaegeuk Kim static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
578127e670aSJaegeuk Kim {
579502c6e0bSGu Zheng 	struct list_head *head;
580127e670aSJaegeuk Kim 	struct f2fs_orphan_block *orphan_blk = NULL;
581127e670aSJaegeuk Kim 	unsigned int nentries = 0;
582bd936f84SChao Yu 	unsigned short index = 1;
5838c402946SJaegeuk Kim 	unsigned short orphan_blocks;
5844531929eSGu Zheng 	struct page *page = NULL;
5856451e041SJaegeuk Kim 	struct ino_entry *orphan = NULL;
58667298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
587127e670aSJaegeuk Kim 
58867298804SChao Yu 	orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num);
5898c402946SJaegeuk Kim 
590d6c67a4fSJaegeuk Kim 	/*
591d6c67a4fSJaegeuk Kim 	 * we don't need to do spin_lock(&im->ino_lock) here, since all the
592d6c67a4fSJaegeuk Kim 	 * orphan inode operations are covered under f2fs_lock_op().
593d6c67a4fSJaegeuk Kim 	 * And, spin_lock should be avoided due to page operations below.
594d6c67a4fSJaegeuk Kim 	 */
59567298804SChao Yu 	head = &im->ino_list;
596127e670aSJaegeuk Kim 
597127e670aSJaegeuk Kim 	/* loop for each orphan inode entry and write them in Jornal block */
598502c6e0bSGu Zheng 	list_for_each_entry(orphan, head, list) {
599502c6e0bSGu Zheng 		if (!page) {
600bd936f84SChao Yu 			page = grab_meta_page(sbi, start_blk++);
601502c6e0bSGu Zheng 			orphan_blk =
602502c6e0bSGu Zheng 				(struct f2fs_orphan_block *)page_address(page);
603502c6e0bSGu Zheng 			memset(orphan_blk, 0, sizeof(*orphan_blk));
604502c6e0bSGu Zheng 		}
605127e670aSJaegeuk Kim 
60636795567SGu Zheng 		orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
607127e670aSJaegeuk Kim 
60836795567SGu Zheng 		if (nentries == F2FS_ORPHANS_PER_BLOCK) {
609127e670aSJaegeuk Kim 			/*
610127e670aSJaegeuk Kim 			 * an orphan block is full of 1020 entries,
611127e670aSJaegeuk Kim 			 * then we need to flush current orphan blocks
612127e670aSJaegeuk Kim 			 * and bring another one in memory
613127e670aSJaegeuk Kim 			 */
614127e670aSJaegeuk Kim 			orphan_blk->blk_addr = cpu_to_le16(index);
615127e670aSJaegeuk Kim 			orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
616127e670aSJaegeuk Kim 			orphan_blk->entry_count = cpu_to_le32(nentries);
617127e670aSJaegeuk Kim 			set_page_dirty(page);
618127e670aSJaegeuk Kim 			f2fs_put_page(page, 1);
619127e670aSJaegeuk Kim 			index++;
620127e670aSJaegeuk Kim 			nentries = 0;
621127e670aSJaegeuk Kim 			page = NULL;
622127e670aSJaegeuk Kim 		}
623127e670aSJaegeuk Kim 	}
624127e670aSJaegeuk Kim 
625502c6e0bSGu Zheng 	if (page) {
626127e670aSJaegeuk Kim 		orphan_blk->blk_addr = cpu_to_le16(index);
627127e670aSJaegeuk Kim 		orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
628127e670aSJaegeuk Kim 		orphan_blk->entry_count = cpu_to_le32(nentries);
629127e670aSJaegeuk Kim 		set_page_dirty(page);
630127e670aSJaegeuk Kim 		f2fs_put_page(page, 1);
631502c6e0bSGu Zheng 	}
632127e670aSJaegeuk Kim }
633127e670aSJaegeuk Kim 
634127e670aSJaegeuk Kim static struct page *validate_checkpoint(struct f2fs_sb_info *sbi,
635127e670aSJaegeuk Kim 				block_t cp_addr, unsigned long long *version)
636127e670aSJaegeuk Kim {
637127e670aSJaegeuk Kim 	struct page *cp_page_1, *cp_page_2 = NULL;
638127e670aSJaegeuk Kim 	unsigned long blk_size = sbi->blocksize;
639127e670aSJaegeuk Kim 	struct f2fs_checkpoint *cp_block;
640127e670aSJaegeuk Kim 	unsigned long long cur_version = 0, pre_version = 0;
641127e670aSJaegeuk Kim 	size_t crc_offset;
6427e586fa0SJaegeuk Kim 	__u32 crc = 0;
643127e670aSJaegeuk Kim 
644127e670aSJaegeuk Kim 	/* Read the 1st cp block in this CP pack */
645127e670aSJaegeuk Kim 	cp_page_1 = get_meta_page(sbi, cp_addr);
646127e670aSJaegeuk Kim 
647127e670aSJaegeuk Kim 	/* get the version number */
648127e670aSJaegeuk Kim 	cp_block = (struct f2fs_checkpoint *)page_address(cp_page_1);
649127e670aSJaegeuk Kim 	crc_offset = le32_to_cpu(cp_block->checksum_offset);
650127e670aSJaegeuk Kim 	if (crc_offset >= blk_size)
651127e670aSJaegeuk Kim 		goto invalid_cp1;
652127e670aSJaegeuk Kim 
65329e7043fSJaegeuk Kim 	crc = le32_to_cpu(*((__le32 *)((unsigned char *)cp_block + crc_offset)));
65443b6573bSKeith Mok 	if (!f2fs_crc_valid(sbi, crc, cp_block, crc_offset))
655127e670aSJaegeuk Kim 		goto invalid_cp1;
656127e670aSJaegeuk Kim 
657d71b5564SJaegeuk Kim 	pre_version = cur_cp_version(cp_block);
658127e670aSJaegeuk Kim 
659127e670aSJaegeuk Kim 	/* Read the 2nd cp block in this CP pack */
66025ca923bSJaegeuk Kim 	cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1;
661127e670aSJaegeuk Kim 	cp_page_2 = get_meta_page(sbi, cp_addr);
662127e670aSJaegeuk Kim 
663127e670aSJaegeuk Kim 	cp_block = (struct f2fs_checkpoint *)page_address(cp_page_2);
664127e670aSJaegeuk Kim 	crc_offset = le32_to_cpu(cp_block->checksum_offset);
665127e670aSJaegeuk Kim 	if (crc_offset >= blk_size)
666127e670aSJaegeuk Kim 		goto invalid_cp2;
667127e670aSJaegeuk Kim 
66829e7043fSJaegeuk Kim 	crc = le32_to_cpu(*((__le32 *)((unsigned char *)cp_block + crc_offset)));
66943b6573bSKeith Mok 	if (!f2fs_crc_valid(sbi, crc, cp_block, crc_offset))
670127e670aSJaegeuk Kim 		goto invalid_cp2;
671127e670aSJaegeuk Kim 
672d71b5564SJaegeuk Kim 	cur_version = cur_cp_version(cp_block);
673127e670aSJaegeuk Kim 
674127e670aSJaegeuk Kim 	if (cur_version == pre_version) {
675127e670aSJaegeuk Kim 		*version = cur_version;
676127e670aSJaegeuk Kim 		f2fs_put_page(cp_page_2, 1);
677127e670aSJaegeuk Kim 		return cp_page_1;
678127e670aSJaegeuk Kim 	}
679127e670aSJaegeuk Kim invalid_cp2:
680127e670aSJaegeuk Kim 	f2fs_put_page(cp_page_2, 1);
681127e670aSJaegeuk Kim invalid_cp1:
682127e670aSJaegeuk Kim 	f2fs_put_page(cp_page_1, 1);
683127e670aSJaegeuk Kim 	return NULL;
684127e670aSJaegeuk Kim }
685127e670aSJaegeuk Kim 
686127e670aSJaegeuk Kim int get_valid_checkpoint(struct f2fs_sb_info *sbi)
687127e670aSJaegeuk Kim {
688127e670aSJaegeuk Kim 	struct f2fs_checkpoint *cp_block;
689127e670aSJaegeuk Kim 	struct f2fs_super_block *fsb = sbi->raw_super;
690127e670aSJaegeuk Kim 	struct page *cp1, *cp2, *cur_page;
691127e670aSJaegeuk Kim 	unsigned long blk_size = sbi->blocksize;
692127e670aSJaegeuk Kim 	unsigned long long cp1_version = 0, cp2_version = 0;
693127e670aSJaegeuk Kim 	unsigned long long cp_start_blk_no;
69455141486SWanpeng Li 	unsigned int cp_blks = 1 + __cp_payload(sbi);
6951dbe4152SChangman Lee 	block_t cp_blk_no;
6961dbe4152SChangman Lee 	int i;
697127e670aSJaegeuk Kim 
6981dbe4152SChangman Lee 	sbi->ckpt = kzalloc(cp_blks * blk_size, GFP_KERNEL);
699127e670aSJaegeuk Kim 	if (!sbi->ckpt)
700127e670aSJaegeuk Kim 		return -ENOMEM;
701127e670aSJaegeuk Kim 	/*
702127e670aSJaegeuk Kim 	 * Finding out valid cp block involves read both
703127e670aSJaegeuk Kim 	 * sets( cp pack1 and cp pack 2)
704127e670aSJaegeuk Kim 	 */
705127e670aSJaegeuk Kim 	cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr);
706127e670aSJaegeuk Kim 	cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
707127e670aSJaegeuk Kim 
708127e670aSJaegeuk Kim 	/* The second checkpoint pack should start at the next segment */
709f9a4e6dfSJaegeuk Kim 	cp_start_blk_no += ((unsigned long long)1) <<
710f9a4e6dfSJaegeuk Kim 				le32_to_cpu(fsb->log_blocks_per_seg);
711127e670aSJaegeuk Kim 	cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
712127e670aSJaegeuk Kim 
713127e670aSJaegeuk Kim 	if (cp1 && cp2) {
714127e670aSJaegeuk Kim 		if (ver_after(cp2_version, cp1_version))
715127e670aSJaegeuk Kim 			cur_page = cp2;
716127e670aSJaegeuk Kim 		else
717127e670aSJaegeuk Kim 			cur_page = cp1;
718127e670aSJaegeuk Kim 	} else if (cp1) {
719127e670aSJaegeuk Kim 		cur_page = cp1;
720127e670aSJaegeuk Kim 	} else if (cp2) {
721127e670aSJaegeuk Kim 		cur_page = cp2;
722127e670aSJaegeuk Kim 	} else {
723127e670aSJaegeuk Kim 		goto fail_no_cp;
724127e670aSJaegeuk Kim 	}
725127e670aSJaegeuk Kim 
726127e670aSJaegeuk Kim 	cp_block = (struct f2fs_checkpoint *)page_address(cur_page);
727127e670aSJaegeuk Kim 	memcpy(sbi->ckpt, cp_block, blk_size);
728127e670aSJaegeuk Kim 
729984ec63cSShawn Lin 	/* Sanity checking of checkpoint */
730984ec63cSShawn Lin 	if (sanity_check_ckpt(sbi))
731984ec63cSShawn Lin 		goto fail_no_cp;
732984ec63cSShawn Lin 
7331dbe4152SChangman Lee 	if (cp_blks <= 1)
7341dbe4152SChangman Lee 		goto done;
7351dbe4152SChangman Lee 
7361dbe4152SChangman Lee 	cp_blk_no = le32_to_cpu(fsb->cp_blkaddr);
7371dbe4152SChangman Lee 	if (cur_page == cp2)
7381dbe4152SChangman Lee 		cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg);
7391dbe4152SChangman Lee 
7401dbe4152SChangman Lee 	for (i = 1; i < cp_blks; i++) {
7411dbe4152SChangman Lee 		void *sit_bitmap_ptr;
7421dbe4152SChangman Lee 		unsigned char *ckpt = (unsigned char *)sbi->ckpt;
7431dbe4152SChangman Lee 
7441dbe4152SChangman Lee 		cur_page = get_meta_page(sbi, cp_blk_no + i);
7451dbe4152SChangman Lee 		sit_bitmap_ptr = page_address(cur_page);
7461dbe4152SChangman Lee 		memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size);
7471dbe4152SChangman Lee 		f2fs_put_page(cur_page, 1);
7481dbe4152SChangman Lee 	}
7491dbe4152SChangman Lee done:
750127e670aSJaegeuk Kim 	f2fs_put_page(cp1, 1);
751127e670aSJaegeuk Kim 	f2fs_put_page(cp2, 1);
752127e670aSJaegeuk Kim 	return 0;
753127e670aSJaegeuk Kim 
754127e670aSJaegeuk Kim fail_no_cp:
755127e670aSJaegeuk Kim 	kfree(sbi->ckpt);
756127e670aSJaegeuk Kim 	return -EINVAL;
757127e670aSJaegeuk Kim }
758127e670aSJaegeuk Kim 
759c227f912SChao Yu static void __add_dirty_inode(struct inode *inode, enum inode_type type)
760127e670aSJaegeuk Kim {
7614081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
762c227f912SChao Yu 	int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
7632d7b822aSChao Yu 
764*91942321SJaegeuk Kim 	if (is_inode_flag_set(inode, flag))
7652710fd7eSChao Yu 		return;
7662d7b822aSChao Yu 
767*91942321SJaegeuk Kim 	set_inode_flag(inode, flag);
768*91942321SJaegeuk Kim 	list_add_tail(&F2FS_I(inode)->dirty_list, &sbi->inode_list[type]);
76933fbd510SChao Yu 	stat_inc_dirty_inode(sbi, type);
7705deb8267SJaegeuk Kim }
7715deb8267SJaegeuk Kim 
772c227f912SChao Yu static void __remove_dirty_inode(struct inode *inode, enum inode_type type)
7736ad7609aSChao Yu {
774c227f912SChao Yu 	int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
7756ad7609aSChao Yu 
776*91942321SJaegeuk Kim 	if (get_dirty_pages(inode) || !is_inode_flag_set(inode, flag))
7776ad7609aSChao Yu 		return;
7786ad7609aSChao Yu 
779*91942321SJaegeuk Kim 	list_del_init(&F2FS_I(inode)->dirty_list);
780*91942321SJaegeuk Kim 	clear_inode_flag(inode, flag);
78133fbd510SChao Yu 	stat_dec_dirty_inode(F2FS_I_SB(inode), type);
7826ad7609aSChao Yu }
7836ad7609aSChao Yu 
784a7ffdbe2SJaegeuk Kim void update_dirty_page(struct inode *inode, struct page *page)
7855deb8267SJaegeuk Kim {
7864081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
787c227f912SChao Yu 	enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
7885deb8267SJaegeuk Kim 
7895ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
7905ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
791127e670aSJaegeuk Kim 		return;
7927bd59381SGu Zheng 
793c227f912SChao Yu 	spin_lock(&sbi->inode_lock[type]);
7941c4bf763SJaegeuk Kim 	if (type != FILE_INODE || test_opt(sbi, DATA_FLUSH))
795c227f912SChao Yu 		__add_dirty_inode(inode, type);
796b951a4ecSYunlei He 	inode_inc_dirty_pages(inode);
7971c4bf763SJaegeuk Kim 	spin_unlock(&sbi->inode_lock[type]);
7981c4bf763SJaegeuk Kim 
799a7ffdbe2SJaegeuk Kim 	SetPagePrivate(page);
8009e4ded3fSJaegeuk Kim 	f2fs_trace_pid(page);
8015deb8267SJaegeuk Kim }
802127e670aSJaegeuk Kim 
803c227f912SChao Yu void remove_dirty_inode(struct inode *inode)
804127e670aSJaegeuk Kim {
8054081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
806c227f912SChao Yu 	enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
807127e670aSJaegeuk Kim 
808c227f912SChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
809c227f912SChao Yu 			!S_ISLNK(inode->i_mode))
810127e670aSJaegeuk Kim 		return;
811127e670aSJaegeuk Kim 
81210aa97c3SJaegeuk Kim 	if (type == FILE_INODE && !test_opt(sbi, DATA_FLUSH))
81310aa97c3SJaegeuk Kim 		return;
81410aa97c3SJaegeuk Kim 
815c227f912SChao Yu 	spin_lock(&sbi->inode_lock[type]);
816c227f912SChao Yu 	__remove_dirty_inode(inode, type);
817c227f912SChao Yu 	spin_unlock(&sbi->inode_lock[type]);
818afc3eda2SJaegeuk Kim }
81974d0b917SJaegeuk Kim 
8206d5a1495SChao Yu int sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type)
821127e670aSJaegeuk Kim {
822ce3b7d80SGu Zheng 	struct list_head *head;
823127e670aSJaegeuk Kim 	struct inode *inode;
8242710fd7eSChao Yu 	struct f2fs_inode_info *fi;
8254cf18537SChao Yu 	bool is_dir = (type == DIR_INODE);
8264cf18537SChao Yu 
8274cf18537SChao Yu 	trace_f2fs_sync_dirty_inodes_enter(sbi->sb, is_dir,
8284cf18537SChao Yu 				get_pages(sbi, is_dir ?
8294cf18537SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
830127e670aSJaegeuk Kim retry:
831af41d3eeSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
8326d5a1495SChao Yu 		return -EIO;
833af41d3eeSJaegeuk Kim 
834c227f912SChao Yu 	spin_lock(&sbi->inode_lock[type]);
835ce3b7d80SGu Zheng 
836c227f912SChao Yu 	head = &sbi->inode_list[type];
837127e670aSJaegeuk Kim 	if (list_empty(head)) {
838c227f912SChao Yu 		spin_unlock(&sbi->inode_lock[type]);
8394cf18537SChao Yu 		trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir,
8404cf18537SChao Yu 				get_pages(sbi, is_dir ?
8414cf18537SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
8426d5a1495SChao Yu 		return 0;
843127e670aSJaegeuk Kim 	}
8442710fd7eSChao Yu 	fi = list_entry(head->next, struct f2fs_inode_info, dirty_list);
8452710fd7eSChao Yu 	inode = igrab(&fi->vfs_inode);
846c227f912SChao Yu 	spin_unlock(&sbi->inode_lock[type]);
847127e670aSJaegeuk Kim 	if (inode) {
84887d6f890SJaegeuk Kim 		filemap_fdatawrite(inode->i_mapping);
849127e670aSJaegeuk Kim 		iput(inode);
850127e670aSJaegeuk Kim 	} else {
851127e670aSJaegeuk Kim 		/*
852127e670aSJaegeuk Kim 		 * We should submit bio, since it exists several
853127e670aSJaegeuk Kim 		 * wribacking dentry pages in the freeing inode.
854127e670aSJaegeuk Kim 		 */
855458e6197SJaegeuk Kim 		f2fs_submit_merged_bio(sbi, DATA, WRITE);
8567ecebe5eSSebastian Andrzej Siewior 		cond_resched();
857127e670aSJaegeuk Kim 	}
858127e670aSJaegeuk Kim 	goto retry;
859127e670aSJaegeuk Kim }
860127e670aSJaegeuk Kim 
8610a8165d7SJaegeuk Kim /*
862127e670aSJaegeuk Kim  * Freeze all the FS-operations for checkpoint.
863127e670aSJaegeuk Kim  */
864cf779cabSJaegeuk Kim static int block_operations(struct f2fs_sb_info *sbi)
865127e670aSJaegeuk Kim {
866127e670aSJaegeuk Kim 	struct writeback_control wbc = {
867127e670aSJaegeuk Kim 		.sync_mode = WB_SYNC_ALL,
868127e670aSJaegeuk Kim 		.nr_to_write = LONG_MAX,
869127e670aSJaegeuk Kim 		.for_reclaim = 0,
870127e670aSJaegeuk Kim 	};
871c718379bSJaegeuk Kim 	struct blk_plug plug;
872cf779cabSJaegeuk Kim 	int err = 0;
873c718379bSJaegeuk Kim 
874c718379bSJaegeuk Kim 	blk_start_plug(&plug);
875c718379bSJaegeuk Kim 
87639936837SJaegeuk Kim retry_flush_dents:
877e479556bSGu Zheng 	f2fs_lock_all(sbi);
878127e670aSJaegeuk Kim 	/* write all the dirty dentry pages */
879127e670aSJaegeuk Kim 	if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
880e479556bSGu Zheng 		f2fs_unlock_all(sbi);
8816d5a1495SChao Yu 		err = sync_dirty_inodes(sbi, DIR_INODE);
8826d5a1495SChao Yu 		if (err)
883cf779cabSJaegeuk Kim 			goto out;
88439936837SJaegeuk Kim 		goto retry_flush_dents;
885127e670aSJaegeuk Kim 	}
886127e670aSJaegeuk Kim 
887127e670aSJaegeuk Kim 	/*
888e1c42045Sarter97 	 * POR: we should ensure that there are no dirty node pages
889127e670aSJaegeuk Kim 	 * until finishing nat/sit flush.
890127e670aSJaegeuk Kim 	 */
89139936837SJaegeuk Kim retry_flush_nodes:
892b3582c68SChao Yu 	down_write(&sbi->node_write);
893127e670aSJaegeuk Kim 
894127e670aSJaegeuk Kim 	if (get_pages(sbi, F2FS_DIRTY_NODES)) {
895b3582c68SChao Yu 		up_write(&sbi->node_write);
89652681375SJaegeuk Kim 		err = sync_node_pages(sbi, &wbc);
8976d5a1495SChao Yu 		if (err) {
898cf779cabSJaegeuk Kim 			f2fs_unlock_all(sbi);
899cf779cabSJaegeuk Kim 			goto out;
900cf779cabSJaegeuk Kim 		}
90139936837SJaegeuk Kim 		goto retry_flush_nodes;
902127e670aSJaegeuk Kim 	}
903cf779cabSJaegeuk Kim out:
904c718379bSJaegeuk Kim 	blk_finish_plug(&plug);
905cf779cabSJaegeuk Kim 	return err;
906127e670aSJaegeuk Kim }
907127e670aSJaegeuk Kim 
908127e670aSJaegeuk Kim static void unblock_operations(struct f2fs_sb_info *sbi)
909127e670aSJaegeuk Kim {
910b3582c68SChao Yu 	up_write(&sbi->node_write);
911e479556bSGu Zheng 	f2fs_unlock_all(sbi);
912127e670aSJaegeuk Kim }
913127e670aSJaegeuk Kim 
914fb51b5efSChangman Lee static void wait_on_all_pages_writeback(struct f2fs_sb_info *sbi)
915fb51b5efSChangman Lee {
916fb51b5efSChangman Lee 	DEFINE_WAIT(wait);
917fb51b5efSChangman Lee 
918fb51b5efSChangman Lee 	for (;;) {
919fb51b5efSChangman Lee 		prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
920fb51b5efSChangman Lee 
921f5730184SJaegeuk Kim 		if (!atomic_read(&sbi->nr_wb_bios))
922fb51b5efSChangman Lee 			break;
923fb51b5efSChangman Lee 
9240ff21646SYunlei He 		io_schedule_timeout(5*HZ);
925fb51b5efSChangman Lee 	}
926fb51b5efSChangman Lee 	finish_wait(&sbi->cp_wait, &wait);
927fb51b5efSChangman Lee }
928fb51b5efSChangman Lee 
929c34f42e2SChao Yu static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
930127e670aSJaegeuk Kim {
931127e670aSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
932cf2271e7SJaegeuk Kim 	struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
93377041823SHuang Ying 	struct f2fs_nm_info *nm_i = NM_I(sbi);
93467298804SChao Yu 	unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num;
93577041823SHuang Ying 	nid_t last_nid = nm_i->next_scan_nid;
936127e670aSJaegeuk Kim 	block_t start_blk;
937127e670aSJaegeuk Kim 	unsigned int data_sum_blocks, orphan_blocks;
9387e586fa0SJaegeuk Kim 	__u32 crc32 = 0;
939127e670aSJaegeuk Kim 	int i;
94055141486SWanpeng Li 	int cp_payload_blks = __cp_payload(sbi);
941e90c2d28SChao Yu 	block_t discard_blk = NEXT_FREE_BLKADDR(sbi, curseg);
942e90c2d28SChao Yu 	bool invalidate = false;
9438f1dbbbbSShuoran Liu 	struct super_block *sb = sbi->sb;
9448f1dbbbbSShuoran Liu 	struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
9458f1dbbbbSShuoran Liu 	u64 kbytes_written;
946127e670aSJaegeuk Kim 
9471e87a78dSJaegeuk Kim 	/*
9481e87a78dSJaegeuk Kim 	 * This avoids to conduct wrong roll-forward operations and uses
9491e87a78dSJaegeuk Kim 	 * metapages, so should be called prior to sync_meta_pages below.
9501e87a78dSJaegeuk Kim 	 */
951e90c2d28SChao Yu 	if (discard_next_dnode(sbi, discard_blk))
952e90c2d28SChao Yu 		invalidate = true;
953127e670aSJaegeuk Kim 
954127e670aSJaegeuk Kim 	/* Flush all the NAT/SIT pages */
955cf779cabSJaegeuk Kim 	while (get_pages(sbi, F2FS_DIRTY_META)) {
956127e670aSJaegeuk Kim 		sync_meta_pages(sbi, META, LONG_MAX);
957cf779cabSJaegeuk Kim 		if (unlikely(f2fs_cp_error(sbi)))
958c34f42e2SChao Yu 			return -EIO;
959cf779cabSJaegeuk Kim 	}
960127e670aSJaegeuk Kim 
961127e670aSJaegeuk Kim 	next_free_nid(sbi, &last_nid);
962127e670aSJaegeuk Kim 
963127e670aSJaegeuk Kim 	/*
964127e670aSJaegeuk Kim 	 * modify checkpoint
965127e670aSJaegeuk Kim 	 * version number is already updated
966127e670aSJaegeuk Kim 	 */
967127e670aSJaegeuk Kim 	ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi));
968127e670aSJaegeuk Kim 	ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
969127e670aSJaegeuk Kim 	ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
970b5b82205SChao Yu 	for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
971127e670aSJaegeuk Kim 		ckpt->cur_node_segno[i] =
972127e670aSJaegeuk Kim 			cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE));
973127e670aSJaegeuk Kim 		ckpt->cur_node_blkoff[i] =
974127e670aSJaegeuk Kim 			cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE));
975127e670aSJaegeuk Kim 		ckpt->alloc_type[i + CURSEG_HOT_NODE] =
976127e670aSJaegeuk Kim 				curseg_alloc_type(sbi, i + CURSEG_HOT_NODE);
977127e670aSJaegeuk Kim 	}
978b5b82205SChao Yu 	for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
979127e670aSJaegeuk Kim 		ckpt->cur_data_segno[i] =
980127e670aSJaegeuk Kim 			cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA));
981127e670aSJaegeuk Kim 		ckpt->cur_data_blkoff[i] =
982127e670aSJaegeuk Kim 			cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA));
983127e670aSJaegeuk Kim 		ckpt->alloc_type[i + CURSEG_HOT_DATA] =
984127e670aSJaegeuk Kim 				curseg_alloc_type(sbi, i + CURSEG_HOT_DATA);
985127e670aSJaegeuk Kim 	}
986127e670aSJaegeuk Kim 
987127e670aSJaegeuk Kim 	ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi));
988127e670aSJaegeuk Kim 	ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi));
989127e670aSJaegeuk Kim 	ckpt->next_free_nid = cpu_to_le32(last_nid);
990127e670aSJaegeuk Kim 
991127e670aSJaegeuk Kim 	/* 2 cp  + n data seg summary + orphan inode blocks */
9923fa06d7bSChao Yu 	data_sum_blocks = npages_for_summary_flush(sbi, false);
993b5b82205SChao Yu 	if (data_sum_blocks < NR_CURSEG_DATA_TYPE)
99425ca923bSJaegeuk Kim 		set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
995127e670aSJaegeuk Kim 	else
99625ca923bSJaegeuk Kim 		clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
997127e670aSJaegeuk Kim 
99867298804SChao Yu 	orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num);
9991dbe4152SChangman Lee 	ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks +
10001dbe4152SChangman Lee 			orphan_blocks);
1001127e670aSJaegeuk Kim 
1002119ee914SJaegeuk Kim 	if (__remain_node_summaries(cpc->reason))
1003b5b82205SChao Yu 		ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS+
10041dbe4152SChangman Lee 				cp_payload_blks + data_sum_blocks +
10051dbe4152SChangman Lee 				orphan_blocks + NR_CURSEG_NODE_TYPE);
1006119ee914SJaegeuk Kim 	else
1007b5b82205SChao Yu 		ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
10081dbe4152SChangman Lee 				cp_payload_blks + data_sum_blocks +
10091dbe4152SChangman Lee 				orphan_blocks);
1010119ee914SJaegeuk Kim 
1011119ee914SJaegeuk Kim 	if (cpc->reason == CP_UMOUNT)
1012119ee914SJaegeuk Kim 		set_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1013119ee914SJaegeuk Kim 	else
1014119ee914SJaegeuk Kim 		clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1015119ee914SJaegeuk Kim 
1016119ee914SJaegeuk Kim 	if (cpc->reason == CP_FASTBOOT)
1017119ee914SJaegeuk Kim 		set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1018119ee914SJaegeuk Kim 	else
1019119ee914SJaegeuk Kim 		clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1020127e670aSJaegeuk Kim 
102167298804SChao Yu 	if (orphan_num)
102225ca923bSJaegeuk Kim 		set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1023127e670aSJaegeuk Kim 	else
102425ca923bSJaegeuk Kim 		clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1025127e670aSJaegeuk Kim 
1026caf0047eSChao Yu 	if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
10272ae4c673SJaegeuk Kim 		set_ckpt_flags(ckpt, CP_FSCK_FLAG);
10282ae4c673SJaegeuk Kim 
1029127e670aSJaegeuk Kim 	/* update SIT/NAT bitmap */
1030127e670aSJaegeuk Kim 	get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
1031127e670aSJaegeuk Kim 	get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
1032127e670aSJaegeuk Kim 
103343b6573bSKeith Mok 	crc32 = f2fs_crc32(sbi, ckpt, le32_to_cpu(ckpt->checksum_offset));
10347e586fa0SJaegeuk Kim 	*((__le32 *)((unsigned char *)ckpt +
10357e586fa0SJaegeuk Kim 				le32_to_cpu(ckpt->checksum_offset)))
1036127e670aSJaegeuk Kim 				= cpu_to_le32(crc32);
1037127e670aSJaegeuk Kim 
1038127e670aSJaegeuk Kim 	start_blk = __start_cp_addr(sbi);
1039127e670aSJaegeuk Kim 
1040a7230d16SJaegeuk Kim 	/* need to wait for end_io results */
1041a7230d16SJaegeuk Kim 	wait_on_all_pages_writeback(sbi);
1042a7230d16SJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
1043c34f42e2SChao Yu 		return -EIO;
1044a7230d16SJaegeuk Kim 
1045127e670aSJaegeuk Kim 	/* write out checkpoint buffer at block 0 */
1046381722d2SChao Yu 	update_meta_page(sbi, ckpt, start_blk++);
1047127e670aSJaegeuk Kim 
1048381722d2SChao Yu 	for (i = 1; i < 1 + cp_payload_blks; i++)
1049381722d2SChao Yu 		update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE,
1050381722d2SChao Yu 							start_blk++);
10511dbe4152SChangman Lee 
105267298804SChao Yu 	if (orphan_num) {
1053127e670aSJaegeuk Kim 		write_orphan_inodes(sbi, start_blk);
1054127e670aSJaegeuk Kim 		start_blk += orphan_blocks;
1055127e670aSJaegeuk Kim 	}
1056127e670aSJaegeuk Kim 
1057127e670aSJaegeuk Kim 	write_data_summaries(sbi, start_blk);
1058127e670aSJaegeuk Kim 	start_blk += data_sum_blocks;
10598f1dbbbbSShuoran Liu 
10608f1dbbbbSShuoran Liu 	/* Record write statistics in the hot node summary */
10618f1dbbbbSShuoran Liu 	kbytes_written = sbi->kbytes_written;
10628f1dbbbbSShuoran Liu 	if (sb->s_bdev->bd_part)
10638f1dbbbbSShuoran Liu 		kbytes_written += BD_PART_WRITTEN(sbi);
10648f1dbbbbSShuoran Liu 
1065b7ad7512SChao Yu 	seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written);
10668f1dbbbbSShuoran Liu 
1067119ee914SJaegeuk Kim 	if (__remain_node_summaries(cpc->reason)) {
1068127e670aSJaegeuk Kim 		write_node_summaries(sbi, start_blk);
1069127e670aSJaegeuk Kim 		start_blk += NR_CURSEG_NODE_TYPE;
1070127e670aSJaegeuk Kim 	}
1071127e670aSJaegeuk Kim 
1072127e670aSJaegeuk Kim 	/* writeout checkpoint block */
1073381722d2SChao Yu 	update_meta_page(sbi, ckpt, start_blk);
1074127e670aSJaegeuk Kim 
1075127e670aSJaegeuk Kim 	/* wait for previous submitted node/meta pages writeback */
1076fb51b5efSChangman Lee 	wait_on_all_pages_writeback(sbi);
1077127e670aSJaegeuk Kim 
1078cf779cabSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
1079c34f42e2SChao Yu 		return -EIO;
1080cf779cabSJaegeuk Kim 
108180dd9c0eSChao Yu 	filemap_fdatawait_range(NODE_MAPPING(sbi), 0, LLONG_MAX);
108280dd9c0eSChao Yu 	filemap_fdatawait_range(META_MAPPING(sbi), 0, LLONG_MAX);
1083127e670aSJaegeuk Kim 
1084127e670aSJaegeuk Kim 	/* update user_block_counts */
1085127e670aSJaegeuk Kim 	sbi->last_valid_block_count = sbi->total_valid_block_count;
108641382ec4SJaegeuk Kim 	percpu_counter_set(&sbi->alloc_valid_block_count, 0);
1087127e670aSJaegeuk Kim 
1088127e670aSJaegeuk Kim 	/* Here, we only have one bio having CP pack */
1089127e670aSJaegeuk Kim 	sync_meta_pages(sbi, META_FLUSH, LONG_MAX);
1090127e670aSJaegeuk Kim 
10916a8f8ca5SJaegeuk Kim 	/* wait for previous submitted meta pages writeback */
10926a8f8ca5SJaegeuk Kim 	wait_on_all_pages_writeback(sbi);
10936a8f8ca5SJaegeuk Kim 
1094e90c2d28SChao Yu 	/*
1095e90c2d28SChao Yu 	 * invalidate meta page which is used temporarily for zeroing out
1096e90c2d28SChao Yu 	 * block at the end of warm node chain.
1097e90c2d28SChao Yu 	 */
1098e90c2d28SChao Yu 	if (invalidate)
1099e90c2d28SChao Yu 		invalidate_mapping_pages(META_MAPPING(sbi), discard_blk,
1100e90c2d28SChao Yu 								discard_blk);
1101e90c2d28SChao Yu 
110274ef9241SJaegeuk Kim 	release_ino_entry(sbi, false);
1103cf779cabSJaegeuk Kim 
1104cf779cabSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
1105c34f42e2SChao Yu 		return -EIO;
1106cf779cabSJaegeuk Kim 
1107836b5a63SJaegeuk Kim 	clear_prefree_segments(sbi, cpc);
1108caf0047eSChao Yu 	clear_sbi_flag(sbi, SBI_IS_DIRTY);
1109c34f42e2SChao Yu 
1110c34f42e2SChao Yu 	return 0;
1111127e670aSJaegeuk Kim }
1112127e670aSJaegeuk Kim 
11130a8165d7SJaegeuk Kim /*
1114e1c42045Sarter97  * We guarantee that this checkpoint procedure will not fail.
1115127e670aSJaegeuk Kim  */
1116c34f42e2SChao Yu int write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1117127e670aSJaegeuk Kim {
1118127e670aSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1119127e670aSJaegeuk Kim 	unsigned long long ckpt_ver;
1120c34f42e2SChao Yu 	int err = 0;
1121127e670aSJaegeuk Kim 
1122127e670aSJaegeuk Kim 	mutex_lock(&sbi->cp_mutex);
11238501017eSJaegeuk Kim 
1124caf0047eSChao Yu 	if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) &&
1125a66cdd98SJaegeuk Kim 		(cpc->reason == CP_FASTBOOT || cpc->reason == CP_SYNC ||
1126a66cdd98SJaegeuk Kim 		(cpc->reason == CP_DISCARD && !sbi->discard_blks)))
11278501017eSJaegeuk Kim 		goto out;
1128c34f42e2SChao Yu 	if (unlikely(f2fs_cp_error(sbi))) {
1129c34f42e2SChao Yu 		err = -EIO;
1130cf779cabSJaegeuk Kim 		goto out;
1131c34f42e2SChao Yu 	}
1132c34f42e2SChao Yu 	if (f2fs_readonly(sbi->sb)) {
1133c34f42e2SChao Yu 		err = -EROFS;
113411504a8eSJaegeuk Kim 		goto out;
1135c34f42e2SChao Yu 	}
11362bda542dSWanpeng Li 
11372bda542dSWanpeng Li 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops");
11382bda542dSWanpeng Li 
1139c34f42e2SChao Yu 	err = block_operations(sbi);
1140c34f42e2SChao Yu 	if (err)
1141cf779cabSJaegeuk Kim 		goto out;
1142127e670aSJaegeuk Kim 
114375ab4cb8SJaegeuk Kim 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops");
11442af4bd6cSNamjae Jeon 
1145406657ddSChao Yu 	f2fs_flush_merged_bios(sbi);
1146127e670aSJaegeuk Kim 
1147127e670aSJaegeuk Kim 	/*
1148127e670aSJaegeuk Kim 	 * update checkpoint pack index
1149127e670aSJaegeuk Kim 	 * Increase the version number so that
1150127e670aSJaegeuk Kim 	 * SIT entries and seg summaries are written at correct place
1151127e670aSJaegeuk Kim 	 */
1152d71b5564SJaegeuk Kim 	ckpt_ver = cur_cp_version(ckpt);
1153127e670aSJaegeuk Kim 	ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
1154127e670aSJaegeuk Kim 
1155127e670aSJaegeuk Kim 	/* write cached NAT/SIT entries to NAT/SIT area */
1156127e670aSJaegeuk Kim 	flush_nat_entries(sbi);
11574b2fecc8SJaegeuk Kim 	flush_sit_entries(sbi, cpc);
1158127e670aSJaegeuk Kim 
1159127e670aSJaegeuk Kim 	/* unlock all the fs_lock[] in do_checkpoint() */
1160c34f42e2SChao Yu 	err = do_checkpoint(sbi, cpc);
1161127e670aSJaegeuk Kim 
1162127e670aSJaegeuk Kim 	unblock_operations(sbi);
1163942e0be6SChangman Lee 	stat_inc_cp_count(sbi->stat_info);
116410027551SJaegeuk Kim 
116510027551SJaegeuk Kim 	if (cpc->reason == CP_RECOVERY)
116610027551SJaegeuk Kim 		f2fs_msg(sbi->sb, KERN_NOTICE,
116710027551SJaegeuk Kim 			"checkpoint: version = %llx", ckpt_ver);
116860b99b48SJaegeuk Kim 
116960b99b48SJaegeuk Kim 	/* do checkpoint periodically */
11706beceb54SJaegeuk Kim 	f2fs_update_time(sbi, CP_TIME);
117155d1cdb2SJaegeuk Kim 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint");
11728501017eSJaegeuk Kim out:
11738501017eSJaegeuk Kim 	mutex_unlock(&sbi->cp_mutex);
1174c34f42e2SChao Yu 	return err;
1175127e670aSJaegeuk Kim }
1176127e670aSJaegeuk Kim 
11776451e041SJaegeuk Kim void init_ino_entry_info(struct f2fs_sb_info *sbi)
1178127e670aSJaegeuk Kim {
11796451e041SJaegeuk Kim 	int i;
11806451e041SJaegeuk Kim 
11816451e041SJaegeuk Kim 	for (i = 0; i < MAX_INO_ENTRY; i++) {
118267298804SChao Yu 		struct inode_management *im = &sbi->im[i];
118367298804SChao Yu 
118467298804SChao Yu 		INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC);
118567298804SChao Yu 		spin_lock_init(&im->ino_lock);
118667298804SChao Yu 		INIT_LIST_HEAD(&im->ino_list);
118767298804SChao Yu 		im->ino_num = 0;
11886451e041SJaegeuk Kim 	}
11896451e041SJaegeuk Kim 
1190b5b82205SChao Yu 	sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS -
119114b42817SWanpeng Li 			NR_CURSEG_TYPE - __cp_payload(sbi)) *
119214b42817SWanpeng Li 				F2FS_ORPHANS_PER_BLOCK;
1193127e670aSJaegeuk Kim }
1194127e670aSJaegeuk Kim 
11956e6093a8SNamjae Jeon int __init create_checkpoint_caches(void)
1196127e670aSJaegeuk Kim {
11976451e041SJaegeuk Kim 	ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry",
11986451e041SJaegeuk Kim 			sizeof(struct ino_entry));
11996451e041SJaegeuk Kim 	if (!ino_entry_slab)
1200127e670aSJaegeuk Kim 		return -ENOMEM;
120106292073SChao Yu 	inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry",
120206292073SChao Yu 			sizeof(struct inode_entry));
12036bacf52fSJaegeuk Kim 	if (!inode_entry_slab) {
12046451e041SJaegeuk Kim 		kmem_cache_destroy(ino_entry_slab);
1205127e670aSJaegeuk Kim 		return -ENOMEM;
1206127e670aSJaegeuk Kim 	}
1207127e670aSJaegeuk Kim 	return 0;
1208127e670aSJaegeuk Kim }
1209127e670aSJaegeuk Kim 
1210127e670aSJaegeuk Kim void destroy_checkpoint_caches(void)
1211127e670aSJaegeuk Kim {
12126451e041SJaegeuk Kim 	kmem_cache_destroy(ino_entry_slab);
1213127e670aSJaegeuk Kim 	kmem_cache_destroy(inode_entry_slab);
1214127e670aSJaegeuk Kim }
1215