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