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