blob: 4ee9d6c4b71916afab0200297400ed44701d1011 [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"
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +090017
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090018#include <trace/events/f2fs.h>
19
Jaegeuk Kim7c457292016-10-14 11:51:23 -070020void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync)
Jaegeuk Kimb56ab832016-06-30 19:09:37 -070021{
Daeho Jeong9ac1e2d2018-01-11 11:26:19 +090022 if (is_inode_flag_set(inode, FI_NEW_INODE))
23 return;
24
Jaegeuk Kim7c457292016-10-14 11:51:23 -070025 if (f2fs_inode_dirtied(inode, sync))
Jaegeuk Kimb56ab832016-06-30 19:09:37 -070026 return;
Jaegeuk Kim7c457292016-10-14 11:51:23 -070027
Jaegeuk Kimb56ab832016-06-30 19:09:37 -070028 mark_inode_dirty_sync(inode);
29}
30
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +090031void f2fs_set_inode_flags(struct inode *inode)
32{
33 unsigned int flags = F2FS_I(inode)->i_flags;
Zhang Zhen8abfb362014-04-15 14:19:38 +080034 unsigned int new_fl = 0;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +090035
Chao Yu59c84402018-04-03 15:08:17 +080036 if (flags & F2FS_SYNC_FL)
Zhang Zhen8abfb362014-04-15 14:19:38 +080037 new_fl |= S_SYNC;
Chao Yu59c84402018-04-03 15:08:17 +080038 if (flags & F2FS_APPEND_FL)
Zhang Zhen8abfb362014-04-15 14:19:38 +080039 new_fl |= S_APPEND;
Chao Yu59c84402018-04-03 15:08:17 +080040 if (flags & F2FS_IMMUTABLE_FL)
Zhang Zhen8abfb362014-04-15 14:19:38 +080041 new_fl |= S_IMMUTABLE;
Chao Yu59c84402018-04-03 15:08:17 +080042 if (flags & F2FS_NOATIME_FL)
Zhang Zhen8abfb362014-04-15 14:19:38 +080043 new_fl |= S_NOATIME;
Chao Yu59c84402018-04-03 15:08:17 +080044 if (flags & F2FS_DIRSYNC_FL)
Zhang Zhen8abfb362014-04-15 14:19:38 +080045 new_fl |= S_DIRSYNC;
Eric Biggers2ee6a572017-10-09 12:15:35 -070046 if (f2fs_encrypted_inode(inode))
47 new_fl |= S_ENCRYPTED;
Zhang Zhen6a678852015-08-24 10:41:32 +080048 inode_set_flags(inode, new_fl,
Eric Biggers2ee6a572017-10-09 12:15:35 -070049 S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|
50 S_ENCRYPTED);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +090051}
52
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090053static void __get_inode_rdev(struct inode *inode, struct f2fs_inode *ri)
54{
Chao Yu7a2af762017-07-19 00:19:06 +080055 int extra_size = get_extra_isize(inode);
56
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090057 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
58 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
Chao Yu7a2af762017-07-19 00:19:06 +080059 if (ri->i_addr[extra_size])
60 inode->i_rdev = old_decode_dev(
61 le32_to_cpu(ri->i_addr[extra_size]));
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090062 else
Chao Yu7a2af762017-07-19 00:19:06 +080063 inode->i_rdev = new_decode_dev(
64 le32_to_cpu(ri->i_addr[extra_size + 1]));
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090065 }
66}
67
Chao Yu91291e92018-07-10 23:01:45 +080068static int __written_first_block(struct f2fs_sb_info *sbi,
Chao Yue1da7872018-06-05 17:44:11 +080069 struct f2fs_inode *ri)
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -070070{
Chao Yu7a2af762017-07-19 00:19:06 +080071 block_t addr = le32_to_cpu(ri->i_addr[offset_in_addr(ri)]);
Jaegeuk Kimadad81e2015-03-24 12:04:20 -070072
Chao Yu91291e92018-07-10 23:01:45 +080073 if (!__is_valid_data_blkaddr(addr))
74 return 1;
75 if (!f2fs_is_valid_blkaddr(sbi, addr, DATA_GENERIC))
76 return -EFAULT;
77 return 0;
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -070078}
79
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090080static void __set_inode_rdev(struct inode *inode, struct f2fs_inode *ri)
81{
Chao Yu7a2af762017-07-19 00:19:06 +080082 int extra_size = get_extra_isize(inode);
83
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090084 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
85 if (old_valid_dev(inode->i_rdev)) {
Chao Yu7a2af762017-07-19 00:19:06 +080086 ri->i_addr[extra_size] =
Chris Fries6c311ec2014-01-17 14:44:39 -060087 cpu_to_le32(old_encode_dev(inode->i_rdev));
Chao Yu7a2af762017-07-19 00:19:06 +080088 ri->i_addr[extra_size + 1] = 0;
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090089 } else {
Chao Yu7a2af762017-07-19 00:19:06 +080090 ri->i_addr[extra_size] = 0;
91 ri->i_addr[extra_size + 1] =
Chris Fries6c311ec2014-01-17 14:44:39 -060092 cpu_to_le32(new_encode_dev(inode->i_rdev));
Chao Yu7a2af762017-07-19 00:19:06 +080093 ri->i_addr[extra_size + 2] = 0;
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090094 }
95 }
96}
97
Chao Yu9e5ba772015-01-06 14:28:43 +080098static void __recover_inline_status(struct inode *inode, struct page *ipage)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070099{
Chao Yuf2470372017-07-19 00:19:05 +0800100 void *inline_data = inline_data_addr(inode, ipage);
Chao Yu9e5ba772015-01-06 14:28:43 +0800101 __le32 *start = inline_data;
Chao Yuf2470372017-07-19 00:19:05 +0800102 __le32 *end = start + MAX_INLINE_DATA(inode) / sizeof(__le32);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700103
Chao Yu9e5ba772015-01-06 14:28:43 +0800104 while (start < end) {
105 if (*start++) {
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800106 f2fs_wait_on_page_writeback(ipage, NODE, true);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700107
Jaegeuk Kim91942322016-05-20 10:13:22 -0700108 set_inode_flag(inode, FI_DATA_EXIST);
109 set_raw_inline(inode, F2FS_INODE(ipage));
Chao Yu9e5ba772015-01-06 14:28:43 +0800110 set_page_dirty(ipage);
111 return;
112 }
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700113 }
Chao Yu9e5ba772015-01-06 14:28:43 +0800114 return;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700115}
116
Chao Yu704956e2017-07-31 20:19:09 +0800117static bool f2fs_enable_inode_chksum(struct f2fs_sb_info *sbi, struct page *page)
118{
119 struct f2fs_inode *ri = &F2FS_NODE(page)->i;
Chao Yu704956e2017-07-31 20:19:09 +0800120
121 if (!f2fs_sb_has_inode_chksum(sbi->sb))
122 return false;
123
Chao Yu4c6b56c2018-07-08 22:16:54 +0800124 if (!IS_INODE(page) || !(ri->i_inline & F2FS_EXTRA_ATTR))
Chao Yu704956e2017-07-31 20:19:09 +0800125 return false;
126
Zhikang Zhangd6964942018-04-14 01:02:34 +0800127 if (!F2FS_FITS_IN_INODE(ri, le16_to_cpu(ri->i_extra_isize),
128 i_inode_checksum))
Chao Yu704956e2017-07-31 20:19:09 +0800129 return false;
130
131 return true;
132}
133
134static __u32 f2fs_inode_chksum(struct f2fs_sb_info *sbi, struct page *page)
135{
136 struct f2fs_node *node = F2FS_NODE(page);
137 struct f2fs_inode *ri = &node->i;
138 __le32 ino = node->footer.ino;
139 __le32 gen = ri->i_generation;
140 __u32 chksum, chksum_seed;
141 __u32 dummy_cs = 0;
142 unsigned int offset = offsetof(struct f2fs_inode, i_inode_checksum);
143 unsigned int cs_size = sizeof(dummy_cs);
144
145 chksum = f2fs_chksum(sbi, sbi->s_chksum_seed, (__u8 *)&ino,
146 sizeof(ino));
147 chksum_seed = f2fs_chksum(sbi, chksum, (__u8 *)&gen, sizeof(gen));
148
149 chksum = f2fs_chksum(sbi, chksum_seed, (__u8 *)ri, offset);
150 chksum = f2fs_chksum(sbi, chksum, (__u8 *)&dummy_cs, cs_size);
151 offset += cs_size;
152 chksum = f2fs_chksum(sbi, chksum, (__u8 *)ri + offset,
153 F2FS_BLKSIZE - offset);
154 return chksum;
155}
156
157bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page)
158{
159 struct f2fs_inode *ri;
160 __u32 provided, calculated;
161
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -0700162 if (unlikely(is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN)))
163 return true;
164
Weichao Guo54c55c42018-03-09 23:10:21 +0800165#ifdef CONFIG_F2FS_CHECK_FS
166 if (!f2fs_enable_inode_chksum(sbi, page))
167#else
Jaegeuk Kimee605232017-08-31 16:54:51 -0700168 if (!f2fs_enable_inode_chksum(sbi, page) ||
169 PageDirty(page) || PageWriteback(page))
Weichao Guo54c55c42018-03-09 23:10:21 +0800170#endif
Chao Yu704956e2017-07-31 20:19:09 +0800171 return true;
172
173 ri = &F2FS_NODE(page)->i;
174 provided = le32_to_cpu(ri->i_inode_checksum);
175 calculated = f2fs_inode_chksum(sbi, page);
176
177 if (provided != calculated)
178 f2fs_msg(sbi->sb, KERN_WARNING,
179 "checksum invalid, ino = %x, %x vs. %x",
180 ino_of_node(page), provided, calculated);
181
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 Yue34438c2018-06-29 13:55:22 +0800195static bool sanity_check_inode(struct inode *inode, struct page *node_page)
Jaegeuk Kim5d646002018-04-24 11:37:18 -0600196{
197 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu18dd6472018-07-08 22:16:55 +0800198 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yue34438c2018-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);
204 f2fs_msg(sbi->sb, KERN_WARNING,
205 "%s: corrupted inode i_blocks i_ino=%lx iblocks=%llu, "
206 "run fsck to fix.",
207 __func__, inode->i_ino, iblocks);
208 return false;
209 }
210
211 if (ino_of_node(node_page) != nid_of_node(node_page)) {
212 set_sbi_flag(sbi, SBI_NEED_FSCK);
213 f2fs_msg(sbi->sb, KERN_WARNING,
214 "%s: corrupted inode footer i_ino=%lx, ino,nid: "
215 "[%u, %u] run fsck to fix.",
216 __func__, inode->i_ino,
217 ino_of_node(node_page), nid_of_node(node_page));
218 return false;
219 }
Jaegeuk Kim5d646002018-04-24 11:37:18 -0600220
221 if (f2fs_sb_has_flexible_inline_xattr(sbi->sb)
222 && !f2fs_has_extra_attr(inode)) {
223 set_sbi_flag(sbi, SBI_NEED_FSCK);
224 f2fs_msg(sbi->sb, KERN_WARNING,
225 "%s: corrupted inode ino=%lx, run fsck to fix.",
226 __func__, inode->i_ino);
227 return false;
228 }
Chao Yu76d56d42018-06-25 23:29:49 +0800229
230 if (f2fs_has_extra_attr(inode) &&
231 !f2fs_sb_has_extra_attr(sbi->sb)) {
232 set_sbi_flag(sbi, SBI_NEED_FSCK);
233 f2fs_msg(sbi->sb, KERN_WARNING,
234 "%s: inode (ino=%lx) is with extra_attr, "
235 "but extra_attr feature is off",
236 __func__, inode->i_ino);
237 return false;
238 }
Chao Yuc9b60782018-08-01 19:13:44 +0800239
Chao Yu18dd6472018-07-08 22:16:55 +0800240 if (fi->i_extra_isize > F2FS_TOTAL_EXTRA_ATTR_SIZE ||
241 fi->i_extra_isize % sizeof(__le32)) {
242 set_sbi_flag(sbi, SBI_NEED_FSCK);
243 f2fs_msg(sbi->sb, KERN_WARNING,
244 "%s: inode (ino=%lx) has corrupted i_extra_isize: %d, "
245 "max: %zu",
246 __func__, inode->i_ino, fi->i_extra_isize,
247 F2FS_TOTAL_EXTRA_ATTR_SIZE);
248 return false;
249 }
250
Chao Yuc9b60782018-08-01 19:13:44 +0800251 if (F2FS_I(inode)->extent_tree) {
252 struct extent_info *ei = &F2FS_I(inode)->extent_tree->largest;
253
254 if (ei->len &&
255 (!f2fs_is_valid_blkaddr(sbi, ei->blk, DATA_GENERIC) ||
256 !f2fs_is_valid_blkaddr(sbi, ei->blk + ei->len - 1,
257 DATA_GENERIC))) {
258 set_sbi_flag(sbi, SBI_NEED_FSCK);
259 f2fs_msg(sbi->sb, KERN_WARNING,
260 "%s: inode (ino=%lx) extent info [%u, %u, %u] "
261 "is incorrect, run fsck to fix",
262 __func__, inode->i_ino,
263 ei->blk, ei->fofs, ei->len);
264 return false;
265 }
266 }
Chao Yubcbfbd62018-06-29 00:19:25 +0800267
268 if (f2fs_has_inline_data(inode) &&
269 (!S_ISREG(inode->i_mode) && !S_ISLNK(inode->i_mode))) {
270 set_sbi_flag(sbi, SBI_NEED_FSCK);
271 f2fs_msg(sbi->sb, KERN_WARNING,
272 "%s: inode (ino=%lx, mode=%u) should not have "
273 "inline_data, run fsck to fix",
274 __func__, inode->i_ino, inode->i_mode);
275 return false;
276 }
277
278 if (f2fs_has_inline_dentry(inode) && !S_ISDIR(inode->i_mode)) {
279 set_sbi_flag(sbi, SBI_NEED_FSCK);
280 f2fs_msg(sbi->sb, KERN_WARNING,
281 "%s: inode (ino=%lx, mode=%u) should not have "
282 "inline_dentry, run fsck to fix",
283 __func__, inode->i_ino, inode->i_mode);
284 return false;
285 }
286
Jaegeuk Kim5d646002018-04-24 11:37:18 -0600287 return true;
288}
289
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900290static int do_read_inode(struct inode *inode)
291{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700292 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900293 struct f2fs_inode_info *fi = F2FS_I(inode);
294 struct page *node_page;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900295 struct f2fs_inode *ri;
Chao Yu5c571322017-07-26 00:01:41 +0800296 projid_t i_projid;
Chao Yu91291e92018-07-10 23:01:45 +0800297 int err;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900298
299 /* Check if ino is within scope */
Chao Yu4d57b862018-05-30 00:20:41 +0800300 if (f2fs_check_nid_range(sbi, inode->i_ino))
Namjae Jeon064e0822013-03-17 17:27:20 +0900301 return -EINVAL;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900302
Chao Yu4d57b862018-05-30 00:20:41 +0800303 node_page = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900304 if (IS_ERR(node_page))
305 return PTR_ERR(node_page);
306
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900307 ri = F2FS_INODE(node_page);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900308
309 inode->i_mode = le16_to_cpu(ri->i_mode);
310 i_uid_write(inode, le32_to_cpu(ri->i_uid));
311 i_gid_write(inode, le32_to_cpu(ri->i_gid));
312 set_nlink(inode, le32_to_cpu(ri->i_links));
313 inode->i_size = le64_to_cpu(ri->i_size);
Chao Yu000519f2017-07-06 01:11:31 +0800314 inode->i_blocks = SECTOR_FROM_BLOCK(le64_to_cpu(ri->i_blocks) - 1);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900315
316 inode->i_atime.tv_sec = le64_to_cpu(ri->i_atime);
317 inode->i_ctime.tv_sec = le64_to_cpu(ri->i_ctime);
318 inode->i_mtime.tv_sec = le64_to_cpu(ri->i_mtime);
319 inode->i_atime.tv_nsec = le32_to_cpu(ri->i_atime_nsec);
320 inode->i_ctime.tv_nsec = le32_to_cpu(ri->i_ctime_nsec);
321 inode->i_mtime.tv_nsec = le32_to_cpu(ri->i_mtime_nsec);
322 inode->i_generation = le32_to_cpu(ri->i_generation);
Chao Yu1c41e6802018-05-07 20:28:52 +0800323 if (S_ISDIR(inode->i_mode))
324 fi->i_current_depth = le32_to_cpu(ri->i_current_depth);
325 else if (S_ISREG(inode->i_mode))
Chao Yu2ef79ec2018-05-07 20:28:54 +0800326 fi->i_gc_failures[GC_FAILURE_PIN] =
327 le16_to_cpu(ri->i_gc_failures);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900328 fi->i_xattr_nid = le32_to_cpu(ri->i_xattr_nid);
329 fi->i_flags = le32_to_cpu(ri->i_flags);
330 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 Yu7a2af762017-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 Yu6afc6622017-09-06 21:59:50 +0800343 if (f2fs_sb_has_flexible_inline_xattr(sbi->sb)) {
Chao Yu6afc6622017-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 Yu18dd6472018-07-08 22:16:55 +0800359 if (!sanity_check_inode(inode, node_page)) {
360 f2fs_put_page(node_page, 1);
361 return -EINVAL;
362 }
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
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +0900368 /* get rdev by using inline_info */
369 __get_inode_rdev(inode, ri);
370
Chao Yudda9f4b2018-08-11 23:42:09 +0800371 if (S_ISREG(inode->i_mode)) {
372 err = __written_first_block(sbi, ri);
373 if (err < 0) {
374 f2fs_put_page(node_page, 1);
375 return err;
376 }
377 if (!err)
378 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Chao Yu91291e92018-07-10 23:01:45 +0800379 }
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -0700380
Chao Yu4d57b862018-05-30 00:20:41 +0800381 if (!f2fs_need_inode_block_update(sbi, inode->i_ino))
Jaegeuk Kim26de9b12016-05-20 20:42:37 -0700382 fi->last_disk_size = inode->i_size;
383
Chao Yu59c84402018-04-03 15:08:17 +0800384 if (fi->i_flags & F2FS_PROJINHERIT_FL)
Chao Yu5c571322017-07-26 00:01:41 +0800385 set_inode_flag(inode, FI_PROJ_INHERIT);
386
387 if (f2fs_has_extra_attr(inode) && f2fs_sb_has_project_quota(sbi->sb) &&
388 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_projid))
389 i_projid = (projid_t)le32_to_cpu(ri->i_projid);
390 else
391 i_projid = F2FS_DEF_PROJID;
392 fi->i_projid = make_kprojid(&init_user_ns, i_projid);
393
Chao Yu1c1d35d2018-01-25 14:54:42 +0800394 if (f2fs_has_extra_attr(inode) && f2fs_sb_has_inode_crtime(sbi->sb) &&
395 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
396 fi->i_crtime.tv_sec = le64_to_cpu(ri->i_crtime);
397 fi->i_crtime.tv_nsec = le32_to_cpu(ri->i_crtime_nsec);
398 }
399
Arnd Bergmann24b81df2018-06-20 10:02:19 +0200400 F2FS_I(inode)->i_disk_time[0] = inode->i_atime;
401 F2FS_I(inode)->i_disk_time[1] = inode->i_ctime;
402 F2FS_I(inode)->i_disk_time[2] = inode->i_mtime;
Jaegeuk Kim214c2462018-03-29 22:50:41 -0700403 F2FS_I(inode)->i_disk_time[3] = F2FS_I(inode)->i_crtime;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900404 f2fs_put_page(node_page, 1);
Jaegeuk Kim9d1015d2014-12-05 10:51:50 -0800405
Chao Yud5e8f6c2015-07-15 17:28:53 +0800406 stat_inc_inline_xattr(inode);
Jaegeuk Kim9d1015d2014-12-05 10:51:50 -0800407 stat_inc_inline_inode(inode);
408 stat_inc_inline_dir(inode);
409
Chao Yu9e5ba772015-01-06 14:28:43 +0800410 return 0;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900411}
412
413struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
414{
415 struct f2fs_sb_info *sbi = F2FS_SB(sb);
416 struct inode *inode;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900417 int ret = 0;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900418
419 inode = iget_locked(sb, ino);
420 if (!inode)
421 return ERR_PTR(-ENOMEM);
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900422
423 if (!(inode->i_state & I_NEW)) {
424 trace_f2fs_iget(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900425 return inode;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900426 }
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900427 if (ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi))
428 goto make_now;
429
430 ret = do_read_inode(inode);
431 if (ret)
432 goto bad_inode;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900433make_now:
434 if (ino == F2FS_NODE_INO(sbi)) {
435 inode->i_mapping->a_ops = &f2fs_node_aops;
Chao Yu81114ba2018-04-09 20:25:06 +0800436 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900437 } else if (ino == F2FS_META_INO(sbi)) {
438 inode->i_mapping->a_ops = &f2fs_meta_aops;
Chao Yu81114ba2018-04-09 20:25:06 +0800439 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900440 } else if (S_ISREG(inode->i_mode)) {
441 inode->i_op = &f2fs_file_inode_operations;
442 inode->i_fop = &f2fs_file_operations;
443 inode->i_mapping->a_ops = &f2fs_dblock_aops;
444 } else if (S_ISDIR(inode->i_mode)) {
445 inode->i_op = &f2fs_dir_inode_operations;
446 inode->i_fop = &f2fs_dir_operations;
447 inode->i_mapping->a_ops = &f2fs_dblock_aops;
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800448 inode_nohighmem(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900449 } else if (S_ISLNK(inode->i_mode)) {
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700450 if (f2fs_encrypted_inode(inode))
451 inode->i_op = &f2fs_encrypted_symlink_inode_operations;
452 else
453 inode->i_op = &f2fs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -0500454 inode_nohighmem(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900455 inode->i_mapping->a_ops = &f2fs_dblock_aops;
456 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
457 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
458 inode->i_op = &f2fs_special_inode_operations;
459 init_special_inode(inode, inode->i_mode, inode->i_rdev);
460 } else {
461 ret = -EIO;
462 goto bad_inode;
463 }
Jaegeuk Kim93607122017-05-16 13:20:16 -0700464 f2fs_set_inode_flags(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900465 unlock_new_inode(inode);
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900466 trace_f2fs_iget(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900467 return inode;
468
469bad_inode:
470 iget_failed(inode);
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900471 trace_f2fs_iget_exit(inode, ret);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900472 return ERR_PTR(ret);
473}
474
Jaegeuk Kime8ea9b32016-09-09 16:59:39 -0700475struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino)
476{
477 struct inode *inode;
478retry:
479 inode = f2fs_iget(sb, ino);
480 if (IS_ERR(inode)) {
481 if (PTR_ERR(inode) == -ENOMEM) {
482 congestion_wait(BLK_RW_ASYNC, HZ/50);
483 goto retry;
484 }
485 }
486 return inode;
487}
488
Chao Yu4d57b862018-05-30 00:20:41 +0800489void f2fs_update_inode(struct inode *inode, struct page *node_page)
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900490{
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900491 struct f2fs_inode *ri;
Chao Yub691d982016-10-11 22:57:05 +0800492 struct extent_tree *et = F2FS_I(inode)->extent_tree;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900493
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800494 f2fs_wait_on_page_writeback(node_page, NODE, true);
Yunlei He211a6fa2017-12-05 12:07:47 +0800495 set_page_dirty(node_page);
496
497 f2fs_inode_synced(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900498
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900499 ri = F2FS_INODE(node_page);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900500
501 ri->i_mode = cpu_to_le16(inode->i_mode);
502 ri->i_advise = F2FS_I(inode)->i_advise;
503 ri->i_uid = cpu_to_le32(i_uid_read(inode));
504 ri->i_gid = cpu_to_le32(i_gid_read(inode));
505 ri->i_links = cpu_to_le32(inode->i_nlink);
506 ri->i_size = cpu_to_le64(i_size_read(inode));
Chao Yu000519f2017-07-06 01:11:31 +0800507 ri->i_blocks = cpu_to_le64(SECTOR_TO_BLOCK(inode->i_blocks) + 1);
Chao Yu0c872e22015-02-05 17:46:29 +0800508
Chao Yub691d982016-10-11 22:57:05 +0800509 if (et) {
510 read_lock(&et->lock);
511 set_raw_extent(&et->largest, &ri->i_ext);
512 read_unlock(&et->lock);
513 } else {
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700514 memset(&ri->i_ext, 0, sizeof(ri->i_ext));
Chao Yub691d982016-10-11 22:57:05 +0800515 }
Jaegeuk Kim91942322016-05-20 10:13:22 -0700516 set_raw_inline(inode, ri);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900517
518 ri->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
519 ri->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
520 ri->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
521 ri->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
522 ri->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
523 ri->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
Chao Yu1c41e6802018-05-07 20:28:52 +0800524 if (S_ISDIR(inode->i_mode))
525 ri->i_current_depth =
526 cpu_to_le32(F2FS_I(inode)->i_current_depth);
527 else if (S_ISREG(inode->i_mode))
Chao Yu2ef79ec2018-05-07 20:28:54 +0800528 ri->i_gc_failures =
529 cpu_to_le16(F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN]);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900530 ri->i_xattr_nid = cpu_to_le32(F2FS_I(inode)->i_xattr_nid);
531 ri->i_flags = cpu_to_le32(F2FS_I(inode)->i_flags);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900532 ri->i_pino = cpu_to_le32(F2FS_I(inode)->i_pino);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900533 ri->i_generation = cpu_to_le32(inode->i_generation);
Jaegeuk Kim38431542014-02-27 18:20:00 +0900534 ri->i_dir_level = F2FS_I(inode)->i_dir_level;
Changman Lee7d79e752013-01-23 09:40:23 +0900535
Chao Yu5c571322017-07-26 00:01:41 +0800536 if (f2fs_has_extra_attr(inode)) {
Chao Yu7a2af762017-07-19 00:19:06 +0800537 ri->i_extra_isize = cpu_to_le16(F2FS_I(inode)->i_extra_isize);
538
Chao Yu6afc6622017-09-06 21:59:50 +0800539 if (f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(inode)->sb))
540 ri->i_inline_xattr_size =
541 cpu_to_le16(F2FS_I(inode)->i_inline_xattr_size);
542
Chao Yu5c571322017-07-26 00:01:41 +0800543 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)->sb) &&
544 F2FS_FITS_IN_INODE(ri, F2FS_I(inode)->i_extra_isize,
545 i_projid)) {
546 projid_t i_projid;
547
548 i_projid = from_kprojid(&init_user_ns,
549 F2FS_I(inode)->i_projid);
550 ri->i_projid = cpu_to_le32(i_projid);
551 }
Chao Yu1c1d35d2018-01-25 14:54:42 +0800552
553 if (f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)->sb) &&
554 F2FS_FITS_IN_INODE(ri, F2FS_I(inode)->i_extra_isize,
555 i_crtime)) {
556 ri->i_crtime =
557 cpu_to_le64(F2FS_I(inode)->i_crtime.tv_sec);
558 ri->i_crtime_nsec =
559 cpu_to_le32(F2FS_I(inode)->i_crtime.tv_nsec);
560 }
Chao Yu5c571322017-07-26 00:01:41 +0800561 }
562
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +0900563 __set_inode_rdev(inode, ri);
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800564
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -0800565 /* deleted inode */
566 if (inode->i_nlink == 0)
567 clear_inline_node(node_page);
568
Arnd Bergmann24b81df2018-06-20 10:02:19 +0200569 F2FS_I(inode)->i_disk_time[0] = inode->i_atime;
570 F2FS_I(inode)->i_disk_time[1] = inode->i_ctime;
571 F2FS_I(inode)->i_disk_time[2] = inode->i_mtime;
Jaegeuk Kim214c2462018-03-29 22:50:41 -0700572 F2FS_I(inode)->i_disk_time[3] = F2FS_I(inode)->i_crtime;
Weichao Guo54c55c42018-03-09 23:10:21 +0800573
574#ifdef CONFIG_F2FS_CHECK_FS
575 f2fs_inode_chksum_set(F2FS_I_SB(inode), node_page);
576#endif
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900577}
578
Chao Yu4d57b862018-05-30 00:20:41 +0800579void f2fs_update_inode_page(struct inode *inode)
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900580{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700581 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900582 struct page *node_page;
Jaegeuk Kim744602c2014-01-24 09:42:16 +0900583retry:
Chao Yu4d57b862018-05-30 00:20:41 +0800584 node_page = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kim744602c2014-01-24 09:42:16 +0900585 if (IS_ERR(node_page)) {
586 int err = PTR_ERR(node_page);
587 if (err == -ENOMEM) {
588 cond_resched();
589 goto retry;
590 } else if (err != -ENOENT) {
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -0700591 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim744602c2014-01-24 09:42:16 +0900592 }
Yunlei He211a6fa2017-12-05 12:07:47 +0800593 return;
Jaegeuk Kim744602c2014-01-24 09:42:16 +0900594 }
Chao Yu4d57b862018-05-30 00:20:41 +0800595 f2fs_update_inode(inode, node_page);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900596 f2fs_put_page(node_page, 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900597}
598
599int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc)
600{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700601 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900602
603 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
604 inode->i_ino == F2FS_META_INO(sbi))
605 return 0;
606
Jaegeuk Kim91942322016-05-20 10:13:22 -0700607 if (!is_inode_flag_set(inode, FI_DIRTY_INODE))
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900608 return 0;
609
Daniel Rosenberg43549942018-08-20 19:21:43 -0700610 if (f2fs_is_checkpoint_ready(sbi))
611 return -ENOSPC;
612
Jaegeuk Kim39936832012-11-22 16:21:29 +0900613 /*
Jaegeuk Kimc5cd29d2015-09-12 11:25:30 -0700614 * We need to balance fs here to prevent from producing dirty node pages
Jaegeuk Kim39936832012-11-22 16:21:29 +0900615 * during the urgent cleaning time when runing out of free sections.
616 */
Chao Yu4d57b862018-05-30 00:20:41 +0800617 f2fs_update_inode_page(inode);
Jaegeuk Kima7881892017-04-20 13:51:57 -0700618 if (wbc && wbc->nr_to_write)
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800619 f2fs_balance_fs(sbi, true);
Jaegeuk Kim744602c2014-01-24 09:42:16 +0900620 return 0;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900621}
622
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900623/*
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900624 * Called at the last iput() if i_nlink is zero
625 */
626void f2fs_evict_inode(struct inode *inode)
627{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700628 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700629 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
Chao Yu13ec7292015-08-24 17:40:45 +0800630 int err = 0;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900631
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700632 /* some remained atomic pages should discarded */
Jaegeuk Kim1e843712014-12-09 06:08:59 -0800633 if (f2fs_is_atomic_file(inode))
Chao Yu4d57b862018-05-30 00:20:41 +0800634 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700635
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900636 trace_f2fs_evict_inode(inode);
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700637 truncate_inode_pages_final(&inode->i_data);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900638
639 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
640 inode->i_ino == F2FS_META_INO(sbi))
Chao Yudbf20cb2014-07-25 12:00:57 +0800641 goto out_clear;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900642
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -0700643 f2fs_bug_on(sbi, get_dirty_pages(inode));
Chao Yu4d57b862018-05-30 00:20:41 +0800644 f2fs_remove_dirty_inode(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900645
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700646 f2fs_destroy_extent_tree(inode);
647
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900648 if (inode->i_nlink || is_bad_inode(inode))
649 goto no_delete;
650
Chao Yu0abd6752017-07-09 00:13:07 +0800651 dquot_initialize(inode);
652
Chao Yu4d57b862018-05-30 00:20:41 +0800653 f2fs_remove_ino_entry(sbi, inode->i_ino, APPEND_INO);
654 f2fs_remove_ino_entry(sbi, inode->i_ino, UPDATE_INO);
655 f2fs_remove_ino_entry(sbi, inode->i_ino, FLUSH_INO);
Chao Yu60dcedc2016-11-02 20:43:21 +0800656
Changman Leed6212a52013-01-29 18:30:07 +0900657 sb_start_intwrite(inode->i_sb);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700658 set_inode_flag(inode, FI_NO_ALLOC);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900659 i_size_write(inode, 0);
Jaegeuk Kim4c0c2942016-05-03 09:22:18 -0700660retry:
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900661 if (F2FS_HAS_BLOCKS(inode))
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700662 err = f2fs_truncate(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900663
Jaegeuk Kim8c1b3c02017-03-07 13:32:20 -0800664 if (time_to_inject(sbi, FAULT_EVICT_INODE)) {
665 f2fs_show_injection_info(FAULT_EVICT_INODE);
666 err = -EIO;
667 }
Arnd Bergmann7fa750a2018-08-13 23:38:06 +0200668
Chao Yu13ec7292015-08-24 17:40:45 +0800669 if (!err) {
670 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +0800671 err = f2fs_remove_inode_page(inode);
Chao Yu13ec7292015-08-24 17:40:45 +0800672 f2fs_unlock_op(sbi);
Chao Yua11b9f62016-10-11 22:56:59 +0800673 if (err == -ENOENT)
674 err = 0;
Chao Yu13ec7292015-08-24 17:40:45 +0800675 }
Jaegeuk Kim39936832012-11-22 16:21:29 +0900676
Jaegeuk Kim4c0c2942016-05-03 09:22:18 -0700677 /* give more chances, if ENOMEM case */
678 if (err == -ENOMEM) {
679 err = 0;
680 goto retry;
681 }
682
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700683 if (err)
Chao Yu4d57b862018-05-30 00:20:41 +0800684 f2fs_update_inode_page(inode);
Chao Yu0abd6752017-07-09 00:13:07 +0800685 dquot_free_inode(inode);
Changman Leed6212a52013-01-29 18:30:07 +0900686 sb_end_intwrite(inode->i_sb);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900687no_delete:
Chao Yu0abd6752017-07-09 00:13:07 +0800688 dquot_drop(inode);
689
Chao Yud5e8f6c2015-07-15 17:28:53 +0800690 stat_dec_inline_xattr(inode);
Jaegeuk Kim3289c062014-10-13 20:00:16 -0700691 stat_dec_inline_dir(inode);
Jaegeuk Kime7a2bf22014-10-14 10:29:50 -0700692 stat_dec_inline_inode(inode);
Chao Yu0bdee482015-03-19 19:27:51 +0800693
Daniel Rosenberg43549942018-08-20 19:21:43 -0700694 if (likely(!is_set_ckpt_flags(sbi, CP_ERROR_FLAG) &&
695 !is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
Chao Yuca7d8022017-09-12 14:04:05 +0800696 f2fs_bug_on(sbi, is_inode_flag_set(inode, FI_DIRTY_INODE));
Jaegeuk Kim943973c2017-10-12 19:12:53 -0700697 else
698 f2fs_inode_synced(inode);
Chao Yuca7d8022017-09-12 14:04:05 +0800699
Jaegeuk Kim4f295442017-03-04 13:56:10 -0800700 /* ino == 0, if f2fs_new_inode() was failed t*/
701 if (inode->i_ino)
702 invalidate_mapping_pages(NODE_MAPPING(sbi), inode->i_ino,
703 inode->i_ino);
Chao Yu002a41c2014-08-04 09:54:58 +0800704 if (xnid)
705 invalidate_mapping_pages(NODE_MAPPING(sbi), xnid, xnid);
Chao Yu60dcedc2016-11-02 20:43:21 +0800706 if (inode->i_nlink) {
707 if (is_inode_flag_set(inode, FI_APPEND_WRITE))
Chao Yu4d57b862018-05-30 00:20:41 +0800708 f2fs_add_ino_entry(sbi, inode->i_ino, APPEND_INO);
Chao Yu60dcedc2016-11-02 20:43:21 +0800709 if (is_inode_flag_set(inode, FI_UPDATE_WRITE))
Chao Yu4d57b862018-05-30 00:20:41 +0800710 f2fs_add_ino_entry(sbi, inode->i_ino, UPDATE_INO);
Chao Yu60dcedc2016-11-02 20:43:21 +0800711 }
Jaegeuk Kim91942322016-05-20 10:13:22 -0700712 if (is_inode_flag_set(inode, FI_FREE_NID)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800713 f2fs_alloc_nid_failed(sbi, inode->i_ino);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700714 clear_inode_flag(inode, FI_FREE_NID);
Jaegeuk Kimd8c42562017-06-01 15:39:27 -0700715 } else {
Jaegeuk Kima4f843b2018-04-23 23:02:31 -0600716 /*
717 * If xattr nid is corrupted, we can reach out error condition,
Chao Yu4d57b862018-05-30 00:20:41 +0800718 * err & !f2fs_exist_written_data(sbi, inode->i_ino, ORPHAN_INO)).
719 * In that case, f2fs_check_nid_range() is enough to give a clue.
Jaegeuk Kima4f843b2018-04-23 23:02:31 -0600720 */
Jaegeuk Kimc9b63bd2015-06-23 10:36:08 -0700721 }
Chao Yudbf20cb2014-07-25 12:00:57 +0800722out_clear:
Eric Biggers3d204e22018-01-11 23:30:13 -0500723 fscrypt_put_encryption_info(inode);
Chao Yudbf20cb2014-07-25 12:00:57 +0800724 clear_inode(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900725}
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700726
727/* caller should call f2fs_lock_op() */
Chao Yu4d57b862018-05-30 00:20:41 +0800728void f2fs_handle_failed_inode(struct inode *inode)
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700729{
730 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700731 struct node_info ni;
Chao Yu77357302018-07-17 00:02:17 +0800732 int err;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700733
Chao Yua11b9f62016-10-11 22:56:59 +0800734 /*
735 * clear nlink of inode in order to release resource of inode
736 * immediately.
737 */
738 clear_nlink(inode);
739
740 /*
741 * we must call this to avoid inode being remained as dirty, resulting
742 * in a panic when flushing dirty inodes in gdirty_list.
743 */
Chao Yu4d57b862018-05-30 00:20:41 +0800744 f2fs_update_inode_page(inode);
Jaegeuk Kim9bb02c32017-04-11 19:01:26 -0700745 f2fs_inode_synced(inode);
Chao Yua11b9f62016-10-11 22:56:59 +0800746
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700747 /* don't make bad inode, since it becomes a regular file. */
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700748 unlock_new_inode(inode);
749
Chao Yu13ec7292015-08-24 17:40:45 +0800750 /*
Chao Yu13ec7292015-08-24 17:40:45 +0800751 * Note: we should add inode to orphan list before f2fs_unlock_op()
752 * so we can prevent losing this orphan when encoutering checkpoint
753 * and following suddenly power-off.
754 */
Chao Yu77357302018-07-17 00:02:17 +0800755 err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
756 if (err) {
757 set_sbi_flag(sbi, SBI_NEED_FSCK);
758 f2fs_msg(sbi->sb, KERN_WARNING,
759 "May loss orphan inode, run fsck to fix.");
760 goto out;
761 }
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700762
763 if (ni.blk_addr != NULL_ADDR) {
Chao Yu77357302018-07-17 00:02:17 +0800764 err = f2fs_acquire_orphan_inode(sbi);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700765 if (err) {
766 set_sbi_flag(sbi, SBI_NEED_FSCK);
767 f2fs_msg(sbi->sb, KERN_WARNING,
768 "Too many orphan inodes, run fsck to fix.");
769 } else {
Chao Yu4d57b862018-05-30 00:20:41 +0800770 f2fs_add_orphan_inode(inode);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700771 }
Chao Yu4d57b862018-05-30 00:20:41 +0800772 f2fs_alloc_nid_done(sbi, inode->i_ino);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700773 } else {
Jaegeuk Kim91942322016-05-20 10:13:22 -0700774 set_inode_flag(inode, FI_FREE_NID);
Chao Yu13ec7292015-08-24 17:40:45 +0800775 }
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700776
Chao Yu77357302018-07-17 00:02:17 +0800777out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700778 f2fs_unlock_op(sbi);
779
780 /* iput will drop the inode object */
781 iput(inode);
782}