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