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/writeback.h> 12 #include <linux/sched/mm.h> 13 #include <linux/lz4.h> 14 #include <linux/zstd.h> 15 16 #include "f2fs.h" 17 #include "node.h" 18 #include "segment.h" 19 #include "xattr.h" 20 21 #include <trace/events/f2fs.h> 22 23 #ifdef CONFIG_F2FS_FS_COMPRESSION 24 extern const struct address_space_operations f2fs_compress_aops; 25 #endif 26 27 void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync) 28 { 29 if (is_inode_flag_set(inode, FI_NEW_INODE)) 30 return; 31 32 if (f2fs_readonly(F2FS_I_SB(inode)->sb)) 33 return; 34 35 if (f2fs_inode_dirtied(inode, sync)) 36 return; 37 38 mark_inode_dirty_sync(inode); 39 } 40 41 void f2fs_set_inode_flags(struct inode *inode) 42 { 43 unsigned int flags = F2FS_I(inode)->i_flags; 44 unsigned int new_fl = 0; 45 46 if (flags & F2FS_SYNC_FL) 47 new_fl |= S_SYNC; 48 if (flags & F2FS_APPEND_FL) 49 new_fl |= S_APPEND; 50 if (flags & F2FS_IMMUTABLE_FL) 51 new_fl |= S_IMMUTABLE; 52 if (flags & F2FS_NOATIME_FL) 53 new_fl |= S_NOATIME; 54 if (flags & F2FS_DIRSYNC_FL) 55 new_fl |= S_DIRSYNC; 56 if (file_is_encrypt(inode)) 57 new_fl |= S_ENCRYPTED; 58 if (file_is_verity(inode)) 59 new_fl |= S_VERITY; 60 if (flags & F2FS_CASEFOLD_FL) 61 new_fl |= S_CASEFOLD; 62 inode_set_flags(inode, new_fl, 63 S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC| 64 S_ENCRYPTED|S_VERITY|S_CASEFOLD); 65 } 66 67 static void __get_inode_rdev(struct inode *inode, struct page *node_page) 68 { 69 __le32 *addr = get_dnode_addr(inode, node_page); 70 71 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 72 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 73 if (addr[0]) 74 inode->i_rdev = old_decode_dev(le32_to_cpu(addr[0])); 75 else 76 inode->i_rdev = new_decode_dev(le32_to_cpu(addr[1])); 77 } 78 } 79 80 static void __set_inode_rdev(struct inode *inode, struct page *node_page) 81 { 82 __le32 *addr = get_dnode_addr(inode, node_page); 83 84 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 85 if (old_valid_dev(inode->i_rdev)) { 86 addr[0] = cpu_to_le32(old_encode_dev(inode->i_rdev)); 87 addr[1] = 0; 88 } else { 89 addr[0] = 0; 90 addr[1] = cpu_to_le32(new_encode_dev(inode->i_rdev)); 91 addr[2] = 0; 92 } 93 } 94 } 95 96 static void __recover_inline_status(struct inode *inode, struct page *ipage) 97 { 98 void *inline_data = inline_data_addr(inode, ipage); 99 __le32 *start = inline_data; 100 __le32 *end = start + MAX_INLINE_DATA(inode) / sizeof(__le32); 101 102 while (start < end) { 103 if (*start++) { 104 f2fs_wait_on_page_writeback(ipage, NODE, true, true); 105 106 set_inode_flag(inode, FI_DATA_EXIST); 107 set_raw_inline(inode, F2FS_INODE(ipage)); 108 set_page_dirty(ipage); 109 return; 110 } 111 } 112 return; 113 } 114 115 static bool f2fs_enable_inode_chksum(struct f2fs_sb_info *sbi, struct page *page) 116 { 117 struct f2fs_inode *ri = &F2FS_NODE(page)->i; 118 119 if (!f2fs_sb_has_inode_chksum(sbi)) 120 return false; 121 122 if (!IS_INODE(page) || !(ri->i_inline & F2FS_EXTRA_ATTR)) 123 return false; 124 125 if (!F2FS_FITS_IN_INODE(ri, le16_to_cpu(ri->i_extra_isize), 126 i_inode_checksum)) 127 return false; 128 129 return true; 130 } 131 132 static __u32 f2fs_inode_chksum(struct f2fs_sb_info *sbi, struct page *page) 133 { 134 struct f2fs_node *node = F2FS_NODE(page); 135 struct f2fs_inode *ri = &node->i; 136 __le32 ino = node->footer.ino; 137 __le32 gen = ri->i_generation; 138 __u32 chksum, chksum_seed; 139 __u32 dummy_cs = 0; 140 unsigned int offset = offsetof(struct f2fs_inode, i_inode_checksum); 141 unsigned int cs_size = sizeof(dummy_cs); 142 143 chksum = f2fs_chksum(sbi, sbi->s_chksum_seed, (__u8 *)&ino, 144 sizeof(ino)); 145 chksum_seed = f2fs_chksum(sbi, chksum, (__u8 *)&gen, sizeof(gen)); 146 147 chksum = f2fs_chksum(sbi, chksum_seed, (__u8 *)ri, offset); 148 chksum = f2fs_chksum(sbi, chksum, (__u8 *)&dummy_cs, cs_size); 149 offset += cs_size; 150 chksum = f2fs_chksum(sbi, chksum, (__u8 *)ri + offset, 151 F2FS_BLKSIZE - offset); 152 return chksum; 153 } 154 155 bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page) 156 { 157 struct f2fs_inode *ri; 158 __u32 provided, calculated; 159 160 if (unlikely(is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN))) 161 return true; 162 163 #ifdef CONFIG_F2FS_CHECK_FS 164 if (!f2fs_enable_inode_chksum(sbi, page)) 165 #else 166 if (!f2fs_enable_inode_chksum(sbi, page) || 167 PageDirty(page) || 168 folio_test_writeback(page_folio(page))) 169 #endif 170 return true; 171 172 ri = &F2FS_NODE(page)->i; 173 provided = le32_to_cpu(ri->i_inode_checksum); 174 calculated = f2fs_inode_chksum(sbi, page); 175 176 if (provided != calculated) 177 f2fs_warn(sbi, "checksum invalid, nid = %lu, ino_of_node = %x, %x vs. %x", 178 page->index, ino_of_node(page), provided, calculated); 179 180 return provided == calculated; 181 } 182 183 void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page) 184 { 185 struct f2fs_inode *ri = &F2FS_NODE(page)->i; 186 187 if (!f2fs_enable_inode_chksum(sbi, page)) 188 return; 189 190 ri->i_inode_checksum = cpu_to_le32(f2fs_inode_chksum(sbi, page)); 191 } 192 193 static bool sanity_check_compress_inode(struct inode *inode, 194 struct f2fs_inode *ri) 195 { 196 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 197 unsigned char clevel; 198 199 if (ri->i_compress_algorithm >= COMPRESS_MAX) { 200 f2fs_warn(sbi, 201 "%s: inode (ino=%lx) has unsupported compress algorithm: %u, run fsck to fix", 202 __func__, inode->i_ino, ri->i_compress_algorithm); 203 return false; 204 } 205 if (le64_to_cpu(ri->i_compr_blocks) > 206 SECTOR_TO_BLOCK(inode->i_blocks)) { 207 f2fs_warn(sbi, 208 "%s: inode (ino=%lx) has inconsistent i_compr_blocks:%llu, i_blocks:%llu, run fsck to fix", 209 __func__, inode->i_ino, le64_to_cpu(ri->i_compr_blocks), 210 SECTOR_TO_BLOCK(inode->i_blocks)); 211 return false; 212 } 213 if (ri->i_log_cluster_size < MIN_COMPRESS_LOG_SIZE || 214 ri->i_log_cluster_size > MAX_COMPRESS_LOG_SIZE) { 215 f2fs_warn(sbi, 216 "%s: inode (ino=%lx) has unsupported log cluster size: %u, run fsck to fix", 217 __func__, inode->i_ino, ri->i_log_cluster_size); 218 return false; 219 } 220 221 clevel = le16_to_cpu(ri->i_compress_flag) >> 222 COMPRESS_LEVEL_OFFSET; 223 switch (ri->i_compress_algorithm) { 224 case COMPRESS_LZO: 225 #ifdef CONFIG_F2FS_FS_LZO 226 if (clevel) 227 goto err_level; 228 #endif 229 break; 230 case COMPRESS_LZORLE: 231 #ifdef CONFIG_F2FS_FS_LZORLE 232 if (clevel) 233 goto err_level; 234 #endif 235 break; 236 case COMPRESS_LZ4: 237 #ifdef CONFIG_F2FS_FS_LZ4 238 #ifdef CONFIG_F2FS_FS_LZ4HC 239 if (clevel && 240 (clevel < LZ4HC_MIN_CLEVEL || clevel > LZ4HC_MAX_CLEVEL)) 241 goto err_level; 242 #else 243 if (clevel) 244 goto err_level; 245 #endif 246 #endif 247 break; 248 case COMPRESS_ZSTD: 249 #ifdef CONFIG_F2FS_FS_ZSTD 250 if (clevel < zstd_min_clevel() || clevel > zstd_max_clevel()) 251 goto err_level; 252 #endif 253 break; 254 default: 255 goto err_level; 256 } 257 258 return true; 259 err_level: 260 f2fs_warn(sbi, "%s: inode (ino=%lx) has unsupported compress level: %u, run fsck to fix", 261 __func__, inode->i_ino, clevel); 262 return false; 263 } 264 265 static bool sanity_check_inode(struct inode *inode, struct page *node_page) 266 { 267 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 268 struct f2fs_inode_info *fi = F2FS_I(inode); 269 struct f2fs_inode *ri = F2FS_INODE(node_page); 270 unsigned long long iblocks; 271 272 iblocks = le64_to_cpu(F2FS_INODE(node_page)->i_blocks); 273 if (!iblocks) { 274 f2fs_warn(sbi, "%s: corrupted inode i_blocks i_ino=%lx iblocks=%llu, run fsck to fix.", 275 __func__, inode->i_ino, iblocks); 276 return false; 277 } 278 279 if (ino_of_node(node_page) != nid_of_node(node_page)) { 280 f2fs_warn(sbi, "%s: corrupted inode footer i_ino=%lx, ino,nid: [%u, %u] run fsck to fix.", 281 __func__, inode->i_ino, 282 ino_of_node(node_page), nid_of_node(node_page)); 283 return false; 284 } 285 286 if (f2fs_has_extra_attr(inode)) { 287 if (!f2fs_sb_has_extra_attr(sbi)) { 288 f2fs_warn(sbi, "%s: inode (ino=%lx) is with extra_attr, but extra_attr feature is off", 289 __func__, inode->i_ino); 290 return false; 291 } 292 if (fi->i_extra_isize > F2FS_TOTAL_EXTRA_ATTR_SIZE || 293 fi->i_extra_isize < F2FS_MIN_EXTRA_ATTR_SIZE || 294 fi->i_extra_isize % sizeof(__le32)) { 295 f2fs_warn(sbi, "%s: inode (ino=%lx) has corrupted i_extra_isize: %d, max: %zu", 296 __func__, inode->i_ino, fi->i_extra_isize, 297 F2FS_TOTAL_EXTRA_ATTR_SIZE); 298 return false; 299 } 300 if (f2fs_sb_has_flexible_inline_xattr(sbi) && 301 f2fs_has_inline_xattr(inode) && 302 (!fi->i_inline_xattr_size || 303 fi->i_inline_xattr_size > MAX_INLINE_XATTR_SIZE)) { 304 f2fs_warn(sbi, "%s: inode (ino=%lx) has corrupted i_inline_xattr_size: %d, max: %lu", 305 __func__, inode->i_ino, fi->i_inline_xattr_size, 306 MAX_INLINE_XATTR_SIZE); 307 return false; 308 } 309 if (f2fs_sb_has_compression(sbi) && 310 fi->i_flags & F2FS_COMPR_FL && 311 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, 312 i_compress_flag)) { 313 if (!sanity_check_compress_inode(inode, ri)) 314 return false; 315 } 316 } 317 318 if (!f2fs_sb_has_extra_attr(sbi)) { 319 if (f2fs_sb_has_project_quota(sbi)) { 320 f2fs_warn(sbi, "%s: corrupted inode ino=%lx, wrong feature flag: %u, run fsck to fix.", 321 __func__, inode->i_ino, F2FS_FEATURE_PRJQUOTA); 322 return false; 323 } 324 if (f2fs_sb_has_inode_chksum(sbi)) { 325 f2fs_warn(sbi, "%s: corrupted inode ino=%lx, wrong feature flag: %u, run fsck to fix.", 326 __func__, inode->i_ino, F2FS_FEATURE_INODE_CHKSUM); 327 return false; 328 } 329 if (f2fs_sb_has_flexible_inline_xattr(sbi)) { 330 f2fs_warn(sbi, "%s: corrupted inode ino=%lx, wrong feature flag: %u, run fsck to fix.", 331 __func__, inode->i_ino, F2FS_FEATURE_FLEXIBLE_INLINE_XATTR); 332 return false; 333 } 334 if (f2fs_sb_has_inode_crtime(sbi)) { 335 f2fs_warn(sbi, "%s: corrupted inode ino=%lx, wrong feature flag: %u, run fsck to fix.", 336 __func__, inode->i_ino, F2FS_FEATURE_INODE_CRTIME); 337 return false; 338 } 339 if (f2fs_sb_has_compression(sbi)) { 340 f2fs_warn(sbi, "%s: corrupted inode ino=%lx, wrong feature flag: %u, run fsck to fix.", 341 __func__, inode->i_ino, F2FS_FEATURE_COMPRESSION); 342 return false; 343 } 344 } 345 346 if (f2fs_sanity_check_inline_data(inode, node_page)) { 347 f2fs_warn(sbi, "%s: inode (ino=%lx, mode=%u) should not have inline_data, run fsck to fix", 348 __func__, inode->i_ino, inode->i_mode); 349 return false; 350 } 351 352 if (f2fs_has_inline_dentry(inode) && !S_ISDIR(inode->i_mode)) { 353 f2fs_warn(sbi, "%s: inode (ino=%lx, mode=%u) should not have inline_dentry, run fsck to fix", 354 __func__, inode->i_ino, inode->i_mode); 355 return false; 356 } 357 358 if ((fi->i_flags & F2FS_CASEFOLD_FL) && !f2fs_sb_has_casefold(sbi)) { 359 f2fs_warn(sbi, "%s: inode (ino=%lx) has casefold flag, but casefold feature is off", 360 __func__, inode->i_ino); 361 return false; 362 } 363 364 if (fi->i_xattr_nid && f2fs_check_nid_range(sbi, fi->i_xattr_nid)) { 365 f2fs_warn(sbi, "%s: inode (ino=%lx) has corrupted i_xattr_nid: %u, run fsck to fix.", 366 __func__, inode->i_ino, fi->i_xattr_nid); 367 return false; 368 } 369 370 return true; 371 } 372 373 static void init_idisk_time(struct inode *inode) 374 { 375 struct f2fs_inode_info *fi = F2FS_I(inode); 376 377 fi->i_disk_time[0] = inode_get_atime(inode); 378 fi->i_disk_time[1] = inode_get_ctime(inode); 379 fi->i_disk_time[2] = inode_get_mtime(inode); 380 } 381 382 static int do_read_inode(struct inode *inode) 383 { 384 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 385 struct f2fs_inode_info *fi = F2FS_I(inode); 386 struct page *node_page; 387 struct f2fs_inode *ri; 388 projid_t i_projid; 389 390 /* Check if ino is within scope */ 391 if (f2fs_check_nid_range(sbi, inode->i_ino)) 392 return -EINVAL; 393 394 node_page = f2fs_get_node_page(sbi, inode->i_ino); 395 if (IS_ERR(node_page)) 396 return PTR_ERR(node_page); 397 398 ri = F2FS_INODE(node_page); 399 400 inode->i_mode = le16_to_cpu(ri->i_mode); 401 i_uid_write(inode, le32_to_cpu(ri->i_uid)); 402 i_gid_write(inode, le32_to_cpu(ri->i_gid)); 403 set_nlink(inode, le32_to_cpu(ri->i_links)); 404 inode->i_size = le64_to_cpu(ri->i_size); 405 inode->i_blocks = SECTOR_FROM_BLOCK(le64_to_cpu(ri->i_blocks) - 1); 406 407 inode_set_atime(inode, le64_to_cpu(ri->i_atime), 408 le32_to_cpu(ri->i_atime_nsec)); 409 inode_set_ctime(inode, le64_to_cpu(ri->i_ctime), 410 le32_to_cpu(ri->i_ctime_nsec)); 411 inode_set_mtime(inode, le64_to_cpu(ri->i_mtime), 412 le32_to_cpu(ri->i_mtime_nsec)); 413 inode->i_generation = le32_to_cpu(ri->i_generation); 414 if (S_ISDIR(inode->i_mode)) 415 fi->i_current_depth = le32_to_cpu(ri->i_current_depth); 416 else if (S_ISREG(inode->i_mode)) 417 fi->i_gc_failures = le16_to_cpu(ri->i_gc_failures); 418 fi->i_xattr_nid = le32_to_cpu(ri->i_xattr_nid); 419 fi->i_flags = le32_to_cpu(ri->i_flags); 420 if (S_ISREG(inode->i_mode)) 421 fi->i_flags &= ~F2FS_PROJINHERIT_FL; 422 bitmap_zero(fi->flags, FI_MAX); 423 fi->i_advise = ri->i_advise; 424 fi->i_pino = le32_to_cpu(ri->i_pino); 425 fi->i_dir_level = ri->i_dir_level; 426 427 get_inline_info(inode, ri); 428 429 fi->i_extra_isize = f2fs_has_extra_attr(inode) ? 430 le16_to_cpu(ri->i_extra_isize) : 0; 431 432 if (f2fs_sb_has_flexible_inline_xattr(sbi)) { 433 fi->i_inline_xattr_size = le16_to_cpu(ri->i_inline_xattr_size); 434 } else if (f2fs_has_inline_xattr(inode) || 435 f2fs_has_inline_dentry(inode)) { 436 fi->i_inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS; 437 } else { 438 439 /* 440 * Previous inline data or directory always reserved 200 bytes 441 * in inode layout, even if inline_xattr is disabled. In order 442 * to keep inline_dentry's structure for backward compatibility, 443 * we get the space back only from inline_data. 444 */ 445 fi->i_inline_xattr_size = 0; 446 } 447 448 if (!sanity_check_inode(inode, node_page)) { 449 f2fs_put_page(node_page, 1); 450 set_sbi_flag(sbi, SBI_NEED_FSCK); 451 f2fs_handle_error(sbi, ERROR_CORRUPTED_INODE); 452 return -EFSCORRUPTED; 453 } 454 455 /* check data exist */ 456 if (f2fs_has_inline_data(inode) && !f2fs_exist_data(inode)) 457 __recover_inline_status(inode, node_page); 458 459 /* try to recover cold bit for non-dir inode */ 460 if (!S_ISDIR(inode->i_mode) && !is_cold_node(node_page)) { 461 f2fs_wait_on_page_writeback(node_page, NODE, true, true); 462 set_cold_node(node_page, false); 463 set_page_dirty(node_page); 464 } 465 466 /* get rdev by using inline_info */ 467 __get_inode_rdev(inode, node_page); 468 469 if (!f2fs_need_inode_block_update(sbi, inode->i_ino)) 470 fi->last_disk_size = inode->i_size; 471 472 if (fi->i_flags & F2FS_PROJINHERIT_FL) 473 set_inode_flag(inode, FI_PROJ_INHERIT); 474 475 if (f2fs_has_extra_attr(inode) && f2fs_sb_has_project_quota(sbi) && 476 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_projid)) 477 i_projid = (projid_t)le32_to_cpu(ri->i_projid); 478 else 479 i_projid = F2FS_DEF_PROJID; 480 fi->i_projid = make_kprojid(&init_user_ns, i_projid); 481 482 if (f2fs_has_extra_attr(inode) && f2fs_sb_has_inode_crtime(sbi) && 483 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) { 484 fi->i_crtime.tv_sec = le64_to_cpu(ri->i_crtime); 485 fi->i_crtime.tv_nsec = le32_to_cpu(ri->i_crtime_nsec); 486 } 487 488 if (f2fs_has_extra_attr(inode) && f2fs_sb_has_compression(sbi) && 489 (fi->i_flags & F2FS_COMPR_FL)) { 490 if (F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, 491 i_compress_flag)) { 492 unsigned short compress_flag; 493 494 atomic_set(&fi->i_compr_blocks, 495 le64_to_cpu(ri->i_compr_blocks)); 496 fi->i_compress_algorithm = ri->i_compress_algorithm; 497 fi->i_log_cluster_size = ri->i_log_cluster_size; 498 compress_flag = le16_to_cpu(ri->i_compress_flag); 499 fi->i_compress_level = compress_flag >> 500 COMPRESS_LEVEL_OFFSET; 501 fi->i_compress_flag = compress_flag & 502 GENMASK(COMPRESS_LEVEL_OFFSET - 1, 0); 503 fi->i_cluster_size = BIT(fi->i_log_cluster_size); 504 set_inode_flag(inode, FI_COMPRESSED_FILE); 505 } 506 } 507 508 init_idisk_time(inode); 509 510 if (!sanity_check_extent_cache(inode, node_page)) { 511 f2fs_put_page(node_page, 1); 512 f2fs_handle_error(sbi, ERROR_CORRUPTED_INODE); 513 return -EFSCORRUPTED; 514 } 515 516 /* Need all the flag bits */ 517 f2fs_init_read_extent_tree(inode, node_page); 518 f2fs_init_age_extent_tree(inode); 519 520 f2fs_put_page(node_page, 1); 521 522 stat_inc_inline_xattr(inode); 523 stat_inc_inline_inode(inode); 524 stat_inc_inline_dir(inode); 525 stat_inc_compr_inode(inode); 526 stat_add_compr_blocks(inode, atomic_read(&fi->i_compr_blocks)); 527 528 return 0; 529 } 530 531 static bool is_meta_ino(struct f2fs_sb_info *sbi, unsigned int ino) 532 { 533 return ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi) || 534 ino == F2FS_COMPRESS_INO(sbi); 535 } 536 537 struct inode *f2fs_iget(struct super_block *sb, unsigned long ino) 538 { 539 struct f2fs_sb_info *sbi = F2FS_SB(sb); 540 struct inode *inode; 541 int ret = 0; 542 543 inode = iget_locked(sb, ino); 544 if (!inode) 545 return ERR_PTR(-ENOMEM); 546 547 if (!(inode->i_state & I_NEW)) { 548 if (is_meta_ino(sbi, ino)) { 549 f2fs_err(sbi, "inaccessible inode: %lu, run fsck to repair", ino); 550 set_sbi_flag(sbi, SBI_NEED_FSCK); 551 ret = -EFSCORRUPTED; 552 trace_f2fs_iget_exit(inode, ret); 553 iput(inode); 554 f2fs_handle_error(sbi, ERROR_CORRUPTED_INODE); 555 return ERR_PTR(ret); 556 } 557 558 trace_f2fs_iget(inode); 559 return inode; 560 } 561 562 if (is_meta_ino(sbi, ino)) 563 goto make_now; 564 565 ret = do_read_inode(inode); 566 if (ret) 567 goto bad_inode; 568 make_now: 569 if (ino == F2FS_NODE_INO(sbi)) { 570 inode->i_mapping->a_ops = &f2fs_node_aops; 571 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS); 572 } else if (ino == F2FS_META_INO(sbi)) { 573 inode->i_mapping->a_ops = &f2fs_meta_aops; 574 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS); 575 } else if (ino == F2FS_COMPRESS_INO(sbi)) { 576 #ifdef CONFIG_F2FS_FS_COMPRESSION 577 inode->i_mapping->a_ops = &f2fs_compress_aops; 578 /* 579 * generic_error_remove_folio only truncates pages of regular 580 * inode 581 */ 582 inode->i_mode |= S_IFREG; 583 #endif 584 mapping_set_gfp_mask(inode->i_mapping, 585 GFP_NOFS | __GFP_HIGHMEM | __GFP_MOVABLE); 586 } else if (S_ISREG(inode->i_mode)) { 587 inode->i_op = &f2fs_file_inode_operations; 588 inode->i_fop = &f2fs_file_operations; 589 inode->i_mapping->a_ops = &f2fs_dblock_aops; 590 } else if (S_ISDIR(inode->i_mode)) { 591 inode->i_op = &f2fs_dir_inode_operations; 592 inode->i_fop = &f2fs_dir_operations; 593 inode->i_mapping->a_ops = &f2fs_dblock_aops; 594 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS); 595 } else if (S_ISLNK(inode->i_mode)) { 596 if (file_is_encrypt(inode)) 597 inode->i_op = &f2fs_encrypted_symlink_inode_operations; 598 else 599 inode->i_op = &f2fs_symlink_inode_operations; 600 inode_nohighmem(inode); 601 inode->i_mapping->a_ops = &f2fs_dblock_aops; 602 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 603 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 604 inode->i_op = &f2fs_special_inode_operations; 605 init_special_inode(inode, inode->i_mode, inode->i_rdev); 606 } else { 607 ret = -EIO; 608 goto bad_inode; 609 } 610 f2fs_set_inode_flags(inode); 611 612 unlock_new_inode(inode); 613 trace_f2fs_iget(inode); 614 return inode; 615 616 bad_inode: 617 f2fs_inode_synced(inode); 618 iget_failed(inode); 619 trace_f2fs_iget_exit(inode, ret); 620 return ERR_PTR(ret); 621 } 622 623 struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino) 624 { 625 struct inode *inode; 626 retry: 627 inode = f2fs_iget(sb, ino); 628 if (IS_ERR(inode)) { 629 if (PTR_ERR(inode) == -ENOMEM) { 630 memalloc_retry_wait(GFP_NOFS); 631 goto retry; 632 } 633 } 634 return inode; 635 } 636 637 void f2fs_update_inode(struct inode *inode, struct page *node_page) 638 { 639 struct f2fs_inode_info *fi = F2FS_I(inode); 640 struct f2fs_inode *ri; 641 struct extent_tree *et = fi->extent_tree[EX_READ]; 642 643 f2fs_wait_on_page_writeback(node_page, NODE, true, true); 644 set_page_dirty(node_page); 645 646 f2fs_inode_synced(inode); 647 648 ri = F2FS_INODE(node_page); 649 650 ri->i_mode = cpu_to_le16(inode->i_mode); 651 ri->i_advise = fi->i_advise; 652 ri->i_uid = cpu_to_le32(i_uid_read(inode)); 653 ri->i_gid = cpu_to_le32(i_gid_read(inode)); 654 ri->i_links = cpu_to_le32(inode->i_nlink); 655 ri->i_blocks = cpu_to_le64(SECTOR_TO_BLOCK(inode->i_blocks) + 1); 656 657 if (!f2fs_is_atomic_file(inode) || 658 is_inode_flag_set(inode, FI_ATOMIC_COMMITTED)) 659 ri->i_size = cpu_to_le64(i_size_read(inode)); 660 661 if (et) { 662 read_lock(&et->lock); 663 set_raw_read_extent(&et->largest, &ri->i_ext); 664 read_unlock(&et->lock); 665 } else { 666 memset(&ri->i_ext, 0, sizeof(ri->i_ext)); 667 } 668 set_raw_inline(inode, ri); 669 670 ri->i_atime = cpu_to_le64(inode_get_atime_sec(inode)); 671 ri->i_ctime = cpu_to_le64(inode_get_ctime_sec(inode)); 672 ri->i_mtime = cpu_to_le64(inode_get_mtime_sec(inode)); 673 ri->i_atime_nsec = cpu_to_le32(inode_get_atime_nsec(inode)); 674 ri->i_ctime_nsec = cpu_to_le32(inode_get_ctime_nsec(inode)); 675 ri->i_mtime_nsec = cpu_to_le32(inode_get_mtime_nsec(inode)); 676 if (S_ISDIR(inode->i_mode)) 677 ri->i_current_depth = cpu_to_le32(fi->i_current_depth); 678 else if (S_ISREG(inode->i_mode)) 679 ri->i_gc_failures = cpu_to_le16(fi->i_gc_failures); 680 ri->i_xattr_nid = cpu_to_le32(fi->i_xattr_nid); 681 ri->i_flags = cpu_to_le32(fi->i_flags); 682 ri->i_pino = cpu_to_le32(fi->i_pino); 683 ri->i_generation = cpu_to_le32(inode->i_generation); 684 ri->i_dir_level = fi->i_dir_level; 685 686 if (f2fs_has_extra_attr(inode)) { 687 ri->i_extra_isize = cpu_to_le16(fi->i_extra_isize); 688 689 if (f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(inode))) 690 ri->i_inline_xattr_size = 691 cpu_to_le16(fi->i_inline_xattr_size); 692 693 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)) && 694 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_projid)) { 695 projid_t i_projid; 696 697 i_projid = from_kprojid(&init_user_ns, fi->i_projid); 698 ri->i_projid = cpu_to_le32(i_projid); 699 } 700 701 if (f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) && 702 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) { 703 ri->i_crtime = cpu_to_le64(fi->i_crtime.tv_sec); 704 ri->i_crtime_nsec = cpu_to_le32(fi->i_crtime.tv_nsec); 705 } 706 707 if (f2fs_sb_has_compression(F2FS_I_SB(inode)) && 708 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, 709 i_compress_flag)) { 710 unsigned short compress_flag; 711 712 ri->i_compr_blocks = cpu_to_le64( 713 atomic_read(&fi->i_compr_blocks)); 714 ri->i_compress_algorithm = fi->i_compress_algorithm; 715 compress_flag = fi->i_compress_flag | 716 fi->i_compress_level << 717 COMPRESS_LEVEL_OFFSET; 718 ri->i_compress_flag = cpu_to_le16(compress_flag); 719 ri->i_log_cluster_size = fi->i_log_cluster_size; 720 } 721 } 722 723 __set_inode_rdev(inode, node_page); 724 725 /* deleted inode */ 726 if (inode->i_nlink == 0) 727 clear_page_private_inline(node_page); 728 729 init_idisk_time(inode); 730 #ifdef CONFIG_F2FS_CHECK_FS 731 f2fs_inode_chksum_set(F2FS_I_SB(inode), node_page); 732 #endif 733 } 734 735 void f2fs_update_inode_page(struct inode *inode) 736 { 737 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 738 struct page *node_page; 739 int count = 0; 740 retry: 741 node_page = f2fs_get_node_page(sbi, inode->i_ino); 742 if (IS_ERR(node_page)) { 743 int err = PTR_ERR(node_page); 744 745 /* The node block was truncated. */ 746 if (err == -ENOENT) 747 return; 748 749 if (err == -ENOMEM || ++count <= DEFAULT_RETRY_IO_COUNT) 750 goto retry; 751 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_UPDATE_INODE); 752 return; 753 } 754 f2fs_update_inode(inode, node_page); 755 f2fs_put_page(node_page, 1); 756 } 757 758 int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc) 759 { 760 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 761 762 if (inode->i_ino == F2FS_NODE_INO(sbi) || 763 inode->i_ino == F2FS_META_INO(sbi)) 764 return 0; 765 766 /* 767 * atime could be updated without dirtying f2fs inode in lazytime mode 768 */ 769 if (f2fs_is_time_consistent(inode) && 770 !is_inode_flag_set(inode, FI_DIRTY_INODE)) 771 return 0; 772 773 if (!f2fs_is_checkpoint_ready(sbi)) 774 return -ENOSPC; 775 776 /* 777 * We need to balance fs here to prevent from producing dirty node pages 778 * during the urgent cleaning time when running out of free sections. 779 */ 780 f2fs_update_inode_page(inode); 781 if (wbc && wbc->nr_to_write) 782 f2fs_balance_fs(sbi, true); 783 return 0; 784 } 785 786 /* 787 * Called at the last iput() if i_nlink is zero 788 */ 789 void f2fs_evict_inode(struct inode *inode) 790 { 791 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 792 struct f2fs_inode_info *fi = F2FS_I(inode); 793 nid_t xnid = fi->i_xattr_nid; 794 int err = 0; 795 bool freeze_protected = false; 796 797 f2fs_abort_atomic_write(inode, true); 798 799 if (fi->cow_inode && f2fs_is_cow_file(fi->cow_inode)) { 800 clear_inode_flag(fi->cow_inode, FI_COW_FILE); 801 F2FS_I(fi->cow_inode)->atomic_inode = NULL; 802 iput(fi->cow_inode); 803 fi->cow_inode = NULL; 804 } 805 806 trace_f2fs_evict_inode(inode); 807 truncate_inode_pages_final(&inode->i_data); 808 809 if ((inode->i_nlink || is_bad_inode(inode)) && 810 test_opt(sbi, COMPRESS_CACHE) && f2fs_compressed_file(inode)) 811 f2fs_invalidate_compress_pages(sbi, inode->i_ino); 812 813 if (inode->i_ino == F2FS_NODE_INO(sbi) || 814 inode->i_ino == F2FS_META_INO(sbi) || 815 inode->i_ino == F2FS_COMPRESS_INO(sbi)) 816 goto out_clear; 817 818 f2fs_bug_on(sbi, get_dirty_pages(inode)); 819 f2fs_remove_dirty_inode(inode); 820 821 f2fs_destroy_extent_tree(inode); 822 823 if (inode->i_nlink || is_bad_inode(inode)) 824 goto no_delete; 825 826 err = f2fs_dquot_initialize(inode); 827 if (err) { 828 err = 0; 829 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR); 830 } 831 832 f2fs_remove_ino_entry(sbi, inode->i_ino, APPEND_INO); 833 f2fs_remove_ino_entry(sbi, inode->i_ino, UPDATE_INO); 834 f2fs_remove_ino_entry(sbi, inode->i_ino, FLUSH_INO); 835 836 if (!is_sbi_flag_set(sbi, SBI_IS_FREEZING)) { 837 sb_start_intwrite(inode->i_sb); 838 freeze_protected = true; 839 } 840 set_inode_flag(inode, FI_NO_ALLOC); 841 i_size_write(inode, 0); 842 retry: 843 if (F2FS_HAS_BLOCKS(inode)) 844 err = f2fs_truncate(inode); 845 846 if (time_to_inject(sbi, FAULT_EVICT_INODE)) 847 err = -EIO; 848 849 if (!err) { 850 f2fs_lock_op(sbi); 851 err = f2fs_remove_inode_page(inode); 852 f2fs_unlock_op(sbi); 853 if (err == -ENOENT) { 854 err = 0; 855 856 /* 857 * in fuzzed image, another node may has the same 858 * block address as inode's, if it was truncated 859 * previously, truncation of inode node will fail. 860 */ 861 if (is_inode_flag_set(inode, FI_DIRTY_INODE)) { 862 f2fs_warn(F2FS_I_SB(inode), 863 "f2fs_evict_inode: inconsistent node id, ino:%lu", 864 inode->i_ino); 865 f2fs_inode_synced(inode); 866 set_sbi_flag(sbi, SBI_NEED_FSCK); 867 } 868 } 869 } 870 871 /* give more chances, if ENOMEM case */ 872 if (err == -ENOMEM) { 873 err = 0; 874 goto retry; 875 } 876 877 if (err) { 878 f2fs_update_inode_page(inode); 879 if (dquot_initialize_needed(inode)) 880 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR); 881 } 882 if (freeze_protected) 883 sb_end_intwrite(inode->i_sb); 884 no_delete: 885 dquot_drop(inode); 886 887 stat_dec_inline_xattr(inode); 888 stat_dec_inline_dir(inode); 889 stat_dec_inline_inode(inode); 890 stat_dec_compr_inode(inode); 891 stat_sub_compr_blocks(inode, 892 atomic_read(&fi->i_compr_blocks)); 893 894 if (likely(!f2fs_cp_error(sbi) && 895 !is_sbi_flag_set(sbi, SBI_CP_DISABLED))) 896 f2fs_bug_on(sbi, is_inode_flag_set(inode, FI_DIRTY_INODE)); 897 else 898 f2fs_inode_synced(inode); 899 900 /* for the case f2fs_new_inode() was failed, .i_ino is zero, skip it */ 901 if (inode->i_ino) 902 invalidate_mapping_pages(NODE_MAPPING(sbi), inode->i_ino, 903 inode->i_ino); 904 if (xnid) 905 invalidate_mapping_pages(NODE_MAPPING(sbi), xnid, xnid); 906 if (inode->i_nlink) { 907 if (is_inode_flag_set(inode, FI_APPEND_WRITE)) 908 f2fs_add_ino_entry(sbi, inode->i_ino, APPEND_INO); 909 if (is_inode_flag_set(inode, FI_UPDATE_WRITE)) 910 f2fs_add_ino_entry(sbi, inode->i_ino, UPDATE_INO); 911 } 912 if (is_inode_flag_set(inode, FI_FREE_NID)) { 913 f2fs_alloc_nid_failed(sbi, inode->i_ino); 914 clear_inode_flag(inode, FI_FREE_NID); 915 } else { 916 /* 917 * If xattr nid is corrupted, we can reach out error condition, 918 * err & !f2fs_exist_written_data(sbi, inode->i_ino, ORPHAN_INO)). 919 * In that case, f2fs_check_nid_range() is enough to give a clue. 920 */ 921 } 922 out_clear: 923 fscrypt_put_encryption_info(inode); 924 fsverity_cleanup_inode(inode); 925 clear_inode(inode); 926 } 927 928 /* caller should call f2fs_lock_op() */ 929 void f2fs_handle_failed_inode(struct inode *inode) 930 { 931 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 932 struct node_info ni; 933 int err; 934 935 /* 936 * clear nlink of inode in order to release resource of inode 937 * immediately. 938 */ 939 clear_nlink(inode); 940 941 /* 942 * we must call this to avoid inode being remained as dirty, resulting 943 * in a panic when flushing dirty inodes in gdirty_list. 944 */ 945 f2fs_update_inode_page(inode); 946 f2fs_inode_synced(inode); 947 948 /* don't make bad inode, since it becomes a regular file. */ 949 unlock_new_inode(inode); 950 951 /* 952 * Note: we should add inode to orphan list before f2fs_unlock_op() 953 * so we can prevent losing this orphan when encoutering checkpoint 954 * and following suddenly power-off. 955 */ 956 err = f2fs_get_node_info(sbi, inode->i_ino, &ni, false); 957 if (err) { 958 set_sbi_flag(sbi, SBI_NEED_FSCK); 959 set_inode_flag(inode, FI_FREE_NID); 960 f2fs_warn(sbi, "May loss orphan inode, run fsck to fix."); 961 goto out; 962 } 963 964 if (ni.blk_addr != NULL_ADDR) { 965 err = f2fs_acquire_orphan_inode(sbi); 966 if (err) { 967 set_sbi_flag(sbi, SBI_NEED_FSCK); 968 f2fs_warn(sbi, "Too many orphan inodes, run fsck to fix."); 969 } else { 970 f2fs_add_orphan_inode(inode); 971 } 972 f2fs_alloc_nid_done(sbi, inode->i_ino); 973 } else { 974 set_inode_flag(inode, FI_FREE_NID); 975 } 976 977 out: 978 f2fs_unlock_op(sbi); 979 980 /* iput will drop the inode object */ 981 iput(inode); 982 } 983