xref: /openbmc/linux/fs/f2fs/checkpoint.c (revision cfb271d485d0ec31eb92b51f4fbe54bf6542e8e6)
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"
232af4bd6cSNamjae Jeon #include <trace/events/f2fs.h>
24127e670aSJaegeuk Kim 
25127e670aSJaegeuk Kim static struct kmem_cache *orphan_entry_slab;
26127e670aSJaegeuk Kim static struct kmem_cache *inode_entry_slab;
27127e670aSJaegeuk Kim 
280a8165d7SJaegeuk Kim /*
29127e670aSJaegeuk Kim  * We guarantee no failure on the returned page.
30127e670aSJaegeuk Kim  */
31127e670aSJaegeuk Kim struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
32127e670aSJaegeuk Kim {
33127e670aSJaegeuk Kim 	struct address_space *mapping = sbi->meta_inode->i_mapping;
34127e670aSJaegeuk Kim 	struct page *page = NULL;
35127e670aSJaegeuk Kim repeat:
36127e670aSJaegeuk Kim 	page = grab_cache_page(mapping, index);
37127e670aSJaegeuk Kim 	if (!page) {
38127e670aSJaegeuk Kim 		cond_resched();
39127e670aSJaegeuk Kim 		goto repeat;
40127e670aSJaegeuk Kim 	}
41127e670aSJaegeuk Kim 
42127e670aSJaegeuk Kim 	/* We wait writeback only inside grab_meta_page() */
43127e670aSJaegeuk Kim 	wait_on_page_writeback(page);
44127e670aSJaegeuk Kim 	SetPageUptodate(page);
45127e670aSJaegeuk Kim 	return page;
46127e670aSJaegeuk Kim }
47127e670aSJaegeuk Kim 
480a8165d7SJaegeuk Kim /*
49127e670aSJaegeuk Kim  * We guarantee no failure on the returned page.
50127e670aSJaegeuk Kim  */
51127e670aSJaegeuk Kim struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
52127e670aSJaegeuk Kim {
53127e670aSJaegeuk Kim 	struct address_space *mapping = sbi->meta_inode->i_mapping;
54127e670aSJaegeuk Kim 	struct page *page;
55127e670aSJaegeuk Kim repeat:
56127e670aSJaegeuk Kim 	page = grab_cache_page(mapping, index);
57127e670aSJaegeuk Kim 	if (!page) {
58127e670aSJaegeuk Kim 		cond_resched();
59127e670aSJaegeuk Kim 		goto repeat;
60127e670aSJaegeuk Kim 	}
61393ff91fSJaegeuk Kim 	if (PageUptodate(page))
62393ff91fSJaegeuk Kim 		goto out;
63127e670aSJaegeuk Kim 
6493dfe2acSJaegeuk Kim 	if (f2fs_submit_page_bio(sbi, page, index,
6593dfe2acSJaegeuk Kim 				READ_SYNC | REQ_META | REQ_PRIO))
66393ff91fSJaegeuk Kim 		goto repeat;
67393ff91fSJaegeuk Kim 
68393ff91fSJaegeuk Kim 	lock_page(page);
69afcb7ca0SJaegeuk Kim 	if (page->mapping != mapping) {
70afcb7ca0SJaegeuk Kim 		f2fs_put_page(page, 1);
71afcb7ca0SJaegeuk Kim 		goto repeat;
72afcb7ca0SJaegeuk Kim 	}
73393ff91fSJaegeuk Kim out:
74393ff91fSJaegeuk Kim 	mark_page_accessed(page);
75127e670aSJaegeuk Kim 	return page;
76127e670aSJaegeuk Kim }
77127e670aSJaegeuk Kim 
78127e670aSJaegeuk Kim static int f2fs_write_meta_page(struct page *page,
79127e670aSJaegeuk Kim 				struct writeback_control *wbc)
80127e670aSJaegeuk Kim {
81127e670aSJaegeuk Kim 	struct inode *inode = page->mapping->host;
82127e670aSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
83577e3495SJaegeuk Kim 
84577e3495SJaegeuk Kim 	/* Should not write any meta pages, if any IO error was occurred */
85*cfb271d4SChao Yu 	if (unlikely(sbi->por_doing ||
86*cfb271d4SChao Yu 			is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ERROR_FLAG)))
87*cfb271d4SChao Yu 		goto redirty_out;
88*cfb271d4SChao Yu 
89*cfb271d4SChao Yu 	if (wbc->for_reclaim)
90*cfb271d4SChao Yu 		goto redirty_out;
91127e670aSJaegeuk Kim 
92127e670aSJaegeuk Kim 	wait_on_page_writeback(page);
93127e670aSJaegeuk Kim 
94577e3495SJaegeuk Kim 	write_meta_page(sbi, page);
95127e670aSJaegeuk Kim 	dec_page_count(sbi, F2FS_DIRTY_META);
96127e670aSJaegeuk Kim 	unlock_page(page);
97577e3495SJaegeuk Kim 	return 0;
98*cfb271d4SChao Yu 
99*cfb271d4SChao Yu redirty_out:
100*cfb271d4SChao Yu 	dec_page_count(sbi, F2FS_DIRTY_META);
101*cfb271d4SChao Yu 	wbc->pages_skipped++;
102*cfb271d4SChao Yu 	set_page_dirty(page);
103*cfb271d4SChao Yu 	return AOP_WRITEPAGE_ACTIVATE;
104127e670aSJaegeuk Kim }
105127e670aSJaegeuk Kim 
106127e670aSJaegeuk Kim static int f2fs_write_meta_pages(struct address_space *mapping,
107127e670aSJaegeuk Kim 				struct writeback_control *wbc)
108127e670aSJaegeuk Kim {
109127e670aSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
110127e670aSJaegeuk Kim 	struct block_device *bdev = sbi->sb->s_bdev;
111127e670aSJaegeuk Kim 	long written;
112127e670aSJaegeuk Kim 
113127e670aSJaegeuk Kim 	if (wbc->for_kupdate)
114127e670aSJaegeuk Kim 		return 0;
115127e670aSJaegeuk Kim 
116127e670aSJaegeuk Kim 	if (get_pages(sbi, F2FS_DIRTY_META) == 0)
117127e670aSJaegeuk Kim 		return 0;
118127e670aSJaegeuk Kim 
119127e670aSJaegeuk Kim 	/* if mounting is failed, skip writing node pages */
120127e670aSJaegeuk Kim 	mutex_lock(&sbi->cp_mutex);
121127e670aSJaegeuk Kim 	written = sync_meta_pages(sbi, META, bio_get_nr_vecs(bdev));
122127e670aSJaegeuk Kim 	mutex_unlock(&sbi->cp_mutex);
123127e670aSJaegeuk Kim 	wbc->nr_to_write -= written;
124127e670aSJaegeuk Kim 	return 0;
125127e670aSJaegeuk Kim }
126127e670aSJaegeuk Kim 
127127e670aSJaegeuk Kim long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
128127e670aSJaegeuk Kim 						long nr_to_write)
129127e670aSJaegeuk Kim {
130127e670aSJaegeuk Kim 	struct address_space *mapping = sbi->meta_inode->i_mapping;
131127e670aSJaegeuk Kim 	pgoff_t index = 0, end = LONG_MAX;
132127e670aSJaegeuk Kim 	struct pagevec pvec;
133127e670aSJaegeuk Kim 	long nwritten = 0;
134127e670aSJaegeuk Kim 	struct writeback_control wbc = {
135127e670aSJaegeuk Kim 		.for_reclaim = 0,
136127e670aSJaegeuk Kim 	};
137127e670aSJaegeuk Kim 
138127e670aSJaegeuk Kim 	pagevec_init(&pvec, 0);
139127e670aSJaegeuk Kim 
140127e670aSJaegeuk Kim 	while (index <= end) {
141127e670aSJaegeuk Kim 		int i, nr_pages;
142127e670aSJaegeuk Kim 		nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
143127e670aSJaegeuk Kim 				PAGECACHE_TAG_DIRTY,
144127e670aSJaegeuk Kim 				min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
145*cfb271d4SChao Yu 		if (unlikely(nr_pages == 0))
146127e670aSJaegeuk Kim 			break;
147127e670aSJaegeuk Kim 
148127e670aSJaegeuk Kim 		for (i = 0; i < nr_pages; i++) {
149127e670aSJaegeuk Kim 			struct page *page = pvec.pages[i];
150127e670aSJaegeuk Kim 			lock_page(page);
1515d56b671SJaegeuk Kim 			f2fs_bug_on(page->mapping != mapping);
1525d56b671SJaegeuk Kim 			f2fs_bug_on(!PageDirty(page));
153127e670aSJaegeuk Kim 			clear_page_dirty_for_io(page);
154577e3495SJaegeuk Kim 			if (f2fs_write_meta_page(page, &wbc)) {
155577e3495SJaegeuk Kim 				unlock_page(page);
156577e3495SJaegeuk Kim 				break;
157577e3495SJaegeuk Kim 			}
158*cfb271d4SChao Yu 			nwritten++;
159*cfb271d4SChao Yu 			if (unlikely(nwritten >= nr_to_write))
160127e670aSJaegeuk Kim 				break;
161127e670aSJaegeuk Kim 		}
162127e670aSJaegeuk Kim 		pagevec_release(&pvec);
163127e670aSJaegeuk Kim 		cond_resched();
164127e670aSJaegeuk Kim 	}
165127e670aSJaegeuk Kim 
166127e670aSJaegeuk Kim 	if (nwritten)
16793dfe2acSJaegeuk Kim 		f2fs_submit_merged_bio(sbi, type, nr_to_write == LONG_MAX,
16893dfe2acSJaegeuk Kim 								WRITE);
169127e670aSJaegeuk Kim 
170127e670aSJaegeuk Kim 	return nwritten;
171127e670aSJaegeuk Kim }
172127e670aSJaegeuk Kim 
173127e670aSJaegeuk Kim static int f2fs_set_meta_page_dirty(struct page *page)
174127e670aSJaegeuk Kim {
175127e670aSJaegeuk Kim 	struct address_space *mapping = page->mapping;
176127e670aSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
177127e670aSJaegeuk Kim 
17826c6b887SJaegeuk Kim 	trace_f2fs_set_page_dirty(page, META);
17926c6b887SJaegeuk Kim 
180127e670aSJaegeuk Kim 	SetPageUptodate(page);
181127e670aSJaegeuk Kim 	if (!PageDirty(page)) {
182127e670aSJaegeuk Kim 		__set_page_dirty_nobuffers(page);
183127e670aSJaegeuk Kim 		inc_page_count(sbi, F2FS_DIRTY_META);
184127e670aSJaegeuk Kim 		return 1;
185127e670aSJaegeuk Kim 	}
186127e670aSJaegeuk Kim 	return 0;
187127e670aSJaegeuk Kim }
188127e670aSJaegeuk Kim 
189127e670aSJaegeuk Kim const struct address_space_operations f2fs_meta_aops = {
190127e670aSJaegeuk Kim 	.writepage	= f2fs_write_meta_page,
191127e670aSJaegeuk Kim 	.writepages	= f2fs_write_meta_pages,
192127e670aSJaegeuk Kim 	.set_page_dirty	= f2fs_set_meta_page_dirty,
193127e670aSJaegeuk Kim };
194127e670aSJaegeuk Kim 
195cbd56e7dSJaegeuk Kim int acquire_orphan_inode(struct f2fs_sb_info *sbi)
196127e670aSJaegeuk Kim {
197127e670aSJaegeuk Kim 	unsigned int max_orphans;
198127e670aSJaegeuk Kim 	int err = 0;
199127e670aSJaegeuk Kim 
200127e670aSJaegeuk Kim 	/*
2016947eea9SChao Yu 	 * considering 512 blocks in a segment 8 blocks are needed for cp
202127e670aSJaegeuk Kim 	 * and log segment summaries. Remaining blocks are used to keep
203127e670aSJaegeuk Kim 	 * orphan entries with the limitation one reserved segment
2046947eea9SChao Yu 	 * for cp pack we can have max 1020*504 orphan entries
205127e670aSJaegeuk Kim 	 */
2066947eea9SChao Yu 	max_orphans = (sbi->blocks_per_seg - 2 - NR_CURSEG_TYPE)
2076947eea9SChao Yu 				* F2FS_ORPHANS_PER_BLOCK;
208127e670aSJaegeuk Kim 	mutex_lock(&sbi->orphan_inode_mutex);
209*cfb271d4SChao Yu 	if (unlikely(sbi->n_orphans >= max_orphans))
210127e670aSJaegeuk Kim 		err = -ENOSPC;
211cbd56e7dSJaegeuk Kim 	else
212cbd56e7dSJaegeuk Kim 		sbi->n_orphans++;
213127e670aSJaegeuk Kim 	mutex_unlock(&sbi->orphan_inode_mutex);
214127e670aSJaegeuk Kim 	return err;
215127e670aSJaegeuk Kim }
216127e670aSJaegeuk Kim 
217cbd56e7dSJaegeuk Kim void release_orphan_inode(struct f2fs_sb_info *sbi)
218cbd56e7dSJaegeuk Kim {
219cbd56e7dSJaegeuk Kim 	mutex_lock(&sbi->orphan_inode_mutex);
2205d56b671SJaegeuk Kim 	f2fs_bug_on(sbi->n_orphans == 0);
221cbd56e7dSJaegeuk Kim 	sbi->n_orphans--;
222cbd56e7dSJaegeuk Kim 	mutex_unlock(&sbi->orphan_inode_mutex);
223cbd56e7dSJaegeuk Kim }
224cbd56e7dSJaegeuk Kim 
225127e670aSJaegeuk Kim void add_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
226127e670aSJaegeuk Kim {
227127e670aSJaegeuk Kim 	struct list_head *head, *this;
228127e670aSJaegeuk Kim 	struct orphan_inode_entry *new = NULL, *orphan = NULL;
229127e670aSJaegeuk Kim 
230127e670aSJaegeuk Kim 	mutex_lock(&sbi->orphan_inode_mutex);
231127e670aSJaegeuk Kim 	head = &sbi->orphan_inode_list;
232127e670aSJaegeuk Kim 	list_for_each(this, head) {
233127e670aSJaegeuk Kim 		orphan = list_entry(this, struct orphan_inode_entry, list);
234127e670aSJaegeuk Kim 		if (orphan->ino == ino)
235127e670aSJaegeuk Kim 			goto out;
236127e670aSJaegeuk Kim 		if (orphan->ino > ino)
237127e670aSJaegeuk Kim 			break;
238127e670aSJaegeuk Kim 		orphan = NULL;
239127e670aSJaegeuk Kim 	}
2407bd59381SGu Zheng 
2417bd59381SGu Zheng 	new = f2fs_kmem_cache_alloc(orphan_entry_slab, GFP_ATOMIC);
242127e670aSJaegeuk Kim 	new->ino = ino;
243127e670aSJaegeuk Kim 
244127e670aSJaegeuk Kim 	/* add new_oentry into list which is sorted by inode number */
245a2617dc6Smajianpeng 	if (orphan)
246a2617dc6Smajianpeng 		list_add(&new->list, this->prev);
247127e670aSJaegeuk Kim 	else
248127e670aSJaegeuk Kim 		list_add_tail(&new->list, head);
249127e670aSJaegeuk Kim out:
250127e670aSJaegeuk Kim 	mutex_unlock(&sbi->orphan_inode_mutex);
251127e670aSJaegeuk Kim }
252127e670aSJaegeuk Kim 
253127e670aSJaegeuk Kim void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
254127e670aSJaegeuk Kim {
25560ed9a0fSGu Zheng 	struct list_head *head;
256127e670aSJaegeuk Kim 	struct orphan_inode_entry *orphan;
257127e670aSJaegeuk Kim 
258127e670aSJaegeuk Kim 	mutex_lock(&sbi->orphan_inode_mutex);
259127e670aSJaegeuk Kim 	head = &sbi->orphan_inode_list;
26060ed9a0fSGu Zheng 	list_for_each_entry(orphan, head, list) {
261127e670aSJaegeuk Kim 		if (orphan->ino == ino) {
262127e670aSJaegeuk Kim 			list_del(&orphan->list);
263127e670aSJaegeuk Kim 			kmem_cache_free(orphan_entry_slab, orphan);
2645d56b671SJaegeuk Kim 			f2fs_bug_on(sbi->n_orphans == 0);
265127e670aSJaegeuk Kim 			sbi->n_orphans--;
266127e670aSJaegeuk Kim 			break;
267127e670aSJaegeuk Kim 		}
268127e670aSJaegeuk Kim 	}
269127e670aSJaegeuk Kim 	mutex_unlock(&sbi->orphan_inode_mutex);
270127e670aSJaegeuk Kim }
271127e670aSJaegeuk Kim 
272127e670aSJaegeuk Kim static void recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
273127e670aSJaegeuk Kim {
274127e670aSJaegeuk Kim 	struct inode *inode = f2fs_iget(sbi->sb, ino);
2755d56b671SJaegeuk Kim 	f2fs_bug_on(IS_ERR(inode));
276127e670aSJaegeuk Kim 	clear_nlink(inode);
277127e670aSJaegeuk Kim 
278127e670aSJaegeuk Kim 	/* truncate all the data during iput */
279127e670aSJaegeuk Kim 	iput(inode);
280127e670aSJaegeuk Kim }
281127e670aSJaegeuk Kim 
2828f99a946SChao Yu void recover_orphan_inodes(struct f2fs_sb_info *sbi)
283127e670aSJaegeuk Kim {
284127e670aSJaegeuk Kim 	block_t start_blk, orphan_blkaddr, i, j;
285127e670aSJaegeuk Kim 
28625ca923bSJaegeuk Kim 	if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG))
2878f99a946SChao Yu 		return;
288127e670aSJaegeuk Kim 
289aabe5136SHaicheng Li 	sbi->por_doing = true;
290127e670aSJaegeuk Kim 	start_blk = __start_cp_addr(sbi) + 1;
291127e670aSJaegeuk Kim 	orphan_blkaddr = __start_sum_addr(sbi) - 1;
292127e670aSJaegeuk Kim 
293127e670aSJaegeuk Kim 	for (i = 0; i < orphan_blkaddr; i++) {
294127e670aSJaegeuk Kim 		struct page *page = get_meta_page(sbi, start_blk + i);
295127e670aSJaegeuk Kim 		struct f2fs_orphan_block *orphan_blk;
296127e670aSJaegeuk Kim 
297127e670aSJaegeuk Kim 		orphan_blk = (struct f2fs_orphan_block *)page_address(page);
298127e670aSJaegeuk Kim 		for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
299127e670aSJaegeuk Kim 			nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
300127e670aSJaegeuk Kim 			recover_orphan_inode(sbi, ino);
301127e670aSJaegeuk Kim 		}
302127e670aSJaegeuk Kim 		f2fs_put_page(page, 1);
303127e670aSJaegeuk Kim 	}
304127e670aSJaegeuk Kim 	/* clear Orphan Flag */
30525ca923bSJaegeuk Kim 	clear_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG);
306aabe5136SHaicheng Li 	sbi->por_doing = false;
3078f99a946SChao Yu 	return;
308127e670aSJaegeuk Kim }
309127e670aSJaegeuk Kim 
310127e670aSJaegeuk Kim static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
311127e670aSJaegeuk Kim {
312502c6e0bSGu Zheng 	struct list_head *head;
313127e670aSJaegeuk Kim 	struct f2fs_orphan_block *orphan_blk = NULL;
314127e670aSJaegeuk Kim 	struct page *page = NULL;
315127e670aSJaegeuk Kim 	unsigned int nentries = 0;
316127e670aSJaegeuk Kim 	unsigned short index = 1;
317127e670aSJaegeuk Kim 	unsigned short orphan_blocks;
318502c6e0bSGu Zheng 	struct orphan_inode_entry *orphan = NULL;
319127e670aSJaegeuk Kim 
320127e670aSJaegeuk Kim 	orphan_blocks = (unsigned short)((sbi->n_orphans +
321127e670aSJaegeuk Kim 		(F2FS_ORPHANS_PER_BLOCK - 1)) / F2FS_ORPHANS_PER_BLOCK);
322127e670aSJaegeuk Kim 
323127e670aSJaegeuk Kim 	mutex_lock(&sbi->orphan_inode_mutex);
324127e670aSJaegeuk Kim 	head = &sbi->orphan_inode_list;
325127e670aSJaegeuk Kim 
326127e670aSJaegeuk Kim 	/* loop for each orphan inode entry and write them in Jornal block */
327502c6e0bSGu Zheng 	list_for_each_entry(orphan, head, list) {
328502c6e0bSGu Zheng 		if (!page) {
329502c6e0bSGu Zheng 			page = grab_meta_page(sbi, start_blk);
330502c6e0bSGu Zheng 			orphan_blk =
331502c6e0bSGu Zheng 				(struct f2fs_orphan_block *)page_address(page);
332502c6e0bSGu Zheng 			memset(orphan_blk, 0, sizeof(*orphan_blk));
333502c6e0bSGu Zheng 		}
334127e670aSJaegeuk Kim 
33536795567SGu Zheng 		orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
336127e670aSJaegeuk Kim 
33736795567SGu Zheng 		if (nentries == F2FS_ORPHANS_PER_BLOCK) {
338127e670aSJaegeuk Kim 			/*
339127e670aSJaegeuk Kim 			 * an orphan block is full of 1020 entries,
340127e670aSJaegeuk Kim 			 * then we need to flush current orphan blocks
341127e670aSJaegeuk Kim 			 * and bring another one in memory
342127e670aSJaegeuk Kim 			 */
343127e670aSJaegeuk Kim 			orphan_blk->blk_addr = cpu_to_le16(index);
344127e670aSJaegeuk Kim 			orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
345127e670aSJaegeuk Kim 			orphan_blk->entry_count = cpu_to_le32(nentries);
346127e670aSJaegeuk Kim 			set_page_dirty(page);
347127e670aSJaegeuk Kim 			f2fs_put_page(page, 1);
348127e670aSJaegeuk Kim 			index++;
349127e670aSJaegeuk Kim 			start_blk++;
350127e670aSJaegeuk Kim 			nentries = 0;
351127e670aSJaegeuk Kim 			page = NULL;
352127e670aSJaegeuk Kim 		}
353127e670aSJaegeuk Kim 	}
354127e670aSJaegeuk Kim 
355502c6e0bSGu Zheng 	if (page) {
356127e670aSJaegeuk Kim 		orphan_blk->blk_addr = cpu_to_le16(index);
357127e670aSJaegeuk Kim 		orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
358127e670aSJaegeuk Kim 		orphan_blk->entry_count = cpu_to_le32(nentries);
359127e670aSJaegeuk Kim 		set_page_dirty(page);
360127e670aSJaegeuk Kim 		f2fs_put_page(page, 1);
361502c6e0bSGu Zheng 	}
362502c6e0bSGu Zheng 
363127e670aSJaegeuk Kim 	mutex_unlock(&sbi->orphan_inode_mutex);
364127e670aSJaegeuk Kim }
365127e670aSJaegeuk Kim 
366127e670aSJaegeuk Kim static struct page *validate_checkpoint(struct f2fs_sb_info *sbi,
367127e670aSJaegeuk Kim 				block_t cp_addr, unsigned long long *version)
368127e670aSJaegeuk Kim {
369127e670aSJaegeuk Kim 	struct page *cp_page_1, *cp_page_2 = NULL;
370127e670aSJaegeuk Kim 	unsigned long blk_size = sbi->blocksize;
371127e670aSJaegeuk Kim 	struct f2fs_checkpoint *cp_block;
372127e670aSJaegeuk Kim 	unsigned long long cur_version = 0, pre_version = 0;
373127e670aSJaegeuk Kim 	size_t crc_offset;
3747e586fa0SJaegeuk Kim 	__u32 crc = 0;
375127e670aSJaegeuk Kim 
376127e670aSJaegeuk Kim 	/* Read the 1st cp block in this CP pack */
377127e670aSJaegeuk Kim 	cp_page_1 = get_meta_page(sbi, cp_addr);
378127e670aSJaegeuk Kim 
379127e670aSJaegeuk Kim 	/* get the version number */
380127e670aSJaegeuk Kim 	cp_block = (struct f2fs_checkpoint *)page_address(cp_page_1);
381127e670aSJaegeuk Kim 	crc_offset = le32_to_cpu(cp_block->checksum_offset);
382127e670aSJaegeuk Kim 	if (crc_offset >= blk_size)
383127e670aSJaegeuk Kim 		goto invalid_cp1;
384127e670aSJaegeuk Kim 
3857e586fa0SJaegeuk Kim 	crc = le32_to_cpu(*((__u32 *)((unsigned char *)cp_block + crc_offset)));
386127e670aSJaegeuk Kim 	if (!f2fs_crc_valid(crc, cp_block, crc_offset))
387127e670aSJaegeuk Kim 		goto invalid_cp1;
388127e670aSJaegeuk Kim 
389d71b5564SJaegeuk Kim 	pre_version = cur_cp_version(cp_block);
390127e670aSJaegeuk Kim 
391127e670aSJaegeuk Kim 	/* Read the 2nd cp block in this CP pack */
39225ca923bSJaegeuk Kim 	cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1;
393127e670aSJaegeuk Kim 	cp_page_2 = get_meta_page(sbi, cp_addr);
394127e670aSJaegeuk Kim 
395127e670aSJaegeuk Kim 	cp_block = (struct f2fs_checkpoint *)page_address(cp_page_2);
396127e670aSJaegeuk Kim 	crc_offset = le32_to_cpu(cp_block->checksum_offset);
397127e670aSJaegeuk Kim 	if (crc_offset >= blk_size)
398127e670aSJaegeuk Kim 		goto invalid_cp2;
399127e670aSJaegeuk Kim 
4007e586fa0SJaegeuk Kim 	crc = le32_to_cpu(*((__u32 *)((unsigned char *)cp_block + crc_offset)));
401127e670aSJaegeuk Kim 	if (!f2fs_crc_valid(crc, cp_block, crc_offset))
402127e670aSJaegeuk Kim 		goto invalid_cp2;
403127e670aSJaegeuk Kim 
404d71b5564SJaegeuk Kim 	cur_version = cur_cp_version(cp_block);
405127e670aSJaegeuk Kim 
406127e670aSJaegeuk Kim 	if (cur_version == pre_version) {
407127e670aSJaegeuk Kim 		*version = cur_version;
408127e670aSJaegeuk Kim 		f2fs_put_page(cp_page_2, 1);
409127e670aSJaegeuk Kim 		return cp_page_1;
410127e670aSJaegeuk Kim 	}
411127e670aSJaegeuk Kim invalid_cp2:
412127e670aSJaegeuk Kim 	f2fs_put_page(cp_page_2, 1);
413127e670aSJaegeuk Kim invalid_cp1:
414127e670aSJaegeuk Kim 	f2fs_put_page(cp_page_1, 1);
415127e670aSJaegeuk Kim 	return NULL;
416127e670aSJaegeuk Kim }
417127e670aSJaegeuk Kim 
418127e670aSJaegeuk Kim int get_valid_checkpoint(struct f2fs_sb_info *sbi)
419127e670aSJaegeuk Kim {
420127e670aSJaegeuk Kim 	struct f2fs_checkpoint *cp_block;
421127e670aSJaegeuk Kim 	struct f2fs_super_block *fsb = sbi->raw_super;
422127e670aSJaegeuk Kim 	struct page *cp1, *cp2, *cur_page;
423127e670aSJaegeuk Kim 	unsigned long blk_size = sbi->blocksize;
424127e670aSJaegeuk Kim 	unsigned long long cp1_version = 0, cp2_version = 0;
425127e670aSJaegeuk Kim 	unsigned long long cp_start_blk_no;
426127e670aSJaegeuk Kim 
427127e670aSJaegeuk Kim 	sbi->ckpt = kzalloc(blk_size, GFP_KERNEL);
428127e670aSJaegeuk Kim 	if (!sbi->ckpt)
429127e670aSJaegeuk Kim 		return -ENOMEM;
430127e670aSJaegeuk Kim 	/*
431127e670aSJaegeuk Kim 	 * Finding out valid cp block involves read both
432127e670aSJaegeuk Kim 	 * sets( cp pack1 and cp pack 2)
433127e670aSJaegeuk Kim 	 */
434127e670aSJaegeuk Kim 	cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr);
435127e670aSJaegeuk Kim 	cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
436127e670aSJaegeuk Kim 
437127e670aSJaegeuk Kim 	/* The second checkpoint pack should start at the next segment */
438f9a4e6dfSJaegeuk Kim 	cp_start_blk_no += ((unsigned long long)1) <<
439f9a4e6dfSJaegeuk Kim 				le32_to_cpu(fsb->log_blocks_per_seg);
440127e670aSJaegeuk Kim 	cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
441127e670aSJaegeuk Kim 
442127e670aSJaegeuk Kim 	if (cp1 && cp2) {
443127e670aSJaegeuk Kim 		if (ver_after(cp2_version, cp1_version))
444127e670aSJaegeuk Kim 			cur_page = cp2;
445127e670aSJaegeuk Kim 		else
446127e670aSJaegeuk Kim 			cur_page = cp1;
447127e670aSJaegeuk Kim 	} else if (cp1) {
448127e670aSJaegeuk Kim 		cur_page = cp1;
449127e670aSJaegeuk Kim 	} else if (cp2) {
450127e670aSJaegeuk Kim 		cur_page = cp2;
451127e670aSJaegeuk Kim 	} else {
452127e670aSJaegeuk Kim 		goto fail_no_cp;
453127e670aSJaegeuk Kim 	}
454127e670aSJaegeuk Kim 
455127e670aSJaegeuk Kim 	cp_block = (struct f2fs_checkpoint *)page_address(cur_page);
456127e670aSJaegeuk Kim 	memcpy(sbi->ckpt, cp_block, blk_size);
457127e670aSJaegeuk Kim 
458127e670aSJaegeuk Kim 	f2fs_put_page(cp1, 1);
459127e670aSJaegeuk Kim 	f2fs_put_page(cp2, 1);
460127e670aSJaegeuk Kim 	return 0;
461127e670aSJaegeuk Kim 
462127e670aSJaegeuk Kim fail_no_cp:
463127e670aSJaegeuk Kim 	kfree(sbi->ckpt);
464127e670aSJaegeuk Kim 	return -EINVAL;
465127e670aSJaegeuk Kim }
466127e670aSJaegeuk Kim 
4675deb8267SJaegeuk Kim static int __add_dirty_inode(struct inode *inode, struct dir_inode_entry *new)
468127e670aSJaegeuk Kim {
469127e670aSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
470127e670aSJaegeuk Kim 	struct list_head *head = &sbi->dir_inode_list;
471127e670aSJaegeuk Kim 	struct list_head *this;
472127e670aSJaegeuk Kim 
4735deb8267SJaegeuk Kim 	list_for_each(this, head) {
4745deb8267SJaegeuk Kim 		struct dir_inode_entry *entry;
4755deb8267SJaegeuk Kim 		entry = list_entry(this, struct dir_inode_entry, list);
4765deb8267SJaegeuk Kim 		if (entry->inode == inode)
4775deb8267SJaegeuk Kim 			return -EEXIST;
4785deb8267SJaegeuk Kim 	}
4795deb8267SJaegeuk Kim 	list_add_tail(&new->list, head);
480dcdfff65SJaegeuk Kim 	stat_inc_dirty_dir(sbi);
4815deb8267SJaegeuk Kim 	return 0;
4825deb8267SJaegeuk Kim }
4835deb8267SJaegeuk Kim 
4845deb8267SJaegeuk Kim void set_dirty_dir_page(struct inode *inode, struct page *page)
4855deb8267SJaegeuk Kim {
4865deb8267SJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
4875deb8267SJaegeuk Kim 	struct dir_inode_entry *new;
4885deb8267SJaegeuk Kim 
489127e670aSJaegeuk Kim 	if (!S_ISDIR(inode->i_mode))
490127e670aSJaegeuk Kim 		return;
4917bd59381SGu Zheng 
4927bd59381SGu Zheng 	new = f2fs_kmem_cache_alloc(inode_entry_slab, GFP_NOFS);
493127e670aSJaegeuk Kim 	new->inode = inode;
494127e670aSJaegeuk Kim 	INIT_LIST_HEAD(&new->list);
495127e670aSJaegeuk Kim 
496127e670aSJaegeuk Kim 	spin_lock(&sbi->dir_inode_lock);
4975deb8267SJaegeuk Kim 	if (__add_dirty_inode(inode, new))
498127e670aSJaegeuk Kim 		kmem_cache_free(inode_entry_slab, new);
499127e670aSJaegeuk Kim 
500127e670aSJaegeuk Kim 	inc_page_count(sbi, F2FS_DIRTY_DENTS);
501127e670aSJaegeuk Kim 	inode_inc_dirty_dents(inode);
502127e670aSJaegeuk Kim 	SetPagePrivate(page);
5035deb8267SJaegeuk Kim 	spin_unlock(&sbi->dir_inode_lock);
5045deb8267SJaegeuk Kim }
505127e670aSJaegeuk Kim 
5065deb8267SJaegeuk Kim void add_dirty_dir_inode(struct inode *inode)
5075deb8267SJaegeuk Kim {
5085deb8267SJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
5097bd59381SGu Zheng 	struct dir_inode_entry *new =
5107bd59381SGu Zheng 			f2fs_kmem_cache_alloc(inode_entry_slab, GFP_NOFS);
5117bd59381SGu Zheng 
5125deb8267SJaegeuk Kim 	new->inode = inode;
5135deb8267SJaegeuk Kim 	INIT_LIST_HEAD(&new->list);
5145deb8267SJaegeuk Kim 
5155deb8267SJaegeuk Kim 	spin_lock(&sbi->dir_inode_lock);
5165deb8267SJaegeuk Kim 	if (__add_dirty_inode(inode, new))
5175deb8267SJaegeuk Kim 		kmem_cache_free(inode_entry_slab, new);
518127e670aSJaegeuk Kim 	spin_unlock(&sbi->dir_inode_lock);
519127e670aSJaegeuk Kim }
520127e670aSJaegeuk Kim 
521127e670aSJaegeuk Kim void remove_dirty_dir_inode(struct inode *inode)
522127e670aSJaegeuk Kim {
523127e670aSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
524ce3b7d80SGu Zheng 
525ce3b7d80SGu Zheng 	struct list_head *this, *head;
526127e670aSJaegeuk Kim 
527127e670aSJaegeuk Kim 	if (!S_ISDIR(inode->i_mode))
528127e670aSJaegeuk Kim 		return;
529127e670aSJaegeuk Kim 
530127e670aSJaegeuk Kim 	spin_lock(&sbi->dir_inode_lock);
5313b10b1fdSJaegeuk Kim 	if (atomic_read(&F2FS_I(inode)->dirty_dents)) {
5323b10b1fdSJaegeuk Kim 		spin_unlock(&sbi->dir_inode_lock);
5333b10b1fdSJaegeuk Kim 		return;
5343b10b1fdSJaegeuk Kim 	}
535127e670aSJaegeuk Kim 
536ce3b7d80SGu Zheng 	head = &sbi->dir_inode_list;
537127e670aSJaegeuk Kim 	list_for_each(this, head) {
538127e670aSJaegeuk Kim 		struct dir_inode_entry *entry;
539127e670aSJaegeuk Kim 		entry = list_entry(this, struct dir_inode_entry, list);
540127e670aSJaegeuk Kim 		if (entry->inode == inode) {
541127e670aSJaegeuk Kim 			list_del(&entry->list);
542127e670aSJaegeuk Kim 			kmem_cache_free(inode_entry_slab, entry);
543dcdfff65SJaegeuk Kim 			stat_dec_dirty_dir(sbi);
544127e670aSJaegeuk Kim 			break;
545127e670aSJaegeuk Kim 		}
546127e670aSJaegeuk Kim 	}
547127e670aSJaegeuk Kim 	spin_unlock(&sbi->dir_inode_lock);
54874d0b917SJaegeuk Kim 
54974d0b917SJaegeuk Kim 	/* Only from the recovery routine */
550afc3eda2SJaegeuk Kim 	if (is_inode_flag_set(F2FS_I(inode), FI_DELAY_IPUT)) {
551afc3eda2SJaegeuk Kim 		clear_inode_flag(F2FS_I(inode), FI_DELAY_IPUT);
55274d0b917SJaegeuk Kim 		iput(inode);
55374d0b917SJaegeuk Kim 	}
554afc3eda2SJaegeuk Kim }
55574d0b917SJaegeuk Kim 
55674d0b917SJaegeuk Kim struct inode *check_dirty_dir_inode(struct f2fs_sb_info *sbi, nid_t ino)
55774d0b917SJaegeuk Kim {
558ce3b7d80SGu Zheng 
559ce3b7d80SGu Zheng 	struct list_head *this, *head;
56074d0b917SJaegeuk Kim 	struct inode *inode = NULL;
56174d0b917SJaegeuk Kim 
56274d0b917SJaegeuk Kim 	spin_lock(&sbi->dir_inode_lock);
563ce3b7d80SGu Zheng 
564ce3b7d80SGu Zheng 	head = &sbi->dir_inode_list;
56574d0b917SJaegeuk Kim 	list_for_each(this, head) {
56674d0b917SJaegeuk Kim 		struct dir_inode_entry *entry;
56774d0b917SJaegeuk Kim 		entry = list_entry(this, struct dir_inode_entry, list);
56874d0b917SJaegeuk Kim 		if (entry->inode->i_ino == ino) {
56974d0b917SJaegeuk Kim 			inode = entry->inode;
57074d0b917SJaegeuk Kim 			break;
57174d0b917SJaegeuk Kim 		}
57274d0b917SJaegeuk Kim 	}
57374d0b917SJaegeuk Kim 	spin_unlock(&sbi->dir_inode_lock);
57474d0b917SJaegeuk Kim 	return inode;
575127e670aSJaegeuk Kim }
576127e670aSJaegeuk Kim 
577127e670aSJaegeuk Kim void sync_dirty_dir_inodes(struct f2fs_sb_info *sbi)
578127e670aSJaegeuk Kim {
579ce3b7d80SGu Zheng 	struct list_head *head;
580127e670aSJaegeuk Kim 	struct dir_inode_entry *entry;
581127e670aSJaegeuk Kim 	struct inode *inode;
582127e670aSJaegeuk Kim retry:
583127e670aSJaegeuk Kim 	spin_lock(&sbi->dir_inode_lock);
584ce3b7d80SGu Zheng 
585ce3b7d80SGu Zheng 	head = &sbi->dir_inode_list;
586127e670aSJaegeuk Kim 	if (list_empty(head)) {
587127e670aSJaegeuk Kim 		spin_unlock(&sbi->dir_inode_lock);
588127e670aSJaegeuk Kim 		return;
589127e670aSJaegeuk Kim 	}
590127e670aSJaegeuk Kim 	entry = list_entry(head->next, struct dir_inode_entry, list);
591127e670aSJaegeuk Kim 	inode = igrab(entry->inode);
592127e670aSJaegeuk Kim 	spin_unlock(&sbi->dir_inode_lock);
593127e670aSJaegeuk Kim 	if (inode) {
594127e670aSJaegeuk Kim 		filemap_flush(inode->i_mapping);
595127e670aSJaegeuk Kim 		iput(inode);
596127e670aSJaegeuk Kim 	} else {
597127e670aSJaegeuk Kim 		/*
598127e670aSJaegeuk Kim 		 * We should submit bio, since it exists several
599127e670aSJaegeuk Kim 		 * wribacking dentry pages in the freeing inode.
600127e670aSJaegeuk Kim 		 */
60193dfe2acSJaegeuk Kim 		f2fs_submit_merged_bio(sbi, DATA, true, WRITE);
602127e670aSJaegeuk Kim 	}
603127e670aSJaegeuk Kim 	goto retry;
604127e670aSJaegeuk Kim }
605127e670aSJaegeuk Kim 
6060a8165d7SJaegeuk Kim /*
607127e670aSJaegeuk Kim  * Freeze all the FS-operations for checkpoint.
608127e670aSJaegeuk Kim  */
60943727527SJaegeuk Kim static void block_operations(struct f2fs_sb_info *sbi)
610127e670aSJaegeuk Kim {
611127e670aSJaegeuk Kim 	struct writeback_control wbc = {
612127e670aSJaegeuk Kim 		.sync_mode = WB_SYNC_ALL,
613127e670aSJaegeuk Kim 		.nr_to_write = LONG_MAX,
614127e670aSJaegeuk Kim 		.for_reclaim = 0,
615127e670aSJaegeuk Kim 	};
616c718379bSJaegeuk Kim 	struct blk_plug plug;
617c718379bSJaegeuk Kim 
618c718379bSJaegeuk Kim 	blk_start_plug(&plug);
619c718379bSJaegeuk Kim 
62039936837SJaegeuk Kim retry_flush_dents:
621e479556bSGu Zheng 	f2fs_lock_all(sbi);
622127e670aSJaegeuk Kim 	/* write all the dirty dentry pages */
623127e670aSJaegeuk Kim 	if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
624e479556bSGu Zheng 		f2fs_unlock_all(sbi);
62539936837SJaegeuk Kim 		sync_dirty_dir_inodes(sbi);
62639936837SJaegeuk Kim 		goto retry_flush_dents;
627127e670aSJaegeuk Kim 	}
628127e670aSJaegeuk Kim 
629127e670aSJaegeuk Kim 	/*
630127e670aSJaegeuk Kim 	 * POR: we should ensure that there is no dirty node pages
631127e670aSJaegeuk Kim 	 * until finishing nat/sit flush.
632127e670aSJaegeuk Kim 	 */
63339936837SJaegeuk Kim retry_flush_nodes:
63439936837SJaegeuk Kim 	mutex_lock(&sbi->node_write);
635127e670aSJaegeuk Kim 
636127e670aSJaegeuk Kim 	if (get_pages(sbi, F2FS_DIRTY_NODES)) {
63739936837SJaegeuk Kim 		mutex_unlock(&sbi->node_write);
63839936837SJaegeuk Kim 		sync_node_pages(sbi, 0, &wbc);
63939936837SJaegeuk Kim 		goto retry_flush_nodes;
640127e670aSJaegeuk Kim 	}
641c718379bSJaegeuk Kim 	blk_finish_plug(&plug);
642127e670aSJaegeuk Kim }
643127e670aSJaegeuk Kim 
644127e670aSJaegeuk Kim static void unblock_operations(struct f2fs_sb_info *sbi)
645127e670aSJaegeuk Kim {
64639936837SJaegeuk Kim 	mutex_unlock(&sbi->node_write);
647e479556bSGu Zheng 	f2fs_unlock_all(sbi);
648127e670aSJaegeuk Kim }
649127e670aSJaegeuk Kim 
650fb51b5efSChangman Lee static void wait_on_all_pages_writeback(struct f2fs_sb_info *sbi)
651fb51b5efSChangman Lee {
652fb51b5efSChangman Lee 	DEFINE_WAIT(wait);
653fb51b5efSChangman Lee 
654fb51b5efSChangman Lee 	for (;;) {
655fb51b5efSChangman Lee 		prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
656fb51b5efSChangman Lee 
657fb51b5efSChangman Lee 		if (!get_pages(sbi, F2FS_WRITEBACK))
658fb51b5efSChangman Lee 			break;
659fb51b5efSChangman Lee 
660fb51b5efSChangman Lee 		io_schedule();
661fb51b5efSChangman Lee 	}
662fb51b5efSChangman Lee 	finish_wait(&sbi->cp_wait, &wait);
663fb51b5efSChangman Lee }
664fb51b5efSChangman Lee 
665127e670aSJaegeuk Kim static void do_checkpoint(struct f2fs_sb_info *sbi, bool is_umount)
666127e670aSJaegeuk Kim {
667127e670aSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
668127e670aSJaegeuk Kim 	nid_t last_nid = 0;
669127e670aSJaegeuk Kim 	block_t start_blk;
670127e670aSJaegeuk Kim 	struct page *cp_page;
671127e670aSJaegeuk Kim 	unsigned int data_sum_blocks, orphan_blocks;
6727e586fa0SJaegeuk Kim 	__u32 crc32 = 0;
673127e670aSJaegeuk Kim 	void *kaddr;
674127e670aSJaegeuk Kim 	int i;
675127e670aSJaegeuk Kim 
676127e670aSJaegeuk Kim 	/* Flush all the NAT/SIT pages */
677127e670aSJaegeuk Kim 	while (get_pages(sbi, F2FS_DIRTY_META))
678127e670aSJaegeuk Kim 		sync_meta_pages(sbi, META, LONG_MAX);
679127e670aSJaegeuk Kim 
680127e670aSJaegeuk Kim 	next_free_nid(sbi, &last_nid);
681127e670aSJaegeuk Kim 
682127e670aSJaegeuk Kim 	/*
683127e670aSJaegeuk Kim 	 * modify checkpoint
684127e670aSJaegeuk Kim 	 * version number is already updated
685127e670aSJaegeuk Kim 	 */
686127e670aSJaegeuk Kim 	ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi));
687127e670aSJaegeuk Kim 	ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
688127e670aSJaegeuk Kim 	ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
689127e670aSJaegeuk Kim 	for (i = 0; i < 3; i++) {
690127e670aSJaegeuk Kim 		ckpt->cur_node_segno[i] =
691127e670aSJaegeuk Kim 			cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE));
692127e670aSJaegeuk Kim 		ckpt->cur_node_blkoff[i] =
693127e670aSJaegeuk Kim 			cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE));
694127e670aSJaegeuk Kim 		ckpt->alloc_type[i + CURSEG_HOT_NODE] =
695127e670aSJaegeuk Kim 				curseg_alloc_type(sbi, i + CURSEG_HOT_NODE);
696127e670aSJaegeuk Kim 	}
697127e670aSJaegeuk Kim 	for (i = 0; i < 3; i++) {
698127e670aSJaegeuk Kim 		ckpt->cur_data_segno[i] =
699127e670aSJaegeuk Kim 			cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA));
700127e670aSJaegeuk Kim 		ckpt->cur_data_blkoff[i] =
701127e670aSJaegeuk Kim 			cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA));
702127e670aSJaegeuk Kim 		ckpt->alloc_type[i + CURSEG_HOT_DATA] =
703127e670aSJaegeuk Kim 				curseg_alloc_type(sbi, i + CURSEG_HOT_DATA);
704127e670aSJaegeuk Kim 	}
705127e670aSJaegeuk Kim 
706127e670aSJaegeuk Kim 	ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi));
707127e670aSJaegeuk Kim 	ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi));
708127e670aSJaegeuk Kim 	ckpt->next_free_nid = cpu_to_le32(last_nid);
709127e670aSJaegeuk Kim 
710127e670aSJaegeuk Kim 	/* 2 cp  + n data seg summary + orphan inode blocks */
711127e670aSJaegeuk Kim 	data_sum_blocks = npages_for_summary_flush(sbi);
712127e670aSJaegeuk Kim 	if (data_sum_blocks < 3)
71325ca923bSJaegeuk Kim 		set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
714127e670aSJaegeuk Kim 	else
71525ca923bSJaegeuk Kim 		clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
716127e670aSJaegeuk Kim 
717127e670aSJaegeuk Kim 	orphan_blocks = (sbi->n_orphans + F2FS_ORPHANS_PER_BLOCK - 1)
718127e670aSJaegeuk Kim 					/ F2FS_ORPHANS_PER_BLOCK;
71925ca923bSJaegeuk Kim 	ckpt->cp_pack_start_sum = cpu_to_le32(1 + orphan_blocks);
720127e670aSJaegeuk Kim 
721127e670aSJaegeuk Kim 	if (is_umount) {
72225ca923bSJaegeuk Kim 		set_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
72325ca923bSJaegeuk Kim 		ckpt->cp_pack_total_block_count = cpu_to_le32(2 +
72425ca923bSJaegeuk Kim 			data_sum_blocks + orphan_blocks + NR_CURSEG_NODE_TYPE);
725127e670aSJaegeuk Kim 	} else {
72625ca923bSJaegeuk Kim 		clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
72725ca923bSJaegeuk Kim 		ckpt->cp_pack_total_block_count = cpu_to_le32(2 +
72825ca923bSJaegeuk Kim 			data_sum_blocks + orphan_blocks);
729127e670aSJaegeuk Kim 	}
730127e670aSJaegeuk Kim 
731127e670aSJaegeuk Kim 	if (sbi->n_orphans)
73225ca923bSJaegeuk Kim 		set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
733127e670aSJaegeuk Kim 	else
73425ca923bSJaegeuk Kim 		clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
735127e670aSJaegeuk Kim 
736127e670aSJaegeuk Kim 	/* update SIT/NAT bitmap */
737127e670aSJaegeuk Kim 	get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
738127e670aSJaegeuk Kim 	get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
739127e670aSJaegeuk Kim 
740127e670aSJaegeuk Kim 	crc32 = f2fs_crc32(ckpt, le32_to_cpu(ckpt->checksum_offset));
7417e586fa0SJaegeuk Kim 	*((__le32 *)((unsigned char *)ckpt +
7427e586fa0SJaegeuk Kim 				le32_to_cpu(ckpt->checksum_offset)))
743127e670aSJaegeuk Kim 				= cpu_to_le32(crc32);
744127e670aSJaegeuk Kim 
745127e670aSJaegeuk Kim 	start_blk = __start_cp_addr(sbi);
746127e670aSJaegeuk Kim 
747127e670aSJaegeuk Kim 	/* write out checkpoint buffer at block 0 */
748127e670aSJaegeuk Kim 	cp_page = grab_meta_page(sbi, start_blk++);
749127e670aSJaegeuk Kim 	kaddr = page_address(cp_page);
750127e670aSJaegeuk Kim 	memcpy(kaddr, ckpt, (1 << sbi->log_blocksize));
751127e670aSJaegeuk Kim 	set_page_dirty(cp_page);
752127e670aSJaegeuk Kim 	f2fs_put_page(cp_page, 1);
753127e670aSJaegeuk Kim 
754127e670aSJaegeuk Kim 	if (sbi->n_orphans) {
755127e670aSJaegeuk Kim 		write_orphan_inodes(sbi, start_blk);
756127e670aSJaegeuk Kim 		start_blk += orphan_blocks;
757127e670aSJaegeuk Kim 	}
758127e670aSJaegeuk Kim 
759127e670aSJaegeuk Kim 	write_data_summaries(sbi, start_blk);
760127e670aSJaegeuk Kim 	start_blk += data_sum_blocks;
761127e670aSJaegeuk Kim 	if (is_umount) {
762127e670aSJaegeuk Kim 		write_node_summaries(sbi, start_blk);
763127e670aSJaegeuk Kim 		start_blk += NR_CURSEG_NODE_TYPE;
764127e670aSJaegeuk Kim 	}
765127e670aSJaegeuk Kim 
766127e670aSJaegeuk Kim 	/* writeout checkpoint block */
767127e670aSJaegeuk Kim 	cp_page = grab_meta_page(sbi, start_blk);
768127e670aSJaegeuk Kim 	kaddr = page_address(cp_page);
769127e670aSJaegeuk Kim 	memcpy(kaddr, ckpt, (1 << sbi->log_blocksize));
770127e670aSJaegeuk Kim 	set_page_dirty(cp_page);
771127e670aSJaegeuk Kim 	f2fs_put_page(cp_page, 1);
772127e670aSJaegeuk Kim 
773127e670aSJaegeuk Kim 	/* wait for previous submitted node/meta pages writeback */
774fb51b5efSChangman Lee 	wait_on_all_pages_writeback(sbi);
775127e670aSJaegeuk Kim 
776127e670aSJaegeuk Kim 	filemap_fdatawait_range(sbi->node_inode->i_mapping, 0, LONG_MAX);
777127e670aSJaegeuk Kim 	filemap_fdatawait_range(sbi->meta_inode->i_mapping, 0, LONG_MAX);
778127e670aSJaegeuk Kim 
779127e670aSJaegeuk Kim 	/* update user_block_counts */
780127e670aSJaegeuk Kim 	sbi->last_valid_block_count = sbi->total_valid_block_count;
781127e670aSJaegeuk Kim 	sbi->alloc_valid_block_count = 0;
782127e670aSJaegeuk Kim 
783127e670aSJaegeuk Kim 	/* Here, we only have one bio having CP pack */
784127e670aSJaegeuk Kim 	sync_meta_pages(sbi, META_FLUSH, LONG_MAX);
785127e670aSJaegeuk Kim 
786577e3495SJaegeuk Kim 	if (!is_set_ckpt_flags(ckpt, CP_ERROR_FLAG)) {
787127e670aSJaegeuk Kim 		clear_prefree_segments(sbi);
788127e670aSJaegeuk Kim 		F2FS_RESET_SB_DIRT(sbi);
789127e670aSJaegeuk Kim 	}
790577e3495SJaegeuk Kim }
791127e670aSJaegeuk Kim 
7920a8165d7SJaegeuk Kim /*
793127e670aSJaegeuk Kim  * We guarantee that this checkpoint procedure should not fail.
794127e670aSJaegeuk Kim  */
79543727527SJaegeuk Kim void write_checkpoint(struct f2fs_sb_info *sbi, bool is_umount)
796127e670aSJaegeuk Kim {
797127e670aSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
798127e670aSJaegeuk Kim 	unsigned long long ckpt_ver;
799127e670aSJaegeuk Kim 
8002af4bd6cSNamjae Jeon 	trace_f2fs_write_checkpoint(sbi->sb, is_umount, "start block_ops");
8012af4bd6cSNamjae Jeon 
802127e670aSJaegeuk Kim 	mutex_lock(&sbi->cp_mutex);
803127e670aSJaegeuk Kim 	block_operations(sbi);
804127e670aSJaegeuk Kim 
8052af4bd6cSNamjae Jeon 	trace_f2fs_write_checkpoint(sbi->sb, is_umount, "finish block_ops");
8062af4bd6cSNamjae Jeon 
80793dfe2acSJaegeuk Kim 	f2fs_submit_merged_bio(sbi, DATA, true, WRITE);
80893dfe2acSJaegeuk Kim 	f2fs_submit_merged_bio(sbi, NODE, true, WRITE);
80993dfe2acSJaegeuk Kim 	f2fs_submit_merged_bio(sbi, META, true, WRITE);
810127e670aSJaegeuk Kim 
811127e670aSJaegeuk Kim 	/*
812127e670aSJaegeuk Kim 	 * update checkpoint pack index
813127e670aSJaegeuk Kim 	 * Increase the version number so that
814127e670aSJaegeuk Kim 	 * SIT entries and seg summaries are written at correct place
815127e670aSJaegeuk Kim 	 */
816d71b5564SJaegeuk Kim 	ckpt_ver = cur_cp_version(ckpt);
817127e670aSJaegeuk Kim 	ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
818127e670aSJaegeuk Kim 
819127e670aSJaegeuk Kim 	/* write cached NAT/SIT entries to NAT/SIT area */
820127e670aSJaegeuk Kim 	flush_nat_entries(sbi);
821127e670aSJaegeuk Kim 	flush_sit_entries(sbi);
822127e670aSJaegeuk Kim 
823127e670aSJaegeuk Kim 	/* unlock all the fs_lock[] in do_checkpoint() */
824127e670aSJaegeuk Kim 	do_checkpoint(sbi, is_umount);
825127e670aSJaegeuk Kim 
826127e670aSJaegeuk Kim 	unblock_operations(sbi);
827127e670aSJaegeuk Kim 	mutex_unlock(&sbi->cp_mutex);
8282af4bd6cSNamjae Jeon 
8292af4bd6cSNamjae Jeon 	trace_f2fs_write_checkpoint(sbi->sb, is_umount, "finish checkpoint");
830127e670aSJaegeuk Kim }
831127e670aSJaegeuk Kim 
832127e670aSJaegeuk Kim void init_orphan_info(struct f2fs_sb_info *sbi)
833127e670aSJaegeuk Kim {
834127e670aSJaegeuk Kim 	mutex_init(&sbi->orphan_inode_mutex);
835127e670aSJaegeuk Kim 	INIT_LIST_HEAD(&sbi->orphan_inode_list);
836127e670aSJaegeuk Kim 	sbi->n_orphans = 0;
837127e670aSJaegeuk Kim }
838127e670aSJaegeuk Kim 
8396e6093a8SNamjae Jeon int __init create_checkpoint_caches(void)
840127e670aSJaegeuk Kim {
841127e670aSJaegeuk Kim 	orphan_entry_slab = f2fs_kmem_cache_create("f2fs_orphan_entry",
842127e670aSJaegeuk Kim 			sizeof(struct orphan_inode_entry), NULL);
843127e670aSJaegeuk Kim 	if (unlikely(!orphan_entry_slab))
844127e670aSJaegeuk Kim 		return -ENOMEM;
845127e670aSJaegeuk Kim 	inode_entry_slab = f2fs_kmem_cache_create("f2fs_dirty_dir_entry",
846127e670aSJaegeuk Kim 			sizeof(struct dir_inode_entry), NULL);
847127e670aSJaegeuk Kim 	if (unlikely(!inode_entry_slab)) {
848127e670aSJaegeuk Kim 		kmem_cache_destroy(orphan_entry_slab);
849127e670aSJaegeuk Kim 		return -ENOMEM;
850127e670aSJaegeuk Kim 	}
851127e670aSJaegeuk Kim 	return 0;
852127e670aSJaegeuk Kim }
853127e670aSJaegeuk Kim 
854127e670aSJaegeuk Kim void destroy_checkpoint_caches(void)
855127e670aSJaegeuk Kim {
856127e670aSJaegeuk Kim 	kmem_cache_destroy(orphan_entry_slab);
857127e670aSJaegeuk Kim 	kmem_cache_destroy(inode_entry_slab);
858127e670aSJaegeuk Kim }
859