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