xref: /openbmc/linux/fs/f2fs/checkpoint.c (revision a515d12f1b180ce88273ff20b065baa1c4da3284)
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 	if (!end_io)
33b9109b0eSJaegeuk Kim 		f2fs_flush_merged_writes(sbi);
3438f91ca8SJaegeuk Kim }
3538f91ca8SJaegeuk Kim 
360a8165d7SJaegeuk Kim /*
37127e670aSJaegeuk Kim  * We guarantee no failure on the returned page.
38127e670aSJaegeuk Kim  */
39127e670aSJaegeuk Kim struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
40127e670aSJaegeuk Kim {
419df27d98SGu Zheng 	struct address_space *mapping = META_MAPPING(sbi);
42127e670aSJaegeuk Kim 	struct page *page = NULL;
43127e670aSJaegeuk Kim repeat:
44300e129cSJaegeuk Kim 	page = f2fs_grab_cache_page(mapping, index, false);
45127e670aSJaegeuk Kim 	if (!page) {
46127e670aSJaegeuk Kim 		cond_resched();
47127e670aSJaegeuk Kim 		goto repeat;
48127e670aSJaegeuk Kim 	}
49fec1d657SJaegeuk Kim 	f2fs_wait_on_page_writeback(page, META, true);
50237c0790SJaegeuk Kim 	if (!PageUptodate(page))
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,
6604d328deSMike Christie 		.op = REQ_OP_READ,
6770fd7614SChristoph Hellwig 		.op_flags = REQ_META | REQ_PRIO,
687a9d7548SChao Yu 		.old_blkaddr = index,
697a9d7548SChao Yu 		.new_blkaddr = index,
704375a336SJaegeuk Kim 		.encrypted_page = NULL,
710833721eSYunlei He 		.is_meta = is_meta,
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 	 */
10381114baaSChao Yu 	if (unlikely(!PageUptodate(page))) {
10481114baaSChao Yu 		memset(page_address(page), 0, PAGE_SIZE);
10538f91ca8SJaegeuk Kim 		f2fs_stop_checkpoint(sbi, false);
10681114baaSChao Yu 	}
107393ff91fSJaegeuk Kim out:
108127e670aSJaegeuk Kim 	return page;
109127e670aSJaegeuk Kim }
110127e670aSJaegeuk Kim 
1112b947003SChao Yu struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
1122b947003SChao Yu {
1132b947003SChao Yu 	return __get_meta_page(sbi, index, true);
1142b947003SChao Yu }
1152b947003SChao Yu 
1162b947003SChao Yu /* for POR only */
1172b947003SChao Yu struct page *get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index)
1182b947003SChao Yu {
1192b947003SChao Yu 	return __get_meta_page(sbi, index, false);
1202b947003SChao Yu }
1212b947003SChao Yu 
122f0c9cadaSChao Yu bool is_valid_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr, int type)
123662befdaSChao Yu {
124662befdaSChao Yu 	switch (type) {
125662befdaSChao Yu 	case META_NAT:
12666b00c18SChao Yu 		break;
127662befdaSChao Yu 	case META_SIT:
12866b00c18SChao Yu 		if (unlikely(blkaddr >= SIT_BLK_CNT(sbi)))
12966b00c18SChao Yu 			return false;
13066b00c18SChao Yu 		break;
13181c1a0f1SChao Yu 	case META_SSA:
13266b00c18SChao Yu 		if (unlikely(blkaddr >= MAIN_BLKADDR(sbi) ||
13366b00c18SChao Yu 			blkaddr < SM_I(sbi)->ssa_blkaddr))
13466b00c18SChao Yu 			return false;
13566b00c18SChao Yu 		break;
136662befdaSChao Yu 	case META_CP:
13766b00c18SChao Yu 		if (unlikely(blkaddr >= SIT_I(sbi)->sit_base_addr ||
13866b00c18SChao Yu 			blkaddr < __start_cp_addr(sbi)))
13966b00c18SChao Yu 			return false;
14066b00c18SChao Yu 		break;
1414c521f49SJaegeuk Kim 	case META_POR:
14266b00c18SChao Yu 		if (unlikely(blkaddr >= MAX_BLKADDR(sbi) ||
14366b00c18SChao Yu 			blkaddr < MAIN_BLKADDR(sbi)))
14466b00c18SChao Yu 			return false;
14566b00c18SChao Yu 		break;
146662befdaSChao Yu 	default:
147662befdaSChao Yu 		BUG();
148662befdaSChao Yu 	}
14966b00c18SChao Yu 
15066b00c18SChao Yu 	return true;
151662befdaSChao Yu }
152662befdaSChao Yu 
153662befdaSChao Yu /*
15481c1a0f1SChao Yu  * Readahead CP/NAT/SIT/SSA pages
155662befdaSChao Yu  */
15626879fb1SChao Yu int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
15726879fb1SChao Yu 							int type, bool sync)
158662befdaSChao Yu {
159662befdaSChao Yu 	struct page *page;
1604c521f49SJaegeuk Kim 	block_t blkno = start;
161662befdaSChao Yu 	struct f2fs_io_info fio = {
16205ca3632SJaegeuk Kim 		.sbi = sbi,
163662befdaSChao Yu 		.type = META,
16404d328deSMike Christie 		.op = REQ_OP_READ,
16570fd7614SChristoph Hellwig 		.op_flags = sync ? (REQ_META | REQ_PRIO) : REQ_RAHEAD,
1664375a336SJaegeuk Kim 		.encrypted_page = NULL,
167fb830fc5SChao Yu 		.in_list = false,
1680833721eSYunlei He 		.is_meta = (type != META_POR),
169662befdaSChao Yu 	};
170e9f5b8b8SChao Yu 	struct blk_plug plug;
171662befdaSChao Yu 
1722b947003SChao Yu 	if (unlikely(type == META_POR))
17304d328deSMike Christie 		fio.op_flags &= ~REQ_META;
1742b947003SChao Yu 
175e9f5b8b8SChao Yu 	blk_start_plug(&plug);
176662befdaSChao Yu 	for (; nrpages-- > 0; blkno++) {
177662befdaSChao Yu 
17866b00c18SChao Yu 		if (!is_valid_blkaddr(sbi, blkno, type))
17966b00c18SChao Yu 			goto out;
18066b00c18SChao Yu 
181662befdaSChao Yu 		switch (type) {
182662befdaSChao Yu 		case META_NAT:
18366b00c18SChao Yu 			if (unlikely(blkno >=
18466b00c18SChao Yu 					NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid)))
185662befdaSChao Yu 				blkno = 0;
18666b00c18SChao Yu 			/* get nat block addr */
1877a9d7548SChao Yu 			fio.new_blkaddr = current_nat_addr(sbi,
188662befdaSChao Yu 					blkno * NAT_ENTRY_PER_BLOCK);
189662befdaSChao Yu 			break;
190662befdaSChao Yu 		case META_SIT:
191662befdaSChao Yu 			/* get sit block addr */
1927a9d7548SChao Yu 			fio.new_blkaddr = current_sit_addr(sbi,
193662befdaSChao Yu 					blkno * SIT_ENTRY_PER_BLOCK);
194662befdaSChao Yu 			break;
19581c1a0f1SChao Yu 		case META_SSA:
196662befdaSChao Yu 		case META_CP:
1974c521f49SJaegeuk Kim 		case META_POR:
1987a9d7548SChao Yu 			fio.new_blkaddr = blkno;
199662befdaSChao Yu 			break;
200662befdaSChao Yu 		default:
201662befdaSChao Yu 			BUG();
202662befdaSChao Yu 		}
203662befdaSChao Yu 
204300e129cSJaegeuk Kim 		page = f2fs_grab_cache_page(META_MAPPING(sbi),
205300e129cSJaegeuk Kim 						fio.new_blkaddr, false);
206662befdaSChao Yu 		if (!page)
207662befdaSChao Yu 			continue;
208662befdaSChao Yu 		if (PageUptodate(page)) {
209662befdaSChao Yu 			f2fs_put_page(page, 1);
210662befdaSChao Yu 			continue;
211662befdaSChao Yu 		}
212662befdaSChao Yu 
21305ca3632SJaegeuk Kim 		fio.page = page;
2141919ffc0SJaegeuk Kim 		f2fs_submit_page_bio(&fio);
215662befdaSChao Yu 		f2fs_put_page(page, 0);
216662befdaSChao Yu 	}
217662befdaSChao Yu out:
218e9f5b8b8SChao Yu 	blk_finish_plug(&plug);
219662befdaSChao Yu 	return blkno - start;
220662befdaSChao Yu }
221662befdaSChao Yu 
222635aee1fSChao Yu void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index)
223635aee1fSChao Yu {
224635aee1fSChao Yu 	struct page *page;
225635aee1fSChao Yu 	bool readahead = false;
226635aee1fSChao Yu 
227635aee1fSChao Yu 	page = find_get_page(META_MAPPING(sbi), index);
2284da7bf5aSJaegeuk Kim 	if (!page || !PageUptodate(page))
229635aee1fSChao Yu 		readahead = true;
230635aee1fSChao Yu 	f2fs_put_page(page, 0);
231635aee1fSChao Yu 
232635aee1fSChao Yu 	if (readahead)
233664ba972SJaegeuk Kim 		ra_meta_pages(sbi, index, BIO_MAX_PAGES, META_POR, true);
234635aee1fSChao Yu }
235635aee1fSChao Yu 
236b0af6d49SChao Yu static int __f2fs_write_meta_page(struct page *page,
237b0af6d49SChao Yu 				struct writeback_control *wbc,
238b0af6d49SChao Yu 				enum iostat_type io_type)
239127e670aSJaegeuk Kim {
2404081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_P_SB(page);
241577e3495SJaegeuk Kim 
242ecda0de3SChao Yu 	trace_f2fs_writepage(page, META);
243ecda0de3SChao Yu 
244db198ae0SChao Yu 	if (unlikely(f2fs_cp_error(sbi))) {
245db198ae0SChao Yu 		dec_page_count(sbi, F2FS_DIRTY_META);
246db198ae0SChao Yu 		unlock_page(page);
247db198ae0SChao Yu 		return 0;
248db198ae0SChao Yu 	}
249caf0047eSChao Yu 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
250cfb271d4SChao Yu 		goto redirty_out;
251857dc4e0SJaegeuk Kim 	if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0))
252cfb271d4SChao Yu 		goto redirty_out;
253127e670aSJaegeuk Kim 
254b0af6d49SChao Yu 	write_meta_page(sbi, page, io_type);
255127e670aSJaegeuk Kim 	dec_page_count(sbi, F2FS_DIRTY_META);
2560c3a5797SChao Yu 
2570c3a5797SChao Yu 	if (wbc->for_reclaim)
258b9109b0eSJaegeuk Kim 		f2fs_submit_merged_write_cond(sbi, page->mapping->host,
259b9109b0eSJaegeuk Kim 						0, page->index, META);
2600c3a5797SChao Yu 
261127e670aSJaegeuk Kim 	unlock_page(page);
262857dc4e0SJaegeuk Kim 
2630c3a5797SChao Yu 	if (unlikely(f2fs_cp_error(sbi)))
264b9109b0eSJaegeuk Kim 		f2fs_submit_merged_write(sbi, META);
2650c3a5797SChao Yu 
266577e3495SJaegeuk Kim 	return 0;
267cfb271d4SChao Yu 
268cfb271d4SChao Yu redirty_out:
26976f60268SJaegeuk Kim 	redirty_page_for_writepage(wbc, page);
270cfb271d4SChao Yu 	return AOP_WRITEPAGE_ACTIVATE;
271127e670aSJaegeuk Kim }
272127e670aSJaegeuk Kim 
273b0af6d49SChao Yu static int f2fs_write_meta_page(struct page *page,
274b0af6d49SChao Yu 				struct writeback_control *wbc)
275b0af6d49SChao Yu {
276b0af6d49SChao Yu 	return __f2fs_write_meta_page(page, wbc, FS_META_IO);
277b0af6d49SChao Yu }
278b0af6d49SChao Yu 
279127e670aSJaegeuk Kim static int f2fs_write_meta_pages(struct address_space *mapping,
280127e670aSJaegeuk Kim 				struct writeback_control *wbc)
281127e670aSJaegeuk Kim {
2824081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
28350c8cdb3SJaegeuk Kim 	long diff, written;
284127e670aSJaegeuk Kim 
2850771fcc7SChao Yu 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2860771fcc7SChao Yu 		goto skip_write;
2870771fcc7SChao Yu 
2885459aa97SJaegeuk Kim 	/* collect a number of dirty meta pages and write together */
28950c8cdb3SJaegeuk Kim 	if (wbc->for_kupdate ||
29050c8cdb3SJaegeuk Kim 		get_pages(sbi, F2FS_DIRTY_META) < nr_pages_to_skip(sbi, META))
291d3baf95dSJaegeuk Kim 		goto skip_write;
292127e670aSJaegeuk Kim 
293a29d0e0bSYunlei He 	/* if locked failed, cp will flush dirty pages instead */
294a29d0e0bSYunlei He 	if (!mutex_trylock(&sbi->cp_mutex))
295a29d0e0bSYunlei He 		goto skip_write;
296d31c7c3fSYunlei He 
297a29d0e0bSYunlei He 	trace_f2fs_writepages(mapping->host, wbc, META);
29850c8cdb3SJaegeuk Kim 	diff = nr_pages_to_write(sbi, META, wbc);
299b0af6d49SChao Yu 	written = sync_meta_pages(sbi, META, wbc->nr_to_write, FS_META_IO);
300127e670aSJaegeuk Kim 	mutex_unlock(&sbi->cp_mutex);
30150c8cdb3SJaegeuk Kim 	wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff);
302127e670aSJaegeuk Kim 	return 0;
303d3baf95dSJaegeuk Kim 
304d3baf95dSJaegeuk Kim skip_write:
305d3baf95dSJaegeuk Kim 	wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META);
306d31c7c3fSYunlei He 	trace_f2fs_writepages(mapping->host, wbc, META);
307d3baf95dSJaegeuk Kim 	return 0;
308127e670aSJaegeuk Kim }
309127e670aSJaegeuk Kim 
310127e670aSJaegeuk Kim long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
311b0af6d49SChao Yu 				long nr_to_write, enum iostat_type io_type)
312127e670aSJaegeuk Kim {
3139df27d98SGu Zheng 	struct address_space *mapping = META_MAPPING(sbi);
314028a63a6SJan Kara 	pgoff_t index = 0, prev = ULONG_MAX;
315127e670aSJaegeuk Kim 	struct pagevec pvec;
316127e670aSJaegeuk Kim 	long nwritten = 0;
317028a63a6SJan Kara 	int nr_pages;
318127e670aSJaegeuk Kim 	struct writeback_control wbc = {
319127e670aSJaegeuk Kim 		.for_reclaim = 0,
320127e670aSJaegeuk Kim 	};
321e9f5b8b8SChao Yu 	struct blk_plug plug;
322127e670aSJaegeuk Kim 
32386679820SMel Gorman 	pagevec_init(&pvec);
324127e670aSJaegeuk Kim 
325e9f5b8b8SChao Yu 	blk_start_plug(&plug);
326e9f5b8b8SChao Yu 
327028a63a6SJan Kara 	while ((nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
32867fd707fSJan Kara 				PAGECACHE_TAG_DIRTY))) {
329028a63a6SJan Kara 		int i;
330127e670aSJaegeuk Kim 
331127e670aSJaegeuk Kim 		for (i = 0; i < nr_pages; i++) {
332127e670aSJaegeuk Kim 			struct page *page = pvec.pages[i];
333203681f6SJaegeuk Kim 
33480dd9c0eSChao Yu 			if (prev == ULONG_MAX)
3356066d8cdSJaegeuk Kim 				prev = page->index - 1;
3366066d8cdSJaegeuk Kim 			if (nr_to_write != LONG_MAX && page->index != prev + 1) {
3376066d8cdSJaegeuk Kim 				pagevec_release(&pvec);
3386066d8cdSJaegeuk Kim 				goto stop;
3396066d8cdSJaegeuk Kim 			}
3406066d8cdSJaegeuk Kim 
341127e670aSJaegeuk Kim 			lock_page(page);
342203681f6SJaegeuk Kim 
343203681f6SJaegeuk Kim 			if (unlikely(page->mapping != mapping)) {
344203681f6SJaegeuk Kim continue_unlock:
345203681f6SJaegeuk Kim 				unlock_page(page);
346203681f6SJaegeuk Kim 				continue;
347203681f6SJaegeuk Kim 			}
348203681f6SJaegeuk Kim 			if (!PageDirty(page)) {
349203681f6SJaegeuk Kim 				/* someone wrote it for us */
350203681f6SJaegeuk Kim 				goto continue_unlock;
351203681f6SJaegeuk Kim 			}
352203681f6SJaegeuk Kim 
353fa3d2bdfSJaegeuk Kim 			f2fs_wait_on_page_writeback(page, META, true);
354fa3d2bdfSJaegeuk Kim 
355fa3d2bdfSJaegeuk Kim 			BUG_ON(PageWriteback(page));
356203681f6SJaegeuk Kim 			if (!clear_page_dirty_for_io(page))
357203681f6SJaegeuk Kim 				goto continue_unlock;
358203681f6SJaegeuk Kim 
359b0af6d49SChao Yu 			if (__f2fs_write_meta_page(page, &wbc, io_type)) {
360577e3495SJaegeuk Kim 				unlock_page(page);
361577e3495SJaegeuk Kim 				break;
362577e3495SJaegeuk Kim 			}
363cfb271d4SChao Yu 			nwritten++;
3646066d8cdSJaegeuk Kim 			prev = page->index;
365cfb271d4SChao Yu 			if (unlikely(nwritten >= nr_to_write))
366127e670aSJaegeuk Kim 				break;
367127e670aSJaegeuk Kim 		}
368127e670aSJaegeuk Kim 		pagevec_release(&pvec);
369127e670aSJaegeuk Kim 		cond_resched();
370127e670aSJaegeuk Kim 	}
3716066d8cdSJaegeuk Kim stop:
372127e670aSJaegeuk Kim 	if (nwritten)
373b9109b0eSJaegeuk Kim 		f2fs_submit_merged_write(sbi, type);
374127e670aSJaegeuk Kim 
375e9f5b8b8SChao Yu 	blk_finish_plug(&plug);
376e9f5b8b8SChao Yu 
377127e670aSJaegeuk Kim 	return nwritten;
378127e670aSJaegeuk Kim }
379127e670aSJaegeuk Kim 
380127e670aSJaegeuk Kim static int f2fs_set_meta_page_dirty(struct page *page)
381127e670aSJaegeuk Kim {
38226c6b887SJaegeuk Kim 	trace_f2fs_set_page_dirty(page, META);
38326c6b887SJaegeuk Kim 
384237c0790SJaegeuk Kim 	if (!PageUptodate(page))
385127e670aSJaegeuk Kim 		SetPageUptodate(page);
386127e670aSJaegeuk Kim 	if (!PageDirty(page)) {
387b87078adSJaegeuk Kim 		__set_page_dirty_nobuffers(page);
3884081363fSJaegeuk Kim 		inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_META);
3891601839eSChao Yu 		SetPagePrivate(page);
3909e4ded3fSJaegeuk Kim 		f2fs_trace_pid(page);
391127e670aSJaegeuk Kim 		return 1;
392127e670aSJaegeuk Kim 	}
393127e670aSJaegeuk Kim 	return 0;
394127e670aSJaegeuk Kim }
395127e670aSJaegeuk Kim 
396127e670aSJaegeuk Kim const struct address_space_operations f2fs_meta_aops = {
397127e670aSJaegeuk Kim 	.writepage	= f2fs_write_meta_page,
398127e670aSJaegeuk Kim 	.writepages	= f2fs_write_meta_pages,
399127e670aSJaegeuk Kim 	.set_page_dirty	= f2fs_set_meta_page_dirty,
400487261f3SChao Yu 	.invalidatepage = f2fs_invalidate_page,
401487261f3SChao Yu 	.releasepage	= f2fs_release_page,
4025b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION
4035b7a487cSWeichao Guo 	.migratepage    = f2fs_migrate_page,
4045b7a487cSWeichao Guo #endif
405127e670aSJaegeuk Kim };
406127e670aSJaegeuk Kim 
40739d787beSChao Yu static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino,
40839d787beSChao Yu 						unsigned int devidx, int type)
409953e6cc6SJaegeuk Kim {
41067298804SChao Yu 	struct inode_management *im = &sbi->im[type];
41180c54505SJaegeuk Kim 	struct ino_entry *e, *tmp;
41280c54505SJaegeuk Kim 
41380c54505SJaegeuk Kim 	tmp = f2fs_kmem_cache_alloc(ino_entry_slab, GFP_NOFS);
41419526d74SChao Yu 
41580c54505SJaegeuk Kim 	radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
416769ec6e5SJaegeuk Kim 
41767298804SChao Yu 	spin_lock(&im->ino_lock);
41867298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
41939efac41SJaegeuk Kim 	if (!e) {
42080c54505SJaegeuk Kim 		e = tmp;
42119526d74SChao Yu 		if (unlikely(radix_tree_insert(&im->ino_root, ino, e)))
42219526d74SChao Yu 			f2fs_bug_on(sbi, 1);
42319526d74SChao Yu 
42439efac41SJaegeuk Kim 		memset(e, 0, sizeof(struct ino_entry));
42539efac41SJaegeuk Kim 		e->ino = ino;
42639efac41SJaegeuk Kim 
42767298804SChao Yu 		list_add_tail(&e->list, &im->ino_list);
4288c402946SJaegeuk Kim 		if (type != ORPHAN_INO)
42967298804SChao Yu 			im->ino_num++;
43039efac41SJaegeuk Kim 	}
43139d787beSChao Yu 
43239d787beSChao Yu 	if (type == FLUSH_INO)
43339d787beSChao Yu 		f2fs_set_bit(devidx, (char *)&e->dirty_device);
43439d787beSChao Yu 
43567298804SChao Yu 	spin_unlock(&im->ino_lock);
436769ec6e5SJaegeuk Kim 	radix_tree_preload_end();
43780c54505SJaegeuk Kim 
43880c54505SJaegeuk Kim 	if (e != tmp)
43980c54505SJaegeuk Kim 		kmem_cache_free(ino_entry_slab, tmp);
440953e6cc6SJaegeuk Kim }
441953e6cc6SJaegeuk Kim 
4426451e041SJaegeuk Kim static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
443953e6cc6SJaegeuk Kim {
44467298804SChao Yu 	struct inode_management *im = &sbi->im[type];
4456451e041SJaegeuk Kim 	struct ino_entry *e;
446953e6cc6SJaegeuk Kim 
44767298804SChao Yu 	spin_lock(&im->ino_lock);
44867298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
44939efac41SJaegeuk Kim 	if (e) {
450953e6cc6SJaegeuk Kim 		list_del(&e->list);
45167298804SChao Yu 		radix_tree_delete(&im->ino_root, ino);
45267298804SChao Yu 		im->ino_num--;
45367298804SChao Yu 		spin_unlock(&im->ino_lock);
4546451e041SJaegeuk Kim 		kmem_cache_free(ino_entry_slab, e);
455953e6cc6SJaegeuk Kim 		return;
456953e6cc6SJaegeuk Kim 	}
45767298804SChao Yu 	spin_unlock(&im->ino_lock);
458953e6cc6SJaegeuk Kim }
459953e6cc6SJaegeuk Kim 
460a49324f1SChao Yu void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
461fff04f90SJaegeuk Kim {
462fff04f90SJaegeuk Kim 	/* add new dirty ino entry into list */
46339d787beSChao Yu 	__add_ino_entry(sbi, ino, 0, type);
464fff04f90SJaegeuk Kim }
465fff04f90SJaegeuk Kim 
466a49324f1SChao Yu void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
467fff04f90SJaegeuk Kim {
468fff04f90SJaegeuk Kim 	/* remove dirty ino entry from list */
469fff04f90SJaegeuk Kim 	__remove_ino_entry(sbi, ino, type);
470fff04f90SJaegeuk Kim }
471fff04f90SJaegeuk Kim 
472fff04f90SJaegeuk Kim /* mode should be APPEND_INO or UPDATE_INO */
473fff04f90SJaegeuk Kim bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode)
474fff04f90SJaegeuk Kim {
47567298804SChao Yu 	struct inode_management *im = &sbi->im[mode];
476fff04f90SJaegeuk Kim 	struct ino_entry *e;
47767298804SChao Yu 
47867298804SChao Yu 	spin_lock(&im->ino_lock);
47967298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
48067298804SChao Yu 	spin_unlock(&im->ino_lock);
481fff04f90SJaegeuk Kim 	return e ? true : false;
482fff04f90SJaegeuk Kim }
483fff04f90SJaegeuk Kim 
48474ef9241SJaegeuk Kim void release_ino_entry(struct f2fs_sb_info *sbi, bool all)
485fff04f90SJaegeuk Kim {
486fff04f90SJaegeuk Kim 	struct ino_entry *e, *tmp;
487fff04f90SJaegeuk Kim 	int i;
488fff04f90SJaegeuk Kim 
48939d787beSChao Yu 	for (i = all ? ORPHAN_INO : APPEND_INO; i < MAX_INO_ENTRY; i++) {
49067298804SChao Yu 		struct inode_management *im = &sbi->im[i];
49167298804SChao Yu 
49267298804SChao Yu 		spin_lock(&im->ino_lock);
49367298804SChao Yu 		list_for_each_entry_safe(e, tmp, &im->ino_list, list) {
494fff04f90SJaegeuk Kim 			list_del(&e->list);
49567298804SChao Yu 			radix_tree_delete(&im->ino_root, e->ino);
496fff04f90SJaegeuk Kim 			kmem_cache_free(ino_entry_slab, e);
49767298804SChao Yu 			im->ino_num--;
498fff04f90SJaegeuk Kim 		}
49967298804SChao Yu 		spin_unlock(&im->ino_lock);
500fff04f90SJaegeuk Kim 	}
501fff04f90SJaegeuk Kim }
502fff04f90SJaegeuk Kim 
50339d787beSChao Yu void set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
50439d787beSChao Yu 					unsigned int devidx, int type)
50539d787beSChao Yu {
50639d787beSChao Yu 	__add_ino_entry(sbi, ino, devidx, type);
50739d787beSChao Yu }
50839d787beSChao Yu 
50939d787beSChao Yu bool is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
51039d787beSChao Yu 					unsigned int devidx, int type)
51139d787beSChao Yu {
51239d787beSChao Yu 	struct inode_management *im = &sbi->im[type];
51339d787beSChao Yu 	struct ino_entry *e;
51439d787beSChao Yu 	bool is_dirty = false;
51539d787beSChao Yu 
51639d787beSChao Yu 	spin_lock(&im->ino_lock);
51739d787beSChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
51839d787beSChao Yu 	if (e && f2fs_test_bit(devidx, (char *)&e->dirty_device))
51939d787beSChao Yu 		is_dirty = true;
52039d787beSChao Yu 	spin_unlock(&im->ino_lock);
52139d787beSChao Yu 	return is_dirty;
52239d787beSChao Yu }
52339d787beSChao Yu 
524cbd56e7dSJaegeuk Kim int acquire_orphan_inode(struct f2fs_sb_info *sbi)
525127e670aSJaegeuk Kim {
52667298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
527127e670aSJaegeuk Kim 	int err = 0;
528127e670aSJaegeuk Kim 
52967298804SChao Yu 	spin_lock(&im->ino_lock);
530cb78942bSJaegeuk Kim 
531cb78942bSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
5321ecc0c5cSChao Yu 	if (time_to_inject(sbi, FAULT_ORPHAN)) {
533cb78942bSJaegeuk Kim 		spin_unlock(&im->ino_lock);
53455523519SChao Yu 		f2fs_show_injection_info(FAULT_ORPHAN);
535cb78942bSJaegeuk Kim 		return -ENOSPC;
536cb78942bSJaegeuk Kim 	}
537cb78942bSJaegeuk Kim #endif
53867298804SChao Yu 	if (unlikely(im->ino_num >= sbi->max_orphans))
539127e670aSJaegeuk Kim 		err = -ENOSPC;
540cbd56e7dSJaegeuk Kim 	else
54167298804SChao Yu 		im->ino_num++;
54267298804SChao Yu 	spin_unlock(&im->ino_lock);
5430d47c1adSGu Zheng 
544127e670aSJaegeuk Kim 	return err;
545127e670aSJaegeuk Kim }
546127e670aSJaegeuk Kim 
547cbd56e7dSJaegeuk Kim void release_orphan_inode(struct f2fs_sb_info *sbi)
548cbd56e7dSJaegeuk Kim {
54967298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
55067298804SChao Yu 
55167298804SChao Yu 	spin_lock(&im->ino_lock);
55267298804SChao Yu 	f2fs_bug_on(sbi, im->ino_num == 0);
55367298804SChao Yu 	im->ino_num--;
55467298804SChao Yu 	spin_unlock(&im->ino_lock);
555cbd56e7dSJaegeuk Kim }
556cbd56e7dSJaegeuk Kim 
55767c3758dSJaegeuk Kim void add_orphan_inode(struct inode *inode)
558127e670aSJaegeuk Kim {
55939efac41SJaegeuk Kim 	/* add new orphan ino entry into list */
56039d787beSChao Yu 	__add_ino_entry(F2FS_I_SB(inode), inode->i_ino, 0, ORPHAN_INO);
56167c3758dSJaegeuk Kim 	update_inode_page(inode);
562127e670aSJaegeuk Kim }
563127e670aSJaegeuk Kim 
564127e670aSJaegeuk Kim void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
565127e670aSJaegeuk Kim {
566953e6cc6SJaegeuk Kim 	/* remove orphan entry from orphan list */
5676451e041SJaegeuk Kim 	__remove_ino_entry(sbi, ino, ORPHAN_INO);
568127e670aSJaegeuk Kim }
569127e670aSJaegeuk Kim 
5708c14bfadSChao Yu static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
571127e670aSJaegeuk Kim {
5728c14bfadSChao Yu 	struct inode *inode;
5735905f9afSJaegeuk Kim 	struct node_info ni;
574d41065e2SJaegeuk Kim 	int err = acquire_orphan_inode(sbi);
575d41065e2SJaegeuk Kim 
5760f9ec2a8SJaegeuk Kim 	if (err)
5770f9ec2a8SJaegeuk Kim 		goto err_out;
578d41065e2SJaegeuk Kim 
57939d787beSChao Yu 	__add_ino_entry(sbi, ino, 0, ORPHAN_INO);
5808c14bfadSChao Yu 
5815905f9afSJaegeuk Kim 	inode = f2fs_iget_retry(sbi->sb, ino);
5828c14bfadSChao Yu 	if (IS_ERR(inode)) {
5838c14bfadSChao Yu 		/*
5848c14bfadSChao Yu 		 * there should be a bug that we can't find the entry
5858c14bfadSChao Yu 		 * to orphan inode.
5868c14bfadSChao Yu 		 */
5878c14bfadSChao Yu 		f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT);
5888c14bfadSChao Yu 		return PTR_ERR(inode);
5898c14bfadSChao Yu 	}
5908c14bfadSChao Yu 
5910f9ec2a8SJaegeuk Kim 	err = dquot_initialize(inode);
592*a515d12fSSheng Yong 	if (err) {
593*a515d12fSSheng Yong 		iput(inode);
5940f9ec2a8SJaegeuk Kim 		goto err_out;
595*a515d12fSSheng Yong 	}
5960f9ec2a8SJaegeuk Kim 
597127e670aSJaegeuk Kim 	clear_nlink(inode);
598127e670aSJaegeuk Kim 
599127e670aSJaegeuk Kim 	/* truncate all the data during iput */
600127e670aSJaegeuk Kim 	iput(inode);
6015905f9afSJaegeuk Kim 
6025905f9afSJaegeuk Kim 	get_node_info(sbi, ino, &ni);
6035905f9afSJaegeuk Kim 
6045905f9afSJaegeuk Kim 	/* ENOMEM was fully retried in f2fs_evict_inode. */
6055905f9afSJaegeuk Kim 	if (ni.blk_addr != NULL_ADDR) {
6060f9ec2a8SJaegeuk Kim 		err = -EIO;
6070f9ec2a8SJaegeuk Kim 		goto err_out;
6085905f9afSJaegeuk Kim 	}
609d41065e2SJaegeuk Kim 	__remove_ino_entry(sbi, ino, ORPHAN_INO);
6108c14bfadSChao Yu 	return 0;
6110f9ec2a8SJaegeuk Kim 
6120f9ec2a8SJaegeuk Kim err_out:
6130f9ec2a8SJaegeuk Kim 	set_sbi_flag(sbi, SBI_NEED_FSCK);
6140f9ec2a8SJaegeuk Kim 	f2fs_msg(sbi->sb, KERN_WARNING,
6150f9ec2a8SJaegeuk Kim 			"%s: orphan failed (ino=%x), run fsck to fix.",
6160f9ec2a8SJaegeuk Kim 			__func__, ino);
6170f9ec2a8SJaegeuk Kim 	return err;
618127e670aSJaegeuk Kim }
619127e670aSJaegeuk Kim 
6208c14bfadSChao Yu int recover_orphan_inodes(struct f2fs_sb_info *sbi)
621127e670aSJaegeuk Kim {
6223c642985SWanpeng Li 	block_t start_blk, orphan_blocks, i, j;
6234b2414d0SChao Yu 	unsigned int s_flags = sbi->sb->s_flags;
6244b2414d0SChao Yu 	int err = 0;
625ea676733SJaegeuk Kim #ifdef CONFIG_QUOTA
626ea676733SJaegeuk Kim 	int quota_enabled;
627ea676733SJaegeuk Kim #endif
628127e670aSJaegeuk Kim 
629aaec2b1dSChao Yu 	if (!is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG))
6308c14bfadSChao Yu 		return 0;
631127e670aSJaegeuk Kim 
6321751e8a6SLinus Torvalds 	if (s_flags & SB_RDONLY) {
6334b2414d0SChao Yu 		f2fs_msg(sbi->sb, KERN_INFO, "orphan cleanup on readonly fs");
6341751e8a6SLinus Torvalds 		sbi->sb->s_flags &= ~SB_RDONLY;
6354b2414d0SChao Yu 	}
6364b2414d0SChao Yu 
6374b2414d0SChao Yu #ifdef CONFIG_QUOTA
6384b2414d0SChao Yu 	/* Needed for iput() to work correctly and not trash data */
6391751e8a6SLinus Torvalds 	sbi->sb->s_flags |= SB_ACTIVE;
640ea676733SJaegeuk Kim 
6414b2414d0SChao Yu 	/* Turn on quotas so that they are updated correctly */
6421751e8a6SLinus Torvalds 	quota_enabled = f2fs_enable_quota_files(sbi, s_flags & SB_RDONLY);
6434b2414d0SChao Yu #endif
6444b2414d0SChao Yu 
64555141486SWanpeng Li 	start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
6463c642985SWanpeng Li 	orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
647127e670aSJaegeuk Kim 
64826879fb1SChao Yu 	ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true);
649662befdaSChao Yu 
6503c642985SWanpeng Li 	for (i = 0; i < orphan_blocks; i++) {
651127e670aSJaegeuk Kim 		struct page *page = get_meta_page(sbi, start_blk + i);
652127e670aSJaegeuk Kim 		struct f2fs_orphan_block *orphan_blk;
653127e670aSJaegeuk Kim 
654127e670aSJaegeuk Kim 		orphan_blk = (struct f2fs_orphan_block *)page_address(page);
655127e670aSJaegeuk Kim 		for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
656127e670aSJaegeuk Kim 			nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
6578c14bfadSChao Yu 			err = recover_orphan_inode(sbi, ino);
6588c14bfadSChao Yu 			if (err) {
6598c14bfadSChao Yu 				f2fs_put_page(page, 1);
6604b2414d0SChao Yu 				goto out;
6618c14bfadSChao Yu 			}
662127e670aSJaegeuk Kim 		}
663127e670aSJaegeuk Kim 		f2fs_put_page(page, 1);
664127e670aSJaegeuk Kim 	}
665127e670aSJaegeuk Kim 	/* clear Orphan Flag */
666aaec2b1dSChao Yu 	clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
6674b2414d0SChao Yu out:
6684b2414d0SChao Yu #ifdef CONFIG_QUOTA
6694b2414d0SChao Yu 	/* Turn quotas off */
670ea676733SJaegeuk Kim 	if (quota_enabled)
6714b2414d0SChao Yu 		f2fs_quota_off_umount(sbi->sb);
6724b2414d0SChao Yu #endif
6731751e8a6SLinus Torvalds 	sbi->sb->s_flags = s_flags; /* Restore SB_RDONLY status */
6744b2414d0SChao Yu 
6754b2414d0SChao Yu 	return err;
676127e670aSJaegeuk Kim }
677127e670aSJaegeuk Kim 
678127e670aSJaegeuk Kim static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
679127e670aSJaegeuk Kim {
680502c6e0bSGu Zheng 	struct list_head *head;
681127e670aSJaegeuk Kim 	struct f2fs_orphan_block *orphan_blk = NULL;
682127e670aSJaegeuk Kim 	unsigned int nentries = 0;
683bd936f84SChao Yu 	unsigned short index = 1;
6848c402946SJaegeuk Kim 	unsigned short orphan_blocks;
6854531929eSGu Zheng 	struct page *page = NULL;
6866451e041SJaegeuk Kim 	struct ino_entry *orphan = NULL;
68767298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
688127e670aSJaegeuk Kim 
68967298804SChao Yu 	orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num);
6908c402946SJaegeuk Kim 
691d6c67a4fSJaegeuk Kim 	/*
692d6c67a4fSJaegeuk Kim 	 * we don't need to do spin_lock(&im->ino_lock) here, since all the
693d6c67a4fSJaegeuk Kim 	 * orphan inode operations are covered under f2fs_lock_op().
694d6c67a4fSJaegeuk Kim 	 * And, spin_lock should be avoided due to page operations below.
695d6c67a4fSJaegeuk Kim 	 */
69667298804SChao Yu 	head = &im->ino_list;
697127e670aSJaegeuk Kim 
698127e670aSJaegeuk Kim 	/* loop for each orphan inode entry and write them in Jornal block */
699502c6e0bSGu Zheng 	list_for_each_entry(orphan, head, list) {
700502c6e0bSGu Zheng 		if (!page) {
701bd936f84SChao Yu 			page = grab_meta_page(sbi, start_blk++);
702502c6e0bSGu Zheng 			orphan_blk =
703502c6e0bSGu Zheng 				(struct f2fs_orphan_block *)page_address(page);
704502c6e0bSGu Zheng 			memset(orphan_blk, 0, sizeof(*orphan_blk));
705502c6e0bSGu Zheng 		}
706127e670aSJaegeuk Kim 
70736795567SGu Zheng 		orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
708127e670aSJaegeuk Kim 
70936795567SGu Zheng 		if (nentries == F2FS_ORPHANS_PER_BLOCK) {
710127e670aSJaegeuk Kim 			/*
711127e670aSJaegeuk Kim 			 * an orphan block is full of 1020 entries,
712127e670aSJaegeuk Kim 			 * then we need to flush current orphan blocks
713127e670aSJaegeuk Kim 			 * and bring another one in memory
714127e670aSJaegeuk Kim 			 */
715127e670aSJaegeuk Kim 			orphan_blk->blk_addr = cpu_to_le16(index);
716127e670aSJaegeuk Kim 			orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
717127e670aSJaegeuk Kim 			orphan_blk->entry_count = cpu_to_le32(nentries);
718127e670aSJaegeuk Kim 			set_page_dirty(page);
719127e670aSJaegeuk Kim 			f2fs_put_page(page, 1);
720127e670aSJaegeuk Kim 			index++;
721127e670aSJaegeuk Kim 			nentries = 0;
722127e670aSJaegeuk Kim 			page = NULL;
723127e670aSJaegeuk Kim 		}
724127e670aSJaegeuk Kim 	}
725127e670aSJaegeuk Kim 
726502c6e0bSGu Zheng 	if (page) {
727127e670aSJaegeuk Kim 		orphan_blk->blk_addr = cpu_to_le16(index);
728127e670aSJaegeuk Kim 		orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
729127e670aSJaegeuk Kim 		orphan_blk->entry_count = cpu_to_le32(nentries);
730127e670aSJaegeuk Kim 		set_page_dirty(page);
731127e670aSJaegeuk Kim 		f2fs_put_page(page, 1);
732502c6e0bSGu Zheng 	}
733127e670aSJaegeuk Kim }
734127e670aSJaegeuk Kim 
735fc0065adSTiezhu Yang static int get_checkpoint_version(struct f2fs_sb_info *sbi, block_t cp_addr,
736fc0065adSTiezhu Yang 		struct f2fs_checkpoint **cp_block, struct page **cp_page,
737fc0065adSTiezhu Yang 		unsigned long long *version)
738fc0065adSTiezhu Yang {
739fc0065adSTiezhu Yang 	unsigned long blk_size = sbi->blocksize;
740fc0065adSTiezhu Yang 	size_t crc_offset = 0;
741fc0065adSTiezhu Yang 	__u32 crc = 0;
742fc0065adSTiezhu Yang 
743fc0065adSTiezhu Yang 	*cp_page = get_meta_page(sbi, cp_addr);
744fc0065adSTiezhu Yang 	*cp_block = (struct f2fs_checkpoint *)page_address(*cp_page);
745fc0065adSTiezhu Yang 
746fc0065adSTiezhu Yang 	crc_offset = le32_to_cpu((*cp_block)->checksum_offset);
747c6f89dfdSKinglong Mee 	if (crc_offset > (blk_size - sizeof(__le32))) {
748fc0065adSTiezhu Yang 		f2fs_msg(sbi->sb, KERN_WARNING,
749fc0065adSTiezhu Yang 			"invalid crc_offset: %zu", crc_offset);
750fc0065adSTiezhu Yang 		return -EINVAL;
751fc0065adSTiezhu Yang 	}
752fc0065adSTiezhu Yang 
753ced2c7eaSKinglong Mee 	crc = cur_cp_crc(*cp_block);
754fc0065adSTiezhu Yang 	if (!f2fs_crc_valid(sbi, crc, *cp_block, crc_offset)) {
755fc0065adSTiezhu Yang 		f2fs_msg(sbi->sb, KERN_WARNING, "invalid crc value");
756fc0065adSTiezhu Yang 		return -EINVAL;
757fc0065adSTiezhu Yang 	}
758fc0065adSTiezhu Yang 
759fc0065adSTiezhu Yang 	*version = cur_cp_version(*cp_block);
760fc0065adSTiezhu Yang 	return 0;
761fc0065adSTiezhu Yang }
762fc0065adSTiezhu Yang 
763127e670aSJaegeuk Kim static struct page *validate_checkpoint(struct f2fs_sb_info *sbi,
764127e670aSJaegeuk Kim 				block_t cp_addr, unsigned long long *version)
765127e670aSJaegeuk Kim {
766fc0065adSTiezhu Yang 	struct page *cp_page_1 = NULL, *cp_page_2 = NULL;
767fc0065adSTiezhu Yang 	struct f2fs_checkpoint *cp_block = NULL;
768127e670aSJaegeuk Kim 	unsigned long long cur_version = 0, pre_version = 0;
769fc0065adSTiezhu Yang 	int err;
770127e670aSJaegeuk Kim 
771fc0065adSTiezhu Yang 	err = get_checkpoint_version(sbi, cp_addr, &cp_block,
772fc0065adSTiezhu Yang 					&cp_page_1, version);
773fc0065adSTiezhu Yang 	if (err)
774127e670aSJaegeuk Kim 		goto invalid_cp1;
775fc0065adSTiezhu Yang 	pre_version = *version;
776127e670aSJaegeuk Kim 
77725ca923bSJaegeuk Kim 	cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1;
778fc0065adSTiezhu Yang 	err = get_checkpoint_version(sbi, cp_addr, &cp_block,
779fc0065adSTiezhu Yang 					&cp_page_2, version);
780fc0065adSTiezhu Yang 	if (err)
781127e670aSJaegeuk Kim 		goto invalid_cp2;
782fc0065adSTiezhu Yang 	cur_version = *version;
783127e670aSJaegeuk Kim 
784127e670aSJaegeuk Kim 	if (cur_version == pre_version) {
785127e670aSJaegeuk Kim 		*version = cur_version;
786127e670aSJaegeuk Kim 		f2fs_put_page(cp_page_2, 1);
787127e670aSJaegeuk Kim 		return cp_page_1;
788127e670aSJaegeuk Kim 	}
789127e670aSJaegeuk Kim invalid_cp2:
790127e670aSJaegeuk Kim 	f2fs_put_page(cp_page_2, 1);
791127e670aSJaegeuk Kim invalid_cp1:
792127e670aSJaegeuk Kim 	f2fs_put_page(cp_page_1, 1);
793127e670aSJaegeuk Kim 	return NULL;
794127e670aSJaegeuk Kim }
795127e670aSJaegeuk Kim 
796127e670aSJaegeuk Kim int get_valid_checkpoint(struct f2fs_sb_info *sbi)
797127e670aSJaegeuk Kim {
798127e670aSJaegeuk Kim 	struct f2fs_checkpoint *cp_block;
799127e670aSJaegeuk Kim 	struct f2fs_super_block *fsb = sbi->raw_super;
800127e670aSJaegeuk Kim 	struct page *cp1, *cp2, *cur_page;
801127e670aSJaegeuk Kim 	unsigned long blk_size = sbi->blocksize;
802127e670aSJaegeuk Kim 	unsigned long long cp1_version = 0, cp2_version = 0;
803127e670aSJaegeuk Kim 	unsigned long long cp_start_blk_no;
80455141486SWanpeng Li 	unsigned int cp_blks = 1 + __cp_payload(sbi);
8051dbe4152SChangman Lee 	block_t cp_blk_no;
8061dbe4152SChangman Lee 	int i;
807127e670aSJaegeuk Kim 
808acbf054dSChao Yu 	sbi->ckpt = f2fs_kzalloc(sbi, cp_blks * blk_size, GFP_KERNEL);
809127e670aSJaegeuk Kim 	if (!sbi->ckpt)
810127e670aSJaegeuk Kim 		return -ENOMEM;
811127e670aSJaegeuk Kim 	/*
812127e670aSJaegeuk Kim 	 * Finding out valid cp block involves read both
813127e670aSJaegeuk Kim 	 * sets( cp pack1 and cp pack 2)
814127e670aSJaegeuk Kim 	 */
815127e670aSJaegeuk Kim 	cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr);
816127e670aSJaegeuk Kim 	cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
817127e670aSJaegeuk Kim 
818127e670aSJaegeuk Kim 	/* The second checkpoint pack should start at the next segment */
819f9a4e6dfSJaegeuk Kim 	cp_start_blk_no += ((unsigned long long)1) <<
820f9a4e6dfSJaegeuk Kim 				le32_to_cpu(fsb->log_blocks_per_seg);
821127e670aSJaegeuk Kim 	cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
822127e670aSJaegeuk Kim 
823127e670aSJaegeuk Kim 	if (cp1 && cp2) {
824127e670aSJaegeuk Kim 		if (ver_after(cp2_version, cp1_version))
825127e670aSJaegeuk Kim 			cur_page = cp2;
826127e670aSJaegeuk Kim 		else
827127e670aSJaegeuk Kim 			cur_page = cp1;
828127e670aSJaegeuk Kim 	} else if (cp1) {
829127e670aSJaegeuk Kim 		cur_page = cp1;
830127e670aSJaegeuk Kim 	} else if (cp2) {
831127e670aSJaegeuk Kim 		cur_page = cp2;
832127e670aSJaegeuk Kim 	} else {
833127e670aSJaegeuk Kim 		goto fail_no_cp;
834127e670aSJaegeuk Kim 	}
835127e670aSJaegeuk Kim 
836127e670aSJaegeuk Kim 	cp_block = (struct f2fs_checkpoint *)page_address(cur_page);
837127e670aSJaegeuk Kim 	memcpy(sbi->ckpt, cp_block, blk_size);
838127e670aSJaegeuk Kim 
839984ec63cSShawn Lin 	/* Sanity checking of checkpoint */
840984ec63cSShawn Lin 	if (sanity_check_ckpt(sbi))
841a2125ff7SJaegeuk Kim 		goto free_fail_no_cp;
842984ec63cSShawn Lin 
8438508e44aSJaegeuk Kim 	if (cur_page == cp1)
8448508e44aSJaegeuk Kim 		sbi->cur_cp_pack = 1;
8458508e44aSJaegeuk Kim 	else
8468508e44aSJaegeuk Kim 		sbi->cur_cp_pack = 2;
8471dbe4152SChangman Lee 
8481dbe4152SChangman Lee 	if (cp_blks <= 1)
8491dbe4152SChangman Lee 		goto done;
8501dbe4152SChangman Lee 
8511dbe4152SChangman Lee 	cp_blk_no = le32_to_cpu(fsb->cp_blkaddr);
8521dbe4152SChangman Lee 	if (cur_page == cp2)
8531dbe4152SChangman Lee 		cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg);
8541dbe4152SChangman Lee 
8551dbe4152SChangman Lee 	for (i = 1; i < cp_blks; i++) {
8561dbe4152SChangman Lee 		void *sit_bitmap_ptr;
8571dbe4152SChangman Lee 		unsigned char *ckpt = (unsigned char *)sbi->ckpt;
8581dbe4152SChangman Lee 
8591dbe4152SChangman Lee 		cur_page = get_meta_page(sbi, cp_blk_no + i);
8601dbe4152SChangman Lee 		sit_bitmap_ptr = page_address(cur_page);
861127e670aSJaegeuk Kim 		memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size);
862127e670aSJaegeuk Kim 		f2fs_put_page(cur_page, 1);
863127e670aSJaegeuk Kim 	}
864127e670aSJaegeuk Kim done:
865127e670aSJaegeuk Kim 	f2fs_put_page(cp1, 1);
866127e670aSJaegeuk Kim 	f2fs_put_page(cp2, 1);
867127e670aSJaegeuk Kim 	return 0;
868127e670aSJaegeuk Kim 
869a2125ff7SJaegeuk Kim free_fail_no_cp:
870a2125ff7SJaegeuk Kim 	f2fs_put_page(cp1, 1);
871a2125ff7SJaegeuk Kim 	f2fs_put_page(cp2, 1);
872127e670aSJaegeuk Kim fail_no_cp:
873127e670aSJaegeuk Kim 	kfree(sbi->ckpt);
874127e670aSJaegeuk Kim 	return -EINVAL;
875127e670aSJaegeuk Kim }
876127e670aSJaegeuk Kim 
877c227f912SChao Yu static void __add_dirty_inode(struct inode *inode, enum inode_type type)
878127e670aSJaegeuk Kim {
8794081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
880c227f912SChao Yu 	int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
8812d7b822aSChao Yu 
88291942321SJaegeuk Kim 	if (is_inode_flag_set(inode, flag))
8832710fd7eSChao Yu 		return;
8842d7b822aSChao Yu 
88591942321SJaegeuk Kim 	set_inode_flag(inode, flag);
88699f4b917SChao Yu 	if (!f2fs_is_volatile_file(inode))
88799f4b917SChao Yu 		list_add_tail(&F2FS_I(inode)->dirty_list,
88899f4b917SChao Yu 						&sbi->inode_list[type]);
88933fbd510SChao Yu 	stat_inc_dirty_inode(sbi, type);
8905deb8267SJaegeuk Kim }
8915deb8267SJaegeuk Kim 
892c227f912SChao Yu static void __remove_dirty_inode(struct inode *inode, enum inode_type type)
8936ad7609aSChao Yu {
894c227f912SChao Yu 	int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
8956ad7609aSChao Yu 
89691942321SJaegeuk Kim 	if (get_dirty_pages(inode) || !is_inode_flag_set(inode, flag))
8976ad7609aSChao Yu 		return;
8986ad7609aSChao Yu 
89991942321SJaegeuk Kim 	list_del_init(&F2FS_I(inode)->dirty_list);
90091942321SJaegeuk Kim 	clear_inode_flag(inode, flag);
90133fbd510SChao Yu 	stat_dec_dirty_inode(F2FS_I_SB(inode), type);
9026ad7609aSChao Yu }
9036ad7609aSChao Yu 
904a7ffdbe2SJaegeuk Kim void update_dirty_page(struct inode *inode, struct page *page)
9055deb8267SJaegeuk Kim {
9064081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
907c227f912SChao Yu 	enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
9085deb8267SJaegeuk Kim 
9095ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
9105ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
911127e670aSJaegeuk Kim 		return;
9127bd59381SGu Zheng 
913c227f912SChao Yu 	spin_lock(&sbi->inode_lock[type]);
9141c4bf763SJaegeuk Kim 	if (type != FILE_INODE || test_opt(sbi, DATA_FLUSH))
915c227f912SChao Yu 		__add_dirty_inode(inode, type);
916b951a4ecSYunlei He 	inode_inc_dirty_pages(inode);
9171c4bf763SJaegeuk Kim 	spin_unlock(&sbi->inode_lock[type]);
9181c4bf763SJaegeuk Kim 
919a7ffdbe2SJaegeuk Kim 	SetPagePrivate(page);
9209e4ded3fSJaegeuk Kim 	f2fs_trace_pid(page);
9215deb8267SJaegeuk Kim }
922127e670aSJaegeuk Kim 
923c227f912SChao Yu void remove_dirty_inode(struct inode *inode)
924127e670aSJaegeuk Kim {
9254081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
926c227f912SChao Yu 	enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
927127e670aSJaegeuk Kim 
928c227f912SChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
929c227f912SChao Yu 			!S_ISLNK(inode->i_mode))
930127e670aSJaegeuk Kim 		return;
931127e670aSJaegeuk Kim 
93210aa97c3SJaegeuk Kim 	if (type == FILE_INODE && !test_opt(sbi, DATA_FLUSH))
93310aa97c3SJaegeuk Kim 		return;
93410aa97c3SJaegeuk Kim 
935c227f912SChao Yu 	spin_lock(&sbi->inode_lock[type]);
936c227f912SChao Yu 	__remove_dirty_inode(inode, type);
937c227f912SChao Yu 	spin_unlock(&sbi->inode_lock[type]);
938afc3eda2SJaegeuk Kim }
93974d0b917SJaegeuk Kim 
9406d5a1495SChao Yu int sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type)
941127e670aSJaegeuk Kim {
942ce3b7d80SGu Zheng 	struct list_head *head;
943127e670aSJaegeuk Kim 	struct inode *inode;
9442710fd7eSChao Yu 	struct f2fs_inode_info *fi;
9454cf18537SChao Yu 	bool is_dir = (type == DIR_INODE);
9464db08d01SJaegeuk Kim 	unsigned long ino = 0;
9474cf18537SChao Yu 
9484cf18537SChao Yu 	trace_f2fs_sync_dirty_inodes_enter(sbi->sb, is_dir,
9494cf18537SChao Yu 				get_pages(sbi, is_dir ?
9504cf18537SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
951127e670aSJaegeuk Kim retry:
952af41d3eeSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
9536d5a1495SChao Yu 		return -EIO;
954af41d3eeSJaegeuk Kim 
955c227f912SChao Yu 	spin_lock(&sbi->inode_lock[type]);
956ce3b7d80SGu Zheng 
957c227f912SChao Yu 	head = &sbi->inode_list[type];
958127e670aSJaegeuk Kim 	if (list_empty(head)) {
959c227f912SChao Yu 		spin_unlock(&sbi->inode_lock[type]);
9604cf18537SChao Yu 		trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir,
9614cf18537SChao Yu 				get_pages(sbi, is_dir ?
9624cf18537SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
9636d5a1495SChao Yu 		return 0;
964127e670aSJaegeuk Kim 	}
965939afa94SChao Yu 	fi = list_first_entry(head, struct f2fs_inode_info, dirty_list);
9662710fd7eSChao Yu 	inode = igrab(&fi->vfs_inode);
967c227f912SChao Yu 	spin_unlock(&sbi->inode_lock[type]);
968127e670aSJaegeuk Kim 	if (inode) {
9694db08d01SJaegeuk Kim 		unsigned long cur_ino = inode->i_ino;
9704db08d01SJaegeuk Kim 
971b0af6d49SChao Yu 		if (is_dir)
972b0af6d49SChao Yu 			F2FS_I(inode)->cp_task = current;
973b0af6d49SChao Yu 
97487d6f890SJaegeuk Kim 		filemap_fdatawrite(inode->i_mapping);
975b0af6d49SChao Yu 
976b0af6d49SChao Yu 		if (is_dir)
977b0af6d49SChao Yu 			F2FS_I(inode)->cp_task = NULL;
978b0af6d49SChao Yu 
979127e670aSJaegeuk Kim 		iput(inode);
9804db08d01SJaegeuk Kim 		/* We need to give cpu to another writers. */
9814db08d01SJaegeuk Kim 		if (ino == cur_ino) {
9824db08d01SJaegeuk Kim 			congestion_wait(BLK_RW_ASYNC, HZ/50);
9834db08d01SJaegeuk Kim 			cond_resched();
9844db08d01SJaegeuk Kim 		} else {
9854db08d01SJaegeuk Kim 			ino = cur_ino;
9864db08d01SJaegeuk Kim 		}
987127e670aSJaegeuk Kim 	} else {
988127e670aSJaegeuk Kim 		/*
989127e670aSJaegeuk Kim 		 * We should submit bio, since it exists several
990127e670aSJaegeuk Kim 		 * wribacking dentry pages in the freeing inode.
991127e670aSJaegeuk Kim 		 */
992b9109b0eSJaegeuk Kim 		f2fs_submit_merged_write(sbi, DATA);
9937ecebe5eSSebastian Andrzej Siewior 		cond_resched();
994127e670aSJaegeuk Kim 	}
995127e670aSJaegeuk Kim 	goto retry;
996127e670aSJaegeuk Kim }
997127e670aSJaegeuk Kim 
9980f18b462SJaegeuk Kim int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi)
9990f18b462SJaegeuk Kim {
10000f18b462SJaegeuk Kim 	struct list_head *head = &sbi->inode_list[DIRTY_META];
10010f18b462SJaegeuk Kim 	struct inode *inode;
10020f18b462SJaegeuk Kim 	struct f2fs_inode_info *fi;
10030f18b462SJaegeuk Kim 	s64 total = get_pages(sbi, F2FS_DIRTY_IMETA);
10040f18b462SJaegeuk Kim 
10050f18b462SJaegeuk Kim 	while (total--) {
10060f18b462SJaegeuk Kim 		if (unlikely(f2fs_cp_error(sbi)))
10070f18b462SJaegeuk Kim 			return -EIO;
10080f18b462SJaegeuk Kim 
10090f18b462SJaegeuk Kim 		spin_lock(&sbi->inode_lock[DIRTY_META]);
10100f18b462SJaegeuk Kim 		if (list_empty(head)) {
10110f18b462SJaegeuk Kim 			spin_unlock(&sbi->inode_lock[DIRTY_META]);
10120f18b462SJaegeuk Kim 			return 0;
10130f18b462SJaegeuk Kim 		}
1014939afa94SChao Yu 		fi = list_first_entry(head, struct f2fs_inode_info,
10150f18b462SJaegeuk Kim 							gdirty_list);
10160f18b462SJaegeuk Kim 		inode = igrab(&fi->vfs_inode);
10170f18b462SJaegeuk Kim 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
10180f18b462SJaegeuk Kim 		if (inode) {
101918340edcSJaegeuk Kim 			sync_inode_metadata(inode, 0);
102018340edcSJaegeuk Kim 
102118340edcSJaegeuk Kim 			/* it's on eviction */
102218340edcSJaegeuk Kim 			if (is_inode_flag_set(inode, FI_DIRTY_INODE))
10230f18b462SJaegeuk Kim 				update_inode_page(inode);
10240f18b462SJaegeuk Kim 			iput(inode);
10250f18b462SJaegeuk Kim 		}
1026dee668c1SChao Yu 	}
10270f18b462SJaegeuk Kim 	return 0;
10280f18b462SJaegeuk Kim }
10290f18b462SJaegeuk Kim 
103059c9081bSYunlei He static void __prepare_cp_block(struct f2fs_sb_info *sbi)
103159c9081bSYunlei He {
103259c9081bSYunlei He 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
103359c9081bSYunlei He 	struct f2fs_nm_info *nm_i = NM_I(sbi);
103459c9081bSYunlei He 	nid_t last_nid = nm_i->next_scan_nid;
103559c9081bSYunlei He 
103659c9081bSYunlei He 	next_free_nid(sbi, &last_nid);
103759c9081bSYunlei He 	ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
103859c9081bSYunlei He 	ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi));
103959c9081bSYunlei He 	ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi));
104059c9081bSYunlei He 	ckpt->next_free_nid = cpu_to_le32(last_nid);
104159c9081bSYunlei He }
104259c9081bSYunlei He 
10430a8165d7SJaegeuk Kim /*
1044127e670aSJaegeuk Kim  * Freeze all the FS-operations for checkpoint.
1045127e670aSJaegeuk Kim  */
1046cf779cabSJaegeuk Kim static int block_operations(struct f2fs_sb_info *sbi)
1047127e670aSJaegeuk Kim {
1048127e670aSJaegeuk Kim 	struct writeback_control wbc = {
1049127e670aSJaegeuk Kim 		.sync_mode = WB_SYNC_ALL,
1050127e670aSJaegeuk Kim 		.nr_to_write = LONG_MAX,
1051127e670aSJaegeuk Kim 		.for_reclaim = 0,
1052127e670aSJaegeuk Kim 	};
1053c718379bSJaegeuk Kim 	struct blk_plug plug;
1054cf779cabSJaegeuk Kim 	int err = 0;
1055c718379bSJaegeuk Kim 
1056c718379bSJaegeuk Kim 	blk_start_plug(&plug);
1057c718379bSJaegeuk Kim 
105839936837SJaegeuk Kim retry_flush_dents:
1059e479556bSGu Zheng 	f2fs_lock_all(sbi);
1060127e670aSJaegeuk Kim 	/* write all the dirty dentry pages */
1061127e670aSJaegeuk Kim 	if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
1062e479556bSGu Zheng 		f2fs_unlock_all(sbi);
10636d5a1495SChao Yu 		err = sync_dirty_inodes(sbi, DIR_INODE);
10646d5a1495SChao Yu 		if (err)
1065cf779cabSJaegeuk Kim 			goto out;
106630973883SJaegeuk Kim 		cond_resched();
106739936837SJaegeuk Kim 		goto retry_flush_dents;
1068127e670aSJaegeuk Kim 	}
1069127e670aSJaegeuk Kim 
107059c9081bSYunlei He 	/*
107159c9081bSYunlei He 	 * POR: we should ensure that there are no dirty node pages
107259c9081bSYunlei He 	 * until finishing nat/sit flush. inode->i_blocks can be updated.
107359c9081bSYunlei He 	 */
107459c9081bSYunlei He 	down_write(&sbi->node_change);
107559c9081bSYunlei He 
10760f18b462SJaegeuk Kim 	if (get_pages(sbi, F2FS_DIRTY_IMETA)) {
107759c9081bSYunlei He 		up_write(&sbi->node_change);
10780f18b462SJaegeuk Kim 		f2fs_unlock_all(sbi);
10790f18b462SJaegeuk Kim 		err = f2fs_sync_inode_meta(sbi);
10800f18b462SJaegeuk Kim 		if (err)
10810f18b462SJaegeuk Kim 			goto out;
108230973883SJaegeuk Kim 		cond_resched();
10830f18b462SJaegeuk Kim 		goto retry_flush_dents;
10840f18b462SJaegeuk Kim 	}
10850f18b462SJaegeuk Kim 
108639936837SJaegeuk Kim retry_flush_nodes:
1087b3582c68SChao Yu 	down_write(&sbi->node_write);
1088127e670aSJaegeuk Kim 
1089127e670aSJaegeuk Kim 	if (get_pages(sbi, F2FS_DIRTY_NODES)) {
1090b3582c68SChao Yu 		up_write(&sbi->node_write);
1091b0af6d49SChao Yu 		err = sync_node_pages(sbi, &wbc, false, FS_CP_NODE_IO);
10926d5a1495SChao Yu 		if (err) {
109359c9081bSYunlei He 			up_write(&sbi->node_change);
1094cf779cabSJaegeuk Kim 			f2fs_unlock_all(sbi);
1095cf779cabSJaegeuk Kim 			goto out;
1096cf779cabSJaegeuk Kim 		}
109730973883SJaegeuk Kim 		cond_resched();
109839936837SJaegeuk Kim 		goto retry_flush_nodes;
1099127e670aSJaegeuk Kim 	}
110059c9081bSYunlei He 
110159c9081bSYunlei He 	/*
110259c9081bSYunlei He 	 * sbi->node_change is used only for AIO write_begin path which produces
110359c9081bSYunlei He 	 * dirty node blocks and some checkpoint values by block allocation.
110459c9081bSYunlei He 	 */
110559c9081bSYunlei He 	__prepare_cp_block(sbi);
110659c9081bSYunlei He 	up_write(&sbi->node_change);
1107cf779cabSJaegeuk Kim out:
1108c718379bSJaegeuk Kim 	blk_finish_plug(&plug);
1109cf779cabSJaegeuk Kim 	return err;
1110127e670aSJaegeuk Kim }
1111127e670aSJaegeuk Kim 
1112127e670aSJaegeuk Kim static void unblock_operations(struct f2fs_sb_info *sbi)
1113127e670aSJaegeuk Kim {
1114b3582c68SChao Yu 	up_write(&sbi->node_write);
1115e479556bSGu Zheng 	f2fs_unlock_all(sbi);
1116127e670aSJaegeuk Kim }
1117127e670aSJaegeuk Kim 
1118fb51b5efSChangman Lee static void wait_on_all_pages_writeback(struct f2fs_sb_info *sbi)
1119fb51b5efSChangman Lee {
1120fb51b5efSChangman Lee 	DEFINE_WAIT(wait);
1121fb51b5efSChangman Lee 
1122fb51b5efSChangman Lee 	for (;;) {
1123fb51b5efSChangman Lee 		prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
1124fb51b5efSChangman Lee 
112536951b38SChao Yu 		if (!get_pages(sbi, F2FS_WB_CP_DATA))
1126fb51b5efSChangman Lee 			break;
1127fb51b5efSChangman Lee 
11280ff21646SYunlei He 		io_schedule_timeout(5*HZ);
1129fb51b5efSChangman Lee 	}
1130fb51b5efSChangman Lee 	finish_wait(&sbi->cp_wait, &wait);
1131fb51b5efSChangman Lee }
1132fb51b5efSChangman Lee 
1133e4c5d848SJaegeuk Kim static void update_ckpt_flags(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1134e4c5d848SJaegeuk Kim {
1135e4c5d848SJaegeuk Kim 	unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num;
1136e4c5d848SJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1137d1aa2453SChao Yu 	unsigned long flags;
1138e4c5d848SJaegeuk Kim 
1139d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1140e4c5d848SJaegeuk Kim 
1141c473f1a9SChao Yu 	if ((cpc->reason & CP_UMOUNT) &&
114210047f53SKinglong Mee 			le32_to_cpu(ckpt->cp_pack_total_block_count) >
114322ad0b6aSJaegeuk Kim 			sbi->blocks_per_seg - NM_I(sbi)->nat_bits_blocks)
114422ad0b6aSJaegeuk Kim 		disable_nat_bits(sbi, false);
114522ad0b6aSJaegeuk Kim 
11461f43e2adSChao Yu 	if (cpc->reason & CP_TRIMMED)
11471f43e2adSChao Yu 		__set_ckpt_flags(ckpt, CP_TRIMMED_FLAG);
1148cd36d7a1SChao Yu 	else
1149cd36d7a1SChao Yu 		__clear_ckpt_flags(ckpt, CP_TRIMMED_FLAG);
11501f43e2adSChao Yu 
1151c473f1a9SChao Yu 	if (cpc->reason & CP_UMOUNT)
1152e4c5d848SJaegeuk Kim 		__set_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1153e4c5d848SJaegeuk Kim 	else
1154e4c5d848SJaegeuk Kim 		__clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1155e4c5d848SJaegeuk Kim 
1156c473f1a9SChao Yu 	if (cpc->reason & CP_FASTBOOT)
1157e4c5d848SJaegeuk Kim 		__set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1158e4c5d848SJaegeuk Kim 	else
1159e4c5d848SJaegeuk Kim 		__clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1160e4c5d848SJaegeuk Kim 
1161e4c5d848SJaegeuk Kim 	if (orphan_num)
1162e4c5d848SJaegeuk Kim 		__set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1163e4c5d848SJaegeuk Kim 	else
1164e4c5d848SJaegeuk Kim 		__clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1165e4c5d848SJaegeuk Kim 
1166e4c5d848SJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
1167e4c5d848SJaegeuk Kim 		__set_ckpt_flags(ckpt, CP_FSCK_FLAG);
1168e4c5d848SJaegeuk Kim 
1169e4c5d848SJaegeuk Kim 	/* set this flag to activate crc|cp_ver for recovery */
1170e4c5d848SJaegeuk Kim 	__set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG);
1171f2367923SJaegeuk Kim 	__clear_ckpt_flags(ckpt, CP_NOCRC_RECOVERY_FLAG);
1172e4c5d848SJaegeuk Kim 
1173d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1174e4c5d848SJaegeuk Kim }
1175e4c5d848SJaegeuk Kim 
117646706d59SGao Xiang static void commit_checkpoint(struct f2fs_sb_info *sbi,
117746706d59SGao Xiang 	void *src, block_t blk_addr)
117846706d59SGao Xiang {
117946706d59SGao Xiang 	struct writeback_control wbc = {
118046706d59SGao Xiang 		.for_reclaim = 0,
118146706d59SGao Xiang 	};
118246706d59SGao Xiang 
118346706d59SGao Xiang 	/*
118446706d59SGao Xiang 	 * pagevec_lookup_tag and lock_page again will take
118546706d59SGao Xiang 	 * some extra time. Therefore, update_meta_pages and
118646706d59SGao Xiang 	 * sync_meta_pages are combined in this function.
118746706d59SGao Xiang 	 */
118846706d59SGao Xiang 	struct page *page = grab_meta_page(sbi, blk_addr);
118946706d59SGao Xiang 	int err;
119046706d59SGao Xiang 
119146706d59SGao Xiang 	memcpy(page_address(page), src, PAGE_SIZE);
119246706d59SGao Xiang 	set_page_dirty(page);
119346706d59SGao Xiang 
119446706d59SGao Xiang 	f2fs_wait_on_page_writeback(page, META, true);
119546706d59SGao Xiang 	f2fs_bug_on(sbi, PageWriteback(page));
119646706d59SGao Xiang 	if (unlikely(!clear_page_dirty_for_io(page)))
119746706d59SGao Xiang 		f2fs_bug_on(sbi, 1);
119846706d59SGao Xiang 
119946706d59SGao Xiang 	/* writeout cp pack 2 page */
120046706d59SGao Xiang 	err = __f2fs_write_meta_page(page, &wbc, FS_CP_META_IO);
120146706d59SGao Xiang 	f2fs_bug_on(sbi, err);
120246706d59SGao Xiang 
120346706d59SGao Xiang 	f2fs_put_page(page, 0);
120446706d59SGao Xiang 
120546706d59SGao Xiang 	/* submit checkpoint (with barrier if NOBARRIER is not set) */
120646706d59SGao Xiang 	f2fs_submit_merged_write(sbi, META_FLUSH);
120746706d59SGao Xiang }
120846706d59SGao Xiang 
1209c34f42e2SChao Yu static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1210127e670aSJaegeuk Kim {
1211127e670aSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
121277041823SHuang Ying 	struct f2fs_nm_info *nm_i = NM_I(sbi);
1213d1aa2453SChao Yu 	unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num, flags;
1214127e670aSJaegeuk Kim 	block_t start_blk;
1215127e670aSJaegeuk Kim 	unsigned int data_sum_blocks, orphan_blocks;
12167e586fa0SJaegeuk Kim 	__u32 crc32 = 0;
1217127e670aSJaegeuk Kim 	int i;
121855141486SWanpeng Li 	int cp_payload_blks = __cp_payload(sbi);
12198f1dbbbbSShuoran Liu 	struct super_block *sb = sbi->sb;
12208f1dbbbbSShuoran Liu 	struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
12218f1dbbbbSShuoran Liu 	u64 kbytes_written;
12221228b482SChao Yu 	int err;
1223127e670aSJaegeuk Kim 
1224127e670aSJaegeuk Kim 	/* Flush all the NAT/SIT pages */
1225cf779cabSJaegeuk Kim 	while (get_pages(sbi, F2FS_DIRTY_META)) {
1226b0af6d49SChao Yu 		sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
1227cf779cabSJaegeuk Kim 		if (unlikely(f2fs_cp_error(sbi)))
1228c34f42e2SChao Yu 			return -EIO;
1229cf779cabSJaegeuk Kim 	}
1230127e670aSJaegeuk Kim 
1231127e670aSJaegeuk Kim 	/*
1232127e670aSJaegeuk Kim 	 * modify checkpoint
1233127e670aSJaegeuk Kim 	 * version number is already updated
1234127e670aSJaegeuk Kim 	 */
1235127e670aSJaegeuk Kim 	ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi));
1236127e670aSJaegeuk Kim 	ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
1237b5b82205SChao Yu 	for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
1238127e670aSJaegeuk Kim 		ckpt->cur_node_segno[i] =
1239127e670aSJaegeuk Kim 			cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE));
1240127e670aSJaegeuk Kim 		ckpt->cur_node_blkoff[i] =
1241127e670aSJaegeuk Kim 			cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE));
1242127e670aSJaegeuk Kim 		ckpt->alloc_type[i + CURSEG_HOT_NODE] =
1243127e670aSJaegeuk Kim 				curseg_alloc_type(sbi, i + CURSEG_HOT_NODE);
1244127e670aSJaegeuk Kim 	}
1245b5b82205SChao Yu 	for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
1246127e670aSJaegeuk Kim 		ckpt->cur_data_segno[i] =
1247127e670aSJaegeuk Kim 			cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA));
1248127e670aSJaegeuk Kim 		ckpt->cur_data_blkoff[i] =
1249127e670aSJaegeuk Kim 			cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA));
1250127e670aSJaegeuk Kim 		ckpt->alloc_type[i + CURSEG_HOT_DATA] =
1251127e670aSJaegeuk Kim 				curseg_alloc_type(sbi, i + CURSEG_HOT_DATA);
1252127e670aSJaegeuk Kim 	}
1253127e670aSJaegeuk Kim 
1254127e670aSJaegeuk Kim 	/* 2 cp  + n data seg summary + orphan inode blocks */
12553fa06d7bSChao Yu 	data_sum_blocks = npages_for_summary_flush(sbi, false);
1256d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1257b5b82205SChao Yu 	if (data_sum_blocks < NR_CURSEG_DATA_TYPE)
1258aaec2b1dSChao Yu 		__set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
1259127e670aSJaegeuk Kim 	else
1260aaec2b1dSChao Yu 		__clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
1261d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1262127e670aSJaegeuk Kim 
126367298804SChao Yu 	orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num);
12641dbe4152SChangman Lee 	ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks +
12651dbe4152SChangman Lee 			orphan_blocks);
1266127e670aSJaegeuk Kim 
1267119ee914SJaegeuk Kim 	if (__remain_node_summaries(cpc->reason))
1268b5b82205SChao Yu 		ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS+
12691dbe4152SChangman Lee 				cp_payload_blks + data_sum_blocks +
12701dbe4152SChangman Lee 				orphan_blocks + NR_CURSEG_NODE_TYPE);
1271119ee914SJaegeuk Kim 	else
1272b5b82205SChao Yu 		ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
12731dbe4152SChangman Lee 				cp_payload_blks + data_sum_blocks +
12741dbe4152SChangman Lee 				orphan_blocks);
1275119ee914SJaegeuk Kim 
1276e4c5d848SJaegeuk Kim 	/* update ckpt flag for checkpoint */
1277e4c5d848SJaegeuk Kim 	update_ckpt_flags(sbi, cpc);
1278a468f0efSJaegeuk Kim 
1279127e670aSJaegeuk Kim 	/* update SIT/NAT bitmap */
1280127e670aSJaegeuk Kim 	get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
1281127e670aSJaegeuk Kim 	get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
1282127e670aSJaegeuk Kim 
128343b6573bSKeith Mok 	crc32 = f2fs_crc32(sbi, ckpt, le32_to_cpu(ckpt->checksum_offset));
12847e586fa0SJaegeuk Kim 	*((__le32 *)((unsigned char *)ckpt +
12857e586fa0SJaegeuk Kim 				le32_to_cpu(ckpt->checksum_offset)))
1286127e670aSJaegeuk Kim 				= cpu_to_le32(crc32);
1287127e670aSJaegeuk Kim 
12888508e44aSJaegeuk Kim 	start_blk = __start_cp_next_addr(sbi);
1289127e670aSJaegeuk Kim 
129022ad0b6aSJaegeuk Kim 	/* write nat bits */
129122ad0b6aSJaegeuk Kim 	if (enabled_nat_bits(sbi, cpc)) {
129222ad0b6aSJaegeuk Kim 		__u64 cp_ver = cur_cp_version(ckpt);
129322ad0b6aSJaegeuk Kim 		block_t blk;
129422ad0b6aSJaegeuk Kim 
129522ad0b6aSJaegeuk Kim 		cp_ver |= ((__u64)crc32 << 32);
129622ad0b6aSJaegeuk Kim 		*(__le64 *)nm_i->nat_bits = cpu_to_le64(cp_ver);
129722ad0b6aSJaegeuk Kim 
129822ad0b6aSJaegeuk Kim 		blk = start_blk + sbi->blocks_per_seg - nm_i->nat_bits_blocks;
129922ad0b6aSJaegeuk Kim 		for (i = 0; i < nm_i->nat_bits_blocks; i++)
130022ad0b6aSJaegeuk Kim 			update_meta_page(sbi, nm_i->nat_bits +
130122ad0b6aSJaegeuk Kim 					(i << F2FS_BLKSIZE_BITS), blk + i);
130222ad0b6aSJaegeuk Kim 
130322ad0b6aSJaegeuk Kim 		/* Flush all the NAT BITS pages */
130422ad0b6aSJaegeuk Kim 		while (get_pages(sbi, F2FS_DIRTY_META)) {
1305b0af6d49SChao Yu 			sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
130622ad0b6aSJaegeuk Kim 			if (unlikely(f2fs_cp_error(sbi)))
130722ad0b6aSJaegeuk Kim 				return -EIO;
130822ad0b6aSJaegeuk Kim 		}
130922ad0b6aSJaegeuk Kim 	}
131022ad0b6aSJaegeuk Kim 
1311127e670aSJaegeuk Kim 	/* write out checkpoint buffer at block 0 */
1312381722d2SChao Yu 	update_meta_page(sbi, ckpt, start_blk++);
1313127e670aSJaegeuk Kim 
1314381722d2SChao Yu 	for (i = 1; i < 1 + cp_payload_blks; i++)
1315381722d2SChao Yu 		update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE,
1316381722d2SChao Yu 							start_blk++);
13171dbe4152SChangman Lee 
131867298804SChao Yu 	if (orphan_num) {
1319127e670aSJaegeuk Kim 		write_orphan_inodes(sbi, start_blk);
1320127e670aSJaegeuk Kim 		start_blk += orphan_blocks;
1321127e670aSJaegeuk Kim 	}
1322127e670aSJaegeuk Kim 
1323127e670aSJaegeuk Kim 	write_data_summaries(sbi, start_blk);
1324127e670aSJaegeuk Kim 	start_blk += data_sum_blocks;
13258f1dbbbbSShuoran Liu 
13268f1dbbbbSShuoran Liu 	/* Record write statistics in the hot node summary */
13278f1dbbbbSShuoran Liu 	kbytes_written = sbi->kbytes_written;
13288f1dbbbbSShuoran Liu 	if (sb->s_bdev->bd_part)
13298f1dbbbbSShuoran Liu 		kbytes_written += BD_PART_WRITTEN(sbi);
13308f1dbbbbSShuoran Liu 
1331b7ad7512SChao Yu 	seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written);
13328f1dbbbbSShuoran Liu 
1333119ee914SJaegeuk Kim 	if (__remain_node_summaries(cpc->reason)) {
1334127e670aSJaegeuk Kim 		write_node_summaries(sbi, start_blk);
1335127e670aSJaegeuk Kim 		start_blk += NR_CURSEG_NODE_TYPE;
1336127e670aSJaegeuk Kim 	}
1337127e670aSJaegeuk Kim 
133846706d59SGao Xiang 	/* update user_block_counts */
133946706d59SGao Xiang 	sbi->last_valid_block_count = sbi->total_valid_block_count;
134046706d59SGao Xiang 	percpu_counter_set(&sbi->alloc_valid_block_count, 0);
1341127e670aSJaegeuk Kim 
134246706d59SGao Xiang 	/* Here, we have one bio having CP pack except cp pack 2 page */
134346706d59SGao Xiang 	sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
134446706d59SGao Xiang 
134546706d59SGao Xiang 	/* wait for previous submitted meta pages writeback */
1346fb51b5efSChangman Lee 	wait_on_all_pages_writeback(sbi);
1347127e670aSJaegeuk Kim 
1348cf779cabSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
1349c34f42e2SChao Yu 		return -EIO;
1350cf779cabSJaegeuk Kim 
135146706d59SGao Xiang 	/* flush all device cache */
135246706d59SGao Xiang 	err = f2fs_flush_device_cache(sbi);
135346706d59SGao Xiang 	if (err)
135446706d59SGao Xiang 		return err;
1355127e670aSJaegeuk Kim 
135646706d59SGao Xiang 	/* barrier and flush checkpoint cp pack 2 page if it can */
135746706d59SGao Xiang 	commit_checkpoint(sbi, ckpt, start_blk);
13586a8f8ca5SJaegeuk Kim 	wait_on_all_pages_writeback(sbi);
13596a8f8ca5SJaegeuk Kim 
136074ef9241SJaegeuk Kim 	release_ino_entry(sbi, false);
1361cf779cabSJaegeuk Kim 
1362cf779cabSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
1363c34f42e2SChao Yu 		return -EIO;
1364cf779cabSJaegeuk Kim 
1365caf0047eSChao Yu 	clear_sbi_flag(sbi, SBI_IS_DIRTY);
1366bbf156f7SJaegeuk Kim 	clear_sbi_flag(sbi, SBI_NEED_CP);
13678508e44aSJaegeuk Kim 	__set_cp_next_pack(sbi);
1368c34f42e2SChao Yu 
1369c2a080aeSChao Yu 	/*
1370c2a080aeSChao Yu 	 * redirty superblock if metadata like node page or inode cache is
1371c2a080aeSChao Yu 	 * updated during writing checkpoint.
1372c2a080aeSChao Yu 	 */
1373c2a080aeSChao Yu 	if (get_pages(sbi, F2FS_DIRTY_NODES) ||
1374c2a080aeSChao Yu 			get_pages(sbi, F2FS_DIRTY_IMETA))
1375c2a080aeSChao Yu 		set_sbi_flag(sbi, SBI_IS_DIRTY);
1376c2a080aeSChao Yu 
1377c2a080aeSChao Yu 	f2fs_bug_on(sbi, get_pages(sbi, F2FS_DIRTY_DENTS));
1378c2a080aeSChao Yu 
1379c34f42e2SChao Yu 	return 0;
1380127e670aSJaegeuk Kim }
1381127e670aSJaegeuk Kim 
13820a8165d7SJaegeuk Kim /*
1383e1c42045Sarter97  * We guarantee that this checkpoint procedure will not fail.
1384127e670aSJaegeuk Kim  */
1385c34f42e2SChao Yu int write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1386127e670aSJaegeuk Kim {
1387127e670aSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1388127e670aSJaegeuk Kim 	unsigned long long ckpt_ver;
1389c34f42e2SChao Yu 	int err = 0;
1390127e670aSJaegeuk Kim 
1391127e670aSJaegeuk Kim 	mutex_lock(&sbi->cp_mutex);
13928501017eSJaegeuk Kim 
1393caf0047eSChao Yu 	if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) &&
1394c473f1a9SChao Yu 		((cpc->reason & CP_FASTBOOT) || (cpc->reason & CP_SYNC) ||
1395c473f1a9SChao Yu 		((cpc->reason & CP_DISCARD) && !sbi->discard_blks)))
13968501017eSJaegeuk Kim 		goto out;
1397c34f42e2SChao Yu 	if (unlikely(f2fs_cp_error(sbi))) {
1398c34f42e2SChao Yu 		err = -EIO;
1399cf779cabSJaegeuk Kim 		goto out;
1400c34f42e2SChao Yu 	}
1401c34f42e2SChao Yu 	if (f2fs_readonly(sbi->sb)) {
1402c34f42e2SChao Yu 		err = -EROFS;
140311504a8eSJaegeuk Kim 		goto out;
1404c34f42e2SChao Yu 	}
14052bda542dSWanpeng Li 
14062bda542dSWanpeng Li 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops");
14072bda542dSWanpeng Li 
1408c34f42e2SChao Yu 	err = block_operations(sbi);
1409c34f42e2SChao Yu 	if (err)
1410cf779cabSJaegeuk Kim 		goto out;
1411127e670aSJaegeuk Kim 
141275ab4cb8SJaegeuk Kim 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops");
14132af4bd6cSNamjae Jeon 
1414b9109b0eSJaegeuk Kim 	f2fs_flush_merged_writes(sbi);
1415127e670aSJaegeuk Kim 
141658cce381SYunlei He 	/* this is the case of multiple fstrims without any changes */
1417c473f1a9SChao Yu 	if (cpc->reason & CP_DISCARD) {
141825290fa5SJaegeuk Kim 		if (!exist_trim_candidates(sbi, cpc)) {
141925290fa5SJaegeuk Kim 			unblock_operations(sbi);
142025290fa5SJaegeuk Kim 			goto out;
142125290fa5SJaegeuk Kim 		}
142225290fa5SJaegeuk Kim 
14230333ad4eSJaegeuk Kim 		if (NM_I(sbi)->dirty_nat_cnt == 0 &&
14240333ad4eSJaegeuk Kim 				SIT_I(sbi)->dirty_sentries == 0 &&
14250333ad4eSJaegeuk Kim 				prefree_segments(sbi) == 0) {
142658cce381SYunlei He 			flush_sit_entries(sbi, cpc);
142758cce381SYunlei He 			clear_prefree_segments(sbi, cpc);
142858cce381SYunlei He 			unblock_operations(sbi);
142958cce381SYunlei He 			goto out;
143058cce381SYunlei He 		}
14310333ad4eSJaegeuk Kim 	}
143258cce381SYunlei He 
1433127e670aSJaegeuk Kim 	/*
1434127e670aSJaegeuk Kim 	 * update checkpoint pack index
1435127e670aSJaegeuk Kim 	 * Increase the version number so that
1436127e670aSJaegeuk Kim 	 * SIT entries and seg summaries are written at correct place
1437127e670aSJaegeuk Kim 	 */
1438d71b5564SJaegeuk Kim 	ckpt_ver = cur_cp_version(ckpt);
1439127e670aSJaegeuk Kim 	ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
1440127e670aSJaegeuk Kim 
1441127e670aSJaegeuk Kim 	/* write cached NAT/SIT entries to NAT/SIT area */
144222ad0b6aSJaegeuk Kim 	flush_nat_entries(sbi, cpc);
14434b2fecc8SJaegeuk Kim 	flush_sit_entries(sbi, cpc);
1444127e670aSJaegeuk Kim 
1445127e670aSJaegeuk Kim 	/* unlock all the fs_lock[] in do_checkpoint() */
1446c34f42e2SChao Yu 	err = do_checkpoint(sbi, cpc);
14474e6a8d9bSJaegeuk Kim 	if (err)
14482dd15654SChao Yu 		release_discard_addrs(sbi);
14494e6a8d9bSJaegeuk Kim 	else
14502dd15654SChao Yu 		clear_prefree_segments(sbi, cpc);
1451275b66b0SChao Yu 
1452127e670aSJaegeuk Kim 	unblock_operations(sbi);
1453942e0be6SChangman Lee 	stat_inc_cp_count(sbi->stat_info);
145410027551SJaegeuk Kim 
1455c473f1a9SChao Yu 	if (cpc->reason & CP_RECOVERY)
145610027551SJaegeuk Kim 		f2fs_msg(sbi->sb, KERN_NOTICE,
145710027551SJaegeuk Kim 			"checkpoint: version = %llx", ckpt_ver);
145860b99b48SJaegeuk Kim 
145960b99b48SJaegeuk Kim 	/* do checkpoint periodically */
14606beceb54SJaegeuk Kim 	f2fs_update_time(sbi, CP_TIME);
146155d1cdb2SJaegeuk Kim 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint");
14628501017eSJaegeuk Kim out:
14638501017eSJaegeuk Kim 	mutex_unlock(&sbi->cp_mutex);
1464c34f42e2SChao Yu 	return err;
1465127e670aSJaegeuk Kim }
1466127e670aSJaegeuk Kim 
14676451e041SJaegeuk Kim void init_ino_entry_info(struct f2fs_sb_info *sbi)
1468127e670aSJaegeuk Kim {
14696451e041SJaegeuk Kim 	int i;
14706451e041SJaegeuk Kim 
14716451e041SJaegeuk Kim 	for (i = 0; i < MAX_INO_ENTRY; i++) {
147267298804SChao Yu 		struct inode_management *im = &sbi->im[i];
147367298804SChao Yu 
147467298804SChao Yu 		INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC);
147567298804SChao Yu 		spin_lock_init(&im->ino_lock);
147667298804SChao Yu 		INIT_LIST_HEAD(&im->ino_list);
147767298804SChao Yu 		im->ino_num = 0;
14786451e041SJaegeuk Kim 	}
14796451e041SJaegeuk Kim 
1480b5b82205SChao Yu 	sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS -
148114b42817SWanpeng Li 			NR_CURSEG_TYPE - __cp_payload(sbi)) *
148214b42817SWanpeng Li 				F2FS_ORPHANS_PER_BLOCK;
1483127e670aSJaegeuk Kim }
1484127e670aSJaegeuk Kim 
14856e6093a8SNamjae Jeon int __init create_checkpoint_caches(void)
1486127e670aSJaegeuk Kim {
14876451e041SJaegeuk Kim 	ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry",
14886451e041SJaegeuk Kim 			sizeof(struct ino_entry));
14896451e041SJaegeuk Kim 	if (!ino_entry_slab)
1490127e670aSJaegeuk Kim 		return -ENOMEM;
149106292073SChao Yu 	inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry",
149206292073SChao Yu 			sizeof(struct inode_entry));
14936bacf52fSJaegeuk Kim 	if (!inode_entry_slab) {
14946451e041SJaegeuk Kim 		kmem_cache_destroy(ino_entry_slab);
1495127e670aSJaegeuk Kim 		return -ENOMEM;
1496127e670aSJaegeuk Kim 	}
1497127e670aSJaegeuk Kim 	return 0;
1498127e670aSJaegeuk Kim }
1499127e670aSJaegeuk Kim 
1500127e670aSJaegeuk Kim void destroy_checkpoint_caches(void)
1501127e670aSJaegeuk Kim {
15026451e041SJaegeuk Kim 	kmem_cache_destroy(ino_entry_slab);
1503127e670aSJaegeuk Kim 	kmem_cache_destroy(inode_entry_slab);
1504127e670aSJaegeuk Kim }
1505