xref: /openbmc/linux/fs/f2fs/checkpoint.c (revision c227f912732f204c0ec4a577ba812401ac4672af)
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 
290a8165d7SJaegeuk Kim /*
30127e670aSJaegeuk Kim  * We guarantee no failure on the returned page.
31127e670aSJaegeuk Kim  */
32127e670aSJaegeuk Kim struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
33127e670aSJaegeuk Kim {
349df27d98SGu Zheng 	struct address_space *mapping = META_MAPPING(sbi);
35127e670aSJaegeuk Kim 	struct page *page = NULL;
36127e670aSJaegeuk Kim repeat:
37bde44686SJaegeuk Kim 	page = grab_cache_page(mapping, index);
38127e670aSJaegeuk Kim 	if (!page) {
39127e670aSJaegeuk Kim 		cond_resched();
40127e670aSJaegeuk Kim 		goto repeat;
41127e670aSJaegeuk Kim 	}
42bde44686SJaegeuk Kim 	f2fs_wait_on_page_writeback(page, META);
43127e670aSJaegeuk Kim 	SetPageUptodate(page);
44127e670aSJaegeuk Kim 	return page;
45127e670aSJaegeuk Kim }
46127e670aSJaegeuk Kim 
470a8165d7SJaegeuk Kim /*
48127e670aSJaegeuk Kim  * We guarantee no failure on the returned page.
49127e670aSJaegeuk Kim  */
502b947003SChao Yu static struct page *__get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index,
512b947003SChao Yu 							bool is_meta)
52127e670aSJaegeuk Kim {
539df27d98SGu Zheng 	struct address_space *mapping = META_MAPPING(sbi);
54127e670aSJaegeuk Kim 	struct page *page;
55cf04e8ebSJaegeuk Kim 	struct f2fs_io_info fio = {
5605ca3632SJaegeuk Kim 		.sbi = sbi,
57cf04e8ebSJaegeuk Kim 		.type = META,
58cf04e8ebSJaegeuk Kim 		.rw = READ_SYNC | REQ_META | REQ_PRIO,
59cf04e8ebSJaegeuk Kim 		.blk_addr = index,
604375a336SJaegeuk Kim 		.encrypted_page = NULL,
61cf04e8ebSJaegeuk Kim 	};
622b947003SChao Yu 
632b947003SChao Yu 	if (unlikely(!is_meta))
642b947003SChao Yu 		fio.rw &= ~REQ_META;
65127e670aSJaegeuk Kim repeat:
66127e670aSJaegeuk Kim 	page = grab_cache_page(mapping, index);
67127e670aSJaegeuk Kim 	if (!page) {
68127e670aSJaegeuk Kim 		cond_resched();
69127e670aSJaegeuk Kim 		goto repeat;
70127e670aSJaegeuk Kim 	}
71393ff91fSJaegeuk Kim 	if (PageUptodate(page))
72393ff91fSJaegeuk Kim 		goto out;
73127e670aSJaegeuk Kim 
7405ca3632SJaegeuk Kim 	fio.page = page;
7505ca3632SJaegeuk Kim 
7686531d6bSJaegeuk Kim 	if (f2fs_submit_page_bio(&fio)) {
7786531d6bSJaegeuk Kim 		f2fs_put_page(page, 1);
78393ff91fSJaegeuk Kim 		goto repeat;
7986531d6bSJaegeuk Kim 	}
80393ff91fSJaegeuk Kim 
81393ff91fSJaegeuk Kim 	lock_page(page);
826bacf52fSJaegeuk Kim 	if (unlikely(page->mapping != mapping)) {
83afcb7ca0SJaegeuk Kim 		f2fs_put_page(page, 1);
84afcb7ca0SJaegeuk Kim 		goto repeat;
85afcb7ca0SJaegeuk Kim 	}
86f3f338caSChao Yu 
87f3f338caSChao Yu 	/*
88f3f338caSChao Yu 	 * if there is any IO error when accessing device, make our filesystem
89f3f338caSChao Yu 	 * readonly and make sure do not write checkpoint with non-uptodate
90f3f338caSChao Yu 	 * meta page.
91f3f338caSChao Yu 	 */
92f3f338caSChao Yu 	if (unlikely(!PageUptodate(page)))
93f3f338caSChao Yu 		f2fs_stop_checkpoint(sbi);
94393ff91fSJaegeuk Kim out:
95127e670aSJaegeuk Kim 	return page;
96127e670aSJaegeuk Kim }
97127e670aSJaegeuk Kim 
982b947003SChao Yu struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
992b947003SChao Yu {
1002b947003SChao Yu 	return __get_meta_page(sbi, index, true);
1012b947003SChao Yu }
1022b947003SChao Yu 
1032b947003SChao Yu /* for POR only */
1042b947003SChao Yu struct page *get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index)
1052b947003SChao Yu {
1062b947003SChao Yu 	return __get_meta_page(sbi, index, false);
1072b947003SChao Yu }
1082b947003SChao Yu 
109f0c9cadaSChao Yu bool is_valid_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr, int type)
110662befdaSChao Yu {
111662befdaSChao Yu 	switch (type) {
112662befdaSChao Yu 	case META_NAT:
11366b00c18SChao Yu 		break;
114662befdaSChao Yu 	case META_SIT:
11566b00c18SChao Yu 		if (unlikely(blkaddr >= SIT_BLK_CNT(sbi)))
11666b00c18SChao Yu 			return false;
11766b00c18SChao Yu 		break;
11881c1a0f1SChao Yu 	case META_SSA:
11966b00c18SChao Yu 		if (unlikely(blkaddr >= MAIN_BLKADDR(sbi) ||
12066b00c18SChao Yu 			blkaddr < SM_I(sbi)->ssa_blkaddr))
12166b00c18SChao Yu 			return false;
12266b00c18SChao Yu 		break;
123662befdaSChao Yu 	case META_CP:
12466b00c18SChao Yu 		if (unlikely(blkaddr >= SIT_I(sbi)->sit_base_addr ||
12566b00c18SChao Yu 			blkaddr < __start_cp_addr(sbi)))
12666b00c18SChao Yu 			return false;
12766b00c18SChao Yu 		break;
1284c521f49SJaegeuk Kim 	case META_POR:
12966b00c18SChao Yu 		if (unlikely(blkaddr >= MAX_BLKADDR(sbi) ||
13066b00c18SChao Yu 			blkaddr < MAIN_BLKADDR(sbi)))
13166b00c18SChao Yu 			return false;
13266b00c18SChao Yu 		break;
133662befdaSChao Yu 	default:
134662befdaSChao Yu 		BUG();
135662befdaSChao Yu 	}
13666b00c18SChao Yu 
13766b00c18SChao Yu 	return true;
138662befdaSChao Yu }
139662befdaSChao Yu 
140662befdaSChao Yu /*
14181c1a0f1SChao Yu  * Readahead CP/NAT/SIT/SSA pages
142662befdaSChao Yu  */
14326879fb1SChao Yu int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
14426879fb1SChao Yu 							int type, bool sync)
145662befdaSChao Yu {
146662befdaSChao Yu 	block_t prev_blk_addr = 0;
147662befdaSChao Yu 	struct page *page;
1484c521f49SJaegeuk Kim 	block_t blkno = start;
149662befdaSChao Yu 	struct f2fs_io_info fio = {
15005ca3632SJaegeuk Kim 		.sbi = sbi,
151662befdaSChao Yu 		.type = META,
15226879fb1SChao Yu 		.rw = sync ? (READ_SYNC | REQ_META | REQ_PRIO) : READA,
1534375a336SJaegeuk Kim 		.encrypted_page = NULL,
154662befdaSChao Yu 	};
155662befdaSChao Yu 
1562b947003SChao Yu 	if (unlikely(type == META_POR))
1572b947003SChao Yu 		fio.rw &= ~REQ_META;
1582b947003SChao Yu 
159662befdaSChao Yu 	for (; nrpages-- > 0; blkno++) {
160662befdaSChao Yu 
16166b00c18SChao Yu 		if (!is_valid_blkaddr(sbi, blkno, type))
16266b00c18SChao Yu 			goto out;
16366b00c18SChao Yu 
164662befdaSChao Yu 		switch (type) {
165662befdaSChao Yu 		case META_NAT:
16666b00c18SChao Yu 			if (unlikely(blkno >=
16766b00c18SChao Yu 					NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid)))
168662befdaSChao Yu 				blkno = 0;
16966b00c18SChao Yu 			/* get nat block addr */
170cf04e8ebSJaegeuk Kim 			fio.blk_addr = current_nat_addr(sbi,
171662befdaSChao Yu 					blkno * NAT_ENTRY_PER_BLOCK);
172662befdaSChao Yu 			break;
173662befdaSChao Yu 		case META_SIT:
174662befdaSChao Yu 			/* get sit block addr */
175cf04e8ebSJaegeuk Kim 			fio.blk_addr = current_sit_addr(sbi,
176662befdaSChao Yu 					blkno * SIT_ENTRY_PER_BLOCK);
177cf04e8ebSJaegeuk Kim 			if (blkno != start && prev_blk_addr + 1 != fio.blk_addr)
178662befdaSChao Yu 				goto out;
179cf04e8ebSJaegeuk Kim 			prev_blk_addr = fio.blk_addr;
180662befdaSChao Yu 			break;
18181c1a0f1SChao Yu 		case META_SSA:
182662befdaSChao Yu 		case META_CP:
1834c521f49SJaegeuk Kim 		case META_POR:
184cf04e8ebSJaegeuk Kim 			fio.blk_addr = blkno;
185662befdaSChao Yu 			break;
186662befdaSChao Yu 		default:
187662befdaSChao Yu 			BUG();
188662befdaSChao Yu 		}
189662befdaSChao Yu 
190cf04e8ebSJaegeuk Kim 		page = grab_cache_page(META_MAPPING(sbi), fio.blk_addr);
191662befdaSChao Yu 		if (!page)
192662befdaSChao Yu 			continue;
193662befdaSChao Yu 		if (PageUptodate(page)) {
194662befdaSChao Yu 			f2fs_put_page(page, 1);
195662befdaSChao Yu 			continue;
196662befdaSChao Yu 		}
197662befdaSChao Yu 
19805ca3632SJaegeuk Kim 		fio.page = page;
19905ca3632SJaegeuk Kim 		f2fs_submit_page_mbio(&fio);
200662befdaSChao Yu 		f2fs_put_page(page, 0);
201662befdaSChao Yu 	}
202662befdaSChao Yu out:
203662befdaSChao Yu 	f2fs_submit_merged_bio(sbi, META, READ);
204662befdaSChao Yu 	return blkno - start;
205662befdaSChao Yu }
206662befdaSChao Yu 
207635aee1fSChao Yu void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index)
208635aee1fSChao Yu {
209635aee1fSChao Yu 	struct page *page;
210635aee1fSChao Yu 	bool readahead = false;
211635aee1fSChao Yu 
212635aee1fSChao Yu 	page = find_get_page(META_MAPPING(sbi), index);
213635aee1fSChao Yu 	if (!page || (page && !PageUptodate(page)))
214635aee1fSChao Yu 		readahead = true;
215635aee1fSChao Yu 	f2fs_put_page(page, 0);
216635aee1fSChao Yu 
217635aee1fSChao Yu 	if (readahead)
21826879fb1SChao Yu 		ra_meta_pages(sbi, index, MAX_BIO_BLOCKS(sbi), META_POR, true);
219635aee1fSChao Yu }
220635aee1fSChao Yu 
221127e670aSJaegeuk Kim static int f2fs_write_meta_page(struct page *page,
222127e670aSJaegeuk Kim 				struct writeback_control *wbc)
223127e670aSJaegeuk Kim {
2244081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_P_SB(page);
225577e3495SJaegeuk Kim 
226ecda0de3SChao Yu 	trace_f2fs_writepage(page, META);
227ecda0de3SChao Yu 
228caf0047eSChao Yu 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
229cfb271d4SChao Yu 		goto redirty_out;
230857dc4e0SJaegeuk Kim 	if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0))
231cfb271d4SChao Yu 		goto redirty_out;
2321e968fdfSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
233cf779cabSJaegeuk Kim 		goto redirty_out;
234127e670aSJaegeuk Kim 
2353cb5ad15SJaegeuk Kim 	f2fs_wait_on_page_writeback(page, META);
236577e3495SJaegeuk Kim 	write_meta_page(sbi, page);
237127e670aSJaegeuk Kim 	dec_page_count(sbi, F2FS_DIRTY_META);
238127e670aSJaegeuk Kim 	unlock_page(page);
239857dc4e0SJaegeuk Kim 
240857dc4e0SJaegeuk Kim 	if (wbc->for_reclaim)
241857dc4e0SJaegeuk Kim 		f2fs_submit_merged_bio(sbi, META, WRITE);
242577e3495SJaegeuk Kim 	return 0;
243cfb271d4SChao Yu 
244cfb271d4SChao Yu redirty_out:
24576f60268SJaegeuk Kim 	redirty_page_for_writepage(wbc, page);
246cfb271d4SChao Yu 	return AOP_WRITEPAGE_ACTIVATE;
247127e670aSJaegeuk Kim }
248127e670aSJaegeuk Kim 
249127e670aSJaegeuk Kim static int f2fs_write_meta_pages(struct address_space *mapping,
250127e670aSJaegeuk Kim 				struct writeback_control *wbc)
251127e670aSJaegeuk Kim {
2524081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
25350c8cdb3SJaegeuk Kim 	long diff, written;
254127e670aSJaegeuk Kim 
255e5748434SChao Yu 	trace_f2fs_writepages(mapping->host, wbc, META);
256e5748434SChao Yu 
2575459aa97SJaegeuk Kim 	/* collect a number of dirty meta pages and write together */
25850c8cdb3SJaegeuk Kim 	if (wbc->for_kupdate ||
25950c8cdb3SJaegeuk Kim 		get_pages(sbi, F2FS_DIRTY_META) < nr_pages_to_skip(sbi, META))
260d3baf95dSJaegeuk Kim 		goto skip_write;
261127e670aSJaegeuk Kim 
262127e670aSJaegeuk Kim 	/* if mounting is failed, skip writing node pages */
263127e670aSJaegeuk Kim 	mutex_lock(&sbi->cp_mutex);
26450c8cdb3SJaegeuk Kim 	diff = nr_pages_to_write(sbi, META, wbc);
26550c8cdb3SJaegeuk Kim 	written = sync_meta_pages(sbi, META, wbc->nr_to_write);
266127e670aSJaegeuk Kim 	mutex_unlock(&sbi->cp_mutex);
26750c8cdb3SJaegeuk Kim 	wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff);
268127e670aSJaegeuk Kim 	return 0;
269d3baf95dSJaegeuk Kim 
270d3baf95dSJaegeuk Kim skip_write:
271d3baf95dSJaegeuk Kim 	wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META);
272d3baf95dSJaegeuk Kim 	return 0;
273127e670aSJaegeuk Kim }
274127e670aSJaegeuk Kim 
275127e670aSJaegeuk Kim long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
276127e670aSJaegeuk Kim 						long nr_to_write)
277127e670aSJaegeuk Kim {
2789df27d98SGu Zheng 	struct address_space *mapping = META_MAPPING(sbi);
2796066d8cdSJaegeuk Kim 	pgoff_t index = 0, end = LONG_MAX, prev = LONG_MAX;
280127e670aSJaegeuk Kim 	struct pagevec pvec;
281127e670aSJaegeuk Kim 	long nwritten = 0;
282127e670aSJaegeuk Kim 	struct writeback_control wbc = {
283127e670aSJaegeuk Kim 		.for_reclaim = 0,
284127e670aSJaegeuk Kim 	};
285127e670aSJaegeuk Kim 
286127e670aSJaegeuk Kim 	pagevec_init(&pvec, 0);
287127e670aSJaegeuk Kim 
288127e670aSJaegeuk Kim 	while (index <= end) {
289127e670aSJaegeuk Kim 		int i, nr_pages;
290127e670aSJaegeuk Kim 		nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
291127e670aSJaegeuk Kim 				PAGECACHE_TAG_DIRTY,
292127e670aSJaegeuk Kim 				min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
293cfb271d4SChao Yu 		if (unlikely(nr_pages == 0))
294127e670aSJaegeuk Kim 			break;
295127e670aSJaegeuk Kim 
296127e670aSJaegeuk Kim 		for (i = 0; i < nr_pages; i++) {
297127e670aSJaegeuk Kim 			struct page *page = pvec.pages[i];
298203681f6SJaegeuk Kim 
2996066d8cdSJaegeuk Kim 			if (prev == LONG_MAX)
3006066d8cdSJaegeuk Kim 				prev = page->index - 1;
3016066d8cdSJaegeuk Kim 			if (nr_to_write != LONG_MAX && page->index != prev + 1) {
3026066d8cdSJaegeuk Kim 				pagevec_release(&pvec);
3036066d8cdSJaegeuk Kim 				goto stop;
3046066d8cdSJaegeuk Kim 			}
3056066d8cdSJaegeuk Kim 
306127e670aSJaegeuk Kim 			lock_page(page);
307203681f6SJaegeuk Kim 
308203681f6SJaegeuk Kim 			if (unlikely(page->mapping != mapping)) {
309203681f6SJaegeuk Kim continue_unlock:
310203681f6SJaegeuk Kim 				unlock_page(page);
311203681f6SJaegeuk Kim 				continue;
312203681f6SJaegeuk Kim 			}
313203681f6SJaegeuk Kim 			if (!PageDirty(page)) {
314203681f6SJaegeuk Kim 				/* someone wrote it for us */
315203681f6SJaegeuk Kim 				goto continue_unlock;
316203681f6SJaegeuk Kim 			}
317203681f6SJaegeuk Kim 
318203681f6SJaegeuk Kim 			if (!clear_page_dirty_for_io(page))
319203681f6SJaegeuk Kim 				goto continue_unlock;
320203681f6SJaegeuk Kim 
32197dc3fd2SChao Yu 			if (mapping->a_ops->writepage(page, &wbc)) {
322577e3495SJaegeuk Kim 				unlock_page(page);
323577e3495SJaegeuk Kim 				break;
324577e3495SJaegeuk Kim 			}
325cfb271d4SChao Yu 			nwritten++;
3266066d8cdSJaegeuk Kim 			prev = page->index;
327cfb271d4SChao Yu 			if (unlikely(nwritten >= nr_to_write))
328127e670aSJaegeuk Kim 				break;
329127e670aSJaegeuk Kim 		}
330127e670aSJaegeuk Kim 		pagevec_release(&pvec);
331127e670aSJaegeuk Kim 		cond_resched();
332127e670aSJaegeuk Kim 	}
3336066d8cdSJaegeuk Kim stop:
334127e670aSJaegeuk Kim 	if (nwritten)
335458e6197SJaegeuk Kim 		f2fs_submit_merged_bio(sbi, type, WRITE);
336127e670aSJaegeuk Kim 
337127e670aSJaegeuk Kim 	return nwritten;
338127e670aSJaegeuk Kim }
339127e670aSJaegeuk Kim 
340127e670aSJaegeuk Kim static int f2fs_set_meta_page_dirty(struct page *page)
341127e670aSJaegeuk Kim {
34226c6b887SJaegeuk Kim 	trace_f2fs_set_page_dirty(page, META);
34326c6b887SJaegeuk Kim 
344127e670aSJaegeuk Kim 	SetPageUptodate(page);
345127e670aSJaegeuk Kim 	if (!PageDirty(page)) {
346127e670aSJaegeuk Kim 		__set_page_dirty_nobuffers(page);
3474081363fSJaegeuk Kim 		inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_META);
3481601839eSChao Yu 		SetPagePrivate(page);
3499e4ded3fSJaegeuk Kim 		f2fs_trace_pid(page);
350127e670aSJaegeuk Kim 		return 1;
351127e670aSJaegeuk Kim 	}
352127e670aSJaegeuk Kim 	return 0;
353127e670aSJaegeuk Kim }
354127e670aSJaegeuk Kim 
355127e670aSJaegeuk Kim const struct address_space_operations f2fs_meta_aops = {
356127e670aSJaegeuk Kim 	.writepage	= f2fs_write_meta_page,
357127e670aSJaegeuk Kim 	.writepages	= f2fs_write_meta_pages,
358127e670aSJaegeuk Kim 	.set_page_dirty	= f2fs_set_meta_page_dirty,
359487261f3SChao Yu 	.invalidatepage = f2fs_invalidate_page,
360487261f3SChao Yu 	.releasepage	= f2fs_release_page,
361127e670aSJaegeuk Kim };
362127e670aSJaegeuk Kim 
3636451e041SJaegeuk Kim static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
364953e6cc6SJaegeuk Kim {
36567298804SChao Yu 	struct inode_management *im = &sbi->im[type];
36680c54505SJaegeuk Kim 	struct ino_entry *e, *tmp;
36780c54505SJaegeuk Kim 
36880c54505SJaegeuk Kim 	tmp = f2fs_kmem_cache_alloc(ino_entry_slab, GFP_NOFS);
36939efac41SJaegeuk Kim retry:
37080c54505SJaegeuk Kim 	radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
371769ec6e5SJaegeuk Kim 
37267298804SChao Yu 	spin_lock(&im->ino_lock);
37367298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
37439efac41SJaegeuk Kim 	if (!e) {
37580c54505SJaegeuk Kim 		e = tmp;
37667298804SChao Yu 		if (radix_tree_insert(&im->ino_root, ino, e)) {
37767298804SChao Yu 			spin_unlock(&im->ino_lock);
378769ec6e5SJaegeuk Kim 			radix_tree_preload_end();
37939efac41SJaegeuk Kim 			goto retry;
38039efac41SJaegeuk Kim 		}
38139efac41SJaegeuk Kim 		memset(e, 0, sizeof(struct ino_entry));
38239efac41SJaegeuk Kim 		e->ino = ino;
38339efac41SJaegeuk Kim 
38467298804SChao Yu 		list_add_tail(&e->list, &im->ino_list);
3858c402946SJaegeuk Kim 		if (type != ORPHAN_INO)
38667298804SChao Yu 			im->ino_num++;
38739efac41SJaegeuk Kim 	}
38867298804SChao Yu 	spin_unlock(&im->ino_lock);
389769ec6e5SJaegeuk Kim 	radix_tree_preload_end();
39080c54505SJaegeuk Kim 
39180c54505SJaegeuk Kim 	if (e != tmp)
39280c54505SJaegeuk Kim 		kmem_cache_free(ino_entry_slab, tmp);
393953e6cc6SJaegeuk Kim }
394953e6cc6SJaegeuk Kim 
3956451e041SJaegeuk Kim static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
396953e6cc6SJaegeuk Kim {
39767298804SChao Yu 	struct inode_management *im = &sbi->im[type];
3986451e041SJaegeuk Kim 	struct ino_entry *e;
399953e6cc6SJaegeuk Kim 
40067298804SChao Yu 	spin_lock(&im->ino_lock);
40167298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
40239efac41SJaegeuk Kim 	if (e) {
403953e6cc6SJaegeuk Kim 		list_del(&e->list);
40467298804SChao Yu 		radix_tree_delete(&im->ino_root, ino);
40567298804SChao Yu 		im->ino_num--;
40667298804SChao Yu 		spin_unlock(&im->ino_lock);
4076451e041SJaegeuk Kim 		kmem_cache_free(ino_entry_slab, e);
408953e6cc6SJaegeuk Kim 		return;
409953e6cc6SJaegeuk Kim 	}
41067298804SChao Yu 	spin_unlock(&im->ino_lock);
411953e6cc6SJaegeuk Kim }
412953e6cc6SJaegeuk Kim 
413a49324f1SChao Yu void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
414fff04f90SJaegeuk Kim {
415fff04f90SJaegeuk Kim 	/* add new dirty ino entry into list */
416fff04f90SJaegeuk Kim 	__add_ino_entry(sbi, ino, type);
417fff04f90SJaegeuk Kim }
418fff04f90SJaegeuk Kim 
419a49324f1SChao Yu void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
420fff04f90SJaegeuk Kim {
421fff04f90SJaegeuk Kim 	/* remove dirty ino entry from list */
422fff04f90SJaegeuk Kim 	__remove_ino_entry(sbi, ino, type);
423fff04f90SJaegeuk Kim }
424fff04f90SJaegeuk Kim 
425fff04f90SJaegeuk Kim /* mode should be APPEND_INO or UPDATE_INO */
426fff04f90SJaegeuk Kim bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode)
427fff04f90SJaegeuk Kim {
42867298804SChao Yu 	struct inode_management *im = &sbi->im[mode];
429fff04f90SJaegeuk Kim 	struct ino_entry *e;
43067298804SChao Yu 
43167298804SChao Yu 	spin_lock(&im->ino_lock);
43267298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
43367298804SChao Yu 	spin_unlock(&im->ino_lock);
434fff04f90SJaegeuk Kim 	return e ? true : false;
435fff04f90SJaegeuk Kim }
436fff04f90SJaegeuk Kim 
437a49324f1SChao Yu void release_ino_entry(struct f2fs_sb_info *sbi)
438fff04f90SJaegeuk Kim {
439fff04f90SJaegeuk Kim 	struct ino_entry *e, *tmp;
440fff04f90SJaegeuk Kim 	int i;
441fff04f90SJaegeuk Kim 
442fff04f90SJaegeuk Kim 	for (i = APPEND_INO; i <= UPDATE_INO; i++) {
44367298804SChao Yu 		struct inode_management *im = &sbi->im[i];
44467298804SChao Yu 
44567298804SChao Yu 		spin_lock(&im->ino_lock);
44667298804SChao Yu 		list_for_each_entry_safe(e, tmp, &im->ino_list, list) {
447fff04f90SJaegeuk Kim 			list_del(&e->list);
44867298804SChao Yu 			radix_tree_delete(&im->ino_root, e->ino);
449fff04f90SJaegeuk Kim 			kmem_cache_free(ino_entry_slab, e);
45067298804SChao Yu 			im->ino_num--;
451fff04f90SJaegeuk Kim 		}
45267298804SChao Yu 		spin_unlock(&im->ino_lock);
453fff04f90SJaegeuk Kim 	}
454fff04f90SJaegeuk Kim }
455fff04f90SJaegeuk Kim 
456cbd56e7dSJaegeuk Kim int acquire_orphan_inode(struct f2fs_sb_info *sbi)
457127e670aSJaegeuk Kim {
45867298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
459127e670aSJaegeuk Kim 	int err = 0;
460127e670aSJaegeuk Kim 
46167298804SChao Yu 	spin_lock(&im->ino_lock);
46267298804SChao Yu 	if (unlikely(im->ino_num >= sbi->max_orphans))
463127e670aSJaegeuk Kim 		err = -ENOSPC;
464cbd56e7dSJaegeuk Kim 	else
46567298804SChao Yu 		im->ino_num++;
46667298804SChao Yu 	spin_unlock(&im->ino_lock);
4670d47c1adSGu Zheng 
468127e670aSJaegeuk Kim 	return err;
469127e670aSJaegeuk Kim }
470127e670aSJaegeuk Kim 
471cbd56e7dSJaegeuk Kim void release_orphan_inode(struct f2fs_sb_info *sbi)
472cbd56e7dSJaegeuk Kim {
47367298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
47467298804SChao Yu 
47567298804SChao Yu 	spin_lock(&im->ino_lock);
47667298804SChao Yu 	f2fs_bug_on(sbi, im->ino_num == 0);
47767298804SChao Yu 	im->ino_num--;
47867298804SChao Yu 	spin_unlock(&im->ino_lock);
479cbd56e7dSJaegeuk Kim }
480cbd56e7dSJaegeuk Kim 
481127e670aSJaegeuk Kim void add_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
482127e670aSJaegeuk Kim {
48339efac41SJaegeuk Kim 	/* add new orphan ino entry into list */
4846451e041SJaegeuk Kim 	__add_ino_entry(sbi, ino, ORPHAN_INO);
485127e670aSJaegeuk Kim }
486127e670aSJaegeuk Kim 
487127e670aSJaegeuk Kim void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
488127e670aSJaegeuk Kim {
489953e6cc6SJaegeuk Kim 	/* remove orphan entry from orphan list */
4906451e041SJaegeuk Kim 	__remove_ino_entry(sbi, ino, ORPHAN_INO);
491127e670aSJaegeuk Kim }
492127e670aSJaegeuk Kim 
4938c14bfadSChao Yu static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
494127e670aSJaegeuk Kim {
4958c14bfadSChao Yu 	struct inode *inode;
4968c14bfadSChao Yu 
4978c14bfadSChao Yu 	inode = f2fs_iget(sbi->sb, ino);
4988c14bfadSChao Yu 	if (IS_ERR(inode)) {
4998c14bfadSChao Yu 		/*
5008c14bfadSChao Yu 		 * there should be a bug that we can't find the entry
5018c14bfadSChao Yu 		 * to orphan inode.
5028c14bfadSChao Yu 		 */
5038c14bfadSChao Yu 		f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT);
5048c14bfadSChao Yu 		return PTR_ERR(inode);
5058c14bfadSChao Yu 	}
5068c14bfadSChao Yu 
507127e670aSJaegeuk Kim 	clear_nlink(inode);
508127e670aSJaegeuk Kim 
509127e670aSJaegeuk Kim 	/* truncate all the data during iput */
510127e670aSJaegeuk Kim 	iput(inode);
5118c14bfadSChao Yu 	return 0;
512127e670aSJaegeuk Kim }
513127e670aSJaegeuk Kim 
5148c14bfadSChao Yu int recover_orphan_inodes(struct f2fs_sb_info *sbi)
515127e670aSJaegeuk Kim {
5163c642985SWanpeng Li 	block_t start_blk, orphan_blocks, i, j;
5178c14bfadSChao Yu 	int err;
518127e670aSJaegeuk Kim 
51925ca923bSJaegeuk Kim 	if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG))
5208c14bfadSChao Yu 		return 0;
521127e670aSJaegeuk Kim 
52255141486SWanpeng Li 	start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
5233c642985SWanpeng Li 	orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
524127e670aSJaegeuk Kim 
52526879fb1SChao Yu 	ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true);
526662befdaSChao Yu 
5273c642985SWanpeng Li 	for (i = 0; i < orphan_blocks; i++) {
528127e670aSJaegeuk Kim 		struct page *page = get_meta_page(sbi, start_blk + i);
529127e670aSJaegeuk Kim 		struct f2fs_orphan_block *orphan_blk;
530127e670aSJaegeuk Kim 
531127e670aSJaegeuk Kim 		orphan_blk = (struct f2fs_orphan_block *)page_address(page);
532127e670aSJaegeuk Kim 		for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
533127e670aSJaegeuk Kim 			nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
5348c14bfadSChao Yu 			err = recover_orphan_inode(sbi, ino);
5358c14bfadSChao Yu 			if (err) {
5368c14bfadSChao Yu 				f2fs_put_page(page, 1);
5378c14bfadSChao Yu 				return err;
5388c14bfadSChao Yu 			}
539127e670aSJaegeuk Kim 		}
540127e670aSJaegeuk Kim 		f2fs_put_page(page, 1);
541127e670aSJaegeuk Kim 	}
542127e670aSJaegeuk Kim 	/* clear Orphan Flag */
54325ca923bSJaegeuk Kim 	clear_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG);
5448c14bfadSChao Yu 	return 0;
545127e670aSJaegeuk Kim }
546127e670aSJaegeuk Kim 
547127e670aSJaegeuk Kim static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
548127e670aSJaegeuk Kim {
549502c6e0bSGu Zheng 	struct list_head *head;
550127e670aSJaegeuk Kim 	struct f2fs_orphan_block *orphan_blk = NULL;
551127e670aSJaegeuk Kim 	unsigned int nentries = 0;
552bd936f84SChao Yu 	unsigned short index = 1;
5538c402946SJaegeuk Kim 	unsigned short orphan_blocks;
5544531929eSGu Zheng 	struct page *page = NULL;
5556451e041SJaegeuk Kim 	struct ino_entry *orphan = NULL;
55667298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
557127e670aSJaegeuk Kim 
55867298804SChao Yu 	orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num);
5598c402946SJaegeuk Kim 
560d6c67a4fSJaegeuk Kim 	/*
561d6c67a4fSJaegeuk Kim 	 * we don't need to do spin_lock(&im->ino_lock) here, since all the
562d6c67a4fSJaegeuk Kim 	 * orphan inode operations are covered under f2fs_lock_op().
563d6c67a4fSJaegeuk Kim 	 * And, spin_lock should be avoided due to page operations below.
564d6c67a4fSJaegeuk Kim 	 */
56567298804SChao Yu 	head = &im->ino_list;
566127e670aSJaegeuk Kim 
567127e670aSJaegeuk Kim 	/* loop for each orphan inode entry and write them in Jornal block */
568502c6e0bSGu Zheng 	list_for_each_entry(orphan, head, list) {
569502c6e0bSGu Zheng 		if (!page) {
570bd936f84SChao Yu 			page = grab_meta_page(sbi, start_blk++);
571502c6e0bSGu Zheng 			orphan_blk =
572502c6e0bSGu Zheng 				(struct f2fs_orphan_block *)page_address(page);
573502c6e0bSGu Zheng 			memset(orphan_blk, 0, sizeof(*orphan_blk));
574502c6e0bSGu Zheng 		}
575127e670aSJaegeuk Kim 
57636795567SGu Zheng 		orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
577127e670aSJaegeuk Kim 
57836795567SGu Zheng 		if (nentries == F2FS_ORPHANS_PER_BLOCK) {
579127e670aSJaegeuk Kim 			/*
580127e670aSJaegeuk Kim 			 * an orphan block is full of 1020 entries,
581127e670aSJaegeuk Kim 			 * then we need to flush current orphan blocks
582127e670aSJaegeuk Kim 			 * and bring another one in memory
583127e670aSJaegeuk Kim 			 */
584127e670aSJaegeuk Kim 			orphan_blk->blk_addr = cpu_to_le16(index);
585127e670aSJaegeuk Kim 			orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
586127e670aSJaegeuk Kim 			orphan_blk->entry_count = cpu_to_le32(nentries);
587127e670aSJaegeuk Kim 			set_page_dirty(page);
588127e670aSJaegeuk Kim 			f2fs_put_page(page, 1);
589127e670aSJaegeuk Kim 			index++;
590127e670aSJaegeuk Kim 			nentries = 0;
591127e670aSJaegeuk Kim 			page = NULL;
592127e670aSJaegeuk Kim 		}
593127e670aSJaegeuk Kim 	}
594127e670aSJaegeuk Kim 
595502c6e0bSGu Zheng 	if (page) {
596127e670aSJaegeuk Kim 		orphan_blk->blk_addr = cpu_to_le16(index);
597127e670aSJaegeuk Kim 		orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
598127e670aSJaegeuk Kim 		orphan_blk->entry_count = cpu_to_le32(nentries);
599127e670aSJaegeuk Kim 		set_page_dirty(page);
600127e670aSJaegeuk Kim 		f2fs_put_page(page, 1);
601502c6e0bSGu Zheng 	}
602127e670aSJaegeuk Kim }
603127e670aSJaegeuk Kim 
604127e670aSJaegeuk Kim static struct page *validate_checkpoint(struct f2fs_sb_info *sbi,
605127e670aSJaegeuk Kim 				block_t cp_addr, unsigned long long *version)
606127e670aSJaegeuk Kim {
607127e670aSJaegeuk Kim 	struct page *cp_page_1, *cp_page_2 = NULL;
608127e670aSJaegeuk Kim 	unsigned long blk_size = sbi->blocksize;
609127e670aSJaegeuk Kim 	struct f2fs_checkpoint *cp_block;
610127e670aSJaegeuk Kim 	unsigned long long cur_version = 0, pre_version = 0;
611127e670aSJaegeuk Kim 	size_t crc_offset;
6127e586fa0SJaegeuk Kim 	__u32 crc = 0;
613127e670aSJaegeuk Kim 
614127e670aSJaegeuk Kim 	/* Read the 1st cp block in this CP pack */
615127e670aSJaegeuk Kim 	cp_page_1 = get_meta_page(sbi, cp_addr);
616127e670aSJaegeuk Kim 
617127e670aSJaegeuk Kim 	/* get the version number */
618127e670aSJaegeuk Kim 	cp_block = (struct f2fs_checkpoint *)page_address(cp_page_1);
619127e670aSJaegeuk Kim 	crc_offset = le32_to_cpu(cp_block->checksum_offset);
620127e670aSJaegeuk Kim 	if (crc_offset >= blk_size)
621127e670aSJaegeuk Kim 		goto invalid_cp1;
622127e670aSJaegeuk Kim 
62329e7043fSJaegeuk Kim 	crc = le32_to_cpu(*((__le32 *)((unsigned char *)cp_block + crc_offset)));
624127e670aSJaegeuk Kim 	if (!f2fs_crc_valid(crc, cp_block, crc_offset))
625127e670aSJaegeuk Kim 		goto invalid_cp1;
626127e670aSJaegeuk Kim 
627d71b5564SJaegeuk Kim 	pre_version = cur_cp_version(cp_block);
628127e670aSJaegeuk Kim 
629127e670aSJaegeuk Kim 	/* Read the 2nd cp block in this CP pack */
63025ca923bSJaegeuk Kim 	cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1;
631127e670aSJaegeuk Kim 	cp_page_2 = get_meta_page(sbi, cp_addr);
632127e670aSJaegeuk Kim 
633127e670aSJaegeuk Kim 	cp_block = (struct f2fs_checkpoint *)page_address(cp_page_2);
634127e670aSJaegeuk Kim 	crc_offset = le32_to_cpu(cp_block->checksum_offset);
635127e670aSJaegeuk Kim 	if (crc_offset >= blk_size)
636127e670aSJaegeuk Kim 		goto invalid_cp2;
637127e670aSJaegeuk Kim 
63829e7043fSJaegeuk Kim 	crc = le32_to_cpu(*((__le32 *)((unsigned char *)cp_block + crc_offset)));
639127e670aSJaegeuk Kim 	if (!f2fs_crc_valid(crc, cp_block, crc_offset))
640127e670aSJaegeuk Kim 		goto invalid_cp2;
641127e670aSJaegeuk Kim 
642d71b5564SJaegeuk Kim 	cur_version = cur_cp_version(cp_block);
643127e670aSJaegeuk Kim 
644127e670aSJaegeuk Kim 	if (cur_version == pre_version) {
645127e670aSJaegeuk Kim 		*version = cur_version;
646127e670aSJaegeuk Kim 		f2fs_put_page(cp_page_2, 1);
647127e670aSJaegeuk Kim 		return cp_page_1;
648127e670aSJaegeuk Kim 	}
649127e670aSJaegeuk Kim invalid_cp2:
650127e670aSJaegeuk Kim 	f2fs_put_page(cp_page_2, 1);
651127e670aSJaegeuk Kim invalid_cp1:
652127e670aSJaegeuk Kim 	f2fs_put_page(cp_page_1, 1);
653127e670aSJaegeuk Kim 	return NULL;
654127e670aSJaegeuk Kim }
655127e670aSJaegeuk Kim 
656127e670aSJaegeuk Kim int get_valid_checkpoint(struct f2fs_sb_info *sbi)
657127e670aSJaegeuk Kim {
658127e670aSJaegeuk Kim 	struct f2fs_checkpoint *cp_block;
659127e670aSJaegeuk Kim 	struct f2fs_super_block *fsb = sbi->raw_super;
660127e670aSJaegeuk Kim 	struct page *cp1, *cp2, *cur_page;
661127e670aSJaegeuk Kim 	unsigned long blk_size = sbi->blocksize;
662127e670aSJaegeuk Kim 	unsigned long long cp1_version = 0, cp2_version = 0;
663127e670aSJaegeuk Kim 	unsigned long long cp_start_blk_no;
66455141486SWanpeng Li 	unsigned int cp_blks = 1 + __cp_payload(sbi);
6651dbe4152SChangman Lee 	block_t cp_blk_no;
6661dbe4152SChangman Lee 	int i;
667127e670aSJaegeuk Kim 
6681dbe4152SChangman Lee 	sbi->ckpt = kzalloc(cp_blks * blk_size, GFP_KERNEL);
669127e670aSJaegeuk Kim 	if (!sbi->ckpt)
670127e670aSJaegeuk Kim 		return -ENOMEM;
671127e670aSJaegeuk Kim 	/*
672127e670aSJaegeuk Kim 	 * Finding out valid cp block involves read both
673127e670aSJaegeuk Kim 	 * sets( cp pack1 and cp pack 2)
674127e670aSJaegeuk Kim 	 */
675127e670aSJaegeuk Kim 	cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr);
676127e670aSJaegeuk Kim 	cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
677127e670aSJaegeuk Kim 
678127e670aSJaegeuk Kim 	/* The second checkpoint pack should start at the next segment */
679f9a4e6dfSJaegeuk Kim 	cp_start_blk_no += ((unsigned long long)1) <<
680f9a4e6dfSJaegeuk Kim 				le32_to_cpu(fsb->log_blocks_per_seg);
681127e670aSJaegeuk Kim 	cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
682127e670aSJaegeuk Kim 
683127e670aSJaegeuk Kim 	if (cp1 && cp2) {
684127e670aSJaegeuk Kim 		if (ver_after(cp2_version, cp1_version))
685127e670aSJaegeuk Kim 			cur_page = cp2;
686127e670aSJaegeuk Kim 		else
687127e670aSJaegeuk Kim 			cur_page = cp1;
688127e670aSJaegeuk Kim 	} else if (cp1) {
689127e670aSJaegeuk Kim 		cur_page = cp1;
690127e670aSJaegeuk Kim 	} else if (cp2) {
691127e670aSJaegeuk Kim 		cur_page = cp2;
692127e670aSJaegeuk Kim 	} else {
693127e670aSJaegeuk Kim 		goto fail_no_cp;
694127e670aSJaegeuk Kim 	}
695127e670aSJaegeuk Kim 
696127e670aSJaegeuk Kim 	cp_block = (struct f2fs_checkpoint *)page_address(cur_page);
697127e670aSJaegeuk Kim 	memcpy(sbi->ckpt, cp_block, blk_size);
698127e670aSJaegeuk Kim 
6991dbe4152SChangman Lee 	if (cp_blks <= 1)
7001dbe4152SChangman Lee 		goto done;
7011dbe4152SChangman Lee 
7021dbe4152SChangman Lee 	cp_blk_no = le32_to_cpu(fsb->cp_blkaddr);
7031dbe4152SChangman Lee 	if (cur_page == cp2)
7041dbe4152SChangman Lee 		cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg);
7051dbe4152SChangman Lee 
7061dbe4152SChangman Lee 	for (i = 1; i < cp_blks; i++) {
7071dbe4152SChangman Lee 		void *sit_bitmap_ptr;
7081dbe4152SChangman Lee 		unsigned char *ckpt = (unsigned char *)sbi->ckpt;
7091dbe4152SChangman Lee 
7101dbe4152SChangman Lee 		cur_page = get_meta_page(sbi, cp_blk_no + i);
7111dbe4152SChangman Lee 		sit_bitmap_ptr = page_address(cur_page);
7121dbe4152SChangman Lee 		memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size);
7131dbe4152SChangman Lee 		f2fs_put_page(cur_page, 1);
7141dbe4152SChangman Lee 	}
7151dbe4152SChangman Lee done:
716127e670aSJaegeuk Kim 	f2fs_put_page(cp1, 1);
717127e670aSJaegeuk Kim 	f2fs_put_page(cp2, 1);
718127e670aSJaegeuk Kim 	return 0;
719127e670aSJaegeuk Kim 
720127e670aSJaegeuk Kim fail_no_cp:
721127e670aSJaegeuk Kim 	kfree(sbi->ckpt);
722127e670aSJaegeuk Kim 	return -EINVAL;
723127e670aSJaegeuk Kim }
724127e670aSJaegeuk Kim 
725*c227f912SChao Yu static void __add_dirty_inode(struct inode *inode, enum inode_type type)
726127e670aSJaegeuk Kim {
7274081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
7282710fd7eSChao Yu 	struct f2fs_inode_info *fi = F2FS_I(inode);
729*c227f912SChao Yu 	int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
7302d7b822aSChao Yu 
731*c227f912SChao Yu 	if (is_inode_flag_set(fi, flag))
7322710fd7eSChao Yu 		return;
7332d7b822aSChao Yu 
734*c227f912SChao Yu 	set_inode_flag(fi, flag);
735*c227f912SChao Yu 	list_add_tail(&fi->dirty_list, &sbi->inode_list[type]);
736*c227f912SChao Yu 	if (type == DIR_INODE)
737dcdfff65SJaegeuk Kim 		stat_inc_dirty_dir(sbi);
7385deb8267SJaegeuk Kim }
7395deb8267SJaegeuk Kim 
740*c227f912SChao Yu static void __remove_dirty_inode(struct inode *inode, enum inode_type type)
7416ad7609aSChao Yu {
7426ad7609aSChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
7436ad7609aSChao Yu 	struct f2fs_inode_info *fi = F2FS_I(inode);
744*c227f912SChao Yu 	int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
7456ad7609aSChao Yu 
7466ad7609aSChao Yu 	if (get_dirty_pages(inode) ||
747*c227f912SChao Yu 			!is_inode_flag_set(F2FS_I(inode), flag))
7486ad7609aSChao Yu 		return;
7496ad7609aSChao Yu 
7506ad7609aSChao Yu 	list_del_init(&fi->dirty_list);
751*c227f912SChao Yu 	clear_inode_flag(fi, flag);
752*c227f912SChao Yu 	if (type == DIR_INODE)
7536ad7609aSChao Yu 		stat_dec_dirty_dir(sbi);
7546ad7609aSChao Yu }
7556ad7609aSChao Yu 
756a7ffdbe2SJaegeuk Kim void update_dirty_page(struct inode *inode, struct page *page)
7575deb8267SJaegeuk Kim {
7584081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
759*c227f912SChao Yu 	enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
7605deb8267SJaegeuk Kim 
7615ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
7625ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
763127e670aSJaegeuk Kim 		return;
7647bd59381SGu Zheng 
765*c227f912SChao Yu 	spin_lock(&sbi->inode_lock[type]);
766*c227f912SChao Yu 	__add_dirty_inode(inode, type);
767a7ffdbe2SJaegeuk Kim 	inode_inc_dirty_pages(inode);
768*c227f912SChao Yu 	spin_unlock(&sbi->inode_lock[type]);
769a7ffdbe2SJaegeuk Kim 
770a7ffdbe2SJaegeuk Kim 	SetPagePrivate(page);
7719e4ded3fSJaegeuk Kim 	f2fs_trace_pid(page);
7725deb8267SJaegeuk Kim }
773127e670aSJaegeuk Kim 
7745deb8267SJaegeuk Kim void add_dirty_dir_inode(struct inode *inode)
7755deb8267SJaegeuk Kim {
7764081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
7775deb8267SJaegeuk Kim 
778*c227f912SChao Yu 	spin_lock(&sbi->inode_lock[DIR_INODE]);
779*c227f912SChao Yu 	__add_dirty_inode(inode, DIR_INODE);
780*c227f912SChao Yu 	spin_unlock(&sbi->inode_lock[DIR_INODE]);
781127e670aSJaegeuk Kim }
782127e670aSJaegeuk Kim 
783*c227f912SChao Yu void remove_dirty_inode(struct inode *inode)
784127e670aSJaegeuk Kim {
7854081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
7862710fd7eSChao Yu 	struct f2fs_inode_info *fi = F2FS_I(inode);
787*c227f912SChao Yu 	enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
788127e670aSJaegeuk Kim 
789*c227f912SChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
790*c227f912SChao Yu 			!S_ISLNK(inode->i_mode))
791127e670aSJaegeuk Kim 		return;
792127e670aSJaegeuk Kim 
793*c227f912SChao Yu 	spin_lock(&sbi->inode_lock[type]);
794*c227f912SChao Yu 	__remove_dirty_inode(inode, type);
795*c227f912SChao Yu 	spin_unlock(&sbi->inode_lock[type]);
79674d0b917SJaegeuk Kim 
79774d0b917SJaegeuk Kim 	/* Only from the recovery routine */
7982710fd7eSChao Yu 	if (is_inode_flag_set(fi, FI_DELAY_IPUT)) {
7992710fd7eSChao Yu 		clear_inode_flag(fi, FI_DELAY_IPUT);
80074d0b917SJaegeuk Kim 		iput(inode);
80174d0b917SJaegeuk Kim 	}
802afc3eda2SJaegeuk Kim }
80374d0b917SJaegeuk Kim 
804*c227f912SChao Yu void sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type)
805127e670aSJaegeuk Kim {
806ce3b7d80SGu Zheng 	struct list_head *head;
807127e670aSJaegeuk Kim 	struct inode *inode;
8082710fd7eSChao Yu 	struct f2fs_inode_info *fi;
809127e670aSJaegeuk Kim retry:
810af41d3eeSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
811af41d3eeSJaegeuk Kim 		return;
812af41d3eeSJaegeuk Kim 
813*c227f912SChao Yu 	spin_lock(&sbi->inode_lock[type]);
814ce3b7d80SGu Zheng 
815*c227f912SChao Yu 	head = &sbi->inode_list[type];
816127e670aSJaegeuk Kim 	if (list_empty(head)) {
817*c227f912SChao Yu 		spin_unlock(&sbi->inode_lock[type]);
818127e670aSJaegeuk Kim 		return;
819127e670aSJaegeuk Kim 	}
8202710fd7eSChao Yu 	fi = list_entry(head->next, struct f2fs_inode_info, dirty_list);
8212710fd7eSChao Yu 	inode = igrab(&fi->vfs_inode);
822*c227f912SChao Yu 	spin_unlock(&sbi->inode_lock[type]);
823127e670aSJaegeuk Kim 	if (inode) {
82487d6f890SJaegeuk Kim 		filemap_fdatawrite(inode->i_mapping);
825127e670aSJaegeuk Kim 		iput(inode);
826127e670aSJaegeuk Kim 	} else {
827127e670aSJaegeuk Kim 		/*
828127e670aSJaegeuk Kim 		 * We should submit bio, since it exists several
829127e670aSJaegeuk Kim 		 * wribacking dentry pages in the freeing inode.
830127e670aSJaegeuk Kim 		 */
831458e6197SJaegeuk Kim 		f2fs_submit_merged_bio(sbi, DATA, WRITE);
8327ecebe5eSSebastian Andrzej Siewior 		cond_resched();
833127e670aSJaegeuk Kim 	}
834127e670aSJaegeuk Kim 	goto retry;
835127e670aSJaegeuk Kim }
836127e670aSJaegeuk Kim 
8370a8165d7SJaegeuk Kim /*
838127e670aSJaegeuk Kim  * Freeze all the FS-operations for checkpoint.
839127e670aSJaegeuk Kim  */
840cf779cabSJaegeuk Kim static int block_operations(struct f2fs_sb_info *sbi)
841127e670aSJaegeuk Kim {
842127e670aSJaegeuk Kim 	struct writeback_control wbc = {
843127e670aSJaegeuk Kim 		.sync_mode = WB_SYNC_ALL,
844127e670aSJaegeuk Kim 		.nr_to_write = LONG_MAX,
845127e670aSJaegeuk Kim 		.for_reclaim = 0,
846127e670aSJaegeuk Kim 	};
847c718379bSJaegeuk Kim 	struct blk_plug plug;
848cf779cabSJaegeuk Kim 	int err = 0;
849c718379bSJaegeuk Kim 
850c718379bSJaegeuk Kim 	blk_start_plug(&plug);
851c718379bSJaegeuk Kim 
85239936837SJaegeuk Kim retry_flush_dents:
853e479556bSGu Zheng 	f2fs_lock_all(sbi);
854127e670aSJaegeuk Kim 	/* write all the dirty dentry pages */
855127e670aSJaegeuk Kim 	if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
856e479556bSGu Zheng 		f2fs_unlock_all(sbi);
857*c227f912SChao Yu 		sync_dirty_inodes(sbi, DIR_INODE);
858cf779cabSJaegeuk Kim 		if (unlikely(f2fs_cp_error(sbi))) {
859cf779cabSJaegeuk Kim 			err = -EIO;
860cf779cabSJaegeuk Kim 			goto out;
861cf779cabSJaegeuk Kim 		}
86239936837SJaegeuk Kim 		goto retry_flush_dents;
863127e670aSJaegeuk Kim 	}
864127e670aSJaegeuk Kim 
865127e670aSJaegeuk Kim 	/*
866e1c42045Sarter97 	 * POR: we should ensure that there are no dirty node pages
867127e670aSJaegeuk Kim 	 * until finishing nat/sit flush.
868127e670aSJaegeuk Kim 	 */
86939936837SJaegeuk Kim retry_flush_nodes:
870b3582c68SChao Yu 	down_write(&sbi->node_write);
871127e670aSJaegeuk Kim 
872127e670aSJaegeuk Kim 	if (get_pages(sbi, F2FS_DIRTY_NODES)) {
873b3582c68SChao Yu 		up_write(&sbi->node_write);
87439936837SJaegeuk Kim 		sync_node_pages(sbi, 0, &wbc);
875cf779cabSJaegeuk Kim 		if (unlikely(f2fs_cp_error(sbi))) {
876cf779cabSJaegeuk Kim 			f2fs_unlock_all(sbi);
877cf779cabSJaegeuk Kim 			err = -EIO;
878cf779cabSJaegeuk Kim 			goto out;
879cf779cabSJaegeuk Kim 		}
88039936837SJaegeuk Kim 		goto retry_flush_nodes;
881127e670aSJaegeuk Kim 	}
882cf779cabSJaegeuk Kim out:
883c718379bSJaegeuk Kim 	blk_finish_plug(&plug);
884cf779cabSJaegeuk Kim 	return err;
885127e670aSJaegeuk Kim }
886127e670aSJaegeuk Kim 
887127e670aSJaegeuk Kim static void unblock_operations(struct f2fs_sb_info *sbi)
888127e670aSJaegeuk Kim {
889b3582c68SChao Yu 	up_write(&sbi->node_write);
890e479556bSGu Zheng 	f2fs_unlock_all(sbi);
891127e670aSJaegeuk Kim }
892127e670aSJaegeuk Kim 
893fb51b5efSChangman Lee static void wait_on_all_pages_writeback(struct f2fs_sb_info *sbi)
894fb51b5efSChangman Lee {
895fb51b5efSChangman Lee 	DEFINE_WAIT(wait);
896fb51b5efSChangman Lee 
897fb51b5efSChangman Lee 	for (;;) {
898fb51b5efSChangman Lee 		prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
899fb51b5efSChangman Lee 
900fb51b5efSChangman Lee 		if (!get_pages(sbi, F2FS_WRITEBACK))
901fb51b5efSChangman Lee 			break;
902fb51b5efSChangman Lee 
903fb51b5efSChangman Lee 		io_schedule();
904fb51b5efSChangman Lee 	}
905fb51b5efSChangman Lee 	finish_wait(&sbi->cp_wait, &wait);
906fb51b5efSChangman Lee }
907fb51b5efSChangman Lee 
90875ab4cb8SJaegeuk Kim static void do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
909127e670aSJaegeuk Kim {
910127e670aSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
911cf2271e7SJaegeuk Kim 	struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
91277041823SHuang Ying 	struct f2fs_nm_info *nm_i = NM_I(sbi);
91367298804SChao Yu 	unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num;
91477041823SHuang Ying 	nid_t last_nid = nm_i->next_scan_nid;
915127e670aSJaegeuk Kim 	block_t start_blk;
916127e670aSJaegeuk Kim 	unsigned int data_sum_blocks, orphan_blocks;
9177e586fa0SJaegeuk Kim 	__u32 crc32 = 0;
918127e670aSJaegeuk Kim 	int i;
91955141486SWanpeng Li 	int cp_payload_blks = __cp_payload(sbi);
920e90c2d28SChao Yu 	block_t discard_blk = NEXT_FREE_BLKADDR(sbi, curseg);
921e90c2d28SChao Yu 	bool invalidate = false;
922127e670aSJaegeuk Kim 
9231e87a78dSJaegeuk Kim 	/*
9241e87a78dSJaegeuk Kim 	 * This avoids to conduct wrong roll-forward operations and uses
9251e87a78dSJaegeuk Kim 	 * metapages, so should be called prior to sync_meta_pages below.
9261e87a78dSJaegeuk Kim 	 */
927e90c2d28SChao Yu 	if (discard_next_dnode(sbi, discard_blk))
928e90c2d28SChao Yu 		invalidate = true;
929127e670aSJaegeuk Kim 
930127e670aSJaegeuk Kim 	/* Flush all the NAT/SIT pages */
931cf779cabSJaegeuk Kim 	while (get_pages(sbi, F2FS_DIRTY_META)) {
932127e670aSJaegeuk Kim 		sync_meta_pages(sbi, META, LONG_MAX);
933cf779cabSJaegeuk Kim 		if (unlikely(f2fs_cp_error(sbi)))
934cf779cabSJaegeuk Kim 			return;
935cf779cabSJaegeuk Kim 	}
936127e670aSJaegeuk Kim 
937127e670aSJaegeuk Kim 	next_free_nid(sbi, &last_nid);
938127e670aSJaegeuk Kim 
939127e670aSJaegeuk Kim 	/*
940127e670aSJaegeuk Kim 	 * modify checkpoint
941127e670aSJaegeuk Kim 	 * version number is already updated
942127e670aSJaegeuk Kim 	 */
943127e670aSJaegeuk Kim 	ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi));
944127e670aSJaegeuk Kim 	ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
945127e670aSJaegeuk Kim 	ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
946b5b82205SChao Yu 	for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
947127e670aSJaegeuk Kim 		ckpt->cur_node_segno[i] =
948127e670aSJaegeuk Kim 			cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE));
949127e670aSJaegeuk Kim 		ckpt->cur_node_blkoff[i] =
950127e670aSJaegeuk Kim 			cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE));
951127e670aSJaegeuk Kim 		ckpt->alloc_type[i + CURSEG_HOT_NODE] =
952127e670aSJaegeuk Kim 				curseg_alloc_type(sbi, i + CURSEG_HOT_NODE);
953127e670aSJaegeuk Kim 	}
954b5b82205SChao Yu 	for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
955127e670aSJaegeuk Kim 		ckpt->cur_data_segno[i] =
956127e670aSJaegeuk Kim 			cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA));
957127e670aSJaegeuk Kim 		ckpt->cur_data_blkoff[i] =
958127e670aSJaegeuk Kim 			cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA));
959127e670aSJaegeuk Kim 		ckpt->alloc_type[i + CURSEG_HOT_DATA] =
960127e670aSJaegeuk Kim 				curseg_alloc_type(sbi, i + CURSEG_HOT_DATA);
961127e670aSJaegeuk Kim 	}
962127e670aSJaegeuk Kim 
963127e670aSJaegeuk Kim 	ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi));
964127e670aSJaegeuk Kim 	ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi));
965127e670aSJaegeuk Kim 	ckpt->next_free_nid = cpu_to_le32(last_nid);
966127e670aSJaegeuk Kim 
967127e670aSJaegeuk Kim 	/* 2 cp  + n data seg summary + orphan inode blocks */
9683fa06d7bSChao Yu 	data_sum_blocks = npages_for_summary_flush(sbi, false);
969b5b82205SChao Yu 	if (data_sum_blocks < NR_CURSEG_DATA_TYPE)
97025ca923bSJaegeuk Kim 		set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
971127e670aSJaegeuk Kim 	else
97225ca923bSJaegeuk Kim 		clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
973127e670aSJaegeuk Kim 
97467298804SChao Yu 	orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num);
9751dbe4152SChangman Lee 	ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks +
9761dbe4152SChangman Lee 			orphan_blocks);
977127e670aSJaegeuk Kim 
978119ee914SJaegeuk Kim 	if (__remain_node_summaries(cpc->reason))
979b5b82205SChao Yu 		ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS+
9801dbe4152SChangman Lee 				cp_payload_blks + data_sum_blocks +
9811dbe4152SChangman Lee 				orphan_blocks + NR_CURSEG_NODE_TYPE);
982119ee914SJaegeuk Kim 	else
983b5b82205SChao Yu 		ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
9841dbe4152SChangman Lee 				cp_payload_blks + data_sum_blocks +
9851dbe4152SChangman Lee 				orphan_blocks);
986119ee914SJaegeuk Kim 
987119ee914SJaegeuk Kim 	if (cpc->reason == CP_UMOUNT)
988119ee914SJaegeuk Kim 		set_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
989119ee914SJaegeuk Kim 	else
990119ee914SJaegeuk Kim 		clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
991119ee914SJaegeuk Kim 
992119ee914SJaegeuk Kim 	if (cpc->reason == CP_FASTBOOT)
993119ee914SJaegeuk Kim 		set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
994119ee914SJaegeuk Kim 	else
995119ee914SJaegeuk Kim 		clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
996127e670aSJaegeuk Kim 
99767298804SChao Yu 	if (orphan_num)
99825ca923bSJaegeuk Kim 		set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
999127e670aSJaegeuk Kim 	else
100025ca923bSJaegeuk Kim 		clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1001127e670aSJaegeuk Kim 
1002caf0047eSChao Yu 	if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
10032ae4c673SJaegeuk Kim 		set_ckpt_flags(ckpt, CP_FSCK_FLAG);
10042ae4c673SJaegeuk Kim 
1005127e670aSJaegeuk Kim 	/* update SIT/NAT bitmap */
1006127e670aSJaegeuk Kim 	get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
1007127e670aSJaegeuk Kim 	get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
1008127e670aSJaegeuk Kim 
1009127e670aSJaegeuk Kim 	crc32 = f2fs_crc32(ckpt, le32_to_cpu(ckpt->checksum_offset));
10107e586fa0SJaegeuk Kim 	*((__le32 *)((unsigned char *)ckpt +
10117e586fa0SJaegeuk Kim 				le32_to_cpu(ckpt->checksum_offset)))
1012127e670aSJaegeuk Kim 				= cpu_to_le32(crc32);
1013127e670aSJaegeuk Kim 
1014127e670aSJaegeuk Kim 	start_blk = __start_cp_addr(sbi);
1015127e670aSJaegeuk Kim 
1016a7230d16SJaegeuk Kim 	/* need to wait for end_io results */
1017a7230d16SJaegeuk Kim 	wait_on_all_pages_writeback(sbi);
1018a7230d16SJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
1019a7230d16SJaegeuk Kim 		return;
1020a7230d16SJaegeuk Kim 
1021127e670aSJaegeuk Kim 	/* write out checkpoint buffer at block 0 */
1022381722d2SChao Yu 	update_meta_page(sbi, ckpt, start_blk++);
1023127e670aSJaegeuk Kim 
1024381722d2SChao Yu 	for (i = 1; i < 1 + cp_payload_blks; i++)
1025381722d2SChao Yu 		update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE,
1026381722d2SChao Yu 							start_blk++);
10271dbe4152SChangman Lee 
102867298804SChao Yu 	if (orphan_num) {
1029127e670aSJaegeuk Kim 		write_orphan_inodes(sbi, start_blk);
1030127e670aSJaegeuk Kim 		start_blk += orphan_blocks;
1031127e670aSJaegeuk Kim 	}
1032127e670aSJaegeuk Kim 
1033127e670aSJaegeuk Kim 	write_data_summaries(sbi, start_blk);
1034127e670aSJaegeuk Kim 	start_blk += data_sum_blocks;
1035119ee914SJaegeuk Kim 	if (__remain_node_summaries(cpc->reason)) {
1036127e670aSJaegeuk Kim 		write_node_summaries(sbi, start_blk);
1037127e670aSJaegeuk Kim 		start_blk += NR_CURSEG_NODE_TYPE;
1038127e670aSJaegeuk Kim 	}
1039127e670aSJaegeuk Kim 
1040127e670aSJaegeuk Kim 	/* writeout checkpoint block */
1041381722d2SChao Yu 	update_meta_page(sbi, ckpt, start_blk);
1042127e670aSJaegeuk Kim 
1043127e670aSJaegeuk Kim 	/* wait for previous submitted node/meta pages writeback */
1044fb51b5efSChangman Lee 	wait_on_all_pages_writeback(sbi);
1045127e670aSJaegeuk Kim 
1046cf779cabSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
1047cf779cabSJaegeuk Kim 		return;
1048cf779cabSJaegeuk Kim 
10494ef51a8fSJaegeuk Kim 	filemap_fdatawait_range(NODE_MAPPING(sbi), 0, LONG_MAX);
10509df27d98SGu Zheng 	filemap_fdatawait_range(META_MAPPING(sbi), 0, LONG_MAX);
1051127e670aSJaegeuk Kim 
1052127e670aSJaegeuk Kim 	/* update user_block_counts */
1053127e670aSJaegeuk Kim 	sbi->last_valid_block_count = sbi->total_valid_block_count;
1054127e670aSJaegeuk Kim 	sbi->alloc_valid_block_count = 0;
1055127e670aSJaegeuk Kim 
1056127e670aSJaegeuk Kim 	/* Here, we only have one bio having CP pack */
1057127e670aSJaegeuk Kim 	sync_meta_pages(sbi, META_FLUSH, LONG_MAX);
1058127e670aSJaegeuk Kim 
10596a8f8ca5SJaegeuk Kim 	/* wait for previous submitted meta pages writeback */
10606a8f8ca5SJaegeuk Kim 	wait_on_all_pages_writeback(sbi);
10616a8f8ca5SJaegeuk Kim 
1062e90c2d28SChao Yu 	/*
1063e90c2d28SChao Yu 	 * invalidate meta page which is used temporarily for zeroing out
1064e90c2d28SChao Yu 	 * block at the end of warm node chain.
1065e90c2d28SChao Yu 	 */
1066e90c2d28SChao Yu 	if (invalidate)
1067e90c2d28SChao Yu 		invalidate_mapping_pages(META_MAPPING(sbi), discard_blk,
1068e90c2d28SChao Yu 								discard_blk);
1069e90c2d28SChao Yu 
1070a49324f1SChao Yu 	release_ino_entry(sbi);
1071cf779cabSJaegeuk Kim 
1072cf779cabSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
1073cf779cabSJaegeuk Kim 		return;
1074cf779cabSJaegeuk Kim 
1075836b5a63SJaegeuk Kim 	clear_prefree_segments(sbi, cpc);
1076caf0047eSChao Yu 	clear_sbi_flag(sbi, SBI_IS_DIRTY);
1077127e670aSJaegeuk Kim }
1078127e670aSJaegeuk Kim 
10790a8165d7SJaegeuk Kim /*
1080e1c42045Sarter97  * We guarantee that this checkpoint procedure will not fail.
1081127e670aSJaegeuk Kim  */
108275ab4cb8SJaegeuk Kim void write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1083127e670aSJaegeuk Kim {
1084127e670aSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1085127e670aSJaegeuk Kim 	unsigned long long ckpt_ver;
1086127e670aSJaegeuk Kim 
1087127e670aSJaegeuk Kim 	mutex_lock(&sbi->cp_mutex);
10888501017eSJaegeuk Kim 
1089caf0047eSChao Yu 	if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) &&
1090a66cdd98SJaegeuk Kim 		(cpc->reason == CP_FASTBOOT || cpc->reason == CP_SYNC ||
1091a66cdd98SJaegeuk Kim 		(cpc->reason == CP_DISCARD && !sbi->discard_blks)))
10928501017eSJaegeuk Kim 		goto out;
1093cf779cabSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
1094cf779cabSJaegeuk Kim 		goto out;
109511504a8eSJaegeuk Kim 	if (f2fs_readonly(sbi->sb))
109611504a8eSJaegeuk Kim 		goto out;
10972bda542dSWanpeng Li 
10982bda542dSWanpeng Li 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops");
10992bda542dSWanpeng Li 
1100cf779cabSJaegeuk Kim 	if (block_operations(sbi))
1101cf779cabSJaegeuk Kim 		goto out;
1102127e670aSJaegeuk Kim 
110375ab4cb8SJaegeuk Kim 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops");
11042af4bd6cSNamjae Jeon 
1105458e6197SJaegeuk Kim 	f2fs_submit_merged_bio(sbi, DATA, WRITE);
1106458e6197SJaegeuk Kim 	f2fs_submit_merged_bio(sbi, NODE, WRITE);
1107458e6197SJaegeuk Kim 	f2fs_submit_merged_bio(sbi, META, WRITE);
1108127e670aSJaegeuk Kim 
1109127e670aSJaegeuk Kim 	/*
1110127e670aSJaegeuk Kim 	 * update checkpoint pack index
1111127e670aSJaegeuk Kim 	 * Increase the version number so that
1112127e670aSJaegeuk Kim 	 * SIT entries and seg summaries are written at correct place
1113127e670aSJaegeuk Kim 	 */
1114d71b5564SJaegeuk Kim 	ckpt_ver = cur_cp_version(ckpt);
1115127e670aSJaegeuk Kim 	ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
1116127e670aSJaegeuk Kim 
1117127e670aSJaegeuk Kim 	/* write cached NAT/SIT entries to NAT/SIT area */
1118127e670aSJaegeuk Kim 	flush_nat_entries(sbi);
11194b2fecc8SJaegeuk Kim 	flush_sit_entries(sbi, cpc);
1120127e670aSJaegeuk Kim 
1121127e670aSJaegeuk Kim 	/* unlock all the fs_lock[] in do_checkpoint() */
112275ab4cb8SJaegeuk Kim 	do_checkpoint(sbi, cpc);
1123127e670aSJaegeuk Kim 
1124127e670aSJaegeuk Kim 	unblock_operations(sbi);
1125942e0be6SChangman Lee 	stat_inc_cp_count(sbi->stat_info);
112610027551SJaegeuk Kim 
112710027551SJaegeuk Kim 	if (cpc->reason == CP_RECOVERY)
112810027551SJaegeuk Kim 		f2fs_msg(sbi->sb, KERN_NOTICE,
112910027551SJaegeuk Kim 			"checkpoint: version = %llx", ckpt_ver);
113060b99b48SJaegeuk Kim 
113160b99b48SJaegeuk Kim 	/* do checkpoint periodically */
113260b99b48SJaegeuk Kim 	sbi->cp_expires = round_jiffies_up(jiffies + HZ * sbi->cp_interval);
113355d1cdb2SJaegeuk Kim 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint");
11348501017eSJaegeuk Kim out:
11358501017eSJaegeuk Kim 	mutex_unlock(&sbi->cp_mutex);
1136127e670aSJaegeuk Kim }
1137127e670aSJaegeuk Kim 
11386451e041SJaegeuk Kim void init_ino_entry_info(struct f2fs_sb_info *sbi)
1139127e670aSJaegeuk Kim {
11406451e041SJaegeuk Kim 	int i;
11416451e041SJaegeuk Kim 
11426451e041SJaegeuk Kim 	for (i = 0; i < MAX_INO_ENTRY; i++) {
114367298804SChao Yu 		struct inode_management *im = &sbi->im[i];
114467298804SChao Yu 
114567298804SChao Yu 		INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC);
114667298804SChao Yu 		spin_lock_init(&im->ino_lock);
114767298804SChao Yu 		INIT_LIST_HEAD(&im->ino_list);
114867298804SChao Yu 		im->ino_num = 0;
11496451e041SJaegeuk Kim 	}
11506451e041SJaegeuk Kim 
1151b5b82205SChao Yu 	sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS -
115214b42817SWanpeng Li 			NR_CURSEG_TYPE - __cp_payload(sbi)) *
115314b42817SWanpeng Li 				F2FS_ORPHANS_PER_BLOCK;
1154127e670aSJaegeuk Kim }
1155127e670aSJaegeuk Kim 
11566e6093a8SNamjae Jeon int __init create_checkpoint_caches(void)
1157127e670aSJaegeuk Kim {
11586451e041SJaegeuk Kim 	ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry",
11596451e041SJaegeuk Kim 			sizeof(struct ino_entry));
11606451e041SJaegeuk Kim 	if (!ino_entry_slab)
1161127e670aSJaegeuk Kim 		return -ENOMEM;
116206292073SChao Yu 	inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry",
116306292073SChao Yu 			sizeof(struct inode_entry));
11646bacf52fSJaegeuk Kim 	if (!inode_entry_slab) {
11656451e041SJaegeuk Kim 		kmem_cache_destroy(ino_entry_slab);
1166127e670aSJaegeuk Kim 		return -ENOMEM;
1167127e670aSJaegeuk Kim 	}
1168127e670aSJaegeuk Kim 	return 0;
1169127e670aSJaegeuk Kim }
1170127e670aSJaegeuk Kim 
1171127e670aSJaegeuk Kim void destroy_checkpoint_caches(void)
1172127e670aSJaegeuk Kim {
11736451e041SJaegeuk Kim 	kmem_cache_destroy(ino_entry_slab);
1174127e670aSJaegeuk Kim 	kmem_cache_destroy(inode_entry_slab);
1175127e670aSJaegeuk Kim }
1176