xref: /linux/fs/f2fs/checkpoint.c (revision 81d8e5e2132215d21f2cddffcd2b16d08c0389fa)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * fs/f2fs/checkpoint.c
4  *
5  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6  *             http://www.samsung.com/
7  */
8 #include <linux/fs.h>
9 #include <linux/bio.h>
10 #include <linux/mpage.h>
11 #include <linux/writeback.h>
12 #include <linux/blkdev.h>
13 #include <linux/f2fs_fs.h>
14 #include <linux/pagevec.h>
15 #include <linux/swap.h>
16 #include <linux/kthread.h>
17 
18 #include "f2fs.h"
19 #include "node.h"
20 #include "segment.h"
21 #include "iostat.h"
22 #include <trace/events/f2fs.h>
23 
24 #define DEFAULT_CHECKPOINT_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_RT, 3))
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,unsigned char reason)29 void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io,
30 						unsigned char reason)
31 {
32 	f2fs_build_fault_attr(sbi, 0, 0);
33 	if (!end_io)
34 		f2fs_flush_merged_writes(sbi);
35 	f2fs_handle_critical_error(sbi, reason);
36 }
37 
38 /*
39  * We guarantee no failure on the returned page.
40  */
f2fs_grab_meta_page(struct f2fs_sb_info * sbi,pgoff_t index)41 struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
42 {
43 	struct address_space *mapping = META_MAPPING(sbi);
44 	struct page *page;
45 repeat:
46 	page = f2fs_grab_cache_page(mapping, index, false);
47 	if (!page) {
48 		cond_resched();
49 		goto repeat;
50 	}
51 	f2fs_wait_on_page_writeback(page, META, true, true);
52 	if (!PageUptodate(page))
53 		SetPageUptodate(page);
54 	return page;
55 }
56 
__get_meta_page(struct f2fs_sb_info * sbi,pgoff_t index,bool is_meta)57 static struct page *__get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index,
58 							bool is_meta)
59 {
60 	struct address_space *mapping = META_MAPPING(sbi);
61 	struct folio *folio;
62 	struct f2fs_io_info fio = {
63 		.sbi = sbi,
64 		.type = META,
65 		.op = REQ_OP_READ,
66 		.op_flags = REQ_META | REQ_PRIO,
67 		.old_blkaddr = index,
68 		.new_blkaddr = index,
69 		.encrypted_page = NULL,
70 		.is_por = !is_meta ? 1 : 0,
71 	};
72 	int err;
73 
74 	if (unlikely(!is_meta))
75 		fio.op_flags &= ~REQ_META;
76 repeat:
77 	folio = f2fs_grab_cache_folio(mapping, index, false);
78 	if (IS_ERR(folio)) {
79 		cond_resched();
80 		goto repeat;
81 	}
82 	if (folio_test_uptodate(folio))
83 		goto out;
84 
85 	fio.page = &folio->page;
86 
87 	err = f2fs_submit_page_bio(&fio);
88 	if (err) {
89 		f2fs_folio_put(folio, true);
90 		return ERR_PTR(err);
91 	}
92 
93 	f2fs_update_iostat(sbi, NULL, FS_META_READ_IO, F2FS_BLKSIZE);
94 
95 	folio_lock(folio);
96 	if (unlikely(folio->mapping != mapping)) {
97 		f2fs_folio_put(folio, true);
98 		goto repeat;
99 	}
100 
101 	if (unlikely(!folio_test_uptodate(folio))) {
102 		f2fs_handle_page_eio(sbi, folio, META);
103 		f2fs_folio_put(folio, true);
104 		return ERR_PTR(-EIO);
105 	}
106 out:
107 	return &folio->page;
108 }
109 
f2fs_get_meta_page(struct f2fs_sb_info * sbi,pgoff_t index)110 struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
111 {
112 	return __get_meta_page(sbi, index, true);
113 }
114 
f2fs_get_meta_page_retry(struct f2fs_sb_info * sbi,pgoff_t index)115 struct page *f2fs_get_meta_page_retry(struct f2fs_sb_info *sbi, pgoff_t index)
116 {
117 	struct page *page;
118 	int count = 0;
119 
120 retry:
121 	page = __get_meta_page(sbi, index, true);
122 	if (IS_ERR(page)) {
123 		if (PTR_ERR(page) == -EIO &&
124 				++count <= DEFAULT_RETRY_IO_COUNT)
125 			goto retry;
126 		f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_META_PAGE);
127 	}
128 	return page;
129 }
130 
131 /* for POR only */
f2fs_get_tmp_page(struct f2fs_sb_info * sbi,pgoff_t index)132 struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index)
133 {
134 	return __get_meta_page(sbi, index, false);
135 }
136 
__is_bitmap_valid(struct f2fs_sb_info * sbi,block_t blkaddr,int type)137 static bool __is_bitmap_valid(struct f2fs_sb_info *sbi, block_t blkaddr,
138 							int type)
139 {
140 	struct seg_entry *se;
141 	unsigned int segno, offset;
142 	bool exist;
143 
144 	if (type == DATA_GENERIC)
145 		return true;
146 
147 	segno = GET_SEGNO(sbi, blkaddr);
148 	offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
149 	se = get_seg_entry(sbi, segno);
150 
151 	exist = f2fs_test_bit(offset, se->cur_valid_map);
152 
153 	/* skip data, if we already have an error in checkpoint. */
154 	if (unlikely(f2fs_cp_error(sbi)))
155 		return exist;
156 
157 	if ((exist && type == DATA_GENERIC_ENHANCE_UPDATE) ||
158 		(!exist && type == DATA_GENERIC_ENHANCE))
159 		goto out_err;
160 	if (!exist && type != DATA_GENERIC_ENHANCE_UPDATE)
161 		goto out_handle;
162 	return exist;
163 
164 out_err:
165 	f2fs_err(sbi, "Inconsistent error blkaddr:%u, sit bitmap:%d",
166 		 blkaddr, exist);
167 	set_sbi_flag(sbi, SBI_NEED_FSCK);
168 	dump_stack();
169 out_handle:
170 	f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR);
171 	return exist;
172 }
173 
__f2fs_is_valid_blkaddr(struct f2fs_sb_info * sbi,block_t blkaddr,int type)174 static bool __f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
175 					block_t blkaddr, int type)
176 {
177 	switch (type) {
178 	case META_NAT:
179 		break;
180 	case META_SIT:
181 		if (unlikely(blkaddr >= SIT_BLK_CNT(sbi)))
182 			goto check_only;
183 		break;
184 	case META_SSA:
185 		if (unlikely(blkaddr >= MAIN_BLKADDR(sbi) ||
186 			blkaddr < SM_I(sbi)->ssa_blkaddr))
187 			goto check_only;
188 		break;
189 	case META_CP:
190 		if (unlikely(blkaddr >= SIT_I(sbi)->sit_base_addr ||
191 			blkaddr < __start_cp_addr(sbi)))
192 			goto check_only;
193 		break;
194 	case META_POR:
195 		if (unlikely(blkaddr >= MAX_BLKADDR(sbi) ||
196 			blkaddr < MAIN_BLKADDR(sbi)))
197 			goto check_only;
198 		break;
199 	case DATA_GENERIC:
200 	case DATA_GENERIC_ENHANCE:
201 	case DATA_GENERIC_ENHANCE_READ:
202 	case DATA_GENERIC_ENHANCE_UPDATE:
203 		if (unlikely(blkaddr >= MAX_BLKADDR(sbi) ||
204 				blkaddr < MAIN_BLKADDR(sbi))) {
205 
206 			/* Skip to emit an error message. */
207 			if (unlikely(f2fs_cp_error(sbi)))
208 				return false;
209 
210 			f2fs_warn(sbi, "access invalid blkaddr:%u",
211 				  blkaddr);
212 			set_sbi_flag(sbi, SBI_NEED_FSCK);
213 			dump_stack();
214 			goto err;
215 		} else {
216 			return __is_bitmap_valid(sbi, blkaddr, type);
217 		}
218 		break;
219 	case META_GENERIC:
220 		if (unlikely(blkaddr < SEG0_BLKADDR(sbi) ||
221 			blkaddr >= MAIN_BLKADDR(sbi)))
222 			goto err;
223 		break;
224 	default:
225 		BUG();
226 	}
227 
228 	return true;
229 err:
230 	f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR);
231 check_only:
232 	return false;
233 }
234 
f2fs_is_valid_blkaddr(struct f2fs_sb_info * sbi,block_t blkaddr,int type)235 bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
236 					block_t blkaddr, int type)
237 {
238 	if (time_to_inject(sbi, FAULT_BLKADDR_VALIDITY))
239 		return false;
240 	return __f2fs_is_valid_blkaddr(sbi, blkaddr, type);
241 }
242 
f2fs_is_valid_blkaddr_raw(struct f2fs_sb_info * sbi,block_t blkaddr,int type)243 bool f2fs_is_valid_blkaddr_raw(struct f2fs_sb_info *sbi,
244 					block_t blkaddr, int type)
245 {
246 	return __f2fs_is_valid_blkaddr(sbi, blkaddr, type);
247 }
248 
249 /*
250  * Readahead CP/NAT/SIT/SSA/POR pages
251  */
f2fs_ra_meta_pages(struct f2fs_sb_info * sbi,block_t start,int nrpages,int type,bool sync)252 int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
253 							int type, bool sync)
254 {
255 	struct page *page;
256 	block_t blkno = start;
257 	struct f2fs_io_info fio = {
258 		.sbi = sbi,
259 		.type = META,
260 		.op = REQ_OP_READ,
261 		.op_flags = sync ? (REQ_META | REQ_PRIO) : REQ_RAHEAD,
262 		.encrypted_page = NULL,
263 		.in_list = 0,
264 		.is_por = (type == META_POR) ? 1 : 0,
265 	};
266 	struct blk_plug plug;
267 	int err;
268 
269 	if (unlikely(type == META_POR))
270 		fio.op_flags &= ~REQ_META;
271 
272 	blk_start_plug(&plug);
273 	for (; nrpages-- > 0; blkno++) {
274 
275 		if (!f2fs_is_valid_blkaddr(sbi, blkno, type))
276 			goto out;
277 
278 		switch (type) {
279 		case META_NAT:
280 			if (unlikely(blkno >=
281 					NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid)))
282 				blkno = 0;
283 			/* get nat block addr */
284 			fio.new_blkaddr = current_nat_addr(sbi,
285 					blkno * NAT_ENTRY_PER_BLOCK);
286 			break;
287 		case META_SIT:
288 			if (unlikely(blkno >= TOTAL_SEGS(sbi)))
289 				goto out;
290 			/* get sit block addr */
291 			fio.new_blkaddr = current_sit_addr(sbi,
292 					blkno * SIT_ENTRY_PER_BLOCK);
293 			break;
294 		case META_SSA:
295 		case META_CP:
296 		case META_POR:
297 			fio.new_blkaddr = blkno;
298 			break;
299 		default:
300 			BUG();
301 		}
302 
303 		page = f2fs_grab_cache_page(META_MAPPING(sbi),
304 						fio.new_blkaddr, false);
305 		if (!page)
306 			continue;
307 		if (PageUptodate(page)) {
308 			f2fs_put_page(page, 1);
309 			continue;
310 		}
311 
312 		fio.page = page;
313 		err = f2fs_submit_page_bio(&fio);
314 		f2fs_put_page(page, err ? 1 : 0);
315 
316 		if (!err)
317 			f2fs_update_iostat(sbi, NULL, FS_META_READ_IO,
318 							F2FS_BLKSIZE);
319 	}
320 out:
321 	blk_finish_plug(&plug);
322 	return blkno - start;
323 }
324 
f2fs_ra_meta_pages_cond(struct f2fs_sb_info * sbi,pgoff_t index,unsigned int ra_blocks)325 void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index,
326 							unsigned int ra_blocks)
327 {
328 	struct page *page;
329 	bool readahead = false;
330 
331 	if (ra_blocks == RECOVERY_MIN_RA_BLOCKS)
332 		return;
333 
334 	page = find_get_page(META_MAPPING(sbi), index);
335 	if (!page || !PageUptodate(page))
336 		readahead = true;
337 	f2fs_put_page(page, 0);
338 
339 	if (readahead)
340 		f2fs_ra_meta_pages(sbi, index, ra_blocks, META_POR, true);
341 }
342 
__f2fs_write_meta_page(struct page * page,struct writeback_control * wbc,enum iostat_type io_type)343 static int __f2fs_write_meta_page(struct page *page,
344 				struct writeback_control *wbc,
345 				enum iostat_type io_type)
346 {
347 	struct f2fs_sb_info *sbi = F2FS_P_SB(page);
348 	struct folio *folio = page_folio(page);
349 
350 	trace_f2fs_writepage(folio, META);
351 
352 	if (unlikely(f2fs_cp_error(sbi))) {
353 		if (is_sbi_flag_set(sbi, SBI_IS_CLOSE)) {
354 			folio_clear_uptodate(folio);
355 			dec_page_count(sbi, F2FS_DIRTY_META);
356 			folio_unlock(folio);
357 			return 0;
358 		}
359 		goto redirty_out;
360 	}
361 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
362 		goto redirty_out;
363 	if (wbc->for_reclaim && folio->index < GET_SUM_BLOCK(sbi, 0))
364 		goto redirty_out;
365 
366 	f2fs_do_write_meta_page(sbi, folio, io_type);
367 	dec_page_count(sbi, F2FS_DIRTY_META);
368 
369 	if (wbc->for_reclaim)
370 		f2fs_submit_merged_write_cond(sbi, NULL, page, 0, META);
371 
372 	folio_unlock(folio);
373 
374 	if (unlikely(f2fs_cp_error(sbi)))
375 		f2fs_submit_merged_write(sbi, META);
376 
377 	return 0;
378 
379 redirty_out:
380 	redirty_page_for_writepage(wbc, page);
381 	return AOP_WRITEPAGE_ACTIVATE;
382 }
383 
f2fs_write_meta_pages(struct address_space * mapping,struct writeback_control * wbc)384 static int f2fs_write_meta_pages(struct address_space *mapping,
385 				struct writeback_control *wbc)
386 {
387 	struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
388 	long diff, written;
389 
390 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
391 		goto skip_write;
392 
393 	/* collect a number of dirty meta pages and write together */
394 	if (wbc->sync_mode != WB_SYNC_ALL &&
395 			get_pages(sbi, F2FS_DIRTY_META) <
396 					nr_pages_to_skip(sbi, META))
397 		goto skip_write;
398 
399 	/* if locked failed, cp will flush dirty pages instead */
400 	if (!f2fs_down_write_trylock(&sbi->cp_global_sem))
401 		goto skip_write;
402 
403 	trace_f2fs_writepages(mapping->host, wbc, META);
404 	diff = nr_pages_to_write(sbi, META, wbc);
405 	written = f2fs_sync_meta_pages(sbi, META, wbc->nr_to_write, FS_META_IO);
406 	f2fs_up_write(&sbi->cp_global_sem);
407 	wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff);
408 	return 0;
409 
410 skip_write:
411 	wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META);
412 	trace_f2fs_writepages(mapping->host, wbc, META);
413 	return 0;
414 }
415 
f2fs_sync_meta_pages(struct f2fs_sb_info * sbi,enum page_type type,long nr_to_write,enum iostat_type io_type)416 long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
417 				long nr_to_write, enum iostat_type io_type)
418 {
419 	struct address_space *mapping = META_MAPPING(sbi);
420 	pgoff_t index = 0, prev = ULONG_MAX;
421 	struct folio_batch fbatch;
422 	long nwritten = 0;
423 	int nr_folios;
424 	struct writeback_control wbc = {
425 		.for_reclaim = 0,
426 	};
427 	struct blk_plug plug;
428 
429 	folio_batch_init(&fbatch);
430 
431 	blk_start_plug(&plug);
432 
433 	while ((nr_folios = filemap_get_folios_tag(mapping, &index,
434 					(pgoff_t)-1,
435 					PAGECACHE_TAG_DIRTY, &fbatch))) {
436 		int i;
437 
438 		for (i = 0; i < nr_folios; i++) {
439 			struct folio *folio = fbatch.folios[i];
440 
441 			if (nr_to_write != LONG_MAX && i != 0 &&
442 					folio->index != prev +
443 					folio_nr_pages(fbatch.folios[i-1])) {
444 				folio_batch_release(&fbatch);
445 				goto stop;
446 			}
447 
448 			folio_lock(folio);
449 
450 			if (unlikely(folio->mapping != mapping)) {
451 continue_unlock:
452 				folio_unlock(folio);
453 				continue;
454 			}
455 			if (!folio_test_dirty(folio)) {
456 				/* someone wrote it for us */
457 				goto continue_unlock;
458 			}
459 
460 			f2fs_wait_on_page_writeback(&folio->page, META,
461 					true, true);
462 
463 			if (!folio_clear_dirty_for_io(folio))
464 				goto continue_unlock;
465 
466 			if (__f2fs_write_meta_page(&folio->page, &wbc,
467 						io_type)) {
468 				folio_unlock(folio);
469 				break;
470 			}
471 			nwritten += folio_nr_pages(folio);
472 			prev = folio->index;
473 			if (unlikely(nwritten >= nr_to_write))
474 				break;
475 		}
476 		folio_batch_release(&fbatch);
477 		cond_resched();
478 	}
479 stop:
480 	if (nwritten)
481 		f2fs_submit_merged_write(sbi, type);
482 
483 	blk_finish_plug(&plug);
484 
485 	return nwritten;
486 }
487 
f2fs_dirty_meta_folio(struct address_space * mapping,struct folio * folio)488 static bool f2fs_dirty_meta_folio(struct address_space *mapping,
489 		struct folio *folio)
490 {
491 	trace_f2fs_set_page_dirty(folio, META);
492 
493 	if (!folio_test_uptodate(folio))
494 		folio_mark_uptodate(folio);
495 	if (filemap_dirty_folio(mapping, folio)) {
496 		inc_page_count(F2FS_M_SB(mapping), F2FS_DIRTY_META);
497 		set_page_private_reference(&folio->page);
498 		return true;
499 	}
500 	return false;
501 }
502 
503 const struct address_space_operations f2fs_meta_aops = {
504 	.writepages	= f2fs_write_meta_pages,
505 	.dirty_folio	= f2fs_dirty_meta_folio,
506 	.invalidate_folio = f2fs_invalidate_folio,
507 	.release_folio	= f2fs_release_folio,
508 	.migrate_folio	= filemap_migrate_folio,
509 };
510 
__add_ino_entry(struct f2fs_sb_info * sbi,nid_t ino,unsigned int devidx,int type)511 static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino,
512 						unsigned int devidx, int type)
513 {
514 	struct inode_management *im = &sbi->im[type];
515 	struct ino_entry *e = NULL, *new = NULL;
516 
517 	if (type == FLUSH_INO) {
518 		rcu_read_lock();
519 		e = radix_tree_lookup(&im->ino_root, ino);
520 		rcu_read_unlock();
521 	}
522 
523 retry:
524 	if (!e)
525 		new = f2fs_kmem_cache_alloc(ino_entry_slab,
526 						GFP_NOFS, true, NULL);
527 
528 	radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
529 
530 	spin_lock(&im->ino_lock);
531 	e = radix_tree_lookup(&im->ino_root, ino);
532 	if (!e) {
533 		if (!new) {
534 			spin_unlock(&im->ino_lock);
535 			radix_tree_preload_end();
536 			goto retry;
537 		}
538 		e = new;
539 		if (unlikely(radix_tree_insert(&im->ino_root, ino, e)))
540 			f2fs_bug_on(sbi, 1);
541 
542 		memset(e, 0, sizeof(struct ino_entry));
543 		e->ino = ino;
544 
545 		list_add_tail(&e->list, &im->ino_list);
546 		if (type != ORPHAN_INO)
547 			im->ino_num++;
548 	}
549 
550 	if (type == FLUSH_INO)
551 		f2fs_set_bit(devidx, (char *)&e->dirty_device);
552 
553 	spin_unlock(&im->ino_lock);
554 	radix_tree_preload_end();
555 
556 	if (new && e != new)
557 		kmem_cache_free(ino_entry_slab, new);
558 }
559 
__remove_ino_entry(struct f2fs_sb_info * sbi,nid_t ino,int type)560 static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
561 {
562 	struct inode_management *im = &sbi->im[type];
563 	struct ino_entry *e;
564 
565 	spin_lock(&im->ino_lock);
566 	e = radix_tree_lookup(&im->ino_root, ino);
567 	if (e) {
568 		list_del(&e->list);
569 		radix_tree_delete(&im->ino_root, ino);
570 		im->ino_num--;
571 		spin_unlock(&im->ino_lock);
572 		kmem_cache_free(ino_entry_slab, e);
573 		return;
574 	}
575 	spin_unlock(&im->ino_lock);
576 }
577 
f2fs_add_ino_entry(struct f2fs_sb_info * sbi,nid_t ino,int type)578 void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
579 {
580 	/* add new dirty ino entry into list */
581 	__add_ino_entry(sbi, ino, 0, type);
582 }
583 
f2fs_remove_ino_entry(struct f2fs_sb_info * sbi,nid_t ino,int type)584 void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
585 {
586 	/* remove dirty ino entry from list */
587 	__remove_ino_entry(sbi, ino, type);
588 }
589 
590 /* mode should be APPEND_INO, UPDATE_INO or TRANS_DIR_INO */
f2fs_exist_written_data(struct f2fs_sb_info * sbi,nid_t ino,int mode)591 bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode)
592 {
593 	struct inode_management *im = &sbi->im[mode];
594 	struct ino_entry *e;
595 
596 	spin_lock(&im->ino_lock);
597 	e = radix_tree_lookup(&im->ino_root, ino);
598 	spin_unlock(&im->ino_lock);
599 	return e ? true : false;
600 }
601 
f2fs_release_ino_entry(struct f2fs_sb_info * sbi,bool all)602 void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all)
603 {
604 	struct ino_entry *e, *tmp;
605 	int i;
606 
607 	for (i = all ? ORPHAN_INO : APPEND_INO; i < MAX_INO_ENTRY; i++) {
608 		struct inode_management *im = &sbi->im[i];
609 
610 		spin_lock(&im->ino_lock);
611 		list_for_each_entry_safe(e, tmp, &im->ino_list, list) {
612 			list_del(&e->list);
613 			radix_tree_delete(&im->ino_root, e->ino);
614 			kmem_cache_free(ino_entry_slab, e);
615 			im->ino_num--;
616 		}
617 		spin_unlock(&im->ino_lock);
618 	}
619 }
620 
f2fs_set_dirty_device(struct f2fs_sb_info * sbi,nid_t ino,unsigned int devidx,int type)621 void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
622 					unsigned int devidx, int type)
623 {
624 	__add_ino_entry(sbi, ino, devidx, type);
625 }
626 
f2fs_is_dirty_device(struct f2fs_sb_info * sbi,nid_t ino,unsigned int devidx,int type)627 bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
628 					unsigned int devidx, int type)
629 {
630 	struct inode_management *im = &sbi->im[type];
631 	struct ino_entry *e;
632 	bool is_dirty = false;
633 
634 	spin_lock(&im->ino_lock);
635 	e = radix_tree_lookup(&im->ino_root, ino);
636 	if (e && f2fs_test_bit(devidx, (char *)&e->dirty_device))
637 		is_dirty = true;
638 	spin_unlock(&im->ino_lock);
639 	return is_dirty;
640 }
641 
f2fs_acquire_orphan_inode(struct f2fs_sb_info * sbi)642 int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi)
643 {
644 	struct inode_management *im = &sbi->im[ORPHAN_INO];
645 	int err = 0;
646 
647 	spin_lock(&im->ino_lock);
648 
649 	if (time_to_inject(sbi, FAULT_ORPHAN)) {
650 		spin_unlock(&im->ino_lock);
651 		return -ENOSPC;
652 	}
653 
654 	if (unlikely(im->ino_num >= sbi->max_orphans))
655 		err = -ENOSPC;
656 	else
657 		im->ino_num++;
658 	spin_unlock(&im->ino_lock);
659 
660 	return err;
661 }
662 
f2fs_release_orphan_inode(struct f2fs_sb_info * sbi)663 void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi)
664 {
665 	struct inode_management *im = &sbi->im[ORPHAN_INO];
666 
667 	spin_lock(&im->ino_lock);
668 	f2fs_bug_on(sbi, im->ino_num == 0);
669 	im->ino_num--;
670 	spin_unlock(&im->ino_lock);
671 }
672 
f2fs_add_orphan_inode(struct inode * inode)673 void f2fs_add_orphan_inode(struct inode *inode)
674 {
675 	/* add new orphan ino entry into list */
676 	__add_ino_entry(F2FS_I_SB(inode), inode->i_ino, 0, ORPHAN_INO);
677 	f2fs_update_inode_page(inode);
678 }
679 
f2fs_remove_orphan_inode(struct f2fs_sb_info * sbi,nid_t ino)680 void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
681 {
682 	/* remove orphan entry from orphan list */
683 	__remove_ino_entry(sbi, ino, ORPHAN_INO);
684 }
685 
recover_orphan_inode(struct f2fs_sb_info * sbi,nid_t ino)686 static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
687 {
688 	struct inode *inode;
689 	struct node_info ni;
690 	int err;
691 
692 	inode = f2fs_iget_retry(sbi->sb, ino);
693 	if (IS_ERR(inode)) {
694 		/*
695 		 * there should be a bug that we can't find the entry
696 		 * to orphan inode.
697 		 */
698 		f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT);
699 		return PTR_ERR(inode);
700 	}
701 
702 	err = f2fs_dquot_initialize(inode);
703 	if (err) {
704 		iput(inode);
705 		goto err_out;
706 	}
707 
708 	clear_nlink(inode);
709 
710 	/* truncate all the data during iput */
711 	iput(inode);
712 
713 	err = f2fs_get_node_info(sbi, ino, &ni, false);
714 	if (err)
715 		goto err_out;
716 
717 	/* ENOMEM was fully retried in f2fs_evict_inode. */
718 	if (ni.blk_addr != NULL_ADDR) {
719 		err = -EIO;
720 		goto err_out;
721 	}
722 	return 0;
723 
724 err_out:
725 	set_sbi_flag(sbi, SBI_NEED_FSCK);
726 	f2fs_warn(sbi, "%s: orphan failed (ino=%x), run fsck to fix.",
727 		  __func__, ino);
728 	return err;
729 }
730 
f2fs_recover_orphan_inodes(struct f2fs_sb_info * sbi)731 int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
732 {
733 	block_t start_blk, orphan_blocks, i, j;
734 	int err = 0;
735 
736 	if (!is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG))
737 		return 0;
738 
739 	if (f2fs_hw_is_readonly(sbi)) {
740 		f2fs_info(sbi, "write access unavailable, skipping orphan cleanup");
741 		return 0;
742 	}
743 
744 	if (is_sbi_flag_set(sbi, SBI_IS_WRITABLE))
745 		f2fs_info(sbi, "orphan cleanup on readonly fs");
746 
747 	start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
748 	orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
749 
750 	f2fs_ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true);
751 
752 	for (i = 0; i < orphan_blocks; i++) {
753 		struct page *page;
754 		struct f2fs_orphan_block *orphan_blk;
755 
756 		page = f2fs_get_meta_page(sbi, start_blk + i);
757 		if (IS_ERR(page)) {
758 			err = PTR_ERR(page);
759 			goto out;
760 		}
761 
762 		orphan_blk = (struct f2fs_orphan_block *)page_address(page);
763 		for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
764 			nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
765 
766 			err = recover_orphan_inode(sbi, ino);
767 			if (err) {
768 				f2fs_put_page(page, 1);
769 				goto out;
770 			}
771 		}
772 		f2fs_put_page(page, 1);
773 	}
774 	/* clear Orphan Flag */
775 	clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
776 out:
777 	set_sbi_flag(sbi, SBI_IS_RECOVERED);
778 
779 	return err;
780 }
781 
write_orphan_inodes(struct f2fs_sb_info * sbi,block_t start_blk)782 static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
783 {
784 	struct list_head *head;
785 	struct f2fs_orphan_block *orphan_blk = NULL;
786 	unsigned int nentries = 0;
787 	unsigned short index = 1;
788 	unsigned short orphan_blocks;
789 	struct page *page = NULL;
790 	struct ino_entry *orphan = NULL;
791 	struct inode_management *im = &sbi->im[ORPHAN_INO];
792 
793 	orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num);
794 
795 	/*
796 	 * we don't need to do spin_lock(&im->ino_lock) here, since all the
797 	 * orphan inode operations are covered under f2fs_lock_op().
798 	 * And, spin_lock should be avoided due to page operations below.
799 	 */
800 	head = &im->ino_list;
801 
802 	/* loop for each orphan inode entry and write them in journal block */
803 	list_for_each_entry(orphan, head, list) {
804 		if (!page) {
805 			page = f2fs_grab_meta_page(sbi, start_blk++);
806 			orphan_blk =
807 				(struct f2fs_orphan_block *)page_address(page);
808 			memset(orphan_blk, 0, sizeof(*orphan_blk));
809 		}
810 
811 		orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
812 
813 		if (nentries == F2FS_ORPHANS_PER_BLOCK) {
814 			/*
815 			 * an orphan block is full of 1020 entries,
816 			 * then we need to flush current orphan blocks
817 			 * and bring another one in memory
818 			 */
819 			orphan_blk->blk_addr = cpu_to_le16(index);
820 			orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
821 			orphan_blk->entry_count = cpu_to_le32(nentries);
822 			set_page_dirty(page);
823 			f2fs_put_page(page, 1);
824 			index++;
825 			nentries = 0;
826 			page = NULL;
827 		}
828 	}
829 
830 	if (page) {
831 		orphan_blk->blk_addr = cpu_to_le16(index);
832 		orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
833 		orphan_blk->entry_count = cpu_to_le32(nentries);
834 		set_page_dirty(page);
835 		f2fs_put_page(page, 1);
836 	}
837 }
838 
f2fs_checkpoint_chksum(struct f2fs_sb_info * sbi,struct f2fs_checkpoint * ckpt)839 static __u32 f2fs_checkpoint_chksum(struct f2fs_sb_info *sbi,
840 						struct f2fs_checkpoint *ckpt)
841 {
842 	unsigned int chksum_ofs = le32_to_cpu(ckpt->checksum_offset);
843 	__u32 chksum;
844 
845 	chksum = f2fs_crc32(sbi, ckpt, chksum_ofs);
846 	if (chksum_ofs < CP_CHKSUM_OFFSET) {
847 		chksum_ofs += sizeof(chksum);
848 		chksum = f2fs_chksum(sbi, chksum, (__u8 *)ckpt + chksum_ofs,
849 						F2FS_BLKSIZE - chksum_ofs);
850 	}
851 	return chksum;
852 }
853 
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)854 static int get_checkpoint_version(struct f2fs_sb_info *sbi, block_t cp_addr,
855 		struct f2fs_checkpoint **cp_block, struct page **cp_page,
856 		unsigned long long *version)
857 {
858 	size_t crc_offset = 0;
859 	__u32 crc;
860 
861 	*cp_page = f2fs_get_meta_page(sbi, cp_addr);
862 	if (IS_ERR(*cp_page))
863 		return PTR_ERR(*cp_page);
864 
865 	*cp_block = (struct f2fs_checkpoint *)page_address(*cp_page);
866 
867 	crc_offset = le32_to_cpu((*cp_block)->checksum_offset);
868 	if (crc_offset < CP_MIN_CHKSUM_OFFSET ||
869 			crc_offset > CP_CHKSUM_OFFSET) {
870 		f2fs_put_page(*cp_page, 1);
871 		f2fs_warn(sbi, "invalid crc_offset: %zu", crc_offset);
872 		return -EINVAL;
873 	}
874 
875 	crc = f2fs_checkpoint_chksum(sbi, *cp_block);
876 	if (crc != cur_cp_crc(*cp_block)) {
877 		f2fs_put_page(*cp_page, 1);
878 		f2fs_warn(sbi, "invalid crc value");
879 		return -EINVAL;
880 	}
881 
882 	*version = cur_cp_version(*cp_block);
883 	return 0;
884 }
885 
validate_checkpoint(struct f2fs_sb_info * sbi,block_t cp_addr,unsigned long long * version)886 static struct page *validate_checkpoint(struct f2fs_sb_info *sbi,
887 				block_t cp_addr, unsigned long long *version)
888 {
889 	struct page *cp_page_1 = NULL, *cp_page_2 = NULL;
890 	struct f2fs_checkpoint *cp_block = NULL;
891 	unsigned long long cur_version = 0, pre_version = 0;
892 	unsigned int cp_blocks;
893 	int err;
894 
895 	err = get_checkpoint_version(sbi, cp_addr, &cp_block,
896 					&cp_page_1, version);
897 	if (err)
898 		return NULL;
899 
900 	cp_blocks = le32_to_cpu(cp_block->cp_pack_total_block_count);
901 
902 	if (cp_blocks > BLKS_PER_SEG(sbi) || cp_blocks <= F2FS_CP_PACKS) {
903 		f2fs_warn(sbi, "invalid cp_pack_total_block_count:%u",
904 			  le32_to_cpu(cp_block->cp_pack_total_block_count));
905 		goto invalid_cp;
906 	}
907 	pre_version = *version;
908 
909 	cp_addr += cp_blocks - 1;
910 	err = get_checkpoint_version(sbi, cp_addr, &cp_block,
911 					&cp_page_2, version);
912 	if (err)
913 		goto invalid_cp;
914 	cur_version = *version;
915 
916 	if (cur_version == pre_version) {
917 		*version = cur_version;
918 		f2fs_put_page(cp_page_2, 1);
919 		return cp_page_1;
920 	}
921 	f2fs_put_page(cp_page_2, 1);
922 invalid_cp:
923 	f2fs_put_page(cp_page_1, 1);
924 	return NULL;
925 }
926 
f2fs_get_valid_checkpoint(struct f2fs_sb_info * sbi)927 int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi)
928 {
929 	struct f2fs_checkpoint *cp_block;
930 	struct f2fs_super_block *fsb = sbi->raw_super;
931 	struct page *cp1, *cp2, *cur_page;
932 	unsigned long blk_size = sbi->blocksize;
933 	unsigned long long cp1_version = 0, cp2_version = 0;
934 	unsigned long long cp_start_blk_no;
935 	unsigned int cp_blks = 1 + __cp_payload(sbi);
936 	block_t cp_blk_no;
937 	int i;
938 	int err;
939 
940 	sbi->ckpt = f2fs_kvzalloc(sbi, array_size(blk_size, cp_blks),
941 				  GFP_KERNEL);
942 	if (!sbi->ckpt)
943 		return -ENOMEM;
944 	/*
945 	 * Finding out valid cp block involves read both
946 	 * sets( cp pack 1 and cp pack 2)
947 	 */
948 	cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr);
949 	cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
950 
951 	/* The second checkpoint pack should start at the next segment */
952 	cp_start_blk_no += ((unsigned long long)1) <<
953 				le32_to_cpu(fsb->log_blocks_per_seg);
954 	cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
955 
956 	if (cp1 && cp2) {
957 		if (ver_after(cp2_version, cp1_version))
958 			cur_page = cp2;
959 		else
960 			cur_page = cp1;
961 	} else if (cp1) {
962 		cur_page = cp1;
963 	} else if (cp2) {
964 		cur_page = cp2;
965 	} else {
966 		err = -EFSCORRUPTED;
967 		goto fail_no_cp;
968 	}
969 
970 	cp_block = (struct f2fs_checkpoint *)page_address(cur_page);
971 	memcpy(sbi->ckpt, cp_block, blk_size);
972 
973 	if (cur_page == cp1)
974 		sbi->cur_cp_pack = 1;
975 	else
976 		sbi->cur_cp_pack = 2;
977 
978 	/* Sanity checking of checkpoint */
979 	if (f2fs_sanity_check_ckpt(sbi)) {
980 		err = -EFSCORRUPTED;
981 		goto free_fail_no_cp;
982 	}
983 
984 	if (cp_blks <= 1)
985 		goto done;
986 
987 	cp_blk_no = le32_to_cpu(fsb->cp_blkaddr);
988 	if (cur_page == cp2)
989 		cp_blk_no += BIT(le32_to_cpu(fsb->log_blocks_per_seg));
990 
991 	for (i = 1; i < cp_blks; i++) {
992 		void *sit_bitmap_ptr;
993 		unsigned char *ckpt = (unsigned char *)sbi->ckpt;
994 
995 		cur_page = f2fs_get_meta_page(sbi, cp_blk_no + i);
996 		if (IS_ERR(cur_page)) {
997 			err = PTR_ERR(cur_page);
998 			goto free_fail_no_cp;
999 		}
1000 		sit_bitmap_ptr = page_address(cur_page);
1001 		memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size);
1002 		f2fs_put_page(cur_page, 1);
1003 	}
1004 done:
1005 	f2fs_put_page(cp1, 1);
1006 	f2fs_put_page(cp2, 1);
1007 	return 0;
1008 
1009 free_fail_no_cp:
1010 	f2fs_put_page(cp1, 1);
1011 	f2fs_put_page(cp2, 1);
1012 fail_no_cp:
1013 	kvfree(sbi->ckpt);
1014 	return err;
1015 }
1016 
__add_dirty_inode(struct inode * inode,enum inode_type type)1017 static void __add_dirty_inode(struct inode *inode, enum inode_type type)
1018 {
1019 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1020 	int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
1021 
1022 	if (is_inode_flag_set(inode, flag))
1023 		return;
1024 
1025 	set_inode_flag(inode, flag);
1026 	list_add_tail(&F2FS_I(inode)->dirty_list, &sbi->inode_list[type]);
1027 	stat_inc_dirty_inode(sbi, type);
1028 }
1029 
__remove_dirty_inode(struct inode * inode,enum inode_type type)1030 static void __remove_dirty_inode(struct inode *inode, enum inode_type type)
1031 {
1032 	int flag = (type == DIR_INODE) ? FI_DIRTY_DIR : FI_DIRTY_FILE;
1033 
1034 	if (get_dirty_pages(inode) || !is_inode_flag_set(inode, flag))
1035 		return;
1036 
1037 	list_del_init(&F2FS_I(inode)->dirty_list);
1038 	clear_inode_flag(inode, flag);
1039 	stat_dec_dirty_inode(F2FS_I_SB(inode), type);
1040 }
1041 
f2fs_update_dirty_folio(struct inode * inode,struct folio * folio)1042 void f2fs_update_dirty_folio(struct inode *inode, struct folio *folio)
1043 {
1044 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1045 	enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
1046 
1047 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
1048 			!S_ISLNK(inode->i_mode))
1049 		return;
1050 
1051 	spin_lock(&sbi->inode_lock[type]);
1052 	if (type != FILE_INODE || test_opt(sbi, DATA_FLUSH))
1053 		__add_dirty_inode(inode, type);
1054 	inode_inc_dirty_pages(inode);
1055 	spin_unlock(&sbi->inode_lock[type]);
1056 
1057 	set_page_private_reference(&folio->page);
1058 }
1059 
f2fs_remove_dirty_inode(struct inode * inode)1060 void f2fs_remove_dirty_inode(struct inode *inode)
1061 {
1062 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1063 	enum inode_type type = S_ISDIR(inode->i_mode) ? DIR_INODE : FILE_INODE;
1064 
1065 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
1066 			!S_ISLNK(inode->i_mode))
1067 		return;
1068 
1069 	if (type == FILE_INODE && !test_opt(sbi, DATA_FLUSH))
1070 		return;
1071 
1072 	spin_lock(&sbi->inode_lock[type]);
1073 	__remove_dirty_inode(inode, type);
1074 	spin_unlock(&sbi->inode_lock[type]);
1075 }
1076 
f2fs_sync_dirty_inodes(struct f2fs_sb_info * sbi,enum inode_type type,bool from_cp)1077 int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type,
1078 						bool from_cp)
1079 {
1080 	struct list_head *head;
1081 	struct inode *inode;
1082 	struct f2fs_inode_info *fi;
1083 	bool is_dir = (type == DIR_INODE);
1084 	unsigned long ino = 0;
1085 
1086 	trace_f2fs_sync_dirty_inodes_enter(sbi->sb, is_dir,
1087 				get_pages(sbi, is_dir ?
1088 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
1089 retry:
1090 	if (unlikely(f2fs_cp_error(sbi))) {
1091 		trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir,
1092 				get_pages(sbi, is_dir ?
1093 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
1094 		return -EIO;
1095 	}
1096 
1097 	spin_lock(&sbi->inode_lock[type]);
1098 
1099 	head = &sbi->inode_list[type];
1100 	if (list_empty(head)) {
1101 		spin_unlock(&sbi->inode_lock[type]);
1102 		trace_f2fs_sync_dirty_inodes_exit(sbi->sb, is_dir,
1103 				get_pages(sbi, is_dir ?
1104 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA));
1105 		return 0;
1106 	}
1107 	fi = list_first_entry(head, struct f2fs_inode_info, dirty_list);
1108 	inode = igrab(&fi->vfs_inode);
1109 	spin_unlock(&sbi->inode_lock[type]);
1110 	if (inode) {
1111 		unsigned long cur_ino = inode->i_ino;
1112 
1113 		if (from_cp)
1114 			F2FS_I(inode)->cp_task = current;
1115 		F2FS_I(inode)->wb_task = current;
1116 
1117 		filemap_fdatawrite(inode->i_mapping);
1118 
1119 		F2FS_I(inode)->wb_task = NULL;
1120 		if (from_cp)
1121 			F2FS_I(inode)->cp_task = NULL;
1122 
1123 		iput(inode);
1124 		/* We need to give cpu to another writers. */
1125 		if (ino == cur_ino)
1126 			cond_resched();
1127 		else
1128 			ino = cur_ino;
1129 	} else {
1130 		/*
1131 		 * We should submit bio, since it exists several
1132 		 * writebacking dentry pages in the freeing inode.
1133 		 */
1134 		f2fs_submit_merged_write(sbi, DATA);
1135 		cond_resched();
1136 	}
1137 	goto retry;
1138 }
1139 
f2fs_sync_inode_meta(struct f2fs_sb_info * sbi)1140 static int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi)
1141 {
1142 	struct list_head *head = &sbi->inode_list[DIRTY_META];
1143 	struct inode *inode;
1144 	struct f2fs_inode_info *fi;
1145 	s64 total = get_pages(sbi, F2FS_DIRTY_IMETA);
1146 
1147 	while (total--) {
1148 		if (unlikely(f2fs_cp_error(sbi)))
1149 			return -EIO;
1150 
1151 		spin_lock(&sbi->inode_lock[DIRTY_META]);
1152 		if (list_empty(head)) {
1153 			spin_unlock(&sbi->inode_lock[DIRTY_META]);
1154 			return 0;
1155 		}
1156 		fi = list_first_entry(head, struct f2fs_inode_info,
1157 							gdirty_list);
1158 		inode = igrab(&fi->vfs_inode);
1159 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
1160 		if (inode) {
1161 			sync_inode_metadata(inode, 0);
1162 
1163 			/* it's on eviction */
1164 			if (is_inode_flag_set(inode, FI_DIRTY_INODE))
1165 				f2fs_update_inode_page(inode);
1166 			iput(inode);
1167 		}
1168 	}
1169 	return 0;
1170 }
1171 
__prepare_cp_block(struct f2fs_sb_info * sbi)1172 static void __prepare_cp_block(struct f2fs_sb_info *sbi)
1173 {
1174 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1175 	struct f2fs_nm_info *nm_i = NM_I(sbi);
1176 	nid_t last_nid = nm_i->next_scan_nid;
1177 
1178 	next_free_nid(sbi, &last_nid);
1179 	ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
1180 	ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi));
1181 	ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi));
1182 	ckpt->next_free_nid = cpu_to_le32(last_nid);
1183 
1184 	/* update user_block_counts */
1185 	sbi->last_valid_block_count = sbi->total_valid_block_count;
1186 	percpu_counter_set(&sbi->alloc_valid_block_count, 0);
1187 	percpu_counter_set(&sbi->rf_node_block_count, 0);
1188 }
1189 
__need_flush_quota(struct f2fs_sb_info * sbi)1190 static bool __need_flush_quota(struct f2fs_sb_info *sbi)
1191 {
1192 	bool ret = false;
1193 
1194 	if (!is_journalled_quota(sbi))
1195 		return false;
1196 
1197 	if (!f2fs_down_write_trylock(&sbi->quota_sem))
1198 		return true;
1199 	if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH)) {
1200 		ret = false;
1201 	} else if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR)) {
1202 		ret = false;
1203 	} else if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_FLUSH)) {
1204 		clear_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH);
1205 		ret = true;
1206 	} else if (get_pages(sbi, F2FS_DIRTY_QDATA)) {
1207 		ret = true;
1208 	}
1209 	f2fs_up_write(&sbi->quota_sem);
1210 	return ret;
1211 }
1212 
1213 /*
1214  * Freeze all the FS-operations for checkpoint.
1215  */
block_operations(struct f2fs_sb_info * sbi)1216 static int block_operations(struct f2fs_sb_info *sbi)
1217 {
1218 	struct writeback_control wbc = {
1219 		.sync_mode = WB_SYNC_ALL,
1220 		.nr_to_write = LONG_MAX,
1221 		.for_reclaim = 0,
1222 	};
1223 	int err = 0, cnt = 0;
1224 
1225 	/*
1226 	 * Let's flush inline_data in dirty node pages.
1227 	 */
1228 	f2fs_flush_inline_data(sbi);
1229 
1230 retry_flush_quotas:
1231 	f2fs_lock_all(sbi);
1232 	if (__need_flush_quota(sbi)) {
1233 		bool need_lock = sbi->umount_lock_holder != current;
1234 
1235 		if (++cnt > DEFAULT_RETRY_QUOTA_FLUSH_COUNT) {
1236 			set_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH);
1237 			set_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH);
1238 			goto retry_flush_dents;
1239 		}
1240 		f2fs_unlock_all(sbi);
1241 
1242 		/* don't grab s_umount lock during mount/umount/remount/freeze/quotactl */
1243 		if (!need_lock) {
1244 			f2fs_do_quota_sync(sbi->sb, -1);
1245 		} else if (down_read_trylock(&sbi->sb->s_umount)) {
1246 			f2fs_do_quota_sync(sbi->sb, -1);
1247 			up_read(&sbi->sb->s_umount);
1248 		}
1249 		cond_resched();
1250 		goto retry_flush_quotas;
1251 	}
1252 
1253 retry_flush_dents:
1254 	/* write all the dirty dentry pages */
1255 	if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
1256 		f2fs_unlock_all(sbi);
1257 		err = f2fs_sync_dirty_inodes(sbi, DIR_INODE, true);
1258 		if (err)
1259 			return err;
1260 		cond_resched();
1261 		goto retry_flush_quotas;
1262 	}
1263 
1264 	/*
1265 	 * POR: we should ensure that there are no dirty node pages
1266 	 * until finishing nat/sit flush. inode->i_blocks can be updated.
1267 	 */
1268 	f2fs_down_write(&sbi->node_change);
1269 
1270 	if (get_pages(sbi, F2FS_DIRTY_IMETA)) {
1271 		f2fs_up_write(&sbi->node_change);
1272 		f2fs_unlock_all(sbi);
1273 		err = f2fs_sync_inode_meta(sbi);
1274 		if (err)
1275 			return err;
1276 		cond_resched();
1277 		goto retry_flush_quotas;
1278 	}
1279 
1280 retry_flush_nodes:
1281 	f2fs_down_write(&sbi->node_write);
1282 
1283 	if (get_pages(sbi, F2FS_DIRTY_NODES)) {
1284 		f2fs_up_write(&sbi->node_write);
1285 		atomic_inc(&sbi->wb_sync_req[NODE]);
1286 		err = f2fs_sync_node_pages(sbi, &wbc, false, FS_CP_NODE_IO);
1287 		atomic_dec(&sbi->wb_sync_req[NODE]);
1288 		if (err) {
1289 			f2fs_up_write(&sbi->node_change);
1290 			f2fs_unlock_all(sbi);
1291 			return err;
1292 		}
1293 		cond_resched();
1294 		goto retry_flush_nodes;
1295 	}
1296 
1297 	/*
1298 	 * sbi->node_change is used only for AIO write_begin path which produces
1299 	 * dirty node blocks and some checkpoint values by block allocation.
1300 	 */
1301 	__prepare_cp_block(sbi);
1302 	f2fs_up_write(&sbi->node_change);
1303 	return err;
1304 }
1305 
unblock_operations(struct f2fs_sb_info * sbi)1306 static void unblock_operations(struct f2fs_sb_info *sbi)
1307 {
1308 	f2fs_up_write(&sbi->node_write);
1309 	f2fs_unlock_all(sbi);
1310 }
1311 
f2fs_wait_on_all_pages(struct f2fs_sb_info * sbi,int type)1312 void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type)
1313 {
1314 	DEFINE_WAIT(wait);
1315 
1316 	for (;;) {
1317 		if (!get_pages(sbi, type))
1318 			break;
1319 
1320 		if (unlikely(f2fs_cp_error(sbi) &&
1321 			!is_sbi_flag_set(sbi, SBI_IS_CLOSE)))
1322 			break;
1323 
1324 		if (type == F2FS_DIRTY_META)
1325 			f2fs_sync_meta_pages(sbi, META, LONG_MAX,
1326 							FS_CP_META_IO);
1327 		else if (type == F2FS_WB_CP_DATA)
1328 			f2fs_submit_merged_write(sbi, DATA);
1329 
1330 		prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
1331 		io_schedule_timeout(DEFAULT_IO_TIMEOUT);
1332 	}
1333 	finish_wait(&sbi->cp_wait, &wait);
1334 }
1335 
update_ckpt_flags(struct f2fs_sb_info * sbi,struct cp_control * cpc)1336 static void update_ckpt_flags(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1337 {
1338 	unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num;
1339 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1340 	unsigned long flags;
1341 
1342 	spin_lock_irqsave(&sbi->cp_lock, flags);
1343 
1344 	if ((cpc->reason & CP_UMOUNT) &&
1345 			le32_to_cpu(ckpt->cp_pack_total_block_count) >
1346 			sbi->blocks_per_seg - NM_I(sbi)->nat_bits_blocks)
1347 		disable_nat_bits(sbi, false);
1348 
1349 	if (cpc->reason & CP_TRIMMED)
1350 		__set_ckpt_flags(ckpt, CP_TRIMMED_FLAG);
1351 	else
1352 		__clear_ckpt_flags(ckpt, CP_TRIMMED_FLAG);
1353 
1354 	if (cpc->reason & CP_UMOUNT)
1355 		__set_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1356 	else
1357 		__clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
1358 
1359 	if (cpc->reason & CP_FASTBOOT)
1360 		__set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1361 	else
1362 		__clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
1363 
1364 	if (orphan_num)
1365 		__set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1366 	else
1367 		__clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
1368 
1369 	if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
1370 		__set_ckpt_flags(ckpt, CP_FSCK_FLAG);
1371 
1372 	if (is_sbi_flag_set(sbi, SBI_IS_RESIZEFS))
1373 		__set_ckpt_flags(ckpt, CP_RESIZEFS_FLAG);
1374 	else
1375 		__clear_ckpt_flags(ckpt, CP_RESIZEFS_FLAG);
1376 
1377 	if (is_sbi_flag_set(sbi, SBI_CP_DISABLED))
1378 		__set_ckpt_flags(ckpt, CP_DISABLED_FLAG);
1379 	else
1380 		__clear_ckpt_flags(ckpt, CP_DISABLED_FLAG);
1381 
1382 	if (is_sbi_flag_set(sbi, SBI_CP_DISABLED_QUICK))
1383 		__set_ckpt_flags(ckpt, CP_DISABLED_QUICK_FLAG);
1384 	else
1385 		__clear_ckpt_flags(ckpt, CP_DISABLED_QUICK_FLAG);
1386 
1387 	if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH))
1388 		__set_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG);
1389 	else
1390 		__clear_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG);
1391 
1392 	if (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR))
1393 		__set_ckpt_flags(ckpt, CP_QUOTA_NEED_FSCK_FLAG);
1394 
1395 	/* set this flag to activate crc|cp_ver for recovery */
1396 	__set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG);
1397 	__clear_ckpt_flags(ckpt, CP_NOCRC_RECOVERY_FLAG);
1398 
1399 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1400 }
1401 
commit_checkpoint(struct f2fs_sb_info * sbi,void * src,block_t blk_addr)1402 static void commit_checkpoint(struct f2fs_sb_info *sbi,
1403 	void *src, block_t blk_addr)
1404 {
1405 	struct writeback_control wbc = {
1406 		.for_reclaim = 0,
1407 	};
1408 
1409 	/*
1410 	 * filemap_get_folios_tag and lock_page again will take
1411 	 * some extra time. Therefore, f2fs_update_meta_pages and
1412 	 * f2fs_sync_meta_pages are combined in this function.
1413 	 */
1414 	struct page *page = f2fs_grab_meta_page(sbi, blk_addr);
1415 	int err;
1416 
1417 	f2fs_wait_on_page_writeback(page, META, true, true);
1418 
1419 	memcpy(page_address(page), src, PAGE_SIZE);
1420 
1421 	set_page_dirty(page);
1422 	if (unlikely(!clear_page_dirty_for_io(page)))
1423 		f2fs_bug_on(sbi, 1);
1424 
1425 	/* writeout cp pack 2 page */
1426 	err = __f2fs_write_meta_page(page, &wbc, FS_CP_META_IO);
1427 	if (unlikely(err && f2fs_cp_error(sbi))) {
1428 		f2fs_put_page(page, 1);
1429 		return;
1430 	}
1431 
1432 	f2fs_bug_on(sbi, err);
1433 	f2fs_put_page(page, 0);
1434 
1435 	/* submit checkpoint (with barrier if NOBARRIER is not set) */
1436 	f2fs_submit_merged_write(sbi, META_FLUSH);
1437 }
1438 
get_sectors_written(struct block_device * bdev)1439 static inline u64 get_sectors_written(struct block_device *bdev)
1440 {
1441 	return (u64)part_stat_read(bdev, sectors[STAT_WRITE]);
1442 }
1443 
f2fs_get_sectors_written(struct f2fs_sb_info * sbi)1444 u64 f2fs_get_sectors_written(struct f2fs_sb_info *sbi)
1445 {
1446 	if (f2fs_is_multi_device(sbi)) {
1447 		u64 sectors = 0;
1448 		int i;
1449 
1450 		for (i = 0; i < sbi->s_ndevs; i++)
1451 			sectors += get_sectors_written(FDEV(i).bdev);
1452 
1453 		return sectors;
1454 	}
1455 
1456 	return get_sectors_written(sbi->sb->s_bdev);
1457 }
1458 
do_checkpoint(struct f2fs_sb_info * sbi,struct cp_control * cpc)1459 static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1460 {
1461 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1462 	struct f2fs_nm_info *nm_i = NM_I(sbi);
1463 	unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num, flags;
1464 	block_t start_blk;
1465 	unsigned int data_sum_blocks, orphan_blocks;
1466 	__u32 crc32 = 0;
1467 	int i;
1468 	int cp_payload_blks = __cp_payload(sbi);
1469 	struct curseg_info *seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
1470 	u64 kbytes_written;
1471 	int err;
1472 
1473 	/* Flush all the NAT/SIT pages */
1474 	f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
1475 
1476 	/* start to update checkpoint, cp ver is already updated previously */
1477 	ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi, true));
1478 	ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
1479 	for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
1480 		struct curseg_info *curseg = CURSEG_I(sbi, i + CURSEG_HOT_NODE);
1481 
1482 		ckpt->cur_node_segno[i] = cpu_to_le32(curseg->segno);
1483 		ckpt->cur_node_blkoff[i] = cpu_to_le16(curseg->next_blkoff);
1484 		ckpt->alloc_type[i + CURSEG_HOT_NODE] = curseg->alloc_type;
1485 	}
1486 	for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
1487 		struct curseg_info *curseg = CURSEG_I(sbi, i + CURSEG_HOT_DATA);
1488 
1489 		ckpt->cur_data_segno[i] = cpu_to_le32(curseg->segno);
1490 		ckpt->cur_data_blkoff[i] = cpu_to_le16(curseg->next_blkoff);
1491 		ckpt->alloc_type[i + CURSEG_HOT_DATA] = curseg->alloc_type;
1492 	}
1493 
1494 	/* 2 cp + n data seg summary + orphan inode blocks */
1495 	data_sum_blocks = f2fs_npages_for_summary_flush(sbi, false);
1496 	spin_lock_irqsave(&sbi->cp_lock, flags);
1497 	if (data_sum_blocks < NR_CURSEG_DATA_TYPE)
1498 		__set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
1499 	else
1500 		__clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
1501 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1502 
1503 	orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num);
1504 	ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks +
1505 			orphan_blocks);
1506 
1507 	if (__remain_node_summaries(cpc->reason))
1508 		ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
1509 				cp_payload_blks + data_sum_blocks +
1510 				orphan_blocks + NR_CURSEG_NODE_TYPE);
1511 	else
1512 		ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
1513 				cp_payload_blks + data_sum_blocks +
1514 				orphan_blocks);
1515 
1516 	/* update ckpt flag for checkpoint */
1517 	update_ckpt_flags(sbi, cpc);
1518 
1519 	/* update SIT/NAT bitmap */
1520 	get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
1521 	get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
1522 
1523 	crc32 = f2fs_checkpoint_chksum(sbi, ckpt);
1524 	*((__le32 *)((unsigned char *)ckpt +
1525 				le32_to_cpu(ckpt->checksum_offset)))
1526 				= cpu_to_le32(crc32);
1527 
1528 	start_blk = __start_cp_next_addr(sbi);
1529 
1530 	/* write nat bits */
1531 	if (enabled_nat_bits(sbi, cpc)) {
1532 		__u64 cp_ver = cur_cp_version(ckpt);
1533 		block_t blk;
1534 
1535 		cp_ver |= ((__u64)crc32 << 32);
1536 		*(__le64 *)nm_i->nat_bits = cpu_to_le64(cp_ver);
1537 
1538 		blk = start_blk + BLKS_PER_SEG(sbi) - nm_i->nat_bits_blocks;
1539 		for (i = 0; i < nm_i->nat_bits_blocks; i++)
1540 			f2fs_update_meta_page(sbi, nm_i->nat_bits +
1541 					F2FS_BLK_TO_BYTES(i), blk + i);
1542 	}
1543 
1544 	/* write out checkpoint buffer at block 0 */
1545 	f2fs_update_meta_page(sbi, ckpt, start_blk++);
1546 
1547 	for (i = 1; i < 1 + cp_payload_blks; i++)
1548 		f2fs_update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE,
1549 							start_blk++);
1550 
1551 	if (orphan_num) {
1552 		write_orphan_inodes(sbi, start_blk);
1553 		start_blk += orphan_blocks;
1554 	}
1555 
1556 	f2fs_write_data_summaries(sbi, start_blk);
1557 	start_blk += data_sum_blocks;
1558 
1559 	/* Record write statistics in the hot node summary */
1560 	kbytes_written = sbi->kbytes_written;
1561 	kbytes_written += (f2fs_get_sectors_written(sbi) -
1562 				sbi->sectors_written_start) >> 1;
1563 	seg_i->journal->info.kbytes_written = cpu_to_le64(kbytes_written);
1564 
1565 	if (__remain_node_summaries(cpc->reason)) {
1566 		f2fs_write_node_summaries(sbi, start_blk);
1567 		start_blk += NR_CURSEG_NODE_TYPE;
1568 	}
1569 
1570 	/* Here, we have one bio having CP pack except cp pack 2 page */
1571 	f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO);
1572 	/* Wait for all dirty meta pages to be submitted for IO */
1573 	f2fs_wait_on_all_pages(sbi, F2FS_DIRTY_META);
1574 
1575 	/* wait for previous submitted meta pages writeback */
1576 	f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
1577 
1578 	/* flush all device cache */
1579 	err = f2fs_flush_device_cache(sbi);
1580 	if (err)
1581 		return err;
1582 
1583 	/* barrier and flush checkpoint cp pack 2 page if it can */
1584 	commit_checkpoint(sbi, ckpt, start_blk);
1585 	f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
1586 
1587 	/*
1588 	 * invalidate intermediate page cache borrowed from meta inode which are
1589 	 * used for migration of encrypted, verity or compressed inode's blocks.
1590 	 */
1591 	if (f2fs_sb_has_encrypt(sbi) || f2fs_sb_has_verity(sbi) ||
1592 		f2fs_sb_has_compression(sbi))
1593 		f2fs_bug_on(sbi,
1594 			invalidate_inode_pages2_range(META_MAPPING(sbi),
1595 				MAIN_BLKADDR(sbi), MAX_BLKADDR(sbi) - 1));
1596 
1597 	f2fs_release_ino_entry(sbi, false);
1598 
1599 	f2fs_reset_fsync_node_info(sbi);
1600 
1601 	clear_sbi_flag(sbi, SBI_IS_DIRTY);
1602 	clear_sbi_flag(sbi, SBI_NEED_CP);
1603 	clear_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH);
1604 
1605 	spin_lock(&sbi->stat_lock);
1606 	sbi->unusable_block_count = 0;
1607 	spin_unlock(&sbi->stat_lock);
1608 
1609 	__set_cp_next_pack(sbi);
1610 
1611 	/*
1612 	 * redirty superblock if metadata like node page or inode cache is
1613 	 * updated during writing checkpoint.
1614 	 */
1615 	if (get_pages(sbi, F2FS_DIRTY_NODES) ||
1616 			get_pages(sbi, F2FS_DIRTY_IMETA))
1617 		set_sbi_flag(sbi, SBI_IS_DIRTY);
1618 
1619 	f2fs_bug_on(sbi, get_pages(sbi, F2FS_DIRTY_DENTS));
1620 
1621 	return unlikely(f2fs_cp_error(sbi)) ? -EIO : 0;
1622 }
1623 
f2fs_write_checkpoint(struct f2fs_sb_info * sbi,struct cp_control * cpc)1624 int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1625 {
1626 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1627 	unsigned long long ckpt_ver;
1628 	int err = 0;
1629 
1630 	if (f2fs_readonly(sbi->sb) || f2fs_hw_is_readonly(sbi))
1631 		return -EROFS;
1632 
1633 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
1634 		if (cpc->reason != CP_PAUSE)
1635 			return 0;
1636 		f2fs_warn(sbi, "Start checkpoint disabled!");
1637 	}
1638 	if (cpc->reason != CP_RESIZE)
1639 		f2fs_down_write(&sbi->cp_global_sem);
1640 
1641 	if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) &&
1642 		((cpc->reason & CP_FASTBOOT) || (cpc->reason & CP_SYNC) ||
1643 		((cpc->reason & CP_DISCARD) && !sbi->discard_blks)))
1644 		goto out;
1645 	if (unlikely(f2fs_cp_error(sbi))) {
1646 		err = -EIO;
1647 		goto out;
1648 	}
1649 
1650 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops");
1651 
1652 	err = block_operations(sbi);
1653 	if (err)
1654 		goto out;
1655 
1656 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops");
1657 
1658 	f2fs_flush_merged_writes(sbi);
1659 
1660 	/* this is the case of multiple fstrims without any changes */
1661 	if (cpc->reason & CP_DISCARD) {
1662 		if (!f2fs_exist_trim_candidates(sbi, cpc)) {
1663 			unblock_operations(sbi);
1664 			goto out;
1665 		}
1666 
1667 		if (NM_I(sbi)->nat_cnt[DIRTY_NAT] == 0 &&
1668 				SIT_I(sbi)->dirty_sentries == 0 &&
1669 				prefree_segments(sbi) == 0) {
1670 			f2fs_flush_sit_entries(sbi, cpc);
1671 			f2fs_clear_prefree_segments(sbi, cpc);
1672 			unblock_operations(sbi);
1673 			goto out;
1674 		}
1675 	}
1676 
1677 	/*
1678 	 * update checkpoint pack index
1679 	 * Increase the version number so that
1680 	 * SIT entries and seg summaries are written at correct place
1681 	 */
1682 	ckpt_ver = cur_cp_version(ckpt);
1683 	ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
1684 
1685 	/* write cached NAT/SIT entries to NAT/SIT area */
1686 	err = f2fs_flush_nat_entries(sbi, cpc);
1687 	if (err) {
1688 		f2fs_err(sbi, "f2fs_flush_nat_entries failed err:%d, stop checkpoint", err);
1689 		f2fs_bug_on(sbi, !f2fs_cp_error(sbi));
1690 		goto stop;
1691 	}
1692 
1693 	f2fs_flush_sit_entries(sbi, cpc);
1694 
1695 	/* save inmem log status */
1696 	f2fs_save_inmem_curseg(sbi);
1697 
1698 	err = do_checkpoint(sbi, cpc);
1699 	if (err) {
1700 		f2fs_err(sbi, "do_checkpoint failed err:%d, stop checkpoint", err);
1701 		f2fs_bug_on(sbi, !f2fs_cp_error(sbi));
1702 		f2fs_release_discard_addrs(sbi);
1703 	} else {
1704 		f2fs_clear_prefree_segments(sbi, cpc);
1705 	}
1706 
1707 	f2fs_restore_inmem_curseg(sbi);
1708 	f2fs_reinit_atgc_curseg(sbi);
1709 	stat_inc_cp_count(sbi);
1710 stop:
1711 	unblock_operations(sbi);
1712 
1713 	if (cpc->reason & CP_RECOVERY)
1714 		f2fs_notice(sbi, "checkpoint: version = %llx", ckpt_ver);
1715 
1716 	/* update CP_TIME to trigger checkpoint periodically */
1717 	f2fs_update_time(sbi, CP_TIME);
1718 	trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint");
1719 out:
1720 	if (cpc->reason != CP_RESIZE)
1721 		f2fs_up_write(&sbi->cp_global_sem);
1722 	return err;
1723 }
1724 
f2fs_init_ino_entry_info(struct f2fs_sb_info * sbi)1725 void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi)
1726 {
1727 	int i;
1728 
1729 	for (i = 0; i < MAX_INO_ENTRY; i++) {
1730 		struct inode_management *im = &sbi->im[i];
1731 
1732 		INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC);
1733 		spin_lock_init(&im->ino_lock);
1734 		INIT_LIST_HEAD(&im->ino_list);
1735 		im->ino_num = 0;
1736 	}
1737 
1738 	sbi->max_orphans = (BLKS_PER_SEG(sbi) - F2FS_CP_PACKS -
1739 			NR_CURSEG_PERSIST_TYPE - __cp_payload(sbi)) *
1740 			F2FS_ORPHANS_PER_BLOCK;
1741 }
1742 
f2fs_create_checkpoint_caches(void)1743 int __init f2fs_create_checkpoint_caches(void)
1744 {
1745 	ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry",
1746 			sizeof(struct ino_entry));
1747 	if (!ino_entry_slab)
1748 		return -ENOMEM;
1749 	f2fs_inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry",
1750 			sizeof(struct inode_entry));
1751 	if (!f2fs_inode_entry_slab) {
1752 		kmem_cache_destroy(ino_entry_slab);
1753 		return -ENOMEM;
1754 	}
1755 	return 0;
1756 }
1757 
f2fs_destroy_checkpoint_caches(void)1758 void f2fs_destroy_checkpoint_caches(void)
1759 {
1760 	kmem_cache_destroy(ino_entry_slab);
1761 	kmem_cache_destroy(f2fs_inode_entry_slab);
1762 }
1763 
__write_checkpoint_sync(struct f2fs_sb_info * sbi)1764 static int __write_checkpoint_sync(struct f2fs_sb_info *sbi)
1765 {
1766 	struct cp_control cpc = { .reason = CP_SYNC, };
1767 	int err;
1768 
1769 	f2fs_down_write(&sbi->gc_lock);
1770 	err = f2fs_write_checkpoint(sbi, &cpc);
1771 	f2fs_up_write(&sbi->gc_lock);
1772 
1773 	return err;
1774 }
1775 
__checkpoint_and_complete_reqs(struct f2fs_sb_info * sbi)1776 static void __checkpoint_and_complete_reqs(struct f2fs_sb_info *sbi)
1777 {
1778 	struct ckpt_req_control *cprc = &sbi->cprc_info;
1779 	struct ckpt_req *req, *next;
1780 	struct llist_node *dispatch_list;
1781 	u64 sum_diff = 0, diff, count = 0;
1782 	int ret;
1783 
1784 	dispatch_list = llist_del_all(&cprc->issue_list);
1785 	if (!dispatch_list)
1786 		return;
1787 	dispatch_list = llist_reverse_order(dispatch_list);
1788 
1789 	ret = __write_checkpoint_sync(sbi);
1790 	atomic_inc(&cprc->issued_ckpt);
1791 
1792 	llist_for_each_entry_safe(req, next, dispatch_list, llnode) {
1793 		diff = (u64)ktime_ms_delta(ktime_get(), req->queue_time);
1794 		req->ret = ret;
1795 		complete(&req->wait);
1796 
1797 		sum_diff += diff;
1798 		count++;
1799 	}
1800 	atomic_sub(count, &cprc->queued_ckpt);
1801 	atomic_add(count, &cprc->total_ckpt);
1802 
1803 	spin_lock(&cprc->stat_lock);
1804 	cprc->cur_time = (unsigned int)div64_u64(sum_diff, count);
1805 	if (cprc->peak_time < cprc->cur_time)
1806 		cprc->peak_time = cprc->cur_time;
1807 	spin_unlock(&cprc->stat_lock);
1808 }
1809 
issue_checkpoint_thread(void * data)1810 static int issue_checkpoint_thread(void *data)
1811 {
1812 	struct f2fs_sb_info *sbi = data;
1813 	struct ckpt_req_control *cprc = &sbi->cprc_info;
1814 	wait_queue_head_t *q = &cprc->ckpt_wait_queue;
1815 repeat:
1816 	if (kthread_should_stop())
1817 		return 0;
1818 
1819 	if (!llist_empty(&cprc->issue_list))
1820 		__checkpoint_and_complete_reqs(sbi);
1821 
1822 	wait_event_interruptible(*q,
1823 		kthread_should_stop() || !llist_empty(&cprc->issue_list));
1824 	goto repeat;
1825 }
1826 
flush_remained_ckpt_reqs(struct f2fs_sb_info * sbi,struct ckpt_req * wait_req)1827 static void flush_remained_ckpt_reqs(struct f2fs_sb_info *sbi,
1828 		struct ckpt_req *wait_req)
1829 {
1830 	struct ckpt_req_control *cprc = &sbi->cprc_info;
1831 
1832 	if (!llist_empty(&cprc->issue_list)) {
1833 		__checkpoint_and_complete_reqs(sbi);
1834 	} else {
1835 		/* already dispatched by issue_checkpoint_thread */
1836 		if (wait_req)
1837 			wait_for_completion(&wait_req->wait);
1838 	}
1839 }
1840 
init_ckpt_req(struct ckpt_req * req)1841 static void init_ckpt_req(struct ckpt_req *req)
1842 {
1843 	memset(req, 0, sizeof(struct ckpt_req));
1844 
1845 	init_completion(&req->wait);
1846 	req->queue_time = ktime_get();
1847 }
1848 
f2fs_issue_checkpoint(struct f2fs_sb_info * sbi)1849 int f2fs_issue_checkpoint(struct f2fs_sb_info *sbi)
1850 {
1851 	struct ckpt_req_control *cprc = &sbi->cprc_info;
1852 	struct ckpt_req req;
1853 	struct cp_control cpc;
1854 
1855 	cpc.reason = __get_cp_reason(sbi);
1856 	if (!test_opt(sbi, MERGE_CHECKPOINT) || cpc.reason != CP_SYNC ||
1857 		sbi->umount_lock_holder == current) {
1858 		int ret;
1859 
1860 		f2fs_down_write(&sbi->gc_lock);
1861 		ret = f2fs_write_checkpoint(sbi, &cpc);
1862 		f2fs_up_write(&sbi->gc_lock);
1863 
1864 		return ret;
1865 	}
1866 
1867 	if (!cprc->f2fs_issue_ckpt)
1868 		return __write_checkpoint_sync(sbi);
1869 
1870 	init_ckpt_req(&req);
1871 
1872 	llist_add(&req.llnode, &cprc->issue_list);
1873 	atomic_inc(&cprc->queued_ckpt);
1874 
1875 	/*
1876 	 * update issue_list before we wake up issue_checkpoint thread,
1877 	 * this smp_mb() pairs with another barrier in ___wait_event(),
1878 	 * see more details in comments of waitqueue_active().
1879 	 */
1880 	smp_mb();
1881 
1882 	if (waitqueue_active(&cprc->ckpt_wait_queue))
1883 		wake_up(&cprc->ckpt_wait_queue);
1884 
1885 	if (cprc->f2fs_issue_ckpt)
1886 		wait_for_completion(&req.wait);
1887 	else
1888 		flush_remained_ckpt_reqs(sbi, &req);
1889 
1890 	return req.ret;
1891 }
1892 
f2fs_start_ckpt_thread(struct f2fs_sb_info * sbi)1893 int f2fs_start_ckpt_thread(struct f2fs_sb_info *sbi)
1894 {
1895 	dev_t dev = sbi->sb->s_bdev->bd_dev;
1896 	struct ckpt_req_control *cprc = &sbi->cprc_info;
1897 
1898 	if (cprc->f2fs_issue_ckpt)
1899 		return 0;
1900 
1901 	cprc->f2fs_issue_ckpt = kthread_run(issue_checkpoint_thread, sbi,
1902 			"f2fs_ckpt-%u:%u", MAJOR(dev), MINOR(dev));
1903 	if (IS_ERR(cprc->f2fs_issue_ckpt)) {
1904 		int err = PTR_ERR(cprc->f2fs_issue_ckpt);
1905 
1906 		cprc->f2fs_issue_ckpt = NULL;
1907 		return err;
1908 	}
1909 
1910 	set_task_ioprio(cprc->f2fs_issue_ckpt, cprc->ckpt_thread_ioprio);
1911 
1912 	return 0;
1913 }
1914 
f2fs_stop_ckpt_thread(struct f2fs_sb_info * sbi)1915 void f2fs_stop_ckpt_thread(struct f2fs_sb_info *sbi)
1916 {
1917 	struct ckpt_req_control *cprc = &sbi->cprc_info;
1918 	struct task_struct *ckpt_task;
1919 
1920 	if (!cprc->f2fs_issue_ckpt)
1921 		return;
1922 
1923 	ckpt_task = cprc->f2fs_issue_ckpt;
1924 	cprc->f2fs_issue_ckpt = NULL;
1925 	kthread_stop(ckpt_task);
1926 
1927 	f2fs_flush_ckpt_thread(sbi);
1928 }
1929 
f2fs_flush_ckpt_thread(struct f2fs_sb_info * sbi)1930 void f2fs_flush_ckpt_thread(struct f2fs_sb_info *sbi)
1931 {
1932 	struct ckpt_req_control *cprc = &sbi->cprc_info;
1933 
1934 	flush_remained_ckpt_reqs(sbi, NULL);
1935 
1936 	/* Let's wait for the previous dispatched checkpoint. */
1937 	while (atomic_read(&cprc->queued_ckpt))
1938 		io_schedule_timeout(DEFAULT_IO_TIMEOUT);
1939 }
1940 
f2fs_init_ckpt_req_control(struct f2fs_sb_info * sbi)1941 void f2fs_init_ckpt_req_control(struct f2fs_sb_info *sbi)
1942 {
1943 	struct ckpt_req_control *cprc = &sbi->cprc_info;
1944 
1945 	atomic_set(&cprc->issued_ckpt, 0);
1946 	atomic_set(&cprc->total_ckpt, 0);
1947 	atomic_set(&cprc->queued_ckpt, 0);
1948 	cprc->ckpt_thread_ioprio = DEFAULT_CHECKPOINT_IOPRIO;
1949 	init_waitqueue_head(&cprc->ckpt_wait_queue);
1950 	init_llist_head(&cprc->issue_list);
1951 	spin_lock_init(&cprc->stat_lock);
1952 }
1953