blob: 66969ae852b978a9a8435d11d4ba5e8a88218c50 [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 Yu4c8ff702019-11-01 18:07:14 +0800203 struct f2fs_inode *ri = F2FS_INODE(node_page);
Chao Yue34438c2018-06-29 13:55:22 +0800204 unsigned long long iblocks;
205
206 iblocks = le64_to_cpu(F2FS_INODE(node_page)->i_blocks);
207 if (!iblocks) {
208 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800209 f2fs_warn(sbi, "%s: corrupted inode i_blocks i_ino=%lx iblocks=%llu, run fsck to fix.",
210 __func__, inode->i_ino, iblocks);
Chao Yue34438c2018-06-29 13:55:22 +0800211 return false;
212 }
213
214 if (ino_of_node(node_page) != nid_of_node(node_page)) {
215 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800216 f2fs_warn(sbi, "%s: corrupted inode footer i_ino=%lx, ino,nid: [%u, %u] run fsck to fix.",
217 __func__, inode->i_ino,
218 ino_of_node(node_page), nid_of_node(node_page));
Chao Yue34438c2018-06-29 13:55:22 +0800219 return false;
220 }
Jaegeuk Kim5d646002018-04-24 11:37:18 -0600221
Chao Yu7beb01f2018-10-24 18:34:26 +0800222 if (f2fs_sb_has_flexible_inline_xattr(sbi)
Jaegeuk Kim5d646002018-04-24 11:37:18 -0600223 && !f2fs_has_extra_attr(inode)) {
224 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800225 f2fs_warn(sbi, "%s: corrupted inode ino=%lx, run fsck to fix.",
226 __func__, inode->i_ino);
Jaegeuk Kim5d646002018-04-24 11:37:18 -0600227 return false;
228 }
Chao Yu76d56d42018-06-25 23:29:49 +0800229
230 if (f2fs_has_extra_attr(inode) &&
Chao Yu7beb01f2018-10-24 18:34:26 +0800231 !f2fs_sb_has_extra_attr(sbi)) {
Chao Yu76d56d42018-06-25 23:29:49 +0800232 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800233 f2fs_warn(sbi, "%s: inode (ino=%lx) is with extra_attr, but extra_attr feature is off",
234 __func__, inode->i_ino);
Chao Yu76d56d42018-06-25 23:29:49 +0800235 return false;
236 }
Chao Yuc9b60782018-08-01 19:13:44 +0800237
Chao Yu18dd6472018-07-08 22:16:55 +0800238 if (fi->i_extra_isize > F2FS_TOTAL_EXTRA_ATTR_SIZE ||
239 fi->i_extra_isize % sizeof(__le32)) {
240 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800241 f2fs_warn(sbi, "%s: inode (ino=%lx) has corrupted i_extra_isize: %d, max: %zu",
242 __func__, inode->i_ino, fi->i_extra_isize,
243 F2FS_TOTAL_EXTRA_ATTR_SIZE);
Chao Yu18dd6472018-07-08 22:16:55 +0800244 return false;
245 }
246
Chao Yudd6c89b2019-03-04 17:19:04 +0800247 if (f2fs_has_extra_attr(inode) &&
248 f2fs_sb_has_flexible_inline_xattr(sbi) &&
249 f2fs_has_inline_xattr(inode) &&
250 (!fi->i_inline_xattr_size ||
251 fi->i_inline_xattr_size > MAX_INLINE_XATTR_SIZE)) {
252 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800253 f2fs_warn(sbi, "%s: inode (ino=%lx) has corrupted i_inline_xattr_size: %d, max: %zu",
254 __func__, inode->i_ino, fi->i_inline_xattr_size,
255 MAX_INLINE_XATTR_SIZE);
Chao Yudd6c89b2019-03-04 17:19:04 +0800256 return false;
257 }
258
Chao Yuc9b60782018-08-01 19:13:44 +0800259 if (F2FS_I(inode)->extent_tree) {
260 struct extent_info *ei = &F2FS_I(inode)->extent_tree->largest;
261
262 if (ei->len &&
Chao Yu93770ab2019-04-15 15:26:32 +0800263 (!f2fs_is_valid_blkaddr(sbi, ei->blk,
264 DATA_GENERIC_ENHANCE) ||
Chao Yuc9b60782018-08-01 19:13:44 +0800265 !f2fs_is_valid_blkaddr(sbi, ei->blk + ei->len - 1,
Chao Yu93770ab2019-04-15 15:26:32 +0800266 DATA_GENERIC_ENHANCE))) {
Chao Yuc9b60782018-08-01 19:13:44 +0800267 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800268 f2fs_warn(sbi, "%s: inode (ino=%lx) extent info [%u, %u, %u] is incorrect, run fsck to fix",
269 __func__, inode->i_ino,
270 ei->blk, ei->fofs, ei->len);
Chao Yuc9b60782018-08-01 19:13:44 +0800271 return false;
272 }
273 }
Chao Yubcbfbd62018-06-29 00:19:25 +0800274
275 if (f2fs_has_inline_data(inode) &&
276 (!S_ISREG(inode->i_mode) && !S_ISLNK(inode->i_mode))) {
277 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800278 f2fs_warn(sbi, "%s: inode (ino=%lx, mode=%u) should not have inline_data, run fsck to fix",
279 __func__, inode->i_ino, inode->i_mode);
Chao Yubcbfbd62018-06-29 00:19:25 +0800280 return false;
281 }
282
283 if (f2fs_has_inline_dentry(inode) && !S_ISDIR(inode->i_mode)) {
284 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800285 f2fs_warn(sbi, "%s: inode (ino=%lx, mode=%u) should not have inline_dentry, run fsck to fix",
286 __func__, inode->i_ino, inode->i_mode);
Chao Yubcbfbd62018-06-29 00:19:25 +0800287 return false;
288 }
289
Chao Yu4c8ff702019-11-01 18:07:14 +0800290 if (f2fs_has_extra_attr(inode) && f2fs_sb_has_compression(sbi) &&
291 fi->i_flags & F2FS_COMPR_FL &&
292 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize,
293 i_log_cluster_size)) {
Chao Yud940aa02020-02-25 18:26:46 +0800294 if (ri->i_compress_algorithm >= COMPRESS_MAX) {
295 f2fs_warn(sbi, "%s: inode (ino=%lx) has unsupported "
296 "compress algorithm: %u, run fsck to fix",
297 __func__, inode->i_ino,
298 ri->i_compress_algorithm);
Chao Yu4c8ff702019-11-01 18:07:14 +0800299 return false;
Chao Yud940aa02020-02-25 18:26:46 +0800300 }
301 if (le64_to_cpu(ri->i_compr_blocks) >
302 SECTOR_TO_BLOCK(inode->i_blocks)) {
303 f2fs_warn(sbi, "%s: inode (ino=%lx) has inconsistent "
304 "i_compr_blocks:%llu, i_blocks:%llu, run fsck to fix",
305 __func__, inode->i_ino,
306 le64_to_cpu(ri->i_compr_blocks),
307 SECTOR_TO_BLOCK(inode->i_blocks));
Chao Yu4c8ff702019-11-01 18:07:14 +0800308 return false;
Chao Yud940aa02020-02-25 18:26:46 +0800309 }
Chao Yu4c8ff702019-11-01 18:07:14 +0800310 if (ri->i_log_cluster_size < MIN_COMPRESS_LOG_SIZE ||
Chao Yud940aa02020-02-25 18:26:46 +0800311 ri->i_log_cluster_size > MAX_COMPRESS_LOG_SIZE) {
312 f2fs_warn(sbi, "%s: inode (ino=%lx) has unsupported "
313 "log cluster size: %u, run fsck to fix",
314 __func__, inode->i_ino,
315 ri->i_log_cluster_size);
Chao Yu4c8ff702019-11-01 18:07:14 +0800316 return false;
Chao Yud940aa02020-02-25 18:26:46 +0800317 }
Chao Yu4c8ff702019-11-01 18:07:14 +0800318 }
319
Jaegeuk Kim5d646002018-04-24 11:37:18 -0600320 return true;
321}
322
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900323static int do_read_inode(struct inode *inode)
324{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700325 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900326 struct f2fs_inode_info *fi = F2FS_I(inode);
327 struct page *node_page;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900328 struct f2fs_inode *ri;
Chao Yu5c571322017-07-26 00:01:41 +0800329 projid_t i_projid;
Chao Yu91291e92018-07-10 23:01:45 +0800330 int err;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900331
332 /* Check if ino is within scope */
Chao Yu4d57b862018-05-30 00:20:41 +0800333 if (f2fs_check_nid_range(sbi, inode->i_ino))
Namjae Jeon064e0822013-03-17 17:27:20 +0900334 return -EINVAL;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900335
Chao Yu4d57b862018-05-30 00:20:41 +0800336 node_page = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900337 if (IS_ERR(node_page))
338 return PTR_ERR(node_page);
339
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900340 ri = F2FS_INODE(node_page);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900341
342 inode->i_mode = le16_to_cpu(ri->i_mode);
343 i_uid_write(inode, le32_to_cpu(ri->i_uid));
344 i_gid_write(inode, le32_to_cpu(ri->i_gid));
345 set_nlink(inode, le32_to_cpu(ri->i_links));
346 inode->i_size = le64_to_cpu(ri->i_size);
Chao Yu000519f2017-07-06 01:11:31 +0800347 inode->i_blocks = SECTOR_FROM_BLOCK(le64_to_cpu(ri->i_blocks) - 1);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900348
349 inode->i_atime.tv_sec = le64_to_cpu(ri->i_atime);
350 inode->i_ctime.tv_sec = le64_to_cpu(ri->i_ctime);
351 inode->i_mtime.tv_sec = le64_to_cpu(ri->i_mtime);
352 inode->i_atime.tv_nsec = le32_to_cpu(ri->i_atime_nsec);
353 inode->i_ctime.tv_nsec = le32_to_cpu(ri->i_ctime_nsec);
354 inode->i_mtime.tv_nsec = le32_to_cpu(ri->i_mtime_nsec);
355 inode->i_generation = le32_to_cpu(ri->i_generation);
Chao Yu1c41e6802018-05-07 20:28:52 +0800356 if (S_ISDIR(inode->i_mode))
357 fi->i_current_depth = le32_to_cpu(ri->i_current_depth);
358 else if (S_ISREG(inode->i_mode))
Chao Yu2ef79ec2018-05-07 20:28:54 +0800359 fi->i_gc_failures[GC_FAILURE_PIN] =
360 le16_to_cpu(ri->i_gc_failures);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900361 fi->i_xattr_nid = le32_to_cpu(ri->i_xattr_nid);
362 fi->i_flags = le32_to_cpu(ri->i_flags);
Wang Shilong5043a962019-06-13 16:29:53 +0900363 if (S_ISREG(inode->i_mode))
364 fi->i_flags &= ~F2FS_PROJINHERIT_FL;
Chao Yu7653b9d2020-03-23 11:18:07 +0800365 bitmap_zero(fi->flags, FI_MAX);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900366 fi->i_advise = ri->i_advise;
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900367 fi->i_pino = le32_to_cpu(ri->i_pino);
Jaegeuk Kim38431542014-02-27 18:20:00 +0900368 fi->i_dir_level = ri->i_dir_level;
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +0900369
Chao Yua6d601f2020-06-28 10:58:17 +0800370 f2fs_init_extent_tree(inode, node_page);
Chao Yu0c872e22015-02-05 17:46:29 +0800371
Jaegeuk Kim91942322016-05-20 10:13:22 -0700372 get_inline_info(inode, ri);
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +0900373
Chao Yu7a2af762017-07-19 00:19:06 +0800374 fi->i_extra_isize = f2fs_has_extra_attr(inode) ?
375 le16_to_cpu(ri->i_extra_isize) : 0;
376
Chao Yu7beb01f2018-10-24 18:34:26 +0800377 if (f2fs_sb_has_flexible_inline_xattr(sbi)) {
Chao Yu6afc6622017-09-06 21:59:50 +0800378 fi->i_inline_xattr_size = le16_to_cpu(ri->i_inline_xattr_size);
379 } else if (f2fs_has_inline_xattr(inode) ||
380 f2fs_has_inline_dentry(inode)) {
381 fi->i_inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
382 } else {
383
384 /*
385 * Previous inline data or directory always reserved 200 bytes
386 * in inode layout, even if inline_xattr is disabled. In order
387 * to keep inline_dentry's structure for backward compatibility,
388 * we get the space back only from inline_data.
389 */
390 fi->i_inline_xattr_size = 0;
391 }
392
Chao Yu18dd6472018-07-08 22:16:55 +0800393 if (!sanity_check_inode(inode, node_page)) {
394 f2fs_put_page(node_page, 1);
Chao Yu10f966b2019-06-20 11:36:14 +0800395 return -EFSCORRUPTED;
Chao Yu18dd6472018-07-08 22:16:55 +0800396 }
397
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700398 /* check data exist */
399 if (f2fs_has_inline_data(inode) && !f2fs_exist_data(inode))
Chao Yu9e5ba772015-01-06 14:28:43 +0800400 __recover_inline_status(inode, node_page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700401
Chao Yuef2a0072018-10-03 22:32:44 +0800402 /* try to recover cold bit for non-dir inode */
403 if (!S_ISDIR(inode->i_mode) && !is_cold_node(node_page)) {
Wuyun Zhaodb5ae362020-06-18 10:58:37 +0800404 f2fs_wait_on_page_writeback(node_page, NODE, true, true);
Chao Yuef2a0072018-10-03 22:32:44 +0800405 set_cold_node(node_page, false);
406 set_page_dirty(node_page);
407 }
408
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +0900409 /* get rdev by using inline_info */
410 __get_inode_rdev(inode, ri);
411
Chao Yudda9f4b2018-08-11 23:42:09 +0800412 if (S_ISREG(inode->i_mode)) {
413 err = __written_first_block(sbi, ri);
414 if (err < 0) {
415 f2fs_put_page(node_page, 1);
416 return err;
417 }
418 if (!err)
419 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Chao Yu91291e92018-07-10 23:01:45 +0800420 }
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -0700421
Chao Yu4d57b862018-05-30 00:20:41 +0800422 if (!f2fs_need_inode_block_update(sbi, inode->i_ino))
Jaegeuk Kim26de9b12016-05-20 20:42:37 -0700423 fi->last_disk_size = inode->i_size;
424
Chao Yu59c84402018-04-03 15:08:17 +0800425 if (fi->i_flags & F2FS_PROJINHERIT_FL)
Chao Yu5c571322017-07-26 00:01:41 +0800426 set_inode_flag(inode, FI_PROJ_INHERIT);
427
Chao Yu7beb01f2018-10-24 18:34:26 +0800428 if (f2fs_has_extra_attr(inode) && f2fs_sb_has_project_quota(sbi) &&
Chao Yu5c571322017-07-26 00:01:41 +0800429 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_projid))
430 i_projid = (projid_t)le32_to_cpu(ri->i_projid);
431 else
432 i_projid = F2FS_DEF_PROJID;
433 fi->i_projid = make_kprojid(&init_user_ns, i_projid);
434
Chao Yu7beb01f2018-10-24 18:34:26 +0800435 if (f2fs_has_extra_attr(inode) && f2fs_sb_has_inode_crtime(sbi) &&
Chao Yu1c1d35d2018-01-25 14:54:42 +0800436 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
437 fi->i_crtime.tv_sec = le64_to_cpu(ri->i_crtime);
438 fi->i_crtime.tv_nsec = le32_to_cpu(ri->i_crtime_nsec);
439 }
440
Chao Yu4c8ff702019-11-01 18:07:14 +0800441 if (f2fs_has_extra_attr(inode) && f2fs_sb_has_compression(sbi) &&
442 (fi->i_flags & F2FS_COMPR_FL)) {
443 if (F2FS_FITS_IN_INODE(ri, fi->i_extra_isize,
444 i_log_cluster_size)) {
445 fi->i_compr_blocks = le64_to_cpu(ri->i_compr_blocks);
446 fi->i_compress_algorithm = ri->i_compress_algorithm;
447 fi->i_log_cluster_size = ri->i_log_cluster_size;
448 fi->i_cluster_size = 1 << fi->i_log_cluster_size;
449 set_inode_flag(inode, FI_COMPRESSED_FILE);
450 }
451 }
452
Arnd Bergmann24b81df2018-06-20 10:02:19 +0200453 F2FS_I(inode)->i_disk_time[0] = inode->i_atime;
454 F2FS_I(inode)->i_disk_time[1] = inode->i_ctime;
455 F2FS_I(inode)->i_disk_time[2] = inode->i_mtime;
Jaegeuk Kim214c2462018-03-29 22:50:41 -0700456 F2FS_I(inode)->i_disk_time[3] = F2FS_I(inode)->i_crtime;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900457 f2fs_put_page(node_page, 1);
Jaegeuk Kim9d1015d2014-12-05 10:51:50 -0800458
Chao Yud5e8f6c2015-07-15 17:28:53 +0800459 stat_inc_inline_xattr(inode);
Jaegeuk Kim9d1015d2014-12-05 10:51:50 -0800460 stat_inc_inline_inode(inode);
461 stat_inc_inline_dir(inode);
Chao Yu4c8ff702019-11-01 18:07:14 +0800462 stat_inc_compr_inode(inode);
463 stat_add_compr_blocks(inode, F2FS_I(inode)->i_compr_blocks);
Jaegeuk Kim9d1015d2014-12-05 10:51:50 -0800464
Chao Yu9e5ba772015-01-06 14:28:43 +0800465 return 0;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900466}
467
468struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
469{
470 struct f2fs_sb_info *sbi = F2FS_SB(sb);
471 struct inode *inode;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900472 int ret = 0;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900473
474 inode = iget_locked(sb, ino);
475 if (!inode)
476 return ERR_PTR(-ENOMEM);
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900477
478 if (!(inode->i_state & I_NEW)) {
479 trace_f2fs_iget(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900480 return inode;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900481 }
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900482 if (ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi))
483 goto make_now;
484
485 ret = do_read_inode(inode);
486 if (ret)
487 goto bad_inode;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900488make_now:
489 if (ino == F2FS_NODE_INO(sbi)) {
490 inode->i_mapping->a_ops = &f2fs_node_aops;
Chao Yu81114ba2018-04-09 20:25:06 +0800491 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900492 } else if (ino == F2FS_META_INO(sbi)) {
493 inode->i_mapping->a_ops = &f2fs_meta_aops;
Chao Yu81114ba2018-04-09 20:25:06 +0800494 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900495 } else if (S_ISREG(inode->i_mode)) {
496 inode->i_op = &f2fs_file_inode_operations;
497 inode->i_fop = &f2fs_file_operations;
498 inode->i_mapping->a_ops = &f2fs_dblock_aops;
499 } else if (S_ISDIR(inode->i_mode)) {
500 inode->i_op = &f2fs_dir_inode_operations;
501 inode->i_fop = &f2fs_dir_operations;
502 inode->i_mapping->a_ops = &f2fs_dblock_aops;
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800503 inode_nohighmem(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900504 } else if (S_ISLNK(inode->i_mode)) {
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530505 if (file_is_encrypt(inode))
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700506 inode->i_op = &f2fs_encrypted_symlink_inode_operations;
507 else
508 inode->i_op = &f2fs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -0500509 inode_nohighmem(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900510 inode->i_mapping->a_ops = &f2fs_dblock_aops;
511 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
512 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
513 inode->i_op = &f2fs_special_inode_operations;
514 init_special_inode(inode, inode->i_mode, inode->i_rdev);
515 } else {
516 ret = -EIO;
517 goto bad_inode;
518 }
Jaegeuk Kim93607122017-05-16 13:20:16 -0700519 f2fs_set_inode_flags(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900520 unlock_new_inode(inode);
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900521 trace_f2fs_iget(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900522 return inode;
523
524bad_inode:
Chao Yu546d22f2019-04-15 15:28:33 +0800525 f2fs_inode_synced(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900526 iget_failed(inode);
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900527 trace_f2fs_iget_exit(inode, ret);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900528 return ERR_PTR(ret);
529}
530
Jaegeuk Kime8ea9b32016-09-09 16:59:39 -0700531struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino)
532{
533 struct inode *inode;
534retry:
535 inode = f2fs_iget(sb, ino);
536 if (IS_ERR(inode)) {
537 if (PTR_ERR(inode) == -ENOMEM) {
Chao Yu5df7731f2020-02-17 17:45:44 +0800538 congestion_wait(BLK_RW_ASYNC, DEFAULT_IO_TIMEOUT);
Jaegeuk Kime8ea9b32016-09-09 16:59:39 -0700539 goto retry;
540 }
541 }
542 return inode;
543}
544
Chao Yu4d57b862018-05-30 00:20:41 +0800545void f2fs_update_inode(struct inode *inode, struct page *node_page)
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900546{
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900547 struct f2fs_inode *ri;
Chao Yub691d982016-10-11 22:57:05 +0800548 struct extent_tree *et = F2FS_I(inode)->extent_tree;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900549
Chao Yubae0ee72018-12-25 17:43:42 +0800550 f2fs_wait_on_page_writeback(node_page, NODE, true, true);
Yunlei He211a6fa2017-12-05 12:07:47 +0800551 set_page_dirty(node_page);
552
553 f2fs_inode_synced(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900554
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900555 ri = F2FS_INODE(node_page);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900556
557 ri->i_mode = cpu_to_le16(inode->i_mode);
558 ri->i_advise = F2FS_I(inode)->i_advise;
559 ri->i_uid = cpu_to_le32(i_uid_read(inode));
560 ri->i_gid = cpu_to_le32(i_gid_read(inode));
561 ri->i_links = cpu_to_le32(inode->i_nlink);
562 ri->i_size = cpu_to_le64(i_size_read(inode));
Chao Yu000519f2017-07-06 01:11:31 +0800563 ri->i_blocks = cpu_to_le64(SECTOR_TO_BLOCK(inode->i_blocks) + 1);
Chao Yu0c872e22015-02-05 17:46:29 +0800564
Chao Yub691d982016-10-11 22:57:05 +0800565 if (et) {
566 read_lock(&et->lock);
567 set_raw_extent(&et->largest, &ri->i_ext);
568 read_unlock(&et->lock);
569 } else {
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700570 memset(&ri->i_ext, 0, sizeof(ri->i_ext));
Chao Yub691d982016-10-11 22:57:05 +0800571 }
Jaegeuk Kim91942322016-05-20 10:13:22 -0700572 set_raw_inline(inode, ri);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900573
574 ri->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
575 ri->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
576 ri->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
577 ri->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
578 ri->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
579 ri->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
Chao Yu1c41e6802018-05-07 20:28:52 +0800580 if (S_ISDIR(inode->i_mode))
581 ri->i_current_depth =
582 cpu_to_le32(F2FS_I(inode)->i_current_depth);
583 else if (S_ISREG(inode->i_mode))
Chao Yu2ef79ec2018-05-07 20:28:54 +0800584 ri->i_gc_failures =
585 cpu_to_le16(F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN]);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900586 ri->i_xattr_nid = cpu_to_le32(F2FS_I(inode)->i_xattr_nid);
587 ri->i_flags = cpu_to_le32(F2FS_I(inode)->i_flags);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900588 ri->i_pino = cpu_to_le32(F2FS_I(inode)->i_pino);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900589 ri->i_generation = cpu_to_le32(inode->i_generation);
Jaegeuk Kim38431542014-02-27 18:20:00 +0900590 ri->i_dir_level = F2FS_I(inode)->i_dir_level;
Changman Lee7d79e752013-01-23 09:40:23 +0900591
Chao Yu5c571322017-07-26 00:01:41 +0800592 if (f2fs_has_extra_attr(inode)) {
Chao Yu7a2af762017-07-19 00:19:06 +0800593 ri->i_extra_isize = cpu_to_le16(F2FS_I(inode)->i_extra_isize);
594
Chao Yu7beb01f2018-10-24 18:34:26 +0800595 if (f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(inode)))
Chao Yu6afc6622017-09-06 21:59:50 +0800596 ri->i_inline_xattr_size =
597 cpu_to_le16(F2FS_I(inode)->i_inline_xattr_size);
598
Chao Yu7beb01f2018-10-24 18:34:26 +0800599 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)) &&
Chao Yu5c571322017-07-26 00:01:41 +0800600 F2FS_FITS_IN_INODE(ri, F2FS_I(inode)->i_extra_isize,
601 i_projid)) {
602 projid_t i_projid;
603
604 i_projid = from_kprojid(&init_user_ns,
605 F2FS_I(inode)->i_projid);
606 ri->i_projid = cpu_to_le32(i_projid);
607 }
Chao Yu1c1d35d2018-01-25 14:54:42 +0800608
Chao Yu7beb01f2018-10-24 18:34:26 +0800609 if (f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
Chao Yu1c1d35d2018-01-25 14:54:42 +0800610 F2FS_FITS_IN_INODE(ri, F2FS_I(inode)->i_extra_isize,
611 i_crtime)) {
612 ri->i_crtime =
613 cpu_to_le64(F2FS_I(inode)->i_crtime.tv_sec);
614 ri->i_crtime_nsec =
615 cpu_to_le32(F2FS_I(inode)->i_crtime.tv_nsec);
616 }
Chao Yu4c8ff702019-11-01 18:07:14 +0800617
618 if (f2fs_sb_has_compression(F2FS_I_SB(inode)) &&
619 F2FS_FITS_IN_INODE(ri, F2FS_I(inode)->i_extra_isize,
620 i_log_cluster_size)) {
621 ri->i_compr_blocks =
622 cpu_to_le64(F2FS_I(inode)->i_compr_blocks);
623 ri->i_compress_algorithm =
624 F2FS_I(inode)->i_compress_algorithm;
625 ri->i_log_cluster_size =
626 F2FS_I(inode)->i_log_cluster_size;
627 }
Chao Yu5c571322017-07-26 00:01:41 +0800628 }
629
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +0900630 __set_inode_rdev(inode, ri);
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800631
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -0800632 /* deleted inode */
633 if (inode->i_nlink == 0)
634 clear_inline_node(node_page);
635
Arnd Bergmann24b81df2018-06-20 10:02:19 +0200636 F2FS_I(inode)->i_disk_time[0] = inode->i_atime;
637 F2FS_I(inode)->i_disk_time[1] = inode->i_ctime;
638 F2FS_I(inode)->i_disk_time[2] = inode->i_mtime;
Jaegeuk Kim214c2462018-03-29 22:50:41 -0700639 F2FS_I(inode)->i_disk_time[3] = F2FS_I(inode)->i_crtime;
Weichao Guo54c55c42018-03-09 23:10:21 +0800640
641#ifdef CONFIG_F2FS_CHECK_FS
642 f2fs_inode_chksum_set(F2FS_I_SB(inode), node_page);
643#endif
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900644}
645
Chao Yu4d57b862018-05-30 00:20:41 +0800646void f2fs_update_inode_page(struct inode *inode)
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900647{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700648 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900649 struct page *node_page;
Jaegeuk Kim744602c2014-01-24 09:42:16 +0900650retry:
Chao Yu4d57b862018-05-30 00:20:41 +0800651 node_page = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kim744602c2014-01-24 09:42:16 +0900652 if (IS_ERR(node_page)) {
653 int err = PTR_ERR(node_page);
654 if (err == -ENOMEM) {
655 cond_resched();
656 goto retry;
657 } else if (err != -ENOENT) {
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -0700658 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim744602c2014-01-24 09:42:16 +0900659 }
Yunlei He211a6fa2017-12-05 12:07:47 +0800660 return;
Jaegeuk Kim744602c2014-01-24 09:42:16 +0900661 }
Chao Yu4d57b862018-05-30 00:20:41 +0800662 f2fs_update_inode(inode, node_page);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900663 f2fs_put_page(node_page, 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900664}
665
666int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc)
667{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700668 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900669
670 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
671 inode->i_ino == F2FS_META_INO(sbi))
672 return 0;
673
Chao Yufe1897e2019-09-27 18:01:35 +0800674 /*
675 * atime could be updated without dirtying f2fs inode in lazytime mode
676 */
677 if (f2fs_is_time_consistent(inode) &&
678 !is_inode_flag_set(inode, FI_DIRTY_INODE))
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900679 return 0;
680
Chao Yu00e09c02019-08-23 17:58:36 +0800681 if (!f2fs_is_checkpoint_ready(sbi))
Daniel Rosenberg43549942018-08-20 19:21:43 -0700682 return -ENOSPC;
683
Jaegeuk Kim39936832012-11-22 16:21:29 +0900684 /*
Jaegeuk Kimc5cd29d2015-09-12 11:25:30 -0700685 * We need to balance fs here to prevent from producing dirty node pages
Jaegeuk Kim39936832012-11-22 16:21:29 +0900686 * during the urgent cleaning time when runing out of free sections.
687 */
Chao Yu4d57b862018-05-30 00:20:41 +0800688 f2fs_update_inode_page(inode);
Jaegeuk Kima7881892017-04-20 13:51:57 -0700689 if (wbc && wbc->nr_to_write)
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800690 f2fs_balance_fs(sbi, true);
Jaegeuk Kim744602c2014-01-24 09:42:16 +0900691 return 0;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900692}
693
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900694/*
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900695 * Called at the last iput() if i_nlink is zero
696 */
697void f2fs_evict_inode(struct inode *inode)
698{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700699 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700700 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
Chao Yu13ec7292015-08-24 17:40:45 +0800701 int err = 0;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900702
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700703 /* some remained atomic pages should discarded */
Jaegeuk Kim1e843712014-12-09 06:08:59 -0800704 if (f2fs_is_atomic_file(inode))
Chao Yu4d57b862018-05-30 00:20:41 +0800705 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700706
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900707 trace_f2fs_evict_inode(inode);
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700708 truncate_inode_pages_final(&inode->i_data);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900709
710 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
711 inode->i_ino == F2FS_META_INO(sbi))
Chao Yudbf20cb2014-07-25 12:00:57 +0800712 goto out_clear;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900713
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -0700714 f2fs_bug_on(sbi, get_dirty_pages(inode));
Chao Yu4d57b862018-05-30 00:20:41 +0800715 f2fs_remove_dirty_inode(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900716
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700717 f2fs_destroy_extent_tree(inode);
718
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900719 if (inode->i_nlink || is_bad_inode(inode))
720 goto no_delete;
721
Chao Yuaf033b22018-09-20 20:05:00 +0800722 err = dquot_initialize(inode);
723 if (err) {
724 err = 0;
725 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
726 }
Chao Yu0abd6752017-07-09 00:13:07 +0800727
Chao Yu4d57b862018-05-30 00:20:41 +0800728 f2fs_remove_ino_entry(sbi, inode->i_ino, APPEND_INO);
729 f2fs_remove_ino_entry(sbi, inode->i_ino, UPDATE_INO);
730 f2fs_remove_ino_entry(sbi, inode->i_ino, FLUSH_INO);
Chao Yu60dcedc2016-11-02 20:43:21 +0800731
Changman Leed6212a52013-01-29 18:30:07 +0900732 sb_start_intwrite(inode->i_sb);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700733 set_inode_flag(inode, FI_NO_ALLOC);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900734 i_size_write(inode, 0);
Jaegeuk Kim4c0c2942016-05-03 09:22:18 -0700735retry:
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900736 if (F2FS_HAS_BLOCKS(inode))
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700737 err = f2fs_truncate(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900738
Jaegeuk Kim8c1b3c02017-03-07 13:32:20 -0800739 if (time_to_inject(sbi, FAULT_EVICT_INODE)) {
Chao Yuc45d6002019-11-01 17:53:23 +0800740 f2fs_show_injection_info(sbi, FAULT_EVICT_INODE);
Jaegeuk Kim8c1b3c02017-03-07 13:32:20 -0800741 err = -EIO;
742 }
Arnd Bergmann7fa750a2018-08-13 23:38:06 +0200743
Chao Yu13ec7292015-08-24 17:40:45 +0800744 if (!err) {
745 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +0800746 err = f2fs_remove_inode_page(inode);
Chao Yu13ec7292015-08-24 17:40:45 +0800747 f2fs_unlock_op(sbi);
Chao Yua11b9f62016-10-11 22:56:59 +0800748 if (err == -ENOENT)
749 err = 0;
Chao Yu13ec7292015-08-24 17:40:45 +0800750 }
Jaegeuk Kim39936832012-11-22 16:21:29 +0900751
Jaegeuk Kim4c0c2942016-05-03 09:22:18 -0700752 /* give more chances, if ENOMEM case */
753 if (err == -ENOMEM) {
754 err = 0;
755 goto retry;
756 }
757
Chao Yuaf033b22018-09-20 20:05:00 +0800758 if (err) {
Chao Yu4d57b862018-05-30 00:20:41 +0800759 f2fs_update_inode_page(inode);
Chao Yu0f1898f2019-07-19 11:51:11 +0800760 if (dquot_initialize_needed(inode))
761 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
Chao Yuaf033b22018-09-20 20:05:00 +0800762 }
Changman Leed6212a52013-01-29 18:30:07 +0900763 sb_end_intwrite(inode->i_sb);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900764no_delete:
Chao Yu0abd6752017-07-09 00:13:07 +0800765 dquot_drop(inode);
766
Chao Yud5e8f6c2015-07-15 17:28:53 +0800767 stat_dec_inline_xattr(inode);
Jaegeuk Kim3289c062014-10-13 20:00:16 -0700768 stat_dec_inline_dir(inode);
Jaegeuk Kime7a2bf22014-10-14 10:29:50 -0700769 stat_dec_inline_inode(inode);
Chao Yu4c8ff702019-11-01 18:07:14 +0800770 stat_dec_compr_inode(inode);
771 stat_sub_compr_blocks(inode, F2FS_I(inode)->i_compr_blocks);
Chao Yu0bdee482015-03-19 19:27:51 +0800772
Chao Yu33ac18a2019-08-15 19:45:35 +0800773 if (likely(!f2fs_cp_error(sbi) &&
Daniel Rosenberg43549942018-08-20 19:21:43 -0700774 !is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
Chao Yuca7d8022017-09-12 14:04:05 +0800775 f2fs_bug_on(sbi, is_inode_flag_set(inode, FI_DIRTY_INODE));
Jaegeuk Kim943973c2017-10-12 19:12:53 -0700776 else
777 f2fs_inode_synced(inode);
Chao Yuca7d8022017-09-12 14:04:05 +0800778
Chao Yu7a88ddb2020-02-27 19:30:05 +0800779 /* for the case f2fs_new_inode() was failed, .i_ino is zero, skip it */
Jaegeuk Kim4f295442017-03-04 13:56:10 -0800780 if (inode->i_ino)
781 invalidate_mapping_pages(NODE_MAPPING(sbi), inode->i_ino,
782 inode->i_ino);
Chao Yu002a41c2014-08-04 09:54:58 +0800783 if (xnid)
784 invalidate_mapping_pages(NODE_MAPPING(sbi), xnid, xnid);
Chao Yu60dcedc2016-11-02 20:43:21 +0800785 if (inode->i_nlink) {
786 if (is_inode_flag_set(inode, FI_APPEND_WRITE))
Chao Yu4d57b862018-05-30 00:20:41 +0800787 f2fs_add_ino_entry(sbi, inode->i_ino, APPEND_INO);
Chao Yu60dcedc2016-11-02 20:43:21 +0800788 if (is_inode_flag_set(inode, FI_UPDATE_WRITE))
Chao Yu4d57b862018-05-30 00:20:41 +0800789 f2fs_add_ino_entry(sbi, inode->i_ino, UPDATE_INO);
Chao Yu60dcedc2016-11-02 20:43:21 +0800790 }
Jaegeuk Kim91942322016-05-20 10:13:22 -0700791 if (is_inode_flag_set(inode, FI_FREE_NID)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800792 f2fs_alloc_nid_failed(sbi, inode->i_ino);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700793 clear_inode_flag(inode, FI_FREE_NID);
Jaegeuk Kimd8c42562017-06-01 15:39:27 -0700794 } else {
Jaegeuk Kima4f843b2018-04-23 23:02:31 -0600795 /*
796 * If xattr nid is corrupted, we can reach out error condition,
Chao Yu4d57b862018-05-30 00:20:41 +0800797 * err & !f2fs_exist_written_data(sbi, inode->i_ino, ORPHAN_INO)).
798 * In that case, f2fs_check_nid_range() is enough to give a clue.
Jaegeuk Kima4f843b2018-04-23 23:02:31 -0600799 */
Jaegeuk Kimc9b63bd2015-06-23 10:36:08 -0700800 }
Chao Yudbf20cb2014-07-25 12:00:57 +0800801out_clear:
Eric Biggers3d204e22018-01-11 23:30:13 -0500802 fscrypt_put_encryption_info(inode);
Eric Biggers95ae2512019-07-22 09:26:24 -0700803 fsverity_cleanup_inode(inode);
Chao Yudbf20cb2014-07-25 12:00:57 +0800804 clear_inode(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900805}
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700806
807/* caller should call f2fs_lock_op() */
Chao Yu4d57b862018-05-30 00:20:41 +0800808void f2fs_handle_failed_inode(struct inode *inode)
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700809{
810 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700811 struct node_info ni;
Chao Yu77357302018-07-17 00:02:17 +0800812 int err;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700813
Chao Yua11b9f62016-10-11 22:56:59 +0800814 /*
815 * clear nlink of inode in order to release resource of inode
816 * immediately.
817 */
818 clear_nlink(inode);
819
820 /*
821 * we must call this to avoid inode being remained as dirty, resulting
822 * in a panic when flushing dirty inodes in gdirty_list.
823 */
Chao Yu4d57b862018-05-30 00:20:41 +0800824 f2fs_update_inode_page(inode);
Jaegeuk Kim9bb02c32017-04-11 19:01:26 -0700825 f2fs_inode_synced(inode);
Chao Yua11b9f62016-10-11 22:56:59 +0800826
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700827 /* don't make bad inode, since it becomes a regular file. */
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700828 unlock_new_inode(inode);
829
Chao Yu13ec7292015-08-24 17:40:45 +0800830 /*
Chao Yu13ec7292015-08-24 17:40:45 +0800831 * Note: we should add inode to orphan list before f2fs_unlock_op()
832 * so we can prevent losing this orphan when encoutering checkpoint
833 * and following suddenly power-off.
834 */
Chao Yu77357302018-07-17 00:02:17 +0800835 err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
836 if (err) {
837 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800838 f2fs_warn(sbi, "May loss orphan inode, run fsck to fix.");
Chao Yu77357302018-07-17 00:02:17 +0800839 goto out;
840 }
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700841
842 if (ni.blk_addr != NULL_ADDR) {
Chao Yu77357302018-07-17 00:02:17 +0800843 err = f2fs_acquire_orphan_inode(sbi);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700844 if (err) {
845 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800846 f2fs_warn(sbi, "Too many orphan inodes, run fsck to fix.");
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700847 } else {
Chao Yu4d57b862018-05-30 00:20:41 +0800848 f2fs_add_orphan_inode(inode);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700849 }
Chao Yu4d57b862018-05-30 00:20:41 +0800850 f2fs_alloc_nid_done(sbi, inode->i_ino);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700851 } else {
Jaegeuk Kim91942322016-05-20 10:13:22 -0700852 set_inode_flag(inode, FI_FREE_NID);
Chao Yu13ec7292015-08-24 17:40:45 +0800853 }
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700854
Chao Yu77357302018-07-17 00:02:17 +0800855out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700856 f2fs_unlock_op(sbi);
857
858 /* iput will drop the inode object */
859 iput(inode);
860}