blob: 502bd491336a8200861232e9b3ba60ccc94965c3 [file] [log] [blame]
Chao Yu7c1a0002018-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 Yu0eb0ada2017-06-14 23:00:56 +080016#include "segment.h"
Chao Yudd6c89b2019-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 Kim7c457292016-10-14 11:51:23 -070021void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync)
Jaegeuk Kimb56ab832016-06-30 19:09:37 -070022{
Daeho Jeong9ac1e2d2018-01-11 11:26:19 +090023 if (is_inode_flag_set(inode, FI_NEW_INODE))
24 return;
25
Jaegeuk Kim7c457292016-10-14 11:51:23 -070026 if (f2fs_inode_dirtied(inode, sync))
Jaegeuk Kimb56ab832016-06-30 19:09:37 -070027 return;
Jaegeuk Kim7c457292016-10-14 11:51:23 -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 Yu59c84402018-04-03 15:08:17 +080037 if (flags & F2FS_SYNC_FL)
Zhang Zhen8abfb362014-04-15 14:19:38 +080038 new_fl |= S_SYNC;
Chao Yu59c84402018-04-03 15:08:17 +080039 if (flags & F2FS_APPEND_FL)
Zhang Zhen8abfb362014-04-15 14:19:38 +080040 new_fl |= S_APPEND;
Chao Yu59c84402018-04-03 15:08:17 +080041 if (flags & F2FS_IMMUTABLE_FL)
Zhang Zhen8abfb362014-04-15 14:19:38 +080042 new_fl |= S_IMMUTABLE;
Chao Yu59c84402018-04-03 15:08:17 +080043 if (flags & F2FS_NOATIME_FL)
Zhang Zhen8abfb362014-04-15 14:19:38 +080044 new_fl |= S_NOATIME;
Chao Yu59c84402018-04-03 15:08:17 +080045 if (flags & F2FS_DIRSYNC_FL)
Zhang Zhen8abfb362014-04-15 14:19:38 +080046 new_fl |= S_DIRSYNC;
Chandan Rajendra62230e0d2018-12-12 15:20:11 +053047 if (file_is_encrypt(inode))
Eric Biggers2ee6a572017-10-09 12:15:35 -070048 new_fl |= S_ENCRYPTED;
Eric Biggers95ae2512019-07-22 09:26:24 -070049 if (file_is_verity(inode))
50 new_fl |= S_VERITY;
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -070051 if (flags & F2FS_CASEFOLD_FL)
52 new_fl |= S_CASEFOLD;
Zhang Zhen6a678852015-08-24 10:41:32 +080053 inode_set_flags(inode, new_fl,
Eric Biggers2ee6a572017-10-09 12:15:35 -070054 S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|
Linus Torvaldsfbc246a2019-09-21 14:26:33 -070055 S_ENCRYPTED|S_VERITY|S_CASEFOLD);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +090056}
57
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090058static void __get_inode_rdev(struct inode *inode, struct f2fs_inode *ri)
59{
Chao Yu7a2af762017-07-19 00:19:06 +080060 int extra_size = get_extra_isize(inode);
61
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090062 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
63 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
Chao Yu7a2af762017-07-19 00:19:06 +080064 if (ri->i_addr[extra_size])
65 inode->i_rdev = old_decode_dev(
66 le32_to_cpu(ri->i_addr[extra_size]));
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090067 else
Chao Yu7a2af762017-07-19 00:19:06 +080068 inode->i_rdev = new_decode_dev(
69 le32_to_cpu(ri->i_addr[extra_size + 1]));
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090070 }
71}
72
Chao Yu91291e92018-07-10 23:01:45 +080073static int __written_first_block(struct f2fs_sb_info *sbi,
Chao Yue1da7872018-06-05 17:44:11 +080074 struct f2fs_inode *ri)
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -070075{
Chao Yu7a2af762017-07-19 00:19:06 +080076 block_t addr = le32_to_cpu(ri->i_addr[offset_in_addr(ri)]);
Jaegeuk Kimadad81e2015-03-24 12:04:20 -070077
Chao Yu91291e92018-07-10 23:01:45 +080078 if (!__is_valid_data_blkaddr(addr))
79 return 1;
Chao Yu93770ab2019-04-15 15:26:32 +080080 if (!f2fs_is_valid_blkaddr(sbi, addr, DATA_GENERIC_ENHANCE))
Chao Yu10f966b2019-06-20 11:36:14 +080081 return -EFSCORRUPTED;
Chao Yu91291e92018-07-10 23:01:45 +080082 return 0;
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -070083}
84
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090085static void __set_inode_rdev(struct inode *inode, struct f2fs_inode *ri)
86{
Chao Yu7a2af762017-07-19 00:19:06 +080087 int extra_size = get_extra_isize(inode);
88
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090089 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
90 if (old_valid_dev(inode->i_rdev)) {
Chao Yu7a2af762017-07-19 00:19:06 +080091 ri->i_addr[extra_size] =
Chris Fries6c311ec2014-01-17 14:44:39 -060092 cpu_to_le32(old_encode_dev(inode->i_rdev));
Chao Yu7a2af762017-07-19 00:19:06 +080093 ri->i_addr[extra_size + 1] = 0;
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090094 } else {
Chao Yu7a2af762017-07-19 00:19:06 +080095 ri->i_addr[extra_size] = 0;
96 ri->i_addr[extra_size + 1] =
Chris Fries6c311ec2014-01-17 14:44:39 -060097 cpu_to_le32(new_encode_dev(inode->i_rdev));
Chao Yu7a2af762017-07-19 00:19:06 +080098 ri->i_addr[extra_size + 2] = 0;
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090099 }
100 }
101}
102
Chao Yu9e5ba772015-01-06 14:28:43 +0800103static void __recover_inline_status(struct inode *inode, struct page *ipage)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700104{
Chao Yuf2470372017-07-19 00:19:05 +0800105 void *inline_data = inline_data_addr(inode, ipage);
Chao Yu9e5ba772015-01-06 14:28:43 +0800106 __le32 *start = inline_data;
Chao Yuf2470372017-07-19 00:19:05 +0800107 __le32 *end = start + MAX_INLINE_DATA(inode) / sizeof(__le32);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700108
Chao Yu9e5ba772015-01-06 14:28:43 +0800109 while (start < end) {
110 if (*start++) {
Chao Yubae0ee72018-12-25 17:43:42 +0800111 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700112
Jaegeuk Kim91942322016-05-20 10:13:22 -0700113 set_inode_flag(inode, FI_DATA_EXIST);
114 set_raw_inline(inode, F2FS_INODE(ipage));
Chao Yu9e5ba772015-01-06 14:28:43 +0800115 set_page_dirty(ipage);
116 return;
117 }
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700118 }
Chao Yu9e5ba772015-01-06 14:28:43 +0800119 return;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700120}
121
Chao Yu704956e2017-07-31 20:19:09 +0800122static bool f2fs_enable_inode_chksum(struct f2fs_sb_info *sbi, struct page *page)
123{
124 struct f2fs_inode *ri = &F2FS_NODE(page)->i;
Chao Yu704956e2017-07-31 20:19:09 +0800125
Chao Yu7beb01f2018-10-24 18:34:26 +0800126 if (!f2fs_sb_has_inode_chksum(sbi))
Chao Yu704956e2017-07-31 20:19:09 +0800127 return false;
128
Chao Yu4c6b56c2018-07-08 22:16:54 +0800129 if (!IS_INODE(page) || !(ri->i_inline & F2FS_EXTRA_ATTR))
Chao Yu704956e2017-07-31 20:19:09 +0800130 return false;
131
Zhikang Zhangd6964942018-04-14 01:02:34 +0800132 if (!F2FS_FITS_IN_INODE(ri, le16_to_cpu(ri->i_extra_isize),
133 i_inode_checksum))
Chao Yu704956e2017-07-31 20:19:09 +0800134 return false;
135
136 return true;
137}
138
139static __u32 f2fs_inode_chksum(struct f2fs_sb_info *sbi, struct page *page)
140{
141 struct f2fs_node *node = F2FS_NODE(page);
142 struct f2fs_inode *ri = &node->i;
143 __le32 ino = node->footer.ino;
144 __le32 gen = ri->i_generation;
145 __u32 chksum, chksum_seed;
146 __u32 dummy_cs = 0;
147 unsigned int offset = offsetof(struct f2fs_inode, i_inode_checksum);
148 unsigned int cs_size = sizeof(dummy_cs);
149
150 chksum = f2fs_chksum(sbi, sbi->s_chksum_seed, (__u8 *)&ino,
151 sizeof(ino));
152 chksum_seed = f2fs_chksum(sbi, chksum, (__u8 *)&gen, sizeof(gen));
153
154 chksum = f2fs_chksum(sbi, chksum_seed, (__u8 *)ri, offset);
155 chksum = f2fs_chksum(sbi, chksum, (__u8 *)&dummy_cs, cs_size);
156 offset += cs_size;
157 chksum = f2fs_chksum(sbi, chksum, (__u8 *)ri + offset,
158 F2FS_BLKSIZE - offset);
159 return chksum;
160}
161
162bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page)
163{
164 struct f2fs_inode *ri;
165 __u32 provided, calculated;
166
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -0700167 if (unlikely(is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN)))
168 return true;
169
Weichao Guo54c55c42018-03-09 23:10:21 +0800170#ifdef CONFIG_F2FS_CHECK_FS
171 if (!f2fs_enable_inode_chksum(sbi, page))
172#else
Jaegeuk Kimee605232017-08-31 16:54:51 -0700173 if (!f2fs_enable_inode_chksum(sbi, page) ||
174 PageDirty(page) || PageWriteback(page))
Weichao Guo54c55c42018-03-09 23:10:21 +0800175#endif
Chao Yu704956e2017-07-31 20:19:09 +0800176 return true;
177
178 ri = &F2FS_NODE(page)->i;
179 provided = le32_to_cpu(ri->i_inode_checksum);
180 calculated = f2fs_inode_chksum(sbi, page);
181
182 if (provided != calculated)
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800183 f2fs_warn(sbi, "checksum invalid, nid = %lu, ino_of_node = %x, %x vs. %x",
184 page->index, ino_of_node(page), provided, calculated);
Chao Yu704956e2017-07-31 20:19:09 +0800185
186 return provided == calculated;
187}
188
189void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page)
190{
191 struct f2fs_inode *ri = &F2FS_NODE(page)->i;
192
193 if (!f2fs_enable_inode_chksum(sbi, page))
194 return;
195
196 ri->i_inode_checksum = cpu_to_le32(f2fs_inode_chksum(sbi, page));
197}
198
Chao Yue34438c2018-06-29 13:55:22 +0800199static bool sanity_check_inode(struct inode *inode, struct page *node_page)
Jaegeuk Kim5d646002018-04-24 11:37:18 -0600200{
201 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu18dd6472018-07-08 22:16:55 +0800202 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yue34438c2018-06-29 13:55:22 +0800203 unsigned long long iblocks;
204
205 iblocks = le64_to_cpu(F2FS_INODE(node_page)->i_blocks);
206 if (!iblocks) {
207 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800208 f2fs_warn(sbi, "%s: corrupted inode i_blocks i_ino=%lx iblocks=%llu, run fsck to fix.",
209 __func__, inode->i_ino, iblocks);
Chao Yue34438c2018-06-29 13:55:22 +0800210 return false;
211 }
212
213 if (ino_of_node(node_page) != nid_of_node(node_page)) {
214 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800215 f2fs_warn(sbi, "%s: corrupted inode footer i_ino=%lx, ino,nid: [%u, %u] run fsck to fix.",
216 __func__, inode->i_ino,
217 ino_of_node(node_page), nid_of_node(node_page));
Chao Yue34438c2018-06-29 13:55:22 +0800218 return false;
219 }
Jaegeuk Kim5d646002018-04-24 11:37:18 -0600220
Chao Yu7beb01f2018-10-24 18:34:26 +0800221 if (f2fs_sb_has_flexible_inline_xattr(sbi)
Jaegeuk Kim5d646002018-04-24 11:37:18 -0600222 && !f2fs_has_extra_attr(inode)) {
223 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800224 f2fs_warn(sbi, "%s: corrupted inode ino=%lx, run fsck to fix.",
225 __func__, inode->i_ino);
Jaegeuk Kim5d646002018-04-24 11:37:18 -0600226 return false;
227 }
Chao Yu76d56d42018-06-25 23:29:49 +0800228
229 if (f2fs_has_extra_attr(inode) &&
Chao Yu7beb01f2018-10-24 18:34:26 +0800230 !f2fs_sb_has_extra_attr(sbi)) {
Chao Yu76d56d42018-06-25 23:29:49 +0800231 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800232 f2fs_warn(sbi, "%s: inode (ino=%lx) is with extra_attr, but extra_attr feature is off",
233 __func__, inode->i_ino);
Chao Yu76d56d42018-06-25 23:29:49 +0800234 return false;
235 }
Chao Yuc9b60782018-08-01 19:13:44 +0800236
Chao Yu18dd6472018-07-08 22:16:55 +0800237 if (fi->i_extra_isize > F2FS_TOTAL_EXTRA_ATTR_SIZE ||
238 fi->i_extra_isize % sizeof(__le32)) {
239 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800240 f2fs_warn(sbi, "%s: inode (ino=%lx) has corrupted i_extra_isize: %d, max: %zu",
241 __func__, inode->i_ino, fi->i_extra_isize,
242 F2FS_TOTAL_EXTRA_ATTR_SIZE);
Chao Yu18dd6472018-07-08 22:16:55 +0800243 return false;
244 }
245
Chao Yudd6c89b2019-03-04 17:19:04 +0800246 if (f2fs_has_extra_attr(inode) &&
247 f2fs_sb_has_flexible_inline_xattr(sbi) &&
248 f2fs_has_inline_xattr(inode) &&
249 (!fi->i_inline_xattr_size ||
250 fi->i_inline_xattr_size > MAX_INLINE_XATTR_SIZE)) {
251 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800252 f2fs_warn(sbi, "%s: inode (ino=%lx) has corrupted i_inline_xattr_size: %d, max: %zu",
253 __func__, inode->i_ino, fi->i_inline_xattr_size,
254 MAX_INLINE_XATTR_SIZE);
Chao Yudd6c89b2019-03-04 17:19:04 +0800255 return false;
256 }
257
Chao Yuc9b60782018-08-01 19:13:44 +0800258 if (F2FS_I(inode)->extent_tree) {
259 struct extent_info *ei = &F2FS_I(inode)->extent_tree->largest;
260
261 if (ei->len &&
Chao Yu93770ab2019-04-15 15:26:32 +0800262 (!f2fs_is_valid_blkaddr(sbi, ei->blk,
263 DATA_GENERIC_ENHANCE) ||
Chao Yuc9b60782018-08-01 19:13:44 +0800264 !f2fs_is_valid_blkaddr(sbi, ei->blk + ei->len - 1,
Chao Yu93770ab2019-04-15 15:26:32 +0800265 DATA_GENERIC_ENHANCE))) {
Chao Yuc9b60782018-08-01 19:13:44 +0800266 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800267 f2fs_warn(sbi, "%s: inode (ino=%lx) extent info [%u, %u, %u] is incorrect, run fsck to fix",
268 __func__, inode->i_ino,
269 ei->blk, ei->fofs, ei->len);
Chao Yuc9b60782018-08-01 19:13:44 +0800270 return false;
271 }
272 }
Chao Yubcbfbd62018-06-29 00:19:25 +0800273
274 if (f2fs_has_inline_data(inode) &&
275 (!S_ISREG(inode->i_mode) && !S_ISLNK(inode->i_mode))) {
276 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800277 f2fs_warn(sbi, "%s: inode (ino=%lx, mode=%u) should not have inline_data, run fsck to fix",
278 __func__, inode->i_ino, inode->i_mode);
Chao Yubcbfbd62018-06-29 00:19:25 +0800279 return false;
280 }
281
282 if (f2fs_has_inline_dentry(inode) && !S_ISDIR(inode->i_mode)) {
283 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800284 f2fs_warn(sbi, "%s: inode (ino=%lx, mode=%u) should not have inline_dentry, run fsck to fix",
285 __func__, inode->i_ino, inode->i_mode);
Chao Yubcbfbd62018-06-29 00:19:25 +0800286 return false;
287 }
288
Jaegeuk Kim5d646002018-04-24 11:37:18 -0600289 return true;
290}
291
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900292static int do_read_inode(struct inode *inode)
293{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700294 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900295 struct f2fs_inode_info *fi = F2FS_I(inode);
296 struct page *node_page;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900297 struct f2fs_inode *ri;
Chao Yu5c571322017-07-26 00:01:41 +0800298 projid_t i_projid;
Chao Yu91291e92018-07-10 23:01:45 +0800299 int err;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900300
301 /* Check if ino is within scope */
Chao Yu4d57b862018-05-30 00:20:41 +0800302 if (f2fs_check_nid_range(sbi, inode->i_ino))
Namjae Jeon064e0822013-03-17 17:27:20 +0900303 return -EINVAL;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900304
Chao Yu4d57b862018-05-30 00:20:41 +0800305 node_page = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900306 if (IS_ERR(node_page))
307 return PTR_ERR(node_page);
308
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900309 ri = F2FS_INODE(node_page);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900310
311 inode->i_mode = le16_to_cpu(ri->i_mode);
312 i_uid_write(inode, le32_to_cpu(ri->i_uid));
313 i_gid_write(inode, le32_to_cpu(ri->i_gid));
314 set_nlink(inode, le32_to_cpu(ri->i_links));
315 inode->i_size = le64_to_cpu(ri->i_size);
Chao Yu000519f2017-07-06 01:11:31 +0800316 inode->i_blocks = SECTOR_FROM_BLOCK(le64_to_cpu(ri->i_blocks) - 1);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900317
318 inode->i_atime.tv_sec = le64_to_cpu(ri->i_atime);
319 inode->i_ctime.tv_sec = le64_to_cpu(ri->i_ctime);
320 inode->i_mtime.tv_sec = le64_to_cpu(ri->i_mtime);
321 inode->i_atime.tv_nsec = le32_to_cpu(ri->i_atime_nsec);
322 inode->i_ctime.tv_nsec = le32_to_cpu(ri->i_ctime_nsec);
323 inode->i_mtime.tv_nsec = le32_to_cpu(ri->i_mtime_nsec);
324 inode->i_generation = le32_to_cpu(ri->i_generation);
Chao Yu1c41e6802018-05-07 20:28:52 +0800325 if (S_ISDIR(inode->i_mode))
326 fi->i_current_depth = le32_to_cpu(ri->i_current_depth);
327 else if (S_ISREG(inode->i_mode))
Chao Yu2ef79ec2018-05-07 20:28:54 +0800328 fi->i_gc_failures[GC_FAILURE_PIN] =
329 le16_to_cpu(ri->i_gc_failures);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900330 fi->i_xattr_nid = le32_to_cpu(ri->i_xattr_nid);
331 fi->i_flags = le32_to_cpu(ri->i_flags);
Wang Shilong5043a962019-06-13 16:29:53 +0900332 if (S_ISREG(inode->i_mode))
333 fi->i_flags &= ~F2FS_PROJINHERIT_FL;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900334 fi->flags = 0;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900335 fi->i_advise = ri->i_advise;
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900336 fi->i_pino = le32_to_cpu(ri->i_pino);
Jaegeuk Kim38431542014-02-27 18:20:00 +0900337 fi->i_dir_level = ri->i_dir_level;
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +0900338
Jaegeuk Kimed3d1252015-12-28 11:39:06 -0800339 if (f2fs_init_extent_tree(inode, &ri->i_ext))
340 set_page_dirty(node_page);
Chao Yu0c872e22015-02-05 17:46:29 +0800341
Jaegeuk Kim91942322016-05-20 10:13:22 -0700342 get_inline_info(inode, ri);
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +0900343
Chao Yu7a2af762017-07-19 00:19:06 +0800344 fi->i_extra_isize = f2fs_has_extra_attr(inode) ?
345 le16_to_cpu(ri->i_extra_isize) : 0;
346
Chao Yu7beb01f2018-10-24 18:34:26 +0800347 if (f2fs_sb_has_flexible_inline_xattr(sbi)) {
Chao Yu6afc6622017-09-06 21:59:50 +0800348 fi->i_inline_xattr_size = le16_to_cpu(ri->i_inline_xattr_size);
349 } else if (f2fs_has_inline_xattr(inode) ||
350 f2fs_has_inline_dentry(inode)) {
351 fi->i_inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
352 } else {
353
354 /*
355 * Previous inline data or directory always reserved 200 bytes
356 * in inode layout, even if inline_xattr is disabled. In order
357 * to keep inline_dentry's structure for backward compatibility,
358 * we get the space back only from inline_data.
359 */
360 fi->i_inline_xattr_size = 0;
361 }
362
Chao Yu18dd6472018-07-08 22:16:55 +0800363 if (!sanity_check_inode(inode, node_page)) {
364 f2fs_put_page(node_page, 1);
Chao Yu10f966b2019-06-20 11:36:14 +0800365 return -EFSCORRUPTED;
Chao Yu18dd6472018-07-08 22:16:55 +0800366 }
367
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700368 /* check data exist */
369 if (f2fs_has_inline_data(inode) && !f2fs_exist_data(inode))
Chao Yu9e5ba772015-01-06 14:28:43 +0800370 __recover_inline_status(inode, node_page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700371
Chao Yuef2a0072018-10-03 22:32:44 +0800372 /* try to recover cold bit for non-dir inode */
373 if (!S_ISDIR(inode->i_mode) && !is_cold_node(node_page)) {
374 set_cold_node(node_page, false);
375 set_page_dirty(node_page);
376 }
377
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +0900378 /* get rdev by using inline_info */
379 __get_inode_rdev(inode, ri);
380
Chao Yudda9f4b2018-08-11 23:42:09 +0800381 if (S_ISREG(inode->i_mode)) {
382 err = __written_first_block(sbi, ri);
383 if (err < 0) {
384 f2fs_put_page(node_page, 1);
385 return err;
386 }
387 if (!err)
388 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Chao Yu91291e92018-07-10 23:01:45 +0800389 }
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -0700390
Chao Yu4d57b862018-05-30 00:20:41 +0800391 if (!f2fs_need_inode_block_update(sbi, inode->i_ino))
Jaegeuk Kim26de9b12016-05-20 20:42:37 -0700392 fi->last_disk_size = inode->i_size;
393
Chao Yu59c84402018-04-03 15:08:17 +0800394 if (fi->i_flags & F2FS_PROJINHERIT_FL)
Chao Yu5c571322017-07-26 00:01:41 +0800395 set_inode_flag(inode, FI_PROJ_INHERIT);
396
Chao Yu7beb01f2018-10-24 18:34:26 +0800397 if (f2fs_has_extra_attr(inode) && f2fs_sb_has_project_quota(sbi) &&
Chao Yu5c571322017-07-26 00:01:41 +0800398 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_projid))
399 i_projid = (projid_t)le32_to_cpu(ri->i_projid);
400 else
401 i_projid = F2FS_DEF_PROJID;
402 fi->i_projid = make_kprojid(&init_user_ns, i_projid);
403
Chao Yu7beb01f2018-10-24 18:34:26 +0800404 if (f2fs_has_extra_attr(inode) && f2fs_sb_has_inode_crtime(sbi) &&
Chao Yu1c1d35d2018-01-25 14:54:42 +0800405 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
406 fi->i_crtime.tv_sec = le64_to_cpu(ri->i_crtime);
407 fi->i_crtime.tv_nsec = le32_to_cpu(ri->i_crtime_nsec);
408 }
409
Arnd Bergmann24b81df2018-06-20 10:02:19 +0200410 F2FS_I(inode)->i_disk_time[0] = inode->i_atime;
411 F2FS_I(inode)->i_disk_time[1] = inode->i_ctime;
412 F2FS_I(inode)->i_disk_time[2] = inode->i_mtime;
Jaegeuk Kim214c2462018-03-29 22:50:41 -0700413 F2FS_I(inode)->i_disk_time[3] = F2FS_I(inode)->i_crtime;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900414 f2fs_put_page(node_page, 1);
Jaegeuk Kim9d1015d2014-12-05 10:51:50 -0800415
Chao Yud5e8f6c2015-07-15 17:28:53 +0800416 stat_inc_inline_xattr(inode);
Jaegeuk Kim9d1015d2014-12-05 10:51:50 -0800417 stat_inc_inline_inode(inode);
418 stat_inc_inline_dir(inode);
419
Chao Yu9e5ba772015-01-06 14:28:43 +0800420 return 0;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900421}
422
423struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
424{
425 struct f2fs_sb_info *sbi = F2FS_SB(sb);
426 struct inode *inode;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900427 int ret = 0;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900428
429 inode = iget_locked(sb, ino);
430 if (!inode)
431 return ERR_PTR(-ENOMEM);
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900432
433 if (!(inode->i_state & I_NEW)) {
434 trace_f2fs_iget(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900435 return inode;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900436 }
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900437 if (ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi))
438 goto make_now;
439
440 ret = do_read_inode(inode);
441 if (ret)
442 goto bad_inode;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900443make_now:
444 if (ino == F2FS_NODE_INO(sbi)) {
445 inode->i_mapping->a_ops = &f2fs_node_aops;
Chao Yu81114ba2018-04-09 20:25:06 +0800446 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900447 } else if (ino == F2FS_META_INO(sbi)) {
448 inode->i_mapping->a_ops = &f2fs_meta_aops;
Chao Yu81114ba2018-04-09 20:25:06 +0800449 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900450 } else if (S_ISREG(inode->i_mode)) {
451 inode->i_op = &f2fs_file_inode_operations;
452 inode->i_fop = &f2fs_file_operations;
453 inode->i_mapping->a_ops = &f2fs_dblock_aops;
454 } else if (S_ISDIR(inode->i_mode)) {
455 inode->i_op = &f2fs_dir_inode_operations;
456 inode->i_fop = &f2fs_dir_operations;
457 inode->i_mapping->a_ops = &f2fs_dblock_aops;
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800458 inode_nohighmem(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900459 } else if (S_ISLNK(inode->i_mode)) {
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530460 if (file_is_encrypt(inode))
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700461 inode->i_op = &f2fs_encrypted_symlink_inode_operations;
462 else
463 inode->i_op = &f2fs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -0500464 inode_nohighmem(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900465 inode->i_mapping->a_ops = &f2fs_dblock_aops;
466 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
467 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
468 inode->i_op = &f2fs_special_inode_operations;
469 init_special_inode(inode, inode->i_mode, inode->i_rdev);
470 } else {
471 ret = -EIO;
472 goto bad_inode;
473 }
Jaegeuk Kim93607122017-05-16 13:20:16 -0700474 f2fs_set_inode_flags(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900475 unlock_new_inode(inode);
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900476 trace_f2fs_iget(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900477 return inode;
478
479bad_inode:
Chao Yu546d22f2019-04-15 15:28:33 +0800480 f2fs_inode_synced(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900481 iget_failed(inode);
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900482 trace_f2fs_iget_exit(inode, ret);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900483 return ERR_PTR(ret);
484}
485
Jaegeuk Kime8ea9b32016-09-09 16:59:39 -0700486struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino)
487{
488 struct inode *inode;
489retry:
490 inode = f2fs_iget(sb, ino);
491 if (IS_ERR(inode)) {
492 if (PTR_ERR(inode) == -ENOMEM) {
493 congestion_wait(BLK_RW_ASYNC, HZ/50);
494 goto retry;
495 }
496 }
497 return inode;
498}
499
Chao Yu4d57b862018-05-30 00:20:41 +0800500void f2fs_update_inode(struct inode *inode, struct page *node_page)
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900501{
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900502 struct f2fs_inode *ri;
Chao Yub691d982016-10-11 22:57:05 +0800503 struct extent_tree *et = F2FS_I(inode)->extent_tree;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900504
Chao Yubae0ee72018-12-25 17:43:42 +0800505 f2fs_wait_on_page_writeback(node_page, NODE, true, true);
Yunlei He211a6fa2017-12-05 12:07:47 +0800506 set_page_dirty(node_page);
507
508 f2fs_inode_synced(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900509
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900510 ri = F2FS_INODE(node_page);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900511
512 ri->i_mode = cpu_to_le16(inode->i_mode);
513 ri->i_advise = F2FS_I(inode)->i_advise;
514 ri->i_uid = cpu_to_le32(i_uid_read(inode));
515 ri->i_gid = cpu_to_le32(i_gid_read(inode));
516 ri->i_links = cpu_to_le32(inode->i_nlink);
517 ri->i_size = cpu_to_le64(i_size_read(inode));
Chao Yu000519f2017-07-06 01:11:31 +0800518 ri->i_blocks = cpu_to_le64(SECTOR_TO_BLOCK(inode->i_blocks) + 1);
Chao Yu0c872e22015-02-05 17:46:29 +0800519
Chao Yub691d982016-10-11 22:57:05 +0800520 if (et) {
521 read_lock(&et->lock);
522 set_raw_extent(&et->largest, &ri->i_ext);
523 read_unlock(&et->lock);
524 } else {
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700525 memset(&ri->i_ext, 0, sizeof(ri->i_ext));
Chao Yub691d982016-10-11 22:57:05 +0800526 }
Jaegeuk Kim91942322016-05-20 10:13:22 -0700527 set_raw_inline(inode, ri);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900528
529 ri->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
530 ri->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
531 ri->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
532 ri->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
533 ri->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
534 ri->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
Chao Yu1c41e6802018-05-07 20:28:52 +0800535 if (S_ISDIR(inode->i_mode))
536 ri->i_current_depth =
537 cpu_to_le32(F2FS_I(inode)->i_current_depth);
538 else if (S_ISREG(inode->i_mode))
Chao Yu2ef79ec2018-05-07 20:28:54 +0800539 ri->i_gc_failures =
540 cpu_to_le16(F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN]);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900541 ri->i_xattr_nid = cpu_to_le32(F2FS_I(inode)->i_xattr_nid);
542 ri->i_flags = cpu_to_le32(F2FS_I(inode)->i_flags);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900543 ri->i_pino = cpu_to_le32(F2FS_I(inode)->i_pino);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900544 ri->i_generation = cpu_to_le32(inode->i_generation);
Jaegeuk Kim38431542014-02-27 18:20:00 +0900545 ri->i_dir_level = F2FS_I(inode)->i_dir_level;
Changman Lee7d79e752013-01-23 09:40:23 +0900546
Chao Yu5c571322017-07-26 00:01:41 +0800547 if (f2fs_has_extra_attr(inode)) {
Chao Yu7a2af762017-07-19 00:19:06 +0800548 ri->i_extra_isize = cpu_to_le16(F2FS_I(inode)->i_extra_isize);
549
Chao Yu7beb01f2018-10-24 18:34:26 +0800550 if (f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(inode)))
Chao Yu6afc6622017-09-06 21:59:50 +0800551 ri->i_inline_xattr_size =
552 cpu_to_le16(F2FS_I(inode)->i_inline_xattr_size);
553
Chao Yu7beb01f2018-10-24 18:34:26 +0800554 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)) &&
Chao Yu5c571322017-07-26 00:01:41 +0800555 F2FS_FITS_IN_INODE(ri, F2FS_I(inode)->i_extra_isize,
556 i_projid)) {
557 projid_t i_projid;
558
559 i_projid = from_kprojid(&init_user_ns,
560 F2FS_I(inode)->i_projid);
561 ri->i_projid = cpu_to_le32(i_projid);
562 }
Chao Yu1c1d35d2018-01-25 14:54:42 +0800563
Chao Yu7beb01f2018-10-24 18:34:26 +0800564 if (f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
Chao Yu1c1d35d2018-01-25 14:54:42 +0800565 F2FS_FITS_IN_INODE(ri, F2FS_I(inode)->i_extra_isize,
566 i_crtime)) {
567 ri->i_crtime =
568 cpu_to_le64(F2FS_I(inode)->i_crtime.tv_sec);
569 ri->i_crtime_nsec =
570 cpu_to_le32(F2FS_I(inode)->i_crtime.tv_nsec);
571 }
Chao Yu5c571322017-07-26 00:01:41 +0800572 }
573
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +0900574 __set_inode_rdev(inode, ri);
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800575
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -0800576 /* deleted inode */
577 if (inode->i_nlink == 0)
578 clear_inline_node(node_page);
579
Arnd Bergmann24b81df2018-06-20 10:02:19 +0200580 F2FS_I(inode)->i_disk_time[0] = inode->i_atime;
581 F2FS_I(inode)->i_disk_time[1] = inode->i_ctime;
582 F2FS_I(inode)->i_disk_time[2] = inode->i_mtime;
Jaegeuk Kim214c2462018-03-29 22:50:41 -0700583 F2FS_I(inode)->i_disk_time[3] = F2FS_I(inode)->i_crtime;
Weichao Guo54c55c42018-03-09 23:10:21 +0800584
585#ifdef CONFIG_F2FS_CHECK_FS
586 f2fs_inode_chksum_set(F2FS_I_SB(inode), node_page);
587#endif
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900588}
589
Chao Yu4d57b862018-05-30 00:20:41 +0800590void f2fs_update_inode_page(struct inode *inode)
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900591{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700592 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900593 struct page *node_page;
Jaegeuk Kim744602c2014-01-24 09:42:16 +0900594retry:
Chao Yu4d57b862018-05-30 00:20:41 +0800595 node_page = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kim744602c2014-01-24 09:42:16 +0900596 if (IS_ERR(node_page)) {
597 int err = PTR_ERR(node_page);
598 if (err == -ENOMEM) {
599 cond_resched();
600 goto retry;
601 } else if (err != -ENOENT) {
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -0700602 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim744602c2014-01-24 09:42:16 +0900603 }
Yunlei He211a6fa2017-12-05 12:07:47 +0800604 return;
Jaegeuk Kim744602c2014-01-24 09:42:16 +0900605 }
Chao Yu4d57b862018-05-30 00:20:41 +0800606 f2fs_update_inode(inode, node_page);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900607 f2fs_put_page(node_page, 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900608}
609
610int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc)
611{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700612 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900613
614 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
615 inode->i_ino == F2FS_META_INO(sbi))
616 return 0;
617
Chao Yufe1897e2019-09-27 18:01:35 +0800618 /*
619 * atime could be updated without dirtying f2fs inode in lazytime mode
620 */
621 if (f2fs_is_time_consistent(inode) &&
622 !is_inode_flag_set(inode, FI_DIRTY_INODE))
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900623 return 0;
624
Chao Yu00e09c02019-08-23 17:58:36 +0800625 if (!f2fs_is_checkpoint_ready(sbi))
Daniel Rosenberg43549942018-08-20 19:21:43 -0700626 return -ENOSPC;
627
Jaegeuk Kim39936832012-11-22 16:21:29 +0900628 /*
Jaegeuk Kimc5cd29d2015-09-12 11:25:30 -0700629 * We need to balance fs here to prevent from producing dirty node pages
Jaegeuk Kim39936832012-11-22 16:21:29 +0900630 * during the urgent cleaning time when runing out of free sections.
631 */
Chao Yu4d57b862018-05-30 00:20:41 +0800632 f2fs_update_inode_page(inode);
Jaegeuk Kima7881892017-04-20 13:51:57 -0700633 if (wbc && wbc->nr_to_write)
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800634 f2fs_balance_fs(sbi, true);
Jaegeuk Kim744602c2014-01-24 09:42:16 +0900635 return 0;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900636}
637
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900638/*
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900639 * Called at the last iput() if i_nlink is zero
640 */
641void f2fs_evict_inode(struct inode *inode)
642{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700643 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700644 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
Chao Yu13ec7292015-08-24 17:40:45 +0800645 int err = 0;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900646
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700647 /* some remained atomic pages should discarded */
Jaegeuk Kim1e843712014-12-09 06:08:59 -0800648 if (f2fs_is_atomic_file(inode))
Chao Yu4d57b862018-05-30 00:20:41 +0800649 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700650
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900651 trace_f2fs_evict_inode(inode);
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700652 truncate_inode_pages_final(&inode->i_data);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900653
654 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
655 inode->i_ino == F2FS_META_INO(sbi))
Chao Yudbf20cb2014-07-25 12:00:57 +0800656 goto out_clear;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900657
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -0700658 f2fs_bug_on(sbi, get_dirty_pages(inode));
Chao Yu4d57b862018-05-30 00:20:41 +0800659 f2fs_remove_dirty_inode(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900660
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700661 f2fs_destroy_extent_tree(inode);
662
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900663 if (inode->i_nlink || is_bad_inode(inode))
664 goto no_delete;
665
Chao Yuaf033b22018-09-20 20:05:00 +0800666 err = dquot_initialize(inode);
667 if (err) {
668 err = 0;
669 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
670 }
Chao Yu0abd6752017-07-09 00:13:07 +0800671
Chao Yu4d57b862018-05-30 00:20:41 +0800672 f2fs_remove_ino_entry(sbi, inode->i_ino, APPEND_INO);
673 f2fs_remove_ino_entry(sbi, inode->i_ino, UPDATE_INO);
674 f2fs_remove_ino_entry(sbi, inode->i_ino, FLUSH_INO);
Chao Yu60dcedc2016-11-02 20:43:21 +0800675
Changman Leed6212a52013-01-29 18:30:07 +0900676 sb_start_intwrite(inode->i_sb);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700677 set_inode_flag(inode, FI_NO_ALLOC);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900678 i_size_write(inode, 0);
Jaegeuk Kim4c0c2942016-05-03 09:22:18 -0700679retry:
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900680 if (F2FS_HAS_BLOCKS(inode))
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700681 err = f2fs_truncate(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900682
Jaegeuk Kim8c1b3c02017-03-07 13:32:20 -0800683 if (time_to_inject(sbi, FAULT_EVICT_INODE)) {
Chao Yuc45d6002019-11-01 17:53:23 +0800684 f2fs_show_injection_info(sbi, FAULT_EVICT_INODE);
Jaegeuk Kim8c1b3c02017-03-07 13:32:20 -0800685 err = -EIO;
686 }
Arnd Bergmann7fa750a2018-08-13 23:38:06 +0200687
Chao Yu13ec7292015-08-24 17:40:45 +0800688 if (!err) {
689 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +0800690 err = f2fs_remove_inode_page(inode);
Chao Yu13ec7292015-08-24 17:40:45 +0800691 f2fs_unlock_op(sbi);
Chao Yua11b9f62016-10-11 22:56:59 +0800692 if (err == -ENOENT)
693 err = 0;
Chao Yu13ec7292015-08-24 17:40:45 +0800694 }
Jaegeuk Kim39936832012-11-22 16:21:29 +0900695
Jaegeuk Kim4c0c2942016-05-03 09:22:18 -0700696 /* give more chances, if ENOMEM case */
697 if (err == -ENOMEM) {
698 err = 0;
699 goto retry;
700 }
701
Chao Yuaf033b22018-09-20 20:05:00 +0800702 if (err) {
Chao Yu4d57b862018-05-30 00:20:41 +0800703 f2fs_update_inode_page(inode);
Chao Yu0f1898f2019-07-19 11:51:11 +0800704 if (dquot_initialize_needed(inode))
705 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
Chao Yuaf033b22018-09-20 20:05:00 +0800706 }
Changman Leed6212a52013-01-29 18:30:07 +0900707 sb_end_intwrite(inode->i_sb);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900708no_delete:
Chao Yu0abd6752017-07-09 00:13:07 +0800709 dquot_drop(inode);
710
Chao Yud5e8f6c2015-07-15 17:28:53 +0800711 stat_dec_inline_xattr(inode);
Jaegeuk Kim3289c062014-10-13 20:00:16 -0700712 stat_dec_inline_dir(inode);
Jaegeuk Kime7a2bf22014-10-14 10:29:50 -0700713 stat_dec_inline_inode(inode);
Chao Yu0bdee482015-03-19 19:27:51 +0800714
Chao Yu33ac18a2019-08-15 19:45:35 +0800715 if (likely(!f2fs_cp_error(sbi) &&
Daniel Rosenberg43549942018-08-20 19:21:43 -0700716 !is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
Chao Yuca7d8022017-09-12 14:04:05 +0800717 f2fs_bug_on(sbi, is_inode_flag_set(inode, FI_DIRTY_INODE));
Jaegeuk Kim943973c2017-10-12 19:12:53 -0700718 else
719 f2fs_inode_synced(inode);
Chao Yuca7d8022017-09-12 14:04:05 +0800720
Jaegeuk Kim4f295442017-03-04 13:56:10 -0800721 /* ino == 0, if f2fs_new_inode() was failed t*/
722 if (inode->i_ino)
723 invalidate_mapping_pages(NODE_MAPPING(sbi), inode->i_ino,
724 inode->i_ino);
Chao Yu002a41c2014-08-04 09:54:58 +0800725 if (xnid)
726 invalidate_mapping_pages(NODE_MAPPING(sbi), xnid, xnid);
Chao Yu60dcedc2016-11-02 20:43:21 +0800727 if (inode->i_nlink) {
728 if (is_inode_flag_set(inode, FI_APPEND_WRITE))
Chao Yu4d57b862018-05-30 00:20:41 +0800729 f2fs_add_ino_entry(sbi, inode->i_ino, APPEND_INO);
Chao Yu60dcedc2016-11-02 20:43:21 +0800730 if (is_inode_flag_set(inode, FI_UPDATE_WRITE))
Chao Yu4d57b862018-05-30 00:20:41 +0800731 f2fs_add_ino_entry(sbi, inode->i_ino, UPDATE_INO);
Chao Yu60dcedc2016-11-02 20:43:21 +0800732 }
Jaegeuk Kim91942322016-05-20 10:13:22 -0700733 if (is_inode_flag_set(inode, FI_FREE_NID)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800734 f2fs_alloc_nid_failed(sbi, inode->i_ino);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700735 clear_inode_flag(inode, FI_FREE_NID);
Jaegeuk Kimd8c42562017-06-01 15:39:27 -0700736 } else {
Jaegeuk Kima4f843b2018-04-23 23:02:31 -0600737 /*
738 * If xattr nid is corrupted, we can reach out error condition,
Chao Yu4d57b862018-05-30 00:20:41 +0800739 * err & !f2fs_exist_written_data(sbi, inode->i_ino, ORPHAN_INO)).
740 * In that case, f2fs_check_nid_range() is enough to give a clue.
Jaegeuk Kima4f843b2018-04-23 23:02:31 -0600741 */
Jaegeuk Kimc9b63bd2015-06-23 10:36:08 -0700742 }
Chao Yudbf20cb2014-07-25 12:00:57 +0800743out_clear:
Eric Biggers3d204e22018-01-11 23:30:13 -0500744 fscrypt_put_encryption_info(inode);
Eric Biggers95ae2512019-07-22 09:26:24 -0700745 fsverity_cleanup_inode(inode);
Chao Yudbf20cb2014-07-25 12:00:57 +0800746 clear_inode(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900747}
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700748
749/* caller should call f2fs_lock_op() */
Chao Yu4d57b862018-05-30 00:20:41 +0800750void f2fs_handle_failed_inode(struct inode *inode)
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700751{
752 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700753 struct node_info ni;
Chao Yu77357302018-07-17 00:02:17 +0800754 int err;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700755
Chao Yua11b9f62016-10-11 22:56:59 +0800756 /*
757 * clear nlink of inode in order to release resource of inode
758 * immediately.
759 */
760 clear_nlink(inode);
761
762 /*
763 * we must call this to avoid inode being remained as dirty, resulting
764 * in a panic when flushing dirty inodes in gdirty_list.
765 */
Chao Yu4d57b862018-05-30 00:20:41 +0800766 f2fs_update_inode_page(inode);
Jaegeuk Kim9bb02c32017-04-11 19:01:26 -0700767 f2fs_inode_synced(inode);
Chao Yua11b9f62016-10-11 22:56:59 +0800768
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700769 /* don't make bad inode, since it becomes a regular file. */
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700770 unlock_new_inode(inode);
771
Chao Yu13ec7292015-08-24 17:40:45 +0800772 /*
Chao Yu13ec7292015-08-24 17:40:45 +0800773 * Note: we should add inode to orphan list before f2fs_unlock_op()
774 * so we can prevent losing this orphan when encoutering checkpoint
775 * and following suddenly power-off.
776 */
Chao Yu77357302018-07-17 00:02:17 +0800777 err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
778 if (err) {
779 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800780 f2fs_warn(sbi, "May loss orphan inode, run fsck to fix.");
Chao Yu77357302018-07-17 00:02:17 +0800781 goto out;
782 }
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700783
784 if (ni.blk_addr != NULL_ADDR) {
Chao Yu77357302018-07-17 00:02:17 +0800785 err = f2fs_acquire_orphan_inode(sbi);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700786 if (err) {
787 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800788 f2fs_warn(sbi, "Too many orphan inodes, run fsck to fix.");
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700789 } else {
Chao Yu4d57b862018-05-30 00:20:41 +0800790 f2fs_add_orphan_inode(inode);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700791 }
Chao Yu4d57b862018-05-30 00:20:41 +0800792 f2fs_alloc_nid_done(sbi, inode->i_ino);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700793 } else {
Jaegeuk Kim91942322016-05-20 10:13:22 -0700794 set_inode_flag(inode, FI_FREE_NID);
Chao Yu13ec7292015-08-24 17:40:45 +0800795 }
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700796
Chao Yu77357302018-07-17 00:02:17 +0800797out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700798 f2fs_unlock_op(sbi);
799
800 /* iput will drop the inode object */
801 iput(inode);
802}