xref: /openbmc/linux/fs/f2fs/checkpoint.c (revision 3c64298579a1343cbdf3d2f17adf774a58546aae)
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  */
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 
193caf0047eSChao Yu 	if (unlikely(is_sbi_flag_set(sbi, 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 
27997dc3fd2SChao Yu 			if (mapping->a_ops->writepage(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);
3051601839eSChao Yu 		SetPagePrivate(page);
3069e4ded3fSJaegeuk Kim 		f2fs_trace_pid(page);
307127e670aSJaegeuk Kim 		return 1;
308127e670aSJaegeuk Kim 	}
309127e670aSJaegeuk Kim 	return 0;
310127e670aSJaegeuk Kim }
311127e670aSJaegeuk Kim 
312127e670aSJaegeuk Kim const struct address_space_operations f2fs_meta_aops = {
313127e670aSJaegeuk Kim 	.writepage	= f2fs_write_meta_page,
314127e670aSJaegeuk Kim 	.writepages	= f2fs_write_meta_pages,
315127e670aSJaegeuk Kim 	.set_page_dirty	= f2fs_set_meta_page_dirty,
316487261f3SChao Yu 	.invalidatepage = f2fs_invalidate_page,
317487261f3SChao Yu 	.releasepage	= f2fs_release_page,
318127e670aSJaegeuk Kim };
319127e670aSJaegeuk Kim 
3206451e041SJaegeuk Kim static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
321953e6cc6SJaegeuk Kim {
32267298804SChao Yu 	struct inode_management *im = &sbi->im[type];
32339efac41SJaegeuk Kim 	struct ino_entry *e;
32439efac41SJaegeuk Kim retry:
325769ec6e5SJaegeuk Kim 	if (radix_tree_preload(GFP_NOFS)) {
326769ec6e5SJaegeuk Kim 		cond_resched();
327769ec6e5SJaegeuk Kim 		goto retry;
328769ec6e5SJaegeuk Kim 	}
329769ec6e5SJaegeuk Kim 
33067298804SChao Yu 	spin_lock(&im->ino_lock);
331953e6cc6SJaegeuk Kim 
33267298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
33339efac41SJaegeuk Kim 	if (!e) {
33439efac41SJaegeuk Kim 		e = kmem_cache_alloc(ino_entry_slab, GFP_ATOMIC);
33539efac41SJaegeuk Kim 		if (!e) {
33667298804SChao Yu 			spin_unlock(&im->ino_lock);
337769ec6e5SJaegeuk Kim 			radix_tree_preload_end();
33839efac41SJaegeuk Kim 			goto retry;
33939efac41SJaegeuk Kim 		}
34067298804SChao Yu 		if (radix_tree_insert(&im->ino_root, ino, e)) {
34167298804SChao Yu 			spin_unlock(&im->ino_lock);
34239efac41SJaegeuk Kim 			kmem_cache_free(ino_entry_slab, e);
343769ec6e5SJaegeuk Kim 			radix_tree_preload_end();
34439efac41SJaegeuk Kim 			goto retry;
34539efac41SJaegeuk Kim 		}
34639efac41SJaegeuk Kim 		memset(e, 0, sizeof(struct ino_entry));
34739efac41SJaegeuk Kim 		e->ino = ino;
34839efac41SJaegeuk Kim 
34967298804SChao Yu 		list_add_tail(&e->list, &im->ino_list);
3508c402946SJaegeuk Kim 		if (type != ORPHAN_INO)
35167298804SChao Yu 			im->ino_num++;
35239efac41SJaegeuk Kim 	}
35367298804SChao Yu 	spin_unlock(&im->ino_lock);
354769ec6e5SJaegeuk Kim 	radix_tree_preload_end();
355953e6cc6SJaegeuk Kim }
356953e6cc6SJaegeuk Kim 
3576451e041SJaegeuk Kim static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
358953e6cc6SJaegeuk Kim {
35967298804SChao Yu 	struct inode_management *im = &sbi->im[type];
3606451e041SJaegeuk Kim 	struct ino_entry *e;
361953e6cc6SJaegeuk Kim 
36267298804SChao Yu 	spin_lock(&im->ino_lock);
36367298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
36439efac41SJaegeuk Kim 	if (e) {
365953e6cc6SJaegeuk Kim 		list_del(&e->list);
36667298804SChao Yu 		radix_tree_delete(&im->ino_root, ino);
36767298804SChao Yu 		im->ino_num--;
36867298804SChao Yu 		spin_unlock(&im->ino_lock);
3696451e041SJaegeuk Kim 		kmem_cache_free(ino_entry_slab, e);
370953e6cc6SJaegeuk Kim 		return;
371953e6cc6SJaegeuk Kim 	}
37267298804SChao Yu 	spin_unlock(&im->ino_lock);
373953e6cc6SJaegeuk Kim }
374953e6cc6SJaegeuk Kim 
375fff04f90SJaegeuk Kim void add_dirty_inode(struct f2fs_sb_info *sbi, nid_t ino, int type)
376fff04f90SJaegeuk Kim {
377fff04f90SJaegeuk Kim 	/* add new dirty ino entry into list */
378fff04f90SJaegeuk Kim 	__add_ino_entry(sbi, ino, type);
379fff04f90SJaegeuk Kim }
380fff04f90SJaegeuk Kim 
381fff04f90SJaegeuk Kim void remove_dirty_inode(struct f2fs_sb_info *sbi, nid_t ino, int type)
382fff04f90SJaegeuk Kim {
383fff04f90SJaegeuk Kim 	/* remove dirty ino entry from list */
384fff04f90SJaegeuk Kim 	__remove_ino_entry(sbi, ino, type);
385fff04f90SJaegeuk Kim }
386fff04f90SJaegeuk Kim 
387fff04f90SJaegeuk Kim /* mode should be APPEND_INO or UPDATE_INO */
388fff04f90SJaegeuk Kim bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode)
389fff04f90SJaegeuk Kim {
39067298804SChao Yu 	struct inode_management *im = &sbi->im[mode];
391fff04f90SJaegeuk Kim 	struct ino_entry *e;
39267298804SChao Yu 
39367298804SChao Yu 	spin_lock(&im->ino_lock);
39467298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
39567298804SChao Yu 	spin_unlock(&im->ino_lock);
396fff04f90SJaegeuk Kim 	return e ? true : false;
397fff04f90SJaegeuk Kim }
398fff04f90SJaegeuk Kim 
3996f12ac25SJaegeuk Kim void release_dirty_inode(struct f2fs_sb_info *sbi)
400fff04f90SJaegeuk Kim {
401fff04f90SJaegeuk Kim 	struct ino_entry *e, *tmp;
402fff04f90SJaegeuk Kim 	int i;
403fff04f90SJaegeuk Kim 
404fff04f90SJaegeuk Kim 	for (i = APPEND_INO; i <= UPDATE_INO; i++) {
40567298804SChao Yu 		struct inode_management *im = &sbi->im[i];
40667298804SChao Yu 
40767298804SChao Yu 		spin_lock(&im->ino_lock);
40867298804SChao Yu 		list_for_each_entry_safe(e, tmp, &im->ino_list, list) {
409fff04f90SJaegeuk Kim 			list_del(&e->list);
41067298804SChao Yu 			radix_tree_delete(&im->ino_root, e->ino);
411fff04f90SJaegeuk Kim 			kmem_cache_free(ino_entry_slab, e);
41267298804SChao Yu 			im->ino_num--;
413fff04f90SJaegeuk Kim 		}
41467298804SChao Yu 		spin_unlock(&im->ino_lock);
415fff04f90SJaegeuk Kim 	}
416fff04f90SJaegeuk Kim }
417fff04f90SJaegeuk Kim 
418cbd56e7dSJaegeuk Kim int acquire_orphan_inode(struct f2fs_sb_info *sbi)
419127e670aSJaegeuk Kim {
42067298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
421127e670aSJaegeuk Kim 	int err = 0;
422127e670aSJaegeuk Kim 
42367298804SChao Yu 	spin_lock(&im->ino_lock);
42467298804SChao Yu 	if (unlikely(im->ino_num >= sbi->max_orphans))
425127e670aSJaegeuk Kim 		err = -ENOSPC;
426cbd56e7dSJaegeuk Kim 	else
42767298804SChao Yu 		im->ino_num++;
42867298804SChao Yu 	spin_unlock(&im->ino_lock);
4290d47c1adSGu Zheng 
430127e670aSJaegeuk Kim 	return err;
431127e670aSJaegeuk Kim }
432127e670aSJaegeuk Kim 
433cbd56e7dSJaegeuk Kim void release_orphan_inode(struct f2fs_sb_info *sbi)
434cbd56e7dSJaegeuk Kim {
43567298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
43667298804SChao Yu 
43767298804SChao Yu 	spin_lock(&im->ino_lock);
43867298804SChao Yu 	f2fs_bug_on(sbi, im->ino_num == 0);
43967298804SChao Yu 	im->ino_num--;
44067298804SChao Yu 	spin_unlock(&im->ino_lock);
441cbd56e7dSJaegeuk Kim }
442cbd56e7dSJaegeuk Kim 
443127e670aSJaegeuk Kim void add_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
444127e670aSJaegeuk Kim {
44539efac41SJaegeuk Kim 	/* add new orphan ino entry into list */
4466451e041SJaegeuk Kim 	__add_ino_entry(sbi, ino, ORPHAN_INO);
447127e670aSJaegeuk Kim }
448127e670aSJaegeuk Kim 
449127e670aSJaegeuk Kim void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
450127e670aSJaegeuk Kim {
451953e6cc6SJaegeuk Kim 	/* remove orphan entry from orphan list */
4526451e041SJaegeuk Kim 	__remove_ino_entry(sbi, ino, ORPHAN_INO);
453127e670aSJaegeuk Kim }
454127e670aSJaegeuk Kim 
455127e670aSJaegeuk Kim static void recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
456127e670aSJaegeuk Kim {
457127e670aSJaegeuk Kim 	struct inode *inode = f2fs_iget(sbi->sb, ino);
4589850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, IS_ERR(inode));
459127e670aSJaegeuk Kim 	clear_nlink(inode);
460127e670aSJaegeuk Kim 
461127e670aSJaegeuk Kim 	/* truncate all the data during iput */
462127e670aSJaegeuk Kim 	iput(inode);
463127e670aSJaegeuk Kim }
464127e670aSJaegeuk Kim 
4658f99a946SChao Yu void recover_orphan_inodes(struct f2fs_sb_info *sbi)
466127e670aSJaegeuk Kim {
467*3c642985SWanpeng Li 	block_t start_blk, orphan_blocks, i, j;
468127e670aSJaegeuk Kim 
46925ca923bSJaegeuk Kim 	if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG))
4708f99a946SChao Yu 		return;
471127e670aSJaegeuk Kim 
472caf0047eSChao Yu 	set_sbi_flag(sbi, SBI_POR_DOING);
4731dbe4152SChangman Lee 
47455141486SWanpeng Li 	start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
475*3c642985SWanpeng Li 	orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
476127e670aSJaegeuk Kim 
477*3c642985SWanpeng Li 	ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP);
478662befdaSChao Yu 
479*3c642985SWanpeng Li 	for (i = 0; i < orphan_blocks; i++) {
480127e670aSJaegeuk Kim 		struct page *page = get_meta_page(sbi, start_blk + i);
481127e670aSJaegeuk Kim 		struct f2fs_orphan_block *orphan_blk;
482127e670aSJaegeuk Kim 
483127e670aSJaegeuk Kim 		orphan_blk = (struct f2fs_orphan_block *)page_address(page);
484127e670aSJaegeuk Kim 		for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
485127e670aSJaegeuk Kim 			nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
486127e670aSJaegeuk Kim 			recover_orphan_inode(sbi, ino);
487127e670aSJaegeuk Kim 		}
488127e670aSJaegeuk Kim 		f2fs_put_page(page, 1);
489127e670aSJaegeuk Kim 	}
490127e670aSJaegeuk Kim 	/* clear Orphan Flag */
49125ca923bSJaegeuk Kim 	clear_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG);
492caf0047eSChao Yu 	clear_sbi_flag(sbi, SBI_POR_DOING);
4938f99a946SChao Yu 	return;
494127e670aSJaegeuk Kim }
495127e670aSJaegeuk Kim 
496127e670aSJaegeuk Kim static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
497127e670aSJaegeuk Kim {
498502c6e0bSGu Zheng 	struct list_head *head;
499127e670aSJaegeuk Kim 	struct f2fs_orphan_block *orphan_blk = NULL;
500127e670aSJaegeuk Kim 	unsigned int nentries = 0;
5014531929eSGu Zheng 	unsigned short index;
5028c402946SJaegeuk Kim 	unsigned short orphan_blocks;
5034531929eSGu Zheng 	struct page *page = NULL;
5046451e041SJaegeuk Kim 	struct ino_entry *orphan = NULL;
50567298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
506127e670aSJaegeuk Kim 
50767298804SChao Yu 	orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num);
5088c402946SJaegeuk Kim 
5094531929eSGu Zheng 	for (index = 0; index < orphan_blocks; index++)
51063f5384cSGu Zheng 		grab_meta_page(sbi, start_blk + index);
511127e670aSJaegeuk Kim 
5124531929eSGu Zheng 	index = 1;
51367298804SChao Yu 	spin_lock(&im->ino_lock);
51467298804SChao Yu 	head = &im->ino_list;
515127e670aSJaegeuk Kim 
516127e670aSJaegeuk Kim 	/* loop for each orphan inode entry and write them in Jornal block */
517502c6e0bSGu Zheng 	list_for_each_entry(orphan, head, list) {
518502c6e0bSGu Zheng 		if (!page) {
51963f5384cSGu Zheng 			page = find_get_page(META_MAPPING(sbi), start_blk++);
5209850cf4aSJaegeuk Kim 			f2fs_bug_on(sbi, !page);
521502c6e0bSGu Zheng 			orphan_blk =
522502c6e0bSGu Zheng 				(struct f2fs_orphan_block *)page_address(page);
523502c6e0bSGu Zheng 			memset(orphan_blk, 0, sizeof(*orphan_blk));
52463f5384cSGu Zheng 			f2fs_put_page(page, 0);
525502c6e0bSGu Zheng 		}
526127e670aSJaegeuk Kim 
52736795567SGu Zheng 		orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
528127e670aSJaegeuk Kim 
52936795567SGu Zheng 		if (nentries == F2FS_ORPHANS_PER_BLOCK) {
530127e670aSJaegeuk Kim 			/*
531127e670aSJaegeuk Kim 			 * an orphan block is full of 1020 entries,
532127e670aSJaegeuk Kim 			 * then we need to flush current orphan blocks
533127e670aSJaegeuk Kim 			 * and bring another one in memory
534127e670aSJaegeuk Kim 			 */
535127e670aSJaegeuk Kim 			orphan_blk->blk_addr = cpu_to_le16(index);
536127e670aSJaegeuk Kim 			orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
537127e670aSJaegeuk Kim 			orphan_blk->entry_count = cpu_to_le32(nentries);
538127e670aSJaegeuk Kim 			set_page_dirty(page);
539127e670aSJaegeuk Kim 			f2fs_put_page(page, 1);
540127e670aSJaegeuk Kim 			index++;
541127e670aSJaegeuk Kim 			nentries = 0;
542127e670aSJaegeuk Kim 			page = NULL;
543127e670aSJaegeuk Kim 		}
544127e670aSJaegeuk Kim 	}
545127e670aSJaegeuk Kim 
546502c6e0bSGu Zheng 	if (page) {
547127e670aSJaegeuk Kim 		orphan_blk->blk_addr = cpu_to_le16(index);
548127e670aSJaegeuk Kim 		orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
549127e670aSJaegeuk Kim 		orphan_blk->entry_count = cpu_to_le32(nentries);
550127e670aSJaegeuk Kim 		set_page_dirty(page);
551127e670aSJaegeuk Kim 		f2fs_put_page(page, 1);
552502c6e0bSGu Zheng 	}
553502c6e0bSGu Zheng 
55467298804SChao Yu 	spin_unlock(&im->ino_lock);
555127e670aSJaegeuk Kim }
556127e670aSJaegeuk Kim 
557127e670aSJaegeuk Kim static struct page *validate_checkpoint(struct f2fs_sb_info *sbi,
558127e670aSJaegeuk Kim 				block_t cp_addr, unsigned long long *version)
559127e670aSJaegeuk Kim {
560127e670aSJaegeuk Kim 	struct page *cp_page_1, *cp_page_2 = NULL;
561127e670aSJaegeuk Kim 	unsigned long blk_size = sbi->blocksize;
562127e670aSJaegeuk Kim 	struct f2fs_checkpoint *cp_block;
563127e670aSJaegeuk Kim 	unsigned long long cur_version = 0, pre_version = 0;
564127e670aSJaegeuk Kim 	size_t crc_offset;
5657e586fa0SJaegeuk Kim 	__u32 crc = 0;
566127e670aSJaegeuk Kim 
567127e670aSJaegeuk Kim 	/* Read the 1st cp block in this CP pack */
568127e670aSJaegeuk Kim 	cp_page_1 = get_meta_page(sbi, cp_addr);
569127e670aSJaegeuk Kim 
570127e670aSJaegeuk Kim 	/* get the version number */
571127e670aSJaegeuk Kim 	cp_block = (struct f2fs_checkpoint *)page_address(cp_page_1);
572127e670aSJaegeuk Kim 	crc_offset = le32_to_cpu(cp_block->checksum_offset);
573127e670aSJaegeuk Kim 	if (crc_offset >= blk_size)
574127e670aSJaegeuk Kim 		goto invalid_cp1;
575127e670aSJaegeuk Kim 
57629e7043fSJaegeuk Kim 	crc = le32_to_cpu(*((__le32 *)((unsigned char *)cp_block + crc_offset)));
577127e670aSJaegeuk Kim 	if (!f2fs_crc_valid(crc, cp_block, crc_offset))
578127e670aSJaegeuk Kim 		goto invalid_cp1;
579127e670aSJaegeuk Kim 
580d71b5564SJaegeuk Kim 	pre_version = cur_cp_version(cp_block);
581127e670aSJaegeuk Kim 
582127e670aSJaegeuk Kim 	/* Read the 2nd cp block in this CP pack */
58325ca923bSJaegeuk Kim 	cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1;
584127e670aSJaegeuk Kim 	cp_page_2 = get_meta_page(sbi, cp_addr);
585127e670aSJaegeuk Kim 
586127e670aSJaegeuk Kim 	cp_block = (struct f2fs_checkpoint *)page_address(cp_page_2);
587127e670aSJaegeuk Kim 	crc_offset = le32_to_cpu(cp_block->checksum_offset);
588127e670aSJaegeuk Kim 	if (crc_offset >= blk_size)
589127e670aSJaegeuk Kim 		goto invalid_cp2;
590127e670aSJaegeuk Kim 
59129e7043fSJaegeuk Kim 	crc = le32_to_cpu(*((__le32 *)((unsigned char *)cp_block + crc_offset)));
592127e670aSJaegeuk Kim 	if (!f2fs_crc_valid(crc, cp_block, crc_offset))
593127e670aSJaegeuk Kim 		goto invalid_cp2;
594127e670aSJaegeuk Kim 
595d71b5564SJaegeuk Kim 	cur_version = cur_cp_version(cp_block);
596127e670aSJaegeuk Kim 
597127e670aSJaegeuk Kim 	if (cur_version == pre_version) {
598127e670aSJaegeuk Kim 		*version = cur_version;
599127e670aSJaegeuk Kim 		f2fs_put_page(cp_page_2, 1);
600127e670aSJaegeuk Kim 		return cp_page_1;
601127e670aSJaegeuk Kim 	}
602127e670aSJaegeuk Kim invalid_cp2:
603127e670aSJaegeuk Kim 	f2fs_put_page(cp_page_2, 1);
604127e670aSJaegeuk Kim invalid_cp1:
605127e670aSJaegeuk Kim 	f2fs_put_page(cp_page_1, 1);
606127e670aSJaegeuk Kim 	return NULL;
607127e670aSJaegeuk Kim }
608127e670aSJaegeuk Kim 
609127e670aSJaegeuk Kim int get_valid_checkpoint(struct f2fs_sb_info *sbi)
610127e670aSJaegeuk Kim {
611127e670aSJaegeuk Kim 	struct f2fs_checkpoint *cp_block;
612127e670aSJaegeuk Kim 	struct f2fs_super_block *fsb = sbi->raw_super;
613127e670aSJaegeuk Kim 	struct page *cp1, *cp2, *cur_page;
614127e670aSJaegeuk Kim 	unsigned long blk_size = sbi->blocksize;
615127e670aSJaegeuk Kim 	unsigned long long cp1_version = 0, cp2_version = 0;
616127e670aSJaegeuk Kim 	unsigned long long cp_start_blk_no;
61755141486SWanpeng Li 	unsigned int cp_blks = 1 + __cp_payload(sbi);
6181dbe4152SChangman Lee 	block_t cp_blk_no;
6191dbe4152SChangman Lee 	int i;
620127e670aSJaegeuk Kim 
6211dbe4152SChangman Lee 	sbi->ckpt = kzalloc(cp_blks * blk_size, GFP_KERNEL);
622127e670aSJaegeuk Kim 	if (!sbi->ckpt)
623127e670aSJaegeuk Kim 		return -ENOMEM;
624127e670aSJaegeuk Kim 	/*
625127e670aSJaegeuk Kim 	 * Finding out valid cp block involves read both
626127e670aSJaegeuk Kim 	 * sets( cp pack1 and cp pack 2)
627127e670aSJaegeuk Kim 	 */
628127e670aSJaegeuk Kim 	cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr);
629127e670aSJaegeuk Kim 	cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
630127e670aSJaegeuk Kim 
631127e670aSJaegeuk Kim 	/* The second checkpoint pack should start at the next segment */
632f9a4e6dfSJaegeuk Kim 	cp_start_blk_no += ((unsigned long long)1) <<
633f9a4e6dfSJaegeuk Kim 				le32_to_cpu(fsb->log_blocks_per_seg);
634127e670aSJaegeuk Kim 	cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
635127e670aSJaegeuk Kim 
636127e670aSJaegeuk Kim 	if (cp1 && cp2) {
637127e670aSJaegeuk Kim 		if (ver_after(cp2_version, cp1_version))
638127e670aSJaegeuk Kim 			cur_page = cp2;
639127e670aSJaegeuk Kim 		else
640127e670aSJaegeuk Kim 			cur_page = cp1;
641127e670aSJaegeuk Kim 	} else if (cp1) {
642127e670aSJaegeuk Kim 		cur_page = cp1;
643127e670aSJaegeuk Kim 	} else if (cp2) {
644127e670aSJaegeuk Kim 		cur_page = cp2;
645127e670aSJaegeuk Kim 	} else {
646127e670aSJaegeuk Kim 		goto fail_no_cp;
647127e670aSJaegeuk Kim 	}
648127e670aSJaegeuk Kim 
649127e670aSJaegeuk Kim 	cp_block = (struct f2fs_checkpoint *)page_address(cur_page);
650127e670aSJaegeuk Kim 	memcpy(sbi->ckpt, cp_block, blk_size);
651127e670aSJaegeuk Kim 
6521dbe4152SChangman Lee 	if (cp_blks <= 1)
6531dbe4152SChangman Lee 		goto done;
6541dbe4152SChangman Lee 
6551dbe4152SChangman Lee 	cp_blk_no = le32_to_cpu(fsb->cp_blkaddr);
6561dbe4152SChangman Lee 	if (cur_page == cp2)
6571dbe4152SChangman Lee 		cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg);
6581dbe4152SChangman Lee 
6591dbe4152SChangman Lee 	for (i = 1; i < cp_blks; i++) {
6601dbe4152SChangman Lee 		void *sit_bitmap_ptr;
6611dbe4152SChangman Lee 		unsigned char *ckpt = (unsigned char *)sbi->ckpt;
6621dbe4152SChangman Lee 
6631dbe4152SChangman Lee 		cur_page = get_meta_page(sbi, cp_blk_no + i);
6641dbe4152SChangman Lee 		sit_bitmap_ptr = page_address(cur_page);
6651dbe4152SChangman Lee 		memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size);
6661dbe4152SChangman Lee 		f2fs_put_page(cur_page, 1);
6671dbe4152SChangman Lee 	}
6681dbe4152SChangman Lee done:
669127e670aSJaegeuk Kim 	f2fs_put_page(cp1, 1);
670127e670aSJaegeuk Kim 	f2fs_put_page(cp2, 1);
671127e670aSJaegeuk Kim 	return 0;
672127e670aSJaegeuk Kim 
673127e670aSJaegeuk Kim fail_no_cp:
674127e670aSJaegeuk Kim 	kfree(sbi->ckpt);
675127e670aSJaegeuk Kim 	return -EINVAL;
676127e670aSJaegeuk Kim }
677127e670aSJaegeuk Kim 
67806292073SChao Yu static int __add_dirty_inode(struct inode *inode, struct inode_entry *new)
679127e670aSJaegeuk Kim {
6804081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
6812d7b822aSChao Yu 
682ed57c27fSJaegeuk Kim 	if (is_inode_flag_set(F2FS_I(inode), FI_DIRTY_DIR))
6835deb8267SJaegeuk Kim 		return -EEXIST;
6842d7b822aSChao Yu 
685ed57c27fSJaegeuk Kim 	set_inode_flag(F2FS_I(inode), FI_DIRTY_DIR);
686ed57c27fSJaegeuk Kim 	F2FS_I(inode)->dirty_dir = new;
687ed57c27fSJaegeuk Kim 	list_add_tail(&new->list, &sbi->dir_inode_list);
688dcdfff65SJaegeuk Kim 	stat_inc_dirty_dir(sbi);
6895deb8267SJaegeuk Kim 	return 0;
6905deb8267SJaegeuk Kim }
6915deb8267SJaegeuk Kim 
692a7ffdbe2SJaegeuk Kim void update_dirty_page(struct inode *inode, struct page *page)
6935deb8267SJaegeuk Kim {
6944081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
69506292073SChao Yu 	struct inode_entry *new;
696cf0ee0f0SChao Yu 	int ret = 0;
6975deb8267SJaegeuk Kim 
698a7ffdbe2SJaegeuk Kim 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode))
699127e670aSJaegeuk Kim 		return;
7007bd59381SGu Zheng 
701a7ffdbe2SJaegeuk Kim 	if (!S_ISDIR(inode->i_mode)) {
702a7ffdbe2SJaegeuk Kim 		inode_inc_dirty_pages(inode);
703a7ffdbe2SJaegeuk Kim 		goto out;
704a7ffdbe2SJaegeuk Kim 	}
705a7ffdbe2SJaegeuk Kim 
7067bd59381SGu Zheng 	new = f2fs_kmem_cache_alloc(inode_entry_slab, GFP_NOFS);
707127e670aSJaegeuk Kim 	new->inode = inode;
708127e670aSJaegeuk Kim 	INIT_LIST_HEAD(&new->list);
709127e670aSJaegeuk Kim 
710127e670aSJaegeuk Kim 	spin_lock(&sbi->dir_inode_lock);
711cf0ee0f0SChao Yu 	ret = __add_dirty_inode(inode, new);
712a7ffdbe2SJaegeuk Kim 	inode_inc_dirty_pages(inode);
7135deb8267SJaegeuk Kim 	spin_unlock(&sbi->dir_inode_lock);
714cf0ee0f0SChao Yu 
715cf0ee0f0SChao Yu 	if (ret)
716cf0ee0f0SChao Yu 		kmem_cache_free(inode_entry_slab, new);
717a7ffdbe2SJaegeuk Kim out:
718a7ffdbe2SJaegeuk Kim 	SetPagePrivate(page);
7199e4ded3fSJaegeuk Kim 	f2fs_trace_pid(page);
7205deb8267SJaegeuk Kim }
721127e670aSJaegeuk Kim 
7225deb8267SJaegeuk Kim void add_dirty_dir_inode(struct inode *inode)
7235deb8267SJaegeuk Kim {
7244081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
72506292073SChao Yu 	struct inode_entry *new =
7267bd59381SGu Zheng 			f2fs_kmem_cache_alloc(inode_entry_slab, GFP_NOFS);
727cf0ee0f0SChao Yu 	int ret = 0;
7287bd59381SGu Zheng 
7295deb8267SJaegeuk Kim 	new->inode = inode;
7305deb8267SJaegeuk Kim 	INIT_LIST_HEAD(&new->list);
7315deb8267SJaegeuk Kim 
7325deb8267SJaegeuk Kim 	spin_lock(&sbi->dir_inode_lock);
733cf0ee0f0SChao Yu 	ret = __add_dirty_inode(inode, new);
734127e670aSJaegeuk Kim 	spin_unlock(&sbi->dir_inode_lock);
735cf0ee0f0SChao Yu 
736cf0ee0f0SChao Yu 	if (ret)
737cf0ee0f0SChao Yu 		kmem_cache_free(inode_entry_slab, new);
738127e670aSJaegeuk Kim }
739127e670aSJaegeuk Kim 
740127e670aSJaegeuk Kim void remove_dirty_dir_inode(struct inode *inode)
741127e670aSJaegeuk Kim {
7424081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
74306292073SChao Yu 	struct inode_entry *entry;
744127e670aSJaegeuk Kim 
745127e670aSJaegeuk Kim 	if (!S_ISDIR(inode->i_mode))
746127e670aSJaegeuk Kim 		return;
747127e670aSJaegeuk Kim 
748127e670aSJaegeuk Kim 	spin_lock(&sbi->dir_inode_lock);
749a7ffdbe2SJaegeuk Kim 	if (get_dirty_pages(inode) ||
750ed57c27fSJaegeuk Kim 			!is_inode_flag_set(F2FS_I(inode), FI_DIRTY_DIR)) {
7513b10b1fdSJaegeuk Kim 		spin_unlock(&sbi->dir_inode_lock);
7523b10b1fdSJaegeuk Kim 		return;
7533b10b1fdSJaegeuk Kim 	}
754127e670aSJaegeuk Kim 
755ed57c27fSJaegeuk Kim 	entry = F2FS_I(inode)->dirty_dir;
756127e670aSJaegeuk Kim 	list_del(&entry->list);
757ed57c27fSJaegeuk Kim 	F2FS_I(inode)->dirty_dir = NULL;
758ed57c27fSJaegeuk Kim 	clear_inode_flag(F2FS_I(inode), FI_DIRTY_DIR);
759dcdfff65SJaegeuk Kim 	stat_dec_dirty_dir(sbi);
760cf0ee0f0SChao Yu 	spin_unlock(&sbi->dir_inode_lock);
761cf0ee0f0SChao Yu 	kmem_cache_free(inode_entry_slab, entry);
76274d0b917SJaegeuk Kim 
76374d0b917SJaegeuk Kim 	/* Only from the recovery routine */
764afc3eda2SJaegeuk Kim 	if (is_inode_flag_set(F2FS_I(inode), FI_DELAY_IPUT)) {
765afc3eda2SJaegeuk Kim 		clear_inode_flag(F2FS_I(inode), FI_DELAY_IPUT);
76674d0b917SJaegeuk Kim 		iput(inode);
76774d0b917SJaegeuk Kim 	}
768afc3eda2SJaegeuk Kim }
76974d0b917SJaegeuk Kim 
770127e670aSJaegeuk Kim void sync_dirty_dir_inodes(struct f2fs_sb_info *sbi)
771127e670aSJaegeuk Kim {
772ce3b7d80SGu Zheng 	struct list_head *head;
77306292073SChao Yu 	struct inode_entry *entry;
774127e670aSJaegeuk Kim 	struct inode *inode;
775127e670aSJaegeuk Kim retry:
776af41d3eeSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
777af41d3eeSJaegeuk Kim 		return;
778af41d3eeSJaegeuk Kim 
779127e670aSJaegeuk Kim 	spin_lock(&sbi->dir_inode_lock);
780ce3b7d80SGu Zheng 
781ce3b7d80SGu Zheng 	head = &sbi->dir_inode_list;
782127e670aSJaegeuk Kim 	if (list_empty(head)) {
783127e670aSJaegeuk Kim 		spin_unlock(&sbi->dir_inode_lock);
784127e670aSJaegeuk Kim 		return;
785127e670aSJaegeuk Kim 	}
78606292073SChao Yu 	entry = list_entry(head->next, struct inode_entry, list);
787127e670aSJaegeuk Kim 	inode = igrab(entry->inode);
788127e670aSJaegeuk Kim 	spin_unlock(&sbi->dir_inode_lock);
789127e670aSJaegeuk Kim 	if (inode) {
79087d6f890SJaegeuk Kim 		filemap_fdatawrite(inode->i_mapping);
791127e670aSJaegeuk Kim 		iput(inode);
792127e670aSJaegeuk Kim 	} else {
793127e670aSJaegeuk Kim 		/*
794127e670aSJaegeuk Kim 		 * We should submit bio, since it exists several
795127e670aSJaegeuk Kim 		 * wribacking dentry pages in the freeing inode.
796127e670aSJaegeuk Kim 		 */
797458e6197SJaegeuk Kim 		f2fs_submit_merged_bio(sbi, DATA, WRITE);
798127e670aSJaegeuk Kim 	}
799127e670aSJaegeuk Kim 	goto retry;
800127e670aSJaegeuk Kim }
801127e670aSJaegeuk Kim 
8020a8165d7SJaegeuk Kim /*
803127e670aSJaegeuk Kim  * Freeze all the FS-operations for checkpoint.
804127e670aSJaegeuk Kim  */
805cf779cabSJaegeuk Kim static int block_operations(struct f2fs_sb_info *sbi)
806127e670aSJaegeuk Kim {
807127e670aSJaegeuk Kim 	struct writeback_control wbc = {
808127e670aSJaegeuk Kim 		.sync_mode = WB_SYNC_ALL,
809127e670aSJaegeuk Kim 		.nr_to_write = LONG_MAX,
810127e670aSJaegeuk Kim 		.for_reclaim = 0,
811127e670aSJaegeuk Kim 	};
812c718379bSJaegeuk Kim 	struct blk_plug plug;
813cf779cabSJaegeuk Kim 	int err = 0;
814c718379bSJaegeuk Kim 
815c718379bSJaegeuk Kim 	blk_start_plug(&plug);
816c718379bSJaegeuk Kim 
81739936837SJaegeuk Kim retry_flush_dents:
818e479556bSGu Zheng 	f2fs_lock_all(sbi);
819127e670aSJaegeuk Kim 	/* write all the dirty dentry pages */
820127e670aSJaegeuk Kim 	if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
821e479556bSGu Zheng 		f2fs_unlock_all(sbi);
82239936837SJaegeuk Kim 		sync_dirty_dir_inodes(sbi);
823cf779cabSJaegeuk Kim 		if (unlikely(f2fs_cp_error(sbi))) {
824cf779cabSJaegeuk Kim 			err = -EIO;
825cf779cabSJaegeuk Kim 			goto out;
826cf779cabSJaegeuk Kim 		}
82739936837SJaegeuk Kim 		goto retry_flush_dents;
828127e670aSJaegeuk Kim 	}
829127e670aSJaegeuk Kim 
830127e670aSJaegeuk Kim 	/*
831e1c42045Sarter97 	 * POR: we should ensure that there are no dirty node pages
832127e670aSJaegeuk Kim 	 * until finishing nat/sit flush.
833127e670aSJaegeuk Kim 	 */
83439936837SJaegeuk Kim retry_flush_nodes:
835b3582c68SChao Yu 	down_write(&sbi->node_write);
836127e670aSJaegeuk Kim 
837127e670aSJaegeuk Kim 	if (get_pages(sbi, F2FS_DIRTY_NODES)) {
838b3582c68SChao Yu 		up_write(&sbi->node_write);
83939936837SJaegeuk Kim 		sync_node_pages(sbi, 0, &wbc);
840cf779cabSJaegeuk Kim 		if (unlikely(f2fs_cp_error(sbi))) {
841cf779cabSJaegeuk Kim 			f2fs_unlock_all(sbi);
842cf779cabSJaegeuk Kim 			err = -EIO;
843cf779cabSJaegeuk Kim 			goto out;
844cf779cabSJaegeuk Kim 		}
84539936837SJaegeuk Kim 		goto retry_flush_nodes;
846127e670aSJaegeuk Kim 	}
847cf779cabSJaegeuk Kim out:
848c718379bSJaegeuk Kim 	blk_finish_plug(&plug);
849cf779cabSJaegeuk Kim 	return err;
850127e670aSJaegeuk Kim }
851127e670aSJaegeuk Kim 
852127e670aSJaegeuk Kim static void unblock_operations(struct f2fs_sb_info *sbi)
853127e670aSJaegeuk Kim {
854b3582c68SChao Yu 	up_write(&sbi->node_write);
855e479556bSGu Zheng 	f2fs_unlock_all(sbi);
856127e670aSJaegeuk Kim }
857127e670aSJaegeuk Kim 
858fb51b5efSChangman Lee static void wait_on_all_pages_writeback(struct f2fs_sb_info *sbi)
859fb51b5efSChangman Lee {
860fb51b5efSChangman Lee 	DEFINE_WAIT(wait);
861fb51b5efSChangman Lee 
862fb51b5efSChangman Lee 	for (;;) {
863fb51b5efSChangman Lee 		prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
864fb51b5efSChangman Lee 
865fb51b5efSChangman Lee 		if (!get_pages(sbi, F2FS_WRITEBACK))
866fb51b5efSChangman Lee 			break;
867fb51b5efSChangman Lee 
868fb51b5efSChangman Lee 		io_schedule();
869fb51b5efSChangman Lee 	}
870fb51b5efSChangman Lee 	finish_wait(&sbi->cp_wait, &wait);
871fb51b5efSChangman Lee }
872fb51b5efSChangman Lee 
87375ab4cb8SJaegeuk Kim static void do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
874127e670aSJaegeuk Kim {
875127e670aSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
876cf2271e7SJaegeuk Kim 	struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
87777041823SHuang Ying 	struct f2fs_nm_info *nm_i = NM_I(sbi);
87867298804SChao Yu 	unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num;
87977041823SHuang Ying 	nid_t last_nid = nm_i->next_scan_nid;
880127e670aSJaegeuk Kim 	block_t start_blk;
881127e670aSJaegeuk Kim 	struct page *cp_page;
882127e670aSJaegeuk Kim 	unsigned int data_sum_blocks, orphan_blocks;
8837e586fa0SJaegeuk Kim 	__u32 crc32 = 0;
884127e670aSJaegeuk Kim 	void *kaddr;
885127e670aSJaegeuk Kim 	int i;
88655141486SWanpeng Li 	int cp_payload_blks = __cp_payload(sbi);
887127e670aSJaegeuk Kim 
8881e87a78dSJaegeuk Kim 	/*
8891e87a78dSJaegeuk Kim 	 * This avoids to conduct wrong roll-forward operations and uses
8901e87a78dSJaegeuk Kim 	 * metapages, so should be called prior to sync_meta_pages below.
8911e87a78dSJaegeuk Kim 	 */
892cf2271e7SJaegeuk Kim 	discard_next_dnode(sbi, NEXT_FREE_BLKADDR(sbi, curseg));
893127e670aSJaegeuk Kim 
894127e670aSJaegeuk Kim 	/* Flush all the NAT/SIT pages */
895cf779cabSJaegeuk Kim 	while (get_pages(sbi, F2FS_DIRTY_META)) {
896127e670aSJaegeuk Kim 		sync_meta_pages(sbi, META, LONG_MAX);
897cf779cabSJaegeuk Kim 		if (unlikely(f2fs_cp_error(sbi)))
898cf779cabSJaegeuk Kim 			return;
899cf779cabSJaegeuk Kim 	}
900127e670aSJaegeuk Kim 
901127e670aSJaegeuk Kim 	next_free_nid(sbi, &last_nid);
902127e670aSJaegeuk Kim 
903127e670aSJaegeuk Kim 	/*
904127e670aSJaegeuk Kim 	 * modify checkpoint
905127e670aSJaegeuk Kim 	 * version number is already updated
906127e670aSJaegeuk Kim 	 */
907127e670aSJaegeuk Kim 	ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi));
908127e670aSJaegeuk Kim 	ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
909127e670aSJaegeuk Kim 	ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
910b5b82205SChao Yu 	for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
911127e670aSJaegeuk Kim 		ckpt->cur_node_segno[i] =
912127e670aSJaegeuk Kim 			cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE));
913127e670aSJaegeuk Kim 		ckpt->cur_node_blkoff[i] =
914127e670aSJaegeuk Kim 			cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE));
915127e670aSJaegeuk Kim 		ckpt->alloc_type[i + CURSEG_HOT_NODE] =
916127e670aSJaegeuk Kim 				curseg_alloc_type(sbi, i + CURSEG_HOT_NODE);
917127e670aSJaegeuk Kim 	}
918b5b82205SChao Yu 	for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
919127e670aSJaegeuk Kim 		ckpt->cur_data_segno[i] =
920127e670aSJaegeuk Kim 			cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA));
921127e670aSJaegeuk Kim 		ckpt->cur_data_blkoff[i] =
922127e670aSJaegeuk Kim 			cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA));
923127e670aSJaegeuk Kim 		ckpt->alloc_type[i + CURSEG_HOT_DATA] =
924127e670aSJaegeuk Kim 				curseg_alloc_type(sbi, i + CURSEG_HOT_DATA);
925127e670aSJaegeuk Kim 	}
926127e670aSJaegeuk Kim 
927127e670aSJaegeuk Kim 	ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi));
928127e670aSJaegeuk Kim 	ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi));
929127e670aSJaegeuk Kim 	ckpt->next_free_nid = cpu_to_le32(last_nid);
930127e670aSJaegeuk Kim 
931127e670aSJaegeuk Kim 	/* 2 cp  + n data seg summary + orphan inode blocks */
9323fa06d7bSChao Yu 	data_sum_blocks = npages_for_summary_flush(sbi, false);
933b5b82205SChao Yu 	if (data_sum_blocks < NR_CURSEG_DATA_TYPE)
93425ca923bSJaegeuk Kim 		set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
935127e670aSJaegeuk Kim 	else
93625ca923bSJaegeuk Kim 		clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
937127e670aSJaegeuk Kim 
93867298804SChao Yu 	orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num);
9391dbe4152SChangman Lee 	ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks +
9401dbe4152SChangman Lee 			orphan_blocks);
941127e670aSJaegeuk Kim 
942119ee914SJaegeuk Kim 	if (__remain_node_summaries(cpc->reason))
943b5b82205SChao Yu 		ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS+
9441dbe4152SChangman Lee 				cp_payload_blks + data_sum_blocks +
9451dbe4152SChangman Lee 				orphan_blocks + NR_CURSEG_NODE_TYPE);
946119ee914SJaegeuk Kim 	else
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);
950119ee914SJaegeuk Kim 
951119ee914SJaegeuk Kim 	if (cpc->reason == CP_UMOUNT)
952119ee914SJaegeuk Kim 		set_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
953119ee914SJaegeuk Kim 	else
954119ee914SJaegeuk Kim 		clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
955119ee914SJaegeuk Kim 
956119ee914SJaegeuk Kim 	if (cpc->reason == CP_FASTBOOT)
957119ee914SJaegeuk Kim 		set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
958119ee914SJaegeuk Kim 	else
959119ee914SJaegeuk Kim 		clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
960127e670aSJaegeuk Kim 
96167298804SChao Yu 	if (orphan_num)
96225ca923bSJaegeuk Kim 		set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
963127e670aSJaegeuk Kim 	else
96425ca923bSJaegeuk Kim 		clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
965127e670aSJaegeuk Kim 
966caf0047eSChao Yu 	if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
9672ae4c673SJaegeuk Kim 		set_ckpt_flags(ckpt, CP_FSCK_FLAG);
9682ae4c673SJaegeuk Kim 
969127e670aSJaegeuk Kim 	/* update SIT/NAT bitmap */
970127e670aSJaegeuk Kim 	get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
971127e670aSJaegeuk Kim 	get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
972127e670aSJaegeuk Kim 
973127e670aSJaegeuk Kim 	crc32 = f2fs_crc32(ckpt, le32_to_cpu(ckpt->checksum_offset));
9747e586fa0SJaegeuk Kim 	*((__le32 *)((unsigned char *)ckpt +
9757e586fa0SJaegeuk Kim 				le32_to_cpu(ckpt->checksum_offset)))
976127e670aSJaegeuk Kim 				= cpu_to_le32(crc32);
977127e670aSJaegeuk Kim 
978127e670aSJaegeuk Kim 	start_blk = __start_cp_addr(sbi);
979127e670aSJaegeuk Kim 
980127e670aSJaegeuk Kim 	/* write out checkpoint buffer at block 0 */
981127e670aSJaegeuk Kim 	cp_page = grab_meta_page(sbi, start_blk++);
982127e670aSJaegeuk Kim 	kaddr = page_address(cp_page);
983f7ef9b83SJaegeuk Kim 	memcpy(kaddr, ckpt, F2FS_BLKSIZE);
984127e670aSJaegeuk Kim 	set_page_dirty(cp_page);
985127e670aSJaegeuk Kim 	f2fs_put_page(cp_page, 1);
986127e670aSJaegeuk Kim 
9871dbe4152SChangman Lee 	for (i = 1; i < 1 + cp_payload_blks; i++) {
9881dbe4152SChangman Lee 		cp_page = grab_meta_page(sbi, start_blk++);
9891dbe4152SChangman Lee 		kaddr = page_address(cp_page);
990f7ef9b83SJaegeuk Kim 		memcpy(kaddr, (char *)ckpt + i * F2FS_BLKSIZE, F2FS_BLKSIZE);
9911dbe4152SChangman Lee 		set_page_dirty(cp_page);
9921dbe4152SChangman Lee 		f2fs_put_page(cp_page, 1);
9931dbe4152SChangman Lee 	}
9941dbe4152SChangman Lee 
99567298804SChao Yu 	if (orphan_num) {
996127e670aSJaegeuk Kim 		write_orphan_inodes(sbi, start_blk);
997127e670aSJaegeuk Kim 		start_blk += orphan_blocks;
998127e670aSJaegeuk Kim 	}
999127e670aSJaegeuk Kim 
1000127e670aSJaegeuk Kim 	write_data_summaries(sbi, start_blk);
1001127e670aSJaegeuk Kim 	start_blk += data_sum_blocks;
1002119ee914SJaegeuk Kim 	if (__remain_node_summaries(cpc->reason)) {
1003127e670aSJaegeuk Kim 		write_node_summaries(sbi, start_blk);
1004127e670aSJaegeuk Kim 		start_blk += NR_CURSEG_NODE_TYPE;
1005127e670aSJaegeuk Kim 	}
1006127e670aSJaegeuk Kim 
1007127e670aSJaegeuk Kim 	/* writeout checkpoint block */
1008127e670aSJaegeuk Kim 	cp_page = grab_meta_page(sbi, start_blk);
1009127e670aSJaegeuk Kim 	kaddr = page_address(cp_page);
1010f7ef9b83SJaegeuk Kim 	memcpy(kaddr, ckpt, F2FS_BLKSIZE);
1011127e670aSJaegeuk Kim 	set_page_dirty(cp_page);
1012127e670aSJaegeuk Kim 	f2fs_put_page(cp_page, 1);
1013127e670aSJaegeuk Kim 
1014127e670aSJaegeuk Kim 	/* wait for previous submitted node/meta pages writeback */
1015fb51b5efSChangman Lee 	wait_on_all_pages_writeback(sbi);
1016127e670aSJaegeuk Kim 
1017cf779cabSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
1018cf779cabSJaegeuk Kim 		return;
1019cf779cabSJaegeuk Kim 
10204ef51a8fSJaegeuk Kim 	filemap_fdatawait_range(NODE_MAPPING(sbi), 0, LONG_MAX);
10219df27d98SGu Zheng 	filemap_fdatawait_range(META_MAPPING(sbi), 0, LONG_MAX);
1022127e670aSJaegeuk Kim 
1023127e670aSJaegeuk Kim 	/* update user_block_counts */
1024127e670aSJaegeuk Kim 	sbi->last_valid_block_count = sbi->total_valid_block_count;
1025127e670aSJaegeuk Kim 	sbi->alloc_valid_block_count = 0;
1026127e670aSJaegeuk Kim 
1027127e670aSJaegeuk Kim 	/* Here, we only have one bio having CP pack */
1028127e670aSJaegeuk Kim 	sync_meta_pages(sbi, META_FLUSH, LONG_MAX);
1029127e670aSJaegeuk Kim 
10306a8f8ca5SJaegeuk Kim 	/* wait for previous submitted meta pages writeback */
10316a8f8ca5SJaegeuk Kim 	wait_on_all_pages_writeback(sbi);
10326a8f8ca5SJaegeuk Kim 
1033fff04f90SJaegeuk Kim 	release_dirty_inode(sbi);
1034cf779cabSJaegeuk Kim 
1035cf779cabSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
1036cf779cabSJaegeuk Kim 		return;
1037cf779cabSJaegeuk Kim 
1038cf779cabSJaegeuk Kim 	clear_prefree_segments(sbi);
1039caf0047eSChao Yu 	clear_sbi_flag(sbi, SBI_IS_DIRTY);
1040127e670aSJaegeuk Kim }
1041127e670aSJaegeuk Kim 
10420a8165d7SJaegeuk Kim /*
1043e1c42045Sarter97  * We guarantee that this checkpoint procedure will not fail.
1044127e670aSJaegeuk Kim  */
104575ab4cb8SJaegeuk Kim void write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1046127e670aSJaegeuk Kim {
1047127e670aSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1048127e670aSJaegeuk Kim 	unsigned long long ckpt_ver;
1049127e670aSJaegeuk Kim 
105075ab4cb8SJaegeuk Kim 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops");
10512af4bd6cSNamjae Jeon 
1052127e670aSJaegeuk Kim 	mutex_lock(&sbi->cp_mutex);
10538501017eSJaegeuk Kim 
1054caf0047eSChao Yu 	if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) &&
105585dc2f2cSJaegeuk Kim 			cpc->reason != CP_DISCARD && cpc->reason != CP_UMOUNT)
10568501017eSJaegeuk Kim 		goto out;
1057cf779cabSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
1058cf779cabSJaegeuk Kim 		goto out;
105911504a8eSJaegeuk Kim 	if (f2fs_readonly(sbi->sb))
106011504a8eSJaegeuk Kim 		goto out;
1061cf779cabSJaegeuk Kim 	if (block_operations(sbi))
1062cf779cabSJaegeuk Kim 		goto out;
1063127e670aSJaegeuk Kim 
106475ab4cb8SJaegeuk Kim 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops");
10652af4bd6cSNamjae Jeon 
1066458e6197SJaegeuk Kim 	f2fs_submit_merged_bio(sbi, DATA, WRITE);
1067458e6197SJaegeuk Kim 	f2fs_submit_merged_bio(sbi, NODE, WRITE);
1068458e6197SJaegeuk Kim 	f2fs_submit_merged_bio(sbi, META, WRITE);
1069127e670aSJaegeuk Kim 
1070127e670aSJaegeuk Kim 	/*
1071127e670aSJaegeuk Kim 	 * update checkpoint pack index
1072127e670aSJaegeuk Kim 	 * Increase the version number so that
1073127e670aSJaegeuk Kim 	 * SIT entries and seg summaries are written at correct place
1074127e670aSJaegeuk Kim 	 */
1075d71b5564SJaegeuk Kim 	ckpt_ver = cur_cp_version(ckpt);
1076127e670aSJaegeuk Kim 	ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
1077127e670aSJaegeuk Kim 
1078127e670aSJaegeuk Kim 	/* write cached NAT/SIT entries to NAT/SIT area */
1079127e670aSJaegeuk Kim 	flush_nat_entries(sbi);
10804b2fecc8SJaegeuk Kim 	flush_sit_entries(sbi, cpc);
1081127e670aSJaegeuk Kim 
1082127e670aSJaegeuk Kim 	/* unlock all the fs_lock[] in do_checkpoint() */
108375ab4cb8SJaegeuk Kim 	do_checkpoint(sbi, cpc);
1084127e670aSJaegeuk Kim 
1085127e670aSJaegeuk Kim 	unblock_operations(sbi);
1086942e0be6SChangman Lee 	stat_inc_cp_count(sbi->stat_info);
10878501017eSJaegeuk Kim out:
10888501017eSJaegeuk Kim 	mutex_unlock(&sbi->cp_mutex);
108975ab4cb8SJaegeuk Kim 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint");
1090127e670aSJaegeuk Kim }
1091127e670aSJaegeuk Kim 
10926451e041SJaegeuk Kim void init_ino_entry_info(struct f2fs_sb_info *sbi)
1093127e670aSJaegeuk Kim {
10946451e041SJaegeuk Kim 	int i;
10956451e041SJaegeuk Kim 
10966451e041SJaegeuk Kim 	for (i = 0; i < MAX_INO_ENTRY; i++) {
109767298804SChao Yu 		struct inode_management *im = &sbi->im[i];
109867298804SChao Yu 
109967298804SChao Yu 		INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC);
110067298804SChao Yu 		spin_lock_init(&im->ino_lock);
110167298804SChao Yu 		INIT_LIST_HEAD(&im->ino_list);
110267298804SChao Yu 		im->ino_num = 0;
11036451e041SJaegeuk Kim 	}
11046451e041SJaegeuk Kim 
11050d47c1adSGu Zheng 	/*
11060d47c1adSGu Zheng 	 * considering 512 blocks in a segment 8 blocks are needed for cp
11070d47c1adSGu Zheng 	 * and log segment summaries. Remaining blocks are used to keep
11080d47c1adSGu Zheng 	 * orphan entries with the limitation one reserved segment
11090d47c1adSGu Zheng 	 * for cp pack we can have max 1020*504 orphan entries
11100d47c1adSGu Zheng 	 */
1111b5b82205SChao Yu 	sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS -
1112b5b82205SChao Yu 			NR_CURSEG_TYPE) * F2FS_ORPHANS_PER_BLOCK;
1113127e670aSJaegeuk Kim }
1114127e670aSJaegeuk Kim 
11156e6093a8SNamjae Jeon int __init create_checkpoint_caches(void)
1116127e670aSJaegeuk Kim {
11176451e041SJaegeuk Kim 	ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry",
11186451e041SJaegeuk Kim 			sizeof(struct ino_entry));
11196451e041SJaegeuk Kim 	if (!ino_entry_slab)
1120127e670aSJaegeuk Kim 		return -ENOMEM;
112106292073SChao Yu 	inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry",
112206292073SChao Yu 			sizeof(struct inode_entry));
11236bacf52fSJaegeuk Kim 	if (!inode_entry_slab) {
11246451e041SJaegeuk Kim 		kmem_cache_destroy(ino_entry_slab);
1125127e670aSJaegeuk Kim 		return -ENOMEM;
1126127e670aSJaegeuk Kim 	}
1127127e670aSJaegeuk Kim 	return 0;
1128127e670aSJaegeuk Kim }
1129127e670aSJaegeuk Kim 
1130127e670aSJaegeuk Kim void destroy_checkpoint_caches(void)
1131127e670aSJaegeuk Kim {
11326451e041SJaegeuk Kim 	kmem_cache_destroy(ino_entry_slab);
1133127e670aSJaegeuk Kim 	kmem_cache_destroy(inode_entry_slab);
1134127e670aSJaegeuk Kim }
1135