blob: a33d7a849b2df8efd97d4684017b770070ed2858 [file] [log] [blame]
Chao Yu6d1a8322018-09-12 09:16:07 +08001// SPDX-License-Identifier: GPL-2.0
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002/*
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +09003 * fs/f2fs/inode.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +09007 */
8#include <linux/fs.h>
9#include <linux/f2fs_fs.h>
10#include <linux/buffer_head.h>
Jaegeuk Kime8ea9b32016-09-09 16:59:39 -070011#include <linux/backing-dev.h>
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +090012#include <linux/writeback.h>
13
14#include "f2fs.h"
15#include "node.h"
Chao Yu56a0d932017-06-14 23:00:56 +080016#include "segment.h"
Chao Yua9e0ca82019-03-04 17:19:04 +080017#include "xattr.h"
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +090018
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090019#include <trace/events/f2fs.h>
20
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070021void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync)
Jaegeuk Kimb56ab832016-06-30 19:09:37 -070022{
Daeho Jeong9ef5e652018-01-11 11:26:19 +090023 if (is_inode_flag_set(inode, FI_NEW_INODE))
24 return;
25
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070026 if (f2fs_inode_dirtied(inode, sync))
Jaegeuk Kimb56ab832016-06-30 19:09:37 -070027 return;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070028
Jaegeuk Kimb56ab832016-06-30 19:09:37 -070029 mark_inode_dirty_sync(inode);
30}
31
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +090032void f2fs_set_inode_flags(struct inode *inode)
33{
34 unsigned int flags = F2FS_I(inode)->i_flags;
Zhang Zhen8abfb362014-04-15 14:19:38 +080035 unsigned int new_fl = 0;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +090036
Chao Yu9bafde62018-04-03 15:08:17 +080037 if (flags & F2FS_SYNC_FL)
Zhang Zhen8abfb362014-04-15 14:19:38 +080038 new_fl |= S_SYNC;
Chao Yu9bafde62018-04-03 15:08:17 +080039 if (flags & F2FS_APPEND_FL)
Zhang Zhen8abfb362014-04-15 14:19:38 +080040 new_fl |= S_APPEND;
Chao Yu9bafde62018-04-03 15:08:17 +080041 if (flags & F2FS_IMMUTABLE_FL)
Zhang Zhen8abfb362014-04-15 14:19:38 +080042 new_fl |= S_IMMUTABLE;
Chao Yu9bafde62018-04-03 15:08:17 +080043 if (flags & F2FS_NOATIME_FL)
Zhang Zhen8abfb362014-04-15 14:19:38 +080044 new_fl |= S_NOATIME;
Chao Yu9bafde62018-04-03 15:08:17 +080045 if (flags & F2FS_DIRSYNC_FL)
Zhang Zhen8abfb362014-04-15 14:19:38 +080046 new_fl |= S_DIRSYNC;
Chandan Rajendrab62c4582018-12-12 15:20:11 +053047 if (file_is_encrypt(inode))
Eric Biggers685285b2017-10-09 12:15:35 -070048 new_fl |= S_ENCRYPTED;
Zhang Zhen6a678852015-08-24 10:41:32 +080049 inode_set_flags(inode, new_fl,
Eric Biggers685285b2017-10-09 12:15:35 -070050 S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|
51 S_ENCRYPTED);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +090052}
53
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090054static void __get_inode_rdev(struct inode *inode, struct f2fs_inode *ri)
55{
Chao Yufbcf9312017-07-19 00:19:06 +080056 int extra_size = get_extra_isize(inode);
57
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090058 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
59 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
Chao Yufbcf9312017-07-19 00:19:06 +080060 if (ri->i_addr[extra_size])
61 inode->i_rdev = old_decode_dev(
62 le32_to_cpu(ri->i_addr[extra_size]));
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090063 else
Chao Yufbcf9312017-07-19 00:19:06 +080064 inode->i_rdev = new_decode_dev(
65 le32_to_cpu(ri->i_addr[extra_size + 1]));
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090066 }
67}
68
Chao Yu10d67032018-07-10 23:01:45 +080069static int __written_first_block(struct f2fs_sb_info *sbi,
Chao Yud05e8712018-06-05 17:44:11 +080070 struct f2fs_inode *ri)
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -070071{
Chao Yufbcf9312017-07-19 00:19:06 +080072 block_t addr = le32_to_cpu(ri->i_addr[offset_in_addr(ri)]);
Jaegeuk Kimadad81e2015-03-24 12:04:20 -070073
Chao Yu10d67032018-07-10 23:01:45 +080074 if (!__is_valid_data_blkaddr(addr))
75 return 1;
Chao Yua9cc2e42019-04-15 15:26:32 +080076 if (!f2fs_is_valid_blkaddr(sbi, addr, DATA_GENERIC_ENHANCE))
Chao Yua6b5c262019-06-20 11:36:14 +080077 return -EFSCORRUPTED;
Chao Yu10d67032018-07-10 23:01:45 +080078 return 0;
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -070079}
80
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090081static void __set_inode_rdev(struct inode *inode, struct f2fs_inode *ri)
82{
Chao Yufbcf9312017-07-19 00:19:06 +080083 int extra_size = get_extra_isize(inode);
84
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090085 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
86 if (old_valid_dev(inode->i_rdev)) {
Chao Yufbcf9312017-07-19 00:19:06 +080087 ri->i_addr[extra_size] =
Chris Fries6c311ec2014-01-17 14:44:39 -060088 cpu_to_le32(old_encode_dev(inode->i_rdev));
Chao Yufbcf9312017-07-19 00:19:06 +080089 ri->i_addr[extra_size + 1] = 0;
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090090 } else {
Chao Yufbcf9312017-07-19 00:19:06 +080091 ri->i_addr[extra_size] = 0;
92 ri->i_addr[extra_size + 1] =
Chris Fries6c311ec2014-01-17 14:44:39 -060093 cpu_to_le32(new_encode_dev(inode->i_rdev));
Chao Yufbcf9312017-07-19 00:19:06 +080094 ri->i_addr[extra_size + 2] = 0;
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090095 }
96 }
97}
98
Chao Yu9e5ba772015-01-06 14:28:43 +080099static void __recover_inline_status(struct inode *inode, struct page *ipage)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700100{
Chao Yue84f88e2017-07-19 00:19:05 +0800101 void *inline_data = inline_data_addr(inode, ipage);
Chao Yu9e5ba772015-01-06 14:28:43 +0800102 __le32 *start = inline_data;
Chao Yue84f88e2017-07-19 00:19:05 +0800103 __le32 *end = start + MAX_INLINE_DATA(inode) / sizeof(__le32);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700104
Chao Yu9e5ba772015-01-06 14:28:43 +0800105 while (start < end) {
106 if (*start++) {
Chao Yud9b3df52018-12-25 17:43:42 +0800107 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700108
Jaegeuk Kim91942322016-05-20 10:13:22 -0700109 set_inode_flag(inode, FI_DATA_EXIST);
110 set_raw_inline(inode, F2FS_INODE(ipage));
Chao Yu9e5ba772015-01-06 14:28:43 +0800111 set_page_dirty(ipage);
112 return;
113 }
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700114 }
Chao Yu9e5ba772015-01-06 14:28:43 +0800115 return;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700116}
117
Chao Yu43101252017-07-31 20:19:09 +0800118static bool f2fs_enable_inode_chksum(struct f2fs_sb_info *sbi, struct page *page)
119{
120 struct f2fs_inode *ri = &F2FS_NODE(page)->i;
Chao Yu43101252017-07-31 20:19:09 +0800121
Chao Yu5b11d2b2018-10-24 18:34:26 +0800122 if (!f2fs_sb_has_inode_chksum(sbi))
Chao Yu43101252017-07-31 20:19:09 +0800123 return false;
124
Chao Yuae3825e2018-07-08 22:16:54 +0800125 if (!IS_INODE(page) || !(ri->i_inline & F2FS_EXTRA_ATTR))
Chao Yu43101252017-07-31 20:19:09 +0800126 return false;
127
Zhikang Zhang97d0c4e2018-04-14 01:02:34 +0800128 if (!F2FS_FITS_IN_INODE(ri, le16_to_cpu(ri->i_extra_isize),
129 i_inode_checksum))
Chao Yu43101252017-07-31 20:19:09 +0800130 return false;
131
132 return true;
133}
134
135static __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
158bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page)
159{
160 struct f2fs_inode *ri;
161 __u32 provided, calculated;
162
Jaegeuk Kimc92b7c72018-06-21 13:46:23 -0700163 if (unlikely(is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN)))
164 return true;
165
Weichao Guof7467672018-03-09 23:10:21 +0800166#ifdef CONFIG_F2FS_CHECK_FS
167 if (!f2fs_enable_inode_chksum(sbi, page))
168#else
Jaegeuk Kimc5497ec2017-08-31 16:54:51 -0700169 if (!f2fs_enable_inode_chksum(sbi, page) ||
170 PageDirty(page) || PageWriteback(page))
Weichao Guof7467672018-03-09 23:10:21 +0800171#endif
Chao Yu43101252017-07-31 20:19:09 +0800172 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)
Joe Perchesa0d68632019-06-18 17:48:42 +0800179 f2fs_warn(sbi, "checksum invalid, nid = %lu, ino_of_node = %x, %x vs. %x",
180 page->index, ino_of_node(page), provided, calculated);
Chao Yu43101252017-07-31 20:19:09 +0800181
182 return provided == calculated;
183}
184
185void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page)
186{
187 struct f2fs_inode *ri = &F2FS_NODE(page)->i;
188
189 if (!f2fs_enable_inode_chksum(sbi, page))
190 return;
191
192 ri->i_inode_checksum = cpu_to_le32(f2fs_inode_chksum(sbi, page));
193}
194
Chao Yu6db154a2018-06-29 13:55:22 +0800195static bool sanity_check_inode(struct inode *inode, struct page *node_page)
Jaegeuk Kim60143bf2018-04-24 11:37:18 -0600196{
197 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yuf9b48642018-07-08 22:16:55 +0800198 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu6db154a2018-06-29 13:55:22 +0800199 unsigned long long iblocks;
200
201 iblocks = le64_to_cpu(F2FS_INODE(node_page)->i_blocks);
202 if (!iblocks) {
203 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesa0d68632019-06-18 17:48:42 +0800204 f2fs_warn(sbi, "%s: corrupted inode i_blocks i_ino=%lx iblocks=%llu, run fsck to fix.",
205 __func__, inode->i_ino, iblocks);
Chao Yu6db154a2018-06-29 13:55:22 +0800206 return false;
207 }
208
209 if (ino_of_node(node_page) != nid_of_node(node_page)) {
210 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesa0d68632019-06-18 17:48:42 +0800211 f2fs_warn(sbi, "%s: corrupted inode footer i_ino=%lx, ino,nid: [%u, %u] run fsck to fix.",
212 __func__, inode->i_ino,
213 ino_of_node(node_page), nid_of_node(node_page));
Chao Yu6db154a2018-06-29 13:55:22 +0800214 return false;
215 }
Jaegeuk Kim60143bf2018-04-24 11:37:18 -0600216
Chao Yu5b11d2b2018-10-24 18:34:26 +0800217 if (f2fs_sb_has_flexible_inline_xattr(sbi)
Jaegeuk Kim60143bf2018-04-24 11:37:18 -0600218 && !f2fs_has_extra_attr(inode)) {
219 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesa0d68632019-06-18 17:48:42 +0800220 f2fs_warn(sbi, "%s: corrupted inode ino=%lx, run fsck to fix.",
221 __func__, inode->i_ino);
Jaegeuk Kim60143bf2018-04-24 11:37:18 -0600222 return false;
223 }
Chao Yu6a6c9352018-06-25 23:29:49 +0800224
225 if (f2fs_has_extra_attr(inode) &&
Chao Yu5b11d2b2018-10-24 18:34:26 +0800226 !f2fs_sb_has_extra_attr(sbi)) {
Chao Yu6a6c9352018-06-25 23:29:49 +0800227 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesa0d68632019-06-18 17:48:42 +0800228 f2fs_warn(sbi, "%s: inode (ino=%lx) is with extra_attr, but extra_attr feature is off",
229 __func__, inode->i_ino);
Chao Yu6a6c9352018-06-25 23:29:49 +0800230 return false;
231 }
Chao Yub3c869a2018-08-01 19:13:44 +0800232
Chao Yuf9b48642018-07-08 22:16:55 +0800233 if (fi->i_extra_isize > F2FS_TOTAL_EXTRA_ATTR_SIZE ||
234 fi->i_extra_isize % sizeof(__le32)) {
235 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesa0d68632019-06-18 17:48:42 +0800236 f2fs_warn(sbi, "%s: inode (ino=%lx) has corrupted i_extra_isize: %d, max: %zu",
237 __func__, inode->i_ino, fi->i_extra_isize,
238 F2FS_TOTAL_EXTRA_ATTR_SIZE);
Chao Yuf9b48642018-07-08 22:16:55 +0800239 return false;
240 }
241
Chao Yua9e0ca82019-03-04 17:19:04 +0800242 if (f2fs_has_extra_attr(inode) &&
243 f2fs_sb_has_flexible_inline_xattr(sbi) &&
244 f2fs_has_inline_xattr(inode) &&
245 (!fi->i_inline_xattr_size ||
246 fi->i_inline_xattr_size > MAX_INLINE_XATTR_SIZE)) {
247 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesa0d68632019-06-18 17:48:42 +0800248 f2fs_warn(sbi, "%s: inode (ino=%lx) has corrupted i_inline_xattr_size: %d, max: %zu",
249 __func__, inode->i_ino, fi->i_inline_xattr_size,
250 MAX_INLINE_XATTR_SIZE);
Chao Yua9e0ca82019-03-04 17:19:04 +0800251 return false;
252 }
253
Chao Yub3c869a2018-08-01 19:13:44 +0800254 if (F2FS_I(inode)->extent_tree) {
255 struct extent_info *ei = &F2FS_I(inode)->extent_tree->largest;
256
257 if (ei->len &&
Chao Yua9cc2e42019-04-15 15:26:32 +0800258 (!f2fs_is_valid_blkaddr(sbi, ei->blk,
259 DATA_GENERIC_ENHANCE) ||
Chao Yub3c869a2018-08-01 19:13:44 +0800260 !f2fs_is_valid_blkaddr(sbi, ei->blk + ei->len - 1,
Chao Yua9cc2e42019-04-15 15:26:32 +0800261 DATA_GENERIC_ENHANCE))) {
Chao Yub3c869a2018-08-01 19:13:44 +0800262 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesa0d68632019-06-18 17:48:42 +0800263 f2fs_warn(sbi, "%s: inode (ino=%lx) extent info [%u, %u, %u] is incorrect, run fsck to fix",
264 __func__, inode->i_ino,
265 ei->blk, ei->fofs, ei->len);
Chao Yub3c869a2018-08-01 19:13:44 +0800266 return false;
267 }
268 }
Chao Yu976de2d2018-06-29 00:19:25 +0800269
270 if (f2fs_has_inline_data(inode) &&
271 (!S_ISREG(inode->i_mode) && !S_ISLNK(inode->i_mode))) {
272 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesa0d68632019-06-18 17:48:42 +0800273 f2fs_warn(sbi, "%s: inode (ino=%lx, mode=%u) should not have inline_data, run fsck to fix",
274 __func__, inode->i_ino, inode->i_mode);
Chao Yu976de2d2018-06-29 00:19:25 +0800275 return false;
276 }
277
278 if (f2fs_has_inline_dentry(inode) && !S_ISDIR(inode->i_mode)) {
279 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesa0d68632019-06-18 17:48:42 +0800280 f2fs_warn(sbi, "%s: inode (ino=%lx, mode=%u) should not have inline_dentry, run fsck to fix",
281 __func__, inode->i_ino, inode->i_mode);
Chao Yu976de2d2018-06-29 00:19:25 +0800282 return false;
283 }
284
Jaegeuk Kim60143bf2018-04-24 11:37:18 -0600285 return true;
286}
287
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900288static int do_read_inode(struct inode *inode)
289{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700290 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900291 struct f2fs_inode_info *fi = F2FS_I(inode);
292 struct page *node_page;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900293 struct f2fs_inode *ri;
Chao Yu5647b302017-07-26 00:01:41 +0800294 projid_t i_projid;
Chao Yu10d67032018-07-10 23:01:45 +0800295 int err;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900296
297 /* Check if ino is within scope */
Chao Yu6b4d6a82018-05-30 00:20:41 +0800298 if (f2fs_check_nid_range(sbi, inode->i_ino))
Namjae Jeon064e0822013-03-17 17:27:20 +0900299 return -EINVAL;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900300
Chao Yu6b4d6a82018-05-30 00:20:41 +0800301 node_page = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900302 if (IS_ERR(node_page))
303 return PTR_ERR(node_page);
304
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900305 ri = F2FS_INODE(node_page);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900306
307 inode->i_mode = le16_to_cpu(ri->i_mode);
308 i_uid_write(inode, le32_to_cpu(ri->i_uid));
309 i_gid_write(inode, le32_to_cpu(ri->i_gid));
310 set_nlink(inode, le32_to_cpu(ri->i_links));
311 inode->i_size = le64_to_cpu(ri->i_size);
Chao Yud9bfbbb2017-07-06 01:11:31 +0800312 inode->i_blocks = SECTOR_FROM_BLOCK(le64_to_cpu(ri->i_blocks) - 1);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900313
314 inode->i_atime.tv_sec = le64_to_cpu(ri->i_atime);
315 inode->i_ctime.tv_sec = le64_to_cpu(ri->i_ctime);
316 inode->i_mtime.tv_sec = le64_to_cpu(ri->i_mtime);
317 inode->i_atime.tv_nsec = le32_to_cpu(ri->i_atime_nsec);
318 inode->i_ctime.tv_nsec = le32_to_cpu(ri->i_ctime_nsec);
319 inode->i_mtime.tv_nsec = le32_to_cpu(ri->i_mtime_nsec);
320 inode->i_generation = le32_to_cpu(ri->i_generation);
Chao Yu2d96ad52018-05-07 20:28:52 +0800321 if (S_ISDIR(inode->i_mode))
322 fi->i_current_depth = le32_to_cpu(ri->i_current_depth);
323 else if (S_ISREG(inode->i_mode))
Chao Yucaf10c62018-05-07 20:28:54 +0800324 fi->i_gc_failures[GC_FAILURE_PIN] =
325 le16_to_cpu(ri->i_gc_failures);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900326 fi->i_xattr_nid = le32_to_cpu(ri->i_xattr_nid);
327 fi->i_flags = le32_to_cpu(ri->i_flags);
Wang Shilonga45bfa72019-06-13 16:29:53 +0900328 if (S_ISREG(inode->i_mode))
329 fi->i_flags &= ~F2FS_PROJINHERIT_FL;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900330 fi->flags = 0;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900331 fi->i_advise = ri->i_advise;
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900332 fi->i_pino = le32_to_cpu(ri->i_pino);
Jaegeuk Kim38431542014-02-27 18:20:00 +0900333 fi->i_dir_level = ri->i_dir_level;
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +0900334
Jaegeuk Kimed3d1252015-12-28 11:39:06 -0800335 if (f2fs_init_extent_tree(inode, &ri->i_ext))
336 set_page_dirty(node_page);
Chao Yu0c872e22015-02-05 17:46:29 +0800337
Jaegeuk Kim91942322016-05-20 10:13:22 -0700338 get_inline_info(inode, ri);
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +0900339
Chao Yufbcf9312017-07-19 00:19:06 +0800340 fi->i_extra_isize = f2fs_has_extra_attr(inode) ?
341 le16_to_cpu(ri->i_extra_isize) : 0;
342
Chao Yu5b11d2b2018-10-24 18:34:26 +0800343 if (f2fs_sb_has_flexible_inline_xattr(sbi)) {
Chao Yu50ffaa92017-09-06 21:59:50 +0800344 fi->i_inline_xattr_size = le16_to_cpu(ri->i_inline_xattr_size);
345 } else if (f2fs_has_inline_xattr(inode) ||
346 f2fs_has_inline_dentry(inode)) {
347 fi->i_inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
348 } else {
349
350 /*
351 * Previous inline data or directory always reserved 200 bytes
352 * in inode layout, even if inline_xattr is disabled. In order
353 * to keep inline_dentry's structure for backward compatibility,
354 * we get the space back only from inline_data.
355 */
356 fi->i_inline_xattr_size = 0;
357 }
358
Chao Yuf9b48642018-07-08 22:16:55 +0800359 if (!sanity_check_inode(inode, node_page)) {
360 f2fs_put_page(node_page, 1);
Chao Yua6b5c262019-06-20 11:36:14 +0800361 return -EFSCORRUPTED;
Chao Yuf9b48642018-07-08 22:16:55 +0800362 }
363
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700364 /* check data exist */
365 if (f2fs_has_inline_data(inode) && !f2fs_exist_data(inode))
Chao Yu9e5ba772015-01-06 14:28:43 +0800366 __recover_inline_status(inode, node_page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700367
Chao Yu343c8062018-10-03 22:32:44 +0800368 /* try to recover cold bit for non-dir inode */
369 if (!S_ISDIR(inode->i_mode) && !is_cold_node(node_page)) {
370 set_cold_node(node_page, false);
371 set_page_dirty(node_page);
372 }
373
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +0900374 /* get rdev by using inline_info */
375 __get_inode_rdev(inode, ri);
376
Chao Yub63686292018-08-11 23:42:09 +0800377 if (S_ISREG(inode->i_mode)) {
378 err = __written_first_block(sbi, ri);
379 if (err < 0) {
380 f2fs_put_page(node_page, 1);
381 return err;
382 }
383 if (!err)
384 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Chao Yu10d67032018-07-10 23:01:45 +0800385 }
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -0700386
Chao Yu6b4d6a82018-05-30 00:20:41 +0800387 if (!f2fs_need_inode_block_update(sbi, inode->i_ino))
Jaegeuk Kim26de9b12016-05-20 20:42:37 -0700388 fi->last_disk_size = inode->i_size;
389
Chao Yu9bafde62018-04-03 15:08:17 +0800390 if (fi->i_flags & F2FS_PROJINHERIT_FL)
Chao Yu5647b302017-07-26 00:01:41 +0800391 set_inode_flag(inode, FI_PROJ_INHERIT);
392
Chao Yu5b11d2b2018-10-24 18:34:26 +0800393 if (f2fs_has_extra_attr(inode) && f2fs_sb_has_project_quota(sbi) &&
Chao Yu5647b302017-07-26 00:01:41 +0800394 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_projid))
395 i_projid = (projid_t)le32_to_cpu(ri->i_projid);
396 else
397 i_projid = F2FS_DEF_PROJID;
398 fi->i_projid = make_kprojid(&init_user_ns, i_projid);
399
Chao Yu5b11d2b2018-10-24 18:34:26 +0800400 if (f2fs_has_extra_attr(inode) && f2fs_sb_has_inode_crtime(sbi) &&
Chao Yu71f8f042018-01-25 14:54:42 +0800401 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
402 fi->i_crtime.tv_sec = le64_to_cpu(ri->i_crtime);
403 fi->i_crtime.tv_nsec = le32_to_cpu(ri->i_crtime_nsec);
404 }
405
Jaegeuk Kimac389af2018-03-29 22:50:41 -0700406 F2FS_I(inode)->i_disk_time[0] = inode->i_atime;
407 F2FS_I(inode)->i_disk_time[1] = inode->i_ctime;
408 F2FS_I(inode)->i_disk_time[2] = inode->i_mtime;
409 F2FS_I(inode)->i_disk_time[3] = F2FS_I(inode)->i_crtime;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900410 f2fs_put_page(node_page, 1);
Jaegeuk Kim9d1015d2014-12-05 10:51:50 -0800411
Chao Yud5e8f6c2015-07-15 17:28:53 +0800412 stat_inc_inline_xattr(inode);
Jaegeuk Kim9d1015d2014-12-05 10:51:50 -0800413 stat_inc_inline_inode(inode);
414 stat_inc_inline_dir(inode);
415
Chao Yu9e5ba772015-01-06 14:28:43 +0800416 return 0;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900417}
418
419struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
420{
421 struct f2fs_sb_info *sbi = F2FS_SB(sb);
422 struct inode *inode;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900423 int ret = 0;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900424
425 inode = iget_locked(sb, ino);
426 if (!inode)
427 return ERR_PTR(-ENOMEM);
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900428
429 if (!(inode->i_state & I_NEW)) {
430 trace_f2fs_iget(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900431 return inode;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900432 }
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900433 if (ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi))
434 goto make_now;
435
436 ret = do_read_inode(inode);
437 if (ret)
438 goto bad_inode;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900439make_now:
440 if (ino == F2FS_NODE_INO(sbi)) {
441 inode->i_mapping->a_ops = &f2fs_node_aops;
Chao Yu7aee0582018-04-09 20:25:06 +0800442 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900443 } else if (ino == F2FS_META_INO(sbi)) {
444 inode->i_mapping->a_ops = &f2fs_meta_aops;
Chao Yu7aee0582018-04-09 20:25:06 +0800445 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900446 } else if (S_ISREG(inode->i_mode)) {
447 inode->i_op = &f2fs_file_inode_operations;
448 inode->i_fop = &f2fs_file_operations;
449 inode->i_mapping->a_ops = &f2fs_dblock_aops;
450 } else if (S_ISDIR(inode->i_mode)) {
451 inode->i_op = &f2fs_dir_inode_operations;
452 inode->i_fop = &f2fs_dir_operations;
453 inode->i_mapping->a_ops = &f2fs_dblock_aops;
Yunlong Song975c5672018-02-28 20:31:52 +0800454 inode_nohighmem(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900455 } else if (S_ISLNK(inode->i_mode)) {
Chandan Rajendrab62c4582018-12-12 15:20:11 +0530456 if (file_is_encrypt(inode))
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700457 inode->i_op = &f2fs_encrypted_symlink_inode_operations;
458 else
459 inode->i_op = &f2fs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -0500460 inode_nohighmem(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900461 inode->i_mapping->a_ops = &f2fs_dblock_aops;
462 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
463 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
464 inode->i_op = &f2fs_special_inode_operations;
465 init_special_inode(inode, inode->i_mode, inode->i_rdev);
466 } else {
467 ret = -EIO;
468 goto bad_inode;
469 }
Jaegeuk Kimf31e1ae2017-05-16 13:20:16 -0700470 f2fs_set_inode_flags(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900471 unlock_new_inode(inode);
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900472 trace_f2fs_iget(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900473 return inode;
474
475bad_inode:
Chao Yu4a5e23b2019-04-15 15:28:33 +0800476 f2fs_inode_synced(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900477 iget_failed(inode);
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900478 trace_f2fs_iget_exit(inode, ret);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900479 return ERR_PTR(ret);
480}
481
Jaegeuk Kime8ea9b32016-09-09 16:59:39 -0700482struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino)
483{
484 struct inode *inode;
485retry:
486 inode = f2fs_iget(sb, ino);
487 if (IS_ERR(inode)) {
488 if (PTR_ERR(inode) == -ENOMEM) {
489 congestion_wait(BLK_RW_ASYNC, HZ/50);
490 goto retry;
491 }
492 }
493 return inode;
494}
495
Chao Yu6b4d6a82018-05-30 00:20:41 +0800496void f2fs_update_inode(struct inode *inode, struct page *node_page)
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900497{
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900498 struct f2fs_inode *ri;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700499 struct extent_tree *et = F2FS_I(inode)->extent_tree;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900500
Chao Yud9b3df52018-12-25 17:43:42 +0800501 f2fs_wait_on_page_writeback(node_page, NODE, true, true);
Yunlei Hee2a05182017-12-05 12:07:47 +0800502 set_page_dirty(node_page);
503
504 f2fs_inode_synced(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900505
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900506 ri = F2FS_INODE(node_page);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900507
508 ri->i_mode = cpu_to_le16(inode->i_mode);
509 ri->i_advise = F2FS_I(inode)->i_advise;
510 ri->i_uid = cpu_to_le32(i_uid_read(inode));
511 ri->i_gid = cpu_to_le32(i_gid_read(inode));
512 ri->i_links = cpu_to_le32(inode->i_nlink);
513 ri->i_size = cpu_to_le64(i_size_read(inode));
Chao Yud9bfbbb2017-07-06 01:11:31 +0800514 ri->i_blocks = cpu_to_le64(SECTOR_TO_BLOCK(inode->i_blocks) + 1);
Chao Yu0c872e22015-02-05 17:46:29 +0800515
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700516 if (et) {
517 read_lock(&et->lock);
518 set_raw_extent(&et->largest, &ri->i_ext);
519 read_unlock(&et->lock);
520 } else {
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700521 memset(&ri->i_ext, 0, sizeof(ri->i_ext));
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700522 }
Jaegeuk Kim91942322016-05-20 10:13:22 -0700523 set_raw_inline(inode, ri);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900524
525 ri->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
526 ri->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
527 ri->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
528 ri->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
529 ri->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
530 ri->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
Chao Yu2d96ad52018-05-07 20:28:52 +0800531 if (S_ISDIR(inode->i_mode))
532 ri->i_current_depth =
533 cpu_to_le32(F2FS_I(inode)->i_current_depth);
534 else if (S_ISREG(inode->i_mode))
Chao Yucaf10c62018-05-07 20:28:54 +0800535 ri->i_gc_failures =
536 cpu_to_le16(F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN]);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900537 ri->i_xattr_nid = cpu_to_le32(F2FS_I(inode)->i_xattr_nid);
538 ri->i_flags = cpu_to_le32(F2FS_I(inode)->i_flags);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900539 ri->i_pino = cpu_to_le32(F2FS_I(inode)->i_pino);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900540 ri->i_generation = cpu_to_le32(inode->i_generation);
Jaegeuk Kim38431542014-02-27 18:20:00 +0900541 ri->i_dir_level = F2FS_I(inode)->i_dir_level;
Changman Lee7d79e752013-01-23 09:40:23 +0900542
Chao Yu5647b302017-07-26 00:01:41 +0800543 if (f2fs_has_extra_attr(inode)) {
Chao Yufbcf9312017-07-19 00:19:06 +0800544 ri->i_extra_isize = cpu_to_le16(F2FS_I(inode)->i_extra_isize);
545
Chao Yu5b11d2b2018-10-24 18:34:26 +0800546 if (f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(inode)))
Chao Yu50ffaa92017-09-06 21:59:50 +0800547 ri->i_inline_xattr_size =
548 cpu_to_le16(F2FS_I(inode)->i_inline_xattr_size);
549
Chao Yu5b11d2b2018-10-24 18:34:26 +0800550 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)) &&
Chao Yu5647b302017-07-26 00:01:41 +0800551 F2FS_FITS_IN_INODE(ri, F2FS_I(inode)->i_extra_isize,
552 i_projid)) {
553 projid_t i_projid;
554
555 i_projid = from_kprojid(&init_user_ns,
556 F2FS_I(inode)->i_projid);
557 ri->i_projid = cpu_to_le32(i_projid);
558 }
Chao Yu71f8f042018-01-25 14:54:42 +0800559
Chao Yu5b11d2b2018-10-24 18:34:26 +0800560 if (f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
Chao Yu71f8f042018-01-25 14:54:42 +0800561 F2FS_FITS_IN_INODE(ri, F2FS_I(inode)->i_extra_isize,
562 i_crtime)) {
563 ri->i_crtime =
564 cpu_to_le64(F2FS_I(inode)->i_crtime.tv_sec);
565 ri->i_crtime_nsec =
566 cpu_to_le32(F2FS_I(inode)->i_crtime.tv_nsec);
567 }
Chao Yu5647b302017-07-26 00:01:41 +0800568 }
569
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +0900570 __set_inode_rdev(inode, ri);
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800571
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -0800572 /* deleted inode */
573 if (inode->i_nlink == 0)
574 clear_inline_node(node_page);
575
Jaegeuk Kimac389af2018-03-29 22:50:41 -0700576 F2FS_I(inode)->i_disk_time[0] = inode->i_atime;
577 F2FS_I(inode)->i_disk_time[1] = inode->i_ctime;
578 F2FS_I(inode)->i_disk_time[2] = inode->i_mtime;
579 F2FS_I(inode)->i_disk_time[3] = F2FS_I(inode)->i_crtime;
Weichao Guof7467672018-03-09 23:10:21 +0800580
581#ifdef CONFIG_F2FS_CHECK_FS
582 f2fs_inode_chksum_set(F2FS_I_SB(inode), node_page);
583#endif
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900584}
585
Chao Yu6b4d6a82018-05-30 00:20:41 +0800586void f2fs_update_inode_page(struct inode *inode)
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900587{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700588 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900589 struct page *node_page;
Jaegeuk Kim744602c2014-01-24 09:42:16 +0900590retry:
Chao Yu6b4d6a82018-05-30 00:20:41 +0800591 node_page = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kim744602c2014-01-24 09:42:16 +0900592 if (IS_ERR(node_page)) {
593 int err = PTR_ERR(node_page);
594 if (err == -ENOMEM) {
595 cond_resched();
596 goto retry;
597 } else if (err != -ENOENT) {
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -0700598 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim744602c2014-01-24 09:42:16 +0900599 }
Yunlei Hee2a05182017-12-05 12:07:47 +0800600 return;
Jaegeuk Kim744602c2014-01-24 09:42:16 +0900601 }
Chao Yu6b4d6a82018-05-30 00:20:41 +0800602 f2fs_update_inode(inode, node_page);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900603 f2fs_put_page(node_page, 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900604}
605
606int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc)
607{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700608 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900609
610 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
611 inode->i_ino == F2FS_META_INO(sbi))
612 return 0;
613
Jaegeuk Kim91942322016-05-20 10:13:22 -0700614 if (!is_inode_flag_set(inode, FI_DIRTY_INODE))
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900615 return 0;
616
Daniel Rosenbergf22f93a2018-08-20 19:21:43 -0700617 if (f2fs_is_checkpoint_ready(sbi))
618 return -ENOSPC;
619
Jaegeuk Kim39936832012-11-22 16:21:29 +0900620 /*
Jaegeuk Kimc5cd29d2015-09-12 11:25:30 -0700621 * We need to balance fs here to prevent from producing dirty node pages
Jaegeuk Kim39936832012-11-22 16:21:29 +0900622 * during the urgent cleaning time when runing out of free sections.
623 */
Chao Yu6b4d6a82018-05-30 00:20:41 +0800624 f2fs_update_inode_page(inode);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700625 if (wbc && wbc->nr_to_write)
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800626 f2fs_balance_fs(sbi, true);
Jaegeuk Kim744602c2014-01-24 09:42:16 +0900627 return 0;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900628}
629
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900630/*
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900631 * Called at the last iput() if i_nlink is zero
632 */
633void f2fs_evict_inode(struct inode *inode)
634{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700635 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700636 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
Chao Yu13ec7292015-08-24 17:40:45 +0800637 int err = 0;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900638
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700639 /* some remained atomic pages should discarded */
Jaegeuk Kim1e843712014-12-09 06:08:59 -0800640 if (f2fs_is_atomic_file(inode))
Chao Yu6b4d6a82018-05-30 00:20:41 +0800641 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700642
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900643 trace_f2fs_evict_inode(inode);
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700644 truncate_inode_pages_final(&inode->i_data);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900645
646 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
647 inode->i_ino == F2FS_META_INO(sbi))
Chao Yudbf20cb2014-07-25 12:00:57 +0800648 goto out_clear;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900649
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -0700650 f2fs_bug_on(sbi, get_dirty_pages(inode));
Chao Yu6b4d6a82018-05-30 00:20:41 +0800651 f2fs_remove_dirty_inode(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900652
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700653 f2fs_destroy_extent_tree(inode);
654
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900655 if (inode->i_nlink || is_bad_inode(inode))
656 goto no_delete;
657
Chao Yud8312722018-09-20 20:05:00 +0800658 err = dquot_initialize(inode);
659 if (err) {
660 err = 0;
661 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
662 }
Chao Yu09c3a722017-07-09 00:13:07 +0800663
Chao Yu6b4d6a82018-05-30 00:20:41 +0800664 f2fs_remove_ino_entry(sbi, inode->i_ino, APPEND_INO);
665 f2fs_remove_ino_entry(sbi, inode->i_ino, UPDATE_INO);
666 f2fs_remove_ino_entry(sbi, inode->i_ino, FLUSH_INO);
Jaegeuk Kim53aa6bb2016-05-25 15:24:18 -0700667
Changman Leed6212a52013-01-29 18:30:07 +0900668 sb_start_intwrite(inode->i_sb);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700669 set_inode_flag(inode, FI_NO_ALLOC);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900670 i_size_write(inode, 0);
Jaegeuk Kim4c0c2942016-05-03 09:22:18 -0700671retry:
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900672 if (F2FS_HAS_BLOCKS(inode))
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700673 err = f2fs_truncate(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900674
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700675 if (time_to_inject(sbi, FAULT_EVICT_INODE)) {
676 f2fs_show_injection_info(FAULT_EVICT_INODE);
677 err = -EIO;
678 }
Arnd Bergmann33c54d52018-08-13 23:38:06 +0200679
Chao Yu13ec7292015-08-24 17:40:45 +0800680 if (!err) {
681 f2fs_lock_op(sbi);
Chao Yu6b4d6a82018-05-30 00:20:41 +0800682 err = f2fs_remove_inode_page(inode);
Chao Yu13ec7292015-08-24 17:40:45 +0800683 f2fs_unlock_op(sbi);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700684 if (err == -ENOENT)
685 err = 0;
Chao Yu13ec7292015-08-24 17:40:45 +0800686 }
Jaegeuk Kim39936832012-11-22 16:21:29 +0900687
Jaegeuk Kim4c0c2942016-05-03 09:22:18 -0700688 /* give more chances, if ENOMEM case */
689 if (err == -ENOMEM) {
690 err = 0;
691 goto retry;
692 }
693
Chao Yud8312722018-09-20 20:05:00 +0800694 if (err) {
Chao Yu6b4d6a82018-05-30 00:20:41 +0800695 f2fs_update_inode_page(inode);
Chao Yud8312722018-09-20 20:05:00 +0800696 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
697 }
Changman Leed6212a52013-01-29 18:30:07 +0900698 sb_end_intwrite(inode->i_sb);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900699no_delete:
Chao Yu09c3a722017-07-09 00:13:07 +0800700 dquot_drop(inode);
701
Chao Yud5e8f6c2015-07-15 17:28:53 +0800702 stat_dec_inline_xattr(inode);
Jaegeuk Kim3289c062014-10-13 20:00:16 -0700703 stat_dec_inline_dir(inode);
Jaegeuk Kime7a2bf22014-10-14 10:29:50 -0700704 stat_dec_inline_inode(inode);
Chao Yu0bdee482015-03-19 19:27:51 +0800705
Daniel Rosenbergf22f93a2018-08-20 19:21:43 -0700706 if (likely(!is_set_ckpt_flags(sbi, CP_ERROR_FLAG) &&
707 !is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
Chao Yu3d065372017-09-12 14:04:05 +0800708 f2fs_bug_on(sbi, is_inode_flag_set(inode, FI_DIRTY_INODE));
Jaegeuk Kim5ded3b82017-10-12 19:12:53 -0700709 else
710 f2fs_inode_synced(inode);
Chao Yu3d065372017-09-12 14:04:05 +0800711
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700712 /* ino == 0, if f2fs_new_inode() was failed t*/
713 if (inode->i_ino)
714 invalidate_mapping_pages(NODE_MAPPING(sbi), inode->i_ino,
715 inode->i_ino);
Chao Yu002a41c2014-08-04 09:54:58 +0800716 if (xnid)
717 invalidate_mapping_pages(NODE_MAPPING(sbi), xnid, xnid);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700718 if (inode->i_nlink) {
719 if (is_inode_flag_set(inode, FI_APPEND_WRITE))
Chao Yu6b4d6a82018-05-30 00:20:41 +0800720 f2fs_add_ino_entry(sbi, inode->i_ino, APPEND_INO);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700721 if (is_inode_flag_set(inode, FI_UPDATE_WRITE))
Chao Yu6b4d6a82018-05-30 00:20:41 +0800722 f2fs_add_ino_entry(sbi, inode->i_ino, UPDATE_INO);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700723 }
Jaegeuk Kim91942322016-05-20 10:13:22 -0700724 if (is_inode_flag_set(inode, FI_FREE_NID)) {
Chao Yu6b4d6a82018-05-30 00:20:41 +0800725 f2fs_alloc_nid_failed(sbi, inode->i_ino);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700726 clear_inode_flag(inode, FI_FREE_NID);
Jaegeuk Kim5d1c2f32017-06-01 15:39:27 -0700727 } else {
Jaegeuk Kimb733d012018-04-23 23:02:31 -0600728 /*
729 * If xattr nid is corrupted, we can reach out error condition,
Chao Yu6b4d6a82018-05-30 00:20:41 +0800730 * err & !f2fs_exist_written_data(sbi, inode->i_ino, ORPHAN_INO)).
731 * In that case, f2fs_check_nid_range() is enough to give a clue.
Jaegeuk Kimb733d012018-04-23 23:02:31 -0600732 */
Jaegeuk Kimc9b63bd2015-06-23 10:36:08 -0700733 }
Chao Yudbf20cb2014-07-25 12:00:57 +0800734out_clear:
Eric Biggers401052f2018-01-11 23:30:13 -0500735 fscrypt_put_encryption_info(inode);
Chao Yudbf20cb2014-07-25 12:00:57 +0800736 clear_inode(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900737}
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700738
739/* caller should call f2fs_lock_op() */
Chao Yu6b4d6a82018-05-30 00:20:41 +0800740void f2fs_handle_failed_inode(struct inode *inode)
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700741{
742 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700743 struct node_info ni;
Chao Yu3c743262018-07-17 00:02:17 +0800744 int err;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700745
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700746 /*
747 * clear nlink of inode in order to release resource of inode
748 * immediately.
749 */
750 clear_nlink(inode);
751
752 /*
753 * we must call this to avoid inode being remained as dirty, resulting
754 * in a panic when flushing dirty inodes in gdirty_list.
755 */
Chao Yu6b4d6a82018-05-30 00:20:41 +0800756 f2fs_update_inode_page(inode);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700757 f2fs_inode_synced(inode);
758
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700759 /* don't make bad inode, since it becomes a regular file. */
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700760 unlock_new_inode(inode);
761
Chao Yu13ec7292015-08-24 17:40:45 +0800762 /*
Chao Yu13ec7292015-08-24 17:40:45 +0800763 * Note: we should add inode to orphan list before f2fs_unlock_op()
764 * so we can prevent losing this orphan when encoutering checkpoint
765 * and following suddenly power-off.
766 */
Chao Yu3c743262018-07-17 00:02:17 +0800767 err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
768 if (err) {
769 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesa0d68632019-06-18 17:48:42 +0800770 f2fs_warn(sbi, "May loss orphan inode, run fsck to fix.");
Chao Yu3c743262018-07-17 00:02:17 +0800771 goto out;
772 }
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700773
774 if (ni.blk_addr != NULL_ADDR) {
Chao Yu3c743262018-07-17 00:02:17 +0800775 err = f2fs_acquire_orphan_inode(sbi);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700776 if (err) {
777 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesa0d68632019-06-18 17:48:42 +0800778 f2fs_warn(sbi, "Too many orphan inodes, run fsck to fix.");
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700779 } else {
Chao Yu6b4d6a82018-05-30 00:20:41 +0800780 f2fs_add_orphan_inode(inode);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700781 }
Chao Yu6b4d6a82018-05-30 00:20:41 +0800782 f2fs_alloc_nid_done(sbi, inode->i_ino);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700783 } else {
Jaegeuk Kim91942322016-05-20 10:13:22 -0700784 set_inode_flag(inode, FI_FREE_NID);
Chao Yu13ec7292015-08-24 17:40:45 +0800785 }
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700786
Chao Yu3c743262018-07-17 00:02:17 +0800787out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700788 f2fs_unlock_op(sbi);
789
790 /* iput will drop the inode object */
791 iput(inode);
792}