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