Merge tag 'for-5.2-rc5-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave...
[linux-2.6-block.git] / fs / f2fs / inode.c
CommitLineData
7c1a000d 1// SPDX-License-Identifier: GPL-2.0
0a8165d7 2/*
19f99cee
JK
3 * fs/f2fs/inode.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
19f99cee
JK
7 */
8#include <linux/fs.h>
9#include <linux/f2fs_fs.h>
10#include <linux/buffer_head.h>
e8ea9b3d 11#include <linux/backing-dev.h>
19f99cee
JK
12#include <linux/writeback.h>
13
14#include "f2fs.h"
15#include "node.h"
0eb0adad 16#include "segment.h"
dd6c89b5 17#include "xattr.h"
19f99cee 18
a2a4a7e4
NJ
19#include <trace/events/f2fs.h>
20
7c45729a 21void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync)
b56ab837 22{
9ac1e2d8
DJ
23 if (is_inode_flag_set(inode, FI_NEW_INODE))
24 return;
25
7c45729a 26 if (f2fs_inode_dirtied(inode, sync))
b56ab837 27 return;
7c45729a 28
b56ab837
JK
29 mark_inode_dirty_sync(inode);
30}
31
19f99cee
JK
32void f2fs_set_inode_flags(struct inode *inode)
33{
34 unsigned int flags = F2FS_I(inode)->i_flags;
8abfb36a 35 unsigned int new_fl = 0;
19f99cee 36
59c84408 37 if (flags & F2FS_SYNC_FL)
8abfb36a 38 new_fl |= S_SYNC;
59c84408 39 if (flags & F2FS_APPEND_FL)
8abfb36a 40 new_fl |= S_APPEND;
59c84408 41 if (flags & F2FS_IMMUTABLE_FL)
8abfb36a 42 new_fl |= S_IMMUTABLE;
59c84408 43 if (flags & F2FS_NOATIME_FL)
8abfb36a 44 new_fl |= S_NOATIME;
59c84408 45 if (flags & F2FS_DIRSYNC_FL)
8abfb36a 46 new_fl |= S_DIRSYNC;
62230e0d 47 if (file_is_encrypt(inode))
2ee6a576 48 new_fl |= S_ENCRYPTED;
6a678857 49 inode_set_flags(inode, new_fl,
2ee6a576
EB
50 S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|
51 S_ENCRYPTED);
19f99cee
JK
52}
53
3d1e3807
JK
54static void __get_inode_rdev(struct inode *inode, struct f2fs_inode *ri)
55{
7a2af766
CY
56 int extra_size = get_extra_isize(inode);
57
3d1e3807
JK
58 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
59 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
7a2af766
CY
60 if (ri->i_addr[extra_size])
61 inode->i_rdev = old_decode_dev(
62 le32_to_cpu(ri->i_addr[extra_size]));
3d1e3807 63 else
7a2af766
CY
64 inode->i_rdev = new_decode_dev(
65 le32_to_cpu(ri->i_addr[extra_size + 1]));
3d1e3807
JK
66 }
67}
68
91291e99 69static int __written_first_block(struct f2fs_sb_info *sbi,
e1da7872 70 struct f2fs_inode *ri)
3c6c2beb 71{
7a2af766 72 block_t addr = le32_to_cpu(ri->i_addr[offset_in_addr(ri)]);
adad81ed 73
91291e99
CY
74 if (!__is_valid_data_blkaddr(addr))
75 return 1;
93770ab7 76 if (!f2fs_is_valid_blkaddr(sbi, addr, DATA_GENERIC_ENHANCE))
91291e99
CY
77 return -EFAULT;
78 return 0;
3c6c2beb
JK
79}
80
3d1e3807
JK
81static void __set_inode_rdev(struct inode *inode, struct f2fs_inode *ri)
82{
7a2af766
CY
83 int extra_size = get_extra_isize(inode);
84
3d1e3807
JK
85 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
86 if (old_valid_dev(inode->i_rdev)) {
7a2af766 87 ri->i_addr[extra_size] =
6c311ec6 88 cpu_to_le32(old_encode_dev(inode->i_rdev));
7a2af766 89 ri->i_addr[extra_size + 1] = 0;
3d1e3807 90 } else {
7a2af766
CY
91 ri->i_addr[extra_size] = 0;
92 ri->i_addr[extra_size + 1] =
6c311ec6 93 cpu_to_le32(new_encode_dev(inode->i_rdev));
7a2af766 94 ri->i_addr[extra_size + 2] = 0;
3d1e3807
JK
95 }
96 }
97}
98
9e5ba77f 99static void __recover_inline_status(struct inode *inode, struct page *ipage)
b3d208f9 100{
f2470371 101 void *inline_data = inline_data_addr(inode, ipage);
9e5ba77f 102 __le32 *start = inline_data;
f2470371 103 __le32 *end = start + MAX_INLINE_DATA(inode) / sizeof(__le32);
b3d208f9 104
9e5ba77f
CY
105 while (start < end) {
106 if (*start++) {
bae0ee7a 107 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
b3d208f9 108
91942321
JK
109 set_inode_flag(inode, FI_DATA_EXIST);
110 set_raw_inline(inode, F2FS_INODE(ipage));
9e5ba77f
CY
111 set_page_dirty(ipage);
112 return;
113 }
b3d208f9 114 }
9e5ba77f 115 return;
b3d208f9
JK
116}
117
704956ec
CY
118static bool f2fs_enable_inode_chksum(struct f2fs_sb_info *sbi, struct page *page)
119{
120 struct f2fs_inode *ri = &F2FS_NODE(page)->i;
704956ec 121
7beb01f7 122 if (!f2fs_sb_has_inode_chksum(sbi))
704956ec
CY
123 return false;
124
4c6b56c0 125 if (!IS_INODE(page) || !(ri->i_inline & F2FS_EXTRA_ATTR))
704956ec
CY
126 return false;
127
d6964949
ZZ
128 if (!F2FS_FITS_IN_INODE(ri, le16_to_cpu(ri->i_extra_isize),
129 i_inode_checksum))
704956ec
CY
130 return false;
131
132 return true;
133}
134
135static __u32 f2fs_inode_chksum(struct f2fs_sb_info *sbi, struct page *page)
136{
137 struct f2fs_node *node = F2FS_NODE(page);
138 struct f2fs_inode *ri = &node->i;
139 __le32 ino = node->footer.ino;
140 __le32 gen = ri->i_generation;
141 __u32 chksum, chksum_seed;
142 __u32 dummy_cs = 0;
143 unsigned int offset = offsetof(struct f2fs_inode, i_inode_checksum);
144 unsigned int cs_size = sizeof(dummy_cs);
145
146 chksum = f2fs_chksum(sbi, sbi->s_chksum_seed, (__u8 *)&ino,
147 sizeof(ino));
148 chksum_seed = f2fs_chksum(sbi, chksum, (__u8 *)&gen, sizeof(gen));
149
150 chksum = f2fs_chksum(sbi, chksum_seed, (__u8 *)ri, offset);
151 chksum = f2fs_chksum(sbi, chksum, (__u8 *)&dummy_cs, cs_size);
152 offset += cs_size;
153 chksum = f2fs_chksum(sbi, chksum, (__u8 *)ri + offset,
154 F2FS_BLKSIZE - offset);
155 return chksum;
156}
157
158bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page)
159{
160 struct f2fs_inode *ri;
161 __u32 provided, calculated;
162
83a3bfdb
JK
163 if (unlikely(is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN)))
164 return true;
165
54c55c4e
WG
166#ifdef CONFIG_F2FS_CHECK_FS
167 if (!f2fs_enable_inode_chksum(sbi, page))
168#else
ee605234
JK
169 if (!f2fs_enable_inode_chksum(sbi, page) ||
170 PageDirty(page) || PageWriteback(page))
54c55c4e 171#endif
704956ec
CY
172 return true;
173
174 ri = &F2FS_NODE(page)->i;
175 provided = le32_to_cpu(ri->i_inode_checksum);
176 calculated = f2fs_inode_chksum(sbi, page);
177
178 if (provided != calculated)
179 f2fs_msg(sbi->sb, KERN_WARNING,
b42b179b
CY
180 "checksum invalid, nid = %lu, ino_of_node = %x, %x vs. %x",
181 page->index, ino_of_node(page), provided, calculated);
704956ec
CY
182
183 return provided == calculated;
184}
185
186void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page)
187{
188 struct f2fs_inode *ri = &F2FS_NODE(page)->i;
189
190 if (!f2fs_enable_inode_chksum(sbi, page))
191 return;
192
193 ri->i_inode_checksum = cpu_to_le32(f2fs_inode_chksum(sbi, page));
194}
195
e34438c9 196static bool sanity_check_inode(struct inode *inode, struct page *node_page)
5d64600d
JK
197{
198 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
18dd6470 199 struct f2fs_inode_info *fi = F2FS_I(inode);
e34438c9
CY
200 unsigned long long iblocks;
201
202 iblocks = le64_to_cpu(F2FS_INODE(node_page)->i_blocks);
203 if (!iblocks) {
204 set_sbi_flag(sbi, SBI_NEED_FSCK);
205 f2fs_msg(sbi->sb, KERN_WARNING,
206 "%s: corrupted inode i_blocks i_ino=%lx iblocks=%llu, "
207 "run fsck to fix.",
208 __func__, inode->i_ino, iblocks);
209 return false;
210 }
211
212 if (ino_of_node(node_page) != nid_of_node(node_page)) {
213 set_sbi_flag(sbi, SBI_NEED_FSCK);
214 f2fs_msg(sbi->sb, KERN_WARNING,
215 "%s: corrupted inode footer i_ino=%lx, ino,nid: "
216 "[%u, %u] run fsck to fix.",
217 __func__, inode->i_ino,
218 ino_of_node(node_page), nid_of_node(node_page));
219 return false;
220 }
5d64600d 221
7beb01f7 222 if (f2fs_sb_has_flexible_inline_xattr(sbi)
5d64600d
JK
223 && !f2fs_has_extra_attr(inode)) {
224 set_sbi_flag(sbi, SBI_NEED_FSCK);
225 f2fs_msg(sbi->sb, KERN_WARNING,
226 "%s: corrupted inode ino=%lx, run fsck to fix.",
227 __func__, inode->i_ino);
228 return false;
229 }
76d56d4a
CY
230
231 if (f2fs_has_extra_attr(inode) &&
7beb01f7 232 !f2fs_sb_has_extra_attr(sbi)) {
76d56d4a
CY
233 set_sbi_flag(sbi, SBI_NEED_FSCK);
234 f2fs_msg(sbi->sb, KERN_WARNING,
235 "%s: inode (ino=%lx) is with extra_attr, "
236 "but extra_attr feature is off",
237 __func__, inode->i_ino);
238 return false;
239 }
c9b60788 240
18dd6470
CY
241 if (fi->i_extra_isize > F2FS_TOTAL_EXTRA_ATTR_SIZE ||
242 fi->i_extra_isize % sizeof(__le32)) {
243 set_sbi_flag(sbi, SBI_NEED_FSCK);
244 f2fs_msg(sbi->sb, KERN_WARNING,
245 "%s: inode (ino=%lx) has corrupted i_extra_isize: %d, "
246 "max: %zu",
247 __func__, inode->i_ino, fi->i_extra_isize,
248 F2FS_TOTAL_EXTRA_ATTR_SIZE);
249 return false;
250 }
251
dd6c89b5
CY
252 if (f2fs_has_extra_attr(inode) &&
253 f2fs_sb_has_flexible_inline_xattr(sbi) &&
254 f2fs_has_inline_xattr(inode) &&
255 (!fi->i_inline_xattr_size ||
256 fi->i_inline_xattr_size > MAX_INLINE_XATTR_SIZE)) {
257 set_sbi_flag(sbi, SBI_NEED_FSCK);
258 f2fs_msg(sbi->sb, KERN_WARNING,
259 "%s: inode (ino=%lx) has corrupted "
260 "i_inline_xattr_size: %d, max: %zu",
261 __func__, inode->i_ino, fi->i_inline_xattr_size,
262 MAX_INLINE_XATTR_SIZE);
263 return false;
264 }
265
c9b60788
CY
266 if (F2FS_I(inode)->extent_tree) {
267 struct extent_info *ei = &F2FS_I(inode)->extent_tree->largest;
268
269 if (ei->len &&
93770ab7
CY
270 (!f2fs_is_valid_blkaddr(sbi, ei->blk,
271 DATA_GENERIC_ENHANCE) ||
c9b60788 272 !f2fs_is_valid_blkaddr(sbi, ei->blk + ei->len - 1,
93770ab7 273 DATA_GENERIC_ENHANCE))) {
c9b60788
CY
274 set_sbi_flag(sbi, SBI_NEED_FSCK);
275 f2fs_msg(sbi->sb, KERN_WARNING,
276 "%s: inode (ino=%lx) extent info [%u, %u, %u] "
277 "is incorrect, run fsck to fix",
278 __func__, inode->i_ino,
279 ei->blk, ei->fofs, ei->len);
280 return false;
281 }
282 }
bcbfbd60
CY
283
284 if (f2fs_has_inline_data(inode) &&
285 (!S_ISREG(inode->i_mode) && !S_ISLNK(inode->i_mode))) {
286 set_sbi_flag(sbi, SBI_NEED_FSCK);
287 f2fs_msg(sbi->sb, KERN_WARNING,
288 "%s: inode (ino=%lx, mode=%u) should not have "
289 "inline_data, run fsck to fix",
290 __func__, inode->i_ino, inode->i_mode);
291 return false;
292 }
293
294 if (f2fs_has_inline_dentry(inode) && !S_ISDIR(inode->i_mode)) {
295 set_sbi_flag(sbi, SBI_NEED_FSCK);
296 f2fs_msg(sbi->sb, KERN_WARNING,
297 "%s: inode (ino=%lx, mode=%u) should not have "
298 "inline_dentry, run fsck to fix",
299 __func__, inode->i_ino, inode->i_mode);
300 return false;
301 }
302
5d64600d
JK
303 return true;
304}
305
19f99cee
JK
306static int do_read_inode(struct inode *inode)
307{
4081363f 308 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
19f99cee
JK
309 struct f2fs_inode_info *fi = F2FS_I(inode);
310 struct page *node_page;
19f99cee 311 struct f2fs_inode *ri;
5c57132e 312 projid_t i_projid;
91291e99 313 int err;
19f99cee
JK
314
315 /* Check if ino is within scope */
4d57b86d 316 if (f2fs_check_nid_range(sbi, inode->i_ino))
064e0823 317 return -EINVAL;
19f99cee 318
4d57b86d 319 node_page = f2fs_get_node_page(sbi, inode->i_ino);
19f99cee
JK
320 if (IS_ERR(node_page))
321 return PTR_ERR(node_page);
322
58bfaf44 323 ri = F2FS_INODE(node_page);
19f99cee
JK
324
325 inode->i_mode = le16_to_cpu(ri->i_mode);
326 i_uid_write(inode, le32_to_cpu(ri->i_uid));
327 i_gid_write(inode, le32_to_cpu(ri->i_gid));
328 set_nlink(inode, le32_to_cpu(ri->i_links));
329 inode->i_size = le64_to_cpu(ri->i_size);
000519f2 330 inode->i_blocks = SECTOR_FROM_BLOCK(le64_to_cpu(ri->i_blocks) - 1);
19f99cee
JK
331
332 inode->i_atime.tv_sec = le64_to_cpu(ri->i_atime);
333 inode->i_ctime.tv_sec = le64_to_cpu(ri->i_ctime);
334 inode->i_mtime.tv_sec = le64_to_cpu(ri->i_mtime);
335 inode->i_atime.tv_nsec = le32_to_cpu(ri->i_atime_nsec);
336 inode->i_ctime.tv_nsec = le32_to_cpu(ri->i_ctime_nsec);
337 inode->i_mtime.tv_nsec = le32_to_cpu(ri->i_mtime_nsec);
338 inode->i_generation = le32_to_cpu(ri->i_generation);
1c41e680
CY
339 if (S_ISDIR(inode->i_mode))
340 fi->i_current_depth = le32_to_cpu(ri->i_current_depth);
341 else if (S_ISREG(inode->i_mode))
2ef79ecb
CY
342 fi->i_gc_failures[GC_FAILURE_PIN] =
343 le16_to_cpu(ri->i_gc_failures);
19f99cee
JK
344 fi->i_xattr_nid = le32_to_cpu(ri->i_xattr_nid);
345 fi->i_flags = le32_to_cpu(ri->i_flags);
346 fi->flags = 0;
19f99cee 347 fi->i_advise = ri->i_advise;
6666e6aa 348 fi->i_pino = le32_to_cpu(ri->i_pino);
38431545 349 fi->i_dir_level = ri->i_dir_level;
3d1e3807 350
ed3d1256
JK
351 if (f2fs_init_extent_tree(inode, &ri->i_ext))
352 set_page_dirty(node_page);
0c872e2d 353
91942321 354 get_inline_info(inode, ri);
3d1e3807 355
7a2af766
CY
356 fi->i_extra_isize = f2fs_has_extra_attr(inode) ?
357 le16_to_cpu(ri->i_extra_isize) : 0;
358
7beb01f7 359 if (f2fs_sb_has_flexible_inline_xattr(sbi)) {
6afc662e
CY
360 fi->i_inline_xattr_size = le16_to_cpu(ri->i_inline_xattr_size);
361 } else if (f2fs_has_inline_xattr(inode) ||
362 f2fs_has_inline_dentry(inode)) {
363 fi->i_inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
364 } else {
365
366 /*
367 * Previous inline data or directory always reserved 200 bytes
368 * in inode layout, even if inline_xattr is disabled. In order
369 * to keep inline_dentry's structure for backward compatibility,
370 * we get the space back only from inline_data.
371 */
372 fi->i_inline_xattr_size = 0;
373 }
374
18dd6470
CY
375 if (!sanity_check_inode(inode, node_page)) {
376 f2fs_put_page(node_page, 1);
377 return -EINVAL;
378 }
379
b3d208f9
JK
380 /* check data exist */
381 if (f2fs_has_inline_data(inode) && !f2fs_exist_data(inode))
9e5ba77f 382 __recover_inline_status(inode, node_page);
b3d208f9 383
ef2a0071
CY
384 /* try to recover cold bit for non-dir inode */
385 if (!S_ISDIR(inode->i_mode) && !is_cold_node(node_page)) {
386 set_cold_node(node_page, false);
387 set_page_dirty(node_page);
388 }
389
3d1e3807
JK
390 /* get rdev by using inline_info */
391 __get_inode_rdev(inode, ri);
392
dda9f4b9
CY
393 if (S_ISREG(inode->i_mode)) {
394 err = __written_first_block(sbi, ri);
395 if (err < 0) {
396 f2fs_put_page(node_page, 1);
397 return err;
398 }
399 if (!err)
400 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
91291e99 401 }
3c6c2beb 402
4d57b86d 403 if (!f2fs_need_inode_block_update(sbi, inode->i_ino))
26de9b11
JK
404 fi->last_disk_size = inode->i_size;
405
59c84408 406 if (fi->i_flags & F2FS_PROJINHERIT_FL)
5c57132e
CY
407 set_inode_flag(inode, FI_PROJ_INHERIT);
408
7beb01f7 409 if (f2fs_has_extra_attr(inode) && f2fs_sb_has_project_quota(sbi) &&
5c57132e
CY
410 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_projid))
411 i_projid = (projid_t)le32_to_cpu(ri->i_projid);
412 else
413 i_projid = F2FS_DEF_PROJID;
414 fi->i_projid = make_kprojid(&init_user_ns, i_projid);
415
7beb01f7 416 if (f2fs_has_extra_attr(inode) && f2fs_sb_has_inode_crtime(sbi) &&
1c1d35df
CY
417 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
418 fi->i_crtime.tv_sec = le64_to_cpu(ri->i_crtime);
419 fi->i_crtime.tv_nsec = le32_to_cpu(ri->i_crtime_nsec);
420 }
421
24b81dfc
AB
422 F2FS_I(inode)->i_disk_time[0] = inode->i_atime;
423 F2FS_I(inode)->i_disk_time[1] = inode->i_ctime;
424 F2FS_I(inode)->i_disk_time[2] = inode->i_mtime;
214c2461 425 F2FS_I(inode)->i_disk_time[3] = F2FS_I(inode)->i_crtime;
19f99cee 426 f2fs_put_page(node_page, 1);
9d1015dd 427
d5e8f6c9 428 stat_inc_inline_xattr(inode);
9d1015dd
JK
429 stat_inc_inline_inode(inode);
430 stat_inc_inline_dir(inode);
431
9e5ba77f 432 return 0;
19f99cee
JK
433}
434
435struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
436{
437 struct f2fs_sb_info *sbi = F2FS_SB(sb);
438 struct inode *inode;
a2a4a7e4 439 int ret = 0;
19f99cee
JK
440
441 inode = iget_locked(sb, ino);
442 if (!inode)
443 return ERR_PTR(-ENOMEM);
a2a4a7e4
NJ
444
445 if (!(inode->i_state & I_NEW)) {
446 trace_f2fs_iget(inode);
19f99cee 447 return inode;
a2a4a7e4 448 }
19f99cee
JK
449 if (ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi))
450 goto make_now;
451
452 ret = do_read_inode(inode);
453 if (ret)
454 goto bad_inode;
19f99cee
JK
455make_now:
456 if (ino == F2FS_NODE_INO(sbi)) {
457 inode->i_mapping->a_ops = &f2fs_node_aops;
81114baa 458 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
19f99cee
JK
459 } else if (ino == F2FS_META_INO(sbi)) {
460 inode->i_mapping->a_ops = &f2fs_meta_aops;
81114baa 461 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
19f99cee
JK
462 } else if (S_ISREG(inode->i_mode)) {
463 inode->i_op = &f2fs_file_inode_operations;
464 inode->i_fop = &f2fs_file_operations;
465 inode->i_mapping->a_ops = &f2fs_dblock_aops;
466 } else if (S_ISDIR(inode->i_mode)) {
467 inode->i_op = &f2fs_dir_inode_operations;
468 inode->i_fop = &f2fs_dir_operations;
469 inode->i_mapping->a_ops = &f2fs_dblock_aops;
bdbc90fa 470 inode_nohighmem(inode);
19f99cee 471 } else if (S_ISLNK(inode->i_mode)) {
62230e0d 472 if (file_is_encrypt(inode))
cbaf042a
JK
473 inode->i_op = &f2fs_encrypted_symlink_inode_operations;
474 else
475 inode->i_op = &f2fs_symlink_inode_operations;
21fc61c7 476 inode_nohighmem(inode);
19f99cee
JK
477 inode->i_mapping->a_ops = &f2fs_dblock_aops;
478 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
479 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
480 inode->i_op = &f2fs_special_inode_operations;
481 init_special_inode(inode, inode->i_mode, inode->i_rdev);
482 } else {
483 ret = -EIO;
484 goto bad_inode;
485 }
93607124 486 f2fs_set_inode_flags(inode);
19f99cee 487 unlock_new_inode(inode);
a2a4a7e4 488 trace_f2fs_iget(inode);
19f99cee
JK
489 return inode;
490
491bad_inode:
546d22f0 492 f2fs_inode_synced(inode);
19f99cee 493 iget_failed(inode);
a2a4a7e4 494 trace_f2fs_iget_exit(inode, ret);
19f99cee
JK
495 return ERR_PTR(ret);
496}
497
e8ea9b3d
JK
498struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino)
499{
500 struct inode *inode;
501retry:
502 inode = f2fs_iget(sb, ino);
503 if (IS_ERR(inode)) {
504 if (PTR_ERR(inode) == -ENOMEM) {
505 congestion_wait(BLK_RW_ASYNC, HZ/50);
506 goto retry;
507 }
508 }
509 return inode;
510}
511
4d57b86d 512void f2fs_update_inode(struct inode *inode, struct page *node_page)
19f99cee 513{
19f99cee 514 struct f2fs_inode *ri;
b691d98f 515 struct extent_tree *et = F2FS_I(inode)->extent_tree;
19f99cee 516
bae0ee7a 517 f2fs_wait_on_page_writeback(node_page, NODE, true, true);
211a6fa0
YH
518 set_page_dirty(node_page);
519
520 f2fs_inode_synced(inode);
19f99cee 521
58bfaf44 522 ri = F2FS_INODE(node_page);
19f99cee
JK
523
524 ri->i_mode = cpu_to_le16(inode->i_mode);
525 ri->i_advise = F2FS_I(inode)->i_advise;
526 ri->i_uid = cpu_to_le32(i_uid_read(inode));
527 ri->i_gid = cpu_to_le32(i_gid_read(inode));
528 ri->i_links = cpu_to_le32(inode->i_nlink);
529 ri->i_size = cpu_to_le64(i_size_read(inode));
000519f2 530 ri->i_blocks = cpu_to_le64(SECTOR_TO_BLOCK(inode->i_blocks) + 1);
0c872e2d 531
b691d98f
CY
532 if (et) {
533 read_lock(&et->lock);
534 set_raw_extent(&et->largest, &ri->i_ext);
535 read_unlock(&et->lock);
536 } else {
3e72f721 537 memset(&ri->i_ext, 0, sizeof(ri->i_ext));
b691d98f 538 }
91942321 539 set_raw_inline(inode, ri);
19f99cee
JK
540
541 ri->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
542 ri->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
543 ri->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
544 ri->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
545 ri->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
546 ri->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
1c41e680
CY
547 if (S_ISDIR(inode->i_mode))
548 ri->i_current_depth =
549 cpu_to_le32(F2FS_I(inode)->i_current_depth);
550 else if (S_ISREG(inode->i_mode))
2ef79ecb
CY
551 ri->i_gc_failures =
552 cpu_to_le16(F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN]);
19f99cee
JK
553 ri->i_xattr_nid = cpu_to_le32(F2FS_I(inode)->i_xattr_nid);
554 ri->i_flags = cpu_to_le32(F2FS_I(inode)->i_flags);
6666e6aa 555 ri->i_pino = cpu_to_le32(F2FS_I(inode)->i_pino);
19f99cee 556 ri->i_generation = cpu_to_le32(inode->i_generation);
38431545 557 ri->i_dir_level = F2FS_I(inode)->i_dir_level;
7d79e75f 558
5c57132e 559 if (f2fs_has_extra_attr(inode)) {
7a2af766
CY
560 ri->i_extra_isize = cpu_to_le16(F2FS_I(inode)->i_extra_isize);
561
7beb01f7 562 if (f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(inode)))
6afc662e
CY
563 ri->i_inline_xattr_size =
564 cpu_to_le16(F2FS_I(inode)->i_inline_xattr_size);
565
7beb01f7 566 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)) &&
5c57132e
CY
567 F2FS_FITS_IN_INODE(ri, F2FS_I(inode)->i_extra_isize,
568 i_projid)) {
569 projid_t i_projid;
570
571 i_projid = from_kprojid(&init_user_ns,
572 F2FS_I(inode)->i_projid);
573 ri->i_projid = cpu_to_le32(i_projid);
574 }
1c1d35df 575
7beb01f7 576 if (f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
1c1d35df
CY
577 F2FS_FITS_IN_INODE(ri, F2FS_I(inode)->i_extra_isize,
578 i_crtime)) {
579 ri->i_crtime =
580 cpu_to_le64(F2FS_I(inode)->i_crtime.tv_sec);
581 ri->i_crtime_nsec =
582 cpu_to_le32(F2FS_I(inode)->i_crtime.tv_nsec);
583 }
5c57132e
CY
584 }
585
3d1e3807 586 __set_inode_rdev(inode, ri);
12719ae1 587
2049d4fc
JK
588 /* deleted inode */
589 if (inode->i_nlink == 0)
590 clear_inline_node(node_page);
591
24b81dfc
AB
592 F2FS_I(inode)->i_disk_time[0] = inode->i_atime;
593 F2FS_I(inode)->i_disk_time[1] = inode->i_ctime;
594 F2FS_I(inode)->i_disk_time[2] = inode->i_mtime;
214c2461 595 F2FS_I(inode)->i_disk_time[3] = F2FS_I(inode)->i_crtime;
54c55c4e
WG
596
597#ifdef CONFIG_F2FS_CHECK_FS
598 f2fs_inode_chksum_set(F2FS_I_SB(inode), node_page);
599#endif
19f99cee
JK
600}
601
4d57b86d 602void f2fs_update_inode_page(struct inode *inode)
19f99cee 603{
4081363f 604 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
19f99cee 605 struct page *node_page;
744602cf 606retry:
4d57b86d 607 node_page = f2fs_get_node_page(sbi, inode->i_ino);
744602cf
JK
608 if (IS_ERR(node_page)) {
609 int err = PTR_ERR(node_page);
610 if (err == -ENOMEM) {
611 cond_resched();
612 goto retry;
613 } else if (err != -ENOENT) {
38f91ca8 614 f2fs_stop_checkpoint(sbi, false);
744602cf 615 }
211a6fa0 616 return;
744602cf 617 }
4d57b86d 618 f2fs_update_inode(inode, node_page);
19f99cee 619 f2fs_put_page(node_page, 1);
19f99cee
JK
620}
621
39936837
JK
622int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc)
623{
4081363f 624 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
39936837
JK
625
626 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
627 inode->i_ino == F2FS_META_INO(sbi))
628 return 0;
629
91942321 630 if (!is_inode_flag_set(inode, FI_DIRTY_INODE))
b3783873
JK
631 return 0;
632
4354994f
DR
633 if (f2fs_is_checkpoint_ready(sbi))
634 return -ENOSPC;
635
39936837 636 /*
c5cd29d2 637 * We need to balance fs here to prevent from producing dirty node pages
39936837
JK
638 * during the urgent cleaning time when runing out of free sections.
639 */
4d57b86d 640 f2fs_update_inode_page(inode);
a7881893 641 if (wbc && wbc->nr_to_write)
2c4db1a6 642 f2fs_balance_fs(sbi, true);
744602cf 643 return 0;
39936837
JK
644}
645
0a8165d7 646/*
19f99cee
JK
647 * Called at the last iput() if i_nlink is zero
648 */
649void f2fs_evict_inode(struct inode *inode)
650{
4081363f 651 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
91942321 652 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
13ec7297 653 int err = 0;
19f99cee 654
88b88a66 655 /* some remained atomic pages should discarded */
1e84371f 656 if (f2fs_is_atomic_file(inode))
4d57b86d 657 f2fs_drop_inmem_pages(inode);
88b88a66 658
a2a4a7e4 659 trace_f2fs_evict_inode(inode);
91b0abe3 660 truncate_inode_pages_final(&inode->i_data);
19f99cee
JK
661
662 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
663 inode->i_ino == F2FS_META_INO(sbi))
dbf20cb2 664 goto out_clear;
19f99cee 665
a7ffdbe2 666 f2fs_bug_on(sbi, get_dirty_pages(inode));
4d57b86d 667 f2fs_remove_dirty_inode(inode);
19f99cee 668
3e72f721
JK
669 f2fs_destroy_extent_tree(inode);
670
19f99cee
JK
671 if (inode->i_nlink || is_bad_inode(inode))
672 goto no_delete;
673
af033b2a
CY
674 err = dquot_initialize(inode);
675 if (err) {
676 err = 0;
677 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
678 }
0abd675e 679
4d57b86d
CY
680 f2fs_remove_ino_entry(sbi, inode->i_ino, APPEND_INO);
681 f2fs_remove_ino_entry(sbi, inode->i_ino, UPDATE_INO);
682 f2fs_remove_ino_entry(sbi, inode->i_ino, FLUSH_INO);
60dcedc9 683
d6212a5f 684 sb_start_intwrite(inode->i_sb);
91942321 685 set_inode_flag(inode, FI_NO_ALLOC);
19f99cee 686 i_size_write(inode, 0);
4c0c2949 687retry:
19f99cee 688 if (F2FS_HAS_BLOCKS(inode))
9a449e9c 689 err = f2fs_truncate(inode);
19f99cee 690
8c1b3c0f
JK
691 if (time_to_inject(sbi, FAULT_EVICT_INODE)) {
692 f2fs_show_injection_info(FAULT_EVICT_INODE);
693 err = -EIO;
694 }
7fa750a1 695
13ec7297
CY
696 if (!err) {
697 f2fs_lock_op(sbi);
4d57b86d 698 err = f2fs_remove_inode_page(inode);
13ec7297 699 f2fs_unlock_op(sbi);
a11b9f65
CY
700 if (err == -ENOENT)
701 err = 0;
13ec7297 702 }
39936837 703
4c0c2949
JK
704 /* give more chances, if ENOMEM case */
705 if (err == -ENOMEM) {
706 err = 0;
707 goto retry;
708 }
709
af033b2a 710 if (err) {
4d57b86d 711 f2fs_update_inode_page(inode);
af033b2a
CY
712 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
713 }
d6212a5f 714 sb_end_intwrite(inode->i_sb);
19f99cee 715no_delete:
0abd675e
CY
716 dquot_drop(inode);
717
d5e8f6c9 718 stat_dec_inline_xattr(inode);
3289c061 719 stat_dec_inline_dir(inode);
e7a2bf22 720 stat_dec_inline_inode(inode);
0bdee482 721
4354994f
DR
722 if (likely(!is_set_ckpt_flags(sbi, CP_ERROR_FLAG) &&
723 !is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
ca7d802a 724 f2fs_bug_on(sbi, is_inode_flag_set(inode, FI_DIRTY_INODE));
943973cd
JK
725 else
726 f2fs_inode_synced(inode);
ca7d802a 727
4f295443
JK
728 /* ino == 0, if f2fs_new_inode() was failed t*/
729 if (inode->i_ino)
730 invalidate_mapping_pages(NODE_MAPPING(sbi), inode->i_ino,
731 inode->i_ino);
002a41ca
CY
732 if (xnid)
733 invalidate_mapping_pages(NODE_MAPPING(sbi), xnid, xnid);
60dcedc9
CY
734 if (inode->i_nlink) {
735 if (is_inode_flag_set(inode, FI_APPEND_WRITE))
4d57b86d 736 f2fs_add_ino_entry(sbi, inode->i_ino, APPEND_INO);
60dcedc9 737 if (is_inode_flag_set(inode, FI_UPDATE_WRITE))
4d57b86d 738 f2fs_add_ino_entry(sbi, inode->i_ino, UPDATE_INO);
60dcedc9 739 }
91942321 740 if (is_inode_flag_set(inode, FI_FREE_NID)) {
4d57b86d 741 f2fs_alloc_nid_failed(sbi, inode->i_ino);
91942321 742 clear_inode_flag(inode, FI_FREE_NID);
d8c4256c 743 } else {
a4f843bd
JK
744 /*
745 * If xattr nid is corrupted, we can reach out error condition,
4d57b86d
CY
746 * err & !f2fs_exist_written_data(sbi, inode->i_ino, ORPHAN_INO)).
747 * In that case, f2fs_check_nid_range() is enough to give a clue.
a4f843bd 748 */
c9b63bd0 749 }
dbf20cb2 750out_clear:
3d204e24 751 fscrypt_put_encryption_info(inode);
dbf20cb2 752 clear_inode(inode);
19f99cee 753}
44c16156
JK
754
755/* caller should call f2fs_lock_op() */
4d57b86d 756void f2fs_handle_failed_inode(struct inode *inode)
44c16156
JK
757{
758 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
221149c0 759 struct node_info ni;
7735730d 760 int err;
44c16156 761
a11b9f65
CY
762 /*
763 * clear nlink of inode in order to release resource of inode
764 * immediately.
765 */
766 clear_nlink(inode);
767
768 /*
769 * we must call this to avoid inode being remained as dirty, resulting
770 * in a panic when flushing dirty inodes in gdirty_list.
771 */
4d57b86d 772 f2fs_update_inode_page(inode);
9bb02c36 773 f2fs_inode_synced(inode);
a11b9f65 774
221149c0 775 /* don't make bad inode, since it becomes a regular file. */
44c16156
JK
776 unlock_new_inode(inode);
777
13ec7297 778 /*
13ec7297
CY
779 * Note: we should add inode to orphan list before f2fs_unlock_op()
780 * so we can prevent losing this orphan when encoutering checkpoint
781 * and following suddenly power-off.
782 */
7735730d
CY
783 err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
784 if (err) {
785 set_sbi_flag(sbi, SBI_NEED_FSCK);
786 f2fs_msg(sbi->sb, KERN_WARNING,
787 "May loss orphan inode, run fsck to fix.");
788 goto out;
789 }
221149c0
JK
790
791 if (ni.blk_addr != NULL_ADDR) {
7735730d 792 err = f2fs_acquire_orphan_inode(sbi);
221149c0
JK
793 if (err) {
794 set_sbi_flag(sbi, SBI_NEED_FSCK);
795 f2fs_msg(sbi->sb, KERN_WARNING,
796 "Too many orphan inodes, run fsck to fix.");
797 } else {
4d57b86d 798 f2fs_add_orphan_inode(inode);
221149c0 799 }
4d57b86d 800 f2fs_alloc_nid_done(sbi, inode->i_ino);
221149c0 801 } else {
91942321 802 set_inode_flag(inode, FI_FREE_NID);
13ec7297 803 }
44c16156 804
7735730d 805out:
44c16156
JK
806 f2fs_unlock_op(sbi);
807
808 /* iput will drop the inode object */
809 iput(inode);
810}