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