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