1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
9 #include <linux/f2fs_fs.h>
15 * Roll forward recovery scenarios.
17 * [Term] F: fsync_mark, D: dentry_mark
19 * 1. inode(x) | CP | inode(x) | dnode(F)
20 * -> Update the latest inode(x).
22 * 2. inode(x) | CP | inode(F) | dnode(F)
25 * 3. inode(x) | CP | dnode(F) | inode(x)
26 * -> Recover to the latest dnode(F), and drop the last inode(x)
28 * 4. inode(x) | CP | dnode(F) | inode(F)
31 * 5. CP | inode(x) | dnode(F)
32 * -> The inode(DF) was missing. Should drop this dnode(F).
34 * 6. CP | inode(DF) | dnode(F)
37 * 7. CP | dnode(F) | inode(DF)
38 * -> If f2fs_iget fails, then goto next to find inode(DF).
40 * 8. CP | dnode(F) | inode(x)
41 * -> If f2fs_iget fails, then goto next to find inode(DF).
42 * But it will fail due to no inode(DF).
45 static struct kmem_cache
*fsync_entry_slab
;
47 bool f2fs_space_for_roll_forward(struct f2fs_sb_info
*sbi
)
49 s64 nalloc
= percpu_counter_sum_positive(&sbi
->alloc_valid_block_count
);
51 if (sbi
->last_valid_block_count
+ nalloc
> sbi
->user_block_count
)
56 static struct fsync_inode_entry
*get_fsync_inode(struct list_head
*head
,
59 struct fsync_inode_entry
*entry
;
61 list_for_each_entry(entry
, head
, list
)
62 if (entry
->inode
->i_ino
== ino
)
68 static struct fsync_inode_entry
*add_fsync_inode(struct f2fs_sb_info
*sbi
,
69 struct list_head
*head
, nid_t ino
, bool quota_inode
)
72 struct fsync_inode_entry
*entry
;
75 inode
= f2fs_iget_retry(sbi
->sb
, ino
);
77 return ERR_CAST(inode
);
79 err
= dquot_initialize(inode
);
84 err
= dquot_alloc_inode(inode
);
89 entry
= f2fs_kmem_cache_alloc(fsync_entry_slab
, GFP_F2FS_ZERO
);
91 list_add_tail(&entry
->list
, head
);
99 static void del_fsync_inode(struct fsync_inode_entry
*entry
, int drop
)
102 /* inode should not be recovered, drop it */
103 f2fs_inode_synced(entry
->inode
);
106 list_del(&entry
->list
);
107 kmem_cache_free(fsync_entry_slab
, entry
);
110 static int recover_dentry(struct inode
*inode
, struct page
*ipage
,
111 struct list_head
*dir_list
)
113 struct f2fs_inode
*raw_inode
= F2FS_INODE(ipage
);
114 nid_t pino
= le32_to_cpu(raw_inode
->i_pino
);
115 struct f2fs_dir_entry
*de
;
116 struct fscrypt_name fname
;
118 struct inode
*dir
, *einode
;
119 struct fsync_inode_entry
*entry
;
123 entry
= get_fsync_inode(dir_list
, pino
);
125 entry
= add_fsync_inode(F2FS_I_SB(inode
), dir_list
,
128 dir
= ERR_CAST(entry
);
129 err
= PTR_ERR(entry
);
136 memset(&fname
, 0, sizeof(struct fscrypt_name
));
137 fname
.disk_name
.len
= le32_to_cpu(raw_inode
->i_namelen
);
138 fname
.disk_name
.name
= raw_inode
->i_name
;
140 if (unlikely(fname
.disk_name
.len
> F2FS_NAME_LEN
)) {
146 de
= __f2fs_find_entry(dir
, &fname
, &page
);
147 if (de
&& inode
->i_ino
== le32_to_cpu(de
->ino
))
151 einode
= f2fs_iget_retry(inode
->i_sb
, le32_to_cpu(de
->ino
));
152 if (IS_ERR(einode
)) {
154 err
= PTR_ERR(einode
);
160 err
= dquot_initialize(einode
);
166 err
= f2fs_acquire_orphan_inode(F2FS_I_SB(inode
));
171 f2fs_delete_entry(de
, page
, dir
, einode
);
174 } else if (IS_ERR(page
)) {
177 err
= f2fs_add_dentry(dir
, &fname
, inode
,
178 inode
->i_ino
, inode
->i_mode
);
185 f2fs_put_page(page
, 0);
187 if (file_enc_name(inode
))
188 name
= "<encrypted>";
190 name
= raw_inode
->i_name
;
191 f2fs_notice(F2FS_I_SB(inode
), "%s: ino = %x, name = %s, dir = %lx, err = %d",
192 __func__
, ino_of_node(ipage
), name
,
193 IS_ERR(dir
) ? 0 : dir
->i_ino
, err
);
197 static int recover_quota_data(struct inode
*inode
, struct page
*page
)
199 struct f2fs_inode
*raw
= F2FS_INODE(page
);
201 uid_t i_uid
= le32_to_cpu(raw
->i_uid
);
202 gid_t i_gid
= le32_to_cpu(raw
->i_gid
);
205 memset(&attr
, 0, sizeof(attr
));
207 attr
.ia_uid
= make_kuid(inode
->i_sb
->s_user_ns
, i_uid
);
208 attr
.ia_gid
= make_kgid(inode
->i_sb
->s_user_ns
, i_gid
);
210 if (!uid_eq(attr
.ia_uid
, inode
->i_uid
))
211 attr
.ia_valid
|= ATTR_UID
;
212 if (!gid_eq(attr
.ia_gid
, inode
->i_gid
))
213 attr
.ia_valid
|= ATTR_GID
;
218 err
= dquot_transfer(inode
, &attr
);
220 set_sbi_flag(F2FS_I_SB(inode
), SBI_QUOTA_NEED_REPAIR
);
224 static void recover_inline_flags(struct inode
*inode
, struct f2fs_inode
*ri
)
226 if (ri
->i_inline
& F2FS_PIN_FILE
)
227 set_inode_flag(inode
, FI_PIN_FILE
);
229 clear_inode_flag(inode
, FI_PIN_FILE
);
230 if (ri
->i_inline
& F2FS_DATA_EXIST
)
231 set_inode_flag(inode
, FI_DATA_EXIST
);
233 clear_inode_flag(inode
, FI_DATA_EXIST
);
236 static int recover_inode(struct inode
*inode
, struct page
*page
)
238 struct f2fs_inode
*raw
= F2FS_INODE(page
);
242 inode
->i_mode
= le16_to_cpu(raw
->i_mode
);
244 err
= recover_quota_data(inode
, page
);
248 i_uid_write(inode
, le32_to_cpu(raw
->i_uid
));
249 i_gid_write(inode
, le32_to_cpu(raw
->i_gid
));
251 if (raw
->i_inline
& F2FS_EXTRA_ATTR
) {
252 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode
)) &&
253 F2FS_FITS_IN_INODE(raw
, le16_to_cpu(raw
->i_extra_isize
),
258 i_projid
= (projid_t
)le32_to_cpu(raw
->i_projid
);
259 kprojid
= make_kprojid(&init_user_ns
, i_projid
);
261 if (!projid_eq(kprojid
, F2FS_I(inode
)->i_projid
)) {
262 err
= f2fs_transfer_project_quota(inode
,
266 F2FS_I(inode
)->i_projid
= kprojid
;
271 f2fs_i_size_write(inode
, le64_to_cpu(raw
->i_size
));
272 inode
->i_atime
.tv_sec
= le64_to_cpu(raw
->i_atime
);
273 inode
->i_ctime
.tv_sec
= le64_to_cpu(raw
->i_ctime
);
274 inode
->i_mtime
.tv_sec
= le64_to_cpu(raw
->i_mtime
);
275 inode
->i_atime
.tv_nsec
= le32_to_cpu(raw
->i_atime_nsec
);
276 inode
->i_ctime
.tv_nsec
= le32_to_cpu(raw
->i_ctime_nsec
);
277 inode
->i_mtime
.tv_nsec
= le32_to_cpu(raw
->i_mtime_nsec
);
279 F2FS_I(inode
)->i_advise
= raw
->i_advise
;
280 F2FS_I(inode
)->i_flags
= le32_to_cpu(raw
->i_flags
);
281 f2fs_set_inode_flags(inode
);
282 F2FS_I(inode
)->i_gc_failures
[GC_FAILURE_PIN
] =
283 le16_to_cpu(raw
->i_gc_failures
);
285 recover_inline_flags(inode
, raw
);
287 f2fs_mark_inode_dirty_sync(inode
, true);
289 if (file_enc_name(inode
))
290 name
= "<encrypted>";
292 name
= F2FS_INODE(page
)->i_name
;
294 f2fs_notice(F2FS_I_SB(inode
), "recover_inode: ino = %x, name = %s, inline = %x",
295 ino_of_node(page
), name
, raw
->i_inline
);
299 static int find_fsync_dnodes(struct f2fs_sb_info
*sbi
, struct list_head
*head
,
302 struct curseg_info
*curseg
;
303 struct page
*page
= NULL
;
305 unsigned int loop_cnt
= 0;
306 unsigned int free_blocks
= MAIN_SEGS(sbi
) * sbi
->blocks_per_seg
-
307 valid_user_blocks(sbi
);
310 /* get node pages in the current segment */
311 curseg
= CURSEG_I(sbi
, CURSEG_WARM_NODE
);
312 blkaddr
= NEXT_FREE_BLKADDR(sbi
, curseg
);
315 struct fsync_inode_entry
*entry
;
317 if (!f2fs_is_valid_blkaddr(sbi
, blkaddr
, META_POR
))
320 page
= f2fs_get_tmp_page(sbi
, blkaddr
);
326 if (!is_recoverable_dnode(page
)) {
327 f2fs_put_page(page
, 1);
331 if (!is_fsync_dnode(page
))
334 entry
= get_fsync_inode(head
, ino_of_node(page
));
336 bool quota_inode
= false;
339 IS_INODE(page
) && is_dent_dnode(page
)) {
340 err
= f2fs_recover_inode_page(sbi
, page
);
342 f2fs_put_page(page
, 1);
349 * CP | dnode(F) | inode(DF)
350 * For this case, we should not give up now.
352 entry
= add_fsync_inode(sbi
, head
, ino_of_node(page
),
355 err
= PTR_ERR(entry
);
356 if (err
== -ENOENT
) {
360 f2fs_put_page(page
, 1);
364 entry
->blkaddr
= blkaddr
;
366 if (IS_INODE(page
) && is_dent_dnode(page
))
367 entry
->last_dentry
= blkaddr
;
369 /* sanity check in order to detect looped node chain */
370 if (++loop_cnt
>= free_blocks
||
371 blkaddr
== next_blkaddr_of_node(page
)) {
372 f2fs_notice(sbi
, "%s: detect looped node chain, blkaddr:%u, next:%u",
374 next_blkaddr_of_node(page
));
375 f2fs_put_page(page
, 1);
380 /* check next segment */
381 blkaddr
= next_blkaddr_of_node(page
);
382 f2fs_put_page(page
, 1);
384 f2fs_ra_meta_pages_cond(sbi
, blkaddr
);
389 static void destroy_fsync_dnodes(struct list_head
*head
, int drop
)
391 struct fsync_inode_entry
*entry
, *tmp
;
393 list_for_each_entry_safe(entry
, tmp
, head
, list
)
394 del_fsync_inode(entry
, drop
);
397 static int check_index_in_prev_nodes(struct f2fs_sb_info
*sbi
,
398 block_t blkaddr
, struct dnode_of_data
*dn
)
400 struct seg_entry
*sentry
;
401 unsigned int segno
= GET_SEGNO(sbi
, blkaddr
);
402 unsigned short blkoff
= GET_BLKOFF_FROM_SEG0(sbi
, blkaddr
);
403 struct f2fs_summary_block
*sum_node
;
404 struct f2fs_summary sum
;
405 struct page
*sum_page
, *node_page
;
406 struct dnode_of_data tdn
= *dn
;
413 sentry
= get_seg_entry(sbi
, segno
);
414 if (!f2fs_test_bit(blkoff
, sentry
->cur_valid_map
))
417 /* Get the previous summary */
418 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
419 struct curseg_info
*curseg
= CURSEG_I(sbi
, i
);
420 if (curseg
->segno
== segno
) {
421 sum
= curseg
->sum_blk
->entries
[blkoff
];
426 sum_page
= f2fs_get_sum_page(sbi
, segno
);
427 if (IS_ERR(sum_page
))
428 return PTR_ERR(sum_page
);
429 sum_node
= (struct f2fs_summary_block
*)page_address(sum_page
);
430 sum
= sum_node
->entries
[blkoff
];
431 f2fs_put_page(sum_page
, 1);
433 /* Use the locked dnode page and inode */
434 nid
= le32_to_cpu(sum
.nid
);
435 if (dn
->inode
->i_ino
== nid
) {
437 if (!dn
->inode_page_locked
)
438 lock_page(dn
->inode_page
);
439 tdn
.node_page
= dn
->inode_page
;
440 tdn
.ofs_in_node
= le16_to_cpu(sum
.ofs_in_node
);
442 } else if (dn
->nid
== nid
) {
443 tdn
.ofs_in_node
= le16_to_cpu(sum
.ofs_in_node
);
447 /* Get the node page */
448 node_page
= f2fs_get_node_page(sbi
, nid
);
449 if (IS_ERR(node_page
))
450 return PTR_ERR(node_page
);
452 offset
= ofs_of_node(node_page
);
453 ino
= ino_of_node(node_page
);
454 f2fs_put_page(node_page
, 1);
456 if (ino
!= dn
->inode
->i_ino
) {
459 /* Deallocate previous index in the node page */
460 inode
= f2fs_iget_retry(sbi
->sb
, ino
);
462 return PTR_ERR(inode
);
464 ret
= dquot_initialize(inode
);
473 bidx
= f2fs_start_bidx_of_node(offset
, inode
) +
474 le16_to_cpu(sum
.ofs_in_node
);
477 * if inode page is locked, unlock temporarily, but its reference
480 if (ino
== dn
->inode
->i_ino
&& dn
->inode_page_locked
)
481 unlock_page(dn
->inode_page
);
483 set_new_dnode(&tdn
, inode
, NULL
, NULL
, 0);
484 if (f2fs_get_dnode_of_data(&tdn
, bidx
, LOOKUP_NODE
))
487 if (tdn
.data_blkaddr
== blkaddr
)
488 f2fs_truncate_data_blocks_range(&tdn
, 1);
490 f2fs_put_dnode(&tdn
);
492 if (ino
!= dn
->inode
->i_ino
)
494 else if (dn
->inode_page_locked
)
495 lock_page(dn
->inode_page
);
499 if (datablock_addr(tdn
.inode
, tdn
.node_page
,
500 tdn
.ofs_in_node
) == blkaddr
)
501 f2fs_truncate_data_blocks_range(&tdn
, 1);
502 if (dn
->inode
->i_ino
== nid
&& !dn
->inode_page_locked
)
503 unlock_page(dn
->inode_page
);
507 static int do_recover_data(struct f2fs_sb_info
*sbi
, struct inode
*inode
,
510 struct dnode_of_data dn
;
512 unsigned int start
, end
;
513 int err
= 0, recovered
= 0;
515 /* step 1: recover xattr */
516 if (IS_INODE(page
)) {
517 f2fs_recover_inline_xattr(inode
, page
);
518 } else if (f2fs_has_xattr_block(ofs_of_node(page
))) {
519 err
= f2fs_recover_xattr_data(inode
, page
);
525 /* step 2: recover inline data */
526 if (f2fs_recover_inline_data(inode
, page
))
529 /* step 3: recover data indices */
530 start
= f2fs_start_bidx_of_node(ofs_of_node(page
), inode
);
531 end
= start
+ ADDRS_PER_PAGE(page
, inode
);
533 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
535 err
= f2fs_get_dnode_of_data(&dn
, start
, ALLOC_NODE
);
537 if (err
== -ENOMEM
) {
538 congestion_wait(BLK_RW_ASYNC
, HZ
/50);
544 f2fs_wait_on_page_writeback(dn
.node_page
, NODE
, true, true);
546 err
= f2fs_get_node_info(sbi
, dn
.nid
, &ni
);
550 f2fs_bug_on(sbi
, ni
.ino
!= ino_of_node(page
));
552 if (ofs_of_node(dn
.node_page
) != ofs_of_node(page
)) {
553 f2fs_warn(sbi
, "Inconsistent ofs_of_node, ino:%lu, ofs:%u, %u",
554 inode
->i_ino
, ofs_of_node(dn
.node_page
),
560 for (; start
< end
; start
++, dn
.ofs_in_node
++) {
563 src
= datablock_addr(dn
.inode
, dn
.node_page
, dn
.ofs_in_node
);
564 dest
= datablock_addr(dn
.inode
, page
, dn
.ofs_in_node
);
566 if (__is_valid_data_blkaddr(src
) &&
567 !f2fs_is_valid_blkaddr(sbi
, src
, META_POR
)) {
572 if (__is_valid_data_blkaddr(dest
) &&
573 !f2fs_is_valid_blkaddr(sbi
, dest
, META_POR
)) {
578 /* skip recovering if dest is the same as src */
582 /* dest is invalid, just invalidate src block */
583 if (dest
== NULL_ADDR
) {
584 f2fs_truncate_data_blocks_range(&dn
, 1);
588 if (!file_keep_isize(inode
) &&
589 (i_size_read(inode
) <= ((loff_t
)start
<< PAGE_SHIFT
)))
590 f2fs_i_size_write(inode
,
591 (loff_t
)(start
+ 1) << PAGE_SHIFT
);
594 * dest is reserved block, invalidate src block
595 * and then reserve one new block in dnode page.
597 if (dest
== NEW_ADDR
) {
598 f2fs_truncate_data_blocks_range(&dn
, 1);
599 f2fs_reserve_new_block(&dn
);
603 /* dest is valid block, try to recover from src to dest */
604 if (f2fs_is_valid_blkaddr(sbi
, dest
, META_POR
)) {
606 if (src
== NULL_ADDR
) {
607 err
= f2fs_reserve_new_block(&dn
);
609 IS_ENABLED(CONFIG_F2FS_FAULT_INJECTION
))
610 err
= f2fs_reserve_new_block(&dn
);
611 /* We should not get -ENOSPC */
612 f2fs_bug_on(sbi
, err
);
617 /* Check the previous node page having this index */
618 err
= check_index_in_prev_nodes(sbi
, dest
, &dn
);
620 if (err
== -ENOMEM
) {
621 congestion_wait(BLK_RW_ASYNC
, HZ
/50);
627 /* write dummy data page */
628 f2fs_replace_block(sbi
, &dn
, src
, dest
,
629 ni
.version
, false, false);
634 copy_node_footer(dn
.node_page
, page
);
635 fill_node_footer(dn
.node_page
, dn
.nid
, ni
.ino
,
636 ofs_of_node(page
), false);
637 set_page_dirty(dn
.node_page
);
641 f2fs_notice(sbi
, "recover_data: ino = %lx (i_size: %s) recovered = %d, err = %d",
642 inode
->i_ino
, file_keep_isize(inode
) ? "keep" : "recover",
647 static int recover_data(struct f2fs_sb_info
*sbi
, struct list_head
*inode_list
,
648 struct list_head
*tmp_inode_list
, struct list_head
*dir_list
)
650 struct curseg_info
*curseg
;
651 struct page
*page
= NULL
;
655 /* get node pages in the current segment */
656 curseg
= CURSEG_I(sbi
, CURSEG_WARM_NODE
);
657 blkaddr
= NEXT_FREE_BLKADDR(sbi
, curseg
);
660 struct fsync_inode_entry
*entry
;
662 if (!f2fs_is_valid_blkaddr(sbi
, blkaddr
, META_POR
))
665 f2fs_ra_meta_pages_cond(sbi
, blkaddr
);
667 page
= f2fs_get_tmp_page(sbi
, blkaddr
);
673 if (!is_recoverable_dnode(page
)) {
674 f2fs_put_page(page
, 1);
678 entry
= get_fsync_inode(inode_list
, ino_of_node(page
));
682 * inode(x) | CP | inode(x) | dnode(F)
683 * In this case, we can lose the latest inode(x).
684 * So, call recover_inode for the inode update.
686 if (IS_INODE(page
)) {
687 err
= recover_inode(entry
->inode
, page
);
689 f2fs_put_page(page
, 1);
693 if (entry
->last_dentry
== blkaddr
) {
694 err
= recover_dentry(entry
->inode
, page
, dir_list
);
696 f2fs_put_page(page
, 1);
700 err
= do_recover_data(sbi
, entry
->inode
, page
);
702 f2fs_put_page(page
, 1);
706 if (entry
->blkaddr
== blkaddr
)
707 list_move_tail(&entry
->list
, tmp_inode_list
);
709 /* check next segment */
710 blkaddr
= next_blkaddr_of_node(page
);
711 f2fs_put_page(page
, 1);
714 f2fs_allocate_new_segments(sbi
);
718 int f2fs_recover_fsync_data(struct f2fs_sb_info
*sbi
, bool check_only
)
720 struct list_head inode_list
, tmp_inode_list
;
721 struct list_head dir_list
;
724 unsigned long s_flags
= sbi
->sb
->s_flags
;
725 bool need_writecp
= false;
730 if (s_flags
& SB_RDONLY
) {
731 f2fs_info(sbi
, "recover fsync data on readonly fs");
732 sbi
->sb
->s_flags
&= ~SB_RDONLY
;
736 /* Needed for iput() to work correctly and not trash data */
737 sbi
->sb
->s_flags
|= SB_ACTIVE
;
738 /* Turn on quotas so that they are updated correctly */
739 quota_enabled
= f2fs_enable_quota_files(sbi
, s_flags
& SB_RDONLY
);
742 fsync_entry_slab
= f2fs_kmem_cache_create("f2fs_fsync_inode_entry",
743 sizeof(struct fsync_inode_entry
));
744 if (!fsync_entry_slab
) {
749 INIT_LIST_HEAD(&inode_list
);
750 INIT_LIST_HEAD(&tmp_inode_list
);
751 INIT_LIST_HEAD(&dir_list
);
753 /* prevent checkpoint */
754 mutex_lock(&sbi
->cp_mutex
);
756 /* step #1: find fsynced inode numbers */
757 err
= find_fsync_dnodes(sbi
, &inode_list
, check_only
);
758 if (err
|| list_empty(&inode_list
))
768 /* step #2: recover data */
769 err
= recover_data(sbi
, &inode_list
, &tmp_inode_list
, &dir_list
);
771 f2fs_bug_on(sbi
, !list_empty(&inode_list
));
773 /* restore s_flags to let iput() trash data */
774 sbi
->sb
->s_flags
= s_flags
;
777 destroy_fsync_dnodes(&inode_list
, err
);
778 destroy_fsync_dnodes(&tmp_inode_list
, err
);
780 /* truncate meta pages to be used by the recovery */
781 truncate_inode_pages_range(META_MAPPING(sbi
),
782 (loff_t
)MAIN_BLKADDR(sbi
) << PAGE_SHIFT
, -1);
785 truncate_inode_pages_final(NODE_MAPPING(sbi
));
786 truncate_inode_pages_final(META_MAPPING(sbi
));
788 clear_sbi_flag(sbi
, SBI_POR_DOING
);
790 mutex_unlock(&sbi
->cp_mutex
);
792 /* let's drop all the directory inodes for clean checkpoint */
793 destroy_fsync_dnodes(&dir_list
, err
);
796 set_sbi_flag(sbi
, SBI_IS_RECOVERED
);
799 struct cp_control cpc
= {
800 .reason
= CP_RECOVERY
,
802 err
= f2fs_write_checkpoint(sbi
, &cpc
);
806 kmem_cache_destroy(fsync_entry_slab
);
809 /* Turn quotas off */
811 f2fs_quota_off_umount(sbi
->sb
);
813 sbi
->sb
->s_flags
= s_flags
; /* Restore SB_RDONLY status */
815 return ret
? ret
: err
;