tools uapi asm: Update asm-generic/unistd.h copy
[linux/fpc-iii.git] / fs / f2fs / inode.c
blob91ceee0ed4c40fcaacb1d57cd6f4cf8edc77a946
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * fs/f2fs/inode.c
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
7 */
8 #include <linux/fs.h>
9 #include <linux/f2fs_fs.h>
10 #include <linux/buffer_head.h>
11 #include <linux/backing-dev.h>
12 #include <linux/writeback.h>
14 #include "f2fs.h"
15 #include "node.h"
16 #include "segment.h"
18 #include <trace/events/f2fs.h>
20 void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync)
22 if (is_inode_flag_set(inode, FI_NEW_INODE))
23 return;
25 if (f2fs_inode_dirtied(inode, sync))
26 return;
28 mark_inode_dirty_sync(inode);
31 void f2fs_set_inode_flags(struct inode *inode)
33 unsigned int flags = F2FS_I(inode)->i_flags;
34 unsigned int new_fl = 0;
36 if (flags & F2FS_SYNC_FL)
37 new_fl |= S_SYNC;
38 if (flags & F2FS_APPEND_FL)
39 new_fl |= S_APPEND;
40 if (flags & F2FS_IMMUTABLE_FL)
41 new_fl |= S_IMMUTABLE;
42 if (flags & F2FS_NOATIME_FL)
43 new_fl |= S_NOATIME;
44 if (flags & F2FS_DIRSYNC_FL)
45 new_fl |= S_DIRSYNC;
46 if (f2fs_encrypted_inode(inode))
47 new_fl |= S_ENCRYPTED;
48 inode_set_flags(inode, new_fl,
49 S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|
50 S_ENCRYPTED);
53 static void __get_inode_rdev(struct inode *inode, struct f2fs_inode *ri)
55 int extra_size = get_extra_isize(inode);
57 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
58 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
59 if (ri->i_addr[extra_size])
60 inode->i_rdev = old_decode_dev(
61 le32_to_cpu(ri->i_addr[extra_size]));
62 else
63 inode->i_rdev = new_decode_dev(
64 le32_to_cpu(ri->i_addr[extra_size + 1]));
68 static int __written_first_block(struct f2fs_sb_info *sbi,
69 struct f2fs_inode *ri)
71 block_t addr = le32_to_cpu(ri->i_addr[offset_in_addr(ri)]);
73 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;
80 static void __set_inode_rdev(struct inode *inode, struct f2fs_inode *ri)
82 int extra_size = get_extra_isize(inode);
84 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
85 if (old_valid_dev(inode->i_rdev)) {
86 ri->i_addr[extra_size] =
87 cpu_to_le32(old_encode_dev(inode->i_rdev));
88 ri->i_addr[extra_size + 1] = 0;
89 } else {
90 ri->i_addr[extra_size] = 0;
91 ri->i_addr[extra_size + 1] =
92 cpu_to_le32(new_encode_dev(inode->i_rdev));
93 ri->i_addr[extra_size + 2] = 0;
98 static void __recover_inline_status(struct inode *inode, struct page *ipage)
100 void *inline_data = inline_data_addr(inode, ipage);
101 __le32 *start = inline_data;
102 __le32 *end = start + MAX_INLINE_DATA(inode) / sizeof(__le32);
104 while (start < end) {
105 if (*start++) {
106 f2fs_wait_on_page_writeback(ipage, NODE, true);
108 set_inode_flag(inode, FI_DATA_EXIST);
109 set_raw_inline(inode, F2FS_INODE(ipage));
110 set_page_dirty(ipage);
111 return;
114 return;
117 static bool f2fs_enable_inode_chksum(struct f2fs_sb_info *sbi, struct page *page)
119 struct f2fs_inode *ri = &F2FS_NODE(page)->i;
121 if (!f2fs_sb_has_inode_chksum(sbi->sb))
122 return false;
124 if (!IS_INODE(page) || !(ri->i_inline & F2FS_EXTRA_ATTR))
125 return false;
127 if (!F2FS_FITS_IN_INODE(ri, le16_to_cpu(ri->i_extra_isize),
128 i_inode_checksum))
129 return false;
131 return true;
134 static __u32 f2fs_inode_chksum(struct f2fs_sb_info *sbi, struct page *page)
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);
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));
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;
157 bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page)
159 struct f2fs_inode *ri;
160 __u32 provided, calculated;
162 if (unlikely(is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN)))
163 return true;
165 #ifdef CONFIG_F2FS_CHECK_FS
166 if (!f2fs_enable_inode_chksum(sbi, page))
167 #else
168 if (!f2fs_enable_inode_chksum(sbi, page) ||
169 PageDirty(page) || PageWriteback(page))
170 #endif
171 return true;
173 ri = &F2FS_NODE(page)->i;
174 provided = le32_to_cpu(ri->i_inode_checksum);
175 calculated = f2fs_inode_chksum(sbi, page);
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);
182 return provided == calculated;
185 void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page)
187 struct f2fs_inode *ri = &F2FS_NODE(page)->i;
189 if (!f2fs_enable_inode_chksum(sbi, page))
190 return;
192 ri->i_inode_checksum = cpu_to_le32(f2fs_inode_chksum(sbi, page));
195 static bool sanity_check_inode(struct inode *inode, struct page *node_page)
197 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
198 struct f2fs_inode_info *fi = F2FS_I(inode);
199 unsigned long long iblocks;
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;
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;
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;
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;
240 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;
251 if (F2FS_I(inode)->extent_tree) {
252 struct extent_info *ei = &F2FS_I(inode)->extent_tree->largest;
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;
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;
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;
287 return true;
290 static int do_read_inode(struct inode *inode)
292 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
293 struct f2fs_inode_info *fi = F2FS_I(inode);
294 struct page *node_page;
295 struct f2fs_inode *ri;
296 projid_t i_projid;
297 int err;
299 /* Check if ino is within scope */
300 if (f2fs_check_nid_range(sbi, inode->i_ino))
301 return -EINVAL;
303 node_page = f2fs_get_node_page(sbi, inode->i_ino);
304 if (IS_ERR(node_page))
305 return PTR_ERR(node_page);
307 ri = F2FS_INODE(node_page);
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);
314 inode->i_blocks = SECTOR_FROM_BLOCK(le64_to_cpu(ri->i_blocks) - 1);
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);
323 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))
326 fi->i_gc_failures[GC_FAILURE_PIN] =
327 le16_to_cpu(ri->i_gc_failures);
328 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;
331 fi->i_advise = ri->i_advise;
332 fi->i_pino = le32_to_cpu(ri->i_pino);
333 fi->i_dir_level = ri->i_dir_level;
335 if (f2fs_init_extent_tree(inode, &ri->i_ext))
336 set_page_dirty(node_page);
338 get_inline_info(inode, ri);
340 fi->i_extra_isize = f2fs_has_extra_attr(inode) ?
341 le16_to_cpu(ri->i_extra_isize) : 0;
343 if (f2fs_sb_has_flexible_inline_xattr(sbi->sb)) {
344 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 {
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.
356 fi->i_inline_xattr_size = 0;
359 if (!sanity_check_inode(inode, node_page)) {
360 f2fs_put_page(node_page, 1);
361 return -EINVAL;
364 /* check data exist */
365 if (f2fs_has_inline_data(inode) && !f2fs_exist_data(inode))
366 __recover_inline_status(inode, node_page);
368 /* try to recover cold bit for non-dir inode */
369 if (!S_ISDIR(inode->i_mode) && !is_cold_node(node_page)) {
370 set_cold_node(node_page, false);
371 set_page_dirty(node_page);
374 /* get rdev by using inline_info */
375 __get_inode_rdev(inode, ri);
377 if (S_ISREG(inode->i_mode)) {
378 err = __written_first_block(sbi, ri);
379 if (err < 0) {
380 f2fs_put_page(node_page, 1);
381 return err;
383 if (!err)
384 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
387 if (!f2fs_need_inode_block_update(sbi, inode->i_ino))
388 fi->last_disk_size = inode->i_size;
390 if (fi->i_flags & F2FS_PROJINHERIT_FL)
391 set_inode_flag(inode, FI_PROJ_INHERIT);
393 if (f2fs_has_extra_attr(inode) && f2fs_sb_has_project_quota(sbi->sb) &&
394 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_projid))
395 i_projid = (projid_t)le32_to_cpu(ri->i_projid);
396 else
397 i_projid = F2FS_DEF_PROJID;
398 fi->i_projid = make_kprojid(&init_user_ns, i_projid);
400 if (f2fs_has_extra_attr(inode) && f2fs_sb_has_inode_crtime(sbi->sb) &&
401 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
402 fi->i_crtime.tv_sec = le64_to_cpu(ri->i_crtime);
403 fi->i_crtime.tv_nsec = le32_to_cpu(ri->i_crtime_nsec);
406 F2FS_I(inode)->i_disk_time[0] = inode->i_atime;
407 F2FS_I(inode)->i_disk_time[1] = inode->i_ctime;
408 F2FS_I(inode)->i_disk_time[2] = inode->i_mtime;
409 F2FS_I(inode)->i_disk_time[3] = F2FS_I(inode)->i_crtime;
410 f2fs_put_page(node_page, 1);
412 stat_inc_inline_xattr(inode);
413 stat_inc_inline_inode(inode);
414 stat_inc_inline_dir(inode);
416 return 0;
419 struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
421 struct f2fs_sb_info *sbi = F2FS_SB(sb);
422 struct inode *inode;
423 int ret = 0;
425 inode = iget_locked(sb, ino);
426 if (!inode)
427 return ERR_PTR(-ENOMEM);
429 if (!(inode->i_state & I_NEW)) {
430 trace_f2fs_iget(inode);
431 return inode;
433 if (ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi))
434 goto make_now;
436 ret = do_read_inode(inode);
437 if (ret)
438 goto bad_inode;
439 make_now:
440 if (ino == F2FS_NODE_INO(sbi)) {
441 inode->i_mapping->a_ops = &f2fs_node_aops;
442 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
443 } else if (ino == F2FS_META_INO(sbi)) {
444 inode->i_mapping->a_ops = &f2fs_meta_aops;
445 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
446 } else if (S_ISREG(inode->i_mode)) {
447 inode->i_op = &f2fs_file_inode_operations;
448 inode->i_fop = &f2fs_file_operations;
449 inode->i_mapping->a_ops = &f2fs_dblock_aops;
450 } else if (S_ISDIR(inode->i_mode)) {
451 inode->i_op = &f2fs_dir_inode_operations;
452 inode->i_fop = &f2fs_dir_operations;
453 inode->i_mapping->a_ops = &f2fs_dblock_aops;
454 inode_nohighmem(inode);
455 } else if (S_ISLNK(inode->i_mode)) {
456 if (f2fs_encrypted_inode(inode))
457 inode->i_op = &f2fs_encrypted_symlink_inode_operations;
458 else
459 inode->i_op = &f2fs_symlink_inode_operations;
460 inode_nohighmem(inode);
461 inode->i_mapping->a_ops = &f2fs_dblock_aops;
462 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
463 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
464 inode->i_op = &f2fs_special_inode_operations;
465 init_special_inode(inode, inode->i_mode, inode->i_rdev);
466 } else {
467 ret = -EIO;
468 goto bad_inode;
470 f2fs_set_inode_flags(inode);
471 unlock_new_inode(inode);
472 trace_f2fs_iget(inode);
473 return inode;
475 bad_inode:
476 iget_failed(inode);
477 trace_f2fs_iget_exit(inode, ret);
478 return ERR_PTR(ret);
481 struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino)
483 struct inode *inode;
484 retry:
485 inode = f2fs_iget(sb, ino);
486 if (IS_ERR(inode)) {
487 if (PTR_ERR(inode) == -ENOMEM) {
488 congestion_wait(BLK_RW_ASYNC, HZ/50);
489 goto retry;
492 return inode;
495 void f2fs_update_inode(struct inode *inode, struct page *node_page)
497 struct f2fs_inode *ri;
498 struct extent_tree *et = F2FS_I(inode)->extent_tree;
500 f2fs_wait_on_page_writeback(node_page, NODE, true);
501 set_page_dirty(node_page);
503 f2fs_inode_synced(inode);
505 ri = F2FS_INODE(node_page);
507 ri->i_mode = cpu_to_le16(inode->i_mode);
508 ri->i_advise = F2FS_I(inode)->i_advise;
509 ri->i_uid = cpu_to_le32(i_uid_read(inode));
510 ri->i_gid = cpu_to_le32(i_gid_read(inode));
511 ri->i_links = cpu_to_le32(inode->i_nlink);
512 ri->i_size = cpu_to_le64(i_size_read(inode));
513 ri->i_blocks = cpu_to_le64(SECTOR_TO_BLOCK(inode->i_blocks) + 1);
515 if (et) {
516 read_lock(&et->lock);
517 set_raw_extent(&et->largest, &ri->i_ext);
518 read_unlock(&et->lock);
519 } else {
520 memset(&ri->i_ext, 0, sizeof(ri->i_ext));
522 set_raw_inline(inode, ri);
524 ri->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
525 ri->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
526 ri->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
527 ri->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
528 ri->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
529 ri->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
530 if (S_ISDIR(inode->i_mode))
531 ri->i_current_depth =
532 cpu_to_le32(F2FS_I(inode)->i_current_depth);
533 else if (S_ISREG(inode->i_mode))
534 ri->i_gc_failures =
535 cpu_to_le16(F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN]);
536 ri->i_xattr_nid = cpu_to_le32(F2FS_I(inode)->i_xattr_nid);
537 ri->i_flags = cpu_to_le32(F2FS_I(inode)->i_flags);
538 ri->i_pino = cpu_to_le32(F2FS_I(inode)->i_pino);
539 ri->i_generation = cpu_to_le32(inode->i_generation);
540 ri->i_dir_level = F2FS_I(inode)->i_dir_level;
542 if (f2fs_has_extra_attr(inode)) {
543 ri->i_extra_isize = cpu_to_le16(F2FS_I(inode)->i_extra_isize);
545 if (f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(inode)->sb))
546 ri->i_inline_xattr_size =
547 cpu_to_le16(F2FS_I(inode)->i_inline_xattr_size);
549 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)->sb) &&
550 F2FS_FITS_IN_INODE(ri, F2FS_I(inode)->i_extra_isize,
551 i_projid)) {
552 projid_t i_projid;
554 i_projid = from_kprojid(&init_user_ns,
555 F2FS_I(inode)->i_projid);
556 ri->i_projid = cpu_to_le32(i_projid);
559 if (f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)->sb) &&
560 F2FS_FITS_IN_INODE(ri, F2FS_I(inode)->i_extra_isize,
561 i_crtime)) {
562 ri->i_crtime =
563 cpu_to_le64(F2FS_I(inode)->i_crtime.tv_sec);
564 ri->i_crtime_nsec =
565 cpu_to_le32(F2FS_I(inode)->i_crtime.tv_nsec);
569 __set_inode_rdev(inode, ri);
571 /* deleted inode */
572 if (inode->i_nlink == 0)
573 clear_inline_node(node_page);
575 F2FS_I(inode)->i_disk_time[0] = inode->i_atime;
576 F2FS_I(inode)->i_disk_time[1] = inode->i_ctime;
577 F2FS_I(inode)->i_disk_time[2] = inode->i_mtime;
578 F2FS_I(inode)->i_disk_time[3] = F2FS_I(inode)->i_crtime;
580 #ifdef CONFIG_F2FS_CHECK_FS
581 f2fs_inode_chksum_set(F2FS_I_SB(inode), node_page);
582 #endif
585 void f2fs_update_inode_page(struct inode *inode)
587 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
588 struct page *node_page;
589 retry:
590 node_page = f2fs_get_node_page(sbi, inode->i_ino);
591 if (IS_ERR(node_page)) {
592 int err = PTR_ERR(node_page);
593 if (err == -ENOMEM) {
594 cond_resched();
595 goto retry;
596 } else if (err != -ENOENT) {
597 f2fs_stop_checkpoint(sbi, false);
599 return;
601 f2fs_update_inode(inode, node_page);
602 f2fs_put_page(node_page, 1);
605 int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc)
607 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
609 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
610 inode->i_ino == F2FS_META_INO(sbi))
611 return 0;
613 if (!is_inode_flag_set(inode, FI_DIRTY_INODE))
614 return 0;
616 if (f2fs_is_checkpoint_ready(sbi))
617 return -ENOSPC;
620 * We need to balance fs here to prevent from producing dirty node pages
621 * during the urgent cleaning time when runing out of free sections.
623 f2fs_update_inode_page(inode);
624 if (wbc && wbc->nr_to_write)
625 f2fs_balance_fs(sbi, true);
626 return 0;
630 * Called at the last iput() if i_nlink is zero
632 void f2fs_evict_inode(struct inode *inode)
634 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
635 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
636 int err = 0;
638 /* some remained atomic pages should discarded */
639 if (f2fs_is_atomic_file(inode))
640 f2fs_drop_inmem_pages(inode);
642 trace_f2fs_evict_inode(inode);
643 truncate_inode_pages_final(&inode->i_data);
645 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
646 inode->i_ino == F2FS_META_INO(sbi))
647 goto out_clear;
649 f2fs_bug_on(sbi, get_dirty_pages(inode));
650 f2fs_remove_dirty_inode(inode);
652 f2fs_destroy_extent_tree(inode);
654 if (inode->i_nlink || is_bad_inode(inode))
655 goto no_delete;
657 err = dquot_initialize(inode);
658 if (err) {
659 err = 0;
660 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
663 f2fs_remove_ino_entry(sbi, inode->i_ino, APPEND_INO);
664 f2fs_remove_ino_entry(sbi, inode->i_ino, UPDATE_INO);
665 f2fs_remove_ino_entry(sbi, inode->i_ino, FLUSH_INO);
667 sb_start_intwrite(inode->i_sb);
668 set_inode_flag(inode, FI_NO_ALLOC);
669 i_size_write(inode, 0);
670 retry:
671 if (F2FS_HAS_BLOCKS(inode))
672 err = f2fs_truncate(inode);
674 if (time_to_inject(sbi, FAULT_EVICT_INODE)) {
675 f2fs_show_injection_info(FAULT_EVICT_INODE);
676 err = -EIO;
679 if (!err) {
680 f2fs_lock_op(sbi);
681 err = f2fs_remove_inode_page(inode);
682 f2fs_unlock_op(sbi);
683 if (err == -ENOENT)
684 err = 0;
687 /* give more chances, if ENOMEM case */
688 if (err == -ENOMEM) {
689 err = 0;
690 goto retry;
693 if (err) {
694 f2fs_update_inode_page(inode);
695 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
697 sb_end_intwrite(inode->i_sb);
698 no_delete:
699 dquot_drop(inode);
701 stat_dec_inline_xattr(inode);
702 stat_dec_inline_dir(inode);
703 stat_dec_inline_inode(inode);
705 if (likely(!is_set_ckpt_flags(sbi, CP_ERROR_FLAG) &&
706 !is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
707 f2fs_bug_on(sbi, is_inode_flag_set(inode, FI_DIRTY_INODE));
708 else
709 f2fs_inode_synced(inode);
711 /* ino == 0, if f2fs_new_inode() was failed t*/
712 if (inode->i_ino)
713 invalidate_mapping_pages(NODE_MAPPING(sbi), inode->i_ino,
714 inode->i_ino);
715 if (xnid)
716 invalidate_mapping_pages(NODE_MAPPING(sbi), xnid, xnid);
717 if (inode->i_nlink) {
718 if (is_inode_flag_set(inode, FI_APPEND_WRITE))
719 f2fs_add_ino_entry(sbi, inode->i_ino, APPEND_INO);
720 if (is_inode_flag_set(inode, FI_UPDATE_WRITE))
721 f2fs_add_ino_entry(sbi, inode->i_ino, UPDATE_INO);
723 if (is_inode_flag_set(inode, FI_FREE_NID)) {
724 f2fs_alloc_nid_failed(sbi, inode->i_ino);
725 clear_inode_flag(inode, FI_FREE_NID);
726 } else {
728 * If xattr nid is corrupted, we can reach out error condition,
729 * err & !f2fs_exist_written_data(sbi, inode->i_ino, ORPHAN_INO)).
730 * In that case, f2fs_check_nid_range() is enough to give a clue.
733 out_clear:
734 fscrypt_put_encryption_info(inode);
735 clear_inode(inode);
738 /* caller should call f2fs_lock_op() */
739 void f2fs_handle_failed_inode(struct inode *inode)
741 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
742 struct node_info ni;
743 int err;
746 * clear nlink of inode in order to release resource of inode
747 * immediately.
749 clear_nlink(inode);
752 * we must call this to avoid inode being remained as dirty, resulting
753 * in a panic when flushing dirty inodes in gdirty_list.
755 f2fs_update_inode_page(inode);
756 f2fs_inode_synced(inode);
758 /* don't make bad inode, since it becomes a regular file. */
759 unlock_new_inode(inode);
762 * Note: we should add inode to orphan list before f2fs_unlock_op()
763 * so we can prevent losing this orphan when encoutering checkpoint
764 * and following suddenly power-off.
766 err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
767 if (err) {
768 set_sbi_flag(sbi, SBI_NEED_FSCK);
769 f2fs_msg(sbi->sb, KERN_WARNING,
770 "May loss orphan inode, run fsck to fix.");
771 goto out;
774 if (ni.blk_addr != NULL_ADDR) {
775 err = f2fs_acquire_orphan_inode(sbi);
776 if (err) {
777 set_sbi_flag(sbi, SBI_NEED_FSCK);
778 f2fs_msg(sbi->sb, KERN_WARNING,
779 "Too many orphan inodes, run fsck to fix.");
780 } else {
781 f2fs_add_orphan_inode(inode);
783 f2fs_alloc_nid_done(sbi, inode->i_ino);
784 } else {
785 set_inode_flag(inode, FI_FREE_NID);
788 out:
789 f2fs_unlock_op(sbi);
791 /* iput will drop the inode object */
792 iput(inode);