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