xref: /openbmc/linux/fs/f2fs/checkpoint.c (revision 55523519bc7227e651fd4febeb3aafdd22b8af1c)
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 {
31aaec2b1dSChao Yu 	set_ckpt_flags(sbi, 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);
51237c0790SJaegeuk Kim 	if (!PageUptodate(page))
52127e670aSJaegeuk Kim 		SetPageUptodate(page);
53127e670aSJaegeuk Kim 	return page;
54127e670aSJaegeuk Kim }
55127e670aSJaegeuk Kim 
560a8165d7SJaegeuk Kim /*
57127e670aSJaegeuk Kim  * We guarantee no failure on the returned page.
58127e670aSJaegeuk Kim  */
592b947003SChao Yu static struct page *__get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index,
602b947003SChao Yu 							bool is_meta)
61127e670aSJaegeuk Kim {
629df27d98SGu Zheng 	struct address_space *mapping = META_MAPPING(sbi);
63127e670aSJaegeuk Kim 	struct page *page;
64cf04e8ebSJaegeuk Kim 	struct f2fs_io_info fio = {
6505ca3632SJaegeuk Kim 		.sbi = sbi,
66cf04e8ebSJaegeuk Kim 		.type = META,
6704d328deSMike Christie 		.op = REQ_OP_READ,
6870fd7614SChristoph Hellwig 		.op_flags = REQ_META | REQ_PRIO,
697a9d7548SChao Yu 		.old_blkaddr = index,
707a9d7548SChao Yu 		.new_blkaddr = index,
714375a336SJaegeuk Kim 		.encrypted_page = NULL,
72cf04e8ebSJaegeuk Kim 	};
732b947003SChao Yu 
742b947003SChao Yu 	if (unlikely(!is_meta))
7504d328deSMike Christie 		fio.op_flags &= ~REQ_META;
76127e670aSJaegeuk Kim repeat:
77300e129cSJaegeuk Kim 	page = f2fs_grab_cache_page(mapping, index, false);
78127e670aSJaegeuk Kim 	if (!page) {
79127e670aSJaegeuk Kim 		cond_resched();
80127e670aSJaegeuk Kim 		goto repeat;
81127e670aSJaegeuk Kim 	}
82393ff91fSJaegeuk Kim 	if (PageUptodate(page))
83393ff91fSJaegeuk Kim 		goto out;
84127e670aSJaegeuk Kim 
8505ca3632SJaegeuk Kim 	fio.page = page;
8605ca3632SJaegeuk Kim 
8786531d6bSJaegeuk Kim 	if (f2fs_submit_page_bio(&fio)) {
8886531d6bSJaegeuk Kim 		f2fs_put_page(page, 1);
89393ff91fSJaegeuk Kim 		goto repeat;
9086531d6bSJaegeuk Kim 	}
91393ff91fSJaegeuk Kim 
92393ff91fSJaegeuk Kim 	lock_page(page);
936bacf52fSJaegeuk Kim 	if (unlikely(page->mapping != mapping)) {
94afcb7ca0SJaegeuk Kim 		f2fs_put_page(page, 1);
95afcb7ca0SJaegeuk Kim 		goto repeat;
96afcb7ca0SJaegeuk Kim 	}
97f3f338caSChao Yu 
98f3f338caSChao Yu 	/*
99f3f338caSChao Yu 	 * if there is any IO error when accessing device, make our filesystem
100f3f338caSChao Yu 	 * readonly and make sure do not write checkpoint with non-uptodate
101f3f338caSChao Yu 	 * meta page.
102f3f338caSChao Yu 	 */
103f3f338caSChao Yu 	if (unlikely(!PageUptodate(page)))
10438f91ca8SJaegeuk Kim 		f2fs_stop_checkpoint(sbi, false);
105393ff91fSJaegeuk Kim out:
106127e670aSJaegeuk Kim 	return page;
107127e670aSJaegeuk Kim }
108127e670aSJaegeuk Kim 
1092b947003SChao Yu struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
1102b947003SChao Yu {
1112b947003SChao Yu 	return __get_meta_page(sbi, index, true);
1122b947003SChao Yu }
1132b947003SChao Yu 
1142b947003SChao Yu /* for POR only */
1152b947003SChao Yu struct page *get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index)
1162b947003SChao Yu {
1172b947003SChao Yu 	return __get_meta_page(sbi, index, false);
1182b947003SChao Yu }
1192b947003SChao Yu 
120f0c9cadaSChao Yu bool is_valid_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr, int type)
121662befdaSChao Yu {
122662befdaSChao Yu 	switch (type) {
123662befdaSChao Yu 	case META_NAT:
12466b00c18SChao Yu 		break;
125662befdaSChao Yu 	case META_SIT:
12666b00c18SChao Yu 		if (unlikely(blkaddr >= SIT_BLK_CNT(sbi)))
12766b00c18SChao Yu 			return false;
12866b00c18SChao Yu 		break;
12981c1a0f1SChao Yu 	case META_SSA:
13066b00c18SChao Yu 		if (unlikely(blkaddr >= MAIN_BLKADDR(sbi) ||
13166b00c18SChao Yu 			blkaddr < SM_I(sbi)->ssa_blkaddr))
13266b00c18SChao Yu 			return false;
13366b00c18SChao Yu 		break;
134662befdaSChao Yu 	case META_CP:
13566b00c18SChao Yu 		if (unlikely(blkaddr >= SIT_I(sbi)->sit_base_addr ||
13666b00c18SChao Yu 			blkaddr < __start_cp_addr(sbi)))
13766b00c18SChao Yu 			return false;
13866b00c18SChao Yu 		break;
1394c521f49SJaegeuk Kim 	case META_POR:
14066b00c18SChao Yu 		if (unlikely(blkaddr >= MAX_BLKADDR(sbi) ||
14166b00c18SChao Yu 			blkaddr < MAIN_BLKADDR(sbi)))
14266b00c18SChao Yu 			return false;
14366b00c18SChao Yu 		break;
144662befdaSChao Yu 	default:
145662befdaSChao Yu 		BUG();
146662befdaSChao Yu 	}
14766b00c18SChao Yu 
14866b00c18SChao Yu 	return true;
149662befdaSChao Yu }
150662befdaSChao Yu 
151662befdaSChao Yu /*
15281c1a0f1SChao Yu  * Readahead CP/NAT/SIT/SSA pages
153662befdaSChao Yu  */
15426879fb1SChao Yu int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
15526879fb1SChao Yu 							int type, bool sync)
156662befdaSChao Yu {
157662befdaSChao Yu 	struct page *page;
1584c521f49SJaegeuk Kim 	block_t blkno = start;
159662befdaSChao Yu 	struct f2fs_io_info fio = {
16005ca3632SJaegeuk Kim 		.sbi = sbi,
161662befdaSChao Yu 		.type = META,
16204d328deSMike Christie 		.op = REQ_OP_READ,
16370fd7614SChristoph Hellwig 		.op_flags = sync ? (REQ_META | REQ_PRIO) : REQ_RAHEAD,
1644375a336SJaegeuk Kim 		.encrypted_page = NULL,
165662befdaSChao Yu 	};
166e9f5b8b8SChao Yu 	struct blk_plug plug;
167662befdaSChao Yu 
1682b947003SChao Yu 	if (unlikely(type == META_POR))
16904d328deSMike Christie 		fio.op_flags &= ~REQ_META;
1702b947003SChao Yu 
171e9f5b8b8SChao Yu 	blk_start_plug(&plug);
172662befdaSChao Yu 	for (; nrpages-- > 0; blkno++) {
173662befdaSChao Yu 
17466b00c18SChao Yu 		if (!is_valid_blkaddr(sbi, blkno, type))
17566b00c18SChao Yu 			goto out;
17666b00c18SChao Yu 
177662befdaSChao Yu 		switch (type) {
178662befdaSChao Yu 		case META_NAT:
17966b00c18SChao Yu 			if (unlikely(blkno >=
18066b00c18SChao Yu 					NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid)))
181662befdaSChao Yu 				blkno = 0;
18266b00c18SChao Yu 			/* get nat block addr */
1837a9d7548SChao Yu 			fio.new_blkaddr = current_nat_addr(sbi,
184662befdaSChao Yu 					blkno * NAT_ENTRY_PER_BLOCK);
185662befdaSChao Yu 			break;
186662befdaSChao Yu 		case META_SIT:
187662befdaSChao Yu 			/* get sit block addr */
1887a9d7548SChao Yu 			fio.new_blkaddr = current_sit_addr(sbi,
189662befdaSChao Yu 					blkno * SIT_ENTRY_PER_BLOCK);
190662befdaSChao Yu 			break;
19181c1a0f1SChao Yu 		case META_SSA:
192662befdaSChao Yu 		case META_CP:
1934c521f49SJaegeuk Kim 		case META_POR:
1947a9d7548SChao Yu 			fio.new_blkaddr = blkno;
195662befdaSChao Yu 			break;
196662befdaSChao Yu 		default:
197662befdaSChao Yu 			BUG();
198662befdaSChao Yu 		}
199662befdaSChao Yu 
200300e129cSJaegeuk Kim 		page = f2fs_grab_cache_page(META_MAPPING(sbi),
201300e129cSJaegeuk Kim 						fio.new_blkaddr, false);
202662befdaSChao Yu 		if (!page)
203662befdaSChao Yu 			continue;
204662befdaSChao Yu 		if (PageUptodate(page)) {
205662befdaSChao Yu 			f2fs_put_page(page, 1);
206662befdaSChao Yu 			continue;
207662befdaSChao Yu 		}
208662befdaSChao Yu 
20905ca3632SJaegeuk Kim 		fio.page = page;
2107a9d7548SChao Yu 		fio.old_blkaddr = fio.new_blkaddr;
21105ca3632SJaegeuk Kim 		f2fs_submit_page_mbio(&fio);
212662befdaSChao Yu 		f2fs_put_page(page, 0);
213662befdaSChao Yu 	}
214662befdaSChao Yu out:
215662befdaSChao Yu 	f2fs_submit_merged_bio(sbi, META, READ);
216e9f5b8b8SChao Yu 	blk_finish_plug(&plug);
217662befdaSChao Yu 	return blkno - start;
218662befdaSChao Yu }
219662befdaSChao Yu 
220635aee1fSChao Yu void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index)
221635aee1fSChao Yu {
222635aee1fSChao Yu 	struct page *page;
223635aee1fSChao Yu 	bool readahead = false;
224635aee1fSChao Yu 
225635aee1fSChao Yu 	page = find_get_page(META_MAPPING(sbi), index);
2264da7bf5aSJaegeuk Kim 	if (!page || !PageUptodate(page))
227635aee1fSChao Yu 		readahead = true;
228635aee1fSChao Yu 	f2fs_put_page(page, 0);
229635aee1fSChao Yu 
230635aee1fSChao Yu 	if (readahead)
231664ba972SJaegeuk Kim 		ra_meta_pages(sbi, index, BIO_MAX_PAGES, META_POR, true);
232635aee1fSChao Yu }
233635aee1fSChao Yu 
234127e670aSJaegeuk Kim static int f2fs_write_meta_page(struct page *page,
235127e670aSJaegeuk Kim 				struct writeback_control *wbc)
236127e670aSJaegeuk Kim {
2374081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_P_SB(page);
238577e3495SJaegeuk Kim 
239ecda0de3SChao Yu 	trace_f2fs_writepage(page, META);
240ecda0de3SChao Yu 
241caf0047eSChao Yu 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
242cfb271d4SChao Yu 		goto redirty_out;
243857dc4e0SJaegeuk Kim 	if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0))
244cfb271d4SChao Yu 		goto redirty_out;
2451e968fdfSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
246cf779cabSJaegeuk Kim 		goto redirty_out;
247127e670aSJaegeuk Kim 
248577e3495SJaegeuk Kim 	write_meta_page(sbi, page);
249127e670aSJaegeuk Kim 	dec_page_count(sbi, F2FS_DIRTY_META);
2500c3a5797SChao Yu 
2510c3a5797SChao Yu 	if (wbc->for_reclaim)
252942fd319SJaegeuk Kim 		f2fs_submit_merged_bio_cond(sbi, page->mapping->host,
253942fd319SJaegeuk Kim 						0, page->index, META, WRITE);
2540c3a5797SChao Yu 
255127e670aSJaegeuk Kim 	unlock_page(page);
256857dc4e0SJaegeuk Kim 
2570c3a5797SChao Yu 	if (unlikely(f2fs_cp_error(sbi)))
258857dc4e0SJaegeuk Kim 		f2fs_submit_merged_bio(sbi, META, WRITE);
2590c3a5797SChao Yu 
260577e3495SJaegeuk Kim 	return 0;
261cfb271d4SChao Yu 
262cfb271d4SChao Yu redirty_out:
26376f60268SJaegeuk Kim 	redirty_page_for_writepage(wbc, page);
264cfb271d4SChao Yu 	return AOP_WRITEPAGE_ACTIVATE;
265127e670aSJaegeuk Kim }
266127e670aSJaegeuk Kim 
267127e670aSJaegeuk Kim static int f2fs_write_meta_pages(struct address_space *mapping,
268127e670aSJaegeuk Kim 				struct writeback_control *wbc)
269127e670aSJaegeuk Kim {
2704081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
27150c8cdb3SJaegeuk Kim 	long diff, written;
272127e670aSJaegeuk Kim 
2735459aa97SJaegeuk Kim 	/* collect a number of dirty meta pages and write together */
27450c8cdb3SJaegeuk Kim 	if (wbc->for_kupdate ||
27550c8cdb3SJaegeuk Kim 		get_pages(sbi, F2FS_DIRTY_META) < nr_pages_to_skip(sbi, META))
276d3baf95dSJaegeuk Kim 		goto skip_write;
277127e670aSJaegeuk Kim 
278d31c7c3fSYunlei He 	trace_f2fs_writepages(mapping->host, wbc, META);
279d31c7c3fSYunlei He 
280127e670aSJaegeuk Kim 	/* if mounting is failed, skip writing node pages */
281127e670aSJaegeuk Kim 	mutex_lock(&sbi->cp_mutex);
28250c8cdb3SJaegeuk Kim 	diff = nr_pages_to_write(sbi, META, wbc);
28350c8cdb3SJaegeuk Kim 	written = sync_meta_pages(sbi, META, wbc->nr_to_write);
284127e670aSJaegeuk Kim 	mutex_unlock(&sbi->cp_mutex);
28550c8cdb3SJaegeuk Kim 	wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff);
286127e670aSJaegeuk Kim 	return 0;
287d3baf95dSJaegeuk Kim 
288d3baf95dSJaegeuk Kim skip_write:
289d3baf95dSJaegeuk Kim 	wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META);
290d31c7c3fSYunlei He 	trace_f2fs_writepages(mapping->host, wbc, META);
291d3baf95dSJaegeuk Kim 	return 0;
292127e670aSJaegeuk Kim }
293127e670aSJaegeuk Kim 
294127e670aSJaegeuk Kim long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
295127e670aSJaegeuk Kim 						long nr_to_write)
296127e670aSJaegeuk Kim {
2979df27d98SGu Zheng 	struct address_space *mapping = META_MAPPING(sbi);
29880dd9c0eSChao Yu 	pgoff_t index = 0, end = ULONG_MAX, prev = ULONG_MAX;
299127e670aSJaegeuk Kim 	struct pagevec pvec;
300127e670aSJaegeuk Kim 	long nwritten = 0;
301127e670aSJaegeuk Kim 	struct writeback_control wbc = {
302127e670aSJaegeuk Kim 		.for_reclaim = 0,
303127e670aSJaegeuk Kim 	};
304e9f5b8b8SChao Yu 	struct blk_plug plug;
305127e670aSJaegeuk Kim 
306127e670aSJaegeuk Kim 	pagevec_init(&pvec, 0);
307127e670aSJaegeuk Kim 
308e9f5b8b8SChao Yu 	blk_start_plug(&plug);
309e9f5b8b8SChao Yu 
310127e670aSJaegeuk Kim 	while (index <= end) {
311127e670aSJaegeuk Kim 		int i, nr_pages;
312127e670aSJaegeuk Kim 		nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
313127e670aSJaegeuk Kim 				PAGECACHE_TAG_DIRTY,
314127e670aSJaegeuk Kim 				min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
315cfb271d4SChao Yu 		if (unlikely(nr_pages == 0))
316127e670aSJaegeuk Kim 			break;
317127e670aSJaegeuk Kim 
318127e670aSJaegeuk Kim 		for (i = 0; i < nr_pages; i++) {
319127e670aSJaegeuk Kim 			struct page *page = pvec.pages[i];
320203681f6SJaegeuk Kim 
32180dd9c0eSChao Yu 			if (prev == ULONG_MAX)
3226066d8cdSJaegeuk Kim 				prev = page->index - 1;
3236066d8cdSJaegeuk Kim 			if (nr_to_write != LONG_MAX && page->index != prev + 1) {
3246066d8cdSJaegeuk Kim 				pagevec_release(&pvec);
3256066d8cdSJaegeuk Kim 				goto stop;
3266066d8cdSJaegeuk Kim 			}
3276066d8cdSJaegeuk Kim 
328127e670aSJaegeuk Kim 			lock_page(page);
329203681f6SJaegeuk Kim 
330203681f6SJaegeuk Kim 			if (unlikely(page->mapping != mapping)) {
331203681f6SJaegeuk Kim continue_unlock:
332203681f6SJaegeuk Kim 				unlock_page(page);
333203681f6SJaegeuk Kim 				continue;
334203681f6SJaegeuk Kim 			}
335203681f6SJaegeuk Kim 			if (!PageDirty(page)) {
336203681f6SJaegeuk Kim 				/* someone wrote it for us */
337203681f6SJaegeuk Kim 				goto continue_unlock;
338203681f6SJaegeuk Kim 			}
339203681f6SJaegeuk Kim 
340fa3d2bdfSJaegeuk Kim 			f2fs_wait_on_page_writeback(page, META, true);
341fa3d2bdfSJaegeuk Kim 
342fa3d2bdfSJaegeuk Kim 			BUG_ON(PageWriteback(page));
343203681f6SJaegeuk Kim 			if (!clear_page_dirty_for_io(page))
344203681f6SJaegeuk Kim 				goto continue_unlock;
345203681f6SJaegeuk Kim 
34697dc3fd2SChao Yu 			if (mapping->a_ops->writepage(page, &wbc)) {
347577e3495SJaegeuk Kim 				unlock_page(page);
348577e3495SJaegeuk Kim 				break;
349577e3495SJaegeuk Kim 			}
350cfb271d4SChao Yu 			nwritten++;
3516066d8cdSJaegeuk Kim 			prev = page->index;
352cfb271d4SChao Yu 			if (unlikely(nwritten >= nr_to_write))
353127e670aSJaegeuk Kim 				break;
354127e670aSJaegeuk Kim 		}
355127e670aSJaegeuk Kim 		pagevec_release(&pvec);
356127e670aSJaegeuk Kim 		cond_resched();
357127e670aSJaegeuk Kim 	}
3586066d8cdSJaegeuk Kim stop:
359127e670aSJaegeuk Kim 	if (nwritten)
360458e6197SJaegeuk Kim 		f2fs_submit_merged_bio(sbi, type, WRITE);
361127e670aSJaegeuk Kim 
362e9f5b8b8SChao Yu 	blk_finish_plug(&plug);
363e9f5b8b8SChao Yu 
364127e670aSJaegeuk Kim 	return nwritten;
365127e670aSJaegeuk Kim }
366127e670aSJaegeuk Kim 
367127e670aSJaegeuk Kim static int f2fs_set_meta_page_dirty(struct page *page)
368127e670aSJaegeuk Kim {
36926c6b887SJaegeuk Kim 	trace_f2fs_set_page_dirty(page, META);
37026c6b887SJaegeuk Kim 
371237c0790SJaegeuk Kim 	if (!PageUptodate(page))
372127e670aSJaegeuk Kim 		SetPageUptodate(page);
373127e670aSJaegeuk Kim 	if (!PageDirty(page)) {
374fe76b796SJaegeuk Kim 		f2fs_set_page_dirty_nobuffers(page);
3754081363fSJaegeuk Kim 		inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_META);
3761601839eSChao Yu 		SetPagePrivate(page);
3779e4ded3fSJaegeuk Kim 		f2fs_trace_pid(page);
378127e670aSJaegeuk Kim 		return 1;
379127e670aSJaegeuk Kim 	}
380127e670aSJaegeuk Kim 	return 0;
381127e670aSJaegeuk Kim }
382127e670aSJaegeuk Kim 
383127e670aSJaegeuk Kim const struct address_space_operations f2fs_meta_aops = {
384127e670aSJaegeuk Kim 	.writepage	= f2fs_write_meta_page,
385127e670aSJaegeuk Kim 	.writepages	= f2fs_write_meta_pages,
386127e670aSJaegeuk Kim 	.set_page_dirty	= f2fs_set_meta_page_dirty,
387487261f3SChao Yu 	.invalidatepage = f2fs_invalidate_page,
388487261f3SChao Yu 	.releasepage	= f2fs_release_page,
3895b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION
3905b7a487cSWeichao Guo 	.migratepage    = f2fs_migrate_page,
3915b7a487cSWeichao Guo #endif
392127e670aSJaegeuk Kim };
393127e670aSJaegeuk Kim 
3946451e041SJaegeuk Kim static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
395953e6cc6SJaegeuk Kim {
39667298804SChao Yu 	struct inode_management *im = &sbi->im[type];
39780c54505SJaegeuk Kim 	struct ino_entry *e, *tmp;
39880c54505SJaegeuk Kim 
39980c54505SJaegeuk Kim 	tmp = f2fs_kmem_cache_alloc(ino_entry_slab, GFP_NOFS);
40039efac41SJaegeuk Kim retry:
40180c54505SJaegeuk Kim 	radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
402769ec6e5SJaegeuk Kim 
40367298804SChao Yu 	spin_lock(&im->ino_lock);
40467298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
40539efac41SJaegeuk Kim 	if (!e) {
40680c54505SJaegeuk Kim 		e = tmp;
40767298804SChao Yu 		if (radix_tree_insert(&im->ino_root, ino, e)) {
40867298804SChao Yu 			spin_unlock(&im->ino_lock);
409769ec6e5SJaegeuk Kim 			radix_tree_preload_end();
41039efac41SJaegeuk Kim 			goto retry;
41139efac41SJaegeuk Kim 		}
41239efac41SJaegeuk Kim 		memset(e, 0, sizeof(struct ino_entry));
41339efac41SJaegeuk Kim 		e->ino = ino;
41439efac41SJaegeuk Kim 
41567298804SChao Yu 		list_add_tail(&e->list, &im->ino_list);
4168c402946SJaegeuk Kim 		if (type != ORPHAN_INO)
41767298804SChao Yu 			im->ino_num++;
41839efac41SJaegeuk Kim 	}
41967298804SChao Yu 	spin_unlock(&im->ino_lock);
420769ec6e5SJaegeuk Kim 	radix_tree_preload_end();
42180c54505SJaegeuk Kim 
42280c54505SJaegeuk Kim 	if (e != tmp)
42380c54505SJaegeuk Kim 		kmem_cache_free(ino_entry_slab, tmp);
424953e6cc6SJaegeuk Kim }
425953e6cc6SJaegeuk Kim 
4266451e041SJaegeuk Kim static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
427953e6cc6SJaegeuk Kim {
42867298804SChao Yu 	struct inode_management *im = &sbi->im[type];
4296451e041SJaegeuk Kim 	struct ino_entry *e;
430953e6cc6SJaegeuk Kim 
43167298804SChao Yu 	spin_lock(&im->ino_lock);
43267298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
43339efac41SJaegeuk Kim 	if (e) {
434953e6cc6SJaegeuk Kim 		list_del(&e->list);
43567298804SChao Yu 		radix_tree_delete(&im->ino_root, ino);
43667298804SChao Yu 		im->ino_num--;
43767298804SChao Yu 		spin_unlock(&im->ino_lock);
4386451e041SJaegeuk Kim 		kmem_cache_free(ino_entry_slab, e);
439953e6cc6SJaegeuk Kim 		return;
440953e6cc6SJaegeuk Kim 	}
44167298804SChao Yu 	spin_unlock(&im->ino_lock);
442953e6cc6SJaegeuk Kim }
443953e6cc6SJaegeuk Kim 
444a49324f1SChao Yu void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
445fff04f90SJaegeuk Kim {
446fff04f90SJaegeuk Kim 	/* add new dirty ino entry into list */
447fff04f90SJaegeuk Kim 	__add_ino_entry(sbi, ino, type);
448fff04f90SJaegeuk Kim }
449fff04f90SJaegeuk Kim 
450a49324f1SChao Yu void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
451fff04f90SJaegeuk Kim {
452fff04f90SJaegeuk Kim 	/* remove dirty ino entry from list */
453fff04f90SJaegeuk Kim 	__remove_ino_entry(sbi, ino, type);
454fff04f90SJaegeuk Kim }
455fff04f90SJaegeuk Kim 
456fff04f90SJaegeuk Kim /* mode should be APPEND_INO or UPDATE_INO */
457fff04f90SJaegeuk Kim bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode)
458fff04f90SJaegeuk Kim {
45967298804SChao Yu 	struct inode_management *im = &sbi->im[mode];
460fff04f90SJaegeuk Kim 	struct ino_entry *e;
46167298804SChao Yu 
46267298804SChao Yu 	spin_lock(&im->ino_lock);
46367298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
46467298804SChao Yu 	spin_unlock(&im->ino_lock);
465fff04f90SJaegeuk Kim 	return e ? true : false;
466fff04f90SJaegeuk Kim }
467fff04f90SJaegeuk Kim 
46874ef9241SJaegeuk Kim void release_ino_entry(struct f2fs_sb_info *sbi, bool all)
469fff04f90SJaegeuk Kim {
470fff04f90SJaegeuk Kim 	struct ino_entry *e, *tmp;
471fff04f90SJaegeuk Kim 	int i;
472fff04f90SJaegeuk Kim 
47374ef9241SJaegeuk Kim 	for (i = all ? ORPHAN_INO: APPEND_INO; i <= UPDATE_INO; i++) {
47467298804SChao Yu 		struct inode_management *im = &sbi->im[i];
47567298804SChao Yu 
47667298804SChao Yu 		spin_lock(&im->ino_lock);
47767298804SChao Yu 		list_for_each_entry_safe(e, tmp, &im->ino_list, list) {
478fff04f90SJaegeuk Kim 			list_del(&e->list);
47967298804SChao Yu 			radix_tree_delete(&im->ino_root, e->ino);
480fff04f90SJaegeuk Kim 			kmem_cache_free(ino_entry_slab, e);
48167298804SChao Yu 			im->ino_num--;
482fff04f90SJaegeuk Kim 		}
48367298804SChao Yu 		spin_unlock(&im->ino_lock);
484fff04f90SJaegeuk Kim 	}
485fff04f90SJaegeuk Kim }
486fff04f90SJaegeuk Kim 
487cbd56e7dSJaegeuk Kim int acquire_orphan_inode(struct f2fs_sb_info *sbi)
488127e670aSJaegeuk Kim {
48967298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
490127e670aSJaegeuk Kim 	int err = 0;
491127e670aSJaegeuk Kim 
49267298804SChao Yu 	spin_lock(&im->ino_lock);
493cb78942bSJaegeuk Kim 
494cb78942bSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
4951ecc0c5cSChao Yu 	if (time_to_inject(sbi, FAULT_ORPHAN)) {
496cb78942bSJaegeuk Kim 		spin_unlock(&im->ino_lock);
497*55523519SChao Yu 		f2fs_show_injection_info(FAULT_ORPHAN);
498cb78942bSJaegeuk Kim 		return -ENOSPC;
499cb78942bSJaegeuk Kim 	}
500cb78942bSJaegeuk Kim #endif
50167298804SChao Yu 	if (unlikely(im->ino_num >= sbi->max_orphans))
502127e670aSJaegeuk Kim 		err = -ENOSPC;
503cbd56e7dSJaegeuk Kim 	else
50467298804SChao Yu 		im->ino_num++;
50567298804SChao Yu 	spin_unlock(&im->ino_lock);
5060d47c1adSGu Zheng 
507127e670aSJaegeuk Kim 	return err;
508127e670aSJaegeuk Kim }
509127e670aSJaegeuk Kim 
510cbd56e7dSJaegeuk Kim void release_orphan_inode(struct f2fs_sb_info *sbi)
511cbd56e7dSJaegeuk Kim {
51267298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
51367298804SChao Yu 
51467298804SChao Yu 	spin_lock(&im->ino_lock);
51567298804SChao Yu 	f2fs_bug_on(sbi, im->ino_num == 0);
51667298804SChao Yu 	im->ino_num--;
51767298804SChao Yu 	spin_unlock(&im->ino_lock);
518cbd56e7dSJaegeuk Kim }
519cbd56e7dSJaegeuk Kim 
52067c3758dSJaegeuk Kim void add_orphan_inode(struct inode *inode)
521127e670aSJaegeuk Kim {
52239efac41SJaegeuk Kim 	/* add new orphan ino entry into list */
52367c3758dSJaegeuk Kim 	__add_ino_entry(F2FS_I_SB(inode), inode->i_ino, ORPHAN_INO);
52467c3758dSJaegeuk Kim 	update_inode_page(inode);
525127e670aSJaegeuk Kim }
526127e670aSJaegeuk Kim 
527127e670aSJaegeuk Kim void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
528127e670aSJaegeuk Kim {
529953e6cc6SJaegeuk Kim 	/* remove orphan entry from orphan list */
5306451e041SJaegeuk Kim 	__remove_ino_entry(sbi, ino, ORPHAN_INO);
531127e670aSJaegeuk Kim }
532127e670aSJaegeuk Kim 
5338c14bfadSChao Yu static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
534127e670aSJaegeuk Kim {
5358c14bfadSChao Yu 	struct inode *inode;
5365905f9afSJaegeuk Kim 	struct node_info ni;
537d41065e2SJaegeuk Kim 	int err = acquire_orphan_inode(sbi);
538d41065e2SJaegeuk Kim 
539d41065e2SJaegeuk Kim 	if (err) {
540d41065e2SJaegeuk Kim 		set_sbi_flag(sbi, SBI_NEED_FSCK);
541d41065e2SJaegeuk Kim 		f2fs_msg(sbi->sb, KERN_WARNING,
542d41065e2SJaegeuk Kim 				"%s: orphan failed (ino=%x), run fsck to fix.",
543d41065e2SJaegeuk Kim 				__func__, ino);
544d41065e2SJaegeuk Kim 		return err;
545d41065e2SJaegeuk Kim 	}
546d41065e2SJaegeuk Kim 
547d41065e2SJaegeuk Kim 	__add_ino_entry(sbi, ino, ORPHAN_INO);
5488c14bfadSChao Yu 
5495905f9afSJaegeuk Kim 	inode = f2fs_iget_retry(sbi->sb, ino);
5508c14bfadSChao Yu 	if (IS_ERR(inode)) {
5518c14bfadSChao Yu 		/*
5528c14bfadSChao Yu 		 * there should be a bug that we can't find the entry
5538c14bfadSChao Yu 		 * to orphan inode.
5548c14bfadSChao Yu 		 */
5558c14bfadSChao Yu 		f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT);
5568c14bfadSChao Yu 		return PTR_ERR(inode);
5578c14bfadSChao Yu 	}
5588c14bfadSChao Yu 
559127e670aSJaegeuk Kim 	clear_nlink(inode);
560127e670aSJaegeuk Kim 
561127e670aSJaegeuk Kim 	/* truncate all the data during iput */
562127e670aSJaegeuk Kim 	iput(inode);
5635905f9afSJaegeuk Kim 
5645905f9afSJaegeuk Kim 	get_node_info(sbi, ino, &ni);
5655905f9afSJaegeuk Kim 
5665905f9afSJaegeuk Kim 	/* ENOMEM was fully retried in f2fs_evict_inode. */
5675905f9afSJaegeuk Kim 	if (ni.blk_addr != NULL_ADDR) {
5685905f9afSJaegeuk Kim 		set_sbi_flag(sbi, SBI_NEED_FSCK);
5695905f9afSJaegeuk Kim 		f2fs_msg(sbi->sb, KERN_WARNING,
5705905f9afSJaegeuk Kim 			"%s: orphan failed (ino=%x), run fsck to fix.",
5715905f9afSJaegeuk Kim 				__func__, ino);
572d41065e2SJaegeuk Kim 		return -EIO;
5735905f9afSJaegeuk Kim 	}
574d41065e2SJaegeuk Kim 	__remove_ino_entry(sbi, ino, ORPHAN_INO);
5758c14bfadSChao Yu 	return 0;
576127e670aSJaegeuk Kim }
577127e670aSJaegeuk Kim 
5788c14bfadSChao Yu int recover_orphan_inodes(struct f2fs_sb_info *sbi)
579127e670aSJaegeuk Kim {
5803c642985SWanpeng Li 	block_t start_blk, orphan_blocks, i, j;
5818c14bfadSChao Yu 	int err;
582127e670aSJaegeuk Kim 
583aaec2b1dSChao Yu 	if (!is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG))
5848c14bfadSChao Yu 		return 0;
585127e670aSJaegeuk Kim 
58655141486SWanpeng Li 	start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
5873c642985SWanpeng Li 	orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
588127e670aSJaegeuk Kim 
58926879fb1SChao Yu 	ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true);
590662befdaSChao Yu 
5913c642985SWanpeng Li 	for (i = 0; i < orphan_blocks; i++) {
592127e670aSJaegeuk Kim 		struct page *page = get_meta_page(sbi, start_blk + i);
593127e670aSJaegeuk Kim 		struct f2fs_orphan_block *orphan_blk;
594127e670aSJaegeuk Kim 
595127e670aSJaegeuk Kim 		orphan_blk = (struct f2fs_orphan_block *)page_address(page);
596127e670aSJaegeuk Kim 		for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
597127e670aSJaegeuk Kim 			nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
5988c14bfadSChao Yu 			err = recover_orphan_inode(sbi, ino);
5998c14bfadSChao Yu 			if (err) {
6008c14bfadSChao Yu 				f2fs_put_page(page, 1);
6018c14bfadSChao Yu 				return err;
6028c14bfadSChao Yu 			}
603127e670aSJaegeuk Kim 		}
604127e670aSJaegeuk Kim 		f2fs_put_page(page, 1);
605127e670aSJaegeuk Kim 	}
606127e670aSJaegeuk Kim 	/* clear Orphan Flag */
607aaec2b1dSChao Yu 	clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
6088c14bfadSChao Yu 	return 0;
609127e670aSJaegeuk Kim }
610127e670aSJaegeuk Kim 
611127e670aSJaegeuk Kim static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
612127e670aSJaegeuk Kim {
613502c6e0bSGu Zheng 	struct list_head *head;
614127e670aSJaegeuk Kim 	struct f2fs_orphan_block *orphan_blk = NULL;
615127e670aSJaegeuk Kim 	unsigned int nentries = 0;
616bd936f84SChao Yu 	unsigned short index = 1;
6178c402946SJaegeuk Kim 	unsigned short orphan_blocks;
6184531929eSGu Zheng 	struct page *page = NULL;
6196451e041SJaegeuk Kim 	struct ino_entry *orphan = NULL;
62067298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
621127e670aSJaegeuk Kim 
62267298804SChao Yu 	orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num);
6238c402946SJaegeuk Kim 
624d6c67a4fSJaegeuk Kim 	/*
625d6c67a4fSJaegeuk Kim 	 * we don't need to do spin_lock(&im->ino_lock) here, since all the
626d6c67a4fSJaegeuk Kim 	 * orphan inode operations are covered under f2fs_lock_op().
627d6c67a4fSJaegeuk Kim 	 * And, spin_lock should be avoided due to page operations below.
628d6c67a4fSJaegeuk Kim 	 */
62967298804SChao Yu 	head = &im->ino_list;
630127e670aSJaegeuk Kim 
631127e670aSJaegeuk Kim 	/* loop for each orphan inode entry and write them in Jornal block */
632502c6e0bSGu Zheng 	list_for_each_entry(orphan, head, list) {
633502c6e0bSGu Zheng 		if (!page) {
634bd936f84SChao Yu 			page = grab_meta_page(sbi, start_blk++);
635502c6e0bSGu Zheng 			orphan_blk =
636502c6e0bSGu Zheng 				(struct f2fs_orphan_block *)page_address(page);
637502c6e0bSGu Zheng 			memset(orphan_blk, 0, sizeof(*orphan_blk));
638502c6e0bSGu Zheng 		}
639127e670aSJaegeuk Kim 
64036795567SGu Zheng 		orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
641127e670aSJaegeuk Kim 
64236795567SGu Zheng 		if (nentries == F2FS_ORPHANS_PER_BLOCK) {
643127e670aSJaegeuk Kim 			/*
644127e670aSJaegeuk Kim 			 * an orphan block is full of 1020 entries,
645127e670aSJaegeuk Kim 			 * then we need to flush current orphan blocks
646127e670aSJaegeuk Kim 			 * and bring another one in memory
647127e670aSJaegeuk Kim 			 */
648127e670aSJaegeuk Kim 			orphan_blk->blk_addr = cpu_to_le16(index);
649127e670aSJaegeuk Kim 			orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
650127e670aSJaegeuk Kim 			orphan_blk->entry_count = cpu_to_le32(nentries);
651127e670aSJaegeuk Kim 			set_page_dirty(page);
652127e670aSJaegeuk Kim 			f2fs_put_page(page, 1);
653127e670aSJaegeuk Kim 			index++;
654127e670aSJaegeuk Kim 			nentries = 0;
655127e670aSJaegeuk Kim 			page = NULL;
656127e670aSJaegeuk Kim 		}
657127e670aSJaegeuk Kim 	}
658127e670aSJaegeuk Kim 
659502c6e0bSGu Zheng 	if (page) {
660127e670aSJaegeuk Kim 		orphan_blk->blk_addr = cpu_to_le16(index);
661127e670aSJaegeuk Kim 		orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
662127e670aSJaegeuk Kim 		orphan_blk->entry_count = cpu_to_le32(nentries);
663127e670aSJaegeuk Kim 		set_page_dirty(page);
664127e670aSJaegeuk Kim 		f2fs_put_page(page, 1);
665502c6e0bSGu Zheng 	}
666127e670aSJaegeuk Kim }
667127e670aSJaegeuk Kim 
668fc0065adSTiezhu Yang static int get_checkpoint_version(struct f2fs_sb_info *sbi, block_t cp_addr,
669fc0065adSTiezhu Yang 		struct f2fs_checkpoint **cp_block, struct page **cp_page,
670fc0065adSTiezhu Yang 		unsigned long long *version)
671fc0065adSTiezhu Yang {
672fc0065adSTiezhu Yang 	unsigned long blk_size = sbi->blocksize;
673fc0065adSTiezhu Yang 	size_t crc_offset = 0;
674fc0065adSTiezhu Yang 	__u32 crc = 0;
675fc0065adSTiezhu Yang 
676fc0065adSTiezhu Yang 	*cp_page = get_meta_page(sbi, cp_addr);
677fc0065adSTiezhu Yang 	*cp_block = (struct f2fs_checkpoint *)page_address(*cp_page);
678fc0065adSTiezhu Yang 
679fc0065adSTiezhu Yang 	crc_offset = le32_to_cpu((*cp_block)->checksum_offset);
680fc0065adSTiezhu Yang 	if (crc_offset >= blk_size) {
681fc0065adSTiezhu Yang 		f2fs_msg(sbi->sb, KERN_WARNING,
682fc0065adSTiezhu Yang 			"invalid crc_offset: %zu", crc_offset);
683fc0065adSTiezhu Yang 		return -EINVAL;
684fc0065adSTiezhu Yang 	}
685fc0065adSTiezhu Yang 
686fc0065adSTiezhu Yang 	crc = le32_to_cpu(*((__le32 *)((unsigned char *)*cp_block
687fc0065adSTiezhu Yang 							+ crc_offset)));
688fc0065adSTiezhu Yang 	if (!f2fs_crc_valid(sbi, crc, *cp_block, crc_offset)) {
689fc0065adSTiezhu Yang 		f2fs_msg(sbi->sb, KERN_WARNING, "invalid crc value");
690fc0065adSTiezhu Yang 		return -EINVAL;
691fc0065adSTiezhu Yang 	}
692fc0065adSTiezhu Yang 
693fc0065adSTiezhu Yang 	*version = cur_cp_version(*cp_block);
694fc0065adSTiezhu Yang 	return 0;
695fc0065adSTiezhu Yang }
696fc0065adSTiezhu Yang 
697127e670aSJaegeuk Kim static struct page *validate_checkpoint(struct f2fs_sb_info *sbi,
698127e670aSJaegeuk Kim 				block_t cp_addr, unsigned long long *version)
699127e670aSJaegeuk Kim {
700fc0065adSTiezhu Yang 	struct page *cp_page_1 = NULL, *cp_page_2 = NULL;
701fc0065adSTiezhu Yang 	struct f2fs_checkpoint *cp_block = NULL;
702127e670aSJaegeuk Kim 	unsigned long long cur_version = 0, pre_version = 0;
703fc0065adSTiezhu Yang 	int err;
704127e670aSJaegeuk Kim 
705fc0065adSTiezhu Yang 	err = get_checkpoint_version(sbi, cp_addr, &cp_block,
706fc0065adSTiezhu Yang 					&cp_page_1, version);
707fc0065adSTiezhu Yang 	if (err)
708127e670aSJaegeuk Kim 		goto invalid_cp1;
709fc0065adSTiezhu Yang 	pre_version = *version;
710127e670aSJaegeuk Kim 
71125ca923bSJaegeuk Kim 	cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1;
712fc0065adSTiezhu Yang 	err = get_checkpoint_version(sbi, cp_addr, &cp_block,
713fc0065adSTiezhu Yang 					&cp_page_2, version);
714fc0065adSTiezhu Yang 	if (err)
715127e670aSJaegeuk Kim 		goto invalid_cp2;
716fc0065adSTiezhu Yang 	cur_version = *version;
717127e670aSJaegeuk Kim 
718127e670aSJaegeuk Kim 	if (cur_version == pre_version) {
719127e670aSJaegeuk Kim 		*version = cur_version;
720127e670aSJaegeuk Kim 		f2fs_put_page(cp_page_2, 1);
721127e670aSJaegeuk Kim 		return cp_page_1;
722127e670aSJaegeuk Kim 	}
723127e670aSJaegeuk Kim invalid_cp2:
724127e670aSJaegeuk Kim 	f2fs_put_page(cp_page_2, 1);
725127e670aSJaegeuk Kim invalid_cp1:
726127e670aSJaegeuk Kim 	f2fs_put_page(cp_page_1, 1);
727127e670aSJaegeuk Kim 	return NULL;
728127e670aSJaegeuk Kim }
729127e670aSJaegeuk Kim 
730127e670aSJaegeuk Kim int get_valid_checkpoint(struct f2fs_sb_info *sbi)
731127e670aSJaegeuk Kim {
732127e670aSJaegeuk Kim 	struct f2fs_checkpoint *cp_block;
733127e670aSJaegeuk Kim 	struct f2fs_super_block *fsb = sbi->raw_super;
734127e670aSJaegeuk Kim 	struct page *cp1, *cp2, *cur_page;
735127e670aSJaegeuk Kim 	unsigned long blk_size = sbi->blocksize;
736127e670aSJaegeuk Kim 	unsigned long long cp1_version = 0, cp2_version = 0;
737127e670aSJaegeuk Kim 	unsigned long long cp_start_blk_no;
73855141486SWanpeng Li 	unsigned int cp_blks = 1 + __cp_payload(sbi);
7391dbe4152SChangman Lee 	block_t cp_blk_no;
7401dbe4152SChangman Lee 	int i;
741127e670aSJaegeuk Kim 
7421dbe4152SChangman Lee 	sbi->ckpt = kzalloc(cp_blks * blk_size, GFP_KERNEL);
743127e670aSJaegeuk Kim 	if (!sbi->ckpt)
744127e670aSJaegeuk Kim 		return -ENOMEM;
745127e670aSJaegeuk Kim 	/*
746127e670aSJaegeuk Kim 	 * Finding out valid cp block involves read both
747127e670aSJaegeuk Kim 	 * sets( cp pack1 and cp pack 2)
748127e670aSJaegeuk Kim 	 */
749127e670aSJaegeuk Kim 	cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr);
750127e670aSJaegeuk Kim 	cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
751127e670aSJaegeuk Kim 
752127e670aSJaegeuk Kim 	/* The second checkpoint pack should start at the next segment */
753f9a4e6dfSJaegeuk Kim 	cp_start_blk_no += ((unsigned long long)1) <<
754f9a4e6dfSJaegeuk Kim 				le32_to_cpu(fsb->log_blocks_per_seg);
755127e670aSJaegeuk Kim 	cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
756127e670aSJaegeuk Kim 
757127e670aSJaegeuk Kim 	if (cp1 && cp2) {
758127e670aSJaegeuk Kim 		if (ver_after(cp2_version, cp1_version))
759127e670aSJaegeuk Kim 			cur_page = cp2;
760127e670aSJaegeuk Kim 		else
761127e670aSJaegeuk Kim 			cur_page = cp1;
762127e670aSJaegeuk Kim 	} else if (cp1) {
763127e670aSJaegeuk Kim 		cur_page = cp1;
764127e670aSJaegeuk Kim 	} else if (cp2) {
765127e670aSJaegeuk Kim 		cur_page = cp2;
766127e670aSJaegeuk Kim 	} else {
767127e670aSJaegeuk Kim 		goto fail_no_cp;
768127e670aSJaegeuk Kim 	}
769127e670aSJaegeuk Kim 
770127e670aSJaegeuk Kim 	cp_block = (struct f2fs_checkpoint *)page_address(cur_page);
771127e670aSJaegeuk Kim 	memcpy(sbi->ckpt, cp_block, blk_size);
772127e670aSJaegeuk Kim 
773984ec63cSShawn Lin 	/* Sanity checking of checkpoint */
774984ec63cSShawn Lin 	if (sanity_check_ckpt(sbi))
775a2125ff7SJaegeuk Kim 		goto free_fail_no_cp;
776984ec63cSShawn Lin 
7778508e44aSJaegeuk Kim 	if (cur_page == cp1)
7788508e44aSJaegeuk Kim 		sbi->cur_cp_pack = 1;
7798508e44aSJaegeuk Kim 	else
7808508e44aSJaegeuk Kim 		sbi->cur_cp_pack = 2;
7811dbe4152SChangman Lee 
7821dbe4152SChangman Lee 	if (cp_blks <= 1)
7831dbe4152SChangman Lee 		goto done;
7841dbe4152SChangman Lee 
7851dbe4152SChangman Lee 	cp_blk_no = le32_to_cpu(fsb->cp_blkaddr);
7861dbe4152SChangman Lee 	if (cur_page == cp2)
7871dbe4152SChangman Lee 		cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg);
7881dbe4152SChangman Lee 
7891dbe4152SChangman Lee 	for (i = 1; i < cp_blks; i++) {
7901dbe4152SChangman Lee 		void *sit_bitmap_ptr;
7911dbe4152SChangman Lee 		unsigned char *ckpt = (unsigned char *)sbi->ckpt;
7921dbe4152SChangman Lee 
7931dbe4152SChangman Lee 		cur_page = get_meta_page(sbi, cp_blk_no + i);
7941dbe4152SChangman Lee 		sit_bitmap_ptr = page_address(cur_page);
795127e670aSJaegeuk Kim 		memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size);
796127e670aSJaegeuk Kim 		f2fs_put_page(cur_page, 1);
797127e670aSJaegeuk Kim 	}
798127e670aSJaegeuk Kim done:
799127e670aSJaegeuk Kim 	f2fs_put_page(cp1, 1);
800127e670aSJaegeuk Kim 	f2fs_put_page(cp2, 1);
801127e670aSJaegeuk Kim 	return 0;
802127e670aSJaegeuk Kim 
803a2125ff7SJaegeuk Kim free_fail_no_cp:
804a2125ff7SJaegeuk Kim 	f2fs_put_page(cp1, 1);
805a2125ff7SJaegeuk Kim 	f2fs_put_page(cp2, 1);
806127e670aSJaegeuk Kim fail_no_cp:
807127e670aSJaegeuk Kim 	kfree(sbi->ckpt);
808127e670aSJaegeuk Kim 	return -EINVAL;
809127e670aSJaegeuk Kim }
810127e670aSJaegeuk Kim 
811c227f912SChao Yu static void __add_dirty_inode(struct inode *inode, enum inode_type type)
812127e670aSJaegeuk Kim {
8134081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
814c227f912SChao Yu 	int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
8152d7b822aSChao Yu 
81691942321SJaegeuk Kim 	if (is_inode_flag_set(inode, flag))
8172710fd7eSChao Yu 		return;
8182d7b822aSChao Yu 
81991942321SJaegeuk Kim 	set_inode_flag(inode, flag);
82091942321SJaegeuk Kim 	list_add_tail(&F2FS_I(inode)->dirty_list, &sbi->inode_list[type]);
82133fbd510SChao Yu 	stat_inc_dirty_inode(sbi, type);
8225deb8267SJaegeuk Kim }
8235deb8267SJaegeuk Kim 
824c227f912SChao Yu static void __remove_dirty_inode(struct inode *inode, enum inode_type type)
8256ad7609aSChao Yu {
826c227f912SChao Yu 	int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
8276ad7609aSChao Yu 
82891942321SJaegeuk Kim 	if (get_dirty_pages(inode) || !is_inode_flag_set(inode, flag))
8296ad7609aSChao Yu 		return;
8306ad7609aSChao Yu 
83191942321SJaegeuk Kim 	list_del_init(&F2FS_I(inode)->dirty_list);
83291942321SJaegeuk Kim 	clear_inode_flag(inode, flag);
83333fbd510SChao Yu 	stat_dec_dirty_inode(F2FS_I_SB(inode), type);
8346ad7609aSChao Yu }
8356ad7609aSChao Yu 
836a7ffdbe2SJaegeuk Kim void update_dirty_page(struct inode *inode, struct page *page)
8375deb8267SJaegeuk Kim {
8384081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
839c227f912SChao Yu 	enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
8405deb8267SJaegeuk Kim 
8415ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
8425ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
843127e670aSJaegeuk Kim 		return;
8447bd59381SGu Zheng 
845c227f912SChao Yu 	spin_lock(&sbi->inode_lock[type]);
8461c4bf763SJaegeuk Kim 	if (type != FILE_INODE || test_opt(sbi, DATA_FLUSH))
847c227f912SChao Yu 		__add_dirty_inode(inode, type);
848b951a4ecSYunlei He 	inode_inc_dirty_pages(inode);
8491c4bf763SJaegeuk Kim 	spin_unlock(&sbi->inode_lock[type]);
8501c4bf763SJaegeuk Kim 
851a7ffdbe2SJaegeuk Kim 	SetPagePrivate(page);
8529e4ded3fSJaegeuk Kim 	f2fs_trace_pid(page);
8535deb8267SJaegeuk Kim }
854127e670aSJaegeuk Kim 
855c227f912SChao Yu void remove_dirty_inode(struct inode *inode)
856127e670aSJaegeuk Kim {
8574081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
858c227f912SChao Yu 	enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
859127e670aSJaegeuk Kim 
860c227f912SChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
861c227f912SChao Yu 			!S_ISLNK(inode->i_mode))
862127e670aSJaegeuk Kim 		return;
863127e670aSJaegeuk Kim 
86410aa97c3SJaegeuk Kim 	if (type == FILE_INODE && !test_opt(sbi, DATA_FLUSH))
86510aa97c3SJaegeuk Kim 		return;
86610aa97c3SJaegeuk Kim 
867c227f912SChao Yu 	spin_lock(&sbi->inode_lock[type]);
868c227f912SChao Yu 	__remove_dirty_inode(inode, type);
869c227f912SChao Yu 	spin_unlock(&sbi->inode_lock[type]);
870afc3eda2SJaegeuk Kim }
87174d0b917SJaegeuk Kim 
8726d5a1495SChao Yu int sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type)
873127e670aSJaegeuk Kim {
874ce3b7d80SGu Zheng 	struct list_head *head;
875127e670aSJaegeuk Kim 	struct inode *inode;
8762710fd7eSChao Yu 	struct f2fs_inode_info *fi;
8774cf18537SChao Yu 	bool is_dir = (type == DIR_INODE);
8784cf18537SChao Yu 
8794cf18537SChao Yu 	trace_f2fs_sync_dirty_inodes_enter(sbi->sb, is_dir,
8804cf18537SChao Yu 				get_pages(sbi, is_dir ?
8814cf18537SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
882127e670aSJaegeuk Kim retry:
883af41d3eeSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
8846d5a1495SChao Yu 		return -EIO;
885af41d3eeSJaegeuk Kim 
886c227f912SChao Yu 	spin_lock(&sbi->inode_lock[type]);
887ce3b7d80SGu Zheng 
888c227f912SChao Yu 	head = &sbi->inode_list[type];
889127e670aSJaegeuk Kim 	if (list_empty(head)) {
890c227f912SChao Yu 		spin_unlock(&sbi->inode_lock[type]);
8914cf18537SChao Yu 		trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir,
8924cf18537SChao Yu 				get_pages(sbi, is_dir ?
8934cf18537SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
8946d5a1495SChao Yu 		return 0;
895127e670aSJaegeuk Kim 	}
896939afa94SChao Yu 	fi = list_first_entry(head, struct f2fs_inode_info, dirty_list);
8972710fd7eSChao Yu 	inode = igrab(&fi->vfs_inode);
898c227f912SChao Yu 	spin_unlock(&sbi->inode_lock[type]);
899127e670aSJaegeuk Kim 	if (inode) {
90087d6f890SJaegeuk Kim 		filemap_fdatawrite(inode->i_mapping);
901127e670aSJaegeuk Kim 		iput(inode);
902127e670aSJaegeuk Kim 	} else {
903127e670aSJaegeuk Kim 		/*
904127e670aSJaegeuk Kim 		 * We should submit bio, since it exists several
905127e670aSJaegeuk Kim 		 * wribacking dentry pages in the freeing inode.
906127e670aSJaegeuk Kim 		 */
907458e6197SJaegeuk Kim 		f2fs_submit_merged_bio(sbi, DATA, WRITE);
9087ecebe5eSSebastian Andrzej Siewior 		cond_resched();
909127e670aSJaegeuk Kim 	}
910127e670aSJaegeuk Kim 	goto retry;
911127e670aSJaegeuk Kim }
912127e670aSJaegeuk Kim 
9130f18b462SJaegeuk Kim int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi)
9140f18b462SJaegeuk Kim {
9150f18b462SJaegeuk Kim 	struct list_head *head = &sbi->inode_list[DIRTY_META];
9160f18b462SJaegeuk Kim 	struct inode *inode;
9170f18b462SJaegeuk Kim 	struct f2fs_inode_info *fi;
9180f18b462SJaegeuk Kim 	s64 total = get_pages(sbi, F2FS_DIRTY_IMETA);
9190f18b462SJaegeuk Kim 
9200f18b462SJaegeuk Kim 	while (total--) {
9210f18b462SJaegeuk Kim 		if (unlikely(f2fs_cp_error(sbi)))
9220f18b462SJaegeuk Kim 			return -EIO;
9230f18b462SJaegeuk Kim 
9240f18b462SJaegeuk Kim 		spin_lock(&sbi->inode_lock[DIRTY_META]);
9250f18b462SJaegeuk Kim 		if (list_empty(head)) {
9260f18b462SJaegeuk Kim 			spin_unlock(&sbi->inode_lock[DIRTY_META]);
9270f18b462SJaegeuk Kim 			return 0;
9280f18b462SJaegeuk Kim 		}
929939afa94SChao Yu 		fi = list_first_entry(head, struct f2fs_inode_info,
9300f18b462SJaegeuk Kim 							gdirty_list);
9310f18b462SJaegeuk Kim 		inode = igrab(&fi->vfs_inode);
9320f18b462SJaegeuk Kim 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
9330f18b462SJaegeuk Kim 		if (inode) {
93418340edcSJaegeuk Kim 			sync_inode_metadata(inode, 0);
93518340edcSJaegeuk Kim 
93618340edcSJaegeuk Kim 			/* it's on eviction */
93718340edcSJaegeuk Kim 			if (is_inode_flag_set(inode, FI_DIRTY_INODE))
9380f18b462SJaegeuk Kim 				update_inode_page(inode);
9390f18b462SJaegeuk Kim 			iput(inode);
9400f18b462SJaegeuk Kim 		}
9410f18b462SJaegeuk Kim 	};
9420f18b462SJaegeuk Kim 	return 0;
9430f18b462SJaegeuk Kim }
9440f18b462SJaegeuk Kim 
9450a8165d7SJaegeuk Kim /*
946127e670aSJaegeuk Kim  * Freeze all the FS-operations for checkpoint.
947127e670aSJaegeuk Kim  */
948cf779cabSJaegeuk Kim static int block_operations(struct f2fs_sb_info *sbi)
949127e670aSJaegeuk Kim {
950127e670aSJaegeuk Kim 	struct writeback_control wbc = {
951127e670aSJaegeuk Kim 		.sync_mode = WB_SYNC_ALL,
952127e670aSJaegeuk Kim 		.nr_to_write = LONG_MAX,
953127e670aSJaegeuk Kim 		.for_reclaim = 0,
954127e670aSJaegeuk Kim 	};
955c718379bSJaegeuk Kim 	struct blk_plug plug;
956cf779cabSJaegeuk Kim 	int err = 0;
957c718379bSJaegeuk Kim 
958c718379bSJaegeuk Kim 	blk_start_plug(&plug);
959c718379bSJaegeuk Kim 
96039936837SJaegeuk Kim retry_flush_dents:
961e479556bSGu Zheng 	f2fs_lock_all(sbi);
962127e670aSJaegeuk Kim 	/* write all the dirty dentry pages */
963127e670aSJaegeuk Kim 	if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
964e479556bSGu Zheng 		f2fs_unlock_all(sbi);
9656d5a1495SChao Yu 		err = sync_dirty_inodes(sbi, DIR_INODE);
9666d5a1495SChao Yu 		if (err)
967cf779cabSJaegeuk Kim 			goto out;
96839936837SJaegeuk Kim 		goto retry_flush_dents;
969127e670aSJaegeuk Kim 	}
970127e670aSJaegeuk Kim 
9710f18b462SJaegeuk Kim 	if (get_pages(sbi, F2FS_DIRTY_IMETA)) {
9720f18b462SJaegeuk Kim 		f2fs_unlock_all(sbi);
9730f18b462SJaegeuk Kim 		err = f2fs_sync_inode_meta(sbi);
9740f18b462SJaegeuk Kim 		if (err)
9750f18b462SJaegeuk Kim 			goto out;
9760f18b462SJaegeuk Kim 		goto retry_flush_dents;
9770f18b462SJaegeuk Kim 	}
9780f18b462SJaegeuk Kim 
979127e670aSJaegeuk Kim 	/*
980e1c42045Sarter97 	 * POR: we should ensure that there are no dirty node pages
981127e670aSJaegeuk Kim 	 * until finishing nat/sit flush.
982127e670aSJaegeuk Kim 	 */
98339936837SJaegeuk Kim retry_flush_nodes:
984b3582c68SChao Yu 	down_write(&sbi->node_write);
985127e670aSJaegeuk Kim 
986127e670aSJaegeuk Kim 	if (get_pages(sbi, F2FS_DIRTY_NODES)) {
987b3582c68SChao Yu 		up_write(&sbi->node_write);
98852681375SJaegeuk Kim 		err = sync_node_pages(sbi, &wbc);
9896d5a1495SChao Yu 		if (err) {
990cf779cabSJaegeuk Kim 			f2fs_unlock_all(sbi);
991cf779cabSJaegeuk Kim 			goto out;
992cf779cabSJaegeuk Kim 		}
99339936837SJaegeuk Kim 		goto retry_flush_nodes;
994127e670aSJaegeuk Kim 	}
995cf779cabSJaegeuk Kim out:
996c718379bSJaegeuk Kim 	blk_finish_plug(&plug);
997cf779cabSJaegeuk Kim 	return err;
998127e670aSJaegeuk Kim }
999127e670aSJaegeuk Kim 
1000127e670aSJaegeuk Kim static void unblock_operations(struct f2fs_sb_info *sbi)
1001127e670aSJaegeuk Kim {
1002b3582c68SChao Yu 	up_write(&sbi->node_write);
1003e479556bSGu Zheng 	f2fs_unlock_all(sbi);
1004127e670aSJaegeuk Kim }
1005127e670aSJaegeuk Kim 
1006fb51b5efSChangman Lee static void wait_on_all_pages_writeback(struct f2fs_sb_info *sbi)
1007fb51b5efSChangman Lee {
1008fb51b5efSChangman Lee 	DEFINE_WAIT(wait);
1009fb51b5efSChangman Lee 
1010fb51b5efSChangman Lee 	for (;;) {
1011fb51b5efSChangman Lee 		prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
1012fb51b5efSChangman Lee 
101336951b38SChao Yu 		if (!get_pages(sbi, F2FS_WB_CP_DATA))
1014fb51b5efSChangman Lee 			break;
1015fb51b5efSChangman Lee 
10160ff21646SYunlei He 		io_schedule_timeout(5*HZ);
1017fb51b5efSChangman Lee 	}
1018fb51b5efSChangman Lee 	finish_wait(&sbi->cp_wait, &wait);
1019fb51b5efSChangman Lee }
1020fb51b5efSChangman Lee 
1021e4c5d848SJaegeuk Kim static void update_ckpt_flags(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1022e4c5d848SJaegeuk Kim {
1023e4c5d848SJaegeuk Kim 	unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num;
1024e4c5d848SJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1025e4c5d848SJaegeuk Kim 
1026e4c5d848SJaegeuk Kim 	spin_lock(&sbi->cp_lock);
1027e4c5d848SJaegeuk Kim 
102822ad0b6aSJaegeuk Kim 	if (cpc->reason == CP_UMOUNT && ckpt->cp_pack_total_block_count >
102922ad0b6aSJaegeuk Kim 			sbi->blocks_per_seg - NM_I(sbi)->nat_bits_blocks)
103022ad0b6aSJaegeuk Kim 		disable_nat_bits(sbi, false);
103122ad0b6aSJaegeuk Kim 
1032e4c5d848SJaegeuk Kim 	if (cpc->reason == CP_UMOUNT)
1033e4c5d848SJaegeuk Kim 		__set_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1034e4c5d848SJaegeuk Kim 	else
1035e4c5d848SJaegeuk Kim 		__clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1036e4c5d848SJaegeuk Kim 
1037e4c5d848SJaegeuk Kim 	if (cpc->reason == CP_FASTBOOT)
1038e4c5d848SJaegeuk Kim 		__set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1039e4c5d848SJaegeuk Kim 	else
1040e4c5d848SJaegeuk Kim 		__clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1041e4c5d848SJaegeuk Kim 
1042e4c5d848SJaegeuk Kim 	if (orphan_num)
1043e4c5d848SJaegeuk Kim 		__set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1044e4c5d848SJaegeuk Kim 	else
1045e4c5d848SJaegeuk Kim 		__clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1046e4c5d848SJaegeuk Kim 
1047e4c5d848SJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
1048e4c5d848SJaegeuk Kim 		__set_ckpt_flags(ckpt, CP_FSCK_FLAG);
1049e4c5d848SJaegeuk Kim 
1050e4c5d848SJaegeuk Kim 	/* set this flag to activate crc|cp_ver for recovery */
1051e4c5d848SJaegeuk Kim 	__set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG);
1052e4c5d848SJaegeuk Kim 
1053e4c5d848SJaegeuk Kim 	spin_unlock(&sbi->cp_lock);
1054e4c5d848SJaegeuk Kim }
1055e4c5d848SJaegeuk Kim 
1056c34f42e2SChao Yu static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1057127e670aSJaegeuk Kim {
1058127e670aSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
105977041823SHuang Ying 	struct f2fs_nm_info *nm_i = NM_I(sbi);
106067298804SChao Yu 	unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num;
106177041823SHuang Ying 	nid_t last_nid = nm_i->next_scan_nid;
1062127e670aSJaegeuk Kim 	block_t start_blk;
1063127e670aSJaegeuk Kim 	unsigned int data_sum_blocks, orphan_blocks;
10647e586fa0SJaegeuk Kim 	__u32 crc32 = 0;
1065127e670aSJaegeuk Kim 	int i;
106655141486SWanpeng Li 	int cp_payload_blks = __cp_payload(sbi);
10678f1dbbbbSShuoran Liu 	struct super_block *sb = sbi->sb;
10688f1dbbbbSShuoran Liu 	struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
10698f1dbbbbSShuoran Liu 	u64 kbytes_written;
1070127e670aSJaegeuk Kim 
1071127e670aSJaegeuk Kim 	/* Flush all the NAT/SIT pages */
1072cf779cabSJaegeuk Kim 	while (get_pages(sbi, F2FS_DIRTY_META)) {
1073127e670aSJaegeuk Kim 		sync_meta_pages(sbi, META, LONG_MAX);
1074cf779cabSJaegeuk Kim 		if (unlikely(f2fs_cp_error(sbi)))
1075c34f42e2SChao Yu 			return -EIO;
1076cf779cabSJaegeuk Kim 	}
1077127e670aSJaegeuk Kim 
1078127e670aSJaegeuk Kim 	next_free_nid(sbi, &last_nid);
1079127e670aSJaegeuk Kim 
1080127e670aSJaegeuk Kim 	/*
1081127e670aSJaegeuk Kim 	 * modify checkpoint
1082127e670aSJaegeuk Kim 	 * version number is already updated
1083127e670aSJaegeuk Kim 	 */
1084127e670aSJaegeuk Kim 	ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi));
1085127e670aSJaegeuk Kim 	ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
1086127e670aSJaegeuk Kim 	ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
1087b5b82205SChao Yu 	for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
1088127e670aSJaegeuk Kim 		ckpt->cur_node_segno[i] =
1089127e670aSJaegeuk Kim 			cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE));
1090127e670aSJaegeuk Kim 		ckpt->cur_node_blkoff[i] =
1091127e670aSJaegeuk Kim 			cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE));
1092127e670aSJaegeuk Kim 		ckpt->alloc_type[i + CURSEG_HOT_NODE] =
1093127e670aSJaegeuk Kim 				curseg_alloc_type(sbi, i + CURSEG_HOT_NODE);
1094127e670aSJaegeuk Kim 	}
1095b5b82205SChao Yu 	for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
1096127e670aSJaegeuk Kim 		ckpt->cur_data_segno[i] =
1097127e670aSJaegeuk Kim 			cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA));
1098127e670aSJaegeuk Kim 		ckpt->cur_data_blkoff[i] =
1099127e670aSJaegeuk Kim 			cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA));
1100127e670aSJaegeuk Kim 		ckpt->alloc_type[i + CURSEG_HOT_DATA] =
1101127e670aSJaegeuk Kim 				curseg_alloc_type(sbi, i + CURSEG_HOT_DATA);
1102127e670aSJaegeuk Kim 	}
1103127e670aSJaegeuk Kim 
1104127e670aSJaegeuk Kim 	ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi));
1105127e670aSJaegeuk Kim 	ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi));
1106127e670aSJaegeuk Kim 	ckpt->next_free_nid = cpu_to_le32(last_nid);
1107127e670aSJaegeuk Kim 
1108127e670aSJaegeuk Kim 	/* 2 cp  + n data seg summary + orphan inode blocks */
11093fa06d7bSChao Yu 	data_sum_blocks = npages_for_summary_flush(sbi, false);
1110aaec2b1dSChao Yu 	spin_lock(&sbi->cp_lock);
1111b5b82205SChao Yu 	if (data_sum_blocks < NR_CURSEG_DATA_TYPE)
1112aaec2b1dSChao Yu 		__set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
1113127e670aSJaegeuk Kim 	else
1114aaec2b1dSChao Yu 		__clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
1115aaec2b1dSChao Yu 	spin_unlock(&sbi->cp_lock);
1116127e670aSJaegeuk Kim 
111767298804SChao Yu 	orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num);
11181dbe4152SChangman Lee 	ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks +
11191dbe4152SChangman Lee 			orphan_blocks);
1120127e670aSJaegeuk Kim 
1121119ee914SJaegeuk Kim 	if (__remain_node_summaries(cpc->reason))
1122b5b82205SChao Yu 		ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS+
11231dbe4152SChangman Lee 				cp_payload_blks + data_sum_blocks +
11241dbe4152SChangman Lee 				orphan_blocks + NR_CURSEG_NODE_TYPE);
1125119ee914SJaegeuk Kim 	else
1126b5b82205SChao Yu 		ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
11271dbe4152SChangman Lee 				cp_payload_blks + data_sum_blocks +
11281dbe4152SChangman Lee 				orphan_blocks);
1129119ee914SJaegeuk Kim 
1130e4c5d848SJaegeuk Kim 	/* update ckpt flag for checkpoint */
1131e4c5d848SJaegeuk Kim 	update_ckpt_flags(sbi, cpc);
1132a468f0efSJaegeuk Kim 
1133127e670aSJaegeuk Kim 	/* update SIT/NAT bitmap */
1134127e670aSJaegeuk Kim 	get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
1135127e670aSJaegeuk Kim 	get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
1136127e670aSJaegeuk Kim 
113743b6573bSKeith Mok 	crc32 = f2fs_crc32(sbi, ckpt, le32_to_cpu(ckpt->checksum_offset));
11387e586fa0SJaegeuk Kim 	*((__le32 *)((unsigned char *)ckpt +
11397e586fa0SJaegeuk Kim 				le32_to_cpu(ckpt->checksum_offset)))
1140127e670aSJaegeuk Kim 				= cpu_to_le32(crc32);
1141127e670aSJaegeuk Kim 
11428508e44aSJaegeuk Kim 	start_blk = __start_cp_next_addr(sbi);
1143127e670aSJaegeuk Kim 
114422ad0b6aSJaegeuk Kim 	/* write nat bits */
114522ad0b6aSJaegeuk Kim 	if (enabled_nat_bits(sbi, cpc)) {
114622ad0b6aSJaegeuk Kim 		__u64 cp_ver = cur_cp_version(ckpt);
114722ad0b6aSJaegeuk Kim 		unsigned int i;
114822ad0b6aSJaegeuk Kim 		block_t blk;
114922ad0b6aSJaegeuk Kim 
115022ad0b6aSJaegeuk Kim 		cp_ver |= ((__u64)crc32 << 32);
115122ad0b6aSJaegeuk Kim 		*(__le64 *)nm_i->nat_bits = cpu_to_le64(cp_ver);
115222ad0b6aSJaegeuk Kim 
115322ad0b6aSJaegeuk Kim 		blk = start_blk + sbi->blocks_per_seg - nm_i->nat_bits_blocks;
115422ad0b6aSJaegeuk Kim 		for (i = 0; i < nm_i->nat_bits_blocks; i++)
115522ad0b6aSJaegeuk Kim 			update_meta_page(sbi, nm_i->nat_bits +
115622ad0b6aSJaegeuk Kim 					(i << F2FS_BLKSIZE_BITS), blk + i);
115722ad0b6aSJaegeuk Kim 
115822ad0b6aSJaegeuk Kim 		/* Flush all the NAT BITS pages */
115922ad0b6aSJaegeuk Kim 		while (get_pages(sbi, F2FS_DIRTY_META)) {
116022ad0b6aSJaegeuk Kim 			sync_meta_pages(sbi, META, LONG_MAX);
116122ad0b6aSJaegeuk Kim 			if (unlikely(f2fs_cp_error(sbi)))
116222ad0b6aSJaegeuk Kim 				return -EIO;
116322ad0b6aSJaegeuk Kim 		}
116422ad0b6aSJaegeuk Kim 	}
116522ad0b6aSJaegeuk Kim 
1166a7230d16SJaegeuk Kim 	/* need to wait for end_io results */
1167a7230d16SJaegeuk Kim 	wait_on_all_pages_writeback(sbi);
1168a7230d16SJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
1169c34f42e2SChao Yu 		return -EIO;
1170a7230d16SJaegeuk Kim 
1171127e670aSJaegeuk Kim 	/* write out checkpoint buffer at block 0 */
1172381722d2SChao Yu 	update_meta_page(sbi, ckpt, start_blk++);
1173127e670aSJaegeuk Kim 
1174381722d2SChao Yu 	for (i = 1; i < 1 + cp_payload_blks; i++)
1175381722d2SChao Yu 		update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE,
1176381722d2SChao Yu 							start_blk++);
11771dbe4152SChangman Lee 
117867298804SChao Yu 	if (orphan_num) {
1179127e670aSJaegeuk Kim 		write_orphan_inodes(sbi, start_blk);
1180127e670aSJaegeuk Kim 		start_blk += orphan_blocks;
1181127e670aSJaegeuk Kim 	}
1182127e670aSJaegeuk Kim 
1183127e670aSJaegeuk Kim 	write_data_summaries(sbi, start_blk);
1184127e670aSJaegeuk Kim 	start_blk += data_sum_blocks;
11858f1dbbbbSShuoran Liu 
11868f1dbbbbSShuoran Liu 	/* Record write statistics in the hot node summary */
11878f1dbbbbSShuoran Liu 	kbytes_written = sbi->kbytes_written;
11888f1dbbbbSShuoran Liu 	if (sb->s_bdev->bd_part)
11898f1dbbbbSShuoran Liu 		kbytes_written += BD_PART_WRITTEN(sbi);
11908f1dbbbbSShuoran Liu 
1191b7ad7512SChao Yu 	seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written);
11928f1dbbbbSShuoran Liu 
1193119ee914SJaegeuk Kim 	if (__remain_node_summaries(cpc->reason)) {
1194127e670aSJaegeuk Kim 		write_node_summaries(sbi, start_blk);
1195127e670aSJaegeuk Kim 		start_blk += NR_CURSEG_NODE_TYPE;
1196127e670aSJaegeuk Kim 	}
1197127e670aSJaegeuk Kim 
1198127e670aSJaegeuk Kim 	/* writeout checkpoint block */
1199381722d2SChao Yu 	update_meta_page(sbi, ckpt, start_blk);
1200127e670aSJaegeuk Kim 
1201127e670aSJaegeuk Kim 	/* wait for previous submitted node/meta pages writeback */
1202fb51b5efSChangman Lee 	wait_on_all_pages_writeback(sbi);
1203127e670aSJaegeuk Kim 
1204cf779cabSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
1205c34f42e2SChao Yu 		return -EIO;
1206cf779cabSJaegeuk Kim 
120780dd9c0eSChao Yu 	filemap_fdatawait_range(NODE_MAPPING(sbi), 0, LLONG_MAX);
120880dd9c0eSChao Yu 	filemap_fdatawait_range(META_MAPPING(sbi), 0, LLONG_MAX);
1209127e670aSJaegeuk Kim 
1210127e670aSJaegeuk Kim 	/* update user_block_counts */
1211127e670aSJaegeuk Kim 	sbi->last_valid_block_count = sbi->total_valid_block_count;
121241382ec4SJaegeuk Kim 	percpu_counter_set(&sbi->alloc_valid_block_count, 0);
1213127e670aSJaegeuk Kim 
1214127e670aSJaegeuk Kim 	/* Here, we only have one bio having CP pack */
1215127e670aSJaegeuk Kim 	sync_meta_pages(sbi, META_FLUSH, LONG_MAX);
1216127e670aSJaegeuk Kim 
12176a8f8ca5SJaegeuk Kim 	/* wait for previous submitted meta pages writeback */
12186a8f8ca5SJaegeuk Kim 	wait_on_all_pages_writeback(sbi);
12196a8f8ca5SJaegeuk Kim 
122074ef9241SJaegeuk Kim 	release_ino_entry(sbi, false);
1221cf779cabSJaegeuk Kim 
1222cf779cabSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
1223c34f42e2SChao Yu 		return -EIO;
1224cf779cabSJaegeuk Kim 
1225caf0047eSChao Yu 	clear_sbi_flag(sbi, SBI_IS_DIRTY);
1226bbf156f7SJaegeuk Kim 	clear_sbi_flag(sbi, SBI_NEED_CP);
12278508e44aSJaegeuk Kim 	__set_cp_next_pack(sbi);
1228c34f42e2SChao Yu 
1229c2a080aeSChao Yu 	/*
1230c2a080aeSChao Yu 	 * redirty superblock if metadata like node page or inode cache is
1231c2a080aeSChao Yu 	 * updated during writing checkpoint.
1232c2a080aeSChao Yu 	 */
1233c2a080aeSChao Yu 	if (get_pages(sbi, F2FS_DIRTY_NODES) ||
1234c2a080aeSChao Yu 			get_pages(sbi, F2FS_DIRTY_IMETA))
1235c2a080aeSChao Yu 		set_sbi_flag(sbi, SBI_IS_DIRTY);
1236c2a080aeSChao Yu 
1237c2a080aeSChao Yu 	f2fs_bug_on(sbi, get_pages(sbi, F2FS_DIRTY_DENTS));
1238c2a080aeSChao Yu 
1239c34f42e2SChao Yu 	return 0;
1240127e670aSJaegeuk Kim }
1241127e670aSJaegeuk Kim 
12420a8165d7SJaegeuk Kim /*
1243e1c42045Sarter97  * We guarantee that this checkpoint procedure will not fail.
1244127e670aSJaegeuk Kim  */
1245c34f42e2SChao Yu int write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1246127e670aSJaegeuk Kim {
1247127e670aSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1248127e670aSJaegeuk Kim 	unsigned long long ckpt_ver;
1249c34f42e2SChao Yu 	int err = 0;
1250127e670aSJaegeuk Kim 
1251127e670aSJaegeuk Kim 	mutex_lock(&sbi->cp_mutex);
12528501017eSJaegeuk Kim 
1253caf0047eSChao Yu 	if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) &&
1254a66cdd98SJaegeuk Kim 		(cpc->reason == CP_FASTBOOT || cpc->reason == CP_SYNC ||
1255a66cdd98SJaegeuk Kim 		(cpc->reason == CP_DISCARD && !sbi->discard_blks)))
12568501017eSJaegeuk Kim 		goto out;
1257c34f42e2SChao Yu 	if (unlikely(f2fs_cp_error(sbi))) {
1258c34f42e2SChao Yu 		err = -EIO;
1259cf779cabSJaegeuk Kim 		goto out;
1260c34f42e2SChao Yu 	}
1261c34f42e2SChao Yu 	if (f2fs_readonly(sbi->sb)) {
1262c34f42e2SChao Yu 		err = -EROFS;
126311504a8eSJaegeuk Kim 		goto out;
1264c34f42e2SChao Yu 	}
12652bda542dSWanpeng Li 
12662bda542dSWanpeng Li 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops");
12672bda542dSWanpeng Li 
1268c34f42e2SChao Yu 	err = block_operations(sbi);
1269c34f42e2SChao Yu 	if (err)
1270cf779cabSJaegeuk Kim 		goto out;
1271127e670aSJaegeuk Kim 
127275ab4cb8SJaegeuk Kim 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops");
12732af4bd6cSNamjae Jeon 
1274406657ddSChao Yu 	f2fs_flush_merged_bios(sbi);
1275127e670aSJaegeuk Kim 
127658cce381SYunlei He 	/* this is the case of multiple fstrims without any changes */
12770333ad4eSJaegeuk Kim 	if (cpc->reason == CP_DISCARD) {
127825290fa5SJaegeuk Kim 		if (!exist_trim_candidates(sbi, cpc)) {
127925290fa5SJaegeuk Kim 			unblock_operations(sbi);
128025290fa5SJaegeuk Kim 			goto out;
128125290fa5SJaegeuk Kim 		}
128225290fa5SJaegeuk Kim 
12830333ad4eSJaegeuk Kim 		if (NM_I(sbi)->dirty_nat_cnt == 0 &&
12840333ad4eSJaegeuk Kim 				SIT_I(sbi)->dirty_sentries == 0 &&
12850333ad4eSJaegeuk Kim 				prefree_segments(sbi) == 0) {
128658cce381SYunlei He 			flush_sit_entries(sbi, cpc);
128758cce381SYunlei He 			clear_prefree_segments(sbi, cpc);
128858cce381SYunlei He 			unblock_operations(sbi);
128958cce381SYunlei He 			goto out;
129058cce381SYunlei He 		}
12910333ad4eSJaegeuk Kim 	}
129258cce381SYunlei He 
1293127e670aSJaegeuk Kim 	/*
1294127e670aSJaegeuk Kim 	 * update checkpoint pack index
1295127e670aSJaegeuk Kim 	 * Increase the version number so that
1296127e670aSJaegeuk Kim 	 * SIT entries and seg summaries are written at correct place
1297127e670aSJaegeuk Kim 	 */
1298d71b5564SJaegeuk Kim 	ckpt_ver = cur_cp_version(ckpt);
1299127e670aSJaegeuk Kim 	ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
1300127e670aSJaegeuk Kim 
1301127e670aSJaegeuk Kim 	/* write cached NAT/SIT entries to NAT/SIT area */
130222ad0b6aSJaegeuk Kim 	flush_nat_entries(sbi, cpc);
13034b2fecc8SJaegeuk Kim 	flush_sit_entries(sbi, cpc);
1304127e670aSJaegeuk Kim 
1305127e670aSJaegeuk Kim 	/* unlock all the fs_lock[] in do_checkpoint() */
1306c34f42e2SChao Yu 	err = do_checkpoint(sbi, cpc);
13074e6a8d9bSJaegeuk Kim 	if (err)
13082dd15654SChao Yu 		release_discard_addrs(sbi);
13094e6a8d9bSJaegeuk Kim 	else
13102dd15654SChao Yu 		clear_prefree_segments(sbi, cpc);
1311275b66b0SChao Yu 
1312127e670aSJaegeuk Kim 	unblock_operations(sbi);
1313942e0be6SChangman Lee 	stat_inc_cp_count(sbi->stat_info);
131410027551SJaegeuk Kim 
131510027551SJaegeuk Kim 	if (cpc->reason == CP_RECOVERY)
131610027551SJaegeuk Kim 		f2fs_msg(sbi->sb, KERN_NOTICE,
131710027551SJaegeuk Kim 			"checkpoint: version = %llx", ckpt_ver);
131860b99b48SJaegeuk Kim 
131960b99b48SJaegeuk Kim 	/* do checkpoint periodically */
13206beceb54SJaegeuk Kim 	f2fs_update_time(sbi, CP_TIME);
132155d1cdb2SJaegeuk Kim 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint");
13228501017eSJaegeuk Kim out:
13238501017eSJaegeuk Kim 	mutex_unlock(&sbi->cp_mutex);
1324c34f42e2SChao Yu 	return err;
1325127e670aSJaegeuk Kim }
1326127e670aSJaegeuk Kim 
13276451e041SJaegeuk Kim void init_ino_entry_info(struct f2fs_sb_info *sbi)
1328127e670aSJaegeuk Kim {
13296451e041SJaegeuk Kim 	int i;
13306451e041SJaegeuk Kim 
13316451e041SJaegeuk Kim 	for (i = 0; i < MAX_INO_ENTRY; i++) {
133267298804SChao Yu 		struct inode_management *im = &sbi->im[i];
133367298804SChao Yu 
133467298804SChao Yu 		INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC);
133567298804SChao Yu 		spin_lock_init(&im->ino_lock);
133667298804SChao Yu 		INIT_LIST_HEAD(&im->ino_list);
133767298804SChao Yu 		im->ino_num = 0;
13386451e041SJaegeuk Kim 	}
13396451e041SJaegeuk Kim 
1340b5b82205SChao Yu 	sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS -
134114b42817SWanpeng Li 			NR_CURSEG_TYPE - __cp_payload(sbi)) *
134214b42817SWanpeng Li 				F2FS_ORPHANS_PER_BLOCK;
1343127e670aSJaegeuk Kim }
1344127e670aSJaegeuk Kim 
13456e6093a8SNamjae Jeon int __init create_checkpoint_caches(void)
1346127e670aSJaegeuk Kim {
13476451e041SJaegeuk Kim 	ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry",
13486451e041SJaegeuk Kim 			sizeof(struct ino_entry));
13496451e041SJaegeuk Kim 	if (!ino_entry_slab)
1350127e670aSJaegeuk Kim 		return -ENOMEM;
135106292073SChao Yu 	inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry",
135206292073SChao Yu 			sizeof(struct inode_entry));
13536bacf52fSJaegeuk Kim 	if (!inode_entry_slab) {
13546451e041SJaegeuk Kim 		kmem_cache_destroy(ino_entry_slab);
1355127e670aSJaegeuk Kim 		return -ENOMEM;
1356127e670aSJaegeuk Kim 	}
1357127e670aSJaegeuk Kim 	return 0;
1358127e670aSJaegeuk Kim }
1359127e670aSJaegeuk Kim 
1360127e670aSJaegeuk Kim void destroy_checkpoint_caches(void)
1361127e670aSJaegeuk Kim {
13626451e041SJaegeuk Kim 	kmem_cache_destroy(ino_entry_slab);
1363127e670aSJaegeuk Kim 	kmem_cache_destroy(inode_entry_slab);
1364127e670aSJaegeuk Kim }
1365