1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * fs/f2fs/super.c 4 * 5 * Copyright (c) 2012 Samsung Electronics Co., Ltd. 6 * http://www.samsung.com/ 7 */ 8 #include <linux/module.h> 9 #include <linux/init.h> 10 #include <linux/fs.h> 11 #include <linux/fs_context.h> 12 #include <linux/sched/mm.h> 13 #include <linux/statfs.h> 14 #include <linux/kthread.h> 15 #include <linux/parser.h> 16 #include <linux/mount.h> 17 #include <linux/seq_file.h> 18 #include <linux/proc_fs.h> 19 #include <linux/random.h> 20 #include <linux/exportfs.h> 21 #include <linux/blkdev.h> 22 #include <linux/quotaops.h> 23 #include <linux/f2fs_fs.h> 24 #include <linux/sysfs.h> 25 #include <linux/quota.h> 26 #include <linux/unicode.h> 27 #include <linux/part_stat.h> 28 #include <linux/zstd.h> 29 #include <linux/lz4.h> 30 #include <linux/ctype.h> 31 #include <linux/fs_parser.h> 32 #include <linux/fserror.h> 33 34 #include "f2fs.h" 35 #include "node.h" 36 #include "segment.h" 37 #include "xattr.h" 38 #include "gc.h" 39 #include "iostat.h" 40 41 #define CREATE_TRACE_POINTS 42 #include <trace/events/f2fs.h> 43 44 static struct kmem_cache *f2fs_inode_cachep; 45 46 #ifdef CONFIG_F2FS_FAULT_INJECTION 47 48 const char *f2fs_fault_name[FAULT_MAX] = { 49 [FAULT_KMALLOC] = "kmalloc", 50 [FAULT_KVMALLOC] = "kvmalloc", 51 [FAULT_PAGE_ALLOC] = "page alloc", 52 [FAULT_PAGE_GET] = "page get", 53 [FAULT_ALLOC_BIO] = "alloc bio(obsolete)", 54 [FAULT_ALLOC_NID] = "alloc nid", 55 [FAULT_ORPHAN] = "orphan", 56 [FAULT_BLOCK] = "no more block", 57 [FAULT_DIR_DEPTH] = "too big dir depth", 58 [FAULT_EVICT_INODE] = "evict_inode fail", 59 [FAULT_TRUNCATE] = "truncate fail", 60 [FAULT_READ_IO] = "read IO error", 61 [FAULT_CHECKPOINT] = "checkpoint error", 62 [FAULT_DISCARD] = "discard error", 63 [FAULT_WRITE_IO] = "write IO error", 64 [FAULT_SLAB_ALLOC] = "slab alloc", 65 [FAULT_DQUOT_INIT] = "dquot initialize", 66 [FAULT_LOCK_OP] = "lock_op", 67 [FAULT_BLKADDR_VALIDITY] = "invalid blkaddr", 68 [FAULT_BLKADDR_CONSISTENCE] = "inconsistent blkaddr", 69 [FAULT_NO_SEGMENT] = "no free segment", 70 [FAULT_INCONSISTENT_FOOTER] = "inconsistent footer", 71 [FAULT_ATOMIC_TIMEOUT] = "atomic timeout", 72 [FAULT_VMALLOC] = "vmalloc", 73 [FAULT_LOCK_TIMEOUT] = "lock timeout", 74 [FAULT_SKIP_WRITE] = "skip write", 75 }; 76 77 int f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned long rate, 78 unsigned long type, enum fault_option fo) 79 { 80 struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info; 81 82 if (fo & FAULT_ALL) { 83 memset(ffi, 0, sizeof(struct f2fs_fault_info)); 84 return 0; 85 } 86 87 if (fo & FAULT_RATE) { 88 if (rate > INT_MAX) 89 return -EINVAL; 90 atomic_set(&ffi->inject_ops, 0); 91 ffi->inject_rate = (int)rate; 92 f2fs_info(sbi, "build fault injection rate: %lu", rate); 93 } 94 95 if (fo & FAULT_TYPE) { 96 if (type >= BIT(FAULT_MAX)) 97 return -EINVAL; 98 ffi->inject_type = (unsigned int)type; 99 f2fs_info(sbi, "build fault injection type: 0x%lx", type); 100 } 101 102 if (fo & FAULT_TIMEOUT) { 103 if (type >= TIMEOUT_TYPE_MAX) 104 return -EINVAL; 105 ffi->inject_lock_timeout = (unsigned int)type; 106 f2fs_info(sbi, "build fault timeout injection type: 0x%lx", type); 107 } 108 109 return 0; 110 } 111 112 static void inject_timeout(struct f2fs_sb_info *sbi) 113 { 114 struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info; 115 enum f2fs_timeout_type type = ffi->inject_lock_timeout; 116 unsigned long start_time = jiffies; 117 unsigned long timeout = HZ; 118 119 switch (type) { 120 case TIMEOUT_TYPE_RUNNING: 121 while (!time_after(jiffies, start_time + timeout)) { 122 if (fatal_signal_pending(current)) 123 return; 124 ; 125 } 126 break; 127 case TIMEOUT_TYPE_IO_SLEEP: 128 f2fs_schedule_timeout_killable(timeout, true); 129 break; 130 case TIMEOUT_TYPE_NONIO_SLEEP: 131 f2fs_schedule_timeout_killable(timeout, false); 132 break; 133 case TIMEOUT_TYPE_RUNNABLE: 134 while (!time_after(jiffies, start_time + timeout)) { 135 if (fatal_signal_pending(current)) 136 return; 137 schedule(); 138 } 139 break; 140 default: 141 return; 142 } 143 } 144 145 void f2fs_simulate_lock_timeout(struct f2fs_sb_info *sbi) 146 { 147 struct f2fs_lock_context lc; 148 149 f2fs_lock_op(sbi, &lc); 150 inject_timeout(sbi); 151 f2fs_unlock_op(sbi, &lc); 152 } 153 #endif 154 155 /* f2fs-wide shrinker description */ 156 static struct shrinker *f2fs_shrinker_info; 157 158 static int __init f2fs_init_shrinker(void) 159 { 160 f2fs_shrinker_info = shrinker_alloc(0, "f2fs-shrinker"); 161 if (!f2fs_shrinker_info) 162 return -ENOMEM; 163 164 f2fs_shrinker_info->count_objects = f2fs_shrink_count; 165 f2fs_shrinker_info->scan_objects = f2fs_shrink_scan; 166 167 shrinker_register(f2fs_shrinker_info); 168 169 return 0; 170 } 171 172 static void f2fs_exit_shrinker(void) 173 { 174 shrinker_free(f2fs_shrinker_info); 175 } 176 177 enum { 178 Opt_gc_background, 179 Opt_disable_roll_forward, 180 Opt_norecovery, 181 Opt_discard, 182 Opt_noheap, 183 Opt_heap, 184 Opt_user_xattr, 185 Opt_acl, 186 Opt_active_logs, 187 Opt_disable_ext_identify, 188 Opt_inline_xattr, 189 Opt_inline_xattr_size, 190 Opt_inline_data, 191 Opt_inline_dentry, 192 Opt_flush_merge, 193 Opt_barrier, 194 Opt_fastboot, 195 Opt_extent_cache, 196 Opt_data_flush, 197 Opt_reserve_root, 198 Opt_reserve_node, 199 Opt_resgid, 200 Opt_resuid, 201 Opt_mode, 202 Opt_fault_injection, 203 Opt_fault_type, 204 Opt_lazytime, 205 Opt_quota, 206 Opt_usrquota, 207 Opt_grpquota, 208 Opt_prjquota, 209 Opt_usrjquota, 210 Opt_grpjquota, 211 Opt_prjjquota, 212 Opt_alloc, 213 Opt_fsync, 214 Opt_test_dummy_encryption, 215 Opt_inlinecrypt, 216 Opt_checkpoint_disable, 217 Opt_checkpoint_disable_cap, 218 Opt_checkpoint_disable_cap_perc, 219 Opt_checkpoint_enable, 220 Opt_checkpoint_merge, 221 Opt_compress_algorithm, 222 Opt_compress_log_size, 223 Opt_nocompress_extension, 224 Opt_compress_extension, 225 Opt_compress_chksum, 226 Opt_compress_mode, 227 Opt_compress_cache, 228 Opt_atgc, 229 Opt_gc_merge, 230 Opt_discard_unit, 231 Opt_memory_mode, 232 Opt_age_extent_cache, 233 Opt_errors, 234 Opt_nat_bits, 235 Opt_jqfmt, 236 Opt_checkpoint, 237 Opt_lookup_mode, 238 Opt_resizable_tail_secno, 239 Opt_err, 240 }; 241 242 static const struct constant_table f2fs_param_background_gc[] = { 243 {"on", BGGC_MODE_ON}, 244 {"off", BGGC_MODE_OFF}, 245 {"sync", BGGC_MODE_SYNC}, 246 {} 247 }; 248 249 static const struct constant_table f2fs_param_mode[] = { 250 {"adaptive", FS_MODE_ADAPTIVE}, 251 {"lfs", FS_MODE_LFS}, 252 {"fragment:segment", FS_MODE_FRAGMENT_SEG}, 253 {"fragment:block", FS_MODE_FRAGMENT_BLK}, 254 {} 255 }; 256 257 static const struct constant_table f2fs_param_jqfmt[] = { 258 {"vfsold", QFMT_VFS_OLD}, 259 {"vfsv0", QFMT_VFS_V0}, 260 {"vfsv1", QFMT_VFS_V1}, 261 {} 262 }; 263 264 static const struct constant_table f2fs_param_alloc_mode[] = { 265 {"default", ALLOC_MODE_DEFAULT}, 266 {"reuse", ALLOC_MODE_REUSE}, 267 {} 268 }; 269 static const struct constant_table f2fs_param_fsync_mode[] = { 270 {"posix", FSYNC_MODE_POSIX}, 271 {"strict", FSYNC_MODE_STRICT}, 272 {"nobarrier", FSYNC_MODE_NOBARRIER}, 273 {} 274 }; 275 276 static const struct constant_table f2fs_param_compress_mode[] = { 277 {"fs", COMPR_MODE_FS}, 278 {"user", COMPR_MODE_USER}, 279 {} 280 }; 281 282 static const struct constant_table f2fs_param_discard_unit[] = { 283 {"block", DISCARD_UNIT_BLOCK}, 284 {"segment", DISCARD_UNIT_SEGMENT}, 285 {"section", DISCARD_UNIT_SECTION}, 286 {} 287 }; 288 289 static const struct constant_table f2fs_param_memory_mode[] = { 290 {"normal", MEMORY_MODE_NORMAL}, 291 {"low", MEMORY_MODE_LOW}, 292 {} 293 }; 294 295 static const struct constant_table f2fs_param_errors[] = { 296 {"remount-ro", MOUNT_ERRORS_READONLY}, 297 {"continue", MOUNT_ERRORS_CONTINUE}, 298 {"panic", MOUNT_ERRORS_PANIC}, 299 {} 300 }; 301 302 static const struct constant_table f2fs_param_lookup_mode[] = { 303 {"perf", LOOKUP_PERF}, 304 {"compat", LOOKUP_COMPAT}, 305 {"auto", LOOKUP_AUTO}, 306 {} 307 }; 308 309 static const struct fs_parameter_spec f2fs_param_specs[] = { 310 fsparam_enum("background_gc", Opt_gc_background, f2fs_param_background_gc), 311 fsparam_flag("disable_roll_forward", Opt_disable_roll_forward), 312 fsparam_flag("norecovery", Opt_norecovery), 313 fsparam_flag_no("discard", Opt_discard), 314 fsparam_flag("no_heap", Opt_noheap), 315 fsparam_flag("heap", Opt_heap), 316 fsparam_flag_no("user_xattr", Opt_user_xattr), 317 fsparam_flag_no("acl", Opt_acl), 318 fsparam_s32("active_logs", Opt_active_logs), 319 fsparam_flag("disable_ext_identify", Opt_disable_ext_identify), 320 fsparam_flag_no("inline_xattr", Opt_inline_xattr), 321 fsparam_s32("inline_xattr_size", Opt_inline_xattr_size), 322 fsparam_flag_no("inline_data", Opt_inline_data), 323 fsparam_flag_no("inline_dentry", Opt_inline_dentry), 324 fsparam_flag_no("flush_merge", Opt_flush_merge), 325 fsparam_flag_no("barrier", Opt_barrier), 326 fsparam_flag("fastboot", Opt_fastboot), 327 fsparam_flag_no("extent_cache", Opt_extent_cache), 328 fsparam_flag("data_flush", Opt_data_flush), 329 fsparam_u32("reserve_root", Opt_reserve_root), 330 fsparam_u32("reserve_node", Opt_reserve_node), 331 fsparam_gid("resgid", Opt_resgid), 332 fsparam_uid("resuid", Opt_resuid), 333 fsparam_enum("mode", Opt_mode, f2fs_param_mode), 334 fsparam_s32("fault_injection", Opt_fault_injection), 335 fsparam_u32("fault_type", Opt_fault_type), 336 fsparam_flag_no("lazytime", Opt_lazytime), 337 fsparam_flag_no("quota", Opt_quota), 338 fsparam_flag("usrquota", Opt_usrquota), 339 fsparam_flag("grpquota", Opt_grpquota), 340 fsparam_flag("prjquota", Opt_prjquota), 341 fsparam_string_empty("usrjquota", Opt_usrjquota), 342 fsparam_flag("usrjquota", Opt_usrjquota), 343 fsparam_string_empty("grpjquota", Opt_grpjquota), 344 fsparam_flag("grpjquota", Opt_grpjquota), 345 fsparam_string_empty("prjjquota", Opt_prjjquota), 346 fsparam_flag("prjjquota", Opt_prjjquota), 347 fsparam_flag("nat_bits", Opt_nat_bits), 348 fsparam_enum("jqfmt", Opt_jqfmt, f2fs_param_jqfmt), 349 fsparam_enum("alloc_mode", Opt_alloc, f2fs_param_alloc_mode), 350 fsparam_enum("fsync_mode", Opt_fsync, f2fs_param_fsync_mode), 351 fsparam_string("test_dummy_encryption", Opt_test_dummy_encryption), 352 fsparam_flag("test_dummy_encryption", Opt_test_dummy_encryption), 353 fsparam_flag("inlinecrypt", Opt_inlinecrypt), 354 fsparam_string("checkpoint", Opt_checkpoint), 355 fsparam_flag_no("checkpoint_merge", Opt_checkpoint_merge), 356 fsparam_string("compress_algorithm", Opt_compress_algorithm), 357 fsparam_u32("compress_log_size", Opt_compress_log_size), 358 fsparam_string("compress_extension", Opt_compress_extension), 359 fsparam_string("nocompress_extension", Opt_nocompress_extension), 360 fsparam_flag("compress_chksum", Opt_compress_chksum), 361 fsparam_enum("compress_mode", Opt_compress_mode, f2fs_param_compress_mode), 362 fsparam_flag("compress_cache", Opt_compress_cache), 363 fsparam_flag("atgc", Opt_atgc), 364 fsparam_flag_no("gc_merge", Opt_gc_merge), 365 fsparam_enum("discard_unit", Opt_discard_unit, f2fs_param_discard_unit), 366 fsparam_enum("memory", Opt_memory_mode, f2fs_param_memory_mode), 367 fsparam_flag("age_extent_cache", Opt_age_extent_cache), 368 fsparam_enum("errors", Opt_errors, f2fs_param_errors), 369 fsparam_enum("lookup_mode", Opt_lookup_mode, f2fs_param_lookup_mode), 370 fsparam_u32("resizable_tail_secno", Opt_resizable_tail_secno), 371 {} 372 }; 373 374 /* Resort to a match_table for this interestingly formatted option */ 375 static match_table_t f2fs_checkpoint_tokens = { 376 {Opt_checkpoint_disable, "disable"}, 377 {Opt_checkpoint_disable_cap, "disable:%u"}, 378 {Opt_checkpoint_disable_cap_perc, "disable:%u%%"}, 379 {Opt_checkpoint_enable, "enable"}, 380 {Opt_err, NULL}, 381 }; 382 383 #define F2FS_SPEC_background_gc (1 << 0) 384 #define F2FS_SPEC_inline_xattr_size (1 << 1) 385 #define F2FS_SPEC_active_logs (1 << 2) 386 #define F2FS_SPEC_reserve_root (1 << 3) 387 #define F2FS_SPEC_resgid (1 << 4) 388 #define F2FS_SPEC_resuid (1 << 5) 389 #define F2FS_SPEC_mode (1 << 6) 390 #define F2FS_SPEC_fault_injection (1 << 7) 391 #define F2FS_SPEC_fault_type (1 << 8) 392 #define F2FS_SPEC_jqfmt (1 << 9) 393 #define F2FS_SPEC_alloc_mode (1 << 10) 394 #define F2FS_SPEC_fsync_mode (1 << 11) 395 #define F2FS_SPEC_checkpoint_disable_cap (1 << 12) 396 #define F2FS_SPEC_checkpoint_disable_cap_perc (1 << 13) 397 #define F2FS_SPEC_compress_level (1 << 14) 398 #define F2FS_SPEC_compress_algorithm (1 << 15) 399 #define F2FS_SPEC_compress_log_size (1 << 16) 400 #define F2FS_SPEC_compress_extension (1 << 17) 401 #define F2FS_SPEC_nocompress_extension (1 << 18) 402 #define F2FS_SPEC_compress_chksum (1 << 19) 403 #define F2FS_SPEC_compress_mode (1 << 20) 404 #define F2FS_SPEC_discard_unit (1 << 21) 405 #define F2FS_SPEC_memory_mode (1 << 22) 406 #define F2FS_SPEC_errors (1 << 23) 407 #define F2FS_SPEC_lookup_mode (1 << 24) 408 #define F2FS_SPEC_reserve_node (1 << 25) 409 #define F2FS_SPEC_resizable_tail_secno (1 << 26) 410 411 struct f2fs_fs_context { 412 struct f2fs_mount_info info; 413 unsigned long long opt_mask; /* Bits changed */ 414 unsigned int spec_mask; 415 unsigned short qname_mask; 416 }; 417 418 #define F2FS_CTX_INFO(ctx) ((ctx)->info) 419 420 static inline void ctx_set_opt(struct f2fs_fs_context *ctx, 421 enum f2fs_mount_opt flag) 422 { 423 ctx->info.opt |= BIT_ULL(flag); 424 ctx->opt_mask |= BIT_ULL(flag); 425 } 426 427 static inline void ctx_clear_opt(struct f2fs_fs_context *ctx, 428 enum f2fs_mount_opt flag) 429 { 430 ctx->info.opt &= ~BIT_ULL(flag); 431 ctx->opt_mask |= BIT_ULL(flag); 432 } 433 434 static inline bool ctx_test_opt(struct f2fs_fs_context *ctx, 435 enum f2fs_mount_opt flag) 436 { 437 return ctx->info.opt & BIT_ULL(flag); 438 } 439 440 static inline void ctx_clear_opt_mask(struct f2fs_fs_context *ctx, 441 enum f2fs_mount_opt flag) 442 { 443 ctx->opt_mask &= ~BIT_ULL(flag); 444 } 445 446 static inline bool ctx_test_opt_mask(struct f2fs_fs_context *ctx, 447 enum f2fs_mount_opt flag) 448 { 449 return ctx->opt_mask & BIT_ULL(flag); 450 } 451 452 void f2fs_printk(struct f2fs_sb_info *sbi, bool limit_rate, 453 const char *fmt, ...) 454 { 455 struct va_format vaf; 456 va_list args; 457 int level; 458 459 va_start(args, fmt); 460 461 level = printk_get_level(fmt); 462 vaf.fmt = printk_skip_level(fmt); 463 vaf.va = &args; 464 if (limit_rate) 465 if (sbi) 466 printk_ratelimited("%c%cF2FS-fs (%s): %pV\n", 467 KERN_SOH_ASCII, level, sbi->sb->s_id, &vaf); 468 else 469 printk_ratelimited("%c%cF2FS-fs: %pV\n", 470 KERN_SOH_ASCII, level, &vaf); 471 else 472 if (sbi) 473 printk("%c%cF2FS-fs (%s): %pV\n", 474 KERN_SOH_ASCII, level, sbi->sb->s_id, &vaf); 475 else 476 printk("%c%cF2FS-fs: %pV\n", 477 KERN_SOH_ASCII, level, &vaf); 478 479 va_end(args); 480 } 481 482 #if IS_ENABLED(CONFIG_UNICODE) 483 static const struct f2fs_sb_encodings { 484 __u16 magic; 485 char *name; 486 unsigned int version; 487 } f2fs_sb_encoding_map[] = { 488 {F2FS_ENC_UTF8_12_1, "utf8", UNICODE_AGE(12, 1, 0)}, 489 }; 490 491 static const struct f2fs_sb_encodings * 492 f2fs_sb_read_encoding(const struct f2fs_super_block *sb) 493 { 494 __u16 magic = le16_to_cpu(sb->s_encoding); 495 int i; 496 497 for (i = 0; i < ARRAY_SIZE(f2fs_sb_encoding_map); i++) 498 if (magic == f2fs_sb_encoding_map[i].magic) 499 return &f2fs_sb_encoding_map[i]; 500 501 return NULL; 502 } 503 504 struct kmem_cache *f2fs_cf_name_slab; 505 static int __init f2fs_create_casefold_cache(void) 506 { 507 f2fs_cf_name_slab = f2fs_kmem_cache_create("f2fs_casefolded_name", 508 F2FS_NAME_LEN); 509 return f2fs_cf_name_slab ? 0 : -ENOMEM; 510 } 511 512 static void f2fs_destroy_casefold_cache(void) 513 { 514 kmem_cache_destroy(f2fs_cf_name_slab); 515 } 516 #else 517 static int __init f2fs_create_casefold_cache(void) { return 0; } 518 static void f2fs_destroy_casefold_cache(void) { } 519 #endif 520 521 static inline void limit_reserve_root(struct f2fs_sb_info *sbi) 522 { 523 block_t block_limit = min((sbi->user_block_count >> 3), 524 sbi->user_block_count - sbi->reserved_blocks); 525 block_t node_limit = sbi->total_node_count >> 3; 526 527 /* limit is 12.5% */ 528 if (test_opt(sbi, RESERVE_ROOT) && 529 F2FS_OPTION(sbi).root_reserved_blocks > block_limit) { 530 F2FS_OPTION(sbi).root_reserved_blocks = block_limit; 531 f2fs_info(sbi, "Reduce reserved blocks for root = %u", 532 F2FS_OPTION(sbi).root_reserved_blocks); 533 } 534 if (test_opt(sbi, RESERVE_NODE) && 535 F2FS_OPTION(sbi).root_reserved_nodes > node_limit) { 536 F2FS_OPTION(sbi).root_reserved_nodes = node_limit; 537 f2fs_info(sbi, "Reduce reserved nodes for root = %u", 538 F2FS_OPTION(sbi).root_reserved_nodes); 539 } 540 if (!test_opt(sbi, RESERVE_ROOT) && !test_opt(sbi, RESERVE_NODE) && 541 (!uid_eq(F2FS_OPTION(sbi).s_resuid, 542 make_kuid(&init_user_ns, F2FS_DEF_RESUID)) || 543 !gid_eq(F2FS_OPTION(sbi).s_resgid, 544 make_kgid(&init_user_ns, F2FS_DEF_RESGID)))) 545 f2fs_info(sbi, "Ignore s_resuid=%u, s_resgid=%u w/o reserve_root" 546 " and reserve_node", 547 from_kuid_munged(&init_user_ns, 548 F2FS_OPTION(sbi).s_resuid), 549 from_kgid_munged(&init_user_ns, 550 F2FS_OPTION(sbi).s_resgid)); 551 } 552 553 static inline void adjust_unusable_cap_perc(struct f2fs_sb_info *sbi) 554 { 555 if (!F2FS_OPTION(sbi).unusable_cap_perc) 556 return; 557 558 if (F2FS_OPTION(sbi).unusable_cap_perc == 100) 559 F2FS_OPTION(sbi).unusable_cap = sbi->user_block_count; 560 else 561 F2FS_OPTION(sbi).unusable_cap = (sbi->user_block_count / 100) * 562 F2FS_OPTION(sbi).unusable_cap_perc; 563 564 f2fs_info(sbi, "Adjust unusable cap for checkpoint=disable = %u / %u%%", 565 F2FS_OPTION(sbi).unusable_cap, 566 F2FS_OPTION(sbi).unusable_cap_perc); 567 } 568 569 static void init_once(void *foo) 570 { 571 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo; 572 573 inode_init_once(&fi->vfs_inode); 574 #ifdef CONFIG_FS_ENCRYPTION 575 fi->i_crypt_info = NULL; 576 #endif 577 } 578 579 #ifdef CONFIG_QUOTA 580 static const char * const quotatypes[] = INITQFNAMES; 581 #define QTYPE2NAME(t) (quotatypes[t]) 582 /* 583 * Note the name of the specified quota file. 584 */ 585 static int f2fs_note_qf_name(struct fs_context *fc, int qtype, 586 struct fs_parameter *param) 587 { 588 struct f2fs_fs_context *ctx = fc->fs_private; 589 char *qname; 590 591 if (param->size < 1) { 592 f2fs_err(NULL, "Missing quota name"); 593 return -EINVAL; 594 } 595 if (strchr(param->string, '/')) { 596 f2fs_err(NULL, "quotafile must be on filesystem root"); 597 return -EINVAL; 598 } 599 if (ctx->info.s_qf_names[qtype]) { 600 if (strcmp(ctx->info.s_qf_names[qtype], param->string) != 0) { 601 f2fs_err(NULL, "Quota file already specified"); 602 return -EINVAL; 603 } 604 return 0; 605 } 606 607 qname = kmemdup_nul(param->string, param->size, GFP_KERNEL); 608 if (!qname) { 609 f2fs_err(NULL, "Not enough memory for storing quotafile name"); 610 return -ENOMEM; 611 } 612 F2FS_CTX_INFO(ctx).s_qf_names[qtype] = qname; 613 ctx->qname_mask |= 1 << qtype; 614 return 0; 615 } 616 617 /* 618 * Clear the name of the specified quota file. 619 */ 620 static int f2fs_unnote_qf_name(struct fs_context *fc, int qtype) 621 { 622 struct f2fs_fs_context *ctx = fc->fs_private; 623 624 kfree(ctx->info.s_qf_names[qtype]); 625 ctx->info.s_qf_names[qtype] = NULL; 626 ctx->qname_mask |= 1 << qtype; 627 return 0; 628 } 629 630 static void f2fs_unnote_qf_name_all(struct fs_context *fc) 631 { 632 int i; 633 634 for (i = 0; i < MAXQUOTAS; i++) 635 f2fs_unnote_qf_name(fc, i); 636 } 637 #endif 638 639 static int f2fs_parse_test_dummy_encryption(const struct fs_parameter *param, 640 struct f2fs_fs_context *ctx) 641 { 642 int err; 643 644 if (!IS_ENABLED(CONFIG_FS_ENCRYPTION)) { 645 f2fs_warn(NULL, "test_dummy_encryption option not supported"); 646 return -EINVAL; 647 } 648 err = fscrypt_parse_test_dummy_encryption(param, 649 &ctx->info.dummy_enc_policy); 650 if (err) { 651 if (err == -EINVAL) 652 f2fs_warn(NULL, "Value of option \"%s\" is unrecognized", 653 param->key); 654 else if (err == -EEXIST) 655 f2fs_warn(NULL, "Conflicting test_dummy_encryption options"); 656 else 657 f2fs_warn(NULL, "Error processing option \"%s\" [%d]", 658 param->key, err); 659 return -EINVAL; 660 } 661 return 0; 662 } 663 664 #ifdef CONFIG_F2FS_FS_COMPRESSION 665 static bool is_compress_extension_exist(struct f2fs_mount_info *info, 666 const char *new_ext, bool is_ext) 667 { 668 unsigned char (*ext)[F2FS_EXTENSION_LEN]; 669 int ext_cnt; 670 int i; 671 672 if (is_ext) { 673 ext = info->extensions; 674 ext_cnt = info->compress_ext_cnt; 675 } else { 676 ext = info->noextensions; 677 ext_cnt = info->nocompress_ext_cnt; 678 } 679 680 for (i = 0; i < ext_cnt; i++) { 681 if (!strcasecmp(new_ext, ext[i])) 682 return true; 683 } 684 685 return false; 686 } 687 688 /* 689 * 1. The same extension name cannot not appear in both compress and non-compress extension 690 * at the same time. 691 * 2. If the compress extension specifies all files, the types specified by the non-compress 692 * extension will be treated as special cases and will not be compressed. 693 * 3. Don't allow the non-compress extension specifies all files. 694 */ 695 static int f2fs_test_compress_extension(unsigned char (*noext)[F2FS_EXTENSION_LEN], 696 int noext_cnt, 697 unsigned char (*ext)[F2FS_EXTENSION_LEN], 698 int ext_cnt) 699 { 700 int index = 0, no_index = 0; 701 702 if (!noext_cnt) 703 return 0; 704 705 for (no_index = 0; no_index < noext_cnt; no_index++) { 706 if (strlen(noext[no_index]) == 0) 707 continue; 708 if (!strcasecmp("*", noext[no_index])) { 709 f2fs_info(NULL, "Don't allow the nocompress extension specifies all files"); 710 return -EINVAL; 711 } 712 for (index = 0; index < ext_cnt; index++) { 713 if (strlen(ext[index]) == 0) 714 continue; 715 if (!strcasecmp(ext[index], noext[no_index])) { 716 f2fs_info(NULL, "Don't allow the same extension %s appear in both compress and nocompress extension", 717 ext[index]); 718 return -EINVAL; 719 } 720 } 721 } 722 return 0; 723 } 724 725 #ifdef CONFIG_F2FS_FS_LZ4 726 static int f2fs_set_lz4hc_level(struct f2fs_fs_context *ctx, const char *str) 727 { 728 #ifdef CONFIG_F2FS_FS_LZ4HC 729 unsigned int level; 730 731 if (strlen(str) == 3) { 732 F2FS_CTX_INFO(ctx).compress_level = 0; 733 ctx->spec_mask |= F2FS_SPEC_compress_level; 734 return 0; 735 } 736 737 str += 3; 738 739 if (str[0] != ':') { 740 f2fs_info(NULL, "wrong format, e.g. <alg_name>:<compr_level>"); 741 return -EINVAL; 742 } 743 if (kstrtouint(str + 1, 10, &level)) 744 return -EINVAL; 745 746 if (!f2fs_is_compress_level_valid(COMPRESS_LZ4, level)) { 747 f2fs_info(NULL, "invalid lz4hc compress level: %d", level); 748 return -EINVAL; 749 } 750 751 F2FS_CTX_INFO(ctx).compress_level = level; 752 ctx->spec_mask |= F2FS_SPEC_compress_level; 753 return 0; 754 #else 755 if (strlen(str) == 3) { 756 F2FS_CTX_INFO(ctx).compress_level = 0; 757 ctx->spec_mask |= F2FS_SPEC_compress_level; 758 return 0; 759 } 760 f2fs_info(NULL, "kernel doesn't support lz4hc compression"); 761 return -EINVAL; 762 #endif 763 } 764 #endif 765 766 #ifdef CONFIG_F2FS_FS_ZSTD 767 static int f2fs_set_zstd_level(struct f2fs_fs_context *ctx, const char *str) 768 { 769 int level; 770 int len = 4; 771 772 if (strlen(str) == len) { 773 F2FS_CTX_INFO(ctx).compress_level = F2FS_ZSTD_DEFAULT_CLEVEL; 774 ctx->spec_mask |= F2FS_SPEC_compress_level; 775 return 0; 776 } 777 778 str += len; 779 780 if (str[0] != ':') { 781 f2fs_info(NULL, "wrong format, e.g. <alg_name>:<compr_level>"); 782 return -EINVAL; 783 } 784 if (kstrtoint(str + 1, 10, &level)) 785 return -EINVAL; 786 787 /* f2fs does not support negative compress level now */ 788 if (level < 0) { 789 f2fs_info(NULL, "do not support negative compress level: %d", level); 790 return -ERANGE; 791 } 792 793 if (!f2fs_is_compress_level_valid(COMPRESS_ZSTD, level)) { 794 f2fs_info(NULL, "invalid zstd compress level: %d", level); 795 return -EINVAL; 796 } 797 798 F2FS_CTX_INFO(ctx).compress_level = level; 799 ctx->spec_mask |= F2FS_SPEC_compress_level; 800 return 0; 801 } 802 #endif 803 #endif 804 805 static int f2fs_parse_param(struct fs_context *fc, struct fs_parameter *param) 806 { 807 struct f2fs_fs_context *ctx = fc->fs_private; 808 #ifdef CONFIG_F2FS_FS_COMPRESSION 809 unsigned char (*ext)[F2FS_EXTENSION_LEN]; 810 unsigned char (*noext)[F2FS_EXTENSION_LEN]; 811 int ext_cnt, noext_cnt; 812 char *name; 813 #endif 814 substring_t args[MAX_OPT_ARGS]; 815 struct fs_parse_result result; 816 int token, ret, arg; 817 818 token = fs_parse(fc, f2fs_param_specs, param, &result); 819 if (token < 0) 820 return token; 821 822 switch (token) { 823 case Opt_gc_background: 824 F2FS_CTX_INFO(ctx).bggc_mode = result.uint_32; 825 ctx->spec_mask |= F2FS_SPEC_background_gc; 826 break; 827 case Opt_disable_roll_forward: 828 ctx_set_opt(ctx, F2FS_MOUNT_DISABLE_ROLL_FORWARD); 829 break; 830 case Opt_norecovery: 831 /* requires ro mount, checked in f2fs_validate_options */ 832 ctx_set_opt(ctx, F2FS_MOUNT_NORECOVERY); 833 break; 834 case Opt_discard: 835 if (result.negated) 836 ctx_clear_opt(ctx, F2FS_MOUNT_DISCARD); 837 else 838 ctx_set_opt(ctx, F2FS_MOUNT_DISCARD); 839 break; 840 case Opt_noheap: 841 case Opt_heap: 842 f2fs_warn(NULL, "heap/no_heap options were deprecated"); 843 break; 844 #ifdef CONFIG_F2FS_FS_XATTR 845 case Opt_user_xattr: 846 if (result.negated) 847 ctx_clear_opt(ctx, F2FS_MOUNT_XATTR_USER); 848 else 849 ctx_set_opt(ctx, F2FS_MOUNT_XATTR_USER); 850 break; 851 case Opt_inline_xattr: 852 if (result.negated) 853 ctx_clear_opt(ctx, F2FS_MOUNT_INLINE_XATTR); 854 else 855 ctx_set_opt(ctx, F2FS_MOUNT_INLINE_XATTR); 856 break; 857 case Opt_inline_xattr_size: 858 if (result.int_32 < MIN_INLINE_XATTR_SIZE || 859 result.int_32 > 860 MAX_INLINE_XATTR_SIZE(F2FS_MAX_BLKSIZE)) { 861 f2fs_err(NULL, "inline xattr size is out of range: %u ~ %u", 862 (u32)MIN_INLINE_XATTR_SIZE, 863 (u32)MAX_INLINE_XATTR_SIZE(F2FS_MAX_BLKSIZE)); 864 return -EINVAL; 865 } 866 ctx_set_opt(ctx, F2FS_MOUNT_INLINE_XATTR_SIZE); 867 F2FS_CTX_INFO(ctx).inline_xattr_size = result.int_32; 868 ctx->spec_mask |= F2FS_SPEC_inline_xattr_size; 869 break; 870 #else 871 case Opt_user_xattr: 872 case Opt_inline_xattr: 873 case Opt_inline_xattr_size: 874 f2fs_info(NULL, "%s options not supported", param->key); 875 break; 876 #endif 877 #ifdef CONFIG_F2FS_FS_POSIX_ACL 878 case Opt_acl: 879 if (result.negated) 880 ctx_clear_opt(ctx, F2FS_MOUNT_POSIX_ACL); 881 else 882 ctx_set_opt(ctx, F2FS_MOUNT_POSIX_ACL); 883 break; 884 #else 885 case Opt_acl: 886 f2fs_info(NULL, "%s options not supported", param->key); 887 break; 888 #endif 889 case Opt_active_logs: 890 if (result.int_32 != 2 && result.int_32 != 4 && 891 result.int_32 != NR_CURSEG_PERSIST_TYPE) 892 return -EINVAL; 893 ctx->spec_mask |= F2FS_SPEC_active_logs; 894 F2FS_CTX_INFO(ctx).active_logs = result.int_32; 895 break; 896 case Opt_disable_ext_identify: 897 ctx_set_opt(ctx, F2FS_MOUNT_DISABLE_EXT_IDENTIFY); 898 break; 899 case Opt_inline_data: 900 if (result.negated) 901 ctx_clear_opt(ctx, F2FS_MOUNT_INLINE_DATA); 902 else 903 ctx_set_opt(ctx, F2FS_MOUNT_INLINE_DATA); 904 break; 905 case Opt_inline_dentry: 906 if (result.negated) 907 ctx_clear_opt(ctx, F2FS_MOUNT_INLINE_DENTRY); 908 else 909 ctx_set_opt(ctx, F2FS_MOUNT_INLINE_DENTRY); 910 break; 911 case Opt_flush_merge: 912 if (result.negated) 913 ctx_clear_opt(ctx, F2FS_MOUNT_FLUSH_MERGE); 914 else 915 ctx_set_opt(ctx, F2FS_MOUNT_FLUSH_MERGE); 916 break; 917 case Opt_barrier: 918 if (result.negated) 919 ctx_set_opt(ctx, F2FS_MOUNT_NOBARRIER); 920 else 921 ctx_clear_opt(ctx, F2FS_MOUNT_NOBARRIER); 922 break; 923 case Opt_fastboot: 924 ctx_set_opt(ctx, F2FS_MOUNT_FASTBOOT); 925 break; 926 case Opt_extent_cache: 927 if (result.negated) 928 ctx_clear_opt(ctx, F2FS_MOUNT_READ_EXTENT_CACHE); 929 else 930 ctx_set_opt(ctx, F2FS_MOUNT_READ_EXTENT_CACHE); 931 break; 932 case Opt_data_flush: 933 ctx_set_opt(ctx, F2FS_MOUNT_DATA_FLUSH); 934 break; 935 case Opt_reserve_root: 936 ctx_set_opt(ctx, F2FS_MOUNT_RESERVE_ROOT); 937 F2FS_CTX_INFO(ctx).root_reserved_blocks = result.uint_32; 938 ctx->spec_mask |= F2FS_SPEC_reserve_root; 939 break; 940 case Opt_reserve_node: 941 ctx_set_opt(ctx, F2FS_MOUNT_RESERVE_NODE); 942 F2FS_CTX_INFO(ctx).root_reserved_nodes = result.uint_32; 943 ctx->spec_mask |= F2FS_SPEC_reserve_node; 944 break; 945 case Opt_resuid: 946 F2FS_CTX_INFO(ctx).s_resuid = result.uid; 947 ctx->spec_mask |= F2FS_SPEC_resuid; 948 break; 949 case Opt_resgid: 950 F2FS_CTX_INFO(ctx).s_resgid = result.gid; 951 ctx->spec_mask |= F2FS_SPEC_resgid; 952 break; 953 case Opt_mode: 954 F2FS_CTX_INFO(ctx).fs_mode = result.uint_32; 955 ctx->spec_mask |= F2FS_SPEC_mode; 956 break; 957 #ifdef CONFIG_F2FS_FAULT_INJECTION 958 case Opt_fault_injection: 959 F2FS_CTX_INFO(ctx).fault_info.inject_rate = result.int_32; 960 ctx->spec_mask |= F2FS_SPEC_fault_injection; 961 ctx_set_opt(ctx, F2FS_MOUNT_FAULT_INJECTION); 962 break; 963 964 case Opt_fault_type: 965 if (result.uint_32 > BIT(FAULT_MAX)) 966 return -EINVAL; 967 F2FS_CTX_INFO(ctx).fault_info.inject_type = result.uint_32; 968 ctx->spec_mask |= F2FS_SPEC_fault_type; 969 ctx_set_opt(ctx, F2FS_MOUNT_FAULT_INJECTION); 970 break; 971 #else 972 case Opt_fault_injection: 973 case Opt_fault_type: 974 f2fs_info(NULL, "%s options not supported", param->key); 975 break; 976 #endif 977 case Opt_lazytime: 978 if (result.negated) 979 ctx_clear_opt(ctx, F2FS_MOUNT_LAZYTIME); 980 else 981 ctx_set_opt(ctx, F2FS_MOUNT_LAZYTIME); 982 break; 983 #ifdef CONFIG_QUOTA 984 case Opt_quota: 985 if (result.negated) { 986 ctx_clear_opt(ctx, F2FS_MOUNT_QUOTA); 987 ctx_clear_opt(ctx, F2FS_MOUNT_USRQUOTA); 988 ctx_clear_opt(ctx, F2FS_MOUNT_GRPQUOTA); 989 ctx_clear_opt(ctx, F2FS_MOUNT_PRJQUOTA); 990 } else 991 ctx_set_opt(ctx, F2FS_MOUNT_USRQUOTA); 992 break; 993 case Opt_usrquota: 994 ctx_set_opt(ctx, F2FS_MOUNT_USRQUOTA); 995 break; 996 case Opt_grpquota: 997 ctx_set_opt(ctx, F2FS_MOUNT_GRPQUOTA); 998 break; 999 case Opt_prjquota: 1000 ctx_set_opt(ctx, F2FS_MOUNT_PRJQUOTA); 1001 break; 1002 case Opt_usrjquota: 1003 if (param->type == fs_value_is_string && *param->string) 1004 ret = f2fs_note_qf_name(fc, USRQUOTA, param); 1005 else 1006 ret = f2fs_unnote_qf_name(fc, USRQUOTA); 1007 if (ret) 1008 return ret; 1009 break; 1010 case Opt_grpjquota: 1011 if (param->type == fs_value_is_string && *param->string) 1012 ret = f2fs_note_qf_name(fc, GRPQUOTA, param); 1013 else 1014 ret = f2fs_unnote_qf_name(fc, GRPQUOTA); 1015 if (ret) 1016 return ret; 1017 break; 1018 case Opt_prjjquota: 1019 if (param->type == fs_value_is_string && *param->string) 1020 ret = f2fs_note_qf_name(fc, PRJQUOTA, param); 1021 else 1022 ret = f2fs_unnote_qf_name(fc, PRJQUOTA); 1023 if (ret) 1024 return ret; 1025 break; 1026 case Opt_jqfmt: 1027 F2FS_CTX_INFO(ctx).s_jquota_fmt = result.int_32; 1028 ctx->spec_mask |= F2FS_SPEC_jqfmt; 1029 break; 1030 #else 1031 case Opt_quota: 1032 case Opt_usrquota: 1033 case Opt_grpquota: 1034 case Opt_prjquota: 1035 case Opt_usrjquota: 1036 case Opt_grpjquota: 1037 case Opt_prjjquota: 1038 f2fs_info(NULL, "quota operations not supported"); 1039 break; 1040 #endif 1041 case Opt_alloc: 1042 F2FS_CTX_INFO(ctx).alloc_mode = result.uint_32; 1043 ctx->spec_mask |= F2FS_SPEC_alloc_mode; 1044 break; 1045 case Opt_fsync: 1046 F2FS_CTX_INFO(ctx).fsync_mode = result.uint_32; 1047 ctx->spec_mask |= F2FS_SPEC_fsync_mode; 1048 break; 1049 case Opt_test_dummy_encryption: 1050 ret = f2fs_parse_test_dummy_encryption(param, ctx); 1051 if (ret) 1052 return ret; 1053 break; 1054 case Opt_inlinecrypt: 1055 #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT 1056 ctx_set_opt(ctx, F2FS_MOUNT_INLINECRYPT); 1057 #else 1058 f2fs_info(NULL, "inline encryption not supported"); 1059 #endif 1060 break; 1061 case Opt_checkpoint: 1062 /* 1063 * Initialize args struct so we know whether arg was 1064 * found; some options take optional arguments. 1065 */ 1066 args[0].from = args[0].to = NULL; 1067 arg = 0; 1068 1069 /* revert to match_table for checkpoint= options */ 1070 token = match_token(param->string, f2fs_checkpoint_tokens, args); 1071 switch (token) { 1072 case Opt_checkpoint_disable_cap_perc: 1073 if (args->from && match_int(args, &arg)) 1074 return -EINVAL; 1075 if (arg < 0 || arg > 100) 1076 return -EINVAL; 1077 F2FS_CTX_INFO(ctx).unusable_cap_perc = arg; 1078 ctx->spec_mask |= F2FS_SPEC_checkpoint_disable_cap_perc; 1079 ctx_set_opt(ctx, F2FS_MOUNT_DISABLE_CHECKPOINT); 1080 break; 1081 case Opt_checkpoint_disable_cap: 1082 if (args->from && match_int(args, &arg)) 1083 return -EINVAL; 1084 F2FS_CTX_INFO(ctx).unusable_cap = arg; 1085 ctx->spec_mask |= F2FS_SPEC_checkpoint_disable_cap; 1086 ctx_set_opt(ctx, F2FS_MOUNT_DISABLE_CHECKPOINT); 1087 break; 1088 case Opt_checkpoint_disable: 1089 ctx_set_opt(ctx, F2FS_MOUNT_DISABLE_CHECKPOINT); 1090 break; 1091 case Opt_checkpoint_enable: 1092 F2FS_CTX_INFO(ctx).unusable_cap_perc = 0; 1093 ctx->spec_mask |= F2FS_SPEC_checkpoint_disable_cap_perc; 1094 F2FS_CTX_INFO(ctx).unusable_cap = 0; 1095 ctx->spec_mask |= F2FS_SPEC_checkpoint_disable_cap; 1096 ctx_clear_opt(ctx, F2FS_MOUNT_DISABLE_CHECKPOINT); 1097 break; 1098 default: 1099 return -EINVAL; 1100 } 1101 break; 1102 case Opt_checkpoint_merge: 1103 if (result.negated) 1104 ctx_clear_opt(ctx, F2FS_MOUNT_MERGE_CHECKPOINT); 1105 else 1106 ctx_set_opt(ctx, F2FS_MOUNT_MERGE_CHECKPOINT); 1107 break; 1108 #ifdef CONFIG_F2FS_FS_COMPRESSION 1109 case Opt_compress_algorithm: 1110 name = param->string; 1111 if (!strcmp(name, "lzo")) { 1112 #ifdef CONFIG_F2FS_FS_LZO 1113 F2FS_CTX_INFO(ctx).compress_level = 0; 1114 F2FS_CTX_INFO(ctx).compress_algorithm = COMPRESS_LZO; 1115 ctx->spec_mask |= F2FS_SPEC_compress_level; 1116 ctx->spec_mask |= F2FS_SPEC_compress_algorithm; 1117 #else 1118 f2fs_info(NULL, "kernel doesn't support lzo compression"); 1119 #endif 1120 } else if (!strncmp(name, "lz4", 3)) { 1121 #ifdef CONFIG_F2FS_FS_LZ4 1122 ret = f2fs_set_lz4hc_level(ctx, name); 1123 if (ret) 1124 return -EINVAL; 1125 F2FS_CTX_INFO(ctx).compress_algorithm = COMPRESS_LZ4; 1126 ctx->spec_mask |= F2FS_SPEC_compress_algorithm; 1127 #else 1128 f2fs_info(NULL, "kernel doesn't support lz4 compression"); 1129 #endif 1130 } else if (!strncmp(name, "zstd", 4)) { 1131 #ifdef CONFIG_F2FS_FS_ZSTD 1132 ret = f2fs_set_zstd_level(ctx, name); 1133 if (ret) 1134 return -EINVAL; 1135 F2FS_CTX_INFO(ctx).compress_algorithm = COMPRESS_ZSTD; 1136 ctx->spec_mask |= F2FS_SPEC_compress_algorithm; 1137 #else 1138 f2fs_info(NULL, "kernel doesn't support zstd compression"); 1139 #endif 1140 } else if (!strcmp(name, "lzo-rle")) { 1141 #ifdef CONFIG_F2FS_FS_LZORLE 1142 F2FS_CTX_INFO(ctx).compress_level = 0; 1143 F2FS_CTX_INFO(ctx).compress_algorithm = COMPRESS_LZORLE; 1144 ctx->spec_mask |= F2FS_SPEC_compress_level; 1145 ctx->spec_mask |= F2FS_SPEC_compress_algorithm; 1146 #else 1147 f2fs_info(NULL, "kernel doesn't support lzorle compression"); 1148 #endif 1149 } else 1150 return -EINVAL; 1151 break; 1152 case Opt_compress_log_size: 1153 if (result.uint_32 < MIN_COMPRESS_LOG_SIZE || 1154 result.uint_32 > MAX_COMPRESS_LOG_SIZE) { 1155 f2fs_err(NULL, 1156 "Compress cluster log size is out of range"); 1157 return -EINVAL; 1158 } 1159 F2FS_CTX_INFO(ctx).compress_log_size = result.uint_32; 1160 ctx->spec_mask |= F2FS_SPEC_compress_log_size; 1161 break; 1162 case Opt_compress_extension: 1163 name = param->string; 1164 ext = F2FS_CTX_INFO(ctx).extensions; 1165 ext_cnt = F2FS_CTX_INFO(ctx).compress_ext_cnt; 1166 1167 if (strlen(name) >= F2FS_EXTENSION_LEN || 1168 ext_cnt >= COMPRESS_EXT_NUM) { 1169 f2fs_err(NULL, "invalid extension length/number"); 1170 return -EINVAL; 1171 } 1172 1173 if (is_compress_extension_exist(&ctx->info, name, true)) 1174 break; 1175 1176 ret = strscpy(ext[ext_cnt], name, F2FS_EXTENSION_LEN); 1177 if (ret < 0) 1178 return ret; 1179 F2FS_CTX_INFO(ctx).compress_ext_cnt++; 1180 ctx->spec_mask |= F2FS_SPEC_compress_extension; 1181 break; 1182 case Opt_nocompress_extension: 1183 name = param->string; 1184 noext = F2FS_CTX_INFO(ctx).noextensions; 1185 noext_cnt = F2FS_CTX_INFO(ctx).nocompress_ext_cnt; 1186 1187 if (strlen(name) >= F2FS_EXTENSION_LEN || 1188 noext_cnt >= COMPRESS_EXT_NUM) { 1189 f2fs_err(NULL, "invalid extension length/number"); 1190 return -EINVAL; 1191 } 1192 1193 if (is_compress_extension_exist(&ctx->info, name, false)) 1194 break; 1195 1196 ret = strscpy(noext[noext_cnt], name, F2FS_EXTENSION_LEN); 1197 if (ret < 0) 1198 return ret; 1199 F2FS_CTX_INFO(ctx).nocompress_ext_cnt++; 1200 ctx->spec_mask |= F2FS_SPEC_nocompress_extension; 1201 break; 1202 case Opt_compress_chksum: 1203 F2FS_CTX_INFO(ctx).compress_chksum = true; 1204 ctx->spec_mask |= F2FS_SPEC_compress_chksum; 1205 break; 1206 case Opt_compress_mode: 1207 F2FS_CTX_INFO(ctx).compress_mode = result.uint_32; 1208 ctx->spec_mask |= F2FS_SPEC_compress_mode; 1209 break; 1210 case Opt_compress_cache: 1211 ctx_set_opt(ctx, F2FS_MOUNT_COMPRESS_CACHE); 1212 break; 1213 #else 1214 case Opt_compress_algorithm: 1215 case Opt_compress_log_size: 1216 case Opt_compress_extension: 1217 case Opt_nocompress_extension: 1218 case Opt_compress_chksum: 1219 case Opt_compress_mode: 1220 case Opt_compress_cache: 1221 f2fs_info(NULL, "compression options not supported"); 1222 break; 1223 #endif 1224 case Opt_atgc: 1225 ctx_set_opt(ctx, F2FS_MOUNT_ATGC); 1226 break; 1227 case Opt_gc_merge: 1228 if (result.negated) 1229 ctx_clear_opt(ctx, F2FS_MOUNT_GC_MERGE); 1230 else 1231 ctx_set_opt(ctx, F2FS_MOUNT_GC_MERGE); 1232 break; 1233 case Opt_discard_unit: 1234 F2FS_CTX_INFO(ctx).discard_unit = result.uint_32; 1235 ctx->spec_mask |= F2FS_SPEC_discard_unit; 1236 break; 1237 case Opt_memory_mode: 1238 F2FS_CTX_INFO(ctx).memory_mode = result.uint_32; 1239 ctx->spec_mask |= F2FS_SPEC_memory_mode; 1240 break; 1241 case Opt_age_extent_cache: 1242 ctx_set_opt(ctx, F2FS_MOUNT_AGE_EXTENT_CACHE); 1243 break; 1244 case Opt_errors: 1245 F2FS_CTX_INFO(ctx).errors = result.uint_32; 1246 ctx->spec_mask |= F2FS_SPEC_errors; 1247 break; 1248 case Opt_nat_bits: 1249 ctx_set_opt(ctx, F2FS_MOUNT_NAT_BITS); 1250 break; 1251 case Opt_lookup_mode: 1252 F2FS_CTX_INFO(ctx).lookup_mode = result.uint_32; 1253 ctx->spec_mask |= F2FS_SPEC_lookup_mode; 1254 break; 1255 case Opt_resizable_tail_secno: 1256 F2FS_CTX_INFO(ctx).resizable_tail_secno = result.uint_32; 1257 ctx->spec_mask |= F2FS_SPEC_resizable_tail_secno; 1258 break; 1259 } 1260 return 0; 1261 } 1262 1263 /* 1264 * Check quota settings consistency. 1265 */ 1266 static int f2fs_check_quota_consistency(struct fs_context *fc, 1267 struct super_block *sb) 1268 { 1269 struct f2fs_sb_info *sbi = F2FS_SB(sb); 1270 #ifdef CONFIG_QUOTA 1271 struct f2fs_fs_context *ctx = fc->fs_private; 1272 bool quota_feature = f2fs_sb_has_quota_ino(sbi); 1273 bool quota_turnon = sb_any_quota_loaded(sb); 1274 char *old_qname, *new_qname; 1275 bool usr_qf_name, grp_qf_name, prj_qf_name, usrquota, grpquota, prjquota; 1276 int i; 1277 1278 /* 1279 * We do the test below only for project quotas. 'usrquota' and 1280 * 'grpquota' mount options are allowed even without quota feature 1281 * to support legacy quotas in quota files. 1282 */ 1283 if (ctx_test_opt(ctx, F2FS_MOUNT_PRJQUOTA) && 1284 !f2fs_sb_has_project_quota(sbi)) { 1285 f2fs_err(sbi, "Project quota feature not enabled. Cannot enable project quota enforcement."); 1286 return -EINVAL; 1287 } 1288 1289 if (ctx->qname_mask) { 1290 for (i = 0; i < MAXQUOTAS; i++) { 1291 if (!(ctx->qname_mask & (1 << i))) 1292 continue; 1293 1294 old_qname = F2FS_OPTION(sbi).s_qf_names[i]; 1295 new_qname = F2FS_CTX_INFO(ctx).s_qf_names[i]; 1296 if (quota_turnon && 1297 !!old_qname != !!new_qname) 1298 goto err_jquota_change; 1299 1300 if (old_qname) { 1301 if (!new_qname) { 1302 f2fs_info(sbi, "remove qf_name %s", 1303 old_qname); 1304 continue; 1305 } else if (strcmp(old_qname, new_qname) == 0) { 1306 ctx->qname_mask &= ~(1 << i); 1307 continue; 1308 } 1309 goto err_jquota_specified; 1310 } 1311 1312 if (quota_feature) { 1313 f2fs_info(sbi, "QUOTA feature is enabled, so ignore qf_name"); 1314 ctx->qname_mask &= ~(1 << i); 1315 kfree(F2FS_CTX_INFO(ctx).s_qf_names[i]); 1316 F2FS_CTX_INFO(ctx).s_qf_names[i] = NULL; 1317 } 1318 } 1319 } 1320 1321 /* Make sure we don't mix old and new quota format */ 1322 usr_qf_name = F2FS_OPTION(sbi).s_qf_names[USRQUOTA] || 1323 F2FS_CTX_INFO(ctx).s_qf_names[USRQUOTA]; 1324 grp_qf_name = F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] || 1325 F2FS_CTX_INFO(ctx).s_qf_names[GRPQUOTA]; 1326 prj_qf_name = F2FS_OPTION(sbi).s_qf_names[PRJQUOTA] || 1327 F2FS_CTX_INFO(ctx).s_qf_names[PRJQUOTA]; 1328 usrquota = test_opt(sbi, USRQUOTA) || 1329 ctx_test_opt(ctx, F2FS_MOUNT_USRQUOTA); 1330 grpquota = test_opt(sbi, GRPQUOTA) || 1331 ctx_test_opt(ctx, F2FS_MOUNT_GRPQUOTA); 1332 prjquota = test_opt(sbi, PRJQUOTA) || 1333 ctx_test_opt(ctx, F2FS_MOUNT_PRJQUOTA); 1334 1335 if (usr_qf_name) { 1336 ctx_clear_opt(ctx, F2FS_MOUNT_USRQUOTA); 1337 usrquota = false; 1338 } 1339 if (grp_qf_name) { 1340 ctx_clear_opt(ctx, F2FS_MOUNT_GRPQUOTA); 1341 grpquota = false; 1342 } 1343 if (prj_qf_name) { 1344 ctx_clear_opt(ctx, F2FS_MOUNT_PRJQUOTA); 1345 prjquota = false; 1346 } 1347 if (usr_qf_name || grp_qf_name || prj_qf_name) { 1348 if (grpquota || usrquota || prjquota) { 1349 f2fs_err(sbi, "old and new quota format mixing"); 1350 return -EINVAL; 1351 } 1352 if (!(ctx->spec_mask & F2FS_SPEC_jqfmt || 1353 F2FS_OPTION(sbi).s_jquota_fmt)) { 1354 f2fs_err(sbi, "journaled quota format not specified"); 1355 return -EINVAL; 1356 } 1357 } 1358 return 0; 1359 1360 err_jquota_change: 1361 f2fs_err(sbi, "Cannot change journaled quota options when quota turned on"); 1362 return -EINVAL; 1363 err_jquota_specified: 1364 f2fs_err(sbi, "%s quota file already specified", 1365 QTYPE2NAME(i)); 1366 return -EINVAL; 1367 1368 #else 1369 if (f2fs_readonly(sbi->sb)) 1370 return 0; 1371 if (f2fs_sb_has_quota_ino(sbi)) { 1372 f2fs_info(sbi, "Filesystem with quota feature cannot be mounted RDWR without CONFIG_QUOTA"); 1373 return -EINVAL; 1374 } 1375 if (f2fs_sb_has_project_quota(sbi)) { 1376 f2fs_err(sbi, "Filesystem with project quota feature cannot be mounted RDWR without CONFIG_QUOTA"); 1377 return -EINVAL; 1378 } 1379 1380 return 0; 1381 #endif 1382 } 1383 1384 static int f2fs_check_test_dummy_encryption(struct fs_context *fc, 1385 struct super_block *sb) 1386 { 1387 struct f2fs_fs_context *ctx = fc->fs_private; 1388 struct f2fs_sb_info *sbi = F2FS_SB(sb); 1389 1390 if (!fscrypt_is_dummy_policy_set(&F2FS_CTX_INFO(ctx).dummy_enc_policy)) 1391 return 0; 1392 1393 if (!f2fs_sb_has_encrypt(sbi)) { 1394 f2fs_err(sbi, "Encrypt feature is off"); 1395 return -EINVAL; 1396 } 1397 1398 /* 1399 * This mount option is just for testing, and it's not worthwhile to 1400 * implement the extra complexity (e.g. RCU protection) that would be 1401 * needed to allow it to be set or changed during remount. We do allow 1402 * it to be specified during remount, but only if there is no change. 1403 */ 1404 if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) { 1405 if (fscrypt_dummy_policies_equal(&F2FS_OPTION(sbi).dummy_enc_policy, 1406 &F2FS_CTX_INFO(ctx).dummy_enc_policy)) 1407 return 0; 1408 f2fs_warn(sbi, "Can't set or change test_dummy_encryption on remount"); 1409 return -EINVAL; 1410 } 1411 return 0; 1412 } 1413 1414 static inline bool test_compression_spec(unsigned int mask) 1415 { 1416 return mask & (F2FS_SPEC_compress_algorithm 1417 | F2FS_SPEC_compress_log_size 1418 | F2FS_SPEC_compress_extension 1419 | F2FS_SPEC_nocompress_extension 1420 | F2FS_SPEC_compress_chksum 1421 | F2FS_SPEC_compress_mode); 1422 } 1423 1424 static inline void clear_compression_spec(struct f2fs_fs_context *ctx) 1425 { 1426 ctx->spec_mask &= ~(F2FS_SPEC_compress_algorithm 1427 | F2FS_SPEC_compress_log_size 1428 | F2FS_SPEC_compress_extension 1429 | F2FS_SPEC_nocompress_extension 1430 | F2FS_SPEC_compress_chksum 1431 | F2FS_SPEC_compress_mode); 1432 } 1433 1434 static int f2fs_check_compression(struct fs_context *fc, 1435 struct super_block *sb) 1436 { 1437 #ifdef CONFIG_F2FS_FS_COMPRESSION 1438 struct f2fs_fs_context *ctx = fc->fs_private; 1439 struct f2fs_sb_info *sbi = F2FS_SB(sb); 1440 int i, cnt; 1441 1442 if (!f2fs_sb_has_compression(sbi)) { 1443 if (test_compression_spec(ctx->spec_mask) || 1444 ctx_test_opt(ctx, F2FS_MOUNT_COMPRESS_CACHE)) 1445 f2fs_info(sbi, "Image doesn't support compression"); 1446 clear_compression_spec(ctx); 1447 ctx_clear_opt_mask(ctx, F2FS_MOUNT_COMPRESS_CACHE); 1448 return 0; 1449 } 1450 if (ctx->spec_mask & F2FS_SPEC_compress_extension) { 1451 cnt = F2FS_CTX_INFO(ctx).compress_ext_cnt; 1452 for (i = 0; i < F2FS_CTX_INFO(ctx).compress_ext_cnt; i++) { 1453 if (is_compress_extension_exist(&F2FS_OPTION(sbi), 1454 F2FS_CTX_INFO(ctx).extensions[i], true)) { 1455 F2FS_CTX_INFO(ctx).extensions[i][0] = '\0'; 1456 cnt--; 1457 } 1458 } 1459 if (F2FS_OPTION(sbi).compress_ext_cnt + cnt > COMPRESS_EXT_NUM) { 1460 f2fs_err(sbi, "invalid extension length/number"); 1461 return -EINVAL; 1462 } 1463 } 1464 if (ctx->spec_mask & F2FS_SPEC_nocompress_extension) { 1465 cnt = F2FS_CTX_INFO(ctx).nocompress_ext_cnt; 1466 for (i = 0; i < F2FS_CTX_INFO(ctx).nocompress_ext_cnt; i++) { 1467 if (is_compress_extension_exist(&F2FS_OPTION(sbi), 1468 F2FS_CTX_INFO(ctx).noextensions[i], false)) { 1469 F2FS_CTX_INFO(ctx).noextensions[i][0] = '\0'; 1470 cnt--; 1471 } 1472 } 1473 if (F2FS_OPTION(sbi).nocompress_ext_cnt + cnt > COMPRESS_EXT_NUM) { 1474 f2fs_err(sbi, "invalid noextension length/number"); 1475 return -EINVAL; 1476 } 1477 } 1478 1479 if (f2fs_test_compress_extension(F2FS_CTX_INFO(ctx).noextensions, 1480 F2FS_CTX_INFO(ctx).nocompress_ext_cnt, 1481 F2FS_CTX_INFO(ctx).extensions, 1482 F2FS_CTX_INFO(ctx).compress_ext_cnt)) { 1483 f2fs_err(sbi, "new noextensions conflicts with new extensions"); 1484 return -EINVAL; 1485 } 1486 if (f2fs_test_compress_extension(F2FS_CTX_INFO(ctx).noextensions, 1487 F2FS_CTX_INFO(ctx).nocompress_ext_cnt, 1488 F2FS_OPTION(sbi).extensions, 1489 F2FS_OPTION(sbi).compress_ext_cnt)) { 1490 f2fs_err(sbi, "new noextensions conflicts with old extensions"); 1491 return -EINVAL; 1492 } 1493 if (f2fs_test_compress_extension(F2FS_OPTION(sbi).noextensions, 1494 F2FS_OPTION(sbi).nocompress_ext_cnt, 1495 F2FS_CTX_INFO(ctx).extensions, 1496 F2FS_CTX_INFO(ctx).compress_ext_cnt)) { 1497 f2fs_err(sbi, "new extensions conflicts with old noextensions"); 1498 return -EINVAL; 1499 } 1500 #endif 1501 return 0; 1502 } 1503 1504 static int f2fs_check_opt_consistency(struct fs_context *fc, 1505 struct super_block *sb) 1506 { 1507 struct f2fs_fs_context *ctx = fc->fs_private; 1508 struct f2fs_sb_info *sbi = F2FS_SB(sb); 1509 int err; 1510 1511 if (ctx_test_opt(ctx, F2FS_MOUNT_NORECOVERY) && !f2fs_readonly(sb)) 1512 return -EINVAL; 1513 1514 if (f2fs_hw_should_discard(sbi) && 1515 ctx_test_opt_mask(ctx, F2FS_MOUNT_DISCARD) && 1516 !ctx_test_opt(ctx, F2FS_MOUNT_DISCARD)) { 1517 f2fs_warn(sbi, "discard is required for zoned block devices"); 1518 return -EINVAL; 1519 } 1520 1521 if (!f2fs_hw_support_discard(sbi) && 1522 ctx_test_opt_mask(ctx, F2FS_MOUNT_DISCARD) && 1523 ctx_test_opt(ctx, F2FS_MOUNT_DISCARD)) { 1524 f2fs_warn(sbi, "device does not support discard"); 1525 ctx_clear_opt(ctx, F2FS_MOUNT_DISCARD); 1526 ctx_clear_opt_mask(ctx, F2FS_MOUNT_DISCARD); 1527 } 1528 1529 if (f2fs_sb_has_device_alias(sbi) && 1530 ctx_test_opt_mask(ctx, F2FS_MOUNT_READ_EXTENT_CACHE) && 1531 !ctx_test_opt(ctx, F2FS_MOUNT_READ_EXTENT_CACHE)) { 1532 f2fs_err(sbi, "device aliasing requires extent cache"); 1533 return -EINVAL; 1534 } 1535 1536 if (test_opt(sbi, RESERVE_ROOT) && 1537 ctx_test_opt_mask(ctx, F2FS_MOUNT_RESERVE_ROOT) && 1538 ctx_test_opt(ctx, F2FS_MOUNT_RESERVE_ROOT)) { 1539 f2fs_info(sbi, "Preserve previous reserve_root=%u", 1540 F2FS_OPTION(sbi).root_reserved_blocks); 1541 ctx_clear_opt(ctx, F2FS_MOUNT_RESERVE_ROOT); 1542 ctx_clear_opt_mask(ctx, F2FS_MOUNT_RESERVE_ROOT); 1543 ctx->spec_mask &= ~F2FS_SPEC_reserve_root; 1544 } 1545 if (test_opt(sbi, RESERVE_NODE) && 1546 ctx_test_opt_mask(ctx, F2FS_MOUNT_RESERVE_NODE) && 1547 ctx_test_opt(ctx, F2FS_MOUNT_RESERVE_NODE)) { 1548 f2fs_info(sbi, "Preserve previous reserve_node=%u", 1549 F2FS_OPTION(sbi).root_reserved_nodes); 1550 ctx_clear_opt(ctx, F2FS_MOUNT_RESERVE_NODE); 1551 ctx_clear_opt_mask(ctx, F2FS_MOUNT_RESERVE_NODE); 1552 ctx->spec_mask &= ~F2FS_SPEC_reserve_node; 1553 } 1554 1555 err = f2fs_check_test_dummy_encryption(fc, sb); 1556 if (err) 1557 return err; 1558 1559 err = f2fs_check_compression(fc, sb); 1560 if (err) 1561 return err; 1562 1563 err = f2fs_check_quota_consistency(fc, sb); 1564 if (err) 1565 return err; 1566 1567 if (!IS_ENABLED(CONFIG_UNICODE) && f2fs_sb_has_casefold(sbi)) { 1568 f2fs_err(sbi, 1569 "Filesystem with casefold feature cannot be mounted without CONFIG_UNICODE"); 1570 return -EINVAL; 1571 } 1572 1573 /* 1574 * The BLKZONED feature indicates that the drive was formatted with 1575 * zone alignment optimization. This is optional for host-aware 1576 * devices, but mandatory for host-managed zoned block devices. 1577 */ 1578 if (f2fs_sb_has_blkzoned(sbi)) { 1579 if (F2FS_CTX_INFO(ctx).bggc_mode == BGGC_MODE_OFF) { 1580 f2fs_warn(sbi, "zoned devices need bggc"); 1581 return -EINVAL; 1582 } 1583 #ifdef CONFIG_BLK_DEV_ZONED 1584 if ((ctx->spec_mask & F2FS_SPEC_discard_unit) && 1585 F2FS_CTX_INFO(ctx).discard_unit != DISCARD_UNIT_SECTION) { 1586 f2fs_info(sbi, "Zoned block device doesn't need small discard, set discard_unit=section by default"); 1587 F2FS_CTX_INFO(ctx).discard_unit = DISCARD_UNIT_SECTION; 1588 } 1589 1590 if ((ctx->spec_mask & F2FS_SPEC_mode) && 1591 F2FS_CTX_INFO(ctx).fs_mode != FS_MODE_LFS) { 1592 f2fs_info(sbi, "Only lfs mode is allowed with zoned block device feature"); 1593 return -EINVAL; 1594 } 1595 #else 1596 f2fs_err(sbi, "Zoned block device support is not enabled"); 1597 return -EINVAL; 1598 #endif 1599 } 1600 1601 if (ctx_test_opt(ctx, F2FS_MOUNT_INLINE_XATTR_SIZE)) { 1602 int min_size, max_size; 1603 1604 if (!f2fs_sb_has_extra_attr(sbi) || 1605 !f2fs_sb_has_flexible_inline_xattr(sbi)) { 1606 f2fs_err(sbi, "extra_attr or flexible_inline_xattr feature is off"); 1607 return -EINVAL; 1608 } 1609 if (!ctx_test_opt(ctx, F2FS_MOUNT_INLINE_XATTR) && !test_opt(sbi, INLINE_XATTR)) { 1610 f2fs_err(sbi, "inline_xattr_size option should be set with inline_xattr option"); 1611 return -EINVAL; 1612 } 1613 min_size = MIN_INLINE_XATTR_SIZE; 1614 max_size = MAX_INLINE_XATTR_SIZE(F2FS_BLKSIZE(sbi)); 1615 1616 if (F2FS_OPTION(sbi).inline_xattr_size < min_size || 1617 F2FS_OPTION(sbi).inline_xattr_size > max_size) { 1618 f2fs_err(sbi, "inline xattr size is out of range: %d ~ %d", 1619 min_size, max_size); 1620 return -EINVAL; 1621 } 1622 } 1623 1624 if (ctx_test_opt(ctx, F2FS_MOUNT_ATGC) && 1625 F2FS_CTX_INFO(ctx).fs_mode == FS_MODE_LFS) { 1626 f2fs_err(sbi, "LFS is not compatible with ATGC"); 1627 return -EINVAL; 1628 } 1629 1630 if (f2fs_is_readonly(sbi) && ctx_test_opt(ctx, F2FS_MOUNT_FLUSH_MERGE)) { 1631 f2fs_err(sbi, "FLUSH_MERGE not compatible with readonly mode"); 1632 return -EINVAL; 1633 } 1634 1635 if (f2fs_sb_has_readonly(sbi) && !f2fs_readonly(sbi->sb)) { 1636 f2fs_err(sbi, "Allow to mount readonly mode only"); 1637 return -EROFS; 1638 } 1639 return 0; 1640 } 1641 1642 static void f2fs_apply_quota_options(struct fs_context *fc, 1643 struct super_block *sb) 1644 { 1645 #ifdef CONFIG_QUOTA 1646 struct f2fs_fs_context *ctx = fc->fs_private; 1647 struct f2fs_sb_info *sbi = F2FS_SB(sb); 1648 bool quota_feature = f2fs_sb_has_quota_ino(sbi); 1649 char *qname; 1650 int i; 1651 1652 if (quota_feature) 1653 return; 1654 1655 for (i = 0; i < MAXQUOTAS; i++) { 1656 if (!(ctx->qname_mask & (1 << i))) 1657 continue; 1658 1659 qname = F2FS_CTX_INFO(ctx).s_qf_names[i]; 1660 if (qname) { 1661 qname = kstrdup(F2FS_CTX_INFO(ctx).s_qf_names[i], 1662 GFP_KERNEL | __GFP_NOFAIL); 1663 set_opt(sbi, QUOTA); 1664 } 1665 F2FS_OPTION(sbi).s_qf_names[i] = qname; 1666 } 1667 1668 if (ctx->spec_mask & F2FS_SPEC_jqfmt) 1669 F2FS_OPTION(sbi).s_jquota_fmt = F2FS_CTX_INFO(ctx).s_jquota_fmt; 1670 1671 if (quota_feature && F2FS_OPTION(sbi).s_jquota_fmt) { 1672 f2fs_info(sbi, "QUOTA feature is enabled, so ignore jquota_fmt"); 1673 F2FS_OPTION(sbi).s_jquota_fmt = 0; 1674 } 1675 #endif 1676 } 1677 1678 static void f2fs_apply_test_dummy_encryption(struct fs_context *fc, 1679 struct super_block *sb) 1680 { 1681 struct f2fs_fs_context *ctx = fc->fs_private; 1682 struct f2fs_sb_info *sbi = F2FS_SB(sb); 1683 1684 if (!fscrypt_is_dummy_policy_set(&F2FS_CTX_INFO(ctx).dummy_enc_policy) || 1685 /* if already set, it was already verified to be the same */ 1686 fscrypt_is_dummy_policy_set(&F2FS_OPTION(sbi).dummy_enc_policy)) 1687 return; 1688 swap(F2FS_OPTION(sbi).dummy_enc_policy, F2FS_CTX_INFO(ctx).dummy_enc_policy); 1689 f2fs_warn(sbi, "Test dummy encryption mode enabled"); 1690 } 1691 1692 static void f2fs_apply_compression(struct fs_context *fc, 1693 struct super_block *sb) 1694 { 1695 #ifdef CONFIG_F2FS_FS_COMPRESSION 1696 struct f2fs_fs_context *ctx = fc->fs_private; 1697 struct f2fs_sb_info *sbi = F2FS_SB(sb); 1698 unsigned char (*ctx_ext)[F2FS_EXTENSION_LEN]; 1699 unsigned char (*sbi_ext)[F2FS_EXTENSION_LEN]; 1700 int ctx_cnt, sbi_cnt, i; 1701 1702 if (ctx->spec_mask & F2FS_SPEC_compress_level) 1703 F2FS_OPTION(sbi).compress_level = 1704 F2FS_CTX_INFO(ctx).compress_level; 1705 if (ctx->spec_mask & F2FS_SPEC_compress_algorithm) 1706 F2FS_OPTION(sbi).compress_algorithm = 1707 F2FS_CTX_INFO(ctx).compress_algorithm; 1708 if (ctx->spec_mask & F2FS_SPEC_compress_log_size) 1709 F2FS_OPTION(sbi).compress_log_size = 1710 F2FS_CTX_INFO(ctx).compress_log_size; 1711 if (ctx->spec_mask & F2FS_SPEC_compress_chksum) 1712 F2FS_OPTION(sbi).compress_chksum = 1713 F2FS_CTX_INFO(ctx).compress_chksum; 1714 if (ctx->spec_mask & F2FS_SPEC_compress_mode) 1715 F2FS_OPTION(sbi).compress_mode = 1716 F2FS_CTX_INFO(ctx).compress_mode; 1717 if (ctx->spec_mask & F2FS_SPEC_compress_extension) { 1718 ctx_ext = F2FS_CTX_INFO(ctx).extensions; 1719 ctx_cnt = F2FS_CTX_INFO(ctx).compress_ext_cnt; 1720 sbi_ext = F2FS_OPTION(sbi).extensions; 1721 sbi_cnt = F2FS_OPTION(sbi).compress_ext_cnt; 1722 for (i = 0; i < ctx_cnt; i++) { 1723 if (strlen(ctx_ext[i]) == 0) 1724 continue; 1725 strscpy(sbi_ext[sbi_cnt], ctx_ext[i]); 1726 sbi_cnt++; 1727 } 1728 F2FS_OPTION(sbi).compress_ext_cnt = sbi_cnt; 1729 } 1730 if (ctx->spec_mask & F2FS_SPEC_nocompress_extension) { 1731 ctx_ext = F2FS_CTX_INFO(ctx).noextensions; 1732 ctx_cnt = F2FS_CTX_INFO(ctx).nocompress_ext_cnt; 1733 sbi_ext = F2FS_OPTION(sbi).noextensions; 1734 sbi_cnt = F2FS_OPTION(sbi).nocompress_ext_cnt; 1735 for (i = 0; i < ctx_cnt; i++) { 1736 if (strlen(ctx_ext[i]) == 0) 1737 continue; 1738 strscpy(sbi_ext[sbi_cnt], ctx_ext[i]); 1739 sbi_cnt++; 1740 } 1741 F2FS_OPTION(sbi).nocompress_ext_cnt = sbi_cnt; 1742 } 1743 #endif 1744 } 1745 1746 static void f2fs_apply_options(struct fs_context *fc, struct super_block *sb) 1747 { 1748 struct f2fs_fs_context *ctx = fc->fs_private; 1749 struct f2fs_sb_info *sbi = F2FS_SB(sb); 1750 1751 F2FS_OPTION(sbi).opt &= ~ctx->opt_mask; 1752 F2FS_OPTION(sbi).opt |= F2FS_CTX_INFO(ctx).opt; 1753 1754 if (ctx->spec_mask & F2FS_SPEC_background_gc) 1755 F2FS_OPTION(sbi).bggc_mode = F2FS_CTX_INFO(ctx).bggc_mode; 1756 if (ctx->spec_mask & F2FS_SPEC_inline_xattr_size) 1757 F2FS_OPTION(sbi).inline_xattr_size = 1758 F2FS_CTX_INFO(ctx).inline_xattr_size; 1759 if (ctx->spec_mask & F2FS_SPEC_active_logs) 1760 F2FS_OPTION(sbi).active_logs = F2FS_CTX_INFO(ctx).active_logs; 1761 if (ctx->spec_mask & F2FS_SPEC_reserve_root) 1762 F2FS_OPTION(sbi).root_reserved_blocks = 1763 F2FS_CTX_INFO(ctx).root_reserved_blocks; 1764 if (ctx->spec_mask & F2FS_SPEC_reserve_node) 1765 F2FS_OPTION(sbi).root_reserved_nodes = 1766 F2FS_CTX_INFO(ctx).root_reserved_nodes; 1767 if (ctx->spec_mask & F2FS_SPEC_resgid) 1768 F2FS_OPTION(sbi).s_resgid = F2FS_CTX_INFO(ctx).s_resgid; 1769 if (ctx->spec_mask & F2FS_SPEC_resuid) 1770 F2FS_OPTION(sbi).s_resuid = F2FS_CTX_INFO(ctx).s_resuid; 1771 if (ctx->spec_mask & F2FS_SPEC_mode) 1772 F2FS_OPTION(sbi).fs_mode = F2FS_CTX_INFO(ctx).fs_mode; 1773 #ifdef CONFIG_F2FS_FAULT_INJECTION 1774 if (ctx->spec_mask & F2FS_SPEC_fault_injection) 1775 (void)f2fs_build_fault_attr(sbi, 1776 F2FS_CTX_INFO(ctx).fault_info.inject_rate, 0, FAULT_RATE); 1777 if (ctx->spec_mask & F2FS_SPEC_fault_type) 1778 (void)f2fs_build_fault_attr(sbi, 0, 1779 F2FS_CTX_INFO(ctx).fault_info.inject_type, FAULT_TYPE); 1780 #endif 1781 if (ctx->spec_mask & F2FS_SPEC_alloc_mode) 1782 F2FS_OPTION(sbi).alloc_mode = F2FS_CTX_INFO(ctx).alloc_mode; 1783 if (ctx->spec_mask & F2FS_SPEC_fsync_mode) 1784 F2FS_OPTION(sbi).fsync_mode = F2FS_CTX_INFO(ctx).fsync_mode; 1785 if (ctx->spec_mask & F2FS_SPEC_checkpoint_disable_cap) 1786 F2FS_OPTION(sbi).unusable_cap = F2FS_CTX_INFO(ctx).unusable_cap; 1787 if (ctx->spec_mask & F2FS_SPEC_checkpoint_disable_cap_perc) 1788 F2FS_OPTION(sbi).unusable_cap_perc = 1789 F2FS_CTX_INFO(ctx).unusable_cap_perc; 1790 if (ctx->spec_mask & F2FS_SPEC_discard_unit) 1791 F2FS_OPTION(sbi).discard_unit = F2FS_CTX_INFO(ctx).discard_unit; 1792 if (ctx->spec_mask & F2FS_SPEC_memory_mode) 1793 F2FS_OPTION(sbi).memory_mode = F2FS_CTX_INFO(ctx).memory_mode; 1794 if (ctx->spec_mask & F2FS_SPEC_errors) 1795 F2FS_OPTION(sbi).errors = F2FS_CTX_INFO(ctx).errors; 1796 if (ctx->spec_mask & F2FS_SPEC_lookup_mode) 1797 F2FS_OPTION(sbi).lookup_mode = F2FS_CTX_INFO(ctx).lookup_mode; 1798 if (ctx->spec_mask & F2FS_SPEC_resizable_tail_secno) 1799 F2FS_OPTION(sbi).resizable_tail_secno = 1800 F2FS_CTX_INFO(ctx).resizable_tail_secno; 1801 1802 f2fs_apply_compression(fc, sb); 1803 f2fs_apply_test_dummy_encryption(fc, sb); 1804 f2fs_apply_quota_options(fc, sb); 1805 } 1806 1807 static int f2fs_sanity_check_options(struct f2fs_sb_info *sbi, bool remount) 1808 { 1809 unsigned int total_sections = le32_to_cpu(sbi->raw_super->section_count); 1810 1811 if (F2FS_OPTION(sbi).resizable_tail_secno >= total_sections) { 1812 f2fs_err(sbi, "Option resizable_tail_secno is larger than or equal to total sections (%u >= %u)", 1813 F2FS_OPTION(sbi).resizable_tail_secno, total_sections); 1814 return -EINVAL; 1815 } 1816 if (f2fs_sb_has_device_alias(sbi) && 1817 !test_opt(sbi, READ_EXTENT_CACHE)) { 1818 f2fs_err(sbi, "device aliasing requires extent cache"); 1819 return -EINVAL; 1820 } 1821 1822 if (!remount) 1823 return 0; 1824 1825 #ifdef CONFIG_BLK_DEV_ZONED 1826 if (f2fs_sb_has_blkzoned(sbi) && 1827 sbi->max_open_zones < F2FS_OPTION(sbi).active_logs) { 1828 f2fs_err(sbi, 1829 "zoned: max open zones %u is too small, need at least %u open zones", 1830 sbi->max_open_zones, F2FS_OPTION(sbi).active_logs); 1831 return -EINVAL; 1832 } 1833 #endif 1834 if (f2fs_lfs_mode(sbi) && !IS_F2FS_IPU_DISABLE(sbi)) { 1835 f2fs_warn(sbi, "LFS is not compatible with IPU"); 1836 return -EINVAL; 1837 } 1838 return 0; 1839 } 1840 1841 static struct inode *f2fs_alloc_inode(struct super_block *sb) 1842 { 1843 struct f2fs_inode_info *fi; 1844 1845 if (time_to_inject(F2FS_SB(sb), FAULT_SLAB_ALLOC)) 1846 return NULL; 1847 1848 fi = alloc_inode_sb(sb, f2fs_inode_cachep, GFP_F2FS_ZERO); 1849 if (!fi) 1850 return NULL; 1851 1852 init_once((void *) fi); 1853 1854 /* Initialize f2fs-specific inode info */ 1855 atomic_set(&fi->dirty_pages, 0); 1856 atomic_set(&fi->i_compr_blocks, 0); 1857 atomic_set(&fi->open_count, 0); 1858 atomic_set(&fi->writeback, 0); 1859 init_f2fs_rwsem(&fi->i_sem); 1860 spin_lock_init(&fi->i_size_lock); 1861 INIT_LIST_HEAD(&fi->dirty_list); 1862 INIT_LIST_HEAD(&fi->gdirty_list); 1863 INIT_LIST_HEAD(&fi->gdonate_list); 1864 init_f2fs_rwsem(&fi->i_gc_rwsem[READ]); 1865 init_f2fs_rwsem(&fi->i_gc_rwsem[WRITE]); 1866 init_f2fs_rwsem(&fi->i_xattr_sem); 1867 1868 /* Will be used by directory only */ 1869 fi->i_dir_level = F2FS_SB(sb)->dir_level; 1870 1871 return &fi->vfs_inode; 1872 } 1873 1874 static int f2fs_drop_inode(struct inode *inode) 1875 { 1876 int ret; 1877 1878 /* 1879 * This is to avoid a deadlock condition like below. 1880 * writeback_single_inode(inode) 1881 * - f2fs_write_data_page 1882 * - f2fs_gc -> iput -> evict 1883 * - inode_wait_for_writeback(inode) 1884 */ 1885 if ((!inode_unhashed(inode) && inode_state_read(inode) & I_SYNC)) { 1886 if (!inode->i_nlink && !is_bad_inode(inode)) { 1887 /* to avoid evict_inode call simultaneously */ 1888 __iget(inode); 1889 spin_unlock(&inode->i_lock); 1890 1891 /* should remain fi->extent_tree for writepage */ 1892 f2fs_destroy_extent_node(inode); 1893 1894 sb_start_intwrite(inode->i_sb); 1895 f2fs_i_size_write(inode, 0); 1896 1897 f2fs_submit_merged_write_cond(F2FS_I_SB(inode), 1898 inode, NULL); 1899 truncate_inode_pages_final(inode->i_mapping); 1900 1901 if (F2FS_HAS_BLOCKS(inode)) 1902 f2fs_truncate(inode); 1903 1904 sb_end_intwrite(inode->i_sb); 1905 1906 spin_lock(&inode->i_lock); 1907 atomic_dec(&inode->i_count); 1908 } 1909 trace_f2fs_drop_inode(inode, 0); 1910 return 0; 1911 } 1912 ret = inode_generic_drop(inode); 1913 if (!ret) 1914 ret = fscrypt_drop_inode(inode); 1915 trace_f2fs_drop_inode(inode, ret); 1916 return ret; 1917 } 1918 1919 int f2fs_inode_dirtied(struct inode *inode, bool sync) 1920 { 1921 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1922 int ret = 0; 1923 1924 spin_lock(&sbi->inode_lock[DIRTY_META]); 1925 if (is_inode_flag_set(inode, FI_DIRTY_INODE)) { 1926 ret = 1; 1927 } else { 1928 set_inode_flag(inode, FI_DIRTY_INODE); 1929 stat_inc_dirty_inode(sbi, DIRTY_META); 1930 } 1931 if (sync && list_empty(&F2FS_I(inode)->gdirty_list)) { 1932 list_add_tail(&F2FS_I(inode)->gdirty_list, 1933 &sbi->inode_list[DIRTY_META]); 1934 inc_cache_count(sbi, F2FS_DIRTY_IMETA); 1935 } 1936 spin_unlock(&sbi->inode_lock[DIRTY_META]); 1937 1938 /* if atomic write is not committed, set inode w/ atomic dirty */ 1939 if (!ret && f2fs_is_atomic_file(inode) && 1940 !is_inode_flag_set(inode, FI_ATOMIC_COMMITTED)) 1941 set_inode_flag(inode, FI_ATOMIC_DIRTIED); 1942 1943 return ret; 1944 } 1945 1946 void f2fs_inode_synced(struct inode *inode) 1947 { 1948 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1949 1950 spin_lock(&sbi->inode_lock[DIRTY_META]); 1951 if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) { 1952 spin_unlock(&sbi->inode_lock[DIRTY_META]); 1953 return; 1954 } 1955 if (!list_empty(&F2FS_I(inode)->gdirty_list)) { 1956 list_del_init(&F2FS_I(inode)->gdirty_list); 1957 dec_cache_count(sbi, F2FS_DIRTY_IMETA); 1958 } 1959 clear_inode_flag(inode, FI_DIRTY_INODE); 1960 clear_inode_flag(inode, FI_AUTO_RECOVER); 1961 stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META); 1962 spin_unlock(&sbi->inode_lock[DIRTY_META]); 1963 } 1964 1965 /* 1966 * f2fs_dirty_inode() is called from __mark_inode_dirty() 1967 * 1968 * We should call set_dirty_inode to write the dirty inode through write_inode. 1969 */ 1970 static void f2fs_dirty_inode(struct inode *inode, int flags) 1971 { 1972 if (is_inode_flag_set(inode, FI_AUTO_RECOVER)) 1973 clear_inode_flag(inode, FI_AUTO_RECOVER); 1974 1975 f2fs_inode_dirtied(inode, false); 1976 } 1977 1978 static void f2fs_free_inode(struct inode *inode) 1979 { 1980 fscrypt_free_inode(inode); 1981 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode)); 1982 } 1983 1984 static void destroy_percpu_info(struct f2fs_sb_info *sbi) 1985 { 1986 percpu_counter_destroy(&sbi->total_valid_inode_count); 1987 percpu_counter_destroy(&sbi->rf_node_block_count); 1988 percpu_counter_destroy(&sbi->alloc_valid_block_count); 1989 } 1990 1991 static void destroy_device_list(struct f2fs_sb_info *sbi) 1992 { 1993 int i; 1994 1995 for (i = 0; i < sbi->s_ndevs; i++) { 1996 if (i > 0) 1997 fs_bdev_file_release(FDEV(i).bdev_file, sbi->sb); 1998 #ifdef CONFIG_BLK_DEV_ZONED 1999 kvfree(FDEV(i).blkz_seq); 2000 #endif 2001 } 2002 kvfree(sbi->devs); 2003 } 2004 2005 static void f2fs_put_super(struct super_block *sb) 2006 { 2007 struct f2fs_sb_info *sbi = F2FS_SB(sb); 2008 int i; 2009 int err = 0; 2010 bool done; 2011 2012 /* unregister procfs/sysfs entries in advance to avoid race case */ 2013 f2fs_unregister_sysfs(sbi); 2014 2015 f2fs_quota_off_umount(sb); 2016 2017 /* prevent remaining shrinker jobs */ 2018 mutex_lock(&sbi->umount_mutex); 2019 2020 /* 2021 * flush all issued checkpoints and stop checkpoint issue thread. 2022 * after then, all checkpoints should be done by each process context. 2023 */ 2024 f2fs_stop_cache_wb_thread(sbi); 2025 f2fs_stop_ckpt_thread(sbi); 2026 2027 /* 2028 * We don't need to do checkpoint when superblock is clean. 2029 * But, the previous checkpoint was not done by umount, it needs to do 2030 * clean checkpoint again. 2031 */ 2032 if ((is_sbi_flag_set(sbi, SBI_IS_DIRTY) || 2033 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG))) { 2034 struct cp_control cpc = { 2035 .reason = CP_UMOUNT, 2036 }; 2037 stat_inc_cp_call_count(sbi, TOTAL_CALL); 2038 err = f2fs_write_checkpoint(sbi, &cpc); 2039 } 2040 2041 /* be sure to wait for any on-going discard commands */ 2042 done = f2fs_issue_discard_timeout(sbi, true); 2043 if (f2fs_realtime_discard_enable(sbi) && !sbi->discard_blks && done) { 2044 struct cp_control cpc = { 2045 .reason = CP_UMOUNT | CP_TRIMMED, 2046 }; 2047 stat_inc_cp_call_count(sbi, TOTAL_CALL); 2048 err = f2fs_write_checkpoint(sbi, &cpc); 2049 } 2050 2051 /* 2052 * normally superblock is clean, so we need to release this. 2053 * In addition, EIO will skip do checkpoint, we need this as well. 2054 */ 2055 f2fs_release_ino_entry(sbi, true); 2056 2057 f2fs_leave_shrinker(sbi); 2058 mutex_unlock(&sbi->umount_mutex); 2059 2060 /* our cp_error case, we can wait for any writeback page */ 2061 f2fs_flush_merged_writes(sbi); 2062 2063 f2fs_sync_dirty_data(sbi, F2FS_WB_CP_DATA); 2064 2065 if (err || f2fs_cp_error(sbi) || 2066 unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) { 2067 f2fs_truncate_node_caches(sbi, 0, ULONG_MAX); 2068 f2fs_truncate_meta_caches(sbi, 0, ULONG_MAX); 2069 } 2070 2071 f2fs_bug_on(sbi, sbi->fsync_node_num); 2072 2073 f2fs_destroy_cache(COMPRESS_CACHE(sbi)); 2074 f2fs_destroy_cache(NODE_CACHE(sbi)); 2075 f2fs_destroy_cache(META_CACHE(sbi)); 2076 2077 /* Should check the page counts after dropping all node/meta pages */ 2078 for (i = 0; i < NR_COUNT_TYPE; i++) { 2079 if (!get_nr_caches(sbi, i)) 2080 continue; 2081 f2fs_err(sbi, "detect filesystem reference count leak during " 2082 "umount, type: %d, count: %lld, err: %d, cp_err: %d", 2083 i, get_nr_caches(sbi, i), err, f2fs_cp_error(sbi)); 2084 f2fs_bug_on(sbi, 1); 2085 } 2086 2087 /* 2088 * iput() can update stat information, if f2fs_write_checkpoint() 2089 * above failed with error. 2090 */ 2091 f2fs_destroy_stats(sbi); 2092 2093 /* destroy f2fs internal modules */ 2094 f2fs_destroy_node_manager(sbi); 2095 f2fs_destroy_segment_manager(sbi); 2096 2097 /* flush s_error_work before sbi destroy */ 2098 flush_work(&sbi->s_error_work); 2099 2100 f2fs_destroy_wq(sbi); 2101 2102 kvfree(sbi->ckpt); 2103 2104 kfree(sbi->raw_super); 2105 2106 f2fs_destroy_page_array_cache(sbi); 2107 #ifdef CONFIG_QUOTA 2108 for (i = 0; i < MAXQUOTAS; i++) 2109 kfree(F2FS_OPTION(sbi).s_qf_names[i]); 2110 #endif 2111 fscrypt_free_dummy_policy(&F2FS_OPTION(sbi).dummy_enc_policy); 2112 destroy_percpu_info(sbi); 2113 f2fs_destroy_iostat(sbi); 2114 for (i = 0; i < NR_PAGE_TYPE; i++) 2115 kfree(sbi->write_io[i]); 2116 #if IS_ENABLED(CONFIG_UNICODE) 2117 utf8_unload(sb->s_encoding); 2118 #endif 2119 sync_blockdev(sb->s_bdev); 2120 invalidate_bdev(sb->s_bdev); 2121 for (i = 1; i < sbi->s_ndevs; i++) { 2122 sync_blockdev(FDEV(i).bdev); 2123 invalidate_bdev(FDEV(i).bdev); 2124 } 2125 } 2126 2127 int f2fs_sync_fs(struct super_block *sb, int sync) 2128 { 2129 struct f2fs_sb_info *sbi = F2FS_SB(sb); 2130 int err = 0; 2131 2132 if (unlikely(f2fs_cp_error(sbi))) 2133 return 0; 2134 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) 2135 return 0; 2136 2137 trace_f2fs_sync_fs(sb, sync); 2138 2139 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) 2140 return -EAGAIN; 2141 2142 if (sync) { 2143 stat_inc_cp_call_count(sbi, TOTAL_CALL); 2144 err = f2fs_issue_checkpoint(sbi); 2145 } 2146 2147 return err; 2148 } 2149 2150 static int f2fs_sync_fs_super(struct super_block *sb, int sync) 2151 { 2152 struct f2fs_sb_info *sbi = F2FS_SB(sb); 2153 bool freeze_sync = sync && 2154 sb->s_writers.frozen == SB_FREEZE_PAGEFAULT; 2155 int err; 2156 2157 /* freeze_super() holds s_umount for write during this sync pass. */ 2158 if (freeze_sync) 2159 sbi->umount_lock_holder = current; 2160 2161 err = f2fs_sync_fs(sb, sync); 2162 2163 if (freeze_sync) 2164 sbi->umount_lock_holder = NULL; 2165 2166 return err; 2167 } 2168 2169 static int f2fs_freeze(struct super_block *sb) 2170 { 2171 struct f2fs_sb_info *sbi = F2FS_SB(sb); 2172 2173 if (f2fs_readonly(sb)) 2174 return 0; 2175 2176 /* IO error happened before */ 2177 if (unlikely(f2fs_cp_error(sbi))) 2178 return -EIO; 2179 2180 /* must be clean, since sync_filesystem() was already called */ 2181 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY)) 2182 return -EINVAL; 2183 2184 sbi->umount_lock_holder = current; 2185 2186 /* Let's flush checkpoints and stop the thread. */ 2187 f2fs_flush_ckpt_thread(sbi); 2188 2189 sbi->umount_lock_holder = NULL; 2190 2191 /* to avoid deadlock on f2fs_evict_inode->SB_FREEZE_FS */ 2192 set_sbi_flag(sbi, SBI_IS_FREEZING); 2193 return 0; 2194 } 2195 2196 static int f2fs_unfreeze(struct super_block *sb) 2197 { 2198 struct f2fs_sb_info *sbi = F2FS_SB(sb); 2199 2200 /* 2201 * It will update discard_max_bytes of mounted lvm device to zero 2202 * after creating snapshot on this lvm device, let's drop all 2203 * remained discards. 2204 * We don't need to disable real-time discard because discard_max_bytes 2205 * will recover after removal of snapshot. 2206 */ 2207 if (test_opt(sbi, DISCARD) && !f2fs_hw_support_discard(sbi)) 2208 f2fs_issue_discard_timeout(sbi, true); 2209 2210 clear_sbi_flag(F2FS_SB(sb), SBI_IS_FREEZING); 2211 return 0; 2212 } 2213 2214 #ifdef CONFIG_QUOTA 2215 static int f2fs_statfs_project(struct super_block *sb, 2216 kprojid_t projid, struct kstatfs *buf) 2217 { 2218 struct kqid qid; 2219 struct dquot *dquot; 2220 u64 limit; 2221 u64 curblock; 2222 2223 qid = make_kqid_projid(projid); 2224 dquot = dqget(sb, qid); 2225 if (IS_ERR(dquot)) 2226 return PTR_ERR(dquot); 2227 spin_lock(&dquot->dq_dqb_lock); 2228 2229 limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit, 2230 dquot->dq_dqb.dqb_bhardlimit); 2231 limit >>= sb->s_blocksize_bits; 2232 2233 if (limit) { 2234 uint64_t remaining = 0; 2235 2236 curblock = (dquot->dq_dqb.dqb_curspace + 2237 dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits; 2238 if (limit > curblock) 2239 remaining = limit - curblock; 2240 2241 buf->f_blocks = min(buf->f_blocks, limit); 2242 buf->f_bfree = min(buf->f_bfree, remaining); 2243 buf->f_bavail = min(buf->f_bavail, remaining); 2244 } 2245 2246 limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit, 2247 dquot->dq_dqb.dqb_ihardlimit); 2248 2249 if (limit) { 2250 uint64_t remaining = 0; 2251 2252 if (limit > dquot->dq_dqb.dqb_curinodes) 2253 remaining = limit - dquot->dq_dqb.dqb_curinodes; 2254 2255 buf->f_files = min(buf->f_files, limit); 2256 buf->f_ffree = min(buf->f_ffree, remaining); 2257 } 2258 2259 spin_unlock(&dquot->dq_dqb_lock); 2260 dqput(dquot); 2261 return 0; 2262 } 2263 #endif 2264 2265 static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf) 2266 { 2267 struct super_block *sb = dentry->d_sb; 2268 struct f2fs_sb_info *sbi = F2FS_SB(sb); 2269 u64 id = huge_encode_dev(sb->s_bdev->bd_dev); 2270 block_t total_count, user_block_count, start_count; 2271 u64 avail_node_count; 2272 unsigned int total_valid_node_count; 2273 2274 total_count = le64_to_cpu(sbi->raw_super->block_count); 2275 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr); 2276 buf->f_type = F2FS_SUPER_MAGIC; 2277 buf->f_bsize = sbi->blocksize; 2278 2279 buf->f_blocks = total_count - start_count; 2280 2281 spin_lock(&sbi->stat_lock); 2282 if (sbi->carve_out) 2283 buf->f_blocks -= sbi->current_reserved_blocks; 2284 user_block_count = sbi->user_block_count; 2285 total_valid_node_count = valid_node_count(sbi); 2286 avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM; 2287 buf->f_bfree = user_block_count - valid_user_blocks(sbi) - 2288 sbi->current_reserved_blocks; 2289 2290 if (unlikely(buf->f_bfree <= sbi->unusable_block_count)) 2291 buf->f_bfree = 0; 2292 else 2293 buf->f_bfree -= sbi->unusable_block_count; 2294 spin_unlock(&sbi->stat_lock); 2295 2296 if (buf->f_bfree > F2FS_OPTION(sbi).root_reserved_blocks) 2297 buf->f_bavail = buf->f_bfree - 2298 F2FS_OPTION(sbi).root_reserved_blocks; 2299 else 2300 buf->f_bavail = 0; 2301 2302 if (avail_node_count > user_block_count) { 2303 buf->f_files = user_block_count; 2304 buf->f_ffree = buf->f_bavail; 2305 } else { 2306 buf->f_files = avail_node_count; 2307 buf->f_ffree = min(avail_node_count - total_valid_node_count, 2308 buf->f_bavail); 2309 } 2310 2311 buf->f_namelen = F2FS_NAME_LEN; 2312 buf->f_fsid = u64_to_fsid(id); 2313 2314 #ifdef CONFIG_QUOTA 2315 if (is_inode_flag_set(d_inode(dentry), FI_PROJ_INHERIT) && 2316 sb_has_quota_limits_enabled(sb, PRJQUOTA)) { 2317 f2fs_statfs_project(sb, F2FS_I(d_inode(dentry))->i_projid, buf); 2318 } 2319 #endif 2320 return 0; 2321 } 2322 2323 static inline void f2fs_show_quota_options(struct seq_file *seq, 2324 struct super_block *sb) 2325 { 2326 #ifdef CONFIG_QUOTA 2327 struct f2fs_sb_info *sbi = F2FS_SB(sb); 2328 2329 if (F2FS_OPTION(sbi).s_jquota_fmt) { 2330 char *fmtname = ""; 2331 2332 switch (F2FS_OPTION(sbi).s_jquota_fmt) { 2333 case QFMT_VFS_OLD: 2334 fmtname = "vfsold"; 2335 break; 2336 case QFMT_VFS_V0: 2337 fmtname = "vfsv0"; 2338 break; 2339 case QFMT_VFS_V1: 2340 fmtname = "vfsv1"; 2341 break; 2342 } 2343 seq_printf(seq, ",jqfmt=%s", fmtname); 2344 } 2345 2346 if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA]) 2347 seq_show_option(seq, "usrjquota", 2348 F2FS_OPTION(sbi).s_qf_names[USRQUOTA]); 2349 2350 if (F2FS_OPTION(sbi).s_qf_names[GRPQUOTA]) 2351 seq_show_option(seq, "grpjquota", 2352 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA]); 2353 2354 if (F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]) 2355 seq_show_option(seq, "prjjquota", 2356 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]); 2357 #endif 2358 } 2359 2360 #ifdef CONFIG_F2FS_FS_COMPRESSION 2361 static inline void f2fs_show_compress_options(struct seq_file *seq, 2362 struct super_block *sb) 2363 { 2364 struct f2fs_sb_info *sbi = F2FS_SB(sb); 2365 char *algtype = ""; 2366 int i; 2367 2368 if (!f2fs_sb_has_compression(sbi)) 2369 return; 2370 2371 switch (F2FS_OPTION(sbi).compress_algorithm) { 2372 case COMPRESS_LZO: 2373 algtype = "lzo"; 2374 break; 2375 case COMPRESS_LZ4: 2376 algtype = "lz4"; 2377 break; 2378 case COMPRESS_ZSTD: 2379 algtype = "zstd"; 2380 break; 2381 case COMPRESS_LZORLE: 2382 algtype = "lzo-rle"; 2383 break; 2384 } 2385 seq_printf(seq, ",compress_algorithm=%s", algtype); 2386 2387 if (F2FS_OPTION(sbi).compress_level) 2388 seq_printf(seq, ":%d", F2FS_OPTION(sbi).compress_level); 2389 2390 seq_printf(seq, ",compress_log_size=%u", 2391 F2FS_OPTION(sbi).compress_log_size); 2392 2393 for (i = 0; i < F2FS_OPTION(sbi).compress_ext_cnt; i++) { 2394 seq_printf(seq, ",compress_extension=%s", 2395 F2FS_OPTION(sbi).extensions[i]); 2396 } 2397 2398 for (i = 0; i < F2FS_OPTION(sbi).nocompress_ext_cnt; i++) { 2399 seq_printf(seq, ",nocompress_extension=%s", 2400 F2FS_OPTION(sbi).noextensions[i]); 2401 } 2402 2403 if (F2FS_OPTION(sbi).compress_chksum) 2404 seq_puts(seq, ",compress_chksum"); 2405 2406 if (F2FS_OPTION(sbi).compress_mode == COMPR_MODE_FS) 2407 seq_printf(seq, ",compress_mode=%s", "fs"); 2408 else if (F2FS_OPTION(sbi).compress_mode == COMPR_MODE_USER) 2409 seq_printf(seq, ",compress_mode=%s", "user"); 2410 2411 if (test_opt(sbi, COMPRESS_CACHE)) 2412 seq_puts(seq, ",compress_cache"); 2413 } 2414 #endif 2415 2416 static int f2fs_show_options(struct seq_file *seq, struct dentry *root) 2417 { 2418 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb); 2419 2420 if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_SYNC) 2421 seq_printf(seq, ",background_gc=%s", "sync"); 2422 else if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_ON) 2423 seq_printf(seq, ",background_gc=%s", "on"); 2424 else if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_OFF) 2425 seq_printf(seq, ",background_gc=%s", "off"); 2426 2427 if (test_opt(sbi, GC_MERGE)) 2428 seq_puts(seq, ",gc_merge"); 2429 else 2430 seq_puts(seq, ",nogc_merge"); 2431 2432 if (test_opt(sbi, DISABLE_ROLL_FORWARD)) 2433 seq_puts(seq, ",disable_roll_forward"); 2434 if (test_opt(sbi, NORECOVERY)) 2435 seq_puts(seq, ",norecovery"); 2436 if (test_opt(sbi, DISCARD)) { 2437 seq_puts(seq, ",discard"); 2438 if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_BLOCK) 2439 seq_printf(seq, ",discard_unit=%s", "block"); 2440 else if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SEGMENT) 2441 seq_printf(seq, ",discard_unit=%s", "segment"); 2442 else if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SECTION) 2443 seq_printf(seq, ",discard_unit=%s", "section"); 2444 } else { 2445 seq_puts(seq, ",nodiscard"); 2446 } 2447 #ifdef CONFIG_F2FS_FS_XATTR 2448 if (test_opt(sbi, XATTR_USER)) 2449 seq_puts(seq, ",user_xattr"); 2450 else 2451 seq_puts(seq, ",nouser_xattr"); 2452 if (test_opt(sbi, INLINE_XATTR)) 2453 seq_puts(seq, ",inline_xattr"); 2454 else 2455 seq_puts(seq, ",noinline_xattr"); 2456 if (test_opt(sbi, INLINE_XATTR_SIZE)) 2457 seq_printf(seq, ",inline_xattr_size=%u", 2458 F2FS_OPTION(sbi).inline_xattr_size); 2459 #endif 2460 #ifdef CONFIG_F2FS_FS_POSIX_ACL 2461 if (test_opt(sbi, POSIX_ACL)) 2462 seq_puts(seq, ",acl"); 2463 else 2464 seq_puts(seq, ",noacl"); 2465 #endif 2466 if (test_opt(sbi, DISABLE_EXT_IDENTIFY)) 2467 seq_puts(seq, ",disable_ext_identify"); 2468 if (test_opt(sbi, INLINE_DATA)) 2469 seq_puts(seq, ",inline_data"); 2470 else 2471 seq_puts(seq, ",noinline_data"); 2472 if (test_opt(sbi, INLINE_DENTRY)) 2473 seq_puts(seq, ",inline_dentry"); 2474 else 2475 seq_puts(seq, ",noinline_dentry"); 2476 if (test_opt(sbi, FLUSH_MERGE)) 2477 seq_puts(seq, ",flush_merge"); 2478 else 2479 seq_puts(seq, ",noflush_merge"); 2480 if (test_opt(sbi, NOBARRIER)) 2481 seq_puts(seq, ",nobarrier"); 2482 else 2483 seq_puts(seq, ",barrier"); 2484 if (test_opt(sbi, FASTBOOT)) 2485 seq_puts(seq, ",fastboot"); 2486 if (test_opt(sbi, READ_EXTENT_CACHE)) 2487 seq_puts(seq, ",extent_cache"); 2488 else 2489 seq_puts(seq, ",noextent_cache"); 2490 if (test_opt(sbi, AGE_EXTENT_CACHE)) 2491 seq_puts(seq, ",age_extent_cache"); 2492 if (test_opt(sbi, DATA_FLUSH)) 2493 seq_puts(seq, ",data_flush"); 2494 2495 seq_puts(seq, ",mode="); 2496 if (F2FS_OPTION(sbi).fs_mode == FS_MODE_ADAPTIVE) 2497 seq_puts(seq, "adaptive"); 2498 else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS) 2499 seq_puts(seq, "lfs"); 2500 else if (f2fs_need_rand_seg(sbi, NO_CHECK_TYPE)) 2501 seq_puts(seq, "fragment:segment"); 2502 else if (f2fs_need_rand_blk(sbi, NO_CHECK_TYPE)) 2503 seq_puts(seq, "fragment:block"); 2504 seq_printf(seq, ",active_logs=%u", F2FS_OPTION(sbi).active_logs); 2505 if (test_opt(sbi, RESERVE_ROOT) || test_opt(sbi, RESERVE_NODE)) 2506 seq_printf(seq, ",reserve_root=%u,reserve_node=%u,resuid=%u," 2507 "resgid=%u", 2508 F2FS_OPTION(sbi).root_reserved_blocks, 2509 F2FS_OPTION(sbi).root_reserved_nodes, 2510 from_kuid_munged(&init_user_ns, 2511 F2FS_OPTION(sbi).s_resuid), 2512 from_kgid_munged(&init_user_ns, 2513 F2FS_OPTION(sbi).s_resgid)); 2514 #ifdef CONFIG_F2FS_FAULT_INJECTION 2515 if (test_opt(sbi, FAULT_INJECTION)) { 2516 seq_printf(seq, ",fault_injection=%u", 2517 F2FS_OPTION(sbi).fault_info.inject_rate); 2518 seq_printf(seq, ",fault_type=%u", 2519 F2FS_OPTION(sbi).fault_info.inject_type); 2520 } 2521 #endif 2522 #ifdef CONFIG_QUOTA 2523 if (test_opt(sbi, QUOTA)) 2524 seq_puts(seq, ",quota"); 2525 if (test_opt(sbi, USRQUOTA)) 2526 seq_puts(seq, ",usrquota"); 2527 if (test_opt(sbi, GRPQUOTA)) 2528 seq_puts(seq, ",grpquota"); 2529 if (test_opt(sbi, PRJQUOTA)) 2530 seq_puts(seq, ",prjquota"); 2531 #endif 2532 f2fs_show_quota_options(seq, sbi->sb); 2533 2534 fscrypt_show_test_dummy_encryption(seq, ',', sbi->sb); 2535 2536 if (sbi->sb->s_flags & SB_INLINECRYPT) 2537 seq_puts(seq, ",inlinecrypt"); 2538 2539 if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_DEFAULT) 2540 seq_printf(seq, ",alloc_mode=%s", "default"); 2541 else if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE) 2542 seq_printf(seq, ",alloc_mode=%s", "reuse"); 2543 2544 if (test_opt(sbi, DISABLE_CHECKPOINT)) 2545 seq_printf(seq, ",checkpoint=disable:%u", 2546 F2FS_OPTION(sbi).unusable_cap); 2547 if (test_opt(sbi, MERGE_CHECKPOINT)) 2548 seq_puts(seq, ",checkpoint_merge"); 2549 else 2550 seq_puts(seq, ",nocheckpoint_merge"); 2551 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_POSIX) 2552 seq_printf(seq, ",fsync_mode=%s", "posix"); 2553 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) 2554 seq_printf(seq, ",fsync_mode=%s", "strict"); 2555 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_NOBARRIER) 2556 seq_printf(seq, ",fsync_mode=%s", "nobarrier"); 2557 2558 #ifdef CONFIG_F2FS_FS_COMPRESSION 2559 f2fs_show_compress_options(seq, sbi->sb); 2560 #endif 2561 2562 if (test_opt(sbi, ATGC)) 2563 seq_puts(seq, ",atgc"); 2564 2565 if (F2FS_OPTION(sbi).memory_mode == MEMORY_MODE_NORMAL) 2566 seq_printf(seq, ",memory=%s", "normal"); 2567 else if (F2FS_OPTION(sbi).memory_mode == MEMORY_MODE_LOW) 2568 seq_printf(seq, ",memory=%s", "low"); 2569 2570 if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_READONLY) 2571 seq_printf(seq, ",errors=%s", "remount-ro"); 2572 else if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_CONTINUE) 2573 seq_printf(seq, ",errors=%s", "continue"); 2574 else if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_PANIC) 2575 seq_printf(seq, ",errors=%s", "panic"); 2576 2577 if (test_opt(sbi, NAT_BITS)) 2578 seq_puts(seq, ",nat_bits"); 2579 2580 if (F2FS_OPTION(sbi).lookup_mode == LOOKUP_PERF) 2581 seq_show_option(seq, "lookup_mode", "perf"); 2582 else if (F2FS_OPTION(sbi).lookup_mode == LOOKUP_COMPAT) 2583 seq_show_option(seq, "lookup_mode", "compat"); 2584 else if (F2FS_OPTION(sbi).lookup_mode == LOOKUP_AUTO) 2585 seq_show_option(seq, "lookup_mode", "auto"); 2586 2587 if (F2FS_OPTION(sbi).resizable_tail_secno) 2588 seq_printf(seq, ",resizable_tail_secno=%u", 2589 F2FS_OPTION(sbi).resizable_tail_secno); 2590 2591 return 0; 2592 } 2593 2594 static void default_options(struct f2fs_sb_info *sbi, bool remount) 2595 { 2596 /* init some FS parameters */ 2597 if (!remount) { 2598 set_opt(sbi, READ_EXTENT_CACHE); 2599 clear_opt(sbi, DISABLE_CHECKPOINT); 2600 2601 if (f2fs_hw_support_discard(sbi) || f2fs_hw_should_discard(sbi)) 2602 set_opt(sbi, DISCARD); 2603 2604 if (f2fs_sb_has_blkzoned(sbi)) 2605 F2FS_OPTION(sbi).discard_unit = DISCARD_UNIT_SECTION; 2606 else 2607 F2FS_OPTION(sbi).discard_unit = DISCARD_UNIT_BLOCK; 2608 } 2609 2610 if (f2fs_sb_has_readonly(sbi)) 2611 F2FS_OPTION(sbi).active_logs = NR_CURSEG_RO_TYPE; 2612 else 2613 F2FS_OPTION(sbi).active_logs = NR_CURSEG_PERSIST_TYPE; 2614 2615 F2FS_OPTION(sbi).inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS; 2616 if (le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_main) <= 2617 SMALL_VOLUME_SEGMENTS) 2618 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE; 2619 else 2620 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT; 2621 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX; 2622 F2FS_OPTION(sbi).s_resuid = make_kuid(&init_user_ns, F2FS_DEF_RESUID); 2623 F2FS_OPTION(sbi).s_resgid = make_kgid(&init_user_ns, F2FS_DEF_RESGID); 2624 if (f2fs_sb_has_compression(sbi)) { 2625 F2FS_OPTION(sbi).compress_algorithm = COMPRESS_LZ4; 2626 F2FS_OPTION(sbi).compress_log_size = MIN_COMPRESS_LOG_SIZE; 2627 F2FS_OPTION(sbi).compress_ext_cnt = 0; 2628 F2FS_OPTION(sbi).compress_mode = COMPR_MODE_FS; 2629 } 2630 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_ON; 2631 F2FS_OPTION(sbi).memory_mode = MEMORY_MODE_NORMAL; 2632 F2FS_OPTION(sbi).errors = MOUNT_ERRORS_CONTINUE; 2633 F2FS_OPTION(sbi).resizable_tail_secno = 0; 2634 2635 set_opt(sbi, INLINE_XATTR); 2636 set_opt(sbi, INLINE_DATA); 2637 set_opt(sbi, INLINE_DENTRY); 2638 set_opt(sbi, MERGE_CHECKPOINT); 2639 set_opt(sbi, LAZYTIME); 2640 F2FS_OPTION(sbi).unusable_cap = 0; 2641 if (!f2fs_is_readonly(sbi)) 2642 set_opt(sbi, FLUSH_MERGE); 2643 if (f2fs_sb_has_blkzoned(sbi)) 2644 F2FS_OPTION(sbi).fs_mode = FS_MODE_LFS; 2645 else 2646 F2FS_OPTION(sbi).fs_mode = FS_MODE_ADAPTIVE; 2647 2648 #ifdef CONFIG_F2FS_FS_XATTR 2649 set_opt(sbi, XATTR_USER); 2650 #endif 2651 #ifdef CONFIG_F2FS_FS_POSIX_ACL 2652 set_opt(sbi, POSIX_ACL); 2653 #endif 2654 2655 f2fs_build_fault_attr(sbi, 0, 0, FAULT_ALL); 2656 2657 F2FS_OPTION(sbi).lookup_mode = LOOKUP_PERF; 2658 } 2659 2660 #ifdef CONFIG_QUOTA 2661 static int f2fs_enable_quotas(struct super_block *sb); 2662 #endif 2663 2664 static int f2fs_disable_checkpoint(struct f2fs_sb_info *sbi) 2665 { 2666 unsigned int s_flags = sbi->sb->s_flags; 2667 struct cp_control cpc; 2668 struct f2fs_lock_context lc; 2669 unsigned int gc_mode = sbi->gc_mode; 2670 int err = 0; 2671 int ret; 2672 block_t unusable; 2673 2674 if (s_flags & SB_RDONLY) { 2675 f2fs_err(sbi, "checkpoint=disable on readonly fs"); 2676 return -EINVAL; 2677 } 2678 sbi->sb->s_flags |= SB_ACTIVE; 2679 2680 /* check if we need more GC first */ 2681 unusable = f2fs_get_unusable_blocks(sbi); 2682 2683 f2fs_info(sbi, "%s starts, unusable: %u", __func__, unusable); 2684 2685 if (!f2fs_disable_cp_again(sbi, unusable)) 2686 goto skip_gc; 2687 2688 f2fs_update_time(sbi, DISABLE_TIME); 2689 2690 sbi->gc_mode = GC_URGENT_HIGH; 2691 2692 f2fs_info(sbi, "%s: run f2fs_gc() to migrate blocks", __func__); 2693 2694 while (!f2fs_time_over(sbi, DISABLE_TIME)) { 2695 struct f2fs_gc_control gc_control = { 2696 .victim_segno = NULL_SEGNO, 2697 .init_gc_type = FG_GC, 2698 .should_migrate_blocks = false, 2699 .err_gc_skipped = true, 2700 .no_bg_gc = true, 2701 .nr_free_secs = 1 }; 2702 2703 f2fs_down_write_trace(&sbi->gc_lock, &gc_control.lc); 2704 stat_inc_gc_call_count(sbi, FOREGROUND); 2705 err = f2fs_gc(sbi, &gc_control); 2706 if (err == -ENODATA) { 2707 err = 0; 2708 break; 2709 } 2710 if (err && err != -EAGAIN) 2711 break; 2712 } 2713 2714 f2fs_info(sbi, "%s: call sync_filesystem() to persist meta: %lld, node: %lld, data: %lld", 2715 __func__, 2716 get_nr_caches(sbi, F2FS_DIRTY_META), 2717 get_nr_caches(sbi, F2FS_DIRTY_NODES), 2718 get_nr_caches(sbi, F2FS_DIRTY_DATA)); 2719 2720 ret = sync_filesystem(sbi->sb); 2721 if (ret || err) { 2722 err = ret ? ret : err; 2723 goto restore_flag; 2724 } 2725 2726 unusable = f2fs_get_unusable_blocks(sbi); 2727 if (f2fs_disable_cp_again(sbi, unusable)) { 2728 err = -EAGAIN; 2729 goto restore_flag; 2730 } 2731 2732 skip_gc: 2733 f2fs_info(sbi, "%s: call f2fs_write_checkpoint(), meta: %lld, node: %lld, data: %lld", 2734 __func__, 2735 get_nr_caches(sbi, F2FS_DIRTY_META), 2736 get_nr_caches(sbi, F2FS_DIRTY_NODES), 2737 get_nr_caches(sbi, F2FS_DIRTY_DATA)); 2738 2739 f2fs_down_write_trace(&sbi->gc_lock, &lc); 2740 cpc.reason = CP_PAUSE; 2741 set_sbi_flag(sbi, SBI_CP_DISABLED); 2742 stat_inc_cp_call_count(sbi, TOTAL_CALL); 2743 err = f2fs_write_checkpoint(sbi, &cpc); 2744 if (err) 2745 goto out_unlock; 2746 2747 spin_lock(&sbi->stat_lock); 2748 sbi->unusable_block_count = unusable; 2749 spin_unlock(&sbi->stat_lock); 2750 2751 out_unlock: 2752 f2fs_up_write_trace(&sbi->gc_lock, &lc); 2753 restore_flag: 2754 sbi->gc_mode = gc_mode; 2755 sbi->sb->s_flags = s_flags; /* Restore SB_RDONLY status */ 2756 f2fs_info(sbi, "%s finishes, err:%d", __func__, err); 2757 return err; 2758 } 2759 2760 static int f2fs_enable_checkpoint(struct f2fs_sb_info *sbi) 2761 { 2762 int retry = MAX_FLUSH_RETRY_COUNT; 2763 long long start, writeback, end; 2764 int ret; 2765 struct f2fs_lock_context lc; 2766 long long skipped_write, dirty_data; 2767 2768 f2fs_info(sbi, "f2fs_enable_checkpoint() starts, meta: %lld, node: %lld, data: %lld", 2769 get_nr_caches(sbi, F2FS_DIRTY_META), 2770 get_nr_caches(sbi, F2FS_DIRTY_NODES), 2771 get_nr_caches(sbi, F2FS_DIRTY_DATA)); 2772 2773 start = ktime_get(); 2774 2775 set_sbi_flag(sbi, SBI_ENABLE_CHECKPOINT); 2776 2777 /* we should flush all the data to keep data consistency */ 2778 do { 2779 skipped_write = get_nr_caches(sbi, F2FS_SKIPPED_WRITE); 2780 dirty_data = get_nr_caches(sbi, F2FS_DIRTY_DATA); 2781 2782 sync_inodes_sb(sbi->sb); 2783 f2fs_io_schedule_timeout(DEFAULT_SCHEDULE_TIMEOUT); 2784 2785 f2fs_info(sbi, "sync_inode_sb done, dirty_data: %lld, %lld, " 2786 "skipped write: %lld, %lld, retry: %d", 2787 get_nr_caches(sbi, F2FS_DIRTY_DATA), 2788 dirty_data, 2789 get_nr_caches(sbi, F2FS_SKIPPED_WRITE), 2790 skipped_write, retry); 2791 2792 /* 2793 * sync_inodes_sb() has retry logic, so let's check dirty_data 2794 * in prior to skipped_write in case there is no dirty data. 2795 */ 2796 if (!get_nr_caches(sbi, F2FS_DIRTY_DATA)) 2797 break; 2798 if (get_nr_caches(sbi, F2FS_SKIPPED_WRITE) == skipped_write) 2799 break; 2800 } while (retry--); 2801 2802 clear_sbi_flag(sbi, SBI_ENABLE_CHECKPOINT); 2803 2804 writeback = ktime_get(); 2805 2806 if (unlikely(get_nr_caches(sbi, F2FS_DIRTY_DATA) || 2807 get_nr_caches(sbi, F2FS_SKIPPED_WRITE))) 2808 f2fs_warn(sbi, "checkpoint=enable unwritten data: %lld, skipped data: %lld, retry: %d", 2809 get_nr_caches(sbi, F2FS_DIRTY_DATA), 2810 get_nr_caches(sbi, F2FS_SKIPPED_WRITE), retry); 2811 2812 if (get_nr_caches(sbi, F2FS_SKIPPED_WRITE)) 2813 atomic_set(&sbi->nr_caches[F2FS_SKIPPED_WRITE], 0); 2814 2815 f2fs_down_write_trace(&sbi->gc_lock, &lc); 2816 f2fs_dirty_to_prefree(sbi); 2817 2818 clear_sbi_flag(sbi, SBI_CP_DISABLED); 2819 set_sbi_flag(sbi, SBI_IS_DIRTY); 2820 f2fs_up_write_trace(&sbi->gc_lock, &lc); 2821 2822 ret = f2fs_sync_fs(sbi->sb, 1); 2823 if (ret) 2824 f2fs_err(sbi, "%s sync_fs failed, ret: %d", __func__, ret); 2825 2826 /* Let's ensure there's no pending checkpoint anymore */ 2827 f2fs_flush_ckpt_thread(sbi); 2828 2829 end = ktime_get(); 2830 2831 f2fs_info(sbi, "f2fs_enable_checkpoint() finishes, writeback:%llu, sync:%llu", 2832 ktime_ms_delta(writeback, start), 2833 ktime_ms_delta(end, writeback)); 2834 return ret; 2835 } 2836 2837 static int __f2fs_remount(struct fs_context *fc, struct super_block *sb) 2838 { 2839 struct f2fs_sb_info *sbi = F2FS_SB(sb); 2840 struct f2fs_mount_info org_mount_opt; 2841 unsigned long old_sb_flags; 2842 unsigned int flags = fc->sb_flags; 2843 int err; 2844 bool need_restart_gc = false, need_stop_gc = false; 2845 bool need_restart_wb = false, need_stop_wb = false; 2846 bool need_restart_flush = false, need_stop_flush = false; 2847 bool need_restart_discard = false, need_stop_discard = false; 2848 bool need_enable_checkpoint = false, need_disable_checkpoint = false; 2849 bool no_read_extent_cache = !test_opt(sbi, READ_EXTENT_CACHE); 2850 bool no_age_extent_cache = !test_opt(sbi, AGE_EXTENT_CACHE); 2851 bool enable_checkpoint = !test_opt(sbi, DISABLE_CHECKPOINT); 2852 bool no_atgc = !test_opt(sbi, ATGC); 2853 bool no_discard = !test_opt(sbi, DISCARD); 2854 bool no_compress_cache = !test_opt(sbi, COMPRESS_CACHE); 2855 bool block_unit_discard = f2fs_block_unit_discard(sbi); 2856 bool no_nat_bits = !test_opt(sbi, NAT_BITS); 2857 #ifdef CONFIG_QUOTA 2858 int i, j; 2859 #endif 2860 2861 /* 2862 * Save the old mount options in case we 2863 * need to restore them. 2864 */ 2865 org_mount_opt = sbi->mount_opt; 2866 old_sb_flags = sb->s_flags; 2867 2868 sbi->umount_lock_holder = current; 2869 2870 #ifdef CONFIG_QUOTA 2871 org_mount_opt.s_jquota_fmt = F2FS_OPTION(sbi).s_jquota_fmt; 2872 for (i = 0; i < MAXQUOTAS; i++) { 2873 if (F2FS_OPTION(sbi).s_qf_names[i]) { 2874 org_mount_opt.s_qf_names[i] = 2875 kstrdup(F2FS_OPTION(sbi).s_qf_names[i], 2876 GFP_KERNEL); 2877 if (!org_mount_opt.s_qf_names[i]) { 2878 for (j = 0; j < i; j++) 2879 kfree(org_mount_opt.s_qf_names[j]); 2880 err = -ENOMEM; 2881 goto restore_holder; 2882 } 2883 } else { 2884 org_mount_opt.s_qf_names[i] = NULL; 2885 } 2886 } 2887 #endif 2888 2889 /* recover superblocks we couldn't write due to previous RO mount */ 2890 if (!(flags & SB_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) { 2891 err = f2fs_commit_super(sbi, false); 2892 f2fs_info(sbi, "Try to recover all the superblocks, ret: %d", 2893 err); 2894 if (!err) 2895 clear_sbi_flag(sbi, SBI_NEED_SB_WRITE); 2896 } 2897 2898 default_options(sbi, true); 2899 2900 err = f2fs_check_opt_consistency(fc, sb); 2901 if (err) 2902 goto restore_opts; 2903 2904 f2fs_apply_options(fc, sb); 2905 2906 err = f2fs_sanity_check_options(sbi, true); 2907 if (err) 2908 goto restore_opts; 2909 2910 /* flush outstanding errors before changing fs state */ 2911 flush_work(&sbi->s_error_work); 2912 2913 /* 2914 * Previous and new state of filesystem is RO, 2915 * so skip checking GC and FLUSH_MERGE conditions. 2916 */ 2917 if (f2fs_readonly(sb) && (flags & SB_RDONLY)) 2918 goto skip; 2919 2920 if (f2fs_dev_is_readonly(sbi) && !(flags & SB_RDONLY)) { 2921 err = -EROFS; 2922 goto restore_opts; 2923 } 2924 2925 #ifdef CONFIG_QUOTA 2926 if (!f2fs_readonly(sb) && (flags & SB_RDONLY)) { 2927 err = dquot_suspend(sb, -1); 2928 if (err < 0) 2929 goto restore_opts; 2930 } else if (f2fs_readonly(sb) && !(flags & SB_RDONLY)) { 2931 /* dquot_resume needs RW */ 2932 sb->s_flags &= ~SB_RDONLY; 2933 if (sb_any_quota_suspended(sb)) { 2934 dquot_resume(sb, -1); 2935 } else if (f2fs_sb_has_quota_ino(sbi)) { 2936 err = f2fs_enable_quotas(sb); 2937 if (err) 2938 goto restore_opts; 2939 } 2940 } 2941 #endif 2942 /* disallow enable atgc dynamically */ 2943 if (no_atgc == !!test_opt(sbi, ATGC)) { 2944 err = -EINVAL; 2945 f2fs_warn(sbi, "switch atgc option is not allowed"); 2946 goto restore_opts; 2947 } 2948 2949 /* disallow enable/disable extent_cache dynamically */ 2950 if (no_read_extent_cache == !!test_opt(sbi, READ_EXTENT_CACHE)) { 2951 err = -EINVAL; 2952 f2fs_warn(sbi, "switch extent_cache option is not allowed"); 2953 goto restore_opts; 2954 } 2955 /* disallow enable/disable age extent_cache dynamically */ 2956 if (no_age_extent_cache == !!test_opt(sbi, AGE_EXTENT_CACHE)) { 2957 err = -EINVAL; 2958 f2fs_warn(sbi, "switch age_extent_cache option is not allowed"); 2959 goto restore_opts; 2960 } 2961 2962 if (no_compress_cache == !!test_opt(sbi, COMPRESS_CACHE)) { 2963 err = -EINVAL; 2964 f2fs_warn(sbi, "switch compress_cache option is not allowed"); 2965 goto restore_opts; 2966 } 2967 2968 if (block_unit_discard != f2fs_block_unit_discard(sbi)) { 2969 err = -EINVAL; 2970 f2fs_warn(sbi, "switch discard_unit option is not allowed"); 2971 goto restore_opts; 2972 } 2973 2974 if (no_nat_bits == !!test_opt(sbi, NAT_BITS)) { 2975 err = -EINVAL; 2976 f2fs_warn(sbi, "switch nat_bits option is not allowed"); 2977 goto restore_opts; 2978 } 2979 2980 if ((flags & SB_RDONLY) && test_opt(sbi, DISABLE_CHECKPOINT)) { 2981 err = -EINVAL; 2982 f2fs_warn(sbi, "disabling checkpoint not compatible with read-only"); 2983 goto restore_opts; 2984 } 2985 2986 /* 2987 * We stop the GC thread if FS is mounted as RO 2988 * or if background_gc = off is passed in mount 2989 * option. Also sync the filesystem. 2990 */ 2991 if ((flags & SB_RDONLY) || 2992 (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_OFF && 2993 !test_opt(sbi, GC_MERGE))) { 2994 if (sbi->gc_thread.f2fs_gc_task) { 2995 f2fs_stop_gc_thread(sbi); 2996 need_restart_gc = true; 2997 } 2998 } else if (!sbi->gc_thread.f2fs_gc_task) { 2999 err = f2fs_start_gc_thread(sbi); 3000 if (err) 3001 goto restore_opts; 3002 need_stop_gc = true; 3003 } 3004 3005 if (flags & SB_RDONLY) { 3006 if (sbi->cache_thread.cache_wb_task) { 3007 f2fs_stop_cache_wb_thread(sbi); 3008 need_restart_wb = true; 3009 } 3010 } else if (!sbi->cache_thread.cache_wb_task) { 3011 err = f2fs_start_cache_wb_thread(sbi); 3012 if (err) 3013 goto restore_gc; 3014 need_stop_wb = true; 3015 } 3016 3017 if (flags & SB_RDONLY) { 3018 sync_inodes_sb(sb); 3019 3020 set_sbi_flag(sbi, SBI_IS_DIRTY); 3021 set_sbi_flag(sbi, SBI_IS_CLOSE); 3022 err = f2fs_sync_fs(sb, 1); 3023 if (err) 3024 goto restore_wb; 3025 clear_sbi_flag(sbi, SBI_IS_CLOSE); 3026 } 3027 3028 /* 3029 * We stop issue flush thread if FS is mounted as RO 3030 * or if flush_merge is not passed in mount option. 3031 */ 3032 if ((flags & SB_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) { 3033 clear_opt(sbi, FLUSH_MERGE); 3034 f2fs_destroy_flush_cmd_control(sbi, false); 3035 need_restart_flush = true; 3036 } else { 3037 err = f2fs_create_flush_cmd_control(sbi); 3038 if (err) 3039 goto restore_wb; 3040 need_stop_flush = true; 3041 } 3042 3043 if (no_discard == !!test_opt(sbi, DISCARD)) { 3044 if (test_opt(sbi, DISCARD)) { 3045 err = f2fs_start_discard_thread(sbi); 3046 if (err) 3047 goto restore_flush; 3048 need_stop_discard = true; 3049 } else { 3050 f2fs_stop_discard_thread(sbi); 3051 /* 3052 * f2fs_ioc_fitrim() won't race w/ "remount ro" 3053 * so it's safe to check discard_cmd_cnt in 3054 * f2fs_issue_discard_timeout(). 3055 */ 3056 f2fs_issue_discard_timeout(sbi, flags & SB_RDONLY); 3057 need_restart_discard = true; 3058 } 3059 } 3060 3061 adjust_unusable_cap_perc(sbi); 3062 if (enable_checkpoint == !!test_opt(sbi, DISABLE_CHECKPOINT)) { 3063 if (test_opt(sbi, DISABLE_CHECKPOINT)) { 3064 err = f2fs_disable_checkpoint(sbi); 3065 if (err) 3066 goto restore_discard; 3067 need_enable_checkpoint = true; 3068 } else { 3069 err = f2fs_enable_checkpoint(sbi); 3070 if (err) 3071 goto restore_discard; 3072 need_disable_checkpoint = true; 3073 } 3074 } 3075 3076 /* 3077 * Place this routine at the end, since a new checkpoint would be 3078 * triggered while remount and we need to take care of it before 3079 * returning from remount. 3080 */ 3081 if ((flags & SB_RDONLY) || test_opt(sbi, DISABLE_CHECKPOINT) || 3082 !test_opt(sbi, MERGE_CHECKPOINT)) { 3083 f2fs_stop_ckpt_thread(sbi); 3084 } else { 3085 /* Flush if the previous checkpoint, if exists. */ 3086 f2fs_flush_ckpt_thread(sbi); 3087 3088 err = f2fs_start_ckpt_thread(sbi); 3089 if (err) { 3090 f2fs_err(sbi, 3091 "Failed to start F2FS issue_checkpoint_thread (%d)", 3092 err); 3093 goto restore_checkpoint; 3094 } 3095 } 3096 3097 skip: 3098 #ifdef CONFIG_QUOTA 3099 /* Release old quota file names */ 3100 for (i = 0; i < MAXQUOTAS; i++) 3101 kfree(org_mount_opt.s_qf_names[i]); 3102 #endif 3103 /* Update the POSIXACL Flag */ 3104 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) | 3105 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0); 3106 3107 f2fs_adjust_pinned_area_boundary(sbi); 3108 limit_reserve_root(sbi); 3109 fc->sb_flags = (flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME); 3110 3111 sbi->umount_lock_holder = NULL; 3112 return 0; 3113 restore_checkpoint: 3114 if (need_enable_checkpoint) { 3115 if (f2fs_enable_checkpoint(sbi)) 3116 f2fs_warn(sbi, "checkpoint has not been enabled"); 3117 } else if (need_disable_checkpoint) { 3118 if (f2fs_disable_checkpoint(sbi)) 3119 f2fs_warn(sbi, "checkpoint has not been disabled"); 3120 } 3121 restore_discard: 3122 if (need_restart_discard) { 3123 if (f2fs_start_discard_thread(sbi)) 3124 f2fs_warn(sbi, "discard has been stopped"); 3125 } else if (need_stop_discard) { 3126 f2fs_stop_discard_thread(sbi); 3127 } 3128 restore_flush: 3129 if (need_restart_flush) { 3130 if (f2fs_create_flush_cmd_control(sbi)) 3131 f2fs_warn(sbi, "background flush thread has stopped"); 3132 } else if (need_stop_flush) { 3133 clear_opt(sbi, FLUSH_MERGE); 3134 f2fs_destroy_flush_cmd_control(sbi, false); 3135 } 3136 restore_wb: 3137 if (need_restart_wb) { 3138 if (f2fs_start_cache_wb_thread(sbi)) 3139 f2fs_warn(sbi, "background cache writeback thread has stopped"); 3140 } else if (need_stop_wb) { 3141 f2fs_stop_cache_wb_thread(sbi); 3142 } 3143 restore_gc: 3144 if (need_restart_gc) { 3145 if (f2fs_start_gc_thread(sbi)) 3146 f2fs_warn(sbi, "background gc thread has stopped"); 3147 } else if (need_stop_gc) { 3148 f2fs_stop_gc_thread(sbi); 3149 } 3150 restore_opts: 3151 #ifdef CONFIG_QUOTA 3152 F2FS_OPTION(sbi).s_jquota_fmt = org_mount_opt.s_jquota_fmt; 3153 for (i = 0; i < MAXQUOTAS; i++) { 3154 kfree(F2FS_OPTION(sbi).s_qf_names[i]); 3155 F2FS_OPTION(sbi).s_qf_names[i] = org_mount_opt.s_qf_names[i]; 3156 } 3157 #endif 3158 sbi->mount_opt = org_mount_opt; 3159 sb->s_flags = old_sb_flags; 3160 3161 #ifdef CONFIG_QUOTA 3162 restore_holder: 3163 #endif 3164 sbi->umount_lock_holder = NULL; 3165 return err; 3166 } 3167 3168 static void f2fs_shutdown(struct super_block *sb) 3169 { 3170 f2fs_do_shutdown(F2FS_SB(sb), F2FS_GOING_DOWN_NOSYNC, false, false); 3171 } 3172 3173 #ifdef CONFIG_QUOTA 3174 static bool f2fs_need_recovery(struct f2fs_sb_info *sbi) 3175 { 3176 /* need to recovery orphan */ 3177 if (is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG)) 3178 return true; 3179 /* need to recovery data */ 3180 if (test_opt(sbi, DISABLE_ROLL_FORWARD)) 3181 return false; 3182 if (test_opt(sbi, NORECOVERY)) 3183 return false; 3184 return !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG); 3185 } 3186 3187 static bool f2fs_recover_quota_begin(struct f2fs_sb_info *sbi) 3188 { 3189 bool readonly = f2fs_readonly(sbi->sb); 3190 3191 if (!f2fs_need_recovery(sbi)) 3192 return false; 3193 3194 /* it doesn't need to check f2fs_sb_has_readonly() */ 3195 if (f2fs_hw_is_readonly(sbi)) 3196 return false; 3197 3198 if (readonly) { 3199 sbi->sb->s_flags &= ~SB_RDONLY; 3200 set_sbi_flag(sbi, SBI_IS_WRITABLE); 3201 } 3202 3203 /* 3204 * Turn on quotas which were not enabled for read-only mounts if 3205 * filesystem has quota feature, so that they are updated correctly. 3206 */ 3207 return f2fs_enable_quota_files(sbi, readonly); 3208 } 3209 3210 static void f2fs_recover_quota_end(struct f2fs_sb_info *sbi, 3211 bool quota_enabled) 3212 { 3213 if (quota_enabled) 3214 f2fs_quota_off_umount(sbi->sb); 3215 3216 if (is_sbi_flag_set(sbi, SBI_IS_WRITABLE)) { 3217 clear_sbi_flag(sbi, SBI_IS_WRITABLE); 3218 sbi->sb->s_flags |= SB_RDONLY; 3219 } 3220 } 3221 3222 /* Read data from quotafile */ 3223 static ssize_t f2fs_quota_read(struct super_block *sb, int type, char *data, 3224 size_t len, loff_t off) 3225 { 3226 struct inode *inode = sb_dqopt(sb)->files[type]; 3227 struct address_space *mapping = inode->i_mapping; 3228 int tocopy; 3229 size_t toread; 3230 loff_t i_size = i_size_read(inode); 3231 3232 if (off > i_size) 3233 return 0; 3234 3235 if (off + len > i_size) 3236 len = i_size - off; 3237 toread = len; 3238 while (toread > 0) { 3239 struct folio *folio; 3240 size_t offset; 3241 3242 repeat: 3243 folio = mapping_read_folio_gfp(mapping, off >> PAGE_SHIFT, 3244 GFP_KERNEL); 3245 if (IS_ERR(folio)) { 3246 if (PTR_ERR(folio) == -ENOMEM) { 3247 memalloc_retry_wait(GFP_NOFS); 3248 goto repeat; 3249 } 3250 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR); 3251 return PTR_ERR(folio); 3252 } 3253 offset = offset_in_folio(folio, off); 3254 tocopy = min(folio_size(folio) - offset, toread); 3255 3256 folio_lock(folio); 3257 3258 if (unlikely(folio->mapping != mapping)) { 3259 f2fs_folio_put(folio, true); 3260 goto repeat; 3261 } 3262 3263 /* 3264 * should never happen, just leave f2fs_bug_on() here to catch 3265 * any potential bug. 3266 */ 3267 f2fs_bug_on(F2FS_SB(sb), !folio_test_uptodate(folio)); 3268 3269 memcpy_from_folio(data, folio, offset, tocopy); 3270 f2fs_folio_put(folio, true); 3271 3272 toread -= tocopy; 3273 data += tocopy; 3274 off += tocopy; 3275 } 3276 return len; 3277 } 3278 3279 /* Write to quotafile */ 3280 static ssize_t f2fs_quota_write(struct super_block *sb, int type, 3281 const char *data, size_t len, loff_t off) 3282 { 3283 struct inode *inode = sb_dqopt(sb)->files[type]; 3284 struct address_space *mapping = inode->i_mapping; 3285 const struct address_space_operations *a_ops = mapping->a_ops; 3286 int offset = off & (sb->s_blocksize - 1); 3287 size_t towrite = len; 3288 struct folio *folio; 3289 void *fsdata = NULL; 3290 int err = 0; 3291 int tocopy; 3292 3293 while (towrite > 0) { 3294 tocopy = min_t(unsigned long, sb->s_blocksize - offset, 3295 towrite); 3296 retry: 3297 err = a_ops->write_begin(NULL, mapping, off, tocopy, 3298 &folio, &fsdata); 3299 if (unlikely(err)) { 3300 if (err == -ENOMEM) { 3301 memalloc_retry_wait(GFP_NOFS); 3302 goto retry; 3303 } 3304 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR); 3305 break; 3306 } 3307 3308 memcpy_to_folio(folio, offset_in_folio(folio, off), data, tocopy); 3309 3310 a_ops->write_end(NULL, mapping, off, tocopy, tocopy, 3311 folio, fsdata); 3312 offset = 0; 3313 towrite -= tocopy; 3314 off += tocopy; 3315 data += tocopy; 3316 cond_resched(); 3317 } 3318 3319 if (len == towrite) 3320 return err; 3321 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); 3322 f2fs_mark_inode_dirty_sync(inode, false); 3323 return len - towrite; 3324 } 3325 3326 int f2fs_dquot_initialize(struct inode *inode) 3327 { 3328 if (time_to_inject(F2FS_I_SB(inode), FAULT_DQUOT_INIT)) 3329 return -ESRCH; 3330 3331 return dquot_initialize(inode); 3332 } 3333 3334 static struct dquot __rcu **f2fs_get_dquots(struct inode *inode) 3335 { 3336 return F2FS_I(inode)->i_dquot; 3337 } 3338 3339 static qsize_t *f2fs_get_reserved_space(struct inode *inode) 3340 { 3341 return &F2FS_I(inode)->i_reserved_quota; 3342 } 3343 3344 /* 3345 * Quota files only grow and there are at most MAXQUOTAS of them, so there is 3346 * no point in keeping them inline. Writeback of quota files does not handle 3347 * inline data and would drop what was written to them. 3348 */ 3349 static int f2fs_quota_convert_inline(struct inode *inode) 3350 { 3351 int err; 3352 3353 if (!f2fs_has_inline_data(inode)) 3354 return 0; 3355 3356 inode_lock(inode); 3357 err = f2fs_convert_inline_inode(inode); 3358 inode_unlock(inode); 3359 return err; 3360 } 3361 3362 static int f2fs_quota_on_mount(struct f2fs_sb_info *sbi, int type) 3363 { 3364 int err; 3365 3366 if (is_set_ckpt_flags(sbi, CP_QUOTA_NEED_FSCK_FLAG)) { 3367 f2fs_err(sbi, "quota sysfile may be corrupted, skip loading it"); 3368 return 0; 3369 } 3370 3371 err = dquot_quota_on_mount(sbi->sb, F2FS_OPTION(sbi).s_qf_names[type], 3372 F2FS_OPTION(sbi).s_jquota_fmt, type); 3373 if (err) 3374 return err; 3375 3376 err = f2fs_quota_convert_inline(sb_dqopt(sbi->sb)->files[type]); 3377 if (err) 3378 dquot_quota_off(sbi->sb, type); 3379 return err; 3380 } 3381 3382 int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly) 3383 { 3384 int enabled = 0; 3385 int i, err; 3386 3387 if (f2fs_sb_has_quota_ino(sbi) && rdonly) { 3388 err = f2fs_enable_quotas(sbi->sb); 3389 if (err) { 3390 f2fs_err(sbi, "Cannot turn on quota_ino: %d", err); 3391 return 0; 3392 } 3393 return 1; 3394 } 3395 3396 for (i = 0; i < MAXQUOTAS; i++) { 3397 if (F2FS_OPTION(sbi).s_qf_names[i]) { 3398 err = f2fs_quota_on_mount(sbi, i); 3399 if (!err) { 3400 enabled = 1; 3401 continue; 3402 } 3403 f2fs_err(sbi, "Cannot turn on quotas: %d on %d", 3404 err, i); 3405 } 3406 } 3407 return enabled; 3408 } 3409 3410 static int f2fs_quota_enable(struct super_block *sb, int type, int format_id, 3411 unsigned int flags, unsigned long qf_inum) 3412 { 3413 struct inode *qf_inode; 3414 unsigned long qf_flag = F2FS_QUOTA_DEFAULT_FL; 3415 int err; 3416 3417 qf_inode = f2fs_iget(sb, qf_inum); 3418 if (IS_ERR(qf_inode)) { 3419 f2fs_err(F2FS_SB(sb), "Bad quota inode %u:%lu", type, qf_inum); 3420 return PTR_ERR(qf_inode); 3421 } 3422 3423 /* Don't account quota for quota files to avoid recursion */ 3424 inode_lock(qf_inode); 3425 qf_inode->i_flags |= S_NOQUOTA; 3426 3427 if ((F2FS_I(qf_inode)->i_flags & qf_flag) != qf_flag) { 3428 F2FS_I(qf_inode)->i_flags |= qf_flag; 3429 f2fs_set_inode_flags(qf_inode); 3430 } 3431 inode_unlock(qf_inode); 3432 3433 err = dquot_load_quota_inode(qf_inode, type, format_id, flags); 3434 iput(qf_inode); 3435 return err; 3436 } 3437 3438 static int f2fs_enable_quotas(struct super_block *sb) 3439 { 3440 struct f2fs_sb_info *sbi = F2FS_SB(sb); 3441 int type, err = 0; 3442 unsigned long qf_inum; 3443 bool quota_mopt[MAXQUOTAS] = { 3444 test_opt(sbi, USRQUOTA), 3445 test_opt(sbi, GRPQUOTA), 3446 test_opt(sbi, PRJQUOTA), 3447 }; 3448 3449 if (is_set_ckpt_flags(sbi, CP_QUOTA_NEED_FSCK_FLAG)) { 3450 f2fs_err(sbi, "quota file may be corrupted, skip loading it"); 3451 return 0; 3452 } 3453 3454 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE; 3455 3456 for (type = 0; type < MAXQUOTAS; type++) { 3457 qf_inum = f2fs_qf_ino(sb, type); 3458 if (qf_inum) { 3459 err = f2fs_quota_enable(sb, type, QFMT_VFS_V1, 3460 DQUOT_USAGE_ENABLED | 3461 (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0), qf_inum); 3462 if (err) { 3463 f2fs_err(sbi, "Failed to enable quota tracking (type=%d, err=%d). Please run fsck to fix.", 3464 type, err); 3465 for (type--; type >= 0; type--) 3466 dquot_quota_off(sb, type); 3467 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR); 3468 return err; 3469 } 3470 } 3471 } 3472 return 0; 3473 } 3474 3475 static int f2fs_quota_sync_file(struct f2fs_sb_info *sbi, int type) 3476 { 3477 struct quota_info *dqopt = sb_dqopt(sbi->sb); 3478 struct address_space *mapping = dqopt->files[type]->i_mapping; 3479 int ret = 0; 3480 3481 ret = dquot_writeback_dquots(sbi->sb, type); 3482 if (ret) 3483 goto out; 3484 3485 ret = filemap_fdatawrite(mapping); 3486 if (ret) 3487 goto out; 3488 3489 /* if we are using journalled quota */ 3490 if (is_journalled_quota(sbi)) 3491 goto out; 3492 3493 ret = filemap_fdatawait(mapping); 3494 3495 truncate_inode_pages(&dqopt->files[type]->i_data, 0); 3496 out: 3497 if (ret) 3498 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR); 3499 return ret; 3500 } 3501 3502 int f2fs_do_quota_sync(struct super_block *sb, int type) 3503 { 3504 struct f2fs_sb_info *sbi = F2FS_SB(sb); 3505 struct quota_info *dqopt = sb_dqopt(sb); 3506 int cnt; 3507 int ret = 0; 3508 3509 /* 3510 * Now when everything is written we can discard the pagecache so 3511 * that userspace sees the changes. 3512 */ 3513 for (cnt = 0; cnt < MAXQUOTAS; cnt++) { 3514 struct f2fs_lock_context lc; 3515 3516 if (type != -1 && cnt != type) 3517 continue; 3518 3519 if (!sb_has_quota_active(sb, cnt)) 3520 continue; 3521 3522 if (!f2fs_sb_has_quota_ino(sbi)) 3523 inode_lock(dqopt->files[cnt]); 3524 3525 /* 3526 * do_quotactl 3527 * f2fs_quota_sync 3528 * f2fs_down_read(quota_sem) 3529 * dquot_writeback_dquots() 3530 * f2fs_dquot_commit 3531 * block_operation 3532 * f2fs_down_read(quota_sem) 3533 */ 3534 f2fs_lock_op(sbi, &lc); 3535 f2fs_down_read(&sbi->quota_sem); 3536 3537 ret = f2fs_quota_sync_file(sbi, cnt); 3538 3539 f2fs_up_read(&sbi->quota_sem); 3540 f2fs_unlock_op(sbi, &lc); 3541 3542 if (!f2fs_sb_has_quota_ino(sbi)) 3543 inode_unlock(dqopt->files[cnt]); 3544 3545 if (ret) 3546 break; 3547 } 3548 return ret; 3549 } 3550 3551 static int f2fs_quota_sync(struct super_block *sb, int type) 3552 { 3553 int ret; 3554 3555 F2FS_SB(sb)->umount_lock_holder = current; 3556 ret = f2fs_do_quota_sync(sb, type); 3557 F2FS_SB(sb)->umount_lock_holder = NULL; 3558 return ret; 3559 } 3560 3561 static int f2fs_quota_on(struct super_block *sb, int type, int format_id, 3562 const struct path *path) 3563 { 3564 struct inode *inode; 3565 int err = 0; 3566 3567 /* if quota sysfile exists, deny enabling quota with specific file */ 3568 if (f2fs_sb_has_quota_ino(F2FS_SB(sb))) { 3569 f2fs_err(F2FS_SB(sb), "quota sysfile already exists"); 3570 return -EBUSY; 3571 } 3572 3573 if (path->dentry->d_sb != sb) 3574 return -EXDEV; 3575 3576 F2FS_SB(sb)->umount_lock_holder = current; 3577 3578 err = f2fs_do_quota_sync(sb, type); 3579 if (err) 3580 goto out; 3581 3582 inode = d_inode(path->dentry); 3583 3584 err = f2fs_quota_convert_inline(inode); 3585 if (err) 3586 goto out; 3587 3588 err = filemap_fdatawrite(inode->i_mapping); 3589 if (err) 3590 goto out; 3591 3592 err = filemap_fdatawait(inode->i_mapping); 3593 if (err) 3594 goto out; 3595 3596 err = dquot_quota_on(sb, type, format_id, path); 3597 if (err) 3598 goto out; 3599 3600 inode_lock(inode); 3601 F2FS_I(inode)->i_flags |= F2FS_QUOTA_DEFAULT_FL; 3602 f2fs_set_inode_flags(inode); 3603 inode_unlock(inode); 3604 f2fs_mark_inode_dirty_sync(inode, false); 3605 out: 3606 F2FS_SB(sb)->umount_lock_holder = NULL; 3607 return err; 3608 } 3609 3610 static int __f2fs_quota_off(struct super_block *sb, int type) 3611 { 3612 struct inode *inode = sb_dqopt(sb)->files[type]; 3613 int err; 3614 3615 if (!inode || !igrab(inode)) 3616 return dquot_quota_off(sb, type); 3617 3618 err = f2fs_do_quota_sync(sb, type); 3619 if (err) 3620 goto out_put; 3621 3622 err = dquot_quota_off(sb, type); 3623 if (err || f2fs_sb_has_quota_ino(F2FS_SB(sb))) 3624 goto out_put; 3625 3626 inode_lock(inode); 3627 F2FS_I(inode)->i_flags &= ~F2FS_QUOTA_DEFAULT_FL; 3628 f2fs_set_inode_flags(inode); 3629 inode_unlock(inode); 3630 f2fs_mark_inode_dirty_sync(inode, false); 3631 out_put: 3632 iput(inode); 3633 return err; 3634 } 3635 3636 static int f2fs_quota_off(struct super_block *sb, int type) 3637 { 3638 struct f2fs_sb_info *sbi = F2FS_SB(sb); 3639 int err; 3640 3641 F2FS_SB(sb)->umount_lock_holder = current; 3642 3643 err = __f2fs_quota_off(sb, type); 3644 3645 /* 3646 * quotactl can shutdown journalled quota, result in inconsistence 3647 * between quota record and fs data by following updates, tag the 3648 * flag to let fsck be aware of it. 3649 */ 3650 if (is_journalled_quota(sbi)) 3651 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR); 3652 3653 F2FS_SB(sb)->umount_lock_holder = NULL; 3654 3655 return err; 3656 } 3657 3658 void f2fs_quota_off_umount(struct super_block *sb) 3659 { 3660 int type; 3661 int err; 3662 3663 for (type = 0; type < MAXQUOTAS; type++) { 3664 err = __f2fs_quota_off(sb, type); 3665 if (err) { 3666 int ret = dquot_quota_off(sb, type); 3667 3668 f2fs_err(F2FS_SB(sb), "Fail to turn off disk quota (type: %d, err: %d, ret:%d), Please run fsck to fix it.", 3669 type, err, ret); 3670 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR); 3671 } 3672 } 3673 /* 3674 * In case of checkpoint=disable, we must flush quota blocks. 3675 * This can cause NULL exception for node_inode in end_io, since 3676 * put_super already dropped it. 3677 */ 3678 sync_filesystem(sb); 3679 } 3680 3681 static void f2fs_truncate_quota_inode_pages(struct super_block *sb) 3682 { 3683 struct quota_info *dqopt = sb_dqopt(sb); 3684 int type; 3685 3686 for (type = 0; type < MAXQUOTAS; type++) { 3687 if (!dqopt->files[type]) 3688 continue; 3689 f2fs_inode_synced(dqopt->files[type]); 3690 } 3691 } 3692 3693 static int f2fs_dquot_commit(struct dquot *dquot) 3694 { 3695 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb); 3696 int ret; 3697 3698 f2fs_down_read_nested(&sbi->quota_sem, SINGLE_DEPTH_NESTING); 3699 ret = dquot_commit(dquot); 3700 if (ret < 0) 3701 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR); 3702 f2fs_up_read(&sbi->quota_sem); 3703 return ret; 3704 } 3705 3706 static int f2fs_dquot_acquire(struct dquot *dquot) 3707 { 3708 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb); 3709 int ret; 3710 3711 f2fs_down_read(&sbi->quota_sem); 3712 ret = dquot_acquire(dquot); 3713 if (ret < 0) 3714 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR); 3715 f2fs_up_read(&sbi->quota_sem); 3716 return ret; 3717 } 3718 3719 static int f2fs_dquot_release(struct dquot *dquot) 3720 { 3721 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb); 3722 int ret = dquot_release(dquot); 3723 3724 if (ret < 0) 3725 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR); 3726 return ret; 3727 } 3728 3729 static int f2fs_dquot_mark_dquot_dirty(struct dquot *dquot) 3730 { 3731 struct super_block *sb = dquot->dq_sb; 3732 struct f2fs_sb_info *sbi = F2FS_SB(sb); 3733 int ret = dquot_mark_dquot_dirty(dquot); 3734 3735 /* if we are using journalled quota */ 3736 if (is_journalled_quota(sbi)) 3737 set_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH); 3738 3739 return ret; 3740 } 3741 3742 static int f2fs_dquot_commit_info(struct super_block *sb, int type) 3743 { 3744 struct f2fs_sb_info *sbi = F2FS_SB(sb); 3745 int ret = dquot_commit_info(sb, type); 3746 3747 if (ret < 0) 3748 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR); 3749 return ret; 3750 } 3751 3752 static int f2fs_get_projid(struct inode *inode, kprojid_t *projid) 3753 { 3754 *projid = F2FS_I(inode)->i_projid; 3755 return 0; 3756 } 3757 3758 static const struct dquot_operations f2fs_quota_operations = { 3759 .get_reserved_space = f2fs_get_reserved_space, 3760 .write_dquot = f2fs_dquot_commit, 3761 .acquire_dquot = f2fs_dquot_acquire, 3762 .release_dquot = f2fs_dquot_release, 3763 .mark_dirty = f2fs_dquot_mark_dquot_dirty, 3764 .write_info = f2fs_dquot_commit_info, 3765 .alloc_dquot = dquot_alloc, 3766 .destroy_dquot = dquot_destroy, 3767 .get_projid = f2fs_get_projid, 3768 .get_next_id = dquot_get_next_id, 3769 }; 3770 3771 static const struct quotactl_ops f2fs_quotactl_ops = { 3772 .quota_on = f2fs_quota_on, 3773 .quota_off = f2fs_quota_off, 3774 .quota_sync = f2fs_quota_sync, 3775 .get_state = dquot_get_state, 3776 .set_info = dquot_set_dqinfo, 3777 .get_dqblk = dquot_get_dqblk, 3778 .set_dqblk = dquot_set_dqblk, 3779 .get_nextdqblk = dquot_get_next_dqblk, 3780 }; 3781 #else 3782 int f2fs_dquot_initialize(struct inode *inode) 3783 { 3784 return 0; 3785 } 3786 3787 int f2fs_do_quota_sync(struct super_block *sb, int type) 3788 { 3789 return 0; 3790 } 3791 3792 void f2fs_quota_off_umount(struct super_block *sb) 3793 { 3794 } 3795 #endif 3796 3797 static const struct super_operations f2fs_sops = { 3798 .alloc_inode = f2fs_alloc_inode, 3799 .free_inode = f2fs_free_inode, 3800 .drop_inode = f2fs_drop_inode, 3801 .write_inode = f2fs_write_inode, 3802 .dirty_inode = f2fs_dirty_inode, 3803 .show_options = f2fs_show_options, 3804 #ifdef CONFIG_QUOTA 3805 .quota_read = f2fs_quota_read, 3806 .quota_write = f2fs_quota_write, 3807 .get_dquots = f2fs_get_dquots, 3808 #endif 3809 .evict_inode = f2fs_evict_inode, 3810 .put_super = f2fs_put_super, 3811 .sync_fs = f2fs_sync_fs_super, 3812 .freeze_fs = f2fs_freeze, 3813 .unfreeze_fs = f2fs_unfreeze, 3814 .statfs = f2fs_statfs, 3815 .shutdown = f2fs_shutdown, 3816 }; 3817 3818 #ifdef CONFIG_FS_ENCRYPTION 3819 static int f2fs_get_context(struct inode *inode, void *ctx, size_t len) 3820 { 3821 return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION, 3822 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT, 3823 ctx, len, NULL); 3824 } 3825 3826 static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len, 3827 void *fs_data) 3828 { 3829 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 3830 3831 /* 3832 * Encrypting the root directory is not allowed because fsck 3833 * expects lost+found directory to exist and remain unencrypted 3834 * if LOST_FOUND feature is enabled. 3835 * 3836 */ 3837 if (f2fs_sb_has_lost_found(sbi) && 3838 inode->i_ino == F2FS_ROOT_INO(sbi)) 3839 return -EPERM; 3840 3841 return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION, 3842 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT, 3843 ctx, len, fs_data, XATTR_CREATE); 3844 } 3845 3846 static const union fscrypt_policy *f2fs_get_dummy_policy(struct super_block *sb) 3847 { 3848 return F2FS_OPTION(F2FS_SB(sb)).dummy_enc_policy.policy; 3849 } 3850 3851 static bool f2fs_has_stable_inodes(struct super_block *sb) 3852 { 3853 return true; 3854 } 3855 3856 static unsigned int 3857 f2fs_get_devices(struct super_block *sb, 3858 struct block_device *devs[FSCRYPT_MAX_DEVICES]) 3859 { 3860 struct f2fs_sb_info *sbi = F2FS_SB(sb); 3861 int ndevs; 3862 int i; 3863 3864 static_assert(MAX_DEVICES <= FSCRYPT_MAX_DEVICES); 3865 3866 if (!f2fs_is_multi_device(sbi)) { 3867 devs[0] = sb->s_bdev; 3868 return 1; 3869 } 3870 ndevs = sbi->s_ndevs; 3871 if (WARN_ON_ONCE(ndevs > FSCRYPT_MAX_DEVICES)) 3872 ndevs = FSCRYPT_MAX_DEVICES; 3873 3874 for (i = 0; i < ndevs; i++) 3875 devs[i] = FDEV(i).bdev; 3876 return ndevs; 3877 } 3878 3879 static const struct fscrypt_operations f2fs_cryptops = { 3880 .inode_info_offs = (int)offsetof(struct f2fs_inode_info, i_crypt_info) - 3881 (int)offsetof(struct f2fs_inode_info, vfs_inode), 3882 .is_block_based = 1, 3883 .has_32bit_inodes = 1, 3884 .supports_subblock_data_units = 1, 3885 .legacy_key_prefix = "f2fs:", 3886 .get_context = f2fs_get_context, 3887 .set_context = f2fs_set_context, 3888 .get_dummy_policy = f2fs_get_dummy_policy, 3889 .empty_dir = f2fs_empty_dir, 3890 .has_stable_inodes = f2fs_has_stable_inodes, 3891 .get_devices = f2fs_get_devices, 3892 }; 3893 #endif /* CONFIG_FS_ENCRYPTION */ 3894 3895 static struct inode *f2fs_nfs_get_inode(struct super_block *sb, 3896 u64 ino, u32 generation) 3897 { 3898 struct f2fs_sb_info *sbi = F2FS_SB(sb); 3899 struct inode *inode; 3900 3901 if (f2fs_check_nid_range(sbi, ino)) 3902 return ERR_PTR(-ESTALE); 3903 3904 /* 3905 * f2fs_iget isn't quite right if the inode is currently unallocated! 3906 * However f2fs_iget currently does appropriate checks to handle stale 3907 * inodes so everything is OK. 3908 */ 3909 inode = f2fs_iget(sb, ino); 3910 if (IS_ERR(inode)) 3911 return ERR_CAST(inode); 3912 if (unlikely(generation && inode->i_generation != generation)) { 3913 /* we didn't find the right inode.. */ 3914 iput(inode); 3915 return ERR_PTR(-ESTALE); 3916 } 3917 return inode; 3918 } 3919 3920 static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid, 3921 int fh_len, int fh_type) 3922 { 3923 return generic_fh_to_dentry(sb, fid, fh_len, fh_type, 3924 f2fs_nfs_get_inode); 3925 } 3926 3927 static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid, 3928 int fh_len, int fh_type) 3929 { 3930 return generic_fh_to_parent(sb, fid, fh_len, fh_type, 3931 f2fs_nfs_get_inode); 3932 } 3933 3934 static const struct export_operations f2fs_export_ops = { 3935 .encode_fh = generic_encode_ino32_fh, 3936 .fh_to_dentry = f2fs_fh_to_dentry, 3937 .fh_to_parent = f2fs_fh_to_parent, 3938 .get_parent = f2fs_get_parent, 3939 }; 3940 3941 loff_t max_file_blocks(struct f2fs_sb_info *sbi, struct inode *inode) 3942 { 3943 loff_t result = 0; 3944 loff_t leaf_count; 3945 3946 /* 3947 * note: previously, result is equal to (DEF_ADDRS_PER_INODE(sbi) - 3948 * DEFAULT_INLINE_XATTR_ADDRS), but now f2fs try to reserve more 3949 * space in inode.i_addr, it will be more safe to reassign 3950 * result as zero. 3951 */ 3952 3953 if (inode && f2fs_compressed_file(inode)) 3954 leaf_count = ADDRS_PER_BLOCK(inode); 3955 else 3956 leaf_count = DEF_ADDRS_PER_BLOCK(sbi); 3957 3958 /* two direct node blocks */ 3959 result += (leaf_count * 2); 3960 3961 /* two indirect node blocks */ 3962 leaf_count *= NIDS_PER_BLOCK(sbi); 3963 result += (leaf_count * 2); 3964 3965 /* one double indirect node block */ 3966 leaf_count *= NIDS_PER_BLOCK(sbi); 3967 result += leaf_count; 3968 3969 /* 3970 * For compatibility with FSCRYPT_POLICY_FLAG_IV_INO_LBLK_{64,32} with 3971 * a 4K crypto data unit, we must restrict the max filesize to what can 3972 * fit within U32_MAX + 1 data units. 3973 */ 3974 3975 result = umin(result, F2FS_BYTES_TO_BLK(sbi, 3976 ((loff_t)U32_MAX + 1) * 4096)); 3977 3978 return result; 3979 } 3980 3981 static int __f2fs_commit_super(struct f2fs_sb_info *sbi, struct folio *folio, 3982 pgoff_t index, bool update) 3983 { 3984 struct bio *bio; 3985 /* it's rare case, we can do fua all the time */ 3986 blk_opf_t opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA; 3987 int ret; 3988 3989 folio_lock(folio); 3990 folio_wait_writeback(folio); 3991 if (update) 3992 memcpy(F2FS_SUPER_BLOCK(folio, index), F2FS_RAW_SUPER(sbi), 3993 sizeof(struct f2fs_super_block)); 3994 folio_mark_dirty(folio); 3995 folio_clear_dirty_for_io(folio); 3996 folio_start_writeback(folio); 3997 folio_unlock(folio); 3998 3999 bio = bio_alloc(sbi->sb->s_bdev, 1, opf, GFP_NOFS); 4000 4001 /* it doesn't need to set crypto context for superblock update */ 4002 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(sbi, folio->index); 4003 4004 if (!bio_add_folio(bio, folio, folio_size(folio), 0)) 4005 f2fs_bug_on(sbi, 1); 4006 4007 ret = submit_bio_wait(bio); 4008 bio_put(bio); 4009 folio_end_writeback(folio); 4010 4011 return ret; 4012 } 4013 4014 static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi, 4015 struct folio *folio, pgoff_t index) 4016 { 4017 struct f2fs_super_block *raw_super = F2FS_SUPER_BLOCK(folio, index); 4018 struct super_block *sb = sbi->sb; 4019 u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr); 4020 u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr); 4021 u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr); 4022 u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr); 4023 u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr); 4024 u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr); 4025 u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt); 4026 u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit); 4027 u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat); 4028 u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa); 4029 u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main); 4030 u32 segment_count = le32_to_cpu(raw_super->segment_count); 4031 u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg); 4032 u64 main_end_blkaddr = main_blkaddr + 4033 ((u64)segment_count_main << log_blocks_per_seg); 4034 u64 seg_end_blkaddr = segment0_blkaddr + 4035 ((u64)segment_count << log_blocks_per_seg); 4036 4037 if (segment0_blkaddr != cp_blkaddr) { 4038 f2fs_info(sbi, "Mismatch start address, segment0(%u) cp_blkaddr(%u)", 4039 segment0_blkaddr, cp_blkaddr); 4040 return true; 4041 } 4042 4043 if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) != 4044 sit_blkaddr) { 4045 f2fs_info(sbi, "Wrong CP boundary, start(%u) end(%u) blocks(%u)", 4046 cp_blkaddr, sit_blkaddr, 4047 segment_count_ckpt << log_blocks_per_seg); 4048 return true; 4049 } 4050 4051 if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) != 4052 nat_blkaddr) { 4053 f2fs_info(sbi, "Wrong SIT boundary, start(%u) end(%u) blocks(%u)", 4054 sit_blkaddr, nat_blkaddr, 4055 segment_count_sit << log_blocks_per_seg); 4056 return true; 4057 } 4058 4059 if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) != 4060 ssa_blkaddr) { 4061 f2fs_info(sbi, "Wrong NAT boundary, start(%u) end(%u) blocks(%u)", 4062 nat_blkaddr, ssa_blkaddr, 4063 segment_count_nat << log_blocks_per_seg); 4064 return true; 4065 } 4066 4067 if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) != 4068 main_blkaddr) { 4069 f2fs_info(sbi, "Wrong SSA boundary, start(%u) end(%u) blocks(%u)", 4070 ssa_blkaddr, main_blkaddr, 4071 segment_count_ssa << log_blocks_per_seg); 4072 return true; 4073 } 4074 4075 if (main_end_blkaddr > seg_end_blkaddr) { 4076 f2fs_info(sbi, "Wrong MAIN_AREA boundary, start(%u) end(%llu) block(%u)", 4077 main_blkaddr, seg_end_blkaddr, 4078 segment_count_main << log_blocks_per_seg); 4079 return true; 4080 } else if (main_end_blkaddr < seg_end_blkaddr) { 4081 int err = 0; 4082 char *res; 4083 4084 /* fix in-memory information all the time */ 4085 raw_super->segment_count = cpu_to_le32((main_end_blkaddr - 4086 segment0_blkaddr) >> log_blocks_per_seg); 4087 if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_SB_CHKSUM)) { 4088 __u32 crc = f2fs_crc32(raw_super, 4089 offsetof(struct f2fs_super_block, crc)); 4090 4091 raw_super->crc = cpu_to_le32(crc); 4092 } 4093 4094 if (f2fs_readonly(sb) || f2fs_hw_is_readonly(sbi)) { 4095 set_sbi_flag(sbi, SBI_NEED_SB_WRITE); 4096 res = "internally"; 4097 } else { 4098 /* 4099 * __f2fs_commit_super() will access log_blocksize 4100 * in SECTOR_FROM_BLOCK(), init it in advance. 4101 */ 4102 sbi->log_blocksize = 4103 le32_to_cpu(raw_super->log_blocksize); 4104 err = __f2fs_commit_super(sbi, folio, index, false); 4105 res = err ? "failed" : "done"; 4106 } 4107 f2fs_info(sbi, "Fix alignment : %s, start(%u) end(%llu) block(%u)", 4108 res, main_blkaddr, seg_end_blkaddr, 4109 segment_count_main << log_blocks_per_seg); 4110 if (err) 4111 return true; 4112 } 4113 return false; 4114 } 4115 4116 static int sanity_check_raw_super(struct f2fs_sb_info *sbi, 4117 struct folio *folio, pgoff_t index) 4118 { 4119 block_t segment_count, segs_per_sec, secs_per_zone, segment_count_main; 4120 block_t total_sections, blocks_per_seg; 4121 struct f2fs_super_block *raw_super = F2FS_SUPER_BLOCK(folio, index); 4122 size_t crc_offset = 0; 4123 __u32 crc = 0; 4124 4125 if (le32_to_cpu(raw_super->magic) != F2FS_SUPER_MAGIC) { 4126 f2fs_info(sbi, "Magic Mismatch, valid(0x%x) - read(0x%x)", 4127 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic)); 4128 return -EINVAL; 4129 } 4130 4131 /* Check checksum_offset and crc in superblock */ 4132 if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_SB_CHKSUM)) { 4133 crc_offset = le32_to_cpu(raw_super->checksum_offset); 4134 if (crc_offset != 4135 offsetof(struct f2fs_super_block, crc)) { 4136 f2fs_info(sbi, "Invalid SB checksum offset: %zu", 4137 crc_offset); 4138 return -EFSCORRUPTED; 4139 } 4140 crc = le32_to_cpu(raw_super->crc); 4141 if (crc != f2fs_crc32(raw_super, crc_offset)) { 4142 f2fs_info(sbi, "Invalid SB checksum value: %u", crc); 4143 return -EFSCORRUPTED; 4144 } 4145 } 4146 4147 /* only support block_size equals to PAGE_SIZE */ 4148 if (le32_to_cpu(raw_super->log_blocksize) != PAGE_SHIFT) { 4149 f2fs_info(sbi, "Invalid log_blocksize (%u), supports only %u", 4150 le32_to_cpu(raw_super->log_blocksize), 4151 PAGE_SHIFT); 4152 return -EFSCORRUPTED; 4153 } 4154 4155 /* check log blocks per segment */ 4156 if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) { 4157 f2fs_info(sbi, "Invalid log blocks per segment (%u)", 4158 le32_to_cpu(raw_super->log_blocks_per_seg)); 4159 return -EFSCORRUPTED; 4160 } 4161 4162 /* Currently, support 512/1024/2048/4096/16K bytes sector size */ 4163 if (le32_to_cpu(raw_super->log_sectorsize) > 4164 F2FS_MAX_LOG_SECTOR_SIZE || 4165 le32_to_cpu(raw_super->log_sectorsize) < 4166 F2FS_MIN_LOG_SECTOR_SIZE) { 4167 f2fs_info(sbi, "Invalid log sectorsize (%u)", 4168 le32_to_cpu(raw_super->log_sectorsize)); 4169 return -EFSCORRUPTED; 4170 } 4171 if (le32_to_cpu(raw_super->log_sectors_per_block) + 4172 le32_to_cpu(raw_super->log_sectorsize) != 4173 F2FS_MAX_LOG_SECTOR_SIZE) { 4174 f2fs_info(sbi, "Invalid log sectors per block(%u) log sectorsize(%u)", 4175 le32_to_cpu(raw_super->log_sectors_per_block), 4176 le32_to_cpu(raw_super->log_sectorsize)); 4177 return -EFSCORRUPTED; 4178 } 4179 4180 segment_count = le32_to_cpu(raw_super->segment_count); 4181 segment_count_main = le32_to_cpu(raw_super->segment_count_main); 4182 segs_per_sec = le32_to_cpu(raw_super->segs_per_sec); 4183 secs_per_zone = le32_to_cpu(raw_super->secs_per_zone); 4184 total_sections = le32_to_cpu(raw_super->section_count); 4185 4186 /* blocks_per_seg should be 512, given the above check */ 4187 blocks_per_seg = BIT(le32_to_cpu(raw_super->log_blocks_per_seg)); 4188 4189 if (segment_count > F2FS_MAX_SEGMENT || 4190 segment_count < F2FS_MIN_SEGMENTS) { 4191 f2fs_info(sbi, "Invalid segment count (%u)", segment_count); 4192 return -EFSCORRUPTED; 4193 } 4194 4195 if (total_sections > segment_count_main || total_sections < 1 || 4196 segs_per_sec > segment_count || !segs_per_sec) { 4197 f2fs_info(sbi, "Invalid segment/section count (%u, %u x %u)", 4198 segment_count, total_sections, segs_per_sec); 4199 return -EFSCORRUPTED; 4200 } 4201 4202 if (segment_count_main != total_sections * segs_per_sec) { 4203 f2fs_info(sbi, "Invalid segment/section count (%u != %u * %u)", 4204 segment_count_main, total_sections, segs_per_sec); 4205 return -EFSCORRUPTED; 4206 } 4207 4208 if ((segment_count / segs_per_sec) < total_sections) { 4209 f2fs_info(sbi, "Small segment_count (%u < %u * %u)", 4210 segment_count, segs_per_sec, total_sections); 4211 return -EFSCORRUPTED; 4212 } 4213 4214 if (segment_count > (le64_to_cpu(raw_super->block_count) >> 9)) { 4215 f2fs_info(sbi, "Wrong segment_count / block_count (%u > %llu)", 4216 segment_count, le64_to_cpu(raw_super->block_count)); 4217 return -EFSCORRUPTED; 4218 } 4219 4220 if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_DEVICE_ALIAS) && 4221 (!RDEV(0).path[0] || !RDEV(1).path[0])) { 4222 f2fs_info(sbi, "Device aliasing requires a multi-device configuration"); 4223 return -EFSCORRUPTED; 4224 } 4225 4226 if (RDEV(0).path[0]) { 4227 block_t dev_seg_count = le32_to_cpu(RDEV(0).total_segments); 4228 int i = 1; 4229 4230 while (i < MAX_DEVICES && RDEV(i).path[0]) { 4231 dev_seg_count += le32_to_cpu(RDEV(i).total_segments); 4232 i++; 4233 } 4234 if (segment_count != dev_seg_count) { 4235 f2fs_info(sbi, "Segment count (%u) mismatch with total segments from devices (%u)", 4236 segment_count, dev_seg_count); 4237 return -EFSCORRUPTED; 4238 } 4239 } else { 4240 if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_BLKZONED) && 4241 !bdev_is_zoned(sbi->sb->s_bdev)) { 4242 f2fs_info(sbi, "Zoned block device path is missing"); 4243 return -EFSCORRUPTED; 4244 } 4245 } 4246 4247 if (secs_per_zone > total_sections || !secs_per_zone) { 4248 f2fs_info(sbi, "Wrong secs_per_zone / total_sections (%u, %u)", 4249 secs_per_zone, total_sections); 4250 return -EFSCORRUPTED; 4251 } 4252 if (le32_to_cpu(raw_super->extension_count) > F2FS_MAX_EXTENSION || 4253 raw_super->hot_ext_count > F2FS_MAX_EXTENSION || 4254 (le32_to_cpu(raw_super->extension_count) + 4255 raw_super->hot_ext_count) > F2FS_MAX_EXTENSION) { 4256 f2fs_info(sbi, "Corrupted extension count (%u + %u > %u)", 4257 le32_to_cpu(raw_super->extension_count), 4258 raw_super->hot_ext_count, 4259 F2FS_MAX_EXTENSION); 4260 return -EFSCORRUPTED; 4261 } 4262 4263 if (le32_to_cpu(raw_super->cp_payload) >= 4264 (blocks_per_seg - F2FS_CP_PACKS - 4265 NR_CURSEG_PERSIST_TYPE)) { 4266 f2fs_info(sbi, "Insane cp_payload (%u >= %u)", 4267 le32_to_cpu(raw_super->cp_payload), 4268 blocks_per_seg - F2FS_CP_PACKS - 4269 NR_CURSEG_PERSIST_TYPE); 4270 return -EFSCORRUPTED; 4271 } 4272 4273 /* check reserved ino info */ 4274 if (le32_to_cpu(raw_super->node_ino) != 1 || 4275 le32_to_cpu(raw_super->meta_ino) != 2 || 4276 le32_to_cpu(raw_super->root_ino) != 3) { 4277 f2fs_info(sbi, "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)", 4278 le32_to_cpu(raw_super->node_ino), 4279 le32_to_cpu(raw_super->meta_ino), 4280 le32_to_cpu(raw_super->root_ino)); 4281 return -EFSCORRUPTED; 4282 } 4283 4284 /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */ 4285 if (sanity_check_area_boundary(sbi, folio, index)) 4286 return -EFSCORRUPTED; 4287 4288 return 0; 4289 } 4290 4291 int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi) 4292 { 4293 unsigned int total, fsmeta; 4294 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi); 4295 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 4296 unsigned int ovp_segments, reserved_segments; 4297 unsigned int main_segs, blocks_per_seg; 4298 unsigned int sit_segs, nat_segs; 4299 unsigned int sit_bitmap_size, nat_bitmap_size; 4300 unsigned int log_blocks_per_seg; 4301 unsigned int segment_count_main; 4302 unsigned int cp_pack_start_sum, cp_payload; 4303 block_t user_block_count, valid_user_blocks; 4304 block_t avail_node_count, valid_node_count; 4305 unsigned int nat_blocks, nat_bits_bytes, nat_bits_blocks; 4306 unsigned int sit_blk_cnt; 4307 unsigned long long ckpt_size, sit_end, nat_end; 4308 int i, j; 4309 4310 total = le32_to_cpu(raw_super->segment_count); 4311 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt); 4312 sit_segs = le32_to_cpu(raw_super->segment_count_sit); 4313 fsmeta += sit_segs; 4314 nat_segs = le32_to_cpu(raw_super->segment_count_nat); 4315 fsmeta += nat_segs; 4316 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count); 4317 fsmeta += le32_to_cpu(raw_super->segment_count_ssa); 4318 4319 if (unlikely(fsmeta >= total)) 4320 return 1; 4321 4322 ovp_segments = le32_to_cpu(ckpt->overprov_segment_count); 4323 reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count); 4324 4325 if (!f2fs_sb_has_readonly(sbi) && 4326 unlikely(fsmeta < F2FS_MIN_META_SEGMENTS || 4327 ovp_segments == 0 || reserved_segments == 0)) { 4328 f2fs_err(sbi, "Wrong layout: check mkfs.f2fs version"); 4329 return 1; 4330 } 4331 user_block_count = le64_to_cpu(ckpt->user_block_count); 4332 segment_count_main = le32_to_cpu(raw_super->segment_count_main) + 4333 (f2fs_sb_has_readonly(sbi) ? 1 : 0); 4334 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg); 4335 if (!user_block_count || user_block_count >= 4336 segment_count_main << log_blocks_per_seg) { 4337 f2fs_err(sbi, "Wrong user_block_count: %u", 4338 user_block_count); 4339 return 1; 4340 } 4341 4342 valid_user_blocks = le64_to_cpu(ckpt->valid_block_count); 4343 if (valid_user_blocks > user_block_count) { 4344 f2fs_err(sbi, "Wrong valid_user_blocks: %u, user_block_count: %u", 4345 valid_user_blocks, user_block_count); 4346 return 1; 4347 } 4348 4349 valid_node_count = le32_to_cpu(ckpt->valid_node_count); 4350 avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM; 4351 if (valid_node_count > avail_node_count) { 4352 f2fs_err(sbi, "Wrong valid_node_count: %u, avail_node_count: %u", 4353 valid_node_count, avail_node_count); 4354 return 1; 4355 } 4356 4357 main_segs = le32_to_cpu(raw_super->segment_count_main); 4358 blocks_per_seg = BLKS_PER_SEG(sbi); 4359 4360 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) { 4361 if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs || 4362 le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg) 4363 return 1; 4364 4365 if (f2fs_sb_has_readonly(sbi)) 4366 continue; 4367 4368 for (j = i + 1; j < NR_CURSEG_NODE_TYPE; j++) { 4369 if (le32_to_cpu(ckpt->cur_node_segno[i]) == 4370 le32_to_cpu(ckpt->cur_node_segno[j])) { 4371 f2fs_err(sbi, "Node segment (%u, %u) has the same segno: %u", 4372 i, j, 4373 le32_to_cpu(ckpt->cur_node_segno[i])); 4374 return 1; 4375 } 4376 } 4377 } 4378 4379 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) { 4380 if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs || 4381 le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg) 4382 return 1; 4383 4384 if (f2fs_sb_has_readonly(sbi)) 4385 continue; 4386 4387 for (j = i + 1; j < NR_CURSEG_DATA_TYPE; j++) { 4388 if (le32_to_cpu(ckpt->cur_data_segno[i]) == 4389 le32_to_cpu(ckpt->cur_data_segno[j])) { 4390 f2fs_err(sbi, "Data segment (%u, %u) has the same segno: %u", 4391 i, j, 4392 le32_to_cpu(ckpt->cur_data_segno[i])); 4393 return 1; 4394 } 4395 } 4396 } 4397 if (f2fs_sb_has_readonly(sbi)) 4398 goto skip_cross; 4399 4400 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) { 4401 for (j = 0; j < NR_CURSEG_DATA_TYPE; j++) { 4402 if (le32_to_cpu(ckpt->cur_node_segno[i]) == 4403 le32_to_cpu(ckpt->cur_data_segno[j])) { 4404 f2fs_err(sbi, "Node segment (%u) and Data segment (%u) has the same segno: %u", 4405 i, j, 4406 le32_to_cpu(ckpt->cur_node_segno[i])); 4407 return 1; 4408 } 4409 } 4410 } 4411 skip_cross: 4412 sit_bitmap_size = le32_to_cpu(ckpt->sit_ver_bitmap_bytesize); 4413 nat_bitmap_size = le32_to_cpu(ckpt->nat_ver_bitmap_bytesize); 4414 4415 if (sit_bitmap_size != ((sit_segs / 2) << log_blocks_per_seg) / 8 || 4416 nat_bitmap_size != ((nat_segs / 2) << log_blocks_per_seg) / 8) { 4417 f2fs_err(sbi, "Wrong bitmap size: sit: %u, nat:%u", 4418 sit_bitmap_size, nat_bitmap_size); 4419 return 1; 4420 } 4421 4422 sit_blk_cnt = DIV_ROUND_UP(main_segs, SIT_ENTRY_PER_BLOCK(sbi)); 4423 if (sit_bitmap_size * 8 < sit_blk_cnt) { 4424 f2fs_err(sbi, "Wrong bitmap size: sit: %u, sit_blk_cnt:%u", 4425 sit_bitmap_size, sit_blk_cnt); 4426 return 1; 4427 } 4428 4429 cp_pack_start_sum = __start_sum_addr(sbi); 4430 cp_payload = __cp_payload(sbi); 4431 if (cp_pack_start_sum < cp_payload + 1 || 4432 cp_pack_start_sum > blocks_per_seg - 1 - 4433 NR_CURSEG_PERSIST_TYPE) { 4434 f2fs_err(sbi, "Wrong cp_pack_start_sum: %u", 4435 cp_pack_start_sum); 4436 return 1; 4437 } 4438 4439 /* 4440 * Nothing above ties the bitmap lengths to sbi->ckpt, which is 4441 * (1 + cp_payload) blocks. Bound both ends the way __bitmap_ptr() 4442 * lays them out. 4443 */ 4444 ckpt_size = (unsigned long long)(cp_payload + 1) * F2FS_BLKSIZE(sbi); 4445 if (__is_set_ckpt_flags(ckpt, CP_LARGE_NAT_BITMAP_FLAG)) { 4446 nat_end = CP_MIN_CHKSUM_OFFSET + sizeof(__le32) + nat_bitmap_size; 4447 sit_end = nat_end + sit_bitmap_size; 4448 } else if (cp_payload > 0) { 4449 nat_end = CP_MIN_CHKSUM_OFFSET + nat_bitmap_size; 4450 sit_end = F2FS_BLKSIZE(sbi) + sit_bitmap_size; 4451 } else { 4452 sit_end = CP_MIN_CHKSUM_OFFSET + sit_bitmap_size; 4453 nat_end = sit_end + nat_bitmap_size; 4454 } 4455 4456 if (sit_end > ckpt_size || nat_end > ckpt_size) { 4457 f2fs_err(sbi, "Bitmaps do not fit the checkpoint pack: sit ends at %llu, nat at %llu, pack is %llu bytes", 4458 sit_end, nat_end, ckpt_size); 4459 return 1; 4460 } 4461 4462 if (__is_set_ckpt_flags(ckpt, CP_LARGE_NAT_BITMAP_FLAG) && 4463 le32_to_cpu(ckpt->checksum_offset) != CP_MIN_CHKSUM_OFFSET) { 4464 f2fs_warn(sbi, "using deprecated layout of large_nat_bitmap, " 4465 "please run fsck v1.13.0 or higher to repair, chksum_offset: %u, " 4466 "fixed with patch: \"f2fs-tools: relocate chksum_offset for large_nat_bitmap feature\"", 4467 le32_to_cpu(ckpt->checksum_offset)); 4468 return 1; 4469 } 4470 4471 nat_blocks = nat_segs << log_blocks_per_seg; 4472 nat_bits_bytes = nat_blocks / BITS_PER_BYTE; 4473 nat_bits_blocks = F2FS_BLK_ALIGN(sbi, (nat_bits_bytes << 1) + 8); 4474 if (__is_set_ckpt_flags(ckpt, CP_NAT_BITS_FLAG) && 4475 (cp_payload + F2FS_CP_PACKS + 4476 NR_CURSEG_PERSIST_TYPE + nat_bits_blocks >= blocks_per_seg)) { 4477 f2fs_warn(sbi, "Insane cp_payload: %u, nat_bits_blocks: %u)", 4478 cp_payload, nat_bits_blocks); 4479 return 1; 4480 } 4481 4482 if (unlikely(f2fs_cp_error(sbi))) { 4483 f2fs_err(sbi, "A bug case: need to run fsck"); 4484 return 1; 4485 } 4486 return 0; 4487 } 4488 4489 static void init_sb_info(struct f2fs_sb_info *sbi) 4490 { 4491 struct f2fs_super_block *raw_super = sbi->raw_super; 4492 int i; 4493 4494 sbi->log_sectors_per_block = 4495 le32_to_cpu(raw_super->log_sectors_per_block); 4496 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize); 4497 sbi->blocksize = BIT(sbi->log_blocksize); 4498 sbi->nat_entries_per_block = sbi->blocksize / 4499 sizeof(struct f2fs_nat_entry); 4500 sbi->addrs_per_inode = F2FS_DEF_ADDRS_PER_INODE(sbi->blocksize); 4501 sbi->addrs_per_block = (sbi->blocksize - 4502 sizeof(struct node_footer)) / sizeof(__le32); 4503 sbi->nids_per_block = sbi->addrs_per_block; 4504 sbi->sit_entries_per_block = sbi->blocksize / 4505 sizeof(struct f2fs_sit_entry); 4506 sbi->orphans_per_block = (sbi->blocksize - 4507 sizeof(struct f2fs_orphan_footer)) / sizeof(__le32); 4508 sbi->dentries_per_block = (BITS_PER_BYTE * sbi->blocksize) / 4509 ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * BITS_PER_BYTE + 1); 4510 sbi->dentry_bitmap_size = DIV_ROUND_UP(sbi->dentries_per_block, 4511 BITS_PER_BYTE); 4512 sbi->dentry_reserved_size = sbi->blocksize - sbi->dentry_bitmap_size - 4513 (SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * sbi->dentries_per_block; 4514 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg); 4515 sbi->blocks_per_seg = BIT(sbi->log_blocks_per_seg); 4516 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec); 4517 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone); 4518 sbi->total_sections = le32_to_cpu(raw_super->section_count); 4519 sbi->total_node_count = SEGS_TO_BLKS(sbi, 4520 ((le32_to_cpu(raw_super->segment_count_nat) / 2) * 4521 NAT_ENTRY_PER_BLOCK(sbi))); 4522 sbi->allocate_section_hint = le32_to_cpu(raw_super->section_count); 4523 sbi->allocate_section_policy = ALLOCATE_FORWARD_NOHINT; 4524 F2FS_ROOT_INO(sbi) = le32_to_cpu(raw_super->root_ino); 4525 sbi->cur_victim_sec = NULL_SECNO; 4526 sbi->gc_mode = GC_NORMAL; 4527 sbi->next_victim_seg[BG_GC] = NULL_SEGNO; 4528 sbi->next_victim_seg[FG_GC] = NULL_SEGNO; 4529 sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH; 4530 sbi->migration_granularity = SEGS_PER_SEC(sbi); 4531 sbi->migration_window_granularity = f2fs_sb_has_blkzoned(sbi) ? 4532 DEF_MIGRATION_WINDOW_GRANULARITY_ZONED : SEGS_PER_SEC(sbi); 4533 sbi->seq_file_ra_mul = MIN_RA_MUL; 4534 sbi->max_fragment_chunk = DEF_FRAGMENT_SIZE; 4535 sbi->max_fragment_hole = DEF_FRAGMENT_SIZE; 4536 spin_lock_init(&sbi->gc_remaining_trials_lock); 4537 atomic64_set(&sbi->current_atomic_write, 0); 4538 sbi->max_lock_elapsed_time = MAX_LOCK_ELAPSED_TIME; 4539 sbi->adjust_lock_priority = 0; 4540 sbi->lock_duration_priority = F2FS_DEFAULT_TASK_PRIORITY; 4541 sbi->critical_task_priority = F2FS_CRITICAL_TASK_PRIORITY; 4542 4543 sbi->sum_blocksize = f2fs_sb_has_packed_ssa(sbi) ? 4544 4096 : sbi->blocksize; 4545 sbi->sums_per_block = sbi->blocksize / sbi->sum_blocksize; 4546 sbi->entries_in_sum = sbi->sum_blocksize / 8; 4547 sbi->sum_entry_size = SUMMARY_SIZE * sbi->entries_in_sum; 4548 sbi->sum_journal_size = sbi->sum_blocksize - SUM_FOOTER_SIZE - 4549 sbi->sum_entry_size; 4550 sbi->nat_journal_entries = (sbi->sum_journal_size - 2) / 4551 sizeof(struct nat_journal_entry); 4552 sbi->sit_journal_entries = (sbi->sum_journal_size - 2) / 4553 sizeof(struct sit_journal_entry); 4554 4555 sbi->dir_level = DEF_DIR_LEVEL; 4556 sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL; 4557 sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL; 4558 sbi->interval_time[DISCARD_TIME] = DEF_IDLE_INTERVAL; 4559 sbi->interval_time[GC_TIME] = DEF_IDLE_INTERVAL; 4560 sbi->interval_time[DISABLE_TIME] = DEF_DISABLE_INTERVAL; 4561 sbi->interval_time[UMOUNT_DISCARD_TIMEOUT] = 4562 DEF_UMOUNT_DISCARD_TIMEOUT; 4563 clear_sbi_flag(sbi, SBI_NEED_FSCK); 4564 4565 for (i = 0; i < NR_COUNT_TYPE; i++) 4566 atomic_set(&sbi->nr_caches[i], 0); 4567 4568 for (i = 0; i < META; i++) 4569 atomic_set(&sbi->wb_sync_req[i], 0); 4570 4571 INIT_LIST_HEAD(&sbi->s_list); 4572 mutex_init(&sbi->umount_mutex); 4573 init_f2fs_rwsem(&sbi->io_order_lock); 4574 spin_lock_init(&sbi->cp_lock); 4575 4576 sbi->dirty_device = 0; 4577 spin_lock_init(&sbi->dev_lock); 4578 4579 init_f2fs_rwsem(&sbi->sb_lock); 4580 init_f2fs_rwsem(&sbi->pin_sem); 4581 } 4582 4583 static int init_percpu_info(struct f2fs_sb_info *sbi) 4584 { 4585 int err; 4586 4587 err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL); 4588 if (err) 4589 return err; 4590 4591 err = percpu_counter_init(&sbi->rf_node_block_count, 0, GFP_KERNEL); 4592 if (err) 4593 goto err_valid_block; 4594 4595 err = percpu_counter_init(&sbi->total_valid_inode_count, 0, 4596 GFP_KERNEL); 4597 if (err) 4598 goto err_node_block; 4599 return 0; 4600 4601 err_node_block: 4602 percpu_counter_destroy(&sbi->rf_node_block_count); 4603 err_valid_block: 4604 percpu_counter_destroy(&sbi->alloc_valid_block_count); 4605 return err; 4606 } 4607 4608 #ifdef CONFIG_BLK_DEV_ZONED 4609 4610 struct f2fs_report_zones_args { 4611 struct f2fs_sb_info *sbi; 4612 struct f2fs_dev_info *dev; 4613 }; 4614 4615 static int f2fs_report_zone_cb(struct blk_zone *zone, unsigned int idx, 4616 void *data) 4617 { 4618 struct f2fs_report_zones_args *rz_args = data; 4619 block_t unusable_blocks = (zone->len - zone->capacity) >> 4620 F2FS_LOG_SECTORS_PER_BLOCK(rz_args->sbi); 4621 4622 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL) 4623 return 0; 4624 4625 set_bit(idx, rz_args->dev->blkz_seq); 4626 if (!rz_args->sbi->unusable_blocks_per_sec) { 4627 rz_args->sbi->unusable_blocks_per_sec = unusable_blocks; 4628 return 0; 4629 } 4630 if (rz_args->sbi->unusable_blocks_per_sec != unusable_blocks) { 4631 f2fs_err(rz_args->sbi, "F2FS supports single zone capacity\n"); 4632 return -EINVAL; 4633 } 4634 return 0; 4635 } 4636 4637 static int init_blkz_info(struct f2fs_sb_info *sbi, int devi) 4638 { 4639 struct block_device *bdev = FDEV(devi).bdev; 4640 sector_t nr_sectors = bdev_nr_sectors(bdev); 4641 struct f2fs_report_zones_args rep_zone_arg; 4642 u64 zone_sectors; 4643 unsigned int max_open_zones; 4644 int ret; 4645 4646 if (!f2fs_sb_has_blkzoned(sbi)) 4647 return 0; 4648 4649 if (bdev_is_zoned(FDEV(devi).bdev)) { 4650 max_open_zones = bdev_max_open_zones(bdev); 4651 if (max_open_zones && (max_open_zones < sbi->max_open_zones)) 4652 sbi->max_open_zones = max_open_zones; 4653 if (sbi->max_open_zones < F2FS_OPTION(sbi).active_logs) { 4654 f2fs_err(sbi, 4655 "zoned: max open zones %u is too small, need at least %u open zones", 4656 sbi->max_open_zones, F2FS_OPTION(sbi).active_logs); 4657 return -EINVAL; 4658 } 4659 } 4660 4661 zone_sectors = bdev_zone_sectors(bdev); 4662 if (sbi->blocks_per_blkz && sbi->blocks_per_blkz != 4663 SECTOR_TO_BLOCK(sbi, zone_sectors)) 4664 return -EINVAL; 4665 sbi->blocks_per_blkz = SECTOR_TO_BLOCK(sbi, zone_sectors); 4666 FDEV(devi).nr_blkz = div_u64(SECTOR_TO_BLOCK(sbi, nr_sectors), 4667 sbi->blocks_per_blkz); 4668 if (nr_sectors & (zone_sectors - 1)) 4669 FDEV(devi).nr_blkz++; 4670 4671 FDEV(devi).blkz_seq = f2fs_kvzalloc(sbi, 4672 BITS_TO_LONGS(FDEV(devi).nr_blkz) 4673 * sizeof(unsigned long), 4674 GFP_KERNEL); 4675 if (!FDEV(devi).blkz_seq) 4676 return -ENOMEM; 4677 4678 rep_zone_arg.sbi = sbi; 4679 rep_zone_arg.dev = &FDEV(devi); 4680 4681 ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES, f2fs_report_zone_cb, 4682 &rep_zone_arg); 4683 if (ret < 0) 4684 return ret; 4685 return 0; 4686 } 4687 #endif 4688 4689 /* 4690 * Read f2fs raw super block. 4691 * Because we have two copies of super block, so read both of them 4692 * to get the first valid one. If any one of them is broken, we pass 4693 * them recovery flag back to the caller. 4694 */ 4695 static int read_raw_super_block(struct f2fs_sb_info *sbi, 4696 struct f2fs_super_block **raw_super, 4697 int *valid_super_block, int *recovery) 4698 { 4699 struct super_block *sb = sbi->sb; 4700 int block; 4701 struct folio *folio; 4702 struct f2fs_super_block *super; 4703 int err = 0; 4704 4705 super = kzalloc_obj(struct f2fs_super_block); 4706 if (!super) 4707 return -ENOMEM; 4708 4709 for (block = 0; block < 2; block++) { 4710 folio = read_mapping_folio(sb->s_bdev->bd_mapping, block, NULL); 4711 if (IS_ERR(folio)) { 4712 f2fs_err(sbi, "Unable to read %dth superblock", 4713 block + 1); 4714 err = PTR_ERR(folio); 4715 *recovery = 1; 4716 continue; 4717 } 4718 4719 /* sanity checking of raw super */ 4720 err = sanity_check_raw_super(sbi, folio, block); 4721 if (err) { 4722 f2fs_err(sbi, "Can't find valid F2FS filesystem in %dth superblock", 4723 block + 1); 4724 folio_put(folio); 4725 *recovery = 1; 4726 continue; 4727 } 4728 4729 if (!*raw_super) { 4730 memcpy(super, F2FS_SUPER_BLOCK(folio, block), 4731 sizeof(*super)); 4732 *valid_super_block = block; 4733 *raw_super = super; 4734 } 4735 folio_put(folio); 4736 } 4737 4738 /* No valid superblock */ 4739 if (!*raw_super) 4740 kfree(super); 4741 else 4742 err = 0; 4743 4744 return err; 4745 } 4746 4747 int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover) 4748 { 4749 struct folio *folio; 4750 pgoff_t index; 4751 __u32 crc = 0; 4752 int err; 4753 4754 if ((recover && f2fs_readonly(sbi->sb)) || 4755 f2fs_hw_is_readonly(sbi)) { 4756 set_sbi_flag(sbi, SBI_NEED_SB_WRITE); 4757 return -EROFS; 4758 } 4759 4760 /* we should update superblock crc here */ 4761 if (!recover && f2fs_sb_has_sb_chksum(sbi)) { 4762 crc = f2fs_crc32(F2FS_RAW_SUPER(sbi), 4763 offsetof(struct f2fs_super_block, crc)); 4764 F2FS_RAW_SUPER(sbi)->crc = cpu_to_le32(crc); 4765 } 4766 4767 /* write back-up superblock first */ 4768 index = sbi->valid_super_block ? 0 : 1; 4769 folio = read_mapping_folio(sbi->sb->s_bdev->bd_mapping, index, NULL); 4770 if (IS_ERR(folio)) 4771 return PTR_ERR(folio); 4772 err = __f2fs_commit_super(sbi, folio, index, true); 4773 folio_put(folio); 4774 4775 /* if we are in recovery path, skip writing valid superblock */ 4776 if (recover || err) 4777 return err; 4778 4779 /* write current valid superblock */ 4780 index = sbi->valid_super_block; 4781 folio = read_mapping_folio(sbi->sb->s_bdev->bd_mapping, index, NULL); 4782 if (IS_ERR(folio)) 4783 return PTR_ERR(folio); 4784 err = __f2fs_commit_super(sbi, folio, index, true); 4785 folio_put(folio); 4786 return err; 4787 } 4788 4789 static void save_stop_reason(struct f2fs_sb_info *sbi, unsigned char reason) 4790 { 4791 unsigned long flags; 4792 4793 spin_lock_irqsave(&sbi->error_lock, flags); 4794 if (sbi->stop_reason[reason] < GENMASK(BITS_PER_BYTE - 1, 0)) 4795 sbi->stop_reason[reason]++; 4796 sbi->stop_reason_dirty = true; 4797 spin_unlock_irqrestore(&sbi->error_lock, flags); 4798 } 4799 4800 static void f2fs_record_stop_reason(struct f2fs_sb_info *sbi) 4801 { 4802 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi); 4803 unsigned long flags; 4804 bool report_shutdown = false; 4805 int err; 4806 4807 f2fs_down_write(&sbi->sb_lock); 4808 4809 spin_lock_irqsave(&sbi->error_lock, flags); 4810 if (sbi->error_dirty) { 4811 memcpy(raw_super->s_errors, sbi->errors, MAX_F2FS_ERRORS); 4812 sbi->error_dirty = false; 4813 } 4814 memcpy(raw_super->s_stop_reason, sbi->stop_reason, MAX_STOP_REASON); 4815 if (sbi->stop_reason_dirty) { 4816 report_shutdown = true; 4817 sbi->stop_reason_dirty = false; 4818 } 4819 spin_unlock_irqrestore(&sbi->error_lock, flags); 4820 4821 err = f2fs_commit_super(sbi, false); 4822 4823 f2fs_up_write(&sbi->sb_lock); 4824 if (err) 4825 f2fs_err_ratelimited(sbi, 4826 "f2fs_commit_super fails to record stop_reason, err:%d", 4827 err); 4828 4829 if (report_shutdown) 4830 fserror_report_shutdown(sbi->sb, GFP_NOFS); 4831 } 4832 4833 void f2fs_save_errors(struct f2fs_sb_info *sbi, unsigned char flag) 4834 { 4835 unsigned long flags; 4836 4837 spin_lock_irqsave(&sbi->error_lock, flags); 4838 if (!test_bit(flag, (unsigned long *)sbi->errors)) { 4839 set_bit(flag, (unsigned long *)sbi->errors); 4840 sbi->error_dirty = true; 4841 } 4842 spin_unlock_irqrestore(&sbi->error_lock, flags); 4843 } 4844 4845 static void f2fs_report_fserror(struct f2fs_sb_info *sbi, unsigned char error) 4846 { 4847 switch (error) { 4848 case ERROR_INVALID_BLKADDR: 4849 case ERROR_CORRUPTED_INODE: 4850 case ERROR_INCONSISTENT_SUMMARY: 4851 case ERROR_INCONSISTENT_SUM_TYPE: 4852 case ERROR_CORRUPTED_JOURNAL: 4853 case ERROR_INCONSISTENT_NODE_COUNT: 4854 case ERROR_INCONSISTENT_BLOCK_COUNT: 4855 case ERROR_INVALID_CURSEG: 4856 case ERROR_INCONSISTENT_SIT: 4857 case ERROR_INVALID_NODE_REFERENCE: 4858 case ERROR_INCONSISTENT_NAT: 4859 fserror_report_metadata(sbi->sb, -EFSCORRUPTED, GFP_NOFS); 4860 break; 4861 default: 4862 return; 4863 } 4864 } 4865 4866 void f2fs_handle_error(struct f2fs_sb_info *sbi, unsigned char error) 4867 { 4868 f2fs_save_errors(sbi, error); 4869 4870 if (!sbi->error_dirty) 4871 return; 4872 if (!test_bit(error, (unsigned long *)sbi->errors)) 4873 return; 4874 schedule_work(&sbi->s_error_work); 4875 4876 f2fs_report_fserror(sbi, error); 4877 } 4878 4879 static bool system_going_down(void) 4880 { 4881 return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF 4882 || system_state == SYSTEM_RESTART; 4883 } 4884 4885 static void f2fs_handle_critical_error(struct f2fs_sb_info *sbi, 4886 unsigned char reason) 4887 { 4888 struct super_block *sb = sbi->sb; 4889 bool shutdown = reason == STOP_CP_REASON_SHUTDOWN; 4890 bool continue_fs = !shutdown && 4891 F2FS_OPTION(sbi).errors == MOUNT_ERRORS_CONTINUE; 4892 4893 set_ckpt_flags(sbi, CP_ERROR_FLAG); 4894 4895 if (!f2fs_hw_is_readonly(sbi)) { 4896 save_stop_reason(sbi, reason); 4897 4898 /* 4899 * always create an asynchronous task to record stop_reason 4900 * in order to avoid potential deadlock when running into 4901 * f2fs_record_stop_reason() synchronously. 4902 */ 4903 schedule_work(&sbi->s_error_work); 4904 } 4905 4906 /* 4907 * We force ERRORS_RO behavior when system is rebooting. Otherwise we 4908 * could panic during 'reboot -f' as the underlying device got already 4909 * disabled. 4910 */ 4911 if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_PANIC && 4912 !shutdown && !system_going_down() && 4913 !is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN)) 4914 panic("F2FS-fs (device %s): panic forced after error\n", 4915 sb->s_id); 4916 4917 if (shutdown) 4918 set_sbi_flag(sbi, SBI_IS_SHUTDOWN); 4919 else 4920 dump_stack(); 4921 4922 /* 4923 * Continue filesystem operators if errors=continue. Should not set 4924 * RO by shutdown, since RO bypasses thaw_super which can hang the 4925 * system. 4926 */ 4927 if (continue_fs || f2fs_readonly(sb) || shutdown) { 4928 f2fs_warn(sbi, "Stopped filesystem due to reason: %d", reason); 4929 return; 4930 } 4931 4932 f2fs_warn(sbi, "Remounting filesystem read-only"); 4933 4934 /* 4935 * We have already set CP_ERROR_FLAG flag to stop all updates 4936 * to filesystem, so it doesn't need to set SB_RDONLY flag here 4937 * because the flag should be set covered w/ sb->s_umount semaphore 4938 * via remount procedure, otherwise, it will confuse code like 4939 * freeze_super() which will lead to deadlocks and other problems. 4940 */ 4941 } 4942 4943 void f2fs_fault_report(struct super_block *sb, unsigned int err_code, 4944 const char *func, unsigned int data) 4945 { 4946 trace_f2fs_fault_report(sb, err_code, func, data); 4947 } 4948 4949 void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io, 4950 unsigned char reason) 4951 { 4952 if (reason != STOP_CP_REASON_SHUTDOWN) 4953 f2fs_fault_report(sbi->sb, REPORT_FAULT_STOP_CP, __func__, reason); 4954 4955 f2fs_build_fault_attr(sbi, 0, 0, FAULT_ALL); 4956 if (!end_io) 4957 f2fs_flush_merged_writes(sbi); 4958 f2fs_handle_critical_error(sbi, reason); 4959 } 4960 4961 4962 static void f2fs_record_error_work(struct work_struct *work) 4963 { 4964 struct f2fs_sb_info *sbi = container_of(work, 4965 struct f2fs_sb_info, s_error_work); 4966 4967 f2fs_record_stop_reason(sbi); 4968 } 4969 4970 static inline unsigned int get_first_seq_zone_segno(struct f2fs_sb_info *sbi) 4971 { 4972 #ifdef CONFIG_BLK_DEV_ZONED 4973 unsigned int zoneno, total_zones; 4974 int devi; 4975 4976 if (!f2fs_sb_has_blkzoned(sbi)) 4977 return NULL_SEGNO; 4978 4979 for (devi = 0; devi < sbi->s_ndevs; devi++) { 4980 if (!bdev_is_zoned(FDEV(devi).bdev)) 4981 continue; 4982 4983 total_zones = GET_ZONE_FROM_SEG(sbi, FDEV(devi).total_segments); 4984 4985 for (zoneno = 0; zoneno < total_zones; zoneno++) { 4986 unsigned int segs, blks; 4987 4988 if (!f2fs_zone_is_seq(sbi, devi, zoneno)) 4989 continue; 4990 4991 segs = GET_SEG_FROM_SEC(sbi, 4992 zoneno * sbi->secs_per_zone); 4993 blks = SEGS_TO_BLKS(sbi, segs); 4994 return GET_SEGNO(sbi, FDEV(devi).start_blk + blks); 4995 } 4996 } 4997 #endif 4998 return NULL_SEGNO; 4999 } 5000 5001 static int f2fs_scan_devices(struct f2fs_sb_info *sbi) 5002 { 5003 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi); 5004 unsigned int max_devices = MAX_DEVICES; 5005 unsigned int logical_blksize; 5006 blk_mode_t mode = sb_open_mode(sbi->sb->s_flags); 5007 int i; 5008 5009 /* Initialize single device information */ 5010 if (!RDEV(0).path[0]) { 5011 if (!bdev_is_zoned(sbi->sb->s_bdev)) 5012 return 0; 5013 max_devices = 1; 5014 } 5015 5016 /* 5017 * Initialize multiple devices information, or single 5018 * zoned block device information. 5019 */ 5020 sbi->devs = f2fs_kzalloc(sbi, 5021 array_size(max_devices, 5022 sizeof(struct f2fs_dev_info)), 5023 GFP_KERNEL); 5024 if (!sbi->devs) 5025 return -ENOMEM; 5026 5027 logical_blksize = bdev_logical_block_size(sbi->sb->s_bdev); 5028 sbi->aligned_blksize = true; 5029 sbi->bggc_io_aware = AWARE_ALL_IO; 5030 #ifdef CONFIG_BLK_DEV_ZONED 5031 sbi->max_open_zones = UINT_MAX; 5032 sbi->blkzone_alloc_policy = BLKZONE_ALLOC_PRIOR_SEQ; 5033 sbi->bggc_io_aware = AWARE_READ_IO; 5034 #endif 5035 5036 for (i = 0; i < max_devices; i++) { 5037 if (max_devices == 1) { 5038 FDEV(i).total_segments = 5039 le32_to_cpu(raw_super->segment_count_main); 5040 FDEV(i).start_blk = 0; 5041 FDEV(i).end_blk = FDEV(i).total_segments * 5042 BLKS_PER_SEG(sbi); 5043 } 5044 5045 if (i == 0) 5046 FDEV(0).bdev_file = sbi->sb->s_bdev_file; 5047 else if (!RDEV(i).path[0]) 5048 break; 5049 5050 if (max_devices > 1) { 5051 /* Multi-device mount */ 5052 memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN); 5053 FDEV(i).total_segments = 5054 le32_to_cpu(RDEV(i).total_segments); 5055 if (i == 0) { 5056 FDEV(i).start_blk = 0; 5057 FDEV(i).end_blk = FDEV(i).start_blk + 5058 SEGS_TO_BLKS(sbi, 5059 FDEV(i).total_segments) - 1 + 5060 le32_to_cpu(raw_super->segment0_blkaddr); 5061 sbi->allocate_section_hint = FDEV(i).total_segments / 5062 SEGS_PER_SEC(sbi); 5063 } else { 5064 FDEV(i).start_blk = FDEV(i - 1).end_blk + 1; 5065 FDEV(i).end_blk = FDEV(i).start_blk + 5066 SEGS_TO_BLKS(sbi, 5067 FDEV(i).total_segments) - 1; 5068 FDEV(i).bdev_file = fs_bdev_file_open_by_path( 5069 FDEV(i).path, mode, sbi->sb, sbi->sb); 5070 } 5071 } 5072 if (IS_ERR(FDEV(i).bdev_file)) 5073 return PTR_ERR(FDEV(i).bdev_file); 5074 5075 FDEV(i).bdev = file_bdev(FDEV(i).bdev_file); 5076 /* to release errored devices */ 5077 sbi->s_ndevs = i + 1; 5078 5079 if (logical_blksize != bdev_logical_block_size(FDEV(i).bdev)) 5080 sbi->aligned_blksize = false; 5081 5082 #ifdef CONFIG_BLK_DEV_ZONED 5083 if (bdev_is_zoned(FDEV(i).bdev)) { 5084 if (!f2fs_sb_has_blkzoned(sbi)) { 5085 f2fs_err(sbi, "Zoned block device feature not enabled"); 5086 return -EINVAL; 5087 } 5088 if (init_blkz_info(sbi, i)) { 5089 f2fs_err(sbi, "Failed to initialize F2FS blkzone information"); 5090 return -EINVAL; 5091 } 5092 if (max_devices == 1) 5093 break; 5094 f2fs_info(sbi, "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: Host-managed)", 5095 i, FDEV(i).path, 5096 FDEV(i).total_segments, 5097 FDEV(i).start_blk, FDEV(i).end_blk); 5098 continue; 5099 } 5100 #endif 5101 f2fs_info(sbi, "Mount Device [%2d]: %20s, %8u, %8x - %8x", 5102 i, FDEV(i).path, 5103 FDEV(i).total_segments, 5104 FDEV(i).start_blk, FDEV(i).end_blk); 5105 } 5106 return 0; 5107 } 5108 5109 static int f2fs_setup_casefold(struct f2fs_sb_info *sbi) 5110 { 5111 #if IS_ENABLED(CONFIG_UNICODE) 5112 if (f2fs_sb_has_casefold(sbi) && !sbi->sb->s_encoding) { 5113 const struct f2fs_sb_encodings *encoding_info; 5114 struct unicode_map *encoding; 5115 __u16 encoding_flags; 5116 5117 encoding_info = f2fs_sb_read_encoding(sbi->raw_super); 5118 if (!encoding_info) { 5119 f2fs_err(sbi, 5120 "Encoding requested by superblock is unknown"); 5121 return -EINVAL; 5122 } 5123 5124 encoding_flags = le16_to_cpu(sbi->raw_super->s_encoding_flags); 5125 encoding = utf8_load(encoding_info->version); 5126 if (IS_ERR(encoding)) { 5127 f2fs_err(sbi, 5128 "can't mount with superblock charset: %s-%u.%u.%u " 5129 "not supported by the kernel. flags: 0x%x.", 5130 encoding_info->name, 5131 unicode_major(encoding_info->version), 5132 unicode_minor(encoding_info->version), 5133 unicode_rev(encoding_info->version), 5134 encoding_flags); 5135 return PTR_ERR(encoding); 5136 } 5137 f2fs_info(sbi, "Using encoding defined by superblock: " 5138 "%s-%u.%u.%u with flags 0x%hx", encoding_info->name, 5139 unicode_major(encoding_info->version), 5140 unicode_minor(encoding_info->version), 5141 unicode_rev(encoding_info->version), 5142 encoding_flags); 5143 5144 sbi->sb->s_encoding = encoding; 5145 sbi->sb->s_encoding_flags = encoding_flags; 5146 } 5147 #else 5148 if (f2fs_sb_has_casefold(sbi)) { 5149 f2fs_err(sbi, "Filesystem with casefold feature cannot be mounted without CONFIG_UNICODE"); 5150 return -EINVAL; 5151 } 5152 #endif 5153 return 0; 5154 } 5155 5156 static void f2fs_tuning_parameters(struct f2fs_sb_info *sbi) 5157 { 5158 /* adjust parameters according to the volume size */ 5159 if (MAIN_SEGS(sbi) <= SMALL_VOLUME_SEGMENTS) { 5160 if (f2fs_block_unit_discard(sbi)) 5161 SM_I(sbi)->dcc_info->discard_granularity = 5162 MIN_DISCARD_GRANULARITY; 5163 if (!f2fs_lfs_mode(sbi)) 5164 SM_I(sbi)->ipu_policy = BIT(F2FS_IPU_FORCE) | 5165 BIT(F2FS_IPU_HONOR_OPU_WRITE); 5166 } 5167 5168 sbi->readdir_ra = true; 5169 } 5170 5171 static void f2fs_restore_device_alias(struct f2fs_sb_info *sbi) 5172 { 5173 struct inode *root = d_inode(sbi->sb->s_root); 5174 struct f2fs_dir_entry *de; 5175 void *dentry_block = NULL; 5176 int i; 5177 5178 if (!f2fs_sb_has_device_alias(sbi)) 5179 return; 5180 5181 for (i = 1; i < sbi->s_ndevs; i++) { 5182 char *name = strrchr(FDEV(i).path, '/'); 5183 struct inode *inode; 5184 struct qstr qstr; 5185 5186 name = name ? name + 1 : FDEV(i).path; 5187 qstr.name = name; 5188 qstr.len = strlen(name); 5189 5190 de = f2fs_find_entry(root, &qstr, &dentry_block); 5191 if (!de) 5192 continue; 5193 5194 inode = f2fs_iget(sbi->sb, le32_to_cpu(de->ino)); 5195 if (!IS_ERR(inode)) { 5196 if (IS_DEVICE_ALIASING(inode)) 5197 FDEV(i).has_alias = true; 5198 iput(inode); 5199 } 5200 f2fs_put_dentry_block(dentry_block, false); 5201 } 5202 } 5203 5204 static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc) 5205 { 5206 struct f2fs_fs_context *ctx = fc->fs_private; 5207 struct f2fs_sb_info *sbi; 5208 struct f2fs_super_block *raw_super; 5209 struct inode *root; 5210 int err; 5211 bool skip_recovery = false, need_fsck = false; 5212 int recovery, i, valid_super_block; 5213 struct curseg_info *seg_i; 5214 int retry_cnt = 1; 5215 #ifdef CONFIG_QUOTA 5216 bool quota_enabled = false; 5217 #endif 5218 5219 try_onemore: 5220 err = -EINVAL; 5221 raw_super = NULL; 5222 valid_super_block = -1; 5223 recovery = 0; 5224 5225 /* allocate memory for f2fs-specific super block info */ 5226 sbi = kzalloc_obj(struct f2fs_sb_info); 5227 if (!sbi) 5228 return -ENOMEM; 5229 5230 sbi->sb = sb; 5231 5232 /* initialize locks within allocated memory */ 5233 init_f2fs_rwsem_trace(&sbi->gc_lock, sbi, LOCK_NAME_GC_LOCK); 5234 mutex_init(&sbi->writepages); 5235 init_f2fs_rwsem_trace(&sbi->cp_global_sem, sbi, LOCK_NAME_CP_GLOBAL); 5236 #ifdef CONFIG_DEBUG_LOCK_ALLOC 5237 lockdep_register_key(&sbi->cp_global_sem_key); 5238 lockdep_set_class(&sbi->cp_global_sem.internal_rwsem, 5239 &sbi->cp_global_sem_key); 5240 #endif 5241 init_f2fs_rwsem_trace(&sbi->node_write, sbi, LOCK_NAME_NODE_WRITE); 5242 init_f2fs_rwsem_trace(&sbi->node_change, sbi, LOCK_NAME_NODE_CHANGE); 5243 spin_lock_init(&sbi->stat_lock); 5244 init_f2fs_rwsem_trace(&sbi->cp_rwsem, sbi, LOCK_NAME_CP_RWSEM); 5245 init_f2fs_rwsem(&sbi->quota_sem); 5246 init_waitqueue_head(&sbi->cp_wait); 5247 spin_lock_init(&sbi->error_lock); 5248 5249 for (i = 0; i < NR_INODE_TYPE; i++) { 5250 INIT_LIST_HEAD(&sbi->inode_list[i]); 5251 spin_lock_init(&sbi->inode_lock[i]); 5252 } 5253 mutex_init(&sbi->flush_lock); 5254 5255 err = read_raw_super_block(sbi, &raw_super, &valid_super_block, 5256 &recovery); 5257 if (err) 5258 goto free_sbi; 5259 5260 sb->s_fs_info = sbi; 5261 sbi->raw_super = raw_super; 5262 init_sb_info(sbi); 5263 5264 /* set a block size */ 5265 if (unlikely(!sb_set_blocksize(sb, sbi->blocksize))) { 5266 f2fs_err(sbi, "unable to set blocksize %u", sbi->blocksize); 5267 err = -EINVAL; 5268 goto free_sb_buf; 5269 } 5270 sbi->max_atc_write_bio_size = UINT_MAX; 5271 sbi->max_atc_write_bio_entry_cnt = UINT_MAX; 5272 5273 INIT_WORK(&sbi->s_error_work, f2fs_record_error_work); 5274 memcpy(sbi->errors, raw_super->s_errors, MAX_F2FS_ERRORS); 5275 memcpy(sbi->stop_reason, raw_super->s_stop_reason, MAX_STOP_REASON); 5276 5277 /* precompute checksum seed for metadata */ 5278 if (f2fs_sb_has_inode_chksum(sbi)) 5279 sbi->s_chksum_seed = f2fs_chksum(~0, raw_super->uuid, 5280 sizeof(raw_super->uuid)); 5281 5282 default_options(sbi, false); 5283 5284 err = f2fs_check_opt_consistency(fc, sb); 5285 if (err) 5286 goto free_sb_buf; 5287 5288 f2fs_apply_options(fc, sb); 5289 5290 err = f2fs_sanity_check_options(sbi, false); 5291 if (err) 5292 goto free_options; 5293 5294 sb->s_maxbytes = max_file_blocks(sbi, NULL) << 5295 le32_to_cpu(raw_super->log_blocksize); 5296 sb->s_max_links = F2FS_LINK_MAX; 5297 5298 err = f2fs_setup_casefold(sbi); 5299 if (err) 5300 goto free_options; 5301 5302 #ifdef CONFIG_QUOTA 5303 sb->dq_op = &f2fs_quota_operations; 5304 sb->s_qcop = &f2fs_quotactl_ops; 5305 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ; 5306 5307 if (f2fs_sb_has_quota_ino(sbi)) { 5308 for (i = 0; i < MAXQUOTAS; i++) { 5309 if (f2fs_qf_ino(sbi->sb, i)) 5310 sbi->nquota_files++; 5311 } 5312 } 5313 #endif 5314 5315 sb->s_op = &f2fs_sops; 5316 #ifdef CONFIG_FS_ENCRYPTION 5317 sb->s_cop = &f2fs_cryptops; 5318 #endif 5319 #ifdef CONFIG_FS_VERITY 5320 sb->s_vop = &f2fs_verityops; 5321 #endif 5322 sb->s_xattr = f2fs_xattr_handlers; 5323 sb->s_export_op = &f2fs_export_ops; 5324 sb->s_magic = F2FS_SUPER_MAGIC; 5325 sb->s_time_gran = 1; 5326 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) | 5327 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0); 5328 if (test_opt(sbi, INLINECRYPT)) 5329 sb->s_flags |= SB_INLINECRYPT; 5330 5331 if (test_opt(sbi, LAZYTIME)) 5332 sb->s_flags |= SB_LAZYTIME; 5333 else 5334 sb->s_flags &= ~SB_LAZYTIME; 5335 5336 super_set_uuid(sb, (void *) raw_super->uuid, sizeof(raw_super->uuid)); 5337 super_set_sysfs_name_bdev(sb); 5338 sb->s_iflags |= SB_I_CGROUPWB; 5339 5340 /* init f2fs-specific super block info */ 5341 sbi->valid_super_block = valid_super_block; 5342 5343 /* disallow all the data/node/meta page writes */ 5344 set_sbi_flag(sbi, SBI_POR_DOING); 5345 5346 err = f2fs_init_write_merge_io(sbi); 5347 if (err) 5348 goto free_bio_info; 5349 5350 err = f2fs_init_iostat(sbi); 5351 if (err) 5352 goto free_bio_info; 5353 5354 err = init_percpu_info(sbi); 5355 if (err) 5356 goto free_iostat; 5357 5358 err = f2fs_init_page_array_cache(sbi); 5359 if (err) 5360 goto free_percpu; 5361 5362 f2fs_init_cache(sbi, META_CACHE(sbi), F2FS_META_CACHE); 5363 f2fs_init_cache(sbi, NODE_CACHE(sbi), F2FS_NODE_CACHE); 5364 f2fs_init_cache(sbi, COMPRESS_CACHE(sbi), F2FS_COMPRESS_CACHE); 5365 5366 err = f2fs_get_valid_checkpoint(sbi); 5367 if (err) { 5368 f2fs_err(sbi, "Failed to get valid F2FS checkpoint"); 5369 goto free_compress_cache; 5370 } 5371 5372 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_QUOTA_NEED_FSCK_FLAG)) 5373 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR); 5374 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_DISABLED_QUICK_FLAG)) { 5375 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK); 5376 sbi->interval_time[DISABLE_TIME] = DEF_DISABLE_QUICK_INTERVAL; 5377 } 5378 5379 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_FSCK_FLAG)) 5380 set_sbi_flag(sbi, SBI_NEED_FSCK); 5381 5382 /* Initialize device list */ 5383 err = f2fs_scan_devices(sbi); 5384 if (err) { 5385 f2fs_err(sbi, "Failed to find devices"); 5386 goto free_devices; 5387 } 5388 5389 err = f2fs_init_wq(sbi); 5390 if (err) { 5391 f2fs_err(sbi, "Failed to create workqueue"); 5392 goto free_devices; 5393 } 5394 5395 sbi->total_valid_node_count = 5396 le32_to_cpu(sbi->ckpt->valid_node_count); 5397 percpu_counter_set(&sbi->total_valid_inode_count, 5398 le32_to_cpu(sbi->ckpt->valid_inode_count)); 5399 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count); 5400 sbi->total_valid_block_count = 5401 le64_to_cpu(sbi->ckpt->valid_block_count); 5402 sbi->last_valid_block_count = sbi->total_valid_block_count; 5403 sbi->reserved_blocks = 0; 5404 sbi->current_reserved_blocks = 0; 5405 sbi->alias_reserved_blocks = 0; 5406 limit_reserve_root(sbi); 5407 adjust_unusable_cap_perc(sbi); 5408 5409 f2fs_init_extent_cache_info(sbi); 5410 5411 f2fs_init_ino_entry_info(sbi); 5412 5413 f2fs_init_fsync_node_info(sbi); 5414 5415 f2fs_init_compress_cache_context(sbi); 5416 5417 /* setup checkpoint request control and start checkpoint issue thread */ 5418 f2fs_init_ckpt_req_control(sbi); 5419 if (!f2fs_readonly(sb) && !test_opt(sbi, DISABLE_CHECKPOINT) && 5420 test_opt(sbi, MERGE_CHECKPOINT)) { 5421 err = f2fs_start_ckpt_thread(sbi); 5422 if (err) { 5423 f2fs_err(sbi, 5424 "Failed to start F2FS issue_checkpoint_thread (%d)", 5425 err); 5426 goto stop_ckpt_thread; 5427 } 5428 } 5429 5430 /* setup f2fs internal modules */ 5431 err = f2fs_build_segment_manager(sbi); 5432 if (err) { 5433 f2fs_err(sbi, "Failed to initialize F2FS segment manager (%d)", 5434 err); 5435 goto free_sm; 5436 } 5437 err = f2fs_build_node_manager(sbi); 5438 if (err) { 5439 f2fs_err(sbi, "Failed to initialize F2FS node manager (%d)", 5440 err); 5441 goto free_nm; 5442 } 5443 5444 /* For write statistics */ 5445 sbi->sectors_written_start = f2fs_get_sectors_written(sbi); 5446 5447 /* get segno of first zoned block device */ 5448 sbi->first_seq_zone_segno = get_first_seq_zone_segno(sbi); 5449 5450 f2fs_adjust_pinned_area_boundary(sbi); 5451 5452 sbi->reserved_pin_section = f2fs_sb_has_blkzoned(sbi) ? 5453 ZONED_PIN_SEC_REQUIRED_COUNT : 5454 GET_SEC_FROM_SEG(sbi, overprovision_segments(sbi)); 5455 5456 /* Read accumulated write IO statistics if exists */ 5457 seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE); 5458 if (__exist_node_summaries(sbi)) 5459 sbi->kbytes_written = 5460 le64_to_cpu(seg_i->journal->info.kbytes_written); 5461 5462 f2fs_build_gc_manager(sbi); 5463 5464 err = f2fs_build_stats(sbi); 5465 if (err) 5466 goto free_nm; 5467 5468 /* read root inode and dentry */ 5469 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi)); 5470 if (IS_ERR(root)) { 5471 f2fs_err(sbi, "Failed to read root inode"); 5472 err = PTR_ERR(root); 5473 goto free_ino_entry; 5474 } 5475 if (!S_ISDIR(root->i_mode) || !root->i_blocks || 5476 !root->i_size || !root->i_nlink) { 5477 iput(root); 5478 err = -EINVAL; 5479 goto free_ino_entry; 5480 } 5481 5482 generic_set_sb_d_ops(sb); 5483 sb->s_root = d_make_root(root); /* allocate root dentry */ 5484 if (!sb->s_root) { 5485 err = -ENOMEM; 5486 goto free_ino_entry; 5487 } 5488 5489 err = f2fs_register_sysfs(sbi); 5490 if (err) 5491 goto free_root_inode; 5492 5493 sbi->umount_lock_holder = current; 5494 #ifdef CONFIG_QUOTA 5495 /* Enable quota usage during mount */ 5496 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sb)) { 5497 err = f2fs_enable_quotas(sb); 5498 if (err) 5499 f2fs_err(sbi, "Cannot turn on quotas: error %d", err); 5500 } 5501 5502 quota_enabled = f2fs_recover_quota_begin(sbi); 5503 #endif 5504 /* if there are any orphan inodes, free them */ 5505 err = f2fs_recover_orphan_inodes(sbi); 5506 if (err) 5507 goto free_meta; 5508 5509 if (unlikely(is_set_ckpt_flags(sbi, CP_DISABLED_FLAG))) { 5510 skip_recovery = true; 5511 goto reset_checkpoint; 5512 } 5513 5514 /* recover fsynced data */ 5515 if (!test_opt(sbi, DISABLE_ROLL_FORWARD) && 5516 !test_opt(sbi, NORECOVERY)) { 5517 /* 5518 * mount should be failed, when device has readonly mode, and 5519 * previous checkpoint was not done by clean system shutdown. 5520 */ 5521 if (f2fs_hw_is_readonly(sbi)) { 5522 if (!is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) { 5523 err = f2fs_recover_fsync_data(sbi, true); 5524 if (err > 0) { 5525 err = -EROFS; 5526 f2fs_err(sbi, "Need to recover fsync data, but " 5527 "write access unavailable, please try " 5528 "mount w/ disable_roll_forward or norecovery"); 5529 } 5530 if (err < 0) 5531 goto free_meta; 5532 } 5533 f2fs_info(sbi, "write access unavailable, skipping recovery"); 5534 goto reset_checkpoint; 5535 } 5536 5537 if (need_fsck) 5538 set_sbi_flag(sbi, SBI_NEED_FSCK); 5539 5540 if (skip_recovery) 5541 goto reset_checkpoint; 5542 5543 err = f2fs_recover_fsync_data(sbi, false); 5544 if (err < 0) { 5545 if (err != -ENOMEM) 5546 skip_recovery = true; 5547 need_fsck = true; 5548 f2fs_err(sbi, "Cannot recover all fsync data errno=%d", 5549 err); 5550 goto free_meta; 5551 } 5552 } else { 5553 err = f2fs_recover_fsync_data(sbi, true); 5554 if (err > 0) { 5555 if (!f2fs_readonly(sb)) { 5556 f2fs_err(sbi, "Need to recover fsync data"); 5557 err = -EINVAL; 5558 goto free_meta; 5559 } else { 5560 f2fs_info(sbi, "drop all fsynced data"); 5561 err = 0; 5562 } 5563 } 5564 } 5565 5566 reset_checkpoint: 5567 #ifdef CONFIG_QUOTA 5568 f2fs_recover_quota_end(sbi, quota_enabled); 5569 #endif 5570 /* 5571 * If the f2fs is not readonly and fsync data recovery succeeds, 5572 * write pointer consistency of cursegs and other zones are already 5573 * checked and fixed during recovery. However, if recovery fails, 5574 * write pointers are left untouched, and retry-mount should check 5575 * them here. 5576 */ 5577 if (skip_recovery) 5578 err = f2fs_check_and_fix_write_pointer(sbi); 5579 if (err) 5580 goto free_meta; 5581 5582 /* f2fs_recover_fsync_data() cleared this already */ 5583 clear_sbi_flag(sbi, SBI_POR_DOING); 5584 5585 err = f2fs_init_inmem_curseg(sbi); 5586 if (err) 5587 goto sync_free_meta; 5588 5589 if (test_opt(sbi, DISABLE_CHECKPOINT)) 5590 err = f2fs_disable_checkpoint(sbi); 5591 else if (is_set_ckpt_flags(sbi, CP_DISABLED_FLAG)) 5592 err = f2fs_enable_checkpoint(sbi); 5593 if (err) 5594 goto sync_free_meta; 5595 5596 /* 5597 * If filesystem is not mounted as read-only then 5598 * do start the gc_thread. 5599 */ 5600 if ((F2FS_OPTION(sbi).bggc_mode != BGGC_MODE_OFF || 5601 test_opt(sbi, GC_MERGE)) && !f2fs_readonly(sb)) { 5602 /* After POR, we can run background GC thread.*/ 5603 err = f2fs_start_gc_thread(sbi); 5604 if (err) 5605 goto sync_free_meta; 5606 } 5607 5608 if (!f2fs_readonly(sb)) { 5609 err = f2fs_start_cache_wb_thread(sbi); 5610 if (err) 5611 goto stop_gc_thread; 5612 } 5613 5614 /* recover broken superblock */ 5615 if (recovery) { 5616 err = f2fs_commit_super(sbi, true); 5617 f2fs_info(sbi, "Try to recover %dth superblock, ret: %d", 5618 sbi->valid_super_block ? 1 : 2, err); 5619 } 5620 5621 f2fs_join_shrinker(sbi); 5622 5623 f2fs_tuning_parameters(sbi); 5624 5625 f2fs_notice(sbi, "Mounted with checkpoint version = %llx", 5626 cur_cp_version(F2FS_CKPT(sbi))); 5627 f2fs_update_time(sbi, CP_TIME); 5628 f2fs_update_time(sbi, REQ_TIME); 5629 clear_sbi_flag(sbi, SBI_CP_DISABLED_QUICK); 5630 5631 f2fs_restore_device_alias(sbi); 5632 5633 sbi->umount_lock_holder = NULL; 5634 return 0; 5635 5636 stop_gc_thread: 5637 f2fs_stop_gc_thread(sbi); 5638 sync_free_meta: 5639 /* safe to flush all the data */ 5640 sync_filesystem(sbi->sb); 5641 retry_cnt = 0; 5642 5643 free_meta: 5644 #ifdef CONFIG_QUOTA 5645 f2fs_truncate_quota_inode_pages(sb); 5646 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sb)) 5647 f2fs_quota_off_umount(sbi->sb); 5648 #endif 5649 /* 5650 * Some dirty meta pages can be produced by f2fs_recover_orphan_inodes() 5651 * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg() 5652 * followed by f2fs_write_checkpoint() through f2fs_write_node_pages(), which 5653 * falls into an infinite loop in f2fs_sync_meta_caches(). 5654 */ 5655 f2fs_truncate_meta_caches(sbi, 0, ULONG_MAX); 5656 /* evict some inodes being cached by GC */ 5657 evict_inodes(sb); 5658 f2fs_unregister_sysfs(sbi); 5659 free_root_inode: 5660 dput(sb->s_root); 5661 sb->s_root = NULL; 5662 free_ino_entry: 5663 f2fs_release_ino_entry(sbi, true); 5664 f2fs_truncate_node_caches(sbi, 0, ULONG_MAX); 5665 f2fs_destroy_stats(sbi); 5666 free_nm: 5667 /* stop discard thread before destroying node manager */ 5668 f2fs_stop_discard_thread(sbi); 5669 f2fs_destroy_node_manager(sbi); 5670 free_sm: 5671 f2fs_destroy_segment_manager(sbi); 5672 stop_ckpt_thread: 5673 f2fs_stop_ckpt_thread(sbi); 5674 /* flush s_error_work before sbi destroy */ 5675 flush_work(&sbi->s_error_work); 5676 f2fs_destroy_wq(sbi); 5677 free_devices: 5678 destroy_device_list(sbi); 5679 kvfree(sbi->ckpt); 5680 free_compress_cache: 5681 f2fs_destroy_cache(COMPRESS_CACHE(sbi)); 5682 f2fs_destroy_cache(NODE_CACHE(sbi)); 5683 f2fs_destroy_cache(META_CACHE(sbi)); 5684 f2fs_destroy_page_array_cache(sbi); 5685 free_percpu: 5686 destroy_percpu_info(sbi); 5687 free_iostat: 5688 f2fs_destroy_iostat(sbi); 5689 free_bio_info: 5690 for (i = 0; i < NR_PAGE_TYPE; i++) 5691 kfree(sbi->write_io[i]); 5692 5693 #if IS_ENABLED(CONFIG_UNICODE) 5694 utf8_unload(sb->s_encoding); 5695 sb->s_encoding = NULL; 5696 #endif 5697 free_options: 5698 #ifdef CONFIG_QUOTA 5699 for (i = 0; i < MAXQUOTAS; i++) 5700 kfree(F2FS_OPTION(sbi).s_qf_names[i]); 5701 #endif 5702 /* no need to free dummy_enc_policy, we just keep it in ctx when failed */ 5703 swap(F2FS_CTX_INFO(ctx).dummy_enc_policy, F2FS_OPTION(sbi).dummy_enc_policy); 5704 free_sb_buf: 5705 kfree(raw_super); 5706 free_sbi: 5707 #ifdef CONFIG_DEBUG_LOCK_ALLOC 5708 lockdep_unregister_key(&sbi->cp_global_sem_key); 5709 #endif 5710 kfree(sbi); 5711 sb->s_fs_info = NULL; 5712 5713 /* give only one another chance */ 5714 if (retry_cnt > 0 && skip_recovery) { 5715 retry_cnt--; 5716 shrink_dcache_sb(sb); 5717 goto try_onemore; 5718 } 5719 return err; 5720 } 5721 5722 static int f2fs_get_tree(struct fs_context *fc) 5723 { 5724 return get_tree_bdev(fc, f2fs_fill_super); 5725 } 5726 5727 static int f2fs_reconfigure(struct fs_context *fc) 5728 { 5729 struct super_block *sb = fc->root->d_sb; 5730 5731 return __f2fs_remount(fc, sb); 5732 } 5733 5734 static void f2fs_fc_free(struct fs_context *fc) 5735 { 5736 struct f2fs_fs_context *ctx = fc->fs_private; 5737 5738 if (!ctx) 5739 return; 5740 5741 #ifdef CONFIG_QUOTA 5742 f2fs_unnote_qf_name_all(fc); 5743 #endif 5744 fscrypt_free_dummy_policy(&F2FS_CTX_INFO(ctx).dummy_enc_policy); 5745 kfree(ctx); 5746 } 5747 5748 static const struct fs_context_operations f2fs_context_ops = { 5749 .parse_param = f2fs_parse_param, 5750 .get_tree = f2fs_get_tree, 5751 .reconfigure = f2fs_reconfigure, 5752 .free = f2fs_fc_free, 5753 }; 5754 5755 static void kill_f2fs_super(struct super_block *sb) 5756 { 5757 struct f2fs_sb_info *sbi = F2FS_SB(sb); 5758 5759 if (sb->s_root) { 5760 sbi->umount_lock_holder = current; 5761 5762 set_sbi_flag(sbi, SBI_IS_CLOSE); 5763 f2fs_stop_gc_thread(sbi); 5764 f2fs_stop_discard_thread(sbi); 5765 5766 #ifdef CONFIG_F2FS_FS_COMPRESSION 5767 /* 5768 * latter evict_inode() can bypass checking and invalidating 5769 * compress inode cache. 5770 */ 5771 if (test_opt(sbi, COMPRESS_CACHE)) 5772 f2fs_invalidate_compress_pages_range(sbi, 5773 0, UINT_MAX); 5774 #endif 5775 5776 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) || 5777 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) { 5778 struct cp_control cpc = { 5779 .reason = CP_UMOUNT, 5780 }; 5781 stat_inc_cp_call_count(sbi, TOTAL_CALL); 5782 f2fs_write_checkpoint(sbi, &cpc); 5783 } 5784 5785 if (is_sbi_flag_set(sbi, SBI_IS_RECOVERED) && f2fs_readonly(sb)) 5786 sb->s_flags &= ~SB_RDONLY; 5787 } 5788 kill_block_super(sb); 5789 /* Release block devices last, after fscrypt_destroy_keyring(). */ 5790 if (sbi) { 5791 destroy_device_list(sbi); 5792 #ifdef CONFIG_DEBUG_LOCK_ALLOC 5793 lockdep_unregister_key(&sbi->cp_global_sem_key); 5794 #endif 5795 kfree(sbi); 5796 sb->s_fs_info = NULL; 5797 } 5798 } 5799 5800 static int f2fs_init_fs_context(struct fs_context *fc) 5801 { 5802 struct f2fs_fs_context *ctx; 5803 5804 ctx = kzalloc_obj(struct f2fs_fs_context); 5805 if (!ctx) 5806 return -ENOMEM; 5807 5808 fc->fs_private = ctx; 5809 fc->ops = &f2fs_context_ops; 5810 5811 return 0; 5812 } 5813 5814 static struct file_system_type f2fs_fs_type = { 5815 .owner = THIS_MODULE, 5816 .name = "f2fs", 5817 .init_fs_context = f2fs_init_fs_context, 5818 .kill_sb = kill_f2fs_super, 5819 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP | FS_MGTIME, 5820 }; 5821 MODULE_ALIAS_FS("f2fs"); 5822 5823 static int __init init_inodecache(void) 5824 { 5825 f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache", 5826 sizeof(struct f2fs_inode_info), 0, 5827 SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL); 5828 return f2fs_inode_cachep ? 0 : -ENOMEM; 5829 } 5830 5831 static void destroy_inodecache(void) 5832 { 5833 /* 5834 * Make sure all delayed rcu free inodes are flushed before we 5835 * destroy cache. 5836 */ 5837 rcu_barrier(); 5838 kmem_cache_destroy(f2fs_inode_cachep); 5839 } 5840 5841 static int __init init_f2fs_fs(void) 5842 { 5843 int err; 5844 5845 err = init_inodecache(); 5846 if (err) 5847 goto fail; 5848 err = f2fs_create_node_manager_caches(); 5849 if (err) 5850 goto free_inodecache; 5851 err = f2fs_create_segment_manager_caches(); 5852 if (err) 5853 goto free_node_manager_caches; 5854 err = f2fs_create_checkpoint_caches(); 5855 if (err) 5856 goto free_segment_manager_caches; 5857 err = f2fs_create_recovery_cache(); 5858 if (err) 5859 goto free_checkpoint_caches; 5860 err = f2fs_create_extent_cache(); 5861 if (err) 5862 goto free_recovery_cache; 5863 err = f2fs_create_garbage_collection_cache(); 5864 if (err) 5865 goto free_extent_cache; 5866 err = f2fs_init_sysfs(); 5867 if (err) 5868 goto free_garbage_collection_cache; 5869 err = f2fs_init_shrinker(); 5870 if (err) 5871 goto free_sysfs; 5872 f2fs_create_root_stats(); 5873 err = f2fs_init_post_read_processing(); 5874 if (err) 5875 goto free_root_stats; 5876 err = f2fs_init_iostat_processing(); 5877 if (err) 5878 goto free_post_read; 5879 err = f2fs_init_bio_entry_cache(); 5880 if (err) 5881 goto free_iostat; 5882 err = f2fs_init_bioset(); 5883 if (err) 5884 goto free_bio_entry_cache; 5885 err = f2fs_init_compress_mempool(); 5886 if (err) 5887 goto free_bioset; 5888 err = f2fs_init_compress_cache(); 5889 if (err) 5890 goto free_compress_mempool; 5891 err = f2fs_create_casefold_cache(); 5892 if (err) 5893 goto free_compress_cache; 5894 err = f2fs_init_xattr_cache(); 5895 if (err) 5896 goto free_casefold_cache; 5897 err = f2fs_init_evict_inode_work(); 5898 if (err) 5899 goto free_xattr_cache; 5900 err = register_filesystem(&f2fs_fs_type); 5901 if (err) 5902 goto free_evict_inode_cache; 5903 return 0; 5904 5905 free_evict_inode_cache: 5906 f2fs_destroy_evict_inode_work(); 5907 free_xattr_cache: 5908 f2fs_destroy_xattr_cache(); 5909 free_casefold_cache: 5910 f2fs_destroy_casefold_cache(); 5911 free_compress_cache: 5912 f2fs_destroy_compress_cache(); 5913 free_compress_mempool: 5914 f2fs_destroy_compress_mempool(); 5915 free_bioset: 5916 f2fs_destroy_bioset(); 5917 free_bio_entry_cache: 5918 f2fs_destroy_bio_entry_cache(); 5919 free_iostat: 5920 f2fs_destroy_iostat_processing(); 5921 free_post_read: 5922 f2fs_destroy_post_read_processing(); 5923 free_root_stats: 5924 f2fs_destroy_root_stats(); 5925 f2fs_exit_shrinker(); 5926 free_sysfs: 5927 f2fs_exit_sysfs(); 5928 free_garbage_collection_cache: 5929 f2fs_destroy_garbage_collection_cache(); 5930 free_extent_cache: 5931 f2fs_destroy_extent_cache(); 5932 free_recovery_cache: 5933 f2fs_destroy_recovery_cache(); 5934 free_checkpoint_caches: 5935 f2fs_destroy_checkpoint_caches(); 5936 free_segment_manager_caches: 5937 f2fs_destroy_segment_manager_caches(); 5938 free_node_manager_caches: 5939 f2fs_destroy_node_manager_caches(); 5940 free_inodecache: 5941 destroy_inodecache(); 5942 fail: 5943 return err; 5944 } 5945 5946 static void __exit exit_f2fs_fs(void) 5947 { 5948 unregister_filesystem(&f2fs_fs_type); 5949 f2fs_destroy_evict_inode_work(); 5950 f2fs_destroy_xattr_cache(); 5951 f2fs_destroy_casefold_cache(); 5952 f2fs_destroy_compress_cache(); 5953 f2fs_destroy_compress_mempool(); 5954 f2fs_destroy_bioset(); 5955 f2fs_destroy_bio_entry_cache(); 5956 f2fs_destroy_iostat_processing(); 5957 f2fs_destroy_post_read_processing(); 5958 f2fs_destroy_root_stats(); 5959 f2fs_exit_shrinker(); 5960 f2fs_exit_sysfs(); 5961 f2fs_destroy_garbage_collection_cache(); 5962 f2fs_destroy_extent_cache(); 5963 f2fs_destroy_recovery_cache(); 5964 f2fs_destroy_checkpoint_caches(); 5965 f2fs_destroy_segment_manager_caches(); 5966 f2fs_destroy_node_manager_caches(); 5967 destroy_inodecache(); 5968 } 5969 5970 module_init(init_f2fs_fs) 5971 module_exit(exit_f2fs_fs) 5972 5973 MODULE_AUTHOR("Samsung Electronics's Praesto Team"); 5974 MODULE_DESCRIPTION("Flash Friendly File System"); 5975 MODULE_LICENSE("GPL"); 5976