Chao Yu | 7c1a000 | 2018-09-12 09:16:07 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 2 | /* |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 3 | * fs/f2fs/inode.c |
| 4 | * |
| 5 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. |
| 6 | * http://www.samsung.com/ |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 7 | */ |
| 8 | #include <linux/fs.h> |
| 9 | #include <linux/f2fs_fs.h> |
| 10 | #include <linux/buffer_head.h> |
| 11 | #include <linux/writeback.h> |
NeilBrown | 4034247 | 2022-01-14 14:07:14 -0800 | [diff] [blame] | 12 | #include <linux/sched/mm.h> |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 13 | |
| 14 | #include "f2fs.h" |
| 15 | #include "node.h" |
Chao Yu | 0eb0ada | 2017-06-14 23:00:56 +0800 | [diff] [blame] | 16 | #include "segment.h" |
Chao Yu | dd6c89b | 2019-03-04 17:19:04 +0800 | [diff] [blame] | 17 | #include "xattr.h" |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 18 | |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 19 | #include <trace/events/f2fs.h> |
| 20 | |
Chao Yu | 6ce19af | 2021-05-20 19:51:50 +0800 | [diff] [blame] | 21 | #ifdef CONFIG_F2FS_FS_COMPRESSION |
| 22 | extern const struct address_space_operations f2fs_compress_aops; |
| 23 | #endif |
| 24 | |
Jaegeuk Kim | 7c45729 | 2016-10-14 11:51:23 -0700 | [diff] [blame] | 25 | void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync) |
Jaegeuk Kim | b56ab83 | 2016-06-30 19:09:37 -0700 | [diff] [blame] | 26 | { |
Daeho Jeong | 9ac1e2d | 2018-01-11 11:26:19 +0900 | [diff] [blame] | 27 | if (is_inode_flag_set(inode, FI_NEW_INODE)) |
| 28 | return; |
| 29 | |
Jaegeuk Kim | 7c45729 | 2016-10-14 11:51:23 -0700 | [diff] [blame] | 30 | if (f2fs_inode_dirtied(inode, sync)) |
Jaegeuk Kim | b56ab83 | 2016-06-30 19:09:37 -0700 | [diff] [blame] | 31 | return; |
Jaegeuk Kim | 7c45729 | 2016-10-14 11:51:23 -0700 | [diff] [blame] | 32 | |
Jaegeuk Kim | b56ab83 | 2016-06-30 19:09:37 -0700 | [diff] [blame] | 33 | mark_inode_dirty_sync(inode); |
| 34 | } |
| 35 | |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 36 | void f2fs_set_inode_flags(struct inode *inode) |
| 37 | { |
| 38 | unsigned int flags = F2FS_I(inode)->i_flags; |
Zhang Zhen | 8abfb36 | 2014-04-15 14:19:38 +0800 | [diff] [blame] | 39 | unsigned int new_fl = 0; |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 40 | |
Chao Yu | 59c8440 | 2018-04-03 15:08:17 +0800 | [diff] [blame] | 41 | if (flags & F2FS_SYNC_FL) |
Zhang Zhen | 8abfb36 | 2014-04-15 14:19:38 +0800 | [diff] [blame] | 42 | new_fl |= S_SYNC; |
Chao Yu | 59c8440 | 2018-04-03 15:08:17 +0800 | [diff] [blame] | 43 | if (flags & F2FS_APPEND_FL) |
Zhang Zhen | 8abfb36 | 2014-04-15 14:19:38 +0800 | [diff] [blame] | 44 | new_fl |= S_APPEND; |
Chao Yu | 59c8440 | 2018-04-03 15:08:17 +0800 | [diff] [blame] | 45 | if (flags & F2FS_IMMUTABLE_FL) |
Zhang Zhen | 8abfb36 | 2014-04-15 14:19:38 +0800 | [diff] [blame] | 46 | new_fl |= S_IMMUTABLE; |
Chao Yu | 59c8440 | 2018-04-03 15:08:17 +0800 | [diff] [blame] | 47 | if (flags & F2FS_NOATIME_FL) |
Zhang Zhen | 8abfb36 | 2014-04-15 14:19:38 +0800 | [diff] [blame] | 48 | new_fl |= S_NOATIME; |
Chao Yu | 59c8440 | 2018-04-03 15:08:17 +0800 | [diff] [blame] | 49 | if (flags & F2FS_DIRSYNC_FL) |
Zhang Zhen | 8abfb36 | 2014-04-15 14:19:38 +0800 | [diff] [blame] | 50 | new_fl |= S_DIRSYNC; |
Chandan Rajendra | 62230e0d | 2018-12-12 15:20:11 +0530 | [diff] [blame] | 51 | if (file_is_encrypt(inode)) |
Eric Biggers | 2ee6a57 | 2017-10-09 12:15:35 -0700 | [diff] [blame] | 52 | new_fl |= S_ENCRYPTED; |
Eric Biggers | 95ae251 | 2019-07-22 09:26:24 -0700 | [diff] [blame] | 53 | if (file_is_verity(inode)) |
| 54 | new_fl |= S_VERITY; |
Daniel Rosenberg | 2c2eb7a | 2019-07-23 16:05:29 -0700 | [diff] [blame] | 55 | if (flags & F2FS_CASEFOLD_FL) |
| 56 | new_fl |= S_CASEFOLD; |
Zhang Zhen | 6a67885 | 2015-08-24 10:41:32 +0800 | [diff] [blame] | 57 | inode_set_flags(inode, new_fl, |
Eric Biggers | 2ee6a57 | 2017-10-09 12:15:35 -0700 | [diff] [blame] | 58 | S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC| |
Linus Torvalds | fbc246a | 2019-09-21 14:26:33 -0700 | [diff] [blame] | 59 | S_ENCRYPTED|S_VERITY|S_CASEFOLD); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 60 | } |
| 61 | |
Jaegeuk Kim | 3d1e380 | 2013-10-08 18:01:51 +0900 | [diff] [blame] | 62 | static void __get_inode_rdev(struct inode *inode, struct f2fs_inode *ri) |
| 63 | { |
Chao Yu | 7a2af76 | 2017-07-19 00:19:06 +0800 | [diff] [blame] | 64 | int extra_size = get_extra_isize(inode); |
| 65 | |
Jaegeuk Kim | 3d1e380 | 2013-10-08 18:01:51 +0900 | [diff] [blame] | 66 | if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || |
| 67 | S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { |
Chao Yu | 7a2af76 | 2017-07-19 00:19:06 +0800 | [diff] [blame] | 68 | if (ri->i_addr[extra_size]) |
| 69 | inode->i_rdev = old_decode_dev( |
| 70 | le32_to_cpu(ri->i_addr[extra_size])); |
Jaegeuk Kim | 3d1e380 | 2013-10-08 18:01:51 +0900 | [diff] [blame] | 71 | else |
Chao Yu | 7a2af76 | 2017-07-19 00:19:06 +0800 | [diff] [blame] | 72 | inode->i_rdev = new_decode_dev( |
| 73 | le32_to_cpu(ri->i_addr[extra_size + 1])); |
Jaegeuk Kim | 3d1e380 | 2013-10-08 18:01:51 +0900 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
Chao Yu | 91291e9 | 2018-07-10 23:01:45 +0800 | [diff] [blame] | 77 | static int __written_first_block(struct f2fs_sb_info *sbi, |
Chao Yu | e1da787 | 2018-06-05 17:44:11 +0800 | [diff] [blame] | 78 | struct f2fs_inode *ri) |
Jaegeuk Kim | 3c6c2be | 2015-03-17 17:16:35 -0700 | [diff] [blame] | 79 | { |
Chao Yu | 7a2af76 | 2017-07-19 00:19:06 +0800 | [diff] [blame] | 80 | block_t addr = le32_to_cpu(ri->i_addr[offset_in_addr(ri)]); |
Jaegeuk Kim | adad81e | 2015-03-24 12:04:20 -0700 | [diff] [blame] | 81 | |
Chao Yu | 91291e9 | 2018-07-10 23:01:45 +0800 | [diff] [blame] | 82 | if (!__is_valid_data_blkaddr(addr)) |
| 83 | return 1; |
Chao Yu | 93770ab | 2019-04-15 15:26:32 +0800 | [diff] [blame] | 84 | if (!f2fs_is_valid_blkaddr(sbi, addr, DATA_GENERIC_ENHANCE)) |
Chao Yu | 10f966b | 2019-06-20 11:36:14 +0800 | [diff] [blame] | 85 | return -EFSCORRUPTED; |
Chao Yu | 91291e9 | 2018-07-10 23:01:45 +0800 | [diff] [blame] | 86 | return 0; |
Jaegeuk Kim | 3c6c2be | 2015-03-17 17:16:35 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Jaegeuk Kim | 3d1e380 | 2013-10-08 18:01:51 +0900 | [diff] [blame] | 89 | static void __set_inode_rdev(struct inode *inode, struct f2fs_inode *ri) |
| 90 | { |
Chao Yu | 7a2af76 | 2017-07-19 00:19:06 +0800 | [diff] [blame] | 91 | int extra_size = get_extra_isize(inode); |
| 92 | |
Jaegeuk Kim | 3d1e380 | 2013-10-08 18:01:51 +0900 | [diff] [blame] | 93 | if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { |
| 94 | if (old_valid_dev(inode->i_rdev)) { |
Chao Yu | 7a2af76 | 2017-07-19 00:19:06 +0800 | [diff] [blame] | 95 | ri->i_addr[extra_size] = |
Chris Fries | 6c311ec | 2014-01-17 14:44:39 -0600 | [diff] [blame] | 96 | cpu_to_le32(old_encode_dev(inode->i_rdev)); |
Chao Yu | 7a2af76 | 2017-07-19 00:19:06 +0800 | [diff] [blame] | 97 | ri->i_addr[extra_size + 1] = 0; |
Jaegeuk Kim | 3d1e380 | 2013-10-08 18:01:51 +0900 | [diff] [blame] | 98 | } else { |
Chao Yu | 7a2af76 | 2017-07-19 00:19:06 +0800 | [diff] [blame] | 99 | ri->i_addr[extra_size] = 0; |
| 100 | ri->i_addr[extra_size + 1] = |
Chris Fries | 6c311ec | 2014-01-17 14:44:39 -0600 | [diff] [blame] | 101 | cpu_to_le32(new_encode_dev(inode->i_rdev)); |
Chao Yu | 7a2af76 | 2017-07-19 00:19:06 +0800 | [diff] [blame] | 102 | ri->i_addr[extra_size + 2] = 0; |
Jaegeuk Kim | 3d1e380 | 2013-10-08 18:01:51 +0900 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
Chao Yu | 9e5ba77 | 2015-01-06 14:28:43 +0800 | [diff] [blame] | 107 | static void __recover_inline_status(struct inode *inode, struct page *ipage) |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 108 | { |
Chao Yu | f247037 | 2017-07-19 00:19:05 +0800 | [diff] [blame] | 109 | void *inline_data = inline_data_addr(inode, ipage); |
Chao Yu | 9e5ba77 | 2015-01-06 14:28:43 +0800 | [diff] [blame] | 110 | __le32 *start = inline_data; |
Chao Yu | f247037 | 2017-07-19 00:19:05 +0800 | [diff] [blame] | 111 | __le32 *end = start + MAX_INLINE_DATA(inode) / sizeof(__le32); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 112 | |
Chao Yu | 9e5ba77 | 2015-01-06 14:28:43 +0800 | [diff] [blame] | 113 | while (start < end) { |
| 114 | if (*start++) { |
Chao Yu | bae0ee7 | 2018-12-25 17:43:42 +0800 | [diff] [blame] | 115 | f2fs_wait_on_page_writeback(ipage, NODE, true, true); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 116 | |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 117 | set_inode_flag(inode, FI_DATA_EXIST); |
| 118 | set_raw_inline(inode, F2FS_INODE(ipage)); |
Chao Yu | 9e5ba77 | 2015-01-06 14:28:43 +0800 | [diff] [blame] | 119 | set_page_dirty(ipage); |
| 120 | return; |
| 121 | } |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 122 | } |
Chao Yu | 9e5ba77 | 2015-01-06 14:28:43 +0800 | [diff] [blame] | 123 | return; |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Chao Yu | 704956e | 2017-07-31 20:19:09 +0800 | [diff] [blame] | 126 | static bool f2fs_enable_inode_chksum(struct f2fs_sb_info *sbi, struct page *page) |
| 127 | { |
| 128 | struct f2fs_inode *ri = &F2FS_NODE(page)->i; |
Chao Yu | 704956e | 2017-07-31 20:19:09 +0800 | [diff] [blame] | 129 | |
Chao Yu | 7beb01f | 2018-10-24 18:34:26 +0800 | [diff] [blame] | 130 | if (!f2fs_sb_has_inode_chksum(sbi)) |
Chao Yu | 704956e | 2017-07-31 20:19:09 +0800 | [diff] [blame] | 131 | return false; |
| 132 | |
Chao Yu | 4c6b56c | 2018-07-08 22:16:54 +0800 | [diff] [blame] | 133 | if (!IS_INODE(page) || !(ri->i_inline & F2FS_EXTRA_ATTR)) |
Chao Yu | 704956e | 2017-07-31 20:19:09 +0800 | [diff] [blame] | 134 | return false; |
| 135 | |
Zhikang Zhang | d696494 | 2018-04-14 01:02:34 +0800 | [diff] [blame] | 136 | if (!F2FS_FITS_IN_INODE(ri, le16_to_cpu(ri->i_extra_isize), |
| 137 | i_inode_checksum)) |
Chao Yu | 704956e | 2017-07-31 20:19:09 +0800 | [diff] [blame] | 138 | return false; |
| 139 | |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | static __u32 f2fs_inode_chksum(struct f2fs_sb_info *sbi, struct page *page) |
| 144 | { |
| 145 | struct f2fs_node *node = F2FS_NODE(page); |
| 146 | struct f2fs_inode *ri = &node->i; |
| 147 | __le32 ino = node->footer.ino; |
| 148 | __le32 gen = ri->i_generation; |
| 149 | __u32 chksum, chksum_seed; |
| 150 | __u32 dummy_cs = 0; |
| 151 | unsigned int offset = offsetof(struct f2fs_inode, i_inode_checksum); |
| 152 | unsigned int cs_size = sizeof(dummy_cs); |
| 153 | |
| 154 | chksum = f2fs_chksum(sbi, sbi->s_chksum_seed, (__u8 *)&ino, |
| 155 | sizeof(ino)); |
| 156 | chksum_seed = f2fs_chksum(sbi, chksum, (__u8 *)&gen, sizeof(gen)); |
| 157 | |
| 158 | chksum = f2fs_chksum(sbi, chksum_seed, (__u8 *)ri, offset); |
| 159 | chksum = f2fs_chksum(sbi, chksum, (__u8 *)&dummy_cs, cs_size); |
| 160 | offset += cs_size; |
| 161 | chksum = f2fs_chksum(sbi, chksum, (__u8 *)ri + offset, |
| 162 | F2FS_BLKSIZE - offset); |
| 163 | return chksum; |
| 164 | } |
| 165 | |
| 166 | bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page) |
| 167 | { |
| 168 | struct f2fs_inode *ri; |
| 169 | __u32 provided, calculated; |
| 170 | |
Jaegeuk Kim | 83a3bfd | 2018-06-21 13:46:23 -0700 | [diff] [blame] | 171 | if (unlikely(is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN))) |
| 172 | return true; |
| 173 | |
Weichao Guo | 54c55c4 | 2018-03-09 23:10:21 +0800 | [diff] [blame] | 174 | #ifdef CONFIG_F2FS_CHECK_FS |
| 175 | if (!f2fs_enable_inode_chksum(sbi, page)) |
| 176 | #else |
Jaegeuk Kim | ee60523 | 2017-08-31 16:54:51 -0700 | [diff] [blame] | 177 | if (!f2fs_enable_inode_chksum(sbi, page) || |
| 178 | PageDirty(page) || PageWriteback(page)) |
Weichao Guo | 54c55c4 | 2018-03-09 23:10:21 +0800 | [diff] [blame] | 179 | #endif |
Chao Yu | 704956e | 2017-07-31 20:19:09 +0800 | [diff] [blame] | 180 | return true; |
| 181 | |
| 182 | ri = &F2FS_NODE(page)->i; |
| 183 | provided = le32_to_cpu(ri->i_inode_checksum); |
| 184 | calculated = f2fs_inode_chksum(sbi, page); |
| 185 | |
| 186 | if (provided != calculated) |
Joe Perches | dcbb4c1 | 2019-06-18 17:48:42 +0800 | [diff] [blame] | 187 | f2fs_warn(sbi, "checksum invalid, nid = %lu, ino_of_node = %x, %x vs. %x", |
| 188 | page->index, ino_of_node(page), provided, calculated); |
Chao Yu | 704956e | 2017-07-31 20:19:09 +0800 | [diff] [blame] | 189 | |
| 190 | return provided == calculated; |
| 191 | } |
| 192 | |
| 193 | void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page) |
| 194 | { |
| 195 | struct f2fs_inode *ri = &F2FS_NODE(page)->i; |
| 196 | |
| 197 | if (!f2fs_enable_inode_chksum(sbi, page)) |
| 198 | return; |
| 199 | |
| 200 | ri->i_inode_checksum = cpu_to_le32(f2fs_inode_chksum(sbi, page)); |
| 201 | } |
| 202 | |
Chao Yu | e34438c | 2018-06-29 13:55:22 +0800 | [diff] [blame] | 203 | static bool sanity_check_inode(struct inode *inode, struct page *node_page) |
Jaegeuk Kim | 5d64600 | 2018-04-24 11:37:18 -0600 | [diff] [blame] | 204 | { |
| 205 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Chao Yu | 18dd647 | 2018-07-08 22:16:55 +0800 | [diff] [blame] | 206 | struct f2fs_inode_info *fi = F2FS_I(inode); |
Chao Yu | 4c8ff70 | 2019-11-01 18:07:14 +0800 | [diff] [blame] | 207 | struct f2fs_inode *ri = F2FS_INODE(node_page); |
Chao Yu | e34438c | 2018-06-29 13:55:22 +0800 | [diff] [blame] | 208 | unsigned long long iblocks; |
| 209 | |
| 210 | iblocks = le64_to_cpu(F2FS_INODE(node_page)->i_blocks); |
| 211 | if (!iblocks) { |
| 212 | set_sbi_flag(sbi, SBI_NEED_FSCK); |
Joe Perches | dcbb4c1 | 2019-06-18 17:48:42 +0800 | [diff] [blame] | 213 | f2fs_warn(sbi, "%s: corrupted inode i_blocks i_ino=%lx iblocks=%llu, run fsck to fix.", |
| 214 | __func__, inode->i_ino, iblocks); |
Chao Yu | e34438c | 2018-06-29 13:55:22 +0800 | [diff] [blame] | 215 | return false; |
| 216 | } |
| 217 | |
| 218 | if (ino_of_node(node_page) != nid_of_node(node_page)) { |
| 219 | set_sbi_flag(sbi, SBI_NEED_FSCK); |
Joe Perches | dcbb4c1 | 2019-06-18 17:48:42 +0800 | [diff] [blame] | 220 | f2fs_warn(sbi, "%s: corrupted inode footer i_ino=%lx, ino,nid: [%u, %u] run fsck to fix.", |
| 221 | __func__, inode->i_ino, |
| 222 | ino_of_node(node_page), nid_of_node(node_page)); |
Chao Yu | e34438c | 2018-06-29 13:55:22 +0800 | [diff] [blame] | 223 | return false; |
| 224 | } |
Jaegeuk Kim | 5d64600 | 2018-04-24 11:37:18 -0600 | [diff] [blame] | 225 | |
Chao Yu | 7beb01f | 2018-10-24 18:34:26 +0800 | [diff] [blame] | 226 | if (f2fs_sb_has_flexible_inline_xattr(sbi) |
Jaegeuk Kim | 5d64600 | 2018-04-24 11:37:18 -0600 | [diff] [blame] | 227 | && !f2fs_has_extra_attr(inode)) { |
| 228 | set_sbi_flag(sbi, SBI_NEED_FSCK); |
Joe Perches | dcbb4c1 | 2019-06-18 17:48:42 +0800 | [diff] [blame] | 229 | f2fs_warn(sbi, "%s: corrupted inode ino=%lx, run fsck to fix.", |
| 230 | __func__, inode->i_ino); |
Jaegeuk Kim | 5d64600 | 2018-04-24 11:37:18 -0600 | [diff] [blame] | 231 | return false; |
| 232 | } |
Chao Yu | 76d56d4 | 2018-06-25 23:29:49 +0800 | [diff] [blame] | 233 | |
| 234 | if (f2fs_has_extra_attr(inode) && |
Chao Yu | 7beb01f | 2018-10-24 18:34:26 +0800 | [diff] [blame] | 235 | !f2fs_sb_has_extra_attr(sbi)) { |
Chao Yu | 76d56d4 | 2018-06-25 23:29:49 +0800 | [diff] [blame] | 236 | set_sbi_flag(sbi, SBI_NEED_FSCK); |
Joe Perches | dcbb4c1 | 2019-06-18 17:48:42 +0800 | [diff] [blame] | 237 | f2fs_warn(sbi, "%s: inode (ino=%lx) is with extra_attr, but extra_attr feature is off", |
| 238 | __func__, inode->i_ino); |
Chao Yu | 76d56d4 | 2018-06-25 23:29:49 +0800 | [diff] [blame] | 239 | return false; |
| 240 | } |
Chao Yu | c9b6078 | 2018-08-01 19:13:44 +0800 | [diff] [blame] | 241 | |
Chao Yu | 18dd647 | 2018-07-08 22:16:55 +0800 | [diff] [blame] | 242 | if (fi->i_extra_isize > F2FS_TOTAL_EXTRA_ATTR_SIZE || |
| 243 | fi->i_extra_isize % sizeof(__le32)) { |
| 244 | set_sbi_flag(sbi, SBI_NEED_FSCK); |
Joe Perches | dcbb4c1 | 2019-06-18 17:48:42 +0800 | [diff] [blame] | 245 | f2fs_warn(sbi, "%s: inode (ino=%lx) has corrupted i_extra_isize: %d, max: %zu", |
| 246 | __func__, inode->i_ino, fi->i_extra_isize, |
| 247 | F2FS_TOTAL_EXTRA_ATTR_SIZE); |
Chao Yu | 18dd647 | 2018-07-08 22:16:55 +0800 | [diff] [blame] | 248 | return false; |
| 249 | } |
| 250 | |
Chao Yu | dd6c89b | 2019-03-04 17:19:04 +0800 | [diff] [blame] | 251 | if (f2fs_has_extra_attr(inode) && |
| 252 | f2fs_sb_has_flexible_inline_xattr(sbi) && |
| 253 | f2fs_has_inline_xattr(inode) && |
| 254 | (!fi->i_inline_xattr_size || |
| 255 | fi->i_inline_xattr_size > MAX_INLINE_XATTR_SIZE)) { |
| 256 | set_sbi_flag(sbi, SBI_NEED_FSCK); |
Joe Perches | dcbb4c1 | 2019-06-18 17:48:42 +0800 | [diff] [blame] | 257 | f2fs_warn(sbi, "%s: inode (ino=%lx) has corrupted i_inline_xattr_size: %d, max: %zu", |
| 258 | __func__, inode->i_ino, fi->i_inline_xattr_size, |
| 259 | MAX_INLINE_XATTR_SIZE); |
Chao Yu | dd6c89b | 2019-03-04 17:19:04 +0800 | [diff] [blame] | 260 | return false; |
| 261 | } |
| 262 | |
Chao Yu | c9b6078 | 2018-08-01 19:13:44 +0800 | [diff] [blame] | 263 | if (F2FS_I(inode)->extent_tree) { |
| 264 | struct extent_info *ei = &F2FS_I(inode)->extent_tree->largest; |
| 265 | |
| 266 | if (ei->len && |
Chao Yu | 93770ab | 2019-04-15 15:26:32 +0800 | [diff] [blame] | 267 | (!f2fs_is_valid_blkaddr(sbi, ei->blk, |
| 268 | DATA_GENERIC_ENHANCE) || |
Chao Yu | c9b6078 | 2018-08-01 19:13:44 +0800 | [diff] [blame] | 269 | !f2fs_is_valid_blkaddr(sbi, ei->blk + ei->len - 1, |
Chao Yu | 93770ab | 2019-04-15 15:26:32 +0800 | [diff] [blame] | 270 | DATA_GENERIC_ENHANCE))) { |
Chao Yu | c9b6078 | 2018-08-01 19:13:44 +0800 | [diff] [blame] | 271 | set_sbi_flag(sbi, SBI_NEED_FSCK); |
Joe Perches | dcbb4c1 | 2019-06-18 17:48:42 +0800 | [diff] [blame] | 272 | f2fs_warn(sbi, "%s: inode (ino=%lx) extent info [%u, %u, %u] is incorrect, run fsck to fix", |
| 273 | __func__, inode->i_ino, |
| 274 | ei->blk, ei->fofs, ei->len); |
Chao Yu | c9b6078 | 2018-08-01 19:13:44 +0800 | [diff] [blame] | 275 | return false; |
| 276 | } |
| 277 | } |
Chao Yu | bcbfbd6 | 2018-06-29 00:19:25 +0800 | [diff] [blame] | 278 | |
| 279 | if (f2fs_has_inline_data(inode) && |
| 280 | (!S_ISREG(inode->i_mode) && !S_ISLNK(inode->i_mode))) { |
| 281 | set_sbi_flag(sbi, SBI_NEED_FSCK); |
Joe Perches | dcbb4c1 | 2019-06-18 17:48:42 +0800 | [diff] [blame] | 282 | f2fs_warn(sbi, "%s: inode (ino=%lx, mode=%u) should not have inline_data, run fsck to fix", |
| 283 | __func__, inode->i_ino, inode->i_mode); |
Chao Yu | bcbfbd6 | 2018-06-29 00:19:25 +0800 | [diff] [blame] | 284 | return false; |
| 285 | } |
| 286 | |
| 287 | if (f2fs_has_inline_dentry(inode) && !S_ISDIR(inode->i_mode)) { |
| 288 | set_sbi_flag(sbi, SBI_NEED_FSCK); |
Joe Perches | dcbb4c1 | 2019-06-18 17:48:42 +0800 | [diff] [blame] | 289 | f2fs_warn(sbi, "%s: inode (ino=%lx, mode=%u) should not have inline_dentry, run fsck to fix", |
| 290 | __func__, inode->i_ino, inode->i_mode); |
Chao Yu | bcbfbd6 | 2018-06-29 00:19:25 +0800 | [diff] [blame] | 291 | return false; |
| 292 | } |
| 293 | |
Eric Biggers | f6322f3 | 2020-10-08 12:15:22 -0700 | [diff] [blame] | 294 | if ((fi->i_flags & F2FS_CASEFOLD_FL) && !f2fs_sb_has_casefold(sbi)) { |
| 295 | set_sbi_flag(sbi, SBI_NEED_FSCK); |
| 296 | f2fs_warn(sbi, "%s: inode (ino=%lx) has casefold flag, but casefold feature is off", |
| 297 | __func__, inode->i_ino); |
| 298 | return false; |
| 299 | } |
| 300 | |
Chao Yu | 4c8ff70 | 2019-11-01 18:07:14 +0800 | [diff] [blame] | 301 | if (f2fs_has_extra_attr(inode) && f2fs_sb_has_compression(sbi) && |
| 302 | fi->i_flags & F2FS_COMPR_FL && |
| 303 | F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, |
| 304 | i_log_cluster_size)) { |
Chao Yu | d940aa0 | 2020-02-25 18:26:46 +0800 | [diff] [blame] | 305 | if (ri->i_compress_algorithm >= COMPRESS_MAX) { |
Chao Yu | d662fad | 2020-10-09 10:40:48 +0800 | [diff] [blame] | 306 | set_sbi_flag(sbi, SBI_NEED_FSCK); |
Chao Yu | d940aa0 | 2020-02-25 18:26:46 +0800 | [diff] [blame] | 307 | f2fs_warn(sbi, "%s: inode (ino=%lx) has unsupported " |
| 308 | "compress algorithm: %u, run fsck to fix", |
| 309 | __func__, inode->i_ino, |
| 310 | ri->i_compress_algorithm); |
Chao Yu | 4c8ff70 | 2019-11-01 18:07:14 +0800 | [diff] [blame] | 311 | return false; |
Chao Yu | d940aa0 | 2020-02-25 18:26:46 +0800 | [diff] [blame] | 312 | } |
| 313 | if (le64_to_cpu(ri->i_compr_blocks) > |
| 314 | SECTOR_TO_BLOCK(inode->i_blocks)) { |
Chao Yu | d662fad | 2020-10-09 10:40:48 +0800 | [diff] [blame] | 315 | set_sbi_flag(sbi, SBI_NEED_FSCK); |
Chao Yu | d940aa0 | 2020-02-25 18:26:46 +0800 | [diff] [blame] | 316 | f2fs_warn(sbi, "%s: inode (ino=%lx) has inconsistent " |
| 317 | "i_compr_blocks:%llu, i_blocks:%llu, run fsck to fix", |
| 318 | __func__, inode->i_ino, |
| 319 | le64_to_cpu(ri->i_compr_blocks), |
| 320 | SECTOR_TO_BLOCK(inode->i_blocks)); |
Chao Yu | 4c8ff70 | 2019-11-01 18:07:14 +0800 | [diff] [blame] | 321 | return false; |
Chao Yu | d940aa0 | 2020-02-25 18:26:46 +0800 | [diff] [blame] | 322 | } |
Chao Yu | 4c8ff70 | 2019-11-01 18:07:14 +0800 | [diff] [blame] | 323 | if (ri->i_log_cluster_size < MIN_COMPRESS_LOG_SIZE || |
Chao Yu | d940aa0 | 2020-02-25 18:26:46 +0800 | [diff] [blame] | 324 | ri->i_log_cluster_size > MAX_COMPRESS_LOG_SIZE) { |
Chao Yu | d662fad | 2020-10-09 10:40:48 +0800 | [diff] [blame] | 325 | set_sbi_flag(sbi, SBI_NEED_FSCK); |
Chao Yu | d940aa0 | 2020-02-25 18:26:46 +0800 | [diff] [blame] | 326 | f2fs_warn(sbi, "%s: inode (ino=%lx) has unsupported " |
| 327 | "log cluster size: %u, run fsck to fix", |
| 328 | __func__, inode->i_ino, |
| 329 | ri->i_log_cluster_size); |
Chao Yu | 4c8ff70 | 2019-11-01 18:07:14 +0800 | [diff] [blame] | 330 | return false; |
Chao Yu | d940aa0 | 2020-02-25 18:26:46 +0800 | [diff] [blame] | 331 | } |
Chao Yu | 4c8ff70 | 2019-11-01 18:07:14 +0800 | [diff] [blame] | 332 | } |
| 333 | |
Jaegeuk Kim | 5d64600 | 2018-04-24 11:37:18 -0600 | [diff] [blame] | 334 | return true; |
| 335 | } |
| 336 | |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 337 | static int do_read_inode(struct inode *inode) |
| 338 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 339 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 340 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 341 | struct page *node_page; |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 342 | struct f2fs_inode *ri; |
Chao Yu | 5c57132 | 2017-07-26 00:01:41 +0800 | [diff] [blame] | 343 | projid_t i_projid; |
Chao Yu | 91291e9 | 2018-07-10 23:01:45 +0800 | [diff] [blame] | 344 | int err; |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 345 | |
| 346 | /* Check if ino is within scope */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 347 | if (f2fs_check_nid_range(sbi, inode->i_ino)) |
Namjae Jeon | 064e082 | 2013-03-17 17:27:20 +0900 | [diff] [blame] | 348 | return -EINVAL; |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 349 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 350 | node_page = f2fs_get_node_page(sbi, inode->i_ino); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 351 | if (IS_ERR(node_page)) |
| 352 | return PTR_ERR(node_page); |
| 353 | |
Jaegeuk Kim | 58bfaf4 | 2013-12-26 16:30:41 +0900 | [diff] [blame] | 354 | ri = F2FS_INODE(node_page); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 355 | |
| 356 | inode->i_mode = le16_to_cpu(ri->i_mode); |
| 357 | i_uid_write(inode, le32_to_cpu(ri->i_uid)); |
| 358 | i_gid_write(inode, le32_to_cpu(ri->i_gid)); |
| 359 | set_nlink(inode, le32_to_cpu(ri->i_links)); |
| 360 | inode->i_size = le64_to_cpu(ri->i_size); |
Chao Yu | 000519f | 2017-07-06 01:11:31 +0800 | [diff] [blame] | 361 | inode->i_blocks = SECTOR_FROM_BLOCK(le64_to_cpu(ri->i_blocks) - 1); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 362 | |
| 363 | inode->i_atime.tv_sec = le64_to_cpu(ri->i_atime); |
| 364 | inode->i_ctime.tv_sec = le64_to_cpu(ri->i_ctime); |
| 365 | inode->i_mtime.tv_sec = le64_to_cpu(ri->i_mtime); |
| 366 | inode->i_atime.tv_nsec = le32_to_cpu(ri->i_atime_nsec); |
| 367 | inode->i_ctime.tv_nsec = le32_to_cpu(ri->i_ctime_nsec); |
| 368 | inode->i_mtime.tv_nsec = le32_to_cpu(ri->i_mtime_nsec); |
| 369 | inode->i_generation = le32_to_cpu(ri->i_generation); |
Chao Yu | 1c41e680 | 2018-05-07 20:28:52 +0800 | [diff] [blame] | 370 | if (S_ISDIR(inode->i_mode)) |
| 371 | fi->i_current_depth = le32_to_cpu(ri->i_current_depth); |
| 372 | else if (S_ISREG(inode->i_mode)) |
Chao Yu | 2ef79ec | 2018-05-07 20:28:54 +0800 | [diff] [blame] | 373 | fi->i_gc_failures[GC_FAILURE_PIN] = |
| 374 | le16_to_cpu(ri->i_gc_failures); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 375 | fi->i_xattr_nid = le32_to_cpu(ri->i_xattr_nid); |
| 376 | fi->i_flags = le32_to_cpu(ri->i_flags); |
Wang Shilong | 5043a96 | 2019-06-13 16:29:53 +0900 | [diff] [blame] | 377 | if (S_ISREG(inode->i_mode)) |
| 378 | fi->i_flags &= ~F2FS_PROJINHERIT_FL; |
Chao Yu | 7653b9d | 2020-03-23 11:18:07 +0800 | [diff] [blame] | 379 | bitmap_zero(fi->flags, FI_MAX); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 380 | fi->i_advise = ri->i_advise; |
Jaegeuk Kim | 6666e6a | 2012-12-10 17:52:48 +0900 | [diff] [blame] | 381 | fi->i_pino = le32_to_cpu(ri->i_pino); |
Jaegeuk Kim | 3843154 | 2014-02-27 18:20:00 +0900 | [diff] [blame] | 382 | fi->i_dir_level = ri->i_dir_level; |
Jaegeuk Kim | 3d1e380 | 2013-10-08 18:01:51 +0900 | [diff] [blame] | 383 | |
Chao Yu | a6d601f | 2020-06-28 10:58:17 +0800 | [diff] [blame] | 384 | f2fs_init_extent_tree(inode, node_page); |
Chao Yu | 0c872e2 | 2015-02-05 17:46:29 +0800 | [diff] [blame] | 385 | |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 386 | get_inline_info(inode, ri); |
Jaegeuk Kim | 3d1e380 | 2013-10-08 18:01:51 +0900 | [diff] [blame] | 387 | |
Chao Yu | 7a2af76 | 2017-07-19 00:19:06 +0800 | [diff] [blame] | 388 | fi->i_extra_isize = f2fs_has_extra_attr(inode) ? |
| 389 | le16_to_cpu(ri->i_extra_isize) : 0; |
| 390 | |
Chao Yu | 7beb01f | 2018-10-24 18:34:26 +0800 | [diff] [blame] | 391 | if (f2fs_sb_has_flexible_inline_xattr(sbi)) { |
Chao Yu | 6afc662 | 2017-09-06 21:59:50 +0800 | [diff] [blame] | 392 | fi->i_inline_xattr_size = le16_to_cpu(ri->i_inline_xattr_size); |
| 393 | } else if (f2fs_has_inline_xattr(inode) || |
| 394 | f2fs_has_inline_dentry(inode)) { |
| 395 | fi->i_inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS; |
| 396 | } else { |
| 397 | |
| 398 | /* |
| 399 | * Previous inline data or directory always reserved 200 bytes |
| 400 | * in inode layout, even if inline_xattr is disabled. In order |
| 401 | * to keep inline_dentry's structure for backward compatibility, |
| 402 | * we get the space back only from inline_data. |
| 403 | */ |
| 404 | fi->i_inline_xattr_size = 0; |
| 405 | } |
| 406 | |
Chao Yu | 18dd647 | 2018-07-08 22:16:55 +0800 | [diff] [blame] | 407 | if (!sanity_check_inode(inode, node_page)) { |
| 408 | f2fs_put_page(node_page, 1); |
Chao Yu | 10f966b | 2019-06-20 11:36:14 +0800 | [diff] [blame] | 409 | return -EFSCORRUPTED; |
Chao Yu | 18dd647 | 2018-07-08 22:16:55 +0800 | [diff] [blame] | 410 | } |
| 411 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 412 | /* check data exist */ |
| 413 | if (f2fs_has_inline_data(inode) && !f2fs_exist_data(inode)) |
Chao Yu | 9e5ba77 | 2015-01-06 14:28:43 +0800 | [diff] [blame] | 414 | __recover_inline_status(inode, node_page); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 415 | |
Chao Yu | ef2a007 | 2018-10-03 22:32:44 +0800 | [diff] [blame] | 416 | /* try to recover cold bit for non-dir inode */ |
| 417 | if (!S_ISDIR(inode->i_mode) && !is_cold_node(node_page)) { |
Wuyun Zhao | db5ae36 | 2020-06-18 10:58:37 +0800 | [diff] [blame] | 418 | f2fs_wait_on_page_writeback(node_page, NODE, true, true); |
Chao Yu | ef2a007 | 2018-10-03 22:32:44 +0800 | [diff] [blame] | 419 | set_cold_node(node_page, false); |
| 420 | set_page_dirty(node_page); |
| 421 | } |
| 422 | |
Jaegeuk Kim | 3d1e380 | 2013-10-08 18:01:51 +0900 | [diff] [blame] | 423 | /* get rdev by using inline_info */ |
| 424 | __get_inode_rdev(inode, ri); |
| 425 | |
Chao Yu | dda9f4b | 2018-08-11 23:42:09 +0800 | [diff] [blame] | 426 | if (S_ISREG(inode->i_mode)) { |
| 427 | err = __written_first_block(sbi, ri); |
| 428 | if (err < 0) { |
| 429 | f2fs_put_page(node_page, 1); |
| 430 | return err; |
| 431 | } |
| 432 | if (!err) |
| 433 | set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN); |
Chao Yu | 91291e9 | 2018-07-10 23:01:45 +0800 | [diff] [blame] | 434 | } |
Jaegeuk Kim | 3c6c2be | 2015-03-17 17:16:35 -0700 | [diff] [blame] | 435 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 436 | if (!f2fs_need_inode_block_update(sbi, inode->i_ino)) |
Jaegeuk Kim | 26de9b1 | 2016-05-20 20:42:37 -0700 | [diff] [blame] | 437 | fi->last_disk_size = inode->i_size; |
| 438 | |
Chao Yu | 59c8440 | 2018-04-03 15:08:17 +0800 | [diff] [blame] | 439 | if (fi->i_flags & F2FS_PROJINHERIT_FL) |
Chao Yu | 5c57132 | 2017-07-26 00:01:41 +0800 | [diff] [blame] | 440 | set_inode_flag(inode, FI_PROJ_INHERIT); |
| 441 | |
Chao Yu | 7beb01f | 2018-10-24 18:34:26 +0800 | [diff] [blame] | 442 | if (f2fs_has_extra_attr(inode) && f2fs_sb_has_project_quota(sbi) && |
Chao Yu | 5c57132 | 2017-07-26 00:01:41 +0800 | [diff] [blame] | 443 | F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_projid)) |
| 444 | i_projid = (projid_t)le32_to_cpu(ri->i_projid); |
| 445 | else |
| 446 | i_projid = F2FS_DEF_PROJID; |
| 447 | fi->i_projid = make_kprojid(&init_user_ns, i_projid); |
| 448 | |
Chao Yu | 7beb01f | 2018-10-24 18:34:26 +0800 | [diff] [blame] | 449 | if (f2fs_has_extra_attr(inode) && f2fs_sb_has_inode_crtime(sbi) && |
Chao Yu | 1c1d35d | 2018-01-25 14:54:42 +0800 | [diff] [blame] | 450 | F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) { |
| 451 | fi->i_crtime.tv_sec = le64_to_cpu(ri->i_crtime); |
| 452 | fi->i_crtime.tv_nsec = le32_to_cpu(ri->i_crtime_nsec); |
| 453 | } |
| 454 | |
Chao Yu | 4c8ff70 | 2019-11-01 18:07:14 +0800 | [diff] [blame] | 455 | if (f2fs_has_extra_attr(inode) && f2fs_sb_has_compression(sbi) && |
| 456 | (fi->i_flags & F2FS_COMPR_FL)) { |
| 457 | if (F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, |
| 458 | i_log_cluster_size)) { |
Daeho Jeong | c2759eb | 2020-09-08 11:44:10 +0900 | [diff] [blame] | 459 | atomic_set(&fi->i_compr_blocks, |
| 460 | le64_to_cpu(ri->i_compr_blocks)); |
Chao Yu | 4c8ff70 | 2019-11-01 18:07:14 +0800 | [diff] [blame] | 461 | fi->i_compress_algorithm = ri->i_compress_algorithm; |
| 462 | fi->i_log_cluster_size = ri->i_log_cluster_size; |
Chao Yu | b28f047 | 2020-11-26 18:32:09 +0800 | [diff] [blame] | 463 | fi->i_compress_flag = le16_to_cpu(ri->i_compress_flag); |
Chao Yu | 4c8ff70 | 2019-11-01 18:07:14 +0800 | [diff] [blame] | 464 | fi->i_cluster_size = 1 << fi->i_log_cluster_size; |
| 465 | set_inode_flag(inode, FI_COMPRESSED_FILE); |
| 466 | } |
| 467 | } |
| 468 | |
Arnd Bergmann | 24b81df | 2018-06-20 10:02:19 +0200 | [diff] [blame] | 469 | F2FS_I(inode)->i_disk_time[0] = inode->i_atime; |
| 470 | F2FS_I(inode)->i_disk_time[1] = inode->i_ctime; |
| 471 | F2FS_I(inode)->i_disk_time[2] = inode->i_mtime; |
Jaegeuk Kim | 214c246 | 2018-03-29 22:50:41 -0700 | [diff] [blame] | 472 | F2FS_I(inode)->i_disk_time[3] = F2FS_I(inode)->i_crtime; |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 473 | f2fs_put_page(node_page, 1); |
Jaegeuk Kim | 9d1015d | 2014-12-05 10:51:50 -0800 | [diff] [blame] | 474 | |
Chao Yu | d5e8f6c | 2015-07-15 17:28:53 +0800 | [diff] [blame] | 475 | stat_inc_inline_xattr(inode); |
Jaegeuk Kim | 9d1015d | 2014-12-05 10:51:50 -0800 | [diff] [blame] | 476 | stat_inc_inline_inode(inode); |
| 477 | stat_inc_inline_dir(inode); |
Chao Yu | 4c8ff70 | 2019-11-01 18:07:14 +0800 | [diff] [blame] | 478 | stat_inc_compr_inode(inode); |
Daeho Jeong | c2759eb | 2020-09-08 11:44:10 +0900 | [diff] [blame] | 479 | stat_add_compr_blocks(inode, atomic_read(&fi->i_compr_blocks)); |
Jaegeuk Kim | 9d1015d | 2014-12-05 10:51:50 -0800 | [diff] [blame] | 480 | |
Chao Yu | 9e5ba77 | 2015-01-06 14:28:43 +0800 | [diff] [blame] | 481 | return 0; |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | struct inode *f2fs_iget(struct super_block *sb, unsigned long ino) |
| 485 | { |
| 486 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
| 487 | struct inode *inode; |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 488 | int ret = 0; |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 489 | |
| 490 | inode = iget_locked(sb, ino); |
| 491 | if (!inode) |
| 492 | return ERR_PTR(-ENOMEM); |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 493 | |
| 494 | if (!(inode->i_state & I_NEW)) { |
| 495 | trace_f2fs_iget(inode); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 496 | return inode; |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 497 | } |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 498 | if (ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi)) |
| 499 | goto make_now; |
| 500 | |
Chao Yu | 6ce19af | 2021-05-20 19:51:50 +0800 | [diff] [blame] | 501 | #ifdef CONFIG_F2FS_FS_COMPRESSION |
| 502 | if (ino == F2FS_COMPRESS_INO(sbi)) |
| 503 | goto make_now; |
| 504 | #endif |
| 505 | |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 506 | ret = do_read_inode(inode); |
| 507 | if (ret) |
| 508 | goto bad_inode; |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 509 | make_now: |
| 510 | if (ino == F2FS_NODE_INO(sbi)) { |
| 511 | inode->i_mapping->a_ops = &f2fs_node_aops; |
Chao Yu | 81114ba | 2018-04-09 20:25:06 +0800 | [diff] [blame] | 512 | mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 513 | } else if (ino == F2FS_META_INO(sbi)) { |
| 514 | inode->i_mapping->a_ops = &f2fs_meta_aops; |
Chao Yu | 81114ba | 2018-04-09 20:25:06 +0800 | [diff] [blame] | 515 | mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS); |
Chao Yu | 6ce19af | 2021-05-20 19:51:50 +0800 | [diff] [blame] | 516 | } else if (ino == F2FS_COMPRESS_INO(sbi)) { |
| 517 | #ifdef CONFIG_F2FS_FS_COMPRESSION |
| 518 | inode->i_mapping->a_ops = &f2fs_compress_aops; |
Fengnan Chang | d191786 | 2021-11-26 18:19:19 +0800 | [diff] [blame] | 519 | /* |
| 520 | * generic_error_remove_page only truncates pages of regular |
| 521 | * inode |
| 522 | */ |
| 523 | inode->i_mode |= S_IFREG; |
Chao Yu | 6ce19af | 2021-05-20 19:51:50 +0800 | [diff] [blame] | 524 | #endif |
| 525 | mapping_set_gfp_mask(inode->i_mapping, |
| 526 | GFP_NOFS | __GFP_HIGHMEM | __GFP_MOVABLE); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 527 | } else if (S_ISREG(inode->i_mode)) { |
| 528 | inode->i_op = &f2fs_file_inode_operations; |
| 529 | inode->i_fop = &f2fs_file_operations; |
| 530 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
| 531 | } else if (S_ISDIR(inode->i_mode)) { |
| 532 | inode->i_op = &f2fs_dir_inode_operations; |
| 533 | inode->i_fop = &f2fs_dir_operations; |
| 534 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
Jaegeuk Kim | 92d602b | 2021-09-07 10:24:21 -0700 | [diff] [blame] | 535 | mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 536 | } else if (S_ISLNK(inode->i_mode)) { |
Chandan Rajendra | 62230e0d | 2018-12-12 15:20:11 +0530 | [diff] [blame] | 537 | if (file_is_encrypt(inode)) |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 538 | inode->i_op = &f2fs_encrypted_symlink_inode_operations; |
| 539 | else |
| 540 | inode->i_op = &f2fs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 541 | inode_nohighmem(inode); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 542 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
| 543 | } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || |
| 544 | S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { |
| 545 | inode->i_op = &f2fs_special_inode_operations; |
| 546 | init_special_inode(inode, inode->i_mode, inode->i_rdev); |
| 547 | } else { |
| 548 | ret = -EIO; |
| 549 | goto bad_inode; |
| 550 | } |
Jaegeuk Kim | 9360712 | 2017-05-16 13:20:16 -0700 | [diff] [blame] | 551 | f2fs_set_inode_flags(inode); |
Jaegeuk Kim | d4dd19e | 2021-11-12 14:31:16 -0800 | [diff] [blame] | 552 | |
| 553 | if (file_should_truncate(inode)) { |
| 554 | ret = f2fs_truncate(inode); |
| 555 | if (ret) |
| 556 | goto bad_inode; |
| 557 | file_dont_truncate(inode); |
| 558 | } |
| 559 | |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 560 | unlock_new_inode(inode); |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 561 | trace_f2fs_iget(inode); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 562 | return inode; |
| 563 | |
| 564 | bad_inode: |
Chao Yu | 546d22f | 2019-04-15 15:28:33 +0800 | [diff] [blame] | 565 | f2fs_inode_synced(inode); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 566 | iget_failed(inode); |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 567 | trace_f2fs_iget_exit(inode, ret); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 568 | return ERR_PTR(ret); |
| 569 | } |
| 570 | |
Jaegeuk Kim | e8ea9b3 | 2016-09-09 16:59:39 -0700 | [diff] [blame] | 571 | struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino) |
| 572 | { |
| 573 | struct inode *inode; |
| 574 | retry: |
| 575 | inode = f2fs_iget(sb, ino); |
| 576 | if (IS_ERR(inode)) { |
| 577 | if (PTR_ERR(inode) == -ENOMEM) { |
NeilBrown | 4034247 | 2022-01-14 14:07:14 -0800 | [diff] [blame] | 578 | memalloc_retry_wait(GFP_NOFS); |
Jaegeuk Kim | e8ea9b3 | 2016-09-09 16:59:39 -0700 | [diff] [blame] | 579 | goto retry; |
| 580 | } |
| 581 | } |
| 582 | return inode; |
| 583 | } |
| 584 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 585 | void f2fs_update_inode(struct inode *inode, struct page *node_page) |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 586 | { |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 587 | struct f2fs_inode *ri; |
Chao Yu | b691d98 | 2016-10-11 22:57:05 +0800 | [diff] [blame] | 588 | struct extent_tree *et = F2FS_I(inode)->extent_tree; |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 589 | |
Chao Yu | bae0ee7 | 2018-12-25 17:43:42 +0800 | [diff] [blame] | 590 | f2fs_wait_on_page_writeback(node_page, NODE, true, true); |
Yunlei He | 211a6fa | 2017-12-05 12:07:47 +0800 | [diff] [blame] | 591 | set_page_dirty(node_page); |
| 592 | |
| 593 | f2fs_inode_synced(inode); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 594 | |
Jaegeuk Kim | 58bfaf4 | 2013-12-26 16:30:41 +0900 | [diff] [blame] | 595 | ri = F2FS_INODE(node_page); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 596 | |
| 597 | ri->i_mode = cpu_to_le16(inode->i_mode); |
| 598 | ri->i_advise = F2FS_I(inode)->i_advise; |
| 599 | ri->i_uid = cpu_to_le32(i_uid_read(inode)); |
| 600 | ri->i_gid = cpu_to_le32(i_gid_read(inode)); |
| 601 | ri->i_links = cpu_to_le32(inode->i_nlink); |
| 602 | ri->i_size = cpu_to_le64(i_size_read(inode)); |
Chao Yu | 000519f | 2017-07-06 01:11:31 +0800 | [diff] [blame] | 603 | ri->i_blocks = cpu_to_le64(SECTOR_TO_BLOCK(inode->i_blocks) + 1); |
Chao Yu | 0c872e2 | 2015-02-05 17:46:29 +0800 | [diff] [blame] | 604 | |
Chao Yu | b691d98 | 2016-10-11 22:57:05 +0800 | [diff] [blame] | 605 | if (et) { |
| 606 | read_lock(&et->lock); |
| 607 | set_raw_extent(&et->largest, &ri->i_ext); |
| 608 | read_unlock(&et->lock); |
| 609 | } else { |
Jaegeuk Kim | 3e72f72 | 2015-06-19 17:53:26 -0700 | [diff] [blame] | 610 | memset(&ri->i_ext, 0, sizeof(ri->i_ext)); |
Chao Yu | b691d98 | 2016-10-11 22:57:05 +0800 | [diff] [blame] | 611 | } |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 612 | set_raw_inline(inode, ri); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 613 | |
| 614 | ri->i_atime = cpu_to_le64(inode->i_atime.tv_sec); |
| 615 | ri->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec); |
| 616 | ri->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec); |
| 617 | ri->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec); |
| 618 | ri->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); |
| 619 | ri->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec); |
Chao Yu | 1c41e680 | 2018-05-07 20:28:52 +0800 | [diff] [blame] | 620 | if (S_ISDIR(inode->i_mode)) |
| 621 | ri->i_current_depth = |
| 622 | cpu_to_le32(F2FS_I(inode)->i_current_depth); |
| 623 | else if (S_ISREG(inode->i_mode)) |
Chao Yu | 2ef79ec | 2018-05-07 20:28:54 +0800 | [diff] [blame] | 624 | ri->i_gc_failures = |
| 625 | cpu_to_le16(F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN]); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 626 | ri->i_xattr_nid = cpu_to_le32(F2FS_I(inode)->i_xattr_nid); |
| 627 | ri->i_flags = cpu_to_le32(F2FS_I(inode)->i_flags); |
Jaegeuk Kim | 6666e6a | 2012-12-10 17:52:48 +0900 | [diff] [blame] | 628 | ri->i_pino = cpu_to_le32(F2FS_I(inode)->i_pino); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 629 | ri->i_generation = cpu_to_le32(inode->i_generation); |
Jaegeuk Kim | 3843154 | 2014-02-27 18:20:00 +0900 | [diff] [blame] | 630 | ri->i_dir_level = F2FS_I(inode)->i_dir_level; |
Changman Lee | 7d79e75 | 2013-01-23 09:40:23 +0900 | [diff] [blame] | 631 | |
Chao Yu | 5c57132 | 2017-07-26 00:01:41 +0800 | [diff] [blame] | 632 | if (f2fs_has_extra_attr(inode)) { |
Chao Yu | 7a2af76 | 2017-07-19 00:19:06 +0800 | [diff] [blame] | 633 | ri->i_extra_isize = cpu_to_le16(F2FS_I(inode)->i_extra_isize); |
| 634 | |
Chao Yu | 7beb01f | 2018-10-24 18:34:26 +0800 | [diff] [blame] | 635 | if (f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(inode))) |
Chao Yu | 6afc662 | 2017-09-06 21:59:50 +0800 | [diff] [blame] | 636 | ri->i_inline_xattr_size = |
| 637 | cpu_to_le16(F2FS_I(inode)->i_inline_xattr_size); |
| 638 | |
Chao Yu | 7beb01f | 2018-10-24 18:34:26 +0800 | [diff] [blame] | 639 | if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)) && |
Chao Yu | 5c57132 | 2017-07-26 00:01:41 +0800 | [diff] [blame] | 640 | F2FS_FITS_IN_INODE(ri, F2FS_I(inode)->i_extra_isize, |
| 641 | i_projid)) { |
| 642 | projid_t i_projid; |
| 643 | |
| 644 | i_projid = from_kprojid(&init_user_ns, |
| 645 | F2FS_I(inode)->i_projid); |
| 646 | ri->i_projid = cpu_to_le32(i_projid); |
| 647 | } |
Chao Yu | 1c1d35d | 2018-01-25 14:54:42 +0800 | [diff] [blame] | 648 | |
Chao Yu | 7beb01f | 2018-10-24 18:34:26 +0800 | [diff] [blame] | 649 | if (f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) && |
Chao Yu | 1c1d35d | 2018-01-25 14:54:42 +0800 | [diff] [blame] | 650 | F2FS_FITS_IN_INODE(ri, F2FS_I(inode)->i_extra_isize, |
| 651 | i_crtime)) { |
| 652 | ri->i_crtime = |
| 653 | cpu_to_le64(F2FS_I(inode)->i_crtime.tv_sec); |
| 654 | ri->i_crtime_nsec = |
| 655 | cpu_to_le32(F2FS_I(inode)->i_crtime.tv_nsec); |
| 656 | } |
Chao Yu | 4c8ff70 | 2019-11-01 18:07:14 +0800 | [diff] [blame] | 657 | |
| 658 | if (f2fs_sb_has_compression(F2FS_I_SB(inode)) && |
| 659 | F2FS_FITS_IN_INODE(ri, F2FS_I(inode)->i_extra_isize, |
| 660 | i_log_cluster_size)) { |
| 661 | ri->i_compr_blocks = |
Daeho Jeong | c2759eb | 2020-09-08 11:44:10 +0900 | [diff] [blame] | 662 | cpu_to_le64(atomic_read( |
| 663 | &F2FS_I(inode)->i_compr_blocks)); |
Chao Yu | 4c8ff70 | 2019-11-01 18:07:14 +0800 | [diff] [blame] | 664 | ri->i_compress_algorithm = |
| 665 | F2FS_I(inode)->i_compress_algorithm; |
Chao Yu | b28f047 | 2020-11-26 18:32:09 +0800 | [diff] [blame] | 666 | ri->i_compress_flag = |
| 667 | cpu_to_le16(F2FS_I(inode)->i_compress_flag); |
Chao Yu | 4c8ff70 | 2019-11-01 18:07:14 +0800 | [diff] [blame] | 668 | ri->i_log_cluster_size = |
| 669 | F2FS_I(inode)->i_log_cluster_size; |
| 670 | } |
Chao Yu | 5c57132 | 2017-07-26 00:01:41 +0800 | [diff] [blame] | 671 | } |
| 672 | |
Jaegeuk Kim | 3d1e380 | 2013-10-08 18:01:51 +0900 | [diff] [blame] | 673 | __set_inode_rdev(inode, ri); |
Jaegeuk Kim | 12719ae | 2016-01-07 13:23:12 -0800 | [diff] [blame] | 674 | |
Jaegeuk Kim | 2049d4f | 2016-01-25 05:57:05 -0800 | [diff] [blame] | 675 | /* deleted inode */ |
| 676 | if (inode->i_nlink == 0) |
Chao Yu | b763f3b | 2021-04-28 17:20:31 +0800 | [diff] [blame] | 677 | clear_page_private_inline(node_page); |
Jaegeuk Kim | 2049d4f | 2016-01-25 05:57:05 -0800 | [diff] [blame] | 678 | |
Arnd Bergmann | 24b81df | 2018-06-20 10:02:19 +0200 | [diff] [blame] | 679 | F2FS_I(inode)->i_disk_time[0] = inode->i_atime; |
| 680 | F2FS_I(inode)->i_disk_time[1] = inode->i_ctime; |
| 681 | F2FS_I(inode)->i_disk_time[2] = inode->i_mtime; |
Jaegeuk Kim | 214c246 | 2018-03-29 22:50:41 -0700 | [diff] [blame] | 682 | F2FS_I(inode)->i_disk_time[3] = F2FS_I(inode)->i_crtime; |
Weichao Guo | 54c55c4 | 2018-03-09 23:10:21 +0800 | [diff] [blame] | 683 | |
| 684 | #ifdef CONFIG_F2FS_CHECK_FS |
| 685 | f2fs_inode_chksum_set(F2FS_I_SB(inode), node_page); |
| 686 | #endif |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 687 | } |
| 688 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 689 | void f2fs_update_inode_page(struct inode *inode) |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 690 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 691 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 692 | struct page *node_page; |
Jaegeuk Kim | 744602c | 2014-01-24 09:42:16 +0900 | [diff] [blame] | 693 | retry: |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 694 | node_page = f2fs_get_node_page(sbi, inode->i_ino); |
Jaegeuk Kim | 744602c | 2014-01-24 09:42:16 +0900 | [diff] [blame] | 695 | if (IS_ERR(node_page)) { |
| 696 | int err = PTR_ERR(node_page); |
Yi Zhuang | 5f029c0 | 2021-04-06 09:47:35 +0800 | [diff] [blame] | 697 | |
Jaegeuk Kim | 744602c | 2014-01-24 09:42:16 +0900 | [diff] [blame] | 698 | if (err == -ENOMEM) { |
| 699 | cond_resched(); |
| 700 | goto retry; |
| 701 | } else if (err != -ENOENT) { |
Jaegeuk Kim | 38f91ca | 2016-05-18 14:07:56 -0700 | [diff] [blame] | 702 | f2fs_stop_checkpoint(sbi, false); |
Jaegeuk Kim | 744602c | 2014-01-24 09:42:16 +0900 | [diff] [blame] | 703 | } |
Yunlei He | 211a6fa | 2017-12-05 12:07:47 +0800 | [diff] [blame] | 704 | return; |
Jaegeuk Kim | 744602c | 2014-01-24 09:42:16 +0900 | [diff] [blame] | 705 | } |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 706 | f2fs_update_inode(inode, node_page); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 707 | f2fs_put_page(node_page, 1); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc) |
| 711 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 712 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 713 | |
| 714 | if (inode->i_ino == F2FS_NODE_INO(sbi) || |
| 715 | inode->i_ino == F2FS_META_INO(sbi)) |
| 716 | return 0; |
| 717 | |
Chao Yu | fe1897e | 2019-09-27 18:01:35 +0800 | [diff] [blame] | 718 | /* |
| 719 | * atime could be updated without dirtying f2fs inode in lazytime mode |
| 720 | */ |
| 721 | if (f2fs_is_time_consistent(inode) && |
| 722 | !is_inode_flag_set(inode, FI_DIRTY_INODE)) |
Jaegeuk Kim | b378387 | 2013-06-10 09:17:01 +0900 | [diff] [blame] | 723 | return 0; |
| 724 | |
Chao Yu | 00e09c0 | 2019-08-23 17:58:36 +0800 | [diff] [blame] | 725 | if (!f2fs_is_checkpoint_ready(sbi)) |
Daniel Rosenberg | 4354994 | 2018-08-20 19:21:43 -0700 | [diff] [blame] | 726 | return -ENOSPC; |
| 727 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 728 | /* |
Jaegeuk Kim | c5cd29d | 2015-09-12 11:25:30 -0700 | [diff] [blame] | 729 | * We need to balance fs here to prevent from producing dirty node pages |
Ruiqi Gong | 2c718fe | 2021-03-25 02:38:11 -0400 | [diff] [blame] | 730 | * during the urgent cleaning time when running out of free sections. |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 731 | */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 732 | f2fs_update_inode_page(inode); |
Jaegeuk Kim | a788189 | 2017-04-20 13:51:57 -0700 | [diff] [blame] | 733 | if (wbc && wbc->nr_to_write) |
Jaegeuk Kim | 2c4db1a | 2016-01-07 14:15:04 -0800 | [diff] [blame] | 734 | f2fs_balance_fs(sbi, true); |
Jaegeuk Kim | 744602c | 2014-01-24 09:42:16 +0900 | [diff] [blame] | 735 | return 0; |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 736 | } |
| 737 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 738 | /* |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 739 | * Called at the last iput() if i_nlink is zero |
| 740 | */ |
| 741 | void f2fs_evict_inode(struct inode *inode) |
| 742 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 743 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 744 | nid_t xnid = F2FS_I(inode)->i_xattr_nid; |
Chao Yu | 13ec729 | 2015-08-24 17:40:45 +0800 | [diff] [blame] | 745 | int err = 0; |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 746 | |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 747 | /* some remained atomic pages should discarded */ |
Jaegeuk Kim | 1e84371 | 2014-12-09 06:08:59 -0800 | [diff] [blame] | 748 | if (f2fs_is_atomic_file(inode)) |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 749 | f2fs_drop_inmem_pages(inode); |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 750 | |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 751 | trace_f2fs_evict_inode(inode); |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 752 | truncate_inode_pages_final(&inode->i_data); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 753 | |
Fengnan Chang | 2b64289 | 2021-12-29 17:47:00 +0800 | [diff] [blame] | 754 | if ((inode->i_nlink || is_bad_inode(inode)) && |
| 755 | test_opt(sbi, COMPRESS_CACHE) && f2fs_compressed_file(inode)) |
Chao Yu | 6ce19af | 2021-05-20 19:51:50 +0800 | [diff] [blame] | 756 | f2fs_invalidate_compress_pages(sbi, inode->i_ino); |
| 757 | |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 758 | if (inode->i_ino == F2FS_NODE_INO(sbi) || |
Chao Yu | 6ce19af | 2021-05-20 19:51:50 +0800 | [diff] [blame] | 759 | inode->i_ino == F2FS_META_INO(sbi) || |
| 760 | inode->i_ino == F2FS_COMPRESS_INO(sbi)) |
Chao Yu | dbf20cb | 2014-07-25 12:00:57 +0800 | [diff] [blame] | 761 | goto out_clear; |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 762 | |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 763 | f2fs_bug_on(sbi, get_dirty_pages(inode)); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 764 | f2fs_remove_dirty_inode(inode); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 765 | |
Jaegeuk Kim | 3e72f72 | 2015-06-19 17:53:26 -0700 | [diff] [blame] | 766 | f2fs_destroy_extent_tree(inode); |
| 767 | |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 768 | if (inode->i_nlink || is_bad_inode(inode)) |
| 769 | goto no_delete; |
| 770 | |
Chao Yu | 10a2687 | 2021-10-28 21:03:05 +0800 | [diff] [blame] | 771 | err = f2fs_dquot_initialize(inode); |
Chao Yu | af033b2 | 2018-09-20 20:05:00 +0800 | [diff] [blame] | 772 | if (err) { |
| 773 | err = 0; |
| 774 | set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR); |
| 775 | } |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 776 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 777 | f2fs_remove_ino_entry(sbi, inode->i_ino, APPEND_INO); |
| 778 | f2fs_remove_ino_entry(sbi, inode->i_ino, UPDATE_INO); |
| 779 | f2fs_remove_ino_entry(sbi, inode->i_ino, FLUSH_INO); |
Chao Yu | 60dcedc | 2016-11-02 20:43:21 +0800 | [diff] [blame] | 780 | |
Changman Lee | d6212a5 | 2013-01-29 18:30:07 +0900 | [diff] [blame] | 781 | sb_start_intwrite(inode->i_sb); |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 782 | set_inode_flag(inode, FI_NO_ALLOC); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 783 | i_size_write(inode, 0); |
Jaegeuk Kim | 4c0c294 | 2016-05-03 09:22:18 -0700 | [diff] [blame] | 784 | retry: |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 785 | if (F2FS_HAS_BLOCKS(inode)) |
Jaegeuk Kim | 9a449e9 | 2016-06-02 13:49:38 -0700 | [diff] [blame] | 786 | err = f2fs_truncate(inode); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 787 | |
Jaegeuk Kim | 8c1b3c0 | 2017-03-07 13:32:20 -0800 | [diff] [blame] | 788 | if (time_to_inject(sbi, FAULT_EVICT_INODE)) { |
Chao Yu | c45d600 | 2019-11-01 17:53:23 +0800 | [diff] [blame] | 789 | f2fs_show_injection_info(sbi, FAULT_EVICT_INODE); |
Jaegeuk Kim | 8c1b3c0 | 2017-03-07 13:32:20 -0800 | [diff] [blame] | 790 | err = -EIO; |
| 791 | } |
Arnd Bergmann | 7fa750a | 2018-08-13 23:38:06 +0200 | [diff] [blame] | 792 | |
Chao Yu | 13ec729 | 2015-08-24 17:40:45 +0800 | [diff] [blame] | 793 | if (!err) { |
| 794 | f2fs_lock_op(sbi); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 795 | err = f2fs_remove_inode_page(inode); |
Chao Yu | 13ec729 | 2015-08-24 17:40:45 +0800 | [diff] [blame] | 796 | f2fs_unlock_op(sbi); |
Chao Yu | a11b9f6 | 2016-10-11 22:56:59 +0800 | [diff] [blame] | 797 | if (err == -ENOENT) |
| 798 | err = 0; |
Chao Yu | 13ec729 | 2015-08-24 17:40:45 +0800 | [diff] [blame] | 799 | } |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 800 | |
Jaegeuk Kim | 4c0c294 | 2016-05-03 09:22:18 -0700 | [diff] [blame] | 801 | /* give more chances, if ENOMEM case */ |
| 802 | if (err == -ENOMEM) { |
| 803 | err = 0; |
| 804 | goto retry; |
| 805 | } |
| 806 | |
Chao Yu | af033b2 | 2018-09-20 20:05:00 +0800 | [diff] [blame] | 807 | if (err) { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 808 | f2fs_update_inode_page(inode); |
Chao Yu | 0f1898f | 2019-07-19 11:51:11 +0800 | [diff] [blame] | 809 | if (dquot_initialize_needed(inode)) |
| 810 | set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR); |
Chao Yu | af033b2 | 2018-09-20 20:05:00 +0800 | [diff] [blame] | 811 | } |
Changman Lee | d6212a5 | 2013-01-29 18:30:07 +0900 | [diff] [blame] | 812 | sb_end_intwrite(inode->i_sb); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 813 | no_delete: |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 814 | dquot_drop(inode); |
| 815 | |
Chao Yu | d5e8f6c | 2015-07-15 17:28:53 +0800 | [diff] [blame] | 816 | stat_dec_inline_xattr(inode); |
Jaegeuk Kim | 3289c06 | 2014-10-13 20:00:16 -0700 | [diff] [blame] | 817 | stat_dec_inline_dir(inode); |
Jaegeuk Kim | e7a2bf2 | 2014-10-14 10:29:50 -0700 | [diff] [blame] | 818 | stat_dec_inline_inode(inode); |
Chao Yu | 4c8ff70 | 2019-11-01 18:07:14 +0800 | [diff] [blame] | 819 | stat_dec_compr_inode(inode); |
Daeho Jeong | c2759eb | 2020-09-08 11:44:10 +0900 | [diff] [blame] | 820 | stat_sub_compr_blocks(inode, |
| 821 | atomic_read(&F2FS_I(inode)->i_compr_blocks)); |
Chao Yu | 0bdee48 | 2015-03-19 19:27:51 +0800 | [diff] [blame] | 822 | |
Chao Yu | 33ac18a | 2019-08-15 19:45:35 +0800 | [diff] [blame] | 823 | if (likely(!f2fs_cp_error(sbi) && |
Daniel Rosenberg | 4354994 | 2018-08-20 19:21:43 -0700 | [diff] [blame] | 824 | !is_sbi_flag_set(sbi, SBI_CP_DISABLED))) |
Chao Yu | ca7d802 | 2017-09-12 14:04:05 +0800 | [diff] [blame] | 825 | f2fs_bug_on(sbi, is_inode_flag_set(inode, FI_DIRTY_INODE)); |
Jaegeuk Kim | 943973c | 2017-10-12 19:12:53 -0700 | [diff] [blame] | 826 | else |
| 827 | f2fs_inode_synced(inode); |
Chao Yu | ca7d802 | 2017-09-12 14:04:05 +0800 | [diff] [blame] | 828 | |
Chao Yu | 7a88ddb | 2020-02-27 19:30:05 +0800 | [diff] [blame] | 829 | /* for the case f2fs_new_inode() was failed, .i_ino is zero, skip it */ |
Jaegeuk Kim | 4f29544 | 2017-03-04 13:56:10 -0800 | [diff] [blame] | 830 | if (inode->i_ino) |
| 831 | invalidate_mapping_pages(NODE_MAPPING(sbi), inode->i_ino, |
| 832 | inode->i_ino); |
Chao Yu | 002a41c | 2014-08-04 09:54:58 +0800 | [diff] [blame] | 833 | if (xnid) |
| 834 | invalidate_mapping_pages(NODE_MAPPING(sbi), xnid, xnid); |
Chao Yu | 60dcedc | 2016-11-02 20:43:21 +0800 | [diff] [blame] | 835 | if (inode->i_nlink) { |
| 836 | if (is_inode_flag_set(inode, FI_APPEND_WRITE)) |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 837 | f2fs_add_ino_entry(sbi, inode->i_ino, APPEND_INO); |
Chao Yu | 60dcedc | 2016-11-02 20:43:21 +0800 | [diff] [blame] | 838 | if (is_inode_flag_set(inode, FI_UPDATE_WRITE)) |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 839 | f2fs_add_ino_entry(sbi, inode->i_ino, UPDATE_INO); |
Chao Yu | 60dcedc | 2016-11-02 20:43:21 +0800 | [diff] [blame] | 840 | } |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 841 | if (is_inode_flag_set(inode, FI_FREE_NID)) { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 842 | f2fs_alloc_nid_failed(sbi, inode->i_ino); |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 843 | clear_inode_flag(inode, FI_FREE_NID); |
Jaegeuk Kim | d8c4256 | 2017-06-01 15:39:27 -0700 | [diff] [blame] | 844 | } else { |
Jaegeuk Kim | a4f843b | 2018-04-23 23:02:31 -0600 | [diff] [blame] | 845 | /* |
| 846 | * If xattr nid is corrupted, we can reach out error condition, |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 847 | * err & !f2fs_exist_written_data(sbi, inode->i_ino, ORPHAN_INO)). |
| 848 | * In that case, f2fs_check_nid_range() is enough to give a clue. |
Jaegeuk Kim | a4f843b | 2018-04-23 23:02:31 -0600 | [diff] [blame] | 849 | */ |
Jaegeuk Kim | c9b63bd | 2015-06-23 10:36:08 -0700 | [diff] [blame] | 850 | } |
Chao Yu | dbf20cb | 2014-07-25 12:00:57 +0800 | [diff] [blame] | 851 | out_clear: |
Eric Biggers | 3d204e2 | 2018-01-11 23:30:13 -0500 | [diff] [blame] | 852 | fscrypt_put_encryption_info(inode); |
Eric Biggers | 95ae251 | 2019-07-22 09:26:24 -0700 | [diff] [blame] | 853 | fsverity_cleanup_inode(inode); |
Chao Yu | dbf20cb | 2014-07-25 12:00:57 +0800 | [diff] [blame] | 854 | clear_inode(inode); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 855 | } |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 856 | |
| 857 | /* caller should call f2fs_lock_op() */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 858 | void f2fs_handle_failed_inode(struct inode *inode) |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 859 | { |
| 860 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 221149c | 2016-05-02 12:34:48 -0700 | [diff] [blame] | 861 | struct node_info ni; |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 862 | int err; |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 863 | |
Chao Yu | a11b9f6 | 2016-10-11 22:56:59 +0800 | [diff] [blame] | 864 | /* |
| 865 | * clear nlink of inode in order to release resource of inode |
| 866 | * immediately. |
| 867 | */ |
| 868 | clear_nlink(inode); |
| 869 | |
| 870 | /* |
| 871 | * we must call this to avoid inode being remained as dirty, resulting |
| 872 | * in a panic when flushing dirty inodes in gdirty_list. |
| 873 | */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 874 | f2fs_update_inode_page(inode); |
Jaegeuk Kim | 9bb02c3 | 2017-04-11 19:01:26 -0700 | [diff] [blame] | 875 | f2fs_inode_synced(inode); |
Chao Yu | a11b9f6 | 2016-10-11 22:56:59 +0800 | [diff] [blame] | 876 | |
Jaegeuk Kim | 221149c | 2016-05-02 12:34:48 -0700 | [diff] [blame] | 877 | /* don't make bad inode, since it becomes a regular file. */ |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 878 | unlock_new_inode(inode); |
| 879 | |
Chao Yu | 13ec729 | 2015-08-24 17:40:45 +0800 | [diff] [blame] | 880 | /* |
Chao Yu | 13ec729 | 2015-08-24 17:40:45 +0800 | [diff] [blame] | 881 | * Note: we should add inode to orphan list before f2fs_unlock_op() |
| 882 | * so we can prevent losing this orphan when encoutering checkpoint |
| 883 | * and following suddenly power-off. |
| 884 | */ |
Jaegeuk Kim | a9419b6 | 2021-12-13 14:16:32 -0800 | [diff] [blame] | 885 | err = f2fs_get_node_info(sbi, inode->i_ino, &ni, false); |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 886 | if (err) { |
| 887 | set_sbi_flag(sbi, SBI_NEED_FSCK); |
Joe Perches | dcbb4c1 | 2019-06-18 17:48:42 +0800 | [diff] [blame] | 888 | f2fs_warn(sbi, "May loss orphan inode, run fsck to fix."); |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 889 | goto out; |
| 890 | } |
Jaegeuk Kim | 221149c | 2016-05-02 12:34:48 -0700 | [diff] [blame] | 891 | |
| 892 | if (ni.blk_addr != NULL_ADDR) { |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 893 | err = f2fs_acquire_orphan_inode(sbi); |
Jaegeuk Kim | 221149c | 2016-05-02 12:34:48 -0700 | [diff] [blame] | 894 | if (err) { |
| 895 | set_sbi_flag(sbi, SBI_NEED_FSCK); |
Joe Perches | dcbb4c1 | 2019-06-18 17:48:42 +0800 | [diff] [blame] | 896 | f2fs_warn(sbi, "Too many orphan inodes, run fsck to fix."); |
Jaegeuk Kim | 221149c | 2016-05-02 12:34:48 -0700 | [diff] [blame] | 897 | } else { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 898 | f2fs_add_orphan_inode(inode); |
Jaegeuk Kim | 221149c | 2016-05-02 12:34:48 -0700 | [diff] [blame] | 899 | } |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 900 | f2fs_alloc_nid_done(sbi, inode->i_ino); |
Jaegeuk Kim | 221149c | 2016-05-02 12:34:48 -0700 | [diff] [blame] | 901 | } else { |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 902 | set_inode_flag(inode, FI_FREE_NID); |
Chao Yu | 13ec729 | 2015-08-24 17:40:45 +0800 | [diff] [blame] | 903 | } |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 904 | |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 905 | out: |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 906 | f2fs_unlock_op(sbi); |
| 907 | |
| 908 | /* iput will drop the inode object */ |
| 909 | iput(inode); |
| 910 | } |