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