• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  /*
2   * fs/f2fs/checkpoint.c
3   *
4   * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5   *             http://www.samsung.com/
6   *
7   * This program is free software; you can redistribute it and/or modify
8   * it under the terms of the GNU General Public License version 2 as
9   * published by the Free Software Foundation.
10   */
11  #include <linux/fs.h>
12  #include <linux/bio.h>
13  #include <linux/mpage.h>
14  #include <linux/writeback.h>
15  #include <linux/blkdev.h>
16  #include <linux/f2fs_fs.h>
17  #include <linux/pagevec.h>
18  #include <linux/swap.h>
19  
20  #include "f2fs.h"
21  #include "node.h"
22  #include "segment.h"
23  #include "trace.h"
24  #include <trace/events/f2fs.h>
25  
26  static struct kmem_cache *ino_entry_slab;
27  struct kmem_cache *inode_entry_slab;
28  
f2fs_stop_checkpoint(struct f2fs_sb_info * sbi,bool end_io)29  void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io)
30  {
31  	set_ckpt_flags(sbi, CP_ERROR_FLAG);
32  	if (!end_io)
33  		f2fs_flush_merged_writes(sbi);
34  }
35  
36  /*
37   * We guarantee no failure on the returned page.
38   */
grab_meta_page(struct f2fs_sb_info * sbi,pgoff_t index)39  struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
40  {
41  	struct address_space *mapping = META_MAPPING(sbi);
42  	struct page *page = NULL;
43  repeat:
44  	page = f2fs_grab_cache_page(mapping, index, false);
45  	if (!page) {
46  		cond_resched();
47  		goto repeat;
48  	}
49  	f2fs_wait_on_page_writeback(page, META, true);
50  	if (!PageUptodate(page))
51  		SetPageUptodate(page);
52  	return page;
53  }
54  
55  /*
56   * We guarantee no failure on the returned page.
57   */
__get_meta_page(struct f2fs_sb_info * sbi,pgoff_t index,bool is_meta)58  static struct page *__get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index,
59  							bool is_meta)
60  {
61  	struct address_space *mapping = META_MAPPING(sbi);
62  	struct page *page;
63  	struct f2fs_io_info fio = {
64  		.sbi = sbi,
65  		.type = META,
66  		.op = REQ_OP_READ,
67  		.op_flags = REQ_SYNC | REQ_META | REQ_PRIO,
68  		.old_blkaddr = index,
69  		.new_blkaddr = index,
70  		.encrypted_page = NULL,
71  		.is_meta = is_meta,
72  	};
73  
74  	if (unlikely(!is_meta))
75  		fio.op_flags &= ~REQ_META;
76  repeat:
77  	page = f2fs_grab_cache_page(mapping, index, false);
78  	if (!page) {
79  		cond_resched();
80  		goto repeat;
81  	}
82  	if (PageUptodate(page))
83  		goto out;
84  
85  	fio.page = page;
86  
87  	if (f2fs_submit_page_bio(&fio)) {
88  		f2fs_put_page(page, 1);
89  		goto repeat;
90  	}
91  
92  	lock_page(page);
93  	if (unlikely(page->mapping != mapping)) {
94  		f2fs_put_page(page, 1);
95  		goto repeat;
96  	}
97  
98  	/*
99  	 * if there is any IO error when accessing device, make our filesystem
100  	 * readonly and make sure do not write checkpoint with non-uptodate
101  	 * meta page.
102  	 */
103  	if (unlikely(!PageUptodate(page)))
104  		f2fs_stop_checkpoint(sbi, false);
105  out:
106  	return page;
107  }
108  
get_meta_page(struct f2fs_sb_info * sbi,pgoff_t index)109  struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
110  {
111  	return __get_meta_page(sbi, index, true);
112  }
113  
114  /* for POR only */
get_tmp_page(struct f2fs_sb_info * sbi,pgoff_t index)115  struct page *get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index)
116  {
117  	return __get_meta_page(sbi, index, false);
118  }
119  
is_valid_blkaddr(struct f2fs_sb_info * sbi,block_t blkaddr,int type)120  bool is_valid_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr, int type)
121  {
122  	switch (type) {
123  	case META_NAT:
124  		break;
125  	case META_SIT:
126  		if (unlikely(blkaddr >= SIT_BLK_CNT(sbi)))
127  			return false;
128  		break;
129  	case META_SSA:
130  		if (unlikely(blkaddr >= MAIN_BLKADDR(sbi) ||
131  			blkaddr < SM_I(sbi)->ssa_blkaddr))
132  			return false;
133  		break;
134  	case META_CP:
135  		if (unlikely(blkaddr >= SIT_I(sbi)->sit_base_addr ||
136  			blkaddr < __start_cp_addr(sbi)))
137  			return false;
138  		break;
139  	case META_POR:
140  		if (unlikely(blkaddr >= MAX_BLKADDR(sbi) ||
141  			blkaddr < MAIN_BLKADDR(sbi)))
142  			return false;
143  		break;
144  	default:
145  		BUG();
146  	}
147  
148  	return true;
149  }
150  
151  /*
152   * Readahead CP/NAT/SIT/SSA pages
153   */
ra_meta_pages(struct f2fs_sb_info * sbi,block_t start,int nrpages,int type,bool sync)154  int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
155  							int type, bool sync)
156  {
157  	struct page *page;
158  	block_t blkno = start;
159  	struct f2fs_io_info fio = {
160  		.sbi = sbi,
161  		.type = META,
162  		.op = REQ_OP_READ,
163  		.op_flags = sync ? (REQ_SYNC | REQ_META | REQ_PRIO) :
164  						REQ_RAHEAD,
165  		.encrypted_page = NULL,
166  		.in_list = false,
167  		.is_meta = (type != META_POR),
168  	};
169  	struct blk_plug plug;
170  
171  	if (unlikely(type == META_POR))
172  		fio.op_flags &= ~REQ_META;
173  
174  	blk_start_plug(&plug);
175  	for (; nrpages-- > 0; blkno++) {
176  
177  		if (!is_valid_blkaddr(sbi, blkno, type))
178  			goto out;
179  
180  		switch (type) {
181  		case META_NAT:
182  			if (unlikely(blkno >=
183  					NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid)))
184  				blkno = 0;
185  			/* get nat block addr */
186  			fio.new_blkaddr = current_nat_addr(sbi,
187  					blkno * NAT_ENTRY_PER_BLOCK);
188  			break;
189  		case META_SIT:
190  			if (unlikely(blkno >= TOTAL_SEGS(sbi)))
191  				goto out;
192  			/* get sit block addr */
193  			fio.new_blkaddr = current_sit_addr(sbi,
194  					blkno * SIT_ENTRY_PER_BLOCK);
195  			break;
196  		case META_SSA:
197  		case META_CP:
198  		case META_POR:
199  			fio.new_blkaddr = blkno;
200  			break;
201  		default:
202  			BUG();
203  		}
204  
205  		page = f2fs_grab_cache_page(META_MAPPING(sbi),
206  						fio.new_blkaddr, false);
207  		if (!page)
208  			continue;
209  		if (PageUptodate(page)) {
210  			f2fs_put_page(page, 1);
211  			continue;
212  		}
213  
214  		fio.page = page;
215  		f2fs_submit_page_bio(&fio);
216  		f2fs_put_page(page, 0);
217  	}
218  out:
219  	blk_finish_plug(&plug);
220  	return blkno - start;
221  }
222  
ra_meta_pages_cond(struct f2fs_sb_info * sbi,pgoff_t index)223  void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index)
224  {
225  	struct page *page;
226  	bool readahead = false;
227  
228  	page = find_get_page(META_MAPPING(sbi), index);
229  	if (!page || !PageUptodate(page))
230  		readahead = true;
231  	f2fs_put_page(page, 0);
232  
233  	if (readahead)
234  		ra_meta_pages(sbi, index, BIO_MAX_PAGES, META_POR, true);
235  }
236  
__f2fs_write_meta_page(struct page * page,struct writeback_control * wbc,enum iostat_type io_type)237  static int __f2fs_write_meta_page(struct page *page,
238  				struct writeback_control *wbc,
239  				enum iostat_type io_type)
240  {
241  	struct f2fs_sb_info *sbi = F2FS_P_SB(page);
242  
243  	trace_f2fs_writepage(page, META);
244  
245  	if (unlikely(f2fs_cp_error(sbi))) {
246  		dec_page_count(sbi, F2FS_DIRTY_META);
247  		unlock_page(page);
248  		return 0;
249  	}
250  	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
251  		goto redirty_out;
252  	if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0))
253  		goto redirty_out;
254  
255  	write_meta_page(sbi, page, io_type);
256  	dec_page_count(sbi, F2FS_DIRTY_META);
257  
258  	if (wbc->for_reclaim)
259  		f2fs_submit_merged_write_cond(sbi, page->mapping->host,
260  						0, page->index, META);
261  
262  	unlock_page(page);
263  
264  	if (unlikely(f2fs_cp_error(sbi)))
265  		f2fs_submit_merged_write(sbi, META);
266  
267  	return 0;
268  
269  redirty_out:
270  	redirty_page_for_writepage(wbc, page);
271  	return AOP_WRITEPAGE_ACTIVATE;
272  }
273  
f2fs_write_meta_page(struct page * page,struct writeback_control * wbc)274  static int f2fs_write_meta_page(struct page *page,
275  				struct writeback_control *wbc)
276  {
277  	return __f2fs_write_meta_page(page, wbc, FS_META_IO);
278  }
279  
f2fs_write_meta_pages(struct address_space * mapping,struct writeback_control * wbc)280  static int f2fs_write_meta_pages(struct address_space *mapping,
281  				struct writeback_control *wbc)
282  {
283  	struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
284  	long diff, written;
285  
286  	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
287  		goto skip_write;
288  
289  	/* collect a number of dirty meta pages and write together */
290  	if (wbc->for_kupdate ||
291  		get_pages(sbi, F2FS_DIRTY_META) < nr_pages_to_skip(sbi, META))
292  		goto skip_write;
293  
294  	/* if locked failed, cp will flush dirty pages instead */
295  	if (!mutex_trylock(&sbi->cp_mutex))
296  		goto skip_write;
297  
298  	trace_f2fs_writepages(mapping->host, wbc, META);
299  	diff = nr_pages_to_write(sbi, META, wbc);
300  	written = sync_meta_pages(sbi, META, wbc->nr_to_write, FS_META_IO);
301  	mutex_unlock(&sbi->cp_mutex);
302  	wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff);
303  	return 0;
304  
305  skip_write:
306  	wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META);
307  	trace_f2fs_writepages(mapping->host, wbc, META);
308  	return 0;
309  }
310  
sync_meta_pages(struct f2fs_sb_info * sbi,enum page_type type,long nr_to_write,enum iostat_type io_type)311  long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
312  				long nr_to_write, enum iostat_type io_type)
313  {
314  	struct address_space *mapping = META_MAPPING(sbi);
315  	pgoff_t index = 0, end = ULONG_MAX, prev = ULONG_MAX;
316  	struct pagevec pvec;
317  	long nwritten = 0;
318  	struct writeback_control wbc = {
319  		.for_reclaim = 0,
320  	};
321  	struct blk_plug plug;
322  
323  	pagevec_init(&pvec, 0);
324  
325  	blk_start_plug(&plug);
326  
327  	while (index <= end) {
328  		int i, nr_pages;
329  		nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
330  				PAGECACHE_TAG_DIRTY,
331  				min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
332  		if (unlikely(nr_pages == 0))
333  			break;
334  
335  		for (i = 0; i < nr_pages; i++) {
336  			struct page *page = pvec.pages[i];
337  
338  			if (prev == ULONG_MAX)
339  				prev = page->index - 1;
340  			if (nr_to_write != LONG_MAX && page->index != prev + 1) {
341  				pagevec_release(&pvec);
342  				goto stop;
343  			}
344  
345  			lock_page(page);
346  
347  			if (unlikely(page->mapping != mapping)) {
348  continue_unlock:
349  				unlock_page(page);
350  				continue;
351  			}
352  			if (!PageDirty(page)) {
353  				/* someone wrote it for us */
354  				goto continue_unlock;
355  			}
356  
357  			f2fs_wait_on_page_writeback(page, META, true);
358  
359  			BUG_ON(PageWriteback(page));
360  			if (!clear_page_dirty_for_io(page))
361  				goto continue_unlock;
362  
363  			if (__f2fs_write_meta_page(page, &wbc, io_type)) {
364  				unlock_page(page);
365  				break;
366  			}
367  			nwritten++;
368  			prev = page->index;
369  			if (unlikely(nwritten >= nr_to_write))
370  				break;
371  		}
372  		pagevec_release(&pvec);
373  		cond_resched();
374  	}
375  stop:
376  	if (nwritten)
377  		f2fs_submit_merged_write(sbi, type);
378  
379  	blk_finish_plug(&plug);
380  
381  	return nwritten;
382  }
383  
f2fs_set_meta_page_dirty(struct page * page)384  static int f2fs_set_meta_page_dirty(struct page *page)
385  {
386  	trace_f2fs_set_page_dirty(page, META);
387  
388  	if (!PageUptodate(page))
389  		SetPageUptodate(page);
390  	if (!PageDirty(page)) {
391  		__set_page_dirty_nobuffers(page);
392  		inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_META);
393  		SetPagePrivate(page);
394  		f2fs_trace_pid(page);
395  		return 1;
396  	}
397  	return 0;
398  }
399  
400  const struct address_space_operations f2fs_meta_aops = {
401  	.writepage	= f2fs_write_meta_page,
402  	.writepages	= f2fs_write_meta_pages,
403  	.set_page_dirty	= f2fs_set_meta_page_dirty,
404  	.invalidatepage = f2fs_invalidate_page,
405  	.releasepage	= f2fs_release_page,
406  #ifdef CONFIG_MIGRATION
407  	.migratepage    = f2fs_migrate_page,
408  #endif
409  };
410  
__add_ino_entry(struct f2fs_sb_info * sbi,nid_t ino,unsigned int devidx,int type)411  static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino,
412  						unsigned int devidx, int type)
413  {
414  	struct inode_management *im = &sbi->im[type];
415  	struct ino_entry *e, *tmp;
416  
417  	tmp = f2fs_kmem_cache_alloc(ino_entry_slab, GFP_NOFS);
418  
419  	radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
420  
421  	spin_lock(&im->ino_lock);
422  	e = radix_tree_lookup(&im->ino_root, ino);
423  	if (!e) {
424  		e = tmp;
425  		if (unlikely(radix_tree_insert(&im->ino_root, ino, e)))
426  			f2fs_bug_on(sbi, 1);
427  
428  		memset(e, 0, sizeof(struct ino_entry));
429  		e->ino = ino;
430  
431  		list_add_tail(&e->list, &im->ino_list);
432  		if (type != ORPHAN_INO)
433  			im->ino_num++;
434  	}
435  
436  	if (type == FLUSH_INO)
437  		f2fs_set_bit(devidx, (char *)&e->dirty_device);
438  
439  	spin_unlock(&im->ino_lock);
440  	radix_tree_preload_end();
441  
442  	if (e != tmp)
443  		kmem_cache_free(ino_entry_slab, tmp);
444  }
445  
__remove_ino_entry(struct f2fs_sb_info * sbi,nid_t ino,int type)446  static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
447  {
448  	struct inode_management *im = &sbi->im[type];
449  	struct ino_entry *e;
450  
451  	spin_lock(&im->ino_lock);
452  	e = radix_tree_lookup(&im->ino_root, ino);
453  	if (e) {
454  		list_del(&e->list);
455  		radix_tree_delete(&im->ino_root, ino);
456  		im->ino_num--;
457  		spin_unlock(&im->ino_lock);
458  		kmem_cache_free(ino_entry_slab, e);
459  		return;
460  	}
461  	spin_unlock(&im->ino_lock);
462  }
463  
add_ino_entry(struct f2fs_sb_info * sbi,nid_t ino,int type)464  void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
465  {
466  	/* add new dirty ino entry into list */
467  	__add_ino_entry(sbi, ino, 0, type);
468  }
469  
remove_ino_entry(struct f2fs_sb_info * sbi,nid_t ino,int type)470  void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
471  {
472  	/* remove dirty ino entry from list */
473  	__remove_ino_entry(sbi, ino, type);
474  }
475  
476  /* mode should be APPEND_INO or UPDATE_INO */
exist_written_data(struct f2fs_sb_info * sbi,nid_t ino,int mode)477  bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode)
478  {
479  	struct inode_management *im = &sbi->im[mode];
480  	struct ino_entry *e;
481  
482  	spin_lock(&im->ino_lock);
483  	e = radix_tree_lookup(&im->ino_root, ino);
484  	spin_unlock(&im->ino_lock);
485  	return e ? true : false;
486  }
487  
release_ino_entry(struct f2fs_sb_info * sbi,bool all)488  void release_ino_entry(struct f2fs_sb_info *sbi, bool all)
489  {
490  	struct ino_entry *e, *tmp;
491  	int i;
492  
493  	for (i = all ? ORPHAN_INO : APPEND_INO; i < MAX_INO_ENTRY; i++) {
494  		struct inode_management *im = &sbi->im[i];
495  
496  		spin_lock(&im->ino_lock);
497  		list_for_each_entry_safe(e, tmp, &im->ino_list, list) {
498  			list_del(&e->list);
499  			radix_tree_delete(&im->ino_root, e->ino);
500  			kmem_cache_free(ino_entry_slab, e);
501  			im->ino_num--;
502  		}
503  		spin_unlock(&im->ino_lock);
504  	}
505  }
506  
set_dirty_device(struct f2fs_sb_info * sbi,nid_t ino,unsigned int devidx,int type)507  void set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
508  					unsigned int devidx, int type)
509  {
510  	__add_ino_entry(sbi, ino, devidx, type);
511  }
512  
is_dirty_device(struct f2fs_sb_info * sbi,nid_t ino,unsigned int devidx,int type)513  bool is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
514  					unsigned int devidx, int type)
515  {
516  	struct inode_management *im = &sbi->im[type];
517  	struct ino_entry *e;
518  	bool is_dirty = false;
519  
520  	spin_lock(&im->ino_lock);
521  	e = radix_tree_lookup(&im->ino_root, ino);
522  	if (e && f2fs_test_bit(devidx, (char *)&e->dirty_device))
523  		is_dirty = true;
524  	spin_unlock(&im->ino_lock);
525  	return is_dirty;
526  }
527  
acquire_orphan_inode(struct f2fs_sb_info * sbi)528  int acquire_orphan_inode(struct f2fs_sb_info *sbi)
529  {
530  	struct inode_management *im = &sbi->im[ORPHAN_INO];
531  	int err = 0;
532  
533  	spin_lock(&im->ino_lock);
534  
535  #ifdef CONFIG_F2FS_FAULT_INJECTION
536  	if (time_to_inject(sbi, FAULT_ORPHAN)) {
537  		spin_unlock(&im->ino_lock);
538  		f2fs_show_injection_info(FAULT_ORPHAN);
539  		return -ENOSPC;
540  	}
541  #endif
542  	if (unlikely(im->ino_num >= sbi->max_orphans))
543  		err = -ENOSPC;
544  	else
545  		im->ino_num++;
546  	spin_unlock(&im->ino_lock);
547  
548  	return err;
549  }
550  
release_orphan_inode(struct f2fs_sb_info * sbi)551  void release_orphan_inode(struct f2fs_sb_info *sbi)
552  {
553  	struct inode_management *im = &sbi->im[ORPHAN_INO];
554  
555  	spin_lock(&im->ino_lock);
556  	f2fs_bug_on(sbi, im->ino_num == 0);
557  	im->ino_num--;
558  	spin_unlock(&im->ino_lock);
559  }
560  
add_orphan_inode(struct inode * inode)561  void add_orphan_inode(struct inode *inode)
562  {
563  	/* add new orphan ino entry into list */
564  	__add_ino_entry(F2FS_I_SB(inode), inode->i_ino, 0, ORPHAN_INO);
565  	update_inode_page(inode);
566  }
567  
remove_orphan_inode(struct f2fs_sb_info * sbi,nid_t ino)568  void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
569  {
570  	/* remove orphan entry from orphan list */
571  	__remove_ino_entry(sbi, ino, ORPHAN_INO);
572  }
573  
recover_orphan_inode(struct f2fs_sb_info * sbi,nid_t ino)574  static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
575  {
576  	struct inode *inode;
577  	struct node_info ni;
578  	int err = acquire_orphan_inode(sbi);
579  
580  	if (err)
581  		goto err_out;
582  
583  	__add_ino_entry(sbi, ino, 0, ORPHAN_INO);
584  
585  	inode = f2fs_iget_retry(sbi->sb, ino);
586  	if (IS_ERR(inode)) {
587  		/*
588  		 * there should be a bug that we can't find the entry
589  		 * to orphan inode.
590  		 */
591  		f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT);
592  		return PTR_ERR(inode);
593  	}
594  
595  	err = dquot_initialize(inode);
596  	if (err)
597  		goto err_out;
598  
599  	dquot_initialize(inode);
600  	clear_nlink(inode);
601  
602  	/* truncate all the data during iput */
603  	iput(inode);
604  
605  	get_node_info(sbi, ino, &ni);
606  
607  	/* ENOMEM was fully retried in f2fs_evict_inode. */
608  	if (ni.blk_addr != NULL_ADDR) {
609  		err = -EIO;
610  		goto err_out;
611  	}
612  	__remove_ino_entry(sbi, ino, ORPHAN_INO);
613  	return 0;
614  
615  err_out:
616  	set_sbi_flag(sbi, SBI_NEED_FSCK);
617  	f2fs_msg(sbi->sb, KERN_WARNING,
618  			"%s: orphan failed (ino=%x), run fsck to fix.",
619  			__func__, ino);
620  	return err;
621  }
622  
recover_orphan_inodes(struct f2fs_sb_info * sbi)623  int recover_orphan_inodes(struct f2fs_sb_info *sbi)
624  {
625  	block_t start_blk, orphan_blocks, i, j;
626  	unsigned int s_flags = sbi->sb->s_flags;
627  	int err = 0;
628  #ifdef CONFIG_QUOTA
629  	int quota_enabled;
630  #endif
631  
632  	if (!is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG))
633  		return 0;
634  
635  	if (s_flags & MS_RDONLY) {
636  		f2fs_msg(sbi->sb, KERN_INFO, "orphan cleanup on readonly fs");
637  		sbi->sb->s_flags &= ~MS_RDONLY;
638  	}
639  
640  #ifdef CONFIG_QUOTA
641  	/* Needed for iput() to work correctly and not trash data */
642  	sbi->sb->s_flags |= MS_ACTIVE;
643  
644  	/* Turn on quotas so that they are updated correctly */
645  	quota_enabled = f2fs_enable_quota_files(sbi, s_flags & MS_RDONLY);
646  #endif
647  
648  	start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
649  	orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
650  
651  	ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true);
652  
653  	for (i = 0; i < orphan_blocks; i++) {
654  		struct page *page = get_meta_page(sbi, start_blk + i);
655  		struct f2fs_orphan_block *orphan_blk;
656  
657  		orphan_blk = (struct f2fs_orphan_block *)page_address(page);
658  		for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
659  			nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
660  			err = recover_orphan_inode(sbi, ino);
661  			if (err) {
662  				f2fs_put_page(page, 1);
663  				goto out;
664  			}
665  		}
666  		f2fs_put_page(page, 1);
667  	}
668  	/* clear Orphan Flag */
669  	clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
670  out:
671  #ifdef CONFIG_QUOTA
672  	/* Turn quotas off */
673  	if (quota_enabled)
674  		f2fs_quota_off_umount(sbi->sb);
675  #endif
676  	sbi->sb->s_flags = s_flags; /* Restore MS_RDONLY status */
677  
678  	return err;
679  }
680  
write_orphan_inodes(struct f2fs_sb_info * sbi,block_t start_blk)681  static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
682  {
683  	struct list_head *head;
684  	struct f2fs_orphan_block *orphan_blk = NULL;
685  	unsigned int nentries = 0;
686  	unsigned short index = 1;
687  	unsigned short orphan_blocks;
688  	struct page *page = NULL;
689  	struct ino_entry *orphan = NULL;
690  	struct inode_management *im = &sbi->im[ORPHAN_INO];
691  
692  	orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num);
693  
694  	/*
695  	 * we don't need to do spin_lock(&im->ino_lock) here, since all the
696  	 * orphan inode operations are covered under f2fs_lock_op().
697  	 * And, spin_lock should be avoided due to page operations below.
698  	 */
699  	head = &im->ino_list;
700  
701  	/* loop for each orphan inode entry and write them in Jornal block */
702  	list_for_each_entry(orphan, head, list) {
703  		if (!page) {
704  			page = grab_meta_page(sbi, start_blk++);
705  			orphan_blk =
706  				(struct f2fs_orphan_block *)page_address(page);
707  			memset(orphan_blk, 0, sizeof(*orphan_blk));
708  		}
709  
710  		orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
711  
712  		if (nentries == F2FS_ORPHANS_PER_BLOCK) {
713  			/*
714  			 * an orphan block is full of 1020 entries,
715  			 * then we need to flush current orphan blocks
716  			 * and bring another one in memory
717  			 */
718  			orphan_blk->blk_addr = cpu_to_le16(index);
719  			orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
720  			orphan_blk->entry_count = cpu_to_le32(nentries);
721  			set_page_dirty(page);
722  			f2fs_put_page(page, 1);
723  			index++;
724  			nentries = 0;
725  			page = NULL;
726  		}
727  	}
728  
729  	if (page) {
730  		orphan_blk->blk_addr = cpu_to_le16(index);
731  		orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
732  		orphan_blk->entry_count = cpu_to_le32(nentries);
733  		set_page_dirty(page);
734  		f2fs_put_page(page, 1);
735  	}
736  }
737  
get_checkpoint_version(struct f2fs_sb_info * sbi,block_t cp_addr,struct f2fs_checkpoint ** cp_block,struct page ** cp_page,unsigned long long * version)738  static int get_checkpoint_version(struct f2fs_sb_info *sbi, block_t cp_addr,
739  		struct f2fs_checkpoint **cp_block, struct page **cp_page,
740  		unsigned long long *version)
741  {
742  	unsigned long blk_size = sbi->blocksize;
743  	size_t crc_offset = 0;
744  	__u32 crc = 0;
745  
746  	*cp_page = get_meta_page(sbi, cp_addr);
747  	*cp_block = (struct f2fs_checkpoint *)page_address(*cp_page);
748  
749  	crc_offset = le32_to_cpu((*cp_block)->checksum_offset);
750  	if (crc_offset > (blk_size - sizeof(__le32))) {
751  		f2fs_msg(sbi->sb, KERN_WARNING,
752  			"invalid crc_offset: %zu", crc_offset);
753  		return -EINVAL;
754  	}
755  
756  	crc = cur_cp_crc(*cp_block);
757  	if (!f2fs_crc_valid(sbi, crc, *cp_block, crc_offset)) {
758  		f2fs_msg(sbi->sb, KERN_WARNING, "invalid crc value");
759  		return -EINVAL;
760  	}
761  
762  	*version = cur_cp_version(*cp_block);
763  	return 0;
764  }
765  
validate_checkpoint(struct f2fs_sb_info * sbi,block_t cp_addr,unsigned long long * version)766  static struct page *validate_checkpoint(struct f2fs_sb_info *sbi,
767  				block_t cp_addr, unsigned long long *version)
768  {
769  	struct page *cp_page_1 = NULL, *cp_page_2 = NULL;
770  	struct f2fs_checkpoint *cp_block = NULL;
771  	unsigned long long cur_version = 0, pre_version = 0;
772  	int err;
773  
774  	err = get_checkpoint_version(sbi, cp_addr, &cp_block,
775  					&cp_page_1, version);
776  	if (err)
777  		goto invalid_cp1;
778  	pre_version = *version;
779  
780  	cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1;
781  	err = get_checkpoint_version(sbi, cp_addr, &cp_block,
782  					&cp_page_2, version);
783  	if (err)
784  		goto invalid_cp2;
785  	cur_version = *version;
786  
787  	if (cur_version == pre_version) {
788  		*version = cur_version;
789  		f2fs_put_page(cp_page_2, 1);
790  		return cp_page_1;
791  	}
792  invalid_cp2:
793  	f2fs_put_page(cp_page_2, 1);
794  invalid_cp1:
795  	f2fs_put_page(cp_page_1, 1);
796  	return NULL;
797  }
798  
get_valid_checkpoint(struct f2fs_sb_info * sbi)799  int get_valid_checkpoint(struct f2fs_sb_info *sbi)
800  {
801  	struct f2fs_checkpoint *cp_block;
802  	struct f2fs_super_block *fsb = sbi->raw_super;
803  	struct page *cp1, *cp2, *cur_page;
804  	unsigned long blk_size = sbi->blocksize;
805  	unsigned long long cp1_version = 0, cp2_version = 0;
806  	unsigned long long cp_start_blk_no;
807  	unsigned int cp_blks = 1 + __cp_payload(sbi);
808  	block_t cp_blk_no;
809  	int i;
810  
811  	sbi->ckpt = f2fs_kzalloc(sbi, cp_blks * blk_size, GFP_KERNEL);
812  	if (!sbi->ckpt)
813  		return -ENOMEM;
814  	/*
815  	 * Finding out valid cp block involves read both
816  	 * sets( cp pack1 and cp pack 2)
817  	 */
818  	cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr);
819  	cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
820  
821  	/* The second checkpoint pack should start at the next segment */
822  	cp_start_blk_no += ((unsigned long long)1) <<
823  				le32_to_cpu(fsb->log_blocks_per_seg);
824  	cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
825  
826  	if (cp1 && cp2) {
827  		if (ver_after(cp2_version, cp1_version))
828  			cur_page = cp2;
829  		else
830  			cur_page = cp1;
831  	} else if (cp1) {
832  		cur_page = cp1;
833  	} else if (cp2) {
834  		cur_page = cp2;
835  	} else {
836  		goto fail_no_cp;
837  	}
838  
839  	cp_block = (struct f2fs_checkpoint *)page_address(cur_page);
840  	memcpy(sbi->ckpt, cp_block, blk_size);
841  
842  	/* Sanity checking of checkpoint */
843  	if (sanity_check_ckpt(sbi))
844  		goto free_fail_no_cp;
845  
846  	if (cur_page == cp1)
847  		sbi->cur_cp_pack = 1;
848  	else
849  		sbi->cur_cp_pack = 2;
850  
851  	if (cp_blks <= 1)
852  		goto done;
853  
854  	cp_blk_no = le32_to_cpu(fsb->cp_blkaddr);
855  	if (cur_page == cp2)
856  		cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg);
857  
858  	for (i = 1; i < cp_blks; i++) {
859  		void *sit_bitmap_ptr;
860  		unsigned char *ckpt = (unsigned char *)sbi->ckpt;
861  
862  		cur_page = get_meta_page(sbi, cp_blk_no + i);
863  		sit_bitmap_ptr = page_address(cur_page);
864  		memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size);
865  		f2fs_put_page(cur_page, 1);
866  	}
867  done:
868  	f2fs_put_page(cp1, 1);
869  	f2fs_put_page(cp2, 1);
870  	return 0;
871  
872  free_fail_no_cp:
873  	f2fs_put_page(cp1, 1);
874  	f2fs_put_page(cp2, 1);
875  fail_no_cp:
876  	kfree(sbi->ckpt);
877  	return -EINVAL;
878  }
879  
__add_dirty_inode(struct inode * inode,enum inode_type type)880  static void __add_dirty_inode(struct inode *inode, enum inode_type type)
881  {
882  	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
883  	int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
884  
885  	if (is_inode_flag_set(inode, flag))
886  		return;
887  
888  	set_inode_flag(inode, flag);
889  	if (!f2fs_is_volatile_file(inode))
890  		list_add_tail(&F2FS_I(inode)->dirty_list,
891  						&sbi->inode_list[type]);
892  	stat_inc_dirty_inode(sbi, type);
893  }
894  
__remove_dirty_inode(struct inode * inode,enum inode_type type)895  static void __remove_dirty_inode(struct inode *inode, enum inode_type type)
896  {
897  	int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
898  
899  	if (get_dirty_pages(inode) || !is_inode_flag_set(inode, flag))
900  		return;
901  
902  	list_del_init(&F2FS_I(inode)->dirty_list);
903  	clear_inode_flag(inode, flag);
904  	stat_dec_dirty_inode(F2FS_I_SB(inode), type);
905  }
906  
update_dirty_page(struct inode * inode,struct page * page)907  void update_dirty_page(struct inode *inode, struct page *page)
908  {
909  	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
910  	enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
911  
912  	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
913  			!S_ISLNK(inode->i_mode))
914  		return;
915  
916  	spin_lock(&sbi->inode_lock[type]);
917  	if (type != FILE_INODE || test_opt(sbi, DATA_FLUSH))
918  		__add_dirty_inode(inode, type);
919  	inode_inc_dirty_pages(inode);
920  	spin_unlock(&sbi->inode_lock[type]);
921  
922  	SetPagePrivate(page);
923  	f2fs_trace_pid(page);
924  }
925  
remove_dirty_inode(struct inode * inode)926  void remove_dirty_inode(struct inode *inode)
927  {
928  	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
929  	enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
930  
931  	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
932  			!S_ISLNK(inode->i_mode))
933  		return;
934  
935  	if (type == FILE_INODE && !test_opt(sbi, DATA_FLUSH))
936  		return;
937  
938  	spin_lock(&sbi->inode_lock[type]);
939  	__remove_dirty_inode(inode, type);
940  	spin_unlock(&sbi->inode_lock[type]);
941  }
942  
sync_dirty_inodes(struct f2fs_sb_info * sbi,enum inode_type type)943  int sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type)
944  {
945  	struct list_head *head;
946  	struct inode *inode;
947  	struct f2fs_inode_info *fi;
948  	bool is_dir = (type == DIR_INODE);
949  	unsigned long ino = 0;
950  
951  	trace_f2fs_sync_dirty_inodes_enter(sbi->sb, is_dir,
952  				get_pages(sbi, is_dir ?
953  				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
954  retry:
955  	if (unlikely(f2fs_cp_error(sbi)))
956  		return -EIO;
957  
958  	spin_lock(&sbi->inode_lock[type]);
959  
960  	head = &sbi->inode_list[type];
961  	if (list_empty(head)) {
962  		spin_unlock(&sbi->inode_lock[type]);
963  		trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir,
964  				get_pages(sbi, is_dir ?
965  				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
966  		return 0;
967  	}
968  	fi = list_first_entry(head, struct f2fs_inode_info, dirty_list);
969  	inode = igrab(&fi->vfs_inode);
970  	spin_unlock(&sbi->inode_lock[type]);
971  	if (inode) {
972  		unsigned long cur_ino = inode->i_ino;
973  
974  		if (is_dir)
975  			F2FS_I(inode)->cp_task = current;
976  
977  		filemap_fdatawrite(inode->i_mapping);
978  
979  		if (is_dir)
980  			F2FS_I(inode)->cp_task = NULL;
981  
982  		iput(inode);
983  		/* We need to give cpu to another writers. */
984  		if (ino == cur_ino) {
985  			congestion_wait(BLK_RW_ASYNC, HZ/50);
986  			cond_resched();
987  		} else {
988  			ino = cur_ino;
989  		}
990  	} else {
991  		/*
992  		 * We should submit bio, since it exists several
993  		 * wribacking dentry pages in the freeing inode.
994  		 */
995  		f2fs_submit_merged_write(sbi, DATA);
996  		cond_resched();
997  	}
998  	goto retry;
999  }
1000  
f2fs_sync_inode_meta(struct f2fs_sb_info * sbi)1001  int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi)
1002  {
1003  	struct list_head *head = &sbi->inode_list[DIRTY_META];
1004  	struct inode *inode;
1005  	struct f2fs_inode_info *fi;
1006  	s64 total = get_pages(sbi, F2FS_DIRTY_IMETA);
1007  
1008  	while (total--) {
1009  		if (unlikely(f2fs_cp_error(sbi)))
1010  			return -EIO;
1011  
1012  		spin_lock(&sbi->inode_lock[DIRTY_META]);
1013  		if (list_empty(head)) {
1014  			spin_unlock(&sbi->inode_lock[DIRTY_META]);
1015  			return 0;
1016  		}
1017  		fi = list_first_entry(head, struct f2fs_inode_info,
1018  							gdirty_list);
1019  		inode = igrab(&fi->vfs_inode);
1020  		spin_unlock(&sbi->inode_lock[DIRTY_META]);
1021  		if (inode) {
1022  			sync_inode_metadata(inode, 0);
1023  
1024  			/* it's on eviction */
1025  			if (is_inode_flag_set(inode, FI_DIRTY_INODE))
1026  				update_inode_page(inode);
1027  			iput(inode);
1028  		}
1029  	}
1030  	return 0;
1031  }
1032  
__prepare_cp_block(struct f2fs_sb_info * sbi)1033  static void __prepare_cp_block(struct f2fs_sb_info *sbi)
1034  {
1035  	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1036  	struct f2fs_nm_info *nm_i = NM_I(sbi);
1037  	nid_t last_nid = nm_i->next_scan_nid;
1038  
1039  	next_free_nid(sbi, &last_nid);
1040  	ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
1041  	ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi));
1042  	ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi));
1043  	ckpt->next_free_nid = cpu_to_le32(last_nid);
1044  }
1045  
1046  /*
1047   * Freeze all the FS-operations for checkpoint.
1048   */
block_operations(struct f2fs_sb_info * sbi)1049  static int block_operations(struct f2fs_sb_info *sbi)
1050  {
1051  	struct writeback_control wbc = {
1052  		.sync_mode = WB_SYNC_ALL,
1053  		.nr_to_write = LONG_MAX,
1054  		.for_reclaim = 0,
1055  	};
1056  	struct blk_plug plug;
1057  	int err = 0;
1058  
1059  	blk_start_plug(&plug);
1060  
1061  retry_flush_dents:
1062  	f2fs_lock_all(sbi);
1063  	/* write all the dirty dentry pages */
1064  	if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
1065  		f2fs_unlock_all(sbi);
1066  		err = sync_dirty_inodes(sbi, DIR_INODE);
1067  		if (err)
1068  			goto out;
1069  		cond_resched();
1070  		goto retry_flush_dents;
1071  	}
1072  
1073  	/*
1074  	 * POR: we should ensure that there are no dirty node pages
1075  	 * until finishing nat/sit flush. inode->i_blocks can be updated.
1076  	 */
1077  	down_write(&sbi->node_change);
1078  
1079  	if (get_pages(sbi, F2FS_DIRTY_IMETA)) {
1080  		up_write(&sbi->node_change);
1081  		f2fs_unlock_all(sbi);
1082  		err = f2fs_sync_inode_meta(sbi);
1083  		if (err)
1084  			goto out;
1085  		cond_resched();
1086  		goto retry_flush_dents;
1087  	}
1088  
1089  retry_flush_nodes:
1090  	down_write(&sbi->node_write);
1091  
1092  	if (get_pages(sbi, F2FS_DIRTY_NODES)) {
1093  		up_write(&sbi->node_write);
1094  		err = sync_node_pages(sbi, &wbc, false, FS_CP_NODE_IO);
1095  		if (err) {
1096  			up_write(&sbi->node_change);
1097  			f2fs_unlock_all(sbi);
1098  			goto out;
1099  		}
1100  		cond_resched();
1101  		goto retry_flush_nodes;
1102  	}
1103  
1104  	/*
1105  	 * sbi->node_change is used only for AIO write_begin path which produces
1106  	 * dirty node blocks and some checkpoint values by block allocation.
1107  	 */
1108  	__prepare_cp_block(sbi);
1109  	up_write(&sbi->node_change);
1110  out:
1111  	blk_finish_plug(&plug);
1112  	return err;
1113  }
1114  
unblock_operations(struct f2fs_sb_info * sbi)1115  static void unblock_operations(struct f2fs_sb_info *sbi)
1116  {
1117  	up_write(&sbi->node_write);
1118  	f2fs_unlock_all(sbi);
1119  }
1120  
wait_on_all_pages_writeback(struct f2fs_sb_info * sbi)1121  static void wait_on_all_pages_writeback(struct f2fs_sb_info *sbi)
1122  {
1123  	DEFINE_WAIT(wait);
1124  
1125  	for (;;) {
1126  		prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
1127  
1128  		if (!get_pages(sbi, F2FS_WB_CP_DATA))
1129  			break;
1130  
1131  		io_schedule_timeout(5*HZ);
1132  	}
1133  	finish_wait(&sbi->cp_wait, &wait);
1134  }
1135  
update_ckpt_flags(struct f2fs_sb_info * sbi,struct cp_control * cpc)1136  static void update_ckpt_flags(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1137  {
1138  	unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num;
1139  	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1140  	unsigned long flags;
1141  
1142  	spin_lock_irqsave(&sbi->cp_lock, flags);
1143  
1144  	if ((cpc->reason & CP_UMOUNT) &&
1145  			le32_to_cpu(ckpt->cp_pack_total_block_count) >
1146  			sbi->blocks_per_seg - NM_I(sbi)->nat_bits_blocks)
1147  		disable_nat_bits(sbi, false);
1148  
1149  	if (cpc->reason & CP_TRIMMED)
1150  		__set_ckpt_flags(ckpt, CP_TRIMMED_FLAG);
1151  	else
1152  		__clear_ckpt_flags(ckpt, CP_TRIMMED_FLAG);
1153  
1154  	if (cpc->reason & CP_UMOUNT)
1155  		__set_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1156  	else
1157  		__clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1158  
1159  	if (cpc->reason & CP_FASTBOOT)
1160  		__set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1161  	else
1162  		__clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1163  
1164  	if (orphan_num)
1165  		__set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1166  	else
1167  		__clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1168  
1169  	if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
1170  		__set_ckpt_flags(ckpt, CP_FSCK_FLAG);
1171  
1172  	/* set this flag to activate crc|cp_ver for recovery */
1173  	__set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG);
1174  	__clear_ckpt_flags(ckpt, CP_NOCRC_RECOVERY_FLAG);
1175  
1176  	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1177  }
1178  
commit_checkpoint(struct f2fs_sb_info * sbi,void * src,block_t blk_addr)1179  static void commit_checkpoint(struct f2fs_sb_info *sbi,
1180  	void *src, block_t blk_addr)
1181  {
1182  	struct writeback_control wbc = {
1183  		.for_reclaim = 0,
1184  	};
1185  
1186  	/*
1187  	 * pagevec_lookup_tag and lock_page again will take
1188  	 * some extra time. Therefore, update_meta_pages and
1189  	 * sync_meta_pages are combined in this function.
1190  	 */
1191  	struct page *page = grab_meta_page(sbi, blk_addr);
1192  	int err;
1193  
1194  	memcpy(page_address(page), src, PAGE_SIZE);
1195  	set_page_dirty(page);
1196  
1197  	f2fs_wait_on_page_writeback(page, META, true);
1198  	f2fs_bug_on(sbi, PageWriteback(page));
1199  	if (unlikely(!clear_page_dirty_for_io(page)))
1200  		f2fs_bug_on(sbi, 1);
1201  
1202  	/* writeout cp pack 2 page */
1203  	err = __f2fs_write_meta_page(page, &wbc, FS_CP_META_IO);
1204  	f2fs_bug_on(sbi, err);
1205  
1206  	f2fs_put_page(page, 0);
1207  
1208  	/* submit checkpoint (with barrier if NOBARRIER is not set) */
1209  	f2fs_submit_merged_write(sbi, META_FLUSH);
1210  }
1211  
do_checkpoint(struct f2fs_sb_info * sbi,struct cp_control * cpc)1212  static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1213  {
1214  	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1215  	struct f2fs_nm_info *nm_i = NM_I(sbi);
1216  	unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num, flags;
1217  	block_t start_blk;
1218  	unsigned int data_sum_blocks, orphan_blocks;
1219  	__u32 crc32 = 0;
1220  	int i;
1221  	int cp_payload_blks = __cp_payload(sbi);
1222  	struct super_block *sb = sbi->sb;
1223  	struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
1224  	u64 kbytes_written;
1225  	int err;
1226  
1227  	/* Flush all the NAT/SIT pages */
1228  	while (get_pages(sbi, F2FS_DIRTY_META)) {
1229  		sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
1230  		if (unlikely(f2fs_cp_error(sbi)))
1231  			return -EIO;
1232  	}
1233  
1234  	/*
1235  	 * modify checkpoint
1236  	 * version number is already updated
1237  	 */
1238  	ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi));
1239  	ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
1240  	for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
1241  		ckpt->cur_node_segno[i] =
1242  			cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE));
1243  		ckpt->cur_node_blkoff[i] =
1244  			cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE));
1245  		ckpt->alloc_type[i + CURSEG_HOT_NODE] =
1246  				curseg_alloc_type(sbi, i + CURSEG_HOT_NODE);
1247  	}
1248  	for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
1249  		ckpt->cur_data_segno[i] =
1250  			cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA));
1251  		ckpt->cur_data_blkoff[i] =
1252  			cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA));
1253  		ckpt->alloc_type[i + CURSEG_HOT_DATA] =
1254  				curseg_alloc_type(sbi, i + CURSEG_HOT_DATA);
1255  	}
1256  
1257  	/* 2 cp  + n data seg summary + orphan inode blocks */
1258  	data_sum_blocks = npages_for_summary_flush(sbi, false);
1259  	spin_lock_irqsave(&sbi->cp_lock, flags);
1260  	if (data_sum_blocks < NR_CURSEG_DATA_TYPE)
1261  		__set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
1262  	else
1263  		__clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
1264  	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1265  
1266  	orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num);
1267  	ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks +
1268  			orphan_blocks);
1269  
1270  	if (__remain_node_summaries(cpc->reason))
1271  		ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS+
1272  				cp_payload_blks + data_sum_blocks +
1273  				orphan_blocks + NR_CURSEG_NODE_TYPE);
1274  	else
1275  		ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
1276  				cp_payload_blks + data_sum_blocks +
1277  				orphan_blocks);
1278  
1279  	/* update ckpt flag for checkpoint */
1280  	update_ckpt_flags(sbi, cpc);
1281  
1282  	/* update SIT/NAT bitmap */
1283  	get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
1284  	get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
1285  
1286  	crc32 = f2fs_crc32(sbi, ckpt, le32_to_cpu(ckpt->checksum_offset));
1287  	*((__le32 *)((unsigned char *)ckpt +
1288  				le32_to_cpu(ckpt->checksum_offset)))
1289  				= cpu_to_le32(crc32);
1290  
1291  	start_blk = __start_cp_next_addr(sbi);
1292  
1293  	/* write nat bits */
1294  	if (enabled_nat_bits(sbi, cpc)) {
1295  		__u64 cp_ver = cur_cp_version(ckpt);
1296  		block_t blk;
1297  
1298  		cp_ver |= ((__u64)crc32 << 32);
1299  		*(__le64 *)nm_i->nat_bits = cpu_to_le64(cp_ver);
1300  
1301  		blk = start_blk + sbi->blocks_per_seg - nm_i->nat_bits_blocks;
1302  		for (i = 0; i < nm_i->nat_bits_blocks; i++)
1303  			update_meta_page(sbi, nm_i->nat_bits +
1304  					(i << F2FS_BLKSIZE_BITS), blk + i);
1305  
1306  		/* Flush all the NAT BITS pages */
1307  		while (get_pages(sbi, F2FS_DIRTY_META)) {
1308  			sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
1309  			if (unlikely(f2fs_cp_error(sbi)))
1310  				return -EIO;
1311  		}
1312  	}
1313  
1314  	/* write out checkpoint buffer at block 0 */
1315  	update_meta_page(sbi, ckpt, start_blk++);
1316  
1317  	for (i = 1; i < 1 + cp_payload_blks; i++)
1318  		update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE,
1319  							start_blk++);
1320  
1321  	if (orphan_num) {
1322  		write_orphan_inodes(sbi, start_blk);
1323  		start_blk += orphan_blocks;
1324  	}
1325  
1326  	write_data_summaries(sbi, start_blk);
1327  	start_blk += data_sum_blocks;
1328  
1329  	/* Record write statistics in the hot node summary */
1330  	kbytes_written = sbi->kbytes_written;
1331  	if (sb->s_bdev->bd_part)
1332  		kbytes_written += BD_PART_WRITTEN(sbi);
1333  
1334  	seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written);
1335  
1336  	if (__remain_node_summaries(cpc->reason)) {
1337  		write_node_summaries(sbi, start_blk);
1338  		start_blk += NR_CURSEG_NODE_TYPE;
1339  	}
1340  
1341  	/* update user_block_counts */
1342  	sbi->last_valid_block_count = sbi->total_valid_block_count;
1343  	percpu_counter_set(&sbi->alloc_valid_block_count, 0);
1344  
1345  	/* Here, we have one bio having CP pack except cp pack 2 page */
1346  	sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
1347  
1348  	/* wait for previous submitted meta pages writeback */
1349  	wait_on_all_pages_writeback(sbi);
1350  
1351  	if (unlikely(f2fs_cp_error(sbi)))
1352  		return -EIO;
1353  
1354  	/* flush all device cache */
1355  	err = f2fs_flush_device_cache(sbi);
1356  	if (err)
1357  		return err;
1358  
1359  	/* barrier and flush checkpoint cp pack 2 page if it can */
1360  	commit_checkpoint(sbi, ckpt, start_blk);
1361  	wait_on_all_pages_writeback(sbi);
1362  
1363  	release_ino_entry(sbi, false);
1364  
1365  	if (unlikely(f2fs_cp_error(sbi)))
1366  		return -EIO;
1367  
1368  	clear_sbi_flag(sbi, SBI_IS_DIRTY);
1369  	clear_sbi_flag(sbi, SBI_NEED_CP);
1370  	__set_cp_next_pack(sbi);
1371  
1372  	/*
1373  	 * redirty superblock if metadata like node page or inode cache is
1374  	 * updated during writing checkpoint.
1375  	 */
1376  	if (get_pages(sbi, F2FS_DIRTY_NODES) ||
1377  			get_pages(sbi, F2FS_DIRTY_IMETA))
1378  		set_sbi_flag(sbi, SBI_IS_DIRTY);
1379  
1380  	f2fs_bug_on(sbi, get_pages(sbi, F2FS_DIRTY_DENTS));
1381  
1382  	return 0;
1383  }
1384  
1385  /*
1386   * We guarantee that this checkpoint procedure will not fail.
1387   */
write_checkpoint(struct f2fs_sb_info * sbi,struct cp_control * cpc)1388  int write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1389  {
1390  	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1391  	unsigned long long ckpt_ver;
1392  	int err = 0;
1393  
1394  	mutex_lock(&sbi->cp_mutex);
1395  
1396  	if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) &&
1397  		((cpc->reason & CP_FASTBOOT) || (cpc->reason & CP_SYNC) ||
1398  		((cpc->reason & CP_DISCARD) && !sbi->discard_blks)))
1399  		goto out;
1400  	if (unlikely(f2fs_cp_error(sbi))) {
1401  		err = -EIO;
1402  		goto out;
1403  	}
1404  	if (f2fs_readonly(sbi->sb)) {
1405  		err = -EROFS;
1406  		goto out;
1407  	}
1408  
1409  	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops");
1410  
1411  	err = block_operations(sbi);
1412  	if (err)
1413  		goto out;
1414  
1415  	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops");
1416  
1417  	f2fs_flush_merged_writes(sbi);
1418  
1419  	/* this is the case of multiple fstrims without any changes */
1420  	if (cpc->reason & CP_DISCARD) {
1421  		if (!exist_trim_candidates(sbi, cpc)) {
1422  			unblock_operations(sbi);
1423  			goto out;
1424  		}
1425  
1426  		if (NM_I(sbi)->dirty_nat_cnt == 0 &&
1427  				SIT_I(sbi)->dirty_sentries == 0 &&
1428  				prefree_segments(sbi) == 0) {
1429  			flush_sit_entries(sbi, cpc);
1430  			clear_prefree_segments(sbi, cpc);
1431  			unblock_operations(sbi);
1432  			goto out;
1433  		}
1434  	}
1435  
1436  	/*
1437  	 * update checkpoint pack index
1438  	 * Increase the version number so that
1439  	 * SIT entries and seg summaries are written at correct place
1440  	 */
1441  	ckpt_ver = cur_cp_version(ckpt);
1442  	ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
1443  
1444  	/* write cached NAT/SIT entries to NAT/SIT area */
1445  	flush_nat_entries(sbi, cpc);
1446  	flush_sit_entries(sbi, cpc);
1447  
1448  	/* unlock all the fs_lock[] in do_checkpoint() */
1449  	err = do_checkpoint(sbi, cpc);
1450  	if (err)
1451  		release_discard_addrs(sbi);
1452  	else
1453  		clear_prefree_segments(sbi, cpc);
1454  
1455  	unblock_operations(sbi);
1456  	stat_inc_cp_count(sbi->stat_info);
1457  
1458  	if (cpc->reason & CP_RECOVERY)
1459  		f2fs_msg(sbi->sb, KERN_NOTICE,
1460  			"checkpoint: version = %llx", ckpt_ver);
1461  
1462  	/* do checkpoint periodically */
1463  	f2fs_update_time(sbi, CP_TIME);
1464  	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint");
1465  out:
1466  	mutex_unlock(&sbi->cp_mutex);
1467  	return err;
1468  }
1469  
init_ino_entry_info(struct f2fs_sb_info * sbi)1470  void init_ino_entry_info(struct f2fs_sb_info *sbi)
1471  {
1472  	int i;
1473  
1474  	for (i = 0; i < MAX_INO_ENTRY; i++) {
1475  		struct inode_management *im = &sbi->im[i];
1476  
1477  		INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC);
1478  		spin_lock_init(&im->ino_lock);
1479  		INIT_LIST_HEAD(&im->ino_list);
1480  		im->ino_num = 0;
1481  	}
1482  
1483  	sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS -
1484  			NR_CURSEG_TYPE - __cp_payload(sbi)) *
1485  				F2FS_ORPHANS_PER_BLOCK;
1486  }
1487  
create_checkpoint_caches(void)1488  int __init create_checkpoint_caches(void)
1489  {
1490  	ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry",
1491  			sizeof(struct ino_entry));
1492  	if (!ino_entry_slab)
1493  		return -ENOMEM;
1494  	inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry",
1495  			sizeof(struct inode_entry));
1496  	if (!inode_entry_slab) {
1497  		kmem_cache_destroy(ino_entry_slab);
1498  		return -ENOMEM;
1499  	}
1500  	return 0;
1501  }
1502  
destroy_checkpoint_caches(void)1503  void destroy_checkpoint_caches(void)
1504  {
1505  	kmem_cache_destroy(ino_entry_slab);
1506  	kmem_cache_destroy(inode_entry_slab);
1507  }
1508