xref: /openbmc/linux/fs/f2fs/checkpoint.c (revision 7fa750a163089cf96866de402314d853a96cb342)
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;
274d57b86dSChao Yu struct kmem_cache *f2fs_inode_entry_slab;
28127e670aSJaegeuk Kim 
2938f91ca8SJaegeuk Kim void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io)
3038f91ca8SJaegeuk Kim {
31d494500aSChao Yu 	f2fs_build_fault_attr(sbi, 0, 0);
32aaec2b1dSChao Yu 	set_ckpt_flags(sbi, CP_ERROR_FLAG);
3338f91ca8SJaegeuk Kim 	if (!end_io)
34b9109b0eSJaegeuk Kim 		f2fs_flush_merged_writes(sbi);
3538f91ca8SJaegeuk Kim }
3638f91ca8SJaegeuk Kim 
370a8165d7SJaegeuk Kim /*
38127e670aSJaegeuk Kim  * We guarantee no failure on the returned page.
39127e670aSJaegeuk Kim  */
404d57b86dSChao Yu struct page *f2fs_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,
720833721eSYunlei He 		.is_meta = is_meta,
73cf04e8ebSJaegeuk Kim 	};
747735730dSChao Yu 	int err;
752b947003SChao Yu 
762b947003SChao Yu 	if (unlikely(!is_meta))
7704d328deSMike Christie 		fio.op_flags &= ~REQ_META;
78127e670aSJaegeuk Kim repeat:
79300e129cSJaegeuk Kim 	page = f2fs_grab_cache_page(mapping, index, false);
80127e670aSJaegeuk Kim 	if (!page) {
81127e670aSJaegeuk Kim 		cond_resched();
82127e670aSJaegeuk Kim 		goto repeat;
83127e670aSJaegeuk Kim 	}
84393ff91fSJaegeuk Kim 	if (PageUptodate(page))
85393ff91fSJaegeuk Kim 		goto out;
86127e670aSJaegeuk Kim 
8705ca3632SJaegeuk Kim 	fio.page = page;
8805ca3632SJaegeuk Kim 
897735730dSChao Yu 	err = f2fs_submit_page_bio(&fio);
907735730dSChao Yu 	if (err) {
917735730dSChao Yu 		f2fs_put_page(page, 1);
927735730dSChao Yu 		return ERR_PTR(err);
9386531d6bSJaegeuk Kim 	}
94393ff91fSJaegeuk Kim 
95393ff91fSJaegeuk Kim 	lock_page(page);
966bacf52fSJaegeuk Kim 	if (unlikely(page->mapping != mapping)) {
97afcb7ca0SJaegeuk Kim 		f2fs_put_page(page, 1);
98afcb7ca0SJaegeuk Kim 		goto repeat;
99afcb7ca0SJaegeuk Kim 	}
100f3f338caSChao Yu 
10181114baaSChao Yu 	if (unlikely(!PageUptodate(page))) {
1027735730dSChao Yu 		f2fs_put_page(page, 1);
1037735730dSChao Yu 		return ERR_PTR(-EIO);
10481114baaSChao Yu 	}
105393ff91fSJaegeuk Kim out:
106127e670aSJaegeuk Kim 	return page;
107127e670aSJaegeuk Kim }
108127e670aSJaegeuk Kim 
1094d57b86dSChao Yu struct page *f2fs_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 
1147735730dSChao Yu struct page *f2fs_get_meta_page_nofail(struct f2fs_sb_info *sbi, pgoff_t index)
1157735730dSChao Yu {
1167735730dSChao Yu 	struct page *page;
1177735730dSChao Yu 	int count = 0;
1187735730dSChao Yu 
1197735730dSChao Yu retry:
1207735730dSChao Yu 	page = __get_meta_page(sbi, index, true);
1217735730dSChao Yu 	if (IS_ERR(page)) {
1227735730dSChao Yu 		if (PTR_ERR(page) == -EIO &&
1237735730dSChao Yu 				++count <= DEFAULT_RETRY_IO_COUNT)
1247735730dSChao Yu 			goto retry;
1257735730dSChao Yu 
1267735730dSChao Yu 		f2fs_stop_checkpoint(sbi, false);
1277735730dSChao Yu 		f2fs_bug_on(sbi, 1);
1287735730dSChao Yu 	}
1297735730dSChao Yu 
1307735730dSChao Yu 	return page;
1317735730dSChao Yu }
1327735730dSChao Yu 
1332b947003SChao Yu /* for POR only */
1344d57b86dSChao Yu struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index)
1352b947003SChao Yu {
1362b947003SChao Yu 	return __get_meta_page(sbi, index, false);
1372b947003SChao Yu }
1382b947003SChao Yu 
139e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
1404d57b86dSChao Yu 					block_t blkaddr, int type)
141662befdaSChao Yu {
142662befdaSChao Yu 	switch (type) {
143662befdaSChao Yu 	case META_NAT:
14466b00c18SChao Yu 		break;
145662befdaSChao Yu 	case META_SIT:
14666b00c18SChao Yu 		if (unlikely(blkaddr >= SIT_BLK_CNT(sbi)))
14766b00c18SChao Yu 			return false;
14866b00c18SChao Yu 		break;
14981c1a0f1SChao Yu 	case META_SSA:
15066b00c18SChao Yu 		if (unlikely(blkaddr >= MAIN_BLKADDR(sbi) ||
15166b00c18SChao Yu 			blkaddr < SM_I(sbi)->ssa_blkaddr))
15266b00c18SChao Yu 			return false;
15366b00c18SChao Yu 		break;
154662befdaSChao Yu 	case META_CP:
15566b00c18SChao Yu 		if (unlikely(blkaddr >= SIT_I(sbi)->sit_base_addr ||
15666b00c18SChao Yu 			blkaddr < __start_cp_addr(sbi)))
15766b00c18SChao Yu 			return false;
15866b00c18SChao Yu 		break;
1594c521f49SJaegeuk Kim 	case META_POR:
160e1da7872SChao Yu 	case DATA_GENERIC:
16166b00c18SChao Yu 		if (unlikely(blkaddr >= MAX_BLKADDR(sbi) ||
162c9b60788SChao Yu 			blkaddr < MAIN_BLKADDR(sbi))) {
163c9b60788SChao Yu 			if (type == DATA_GENERIC) {
164c9b60788SChao Yu 				f2fs_msg(sbi->sb, KERN_WARNING,
165c9b60788SChao Yu 					"access invalid blkaddr:%u", blkaddr);
166c9b60788SChao Yu 				WARN_ON(1);
167c9b60788SChao Yu 			}
16866b00c18SChao Yu 			return false;
169c9b60788SChao Yu 		}
17066b00c18SChao Yu 		break;
171e1da7872SChao Yu 	case META_GENERIC:
172e1da7872SChao Yu 		if (unlikely(blkaddr < SEG0_BLKADDR(sbi) ||
173e1da7872SChao Yu 			blkaddr >= MAIN_BLKADDR(sbi)))
174e1da7872SChao Yu 			return false;
175e1da7872SChao Yu 		break;
176662befdaSChao Yu 	default:
177662befdaSChao Yu 		BUG();
178662befdaSChao Yu 	}
17966b00c18SChao Yu 
18066b00c18SChao Yu 	return true;
181662befdaSChao Yu }
182662befdaSChao Yu 
183662befdaSChao Yu /*
18481c1a0f1SChao Yu  * Readahead CP/NAT/SIT/SSA pages
185662befdaSChao Yu  */
1864d57b86dSChao Yu int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
18726879fb1SChao Yu 							int type, bool sync)
188662befdaSChao Yu {
189662befdaSChao Yu 	struct page *page;
1904c521f49SJaegeuk Kim 	block_t blkno = start;
191662befdaSChao Yu 	struct f2fs_io_info fio = {
19205ca3632SJaegeuk Kim 		.sbi = sbi,
193662befdaSChao Yu 		.type = META,
19404d328deSMike Christie 		.op = REQ_OP_READ,
19570fd7614SChristoph Hellwig 		.op_flags = sync ? (REQ_META | REQ_PRIO) : REQ_RAHEAD,
1964375a336SJaegeuk Kim 		.encrypted_page = NULL,
197fb830fc5SChao Yu 		.in_list = false,
1980833721eSYunlei He 		.is_meta = (type != META_POR),
199662befdaSChao Yu 	};
200e9f5b8b8SChao Yu 	struct blk_plug plug;
201662befdaSChao Yu 
2022b947003SChao Yu 	if (unlikely(type == META_POR))
20304d328deSMike Christie 		fio.op_flags &= ~REQ_META;
2042b947003SChao Yu 
205e9f5b8b8SChao Yu 	blk_start_plug(&plug);
206662befdaSChao Yu 	for (; nrpages-- > 0; blkno++) {
207662befdaSChao Yu 
208e1da7872SChao Yu 		if (!f2fs_is_valid_blkaddr(sbi, blkno, type))
20966b00c18SChao Yu 			goto out;
21066b00c18SChao Yu 
211662befdaSChao Yu 		switch (type) {
212662befdaSChao Yu 		case META_NAT:
21366b00c18SChao Yu 			if (unlikely(blkno >=
21466b00c18SChao Yu 					NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid)))
215662befdaSChao Yu 				blkno = 0;
21666b00c18SChao Yu 			/* get nat block addr */
2177a9d7548SChao Yu 			fio.new_blkaddr = current_nat_addr(sbi,
218662befdaSChao Yu 					blkno * NAT_ENTRY_PER_BLOCK);
219662befdaSChao Yu 			break;
220662befdaSChao Yu 		case META_SIT:
221662befdaSChao Yu 			/* get sit block addr */
2227a9d7548SChao Yu 			fio.new_blkaddr = current_sit_addr(sbi,
223662befdaSChao Yu 					blkno * SIT_ENTRY_PER_BLOCK);
224662befdaSChao Yu 			break;
22581c1a0f1SChao Yu 		case META_SSA:
226662befdaSChao Yu 		case META_CP:
2274c521f49SJaegeuk Kim 		case META_POR:
2287a9d7548SChao Yu 			fio.new_blkaddr = blkno;
229662befdaSChao Yu 			break;
230662befdaSChao Yu 		default:
231662befdaSChao Yu 			BUG();
232662befdaSChao Yu 		}
233662befdaSChao Yu 
234300e129cSJaegeuk Kim 		page = f2fs_grab_cache_page(META_MAPPING(sbi),
235300e129cSJaegeuk Kim 						fio.new_blkaddr, false);
236662befdaSChao Yu 		if (!page)
237662befdaSChao Yu 			continue;
238662befdaSChao Yu 		if (PageUptodate(page)) {
239662befdaSChao Yu 			f2fs_put_page(page, 1);
240662befdaSChao Yu 			continue;
241662befdaSChao Yu 		}
242662befdaSChao Yu 
24305ca3632SJaegeuk Kim 		fio.page = page;
2441919ffc0SJaegeuk Kim 		f2fs_submit_page_bio(&fio);
245662befdaSChao Yu 		f2fs_put_page(page, 0);
246662befdaSChao Yu 	}
247662befdaSChao Yu out:
248e9f5b8b8SChao Yu 	blk_finish_plug(&plug);
249662befdaSChao Yu 	return blkno - start;
250662befdaSChao Yu }
251662befdaSChao Yu 
2524d57b86dSChao Yu void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index)
253635aee1fSChao Yu {
254635aee1fSChao Yu 	struct page *page;
255635aee1fSChao Yu 	bool readahead = false;
256635aee1fSChao Yu 
257635aee1fSChao Yu 	page = find_get_page(META_MAPPING(sbi), index);
2584da7bf5aSJaegeuk Kim 	if (!page || !PageUptodate(page))
259635aee1fSChao Yu 		readahead = true;
260635aee1fSChao Yu 	f2fs_put_page(page, 0);
261635aee1fSChao Yu 
262635aee1fSChao Yu 	if (readahead)
2634d57b86dSChao Yu 		f2fs_ra_meta_pages(sbi, index, BIO_MAX_PAGES, META_POR, true);
264635aee1fSChao Yu }
265635aee1fSChao Yu 
266b0af6d49SChao Yu static int __f2fs_write_meta_page(struct page *page,
267b0af6d49SChao Yu 				struct writeback_control *wbc,
268b0af6d49SChao Yu 				enum iostat_type io_type)
269127e670aSJaegeuk Kim {
2704081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_P_SB(page);
271577e3495SJaegeuk Kim 
272ecda0de3SChao Yu 	trace_f2fs_writepage(page, META);
273ecda0de3SChao Yu 
274af697c0fSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
275af697c0fSJaegeuk Kim 		goto redirty_out;
276caf0047eSChao Yu 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
277cfb271d4SChao Yu 		goto redirty_out;
278857dc4e0SJaegeuk Kim 	if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0))
279cfb271d4SChao Yu 		goto redirty_out;
280127e670aSJaegeuk Kim 
2814d57b86dSChao Yu 	f2fs_do_write_meta_page(sbi, page, io_type);
282127e670aSJaegeuk Kim 	dec_page_count(sbi, F2FS_DIRTY_META);
2830c3a5797SChao Yu 
2840c3a5797SChao Yu 	if (wbc->for_reclaim)
285b9109b0eSJaegeuk Kim 		f2fs_submit_merged_write_cond(sbi, page->mapping->host,
286b9109b0eSJaegeuk Kim 						0, page->index, META);
2870c3a5797SChao Yu 
288127e670aSJaegeuk Kim 	unlock_page(page);
289857dc4e0SJaegeuk Kim 
2900c3a5797SChao Yu 	if (unlikely(f2fs_cp_error(sbi)))
291b9109b0eSJaegeuk Kim 		f2fs_submit_merged_write(sbi, META);
2920c3a5797SChao Yu 
293577e3495SJaegeuk Kim 	return 0;
294cfb271d4SChao Yu 
295cfb271d4SChao Yu redirty_out:
29676f60268SJaegeuk Kim 	redirty_page_for_writepage(wbc, page);
297cfb271d4SChao Yu 	return AOP_WRITEPAGE_ACTIVATE;
298127e670aSJaegeuk Kim }
299127e670aSJaegeuk Kim 
300b0af6d49SChao Yu static int f2fs_write_meta_page(struct page *page,
301b0af6d49SChao Yu 				struct writeback_control *wbc)
302b0af6d49SChao Yu {
303b0af6d49SChao Yu 	return __f2fs_write_meta_page(page, wbc, FS_META_IO);
304b0af6d49SChao Yu }
305b0af6d49SChao Yu 
306127e670aSJaegeuk Kim static int f2fs_write_meta_pages(struct address_space *mapping,
307127e670aSJaegeuk Kim 				struct writeback_control *wbc)
308127e670aSJaegeuk Kim {
3094081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
31050c8cdb3SJaegeuk Kim 	long diff, written;
311127e670aSJaegeuk Kim 
3120771fcc7SChao Yu 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
3130771fcc7SChao Yu 		goto skip_write;
3140771fcc7SChao Yu 
3155459aa97SJaegeuk Kim 	/* collect a number of dirty meta pages and write together */
31650c8cdb3SJaegeuk Kim 	if (wbc->for_kupdate ||
31750c8cdb3SJaegeuk Kim 		get_pages(sbi, F2FS_DIRTY_META) < nr_pages_to_skip(sbi, META))
318d3baf95dSJaegeuk Kim 		goto skip_write;
319127e670aSJaegeuk Kim 
320a29d0e0bSYunlei He 	/* if locked failed, cp will flush dirty pages instead */
321a29d0e0bSYunlei He 	if (!mutex_trylock(&sbi->cp_mutex))
322a29d0e0bSYunlei He 		goto skip_write;
323d31c7c3fSYunlei He 
324a29d0e0bSYunlei He 	trace_f2fs_writepages(mapping->host, wbc, META);
32550c8cdb3SJaegeuk Kim 	diff = nr_pages_to_write(sbi, META, wbc);
3264d57b86dSChao Yu 	written = f2fs_sync_meta_pages(sbi, META, wbc->nr_to_write, FS_META_IO);
327127e670aSJaegeuk Kim 	mutex_unlock(&sbi->cp_mutex);
32850c8cdb3SJaegeuk Kim 	wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff);
329127e670aSJaegeuk Kim 	return 0;
330d3baf95dSJaegeuk Kim 
331d3baf95dSJaegeuk Kim skip_write:
332d3baf95dSJaegeuk Kim 	wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META);
333d31c7c3fSYunlei He 	trace_f2fs_writepages(mapping->host, wbc, META);
334d3baf95dSJaegeuk Kim 	return 0;
335127e670aSJaegeuk Kim }
336127e670aSJaegeuk Kim 
3374d57b86dSChao Yu long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
338b0af6d49SChao Yu 				long nr_to_write, enum iostat_type io_type)
339127e670aSJaegeuk Kim {
3409df27d98SGu Zheng 	struct address_space *mapping = META_MAPPING(sbi);
341028a63a6SJan Kara 	pgoff_t index = 0, prev = ULONG_MAX;
342127e670aSJaegeuk Kim 	struct pagevec pvec;
343127e670aSJaegeuk Kim 	long nwritten = 0;
344028a63a6SJan Kara 	int nr_pages;
345127e670aSJaegeuk Kim 	struct writeback_control wbc = {
346127e670aSJaegeuk Kim 		.for_reclaim = 0,
347127e670aSJaegeuk Kim 	};
348e9f5b8b8SChao Yu 	struct blk_plug plug;
349127e670aSJaegeuk Kim 
35086679820SMel Gorman 	pagevec_init(&pvec);
351127e670aSJaegeuk Kim 
352e9f5b8b8SChao Yu 	blk_start_plug(&plug);
353e9f5b8b8SChao Yu 
354028a63a6SJan Kara 	while ((nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
35567fd707fSJan Kara 				PAGECACHE_TAG_DIRTY))) {
356028a63a6SJan Kara 		int i;
357127e670aSJaegeuk Kim 
358127e670aSJaegeuk Kim 		for (i = 0; i < nr_pages; i++) {
359127e670aSJaegeuk Kim 			struct page *page = pvec.pages[i];
360203681f6SJaegeuk Kim 
36180dd9c0eSChao Yu 			if (prev == ULONG_MAX)
3626066d8cdSJaegeuk Kim 				prev = page->index - 1;
3636066d8cdSJaegeuk Kim 			if (nr_to_write != LONG_MAX && page->index != prev + 1) {
3646066d8cdSJaegeuk Kim 				pagevec_release(&pvec);
3656066d8cdSJaegeuk Kim 				goto stop;
3666066d8cdSJaegeuk Kim 			}
3676066d8cdSJaegeuk Kim 
368127e670aSJaegeuk Kim 			lock_page(page);
369203681f6SJaegeuk Kim 
370203681f6SJaegeuk Kim 			if (unlikely(page->mapping != mapping)) {
371203681f6SJaegeuk Kim continue_unlock:
372203681f6SJaegeuk Kim 				unlock_page(page);
373203681f6SJaegeuk Kim 				continue;
374203681f6SJaegeuk Kim 			}
375203681f6SJaegeuk Kim 			if (!PageDirty(page)) {
376203681f6SJaegeuk Kim 				/* someone wrote it for us */
377203681f6SJaegeuk Kim 				goto continue_unlock;
378203681f6SJaegeuk Kim 			}
379203681f6SJaegeuk Kim 
380fa3d2bdfSJaegeuk Kim 			f2fs_wait_on_page_writeback(page, META, true);
381fa3d2bdfSJaegeuk Kim 
382fa3d2bdfSJaegeuk Kim 			BUG_ON(PageWriteback(page));
383203681f6SJaegeuk Kim 			if (!clear_page_dirty_for_io(page))
384203681f6SJaegeuk Kim 				goto continue_unlock;
385203681f6SJaegeuk Kim 
386b0af6d49SChao Yu 			if (__f2fs_write_meta_page(page, &wbc, io_type)) {
387577e3495SJaegeuk Kim 				unlock_page(page);
388577e3495SJaegeuk Kim 				break;
389577e3495SJaegeuk Kim 			}
390cfb271d4SChao Yu 			nwritten++;
3916066d8cdSJaegeuk Kim 			prev = page->index;
392cfb271d4SChao Yu 			if (unlikely(nwritten >= nr_to_write))
393127e670aSJaegeuk Kim 				break;
394127e670aSJaegeuk Kim 		}
395127e670aSJaegeuk Kim 		pagevec_release(&pvec);
396127e670aSJaegeuk Kim 		cond_resched();
397127e670aSJaegeuk Kim 	}
3986066d8cdSJaegeuk Kim stop:
399127e670aSJaegeuk Kim 	if (nwritten)
400b9109b0eSJaegeuk Kim 		f2fs_submit_merged_write(sbi, type);
401127e670aSJaegeuk Kim 
402e9f5b8b8SChao Yu 	blk_finish_plug(&plug);
403e9f5b8b8SChao Yu 
404127e670aSJaegeuk Kim 	return nwritten;
405127e670aSJaegeuk Kim }
406127e670aSJaegeuk Kim 
407127e670aSJaegeuk Kim static int f2fs_set_meta_page_dirty(struct page *page)
408127e670aSJaegeuk Kim {
40926c6b887SJaegeuk Kim 	trace_f2fs_set_page_dirty(page, META);
41026c6b887SJaegeuk Kim 
411237c0790SJaegeuk Kim 	if (!PageUptodate(page))
412127e670aSJaegeuk Kim 		SetPageUptodate(page);
413127e670aSJaegeuk Kim 	if (!PageDirty(page)) {
414b87078adSJaegeuk Kim 		__set_page_dirty_nobuffers(page);
4154081363fSJaegeuk Kim 		inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_META);
4161601839eSChao Yu 		SetPagePrivate(page);
4179e4ded3fSJaegeuk Kim 		f2fs_trace_pid(page);
418127e670aSJaegeuk Kim 		return 1;
419127e670aSJaegeuk Kim 	}
420127e670aSJaegeuk Kim 	return 0;
421127e670aSJaegeuk Kim }
422127e670aSJaegeuk Kim 
423127e670aSJaegeuk Kim const struct address_space_operations f2fs_meta_aops = {
424127e670aSJaegeuk Kim 	.writepage	= f2fs_write_meta_page,
425127e670aSJaegeuk Kim 	.writepages	= f2fs_write_meta_pages,
426127e670aSJaegeuk Kim 	.set_page_dirty	= f2fs_set_meta_page_dirty,
427487261f3SChao Yu 	.invalidatepage = f2fs_invalidate_page,
428487261f3SChao Yu 	.releasepage	= f2fs_release_page,
4295b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION
4305b7a487cSWeichao Guo 	.migratepage    = f2fs_migrate_page,
4315b7a487cSWeichao Guo #endif
432127e670aSJaegeuk Kim };
433127e670aSJaegeuk Kim 
43439d787beSChao Yu static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino,
43539d787beSChao Yu 						unsigned int devidx, int type)
436953e6cc6SJaegeuk Kim {
43767298804SChao Yu 	struct inode_management *im = &sbi->im[type];
43880c54505SJaegeuk Kim 	struct ino_entry *e, *tmp;
43980c54505SJaegeuk Kim 
44080c54505SJaegeuk Kim 	tmp = f2fs_kmem_cache_alloc(ino_entry_slab, GFP_NOFS);
44119526d74SChao Yu 
44280c54505SJaegeuk Kim 	radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
443769ec6e5SJaegeuk Kim 
44467298804SChao Yu 	spin_lock(&im->ino_lock);
44567298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
44639efac41SJaegeuk Kim 	if (!e) {
44780c54505SJaegeuk Kim 		e = tmp;
44819526d74SChao Yu 		if (unlikely(radix_tree_insert(&im->ino_root, ino, e)))
44919526d74SChao Yu 			f2fs_bug_on(sbi, 1);
45019526d74SChao Yu 
45139efac41SJaegeuk Kim 		memset(e, 0, sizeof(struct ino_entry));
45239efac41SJaegeuk Kim 		e->ino = ino;
45339efac41SJaegeuk Kim 
45467298804SChao Yu 		list_add_tail(&e->list, &im->ino_list);
4558c402946SJaegeuk Kim 		if (type != ORPHAN_INO)
45667298804SChao Yu 			im->ino_num++;
45739efac41SJaegeuk Kim 	}
45839d787beSChao Yu 
45939d787beSChao Yu 	if (type == FLUSH_INO)
46039d787beSChao Yu 		f2fs_set_bit(devidx, (char *)&e->dirty_device);
46139d787beSChao Yu 
46267298804SChao Yu 	spin_unlock(&im->ino_lock);
463769ec6e5SJaegeuk Kim 	radix_tree_preload_end();
46480c54505SJaegeuk Kim 
46580c54505SJaegeuk Kim 	if (e != tmp)
46680c54505SJaegeuk Kim 		kmem_cache_free(ino_entry_slab, tmp);
467953e6cc6SJaegeuk Kim }
468953e6cc6SJaegeuk Kim 
4696451e041SJaegeuk Kim static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
470953e6cc6SJaegeuk Kim {
47167298804SChao Yu 	struct inode_management *im = &sbi->im[type];
4726451e041SJaegeuk Kim 	struct ino_entry *e;
473953e6cc6SJaegeuk Kim 
47467298804SChao Yu 	spin_lock(&im->ino_lock);
47567298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
47639efac41SJaegeuk Kim 	if (e) {
477953e6cc6SJaegeuk Kim 		list_del(&e->list);
47867298804SChao Yu 		radix_tree_delete(&im->ino_root, ino);
47967298804SChao Yu 		im->ino_num--;
48067298804SChao Yu 		spin_unlock(&im->ino_lock);
4816451e041SJaegeuk Kim 		kmem_cache_free(ino_entry_slab, e);
482953e6cc6SJaegeuk Kim 		return;
483953e6cc6SJaegeuk Kim 	}
48467298804SChao Yu 	spin_unlock(&im->ino_lock);
485953e6cc6SJaegeuk Kim }
486953e6cc6SJaegeuk Kim 
4874d57b86dSChao Yu void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
488fff04f90SJaegeuk Kim {
489fff04f90SJaegeuk Kim 	/* add new dirty ino entry into list */
49039d787beSChao Yu 	__add_ino_entry(sbi, ino, 0, type);
491fff04f90SJaegeuk Kim }
492fff04f90SJaegeuk Kim 
4934d57b86dSChao Yu void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
494fff04f90SJaegeuk Kim {
495fff04f90SJaegeuk Kim 	/* remove dirty ino entry from list */
496fff04f90SJaegeuk Kim 	__remove_ino_entry(sbi, ino, type);
497fff04f90SJaegeuk Kim }
498fff04f90SJaegeuk Kim 
499fff04f90SJaegeuk Kim /* mode should be APPEND_INO or UPDATE_INO */
5004d57b86dSChao Yu bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode)
501fff04f90SJaegeuk Kim {
50267298804SChao Yu 	struct inode_management *im = &sbi->im[mode];
503fff04f90SJaegeuk Kim 	struct ino_entry *e;
50467298804SChao Yu 
50567298804SChao Yu 	spin_lock(&im->ino_lock);
50667298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
50767298804SChao Yu 	spin_unlock(&im->ino_lock);
508fff04f90SJaegeuk Kim 	return e ? true : false;
509fff04f90SJaegeuk Kim }
510fff04f90SJaegeuk Kim 
5114d57b86dSChao Yu void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all)
512fff04f90SJaegeuk Kim {
513fff04f90SJaegeuk Kim 	struct ino_entry *e, *tmp;
514fff04f90SJaegeuk Kim 	int i;
515fff04f90SJaegeuk Kim 
51639d787beSChao Yu 	for (i = all ? ORPHAN_INO : APPEND_INO; i < MAX_INO_ENTRY; i++) {
51767298804SChao Yu 		struct inode_management *im = &sbi->im[i];
51867298804SChao Yu 
51967298804SChao Yu 		spin_lock(&im->ino_lock);
52067298804SChao Yu 		list_for_each_entry_safe(e, tmp, &im->ino_list, list) {
521fff04f90SJaegeuk Kim 			list_del(&e->list);
52267298804SChao Yu 			radix_tree_delete(&im->ino_root, e->ino);
523fff04f90SJaegeuk Kim 			kmem_cache_free(ino_entry_slab, e);
52467298804SChao Yu 			im->ino_num--;
525fff04f90SJaegeuk Kim 		}
52667298804SChao Yu 		spin_unlock(&im->ino_lock);
527fff04f90SJaegeuk Kim 	}
528fff04f90SJaegeuk Kim }
529fff04f90SJaegeuk Kim 
5304d57b86dSChao Yu void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
53139d787beSChao Yu 					unsigned int devidx, int type)
53239d787beSChao Yu {
53339d787beSChao Yu 	__add_ino_entry(sbi, ino, devidx, type);
53439d787beSChao Yu }
53539d787beSChao Yu 
5364d57b86dSChao Yu bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
53739d787beSChao Yu 					unsigned int devidx, int type)
53839d787beSChao Yu {
53939d787beSChao Yu 	struct inode_management *im = &sbi->im[type];
54039d787beSChao Yu 	struct ino_entry *e;
54139d787beSChao Yu 	bool is_dirty = false;
54239d787beSChao Yu 
54339d787beSChao Yu 	spin_lock(&im->ino_lock);
54439d787beSChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
54539d787beSChao Yu 	if (e && f2fs_test_bit(devidx, (char *)&e->dirty_device))
54639d787beSChao Yu 		is_dirty = true;
54739d787beSChao Yu 	spin_unlock(&im->ino_lock);
54839d787beSChao Yu 	return is_dirty;
54939d787beSChao Yu }
55039d787beSChao Yu 
5514d57b86dSChao Yu int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi)
552127e670aSJaegeuk Kim {
55367298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
554127e670aSJaegeuk Kim 	int err = 0;
555127e670aSJaegeuk Kim 
55667298804SChao Yu 	spin_lock(&im->ino_lock);
557cb78942bSJaegeuk Kim 
5581ecc0c5cSChao Yu 	if (time_to_inject(sbi, FAULT_ORPHAN)) {
559cb78942bSJaegeuk Kim 		spin_unlock(&im->ino_lock);
56055523519SChao Yu 		f2fs_show_injection_info(FAULT_ORPHAN);
561cb78942bSJaegeuk Kim 		return -ENOSPC;
562cb78942bSJaegeuk Kim 	}
563*7fa750a1SArnd Bergmann 
56467298804SChao Yu 	if (unlikely(im->ino_num >= sbi->max_orphans))
565127e670aSJaegeuk Kim 		err = -ENOSPC;
566cbd56e7dSJaegeuk Kim 	else
56767298804SChao Yu 		im->ino_num++;
56867298804SChao Yu 	spin_unlock(&im->ino_lock);
5690d47c1adSGu Zheng 
570127e670aSJaegeuk Kim 	return err;
571127e670aSJaegeuk Kim }
572127e670aSJaegeuk Kim 
5734d57b86dSChao Yu void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi)
574cbd56e7dSJaegeuk Kim {
57567298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
57667298804SChao Yu 
57767298804SChao Yu 	spin_lock(&im->ino_lock);
57867298804SChao Yu 	f2fs_bug_on(sbi, im->ino_num == 0);
57967298804SChao Yu 	im->ino_num--;
58067298804SChao Yu 	spin_unlock(&im->ino_lock);
581cbd56e7dSJaegeuk Kim }
582cbd56e7dSJaegeuk Kim 
5834d57b86dSChao Yu void f2fs_add_orphan_inode(struct inode *inode)
584127e670aSJaegeuk Kim {
58539efac41SJaegeuk Kim 	/* add new orphan ino entry into list */
58639d787beSChao Yu 	__add_ino_entry(F2FS_I_SB(inode), inode->i_ino, 0, ORPHAN_INO);
5874d57b86dSChao Yu 	f2fs_update_inode_page(inode);
588127e670aSJaegeuk Kim }
589127e670aSJaegeuk Kim 
5904d57b86dSChao Yu void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
591127e670aSJaegeuk Kim {
592953e6cc6SJaegeuk Kim 	/* remove orphan entry from orphan list */
5936451e041SJaegeuk Kim 	__remove_ino_entry(sbi, ino, ORPHAN_INO);
594127e670aSJaegeuk Kim }
595127e670aSJaegeuk Kim 
5968c14bfadSChao Yu static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
597127e670aSJaegeuk Kim {
5988c14bfadSChao Yu 	struct inode *inode;
5995905f9afSJaegeuk Kim 	struct node_info ni;
60076a45e3cSChao Yu 	int err;
6018c14bfadSChao Yu 
6025905f9afSJaegeuk Kim 	inode = f2fs_iget_retry(sbi->sb, ino);
6038c14bfadSChao Yu 	if (IS_ERR(inode)) {
6048c14bfadSChao Yu 		/*
6058c14bfadSChao Yu 		 * there should be a bug that we can't find the entry
6068c14bfadSChao Yu 		 * to orphan inode.
6078c14bfadSChao Yu 		 */
6088c14bfadSChao Yu 		f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT);
6098c14bfadSChao Yu 		return PTR_ERR(inode);
6108c14bfadSChao Yu 	}
6118c14bfadSChao Yu 
6120f9ec2a8SJaegeuk Kim 	err = dquot_initialize(inode);
613a515d12fSSheng Yong 	if (err) {
614a515d12fSSheng Yong 		iput(inode);
6150f9ec2a8SJaegeuk Kim 		goto err_out;
616a515d12fSSheng Yong 	}
6170f9ec2a8SJaegeuk Kim 
618127e670aSJaegeuk Kim 	clear_nlink(inode);
619127e670aSJaegeuk Kim 
620127e670aSJaegeuk Kim 	/* truncate all the data during iput */
621127e670aSJaegeuk Kim 	iput(inode);
6225905f9afSJaegeuk Kim 
6237735730dSChao Yu 	err = f2fs_get_node_info(sbi, ino, &ni);
6247735730dSChao Yu 	if (err)
6257735730dSChao Yu 		goto err_out;
6265905f9afSJaegeuk Kim 
6275905f9afSJaegeuk Kim 	/* ENOMEM was fully retried in f2fs_evict_inode. */
6285905f9afSJaegeuk Kim 	if (ni.blk_addr != NULL_ADDR) {
6290f9ec2a8SJaegeuk Kim 		err = -EIO;
6300f9ec2a8SJaegeuk Kim 		goto err_out;
6315905f9afSJaegeuk Kim 	}
6328c14bfadSChao Yu 	return 0;
6330f9ec2a8SJaegeuk Kim 
6340f9ec2a8SJaegeuk Kim err_out:
6350f9ec2a8SJaegeuk Kim 	set_sbi_flag(sbi, SBI_NEED_FSCK);
6360f9ec2a8SJaegeuk Kim 	f2fs_msg(sbi->sb, KERN_WARNING,
6370f9ec2a8SJaegeuk Kim 			"%s: orphan failed (ino=%x), run fsck to fix.",
6380f9ec2a8SJaegeuk Kim 			__func__, ino);
6390f9ec2a8SJaegeuk Kim 	return err;
640127e670aSJaegeuk Kim }
641127e670aSJaegeuk Kim 
6424d57b86dSChao Yu int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
643127e670aSJaegeuk Kim {
6443c642985SWanpeng Li 	block_t start_blk, orphan_blocks, i, j;
6454b2414d0SChao Yu 	unsigned int s_flags = sbi->sb->s_flags;
6464b2414d0SChao Yu 	int err = 0;
647ea676733SJaegeuk Kim #ifdef CONFIG_QUOTA
648ea676733SJaegeuk Kim 	int quota_enabled;
649ea676733SJaegeuk Kim #endif
650127e670aSJaegeuk Kim 
651aaec2b1dSChao Yu 	if (!is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG))
6528c14bfadSChao Yu 		return 0;
653127e670aSJaegeuk Kim 
6541751e8a6SLinus Torvalds 	if (s_flags & SB_RDONLY) {
6554b2414d0SChao Yu 		f2fs_msg(sbi->sb, KERN_INFO, "orphan cleanup on readonly fs");
6561751e8a6SLinus Torvalds 		sbi->sb->s_flags &= ~SB_RDONLY;
6574b2414d0SChao Yu 	}
6584b2414d0SChao Yu 
6594b2414d0SChao Yu #ifdef CONFIG_QUOTA
6604b2414d0SChao Yu 	/* Needed for iput() to work correctly and not trash data */
6611751e8a6SLinus Torvalds 	sbi->sb->s_flags |= SB_ACTIVE;
662ea676733SJaegeuk Kim 
66376cf05d7SSheng Yong 	/*
66476cf05d7SSheng Yong 	 * Turn on quotas which were not enabled for read-only mounts if
66576cf05d7SSheng Yong 	 * filesystem has quota feature, so that they are updated correctly.
66676cf05d7SSheng Yong 	 */
6671751e8a6SLinus Torvalds 	quota_enabled = f2fs_enable_quota_files(sbi, s_flags & SB_RDONLY);
6684b2414d0SChao Yu #endif
6694b2414d0SChao Yu 
67055141486SWanpeng Li 	start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
6713c642985SWanpeng Li 	orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
672127e670aSJaegeuk Kim 
6734d57b86dSChao Yu 	f2fs_ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true);
674662befdaSChao Yu 
6753c642985SWanpeng Li 	for (i = 0; i < orphan_blocks; i++) {
6767735730dSChao Yu 		struct page *page;
677127e670aSJaegeuk Kim 		struct f2fs_orphan_block *orphan_blk;
678127e670aSJaegeuk Kim 
6797735730dSChao Yu 		page = f2fs_get_meta_page(sbi, start_blk + i);
6807735730dSChao Yu 		if (IS_ERR(page)) {
6817735730dSChao Yu 			err = PTR_ERR(page);
6827735730dSChao Yu 			goto out;
6837735730dSChao Yu 		}
6847735730dSChao Yu 
685127e670aSJaegeuk Kim 		orphan_blk = (struct f2fs_orphan_block *)page_address(page);
686127e670aSJaegeuk Kim 		for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
687127e670aSJaegeuk Kim 			nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
6888c14bfadSChao Yu 			err = recover_orphan_inode(sbi, ino);
6898c14bfadSChao Yu 			if (err) {
6908c14bfadSChao Yu 				f2fs_put_page(page, 1);
6914b2414d0SChao Yu 				goto out;
6928c14bfadSChao Yu 			}
693127e670aSJaegeuk Kim 		}
694127e670aSJaegeuk Kim 		f2fs_put_page(page, 1);
695127e670aSJaegeuk Kim 	}
696127e670aSJaegeuk Kim 	/* clear Orphan Flag */
697aaec2b1dSChao Yu 	clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
6984b2414d0SChao Yu out:
6994b2414d0SChao Yu #ifdef CONFIG_QUOTA
7004b2414d0SChao Yu 	/* Turn quotas off */
701ea676733SJaegeuk Kim 	if (quota_enabled)
7024b2414d0SChao Yu 		f2fs_quota_off_umount(sbi->sb);
7034b2414d0SChao Yu #endif
7041751e8a6SLinus Torvalds 	sbi->sb->s_flags = s_flags; /* Restore SB_RDONLY status */
7054b2414d0SChao Yu 
7064b2414d0SChao Yu 	return err;
707127e670aSJaegeuk Kim }
708127e670aSJaegeuk Kim 
709127e670aSJaegeuk Kim static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
710127e670aSJaegeuk Kim {
711502c6e0bSGu Zheng 	struct list_head *head;
712127e670aSJaegeuk Kim 	struct f2fs_orphan_block *orphan_blk = NULL;
713127e670aSJaegeuk Kim 	unsigned int nentries = 0;
714bd936f84SChao Yu 	unsigned short index = 1;
7158c402946SJaegeuk Kim 	unsigned short orphan_blocks;
7164531929eSGu Zheng 	struct page *page = NULL;
7176451e041SJaegeuk Kim 	struct ino_entry *orphan = NULL;
71867298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
719127e670aSJaegeuk Kim 
72067298804SChao Yu 	orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num);
7218c402946SJaegeuk Kim 
722d6c67a4fSJaegeuk Kim 	/*
723d6c67a4fSJaegeuk Kim 	 * we don't need to do spin_lock(&im->ino_lock) here, since all the
724d6c67a4fSJaegeuk Kim 	 * orphan inode operations are covered under f2fs_lock_op().
725d6c67a4fSJaegeuk Kim 	 * And, spin_lock should be avoided due to page operations below.
726d6c67a4fSJaegeuk Kim 	 */
72767298804SChao Yu 	head = &im->ino_list;
728127e670aSJaegeuk Kim 
729127e670aSJaegeuk Kim 	/* loop for each orphan inode entry and write them in Jornal block */
730502c6e0bSGu Zheng 	list_for_each_entry(orphan, head, list) {
731502c6e0bSGu Zheng 		if (!page) {
7324d57b86dSChao Yu 			page = f2fs_grab_meta_page(sbi, start_blk++);
733502c6e0bSGu Zheng 			orphan_blk =
734502c6e0bSGu Zheng 				(struct f2fs_orphan_block *)page_address(page);
735502c6e0bSGu Zheng 			memset(orphan_blk, 0, sizeof(*orphan_blk));
736502c6e0bSGu Zheng 		}
737127e670aSJaegeuk Kim 
73836795567SGu Zheng 		orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
739127e670aSJaegeuk Kim 
74036795567SGu Zheng 		if (nentries == F2FS_ORPHANS_PER_BLOCK) {
741127e670aSJaegeuk Kim 			/*
742127e670aSJaegeuk Kim 			 * an orphan block is full of 1020 entries,
743127e670aSJaegeuk Kim 			 * then we need to flush current orphan blocks
744127e670aSJaegeuk Kim 			 * and bring another one in memory
745127e670aSJaegeuk Kim 			 */
746127e670aSJaegeuk Kim 			orphan_blk->blk_addr = cpu_to_le16(index);
747127e670aSJaegeuk Kim 			orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
748127e670aSJaegeuk Kim 			orphan_blk->entry_count = cpu_to_le32(nentries);
749127e670aSJaegeuk Kim 			set_page_dirty(page);
750127e670aSJaegeuk Kim 			f2fs_put_page(page, 1);
751127e670aSJaegeuk Kim 			index++;
752127e670aSJaegeuk Kim 			nentries = 0;
753127e670aSJaegeuk Kim 			page = NULL;
754127e670aSJaegeuk Kim 		}
755127e670aSJaegeuk Kim 	}
756127e670aSJaegeuk Kim 
757502c6e0bSGu Zheng 	if (page) {
758127e670aSJaegeuk Kim 		orphan_blk->blk_addr = cpu_to_le16(index);
759127e670aSJaegeuk Kim 		orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
760127e670aSJaegeuk Kim 		orphan_blk->entry_count = cpu_to_le32(nentries);
761127e670aSJaegeuk Kim 		set_page_dirty(page);
762127e670aSJaegeuk Kim 		f2fs_put_page(page, 1);
763502c6e0bSGu Zheng 	}
764127e670aSJaegeuk Kim }
765127e670aSJaegeuk Kim 
766fc0065adSTiezhu Yang static int get_checkpoint_version(struct f2fs_sb_info *sbi, block_t cp_addr,
767fc0065adSTiezhu Yang 		struct f2fs_checkpoint **cp_block, struct page **cp_page,
768fc0065adSTiezhu Yang 		unsigned long long *version)
769fc0065adSTiezhu Yang {
770fc0065adSTiezhu Yang 	unsigned long blk_size = sbi->blocksize;
771fc0065adSTiezhu Yang 	size_t crc_offset = 0;
772fc0065adSTiezhu Yang 	__u32 crc = 0;
773fc0065adSTiezhu Yang 
7744d57b86dSChao Yu 	*cp_page = f2fs_get_meta_page(sbi, cp_addr);
7757735730dSChao Yu 	if (IS_ERR(*cp_page))
7767735730dSChao Yu 		return PTR_ERR(*cp_page);
7777735730dSChao Yu 
778fc0065adSTiezhu Yang 	*cp_block = (struct f2fs_checkpoint *)page_address(*cp_page);
779fc0065adSTiezhu Yang 
780fc0065adSTiezhu Yang 	crc_offset = le32_to_cpu((*cp_block)->checksum_offset);
781c6f89dfdSKinglong Mee 	if (crc_offset > (blk_size - sizeof(__le32))) {
782d3f07c04SChao Yu 		f2fs_put_page(*cp_page, 1);
783fc0065adSTiezhu Yang 		f2fs_msg(sbi->sb, KERN_WARNING,
784fc0065adSTiezhu Yang 			"invalid crc_offset: %zu", crc_offset);
785fc0065adSTiezhu Yang 		return -EINVAL;
786fc0065adSTiezhu Yang 	}
787fc0065adSTiezhu Yang 
788ced2c7eaSKinglong Mee 	crc = cur_cp_crc(*cp_block);
789fc0065adSTiezhu Yang 	if (!f2fs_crc_valid(sbi, crc, *cp_block, crc_offset)) {
790d3f07c04SChao Yu 		f2fs_put_page(*cp_page, 1);
791fc0065adSTiezhu Yang 		f2fs_msg(sbi->sb, KERN_WARNING, "invalid crc value");
792fc0065adSTiezhu Yang 		return -EINVAL;
793fc0065adSTiezhu Yang 	}
794fc0065adSTiezhu Yang 
795fc0065adSTiezhu Yang 	*version = cur_cp_version(*cp_block);
796fc0065adSTiezhu Yang 	return 0;
797fc0065adSTiezhu Yang }
798fc0065adSTiezhu Yang 
799127e670aSJaegeuk Kim static struct page *validate_checkpoint(struct f2fs_sb_info *sbi,
800127e670aSJaegeuk Kim 				block_t cp_addr, unsigned long long *version)
801127e670aSJaegeuk Kim {
802fc0065adSTiezhu Yang 	struct page *cp_page_1 = NULL, *cp_page_2 = NULL;
803fc0065adSTiezhu Yang 	struct f2fs_checkpoint *cp_block = NULL;
804127e670aSJaegeuk Kim 	unsigned long long cur_version = 0, pre_version = 0;
805fc0065adSTiezhu Yang 	int err;
806127e670aSJaegeuk Kim 
807fc0065adSTiezhu Yang 	err = get_checkpoint_version(sbi, cp_addr, &cp_block,
808fc0065adSTiezhu Yang 					&cp_page_1, version);
809fc0065adSTiezhu Yang 	if (err)
810d3f07c04SChao Yu 		return NULL;
811c9b60788SChao Yu 
812c9b60788SChao Yu 	if (le32_to_cpu(cp_block->cp_pack_total_block_count) >
813c9b60788SChao Yu 					sbi->blocks_per_seg) {
814c9b60788SChao Yu 		f2fs_msg(sbi->sb, KERN_WARNING,
815c9b60788SChao Yu 			"invalid cp_pack_total_block_count:%u",
816c9b60788SChao Yu 			le32_to_cpu(cp_block->cp_pack_total_block_count));
817d3f07c04SChao Yu 		goto invalid_cp;
818c9b60788SChao Yu 	}
819fc0065adSTiezhu Yang 	pre_version = *version;
820127e670aSJaegeuk Kim 
82125ca923bSJaegeuk Kim 	cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1;
822fc0065adSTiezhu Yang 	err = get_checkpoint_version(sbi, cp_addr, &cp_block,
823fc0065adSTiezhu Yang 					&cp_page_2, version);
824fc0065adSTiezhu Yang 	if (err)
825d3f07c04SChao Yu 		goto invalid_cp;
826fc0065adSTiezhu Yang 	cur_version = *version;
827127e670aSJaegeuk Kim 
828127e670aSJaegeuk Kim 	if (cur_version == pre_version) {
829127e670aSJaegeuk Kim 		*version = cur_version;
830127e670aSJaegeuk Kim 		f2fs_put_page(cp_page_2, 1);
831127e670aSJaegeuk Kim 		return cp_page_1;
832127e670aSJaegeuk Kim 	}
833127e670aSJaegeuk Kim 	f2fs_put_page(cp_page_2, 1);
834d3f07c04SChao Yu invalid_cp:
835127e670aSJaegeuk Kim 	f2fs_put_page(cp_page_1, 1);
836127e670aSJaegeuk Kim 	return NULL;
837127e670aSJaegeuk Kim }
838127e670aSJaegeuk Kim 
8394d57b86dSChao Yu int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi)
840127e670aSJaegeuk Kim {
841127e670aSJaegeuk Kim 	struct f2fs_checkpoint *cp_block;
842127e670aSJaegeuk Kim 	struct f2fs_super_block *fsb = sbi->raw_super;
843127e670aSJaegeuk Kim 	struct page *cp1, *cp2, *cur_page;
844127e670aSJaegeuk Kim 	unsigned long blk_size = sbi->blocksize;
845127e670aSJaegeuk Kim 	unsigned long long cp1_version = 0, cp2_version = 0;
846127e670aSJaegeuk Kim 	unsigned long long cp_start_blk_no;
84755141486SWanpeng Li 	unsigned int cp_blks = 1 + __cp_payload(sbi);
8481dbe4152SChangman Lee 	block_t cp_blk_no;
8491dbe4152SChangman Lee 	int i;
850127e670aSJaegeuk Kim 
851026f0507SKees Cook 	sbi->ckpt = f2fs_kzalloc(sbi, array_size(blk_size, cp_blks),
852026f0507SKees Cook 				 GFP_KERNEL);
853127e670aSJaegeuk Kim 	if (!sbi->ckpt)
854127e670aSJaegeuk Kim 		return -ENOMEM;
855127e670aSJaegeuk Kim 	/*
856127e670aSJaegeuk Kim 	 * Finding out valid cp block involves read both
857127e670aSJaegeuk Kim 	 * sets( cp pack1 and cp pack 2)
858127e670aSJaegeuk Kim 	 */
859127e670aSJaegeuk Kim 	cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr);
860127e670aSJaegeuk Kim 	cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
861127e670aSJaegeuk Kim 
862127e670aSJaegeuk Kim 	/* The second checkpoint pack should start at the next segment */
863f9a4e6dfSJaegeuk Kim 	cp_start_blk_no += ((unsigned long long)1) <<
864f9a4e6dfSJaegeuk Kim 				le32_to_cpu(fsb->log_blocks_per_seg);
865127e670aSJaegeuk Kim 	cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
866127e670aSJaegeuk Kim 
867127e670aSJaegeuk Kim 	if (cp1 && cp2) {
868127e670aSJaegeuk Kim 		if (ver_after(cp2_version, cp1_version))
869127e670aSJaegeuk Kim 			cur_page = cp2;
870127e670aSJaegeuk Kim 		else
871127e670aSJaegeuk Kim 			cur_page = cp1;
872127e670aSJaegeuk Kim 	} else if (cp1) {
873127e670aSJaegeuk Kim 		cur_page = cp1;
874127e670aSJaegeuk Kim 	} else if (cp2) {
875127e670aSJaegeuk Kim 		cur_page = cp2;
876127e670aSJaegeuk Kim 	} else {
877127e670aSJaegeuk Kim 		goto fail_no_cp;
878127e670aSJaegeuk Kim 	}
879127e670aSJaegeuk Kim 
880127e670aSJaegeuk Kim 	cp_block = (struct f2fs_checkpoint *)page_address(cur_page);
881127e670aSJaegeuk Kim 	memcpy(sbi->ckpt, cp_block, blk_size);
882127e670aSJaegeuk Kim 
8838508e44aSJaegeuk Kim 	if (cur_page == cp1)
8848508e44aSJaegeuk Kim 		sbi->cur_cp_pack = 1;
8858508e44aSJaegeuk Kim 	else
8868508e44aSJaegeuk Kim 		sbi->cur_cp_pack = 2;
887984ec63cSShawn Lin 
888e494c2f9SChao Yu 	/* Sanity checking of checkpoint */
889e494c2f9SChao Yu 	if (f2fs_sanity_check_ckpt(sbi))
890e494c2f9SChao Yu 		goto free_fail_no_cp;
891e494c2f9SChao Yu 
8921dbe4152SChangman Lee 	if (cp_blks <= 1)
8931dbe4152SChangman Lee 		goto done;
8941dbe4152SChangman Lee 
8951dbe4152SChangman Lee 	cp_blk_no = le32_to_cpu(fsb->cp_blkaddr);
8961dbe4152SChangman Lee 	if (cur_page == cp2)
8971dbe4152SChangman Lee 		cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg);
8981dbe4152SChangman Lee 
8991dbe4152SChangman Lee 	for (i = 1; i < cp_blks; i++) {
9001dbe4152SChangman Lee 		void *sit_bitmap_ptr;
9011dbe4152SChangman Lee 		unsigned char *ckpt = (unsigned char *)sbi->ckpt;
9021dbe4152SChangman Lee 
9034d57b86dSChao Yu 		cur_page = f2fs_get_meta_page(sbi, cp_blk_no + i);
9047735730dSChao Yu 		if (IS_ERR(cur_page))
9057735730dSChao Yu 			goto free_fail_no_cp;
9061dbe4152SChangman Lee 		sit_bitmap_ptr = page_address(cur_page);
9071dbe4152SChangman Lee 		memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size);
9081dbe4152SChangman Lee 		f2fs_put_page(cur_page, 1);
9091dbe4152SChangman Lee 	}
9101dbe4152SChangman Lee done:
911127e670aSJaegeuk Kim 	f2fs_put_page(cp1, 1);
912127e670aSJaegeuk Kim 	f2fs_put_page(cp2, 1);
913127e670aSJaegeuk Kim 	return 0;
914127e670aSJaegeuk Kim 
915a2125ff7SJaegeuk Kim free_fail_no_cp:
916a2125ff7SJaegeuk Kim 	f2fs_put_page(cp1, 1);
917a2125ff7SJaegeuk Kim 	f2fs_put_page(cp2, 1);
918127e670aSJaegeuk Kim fail_no_cp:
919127e670aSJaegeuk Kim 	kfree(sbi->ckpt);
920127e670aSJaegeuk Kim 	return -EINVAL;
921127e670aSJaegeuk Kim }
922127e670aSJaegeuk Kim 
923c227f912SChao Yu static void __add_dirty_inode(struct inode *inode, enum inode_type type)
924127e670aSJaegeuk Kim {
9254081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
926c227f912SChao Yu 	int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
9272d7b822aSChao Yu 
92891942321SJaegeuk Kim 	if (is_inode_flag_set(inode, flag))
9292710fd7eSChao Yu 		return;
9302d7b822aSChao Yu 
93191942321SJaegeuk Kim 	set_inode_flag(inode, flag);
93299f4b917SChao Yu 	if (!f2fs_is_volatile_file(inode))
93399f4b917SChao Yu 		list_add_tail(&F2FS_I(inode)->dirty_list,
93499f4b917SChao Yu 						&sbi->inode_list[type]);
93533fbd510SChao Yu 	stat_inc_dirty_inode(sbi, type);
9365deb8267SJaegeuk Kim }
9375deb8267SJaegeuk Kim 
938c227f912SChao Yu static void __remove_dirty_inode(struct inode *inode, enum inode_type type)
9396ad7609aSChao Yu {
940c227f912SChao Yu 	int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
9416ad7609aSChao Yu 
94291942321SJaegeuk Kim 	if (get_dirty_pages(inode) || !is_inode_flag_set(inode, flag))
9436ad7609aSChao Yu 		return;
9446ad7609aSChao Yu 
94591942321SJaegeuk Kim 	list_del_init(&F2FS_I(inode)->dirty_list);
94691942321SJaegeuk Kim 	clear_inode_flag(inode, flag);
94733fbd510SChao Yu 	stat_dec_dirty_inode(F2FS_I_SB(inode), type);
9486ad7609aSChao Yu }
9496ad7609aSChao Yu 
9504d57b86dSChao Yu void f2fs_update_dirty_page(struct inode *inode, struct page *page)
9515deb8267SJaegeuk Kim {
9524081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
953c227f912SChao Yu 	enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
9545deb8267SJaegeuk Kim 
9555ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
9565ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
957127e670aSJaegeuk Kim 		return;
9587bd59381SGu Zheng 
959c227f912SChao Yu 	spin_lock(&sbi->inode_lock[type]);
9601c4bf763SJaegeuk Kim 	if (type != FILE_INODE || test_opt(sbi, DATA_FLUSH))
961c227f912SChao Yu 		__add_dirty_inode(inode, type);
962b951a4ecSYunlei He 	inode_inc_dirty_pages(inode);
9631c4bf763SJaegeuk Kim 	spin_unlock(&sbi->inode_lock[type]);
9641c4bf763SJaegeuk Kim 
965a7ffdbe2SJaegeuk Kim 	SetPagePrivate(page);
9669e4ded3fSJaegeuk Kim 	f2fs_trace_pid(page);
9675deb8267SJaegeuk Kim }
968127e670aSJaegeuk Kim 
9694d57b86dSChao Yu void f2fs_remove_dirty_inode(struct inode *inode)
970127e670aSJaegeuk Kim {
9714081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
972c227f912SChao Yu 	enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
973127e670aSJaegeuk Kim 
974c227f912SChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
975c227f912SChao Yu 			!S_ISLNK(inode->i_mode))
976127e670aSJaegeuk Kim 		return;
977127e670aSJaegeuk Kim 
97810aa97c3SJaegeuk Kim 	if (type == FILE_INODE && !test_opt(sbi, DATA_FLUSH))
97910aa97c3SJaegeuk Kim 		return;
98010aa97c3SJaegeuk Kim 
981c227f912SChao Yu 	spin_lock(&sbi->inode_lock[type]);
982c227f912SChao Yu 	__remove_dirty_inode(inode, type);
983c227f912SChao Yu 	spin_unlock(&sbi->inode_lock[type]);
984afc3eda2SJaegeuk Kim }
98574d0b917SJaegeuk Kim 
9864d57b86dSChao Yu int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type)
987127e670aSJaegeuk Kim {
988ce3b7d80SGu Zheng 	struct list_head *head;
989127e670aSJaegeuk Kim 	struct inode *inode;
9902710fd7eSChao Yu 	struct f2fs_inode_info *fi;
9914cf18537SChao Yu 	bool is_dir = (type == DIR_INODE);
9924db08d01SJaegeuk Kim 	unsigned long ino = 0;
9934cf18537SChao Yu 
9944cf18537SChao Yu 	trace_f2fs_sync_dirty_inodes_enter(sbi->sb, is_dir,
9954cf18537SChao Yu 				get_pages(sbi, is_dir ?
9964cf18537SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
997127e670aSJaegeuk Kim retry:
998af41d3eeSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
9996d5a1495SChao Yu 		return -EIO;
1000af41d3eeSJaegeuk Kim 
1001c227f912SChao Yu 	spin_lock(&sbi->inode_lock[type]);
1002ce3b7d80SGu Zheng 
1003c227f912SChao Yu 	head = &sbi->inode_list[type];
1004127e670aSJaegeuk Kim 	if (list_empty(head)) {
1005c227f912SChao Yu 		spin_unlock(&sbi->inode_lock[type]);
10064cf18537SChao Yu 		trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir,
10074cf18537SChao Yu 				get_pages(sbi, is_dir ?
10084cf18537SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
10096d5a1495SChao Yu 		return 0;
1010127e670aSJaegeuk Kim 	}
1011939afa94SChao Yu 	fi = list_first_entry(head, struct f2fs_inode_info, dirty_list);
10122710fd7eSChao Yu 	inode = igrab(&fi->vfs_inode);
1013c227f912SChao Yu 	spin_unlock(&sbi->inode_lock[type]);
1014127e670aSJaegeuk Kim 	if (inode) {
10154db08d01SJaegeuk Kim 		unsigned long cur_ino = inode->i_ino;
10164db08d01SJaegeuk Kim 
1017b0af6d49SChao Yu 		if (is_dir)
1018b0af6d49SChao Yu 			F2FS_I(inode)->cp_task = current;
1019b0af6d49SChao Yu 
102087d6f890SJaegeuk Kim 		filemap_fdatawrite(inode->i_mapping);
1021b0af6d49SChao Yu 
1022b0af6d49SChao Yu 		if (is_dir)
1023b0af6d49SChao Yu 			F2FS_I(inode)->cp_task = NULL;
1024b0af6d49SChao Yu 
1025127e670aSJaegeuk Kim 		iput(inode);
10264db08d01SJaegeuk Kim 		/* We need to give cpu to another writers. */
10272a63531aSYunlei He 		if (ino == cur_ino)
10284db08d01SJaegeuk Kim 			cond_resched();
10292a63531aSYunlei He 		else
10304db08d01SJaegeuk Kim 			ino = cur_ino;
1031127e670aSJaegeuk Kim 	} else {
1032127e670aSJaegeuk Kim 		/*
1033127e670aSJaegeuk Kim 		 * We should submit bio, since it exists several
1034127e670aSJaegeuk Kim 		 * wribacking dentry pages in the freeing inode.
1035127e670aSJaegeuk Kim 		 */
1036b9109b0eSJaegeuk Kim 		f2fs_submit_merged_write(sbi, DATA);
10377ecebe5eSSebastian Andrzej Siewior 		cond_resched();
1038127e670aSJaegeuk Kim 	}
1039127e670aSJaegeuk Kim 	goto retry;
1040127e670aSJaegeuk Kim }
1041127e670aSJaegeuk Kim 
10420f18b462SJaegeuk Kim int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi)
10430f18b462SJaegeuk Kim {
10440f18b462SJaegeuk Kim 	struct list_head *head = &sbi->inode_list[DIRTY_META];
10450f18b462SJaegeuk Kim 	struct inode *inode;
10460f18b462SJaegeuk Kim 	struct f2fs_inode_info *fi;
10470f18b462SJaegeuk Kim 	s64 total = get_pages(sbi, F2FS_DIRTY_IMETA);
10480f18b462SJaegeuk Kim 
10490f18b462SJaegeuk Kim 	while (total--) {
10500f18b462SJaegeuk Kim 		if (unlikely(f2fs_cp_error(sbi)))
10510f18b462SJaegeuk Kim 			return -EIO;
10520f18b462SJaegeuk Kim 
10530f18b462SJaegeuk Kim 		spin_lock(&sbi->inode_lock[DIRTY_META]);
10540f18b462SJaegeuk Kim 		if (list_empty(head)) {
10550f18b462SJaegeuk Kim 			spin_unlock(&sbi->inode_lock[DIRTY_META]);
10560f18b462SJaegeuk Kim 			return 0;
10570f18b462SJaegeuk Kim 		}
1058939afa94SChao Yu 		fi = list_first_entry(head, struct f2fs_inode_info,
10590f18b462SJaegeuk Kim 							gdirty_list);
10600f18b462SJaegeuk Kim 		inode = igrab(&fi->vfs_inode);
10610f18b462SJaegeuk Kim 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
10620f18b462SJaegeuk Kim 		if (inode) {
106318340edcSJaegeuk Kim 			sync_inode_metadata(inode, 0);
106418340edcSJaegeuk Kim 
106518340edcSJaegeuk Kim 			/* it's on eviction */
106618340edcSJaegeuk Kim 			if (is_inode_flag_set(inode, FI_DIRTY_INODE))
10674d57b86dSChao Yu 				f2fs_update_inode_page(inode);
10680f18b462SJaegeuk Kim 			iput(inode);
10690f18b462SJaegeuk Kim 		}
1070dee668c1SChao Yu 	}
10710f18b462SJaegeuk Kim 	return 0;
10720f18b462SJaegeuk Kim }
10730f18b462SJaegeuk Kim 
107459c9081bSYunlei He static void __prepare_cp_block(struct f2fs_sb_info *sbi)
107559c9081bSYunlei He {
107659c9081bSYunlei He 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
107759c9081bSYunlei He 	struct f2fs_nm_info *nm_i = NM_I(sbi);
107859c9081bSYunlei He 	nid_t last_nid = nm_i->next_scan_nid;
107959c9081bSYunlei He 
108059c9081bSYunlei He 	next_free_nid(sbi, &last_nid);
108159c9081bSYunlei He 	ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
108259c9081bSYunlei He 	ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi));
108359c9081bSYunlei He 	ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi));
108459c9081bSYunlei He 	ckpt->next_free_nid = cpu_to_le32(last_nid);
108559c9081bSYunlei He }
108659c9081bSYunlei He 
10870a8165d7SJaegeuk Kim /*
1088127e670aSJaegeuk Kim  * Freeze all the FS-operations for checkpoint.
1089127e670aSJaegeuk Kim  */
1090cf779cabSJaegeuk Kim static int block_operations(struct f2fs_sb_info *sbi)
1091127e670aSJaegeuk Kim {
1092127e670aSJaegeuk Kim 	struct writeback_control wbc = {
1093127e670aSJaegeuk Kim 		.sync_mode = WB_SYNC_ALL,
1094127e670aSJaegeuk Kim 		.nr_to_write = LONG_MAX,
1095127e670aSJaegeuk Kim 		.for_reclaim = 0,
1096127e670aSJaegeuk Kim 	};
1097c718379bSJaegeuk Kim 	struct blk_plug plug;
1098cf779cabSJaegeuk Kim 	int err = 0;
1099c718379bSJaegeuk Kim 
1100c718379bSJaegeuk Kim 	blk_start_plug(&plug);
1101c718379bSJaegeuk Kim 
110239936837SJaegeuk Kim retry_flush_dents:
1103e479556bSGu Zheng 	f2fs_lock_all(sbi);
1104127e670aSJaegeuk Kim 	/* write all the dirty dentry pages */
1105127e670aSJaegeuk Kim 	if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
1106e479556bSGu Zheng 		f2fs_unlock_all(sbi);
11074d57b86dSChao Yu 		err = f2fs_sync_dirty_inodes(sbi, DIR_INODE);
11086d5a1495SChao Yu 		if (err)
1109cf779cabSJaegeuk Kim 			goto out;
111030973883SJaegeuk Kim 		cond_resched();
111139936837SJaegeuk Kim 		goto retry_flush_dents;
1112127e670aSJaegeuk Kim 	}
1113127e670aSJaegeuk Kim 
111459c9081bSYunlei He 	/*
111559c9081bSYunlei He 	 * POR: we should ensure that there are no dirty node pages
111659c9081bSYunlei He 	 * until finishing nat/sit flush. inode->i_blocks can be updated.
111759c9081bSYunlei He 	 */
111859c9081bSYunlei He 	down_write(&sbi->node_change);
111959c9081bSYunlei He 
11200f18b462SJaegeuk Kim 	if (get_pages(sbi, F2FS_DIRTY_IMETA)) {
112159c9081bSYunlei He 		up_write(&sbi->node_change);
11220f18b462SJaegeuk Kim 		f2fs_unlock_all(sbi);
11230f18b462SJaegeuk Kim 		err = f2fs_sync_inode_meta(sbi);
11240f18b462SJaegeuk Kim 		if (err)
11250f18b462SJaegeuk Kim 			goto out;
112630973883SJaegeuk Kim 		cond_resched();
11270f18b462SJaegeuk Kim 		goto retry_flush_dents;
11280f18b462SJaegeuk Kim 	}
11290f18b462SJaegeuk Kim 
113039936837SJaegeuk Kim retry_flush_nodes:
1131b3582c68SChao Yu 	down_write(&sbi->node_write);
1132127e670aSJaegeuk Kim 
1133127e670aSJaegeuk Kim 	if (get_pages(sbi, F2FS_DIRTY_NODES)) {
1134b3582c68SChao Yu 		up_write(&sbi->node_write);
1135c29fd0c0SChao Yu 		atomic_inc(&sbi->wb_sync_req[NODE]);
11364d57b86dSChao Yu 		err = f2fs_sync_node_pages(sbi, &wbc, false, FS_CP_NODE_IO);
1137c29fd0c0SChao Yu 		atomic_dec(&sbi->wb_sync_req[NODE]);
11386d5a1495SChao Yu 		if (err) {
113959c9081bSYunlei He 			up_write(&sbi->node_change);
1140cf779cabSJaegeuk Kim 			f2fs_unlock_all(sbi);
1141cf779cabSJaegeuk Kim 			goto out;
1142cf779cabSJaegeuk Kim 		}
114330973883SJaegeuk Kim 		cond_resched();
114439936837SJaegeuk Kim 		goto retry_flush_nodes;
1145127e670aSJaegeuk Kim 	}
114659c9081bSYunlei He 
114759c9081bSYunlei He 	/*
114859c9081bSYunlei He 	 * sbi->node_change is used only for AIO write_begin path which produces
114959c9081bSYunlei He 	 * dirty node blocks and some checkpoint values by block allocation.
115059c9081bSYunlei He 	 */
115159c9081bSYunlei He 	__prepare_cp_block(sbi);
115259c9081bSYunlei He 	up_write(&sbi->node_change);
1153cf779cabSJaegeuk Kim out:
1154c718379bSJaegeuk Kim 	blk_finish_plug(&plug);
1155cf779cabSJaegeuk Kim 	return err;
1156127e670aSJaegeuk Kim }
1157127e670aSJaegeuk Kim 
1158127e670aSJaegeuk Kim static void unblock_operations(struct f2fs_sb_info *sbi)
1159127e670aSJaegeuk Kim {
1160b3582c68SChao Yu 	up_write(&sbi->node_write);
1161e479556bSGu Zheng 	f2fs_unlock_all(sbi);
1162127e670aSJaegeuk Kim }
1163127e670aSJaegeuk Kim 
116450fa53ecSChao Yu void f2fs_wait_on_all_pages_writeback(struct f2fs_sb_info *sbi)
1165fb51b5efSChangman Lee {
1166fb51b5efSChangman Lee 	DEFINE_WAIT(wait);
1167fb51b5efSChangman Lee 
1168fb51b5efSChangman Lee 	for (;;) {
1169fb51b5efSChangman Lee 		prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
1170fb51b5efSChangman Lee 
117136951b38SChao Yu 		if (!get_pages(sbi, F2FS_WB_CP_DATA))
1172fb51b5efSChangman Lee 			break;
1173fb51b5efSChangman Lee 
1174af697c0fSJaegeuk Kim 		if (unlikely(f2fs_cp_error(sbi)))
1175af697c0fSJaegeuk Kim 			break;
1176af697c0fSJaegeuk Kim 
11770ff21646SYunlei He 		io_schedule_timeout(5*HZ);
1178fb51b5efSChangman Lee 	}
1179fb51b5efSChangman Lee 	finish_wait(&sbi->cp_wait, &wait);
1180fb51b5efSChangman Lee }
1181fb51b5efSChangman Lee 
1182e4c5d848SJaegeuk Kim static void update_ckpt_flags(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1183e4c5d848SJaegeuk Kim {
1184e4c5d848SJaegeuk Kim 	unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num;
1185e4c5d848SJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1186d1aa2453SChao Yu 	unsigned long flags;
1187e4c5d848SJaegeuk Kim 
1188d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1189e4c5d848SJaegeuk Kim 
1190c473f1a9SChao Yu 	if ((cpc->reason & CP_UMOUNT) &&
119110047f53SKinglong Mee 			le32_to_cpu(ckpt->cp_pack_total_block_count) >
119222ad0b6aSJaegeuk Kim 			sbi->blocks_per_seg - NM_I(sbi)->nat_bits_blocks)
119322ad0b6aSJaegeuk Kim 		disable_nat_bits(sbi, false);
119422ad0b6aSJaegeuk Kim 
11951f43e2adSChao Yu 	if (cpc->reason & CP_TRIMMED)
11961f43e2adSChao Yu 		__set_ckpt_flags(ckpt, CP_TRIMMED_FLAG);
1197cd36d7a1SChao Yu 	else
1198cd36d7a1SChao Yu 		__clear_ckpt_flags(ckpt, CP_TRIMMED_FLAG);
11991f43e2adSChao Yu 
1200c473f1a9SChao Yu 	if (cpc->reason & CP_UMOUNT)
1201e4c5d848SJaegeuk Kim 		__set_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1202e4c5d848SJaegeuk Kim 	else
1203e4c5d848SJaegeuk Kim 		__clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1204e4c5d848SJaegeuk Kim 
1205c473f1a9SChao Yu 	if (cpc->reason & CP_FASTBOOT)
1206e4c5d848SJaegeuk Kim 		__set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1207e4c5d848SJaegeuk Kim 	else
1208e4c5d848SJaegeuk Kim 		__clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1209e4c5d848SJaegeuk Kim 
1210e4c5d848SJaegeuk Kim 	if (orphan_num)
1211e4c5d848SJaegeuk Kim 		__set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1212e4c5d848SJaegeuk Kim 	else
1213e4c5d848SJaegeuk Kim 		__clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1214e4c5d848SJaegeuk Kim 
1215e4c5d848SJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
1216e4c5d848SJaegeuk Kim 		__set_ckpt_flags(ckpt, CP_FSCK_FLAG);
1217e4c5d848SJaegeuk Kim 
1218e4c5d848SJaegeuk Kim 	/* set this flag to activate crc|cp_ver for recovery */
1219e4c5d848SJaegeuk Kim 	__set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG);
1220f2367923SJaegeuk Kim 	__clear_ckpt_flags(ckpt, CP_NOCRC_RECOVERY_FLAG);
1221e4c5d848SJaegeuk Kim 
1222d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1223e4c5d848SJaegeuk Kim }
1224e4c5d848SJaegeuk Kim 
122546706d59SGao Xiang static void commit_checkpoint(struct f2fs_sb_info *sbi,
122646706d59SGao Xiang 	void *src, block_t blk_addr)
122746706d59SGao Xiang {
122846706d59SGao Xiang 	struct writeback_control wbc = {
122946706d59SGao Xiang 		.for_reclaim = 0,
123046706d59SGao Xiang 	};
123146706d59SGao Xiang 
123246706d59SGao Xiang 	/*
123346706d59SGao Xiang 	 * pagevec_lookup_tag and lock_page again will take
12344d57b86dSChao Yu 	 * some extra time. Therefore, f2fs_update_meta_pages and
12354d57b86dSChao Yu 	 * f2fs_sync_meta_pages are combined in this function.
123646706d59SGao Xiang 	 */
12374d57b86dSChao Yu 	struct page *page = f2fs_grab_meta_page(sbi, blk_addr);
123846706d59SGao Xiang 	int err;
123946706d59SGao Xiang 
124046706d59SGao Xiang 	memcpy(page_address(page), src, PAGE_SIZE);
124146706d59SGao Xiang 	set_page_dirty(page);
124246706d59SGao Xiang 
124346706d59SGao Xiang 	f2fs_wait_on_page_writeback(page, META, true);
124446706d59SGao Xiang 	f2fs_bug_on(sbi, PageWriteback(page));
124546706d59SGao Xiang 	if (unlikely(!clear_page_dirty_for_io(page)))
124646706d59SGao Xiang 		f2fs_bug_on(sbi, 1);
124746706d59SGao Xiang 
124846706d59SGao Xiang 	/* writeout cp pack 2 page */
124946706d59SGao Xiang 	err = __f2fs_write_meta_page(page, &wbc, FS_CP_META_IO);
1250af697c0fSJaegeuk Kim 	if (unlikely(err && f2fs_cp_error(sbi))) {
1251af697c0fSJaegeuk Kim 		f2fs_put_page(page, 1);
1252af697c0fSJaegeuk Kim 		return;
1253af697c0fSJaegeuk Kim 	}
125446706d59SGao Xiang 
1255af697c0fSJaegeuk Kim 	f2fs_bug_on(sbi, err);
125646706d59SGao Xiang 	f2fs_put_page(page, 0);
125746706d59SGao Xiang 
125846706d59SGao Xiang 	/* submit checkpoint (with barrier if NOBARRIER is not set) */
125946706d59SGao Xiang 	f2fs_submit_merged_write(sbi, META_FLUSH);
126046706d59SGao Xiang }
126146706d59SGao Xiang 
1262c34f42e2SChao Yu static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1263127e670aSJaegeuk Kim {
1264127e670aSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
126577041823SHuang Ying 	struct f2fs_nm_info *nm_i = NM_I(sbi);
1266d1aa2453SChao Yu 	unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num, flags;
1267127e670aSJaegeuk Kim 	block_t start_blk;
1268127e670aSJaegeuk Kim 	unsigned int data_sum_blocks, orphan_blocks;
12697e586fa0SJaegeuk Kim 	__u32 crc32 = 0;
1270127e670aSJaegeuk Kim 	int i;
127155141486SWanpeng Li 	int cp_payload_blks = __cp_payload(sbi);
12728f1dbbbbSShuoran Liu 	struct super_block *sb = sbi->sb;
12738f1dbbbbSShuoran Liu 	struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
12748f1dbbbbSShuoran Liu 	u64 kbytes_written;
12751228b482SChao Yu 	int err;
1276127e670aSJaegeuk Kim 
1277127e670aSJaegeuk Kim 	/* Flush all the NAT/SIT pages */
1278cf779cabSJaegeuk Kim 	while (get_pages(sbi, F2FS_DIRTY_META)) {
12794d57b86dSChao Yu 		f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
1280cf779cabSJaegeuk Kim 		if (unlikely(f2fs_cp_error(sbi)))
1281af697c0fSJaegeuk Kim 			break;
1282cf779cabSJaegeuk Kim 	}
1283127e670aSJaegeuk Kim 
1284127e670aSJaegeuk Kim 	/*
1285127e670aSJaegeuk Kim 	 * modify checkpoint
1286127e670aSJaegeuk Kim 	 * version number is already updated
1287127e670aSJaegeuk Kim 	 */
1288a1f72ac2SChao Yu 	ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi, true));
1289127e670aSJaegeuk Kim 	ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
1290b5b82205SChao Yu 	for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
1291127e670aSJaegeuk Kim 		ckpt->cur_node_segno[i] =
1292127e670aSJaegeuk Kim 			cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE));
1293127e670aSJaegeuk Kim 		ckpt->cur_node_blkoff[i] =
1294127e670aSJaegeuk Kim 			cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE));
1295127e670aSJaegeuk Kim 		ckpt->alloc_type[i + CURSEG_HOT_NODE] =
1296127e670aSJaegeuk Kim 				curseg_alloc_type(sbi, i + CURSEG_HOT_NODE);
1297127e670aSJaegeuk Kim 	}
1298b5b82205SChao Yu 	for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
1299127e670aSJaegeuk Kim 		ckpt->cur_data_segno[i] =
1300127e670aSJaegeuk Kim 			cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA));
1301127e670aSJaegeuk Kim 		ckpt->cur_data_blkoff[i] =
1302127e670aSJaegeuk Kim 			cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA));
1303127e670aSJaegeuk Kim 		ckpt->alloc_type[i + CURSEG_HOT_DATA] =
1304127e670aSJaegeuk Kim 				curseg_alloc_type(sbi, i + CURSEG_HOT_DATA);
1305127e670aSJaegeuk Kim 	}
1306127e670aSJaegeuk Kim 
1307127e670aSJaegeuk Kim 	/* 2 cp  + n data seg summary + orphan inode blocks */
13084d57b86dSChao Yu 	data_sum_blocks = f2fs_npages_for_summary_flush(sbi, false);
1309d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1310b5b82205SChao Yu 	if (data_sum_blocks < NR_CURSEG_DATA_TYPE)
1311aaec2b1dSChao Yu 		__set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
1312127e670aSJaegeuk Kim 	else
1313aaec2b1dSChao Yu 		__clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
1314d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1315127e670aSJaegeuk Kim 
131667298804SChao Yu 	orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num);
13171dbe4152SChangman Lee 	ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks +
13181dbe4152SChangman Lee 			orphan_blocks);
1319127e670aSJaegeuk Kim 
1320119ee914SJaegeuk Kim 	if (__remain_node_summaries(cpc->reason))
1321b5b82205SChao Yu 		ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS+
13221dbe4152SChangman Lee 				cp_payload_blks + data_sum_blocks +
13231dbe4152SChangman Lee 				orphan_blocks + NR_CURSEG_NODE_TYPE);
1324119ee914SJaegeuk Kim 	else
1325b5b82205SChao Yu 		ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
13261dbe4152SChangman Lee 				cp_payload_blks + data_sum_blocks +
13271dbe4152SChangman Lee 				orphan_blocks);
1328119ee914SJaegeuk Kim 
1329e4c5d848SJaegeuk Kim 	/* update ckpt flag for checkpoint */
1330e4c5d848SJaegeuk Kim 	update_ckpt_flags(sbi, cpc);
1331a468f0efSJaegeuk Kim 
1332127e670aSJaegeuk Kim 	/* update SIT/NAT bitmap */
1333127e670aSJaegeuk Kim 	get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
1334127e670aSJaegeuk Kim 	get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
1335127e670aSJaegeuk Kim 
133643b6573bSKeith Mok 	crc32 = f2fs_crc32(sbi, ckpt, le32_to_cpu(ckpt->checksum_offset));
13377e586fa0SJaegeuk Kim 	*((__le32 *)((unsigned char *)ckpt +
13387e586fa0SJaegeuk Kim 				le32_to_cpu(ckpt->checksum_offset)))
1339127e670aSJaegeuk Kim 				= cpu_to_le32(crc32);
1340127e670aSJaegeuk Kim 
13418508e44aSJaegeuk Kim 	start_blk = __start_cp_next_addr(sbi);
1342127e670aSJaegeuk Kim 
134322ad0b6aSJaegeuk Kim 	/* write nat bits */
134422ad0b6aSJaegeuk Kim 	if (enabled_nat_bits(sbi, cpc)) {
134522ad0b6aSJaegeuk Kim 		__u64 cp_ver = cur_cp_version(ckpt);
134622ad0b6aSJaegeuk Kim 		block_t blk;
134722ad0b6aSJaegeuk Kim 
134822ad0b6aSJaegeuk Kim 		cp_ver |= ((__u64)crc32 << 32);
134922ad0b6aSJaegeuk Kim 		*(__le64 *)nm_i->nat_bits = cpu_to_le64(cp_ver);
135022ad0b6aSJaegeuk Kim 
135122ad0b6aSJaegeuk Kim 		blk = start_blk + sbi->blocks_per_seg - nm_i->nat_bits_blocks;
135222ad0b6aSJaegeuk Kim 		for (i = 0; i < nm_i->nat_bits_blocks; i++)
13534d57b86dSChao Yu 			f2fs_update_meta_page(sbi, nm_i->nat_bits +
135422ad0b6aSJaegeuk Kim 					(i << F2FS_BLKSIZE_BITS), blk + i);
135522ad0b6aSJaegeuk Kim 
135622ad0b6aSJaegeuk Kim 		/* Flush all the NAT BITS pages */
135722ad0b6aSJaegeuk Kim 		while (get_pages(sbi, F2FS_DIRTY_META)) {
13584d57b86dSChao Yu 			f2fs_sync_meta_pages(sbi, META, LONG_MAX,
13594d57b86dSChao Yu 							FS_CP_META_IO);
136022ad0b6aSJaegeuk Kim 			if (unlikely(f2fs_cp_error(sbi)))
1361af697c0fSJaegeuk Kim 				break;
136222ad0b6aSJaegeuk Kim 		}
136322ad0b6aSJaegeuk Kim 	}
136422ad0b6aSJaegeuk Kim 
1365127e670aSJaegeuk Kim 	/* write out checkpoint buffer at block 0 */
13664d57b86dSChao Yu 	f2fs_update_meta_page(sbi, ckpt, start_blk++);
1367127e670aSJaegeuk Kim 
1368381722d2SChao Yu 	for (i = 1; i < 1 + cp_payload_blks; i++)
13694d57b86dSChao Yu 		f2fs_update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE,
1370381722d2SChao Yu 							start_blk++);
13711dbe4152SChangman Lee 
137267298804SChao Yu 	if (orphan_num) {
1373127e670aSJaegeuk Kim 		write_orphan_inodes(sbi, start_blk);
1374127e670aSJaegeuk Kim 		start_blk += orphan_blocks;
1375127e670aSJaegeuk Kim 	}
1376127e670aSJaegeuk Kim 
13774d57b86dSChao Yu 	f2fs_write_data_summaries(sbi, start_blk);
1378127e670aSJaegeuk Kim 	start_blk += data_sum_blocks;
13798f1dbbbbSShuoran Liu 
13808f1dbbbbSShuoran Liu 	/* Record write statistics in the hot node summary */
13818f1dbbbbSShuoran Liu 	kbytes_written = sbi->kbytes_written;
13828f1dbbbbSShuoran Liu 	if (sb->s_bdev->bd_part)
13838f1dbbbbSShuoran Liu 		kbytes_written += BD_PART_WRITTEN(sbi);
13848f1dbbbbSShuoran Liu 
1385b7ad7512SChao Yu 	seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written);
13868f1dbbbbSShuoran Liu 
1387119ee914SJaegeuk Kim 	if (__remain_node_summaries(cpc->reason)) {
13884d57b86dSChao Yu 		f2fs_write_node_summaries(sbi, start_blk);
1389127e670aSJaegeuk Kim 		start_blk += NR_CURSEG_NODE_TYPE;
1390127e670aSJaegeuk Kim 	}
1391127e670aSJaegeuk Kim 
139246706d59SGao Xiang 	/* update user_block_counts */
139346706d59SGao Xiang 	sbi->last_valid_block_count = sbi->total_valid_block_count;
139446706d59SGao Xiang 	percpu_counter_set(&sbi->alloc_valid_block_count, 0);
1395127e670aSJaegeuk Kim 
139646706d59SGao Xiang 	/* Here, we have one bio having CP pack except cp pack 2 page */
13974d57b86dSChao Yu 	f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
139846706d59SGao Xiang 
139946706d59SGao Xiang 	/* wait for previous submitted meta pages writeback */
140050fa53ecSChao Yu 	f2fs_wait_on_all_pages_writeback(sbi);
1401127e670aSJaegeuk Kim 
140246706d59SGao Xiang 	/* flush all device cache */
140346706d59SGao Xiang 	err = f2fs_flush_device_cache(sbi);
140446706d59SGao Xiang 	if (err)
140546706d59SGao Xiang 		return err;
1406127e670aSJaegeuk Kim 
140746706d59SGao Xiang 	/* barrier and flush checkpoint cp pack 2 page if it can */
140846706d59SGao Xiang 	commit_checkpoint(sbi, ckpt, start_blk);
140950fa53ecSChao Yu 	f2fs_wait_on_all_pages_writeback(sbi);
14106a8f8ca5SJaegeuk Kim 
141118767e62SChao Yu 	/*
141218767e62SChao Yu 	 * invalidate intermediate page cache borrowed from meta inode
141318767e62SChao Yu 	 * which are used for migration of encrypted inode's blocks.
141418767e62SChao Yu 	 */
141518767e62SChao Yu 	if (f2fs_sb_has_encrypt(sbi->sb))
141618767e62SChao Yu 		invalidate_mapping_pages(META_MAPPING(sbi),
141718767e62SChao Yu 				MAIN_BLKADDR(sbi), MAX_BLKADDR(sbi) - 1);
141818767e62SChao Yu 
14194d57b86dSChao Yu 	f2fs_release_ino_entry(sbi, false);
1420cf779cabSJaegeuk Kim 
142150fa53ecSChao Yu 	f2fs_reset_fsync_node_info(sbi);
142250fa53ecSChao Yu 
1423caf0047eSChao Yu 	clear_sbi_flag(sbi, SBI_IS_DIRTY);
1424bbf156f7SJaegeuk Kim 	clear_sbi_flag(sbi, SBI_NEED_CP);
14258508e44aSJaegeuk Kim 	__set_cp_next_pack(sbi);
1426c34f42e2SChao Yu 
1427c2a080aeSChao Yu 	/*
1428c2a080aeSChao Yu 	 * redirty superblock if metadata like node page or inode cache is
1429c2a080aeSChao Yu 	 * updated during writing checkpoint.
1430c2a080aeSChao Yu 	 */
1431c2a080aeSChao Yu 	if (get_pages(sbi, F2FS_DIRTY_NODES) ||
1432c2a080aeSChao Yu 			get_pages(sbi, F2FS_DIRTY_IMETA))
1433c2a080aeSChao Yu 		set_sbi_flag(sbi, SBI_IS_DIRTY);
1434c2a080aeSChao Yu 
1435c2a080aeSChao Yu 	f2fs_bug_on(sbi, get_pages(sbi, F2FS_DIRTY_DENTS));
1436c2a080aeSChao Yu 
1437af697c0fSJaegeuk Kim 	return unlikely(f2fs_cp_error(sbi)) ? -EIO : 0;
1438127e670aSJaegeuk Kim }
1439127e670aSJaegeuk Kim 
14400a8165d7SJaegeuk Kim /*
1441e1c42045Sarter97  * We guarantee that this checkpoint procedure will not fail.
1442127e670aSJaegeuk Kim  */
14434d57b86dSChao Yu int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1444127e670aSJaegeuk Kim {
1445127e670aSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1446127e670aSJaegeuk Kim 	unsigned long long ckpt_ver;
1447c34f42e2SChao Yu 	int err = 0;
1448127e670aSJaegeuk Kim 
1449127e670aSJaegeuk Kim 	mutex_lock(&sbi->cp_mutex);
14508501017eSJaegeuk Kim 
1451caf0047eSChao Yu 	if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) &&
1452c473f1a9SChao Yu 		((cpc->reason & CP_FASTBOOT) || (cpc->reason & CP_SYNC) ||
1453c473f1a9SChao Yu 		((cpc->reason & CP_DISCARD) && !sbi->discard_blks)))
14548501017eSJaegeuk Kim 		goto out;
1455c34f42e2SChao Yu 	if (unlikely(f2fs_cp_error(sbi))) {
1456c34f42e2SChao Yu 		err = -EIO;
1457cf779cabSJaegeuk Kim 		goto out;
1458c34f42e2SChao Yu 	}
1459c34f42e2SChao Yu 	if (f2fs_readonly(sbi->sb)) {
1460c34f42e2SChao Yu 		err = -EROFS;
146111504a8eSJaegeuk Kim 		goto out;
1462c34f42e2SChao Yu 	}
14632bda542dSWanpeng Li 
14642bda542dSWanpeng Li 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops");
14652bda542dSWanpeng Li 
1466c34f42e2SChao Yu 	err = block_operations(sbi);
1467c34f42e2SChao Yu 	if (err)
1468cf779cabSJaegeuk Kim 		goto out;
1469127e670aSJaegeuk Kim 
147075ab4cb8SJaegeuk Kim 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops");
14712af4bd6cSNamjae Jeon 
1472b9109b0eSJaegeuk Kim 	f2fs_flush_merged_writes(sbi);
1473127e670aSJaegeuk Kim 
147458cce381SYunlei He 	/* this is the case of multiple fstrims without any changes */
1475c473f1a9SChao Yu 	if (cpc->reason & CP_DISCARD) {
14764d57b86dSChao Yu 		if (!f2fs_exist_trim_candidates(sbi, cpc)) {
147725290fa5SJaegeuk Kim 			unblock_operations(sbi);
147825290fa5SJaegeuk Kim 			goto out;
147925290fa5SJaegeuk Kim 		}
148025290fa5SJaegeuk Kim 
14810333ad4eSJaegeuk Kim 		if (NM_I(sbi)->dirty_nat_cnt == 0 &&
14820333ad4eSJaegeuk Kim 				SIT_I(sbi)->dirty_sentries == 0 &&
14830333ad4eSJaegeuk Kim 				prefree_segments(sbi) == 0) {
14844d57b86dSChao Yu 			f2fs_flush_sit_entries(sbi, cpc);
14854d57b86dSChao Yu 			f2fs_clear_prefree_segments(sbi, cpc);
148658cce381SYunlei He 			unblock_operations(sbi);
148758cce381SYunlei He 			goto out;
148858cce381SYunlei He 		}
14890333ad4eSJaegeuk Kim 	}
149058cce381SYunlei He 
1491127e670aSJaegeuk Kim 	/*
1492127e670aSJaegeuk Kim 	 * update checkpoint pack index
1493127e670aSJaegeuk Kim 	 * Increase the version number so that
1494127e670aSJaegeuk Kim 	 * SIT entries and seg summaries are written at correct place
1495127e670aSJaegeuk Kim 	 */
1496d71b5564SJaegeuk Kim 	ckpt_ver = cur_cp_version(ckpt);
1497127e670aSJaegeuk Kim 	ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
1498127e670aSJaegeuk Kim 
1499127e670aSJaegeuk Kim 	/* write cached NAT/SIT entries to NAT/SIT area */
15004d57b86dSChao Yu 	f2fs_flush_nat_entries(sbi, cpc);
15014d57b86dSChao Yu 	f2fs_flush_sit_entries(sbi, cpc);
1502127e670aSJaegeuk Kim 
1503127e670aSJaegeuk Kim 	/* unlock all the fs_lock[] in do_checkpoint() */
1504c34f42e2SChao Yu 	err = do_checkpoint(sbi, cpc);
15054e6a8d9bSJaegeuk Kim 	if (err)
15064d57b86dSChao Yu 		f2fs_release_discard_addrs(sbi);
15074e6a8d9bSJaegeuk Kim 	else
15084d57b86dSChao Yu 		f2fs_clear_prefree_segments(sbi, cpc);
1509275b66b0SChao Yu 
1510127e670aSJaegeuk Kim 	unblock_operations(sbi);
1511942e0be6SChangman Lee 	stat_inc_cp_count(sbi->stat_info);
151210027551SJaegeuk Kim 
1513c473f1a9SChao Yu 	if (cpc->reason & CP_RECOVERY)
151410027551SJaegeuk Kim 		f2fs_msg(sbi->sb, KERN_NOTICE,
151510027551SJaegeuk Kim 			"checkpoint: version = %llx", ckpt_ver);
151660b99b48SJaegeuk Kim 
151760b99b48SJaegeuk Kim 	/* do checkpoint periodically */
15186beceb54SJaegeuk Kim 	f2fs_update_time(sbi, CP_TIME);
151955d1cdb2SJaegeuk Kim 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint");
15208501017eSJaegeuk Kim out:
15218501017eSJaegeuk Kim 	mutex_unlock(&sbi->cp_mutex);
1522c34f42e2SChao Yu 	return err;
1523127e670aSJaegeuk Kim }
1524127e670aSJaegeuk Kim 
15254d57b86dSChao Yu void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi)
1526127e670aSJaegeuk Kim {
15276451e041SJaegeuk Kim 	int i;
15286451e041SJaegeuk Kim 
15296451e041SJaegeuk Kim 	for (i = 0; i < MAX_INO_ENTRY; i++) {
153067298804SChao Yu 		struct inode_management *im = &sbi->im[i];
153167298804SChao Yu 
153267298804SChao Yu 		INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC);
153367298804SChao Yu 		spin_lock_init(&im->ino_lock);
153467298804SChao Yu 		INIT_LIST_HEAD(&im->ino_list);
153567298804SChao Yu 		im->ino_num = 0;
15366451e041SJaegeuk Kim 	}
15376451e041SJaegeuk Kim 
1538b5b82205SChao Yu 	sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS -
153914b42817SWanpeng Li 			NR_CURSEG_TYPE - __cp_payload(sbi)) *
154014b42817SWanpeng Li 				F2FS_ORPHANS_PER_BLOCK;
1541127e670aSJaegeuk Kim }
1542127e670aSJaegeuk Kim 
15434d57b86dSChao Yu int __init f2fs_create_checkpoint_caches(void)
1544127e670aSJaegeuk Kim {
15456451e041SJaegeuk Kim 	ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry",
15466451e041SJaegeuk Kim 			sizeof(struct ino_entry));
15476451e041SJaegeuk Kim 	if (!ino_entry_slab)
1548127e670aSJaegeuk Kim 		return -ENOMEM;
15494d57b86dSChao Yu 	f2fs_inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry",
155006292073SChao Yu 			sizeof(struct inode_entry));
15514d57b86dSChao Yu 	if (!f2fs_inode_entry_slab) {
15526451e041SJaegeuk Kim 		kmem_cache_destroy(ino_entry_slab);
1553127e670aSJaegeuk Kim 		return -ENOMEM;
1554127e670aSJaegeuk Kim 	}
1555127e670aSJaegeuk Kim 	return 0;
1556127e670aSJaegeuk Kim }
1557127e670aSJaegeuk Kim 
15584d57b86dSChao Yu void f2fs_destroy_checkpoint_caches(void)
1559127e670aSJaegeuk Kim {
15606451e041SJaegeuk Kim 	kmem_cache_destroy(ino_entry_slab);
15614d57b86dSChao Yu 	kmem_cache_destroy(f2fs_inode_entry_slab);
1562127e670aSJaegeuk Kim }
1563