xref: /openbmc/linux/fs/f2fs/checkpoint.c (revision 05ca3632e5a73b493b27ec3e2a337885563abff0)
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 = {
55*05ca3632SJaegeuk Kim 		.sbi = sbi,
56cf04e8ebSJaegeuk Kim 		.type = META,
57cf04e8ebSJaegeuk Kim 		.rw = READ_SYNC | REQ_META | REQ_PRIO,
58cf04e8ebSJaegeuk Kim 		.blk_addr = index,
59cf04e8ebSJaegeuk Kim 	};
60127e670aSJaegeuk Kim repeat:
61127e670aSJaegeuk Kim 	page = grab_cache_page(mapping, index);
62127e670aSJaegeuk Kim 	if (!page) {
63127e670aSJaegeuk Kim 		cond_resched();
64127e670aSJaegeuk Kim 		goto repeat;
65127e670aSJaegeuk Kim 	}
66393ff91fSJaegeuk Kim 	if (PageUptodate(page))
67393ff91fSJaegeuk Kim 		goto out;
68127e670aSJaegeuk Kim 
69*05ca3632SJaegeuk Kim 	fio.page = page;
70*05ca3632SJaegeuk Kim 
71*05ca3632SJaegeuk Kim 	if (f2fs_submit_page_bio(&fio))
72393ff91fSJaegeuk Kim 		goto repeat;
73393ff91fSJaegeuk Kim 
74393ff91fSJaegeuk Kim 	lock_page(page);
756bacf52fSJaegeuk Kim 	if (unlikely(page->mapping != mapping)) {
76afcb7ca0SJaegeuk Kim 		f2fs_put_page(page, 1);
77afcb7ca0SJaegeuk Kim 		goto repeat;
78afcb7ca0SJaegeuk Kim 	}
79393ff91fSJaegeuk Kim out:
80127e670aSJaegeuk Kim 	return page;
81127e670aSJaegeuk Kim }
82127e670aSJaegeuk Kim 
83f0c9cadaSChao Yu bool is_valid_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr, int type)
84662befdaSChao Yu {
85662befdaSChao Yu 	switch (type) {
86662befdaSChao Yu 	case META_NAT:
8766b00c18SChao Yu 		break;
88662befdaSChao Yu 	case META_SIT:
8966b00c18SChao Yu 		if (unlikely(blkaddr >= SIT_BLK_CNT(sbi)))
9066b00c18SChao Yu 			return false;
9166b00c18SChao Yu 		break;
9281c1a0f1SChao Yu 	case META_SSA:
9366b00c18SChao Yu 		if (unlikely(blkaddr >= MAIN_BLKADDR(sbi) ||
9466b00c18SChao Yu 			blkaddr < SM_I(sbi)->ssa_blkaddr))
9566b00c18SChao Yu 			return false;
9666b00c18SChao Yu 		break;
97662befdaSChao Yu 	case META_CP:
9866b00c18SChao Yu 		if (unlikely(blkaddr >= SIT_I(sbi)->sit_base_addr ||
9966b00c18SChao Yu 			blkaddr < __start_cp_addr(sbi)))
10066b00c18SChao Yu 			return false;
10166b00c18SChao Yu 		break;
1024c521f49SJaegeuk Kim 	case META_POR:
10366b00c18SChao Yu 		if (unlikely(blkaddr >= MAX_BLKADDR(sbi) ||
10466b00c18SChao Yu 			blkaddr < MAIN_BLKADDR(sbi)))
10566b00c18SChao Yu 			return false;
10666b00c18SChao Yu 		break;
107662befdaSChao Yu 	default:
108662befdaSChao Yu 		BUG();
109662befdaSChao Yu 	}
11066b00c18SChao Yu 
11166b00c18SChao Yu 	return true;
112662befdaSChao Yu }
113662befdaSChao Yu 
114662befdaSChao Yu /*
11581c1a0f1SChao Yu  * Readahead CP/NAT/SIT/SSA pages
116662befdaSChao Yu  */
1174c521f49SJaegeuk Kim int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages, int type)
118662befdaSChao Yu {
119662befdaSChao Yu 	block_t prev_blk_addr = 0;
120662befdaSChao Yu 	struct page *page;
1214c521f49SJaegeuk Kim 	block_t blkno = start;
122662befdaSChao Yu 	struct f2fs_io_info fio = {
123*05ca3632SJaegeuk Kim 		.sbi = sbi,
124662befdaSChao Yu 		.type = META,
125662befdaSChao Yu 		.rw = READ_SYNC | REQ_META | REQ_PRIO
126662befdaSChao Yu 	};
127662befdaSChao Yu 
128662befdaSChao Yu 	for (; nrpages-- > 0; blkno++) {
129662befdaSChao Yu 
13066b00c18SChao Yu 		if (!is_valid_blkaddr(sbi, blkno, type))
13166b00c18SChao Yu 			goto out;
13266b00c18SChao Yu 
133662befdaSChao Yu 		switch (type) {
134662befdaSChao Yu 		case META_NAT:
13566b00c18SChao Yu 			if (unlikely(blkno >=
13666b00c18SChao Yu 					NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid)))
137662befdaSChao Yu 				blkno = 0;
13866b00c18SChao Yu 			/* get nat block addr */
139cf04e8ebSJaegeuk Kim 			fio.blk_addr = current_nat_addr(sbi,
140662befdaSChao Yu 					blkno * NAT_ENTRY_PER_BLOCK);
141662befdaSChao Yu 			break;
142662befdaSChao Yu 		case META_SIT:
143662befdaSChao Yu 			/* get sit block addr */
144cf04e8ebSJaegeuk Kim 			fio.blk_addr = current_sit_addr(sbi,
145662befdaSChao Yu 					blkno * SIT_ENTRY_PER_BLOCK);
146cf04e8ebSJaegeuk Kim 			if (blkno != start && prev_blk_addr + 1 != fio.blk_addr)
147662befdaSChao Yu 				goto out;
148cf04e8ebSJaegeuk Kim 			prev_blk_addr = fio.blk_addr;
149662befdaSChao Yu 			break;
15081c1a0f1SChao Yu 		case META_SSA:
151662befdaSChao Yu 		case META_CP:
1524c521f49SJaegeuk Kim 		case META_POR:
153cf04e8ebSJaegeuk Kim 			fio.blk_addr = blkno;
154662befdaSChao Yu 			break;
155662befdaSChao Yu 		default:
156662befdaSChao Yu 			BUG();
157662befdaSChao Yu 		}
158662befdaSChao Yu 
159cf04e8ebSJaegeuk Kim 		page = grab_cache_page(META_MAPPING(sbi), fio.blk_addr);
160662befdaSChao Yu 		if (!page)
161662befdaSChao Yu 			continue;
162662befdaSChao Yu 		if (PageUptodate(page)) {
163662befdaSChao Yu 			f2fs_put_page(page, 1);
164662befdaSChao Yu 			continue;
165662befdaSChao Yu 		}
166662befdaSChao Yu 
167*05ca3632SJaegeuk Kim 		fio.page = page;
168*05ca3632SJaegeuk Kim 		f2fs_submit_page_mbio(&fio);
169662befdaSChao Yu 		f2fs_put_page(page, 0);
170662befdaSChao Yu 	}
171662befdaSChao Yu out:
172662befdaSChao Yu 	f2fs_submit_merged_bio(sbi, META, READ);
173662befdaSChao Yu 	return blkno - start;
174662befdaSChao Yu }
175662befdaSChao Yu 
176635aee1fSChao Yu void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index)
177635aee1fSChao Yu {
178635aee1fSChao Yu 	struct page *page;
179635aee1fSChao Yu 	bool readahead = false;
180635aee1fSChao Yu 
181635aee1fSChao Yu 	page = find_get_page(META_MAPPING(sbi), index);
182635aee1fSChao Yu 	if (!page || (page && !PageUptodate(page)))
183635aee1fSChao Yu 		readahead = true;
184635aee1fSChao Yu 	f2fs_put_page(page, 0);
185635aee1fSChao Yu 
186635aee1fSChao Yu 	if (readahead)
187635aee1fSChao Yu 		ra_meta_pages(sbi, index, MAX_BIO_BLOCKS(sbi), META_POR);
188635aee1fSChao Yu }
189635aee1fSChao Yu 
190127e670aSJaegeuk Kim static int f2fs_write_meta_page(struct page *page,
191127e670aSJaegeuk Kim 				struct writeback_control *wbc)
192127e670aSJaegeuk Kim {
1934081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_P_SB(page);
194577e3495SJaegeuk Kim 
195ecda0de3SChao Yu 	trace_f2fs_writepage(page, META);
196ecda0de3SChao Yu 
197caf0047eSChao Yu 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
198cfb271d4SChao Yu 		goto redirty_out;
199857dc4e0SJaegeuk Kim 	if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0))
200cfb271d4SChao Yu 		goto redirty_out;
2011e968fdfSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
202cf779cabSJaegeuk Kim 		goto redirty_out;
203127e670aSJaegeuk Kim 
2043cb5ad15SJaegeuk Kim 	f2fs_wait_on_page_writeback(page, META);
205577e3495SJaegeuk Kim 	write_meta_page(sbi, page);
206127e670aSJaegeuk Kim 	dec_page_count(sbi, F2FS_DIRTY_META);
207127e670aSJaegeuk Kim 	unlock_page(page);
208857dc4e0SJaegeuk Kim 
209857dc4e0SJaegeuk Kim 	if (wbc->for_reclaim)
210857dc4e0SJaegeuk Kim 		f2fs_submit_merged_bio(sbi, META, WRITE);
211577e3495SJaegeuk Kim 	return 0;
212cfb271d4SChao Yu 
213cfb271d4SChao Yu redirty_out:
21476f60268SJaegeuk Kim 	redirty_page_for_writepage(wbc, page);
215cfb271d4SChao Yu 	return AOP_WRITEPAGE_ACTIVATE;
216127e670aSJaegeuk Kim }
217127e670aSJaegeuk Kim 
218127e670aSJaegeuk Kim static int f2fs_write_meta_pages(struct address_space *mapping,
219127e670aSJaegeuk Kim 				struct writeback_control *wbc)
220127e670aSJaegeuk Kim {
2214081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
22250c8cdb3SJaegeuk Kim 	long diff, written;
223127e670aSJaegeuk Kim 
224e5748434SChao Yu 	trace_f2fs_writepages(mapping->host, wbc, META);
225e5748434SChao Yu 
2265459aa97SJaegeuk Kim 	/* collect a number of dirty meta pages and write together */
22750c8cdb3SJaegeuk Kim 	if (wbc->for_kupdate ||
22850c8cdb3SJaegeuk Kim 		get_pages(sbi, F2FS_DIRTY_META) < nr_pages_to_skip(sbi, META))
229d3baf95dSJaegeuk Kim 		goto skip_write;
230127e670aSJaegeuk Kim 
231127e670aSJaegeuk Kim 	/* if mounting is failed, skip writing node pages */
232127e670aSJaegeuk Kim 	mutex_lock(&sbi->cp_mutex);
23350c8cdb3SJaegeuk Kim 	diff = nr_pages_to_write(sbi, META, wbc);
23450c8cdb3SJaegeuk Kim 	written = sync_meta_pages(sbi, META, wbc->nr_to_write);
235127e670aSJaegeuk Kim 	mutex_unlock(&sbi->cp_mutex);
23650c8cdb3SJaegeuk Kim 	wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff);
237127e670aSJaegeuk Kim 	return 0;
238d3baf95dSJaegeuk Kim 
239d3baf95dSJaegeuk Kim skip_write:
240d3baf95dSJaegeuk Kim 	wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META);
241d3baf95dSJaegeuk Kim 	return 0;
242127e670aSJaegeuk Kim }
243127e670aSJaegeuk Kim 
244127e670aSJaegeuk Kim long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
245127e670aSJaegeuk Kim 						long nr_to_write)
246127e670aSJaegeuk Kim {
2479df27d98SGu Zheng 	struct address_space *mapping = META_MAPPING(sbi);
248127e670aSJaegeuk Kim 	pgoff_t index = 0, end = LONG_MAX;
249127e670aSJaegeuk Kim 	struct pagevec pvec;
250127e670aSJaegeuk Kim 	long nwritten = 0;
251127e670aSJaegeuk Kim 	struct writeback_control wbc = {
252127e670aSJaegeuk Kim 		.for_reclaim = 0,
253127e670aSJaegeuk Kim 	};
254127e670aSJaegeuk Kim 
255127e670aSJaegeuk Kim 	pagevec_init(&pvec, 0);
256127e670aSJaegeuk Kim 
257127e670aSJaegeuk Kim 	while (index <= end) {
258127e670aSJaegeuk Kim 		int i, nr_pages;
259127e670aSJaegeuk Kim 		nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
260127e670aSJaegeuk Kim 				PAGECACHE_TAG_DIRTY,
261127e670aSJaegeuk Kim 				min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
262cfb271d4SChao Yu 		if (unlikely(nr_pages == 0))
263127e670aSJaegeuk Kim 			break;
264127e670aSJaegeuk Kim 
265127e670aSJaegeuk Kim 		for (i = 0; i < nr_pages; i++) {
266127e670aSJaegeuk Kim 			struct page *page = pvec.pages[i];
267203681f6SJaegeuk Kim 
268127e670aSJaegeuk Kim 			lock_page(page);
269203681f6SJaegeuk Kim 
270203681f6SJaegeuk Kim 			if (unlikely(page->mapping != mapping)) {
271203681f6SJaegeuk Kim continue_unlock:
272203681f6SJaegeuk Kim 				unlock_page(page);
273203681f6SJaegeuk Kim 				continue;
274203681f6SJaegeuk Kim 			}
275203681f6SJaegeuk Kim 			if (!PageDirty(page)) {
276203681f6SJaegeuk Kim 				/* someone wrote it for us */
277203681f6SJaegeuk Kim 				goto continue_unlock;
278203681f6SJaegeuk Kim 			}
279203681f6SJaegeuk Kim 
280203681f6SJaegeuk Kim 			if (!clear_page_dirty_for_io(page))
281203681f6SJaegeuk Kim 				goto continue_unlock;
282203681f6SJaegeuk Kim 
28397dc3fd2SChao Yu 			if (mapping->a_ops->writepage(page, &wbc)) {
284577e3495SJaegeuk Kim 				unlock_page(page);
285577e3495SJaegeuk Kim 				break;
286577e3495SJaegeuk Kim 			}
287cfb271d4SChao Yu 			nwritten++;
288cfb271d4SChao Yu 			if (unlikely(nwritten >= nr_to_write))
289127e670aSJaegeuk Kim 				break;
290127e670aSJaegeuk Kim 		}
291127e670aSJaegeuk Kim 		pagevec_release(&pvec);
292127e670aSJaegeuk Kim 		cond_resched();
293127e670aSJaegeuk Kim 	}
294127e670aSJaegeuk Kim 
295127e670aSJaegeuk Kim 	if (nwritten)
296458e6197SJaegeuk Kim 		f2fs_submit_merged_bio(sbi, type, WRITE);
297127e670aSJaegeuk Kim 
298127e670aSJaegeuk Kim 	return nwritten;
299127e670aSJaegeuk Kim }
300127e670aSJaegeuk Kim 
301127e670aSJaegeuk Kim static int f2fs_set_meta_page_dirty(struct page *page)
302127e670aSJaegeuk Kim {
30326c6b887SJaegeuk Kim 	trace_f2fs_set_page_dirty(page, META);
30426c6b887SJaegeuk Kim 
305127e670aSJaegeuk Kim 	SetPageUptodate(page);
306127e670aSJaegeuk Kim 	if (!PageDirty(page)) {
307127e670aSJaegeuk Kim 		__set_page_dirty_nobuffers(page);
3084081363fSJaegeuk Kim 		inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_META);
3091601839eSChao Yu 		SetPagePrivate(page);
3109e4ded3fSJaegeuk Kim 		f2fs_trace_pid(page);
311127e670aSJaegeuk Kim 		return 1;
312127e670aSJaegeuk Kim 	}
313127e670aSJaegeuk Kim 	return 0;
314127e670aSJaegeuk Kim }
315127e670aSJaegeuk Kim 
316127e670aSJaegeuk Kim const struct address_space_operations f2fs_meta_aops = {
317127e670aSJaegeuk Kim 	.writepage	= f2fs_write_meta_page,
318127e670aSJaegeuk Kim 	.writepages	= f2fs_write_meta_pages,
319127e670aSJaegeuk Kim 	.set_page_dirty	= f2fs_set_meta_page_dirty,
320487261f3SChao Yu 	.invalidatepage = f2fs_invalidate_page,
321487261f3SChao Yu 	.releasepage	= f2fs_release_page,
322127e670aSJaegeuk Kim };
323127e670aSJaegeuk Kim 
3246451e041SJaegeuk Kim static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
325953e6cc6SJaegeuk Kim {
32667298804SChao Yu 	struct inode_management *im = &sbi->im[type];
32739efac41SJaegeuk Kim 	struct ino_entry *e;
32839efac41SJaegeuk Kim retry:
329769ec6e5SJaegeuk Kim 	if (radix_tree_preload(GFP_NOFS)) {
330769ec6e5SJaegeuk Kim 		cond_resched();
331769ec6e5SJaegeuk Kim 		goto retry;
332769ec6e5SJaegeuk Kim 	}
333769ec6e5SJaegeuk Kim 
33467298804SChao Yu 	spin_lock(&im->ino_lock);
335953e6cc6SJaegeuk Kim 
33667298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
33739efac41SJaegeuk Kim 	if (!e) {
33839efac41SJaegeuk Kim 		e = kmem_cache_alloc(ino_entry_slab, GFP_ATOMIC);
33939efac41SJaegeuk Kim 		if (!e) {
34067298804SChao Yu 			spin_unlock(&im->ino_lock);
341769ec6e5SJaegeuk Kim 			radix_tree_preload_end();
34239efac41SJaegeuk Kim 			goto retry;
34339efac41SJaegeuk Kim 		}
34467298804SChao Yu 		if (radix_tree_insert(&im->ino_root, ino, e)) {
34567298804SChao Yu 			spin_unlock(&im->ino_lock);
34639efac41SJaegeuk Kim 			kmem_cache_free(ino_entry_slab, e);
347769ec6e5SJaegeuk Kim 			radix_tree_preload_end();
34839efac41SJaegeuk Kim 			goto retry;
34939efac41SJaegeuk Kim 		}
35039efac41SJaegeuk Kim 		memset(e, 0, sizeof(struct ino_entry));
35139efac41SJaegeuk Kim 		e->ino = ino;
35239efac41SJaegeuk Kim 
35367298804SChao Yu 		list_add_tail(&e->list, &im->ino_list);
3548c402946SJaegeuk Kim 		if (type != ORPHAN_INO)
35567298804SChao Yu 			im->ino_num++;
35639efac41SJaegeuk Kim 	}
35767298804SChao Yu 	spin_unlock(&im->ino_lock);
358769ec6e5SJaegeuk Kim 	radix_tree_preload_end();
359953e6cc6SJaegeuk Kim }
360953e6cc6SJaegeuk Kim 
3616451e041SJaegeuk Kim static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
362953e6cc6SJaegeuk Kim {
36367298804SChao Yu 	struct inode_management *im = &sbi->im[type];
3646451e041SJaegeuk Kim 	struct ino_entry *e;
365953e6cc6SJaegeuk Kim 
36667298804SChao Yu 	spin_lock(&im->ino_lock);
36767298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
36839efac41SJaegeuk Kim 	if (e) {
369953e6cc6SJaegeuk Kim 		list_del(&e->list);
37067298804SChao Yu 		radix_tree_delete(&im->ino_root, ino);
37167298804SChao Yu 		im->ino_num--;
37267298804SChao Yu 		spin_unlock(&im->ino_lock);
3736451e041SJaegeuk Kim 		kmem_cache_free(ino_entry_slab, e);
374953e6cc6SJaegeuk Kim 		return;
375953e6cc6SJaegeuk Kim 	}
37667298804SChao Yu 	spin_unlock(&im->ino_lock);
377953e6cc6SJaegeuk Kim }
378953e6cc6SJaegeuk Kim 
379fff04f90SJaegeuk Kim void add_dirty_inode(struct f2fs_sb_info *sbi, nid_t ino, int type)
380fff04f90SJaegeuk Kim {
381fff04f90SJaegeuk Kim 	/* add new dirty ino entry into list */
382fff04f90SJaegeuk Kim 	__add_ino_entry(sbi, ino, type);
383fff04f90SJaegeuk Kim }
384fff04f90SJaegeuk Kim 
385fff04f90SJaegeuk Kim void remove_dirty_inode(struct f2fs_sb_info *sbi, nid_t ino, int type)
386fff04f90SJaegeuk Kim {
387fff04f90SJaegeuk Kim 	/* remove dirty ino entry from list */
388fff04f90SJaegeuk Kim 	__remove_ino_entry(sbi, ino, type);
389fff04f90SJaegeuk Kim }
390fff04f90SJaegeuk Kim 
391fff04f90SJaegeuk Kim /* mode should be APPEND_INO or UPDATE_INO */
392fff04f90SJaegeuk Kim bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode)
393fff04f90SJaegeuk Kim {
39467298804SChao Yu 	struct inode_management *im = &sbi->im[mode];
395fff04f90SJaegeuk Kim 	struct ino_entry *e;
39667298804SChao Yu 
39767298804SChao Yu 	spin_lock(&im->ino_lock);
39867298804SChao Yu 	e = radix_tree_lookup(&im->ino_root, ino);
39967298804SChao Yu 	spin_unlock(&im->ino_lock);
400fff04f90SJaegeuk Kim 	return e ? true : false;
401fff04f90SJaegeuk Kim }
402fff04f90SJaegeuk Kim 
4036f12ac25SJaegeuk Kim void release_dirty_inode(struct f2fs_sb_info *sbi)
404fff04f90SJaegeuk Kim {
405fff04f90SJaegeuk Kim 	struct ino_entry *e, *tmp;
406fff04f90SJaegeuk Kim 	int i;
407fff04f90SJaegeuk Kim 
408fff04f90SJaegeuk Kim 	for (i = APPEND_INO; i <= UPDATE_INO; i++) {
40967298804SChao Yu 		struct inode_management *im = &sbi->im[i];
41067298804SChao Yu 
41167298804SChao Yu 		spin_lock(&im->ino_lock);
41267298804SChao Yu 		list_for_each_entry_safe(e, tmp, &im->ino_list, list) {
413fff04f90SJaegeuk Kim 			list_del(&e->list);
41467298804SChao Yu 			radix_tree_delete(&im->ino_root, e->ino);
415fff04f90SJaegeuk Kim 			kmem_cache_free(ino_entry_slab, e);
41667298804SChao Yu 			im->ino_num--;
417fff04f90SJaegeuk Kim 		}
41867298804SChao Yu 		spin_unlock(&im->ino_lock);
419fff04f90SJaegeuk Kim 	}
420fff04f90SJaegeuk Kim }
421fff04f90SJaegeuk Kim 
422cbd56e7dSJaegeuk Kim int acquire_orphan_inode(struct f2fs_sb_info *sbi)
423127e670aSJaegeuk Kim {
42467298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
425127e670aSJaegeuk Kim 	int err = 0;
426127e670aSJaegeuk Kim 
42767298804SChao Yu 	spin_lock(&im->ino_lock);
42867298804SChao Yu 	if (unlikely(im->ino_num >= sbi->max_orphans))
429127e670aSJaegeuk Kim 		err = -ENOSPC;
430cbd56e7dSJaegeuk Kim 	else
43167298804SChao Yu 		im->ino_num++;
43267298804SChao Yu 	spin_unlock(&im->ino_lock);
4330d47c1adSGu Zheng 
434127e670aSJaegeuk Kim 	return err;
435127e670aSJaegeuk Kim }
436127e670aSJaegeuk Kim 
437cbd56e7dSJaegeuk Kim void release_orphan_inode(struct f2fs_sb_info *sbi)
438cbd56e7dSJaegeuk Kim {
43967298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
44067298804SChao Yu 
44167298804SChao Yu 	spin_lock(&im->ino_lock);
44267298804SChao Yu 	f2fs_bug_on(sbi, im->ino_num == 0);
44367298804SChao Yu 	im->ino_num--;
44467298804SChao Yu 	spin_unlock(&im->ino_lock);
445cbd56e7dSJaegeuk Kim }
446cbd56e7dSJaegeuk Kim 
447127e670aSJaegeuk Kim void add_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
448127e670aSJaegeuk Kim {
44939efac41SJaegeuk Kim 	/* add new orphan ino entry into list */
4506451e041SJaegeuk Kim 	__add_ino_entry(sbi, ino, ORPHAN_INO);
451127e670aSJaegeuk Kim }
452127e670aSJaegeuk Kim 
453127e670aSJaegeuk Kim void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
454127e670aSJaegeuk Kim {
455953e6cc6SJaegeuk Kim 	/* remove orphan entry from orphan list */
4566451e041SJaegeuk Kim 	__remove_ino_entry(sbi, ino, ORPHAN_INO);
457127e670aSJaegeuk Kim }
458127e670aSJaegeuk Kim 
459127e670aSJaegeuk Kim static void recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
460127e670aSJaegeuk Kim {
461127e670aSJaegeuk Kim 	struct inode *inode = f2fs_iget(sbi->sb, ino);
4629850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, IS_ERR(inode));
463127e670aSJaegeuk Kim 	clear_nlink(inode);
464127e670aSJaegeuk Kim 
465127e670aSJaegeuk Kim 	/* truncate all the data during iput */
466127e670aSJaegeuk Kim 	iput(inode);
467127e670aSJaegeuk Kim }
468127e670aSJaegeuk Kim 
4698f99a946SChao Yu void recover_orphan_inodes(struct f2fs_sb_info *sbi)
470127e670aSJaegeuk Kim {
4713c642985SWanpeng Li 	block_t start_blk, orphan_blocks, i, j;
472127e670aSJaegeuk Kim 
47325ca923bSJaegeuk Kim 	if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG))
4748f99a946SChao Yu 		return;
475127e670aSJaegeuk Kim 
476caf0047eSChao Yu 	set_sbi_flag(sbi, SBI_POR_DOING);
4771dbe4152SChangman Lee 
47855141486SWanpeng Li 	start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
4793c642985SWanpeng Li 	orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
480127e670aSJaegeuk Kim 
4813c642985SWanpeng Li 	ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP);
482662befdaSChao Yu 
4833c642985SWanpeng Li 	for (i = 0; i < orphan_blocks; i++) {
484127e670aSJaegeuk Kim 		struct page *page = get_meta_page(sbi, start_blk + i);
485127e670aSJaegeuk Kim 		struct f2fs_orphan_block *orphan_blk;
486127e670aSJaegeuk Kim 
487127e670aSJaegeuk Kim 		orphan_blk = (struct f2fs_orphan_block *)page_address(page);
488127e670aSJaegeuk Kim 		for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
489127e670aSJaegeuk Kim 			nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
490127e670aSJaegeuk Kim 			recover_orphan_inode(sbi, ino);
491127e670aSJaegeuk Kim 		}
492127e670aSJaegeuk Kim 		f2fs_put_page(page, 1);
493127e670aSJaegeuk Kim 	}
494127e670aSJaegeuk Kim 	/* clear Orphan Flag */
49525ca923bSJaegeuk Kim 	clear_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG);
496caf0047eSChao Yu 	clear_sbi_flag(sbi, SBI_POR_DOING);
4978f99a946SChao Yu 	return;
498127e670aSJaegeuk Kim }
499127e670aSJaegeuk Kim 
500127e670aSJaegeuk Kim static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
501127e670aSJaegeuk Kim {
502502c6e0bSGu Zheng 	struct list_head *head;
503127e670aSJaegeuk Kim 	struct f2fs_orphan_block *orphan_blk = NULL;
504127e670aSJaegeuk Kim 	unsigned int nentries = 0;
5054531929eSGu Zheng 	unsigned short index;
5068c402946SJaegeuk Kim 	unsigned short orphan_blocks;
5074531929eSGu Zheng 	struct page *page = NULL;
5086451e041SJaegeuk Kim 	struct ino_entry *orphan = NULL;
50967298804SChao Yu 	struct inode_management *im = &sbi->im[ORPHAN_INO];
510127e670aSJaegeuk Kim 
51167298804SChao Yu 	orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num);
5128c402946SJaegeuk Kim 
5134531929eSGu Zheng 	for (index = 0; index < orphan_blocks; index++)
51463f5384cSGu Zheng 		grab_meta_page(sbi, start_blk + index);
515127e670aSJaegeuk Kim 
5164531929eSGu Zheng 	index = 1;
51767298804SChao Yu 	spin_lock(&im->ino_lock);
51867298804SChao Yu 	head = &im->ino_list;
519127e670aSJaegeuk Kim 
520127e670aSJaegeuk Kim 	/* loop for each orphan inode entry and write them in Jornal block */
521502c6e0bSGu Zheng 	list_for_each_entry(orphan, head, list) {
522502c6e0bSGu Zheng 		if (!page) {
52363f5384cSGu Zheng 			page = find_get_page(META_MAPPING(sbi), start_blk++);
5249850cf4aSJaegeuk Kim 			f2fs_bug_on(sbi, !page);
525502c6e0bSGu Zheng 			orphan_blk =
526502c6e0bSGu Zheng 				(struct f2fs_orphan_block *)page_address(page);
527502c6e0bSGu Zheng 			memset(orphan_blk, 0, sizeof(*orphan_blk));
52863f5384cSGu Zheng 			f2fs_put_page(page, 0);
529502c6e0bSGu Zheng 		}
530127e670aSJaegeuk Kim 
53136795567SGu Zheng 		orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
532127e670aSJaegeuk Kim 
53336795567SGu Zheng 		if (nentries == F2FS_ORPHANS_PER_BLOCK) {
534127e670aSJaegeuk Kim 			/*
535127e670aSJaegeuk Kim 			 * an orphan block is full of 1020 entries,
536127e670aSJaegeuk Kim 			 * then we need to flush current orphan blocks
537127e670aSJaegeuk Kim 			 * and bring another one in memory
538127e670aSJaegeuk Kim 			 */
539127e670aSJaegeuk Kim 			orphan_blk->blk_addr = cpu_to_le16(index);
540127e670aSJaegeuk Kim 			orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
541127e670aSJaegeuk Kim 			orphan_blk->entry_count = cpu_to_le32(nentries);
542127e670aSJaegeuk Kim 			set_page_dirty(page);
543127e670aSJaegeuk Kim 			f2fs_put_page(page, 1);
544127e670aSJaegeuk Kim 			index++;
545127e670aSJaegeuk Kim 			nentries = 0;
546127e670aSJaegeuk Kim 			page = NULL;
547127e670aSJaegeuk Kim 		}
548127e670aSJaegeuk Kim 	}
549127e670aSJaegeuk Kim 
550502c6e0bSGu Zheng 	if (page) {
551127e670aSJaegeuk Kim 		orphan_blk->blk_addr = cpu_to_le16(index);
552127e670aSJaegeuk Kim 		orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
553127e670aSJaegeuk Kim 		orphan_blk->entry_count = cpu_to_le32(nentries);
554127e670aSJaegeuk Kim 		set_page_dirty(page);
555127e670aSJaegeuk Kim 		f2fs_put_page(page, 1);
556502c6e0bSGu Zheng 	}
557502c6e0bSGu Zheng 
55867298804SChao Yu 	spin_unlock(&im->ino_lock);
559127e670aSJaegeuk Kim }
560127e670aSJaegeuk Kim 
561127e670aSJaegeuk Kim static struct page *validate_checkpoint(struct f2fs_sb_info *sbi,
562127e670aSJaegeuk Kim 				block_t cp_addr, unsigned long long *version)
563127e670aSJaegeuk Kim {
564127e670aSJaegeuk Kim 	struct page *cp_page_1, *cp_page_2 = NULL;
565127e670aSJaegeuk Kim 	unsigned long blk_size = sbi->blocksize;
566127e670aSJaegeuk Kim 	struct f2fs_checkpoint *cp_block;
567127e670aSJaegeuk Kim 	unsigned long long cur_version = 0, pre_version = 0;
568127e670aSJaegeuk Kim 	size_t crc_offset;
5697e586fa0SJaegeuk Kim 	__u32 crc = 0;
570127e670aSJaegeuk Kim 
571127e670aSJaegeuk Kim 	/* Read the 1st cp block in this CP pack */
572127e670aSJaegeuk Kim 	cp_page_1 = get_meta_page(sbi, cp_addr);
573127e670aSJaegeuk Kim 
574127e670aSJaegeuk Kim 	/* get the version number */
575127e670aSJaegeuk Kim 	cp_block = (struct f2fs_checkpoint *)page_address(cp_page_1);
576127e670aSJaegeuk Kim 	crc_offset = le32_to_cpu(cp_block->checksum_offset);
577127e670aSJaegeuk Kim 	if (crc_offset >= blk_size)
578127e670aSJaegeuk Kim 		goto invalid_cp1;
579127e670aSJaegeuk Kim 
58029e7043fSJaegeuk Kim 	crc = le32_to_cpu(*((__le32 *)((unsigned char *)cp_block + crc_offset)));
581127e670aSJaegeuk Kim 	if (!f2fs_crc_valid(crc, cp_block, crc_offset))
582127e670aSJaegeuk Kim 		goto invalid_cp1;
583127e670aSJaegeuk Kim 
584d71b5564SJaegeuk Kim 	pre_version = cur_cp_version(cp_block);
585127e670aSJaegeuk Kim 
586127e670aSJaegeuk Kim 	/* Read the 2nd cp block in this CP pack */
58725ca923bSJaegeuk Kim 	cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1;
588127e670aSJaegeuk Kim 	cp_page_2 = get_meta_page(sbi, cp_addr);
589127e670aSJaegeuk Kim 
590127e670aSJaegeuk Kim 	cp_block = (struct f2fs_checkpoint *)page_address(cp_page_2);
591127e670aSJaegeuk Kim 	crc_offset = le32_to_cpu(cp_block->checksum_offset);
592127e670aSJaegeuk Kim 	if (crc_offset >= blk_size)
593127e670aSJaegeuk Kim 		goto invalid_cp2;
594127e670aSJaegeuk Kim 
59529e7043fSJaegeuk Kim 	crc = le32_to_cpu(*((__le32 *)((unsigned char *)cp_block + crc_offset)));
596127e670aSJaegeuk Kim 	if (!f2fs_crc_valid(crc, cp_block, crc_offset))
597127e670aSJaegeuk Kim 		goto invalid_cp2;
598127e670aSJaegeuk Kim 
599d71b5564SJaegeuk Kim 	cur_version = cur_cp_version(cp_block);
600127e670aSJaegeuk Kim 
601127e670aSJaegeuk Kim 	if (cur_version == pre_version) {
602127e670aSJaegeuk Kim 		*version = cur_version;
603127e670aSJaegeuk Kim 		f2fs_put_page(cp_page_2, 1);
604127e670aSJaegeuk Kim 		return cp_page_1;
605127e670aSJaegeuk Kim 	}
606127e670aSJaegeuk Kim invalid_cp2:
607127e670aSJaegeuk Kim 	f2fs_put_page(cp_page_2, 1);
608127e670aSJaegeuk Kim invalid_cp1:
609127e670aSJaegeuk Kim 	f2fs_put_page(cp_page_1, 1);
610127e670aSJaegeuk Kim 	return NULL;
611127e670aSJaegeuk Kim }
612127e670aSJaegeuk Kim 
613127e670aSJaegeuk Kim int get_valid_checkpoint(struct f2fs_sb_info *sbi)
614127e670aSJaegeuk Kim {
615127e670aSJaegeuk Kim 	struct f2fs_checkpoint *cp_block;
616127e670aSJaegeuk Kim 	struct f2fs_super_block *fsb = sbi->raw_super;
617127e670aSJaegeuk Kim 	struct page *cp1, *cp2, *cur_page;
618127e670aSJaegeuk Kim 	unsigned long blk_size = sbi->blocksize;
619127e670aSJaegeuk Kim 	unsigned long long cp1_version = 0, cp2_version = 0;
620127e670aSJaegeuk Kim 	unsigned long long cp_start_blk_no;
62155141486SWanpeng Li 	unsigned int cp_blks = 1 + __cp_payload(sbi);
6221dbe4152SChangman Lee 	block_t cp_blk_no;
6231dbe4152SChangman Lee 	int i;
624127e670aSJaegeuk Kim 
6251dbe4152SChangman Lee 	sbi->ckpt = kzalloc(cp_blks * blk_size, GFP_KERNEL);
626127e670aSJaegeuk Kim 	if (!sbi->ckpt)
627127e670aSJaegeuk Kim 		return -ENOMEM;
628127e670aSJaegeuk Kim 	/*
629127e670aSJaegeuk Kim 	 * Finding out valid cp block involves read both
630127e670aSJaegeuk Kim 	 * sets( cp pack1 and cp pack 2)
631127e670aSJaegeuk Kim 	 */
632127e670aSJaegeuk Kim 	cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr);
633127e670aSJaegeuk Kim 	cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
634127e670aSJaegeuk Kim 
635127e670aSJaegeuk Kim 	/* The second checkpoint pack should start at the next segment */
636f9a4e6dfSJaegeuk Kim 	cp_start_blk_no += ((unsigned long long)1) <<
637f9a4e6dfSJaegeuk Kim 				le32_to_cpu(fsb->log_blocks_per_seg);
638127e670aSJaegeuk Kim 	cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
639127e670aSJaegeuk Kim 
640127e670aSJaegeuk Kim 	if (cp1 && cp2) {
641127e670aSJaegeuk Kim 		if (ver_after(cp2_version, cp1_version))
642127e670aSJaegeuk Kim 			cur_page = cp2;
643127e670aSJaegeuk Kim 		else
644127e670aSJaegeuk Kim 			cur_page = cp1;
645127e670aSJaegeuk Kim 	} else if (cp1) {
646127e670aSJaegeuk Kim 		cur_page = cp1;
647127e670aSJaegeuk Kim 	} else if (cp2) {
648127e670aSJaegeuk Kim 		cur_page = cp2;
649127e670aSJaegeuk Kim 	} else {
650127e670aSJaegeuk Kim 		goto fail_no_cp;
651127e670aSJaegeuk Kim 	}
652127e670aSJaegeuk Kim 
653127e670aSJaegeuk Kim 	cp_block = (struct f2fs_checkpoint *)page_address(cur_page);
654127e670aSJaegeuk Kim 	memcpy(sbi->ckpt, cp_block, blk_size);
655127e670aSJaegeuk Kim 
6561dbe4152SChangman Lee 	if (cp_blks <= 1)
6571dbe4152SChangman Lee 		goto done;
6581dbe4152SChangman Lee 
6591dbe4152SChangman Lee 	cp_blk_no = le32_to_cpu(fsb->cp_blkaddr);
6601dbe4152SChangman Lee 	if (cur_page == cp2)
6611dbe4152SChangman Lee 		cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg);
6621dbe4152SChangman Lee 
6631dbe4152SChangman Lee 	for (i = 1; i < cp_blks; i++) {
6641dbe4152SChangman Lee 		void *sit_bitmap_ptr;
6651dbe4152SChangman Lee 		unsigned char *ckpt = (unsigned char *)sbi->ckpt;
6661dbe4152SChangman Lee 
6671dbe4152SChangman Lee 		cur_page = get_meta_page(sbi, cp_blk_no + i);
6681dbe4152SChangman Lee 		sit_bitmap_ptr = page_address(cur_page);
6691dbe4152SChangman Lee 		memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size);
6701dbe4152SChangman Lee 		f2fs_put_page(cur_page, 1);
6711dbe4152SChangman Lee 	}
6721dbe4152SChangman Lee done:
673127e670aSJaegeuk Kim 	f2fs_put_page(cp1, 1);
674127e670aSJaegeuk Kim 	f2fs_put_page(cp2, 1);
675127e670aSJaegeuk Kim 	return 0;
676127e670aSJaegeuk Kim 
677127e670aSJaegeuk Kim fail_no_cp:
678127e670aSJaegeuk Kim 	kfree(sbi->ckpt);
679127e670aSJaegeuk Kim 	return -EINVAL;
680127e670aSJaegeuk Kim }
681127e670aSJaegeuk Kim 
68206292073SChao Yu static int __add_dirty_inode(struct inode *inode, struct inode_entry *new)
683127e670aSJaegeuk Kim {
6844081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
6852d7b822aSChao Yu 
686ed57c27fSJaegeuk Kim 	if (is_inode_flag_set(F2FS_I(inode), FI_DIRTY_DIR))
6875deb8267SJaegeuk Kim 		return -EEXIST;
6882d7b822aSChao Yu 
689ed57c27fSJaegeuk Kim 	set_inode_flag(F2FS_I(inode), FI_DIRTY_DIR);
690ed57c27fSJaegeuk Kim 	F2FS_I(inode)->dirty_dir = new;
691ed57c27fSJaegeuk Kim 	list_add_tail(&new->list, &sbi->dir_inode_list);
692dcdfff65SJaegeuk Kim 	stat_inc_dirty_dir(sbi);
6935deb8267SJaegeuk Kim 	return 0;
6945deb8267SJaegeuk Kim }
6955deb8267SJaegeuk Kim 
696a7ffdbe2SJaegeuk Kim void update_dirty_page(struct inode *inode, struct page *page)
6975deb8267SJaegeuk Kim {
6984081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
69906292073SChao Yu 	struct inode_entry *new;
700cf0ee0f0SChao Yu 	int ret = 0;
7015deb8267SJaegeuk Kim 
702a7ffdbe2SJaegeuk Kim 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode))
703127e670aSJaegeuk Kim 		return;
7047bd59381SGu Zheng 
705a7ffdbe2SJaegeuk Kim 	if (!S_ISDIR(inode->i_mode)) {
706a7ffdbe2SJaegeuk Kim 		inode_inc_dirty_pages(inode);
707a7ffdbe2SJaegeuk Kim 		goto out;
708a7ffdbe2SJaegeuk Kim 	}
709a7ffdbe2SJaegeuk Kim 
7107bd59381SGu Zheng 	new = f2fs_kmem_cache_alloc(inode_entry_slab, GFP_NOFS);
711127e670aSJaegeuk Kim 	new->inode = inode;
712127e670aSJaegeuk Kim 	INIT_LIST_HEAD(&new->list);
713127e670aSJaegeuk Kim 
714127e670aSJaegeuk Kim 	spin_lock(&sbi->dir_inode_lock);
715cf0ee0f0SChao Yu 	ret = __add_dirty_inode(inode, new);
716a7ffdbe2SJaegeuk Kim 	inode_inc_dirty_pages(inode);
7175deb8267SJaegeuk Kim 	spin_unlock(&sbi->dir_inode_lock);
718cf0ee0f0SChao Yu 
719cf0ee0f0SChao Yu 	if (ret)
720cf0ee0f0SChao Yu 		kmem_cache_free(inode_entry_slab, new);
721a7ffdbe2SJaegeuk Kim out:
722a7ffdbe2SJaegeuk Kim 	SetPagePrivate(page);
7239e4ded3fSJaegeuk Kim 	f2fs_trace_pid(page);
7245deb8267SJaegeuk Kim }
725127e670aSJaegeuk Kim 
7265deb8267SJaegeuk Kim void add_dirty_dir_inode(struct inode *inode)
7275deb8267SJaegeuk Kim {
7284081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
72906292073SChao Yu 	struct inode_entry *new =
7307bd59381SGu Zheng 			f2fs_kmem_cache_alloc(inode_entry_slab, GFP_NOFS);
731cf0ee0f0SChao Yu 	int ret = 0;
7327bd59381SGu Zheng 
7335deb8267SJaegeuk Kim 	new->inode = inode;
7345deb8267SJaegeuk Kim 	INIT_LIST_HEAD(&new->list);
7355deb8267SJaegeuk Kim 
7365deb8267SJaegeuk Kim 	spin_lock(&sbi->dir_inode_lock);
737cf0ee0f0SChao Yu 	ret = __add_dirty_inode(inode, new);
738127e670aSJaegeuk Kim 	spin_unlock(&sbi->dir_inode_lock);
739cf0ee0f0SChao Yu 
740cf0ee0f0SChao Yu 	if (ret)
741cf0ee0f0SChao Yu 		kmem_cache_free(inode_entry_slab, new);
742127e670aSJaegeuk Kim }
743127e670aSJaegeuk Kim 
744127e670aSJaegeuk Kim void remove_dirty_dir_inode(struct inode *inode)
745127e670aSJaegeuk Kim {
7464081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
74706292073SChao Yu 	struct inode_entry *entry;
748127e670aSJaegeuk Kim 
749127e670aSJaegeuk Kim 	if (!S_ISDIR(inode->i_mode))
750127e670aSJaegeuk Kim 		return;
751127e670aSJaegeuk Kim 
752127e670aSJaegeuk Kim 	spin_lock(&sbi->dir_inode_lock);
753a7ffdbe2SJaegeuk Kim 	if (get_dirty_pages(inode) ||
754ed57c27fSJaegeuk Kim 			!is_inode_flag_set(F2FS_I(inode), FI_DIRTY_DIR)) {
7553b10b1fdSJaegeuk Kim 		spin_unlock(&sbi->dir_inode_lock);
7563b10b1fdSJaegeuk Kim 		return;
7573b10b1fdSJaegeuk Kim 	}
758127e670aSJaegeuk Kim 
759ed57c27fSJaegeuk Kim 	entry = F2FS_I(inode)->dirty_dir;
760127e670aSJaegeuk Kim 	list_del(&entry->list);
761ed57c27fSJaegeuk Kim 	F2FS_I(inode)->dirty_dir = NULL;
762ed57c27fSJaegeuk Kim 	clear_inode_flag(F2FS_I(inode), FI_DIRTY_DIR);
763dcdfff65SJaegeuk Kim 	stat_dec_dirty_dir(sbi);
764cf0ee0f0SChao Yu 	spin_unlock(&sbi->dir_inode_lock);
765cf0ee0f0SChao Yu 	kmem_cache_free(inode_entry_slab, entry);
76674d0b917SJaegeuk Kim 
76774d0b917SJaegeuk Kim 	/* Only from the recovery routine */
768afc3eda2SJaegeuk Kim 	if (is_inode_flag_set(F2FS_I(inode), FI_DELAY_IPUT)) {
769afc3eda2SJaegeuk Kim 		clear_inode_flag(F2FS_I(inode), FI_DELAY_IPUT);
77074d0b917SJaegeuk Kim 		iput(inode);
77174d0b917SJaegeuk Kim 	}
772afc3eda2SJaegeuk Kim }
77374d0b917SJaegeuk Kim 
774127e670aSJaegeuk Kim void sync_dirty_dir_inodes(struct f2fs_sb_info *sbi)
775127e670aSJaegeuk Kim {
776ce3b7d80SGu Zheng 	struct list_head *head;
77706292073SChao Yu 	struct inode_entry *entry;
778127e670aSJaegeuk Kim 	struct inode *inode;
779127e670aSJaegeuk Kim retry:
780af41d3eeSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
781af41d3eeSJaegeuk Kim 		return;
782af41d3eeSJaegeuk Kim 
783127e670aSJaegeuk Kim 	spin_lock(&sbi->dir_inode_lock);
784ce3b7d80SGu Zheng 
785ce3b7d80SGu Zheng 	head = &sbi->dir_inode_list;
786127e670aSJaegeuk Kim 	if (list_empty(head)) {
787127e670aSJaegeuk Kim 		spin_unlock(&sbi->dir_inode_lock);
788127e670aSJaegeuk Kim 		return;
789127e670aSJaegeuk Kim 	}
79006292073SChao Yu 	entry = list_entry(head->next, struct inode_entry, list);
791127e670aSJaegeuk Kim 	inode = igrab(entry->inode);
792127e670aSJaegeuk Kim 	spin_unlock(&sbi->dir_inode_lock);
793127e670aSJaegeuk Kim 	if (inode) {
79487d6f890SJaegeuk Kim 		filemap_fdatawrite(inode->i_mapping);
795127e670aSJaegeuk Kim 		iput(inode);
796127e670aSJaegeuk Kim 	} else {
797127e670aSJaegeuk Kim 		/*
798127e670aSJaegeuk Kim 		 * We should submit bio, since it exists several
799127e670aSJaegeuk Kim 		 * wribacking dentry pages in the freeing inode.
800127e670aSJaegeuk Kim 		 */
801458e6197SJaegeuk Kim 		f2fs_submit_merged_bio(sbi, DATA, WRITE);
8027ecebe5eSSebastian Andrzej Siewior 		cond_resched();
803127e670aSJaegeuk Kim 	}
804127e670aSJaegeuk Kim 	goto retry;
805127e670aSJaegeuk Kim }
806127e670aSJaegeuk Kim 
8070a8165d7SJaegeuk Kim /*
808127e670aSJaegeuk Kim  * Freeze all the FS-operations for checkpoint.
809127e670aSJaegeuk Kim  */
810cf779cabSJaegeuk Kim static int block_operations(struct f2fs_sb_info *sbi)
811127e670aSJaegeuk Kim {
812127e670aSJaegeuk Kim 	struct writeback_control wbc = {
813127e670aSJaegeuk Kim 		.sync_mode = WB_SYNC_ALL,
814127e670aSJaegeuk Kim 		.nr_to_write = LONG_MAX,
815127e670aSJaegeuk Kim 		.for_reclaim = 0,
816127e670aSJaegeuk Kim 	};
817c718379bSJaegeuk Kim 	struct blk_plug plug;
818cf779cabSJaegeuk Kim 	int err = 0;
819c718379bSJaegeuk Kim 
820c718379bSJaegeuk Kim 	blk_start_plug(&plug);
821c718379bSJaegeuk Kim 
82239936837SJaegeuk Kim retry_flush_dents:
823e479556bSGu Zheng 	f2fs_lock_all(sbi);
824127e670aSJaegeuk Kim 	/* write all the dirty dentry pages */
825127e670aSJaegeuk Kim 	if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
826e479556bSGu Zheng 		f2fs_unlock_all(sbi);
82739936837SJaegeuk Kim 		sync_dirty_dir_inodes(sbi);
828cf779cabSJaegeuk Kim 		if (unlikely(f2fs_cp_error(sbi))) {
829cf779cabSJaegeuk Kim 			err = -EIO;
830cf779cabSJaegeuk Kim 			goto out;
831cf779cabSJaegeuk Kim 		}
83239936837SJaegeuk Kim 		goto retry_flush_dents;
833127e670aSJaegeuk Kim 	}
834127e670aSJaegeuk Kim 
835127e670aSJaegeuk Kim 	/*
836e1c42045Sarter97 	 * POR: we should ensure that there are no dirty node pages
837127e670aSJaegeuk Kim 	 * until finishing nat/sit flush.
838127e670aSJaegeuk Kim 	 */
83939936837SJaegeuk Kim retry_flush_nodes:
840b3582c68SChao Yu 	down_write(&sbi->node_write);
841127e670aSJaegeuk Kim 
842127e670aSJaegeuk Kim 	if (get_pages(sbi, F2FS_DIRTY_NODES)) {
843b3582c68SChao Yu 		up_write(&sbi->node_write);
84439936837SJaegeuk Kim 		sync_node_pages(sbi, 0, &wbc);
845cf779cabSJaegeuk Kim 		if (unlikely(f2fs_cp_error(sbi))) {
846cf779cabSJaegeuk Kim 			f2fs_unlock_all(sbi);
847cf779cabSJaegeuk Kim 			err = -EIO;
848cf779cabSJaegeuk Kim 			goto out;
849cf779cabSJaegeuk Kim 		}
85039936837SJaegeuk Kim 		goto retry_flush_nodes;
851127e670aSJaegeuk Kim 	}
852cf779cabSJaegeuk Kim out:
853c718379bSJaegeuk Kim 	blk_finish_plug(&plug);
854cf779cabSJaegeuk Kim 	return err;
855127e670aSJaegeuk Kim }
856127e670aSJaegeuk Kim 
857127e670aSJaegeuk Kim static void unblock_operations(struct f2fs_sb_info *sbi)
858127e670aSJaegeuk Kim {
859b3582c68SChao Yu 	up_write(&sbi->node_write);
860e479556bSGu Zheng 	f2fs_unlock_all(sbi);
861127e670aSJaegeuk Kim }
862127e670aSJaegeuk Kim 
863fb51b5efSChangman Lee static void wait_on_all_pages_writeback(struct f2fs_sb_info *sbi)
864fb51b5efSChangman Lee {
865fb51b5efSChangman Lee 	DEFINE_WAIT(wait);
866fb51b5efSChangman Lee 
867fb51b5efSChangman Lee 	for (;;) {
868fb51b5efSChangman Lee 		prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
869fb51b5efSChangman Lee 
870fb51b5efSChangman Lee 		if (!get_pages(sbi, F2FS_WRITEBACK))
871fb51b5efSChangman Lee 			break;
872fb51b5efSChangman Lee 
873fb51b5efSChangman Lee 		io_schedule();
874fb51b5efSChangman Lee 	}
875fb51b5efSChangman Lee 	finish_wait(&sbi->cp_wait, &wait);
876fb51b5efSChangman Lee }
877fb51b5efSChangman Lee 
87875ab4cb8SJaegeuk Kim static void do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
879127e670aSJaegeuk Kim {
880127e670aSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
881cf2271e7SJaegeuk Kim 	struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
88277041823SHuang Ying 	struct f2fs_nm_info *nm_i = NM_I(sbi);
88367298804SChao Yu 	unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num;
88477041823SHuang Ying 	nid_t last_nid = nm_i->next_scan_nid;
885127e670aSJaegeuk Kim 	block_t start_blk;
886127e670aSJaegeuk Kim 	struct page *cp_page;
887127e670aSJaegeuk Kim 	unsigned int data_sum_blocks, orphan_blocks;
8887e586fa0SJaegeuk Kim 	__u32 crc32 = 0;
889127e670aSJaegeuk Kim 	void *kaddr;
890127e670aSJaegeuk Kim 	int i;
89155141486SWanpeng Li 	int cp_payload_blks = __cp_payload(sbi);
892127e670aSJaegeuk Kim 
8931e87a78dSJaegeuk Kim 	/*
8941e87a78dSJaegeuk Kim 	 * This avoids to conduct wrong roll-forward operations and uses
8951e87a78dSJaegeuk Kim 	 * metapages, so should be called prior to sync_meta_pages below.
8961e87a78dSJaegeuk Kim 	 */
897cf2271e7SJaegeuk Kim 	discard_next_dnode(sbi, NEXT_FREE_BLKADDR(sbi, curseg));
898127e670aSJaegeuk Kim 
899127e670aSJaegeuk Kim 	/* Flush all the NAT/SIT pages */
900cf779cabSJaegeuk Kim 	while (get_pages(sbi, F2FS_DIRTY_META)) {
901127e670aSJaegeuk Kim 		sync_meta_pages(sbi, META, LONG_MAX);
902cf779cabSJaegeuk Kim 		if (unlikely(f2fs_cp_error(sbi)))
903cf779cabSJaegeuk Kim 			return;
904cf779cabSJaegeuk Kim 	}
905127e670aSJaegeuk Kim 
906127e670aSJaegeuk Kim 	next_free_nid(sbi, &last_nid);
907127e670aSJaegeuk Kim 
908127e670aSJaegeuk Kim 	/*
909127e670aSJaegeuk Kim 	 * modify checkpoint
910127e670aSJaegeuk Kim 	 * version number is already updated
911127e670aSJaegeuk Kim 	 */
912127e670aSJaegeuk Kim 	ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi));
913127e670aSJaegeuk Kim 	ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
914127e670aSJaegeuk Kim 	ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
915b5b82205SChao Yu 	for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
916127e670aSJaegeuk Kim 		ckpt->cur_node_segno[i] =
917127e670aSJaegeuk Kim 			cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE));
918127e670aSJaegeuk Kim 		ckpt->cur_node_blkoff[i] =
919127e670aSJaegeuk Kim 			cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE));
920127e670aSJaegeuk Kim 		ckpt->alloc_type[i + CURSEG_HOT_NODE] =
921127e670aSJaegeuk Kim 				curseg_alloc_type(sbi, i + CURSEG_HOT_NODE);
922127e670aSJaegeuk Kim 	}
923b5b82205SChao Yu 	for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
924127e670aSJaegeuk Kim 		ckpt->cur_data_segno[i] =
925127e670aSJaegeuk Kim 			cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA));
926127e670aSJaegeuk Kim 		ckpt->cur_data_blkoff[i] =
927127e670aSJaegeuk Kim 			cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA));
928127e670aSJaegeuk Kim 		ckpt->alloc_type[i + CURSEG_HOT_DATA] =
929127e670aSJaegeuk Kim 				curseg_alloc_type(sbi, i + CURSEG_HOT_DATA);
930127e670aSJaegeuk Kim 	}
931127e670aSJaegeuk Kim 
932127e670aSJaegeuk Kim 	ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi));
933127e670aSJaegeuk Kim 	ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi));
934127e670aSJaegeuk Kim 	ckpt->next_free_nid = cpu_to_le32(last_nid);
935127e670aSJaegeuk Kim 
936127e670aSJaegeuk Kim 	/* 2 cp  + n data seg summary + orphan inode blocks */
9373fa06d7bSChao Yu 	data_sum_blocks = npages_for_summary_flush(sbi, false);
938b5b82205SChao Yu 	if (data_sum_blocks < NR_CURSEG_DATA_TYPE)
93925ca923bSJaegeuk Kim 		set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
940127e670aSJaegeuk Kim 	else
94125ca923bSJaegeuk Kim 		clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
942127e670aSJaegeuk Kim 
94367298804SChao Yu 	orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num);
9441dbe4152SChangman Lee 	ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks +
9451dbe4152SChangman Lee 			orphan_blocks);
946127e670aSJaegeuk Kim 
947119ee914SJaegeuk Kim 	if (__remain_node_summaries(cpc->reason))
948b5b82205SChao Yu 		ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS+
9491dbe4152SChangman Lee 				cp_payload_blks + data_sum_blocks +
9501dbe4152SChangman Lee 				orphan_blocks + NR_CURSEG_NODE_TYPE);
951119ee914SJaegeuk Kim 	else
952b5b82205SChao Yu 		ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
9531dbe4152SChangman Lee 				cp_payload_blks + data_sum_blocks +
9541dbe4152SChangman Lee 				orphan_blocks);
955119ee914SJaegeuk Kim 
956119ee914SJaegeuk Kim 	if (cpc->reason == CP_UMOUNT)
957119ee914SJaegeuk Kim 		set_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
958119ee914SJaegeuk Kim 	else
959119ee914SJaegeuk Kim 		clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
960119ee914SJaegeuk Kim 
961119ee914SJaegeuk Kim 	if (cpc->reason == CP_FASTBOOT)
962119ee914SJaegeuk Kim 		set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
963119ee914SJaegeuk Kim 	else
964119ee914SJaegeuk Kim 		clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
965127e670aSJaegeuk Kim 
96667298804SChao Yu 	if (orphan_num)
96725ca923bSJaegeuk Kim 		set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
968127e670aSJaegeuk Kim 	else
96925ca923bSJaegeuk Kim 		clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
970127e670aSJaegeuk Kim 
971caf0047eSChao Yu 	if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
9722ae4c673SJaegeuk Kim 		set_ckpt_flags(ckpt, CP_FSCK_FLAG);
9732ae4c673SJaegeuk Kim 
974127e670aSJaegeuk Kim 	/* update SIT/NAT bitmap */
975127e670aSJaegeuk Kim 	get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
976127e670aSJaegeuk Kim 	get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
977127e670aSJaegeuk Kim 
978127e670aSJaegeuk Kim 	crc32 = f2fs_crc32(ckpt, le32_to_cpu(ckpt->checksum_offset));
9797e586fa0SJaegeuk Kim 	*((__le32 *)((unsigned char *)ckpt +
9807e586fa0SJaegeuk Kim 				le32_to_cpu(ckpt->checksum_offset)))
981127e670aSJaegeuk Kim 				= cpu_to_le32(crc32);
982127e670aSJaegeuk Kim 
983127e670aSJaegeuk Kim 	start_blk = __start_cp_addr(sbi);
984127e670aSJaegeuk Kim 
985127e670aSJaegeuk Kim 	/* write out checkpoint buffer at block 0 */
986127e670aSJaegeuk Kim 	cp_page = grab_meta_page(sbi, start_blk++);
987127e670aSJaegeuk Kim 	kaddr = page_address(cp_page);
988f7ef9b83SJaegeuk Kim 	memcpy(kaddr, ckpt, F2FS_BLKSIZE);
989127e670aSJaegeuk Kim 	set_page_dirty(cp_page);
990127e670aSJaegeuk Kim 	f2fs_put_page(cp_page, 1);
991127e670aSJaegeuk Kim 
9921dbe4152SChangman Lee 	for (i = 1; i < 1 + cp_payload_blks; i++) {
9931dbe4152SChangman Lee 		cp_page = grab_meta_page(sbi, start_blk++);
9941dbe4152SChangman Lee 		kaddr = page_address(cp_page);
995f7ef9b83SJaegeuk Kim 		memcpy(kaddr, (char *)ckpt + i * F2FS_BLKSIZE, F2FS_BLKSIZE);
9961dbe4152SChangman Lee 		set_page_dirty(cp_page);
9971dbe4152SChangman Lee 		f2fs_put_page(cp_page, 1);
9981dbe4152SChangman Lee 	}
9991dbe4152SChangman Lee 
100067298804SChao Yu 	if (orphan_num) {
1001127e670aSJaegeuk Kim 		write_orphan_inodes(sbi, start_blk);
1002127e670aSJaegeuk Kim 		start_blk += orphan_blocks;
1003127e670aSJaegeuk Kim 	}
1004127e670aSJaegeuk Kim 
1005127e670aSJaegeuk Kim 	write_data_summaries(sbi, start_blk);
1006127e670aSJaegeuk Kim 	start_blk += data_sum_blocks;
1007119ee914SJaegeuk Kim 	if (__remain_node_summaries(cpc->reason)) {
1008127e670aSJaegeuk Kim 		write_node_summaries(sbi, start_blk);
1009127e670aSJaegeuk Kim 		start_blk += NR_CURSEG_NODE_TYPE;
1010127e670aSJaegeuk Kim 	}
1011127e670aSJaegeuk Kim 
1012127e670aSJaegeuk Kim 	/* writeout checkpoint block */
1013127e670aSJaegeuk Kim 	cp_page = grab_meta_page(sbi, start_blk);
1014127e670aSJaegeuk Kim 	kaddr = page_address(cp_page);
1015f7ef9b83SJaegeuk Kim 	memcpy(kaddr, ckpt, F2FS_BLKSIZE);
1016127e670aSJaegeuk Kim 	set_page_dirty(cp_page);
1017127e670aSJaegeuk Kim 	f2fs_put_page(cp_page, 1);
1018127e670aSJaegeuk Kim 
1019127e670aSJaegeuk Kim 	/* wait for previous submitted node/meta pages writeback */
1020fb51b5efSChangman Lee 	wait_on_all_pages_writeback(sbi);
1021127e670aSJaegeuk Kim 
1022cf779cabSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
1023cf779cabSJaegeuk Kim 		return;
1024cf779cabSJaegeuk Kim 
10254ef51a8fSJaegeuk Kim 	filemap_fdatawait_range(NODE_MAPPING(sbi), 0, LONG_MAX);
10269df27d98SGu Zheng 	filemap_fdatawait_range(META_MAPPING(sbi), 0, LONG_MAX);
1027127e670aSJaegeuk Kim 
1028127e670aSJaegeuk Kim 	/* update user_block_counts */
1029127e670aSJaegeuk Kim 	sbi->last_valid_block_count = sbi->total_valid_block_count;
1030127e670aSJaegeuk Kim 	sbi->alloc_valid_block_count = 0;
1031127e670aSJaegeuk Kim 
1032127e670aSJaegeuk Kim 	/* Here, we only have one bio having CP pack */
1033127e670aSJaegeuk Kim 	sync_meta_pages(sbi, META_FLUSH, LONG_MAX);
1034127e670aSJaegeuk Kim 
10356a8f8ca5SJaegeuk Kim 	/* wait for previous submitted meta pages writeback */
10366a8f8ca5SJaegeuk Kim 	wait_on_all_pages_writeback(sbi);
10376a8f8ca5SJaegeuk Kim 
1038fff04f90SJaegeuk Kim 	release_dirty_inode(sbi);
1039cf779cabSJaegeuk Kim 
1040cf779cabSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
1041cf779cabSJaegeuk Kim 		return;
1042cf779cabSJaegeuk Kim 
1043cf779cabSJaegeuk Kim 	clear_prefree_segments(sbi);
1044caf0047eSChao Yu 	clear_sbi_flag(sbi, SBI_IS_DIRTY);
1045127e670aSJaegeuk Kim }
1046127e670aSJaegeuk Kim 
10470a8165d7SJaegeuk Kim /*
1048e1c42045Sarter97  * We guarantee that this checkpoint procedure will not fail.
1049127e670aSJaegeuk Kim  */
105075ab4cb8SJaegeuk Kim void write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1051127e670aSJaegeuk Kim {
1052127e670aSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1053127e670aSJaegeuk Kim 	unsigned long long ckpt_ver;
1054127e670aSJaegeuk Kim 
1055127e670aSJaegeuk Kim 	mutex_lock(&sbi->cp_mutex);
10568501017eSJaegeuk Kim 
1057caf0047eSChao Yu 	if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) &&
105810027551SJaegeuk Kim 		(cpc->reason == CP_FASTBOOT || cpc->reason == CP_SYNC))
10598501017eSJaegeuk Kim 		goto out;
1060cf779cabSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
1061cf779cabSJaegeuk Kim 		goto out;
106211504a8eSJaegeuk Kim 	if (f2fs_readonly(sbi->sb))
106311504a8eSJaegeuk Kim 		goto out;
10642bda542dSWanpeng Li 
10652bda542dSWanpeng Li 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops");
10662bda542dSWanpeng Li 
1067cf779cabSJaegeuk Kim 	if (block_operations(sbi))
1068cf779cabSJaegeuk Kim 		goto out;
1069127e670aSJaegeuk Kim 
107075ab4cb8SJaegeuk Kim 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops");
10712af4bd6cSNamjae Jeon 
1072458e6197SJaegeuk Kim 	f2fs_submit_merged_bio(sbi, DATA, WRITE);
1073458e6197SJaegeuk Kim 	f2fs_submit_merged_bio(sbi, NODE, WRITE);
1074458e6197SJaegeuk Kim 	f2fs_submit_merged_bio(sbi, META, WRITE);
1075127e670aSJaegeuk Kim 
1076127e670aSJaegeuk Kim 	/*
1077127e670aSJaegeuk Kim 	 * update checkpoint pack index
1078127e670aSJaegeuk Kim 	 * Increase the version number so that
1079127e670aSJaegeuk Kim 	 * SIT entries and seg summaries are written at correct place
1080127e670aSJaegeuk Kim 	 */
1081d71b5564SJaegeuk Kim 	ckpt_ver = cur_cp_version(ckpt);
1082127e670aSJaegeuk Kim 	ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
1083127e670aSJaegeuk Kim 
1084127e670aSJaegeuk Kim 	/* write cached NAT/SIT entries to NAT/SIT area */
1085127e670aSJaegeuk Kim 	flush_nat_entries(sbi);
10864b2fecc8SJaegeuk Kim 	flush_sit_entries(sbi, cpc);
1087127e670aSJaegeuk Kim 
1088127e670aSJaegeuk Kim 	/* unlock all the fs_lock[] in do_checkpoint() */
108975ab4cb8SJaegeuk Kim 	do_checkpoint(sbi, cpc);
1090127e670aSJaegeuk Kim 
1091127e670aSJaegeuk Kim 	unblock_operations(sbi);
1092942e0be6SChangman Lee 	stat_inc_cp_count(sbi->stat_info);
109310027551SJaegeuk Kim 
109410027551SJaegeuk Kim 	if (cpc->reason == CP_RECOVERY)
109510027551SJaegeuk Kim 		f2fs_msg(sbi->sb, KERN_NOTICE,
109610027551SJaegeuk Kim 			"checkpoint: version = %llx", ckpt_ver);
10978501017eSJaegeuk Kim out:
10988501017eSJaegeuk Kim 	mutex_unlock(&sbi->cp_mutex);
109975ab4cb8SJaegeuk Kim 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint");
1100127e670aSJaegeuk Kim }
1101127e670aSJaegeuk Kim 
11026451e041SJaegeuk Kim void init_ino_entry_info(struct f2fs_sb_info *sbi)
1103127e670aSJaegeuk Kim {
11046451e041SJaegeuk Kim 	int i;
11056451e041SJaegeuk Kim 
11066451e041SJaegeuk Kim 	for (i = 0; i < MAX_INO_ENTRY; i++) {
110767298804SChao Yu 		struct inode_management *im = &sbi->im[i];
110867298804SChao Yu 
110967298804SChao Yu 		INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC);
111067298804SChao Yu 		spin_lock_init(&im->ino_lock);
111167298804SChao Yu 		INIT_LIST_HEAD(&im->ino_list);
111267298804SChao Yu 		im->ino_num = 0;
11136451e041SJaegeuk Kim 	}
11146451e041SJaegeuk Kim 
1115b5b82205SChao Yu 	sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS -
111614b42817SWanpeng Li 			NR_CURSEG_TYPE - __cp_payload(sbi)) *
111714b42817SWanpeng Li 				F2FS_ORPHANS_PER_BLOCK;
1118127e670aSJaegeuk Kim }
1119127e670aSJaegeuk Kim 
11206e6093a8SNamjae Jeon int __init create_checkpoint_caches(void)
1121127e670aSJaegeuk Kim {
11226451e041SJaegeuk Kim 	ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry",
11236451e041SJaegeuk Kim 			sizeof(struct ino_entry));
11246451e041SJaegeuk Kim 	if (!ino_entry_slab)
1125127e670aSJaegeuk Kim 		return -ENOMEM;
112606292073SChao Yu 	inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry",
112706292073SChao Yu 			sizeof(struct inode_entry));
11286bacf52fSJaegeuk Kim 	if (!inode_entry_slab) {
11296451e041SJaegeuk Kim 		kmem_cache_destroy(ino_entry_slab);
1130127e670aSJaegeuk Kim 		return -ENOMEM;
1131127e670aSJaegeuk Kim 	}
1132127e670aSJaegeuk Kim 	return 0;
1133127e670aSJaegeuk Kim }
1134127e670aSJaegeuk Kim 
1135127e670aSJaegeuk Kim void destroy_checkpoint_caches(void)
1136127e670aSJaegeuk Kim {
11376451e041SJaegeuk Kim 	kmem_cache_destroy(ino_entry_slab);
1138127e670aSJaegeuk Kim 	kmem_cache_destroy(inode_entry_slab);
1139127e670aSJaegeuk Kim }
1140