xref: /openbmc/linux/fs/f2fs/checkpoint.c (revision 9e4ded3f309eb5b5a9be0ca2acd26e5ea7f00914)
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"
23*9e4ded3fSJaegeuk Kim #include "trace.h"
242af4bd6cSNamjae Jeon #include <trace/events/f2fs.h>
25127e670aSJaegeuk Kim 
266451e041SJaegeuk Kim static struct kmem_cache *ino_entry_slab;
27127e670aSJaegeuk Kim static 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  */
50127e670aSJaegeuk Kim struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
51127e670aSJaegeuk Kim {
529df27d98SGu Zheng 	struct address_space *mapping = META_MAPPING(sbi);
53127e670aSJaegeuk Kim 	struct page *page;
54cf04e8ebSJaegeuk Kim 	struct f2fs_io_info fio = {
55cf04e8ebSJaegeuk Kim 		.type = META,
56cf04e8ebSJaegeuk Kim 		.rw = READ_SYNC | REQ_META | REQ_PRIO,
57cf04e8ebSJaegeuk Kim 		.blk_addr = index,
58cf04e8ebSJaegeuk Kim 	};
59127e670aSJaegeuk Kim repeat:
60127e670aSJaegeuk Kim 	page = grab_cache_page(mapping, index);
61127e670aSJaegeuk Kim 	if (!page) {
62127e670aSJaegeuk Kim 		cond_resched();
63127e670aSJaegeuk Kim 		goto repeat;
64127e670aSJaegeuk Kim 	}
65393ff91fSJaegeuk Kim 	if (PageUptodate(page))
66393ff91fSJaegeuk Kim 		goto out;
67127e670aSJaegeuk Kim 
68cf04e8ebSJaegeuk Kim 	if (f2fs_submit_page_bio(sbi, page, &fio))
69393ff91fSJaegeuk Kim 		goto repeat;
70393ff91fSJaegeuk Kim 
71393ff91fSJaegeuk Kim 	lock_page(page);
726bacf52fSJaegeuk Kim 	if (unlikely(page->mapping != mapping)) {
73afcb7ca0SJaegeuk Kim 		f2fs_put_page(page, 1);
74afcb7ca0SJaegeuk Kim 		goto repeat;
75afcb7ca0SJaegeuk Kim 	}
76393ff91fSJaegeuk Kim out:
77127e670aSJaegeuk Kim 	return page;
78127e670aSJaegeuk Kim }
79127e670aSJaegeuk Kim 
8066b00c18SChao Yu static inline bool is_valid_blkaddr(struct f2fs_sb_info *sbi,
8166b00c18SChao Yu 						block_t blkaddr, int type)
82662befdaSChao Yu {
83662befdaSChao Yu 	switch (type) {
84662befdaSChao Yu 	case META_NAT:
8566b00c18SChao Yu 		break;
86662befdaSChao Yu 	case META_SIT:
8766b00c18SChao Yu 		if (unlikely(blkaddr >= SIT_BLK_CNT(sbi)))
8866b00c18SChao Yu 			return false;
8966b00c18SChao Yu 		break;
9081c1a0f1SChao Yu 	case META_SSA:
9166b00c18SChao Yu 		if (unlikely(blkaddr >= MAIN_BLKADDR(sbi) ||
9266b00c18SChao Yu 			blkaddr < SM_I(sbi)->ssa_blkaddr))
9366b00c18SChao Yu 			return false;
9466b00c18SChao Yu 		break;
95662befdaSChao Yu 	case META_CP:
9666b00c18SChao Yu 		if (unlikely(blkaddr >= SIT_I(sbi)->sit_base_addr ||
9766b00c18SChao Yu 			blkaddr < __start_cp_addr(sbi)))
9866b00c18SChao Yu 			return false;
9966b00c18SChao Yu 		break;
1004c521f49SJaegeuk Kim 	case META_POR:
10166b00c18SChao Yu 		if (unlikely(blkaddr >= MAX_BLKADDR(sbi) ||
10266b00c18SChao Yu 			blkaddr < MAIN_BLKADDR(sbi)))
10366b00c18SChao Yu 			return false;
10466b00c18SChao Yu 		break;
105662befdaSChao Yu 	default:
106662befdaSChao Yu 		BUG();
107662befdaSChao Yu 	}
10866b00c18SChao Yu 
10966b00c18SChao Yu 	return true;
110662befdaSChao Yu }
111662befdaSChao Yu 
112662befdaSChao Yu /*
11381c1a0f1SChao Yu  * Readahead CP/NAT/SIT/SSA pages
114662befdaSChao Yu  */
1154c521f49SJaegeuk Kim int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages, int type)
116662befdaSChao Yu {
117662befdaSChao Yu 	block_t prev_blk_addr = 0;
118662befdaSChao Yu 	struct page *page;
1194c521f49SJaegeuk Kim 	block_t blkno = start;
120662befdaSChao Yu 	struct f2fs_io_info fio = {
121662befdaSChao Yu 		.type = META,
122662befdaSChao Yu 		.rw = READ_SYNC | REQ_META | REQ_PRIO
123662befdaSChao Yu 	};
124662befdaSChao Yu 
125662befdaSChao Yu 	for (; nrpages-- > 0; blkno++) {
126662befdaSChao Yu 
12766b00c18SChao Yu 		if (!is_valid_blkaddr(sbi, blkno, type))
12866b00c18SChao Yu 			goto out;
12966b00c18SChao Yu 
130662befdaSChao Yu 		switch (type) {
131662befdaSChao Yu 		case META_NAT:
13266b00c18SChao Yu 			if (unlikely(blkno >=
13366b00c18SChao Yu 					NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid)))
134662befdaSChao Yu 				blkno = 0;
13566b00c18SChao Yu 			/* get nat block addr */
136cf04e8ebSJaegeuk Kim 			fio.blk_addr = current_nat_addr(sbi,
137662befdaSChao Yu 					blkno * NAT_ENTRY_PER_BLOCK);
138662befdaSChao Yu 			break;
139662befdaSChao Yu 		case META_SIT:
140662befdaSChao Yu 			/* get sit block addr */
141cf04e8ebSJaegeuk Kim 			fio.blk_addr = current_sit_addr(sbi,
142662befdaSChao Yu 					blkno * SIT_ENTRY_PER_BLOCK);
143cf04e8ebSJaegeuk Kim 			if (blkno != start && prev_blk_addr + 1 != fio.blk_addr)
144662befdaSChao Yu 				goto out;
145cf04e8ebSJaegeuk Kim 			prev_blk_addr = fio.blk_addr;
146662befdaSChao Yu 			break;
14781c1a0f1SChao Yu 		case META_SSA:
148662befdaSChao Yu 		case META_CP:
1494c521f49SJaegeuk Kim 		case META_POR:
150cf04e8ebSJaegeuk Kim 			fio.blk_addr = blkno;
151662befdaSChao Yu 			break;
152662befdaSChao Yu 		default:
153662befdaSChao Yu 			BUG();
154662befdaSChao Yu 		}
155662befdaSChao Yu 
156cf04e8ebSJaegeuk Kim 		page = grab_cache_page(META_MAPPING(sbi), fio.blk_addr);
157662befdaSChao Yu 		if (!page)
158662befdaSChao Yu 			continue;
159662befdaSChao Yu 		if (PageUptodate(page)) {
160662befdaSChao Yu 			f2fs_put_page(page, 1);
161662befdaSChao Yu 			continue;
162662befdaSChao Yu 		}
163662befdaSChao Yu 
164cf04e8ebSJaegeuk Kim 		f2fs_submit_page_mbio(sbi, page, &fio);
165662befdaSChao Yu 		f2fs_put_page(page, 0);
166662befdaSChao Yu 	}
167662befdaSChao Yu out:
168662befdaSChao Yu 	f2fs_submit_merged_bio(sbi, META, READ);
169662befdaSChao Yu 	return blkno - start;
170662befdaSChao Yu }
171662befdaSChao Yu 
172635aee1fSChao Yu void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index)
173635aee1fSChao Yu {
174635aee1fSChao Yu 	struct page *page;
175635aee1fSChao Yu 	bool readahead = false;
176635aee1fSChao Yu 
177635aee1fSChao Yu 	page = find_get_page(META_MAPPING(sbi), index);
178635aee1fSChao Yu 	if (!page || (page && !PageUptodate(page)))
179635aee1fSChao Yu 		readahead = true;
180635aee1fSChao Yu 	f2fs_put_page(page, 0);
181635aee1fSChao Yu 
182635aee1fSChao Yu 	if (readahead)
183635aee1fSChao Yu 		ra_meta_pages(sbi, index, MAX_BIO_BLOCKS(sbi), META_POR);
184635aee1fSChao Yu }
185635aee1fSChao Yu 
186127e670aSJaegeuk Kim static int f2fs_write_meta_page(struct page *page,
187127e670aSJaegeuk Kim 				struct writeback_control *wbc)
188127e670aSJaegeuk Kim {
1894081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_P_SB(page);
190577e3495SJaegeuk Kim 
191ecda0de3SChao Yu 	trace_f2fs_writepage(page, META);
192ecda0de3SChao Yu 
193203681f6SJaegeuk Kim 	if (unlikely(sbi->por_doing))
194cfb271d4SChao Yu 		goto redirty_out;
195857dc4e0SJaegeuk Kim 	if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0))
196cfb271d4SChao Yu 		goto redirty_out;
1971e968fdfSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
198cf779cabSJaegeuk Kim 		goto redirty_out;
199127e670aSJaegeuk Kim 
2003cb5ad15SJaegeuk Kim 	f2fs_wait_on_page_writeback(page, META);
201577e3495SJaegeuk Kim 	write_meta_page(sbi, page);
202127e670aSJaegeuk Kim 	dec_page_count(sbi, F2FS_DIRTY_META);
203127e670aSJaegeuk Kim 	unlock_page(page);
204857dc4e0SJaegeuk Kim 
205857dc4e0SJaegeuk Kim 	if (wbc->for_reclaim)
206857dc4e0SJaegeuk Kim 		f2fs_submit_merged_bio(sbi, META, WRITE);
207577e3495SJaegeuk Kim 	return 0;
208cfb271d4SChao Yu 
209cfb271d4SChao Yu redirty_out:
21076f60268SJaegeuk Kim 	redirty_page_for_writepage(wbc, page);
211cfb271d4SChao Yu 	return AOP_WRITEPAGE_ACTIVATE;
212127e670aSJaegeuk Kim }
213127e670aSJaegeuk Kim 
214127e670aSJaegeuk Kim static int f2fs_write_meta_pages(struct address_space *mapping,
215127e670aSJaegeuk Kim 				struct writeback_control *wbc)
216127e670aSJaegeuk Kim {
2174081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
21850c8cdb3SJaegeuk Kim 	long diff, written;
219127e670aSJaegeuk Kim 
220e5748434SChao Yu 	trace_f2fs_writepages(mapping->host, wbc, META);
221e5748434SChao Yu 
2225459aa97SJaegeuk Kim 	/* collect a number of dirty meta pages and write together */
22350c8cdb3SJaegeuk Kim 	if (wbc->for_kupdate ||
22450c8cdb3SJaegeuk Kim 		get_pages(sbi, F2FS_DIRTY_META) < nr_pages_to_skip(sbi, META))
225d3baf95dSJaegeuk Kim 		goto skip_write;
226127e670aSJaegeuk Kim 
227127e670aSJaegeuk Kim 	/* if mounting is failed, skip writing node pages */
228127e670aSJaegeuk Kim 	mutex_lock(&sbi->cp_mutex);
22950c8cdb3SJaegeuk Kim 	diff = nr_pages_to_write(sbi, META, wbc);
23050c8cdb3SJaegeuk Kim 	written = sync_meta_pages(sbi, META, wbc->nr_to_write);
231127e670aSJaegeuk Kim 	mutex_unlock(&sbi->cp_mutex);
23250c8cdb3SJaegeuk Kim 	wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff);
233127e670aSJaegeuk Kim 	return 0;
234d3baf95dSJaegeuk Kim 
235d3baf95dSJaegeuk Kim skip_write:
236d3baf95dSJaegeuk Kim 	wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META);
237d3baf95dSJaegeuk Kim 	return 0;
238127e670aSJaegeuk Kim }
239127e670aSJaegeuk Kim 
240127e670aSJaegeuk Kim long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
241127e670aSJaegeuk Kim 						long nr_to_write)
242127e670aSJaegeuk Kim {
2439df27d98SGu Zheng 	struct address_space *mapping = META_MAPPING(sbi);
244127e670aSJaegeuk Kim 	pgoff_t index = 0, end = LONG_MAX;
245127e670aSJaegeuk Kim 	struct pagevec pvec;
246127e670aSJaegeuk Kim 	long nwritten = 0;
247127e670aSJaegeuk Kim 	struct writeback_control wbc = {
248127e670aSJaegeuk Kim 		.for_reclaim = 0,
249127e670aSJaegeuk Kim 	};
250127e670aSJaegeuk Kim 
251127e670aSJaegeuk Kim 	pagevec_init(&pvec, 0);
252127e670aSJaegeuk Kim 
253127e670aSJaegeuk Kim 	while (index <= end) {
254127e670aSJaegeuk Kim 		int i, nr_pages;
255127e670aSJaegeuk Kim 		nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
256127e670aSJaegeuk Kim 				PAGECACHE_TAG_DIRTY,
257127e670aSJaegeuk Kim 				min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
258cfb271d4SChao Yu 		if (unlikely(nr_pages == 0))
259127e670aSJaegeuk Kim 			break;
260127e670aSJaegeuk Kim 
261127e670aSJaegeuk Kim 		for (i = 0; i < nr_pages; i++) {
262127e670aSJaegeuk Kim 			struct page *page = pvec.pages[i];
263203681f6SJaegeuk Kim 
264127e670aSJaegeuk Kim 			lock_page(page);
265203681f6SJaegeuk Kim 
266203681f6SJaegeuk Kim 			if (unlikely(page->mapping != mapping)) {
267203681f6SJaegeuk Kim continue_unlock:
268203681f6SJaegeuk Kim 				unlock_page(page);
269203681f6SJaegeuk Kim 				continue;
270203681f6SJaegeuk Kim 			}
271203681f6SJaegeuk Kim 			if (!PageDirty(page)) {
272203681f6SJaegeuk Kim 				/* someone wrote it for us */
273203681f6SJaegeuk Kim 				goto continue_unlock;
274203681f6SJaegeuk Kim 			}
275203681f6SJaegeuk Kim 
276203681f6SJaegeuk Kim 			if (!clear_page_dirty_for_io(page))
277203681f6SJaegeuk Kim 				goto continue_unlock;
278203681f6SJaegeuk Kim 
279577e3495SJaegeuk Kim 			if (f2fs_write_meta_page(page, &wbc)) {
280577e3495SJaegeuk Kim 				unlock_page(page);
281577e3495SJaegeuk Kim 				break;
282577e3495SJaegeuk Kim 			}
283cfb271d4SChao Yu 			nwritten++;
284cfb271d4SChao Yu 			if (unlikely(nwritten >= nr_to_write))
285127e670aSJaegeuk Kim 				break;
286127e670aSJaegeuk Kim 		}
287127e670aSJaegeuk Kim 		pagevec_release(&pvec);
288127e670aSJaegeuk Kim 		cond_resched();
289127e670aSJaegeuk Kim 	}
290127e670aSJaegeuk Kim 
291127e670aSJaegeuk Kim 	if (nwritten)
292458e6197SJaegeuk Kim 		f2fs_submit_merged_bio(sbi, type, WRITE);
293127e670aSJaegeuk Kim 
294127e670aSJaegeuk Kim 	return nwritten;
295127e670aSJaegeuk Kim }
296127e670aSJaegeuk Kim 
297127e670aSJaegeuk Kim static int f2fs_set_meta_page_dirty(struct page *page)
298127e670aSJaegeuk Kim {
29926c6b887SJaegeuk Kim 	trace_f2fs_set_page_dirty(page, META);
30026c6b887SJaegeuk Kim 
301127e670aSJaegeuk Kim 	SetPageUptodate(page);
302127e670aSJaegeuk Kim 	if (!PageDirty(page)) {
303127e670aSJaegeuk Kim 		__set_page_dirty_nobuffers(page);
3044081363fSJaegeuk Kim 		inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_META);
305*9e4ded3fSJaegeuk Kim 		f2fs_trace_pid(page);
306127e670aSJaegeuk Kim 		return 1;
307127e670aSJaegeuk Kim 	}
308127e670aSJaegeuk Kim 	return 0;
309127e670aSJaegeuk Kim }
310127e670aSJaegeuk Kim 
311127e670aSJaegeuk Kim const struct address_space_operations f2fs_meta_aops = {
312127e670aSJaegeuk Kim 	.writepage	= f2fs_write_meta_page,
313127e670aSJaegeuk Kim 	.writepages	= f2fs_write_meta_pages,
314127e670aSJaegeuk Kim 	.set_page_dirty	= f2fs_set_meta_page_dirty,
315127e670aSJaegeuk Kim };
316127e670aSJaegeuk Kim 
3176451e041SJaegeuk Kim static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
318953e6cc6SJaegeuk Kim {
31967298804SChao Yu 	struct inode_management *im = &sbi->im[type];
32039efac41SJaegeuk Kim 	struct ino_entry *e;
32139efac41SJaegeuk Kim retry:
322769ec6e5SJaegeuk Kim 	if (radix_tree_preload(GFP_NOFS)) {
323769ec6e5SJaegeuk Kim 		cond_resched();
324769ec6e5SJaegeuk Kim 		goto retry;
325769ec6e5SJaegeuk Kim 	}
326769ec6e5SJaegeuk Kim 
32767298804SChao Yu 	spin_lock(&im->ino_lock);
328953e6cc6SJaegeuk Kim 
32967298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
33039efac41SJaegeuk Kim 	if (!e) {
33139efac41SJaegeuk Kim 		e = kmem_cache_alloc(ino_entry_slab, GFP_ATOMIC);
33239efac41SJaegeuk Kim 		if (!e) {
33367298804SChao Yu 			spin_unlock(&im->ino_lock);
334769ec6e5SJaegeuk Kim 			radix_tree_preload_end();
33539efac41SJaegeuk Kim 			goto retry;
33639efac41SJaegeuk Kim 		}
33767298804SChao Yu 		if (radix_tree_insert(&im->ino_root, ino, e)) {
33867298804SChao Yu 			spin_unlock(&im->ino_lock);
33939efac41SJaegeuk Kim 			kmem_cache_free(ino_entry_slab, e);
340769ec6e5SJaegeuk Kim 			radix_tree_preload_end();
34139efac41SJaegeuk Kim 			goto retry;
34239efac41SJaegeuk Kim 		}
34339efac41SJaegeuk Kim 		memset(e, 0, sizeof(struct ino_entry));
34439efac41SJaegeuk Kim 		e->ino = ino;
34539efac41SJaegeuk Kim 
34667298804SChao Yu 		list_add_tail(&e->list, &im->ino_list);
3478c402946SJaegeuk Kim 		if (type != ORPHAN_INO)
34867298804SChao Yu 			im->ino_num++;
34939efac41SJaegeuk Kim 	}
35067298804SChao Yu 	spin_unlock(&im->ino_lock);
351769ec6e5SJaegeuk Kim 	radix_tree_preload_end();
352953e6cc6SJaegeuk Kim }
353953e6cc6SJaegeuk Kim 
3546451e041SJaegeuk Kim static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
355953e6cc6SJaegeuk Kim {
35667298804SChao Yu 	struct inode_management *im = &sbi->im[type];
3576451e041SJaegeuk Kim 	struct ino_entry *e;
358953e6cc6SJaegeuk Kim 
35967298804SChao Yu 	spin_lock(&im->ino_lock);
36067298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
36139efac41SJaegeuk Kim 	if (e) {
362953e6cc6SJaegeuk Kim 		list_del(&e->list);
36367298804SChao Yu 		radix_tree_delete(&im->ino_root, ino);
36467298804SChao Yu 		im->ino_num--;
36567298804SChao Yu 		spin_unlock(&im->ino_lock);
3666451e041SJaegeuk Kim 		kmem_cache_free(ino_entry_slab, e);
367953e6cc6SJaegeuk Kim 		return;
368953e6cc6SJaegeuk Kim 	}
36967298804SChao Yu 	spin_unlock(&im->ino_lock);
370953e6cc6SJaegeuk Kim }
371953e6cc6SJaegeuk Kim 
372fff04f90SJaegeuk Kim void add_dirty_inode(struct f2fs_sb_info *sbi, nid_t ino, int type)
373fff04f90SJaegeuk Kim {
374fff04f90SJaegeuk Kim 	/* add new dirty ino entry into list */
375fff04f90SJaegeuk Kim 	__add_ino_entry(sbi, ino, type);
376fff04f90SJaegeuk Kim }
377fff04f90SJaegeuk Kim 
378fff04f90SJaegeuk Kim void remove_dirty_inode(struct f2fs_sb_info *sbi, nid_t ino, int type)
379fff04f90SJaegeuk Kim {
380fff04f90SJaegeuk Kim 	/* remove dirty ino entry from list */
381fff04f90SJaegeuk Kim 	__remove_ino_entry(sbi, ino, type);
382fff04f90SJaegeuk Kim }
383fff04f90SJaegeuk Kim 
384fff04f90SJaegeuk Kim /* mode should be APPEND_INO or UPDATE_INO */
385fff04f90SJaegeuk Kim bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode)
386fff04f90SJaegeuk Kim {
38767298804SChao Yu 	struct inode_management *im = &sbi->im[mode];
388fff04f90SJaegeuk Kim 	struct ino_entry *e;
38967298804SChao Yu 
39067298804SChao Yu 	spin_lock(&im->ino_lock);
39167298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
39267298804SChao Yu 	spin_unlock(&im->ino_lock);
393fff04f90SJaegeuk Kim 	return e ? true : false;
394fff04f90SJaegeuk Kim }
395fff04f90SJaegeuk Kim 
3966f12ac25SJaegeuk Kim void release_dirty_inode(struct f2fs_sb_info *sbi)
397fff04f90SJaegeuk Kim {
398fff04f90SJaegeuk Kim 	struct ino_entry *e, *tmp;
399fff04f90SJaegeuk Kim 	int i;
400fff04f90SJaegeuk Kim 
401fff04f90SJaegeuk Kim 	for (i = APPEND_INO; i <= UPDATE_INO; i++) {
40267298804SChao Yu 		struct inode_management *im = &sbi->im[i];
40367298804SChao Yu 
40467298804SChao Yu 		spin_lock(&im->ino_lock);
40567298804SChao Yu 		list_for_each_entry_safe(e, tmp, &im->ino_list, list) {
406fff04f90SJaegeuk Kim 			list_del(&e->list);
40767298804SChao Yu 			radix_tree_delete(&im->ino_root, e->ino);
408fff04f90SJaegeuk Kim 			kmem_cache_free(ino_entry_slab, e);
40967298804SChao Yu 			im->ino_num--;
410fff04f90SJaegeuk Kim 		}
41167298804SChao Yu 		spin_unlock(&im->ino_lock);
412fff04f90SJaegeuk Kim 	}
413fff04f90SJaegeuk Kim }
414fff04f90SJaegeuk Kim 
415cbd56e7dSJaegeuk Kim int acquire_orphan_inode(struct f2fs_sb_info *sbi)
416127e670aSJaegeuk Kim {
41767298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
418127e670aSJaegeuk Kim 	int err = 0;
419127e670aSJaegeuk Kim 
42067298804SChao Yu 	spin_lock(&im->ino_lock);
42167298804SChao Yu 	if (unlikely(im->ino_num >= sbi->max_orphans))
422127e670aSJaegeuk Kim 		err = -ENOSPC;
423cbd56e7dSJaegeuk Kim 	else
42467298804SChao Yu 		im->ino_num++;
42567298804SChao Yu 	spin_unlock(&im->ino_lock);
4260d47c1adSGu Zheng 
427127e670aSJaegeuk Kim 	return err;
428127e670aSJaegeuk Kim }
429127e670aSJaegeuk Kim 
430cbd56e7dSJaegeuk Kim void release_orphan_inode(struct f2fs_sb_info *sbi)
431cbd56e7dSJaegeuk Kim {
43267298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
43367298804SChao Yu 
43467298804SChao Yu 	spin_lock(&im->ino_lock);
43567298804SChao Yu 	f2fs_bug_on(sbi, im->ino_num == 0);
43667298804SChao Yu 	im->ino_num--;
43767298804SChao Yu 	spin_unlock(&im->ino_lock);
438cbd56e7dSJaegeuk Kim }
439cbd56e7dSJaegeuk Kim 
440127e670aSJaegeuk Kim void add_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
441127e670aSJaegeuk Kim {
44239efac41SJaegeuk Kim 	/* add new orphan ino entry into list */
4436451e041SJaegeuk Kim 	__add_ino_entry(sbi, ino, ORPHAN_INO);
444127e670aSJaegeuk Kim }
445127e670aSJaegeuk Kim 
446127e670aSJaegeuk Kim void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
447127e670aSJaegeuk Kim {
448953e6cc6SJaegeuk Kim 	/* remove orphan entry from orphan list */
4496451e041SJaegeuk Kim 	__remove_ino_entry(sbi, ino, ORPHAN_INO);
450127e670aSJaegeuk Kim }
451127e670aSJaegeuk Kim 
452127e670aSJaegeuk Kim static void recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
453127e670aSJaegeuk Kim {
454127e670aSJaegeuk Kim 	struct inode *inode = f2fs_iget(sbi->sb, ino);
4559850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, IS_ERR(inode));
456127e670aSJaegeuk Kim 	clear_nlink(inode);
457127e670aSJaegeuk Kim 
458127e670aSJaegeuk Kim 	/* truncate all the data during iput */
459127e670aSJaegeuk Kim 	iput(inode);
460127e670aSJaegeuk Kim }
461127e670aSJaegeuk Kim 
4628f99a946SChao Yu void recover_orphan_inodes(struct f2fs_sb_info *sbi)
463127e670aSJaegeuk Kim {
464127e670aSJaegeuk Kim 	block_t start_blk, orphan_blkaddr, i, j;
465127e670aSJaegeuk Kim 
46625ca923bSJaegeuk Kim 	if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG))
4678f99a946SChao Yu 		return;
468127e670aSJaegeuk Kim 
469aabe5136SHaicheng Li 	sbi->por_doing = true;
4701dbe4152SChangman Lee 
4711dbe4152SChangman Lee 	start_blk = __start_cp_addr(sbi) + 1 +
4721dbe4152SChangman Lee 		le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
473127e670aSJaegeuk Kim 	orphan_blkaddr = __start_sum_addr(sbi) - 1;
474127e670aSJaegeuk Kim 
475662befdaSChao Yu 	ra_meta_pages(sbi, start_blk, orphan_blkaddr, META_CP);
476662befdaSChao Yu 
477127e670aSJaegeuk Kim 	for (i = 0; i < orphan_blkaddr; i++) {
478127e670aSJaegeuk Kim 		struct page *page = get_meta_page(sbi, start_blk + i);
479127e670aSJaegeuk Kim 		struct f2fs_orphan_block *orphan_blk;
480127e670aSJaegeuk Kim 
481127e670aSJaegeuk Kim 		orphan_blk = (struct f2fs_orphan_block *)page_address(page);
482127e670aSJaegeuk Kim 		for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
483127e670aSJaegeuk Kim 			nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
484127e670aSJaegeuk Kim 			recover_orphan_inode(sbi, ino);
485127e670aSJaegeuk Kim 		}
486127e670aSJaegeuk Kim 		f2fs_put_page(page, 1);
487127e670aSJaegeuk Kim 	}
488127e670aSJaegeuk Kim 	/* clear Orphan Flag */
48925ca923bSJaegeuk Kim 	clear_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG);
490aabe5136SHaicheng Li 	sbi->por_doing = false;
4918f99a946SChao Yu 	return;
492127e670aSJaegeuk Kim }
493127e670aSJaegeuk Kim 
494127e670aSJaegeuk Kim static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
495127e670aSJaegeuk Kim {
496502c6e0bSGu Zheng 	struct list_head *head;
497127e670aSJaegeuk Kim 	struct f2fs_orphan_block *orphan_blk = NULL;
498127e670aSJaegeuk Kim 	unsigned int nentries = 0;
4994531929eSGu Zheng 	unsigned short index;
5008c402946SJaegeuk Kim 	unsigned short orphan_blocks;
5014531929eSGu Zheng 	struct page *page = NULL;
5026451e041SJaegeuk Kim 	struct ino_entry *orphan = NULL;
50367298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
504127e670aSJaegeuk Kim 
50567298804SChao Yu 	orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num);
5068c402946SJaegeuk Kim 
5074531929eSGu Zheng 	for (index = 0; index < orphan_blocks; index++)
50863f5384cSGu Zheng 		grab_meta_page(sbi, start_blk + index);
509127e670aSJaegeuk Kim 
5104531929eSGu Zheng 	index = 1;
51167298804SChao Yu 	spin_lock(&im->ino_lock);
51267298804SChao Yu 	head = &im->ino_list;
513127e670aSJaegeuk Kim 
514127e670aSJaegeuk Kim 	/* loop for each orphan inode entry and write them in Jornal block */
515502c6e0bSGu Zheng 	list_for_each_entry(orphan, head, list) {
516502c6e0bSGu Zheng 		if (!page) {
51763f5384cSGu Zheng 			page = find_get_page(META_MAPPING(sbi), start_blk++);
5189850cf4aSJaegeuk Kim 			f2fs_bug_on(sbi, !page);
519502c6e0bSGu Zheng 			orphan_blk =
520502c6e0bSGu Zheng 				(struct f2fs_orphan_block *)page_address(page);
521502c6e0bSGu Zheng 			memset(orphan_blk, 0, sizeof(*orphan_blk));
52263f5384cSGu Zheng 			f2fs_put_page(page, 0);
523502c6e0bSGu Zheng 		}
524127e670aSJaegeuk Kim 
52536795567SGu Zheng 		orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
526127e670aSJaegeuk Kim 
52736795567SGu Zheng 		if (nentries == F2FS_ORPHANS_PER_BLOCK) {
528127e670aSJaegeuk Kim 			/*
529127e670aSJaegeuk Kim 			 * an orphan block is full of 1020 entries,
530127e670aSJaegeuk Kim 			 * then we need to flush current orphan blocks
531127e670aSJaegeuk Kim 			 * and bring another one in memory
532127e670aSJaegeuk Kim 			 */
533127e670aSJaegeuk Kim 			orphan_blk->blk_addr = cpu_to_le16(index);
534127e670aSJaegeuk Kim 			orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
535127e670aSJaegeuk Kim 			orphan_blk->entry_count = cpu_to_le32(nentries);
536127e670aSJaegeuk Kim 			set_page_dirty(page);
537127e670aSJaegeuk Kim 			f2fs_put_page(page, 1);
538127e670aSJaegeuk Kim 			index++;
539127e670aSJaegeuk Kim 			nentries = 0;
540127e670aSJaegeuk Kim 			page = NULL;
541127e670aSJaegeuk Kim 		}
542127e670aSJaegeuk Kim 	}
543127e670aSJaegeuk Kim 
544502c6e0bSGu Zheng 	if (page) {
545127e670aSJaegeuk Kim 		orphan_blk->blk_addr = cpu_to_le16(index);
546127e670aSJaegeuk Kim 		orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
547127e670aSJaegeuk Kim 		orphan_blk->entry_count = cpu_to_le32(nentries);
548127e670aSJaegeuk Kim 		set_page_dirty(page);
549127e670aSJaegeuk Kim 		f2fs_put_page(page, 1);
550502c6e0bSGu Zheng 	}
551502c6e0bSGu Zheng 
55267298804SChao Yu 	spin_unlock(&im->ino_lock);
553127e670aSJaegeuk Kim }
554127e670aSJaegeuk Kim 
555127e670aSJaegeuk Kim static struct page *validate_checkpoint(struct f2fs_sb_info *sbi,
556127e670aSJaegeuk Kim 				block_t cp_addr, unsigned long long *version)
557127e670aSJaegeuk Kim {
558127e670aSJaegeuk Kim 	struct page *cp_page_1, *cp_page_2 = NULL;
559127e670aSJaegeuk Kim 	unsigned long blk_size = sbi->blocksize;
560127e670aSJaegeuk Kim 	struct f2fs_checkpoint *cp_block;
561127e670aSJaegeuk Kim 	unsigned long long cur_version = 0, pre_version = 0;
562127e670aSJaegeuk Kim 	size_t crc_offset;
5637e586fa0SJaegeuk Kim 	__u32 crc = 0;
564127e670aSJaegeuk Kim 
565127e670aSJaegeuk Kim 	/* Read the 1st cp block in this CP pack */
566127e670aSJaegeuk Kim 	cp_page_1 = get_meta_page(sbi, cp_addr);
567127e670aSJaegeuk Kim 
568127e670aSJaegeuk Kim 	/* get the version number */
569127e670aSJaegeuk Kim 	cp_block = (struct f2fs_checkpoint *)page_address(cp_page_1);
570127e670aSJaegeuk Kim 	crc_offset = le32_to_cpu(cp_block->checksum_offset);
571127e670aSJaegeuk Kim 	if (crc_offset >= blk_size)
572127e670aSJaegeuk Kim 		goto invalid_cp1;
573127e670aSJaegeuk Kim 
5747e586fa0SJaegeuk Kim 	crc = le32_to_cpu(*((__u32 *)((unsigned char *)cp_block + crc_offset)));
575127e670aSJaegeuk Kim 	if (!f2fs_crc_valid(crc, cp_block, crc_offset))
576127e670aSJaegeuk Kim 		goto invalid_cp1;
577127e670aSJaegeuk Kim 
578d71b5564SJaegeuk Kim 	pre_version = cur_cp_version(cp_block);
579127e670aSJaegeuk Kim 
580127e670aSJaegeuk Kim 	/* Read the 2nd cp block in this CP pack */
58125ca923bSJaegeuk Kim 	cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1;
582127e670aSJaegeuk Kim 	cp_page_2 = get_meta_page(sbi, cp_addr);
583127e670aSJaegeuk Kim 
584127e670aSJaegeuk Kim 	cp_block = (struct f2fs_checkpoint *)page_address(cp_page_2);
585127e670aSJaegeuk Kim 	crc_offset = le32_to_cpu(cp_block->checksum_offset);
586127e670aSJaegeuk Kim 	if (crc_offset >= blk_size)
587127e670aSJaegeuk Kim 		goto invalid_cp2;
588127e670aSJaegeuk Kim 
5897e586fa0SJaegeuk Kim 	crc = le32_to_cpu(*((__u32 *)((unsigned char *)cp_block + crc_offset)));
590127e670aSJaegeuk Kim 	if (!f2fs_crc_valid(crc, cp_block, crc_offset))
591127e670aSJaegeuk Kim 		goto invalid_cp2;
592127e670aSJaegeuk Kim 
593d71b5564SJaegeuk Kim 	cur_version = cur_cp_version(cp_block);
594127e670aSJaegeuk Kim 
595127e670aSJaegeuk Kim 	if (cur_version == pre_version) {
596127e670aSJaegeuk Kim 		*version = cur_version;
597127e670aSJaegeuk Kim 		f2fs_put_page(cp_page_2, 1);
598127e670aSJaegeuk Kim 		return cp_page_1;
599127e670aSJaegeuk Kim 	}
600127e670aSJaegeuk Kim invalid_cp2:
601127e670aSJaegeuk Kim 	f2fs_put_page(cp_page_2, 1);
602127e670aSJaegeuk Kim invalid_cp1:
603127e670aSJaegeuk Kim 	f2fs_put_page(cp_page_1, 1);
604127e670aSJaegeuk Kim 	return NULL;
605127e670aSJaegeuk Kim }
606127e670aSJaegeuk Kim 
607127e670aSJaegeuk Kim int get_valid_checkpoint(struct f2fs_sb_info *sbi)
608127e670aSJaegeuk Kim {
609127e670aSJaegeuk Kim 	struct f2fs_checkpoint *cp_block;
610127e670aSJaegeuk Kim 	struct f2fs_super_block *fsb = sbi->raw_super;
611127e670aSJaegeuk Kim 	struct page *cp1, *cp2, *cur_page;
612127e670aSJaegeuk Kim 	unsigned long blk_size = sbi->blocksize;
613127e670aSJaegeuk Kim 	unsigned long long cp1_version = 0, cp2_version = 0;
614127e670aSJaegeuk Kim 	unsigned long long cp_start_blk_no;
6151dbe4152SChangman Lee 	unsigned int cp_blks = 1 + le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
6161dbe4152SChangman Lee 	block_t cp_blk_no;
6171dbe4152SChangman Lee 	int i;
618127e670aSJaegeuk Kim 
6191dbe4152SChangman Lee 	sbi->ckpt = kzalloc(cp_blks * blk_size, GFP_KERNEL);
620127e670aSJaegeuk Kim 	if (!sbi->ckpt)
621127e670aSJaegeuk Kim 		return -ENOMEM;
622127e670aSJaegeuk Kim 	/*
623127e670aSJaegeuk Kim 	 * Finding out valid cp block involves read both
624127e670aSJaegeuk Kim 	 * sets( cp pack1 and cp pack 2)
625127e670aSJaegeuk Kim 	 */
626127e670aSJaegeuk Kim 	cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr);
627127e670aSJaegeuk Kim 	cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
628127e670aSJaegeuk Kim 
629127e670aSJaegeuk Kim 	/* The second checkpoint pack should start at the next segment */
630f9a4e6dfSJaegeuk Kim 	cp_start_blk_no += ((unsigned long long)1) <<
631f9a4e6dfSJaegeuk Kim 				le32_to_cpu(fsb->log_blocks_per_seg);
632127e670aSJaegeuk Kim 	cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
633127e670aSJaegeuk Kim 
634127e670aSJaegeuk Kim 	if (cp1 && cp2) {
635127e670aSJaegeuk Kim 		if (ver_after(cp2_version, cp1_version))
636127e670aSJaegeuk Kim 			cur_page = cp2;
637127e670aSJaegeuk Kim 		else
638127e670aSJaegeuk Kim 			cur_page = cp1;
639127e670aSJaegeuk Kim 	} else if (cp1) {
640127e670aSJaegeuk Kim 		cur_page = cp1;
641127e670aSJaegeuk Kim 	} else if (cp2) {
642127e670aSJaegeuk Kim 		cur_page = cp2;
643127e670aSJaegeuk Kim 	} else {
644127e670aSJaegeuk Kim 		goto fail_no_cp;
645127e670aSJaegeuk Kim 	}
646127e670aSJaegeuk Kim 
647127e670aSJaegeuk Kim 	cp_block = (struct f2fs_checkpoint *)page_address(cur_page);
648127e670aSJaegeuk Kim 	memcpy(sbi->ckpt, cp_block, blk_size);
649127e670aSJaegeuk Kim 
6501dbe4152SChangman Lee 	if (cp_blks <= 1)
6511dbe4152SChangman Lee 		goto done;
6521dbe4152SChangman Lee 
6531dbe4152SChangman Lee 	cp_blk_no = le32_to_cpu(fsb->cp_blkaddr);
6541dbe4152SChangman Lee 	if (cur_page == cp2)
6551dbe4152SChangman Lee 		cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg);
6561dbe4152SChangman Lee 
6571dbe4152SChangman Lee 	for (i = 1; i < cp_blks; i++) {
6581dbe4152SChangman Lee 		void *sit_bitmap_ptr;
6591dbe4152SChangman Lee 		unsigned char *ckpt = (unsigned char *)sbi->ckpt;
6601dbe4152SChangman Lee 
6611dbe4152SChangman Lee 		cur_page = get_meta_page(sbi, cp_blk_no + i);
6621dbe4152SChangman Lee 		sit_bitmap_ptr = page_address(cur_page);
6631dbe4152SChangman Lee 		memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size);
6641dbe4152SChangman Lee 		f2fs_put_page(cur_page, 1);
6651dbe4152SChangman Lee 	}
6661dbe4152SChangman Lee done:
667127e670aSJaegeuk Kim 	f2fs_put_page(cp1, 1);
668127e670aSJaegeuk Kim 	f2fs_put_page(cp2, 1);
669127e670aSJaegeuk Kim 	return 0;
670127e670aSJaegeuk Kim 
671127e670aSJaegeuk Kim fail_no_cp:
672127e670aSJaegeuk Kim 	kfree(sbi->ckpt);
673127e670aSJaegeuk Kim 	return -EINVAL;
674127e670aSJaegeuk Kim }
675127e670aSJaegeuk Kim 
6765deb8267SJaegeuk Kim static int __add_dirty_inode(struct inode *inode, struct dir_inode_entry *new)
677127e670aSJaegeuk Kim {
6784081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
6792d7b822aSChao Yu 
680ed57c27fSJaegeuk Kim 	if (is_inode_flag_set(F2FS_I(inode), FI_DIRTY_DIR))
6815deb8267SJaegeuk Kim 		return -EEXIST;
6822d7b822aSChao Yu 
683ed57c27fSJaegeuk Kim 	set_inode_flag(F2FS_I(inode), FI_DIRTY_DIR);
684ed57c27fSJaegeuk Kim 	F2FS_I(inode)->dirty_dir = new;
685ed57c27fSJaegeuk Kim 	list_add_tail(&new->list, &sbi->dir_inode_list);
686dcdfff65SJaegeuk Kim 	stat_inc_dirty_dir(sbi);
6875deb8267SJaegeuk Kim 	return 0;
6885deb8267SJaegeuk Kim }
6895deb8267SJaegeuk Kim 
690a7ffdbe2SJaegeuk Kim void update_dirty_page(struct inode *inode, struct page *page)
6915deb8267SJaegeuk Kim {
6924081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
6935deb8267SJaegeuk Kim 	struct dir_inode_entry *new;
694cf0ee0f0SChao Yu 	int ret = 0;
6955deb8267SJaegeuk Kim 
696a7ffdbe2SJaegeuk Kim 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode))
697127e670aSJaegeuk Kim 		return;
6987bd59381SGu Zheng 
699a7ffdbe2SJaegeuk Kim 	if (!S_ISDIR(inode->i_mode)) {
700a7ffdbe2SJaegeuk Kim 		inode_inc_dirty_pages(inode);
701a7ffdbe2SJaegeuk Kim 		goto out;
702a7ffdbe2SJaegeuk Kim 	}
703a7ffdbe2SJaegeuk Kim 
7047bd59381SGu Zheng 	new = f2fs_kmem_cache_alloc(inode_entry_slab, GFP_NOFS);
705127e670aSJaegeuk Kim 	new->inode = inode;
706127e670aSJaegeuk Kim 	INIT_LIST_HEAD(&new->list);
707127e670aSJaegeuk Kim 
708127e670aSJaegeuk Kim 	spin_lock(&sbi->dir_inode_lock);
709cf0ee0f0SChao Yu 	ret = __add_dirty_inode(inode, new);
710a7ffdbe2SJaegeuk Kim 	inode_inc_dirty_pages(inode);
7115deb8267SJaegeuk Kim 	spin_unlock(&sbi->dir_inode_lock);
712cf0ee0f0SChao Yu 
713cf0ee0f0SChao Yu 	if (ret)
714cf0ee0f0SChao Yu 		kmem_cache_free(inode_entry_slab, new);
715a7ffdbe2SJaegeuk Kim out:
716a7ffdbe2SJaegeuk Kim 	SetPagePrivate(page);
717*9e4ded3fSJaegeuk Kim 	f2fs_trace_pid(page);
7185deb8267SJaegeuk Kim }
719127e670aSJaegeuk Kim 
7205deb8267SJaegeuk Kim void add_dirty_dir_inode(struct inode *inode)
7215deb8267SJaegeuk Kim {
7224081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
7237bd59381SGu Zheng 	struct dir_inode_entry *new =
7247bd59381SGu Zheng 			f2fs_kmem_cache_alloc(inode_entry_slab, GFP_NOFS);
725cf0ee0f0SChao Yu 	int ret = 0;
7267bd59381SGu Zheng 
7275deb8267SJaegeuk Kim 	new->inode = inode;
7285deb8267SJaegeuk Kim 	INIT_LIST_HEAD(&new->list);
7295deb8267SJaegeuk Kim 
7305deb8267SJaegeuk Kim 	spin_lock(&sbi->dir_inode_lock);
731cf0ee0f0SChao Yu 	ret = __add_dirty_inode(inode, new);
732127e670aSJaegeuk Kim 	spin_unlock(&sbi->dir_inode_lock);
733cf0ee0f0SChao Yu 
734cf0ee0f0SChao Yu 	if (ret)
735cf0ee0f0SChao Yu 		kmem_cache_free(inode_entry_slab, new);
736127e670aSJaegeuk Kim }
737127e670aSJaegeuk Kim 
738127e670aSJaegeuk Kim void remove_dirty_dir_inode(struct inode *inode)
739127e670aSJaegeuk Kim {
7404081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
7412d7b822aSChao Yu 	struct dir_inode_entry *entry;
742127e670aSJaegeuk Kim 
743127e670aSJaegeuk Kim 	if (!S_ISDIR(inode->i_mode))
744127e670aSJaegeuk Kim 		return;
745127e670aSJaegeuk Kim 
746127e670aSJaegeuk Kim 	spin_lock(&sbi->dir_inode_lock);
747a7ffdbe2SJaegeuk Kim 	if (get_dirty_pages(inode) ||
748ed57c27fSJaegeuk Kim 			!is_inode_flag_set(F2FS_I(inode), FI_DIRTY_DIR)) {
7493b10b1fdSJaegeuk Kim 		spin_unlock(&sbi->dir_inode_lock);
7503b10b1fdSJaegeuk Kim 		return;
7513b10b1fdSJaegeuk Kim 	}
752127e670aSJaegeuk Kim 
753ed57c27fSJaegeuk Kim 	entry = F2FS_I(inode)->dirty_dir;
754127e670aSJaegeuk Kim 	list_del(&entry->list);
755ed57c27fSJaegeuk Kim 	F2FS_I(inode)->dirty_dir = NULL;
756ed57c27fSJaegeuk Kim 	clear_inode_flag(F2FS_I(inode), FI_DIRTY_DIR);
757dcdfff65SJaegeuk Kim 	stat_dec_dirty_dir(sbi);
758cf0ee0f0SChao Yu 	spin_unlock(&sbi->dir_inode_lock);
759cf0ee0f0SChao Yu 	kmem_cache_free(inode_entry_slab, entry);
76074d0b917SJaegeuk Kim 
76174d0b917SJaegeuk Kim 	/* Only from the recovery routine */
762afc3eda2SJaegeuk Kim 	if (is_inode_flag_set(F2FS_I(inode), FI_DELAY_IPUT)) {
763afc3eda2SJaegeuk Kim 		clear_inode_flag(F2FS_I(inode), FI_DELAY_IPUT);
76474d0b917SJaegeuk Kim 		iput(inode);
76574d0b917SJaegeuk Kim 	}
766afc3eda2SJaegeuk Kim }
76774d0b917SJaegeuk Kim 
768127e670aSJaegeuk Kim void sync_dirty_dir_inodes(struct f2fs_sb_info *sbi)
769127e670aSJaegeuk Kim {
770ce3b7d80SGu Zheng 	struct list_head *head;
771127e670aSJaegeuk Kim 	struct dir_inode_entry *entry;
772127e670aSJaegeuk Kim 	struct inode *inode;
773127e670aSJaegeuk Kim retry:
774af41d3eeSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
775af41d3eeSJaegeuk Kim 		return;
776af41d3eeSJaegeuk Kim 
777127e670aSJaegeuk Kim 	spin_lock(&sbi->dir_inode_lock);
778ce3b7d80SGu Zheng 
779ce3b7d80SGu Zheng 	head = &sbi->dir_inode_list;
780127e670aSJaegeuk Kim 	if (list_empty(head)) {
781127e670aSJaegeuk Kim 		spin_unlock(&sbi->dir_inode_lock);
782127e670aSJaegeuk Kim 		return;
783127e670aSJaegeuk Kim 	}
784127e670aSJaegeuk Kim 	entry = list_entry(head->next, struct dir_inode_entry, list);
785127e670aSJaegeuk Kim 	inode = igrab(entry->inode);
786127e670aSJaegeuk Kim 	spin_unlock(&sbi->dir_inode_lock);
787127e670aSJaegeuk Kim 	if (inode) {
78887d6f890SJaegeuk Kim 		filemap_fdatawrite(inode->i_mapping);
789127e670aSJaegeuk Kim 		iput(inode);
790127e670aSJaegeuk Kim 	} else {
791127e670aSJaegeuk Kim 		/*
792127e670aSJaegeuk Kim 		 * We should submit bio, since it exists several
793127e670aSJaegeuk Kim 		 * wribacking dentry pages in the freeing inode.
794127e670aSJaegeuk Kim 		 */
795458e6197SJaegeuk Kim 		f2fs_submit_merged_bio(sbi, DATA, WRITE);
796127e670aSJaegeuk Kim 	}
797127e670aSJaegeuk Kim 	goto retry;
798127e670aSJaegeuk Kim }
799127e670aSJaegeuk Kim 
8000a8165d7SJaegeuk Kim /*
801127e670aSJaegeuk Kim  * Freeze all the FS-operations for checkpoint.
802127e670aSJaegeuk Kim  */
803cf779cabSJaegeuk Kim static int block_operations(struct f2fs_sb_info *sbi)
804127e670aSJaegeuk Kim {
805127e670aSJaegeuk Kim 	struct writeback_control wbc = {
806127e670aSJaegeuk Kim 		.sync_mode = WB_SYNC_ALL,
807127e670aSJaegeuk Kim 		.nr_to_write = LONG_MAX,
808127e670aSJaegeuk Kim 		.for_reclaim = 0,
809127e670aSJaegeuk Kim 	};
810c718379bSJaegeuk Kim 	struct blk_plug plug;
811cf779cabSJaegeuk Kim 	int err = 0;
812c718379bSJaegeuk Kim 
813c718379bSJaegeuk Kim 	blk_start_plug(&plug);
814c718379bSJaegeuk Kim 
81539936837SJaegeuk Kim retry_flush_dents:
816e479556bSGu Zheng 	f2fs_lock_all(sbi);
817127e670aSJaegeuk Kim 	/* write all the dirty dentry pages */
818127e670aSJaegeuk Kim 	if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
819e479556bSGu Zheng 		f2fs_unlock_all(sbi);
82039936837SJaegeuk Kim 		sync_dirty_dir_inodes(sbi);
821cf779cabSJaegeuk Kim 		if (unlikely(f2fs_cp_error(sbi))) {
822cf779cabSJaegeuk Kim 			err = -EIO;
823cf779cabSJaegeuk Kim 			goto out;
824cf779cabSJaegeuk Kim 		}
82539936837SJaegeuk Kim 		goto retry_flush_dents;
826127e670aSJaegeuk Kim 	}
827127e670aSJaegeuk Kim 
828127e670aSJaegeuk Kim 	/*
829e1c42045Sarter97 	 * POR: we should ensure that there are no dirty node pages
830127e670aSJaegeuk Kim 	 * until finishing nat/sit flush.
831127e670aSJaegeuk Kim 	 */
83239936837SJaegeuk Kim retry_flush_nodes:
833b3582c68SChao Yu 	down_write(&sbi->node_write);
834127e670aSJaegeuk Kim 
835127e670aSJaegeuk Kim 	if (get_pages(sbi, F2FS_DIRTY_NODES)) {
836b3582c68SChao Yu 		up_write(&sbi->node_write);
83739936837SJaegeuk Kim 		sync_node_pages(sbi, 0, &wbc);
838cf779cabSJaegeuk Kim 		if (unlikely(f2fs_cp_error(sbi))) {
839cf779cabSJaegeuk Kim 			f2fs_unlock_all(sbi);
840cf779cabSJaegeuk Kim 			err = -EIO;
841cf779cabSJaegeuk Kim 			goto out;
842cf779cabSJaegeuk Kim 		}
84339936837SJaegeuk Kim 		goto retry_flush_nodes;
844127e670aSJaegeuk Kim 	}
845cf779cabSJaegeuk Kim out:
846c718379bSJaegeuk Kim 	blk_finish_plug(&plug);
847cf779cabSJaegeuk Kim 	return err;
848127e670aSJaegeuk Kim }
849127e670aSJaegeuk Kim 
850127e670aSJaegeuk Kim static void unblock_operations(struct f2fs_sb_info *sbi)
851127e670aSJaegeuk Kim {
852b3582c68SChao Yu 	up_write(&sbi->node_write);
853e479556bSGu Zheng 	f2fs_unlock_all(sbi);
854127e670aSJaegeuk Kim }
855127e670aSJaegeuk Kim 
856fb51b5efSChangman Lee static void wait_on_all_pages_writeback(struct f2fs_sb_info *sbi)
857fb51b5efSChangman Lee {
858fb51b5efSChangman Lee 	DEFINE_WAIT(wait);
859fb51b5efSChangman Lee 
860fb51b5efSChangman Lee 	for (;;) {
861fb51b5efSChangman Lee 		prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
862fb51b5efSChangman Lee 
863fb51b5efSChangman Lee 		if (!get_pages(sbi, F2FS_WRITEBACK))
864fb51b5efSChangman Lee 			break;
865fb51b5efSChangman Lee 
866fb51b5efSChangman Lee 		io_schedule();
867fb51b5efSChangman Lee 	}
868fb51b5efSChangman Lee 	finish_wait(&sbi->cp_wait, &wait);
869fb51b5efSChangman Lee }
870fb51b5efSChangman Lee 
87175ab4cb8SJaegeuk Kim static void do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
872127e670aSJaegeuk Kim {
873127e670aSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
874cf2271e7SJaegeuk Kim 	struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
87577041823SHuang Ying 	struct f2fs_nm_info *nm_i = NM_I(sbi);
87667298804SChao Yu 	unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num;
87777041823SHuang Ying 	nid_t last_nid = nm_i->next_scan_nid;
878127e670aSJaegeuk Kim 	block_t start_blk;
879127e670aSJaegeuk Kim 	struct page *cp_page;
880127e670aSJaegeuk Kim 	unsigned int data_sum_blocks, orphan_blocks;
8817e586fa0SJaegeuk Kim 	__u32 crc32 = 0;
882127e670aSJaegeuk Kim 	void *kaddr;
883127e670aSJaegeuk Kim 	int i;
8841dbe4152SChangman Lee 	int cp_payload_blks = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
885127e670aSJaegeuk Kim 
8861e87a78dSJaegeuk Kim 	/*
8871e87a78dSJaegeuk Kim 	 * This avoids to conduct wrong roll-forward operations and uses
8881e87a78dSJaegeuk Kim 	 * metapages, so should be called prior to sync_meta_pages below.
8891e87a78dSJaegeuk Kim 	 */
890cf2271e7SJaegeuk Kim 	discard_next_dnode(sbi, NEXT_FREE_BLKADDR(sbi, curseg));
891127e670aSJaegeuk Kim 
892127e670aSJaegeuk Kim 	/* Flush all the NAT/SIT pages */
893cf779cabSJaegeuk Kim 	while (get_pages(sbi, F2FS_DIRTY_META)) {
894127e670aSJaegeuk Kim 		sync_meta_pages(sbi, META, LONG_MAX);
895cf779cabSJaegeuk Kim 		if (unlikely(f2fs_cp_error(sbi)))
896cf779cabSJaegeuk Kim 			return;
897cf779cabSJaegeuk Kim 	}
898127e670aSJaegeuk Kim 
899127e670aSJaegeuk Kim 	next_free_nid(sbi, &last_nid);
900127e670aSJaegeuk Kim 
901127e670aSJaegeuk Kim 	/*
902127e670aSJaegeuk Kim 	 * modify checkpoint
903127e670aSJaegeuk Kim 	 * version number is already updated
904127e670aSJaegeuk Kim 	 */
905127e670aSJaegeuk Kim 	ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi));
906127e670aSJaegeuk Kim 	ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
907127e670aSJaegeuk Kim 	ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
908b5b82205SChao Yu 	for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
909127e670aSJaegeuk Kim 		ckpt->cur_node_segno[i] =
910127e670aSJaegeuk Kim 			cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE));
911127e670aSJaegeuk Kim 		ckpt->cur_node_blkoff[i] =
912127e670aSJaegeuk Kim 			cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE));
913127e670aSJaegeuk Kim 		ckpt->alloc_type[i + CURSEG_HOT_NODE] =
914127e670aSJaegeuk Kim 				curseg_alloc_type(sbi, i + CURSEG_HOT_NODE);
915127e670aSJaegeuk Kim 	}
916b5b82205SChao Yu 	for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
917127e670aSJaegeuk Kim 		ckpt->cur_data_segno[i] =
918127e670aSJaegeuk Kim 			cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA));
919127e670aSJaegeuk Kim 		ckpt->cur_data_blkoff[i] =
920127e670aSJaegeuk Kim 			cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA));
921127e670aSJaegeuk Kim 		ckpt->alloc_type[i + CURSEG_HOT_DATA] =
922127e670aSJaegeuk Kim 				curseg_alloc_type(sbi, i + CURSEG_HOT_DATA);
923127e670aSJaegeuk Kim 	}
924127e670aSJaegeuk Kim 
925127e670aSJaegeuk Kim 	ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi));
926127e670aSJaegeuk Kim 	ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi));
927127e670aSJaegeuk Kim 	ckpt->next_free_nid = cpu_to_le32(last_nid);
928127e670aSJaegeuk Kim 
929127e670aSJaegeuk Kim 	/* 2 cp  + n data seg summary + orphan inode blocks */
9303fa06d7bSChao Yu 	data_sum_blocks = npages_for_summary_flush(sbi, false);
931b5b82205SChao Yu 	if (data_sum_blocks < NR_CURSEG_DATA_TYPE)
93225ca923bSJaegeuk Kim 		set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
933127e670aSJaegeuk Kim 	else
93425ca923bSJaegeuk Kim 		clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
935127e670aSJaegeuk Kim 
93667298804SChao Yu 	orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num);
9371dbe4152SChangman Lee 	ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks +
9381dbe4152SChangman Lee 			orphan_blocks);
939127e670aSJaegeuk Kim 
94075ab4cb8SJaegeuk Kim 	if (cpc->reason == CP_UMOUNT) {
94125ca923bSJaegeuk Kim 		set_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
942b5b82205SChao Yu 		ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS+
9431dbe4152SChangman Lee 				cp_payload_blks + data_sum_blocks +
9441dbe4152SChangman Lee 				orphan_blocks + NR_CURSEG_NODE_TYPE);
945127e670aSJaegeuk Kim 	} else {
94625ca923bSJaegeuk Kim 		clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
947b5b82205SChao Yu 		ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
9481dbe4152SChangman Lee 				cp_payload_blks + data_sum_blocks +
9491dbe4152SChangman Lee 				orphan_blocks);
950127e670aSJaegeuk Kim 	}
951127e670aSJaegeuk Kim 
95267298804SChao Yu 	if (orphan_num)
95325ca923bSJaegeuk Kim 		set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
954127e670aSJaegeuk Kim 	else
95525ca923bSJaegeuk Kim 		clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
956127e670aSJaegeuk Kim 
9572ae4c673SJaegeuk Kim 	if (sbi->need_fsck)
9582ae4c673SJaegeuk Kim 		set_ckpt_flags(ckpt, CP_FSCK_FLAG);
9592ae4c673SJaegeuk Kim 
960127e670aSJaegeuk Kim 	/* update SIT/NAT bitmap */
961127e670aSJaegeuk Kim 	get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
962127e670aSJaegeuk Kim 	get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
963127e670aSJaegeuk Kim 
964127e670aSJaegeuk Kim 	crc32 = f2fs_crc32(ckpt, le32_to_cpu(ckpt->checksum_offset));
9657e586fa0SJaegeuk Kim 	*((__le32 *)((unsigned char *)ckpt +
9667e586fa0SJaegeuk Kim 				le32_to_cpu(ckpt->checksum_offset)))
967127e670aSJaegeuk Kim 				= cpu_to_le32(crc32);
968127e670aSJaegeuk Kim 
969127e670aSJaegeuk Kim 	start_blk = __start_cp_addr(sbi);
970127e670aSJaegeuk Kim 
971127e670aSJaegeuk Kim 	/* write out checkpoint buffer at block 0 */
972127e670aSJaegeuk Kim 	cp_page = grab_meta_page(sbi, start_blk++);
973127e670aSJaegeuk Kim 	kaddr = page_address(cp_page);
974127e670aSJaegeuk Kim 	memcpy(kaddr, ckpt, (1 << sbi->log_blocksize));
975127e670aSJaegeuk Kim 	set_page_dirty(cp_page);
976127e670aSJaegeuk Kim 	f2fs_put_page(cp_page, 1);
977127e670aSJaegeuk Kim 
9781dbe4152SChangman Lee 	for (i = 1; i < 1 + cp_payload_blks; i++) {
9791dbe4152SChangman Lee 		cp_page = grab_meta_page(sbi, start_blk++);
9801dbe4152SChangman Lee 		kaddr = page_address(cp_page);
9811dbe4152SChangman Lee 		memcpy(kaddr, (char *)ckpt + i * F2FS_BLKSIZE,
9821dbe4152SChangman Lee 				(1 << sbi->log_blocksize));
9831dbe4152SChangman Lee 		set_page_dirty(cp_page);
9841dbe4152SChangman Lee 		f2fs_put_page(cp_page, 1);
9851dbe4152SChangman Lee 	}
9861dbe4152SChangman Lee 
98767298804SChao Yu 	if (orphan_num) {
988127e670aSJaegeuk Kim 		write_orphan_inodes(sbi, start_blk);
989127e670aSJaegeuk Kim 		start_blk += orphan_blocks;
990127e670aSJaegeuk Kim 	}
991127e670aSJaegeuk Kim 
992127e670aSJaegeuk Kim 	write_data_summaries(sbi, start_blk);
993127e670aSJaegeuk Kim 	start_blk += data_sum_blocks;
99475ab4cb8SJaegeuk Kim 	if (cpc->reason == CP_UMOUNT) {
995127e670aSJaegeuk Kim 		write_node_summaries(sbi, start_blk);
996127e670aSJaegeuk Kim 		start_blk += NR_CURSEG_NODE_TYPE;
997127e670aSJaegeuk Kim 	}
998127e670aSJaegeuk Kim 
999127e670aSJaegeuk Kim 	/* writeout checkpoint block */
1000127e670aSJaegeuk Kim 	cp_page = grab_meta_page(sbi, start_blk);
1001127e670aSJaegeuk Kim 	kaddr = page_address(cp_page);
1002127e670aSJaegeuk Kim 	memcpy(kaddr, ckpt, (1 << sbi->log_blocksize));
1003127e670aSJaegeuk Kim 	set_page_dirty(cp_page);
1004127e670aSJaegeuk Kim 	f2fs_put_page(cp_page, 1);
1005127e670aSJaegeuk Kim 
1006127e670aSJaegeuk Kim 	/* wait for previous submitted node/meta pages writeback */
1007fb51b5efSChangman Lee 	wait_on_all_pages_writeback(sbi);
1008127e670aSJaegeuk Kim 
1009cf779cabSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
1010cf779cabSJaegeuk Kim 		return;
1011cf779cabSJaegeuk Kim 
10124ef51a8fSJaegeuk Kim 	filemap_fdatawait_range(NODE_MAPPING(sbi), 0, LONG_MAX);
10139df27d98SGu Zheng 	filemap_fdatawait_range(META_MAPPING(sbi), 0, LONG_MAX);
1014127e670aSJaegeuk Kim 
1015127e670aSJaegeuk Kim 	/* update user_block_counts */
1016127e670aSJaegeuk Kim 	sbi->last_valid_block_count = sbi->total_valid_block_count;
1017127e670aSJaegeuk Kim 	sbi->alloc_valid_block_count = 0;
1018127e670aSJaegeuk Kim 
1019127e670aSJaegeuk Kim 	/* Here, we only have one bio having CP pack */
1020127e670aSJaegeuk Kim 	sync_meta_pages(sbi, META_FLUSH, LONG_MAX);
1021127e670aSJaegeuk Kim 
10226a8f8ca5SJaegeuk Kim 	/* wait for previous submitted meta pages writeback */
10236a8f8ca5SJaegeuk Kim 	wait_on_all_pages_writeback(sbi);
10246a8f8ca5SJaegeuk Kim 
1025fff04f90SJaegeuk Kim 	release_dirty_inode(sbi);
1026cf779cabSJaegeuk Kim 
1027cf779cabSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
1028cf779cabSJaegeuk Kim 		return;
1029cf779cabSJaegeuk Kim 
1030cf779cabSJaegeuk Kim 	clear_prefree_segments(sbi);
1031127e670aSJaegeuk Kim 	F2FS_RESET_SB_DIRT(sbi);
1032127e670aSJaegeuk Kim }
1033127e670aSJaegeuk Kim 
10340a8165d7SJaegeuk Kim /*
1035e1c42045Sarter97  * We guarantee that this checkpoint procedure will not fail.
1036127e670aSJaegeuk Kim  */
103775ab4cb8SJaegeuk Kim void write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1038127e670aSJaegeuk Kim {
1039127e670aSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1040127e670aSJaegeuk Kim 	unsigned long long ckpt_ver;
1041127e670aSJaegeuk Kim 
104275ab4cb8SJaegeuk Kim 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops");
10432af4bd6cSNamjae Jeon 
1044127e670aSJaegeuk Kim 	mutex_lock(&sbi->cp_mutex);
10458501017eSJaegeuk Kim 
10464b2fecc8SJaegeuk Kim 	if (!sbi->s_dirty && cpc->reason != CP_DISCARD)
10478501017eSJaegeuk Kim 		goto out;
1048cf779cabSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
1049cf779cabSJaegeuk Kim 		goto out;
1050cf779cabSJaegeuk Kim 	if (block_operations(sbi))
1051cf779cabSJaegeuk Kim 		goto out;
1052127e670aSJaegeuk Kim 
105375ab4cb8SJaegeuk Kim 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops");
10542af4bd6cSNamjae Jeon 
1055458e6197SJaegeuk Kim 	f2fs_submit_merged_bio(sbi, DATA, WRITE);
1056458e6197SJaegeuk Kim 	f2fs_submit_merged_bio(sbi, NODE, WRITE);
1057458e6197SJaegeuk Kim 	f2fs_submit_merged_bio(sbi, META, WRITE);
1058127e670aSJaegeuk Kim 
1059127e670aSJaegeuk Kim 	/*
1060127e670aSJaegeuk Kim 	 * update checkpoint pack index
1061127e670aSJaegeuk Kim 	 * Increase the version number so that
1062127e670aSJaegeuk Kim 	 * SIT entries and seg summaries are written at correct place
1063127e670aSJaegeuk Kim 	 */
1064d71b5564SJaegeuk Kim 	ckpt_ver = cur_cp_version(ckpt);
1065127e670aSJaegeuk Kim 	ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
1066127e670aSJaegeuk Kim 
1067127e670aSJaegeuk Kim 	/* write cached NAT/SIT entries to NAT/SIT area */
1068127e670aSJaegeuk Kim 	flush_nat_entries(sbi);
10694b2fecc8SJaegeuk Kim 	flush_sit_entries(sbi, cpc);
1070127e670aSJaegeuk Kim 
1071127e670aSJaegeuk Kim 	/* unlock all the fs_lock[] in do_checkpoint() */
107275ab4cb8SJaegeuk Kim 	do_checkpoint(sbi, cpc);
1073127e670aSJaegeuk Kim 
1074127e670aSJaegeuk Kim 	unblock_operations(sbi);
1075942e0be6SChangman Lee 	stat_inc_cp_count(sbi->stat_info);
10768501017eSJaegeuk Kim out:
10778501017eSJaegeuk Kim 	mutex_unlock(&sbi->cp_mutex);
107875ab4cb8SJaegeuk Kim 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint");
1079127e670aSJaegeuk Kim }
1080127e670aSJaegeuk Kim 
10816451e041SJaegeuk Kim void init_ino_entry_info(struct f2fs_sb_info *sbi)
1082127e670aSJaegeuk Kim {
10836451e041SJaegeuk Kim 	int i;
10846451e041SJaegeuk Kim 
10856451e041SJaegeuk Kim 	for (i = 0; i < MAX_INO_ENTRY; i++) {
108667298804SChao Yu 		struct inode_management *im = &sbi->im[i];
108767298804SChao Yu 
108867298804SChao Yu 		INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC);
108967298804SChao Yu 		spin_lock_init(&im->ino_lock);
109067298804SChao Yu 		INIT_LIST_HEAD(&im->ino_list);
109167298804SChao Yu 		im->ino_num = 0;
10926451e041SJaegeuk Kim 	}
10936451e041SJaegeuk Kim 
10940d47c1adSGu Zheng 	/*
10950d47c1adSGu Zheng 	 * considering 512 blocks in a segment 8 blocks are needed for cp
10960d47c1adSGu Zheng 	 * and log segment summaries. Remaining blocks are used to keep
10970d47c1adSGu Zheng 	 * orphan entries with the limitation one reserved segment
10980d47c1adSGu Zheng 	 * for cp pack we can have max 1020*504 orphan entries
10990d47c1adSGu Zheng 	 */
1100b5b82205SChao Yu 	sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS -
1101b5b82205SChao Yu 			NR_CURSEG_TYPE) * F2FS_ORPHANS_PER_BLOCK;
1102127e670aSJaegeuk Kim }
1103127e670aSJaegeuk Kim 
11046e6093a8SNamjae Jeon int __init create_checkpoint_caches(void)
1105127e670aSJaegeuk Kim {
11066451e041SJaegeuk Kim 	ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry",
11076451e041SJaegeuk Kim 			sizeof(struct ino_entry));
11086451e041SJaegeuk Kim 	if (!ino_entry_slab)
1109127e670aSJaegeuk Kim 		return -ENOMEM;
1110127e670aSJaegeuk Kim 	inode_entry_slab = f2fs_kmem_cache_create("f2fs_dirty_dir_entry",
1111e8512d2eSGu Zheng 			sizeof(struct dir_inode_entry));
11126bacf52fSJaegeuk Kim 	if (!inode_entry_slab) {
11136451e041SJaegeuk Kim 		kmem_cache_destroy(ino_entry_slab);
1114127e670aSJaegeuk Kim 		return -ENOMEM;
1115127e670aSJaegeuk Kim 	}
1116127e670aSJaegeuk Kim 	return 0;
1117127e670aSJaegeuk Kim }
1118127e670aSJaegeuk Kim 
1119127e670aSJaegeuk Kim void destroy_checkpoint_caches(void)
1120127e670aSJaegeuk Kim {
11216451e041SJaegeuk Kim 	kmem_cache_destroy(ino_entry_slab);
1122127e670aSJaegeuk Kim 	kmem_cache_destroy(inode_entry_slab);
1123127e670aSJaegeuk Kim }
1124