blob: fc2c98b9e255b43551b4af98032cb9db106c88fe [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +09002 * fs/f2fs/inode.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/fs.h>
12#include <linux/f2fs_fs.h>
13#include <linux/buffer_head.h>
Jaegeuk Kime8ea9b32016-09-09 16:59:39 -070014#include <linux/backing-dev.h>
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +090015#include <linux/writeback.h>
16
17#include "f2fs.h"
18#include "node.h"
Chao Yu0eb0ada2017-06-14 23:00:56 +080019#include "segment.h"
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +090020
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090021#include <trace/events/f2fs.h>
22
Jaegeuk Kim7c457292016-10-14 11:51:23 -070023void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync)
Jaegeuk Kimb56ab832016-06-30 19:09:37 -070024{
Daeho Jeong9ac1e2d2018-01-11 11:26:19 +090025 if (is_inode_flag_set(inode, FI_NEW_INODE))
26 return;
27
Jaegeuk Kim7c457292016-10-14 11:51:23 -070028 if (f2fs_inode_dirtied(inode, sync))
Jaegeuk Kimb56ab832016-06-30 19:09:37 -070029 return;
Jaegeuk Kim7c457292016-10-14 11:51:23 -070030
Jaegeuk Kimb56ab832016-06-30 19:09:37 -070031 mark_inode_dirty_sync(inode);
32}
33
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +090034void f2fs_set_inode_flags(struct inode *inode)
35{
36 unsigned int flags = F2FS_I(inode)->i_flags;
Zhang Zhen8abfb362014-04-15 14:19:38 +080037 unsigned int new_fl = 0;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +090038
Chao Yu59c84402018-04-03 15:08:17 +080039 if (flags & F2FS_SYNC_FL)
Zhang Zhen8abfb362014-04-15 14:19:38 +080040 new_fl |= S_SYNC;
Chao Yu59c84402018-04-03 15:08:17 +080041 if (flags & F2FS_APPEND_FL)
Zhang Zhen8abfb362014-04-15 14:19:38 +080042 new_fl |= S_APPEND;
Chao Yu59c84402018-04-03 15:08:17 +080043 if (flags & F2FS_IMMUTABLE_FL)
Zhang Zhen8abfb362014-04-15 14:19:38 +080044 new_fl |= S_IMMUTABLE;
Chao Yu59c84402018-04-03 15:08:17 +080045 if (flags & F2FS_NOATIME_FL)
Zhang Zhen8abfb362014-04-15 14:19:38 +080046 new_fl |= S_NOATIME;
Chao Yu59c84402018-04-03 15:08:17 +080047 if (flags & F2FS_DIRSYNC_FL)
Zhang Zhen8abfb362014-04-15 14:19:38 +080048 new_fl |= S_DIRSYNC;
Eric Biggers2ee6a572017-10-09 12:15:35 -070049 if (f2fs_encrypted_inode(inode))
50 new_fl |= S_ENCRYPTED;
Zhang Zhen6a678852015-08-24 10:41:32 +080051 inode_set_flags(inode, new_fl,
Eric Biggers2ee6a572017-10-09 12:15:35 -070052 S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|
53 S_ENCRYPTED);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +090054}
55
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090056static void __get_inode_rdev(struct inode *inode, struct f2fs_inode *ri)
57{
Chao Yu7a2af762017-07-19 00:19:06 +080058 int extra_size = get_extra_isize(inode);
59
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090060 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
61 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
Chao Yu7a2af762017-07-19 00:19:06 +080062 if (ri->i_addr[extra_size])
63 inode->i_rdev = old_decode_dev(
64 le32_to_cpu(ri->i_addr[extra_size]));
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090065 else
Chao Yu7a2af762017-07-19 00:19:06 +080066 inode->i_rdev = new_decode_dev(
67 le32_to_cpu(ri->i_addr[extra_size + 1]));
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090068 }
69}
70
Chao Yu91291e92018-07-10 23:01:45 +080071static int __written_first_block(struct f2fs_sb_info *sbi,
Chao Yue1da7872018-06-05 17:44:11 +080072 struct f2fs_inode *ri)
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -070073{
Chao Yu7a2af762017-07-19 00:19:06 +080074 block_t addr = le32_to_cpu(ri->i_addr[offset_in_addr(ri)]);
Jaegeuk Kimadad81e2015-03-24 12:04:20 -070075
Chao Yu91291e92018-07-10 23:01:45 +080076 if (!__is_valid_data_blkaddr(addr))
77 return 1;
78 if (!f2fs_is_valid_blkaddr(sbi, addr, DATA_GENERIC))
79 return -EFAULT;
80 return 0;
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -070081}
82
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090083static void __set_inode_rdev(struct inode *inode, struct f2fs_inode *ri)
84{
Chao Yu7a2af762017-07-19 00:19:06 +080085 int extra_size = get_extra_isize(inode);
86
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090087 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
88 if (old_valid_dev(inode->i_rdev)) {
Chao Yu7a2af762017-07-19 00:19:06 +080089 ri->i_addr[extra_size] =
Chris Fries6c311ec2014-01-17 14:44:39 -060090 cpu_to_le32(old_encode_dev(inode->i_rdev));
Chao Yu7a2af762017-07-19 00:19:06 +080091 ri->i_addr[extra_size + 1] = 0;
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090092 } else {
Chao Yu7a2af762017-07-19 00:19:06 +080093 ri->i_addr[extra_size] = 0;
94 ri->i_addr[extra_size + 1] =
Chris Fries6c311ec2014-01-17 14:44:39 -060095 cpu_to_le32(new_encode_dev(inode->i_rdev));
Chao Yu7a2af762017-07-19 00:19:06 +080096 ri->i_addr[extra_size + 2] = 0;
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090097 }
98 }
99}
100
Chao Yu9e5ba772015-01-06 14:28:43 +0800101static void __recover_inline_status(struct inode *inode, struct page *ipage)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700102{
Chao Yuf2470372017-07-19 00:19:05 +0800103 void *inline_data = inline_data_addr(inode, ipage);
Chao Yu9e5ba772015-01-06 14:28:43 +0800104 __le32 *start = inline_data;
Chao Yuf2470372017-07-19 00:19:05 +0800105 __le32 *end = start + MAX_INLINE_DATA(inode) / sizeof(__le32);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700106
Chao Yu9e5ba772015-01-06 14:28:43 +0800107 while (start < end) {
108 if (*start++) {
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800109 f2fs_wait_on_page_writeback(ipage, NODE, true);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700110
Jaegeuk Kim91942322016-05-20 10:13:22 -0700111 set_inode_flag(inode, FI_DATA_EXIST);
112 set_raw_inline(inode, F2FS_INODE(ipage));
Chao Yu9e5ba772015-01-06 14:28:43 +0800113 set_page_dirty(ipage);
114 return;
115 }
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700116 }
Chao Yu9e5ba772015-01-06 14:28:43 +0800117 return;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700118}
119
Chao Yu704956e2017-07-31 20:19:09 +0800120static bool f2fs_enable_inode_chksum(struct f2fs_sb_info *sbi, struct page *page)
121{
122 struct f2fs_inode *ri = &F2FS_NODE(page)->i;
Chao Yu704956e2017-07-31 20:19:09 +0800123
124 if (!f2fs_sb_has_inode_chksum(sbi->sb))
125 return false;
126
Chao Yu4c6b56c2018-07-08 22:16:54 +0800127 if (!IS_INODE(page) || !(ri->i_inline & F2FS_EXTRA_ATTR))
Chao Yu704956e2017-07-31 20:19:09 +0800128 return false;
129
Zhikang Zhangd6964942018-04-14 01:02:34 +0800130 if (!F2FS_FITS_IN_INODE(ri, le16_to_cpu(ri->i_extra_isize),
131 i_inode_checksum))
Chao Yu704956e2017-07-31 20:19:09 +0800132 return false;
133
134 return true;
135}
136
137static __u32 f2fs_inode_chksum(struct f2fs_sb_info *sbi, struct page *page)
138{
139 struct f2fs_node *node = F2FS_NODE(page);
140 struct f2fs_inode *ri = &node->i;
141 __le32 ino = node->footer.ino;
142 __le32 gen = ri->i_generation;
143 __u32 chksum, chksum_seed;
144 __u32 dummy_cs = 0;
145 unsigned int offset = offsetof(struct f2fs_inode, i_inode_checksum);
146 unsigned int cs_size = sizeof(dummy_cs);
147
148 chksum = f2fs_chksum(sbi, sbi->s_chksum_seed, (__u8 *)&ino,
149 sizeof(ino));
150 chksum_seed = f2fs_chksum(sbi, chksum, (__u8 *)&gen, sizeof(gen));
151
152 chksum = f2fs_chksum(sbi, chksum_seed, (__u8 *)ri, offset);
153 chksum = f2fs_chksum(sbi, chksum, (__u8 *)&dummy_cs, cs_size);
154 offset += cs_size;
155 chksum = f2fs_chksum(sbi, chksum, (__u8 *)ri + offset,
156 F2FS_BLKSIZE - offset);
157 return chksum;
158}
159
160bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page)
161{
162 struct f2fs_inode *ri;
163 __u32 provided, calculated;
164
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -0700165 if (unlikely(is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN)))
166 return true;
167
Weichao Guo54c55c42018-03-09 23:10:21 +0800168#ifdef CONFIG_F2FS_CHECK_FS
169 if (!f2fs_enable_inode_chksum(sbi, page))
170#else
Jaegeuk Kimee605232017-08-31 16:54:51 -0700171 if (!f2fs_enable_inode_chksum(sbi, page) ||
172 PageDirty(page) || PageWriteback(page))
Weichao Guo54c55c42018-03-09 23:10:21 +0800173#endif
Chao Yu704956e2017-07-31 20:19:09 +0800174 return true;
175
176 ri = &F2FS_NODE(page)->i;
177 provided = le32_to_cpu(ri->i_inode_checksum);
178 calculated = f2fs_inode_chksum(sbi, page);
179
180 if (provided != calculated)
181 f2fs_msg(sbi->sb, KERN_WARNING,
182 "checksum invalid, ino = %x, %x vs. %x",
183 ino_of_node(page), provided, calculated);
184
185 return provided == calculated;
186}
187
188void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page)
189{
190 struct f2fs_inode *ri = &F2FS_NODE(page)->i;
191
192 if (!f2fs_enable_inode_chksum(sbi, page))
193 return;
194
195 ri->i_inode_checksum = cpu_to_le32(f2fs_inode_chksum(sbi, page));
196}
197
Chao Yue34438c2018-06-29 13:55:22 +0800198static bool sanity_check_inode(struct inode *inode, struct page *node_page)
Jaegeuk Kim5d646002018-04-24 11:37:18 -0600199{
200 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu18dd6472018-07-08 22:16:55 +0800201 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yue34438c2018-06-29 13:55:22 +0800202 unsigned long long iblocks;
203
204 iblocks = le64_to_cpu(F2FS_INODE(node_page)->i_blocks);
205 if (!iblocks) {
206 set_sbi_flag(sbi, SBI_NEED_FSCK);
207 f2fs_msg(sbi->sb, KERN_WARNING,
208 "%s: corrupted inode i_blocks i_ino=%lx iblocks=%llu, "
209 "run fsck to fix.",
210 __func__, inode->i_ino, iblocks);
211 return false;
212 }
213
214 if (ino_of_node(node_page) != nid_of_node(node_page)) {
215 set_sbi_flag(sbi, SBI_NEED_FSCK);
216 f2fs_msg(sbi->sb, KERN_WARNING,
217 "%s: corrupted inode footer i_ino=%lx, ino,nid: "
218 "[%u, %u] run fsck to fix.",
219 __func__, inode->i_ino,
220 ino_of_node(node_page), nid_of_node(node_page));
221 return false;
222 }
Jaegeuk Kim5d646002018-04-24 11:37:18 -0600223
224 if (f2fs_sb_has_flexible_inline_xattr(sbi->sb)
225 && !f2fs_has_extra_attr(inode)) {
226 set_sbi_flag(sbi, SBI_NEED_FSCK);
227 f2fs_msg(sbi->sb, KERN_WARNING,
228 "%s: corrupted inode ino=%lx, run fsck to fix.",
229 __func__, inode->i_ino);
230 return false;
231 }
Chao Yu76d56d42018-06-25 23:29:49 +0800232
233 if (f2fs_has_extra_attr(inode) &&
234 !f2fs_sb_has_extra_attr(sbi->sb)) {
235 set_sbi_flag(sbi, SBI_NEED_FSCK);
236 f2fs_msg(sbi->sb, KERN_WARNING,
237 "%s: inode (ino=%lx) is with extra_attr, "
238 "but extra_attr feature is off",
239 __func__, inode->i_ino);
240 return false;
241 }
Chao Yuc9b60782018-08-01 19:13:44 +0800242
Chao Yu18dd6472018-07-08 22:16:55 +0800243 if (fi->i_extra_isize > F2FS_TOTAL_EXTRA_ATTR_SIZE ||
244 fi->i_extra_isize % sizeof(__le32)) {
245 set_sbi_flag(sbi, SBI_NEED_FSCK);
246 f2fs_msg(sbi->sb, KERN_WARNING,
247 "%s: inode (ino=%lx) has corrupted i_extra_isize: %d, "
248 "max: %zu",
249 __func__, inode->i_ino, fi->i_extra_isize,
250 F2FS_TOTAL_EXTRA_ATTR_SIZE);
251 return false;
252 }
253
Chao Yuc9b60782018-08-01 19:13:44 +0800254 if (F2FS_I(inode)->extent_tree) {
255 struct extent_info *ei = &F2FS_I(inode)->extent_tree->largest;
256
257 if (ei->len &&
258 (!f2fs_is_valid_blkaddr(sbi, ei->blk, DATA_GENERIC) ||
259 !f2fs_is_valid_blkaddr(sbi, ei->blk + ei->len - 1,
260 DATA_GENERIC))) {
261 set_sbi_flag(sbi, SBI_NEED_FSCK);
262 f2fs_msg(sbi->sb, KERN_WARNING,
263 "%s: inode (ino=%lx) extent info [%u, %u, %u] "
264 "is incorrect, run fsck to fix",
265 __func__, inode->i_ino,
266 ei->blk, ei->fofs, ei->len);
267 return false;
268 }
269 }
Chao Yubcbfbd62018-06-29 00:19:25 +0800270
271 if (f2fs_has_inline_data(inode) &&
272 (!S_ISREG(inode->i_mode) && !S_ISLNK(inode->i_mode))) {
273 set_sbi_flag(sbi, SBI_NEED_FSCK);
274 f2fs_msg(sbi->sb, KERN_WARNING,
275 "%s: inode (ino=%lx, mode=%u) should not have "
276 "inline_data, run fsck to fix",
277 __func__, inode->i_ino, inode->i_mode);
278 return false;
279 }
280
281 if (f2fs_has_inline_dentry(inode) && !S_ISDIR(inode->i_mode)) {
282 set_sbi_flag(sbi, SBI_NEED_FSCK);
283 f2fs_msg(sbi->sb, KERN_WARNING,
284 "%s: inode (ino=%lx, mode=%u) should not have "
285 "inline_dentry, run fsck to fix",
286 __func__, inode->i_ino, inode->i_mode);
287 return false;
288 }
289
Jaegeuk Kim5d646002018-04-24 11:37:18 -0600290 return true;
291}
292
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900293static int do_read_inode(struct inode *inode)
294{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700295 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900296 struct f2fs_inode_info *fi = F2FS_I(inode);
297 struct page *node_page;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900298 struct f2fs_inode *ri;
Chao Yu5c571322017-07-26 00:01:41 +0800299 projid_t i_projid;
Chao Yu91291e92018-07-10 23:01:45 +0800300 int err;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900301
302 /* Check if ino is within scope */
Chao Yu4d57b862018-05-30 00:20:41 +0800303 if (f2fs_check_nid_range(sbi, inode->i_ino))
Namjae Jeon064e0822013-03-17 17:27:20 +0900304 return -EINVAL;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900305
Chao Yu4d57b862018-05-30 00:20:41 +0800306 node_page = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900307 if (IS_ERR(node_page))
308 return PTR_ERR(node_page);
309
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900310 ri = F2FS_INODE(node_page);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900311
312 inode->i_mode = le16_to_cpu(ri->i_mode);
313 i_uid_write(inode, le32_to_cpu(ri->i_uid));
314 i_gid_write(inode, le32_to_cpu(ri->i_gid));
315 set_nlink(inode, le32_to_cpu(ri->i_links));
316 inode->i_size = le64_to_cpu(ri->i_size);
Chao Yu000519f2017-07-06 01:11:31 +0800317 inode->i_blocks = SECTOR_FROM_BLOCK(le64_to_cpu(ri->i_blocks) - 1);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900318
319 inode->i_atime.tv_sec = le64_to_cpu(ri->i_atime);
320 inode->i_ctime.tv_sec = le64_to_cpu(ri->i_ctime);
321 inode->i_mtime.tv_sec = le64_to_cpu(ri->i_mtime);
322 inode->i_atime.tv_nsec = le32_to_cpu(ri->i_atime_nsec);
323 inode->i_ctime.tv_nsec = le32_to_cpu(ri->i_ctime_nsec);
324 inode->i_mtime.tv_nsec = le32_to_cpu(ri->i_mtime_nsec);
325 inode->i_generation = le32_to_cpu(ri->i_generation);
Chao Yu1c41e6802018-05-07 20:28:52 +0800326 if (S_ISDIR(inode->i_mode))
327 fi->i_current_depth = le32_to_cpu(ri->i_current_depth);
328 else if (S_ISREG(inode->i_mode))
Chao Yu2ef79ec2018-05-07 20:28:54 +0800329 fi->i_gc_failures[GC_FAILURE_PIN] =
330 le16_to_cpu(ri->i_gc_failures);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900331 fi->i_xattr_nid = le32_to_cpu(ri->i_xattr_nid);
332 fi->i_flags = le32_to_cpu(ri->i_flags);
333 fi->flags = 0;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900334 fi->i_advise = ri->i_advise;
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900335 fi->i_pino = le32_to_cpu(ri->i_pino);
Jaegeuk Kim38431542014-02-27 18:20:00 +0900336 fi->i_dir_level = ri->i_dir_level;
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +0900337
Jaegeuk Kimed3d1252015-12-28 11:39:06 -0800338 if (f2fs_init_extent_tree(inode, &ri->i_ext))
339 set_page_dirty(node_page);
Chao Yu0c872e22015-02-05 17:46:29 +0800340
Jaegeuk Kim91942322016-05-20 10:13:22 -0700341 get_inline_info(inode, ri);
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +0900342
Chao Yu7a2af762017-07-19 00:19:06 +0800343 fi->i_extra_isize = f2fs_has_extra_attr(inode) ?
344 le16_to_cpu(ri->i_extra_isize) : 0;
345
Chao Yu6afc6622017-09-06 21:59:50 +0800346 if (f2fs_sb_has_flexible_inline_xattr(sbi->sb)) {
Chao Yu6afc6622017-09-06 21:59:50 +0800347 fi->i_inline_xattr_size = le16_to_cpu(ri->i_inline_xattr_size);
348 } else if (f2fs_has_inline_xattr(inode) ||
349 f2fs_has_inline_dentry(inode)) {
350 fi->i_inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
351 } else {
352
353 /*
354 * Previous inline data or directory always reserved 200 bytes
355 * in inode layout, even if inline_xattr is disabled. In order
356 * to keep inline_dentry's structure for backward compatibility,
357 * we get the space back only from inline_data.
358 */
359 fi->i_inline_xattr_size = 0;
360 }
361
Chao Yu18dd6472018-07-08 22:16:55 +0800362 if (!sanity_check_inode(inode, node_page)) {
363 f2fs_put_page(node_page, 1);
364 return -EINVAL;
365 }
366
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700367 /* check data exist */
368 if (f2fs_has_inline_data(inode) && !f2fs_exist_data(inode))
Chao Yu9e5ba772015-01-06 14:28:43 +0800369 __recover_inline_status(inode, node_page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700370
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +0900371 /* get rdev by using inline_info */
372 __get_inode_rdev(inode, ri);
373
Chao Yu91291e92018-07-10 23:01:45 +0800374 err = __written_first_block(sbi, ri);
375 if (err < 0) {
376 f2fs_put_page(node_page, 1);
377 return err;
378 }
379 if (!err)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700380 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -0700381
Chao Yu4d57b862018-05-30 00:20:41 +0800382 if (!f2fs_need_inode_block_update(sbi, inode->i_ino))
Jaegeuk Kim26de9b12016-05-20 20:42:37 -0700383 fi->last_disk_size = inode->i_size;
384
Chao Yu59c84402018-04-03 15:08:17 +0800385 if (fi->i_flags & F2FS_PROJINHERIT_FL)
Chao Yu5c571322017-07-26 00:01:41 +0800386 set_inode_flag(inode, FI_PROJ_INHERIT);
387
388 if (f2fs_has_extra_attr(inode) && f2fs_sb_has_project_quota(sbi->sb) &&
389 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_projid))
390 i_projid = (projid_t)le32_to_cpu(ri->i_projid);
391 else
392 i_projid = F2FS_DEF_PROJID;
393 fi->i_projid = make_kprojid(&init_user_ns, i_projid);
394
Chao Yu1c1d35d2018-01-25 14:54:42 +0800395 if (f2fs_has_extra_attr(inode) && f2fs_sb_has_inode_crtime(sbi->sb) &&
396 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
397 fi->i_crtime.tv_sec = le64_to_cpu(ri->i_crtime);
398 fi->i_crtime.tv_nsec = le32_to_cpu(ri->i_crtime_nsec);
399 }
400
Arnd Bergmann24b81df2018-06-20 10:02:19 +0200401 F2FS_I(inode)->i_disk_time[0] = inode->i_atime;
402 F2FS_I(inode)->i_disk_time[1] = inode->i_ctime;
403 F2FS_I(inode)->i_disk_time[2] = inode->i_mtime;
Jaegeuk Kim214c2462018-03-29 22:50:41 -0700404 F2FS_I(inode)->i_disk_time[3] = F2FS_I(inode)->i_crtime;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900405 f2fs_put_page(node_page, 1);
Jaegeuk Kim9d1015d2014-12-05 10:51:50 -0800406
Chao Yud5e8f6c2015-07-15 17:28:53 +0800407 stat_inc_inline_xattr(inode);
Jaegeuk Kim9d1015d2014-12-05 10:51:50 -0800408 stat_inc_inline_inode(inode);
409 stat_inc_inline_dir(inode);
410
Chao Yu9e5ba772015-01-06 14:28:43 +0800411 return 0;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900412}
413
414struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
415{
416 struct f2fs_sb_info *sbi = F2FS_SB(sb);
417 struct inode *inode;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900418 int ret = 0;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900419
420 inode = iget_locked(sb, ino);
421 if (!inode)
422 return ERR_PTR(-ENOMEM);
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900423
424 if (!(inode->i_state & I_NEW)) {
425 trace_f2fs_iget(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900426 return inode;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900427 }
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900428 if (ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi))
429 goto make_now;
430
431 ret = do_read_inode(inode);
432 if (ret)
433 goto bad_inode;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900434make_now:
435 if (ino == F2FS_NODE_INO(sbi)) {
436 inode->i_mapping->a_ops = &f2fs_node_aops;
Chao Yu81114ba2018-04-09 20:25:06 +0800437 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900438 } else if (ino == F2FS_META_INO(sbi)) {
439 inode->i_mapping->a_ops = &f2fs_meta_aops;
Chao Yu81114ba2018-04-09 20:25:06 +0800440 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900441 } else if (S_ISREG(inode->i_mode)) {
442 inode->i_op = &f2fs_file_inode_operations;
443 inode->i_fop = &f2fs_file_operations;
444 inode->i_mapping->a_ops = &f2fs_dblock_aops;
445 } else if (S_ISDIR(inode->i_mode)) {
446 inode->i_op = &f2fs_dir_inode_operations;
447 inode->i_fop = &f2fs_dir_operations;
448 inode->i_mapping->a_ops = &f2fs_dblock_aops;
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800449 inode_nohighmem(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900450 } else if (S_ISLNK(inode->i_mode)) {
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700451 if (f2fs_encrypted_inode(inode))
452 inode->i_op = &f2fs_encrypted_symlink_inode_operations;
453 else
454 inode->i_op = &f2fs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -0500455 inode_nohighmem(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900456 inode->i_mapping->a_ops = &f2fs_dblock_aops;
457 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
458 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
459 inode->i_op = &f2fs_special_inode_operations;
460 init_special_inode(inode, inode->i_mode, inode->i_rdev);
461 } else {
462 ret = -EIO;
463 goto bad_inode;
464 }
Jaegeuk Kim93607122017-05-16 13:20:16 -0700465 f2fs_set_inode_flags(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900466 unlock_new_inode(inode);
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900467 trace_f2fs_iget(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900468 return inode;
469
470bad_inode:
471 iget_failed(inode);
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900472 trace_f2fs_iget_exit(inode, ret);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900473 return ERR_PTR(ret);
474}
475
Jaegeuk Kime8ea9b32016-09-09 16:59:39 -0700476struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino)
477{
478 struct inode *inode;
479retry:
480 inode = f2fs_iget(sb, ino);
481 if (IS_ERR(inode)) {
482 if (PTR_ERR(inode) == -ENOMEM) {
483 congestion_wait(BLK_RW_ASYNC, HZ/50);
484 goto retry;
485 }
486 }
487 return inode;
488}
489
Chao Yu4d57b862018-05-30 00:20:41 +0800490void f2fs_update_inode(struct inode *inode, struct page *node_page)
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900491{
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900492 struct f2fs_inode *ri;
Chao Yub691d982016-10-11 22:57:05 +0800493 struct extent_tree *et = F2FS_I(inode)->extent_tree;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900494
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800495 f2fs_wait_on_page_writeback(node_page, NODE, true);
Yunlei He211a6fa2017-12-05 12:07:47 +0800496 set_page_dirty(node_page);
497
498 f2fs_inode_synced(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900499
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900500 ri = F2FS_INODE(node_page);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900501
502 ri->i_mode = cpu_to_le16(inode->i_mode);
503 ri->i_advise = F2FS_I(inode)->i_advise;
504 ri->i_uid = cpu_to_le32(i_uid_read(inode));
505 ri->i_gid = cpu_to_le32(i_gid_read(inode));
506 ri->i_links = cpu_to_le32(inode->i_nlink);
507 ri->i_size = cpu_to_le64(i_size_read(inode));
Chao Yu000519f2017-07-06 01:11:31 +0800508 ri->i_blocks = cpu_to_le64(SECTOR_TO_BLOCK(inode->i_blocks) + 1);
Chao Yu0c872e22015-02-05 17:46:29 +0800509
Chao Yub691d982016-10-11 22:57:05 +0800510 if (et) {
511 read_lock(&et->lock);
512 set_raw_extent(&et->largest, &ri->i_ext);
513 read_unlock(&et->lock);
514 } else {
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700515 memset(&ri->i_ext, 0, sizeof(ri->i_ext));
Chao Yub691d982016-10-11 22:57:05 +0800516 }
Jaegeuk Kim91942322016-05-20 10:13:22 -0700517 set_raw_inline(inode, ri);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900518
519 ri->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
520 ri->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
521 ri->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
522 ri->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
523 ri->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
524 ri->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
Chao Yu1c41e6802018-05-07 20:28:52 +0800525 if (S_ISDIR(inode->i_mode))
526 ri->i_current_depth =
527 cpu_to_le32(F2FS_I(inode)->i_current_depth);
528 else if (S_ISREG(inode->i_mode))
Chao Yu2ef79ec2018-05-07 20:28:54 +0800529 ri->i_gc_failures =
530 cpu_to_le16(F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN]);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900531 ri->i_xattr_nid = cpu_to_le32(F2FS_I(inode)->i_xattr_nid);
532 ri->i_flags = cpu_to_le32(F2FS_I(inode)->i_flags);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900533 ri->i_pino = cpu_to_le32(F2FS_I(inode)->i_pino);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900534 ri->i_generation = cpu_to_le32(inode->i_generation);
Jaegeuk Kim38431542014-02-27 18:20:00 +0900535 ri->i_dir_level = F2FS_I(inode)->i_dir_level;
Changman Lee7d79e752013-01-23 09:40:23 +0900536
Chao Yu5c571322017-07-26 00:01:41 +0800537 if (f2fs_has_extra_attr(inode)) {
Chao Yu7a2af762017-07-19 00:19:06 +0800538 ri->i_extra_isize = cpu_to_le16(F2FS_I(inode)->i_extra_isize);
539
Chao Yu6afc6622017-09-06 21:59:50 +0800540 if (f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(inode)->sb))
541 ri->i_inline_xattr_size =
542 cpu_to_le16(F2FS_I(inode)->i_inline_xattr_size);
543
Chao Yu5c571322017-07-26 00:01:41 +0800544 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)->sb) &&
545 F2FS_FITS_IN_INODE(ri, F2FS_I(inode)->i_extra_isize,
546 i_projid)) {
547 projid_t i_projid;
548
549 i_projid = from_kprojid(&init_user_ns,
550 F2FS_I(inode)->i_projid);
551 ri->i_projid = cpu_to_le32(i_projid);
552 }
Chao Yu1c1d35d2018-01-25 14:54:42 +0800553
554 if (f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)->sb) &&
555 F2FS_FITS_IN_INODE(ri, F2FS_I(inode)->i_extra_isize,
556 i_crtime)) {
557 ri->i_crtime =
558 cpu_to_le64(F2FS_I(inode)->i_crtime.tv_sec);
559 ri->i_crtime_nsec =
560 cpu_to_le32(F2FS_I(inode)->i_crtime.tv_nsec);
561 }
Chao Yu5c571322017-07-26 00:01:41 +0800562 }
563
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +0900564 __set_inode_rdev(inode, ri);
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800565
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -0800566 /* deleted inode */
567 if (inode->i_nlink == 0)
568 clear_inline_node(node_page);
569
Arnd Bergmann24b81df2018-06-20 10:02:19 +0200570 F2FS_I(inode)->i_disk_time[0] = inode->i_atime;
571 F2FS_I(inode)->i_disk_time[1] = inode->i_ctime;
572 F2FS_I(inode)->i_disk_time[2] = inode->i_mtime;
Jaegeuk Kim214c2462018-03-29 22:50:41 -0700573 F2FS_I(inode)->i_disk_time[3] = F2FS_I(inode)->i_crtime;
Weichao Guo54c55c42018-03-09 23:10:21 +0800574
575#ifdef CONFIG_F2FS_CHECK_FS
576 f2fs_inode_chksum_set(F2FS_I_SB(inode), node_page);
577#endif
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900578}
579
Chao Yu4d57b862018-05-30 00:20:41 +0800580void f2fs_update_inode_page(struct inode *inode)
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900581{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700582 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900583 struct page *node_page;
Jaegeuk Kim744602c2014-01-24 09:42:16 +0900584retry:
Chao Yu4d57b862018-05-30 00:20:41 +0800585 node_page = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kim744602c2014-01-24 09:42:16 +0900586 if (IS_ERR(node_page)) {
587 int err = PTR_ERR(node_page);
588 if (err == -ENOMEM) {
589 cond_resched();
590 goto retry;
591 } else if (err != -ENOENT) {
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -0700592 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim744602c2014-01-24 09:42:16 +0900593 }
Yunlei He211a6fa2017-12-05 12:07:47 +0800594 return;
Jaegeuk Kim744602c2014-01-24 09:42:16 +0900595 }
Chao Yu4d57b862018-05-30 00:20:41 +0800596 f2fs_update_inode(inode, node_page);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900597 f2fs_put_page(node_page, 1);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900598}
599
600int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc)
601{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700602 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900603
604 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
605 inode->i_ino == F2FS_META_INO(sbi))
606 return 0;
607
Jaegeuk Kim91942322016-05-20 10:13:22 -0700608 if (!is_inode_flag_set(inode, FI_DIRTY_INODE))
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900609 return 0;
610
Jaegeuk Kim39936832012-11-22 16:21:29 +0900611 /*
Jaegeuk Kimc5cd29d2015-09-12 11:25:30 -0700612 * We need to balance fs here to prevent from producing dirty node pages
Jaegeuk Kim39936832012-11-22 16:21:29 +0900613 * during the urgent cleaning time when runing out of free sections.
614 */
Chao Yu4d57b862018-05-30 00:20:41 +0800615 f2fs_update_inode_page(inode);
Jaegeuk Kima7881892017-04-20 13:51:57 -0700616 if (wbc && wbc->nr_to_write)
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800617 f2fs_balance_fs(sbi, true);
Jaegeuk Kim744602c2014-01-24 09:42:16 +0900618 return 0;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900619}
620
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900621/*
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900622 * Called at the last iput() if i_nlink is zero
623 */
624void f2fs_evict_inode(struct inode *inode)
625{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700626 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700627 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
Chao Yu13ec7292015-08-24 17:40:45 +0800628 int err = 0;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900629
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700630 /* some remained atomic pages should discarded */
Jaegeuk Kim1e843712014-12-09 06:08:59 -0800631 if (f2fs_is_atomic_file(inode))
Chao Yu4d57b862018-05-30 00:20:41 +0800632 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700633
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900634 trace_f2fs_evict_inode(inode);
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700635 truncate_inode_pages_final(&inode->i_data);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900636
637 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
638 inode->i_ino == F2FS_META_INO(sbi))
Chao Yudbf20cb2014-07-25 12:00:57 +0800639 goto out_clear;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900640
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -0700641 f2fs_bug_on(sbi, get_dirty_pages(inode));
Chao Yu4d57b862018-05-30 00:20:41 +0800642 f2fs_remove_dirty_inode(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900643
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700644 f2fs_destroy_extent_tree(inode);
645
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900646 if (inode->i_nlink || is_bad_inode(inode))
647 goto no_delete;
648
Chao Yu0abd6752017-07-09 00:13:07 +0800649 dquot_initialize(inode);
650
Chao Yu4d57b862018-05-30 00:20:41 +0800651 f2fs_remove_ino_entry(sbi, inode->i_ino, APPEND_INO);
652 f2fs_remove_ino_entry(sbi, inode->i_ino, UPDATE_INO);
653 f2fs_remove_ino_entry(sbi, inode->i_ino, FLUSH_INO);
Chao Yu60dcedc2016-11-02 20:43:21 +0800654
Changman Leed6212a52013-01-29 18:30:07 +0900655 sb_start_intwrite(inode->i_sb);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700656 set_inode_flag(inode, FI_NO_ALLOC);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900657 i_size_write(inode, 0);
Jaegeuk Kim4c0c2942016-05-03 09:22:18 -0700658retry:
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900659 if (F2FS_HAS_BLOCKS(inode))
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700660 err = f2fs_truncate(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900661
Jaegeuk Kim8c1b3c02017-03-07 13:32:20 -0800662#ifdef CONFIG_F2FS_FAULT_INJECTION
663 if (time_to_inject(sbi, FAULT_EVICT_INODE)) {
664 f2fs_show_injection_info(FAULT_EVICT_INODE);
665 err = -EIO;
666 }
667#endif
Chao Yu13ec7292015-08-24 17:40:45 +0800668 if (!err) {
669 f2fs_lock_op(sbi);
Chao Yu4d57b862018-05-30 00:20:41 +0800670 err = f2fs_remove_inode_page(inode);
Chao Yu13ec7292015-08-24 17:40:45 +0800671 f2fs_unlock_op(sbi);
Chao Yua11b9f62016-10-11 22:56:59 +0800672 if (err == -ENOENT)
673 err = 0;
Chao Yu13ec7292015-08-24 17:40:45 +0800674 }
Jaegeuk Kim39936832012-11-22 16:21:29 +0900675
Jaegeuk Kim4c0c2942016-05-03 09:22:18 -0700676 /* give more chances, if ENOMEM case */
677 if (err == -ENOMEM) {
678 err = 0;
679 goto retry;
680 }
681
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700682 if (err)
Chao Yu4d57b862018-05-30 00:20:41 +0800683 f2fs_update_inode_page(inode);
Chao Yu0abd6752017-07-09 00:13:07 +0800684 dquot_free_inode(inode);
Changman Leed6212a52013-01-29 18:30:07 +0900685 sb_end_intwrite(inode->i_sb);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900686no_delete:
Chao Yu0abd6752017-07-09 00:13:07 +0800687 dquot_drop(inode);
688
Chao Yud5e8f6c2015-07-15 17:28:53 +0800689 stat_dec_inline_xattr(inode);
Jaegeuk Kim3289c062014-10-13 20:00:16 -0700690 stat_dec_inline_dir(inode);
Jaegeuk Kime7a2bf22014-10-14 10:29:50 -0700691 stat_dec_inline_inode(inode);
Chao Yu0bdee482015-03-19 19:27:51 +0800692
Jaegeuk Kim943973c2017-10-12 19:12:53 -0700693 if (likely(!is_set_ckpt_flags(sbi, CP_ERROR_FLAG)))
Chao Yuca7d8022017-09-12 14:04:05 +0800694 f2fs_bug_on(sbi, is_inode_flag_set(inode, FI_DIRTY_INODE));
Jaegeuk Kim943973c2017-10-12 19:12:53 -0700695 else
696 f2fs_inode_synced(inode);
Chao Yuca7d8022017-09-12 14:04:05 +0800697
Jaegeuk Kim4f295442017-03-04 13:56:10 -0800698 /* ino == 0, if f2fs_new_inode() was failed t*/
699 if (inode->i_ino)
700 invalidate_mapping_pages(NODE_MAPPING(sbi), inode->i_ino,
701 inode->i_ino);
Chao Yu002a41c2014-08-04 09:54:58 +0800702 if (xnid)
703 invalidate_mapping_pages(NODE_MAPPING(sbi), xnid, xnid);
Chao Yu60dcedc2016-11-02 20:43:21 +0800704 if (inode->i_nlink) {
705 if (is_inode_flag_set(inode, FI_APPEND_WRITE))
Chao Yu4d57b862018-05-30 00:20:41 +0800706 f2fs_add_ino_entry(sbi, inode->i_ino, APPEND_INO);
Chao Yu60dcedc2016-11-02 20:43:21 +0800707 if (is_inode_flag_set(inode, FI_UPDATE_WRITE))
Chao Yu4d57b862018-05-30 00:20:41 +0800708 f2fs_add_ino_entry(sbi, inode->i_ino, UPDATE_INO);
Chao Yu60dcedc2016-11-02 20:43:21 +0800709 }
Jaegeuk Kim91942322016-05-20 10:13:22 -0700710 if (is_inode_flag_set(inode, FI_FREE_NID)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800711 f2fs_alloc_nid_failed(sbi, inode->i_ino);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700712 clear_inode_flag(inode, FI_FREE_NID);
Jaegeuk Kimd8c42562017-06-01 15:39:27 -0700713 } else {
Jaegeuk Kima4f843b2018-04-23 23:02:31 -0600714 /*
715 * If xattr nid is corrupted, we can reach out error condition,
Chao Yu4d57b862018-05-30 00:20:41 +0800716 * err & !f2fs_exist_written_data(sbi, inode->i_ino, ORPHAN_INO)).
717 * In that case, f2fs_check_nid_range() is enough to give a clue.
Jaegeuk Kima4f843b2018-04-23 23:02:31 -0600718 */
Jaegeuk Kimc9b63bd2015-06-23 10:36:08 -0700719 }
Chao Yudbf20cb2014-07-25 12:00:57 +0800720out_clear:
Eric Biggers3d204e22018-01-11 23:30:13 -0500721 fscrypt_put_encryption_info(inode);
Chao Yudbf20cb2014-07-25 12:00:57 +0800722 clear_inode(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900723}
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700724
725/* caller should call f2fs_lock_op() */
Chao Yu4d57b862018-05-30 00:20:41 +0800726void f2fs_handle_failed_inode(struct inode *inode)
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700727{
728 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700729 struct node_info ni;
Chao Yu77357302018-07-17 00:02:17 +0800730 int err;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700731
Chao Yua11b9f62016-10-11 22:56:59 +0800732 /*
733 * clear nlink of inode in order to release resource of inode
734 * immediately.
735 */
736 clear_nlink(inode);
737
738 /*
739 * we must call this to avoid inode being remained as dirty, resulting
740 * in a panic when flushing dirty inodes in gdirty_list.
741 */
Chao Yu4d57b862018-05-30 00:20:41 +0800742 f2fs_update_inode_page(inode);
Jaegeuk Kim9bb02c32017-04-11 19:01:26 -0700743 f2fs_inode_synced(inode);
Chao Yua11b9f62016-10-11 22:56:59 +0800744
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700745 /* don't make bad inode, since it becomes a regular file. */
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700746 unlock_new_inode(inode);
747
Chao Yu13ec7292015-08-24 17:40:45 +0800748 /*
Chao Yu13ec7292015-08-24 17:40:45 +0800749 * Note: we should add inode to orphan list before f2fs_unlock_op()
750 * so we can prevent losing this orphan when encoutering checkpoint
751 * and following suddenly power-off.
752 */
Chao Yu77357302018-07-17 00:02:17 +0800753 err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
754 if (err) {
755 set_sbi_flag(sbi, SBI_NEED_FSCK);
756 f2fs_msg(sbi->sb, KERN_WARNING,
757 "May loss orphan inode, run fsck to fix.");
758 goto out;
759 }
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700760
761 if (ni.blk_addr != NULL_ADDR) {
Chao Yu77357302018-07-17 00:02:17 +0800762 err = f2fs_acquire_orphan_inode(sbi);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700763 if (err) {
764 set_sbi_flag(sbi, SBI_NEED_FSCK);
765 f2fs_msg(sbi->sb, KERN_WARNING,
766 "Too many orphan inodes, run fsck to fix.");
767 } else {
Chao Yu4d57b862018-05-30 00:20:41 +0800768 f2fs_add_orphan_inode(inode);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700769 }
Chao Yu4d57b862018-05-30 00:20:41 +0800770 f2fs_alloc_nid_done(sbi, inode->i_ino);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700771 } else {
Jaegeuk Kim91942322016-05-20 10:13:22 -0700772 set_inode_flag(inode, FI_FREE_NID);
Chao Yu13ec7292015-08-24 17:40:45 +0800773 }
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700774
Chao Yu77357302018-07-17 00:02:17 +0800775out:
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700776 f2fs_unlock_op(sbi);
777
778 /* iput will drop the inode object */
779 iput(inode);
780}