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