Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1 | /* |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 2 | * fs/f2fs/inode.c |
| 3 | * |
| 4 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. |
| 5 | * http://www.samsung.com/ |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | #include <linux/fs.h> |
| 12 | #include <linux/f2fs_fs.h> |
| 13 | #include <linux/buffer_head.h> |
Jaegeuk Kim | e8ea9b3 | 2016-09-09 16:59:39 -0700 | [diff] [blame] | 14 | #include <linux/backing-dev.h> |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 15 | #include <linux/writeback.h> |
| 16 | |
| 17 | #include "f2fs.h" |
| 18 | #include "node.h" |
Chao Yu | 0eb0ada | 2017-06-14 23:00:56 +0800 | [diff] [blame] | 19 | #include "segment.h" |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 20 | |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 21 | #include <trace/events/f2fs.h> |
| 22 | |
Jaegeuk Kim | 7c45729 | 2016-10-14 11:51:23 -0700 | [diff] [blame] | 23 | void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync) |
Jaegeuk Kim | b56ab83 | 2016-06-30 19:09:37 -0700 | [diff] [blame] | 24 | { |
Daeho Jeong | 9ac1e2d | 2018-01-11 11:26:19 +0900 | [diff] [blame] | 25 | if (is_inode_flag_set(inode, FI_NEW_INODE)) |
| 26 | return; |
| 27 | |
Jaegeuk Kim | 7c45729 | 2016-10-14 11:51:23 -0700 | [diff] [blame] | 28 | if (f2fs_inode_dirtied(inode, sync)) |
Jaegeuk Kim | b56ab83 | 2016-06-30 19:09:37 -0700 | [diff] [blame] | 29 | return; |
Jaegeuk Kim | 7c45729 | 2016-10-14 11:51:23 -0700 | [diff] [blame] | 30 | |
Jaegeuk Kim | b56ab83 | 2016-06-30 19:09:37 -0700 | [diff] [blame] | 31 | mark_inode_dirty_sync(inode); |
| 32 | } |
| 33 | |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 34 | void f2fs_set_inode_flags(struct inode *inode) |
| 35 | { |
| 36 | unsigned int flags = F2FS_I(inode)->i_flags; |
Zhang Zhen | 8abfb36 | 2014-04-15 14:19:38 +0800 | [diff] [blame] | 37 | unsigned int new_fl = 0; |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 38 | |
Chao Yu | 59c8440 | 2018-04-03 15:08:17 +0800 | [diff] [blame] | 39 | if (flags & F2FS_SYNC_FL) |
Zhang Zhen | 8abfb36 | 2014-04-15 14:19:38 +0800 | [diff] [blame] | 40 | new_fl |= S_SYNC; |
Chao Yu | 59c8440 | 2018-04-03 15:08:17 +0800 | [diff] [blame] | 41 | if (flags & F2FS_APPEND_FL) |
Zhang Zhen | 8abfb36 | 2014-04-15 14:19:38 +0800 | [diff] [blame] | 42 | new_fl |= S_APPEND; |
Chao Yu | 59c8440 | 2018-04-03 15:08:17 +0800 | [diff] [blame] | 43 | if (flags & F2FS_IMMUTABLE_FL) |
Zhang Zhen | 8abfb36 | 2014-04-15 14:19:38 +0800 | [diff] [blame] | 44 | new_fl |= S_IMMUTABLE; |
Chao Yu | 59c8440 | 2018-04-03 15:08:17 +0800 | [diff] [blame] | 45 | if (flags & F2FS_NOATIME_FL) |
Zhang Zhen | 8abfb36 | 2014-04-15 14:19:38 +0800 | [diff] [blame] | 46 | new_fl |= S_NOATIME; |
Chao Yu | 59c8440 | 2018-04-03 15:08:17 +0800 | [diff] [blame] | 47 | if (flags & F2FS_DIRSYNC_FL) |
Zhang Zhen | 8abfb36 | 2014-04-15 14:19:38 +0800 | [diff] [blame] | 48 | new_fl |= S_DIRSYNC; |
Eric Biggers | 2ee6a57 | 2017-10-09 12:15:35 -0700 | [diff] [blame] | 49 | if (f2fs_encrypted_inode(inode)) |
| 50 | new_fl |= S_ENCRYPTED; |
Zhang Zhen | 6a67885 | 2015-08-24 10:41:32 +0800 | [diff] [blame] | 51 | inode_set_flags(inode, new_fl, |
Eric Biggers | 2ee6a57 | 2017-10-09 12:15:35 -0700 | [diff] [blame] | 52 | S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC| |
| 53 | S_ENCRYPTED); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 54 | } |
| 55 | |
Jaegeuk Kim | 3d1e380 | 2013-10-08 18:01:51 +0900 | [diff] [blame] | 56 | static void __get_inode_rdev(struct inode *inode, struct f2fs_inode *ri) |
| 57 | { |
Chao Yu | 7a2af76 | 2017-07-19 00:19:06 +0800 | [diff] [blame] | 58 | int extra_size = get_extra_isize(inode); |
| 59 | |
Jaegeuk Kim | 3d1e380 | 2013-10-08 18:01:51 +0900 | [diff] [blame] | 60 | if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || |
| 61 | S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { |
Chao Yu | 7a2af76 | 2017-07-19 00:19:06 +0800 | [diff] [blame] | 62 | if (ri->i_addr[extra_size]) |
| 63 | inode->i_rdev = old_decode_dev( |
| 64 | le32_to_cpu(ri->i_addr[extra_size])); |
Jaegeuk Kim | 3d1e380 | 2013-10-08 18:01:51 +0900 | [diff] [blame] | 65 | else |
Chao Yu | 7a2af76 | 2017-07-19 00:19:06 +0800 | [diff] [blame] | 66 | inode->i_rdev = new_decode_dev( |
| 67 | le32_to_cpu(ri->i_addr[extra_size + 1])); |
Jaegeuk Kim | 3d1e380 | 2013-10-08 18:01:51 +0900 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
Chao Yu | e1da787 | 2018-06-05 17:44:11 +0800 | [diff] [blame] | 71 | static bool __written_first_block(struct f2fs_sb_info *sbi, |
| 72 | struct f2fs_inode *ri) |
Jaegeuk Kim | 3c6c2be | 2015-03-17 17:16:35 -0700 | [diff] [blame] | 73 | { |
Chao Yu | 7a2af76 | 2017-07-19 00:19:06 +0800 | [diff] [blame] | 74 | 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] | 75 | |
Chao Yu | e1da787 | 2018-06-05 17:44:11 +0800 | [diff] [blame] | 76 | if (is_valid_data_blkaddr(sbi, addr)) |
Jaegeuk Kim | 3c6c2be | 2015-03-17 17:16:35 -0700 | [diff] [blame] | 77 | return true; |
| 78 | return false; |
| 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++) { |
Jaegeuk Kim | fec1d65 | 2016-01-20 23:43:51 +0800 | [diff] [blame] | 107 | f2fs_wait_on_page_writeback(ipage, NODE, 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 | |
| 122 | if (!f2fs_sb_has_inode_chksum(sbi->sb)) |
| 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 | |
| 222 | if (f2fs_sb_has_flexible_inline_xattr(sbi->sb) |
| 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) && |
| 232 | !f2fs_sb_has_extra_attr(sbi->sb)) { |
| 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 | c9b6078 | 2018-08-01 19:13:44 +0800 | [diff] [blame] | 252 | if (F2FS_I(inode)->extent_tree) { |
| 253 | struct extent_info *ei = &F2FS_I(inode)->extent_tree->largest; |
| 254 | |
| 255 | if (ei->len && |
| 256 | (!f2fs_is_valid_blkaddr(sbi, ei->blk, DATA_GENERIC) || |
| 257 | !f2fs_is_valid_blkaddr(sbi, ei->blk + ei->len - 1, |
| 258 | DATA_GENERIC))) { |
| 259 | set_sbi_flag(sbi, SBI_NEED_FSCK); |
| 260 | f2fs_msg(sbi->sb, KERN_WARNING, |
| 261 | "%s: inode (ino=%lx) extent info [%u, %u, %u] " |
| 262 | "is incorrect, run fsck to fix", |
| 263 | __func__, inode->i_ino, |
| 264 | ei->blk, ei->fofs, ei->len); |
| 265 | return false; |
| 266 | } |
| 267 | } |
Jaegeuk Kim | 5d64600 | 2018-04-24 11:37:18 -0600 | [diff] [blame] | 268 | return true; |
| 269 | } |
| 270 | |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 271 | static int do_read_inode(struct inode *inode) |
| 272 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 273 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 274 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 275 | struct page *node_page; |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 276 | struct f2fs_inode *ri; |
Chao Yu | 5c57132 | 2017-07-26 00:01:41 +0800 | [diff] [blame] | 277 | projid_t i_projid; |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 278 | |
| 279 | /* Check if ino is within scope */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 280 | if (f2fs_check_nid_range(sbi, inode->i_ino)) |
Namjae Jeon | 064e082 | 2013-03-17 17:27:20 +0900 | [diff] [blame] | 281 | return -EINVAL; |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 282 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 283 | node_page = f2fs_get_node_page(sbi, inode->i_ino); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 284 | if (IS_ERR(node_page)) |
| 285 | return PTR_ERR(node_page); |
| 286 | |
Jaegeuk Kim | 58bfaf4 | 2013-12-26 16:30:41 +0900 | [diff] [blame] | 287 | ri = F2FS_INODE(node_page); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 288 | |
| 289 | inode->i_mode = le16_to_cpu(ri->i_mode); |
| 290 | i_uid_write(inode, le32_to_cpu(ri->i_uid)); |
| 291 | i_gid_write(inode, le32_to_cpu(ri->i_gid)); |
| 292 | set_nlink(inode, le32_to_cpu(ri->i_links)); |
| 293 | inode->i_size = le64_to_cpu(ri->i_size); |
Chao Yu | 000519f | 2017-07-06 01:11:31 +0800 | [diff] [blame] | 294 | 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] | 295 | |
| 296 | inode->i_atime.tv_sec = le64_to_cpu(ri->i_atime); |
| 297 | inode->i_ctime.tv_sec = le64_to_cpu(ri->i_ctime); |
| 298 | inode->i_mtime.tv_sec = le64_to_cpu(ri->i_mtime); |
| 299 | inode->i_atime.tv_nsec = le32_to_cpu(ri->i_atime_nsec); |
| 300 | inode->i_ctime.tv_nsec = le32_to_cpu(ri->i_ctime_nsec); |
| 301 | inode->i_mtime.tv_nsec = le32_to_cpu(ri->i_mtime_nsec); |
| 302 | inode->i_generation = le32_to_cpu(ri->i_generation); |
Chao Yu | 1c41e680 | 2018-05-07 20:28:52 +0800 | [diff] [blame] | 303 | if (S_ISDIR(inode->i_mode)) |
| 304 | fi->i_current_depth = le32_to_cpu(ri->i_current_depth); |
| 305 | else if (S_ISREG(inode->i_mode)) |
Chao Yu | 2ef79ec | 2018-05-07 20:28:54 +0800 | [diff] [blame] | 306 | fi->i_gc_failures[GC_FAILURE_PIN] = |
| 307 | le16_to_cpu(ri->i_gc_failures); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 308 | fi->i_xattr_nid = le32_to_cpu(ri->i_xattr_nid); |
| 309 | fi->i_flags = le32_to_cpu(ri->i_flags); |
| 310 | fi->flags = 0; |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 311 | fi->i_advise = ri->i_advise; |
Jaegeuk Kim | 6666e6a | 2012-12-10 17:52:48 +0900 | [diff] [blame] | 312 | fi->i_pino = le32_to_cpu(ri->i_pino); |
Jaegeuk Kim | 3843154 | 2014-02-27 18:20:00 +0900 | [diff] [blame] | 313 | fi->i_dir_level = ri->i_dir_level; |
Jaegeuk Kim | 3d1e380 | 2013-10-08 18:01:51 +0900 | [diff] [blame] | 314 | |
Jaegeuk Kim | ed3d125 | 2015-12-28 11:39:06 -0800 | [diff] [blame] | 315 | if (f2fs_init_extent_tree(inode, &ri->i_ext)) |
| 316 | set_page_dirty(node_page); |
Chao Yu | 0c872e2 | 2015-02-05 17:46:29 +0800 | [diff] [blame] | 317 | |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 318 | get_inline_info(inode, ri); |
Jaegeuk Kim | 3d1e380 | 2013-10-08 18:01:51 +0900 | [diff] [blame] | 319 | |
Chao Yu | 7a2af76 | 2017-07-19 00:19:06 +0800 | [diff] [blame] | 320 | fi->i_extra_isize = f2fs_has_extra_attr(inode) ? |
| 321 | le16_to_cpu(ri->i_extra_isize) : 0; |
| 322 | |
Chao Yu | 6afc662 | 2017-09-06 21:59:50 +0800 | [diff] [blame] | 323 | if (f2fs_sb_has_flexible_inline_xattr(sbi->sb)) { |
Chao Yu | 6afc662 | 2017-09-06 21:59:50 +0800 | [diff] [blame] | 324 | fi->i_inline_xattr_size = le16_to_cpu(ri->i_inline_xattr_size); |
| 325 | } else if (f2fs_has_inline_xattr(inode) || |
| 326 | f2fs_has_inline_dentry(inode)) { |
| 327 | fi->i_inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS; |
| 328 | } else { |
| 329 | |
| 330 | /* |
| 331 | * Previous inline data or directory always reserved 200 bytes |
| 332 | * in inode layout, even if inline_xattr is disabled. In order |
| 333 | * to keep inline_dentry's structure for backward compatibility, |
| 334 | * we get the space back only from inline_data. |
| 335 | */ |
| 336 | fi->i_inline_xattr_size = 0; |
| 337 | } |
| 338 | |
Chao Yu | 18dd647 | 2018-07-08 22:16:55 +0800 | [diff] [blame^] | 339 | if (!sanity_check_inode(inode, node_page)) { |
| 340 | f2fs_put_page(node_page, 1); |
| 341 | return -EINVAL; |
| 342 | } |
| 343 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 344 | /* check data exist */ |
| 345 | if (f2fs_has_inline_data(inode) && !f2fs_exist_data(inode)) |
Chao Yu | 9e5ba77 | 2015-01-06 14:28:43 +0800 | [diff] [blame] | 346 | __recover_inline_status(inode, node_page); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 347 | |
Jaegeuk Kim | 3d1e380 | 2013-10-08 18:01:51 +0900 | [diff] [blame] | 348 | /* get rdev by using inline_info */ |
| 349 | __get_inode_rdev(inode, ri); |
| 350 | |
Chao Yu | e1da787 | 2018-06-05 17:44:11 +0800 | [diff] [blame] | 351 | if (__written_first_block(sbi, ri)) |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 352 | set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN); |
Jaegeuk Kim | 3c6c2be | 2015-03-17 17:16:35 -0700 | [diff] [blame] | 353 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 354 | if (!f2fs_need_inode_block_update(sbi, inode->i_ino)) |
Jaegeuk Kim | 26de9b1 | 2016-05-20 20:42:37 -0700 | [diff] [blame] | 355 | fi->last_disk_size = inode->i_size; |
| 356 | |
Chao Yu | 59c8440 | 2018-04-03 15:08:17 +0800 | [diff] [blame] | 357 | if (fi->i_flags & F2FS_PROJINHERIT_FL) |
Chao Yu | 5c57132 | 2017-07-26 00:01:41 +0800 | [diff] [blame] | 358 | set_inode_flag(inode, FI_PROJ_INHERIT); |
| 359 | |
| 360 | if (f2fs_has_extra_attr(inode) && f2fs_sb_has_project_quota(sbi->sb) && |
| 361 | F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_projid)) |
| 362 | i_projid = (projid_t)le32_to_cpu(ri->i_projid); |
| 363 | else |
| 364 | i_projid = F2FS_DEF_PROJID; |
| 365 | fi->i_projid = make_kprojid(&init_user_ns, i_projid); |
| 366 | |
Chao Yu | 1c1d35d | 2018-01-25 14:54:42 +0800 | [diff] [blame] | 367 | if (f2fs_has_extra_attr(inode) && f2fs_sb_has_inode_crtime(sbi->sb) && |
| 368 | F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) { |
| 369 | fi->i_crtime.tv_sec = le64_to_cpu(ri->i_crtime); |
| 370 | fi->i_crtime.tv_nsec = le32_to_cpu(ri->i_crtime_nsec); |
| 371 | } |
| 372 | |
Arnd Bergmann | 24b81df | 2018-06-20 10:02:19 +0200 | [diff] [blame] | 373 | F2FS_I(inode)->i_disk_time[0] = inode->i_atime; |
| 374 | F2FS_I(inode)->i_disk_time[1] = inode->i_ctime; |
| 375 | F2FS_I(inode)->i_disk_time[2] = inode->i_mtime; |
Jaegeuk Kim | 214c246 | 2018-03-29 22:50:41 -0700 | [diff] [blame] | 376 | F2FS_I(inode)->i_disk_time[3] = F2FS_I(inode)->i_crtime; |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 377 | f2fs_put_page(node_page, 1); |
Jaegeuk Kim | 9d1015d | 2014-12-05 10:51:50 -0800 | [diff] [blame] | 378 | |
Chao Yu | d5e8f6c | 2015-07-15 17:28:53 +0800 | [diff] [blame] | 379 | stat_inc_inline_xattr(inode); |
Jaegeuk Kim | 9d1015d | 2014-12-05 10:51:50 -0800 | [diff] [blame] | 380 | stat_inc_inline_inode(inode); |
| 381 | stat_inc_inline_dir(inode); |
| 382 | |
Chao Yu | 9e5ba77 | 2015-01-06 14:28:43 +0800 | [diff] [blame] | 383 | return 0; |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | struct inode *f2fs_iget(struct super_block *sb, unsigned long ino) |
| 387 | { |
| 388 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
| 389 | struct inode *inode; |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 390 | int ret = 0; |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 391 | |
| 392 | inode = iget_locked(sb, ino); |
| 393 | if (!inode) |
| 394 | return ERR_PTR(-ENOMEM); |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 395 | |
| 396 | if (!(inode->i_state & I_NEW)) { |
| 397 | trace_f2fs_iget(inode); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 398 | return inode; |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 399 | } |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 400 | if (ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi)) |
| 401 | goto make_now; |
| 402 | |
| 403 | ret = do_read_inode(inode); |
| 404 | if (ret) |
| 405 | goto bad_inode; |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 406 | make_now: |
| 407 | if (ino == F2FS_NODE_INO(sbi)) { |
| 408 | inode->i_mapping->a_ops = &f2fs_node_aops; |
Chao Yu | 81114ba | 2018-04-09 20:25:06 +0800 | [diff] [blame] | 409 | mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 410 | } else if (ino == F2FS_META_INO(sbi)) { |
| 411 | inode->i_mapping->a_ops = &f2fs_meta_aops; |
Chao Yu | 81114ba | 2018-04-09 20:25:06 +0800 | [diff] [blame] | 412 | mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 413 | } else if (S_ISREG(inode->i_mode)) { |
| 414 | inode->i_op = &f2fs_file_inode_operations; |
| 415 | inode->i_fop = &f2fs_file_operations; |
| 416 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
| 417 | } else if (S_ISDIR(inode->i_mode)) { |
| 418 | inode->i_op = &f2fs_dir_inode_operations; |
| 419 | inode->i_fop = &f2fs_dir_operations; |
| 420 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
Yunlong Song | bdbc90f | 2018-02-28 20:31:52 +0800 | [diff] [blame] | 421 | inode_nohighmem(inode); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 422 | } else if (S_ISLNK(inode->i_mode)) { |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 423 | if (f2fs_encrypted_inode(inode)) |
| 424 | inode->i_op = &f2fs_encrypted_symlink_inode_operations; |
| 425 | else |
| 426 | inode->i_op = &f2fs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 427 | inode_nohighmem(inode); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 428 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
| 429 | } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || |
| 430 | S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { |
| 431 | inode->i_op = &f2fs_special_inode_operations; |
| 432 | init_special_inode(inode, inode->i_mode, inode->i_rdev); |
| 433 | } else { |
| 434 | ret = -EIO; |
| 435 | goto bad_inode; |
| 436 | } |
Jaegeuk Kim | 9360712 | 2017-05-16 13:20:16 -0700 | [diff] [blame] | 437 | f2fs_set_inode_flags(inode); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 438 | unlock_new_inode(inode); |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 439 | trace_f2fs_iget(inode); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 440 | return inode; |
| 441 | |
| 442 | bad_inode: |
| 443 | iget_failed(inode); |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 444 | trace_f2fs_iget_exit(inode, ret); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 445 | return ERR_PTR(ret); |
| 446 | } |
| 447 | |
Jaegeuk Kim | e8ea9b3 | 2016-09-09 16:59:39 -0700 | [diff] [blame] | 448 | struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino) |
| 449 | { |
| 450 | struct inode *inode; |
| 451 | retry: |
| 452 | inode = f2fs_iget(sb, ino); |
| 453 | if (IS_ERR(inode)) { |
| 454 | if (PTR_ERR(inode) == -ENOMEM) { |
| 455 | congestion_wait(BLK_RW_ASYNC, HZ/50); |
| 456 | goto retry; |
| 457 | } |
| 458 | } |
| 459 | return inode; |
| 460 | } |
| 461 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 462 | void f2fs_update_inode(struct inode *inode, struct page *node_page) |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 463 | { |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 464 | struct f2fs_inode *ri; |
Chao Yu | b691d98 | 2016-10-11 22:57:05 +0800 | [diff] [blame] | 465 | struct extent_tree *et = F2FS_I(inode)->extent_tree; |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 466 | |
Jaegeuk Kim | fec1d65 | 2016-01-20 23:43:51 +0800 | [diff] [blame] | 467 | f2fs_wait_on_page_writeback(node_page, NODE, true); |
Yunlei He | 211a6fa | 2017-12-05 12:07:47 +0800 | [diff] [blame] | 468 | set_page_dirty(node_page); |
| 469 | |
| 470 | f2fs_inode_synced(inode); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 471 | |
Jaegeuk Kim | 58bfaf4 | 2013-12-26 16:30:41 +0900 | [diff] [blame] | 472 | ri = F2FS_INODE(node_page); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 473 | |
| 474 | ri->i_mode = cpu_to_le16(inode->i_mode); |
| 475 | ri->i_advise = F2FS_I(inode)->i_advise; |
| 476 | ri->i_uid = cpu_to_le32(i_uid_read(inode)); |
| 477 | ri->i_gid = cpu_to_le32(i_gid_read(inode)); |
| 478 | ri->i_links = cpu_to_le32(inode->i_nlink); |
| 479 | ri->i_size = cpu_to_le64(i_size_read(inode)); |
Chao Yu | 000519f | 2017-07-06 01:11:31 +0800 | [diff] [blame] | 480 | 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] | 481 | |
Chao Yu | b691d98 | 2016-10-11 22:57:05 +0800 | [diff] [blame] | 482 | if (et) { |
| 483 | read_lock(&et->lock); |
| 484 | set_raw_extent(&et->largest, &ri->i_ext); |
| 485 | read_unlock(&et->lock); |
| 486 | } else { |
Jaegeuk Kim | 3e72f72 | 2015-06-19 17:53:26 -0700 | [diff] [blame] | 487 | memset(&ri->i_ext, 0, sizeof(ri->i_ext)); |
Chao Yu | b691d98 | 2016-10-11 22:57:05 +0800 | [diff] [blame] | 488 | } |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 489 | set_raw_inline(inode, ri); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 490 | |
| 491 | ri->i_atime = cpu_to_le64(inode->i_atime.tv_sec); |
| 492 | ri->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec); |
| 493 | ri->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec); |
| 494 | ri->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec); |
| 495 | ri->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); |
| 496 | ri->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec); |
Chao Yu | 1c41e680 | 2018-05-07 20:28:52 +0800 | [diff] [blame] | 497 | if (S_ISDIR(inode->i_mode)) |
| 498 | ri->i_current_depth = |
| 499 | cpu_to_le32(F2FS_I(inode)->i_current_depth); |
| 500 | else if (S_ISREG(inode->i_mode)) |
Chao Yu | 2ef79ec | 2018-05-07 20:28:54 +0800 | [diff] [blame] | 501 | ri->i_gc_failures = |
| 502 | cpu_to_le16(F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN]); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 503 | ri->i_xattr_nid = cpu_to_le32(F2FS_I(inode)->i_xattr_nid); |
| 504 | ri->i_flags = cpu_to_le32(F2FS_I(inode)->i_flags); |
Jaegeuk Kim | 6666e6a | 2012-12-10 17:52:48 +0900 | [diff] [blame] | 505 | ri->i_pino = cpu_to_le32(F2FS_I(inode)->i_pino); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 506 | ri->i_generation = cpu_to_le32(inode->i_generation); |
Jaegeuk Kim | 3843154 | 2014-02-27 18:20:00 +0900 | [diff] [blame] | 507 | ri->i_dir_level = F2FS_I(inode)->i_dir_level; |
Changman Lee | 7d79e75 | 2013-01-23 09:40:23 +0900 | [diff] [blame] | 508 | |
Chao Yu | 5c57132 | 2017-07-26 00:01:41 +0800 | [diff] [blame] | 509 | if (f2fs_has_extra_attr(inode)) { |
Chao Yu | 7a2af76 | 2017-07-19 00:19:06 +0800 | [diff] [blame] | 510 | ri->i_extra_isize = cpu_to_le16(F2FS_I(inode)->i_extra_isize); |
| 511 | |
Chao Yu | 6afc662 | 2017-09-06 21:59:50 +0800 | [diff] [blame] | 512 | if (f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(inode)->sb)) |
| 513 | ri->i_inline_xattr_size = |
| 514 | cpu_to_le16(F2FS_I(inode)->i_inline_xattr_size); |
| 515 | |
Chao Yu | 5c57132 | 2017-07-26 00:01:41 +0800 | [diff] [blame] | 516 | if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)->sb) && |
| 517 | F2FS_FITS_IN_INODE(ri, F2FS_I(inode)->i_extra_isize, |
| 518 | i_projid)) { |
| 519 | projid_t i_projid; |
| 520 | |
| 521 | i_projid = from_kprojid(&init_user_ns, |
| 522 | F2FS_I(inode)->i_projid); |
| 523 | ri->i_projid = cpu_to_le32(i_projid); |
| 524 | } |
Chao Yu | 1c1d35d | 2018-01-25 14:54:42 +0800 | [diff] [blame] | 525 | |
| 526 | if (f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)->sb) && |
| 527 | F2FS_FITS_IN_INODE(ri, F2FS_I(inode)->i_extra_isize, |
| 528 | i_crtime)) { |
| 529 | ri->i_crtime = |
| 530 | cpu_to_le64(F2FS_I(inode)->i_crtime.tv_sec); |
| 531 | ri->i_crtime_nsec = |
| 532 | cpu_to_le32(F2FS_I(inode)->i_crtime.tv_nsec); |
| 533 | } |
Chao Yu | 5c57132 | 2017-07-26 00:01:41 +0800 | [diff] [blame] | 534 | } |
| 535 | |
Jaegeuk Kim | 3d1e380 | 2013-10-08 18:01:51 +0900 | [diff] [blame] | 536 | __set_inode_rdev(inode, ri); |
Jaegeuk Kim | 12719ae | 2016-01-07 13:23:12 -0800 | [diff] [blame] | 537 | |
Jaegeuk Kim | 2049d4f | 2016-01-25 05:57:05 -0800 | [diff] [blame] | 538 | /* deleted inode */ |
| 539 | if (inode->i_nlink == 0) |
| 540 | clear_inline_node(node_page); |
| 541 | |
Arnd Bergmann | 24b81df | 2018-06-20 10:02:19 +0200 | [diff] [blame] | 542 | F2FS_I(inode)->i_disk_time[0] = inode->i_atime; |
| 543 | F2FS_I(inode)->i_disk_time[1] = inode->i_ctime; |
| 544 | F2FS_I(inode)->i_disk_time[2] = inode->i_mtime; |
Jaegeuk Kim | 214c246 | 2018-03-29 22:50:41 -0700 | [diff] [blame] | 545 | F2FS_I(inode)->i_disk_time[3] = F2FS_I(inode)->i_crtime; |
Weichao Guo | 54c55c4 | 2018-03-09 23:10:21 +0800 | [diff] [blame] | 546 | |
| 547 | #ifdef CONFIG_F2FS_CHECK_FS |
| 548 | f2fs_inode_chksum_set(F2FS_I_SB(inode), node_page); |
| 549 | #endif |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 550 | } |
| 551 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 552 | void f2fs_update_inode_page(struct inode *inode) |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 553 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 554 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 555 | struct page *node_page; |
Jaegeuk Kim | 744602c | 2014-01-24 09:42:16 +0900 | [diff] [blame] | 556 | retry: |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 557 | node_page = f2fs_get_node_page(sbi, inode->i_ino); |
Jaegeuk Kim | 744602c | 2014-01-24 09:42:16 +0900 | [diff] [blame] | 558 | if (IS_ERR(node_page)) { |
| 559 | int err = PTR_ERR(node_page); |
| 560 | if (err == -ENOMEM) { |
| 561 | cond_resched(); |
| 562 | goto retry; |
| 563 | } else if (err != -ENOENT) { |
Jaegeuk Kim | 38f91ca | 2016-05-18 14:07:56 -0700 | [diff] [blame] | 564 | f2fs_stop_checkpoint(sbi, false); |
Jaegeuk Kim | 744602c | 2014-01-24 09:42:16 +0900 | [diff] [blame] | 565 | } |
Yunlei He | 211a6fa | 2017-12-05 12:07:47 +0800 | [diff] [blame] | 566 | return; |
Jaegeuk Kim | 744602c | 2014-01-24 09:42:16 +0900 | [diff] [blame] | 567 | } |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 568 | f2fs_update_inode(inode, node_page); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 569 | f2fs_put_page(node_page, 1); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc) |
| 573 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 574 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 575 | |
| 576 | if (inode->i_ino == F2FS_NODE_INO(sbi) || |
| 577 | inode->i_ino == F2FS_META_INO(sbi)) |
| 578 | return 0; |
| 579 | |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 580 | if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) |
Jaegeuk Kim | b378387 | 2013-06-10 09:17:01 +0900 | [diff] [blame] | 581 | return 0; |
| 582 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 583 | /* |
Jaegeuk Kim | c5cd29d | 2015-09-12 11:25:30 -0700 | [diff] [blame] | 584 | * 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] | 585 | * during the urgent cleaning time when runing out of free sections. |
| 586 | */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 587 | f2fs_update_inode_page(inode); |
Jaegeuk Kim | a788189 | 2017-04-20 13:51:57 -0700 | [diff] [blame] | 588 | if (wbc && wbc->nr_to_write) |
Jaegeuk Kim | 2c4db1a | 2016-01-07 14:15:04 -0800 | [diff] [blame] | 589 | f2fs_balance_fs(sbi, true); |
Jaegeuk Kim | 744602c | 2014-01-24 09:42:16 +0900 | [diff] [blame] | 590 | return 0; |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 591 | } |
| 592 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 593 | /* |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 594 | * Called at the last iput() if i_nlink is zero |
| 595 | */ |
| 596 | void f2fs_evict_inode(struct inode *inode) |
| 597 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 598 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 599 | nid_t xnid = F2FS_I(inode)->i_xattr_nid; |
Chao Yu | 13ec729 | 2015-08-24 17:40:45 +0800 | [diff] [blame] | 600 | int err = 0; |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 601 | |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 602 | /* some remained atomic pages should discarded */ |
Jaegeuk Kim | 1e84371 | 2014-12-09 06:08:59 -0800 | [diff] [blame] | 603 | if (f2fs_is_atomic_file(inode)) |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 604 | f2fs_drop_inmem_pages(inode); |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 605 | |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 606 | trace_f2fs_evict_inode(inode); |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 607 | truncate_inode_pages_final(&inode->i_data); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 608 | |
| 609 | if (inode->i_ino == F2FS_NODE_INO(sbi) || |
| 610 | inode->i_ino == F2FS_META_INO(sbi)) |
Chao Yu | dbf20cb | 2014-07-25 12:00:57 +0800 | [diff] [blame] | 611 | goto out_clear; |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 612 | |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 613 | f2fs_bug_on(sbi, get_dirty_pages(inode)); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 614 | f2fs_remove_dirty_inode(inode); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 615 | |
Jaegeuk Kim | 3e72f72 | 2015-06-19 17:53:26 -0700 | [diff] [blame] | 616 | f2fs_destroy_extent_tree(inode); |
| 617 | |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 618 | if (inode->i_nlink || is_bad_inode(inode)) |
| 619 | goto no_delete; |
| 620 | |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 621 | dquot_initialize(inode); |
| 622 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 623 | f2fs_remove_ino_entry(sbi, inode->i_ino, APPEND_INO); |
| 624 | f2fs_remove_ino_entry(sbi, inode->i_ino, UPDATE_INO); |
| 625 | f2fs_remove_ino_entry(sbi, inode->i_ino, FLUSH_INO); |
Chao Yu | 60dcedc | 2016-11-02 20:43:21 +0800 | [diff] [blame] | 626 | |
Changman Lee | d6212a5 | 2013-01-29 18:30:07 +0900 | [diff] [blame] | 627 | sb_start_intwrite(inode->i_sb); |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 628 | set_inode_flag(inode, FI_NO_ALLOC); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 629 | i_size_write(inode, 0); |
Jaegeuk Kim | 4c0c294 | 2016-05-03 09:22:18 -0700 | [diff] [blame] | 630 | retry: |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 631 | if (F2FS_HAS_BLOCKS(inode)) |
Jaegeuk Kim | 9a449e9 | 2016-06-02 13:49:38 -0700 | [diff] [blame] | 632 | err = f2fs_truncate(inode); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 633 | |
Jaegeuk Kim | 8c1b3c0 | 2017-03-07 13:32:20 -0800 | [diff] [blame] | 634 | #ifdef CONFIG_F2FS_FAULT_INJECTION |
| 635 | if (time_to_inject(sbi, FAULT_EVICT_INODE)) { |
| 636 | f2fs_show_injection_info(FAULT_EVICT_INODE); |
| 637 | err = -EIO; |
| 638 | } |
| 639 | #endif |
Chao Yu | 13ec729 | 2015-08-24 17:40:45 +0800 | [diff] [blame] | 640 | if (!err) { |
| 641 | f2fs_lock_op(sbi); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 642 | err = f2fs_remove_inode_page(inode); |
Chao Yu | 13ec729 | 2015-08-24 17:40:45 +0800 | [diff] [blame] | 643 | f2fs_unlock_op(sbi); |
Chao Yu | a11b9f6 | 2016-10-11 22:56:59 +0800 | [diff] [blame] | 644 | if (err == -ENOENT) |
| 645 | err = 0; |
Chao Yu | 13ec729 | 2015-08-24 17:40:45 +0800 | [diff] [blame] | 646 | } |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 647 | |
Jaegeuk Kim | 4c0c294 | 2016-05-03 09:22:18 -0700 | [diff] [blame] | 648 | /* give more chances, if ENOMEM case */ |
| 649 | if (err == -ENOMEM) { |
| 650 | err = 0; |
| 651 | goto retry; |
| 652 | } |
| 653 | |
Jaegeuk Kim | 0f18b46 | 2016-05-20 11:10:10 -0700 | [diff] [blame] | 654 | if (err) |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 655 | f2fs_update_inode_page(inode); |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 656 | dquot_free_inode(inode); |
Changman Lee | d6212a5 | 2013-01-29 18:30:07 +0900 | [diff] [blame] | 657 | sb_end_intwrite(inode->i_sb); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 658 | no_delete: |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 659 | dquot_drop(inode); |
| 660 | |
Chao Yu | d5e8f6c | 2015-07-15 17:28:53 +0800 | [diff] [blame] | 661 | stat_dec_inline_xattr(inode); |
Jaegeuk Kim | 3289c06 | 2014-10-13 20:00:16 -0700 | [diff] [blame] | 662 | stat_dec_inline_dir(inode); |
Jaegeuk Kim | e7a2bf2 | 2014-10-14 10:29:50 -0700 | [diff] [blame] | 663 | stat_dec_inline_inode(inode); |
Chao Yu | 0bdee48 | 2015-03-19 19:27:51 +0800 | [diff] [blame] | 664 | |
Jaegeuk Kim | 943973c | 2017-10-12 19:12:53 -0700 | [diff] [blame] | 665 | if (likely(!is_set_ckpt_flags(sbi, CP_ERROR_FLAG))) |
Chao Yu | ca7d802 | 2017-09-12 14:04:05 +0800 | [diff] [blame] | 666 | f2fs_bug_on(sbi, is_inode_flag_set(inode, FI_DIRTY_INODE)); |
Jaegeuk Kim | 943973c | 2017-10-12 19:12:53 -0700 | [diff] [blame] | 667 | else |
| 668 | f2fs_inode_synced(inode); |
Chao Yu | ca7d802 | 2017-09-12 14:04:05 +0800 | [diff] [blame] | 669 | |
Jaegeuk Kim | 4f29544 | 2017-03-04 13:56:10 -0800 | [diff] [blame] | 670 | /* ino == 0, if f2fs_new_inode() was failed t*/ |
| 671 | if (inode->i_ino) |
| 672 | invalidate_mapping_pages(NODE_MAPPING(sbi), inode->i_ino, |
| 673 | inode->i_ino); |
Chao Yu | 002a41c | 2014-08-04 09:54:58 +0800 | [diff] [blame] | 674 | if (xnid) |
| 675 | invalidate_mapping_pages(NODE_MAPPING(sbi), xnid, xnid); |
Chao Yu | 60dcedc | 2016-11-02 20:43:21 +0800 | [diff] [blame] | 676 | if (inode->i_nlink) { |
| 677 | if (is_inode_flag_set(inode, FI_APPEND_WRITE)) |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 678 | f2fs_add_ino_entry(sbi, inode->i_ino, APPEND_INO); |
Chao Yu | 60dcedc | 2016-11-02 20:43:21 +0800 | [diff] [blame] | 679 | if (is_inode_flag_set(inode, FI_UPDATE_WRITE)) |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 680 | f2fs_add_ino_entry(sbi, inode->i_ino, UPDATE_INO); |
Chao Yu | 60dcedc | 2016-11-02 20:43:21 +0800 | [diff] [blame] | 681 | } |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 682 | if (is_inode_flag_set(inode, FI_FREE_NID)) { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 683 | f2fs_alloc_nid_failed(sbi, inode->i_ino); |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 684 | clear_inode_flag(inode, FI_FREE_NID); |
Jaegeuk Kim | d8c4256 | 2017-06-01 15:39:27 -0700 | [diff] [blame] | 685 | } else { |
Jaegeuk Kim | a4f843b | 2018-04-23 23:02:31 -0600 | [diff] [blame] | 686 | /* |
| 687 | * If xattr nid is corrupted, we can reach out error condition, |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 688 | * err & !f2fs_exist_written_data(sbi, inode->i_ino, ORPHAN_INO)). |
| 689 | * 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] | 690 | */ |
Jaegeuk Kim | c9b63bd | 2015-06-23 10:36:08 -0700 | [diff] [blame] | 691 | } |
Chao Yu | dbf20cb | 2014-07-25 12:00:57 +0800 | [diff] [blame] | 692 | out_clear: |
Eric Biggers | 3d204e2 | 2018-01-11 23:30:13 -0500 | [diff] [blame] | 693 | fscrypt_put_encryption_info(inode); |
Chao Yu | dbf20cb | 2014-07-25 12:00:57 +0800 | [diff] [blame] | 694 | clear_inode(inode); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 695 | } |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 696 | |
| 697 | /* caller should call f2fs_lock_op() */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 698 | void f2fs_handle_failed_inode(struct inode *inode) |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 699 | { |
| 700 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 221149c | 2016-05-02 12:34:48 -0700 | [diff] [blame] | 701 | struct node_info ni; |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 702 | |
Chao Yu | a11b9f6 | 2016-10-11 22:56:59 +0800 | [diff] [blame] | 703 | /* |
| 704 | * clear nlink of inode in order to release resource of inode |
| 705 | * immediately. |
| 706 | */ |
| 707 | clear_nlink(inode); |
| 708 | |
| 709 | /* |
| 710 | * we must call this to avoid inode being remained as dirty, resulting |
| 711 | * in a panic when flushing dirty inodes in gdirty_list. |
| 712 | */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 713 | f2fs_update_inode_page(inode); |
Jaegeuk Kim | 9bb02c3 | 2017-04-11 19:01:26 -0700 | [diff] [blame] | 714 | f2fs_inode_synced(inode); |
Chao Yu | a11b9f6 | 2016-10-11 22:56:59 +0800 | [diff] [blame] | 715 | |
Jaegeuk Kim | 221149c | 2016-05-02 12:34:48 -0700 | [diff] [blame] | 716 | /* don't make bad inode, since it becomes a regular file. */ |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 717 | unlock_new_inode(inode); |
| 718 | |
Chao Yu | 13ec729 | 2015-08-24 17:40:45 +0800 | [diff] [blame] | 719 | /* |
Chao Yu | 13ec729 | 2015-08-24 17:40:45 +0800 | [diff] [blame] | 720 | * Note: we should add inode to orphan list before f2fs_unlock_op() |
| 721 | * so we can prevent losing this orphan when encoutering checkpoint |
| 722 | * and following suddenly power-off. |
| 723 | */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 724 | f2fs_get_node_info(sbi, inode->i_ino, &ni); |
Jaegeuk Kim | 221149c | 2016-05-02 12:34:48 -0700 | [diff] [blame] | 725 | |
| 726 | if (ni.blk_addr != NULL_ADDR) { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 727 | int err = f2fs_acquire_orphan_inode(sbi); |
Jaegeuk Kim | 221149c | 2016-05-02 12:34:48 -0700 | [diff] [blame] | 728 | if (err) { |
| 729 | set_sbi_flag(sbi, SBI_NEED_FSCK); |
| 730 | f2fs_msg(sbi->sb, KERN_WARNING, |
| 731 | "Too many orphan inodes, run fsck to fix."); |
| 732 | } else { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 733 | f2fs_add_orphan_inode(inode); |
Jaegeuk Kim | 221149c | 2016-05-02 12:34:48 -0700 | [diff] [blame] | 734 | } |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 735 | f2fs_alloc_nid_done(sbi, inode->i_ino); |
Jaegeuk Kim | 221149c | 2016-05-02 12:34:48 -0700 | [diff] [blame] | 736 | } else { |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 737 | set_inode_flag(inode, FI_FREE_NID); |
Chao Yu | 13ec729 | 2015-08-24 17:40:45 +0800 | [diff] [blame] | 738 | } |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 739 | |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 740 | f2fs_unlock_op(sbi); |
| 741 | |
| 742 | /* iput will drop the inode object */ |
| 743 | iput(inode); |
| 744 | } |