^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * fs/f2fs/recovery.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2012 Samsung Electronics Co., Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * http://www.samsung.com/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/f2fs_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "f2fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "node.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "segment.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * Roll forward recovery scenarios.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * [Term] F: fsync_mark, D: dentry_mark
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * 1. inode(x) | CP | inode(x) | dnode(F)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * -> Update the latest inode(x).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * 2. inode(x) | CP | inode(F) | dnode(F)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * -> No problem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * 3. inode(x) | CP | dnode(F) | inode(x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * -> Recover to the latest dnode(F), and drop the last inode(x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * 4. inode(x) | CP | dnode(F) | inode(F)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * -> No problem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * 5. CP | inode(x) | dnode(F)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * -> The inode(DF) was missing. Should drop this dnode(F).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * 6. CP | inode(DF) | dnode(F)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * -> No problem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * 7. CP | dnode(F) | inode(DF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * -> If f2fs_iget fails, then goto next to find inode(DF).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * 8. CP | dnode(F) | inode(x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * -> If f2fs_iget fails, then goto next to find inode(DF).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * But it will fail due to no inode(DF).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static struct kmem_cache *fsync_entry_slab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #ifdef CONFIG_UNICODE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) extern struct kmem_cache *f2fs_cf_name_slab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) bool f2fs_space_for_roll_forward(struct f2fs_sb_info *sbi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) s64 nalloc = percpu_counter_sum_positive(&sbi->alloc_valid_block_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (sbi->last_valid_block_count + nalloc > sbi->user_block_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) static struct fsync_inode_entry *get_fsync_inode(struct list_head *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) nid_t ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct fsync_inode_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) list_for_each_entry(entry, head, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (entry->inode->i_ino == ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) static struct fsync_inode_entry *add_fsync_inode(struct f2fs_sb_info *sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct list_head *head, nid_t ino, bool quota_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) struct fsync_inode_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) inode = f2fs_iget_retry(sbi->sb, ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (IS_ERR(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return ERR_CAST(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) err = dquot_initialize(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (quota_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) err = dquot_alloc_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) entry = f2fs_kmem_cache_alloc(fsync_entry_slab, GFP_F2FS_ZERO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) entry->inode = inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) list_add_tail(&entry->list, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static void del_fsync_inode(struct fsync_inode_entry *entry, int drop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (drop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /* inode should not be recovered, drop it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) f2fs_inode_synced(entry->inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) iput(entry->inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) list_del(&entry->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) kmem_cache_free(fsync_entry_slab, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static int init_recovered_filename(const struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) struct f2fs_inode *raw_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) struct f2fs_filename *fname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct qstr *usr_fname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) memset(fname, 0, sizeof(*fname));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) fname->disk_name.len = le32_to_cpu(raw_inode->i_namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) fname->disk_name.name = raw_inode->i_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (WARN_ON(fname->disk_name.len > F2FS_NAME_LEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (!IS_ENCRYPTED(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) usr_fname->name = fname->disk_name.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) usr_fname->len = fname->disk_name.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) fname->usr_fname = usr_fname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) /* Compute the hash of the filename */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (IS_ENCRYPTED(dir) && IS_CASEFOLDED(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * In this case the hash isn't computable without the key, so it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * was saved on-disk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (fname->disk_name.len + sizeof(f2fs_hash_t) > F2FS_NAME_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) fname->hash = get_unaligned((f2fs_hash_t *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) &raw_inode->i_name[fname->disk_name.len]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) } else if (IS_CASEFOLDED(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) err = f2fs_init_casefolded_name(dir, fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) f2fs_hash_filename(dir, fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) #ifdef CONFIG_UNICODE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) /* Case-sensitive match is fine for recovery */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) kmem_cache_free(f2fs_cf_name_slab, fname->cf_name.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) fname->cf_name.name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) f2fs_hash_filename(dir, fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) static int recover_dentry(struct inode *inode, struct page *ipage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) struct list_head *dir_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) struct f2fs_inode *raw_inode = F2FS_INODE(ipage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) nid_t pino = le32_to_cpu(raw_inode->i_pino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) struct f2fs_dir_entry *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct f2fs_filename fname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) struct qstr usr_fname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) struct inode *dir, *einode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) struct fsync_inode_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) entry = get_fsync_inode(dir_list, pino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (!entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) entry = add_fsync_inode(F2FS_I_SB(inode), dir_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) pino, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (IS_ERR(entry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) dir = ERR_CAST(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) err = PTR_ERR(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) dir = entry->inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) err = init_recovered_filename(dir, raw_inode, &fname, &usr_fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) de = __f2fs_find_entry(dir, &fname, &page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) if (de && inode->i_ino == le32_to_cpu(de->ino))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (de) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) einode = f2fs_iget_retry(inode->i_sb, le32_to_cpu(de->ino));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) if (IS_ERR(einode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) err = PTR_ERR(einode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (err == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) err = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) err = dquot_initialize(einode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) iput(einode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) err = f2fs_acquire_orphan_inode(F2FS_I_SB(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) iput(einode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) f2fs_delete_entry(de, page, dir, einode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) iput(einode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) } else if (IS_ERR(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) err = PTR_ERR(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) err = f2fs_add_dentry(dir, &fname, inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) inode->i_ino, inode->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (err == -ENOMEM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) out_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) f2fs_put_page(page, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) if (file_enc_name(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) name = "<encrypted>";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) name = raw_inode->i_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) f2fs_notice(F2FS_I_SB(inode), "%s: ino = %x, name = %s, dir = %lx, err = %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) __func__, ino_of_node(ipage), name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) IS_ERR(dir) ? 0 : dir->i_ino, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) static int recover_quota_data(struct inode *inode, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) struct f2fs_inode *raw = F2FS_INODE(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) struct iattr attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) uid_t i_uid = le32_to_cpu(raw->i_uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) gid_t i_gid = le32_to_cpu(raw->i_gid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) memset(&attr, 0, sizeof(attr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) attr.ia_uid = make_kuid(inode->i_sb->s_user_ns, i_uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) attr.ia_gid = make_kgid(inode->i_sb->s_user_ns, i_gid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) if (!uid_eq(attr.ia_uid, inode->i_uid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) attr.ia_valid |= ATTR_UID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (!gid_eq(attr.ia_gid, inode->i_gid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) attr.ia_valid |= ATTR_GID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) if (!attr.ia_valid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) err = dquot_transfer(inode, &attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) set_sbi_flag(F2FS_I_SB(inode), SBI_QUOTA_NEED_REPAIR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) static void recover_inline_flags(struct inode *inode, struct f2fs_inode *ri)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (ri->i_inline & F2FS_PIN_FILE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) set_inode_flag(inode, FI_PIN_FILE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) clear_inode_flag(inode, FI_PIN_FILE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (ri->i_inline & F2FS_DATA_EXIST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) set_inode_flag(inode, FI_DATA_EXIST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) clear_inode_flag(inode, FI_DATA_EXIST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) static int recover_inode(struct inode *inode, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) struct f2fs_inode *raw = F2FS_INODE(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) inode->i_mode = le16_to_cpu(raw->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) err = recover_quota_data(inode, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) i_uid_write(inode, le32_to_cpu(raw->i_uid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) i_gid_write(inode, le32_to_cpu(raw->i_gid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) if (raw->i_inline & F2FS_EXTRA_ATTR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) F2FS_FITS_IN_INODE(raw, le16_to_cpu(raw->i_extra_isize),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) i_projid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) projid_t i_projid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) kprojid_t kprojid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) i_projid = (projid_t)le32_to_cpu(raw->i_projid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) kprojid = make_kprojid(&init_user_ns, i_projid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) if (!projid_eq(kprojid, F2FS_I(inode)->i_projid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) err = f2fs_transfer_project_quota(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) kprojid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) F2FS_I(inode)->i_projid = kprojid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) f2fs_i_size_write(inode, le64_to_cpu(raw->i_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) inode->i_atime.tv_sec = le64_to_cpu(raw->i_atime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) inode->i_ctime.tv_sec = le64_to_cpu(raw->i_ctime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) inode->i_mtime.tv_sec = le64_to_cpu(raw->i_mtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) inode->i_atime.tv_nsec = le32_to_cpu(raw->i_atime_nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) inode->i_ctime.tv_nsec = le32_to_cpu(raw->i_ctime_nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) inode->i_mtime.tv_nsec = le32_to_cpu(raw->i_mtime_nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) F2FS_I(inode)->i_advise = raw->i_advise;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) F2FS_I(inode)->i_flags = le32_to_cpu(raw->i_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) f2fs_set_inode_flags(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) le16_to_cpu(raw->i_gc_failures);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) recover_inline_flags(inode, raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) f2fs_mark_inode_dirty_sync(inode, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) if (file_enc_name(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) name = "<encrypted>";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) name = F2FS_INODE(page)->i_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) f2fs_notice(F2FS_I_SB(inode), "recover_inode: ino = %x, name = %s, inline = %x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) ino_of_node(page), name, raw->i_inline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) static int find_fsync_dnodes(struct f2fs_sb_info *sbi, struct list_head *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) bool check_only)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) struct curseg_info *curseg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) struct page *page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) block_t blkaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) unsigned int loop_cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) unsigned int free_blocks = MAIN_SEGS(sbi) * sbi->blocks_per_seg -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) valid_user_blocks(sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) /* get node pages in the current segment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) struct fsync_inode_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) if (!f2fs_is_valid_blkaddr(sbi, blkaddr, META_POR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) page = f2fs_get_tmp_page(sbi, blkaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (IS_ERR(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) err = PTR_ERR(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if (!is_recoverable_dnode(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) f2fs_put_page(page, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) if (!is_fsync_dnode(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) entry = get_fsync_inode(head, ino_of_node(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (!entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) bool quota_inode = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) if (!check_only &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) IS_INODE(page) && is_dent_dnode(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) err = f2fs_recover_inode_page(sbi, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) f2fs_put_page(page, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) quota_inode = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) * CP | dnode(F) | inode(DF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) * For this case, we should not give up now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) entry = add_fsync_inode(sbi, head, ino_of_node(page),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) quota_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) if (IS_ERR(entry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) err = PTR_ERR(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) if (err == -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) f2fs_put_page(page, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) entry->blkaddr = blkaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) if (IS_INODE(page) && is_dent_dnode(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) entry->last_dentry = blkaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) /* sanity check in order to detect looped node chain */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) if (++loop_cnt >= free_blocks ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) blkaddr == next_blkaddr_of_node(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) f2fs_notice(sbi, "%s: detect looped node chain, blkaddr:%u, next:%u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) __func__, blkaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) next_blkaddr_of_node(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) f2fs_put_page(page, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) /* check next segment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) blkaddr = next_blkaddr_of_node(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) f2fs_put_page(page, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) f2fs_ra_meta_pages_cond(sbi, blkaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) static void destroy_fsync_dnodes(struct list_head *head, int drop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) struct fsync_inode_entry *entry, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) list_for_each_entry_safe(entry, tmp, head, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) del_fsync_inode(entry, drop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) static int check_index_in_prev_nodes(struct f2fs_sb_info *sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) block_t blkaddr, struct dnode_of_data *dn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) struct seg_entry *sentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) unsigned int segno = GET_SEGNO(sbi, blkaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) unsigned short blkoff = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) struct f2fs_summary_block *sum_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) struct f2fs_summary sum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) struct page *sum_page, *node_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) struct dnode_of_data tdn = *dn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) nid_t ino, nid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) unsigned int offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) block_t bidx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) sentry = get_seg_entry(sbi, segno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) if (!f2fs_test_bit(blkoff, sentry->cur_valid_map))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) /* Get the previous summary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) struct curseg_info *curseg = CURSEG_I(sbi, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) if (curseg->segno == segno) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) sum = curseg->sum_blk->entries[blkoff];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) goto got_it;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) sum_page = f2fs_get_sum_page(sbi, segno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) if (IS_ERR(sum_page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) return PTR_ERR(sum_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) sum_node = (struct f2fs_summary_block *)page_address(sum_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) sum = sum_node->entries[blkoff];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) f2fs_put_page(sum_page, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) got_it:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) /* Use the locked dnode page and inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) nid = le32_to_cpu(sum.nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) if (dn->inode->i_ino == nid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) tdn.nid = nid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) if (!dn->inode_page_locked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) lock_page(dn->inode_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) tdn.node_page = dn->inode_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) tdn.ofs_in_node = le16_to_cpu(sum.ofs_in_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) goto truncate_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) } else if (dn->nid == nid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) tdn.ofs_in_node = le16_to_cpu(sum.ofs_in_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) goto truncate_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) /* Get the node page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) node_page = f2fs_get_node_page(sbi, nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) if (IS_ERR(node_page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) return PTR_ERR(node_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) offset = ofs_of_node(node_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) ino = ino_of_node(node_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) f2fs_put_page(node_page, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) if (ino != dn->inode->i_ino) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) /* Deallocate previous index in the node page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) inode = f2fs_iget_retry(sbi->sb, ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) if (IS_ERR(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) return PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) ret = dquot_initialize(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) inode = dn->inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) bidx = f2fs_start_bidx_of_node(offset, inode) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) le16_to_cpu(sum.ofs_in_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) * if inode page is locked, unlock temporarily, but its reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) * count keeps alive.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) if (ino == dn->inode->i_ino && dn->inode_page_locked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) unlock_page(dn->inode_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) set_new_dnode(&tdn, inode, NULL, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) if (f2fs_get_dnode_of_data(&tdn, bidx, LOOKUP_NODE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) if (tdn.data_blkaddr == blkaddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) f2fs_truncate_data_blocks_range(&tdn, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) f2fs_put_dnode(&tdn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) if (ino != dn->inode->i_ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) else if (dn->inode_page_locked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) lock_page(dn->inode_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) truncate_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) if (f2fs_data_blkaddr(&tdn) == blkaddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) f2fs_truncate_data_blocks_range(&tdn, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) if (dn->inode->i_ino == nid && !dn->inode_page_locked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) unlock_page(dn->inode_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) static int do_recover_data(struct f2fs_sb_info *sbi, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) struct dnode_of_data dn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) struct node_info ni;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) unsigned int start, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) int err = 0, recovered = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) /* step 1: recover xattr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) if (IS_INODE(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) err = f2fs_recover_inline_xattr(inode, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) } else if (f2fs_has_xattr_block(ofs_of_node(page))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) err = f2fs_recover_xattr_data(inode, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) recovered++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) /* step 2: recover inline data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) err = f2fs_recover_inline_data(inode, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) if (err == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) /* step 3: recover data indices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) start = f2fs_start_bidx_of_node(ofs_of_node(page), inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) end = start + ADDRS_PER_PAGE(page, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) set_new_dnode(&dn, inode, NULL, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) retry_dn:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) err = f2fs_get_dnode_of_data(&dn, start, ALLOC_NODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) if (err == -ENOMEM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) congestion_wait(BLK_RW_ASYNC, DEFAULT_IO_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) goto retry_dn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) f2fs_wait_on_page_writeback(dn.node_page, NODE, true, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) err = f2fs_get_node_info(sbi, dn.nid, &ni, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) f2fs_bug_on(sbi, ni.ino != ino_of_node(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) if (ofs_of_node(dn.node_page) != ofs_of_node(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) f2fs_warn(sbi, "Inconsistent ofs_of_node, ino:%lu, ofs:%u, %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) inode->i_ino, ofs_of_node(dn.node_page),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) ofs_of_node(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) err = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) for (; start < end; start++, dn.ofs_in_node++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) block_t src, dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) src = f2fs_data_blkaddr(&dn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) dest = data_blkaddr(dn.inode, page, dn.ofs_in_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) if (__is_valid_data_blkaddr(src) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) !f2fs_is_valid_blkaddr(sbi, src, META_POR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) err = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) if (__is_valid_data_blkaddr(dest) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) !f2fs_is_valid_blkaddr(sbi, dest, META_POR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) err = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) /* skip recovering if dest is the same as src */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) if (src == dest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) /* dest is invalid, just invalidate src block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) if (dest == NULL_ADDR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) f2fs_truncate_data_blocks_range(&dn, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) if (!file_keep_isize(inode) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) (i_size_read(inode) <= ((loff_t)start << PAGE_SHIFT)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) f2fs_i_size_write(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) (loff_t)(start + 1) << PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) * dest is reserved block, invalidate src block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) * and then reserve one new block in dnode page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) if (dest == NEW_ADDR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) f2fs_truncate_data_blocks_range(&dn, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) f2fs_reserve_new_block(&dn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) /* dest is valid block, try to recover from src to dest */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) if (f2fs_is_valid_blkaddr(sbi, dest, META_POR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) if (src == NULL_ADDR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) err = f2fs_reserve_new_block(&dn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) while (err &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) IS_ENABLED(CONFIG_F2FS_FAULT_INJECTION))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) err = f2fs_reserve_new_block(&dn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) /* We should not get -ENOSPC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) f2fs_bug_on(sbi, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) retry_prev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) /* Check the previous node page having this index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) err = check_index_in_prev_nodes(sbi, dest, &dn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) if (err == -ENOMEM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) congestion_wait(BLK_RW_ASYNC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) DEFAULT_IO_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) goto retry_prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) /* write dummy data page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) f2fs_replace_block(sbi, &dn, src, dest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) ni.version, false, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) recovered++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) copy_node_footer(dn.node_page, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) fill_node_footer(dn.node_page, dn.nid, ni.ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) ofs_of_node(page), false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) set_page_dirty(dn.node_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) f2fs_put_dnode(&dn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) f2fs_notice(sbi, "recover_data: ino = %lx (i_size: %s) recovered = %d, err = %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) inode->i_ino, file_keep_isize(inode) ? "keep" : "recover",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) recovered, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) static int recover_data(struct f2fs_sb_info *sbi, struct list_head *inode_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) struct list_head *tmp_inode_list, struct list_head *dir_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) struct curseg_info *curseg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) struct page *page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) block_t blkaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) /* get node pages in the current segment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) struct fsync_inode_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) if (!f2fs_is_valid_blkaddr(sbi, blkaddr, META_POR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) f2fs_ra_meta_pages_cond(sbi, blkaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) page = f2fs_get_tmp_page(sbi, blkaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) if (IS_ERR(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) err = PTR_ERR(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) if (!is_recoverable_dnode(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) f2fs_put_page(page, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) entry = get_fsync_inode(inode_list, ino_of_node(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) if (!entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) * inode(x) | CP | inode(x) | dnode(F)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) * In this case, we can lose the latest inode(x).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) * So, call recover_inode for the inode update.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) if (IS_INODE(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) err = recover_inode(entry->inode, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) f2fs_put_page(page, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) if (entry->last_dentry == blkaddr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) err = recover_dentry(entry->inode, page, dir_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) f2fs_put_page(page, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) err = do_recover_data(sbi, entry->inode, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) f2fs_put_page(page, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) if (entry->blkaddr == blkaddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) list_move_tail(&entry->list, tmp_inode_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) /* check next segment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) blkaddr = next_blkaddr_of_node(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) f2fs_put_page(page, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) f2fs_allocate_new_segments(sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) struct list_head inode_list, tmp_inode_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) struct list_head dir_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) unsigned long s_flags = sbi->sb->s_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) bool need_writecp = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) bool fix_curseg_write_pointer = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) #ifdef CONFIG_QUOTA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) int quota_enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) if (s_flags & SB_RDONLY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) f2fs_info(sbi, "recover fsync data on readonly fs");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) sbi->sb->s_flags &= ~SB_RDONLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) #ifdef CONFIG_QUOTA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) /* Needed for iput() to work correctly and not trash data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) sbi->sb->s_flags |= SB_ACTIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) /* Turn on quotas so that they are updated correctly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) quota_enabled = f2fs_enable_quota_files(sbi, s_flags & SB_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) INIT_LIST_HEAD(&inode_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) INIT_LIST_HEAD(&tmp_inode_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) INIT_LIST_HEAD(&dir_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) /* prevent checkpoint */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) f2fs_down_write(&sbi->cp_global_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) /* step #1: find fsynced inode numbers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) err = find_fsync_dnodes(sbi, &inode_list, check_only);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) if (err || list_empty(&inode_list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) goto skip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) if (check_only) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) goto skip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) need_writecp = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) /* step #2: recover data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) err = recover_data(sbi, &inode_list, &tmp_inode_list, &dir_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) f2fs_bug_on(sbi, !list_empty(&inode_list));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) /* restore s_flags to let iput() trash data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) sbi->sb->s_flags = s_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) skip:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) fix_curseg_write_pointer = !check_only || list_empty(&inode_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) destroy_fsync_dnodes(&inode_list, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) destroy_fsync_dnodes(&tmp_inode_list, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) /* truncate meta pages to be used by the recovery */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) truncate_inode_pages_range(META_MAPPING(sbi),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) (loff_t)MAIN_BLKADDR(sbi) << PAGE_SHIFT, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) truncate_inode_pages_final(NODE_MAPPING(sbi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) truncate_inode_pages_final(META_MAPPING(sbi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) * If fsync data succeeds or there is no fsync data to recover,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) * and the f2fs is not read only, check and fix zoned block devices'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) * write pointer consistency.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) if (!err && fix_curseg_write_pointer && !f2fs_readonly(sbi->sb) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) f2fs_sb_has_blkzoned(sbi)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) err = f2fs_fix_curseg_write_pointer(sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) clear_sbi_flag(sbi, SBI_POR_DOING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) f2fs_up_write(&sbi->cp_global_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) /* let's drop all the directory inodes for clean checkpoint */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) destroy_fsync_dnodes(&dir_list, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) if (need_writecp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) set_sbi_flag(sbi, SBI_IS_RECOVERED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) struct cp_control cpc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) .reason = CP_RECOVERY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) err = f2fs_write_checkpoint(sbi, &cpc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) #ifdef CONFIG_QUOTA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) /* Turn quotas off */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) if (quota_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) f2fs_quota_off_umount(sbi->sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) sbi->sb->s_flags = s_flags; /* Restore SB_RDONLY status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) return ret ? ret : err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) int __init f2fs_create_recovery_cache(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) fsync_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_inode_entry",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) sizeof(struct fsync_inode_entry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) if (!fsync_entry_slab)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) void f2fs_destroy_recovery_cache(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) kmem_cache_destroy(fsync_entry_slab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) }