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 43 static struct kmem_cache *f2fs_inode_cachep; 44 45 #ifdef CONFIG_F2FS_FAULT_INJECTION 46 47 const 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 74 int 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 */ 104 static struct shrinker *f2fs_shrinker_info; 105 106 static 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 120 static void f2fs_exit_shrinker(void) 121 { 122 shrinker_free(f2fs_shrinker_info); 123 } 124 125 enum { 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 189 static 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 196 static 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 204 static 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 211 static const struct constant_table f2fs_param_alloc_mode[] = { 212 {"default", ALLOC_MODE_DEFAULT}, 213 {"reuse", ALLOC_MODE_REUSE}, 214 {} 215 }; 216 static 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 223 static const struct constant_table f2fs_param_compress_mode[] = { 224 {"fs", COMPR_MODE_FS}, 225 {"user", COMPR_MODE_USER}, 226 {} 227 }; 228 229 static 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 236 static const struct constant_table f2fs_param_memory_mode[] = { 237 {"normal", MEMORY_MODE_NORMAL}, 238 {"low", MEMORY_MODE_LOW}, 239 {} 240 }; 241 242 static 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 249 static const struct constant_table f2fs_param_lookup_mode[] = { 250 {"perf", LOOKUP_PERF}, 251 {"compat", LOOKUP_COMPAT}, 252 {"auto", LOOKUP_AUTO}, 253 {} 254 }; 255 256 static 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 */ 318 static 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 353 struct f2fs_fs_context { 354 struct f2fs_mount_info info; 355 unsigned int 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 362 static inline void ctx_set_opt(struct f2fs_fs_context *ctx, 363 unsigned int flag) 364 { 365 ctx->info.opt |= flag; 366 ctx->opt_mask |= flag; 367 } 368 369 static inline void ctx_clear_opt(struct f2fs_fs_context *ctx, 370 unsigned int flag) 371 { 372 ctx->info.opt &= ~flag; 373 ctx->opt_mask |= flag; 374 } 375 376 static inline bool ctx_test_opt(struct f2fs_fs_context *ctx, 377 unsigned int flag) 378 { 379 return ctx->info.opt & flag; 380 } 381 382 void 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) 413 static 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 421 static const struct f2fs_sb_encodings * 422 f2fs_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 434 struct kmem_cache *f2fs_cf_name_slab; 435 static 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 442 static void f2fs_destroy_casefold_cache(void) 443 { 444 kmem_cache_destroy(f2fs_cf_name_slab); 445 } 446 #else 447 static int __init f2fs_create_casefold_cache(void) { return 0; } 448 static void f2fs_destroy_casefold_cache(void) { } 449 #endif 450 451 static 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 483 static 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 499 static 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 513 static const char * const quotatypes[] = INITQFNAMES; 514 #define QTYPE2NAME(t) (quotatypes[t]) 515 /* 516 * Note the name of the specified quota file. 517 */ 518 static 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 */ 553 static 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 563 static 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 572 static 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 598 static 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 */ 628 static 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 659 static 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 700 static 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 738 static 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 */ 1193 static 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 1287 err_jquota_change: 1288 f2fs_err(sbi, "Cannot change journaled quota options when quota turned on"); 1289 return -EINVAL; 1290 err_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 1311 static 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 1341 static 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 1351 static 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 1361 static 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 &= ~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 1431 static 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 & 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 & 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 &= ~F2FS_MOUNT_DISCARD; 1454 } 1455 1456 if (f2fs_sb_has_device_alias(sbi) && 1457 (ctx->opt_mask & 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 & 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 &= ~F2FS_MOUNT_RESERVE_ROOT; 1470 } 1471 if (test_opt(sbi, RESERVE_NODE) && 1472 (ctx->opt_mask & 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 &= ~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 1556 static 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 1592 static 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 1606 static 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 1660 static 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 1718 static 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 1745 static 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 init_f2fs_rwsem(&fi->i_sem); 1763 spin_lock_init(&fi->i_size_lock); 1764 INIT_LIST_HEAD(&fi->dirty_list); 1765 INIT_LIST_HEAD(&fi->gdirty_list); 1766 INIT_LIST_HEAD(&fi->gdonate_list); 1767 init_f2fs_rwsem(&fi->i_gc_rwsem[READ]); 1768 init_f2fs_rwsem(&fi->i_gc_rwsem[WRITE]); 1769 init_f2fs_rwsem(&fi->i_xattr_sem); 1770 1771 /* Will be used by directory only */ 1772 fi->i_dir_level = F2FS_SB(sb)->dir_level; 1773 1774 return &fi->vfs_inode; 1775 } 1776 1777 static int f2fs_drop_inode(struct inode *inode) 1778 { 1779 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1780 int ret; 1781 1782 /* 1783 * during filesystem shutdown, if checkpoint is disabled, 1784 * drop useless meta/node dirty pages. 1785 */ 1786 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) { 1787 if (inode->i_ino == F2FS_NODE_INO(sbi) || 1788 inode->i_ino == F2FS_META_INO(sbi)) { 1789 trace_f2fs_drop_inode(inode, 1); 1790 return 1; 1791 } 1792 } 1793 1794 /* 1795 * This is to avoid a deadlock condition like below. 1796 * writeback_single_inode(inode) 1797 * - f2fs_write_data_page 1798 * - f2fs_gc -> iput -> evict 1799 * - inode_wait_for_writeback(inode) 1800 */ 1801 if ((!inode_unhashed(inode) && inode->i_state & I_SYNC)) { 1802 if (!inode->i_nlink && !is_bad_inode(inode)) { 1803 /* to avoid evict_inode call simultaneously */ 1804 __iget(inode); 1805 spin_unlock(&inode->i_lock); 1806 1807 /* should remain fi->extent_tree for writepage */ 1808 f2fs_destroy_extent_node(inode); 1809 1810 sb_start_intwrite(inode->i_sb); 1811 f2fs_i_size_write(inode, 0); 1812 1813 f2fs_submit_merged_write_cond(F2FS_I_SB(inode), 1814 inode, NULL, 0, DATA); 1815 truncate_inode_pages_final(inode->i_mapping); 1816 1817 if (F2FS_HAS_BLOCKS(inode)) 1818 f2fs_truncate(inode); 1819 1820 sb_end_intwrite(inode->i_sb); 1821 1822 spin_lock(&inode->i_lock); 1823 iput(inode); 1824 } 1825 trace_f2fs_drop_inode(inode, 0); 1826 return 0; 1827 } 1828 ret = inode_generic_drop(inode); 1829 if (!ret) 1830 ret = fscrypt_drop_inode(inode); 1831 trace_f2fs_drop_inode(inode, ret); 1832 return ret; 1833 } 1834 1835 int f2fs_inode_dirtied(struct inode *inode, bool sync) 1836 { 1837 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1838 int ret = 0; 1839 1840 spin_lock(&sbi->inode_lock[DIRTY_META]); 1841 if (is_inode_flag_set(inode, FI_DIRTY_INODE)) { 1842 ret = 1; 1843 } else { 1844 set_inode_flag(inode, FI_DIRTY_INODE); 1845 stat_inc_dirty_inode(sbi, DIRTY_META); 1846 } 1847 if (sync && list_empty(&F2FS_I(inode)->gdirty_list)) { 1848 list_add_tail(&F2FS_I(inode)->gdirty_list, 1849 &sbi->inode_list[DIRTY_META]); 1850 inc_page_count(sbi, F2FS_DIRTY_IMETA); 1851 } 1852 spin_unlock(&sbi->inode_lock[DIRTY_META]); 1853 1854 /* if atomic write is not committed, set inode w/ atomic dirty */ 1855 if (!ret && f2fs_is_atomic_file(inode) && 1856 !is_inode_flag_set(inode, FI_ATOMIC_COMMITTED)) 1857 set_inode_flag(inode, FI_ATOMIC_DIRTIED); 1858 1859 return ret; 1860 } 1861 1862 void f2fs_inode_synced(struct inode *inode) 1863 { 1864 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1865 1866 spin_lock(&sbi->inode_lock[DIRTY_META]); 1867 if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) { 1868 spin_unlock(&sbi->inode_lock[DIRTY_META]); 1869 return; 1870 } 1871 if (!list_empty(&F2FS_I(inode)->gdirty_list)) { 1872 list_del_init(&F2FS_I(inode)->gdirty_list); 1873 dec_page_count(sbi, F2FS_DIRTY_IMETA); 1874 } 1875 clear_inode_flag(inode, FI_DIRTY_INODE); 1876 clear_inode_flag(inode, FI_AUTO_RECOVER); 1877 stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META); 1878 spin_unlock(&sbi->inode_lock[DIRTY_META]); 1879 } 1880 1881 /* 1882 * f2fs_dirty_inode() is called from __mark_inode_dirty() 1883 * 1884 * We should call set_dirty_inode to write the dirty inode through write_inode. 1885 */ 1886 static void f2fs_dirty_inode(struct inode *inode, int flags) 1887 { 1888 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1889 1890 if (inode->i_ino == F2FS_NODE_INO(sbi) || 1891 inode->i_ino == F2FS_META_INO(sbi)) 1892 return; 1893 1894 if (is_inode_flag_set(inode, FI_AUTO_RECOVER)) 1895 clear_inode_flag(inode, FI_AUTO_RECOVER); 1896 1897 f2fs_inode_dirtied(inode, false); 1898 } 1899 1900 static void f2fs_free_inode(struct inode *inode) 1901 { 1902 fscrypt_free_inode(inode); 1903 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode)); 1904 } 1905 1906 static void destroy_percpu_info(struct f2fs_sb_info *sbi) 1907 { 1908 percpu_counter_destroy(&sbi->total_valid_inode_count); 1909 percpu_counter_destroy(&sbi->rf_node_block_count); 1910 percpu_counter_destroy(&sbi->alloc_valid_block_count); 1911 } 1912 1913 static void destroy_device_list(struct f2fs_sb_info *sbi) 1914 { 1915 int i; 1916 1917 for (i = 0; i < sbi->s_ndevs; i++) { 1918 if (i > 0) 1919 bdev_fput(FDEV(i).bdev_file); 1920 #ifdef CONFIG_BLK_DEV_ZONED 1921 kvfree(FDEV(i).blkz_seq); 1922 #endif 1923 } 1924 kvfree(sbi->devs); 1925 } 1926 1927 static void f2fs_put_super(struct super_block *sb) 1928 { 1929 struct f2fs_sb_info *sbi = F2FS_SB(sb); 1930 int i; 1931 int err = 0; 1932 bool done; 1933 1934 /* unregister procfs/sysfs entries in advance to avoid race case */ 1935 f2fs_unregister_sysfs(sbi); 1936 1937 f2fs_quota_off_umount(sb); 1938 1939 /* prevent remaining shrinker jobs */ 1940 mutex_lock(&sbi->umount_mutex); 1941 1942 /* 1943 * flush all issued checkpoints and stop checkpoint issue thread. 1944 * after then, all checkpoints should be done by each process context. 1945 */ 1946 f2fs_stop_ckpt_thread(sbi); 1947 1948 /* 1949 * We don't need to do checkpoint when superblock is clean. 1950 * But, the previous checkpoint was not done by umount, it needs to do 1951 * clean checkpoint again. 1952 */ 1953 if ((is_sbi_flag_set(sbi, SBI_IS_DIRTY) || 1954 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG))) { 1955 struct cp_control cpc = { 1956 .reason = CP_UMOUNT, 1957 }; 1958 stat_inc_cp_call_count(sbi, TOTAL_CALL); 1959 err = f2fs_write_checkpoint(sbi, &cpc); 1960 } 1961 1962 /* be sure to wait for any on-going discard commands */ 1963 done = f2fs_issue_discard_timeout(sbi); 1964 if (f2fs_realtime_discard_enable(sbi) && !sbi->discard_blks && done) { 1965 struct cp_control cpc = { 1966 .reason = CP_UMOUNT | CP_TRIMMED, 1967 }; 1968 stat_inc_cp_call_count(sbi, TOTAL_CALL); 1969 err = f2fs_write_checkpoint(sbi, &cpc); 1970 } 1971 1972 /* 1973 * normally superblock is clean, so we need to release this. 1974 * In addition, EIO will skip do checkpoint, we need this as well. 1975 */ 1976 f2fs_release_ino_entry(sbi, true); 1977 1978 f2fs_leave_shrinker(sbi); 1979 mutex_unlock(&sbi->umount_mutex); 1980 1981 /* our cp_error case, we can wait for any writeback page */ 1982 f2fs_flush_merged_writes(sbi); 1983 1984 f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA); 1985 1986 if (err || f2fs_cp_error(sbi)) { 1987 truncate_inode_pages_final(NODE_MAPPING(sbi)); 1988 truncate_inode_pages_final(META_MAPPING(sbi)); 1989 } 1990 1991 for (i = 0; i < NR_COUNT_TYPE; i++) { 1992 if (!get_pages(sbi, i)) 1993 continue; 1994 f2fs_err(sbi, "detect filesystem reference count leak during " 1995 "umount, type: %d, count: %lld", i, get_pages(sbi, i)); 1996 f2fs_bug_on(sbi, 1); 1997 } 1998 1999 f2fs_bug_on(sbi, sbi->fsync_node_num); 2000 2001 f2fs_destroy_compress_inode(sbi); 2002 2003 iput(sbi->node_inode); 2004 sbi->node_inode = NULL; 2005 2006 iput(sbi->meta_inode); 2007 sbi->meta_inode = NULL; 2008 2009 /* 2010 * iput() can update stat information, if f2fs_write_checkpoint() 2011 * above failed with error. 2012 */ 2013 f2fs_destroy_stats(sbi); 2014 2015 /* destroy f2fs internal modules */ 2016 f2fs_destroy_node_manager(sbi); 2017 f2fs_destroy_segment_manager(sbi); 2018 2019 /* flush s_error_work before sbi destroy */ 2020 flush_work(&sbi->s_error_work); 2021 2022 f2fs_destroy_post_read_wq(sbi); 2023 2024 kvfree(sbi->ckpt); 2025 2026 kfree(sbi->raw_super); 2027 2028 f2fs_destroy_page_array_cache(sbi); 2029 f2fs_destroy_xattr_caches(sbi); 2030 #ifdef CONFIG_QUOTA 2031 for (i = 0; i < MAXQUOTAS; i++) 2032 kfree(F2FS_OPTION(sbi).s_qf_names[i]); 2033 #endif 2034 fscrypt_free_dummy_policy(&F2FS_OPTION(sbi).dummy_enc_policy); 2035 destroy_percpu_info(sbi); 2036 f2fs_destroy_iostat(sbi); 2037 for (i = 0; i < NR_PAGE_TYPE; i++) 2038 kfree(sbi->write_io[i]); 2039 #if IS_ENABLED(CONFIG_UNICODE) 2040 utf8_unload(sb->s_encoding); 2041 #endif 2042 } 2043 2044 int f2fs_sync_fs(struct super_block *sb, int sync) 2045 { 2046 struct f2fs_sb_info *sbi = F2FS_SB(sb); 2047 int err = 0; 2048 2049 if (unlikely(f2fs_cp_error(sbi))) 2050 return 0; 2051 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) 2052 return 0; 2053 2054 trace_f2fs_sync_fs(sb, sync); 2055 2056 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) 2057 return -EAGAIN; 2058 2059 if (sync) { 2060 stat_inc_cp_call_count(sbi, TOTAL_CALL); 2061 err = f2fs_issue_checkpoint(sbi); 2062 } 2063 2064 return err; 2065 } 2066 2067 static int f2fs_freeze(struct super_block *sb) 2068 { 2069 struct f2fs_sb_info *sbi = F2FS_SB(sb); 2070 2071 if (f2fs_readonly(sb)) 2072 return 0; 2073 2074 /* IO error happened before */ 2075 if (unlikely(f2fs_cp_error(sbi))) 2076 return -EIO; 2077 2078 /* must be clean, since sync_filesystem() was already called */ 2079 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY)) 2080 return -EINVAL; 2081 2082 sbi->umount_lock_holder = current; 2083 2084 /* Let's flush checkpoints and stop the thread. */ 2085 f2fs_flush_ckpt_thread(sbi); 2086 2087 sbi->umount_lock_holder = NULL; 2088 2089 /* to avoid deadlock on f2fs_evict_inode->SB_FREEZE_FS */ 2090 set_sbi_flag(sbi, SBI_IS_FREEZING); 2091 return 0; 2092 } 2093 2094 static int f2fs_unfreeze(struct super_block *sb) 2095 { 2096 struct f2fs_sb_info *sbi = F2FS_SB(sb); 2097 2098 /* 2099 * It will update discard_max_bytes of mounted lvm device to zero 2100 * after creating snapshot on this lvm device, let's drop all 2101 * remained discards. 2102 * We don't need to disable real-time discard because discard_max_bytes 2103 * will recover after removal of snapshot. 2104 */ 2105 if (test_opt(sbi, DISCARD) && !f2fs_hw_support_discard(sbi)) 2106 f2fs_issue_discard_timeout(sbi); 2107 2108 clear_sbi_flag(F2FS_SB(sb), SBI_IS_FREEZING); 2109 return 0; 2110 } 2111 2112 #ifdef CONFIG_QUOTA 2113 static int f2fs_statfs_project(struct super_block *sb, 2114 kprojid_t projid, struct kstatfs *buf) 2115 { 2116 struct kqid qid; 2117 struct dquot *dquot; 2118 u64 limit; 2119 u64 curblock; 2120 2121 qid = make_kqid_projid(projid); 2122 dquot = dqget(sb, qid); 2123 if (IS_ERR(dquot)) 2124 return PTR_ERR(dquot); 2125 spin_lock(&dquot->dq_dqb_lock); 2126 2127 limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit, 2128 dquot->dq_dqb.dqb_bhardlimit); 2129 limit >>= sb->s_blocksize_bits; 2130 2131 if (limit) { 2132 uint64_t remaining = 0; 2133 2134 curblock = (dquot->dq_dqb.dqb_curspace + 2135 dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits; 2136 if (limit > curblock) 2137 remaining = limit - curblock; 2138 2139 buf->f_blocks = min(buf->f_blocks, limit); 2140 buf->f_bfree = min(buf->f_bfree, remaining); 2141 buf->f_bavail = min(buf->f_bavail, remaining); 2142 } 2143 2144 limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit, 2145 dquot->dq_dqb.dqb_ihardlimit); 2146 2147 if (limit) { 2148 uint64_t remaining = 0; 2149 2150 if (limit > dquot->dq_dqb.dqb_curinodes) 2151 remaining = limit - dquot->dq_dqb.dqb_curinodes; 2152 2153 buf->f_files = min(buf->f_files, limit); 2154 buf->f_ffree = min(buf->f_ffree, remaining); 2155 } 2156 2157 spin_unlock(&dquot->dq_dqb_lock); 2158 dqput(dquot); 2159 return 0; 2160 } 2161 #endif 2162 2163 static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf) 2164 { 2165 struct super_block *sb = dentry->d_sb; 2166 struct f2fs_sb_info *sbi = F2FS_SB(sb); 2167 u64 id = huge_encode_dev(sb->s_bdev->bd_dev); 2168 block_t total_count, user_block_count, start_count; 2169 u64 avail_node_count; 2170 unsigned int total_valid_node_count; 2171 2172 total_count = le64_to_cpu(sbi->raw_super->block_count); 2173 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr); 2174 buf->f_type = F2FS_SUPER_MAGIC; 2175 buf->f_bsize = sbi->blocksize; 2176 2177 buf->f_blocks = total_count - start_count; 2178 2179 spin_lock(&sbi->stat_lock); 2180 if (sbi->carve_out) 2181 buf->f_blocks -= sbi->current_reserved_blocks; 2182 user_block_count = sbi->user_block_count; 2183 total_valid_node_count = valid_node_count(sbi); 2184 avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM; 2185 buf->f_bfree = user_block_count - valid_user_blocks(sbi) - 2186 sbi->current_reserved_blocks; 2187 2188 if (unlikely(buf->f_bfree <= sbi->unusable_block_count)) 2189 buf->f_bfree = 0; 2190 else 2191 buf->f_bfree -= sbi->unusable_block_count; 2192 spin_unlock(&sbi->stat_lock); 2193 2194 if (buf->f_bfree > F2FS_OPTION(sbi).root_reserved_blocks) 2195 buf->f_bavail = buf->f_bfree - 2196 F2FS_OPTION(sbi).root_reserved_blocks; 2197 else 2198 buf->f_bavail = 0; 2199 2200 if (avail_node_count > user_block_count) { 2201 buf->f_files = user_block_count; 2202 buf->f_ffree = buf->f_bavail; 2203 } else { 2204 buf->f_files = avail_node_count; 2205 buf->f_ffree = min(avail_node_count - total_valid_node_count, 2206 buf->f_bavail); 2207 } 2208 2209 buf->f_namelen = F2FS_NAME_LEN; 2210 buf->f_fsid = u64_to_fsid(id); 2211 2212 #ifdef CONFIG_QUOTA 2213 if (is_inode_flag_set(d_inode(dentry), FI_PROJ_INHERIT) && 2214 sb_has_quota_limits_enabled(sb, PRJQUOTA)) { 2215 f2fs_statfs_project(sb, F2FS_I(d_inode(dentry))->i_projid, buf); 2216 } 2217 #endif 2218 return 0; 2219 } 2220 2221 static inline void f2fs_show_quota_options(struct seq_file *seq, 2222 struct super_block *sb) 2223 { 2224 #ifdef CONFIG_QUOTA 2225 struct f2fs_sb_info *sbi = F2FS_SB(sb); 2226 2227 if (F2FS_OPTION(sbi).s_jquota_fmt) { 2228 char *fmtname = ""; 2229 2230 switch (F2FS_OPTION(sbi).s_jquota_fmt) { 2231 case QFMT_VFS_OLD: 2232 fmtname = "vfsold"; 2233 break; 2234 case QFMT_VFS_V0: 2235 fmtname = "vfsv0"; 2236 break; 2237 case QFMT_VFS_V1: 2238 fmtname = "vfsv1"; 2239 break; 2240 } 2241 seq_printf(seq, ",jqfmt=%s", fmtname); 2242 } 2243 2244 if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA]) 2245 seq_show_option(seq, "usrjquota", 2246 F2FS_OPTION(sbi).s_qf_names[USRQUOTA]); 2247 2248 if (F2FS_OPTION(sbi).s_qf_names[GRPQUOTA]) 2249 seq_show_option(seq, "grpjquota", 2250 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA]); 2251 2252 if (F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]) 2253 seq_show_option(seq, "prjjquota", 2254 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]); 2255 #endif 2256 } 2257 2258 #ifdef CONFIG_F2FS_FS_COMPRESSION 2259 static inline void f2fs_show_compress_options(struct seq_file *seq, 2260 struct super_block *sb) 2261 { 2262 struct f2fs_sb_info *sbi = F2FS_SB(sb); 2263 char *algtype = ""; 2264 int i; 2265 2266 if (!f2fs_sb_has_compression(sbi)) 2267 return; 2268 2269 switch (F2FS_OPTION(sbi).compress_algorithm) { 2270 case COMPRESS_LZO: 2271 algtype = "lzo"; 2272 break; 2273 case COMPRESS_LZ4: 2274 algtype = "lz4"; 2275 break; 2276 case COMPRESS_ZSTD: 2277 algtype = "zstd"; 2278 break; 2279 case COMPRESS_LZORLE: 2280 algtype = "lzo-rle"; 2281 break; 2282 } 2283 seq_printf(seq, ",compress_algorithm=%s", algtype); 2284 2285 if (F2FS_OPTION(sbi).compress_level) 2286 seq_printf(seq, ":%d", F2FS_OPTION(sbi).compress_level); 2287 2288 seq_printf(seq, ",compress_log_size=%u", 2289 F2FS_OPTION(sbi).compress_log_size); 2290 2291 for (i = 0; i < F2FS_OPTION(sbi).compress_ext_cnt; i++) { 2292 seq_printf(seq, ",compress_extension=%s", 2293 F2FS_OPTION(sbi).extensions[i]); 2294 } 2295 2296 for (i = 0; i < F2FS_OPTION(sbi).nocompress_ext_cnt; i++) { 2297 seq_printf(seq, ",nocompress_extension=%s", 2298 F2FS_OPTION(sbi).noextensions[i]); 2299 } 2300 2301 if (F2FS_OPTION(sbi).compress_chksum) 2302 seq_puts(seq, ",compress_chksum"); 2303 2304 if (F2FS_OPTION(sbi).compress_mode == COMPR_MODE_FS) 2305 seq_printf(seq, ",compress_mode=%s", "fs"); 2306 else if (F2FS_OPTION(sbi).compress_mode == COMPR_MODE_USER) 2307 seq_printf(seq, ",compress_mode=%s", "user"); 2308 2309 if (test_opt(sbi, COMPRESS_CACHE)) 2310 seq_puts(seq, ",compress_cache"); 2311 } 2312 #endif 2313 2314 static int f2fs_show_options(struct seq_file *seq, struct dentry *root) 2315 { 2316 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb); 2317 2318 if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_SYNC) 2319 seq_printf(seq, ",background_gc=%s", "sync"); 2320 else if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_ON) 2321 seq_printf(seq, ",background_gc=%s", "on"); 2322 else if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_OFF) 2323 seq_printf(seq, ",background_gc=%s", "off"); 2324 2325 if (test_opt(sbi, GC_MERGE)) 2326 seq_puts(seq, ",gc_merge"); 2327 else 2328 seq_puts(seq, ",nogc_merge"); 2329 2330 if (test_opt(sbi, DISABLE_ROLL_FORWARD)) 2331 seq_puts(seq, ",disable_roll_forward"); 2332 if (test_opt(sbi, NORECOVERY)) 2333 seq_puts(seq, ",norecovery"); 2334 if (test_opt(sbi, DISCARD)) { 2335 seq_puts(seq, ",discard"); 2336 if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_BLOCK) 2337 seq_printf(seq, ",discard_unit=%s", "block"); 2338 else if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SEGMENT) 2339 seq_printf(seq, ",discard_unit=%s", "segment"); 2340 else if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SECTION) 2341 seq_printf(seq, ",discard_unit=%s", "section"); 2342 } else { 2343 seq_puts(seq, ",nodiscard"); 2344 } 2345 #ifdef CONFIG_F2FS_FS_XATTR 2346 if (test_opt(sbi, XATTR_USER)) 2347 seq_puts(seq, ",user_xattr"); 2348 else 2349 seq_puts(seq, ",nouser_xattr"); 2350 if (test_opt(sbi, INLINE_XATTR)) 2351 seq_puts(seq, ",inline_xattr"); 2352 else 2353 seq_puts(seq, ",noinline_xattr"); 2354 if (test_opt(sbi, INLINE_XATTR_SIZE)) 2355 seq_printf(seq, ",inline_xattr_size=%u", 2356 F2FS_OPTION(sbi).inline_xattr_size); 2357 #endif 2358 #ifdef CONFIG_F2FS_FS_POSIX_ACL 2359 if (test_opt(sbi, POSIX_ACL)) 2360 seq_puts(seq, ",acl"); 2361 else 2362 seq_puts(seq, ",noacl"); 2363 #endif 2364 if (test_opt(sbi, DISABLE_EXT_IDENTIFY)) 2365 seq_puts(seq, ",disable_ext_identify"); 2366 if (test_opt(sbi, INLINE_DATA)) 2367 seq_puts(seq, ",inline_data"); 2368 else 2369 seq_puts(seq, ",noinline_data"); 2370 if (test_opt(sbi, INLINE_DENTRY)) 2371 seq_puts(seq, ",inline_dentry"); 2372 else 2373 seq_puts(seq, ",noinline_dentry"); 2374 if (test_opt(sbi, FLUSH_MERGE)) 2375 seq_puts(seq, ",flush_merge"); 2376 else 2377 seq_puts(seq, ",noflush_merge"); 2378 if (test_opt(sbi, NOBARRIER)) 2379 seq_puts(seq, ",nobarrier"); 2380 else 2381 seq_puts(seq, ",barrier"); 2382 if (test_opt(sbi, FASTBOOT)) 2383 seq_puts(seq, ",fastboot"); 2384 if (test_opt(sbi, READ_EXTENT_CACHE)) 2385 seq_puts(seq, ",extent_cache"); 2386 else 2387 seq_puts(seq, ",noextent_cache"); 2388 if (test_opt(sbi, AGE_EXTENT_CACHE)) 2389 seq_puts(seq, ",age_extent_cache"); 2390 if (test_opt(sbi, DATA_FLUSH)) 2391 seq_puts(seq, ",data_flush"); 2392 2393 seq_puts(seq, ",mode="); 2394 if (F2FS_OPTION(sbi).fs_mode == FS_MODE_ADAPTIVE) 2395 seq_puts(seq, "adaptive"); 2396 else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS) 2397 seq_puts(seq, "lfs"); 2398 else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_SEG) 2399 seq_puts(seq, "fragment:segment"); 2400 else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK) 2401 seq_puts(seq, "fragment:block"); 2402 seq_printf(seq, ",active_logs=%u", F2FS_OPTION(sbi).active_logs); 2403 if (test_opt(sbi, RESERVE_ROOT) || test_opt(sbi, RESERVE_NODE)) 2404 seq_printf(seq, ",reserve_root=%u,reserve_node=%u,resuid=%u," 2405 "resgid=%u", 2406 F2FS_OPTION(sbi).root_reserved_blocks, 2407 F2FS_OPTION(sbi).root_reserved_nodes, 2408 from_kuid_munged(&init_user_ns, 2409 F2FS_OPTION(sbi).s_resuid), 2410 from_kgid_munged(&init_user_ns, 2411 F2FS_OPTION(sbi).s_resgid)); 2412 #ifdef CONFIG_F2FS_FAULT_INJECTION 2413 if (test_opt(sbi, FAULT_INJECTION)) { 2414 seq_printf(seq, ",fault_injection=%u", 2415 F2FS_OPTION(sbi).fault_info.inject_rate); 2416 seq_printf(seq, ",fault_type=%u", 2417 F2FS_OPTION(sbi).fault_info.inject_type); 2418 } 2419 #endif 2420 #ifdef CONFIG_QUOTA 2421 if (test_opt(sbi, QUOTA)) 2422 seq_puts(seq, ",quota"); 2423 if (test_opt(sbi, USRQUOTA)) 2424 seq_puts(seq, ",usrquota"); 2425 if (test_opt(sbi, GRPQUOTA)) 2426 seq_puts(seq, ",grpquota"); 2427 if (test_opt(sbi, PRJQUOTA)) 2428 seq_puts(seq, ",prjquota"); 2429 #endif 2430 f2fs_show_quota_options(seq, sbi->sb); 2431 2432 fscrypt_show_test_dummy_encryption(seq, ',', sbi->sb); 2433 2434 if (sbi->sb->s_flags & SB_INLINECRYPT) 2435 seq_puts(seq, ",inlinecrypt"); 2436 2437 if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_DEFAULT) 2438 seq_printf(seq, ",alloc_mode=%s", "default"); 2439 else if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE) 2440 seq_printf(seq, ",alloc_mode=%s", "reuse"); 2441 2442 if (test_opt(sbi, DISABLE_CHECKPOINT)) 2443 seq_printf(seq, ",checkpoint=disable:%u", 2444 F2FS_OPTION(sbi).unusable_cap); 2445 if (test_opt(sbi, MERGE_CHECKPOINT)) 2446 seq_puts(seq, ",checkpoint_merge"); 2447 else 2448 seq_puts(seq, ",nocheckpoint_merge"); 2449 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_POSIX) 2450 seq_printf(seq, ",fsync_mode=%s", "posix"); 2451 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) 2452 seq_printf(seq, ",fsync_mode=%s", "strict"); 2453 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_NOBARRIER) 2454 seq_printf(seq, ",fsync_mode=%s", "nobarrier"); 2455 2456 #ifdef CONFIG_F2FS_FS_COMPRESSION 2457 f2fs_show_compress_options(seq, sbi->sb); 2458 #endif 2459 2460 if (test_opt(sbi, ATGC)) 2461 seq_puts(seq, ",atgc"); 2462 2463 if (F2FS_OPTION(sbi).memory_mode == MEMORY_MODE_NORMAL) 2464 seq_printf(seq, ",memory=%s", "normal"); 2465 else if (F2FS_OPTION(sbi).memory_mode == MEMORY_MODE_LOW) 2466 seq_printf(seq, ",memory=%s", "low"); 2467 2468 if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_READONLY) 2469 seq_printf(seq, ",errors=%s", "remount-ro"); 2470 else if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_CONTINUE) 2471 seq_printf(seq, ",errors=%s", "continue"); 2472 else if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_PANIC) 2473 seq_printf(seq, ",errors=%s", "panic"); 2474 2475 if (test_opt(sbi, NAT_BITS)) 2476 seq_puts(seq, ",nat_bits"); 2477 2478 if (F2FS_OPTION(sbi).lookup_mode == LOOKUP_PERF) 2479 seq_show_option(seq, "lookup_mode", "perf"); 2480 else if (F2FS_OPTION(sbi).lookup_mode == LOOKUP_COMPAT) 2481 seq_show_option(seq, "lookup_mode", "compat"); 2482 else if (F2FS_OPTION(sbi).lookup_mode == LOOKUP_AUTO) 2483 seq_show_option(seq, "lookup_mode", "auto"); 2484 2485 return 0; 2486 } 2487 2488 static void default_options(struct f2fs_sb_info *sbi, bool remount) 2489 { 2490 /* init some FS parameters */ 2491 if (!remount) { 2492 set_opt(sbi, READ_EXTENT_CACHE); 2493 clear_opt(sbi, DISABLE_CHECKPOINT); 2494 2495 if (f2fs_hw_support_discard(sbi) || f2fs_hw_should_discard(sbi)) 2496 set_opt(sbi, DISCARD); 2497 2498 if (f2fs_sb_has_blkzoned(sbi)) 2499 F2FS_OPTION(sbi).discard_unit = DISCARD_UNIT_SECTION; 2500 else 2501 F2FS_OPTION(sbi).discard_unit = DISCARD_UNIT_BLOCK; 2502 } 2503 2504 if (f2fs_sb_has_readonly(sbi)) 2505 F2FS_OPTION(sbi).active_logs = NR_CURSEG_RO_TYPE; 2506 else 2507 F2FS_OPTION(sbi).active_logs = NR_CURSEG_PERSIST_TYPE; 2508 2509 F2FS_OPTION(sbi).inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS; 2510 if (le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_main) <= 2511 SMALL_VOLUME_SEGMENTS) 2512 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE; 2513 else 2514 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT; 2515 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX; 2516 F2FS_OPTION(sbi).s_resuid = make_kuid(&init_user_ns, F2FS_DEF_RESUID); 2517 F2FS_OPTION(sbi).s_resgid = make_kgid(&init_user_ns, F2FS_DEF_RESGID); 2518 if (f2fs_sb_has_compression(sbi)) { 2519 F2FS_OPTION(sbi).compress_algorithm = COMPRESS_LZ4; 2520 F2FS_OPTION(sbi).compress_log_size = MIN_COMPRESS_LOG_SIZE; 2521 F2FS_OPTION(sbi).compress_ext_cnt = 0; 2522 F2FS_OPTION(sbi).compress_mode = COMPR_MODE_FS; 2523 } 2524 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_ON; 2525 F2FS_OPTION(sbi).memory_mode = MEMORY_MODE_NORMAL; 2526 F2FS_OPTION(sbi).errors = MOUNT_ERRORS_CONTINUE; 2527 2528 set_opt(sbi, INLINE_XATTR); 2529 set_opt(sbi, INLINE_DATA); 2530 set_opt(sbi, INLINE_DENTRY); 2531 set_opt(sbi, MERGE_CHECKPOINT); 2532 set_opt(sbi, LAZYTIME); 2533 F2FS_OPTION(sbi).unusable_cap = 0; 2534 if (!f2fs_is_readonly(sbi)) 2535 set_opt(sbi, FLUSH_MERGE); 2536 if (f2fs_sb_has_blkzoned(sbi)) 2537 F2FS_OPTION(sbi).fs_mode = FS_MODE_LFS; 2538 else 2539 F2FS_OPTION(sbi).fs_mode = FS_MODE_ADAPTIVE; 2540 2541 #ifdef CONFIG_F2FS_FS_XATTR 2542 set_opt(sbi, XATTR_USER); 2543 #endif 2544 #ifdef CONFIG_F2FS_FS_POSIX_ACL 2545 set_opt(sbi, POSIX_ACL); 2546 #endif 2547 2548 f2fs_build_fault_attr(sbi, 0, 0, FAULT_ALL); 2549 2550 F2FS_OPTION(sbi).lookup_mode = LOOKUP_PERF; 2551 } 2552 2553 #ifdef CONFIG_QUOTA 2554 static int f2fs_enable_quotas(struct super_block *sb); 2555 #endif 2556 2557 static int f2fs_disable_checkpoint(struct f2fs_sb_info *sbi) 2558 { 2559 unsigned int s_flags = sbi->sb->s_flags; 2560 struct cp_control cpc; 2561 unsigned int gc_mode = sbi->gc_mode; 2562 int err = 0; 2563 int ret; 2564 block_t unusable; 2565 2566 if (s_flags & SB_RDONLY) { 2567 f2fs_err(sbi, "checkpoint=disable on readonly fs"); 2568 return -EINVAL; 2569 } 2570 sbi->sb->s_flags |= SB_ACTIVE; 2571 2572 /* check if we need more GC first */ 2573 unusable = f2fs_get_unusable_blocks(sbi); 2574 if (!f2fs_disable_cp_again(sbi, unusable)) 2575 goto skip_gc; 2576 2577 f2fs_update_time(sbi, DISABLE_TIME); 2578 2579 sbi->gc_mode = GC_URGENT_HIGH; 2580 2581 while (!f2fs_time_over(sbi, DISABLE_TIME)) { 2582 struct f2fs_gc_control gc_control = { 2583 .victim_segno = NULL_SEGNO, 2584 .init_gc_type = FG_GC, 2585 .should_migrate_blocks = false, 2586 .err_gc_skipped = true, 2587 .no_bg_gc = true, 2588 .nr_free_secs = 1 }; 2589 2590 f2fs_down_write(&sbi->gc_lock); 2591 stat_inc_gc_call_count(sbi, FOREGROUND); 2592 err = f2fs_gc(sbi, &gc_control); 2593 if (err == -ENODATA) { 2594 err = 0; 2595 break; 2596 } 2597 if (err && err != -EAGAIN) 2598 break; 2599 } 2600 2601 ret = sync_filesystem(sbi->sb); 2602 if (ret || err) { 2603 err = ret ? ret : err; 2604 goto restore_flag; 2605 } 2606 2607 unusable = f2fs_get_unusable_blocks(sbi); 2608 if (f2fs_disable_cp_again(sbi, unusable)) { 2609 err = -EAGAIN; 2610 goto restore_flag; 2611 } 2612 2613 skip_gc: 2614 f2fs_down_write(&sbi->gc_lock); 2615 cpc.reason = CP_PAUSE; 2616 set_sbi_flag(sbi, SBI_CP_DISABLED); 2617 stat_inc_cp_call_count(sbi, TOTAL_CALL); 2618 err = f2fs_write_checkpoint(sbi, &cpc); 2619 if (err) 2620 goto out_unlock; 2621 2622 spin_lock(&sbi->stat_lock); 2623 sbi->unusable_block_count = unusable; 2624 spin_unlock(&sbi->stat_lock); 2625 2626 out_unlock: 2627 f2fs_up_write(&sbi->gc_lock); 2628 restore_flag: 2629 sbi->gc_mode = gc_mode; 2630 sbi->sb->s_flags = s_flags; /* Restore SB_RDONLY status */ 2631 f2fs_info(sbi, "f2fs_disable_checkpoint() finish, err:%d", err); 2632 return err; 2633 } 2634 2635 static void f2fs_enable_checkpoint(struct f2fs_sb_info *sbi) 2636 { 2637 unsigned int nr_pages = get_pages(sbi, F2FS_DIRTY_DATA) / 16; 2638 long long start, writeback, end; 2639 2640 f2fs_info(sbi, "f2fs_enable_checkpoint() starts, meta: %lld, node: %lld, data: %lld", 2641 get_pages(sbi, F2FS_DIRTY_META), 2642 get_pages(sbi, F2FS_DIRTY_NODES), 2643 get_pages(sbi, F2FS_DIRTY_DATA)); 2644 2645 f2fs_update_time(sbi, ENABLE_TIME); 2646 2647 start = ktime_get(); 2648 2649 /* we should flush all the data to keep data consistency */ 2650 while (get_pages(sbi, F2FS_DIRTY_DATA)) { 2651 writeback_inodes_sb_nr(sbi->sb, nr_pages, WB_REASON_SYNC); 2652 f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT); 2653 2654 if (f2fs_time_over(sbi, ENABLE_TIME)) 2655 break; 2656 } 2657 writeback = ktime_get(); 2658 2659 sync_inodes_sb(sbi->sb); 2660 2661 if (unlikely(get_pages(sbi, F2FS_DIRTY_DATA))) 2662 f2fs_warn(sbi, "checkpoint=enable has some unwritten data: %lld", 2663 get_pages(sbi, F2FS_DIRTY_DATA)); 2664 2665 f2fs_down_write(&sbi->gc_lock); 2666 f2fs_dirty_to_prefree(sbi); 2667 2668 clear_sbi_flag(sbi, SBI_CP_DISABLED); 2669 set_sbi_flag(sbi, SBI_IS_DIRTY); 2670 f2fs_up_write(&sbi->gc_lock); 2671 2672 f2fs_sync_fs(sbi->sb, 1); 2673 2674 /* Let's ensure there's no pending checkpoint anymore */ 2675 f2fs_flush_ckpt_thread(sbi); 2676 2677 end = ktime_get(); 2678 2679 f2fs_info(sbi, "f2fs_enable_checkpoint() finishes, writeback:%llu, sync:%llu", 2680 ktime_ms_delta(writeback, start), 2681 ktime_ms_delta(end, writeback)); 2682 } 2683 2684 static int __f2fs_remount(struct fs_context *fc, struct super_block *sb) 2685 { 2686 struct f2fs_sb_info *sbi = F2FS_SB(sb); 2687 struct f2fs_mount_info org_mount_opt; 2688 unsigned long old_sb_flags; 2689 unsigned int flags = fc->sb_flags; 2690 int err; 2691 bool need_restart_gc = false, need_stop_gc = false; 2692 bool need_restart_flush = false, need_stop_flush = false; 2693 bool need_restart_discard = false, need_stop_discard = false; 2694 bool need_enable_checkpoint = false, need_disable_checkpoint = false; 2695 bool no_read_extent_cache = !test_opt(sbi, READ_EXTENT_CACHE); 2696 bool no_age_extent_cache = !test_opt(sbi, AGE_EXTENT_CACHE); 2697 bool enable_checkpoint = !test_opt(sbi, DISABLE_CHECKPOINT); 2698 bool no_atgc = !test_opt(sbi, ATGC); 2699 bool no_discard = !test_opt(sbi, DISCARD); 2700 bool no_compress_cache = !test_opt(sbi, COMPRESS_CACHE); 2701 bool block_unit_discard = f2fs_block_unit_discard(sbi); 2702 bool no_nat_bits = !test_opt(sbi, NAT_BITS); 2703 #ifdef CONFIG_QUOTA 2704 int i, j; 2705 #endif 2706 2707 /* 2708 * Save the old mount options in case we 2709 * need to restore them. 2710 */ 2711 org_mount_opt = sbi->mount_opt; 2712 old_sb_flags = sb->s_flags; 2713 2714 sbi->umount_lock_holder = current; 2715 2716 #ifdef CONFIG_QUOTA 2717 org_mount_opt.s_jquota_fmt = F2FS_OPTION(sbi).s_jquota_fmt; 2718 for (i = 0; i < MAXQUOTAS; i++) { 2719 if (F2FS_OPTION(sbi).s_qf_names[i]) { 2720 org_mount_opt.s_qf_names[i] = 2721 kstrdup(F2FS_OPTION(sbi).s_qf_names[i], 2722 GFP_KERNEL); 2723 if (!org_mount_opt.s_qf_names[i]) { 2724 for (j = 0; j < i; j++) 2725 kfree(org_mount_opt.s_qf_names[j]); 2726 return -ENOMEM; 2727 } 2728 } else { 2729 org_mount_opt.s_qf_names[i] = NULL; 2730 } 2731 } 2732 #endif 2733 2734 /* recover superblocks we couldn't write due to previous RO mount */ 2735 if (!(flags & SB_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) { 2736 err = f2fs_commit_super(sbi, false); 2737 f2fs_info(sbi, "Try to recover all the superblocks, ret: %d", 2738 err); 2739 if (!err) 2740 clear_sbi_flag(sbi, SBI_NEED_SB_WRITE); 2741 } 2742 2743 default_options(sbi, true); 2744 2745 err = f2fs_check_opt_consistency(fc, sb); 2746 if (err) 2747 goto restore_opts; 2748 2749 f2fs_apply_options(fc, sb); 2750 2751 err = f2fs_sanity_check_options(sbi, true); 2752 if (err) 2753 goto restore_opts; 2754 2755 /* flush outstanding errors before changing fs state */ 2756 flush_work(&sbi->s_error_work); 2757 2758 /* 2759 * Previous and new state of filesystem is RO, 2760 * so skip checking GC and FLUSH_MERGE conditions. 2761 */ 2762 if (f2fs_readonly(sb) && (flags & SB_RDONLY)) 2763 goto skip; 2764 2765 if (f2fs_dev_is_readonly(sbi) && !(flags & SB_RDONLY)) { 2766 err = -EROFS; 2767 goto restore_opts; 2768 } 2769 2770 #ifdef CONFIG_QUOTA 2771 if (!f2fs_readonly(sb) && (flags & SB_RDONLY)) { 2772 err = dquot_suspend(sb, -1); 2773 if (err < 0) 2774 goto restore_opts; 2775 } else if (f2fs_readonly(sb) && !(flags & SB_RDONLY)) { 2776 /* dquot_resume needs RW */ 2777 sb->s_flags &= ~SB_RDONLY; 2778 if (sb_any_quota_suspended(sb)) { 2779 dquot_resume(sb, -1); 2780 } else if (f2fs_sb_has_quota_ino(sbi)) { 2781 err = f2fs_enable_quotas(sb); 2782 if (err) 2783 goto restore_opts; 2784 } 2785 } 2786 #endif 2787 /* disallow enable atgc dynamically */ 2788 if (no_atgc == !!test_opt(sbi, ATGC)) { 2789 err = -EINVAL; 2790 f2fs_warn(sbi, "switch atgc option is not allowed"); 2791 goto restore_opts; 2792 } 2793 2794 /* disallow enable/disable extent_cache dynamically */ 2795 if (no_read_extent_cache == !!test_opt(sbi, READ_EXTENT_CACHE)) { 2796 err = -EINVAL; 2797 f2fs_warn(sbi, "switch extent_cache option is not allowed"); 2798 goto restore_opts; 2799 } 2800 /* disallow enable/disable age extent_cache dynamically */ 2801 if (no_age_extent_cache == !!test_opt(sbi, AGE_EXTENT_CACHE)) { 2802 err = -EINVAL; 2803 f2fs_warn(sbi, "switch age_extent_cache option is not allowed"); 2804 goto restore_opts; 2805 } 2806 2807 if (no_compress_cache == !!test_opt(sbi, COMPRESS_CACHE)) { 2808 err = -EINVAL; 2809 f2fs_warn(sbi, "switch compress_cache option is not allowed"); 2810 goto restore_opts; 2811 } 2812 2813 if (block_unit_discard != f2fs_block_unit_discard(sbi)) { 2814 err = -EINVAL; 2815 f2fs_warn(sbi, "switch discard_unit option is not allowed"); 2816 goto restore_opts; 2817 } 2818 2819 if (no_nat_bits == !!test_opt(sbi, NAT_BITS)) { 2820 err = -EINVAL; 2821 f2fs_warn(sbi, "switch nat_bits option is not allowed"); 2822 goto restore_opts; 2823 } 2824 2825 if ((flags & SB_RDONLY) && test_opt(sbi, DISABLE_CHECKPOINT)) { 2826 err = -EINVAL; 2827 f2fs_warn(sbi, "disabling checkpoint not compatible with read-only"); 2828 goto restore_opts; 2829 } 2830 2831 /* 2832 * We stop the GC thread if FS is mounted as RO 2833 * or if background_gc = off is passed in mount 2834 * option. Also sync the filesystem. 2835 */ 2836 if ((flags & SB_RDONLY) || 2837 (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_OFF && 2838 !test_opt(sbi, GC_MERGE))) { 2839 if (sbi->gc_thread) { 2840 f2fs_stop_gc_thread(sbi); 2841 need_restart_gc = true; 2842 } 2843 } else if (!sbi->gc_thread) { 2844 err = f2fs_start_gc_thread(sbi); 2845 if (err) 2846 goto restore_opts; 2847 need_stop_gc = true; 2848 } 2849 2850 if (flags & SB_RDONLY) { 2851 sync_inodes_sb(sb); 2852 2853 set_sbi_flag(sbi, SBI_IS_DIRTY); 2854 set_sbi_flag(sbi, SBI_IS_CLOSE); 2855 f2fs_sync_fs(sb, 1); 2856 clear_sbi_flag(sbi, SBI_IS_CLOSE); 2857 } 2858 2859 /* 2860 * We stop issue flush thread if FS is mounted as RO 2861 * or if flush_merge is not passed in mount option. 2862 */ 2863 if ((flags & SB_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) { 2864 clear_opt(sbi, FLUSH_MERGE); 2865 f2fs_destroy_flush_cmd_control(sbi, false); 2866 need_restart_flush = true; 2867 } else { 2868 err = f2fs_create_flush_cmd_control(sbi); 2869 if (err) 2870 goto restore_gc; 2871 need_stop_flush = true; 2872 } 2873 2874 if (no_discard == !!test_opt(sbi, DISCARD)) { 2875 if (test_opt(sbi, DISCARD)) { 2876 err = f2fs_start_discard_thread(sbi); 2877 if (err) 2878 goto restore_flush; 2879 need_stop_discard = true; 2880 } else { 2881 f2fs_stop_discard_thread(sbi); 2882 f2fs_issue_discard_timeout(sbi); 2883 need_restart_discard = true; 2884 } 2885 } 2886 2887 adjust_unusable_cap_perc(sbi); 2888 if (enable_checkpoint == !!test_opt(sbi, DISABLE_CHECKPOINT)) { 2889 if (test_opt(sbi, DISABLE_CHECKPOINT)) { 2890 err = f2fs_disable_checkpoint(sbi); 2891 if (err) 2892 goto restore_discard; 2893 need_enable_checkpoint = true; 2894 } else { 2895 f2fs_enable_checkpoint(sbi); 2896 need_disable_checkpoint = true; 2897 } 2898 } 2899 2900 /* 2901 * Place this routine at the end, since a new checkpoint would be 2902 * triggered while remount and we need to take care of it before 2903 * returning from remount. 2904 */ 2905 if ((flags & SB_RDONLY) || test_opt(sbi, DISABLE_CHECKPOINT) || 2906 !test_opt(sbi, MERGE_CHECKPOINT)) { 2907 f2fs_stop_ckpt_thread(sbi); 2908 } else { 2909 /* Flush if the previous checkpoint, if exists. */ 2910 f2fs_flush_ckpt_thread(sbi); 2911 2912 err = f2fs_start_ckpt_thread(sbi); 2913 if (err) { 2914 f2fs_err(sbi, 2915 "Failed to start F2FS issue_checkpoint_thread (%d)", 2916 err); 2917 goto restore_checkpoint; 2918 } 2919 } 2920 2921 skip: 2922 #ifdef CONFIG_QUOTA 2923 /* Release old quota file names */ 2924 for (i = 0; i < MAXQUOTAS; i++) 2925 kfree(org_mount_opt.s_qf_names[i]); 2926 #endif 2927 /* Update the POSIXACL Flag */ 2928 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) | 2929 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0); 2930 2931 limit_reserve_root(sbi); 2932 fc->sb_flags = (flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME); 2933 2934 sbi->umount_lock_holder = NULL; 2935 return 0; 2936 restore_checkpoint: 2937 if (need_enable_checkpoint) { 2938 f2fs_enable_checkpoint(sbi); 2939 } else if (need_disable_checkpoint) { 2940 if (f2fs_disable_checkpoint(sbi)) 2941 f2fs_warn(sbi, "checkpoint has not been disabled"); 2942 } 2943 restore_discard: 2944 if (need_restart_discard) { 2945 if (f2fs_start_discard_thread(sbi)) 2946 f2fs_warn(sbi, "discard has been stopped"); 2947 } else if (need_stop_discard) { 2948 f2fs_stop_discard_thread(sbi); 2949 } 2950 restore_flush: 2951 if (need_restart_flush) { 2952 if (f2fs_create_flush_cmd_control(sbi)) 2953 f2fs_warn(sbi, "background flush thread has stopped"); 2954 } else if (need_stop_flush) { 2955 clear_opt(sbi, FLUSH_MERGE); 2956 f2fs_destroy_flush_cmd_control(sbi, false); 2957 } 2958 restore_gc: 2959 if (need_restart_gc) { 2960 if (f2fs_start_gc_thread(sbi)) 2961 f2fs_warn(sbi, "background gc thread has stopped"); 2962 } else if (need_stop_gc) { 2963 f2fs_stop_gc_thread(sbi); 2964 } 2965 restore_opts: 2966 #ifdef CONFIG_QUOTA 2967 F2FS_OPTION(sbi).s_jquota_fmt = org_mount_opt.s_jquota_fmt; 2968 for (i = 0; i < MAXQUOTAS; i++) { 2969 kfree(F2FS_OPTION(sbi).s_qf_names[i]); 2970 F2FS_OPTION(sbi).s_qf_names[i] = org_mount_opt.s_qf_names[i]; 2971 } 2972 #endif 2973 sbi->mount_opt = org_mount_opt; 2974 sb->s_flags = old_sb_flags; 2975 2976 sbi->umount_lock_holder = NULL; 2977 return err; 2978 } 2979 2980 static void f2fs_shutdown(struct super_block *sb) 2981 { 2982 f2fs_do_shutdown(F2FS_SB(sb), F2FS_GOING_DOWN_NOSYNC, false, false); 2983 } 2984 2985 #ifdef CONFIG_QUOTA 2986 static bool f2fs_need_recovery(struct f2fs_sb_info *sbi) 2987 { 2988 /* need to recovery orphan */ 2989 if (is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG)) 2990 return true; 2991 /* need to recovery data */ 2992 if (test_opt(sbi, DISABLE_ROLL_FORWARD)) 2993 return false; 2994 if (test_opt(sbi, NORECOVERY)) 2995 return false; 2996 return !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG); 2997 } 2998 2999 static bool f2fs_recover_quota_begin(struct f2fs_sb_info *sbi) 3000 { 3001 bool readonly = f2fs_readonly(sbi->sb); 3002 3003 if (!f2fs_need_recovery(sbi)) 3004 return false; 3005 3006 /* it doesn't need to check f2fs_sb_has_readonly() */ 3007 if (f2fs_hw_is_readonly(sbi)) 3008 return false; 3009 3010 if (readonly) { 3011 sbi->sb->s_flags &= ~SB_RDONLY; 3012 set_sbi_flag(sbi, SBI_IS_WRITABLE); 3013 } 3014 3015 /* 3016 * Turn on quotas which were not enabled for read-only mounts if 3017 * filesystem has quota feature, so that they are updated correctly. 3018 */ 3019 return f2fs_enable_quota_files(sbi, readonly); 3020 } 3021 3022 static void f2fs_recover_quota_end(struct f2fs_sb_info *sbi, 3023 bool quota_enabled) 3024 { 3025 if (quota_enabled) 3026 f2fs_quota_off_umount(sbi->sb); 3027 3028 if (is_sbi_flag_set(sbi, SBI_IS_WRITABLE)) { 3029 clear_sbi_flag(sbi, SBI_IS_WRITABLE); 3030 sbi->sb->s_flags |= SB_RDONLY; 3031 } 3032 } 3033 3034 /* Read data from quotafile */ 3035 static ssize_t f2fs_quota_read(struct super_block *sb, int type, char *data, 3036 size_t len, loff_t off) 3037 { 3038 struct inode *inode = sb_dqopt(sb)->files[type]; 3039 struct address_space *mapping = inode->i_mapping; 3040 int tocopy; 3041 size_t toread; 3042 loff_t i_size = i_size_read(inode); 3043 3044 if (off > i_size) 3045 return 0; 3046 3047 if (off + len > i_size) 3048 len = i_size - off; 3049 toread = len; 3050 while (toread > 0) { 3051 struct folio *folio; 3052 size_t offset; 3053 3054 repeat: 3055 folio = mapping_read_folio_gfp(mapping, off >> PAGE_SHIFT, 3056 GFP_NOFS); 3057 if (IS_ERR(folio)) { 3058 if (PTR_ERR(folio) == -ENOMEM) { 3059 memalloc_retry_wait(GFP_NOFS); 3060 goto repeat; 3061 } 3062 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR); 3063 return PTR_ERR(folio); 3064 } 3065 offset = offset_in_folio(folio, off); 3066 tocopy = min(folio_size(folio) - offset, toread); 3067 3068 folio_lock(folio); 3069 3070 if (unlikely(folio->mapping != mapping)) { 3071 f2fs_folio_put(folio, true); 3072 goto repeat; 3073 } 3074 3075 /* 3076 * should never happen, just leave f2fs_bug_on() here to catch 3077 * any potential bug. 3078 */ 3079 f2fs_bug_on(F2FS_SB(sb), !folio_test_uptodate(folio)); 3080 3081 memcpy_from_folio(data, folio, offset, tocopy); 3082 f2fs_folio_put(folio, true); 3083 3084 toread -= tocopy; 3085 data += tocopy; 3086 off += tocopy; 3087 } 3088 return len; 3089 } 3090 3091 /* Write to quotafile */ 3092 static ssize_t f2fs_quota_write(struct super_block *sb, int type, 3093 const char *data, size_t len, loff_t off) 3094 { 3095 struct inode *inode = sb_dqopt(sb)->files[type]; 3096 struct address_space *mapping = inode->i_mapping; 3097 const struct address_space_operations *a_ops = mapping->a_ops; 3098 int offset = off & (sb->s_blocksize - 1); 3099 size_t towrite = len; 3100 struct folio *folio; 3101 void *fsdata = NULL; 3102 int err = 0; 3103 int tocopy; 3104 3105 while (towrite > 0) { 3106 tocopy = min_t(unsigned long, sb->s_blocksize - offset, 3107 towrite); 3108 retry: 3109 err = a_ops->write_begin(NULL, mapping, off, tocopy, 3110 &folio, &fsdata); 3111 if (unlikely(err)) { 3112 if (err == -ENOMEM) { 3113 f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT); 3114 goto retry; 3115 } 3116 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR); 3117 break; 3118 } 3119 3120 memcpy_to_folio(folio, offset_in_folio(folio, off), data, tocopy); 3121 3122 a_ops->write_end(NULL, mapping, off, tocopy, tocopy, 3123 folio, fsdata); 3124 offset = 0; 3125 towrite -= tocopy; 3126 off += tocopy; 3127 data += tocopy; 3128 cond_resched(); 3129 } 3130 3131 if (len == towrite) 3132 return err; 3133 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); 3134 f2fs_mark_inode_dirty_sync(inode, false); 3135 return len - towrite; 3136 } 3137 3138 int f2fs_dquot_initialize(struct inode *inode) 3139 { 3140 if (time_to_inject(F2FS_I_SB(inode), FAULT_DQUOT_INIT)) 3141 return -ESRCH; 3142 3143 return dquot_initialize(inode); 3144 } 3145 3146 static struct dquot __rcu **f2fs_get_dquots(struct inode *inode) 3147 { 3148 return F2FS_I(inode)->i_dquot; 3149 } 3150 3151 static qsize_t *f2fs_get_reserved_space(struct inode *inode) 3152 { 3153 return &F2FS_I(inode)->i_reserved_quota; 3154 } 3155 3156 static int f2fs_quota_on_mount(struct f2fs_sb_info *sbi, int type) 3157 { 3158 if (is_set_ckpt_flags(sbi, CP_QUOTA_NEED_FSCK_FLAG)) { 3159 f2fs_err(sbi, "quota sysfile may be corrupted, skip loading it"); 3160 return 0; 3161 } 3162 3163 return dquot_quota_on_mount(sbi->sb, F2FS_OPTION(sbi).s_qf_names[type], 3164 F2FS_OPTION(sbi).s_jquota_fmt, type); 3165 } 3166 3167 int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly) 3168 { 3169 int enabled = 0; 3170 int i, err; 3171 3172 if (f2fs_sb_has_quota_ino(sbi) && rdonly) { 3173 err = f2fs_enable_quotas(sbi->sb); 3174 if (err) { 3175 f2fs_err(sbi, "Cannot turn on quota_ino: %d", err); 3176 return 0; 3177 } 3178 return 1; 3179 } 3180 3181 for (i = 0; i < MAXQUOTAS; i++) { 3182 if (F2FS_OPTION(sbi).s_qf_names[i]) { 3183 err = f2fs_quota_on_mount(sbi, i); 3184 if (!err) { 3185 enabled = 1; 3186 continue; 3187 } 3188 f2fs_err(sbi, "Cannot turn on quotas: %d on %d", 3189 err, i); 3190 } 3191 } 3192 return enabled; 3193 } 3194 3195 static int f2fs_quota_enable(struct super_block *sb, int type, int format_id, 3196 unsigned int flags) 3197 { 3198 struct inode *qf_inode; 3199 unsigned long qf_inum; 3200 unsigned long qf_flag = F2FS_QUOTA_DEFAULT_FL; 3201 int err; 3202 3203 BUG_ON(!f2fs_sb_has_quota_ino(F2FS_SB(sb))); 3204 3205 qf_inum = f2fs_qf_ino(sb, type); 3206 if (!qf_inum) 3207 return -EPERM; 3208 3209 qf_inode = f2fs_iget(sb, qf_inum); 3210 if (IS_ERR(qf_inode)) { 3211 f2fs_err(F2FS_SB(sb), "Bad quota inode %u:%lu", type, qf_inum); 3212 return PTR_ERR(qf_inode); 3213 } 3214 3215 /* Don't account quota for quota files to avoid recursion */ 3216 inode_lock(qf_inode); 3217 qf_inode->i_flags |= S_NOQUOTA; 3218 3219 if ((F2FS_I(qf_inode)->i_flags & qf_flag) != qf_flag) { 3220 F2FS_I(qf_inode)->i_flags |= qf_flag; 3221 f2fs_set_inode_flags(qf_inode); 3222 } 3223 inode_unlock(qf_inode); 3224 3225 err = dquot_load_quota_inode(qf_inode, type, format_id, flags); 3226 iput(qf_inode); 3227 return err; 3228 } 3229 3230 static int f2fs_enable_quotas(struct super_block *sb) 3231 { 3232 struct f2fs_sb_info *sbi = F2FS_SB(sb); 3233 int type, err = 0; 3234 unsigned long qf_inum; 3235 bool quota_mopt[MAXQUOTAS] = { 3236 test_opt(sbi, USRQUOTA), 3237 test_opt(sbi, GRPQUOTA), 3238 test_opt(sbi, PRJQUOTA), 3239 }; 3240 3241 if (is_set_ckpt_flags(F2FS_SB(sb), CP_QUOTA_NEED_FSCK_FLAG)) { 3242 f2fs_err(sbi, "quota file may be corrupted, skip loading it"); 3243 return 0; 3244 } 3245 3246 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE; 3247 3248 for (type = 0; type < MAXQUOTAS; type++) { 3249 qf_inum = f2fs_qf_ino(sb, type); 3250 if (qf_inum) { 3251 err = f2fs_quota_enable(sb, type, QFMT_VFS_V1, 3252 DQUOT_USAGE_ENABLED | 3253 (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0)); 3254 if (err) { 3255 f2fs_err(sbi, "Failed to enable quota tracking (type=%d, err=%d). Please run fsck to fix.", 3256 type, err); 3257 for (type--; type >= 0; type--) 3258 dquot_quota_off(sb, type); 3259 set_sbi_flag(F2FS_SB(sb), 3260 SBI_QUOTA_NEED_REPAIR); 3261 return err; 3262 } 3263 } 3264 } 3265 return 0; 3266 } 3267 3268 static int f2fs_quota_sync_file(struct f2fs_sb_info *sbi, int type) 3269 { 3270 struct quota_info *dqopt = sb_dqopt(sbi->sb); 3271 struct address_space *mapping = dqopt->files[type]->i_mapping; 3272 int ret = 0; 3273 3274 ret = dquot_writeback_dquots(sbi->sb, type); 3275 if (ret) 3276 goto out; 3277 3278 ret = filemap_fdatawrite(mapping); 3279 if (ret) 3280 goto out; 3281 3282 /* if we are using journalled quota */ 3283 if (is_journalled_quota(sbi)) 3284 goto out; 3285 3286 ret = filemap_fdatawait(mapping); 3287 3288 truncate_inode_pages(&dqopt->files[type]->i_data, 0); 3289 out: 3290 if (ret) 3291 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR); 3292 return ret; 3293 } 3294 3295 int f2fs_do_quota_sync(struct super_block *sb, int type) 3296 { 3297 struct f2fs_sb_info *sbi = F2FS_SB(sb); 3298 struct quota_info *dqopt = sb_dqopt(sb); 3299 int cnt; 3300 int ret = 0; 3301 3302 /* 3303 * Now when everything is written we can discard the pagecache so 3304 * that userspace sees the changes. 3305 */ 3306 for (cnt = 0; cnt < MAXQUOTAS; cnt++) { 3307 3308 if (type != -1 && cnt != type) 3309 continue; 3310 3311 if (!sb_has_quota_active(sb, cnt)) 3312 continue; 3313 3314 if (!f2fs_sb_has_quota_ino(sbi)) 3315 inode_lock(dqopt->files[cnt]); 3316 3317 /* 3318 * do_quotactl 3319 * f2fs_quota_sync 3320 * f2fs_down_read(quota_sem) 3321 * dquot_writeback_dquots() 3322 * f2fs_dquot_commit 3323 * block_operation 3324 * f2fs_down_read(quota_sem) 3325 */ 3326 f2fs_lock_op(sbi); 3327 f2fs_down_read(&sbi->quota_sem); 3328 3329 ret = f2fs_quota_sync_file(sbi, cnt); 3330 3331 f2fs_up_read(&sbi->quota_sem); 3332 f2fs_unlock_op(sbi); 3333 3334 if (!f2fs_sb_has_quota_ino(sbi)) 3335 inode_unlock(dqopt->files[cnt]); 3336 3337 if (ret) 3338 break; 3339 } 3340 return ret; 3341 } 3342 3343 static int f2fs_quota_sync(struct super_block *sb, int type) 3344 { 3345 int ret; 3346 3347 F2FS_SB(sb)->umount_lock_holder = current; 3348 ret = f2fs_do_quota_sync(sb, type); 3349 F2FS_SB(sb)->umount_lock_holder = NULL; 3350 return ret; 3351 } 3352 3353 static int f2fs_quota_on(struct super_block *sb, int type, int format_id, 3354 const struct path *path) 3355 { 3356 struct inode *inode; 3357 int err = 0; 3358 3359 /* if quota sysfile exists, deny enabling quota with specific file */ 3360 if (f2fs_sb_has_quota_ino(F2FS_SB(sb))) { 3361 f2fs_err(F2FS_SB(sb), "quota sysfile already exists"); 3362 return -EBUSY; 3363 } 3364 3365 if (path->dentry->d_sb != sb) 3366 return -EXDEV; 3367 3368 F2FS_SB(sb)->umount_lock_holder = current; 3369 3370 err = f2fs_do_quota_sync(sb, type); 3371 if (err) 3372 goto out; 3373 3374 inode = d_inode(path->dentry); 3375 3376 err = filemap_fdatawrite(inode->i_mapping); 3377 if (err) 3378 goto out; 3379 3380 err = filemap_fdatawait(inode->i_mapping); 3381 if (err) 3382 goto out; 3383 3384 err = dquot_quota_on(sb, type, format_id, path); 3385 if (err) 3386 goto out; 3387 3388 inode_lock(inode); 3389 F2FS_I(inode)->i_flags |= F2FS_QUOTA_DEFAULT_FL; 3390 f2fs_set_inode_flags(inode); 3391 inode_unlock(inode); 3392 f2fs_mark_inode_dirty_sync(inode, false); 3393 out: 3394 F2FS_SB(sb)->umount_lock_holder = NULL; 3395 return err; 3396 } 3397 3398 static int __f2fs_quota_off(struct super_block *sb, int type) 3399 { 3400 struct inode *inode = sb_dqopt(sb)->files[type]; 3401 int err; 3402 3403 if (!inode || !igrab(inode)) 3404 return dquot_quota_off(sb, type); 3405 3406 err = f2fs_do_quota_sync(sb, type); 3407 if (err) 3408 goto out_put; 3409 3410 err = dquot_quota_off(sb, type); 3411 if (err || f2fs_sb_has_quota_ino(F2FS_SB(sb))) 3412 goto out_put; 3413 3414 inode_lock(inode); 3415 F2FS_I(inode)->i_flags &= ~F2FS_QUOTA_DEFAULT_FL; 3416 f2fs_set_inode_flags(inode); 3417 inode_unlock(inode); 3418 f2fs_mark_inode_dirty_sync(inode, false); 3419 out_put: 3420 iput(inode); 3421 return err; 3422 } 3423 3424 static int f2fs_quota_off(struct super_block *sb, int type) 3425 { 3426 struct f2fs_sb_info *sbi = F2FS_SB(sb); 3427 int err; 3428 3429 F2FS_SB(sb)->umount_lock_holder = current; 3430 3431 err = __f2fs_quota_off(sb, type); 3432 3433 /* 3434 * quotactl can shutdown journalled quota, result in inconsistence 3435 * between quota record and fs data by following updates, tag the 3436 * flag to let fsck be aware of it. 3437 */ 3438 if (is_journalled_quota(sbi)) 3439 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR); 3440 3441 F2FS_SB(sb)->umount_lock_holder = NULL; 3442 3443 return err; 3444 } 3445 3446 void f2fs_quota_off_umount(struct super_block *sb) 3447 { 3448 int type; 3449 int err; 3450 3451 for (type = 0; type < MAXQUOTAS; type++) { 3452 err = __f2fs_quota_off(sb, type); 3453 if (err) { 3454 int ret = dquot_quota_off(sb, type); 3455 3456 f2fs_err(F2FS_SB(sb), "Fail to turn off disk quota (type: %d, err: %d, ret:%d), Please run fsck to fix it.", 3457 type, err, ret); 3458 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR); 3459 } 3460 } 3461 /* 3462 * In case of checkpoint=disable, we must flush quota blocks. 3463 * This can cause NULL exception for node_inode in end_io, since 3464 * put_super already dropped it. 3465 */ 3466 sync_filesystem(sb); 3467 } 3468 3469 static void f2fs_truncate_quota_inode_pages(struct super_block *sb) 3470 { 3471 struct quota_info *dqopt = sb_dqopt(sb); 3472 int type; 3473 3474 for (type = 0; type < MAXQUOTAS; type++) { 3475 if (!dqopt->files[type]) 3476 continue; 3477 f2fs_inode_synced(dqopt->files[type]); 3478 } 3479 } 3480 3481 static int f2fs_dquot_commit(struct dquot *dquot) 3482 { 3483 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb); 3484 int ret; 3485 3486 f2fs_down_read_nested(&sbi->quota_sem, SINGLE_DEPTH_NESTING); 3487 ret = dquot_commit(dquot); 3488 if (ret < 0) 3489 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR); 3490 f2fs_up_read(&sbi->quota_sem); 3491 return ret; 3492 } 3493 3494 static int f2fs_dquot_acquire(struct dquot *dquot) 3495 { 3496 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb); 3497 int ret; 3498 3499 f2fs_down_read(&sbi->quota_sem); 3500 ret = dquot_acquire(dquot); 3501 if (ret < 0) 3502 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR); 3503 f2fs_up_read(&sbi->quota_sem); 3504 return ret; 3505 } 3506 3507 static int f2fs_dquot_release(struct dquot *dquot) 3508 { 3509 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb); 3510 int ret = dquot_release(dquot); 3511 3512 if (ret < 0) 3513 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR); 3514 return ret; 3515 } 3516 3517 static int f2fs_dquot_mark_dquot_dirty(struct dquot *dquot) 3518 { 3519 struct super_block *sb = dquot->dq_sb; 3520 struct f2fs_sb_info *sbi = F2FS_SB(sb); 3521 int ret = dquot_mark_dquot_dirty(dquot); 3522 3523 /* if we are using journalled quota */ 3524 if (is_journalled_quota(sbi)) 3525 set_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH); 3526 3527 return ret; 3528 } 3529 3530 static int f2fs_dquot_commit_info(struct super_block *sb, int type) 3531 { 3532 struct f2fs_sb_info *sbi = F2FS_SB(sb); 3533 int ret = dquot_commit_info(sb, type); 3534 3535 if (ret < 0) 3536 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR); 3537 return ret; 3538 } 3539 3540 static int f2fs_get_projid(struct inode *inode, kprojid_t *projid) 3541 { 3542 *projid = F2FS_I(inode)->i_projid; 3543 return 0; 3544 } 3545 3546 static const struct dquot_operations f2fs_quota_operations = { 3547 .get_reserved_space = f2fs_get_reserved_space, 3548 .write_dquot = f2fs_dquot_commit, 3549 .acquire_dquot = f2fs_dquot_acquire, 3550 .release_dquot = f2fs_dquot_release, 3551 .mark_dirty = f2fs_dquot_mark_dquot_dirty, 3552 .write_info = f2fs_dquot_commit_info, 3553 .alloc_dquot = dquot_alloc, 3554 .destroy_dquot = dquot_destroy, 3555 .get_projid = f2fs_get_projid, 3556 .get_next_id = dquot_get_next_id, 3557 }; 3558 3559 static const struct quotactl_ops f2fs_quotactl_ops = { 3560 .quota_on = f2fs_quota_on, 3561 .quota_off = f2fs_quota_off, 3562 .quota_sync = f2fs_quota_sync, 3563 .get_state = dquot_get_state, 3564 .set_info = dquot_set_dqinfo, 3565 .get_dqblk = dquot_get_dqblk, 3566 .set_dqblk = dquot_set_dqblk, 3567 .get_nextdqblk = dquot_get_next_dqblk, 3568 }; 3569 #else 3570 int f2fs_dquot_initialize(struct inode *inode) 3571 { 3572 return 0; 3573 } 3574 3575 int f2fs_do_quota_sync(struct super_block *sb, int type) 3576 { 3577 return 0; 3578 } 3579 3580 void f2fs_quota_off_umount(struct super_block *sb) 3581 { 3582 } 3583 #endif 3584 3585 static const struct super_operations f2fs_sops = { 3586 .alloc_inode = f2fs_alloc_inode, 3587 .free_inode = f2fs_free_inode, 3588 .drop_inode = f2fs_drop_inode, 3589 .write_inode = f2fs_write_inode, 3590 .dirty_inode = f2fs_dirty_inode, 3591 .show_options = f2fs_show_options, 3592 #ifdef CONFIG_QUOTA 3593 .quota_read = f2fs_quota_read, 3594 .quota_write = f2fs_quota_write, 3595 .get_dquots = f2fs_get_dquots, 3596 #endif 3597 .evict_inode = f2fs_evict_inode, 3598 .put_super = f2fs_put_super, 3599 .sync_fs = f2fs_sync_fs, 3600 .freeze_fs = f2fs_freeze, 3601 .unfreeze_fs = f2fs_unfreeze, 3602 .statfs = f2fs_statfs, 3603 .shutdown = f2fs_shutdown, 3604 }; 3605 3606 #ifdef CONFIG_FS_ENCRYPTION 3607 static int f2fs_get_context(struct inode *inode, void *ctx, size_t len) 3608 { 3609 return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION, 3610 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT, 3611 ctx, len, NULL); 3612 } 3613 3614 static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len, 3615 void *fs_data) 3616 { 3617 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 3618 3619 /* 3620 * Encrypting the root directory is not allowed because fsck 3621 * expects lost+found directory to exist and remain unencrypted 3622 * if LOST_FOUND feature is enabled. 3623 * 3624 */ 3625 if (f2fs_sb_has_lost_found(sbi) && 3626 inode->i_ino == F2FS_ROOT_INO(sbi)) 3627 return -EPERM; 3628 3629 return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION, 3630 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT, 3631 ctx, len, fs_data, XATTR_CREATE); 3632 } 3633 3634 static const union fscrypt_policy *f2fs_get_dummy_policy(struct super_block *sb) 3635 { 3636 return F2FS_OPTION(F2FS_SB(sb)).dummy_enc_policy.policy; 3637 } 3638 3639 static bool f2fs_has_stable_inodes(struct super_block *sb) 3640 { 3641 return true; 3642 } 3643 3644 static struct block_device **f2fs_get_devices(struct super_block *sb, 3645 unsigned int *num_devs) 3646 { 3647 struct f2fs_sb_info *sbi = F2FS_SB(sb); 3648 struct block_device **devs; 3649 int i; 3650 3651 if (!f2fs_is_multi_device(sbi)) 3652 return NULL; 3653 3654 devs = kmalloc_array(sbi->s_ndevs, sizeof(*devs), GFP_KERNEL); 3655 if (!devs) 3656 return ERR_PTR(-ENOMEM); 3657 3658 for (i = 0; i < sbi->s_ndevs; i++) 3659 devs[i] = FDEV(i).bdev; 3660 *num_devs = sbi->s_ndevs; 3661 return devs; 3662 } 3663 3664 static const struct fscrypt_operations f2fs_cryptops = { 3665 .inode_info_offs = (int)offsetof(struct f2fs_inode_info, i_crypt_info) - 3666 (int)offsetof(struct f2fs_inode_info, vfs_inode), 3667 .needs_bounce_pages = 1, 3668 .has_32bit_inodes = 1, 3669 .supports_subblock_data_units = 1, 3670 .legacy_key_prefix = "f2fs:", 3671 .get_context = f2fs_get_context, 3672 .set_context = f2fs_set_context, 3673 .get_dummy_policy = f2fs_get_dummy_policy, 3674 .empty_dir = f2fs_empty_dir, 3675 .has_stable_inodes = f2fs_has_stable_inodes, 3676 .get_devices = f2fs_get_devices, 3677 }; 3678 #endif /* CONFIG_FS_ENCRYPTION */ 3679 3680 static struct inode *f2fs_nfs_get_inode(struct super_block *sb, 3681 u64 ino, u32 generation) 3682 { 3683 struct f2fs_sb_info *sbi = F2FS_SB(sb); 3684 struct inode *inode; 3685 3686 if (f2fs_check_nid_range(sbi, ino)) 3687 return ERR_PTR(-ESTALE); 3688 3689 /* 3690 * f2fs_iget isn't quite right if the inode is currently unallocated! 3691 * However f2fs_iget currently does appropriate checks to handle stale 3692 * inodes so everything is OK. 3693 */ 3694 inode = f2fs_iget(sb, ino); 3695 if (IS_ERR(inode)) 3696 return ERR_CAST(inode); 3697 if (unlikely(generation && inode->i_generation != generation)) { 3698 /* we didn't find the right inode.. */ 3699 iput(inode); 3700 return ERR_PTR(-ESTALE); 3701 } 3702 return inode; 3703 } 3704 3705 static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid, 3706 int fh_len, int fh_type) 3707 { 3708 return generic_fh_to_dentry(sb, fid, fh_len, fh_type, 3709 f2fs_nfs_get_inode); 3710 } 3711 3712 static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid, 3713 int fh_len, int fh_type) 3714 { 3715 return generic_fh_to_parent(sb, fid, fh_len, fh_type, 3716 f2fs_nfs_get_inode); 3717 } 3718 3719 static const struct export_operations f2fs_export_ops = { 3720 .encode_fh = generic_encode_ino32_fh, 3721 .fh_to_dentry = f2fs_fh_to_dentry, 3722 .fh_to_parent = f2fs_fh_to_parent, 3723 .get_parent = f2fs_get_parent, 3724 }; 3725 3726 loff_t max_file_blocks(struct inode *inode) 3727 { 3728 loff_t result = 0; 3729 loff_t leaf_count; 3730 3731 /* 3732 * note: previously, result is equal to (DEF_ADDRS_PER_INODE - 3733 * DEFAULT_INLINE_XATTR_ADDRS), but now f2fs try to reserve more 3734 * space in inode.i_addr, it will be more safe to reassign 3735 * result as zero. 3736 */ 3737 3738 if (inode && f2fs_compressed_file(inode)) 3739 leaf_count = ADDRS_PER_BLOCK(inode); 3740 else 3741 leaf_count = DEF_ADDRS_PER_BLOCK; 3742 3743 /* two direct node blocks */ 3744 result += (leaf_count * 2); 3745 3746 /* two indirect node blocks */ 3747 leaf_count *= NIDS_PER_BLOCK; 3748 result += (leaf_count * 2); 3749 3750 /* one double indirect node block */ 3751 leaf_count *= NIDS_PER_BLOCK; 3752 result += leaf_count; 3753 3754 /* 3755 * For compatibility with FSCRYPT_POLICY_FLAG_IV_INO_LBLK_{64,32} with 3756 * a 4K crypto data unit, we must restrict the max filesize to what can 3757 * fit within U32_MAX + 1 data units. 3758 */ 3759 3760 result = umin(result, F2FS_BYTES_TO_BLK(((loff_t)U32_MAX + 1) * 4096)); 3761 3762 return result; 3763 } 3764 3765 static int __f2fs_commit_super(struct f2fs_sb_info *sbi, struct folio *folio, 3766 pgoff_t index, bool update) 3767 { 3768 struct bio *bio; 3769 /* it's rare case, we can do fua all the time */ 3770 blk_opf_t opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA; 3771 int ret; 3772 3773 folio_lock(folio); 3774 folio_wait_writeback(folio); 3775 if (update) 3776 memcpy(F2FS_SUPER_BLOCK(folio, index), F2FS_RAW_SUPER(sbi), 3777 sizeof(struct f2fs_super_block)); 3778 folio_mark_dirty(folio); 3779 folio_clear_dirty_for_io(folio); 3780 folio_start_writeback(folio); 3781 folio_unlock(folio); 3782 3783 bio = bio_alloc(sbi->sb->s_bdev, 1, opf, GFP_NOFS); 3784 3785 /* it doesn't need to set crypto context for superblock update */ 3786 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(folio->index); 3787 3788 if (!bio_add_folio(bio, folio, folio_size(folio), 0)) 3789 f2fs_bug_on(sbi, 1); 3790 3791 ret = submit_bio_wait(bio); 3792 bio_put(bio); 3793 folio_end_writeback(folio); 3794 3795 return ret; 3796 } 3797 3798 static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi, 3799 struct folio *folio, pgoff_t index) 3800 { 3801 struct f2fs_super_block *raw_super = F2FS_SUPER_BLOCK(folio, index); 3802 struct super_block *sb = sbi->sb; 3803 u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr); 3804 u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr); 3805 u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr); 3806 u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr); 3807 u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr); 3808 u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr); 3809 u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt); 3810 u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit); 3811 u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat); 3812 u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa); 3813 u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main); 3814 u32 segment_count = le32_to_cpu(raw_super->segment_count); 3815 u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg); 3816 u64 main_end_blkaddr = main_blkaddr + 3817 ((u64)segment_count_main << log_blocks_per_seg); 3818 u64 seg_end_blkaddr = segment0_blkaddr + 3819 ((u64)segment_count << log_blocks_per_seg); 3820 3821 if (segment0_blkaddr != cp_blkaddr) { 3822 f2fs_info(sbi, "Mismatch start address, segment0(%u) cp_blkaddr(%u)", 3823 segment0_blkaddr, cp_blkaddr); 3824 return true; 3825 } 3826 3827 if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) != 3828 sit_blkaddr) { 3829 f2fs_info(sbi, "Wrong CP boundary, start(%u) end(%u) blocks(%u)", 3830 cp_blkaddr, sit_blkaddr, 3831 segment_count_ckpt << log_blocks_per_seg); 3832 return true; 3833 } 3834 3835 if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) != 3836 nat_blkaddr) { 3837 f2fs_info(sbi, "Wrong SIT boundary, start(%u) end(%u) blocks(%u)", 3838 sit_blkaddr, nat_blkaddr, 3839 segment_count_sit << log_blocks_per_seg); 3840 return true; 3841 } 3842 3843 if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) != 3844 ssa_blkaddr) { 3845 f2fs_info(sbi, "Wrong NAT boundary, start(%u) end(%u) blocks(%u)", 3846 nat_blkaddr, ssa_blkaddr, 3847 segment_count_nat << log_blocks_per_seg); 3848 return true; 3849 } 3850 3851 if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) != 3852 main_blkaddr) { 3853 f2fs_info(sbi, "Wrong SSA boundary, start(%u) end(%u) blocks(%u)", 3854 ssa_blkaddr, main_blkaddr, 3855 segment_count_ssa << log_blocks_per_seg); 3856 return true; 3857 } 3858 3859 if (main_end_blkaddr > seg_end_blkaddr) { 3860 f2fs_info(sbi, "Wrong MAIN_AREA boundary, start(%u) end(%llu) block(%u)", 3861 main_blkaddr, seg_end_blkaddr, 3862 segment_count_main << log_blocks_per_seg); 3863 return true; 3864 } else if (main_end_blkaddr < seg_end_blkaddr) { 3865 int err = 0; 3866 char *res; 3867 3868 /* fix in-memory information all the time */ 3869 raw_super->segment_count = cpu_to_le32((main_end_blkaddr - 3870 segment0_blkaddr) >> log_blocks_per_seg); 3871 3872 if (f2fs_readonly(sb) || f2fs_hw_is_readonly(sbi)) { 3873 set_sbi_flag(sbi, SBI_NEED_SB_WRITE); 3874 res = "internally"; 3875 } else { 3876 err = __f2fs_commit_super(sbi, folio, index, false); 3877 res = err ? "failed" : "done"; 3878 } 3879 f2fs_info(sbi, "Fix alignment : %s, start(%u) end(%llu) block(%u)", 3880 res, main_blkaddr, seg_end_blkaddr, 3881 segment_count_main << log_blocks_per_seg); 3882 if (err) 3883 return true; 3884 } 3885 return false; 3886 } 3887 3888 static int sanity_check_raw_super(struct f2fs_sb_info *sbi, 3889 struct folio *folio, pgoff_t index) 3890 { 3891 block_t segment_count, segs_per_sec, secs_per_zone, segment_count_main; 3892 block_t total_sections, blocks_per_seg; 3893 struct f2fs_super_block *raw_super = F2FS_SUPER_BLOCK(folio, index); 3894 size_t crc_offset = 0; 3895 __u32 crc = 0; 3896 3897 if (le32_to_cpu(raw_super->magic) != F2FS_SUPER_MAGIC) { 3898 f2fs_info(sbi, "Magic Mismatch, valid(0x%x) - read(0x%x)", 3899 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic)); 3900 return -EINVAL; 3901 } 3902 3903 /* Check checksum_offset and crc in superblock */ 3904 if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_SB_CHKSUM)) { 3905 crc_offset = le32_to_cpu(raw_super->checksum_offset); 3906 if (crc_offset != 3907 offsetof(struct f2fs_super_block, crc)) { 3908 f2fs_info(sbi, "Invalid SB checksum offset: %zu", 3909 crc_offset); 3910 return -EFSCORRUPTED; 3911 } 3912 crc = le32_to_cpu(raw_super->crc); 3913 if (crc != f2fs_crc32(raw_super, crc_offset)) { 3914 f2fs_info(sbi, "Invalid SB checksum value: %u", crc); 3915 return -EFSCORRUPTED; 3916 } 3917 } 3918 3919 /* only support block_size equals to PAGE_SIZE */ 3920 if (le32_to_cpu(raw_super->log_blocksize) != F2FS_BLKSIZE_BITS) { 3921 f2fs_info(sbi, "Invalid log_blocksize (%u), supports only %u", 3922 le32_to_cpu(raw_super->log_blocksize), 3923 F2FS_BLKSIZE_BITS); 3924 return -EFSCORRUPTED; 3925 } 3926 3927 /* check log blocks per segment */ 3928 if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) { 3929 f2fs_info(sbi, "Invalid log blocks per segment (%u)", 3930 le32_to_cpu(raw_super->log_blocks_per_seg)); 3931 return -EFSCORRUPTED; 3932 } 3933 3934 /* Currently, support 512/1024/2048/4096/16K bytes sector size */ 3935 if (le32_to_cpu(raw_super->log_sectorsize) > 3936 F2FS_MAX_LOG_SECTOR_SIZE || 3937 le32_to_cpu(raw_super->log_sectorsize) < 3938 F2FS_MIN_LOG_SECTOR_SIZE) { 3939 f2fs_info(sbi, "Invalid log sectorsize (%u)", 3940 le32_to_cpu(raw_super->log_sectorsize)); 3941 return -EFSCORRUPTED; 3942 } 3943 if (le32_to_cpu(raw_super->log_sectors_per_block) + 3944 le32_to_cpu(raw_super->log_sectorsize) != 3945 F2FS_MAX_LOG_SECTOR_SIZE) { 3946 f2fs_info(sbi, "Invalid log sectors per block(%u) log sectorsize(%u)", 3947 le32_to_cpu(raw_super->log_sectors_per_block), 3948 le32_to_cpu(raw_super->log_sectorsize)); 3949 return -EFSCORRUPTED; 3950 } 3951 3952 segment_count = le32_to_cpu(raw_super->segment_count); 3953 segment_count_main = le32_to_cpu(raw_super->segment_count_main); 3954 segs_per_sec = le32_to_cpu(raw_super->segs_per_sec); 3955 secs_per_zone = le32_to_cpu(raw_super->secs_per_zone); 3956 total_sections = le32_to_cpu(raw_super->section_count); 3957 3958 /* blocks_per_seg should be 512, given the above check */ 3959 blocks_per_seg = BIT(le32_to_cpu(raw_super->log_blocks_per_seg)); 3960 3961 if (segment_count > F2FS_MAX_SEGMENT || 3962 segment_count < F2FS_MIN_SEGMENTS) { 3963 f2fs_info(sbi, "Invalid segment count (%u)", segment_count); 3964 return -EFSCORRUPTED; 3965 } 3966 3967 if (total_sections > segment_count_main || total_sections < 1 || 3968 segs_per_sec > segment_count || !segs_per_sec) { 3969 f2fs_info(sbi, "Invalid segment/section count (%u, %u x %u)", 3970 segment_count, total_sections, segs_per_sec); 3971 return -EFSCORRUPTED; 3972 } 3973 3974 if (segment_count_main != total_sections * segs_per_sec) { 3975 f2fs_info(sbi, "Invalid segment/section count (%u != %u * %u)", 3976 segment_count_main, total_sections, segs_per_sec); 3977 return -EFSCORRUPTED; 3978 } 3979 3980 if ((segment_count / segs_per_sec) < total_sections) { 3981 f2fs_info(sbi, "Small segment_count (%u < %u * %u)", 3982 segment_count, segs_per_sec, total_sections); 3983 return -EFSCORRUPTED; 3984 } 3985 3986 if (segment_count > (le64_to_cpu(raw_super->block_count) >> 9)) { 3987 f2fs_info(sbi, "Wrong segment_count / block_count (%u > %llu)", 3988 segment_count, le64_to_cpu(raw_super->block_count)); 3989 return -EFSCORRUPTED; 3990 } 3991 3992 if (RDEV(0).path[0]) { 3993 block_t dev_seg_count = le32_to_cpu(RDEV(0).total_segments); 3994 int i = 1; 3995 3996 while (i < MAX_DEVICES && RDEV(i).path[0]) { 3997 dev_seg_count += le32_to_cpu(RDEV(i).total_segments); 3998 i++; 3999 } 4000 if (segment_count != dev_seg_count) { 4001 f2fs_info(sbi, "Segment count (%u) mismatch with total segments from devices (%u)", 4002 segment_count, dev_seg_count); 4003 return -EFSCORRUPTED; 4004 } 4005 } else { 4006 if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_BLKZONED) && 4007 !bdev_is_zoned(sbi->sb->s_bdev)) { 4008 f2fs_info(sbi, "Zoned block device path is missing"); 4009 return -EFSCORRUPTED; 4010 } 4011 } 4012 4013 if (secs_per_zone > total_sections || !secs_per_zone) { 4014 f2fs_info(sbi, "Wrong secs_per_zone / total_sections (%u, %u)", 4015 secs_per_zone, total_sections); 4016 return -EFSCORRUPTED; 4017 } 4018 if (le32_to_cpu(raw_super->extension_count) > F2FS_MAX_EXTENSION || 4019 raw_super->hot_ext_count > F2FS_MAX_EXTENSION || 4020 (le32_to_cpu(raw_super->extension_count) + 4021 raw_super->hot_ext_count) > F2FS_MAX_EXTENSION) { 4022 f2fs_info(sbi, "Corrupted extension count (%u + %u > %u)", 4023 le32_to_cpu(raw_super->extension_count), 4024 raw_super->hot_ext_count, 4025 F2FS_MAX_EXTENSION); 4026 return -EFSCORRUPTED; 4027 } 4028 4029 if (le32_to_cpu(raw_super->cp_payload) >= 4030 (blocks_per_seg - F2FS_CP_PACKS - 4031 NR_CURSEG_PERSIST_TYPE)) { 4032 f2fs_info(sbi, "Insane cp_payload (%u >= %u)", 4033 le32_to_cpu(raw_super->cp_payload), 4034 blocks_per_seg - F2FS_CP_PACKS - 4035 NR_CURSEG_PERSIST_TYPE); 4036 return -EFSCORRUPTED; 4037 } 4038 4039 /* check reserved ino info */ 4040 if (le32_to_cpu(raw_super->node_ino) != 1 || 4041 le32_to_cpu(raw_super->meta_ino) != 2 || 4042 le32_to_cpu(raw_super->root_ino) != 3) { 4043 f2fs_info(sbi, "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)", 4044 le32_to_cpu(raw_super->node_ino), 4045 le32_to_cpu(raw_super->meta_ino), 4046 le32_to_cpu(raw_super->root_ino)); 4047 return -EFSCORRUPTED; 4048 } 4049 4050 /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */ 4051 if (sanity_check_area_boundary(sbi, folio, index)) 4052 return -EFSCORRUPTED; 4053 4054 return 0; 4055 } 4056 4057 int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi) 4058 { 4059 unsigned int total, fsmeta; 4060 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi); 4061 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 4062 unsigned int ovp_segments, reserved_segments; 4063 unsigned int main_segs, blocks_per_seg; 4064 unsigned int sit_segs, nat_segs; 4065 unsigned int sit_bitmap_size, nat_bitmap_size; 4066 unsigned int log_blocks_per_seg; 4067 unsigned int segment_count_main; 4068 unsigned int cp_pack_start_sum, cp_payload; 4069 block_t user_block_count, valid_user_blocks; 4070 block_t avail_node_count, valid_node_count; 4071 unsigned int nat_blocks, nat_bits_bytes, nat_bits_blocks; 4072 unsigned int sit_blk_cnt; 4073 int i, j; 4074 4075 total = le32_to_cpu(raw_super->segment_count); 4076 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt); 4077 sit_segs = le32_to_cpu(raw_super->segment_count_sit); 4078 fsmeta += sit_segs; 4079 nat_segs = le32_to_cpu(raw_super->segment_count_nat); 4080 fsmeta += nat_segs; 4081 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count); 4082 fsmeta += le32_to_cpu(raw_super->segment_count_ssa); 4083 4084 if (unlikely(fsmeta >= total)) 4085 return 1; 4086 4087 ovp_segments = le32_to_cpu(ckpt->overprov_segment_count); 4088 reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count); 4089 4090 if (!f2fs_sb_has_readonly(sbi) && 4091 unlikely(fsmeta < F2FS_MIN_META_SEGMENTS || 4092 ovp_segments == 0 || reserved_segments == 0)) { 4093 f2fs_err(sbi, "Wrong layout: check mkfs.f2fs version"); 4094 return 1; 4095 } 4096 user_block_count = le64_to_cpu(ckpt->user_block_count); 4097 segment_count_main = le32_to_cpu(raw_super->segment_count_main) + 4098 (f2fs_sb_has_readonly(sbi) ? 1 : 0); 4099 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg); 4100 if (!user_block_count || user_block_count >= 4101 segment_count_main << log_blocks_per_seg) { 4102 f2fs_err(sbi, "Wrong user_block_count: %u", 4103 user_block_count); 4104 return 1; 4105 } 4106 4107 valid_user_blocks = le64_to_cpu(ckpt->valid_block_count); 4108 if (valid_user_blocks > user_block_count) { 4109 f2fs_err(sbi, "Wrong valid_user_blocks: %u, user_block_count: %u", 4110 valid_user_blocks, user_block_count); 4111 return 1; 4112 } 4113 4114 valid_node_count = le32_to_cpu(ckpt->valid_node_count); 4115 avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM; 4116 if (valid_node_count > avail_node_count) { 4117 f2fs_err(sbi, "Wrong valid_node_count: %u, avail_node_count: %u", 4118 valid_node_count, avail_node_count); 4119 return 1; 4120 } 4121 4122 main_segs = le32_to_cpu(raw_super->segment_count_main); 4123 blocks_per_seg = BLKS_PER_SEG(sbi); 4124 4125 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) { 4126 if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs || 4127 le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg) 4128 return 1; 4129 4130 if (f2fs_sb_has_readonly(sbi)) 4131 goto check_data; 4132 4133 for (j = i + 1; j < NR_CURSEG_NODE_TYPE; j++) { 4134 if (le32_to_cpu(ckpt->cur_node_segno[i]) == 4135 le32_to_cpu(ckpt->cur_node_segno[j])) { 4136 f2fs_err(sbi, "Node segment (%u, %u) has the same segno: %u", 4137 i, j, 4138 le32_to_cpu(ckpt->cur_node_segno[i])); 4139 return 1; 4140 } 4141 } 4142 } 4143 check_data: 4144 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) { 4145 if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs || 4146 le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg) 4147 return 1; 4148 4149 if (f2fs_sb_has_readonly(sbi)) 4150 goto skip_cross; 4151 4152 for (j = i + 1; j < NR_CURSEG_DATA_TYPE; j++) { 4153 if (le32_to_cpu(ckpt->cur_data_segno[i]) == 4154 le32_to_cpu(ckpt->cur_data_segno[j])) { 4155 f2fs_err(sbi, "Data segment (%u, %u) has the same segno: %u", 4156 i, j, 4157 le32_to_cpu(ckpt->cur_data_segno[i])); 4158 return 1; 4159 } 4160 } 4161 } 4162 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) { 4163 for (j = 0; j < NR_CURSEG_DATA_TYPE; j++) { 4164 if (le32_to_cpu(ckpt->cur_node_segno[i]) == 4165 le32_to_cpu(ckpt->cur_data_segno[j])) { 4166 f2fs_err(sbi, "Node segment (%u) and Data segment (%u) has the same segno: %u", 4167 i, j, 4168 le32_to_cpu(ckpt->cur_node_segno[i])); 4169 return 1; 4170 } 4171 } 4172 } 4173 skip_cross: 4174 sit_bitmap_size = le32_to_cpu(ckpt->sit_ver_bitmap_bytesize); 4175 nat_bitmap_size = le32_to_cpu(ckpt->nat_ver_bitmap_bytesize); 4176 4177 if (sit_bitmap_size != ((sit_segs / 2) << log_blocks_per_seg) / 8 || 4178 nat_bitmap_size != ((nat_segs / 2) << log_blocks_per_seg) / 8) { 4179 f2fs_err(sbi, "Wrong bitmap size: sit: %u, nat:%u", 4180 sit_bitmap_size, nat_bitmap_size); 4181 return 1; 4182 } 4183 4184 sit_blk_cnt = DIV_ROUND_UP(main_segs, SIT_ENTRY_PER_BLOCK); 4185 if (sit_bitmap_size * 8 < sit_blk_cnt) { 4186 f2fs_err(sbi, "Wrong bitmap size: sit: %u, sit_blk_cnt:%u", 4187 sit_bitmap_size, sit_blk_cnt); 4188 return 1; 4189 } 4190 4191 cp_pack_start_sum = __start_sum_addr(sbi); 4192 cp_payload = __cp_payload(sbi); 4193 if (cp_pack_start_sum < cp_payload + 1 || 4194 cp_pack_start_sum > blocks_per_seg - 1 - 4195 NR_CURSEG_PERSIST_TYPE) { 4196 f2fs_err(sbi, "Wrong cp_pack_start_sum: %u", 4197 cp_pack_start_sum); 4198 return 1; 4199 } 4200 4201 if (__is_set_ckpt_flags(ckpt, CP_LARGE_NAT_BITMAP_FLAG) && 4202 le32_to_cpu(ckpt->checksum_offset) != CP_MIN_CHKSUM_OFFSET) { 4203 f2fs_warn(sbi, "using deprecated layout of large_nat_bitmap, " 4204 "please run fsck v1.13.0 or higher to repair, chksum_offset: %u, " 4205 "fixed with patch: \"f2fs-tools: relocate chksum_offset for large_nat_bitmap feature\"", 4206 le32_to_cpu(ckpt->checksum_offset)); 4207 return 1; 4208 } 4209 4210 nat_blocks = nat_segs << log_blocks_per_seg; 4211 nat_bits_bytes = nat_blocks / BITS_PER_BYTE; 4212 nat_bits_blocks = F2FS_BLK_ALIGN((nat_bits_bytes << 1) + 8); 4213 if (__is_set_ckpt_flags(ckpt, CP_NAT_BITS_FLAG) && 4214 (cp_payload + F2FS_CP_PACKS + 4215 NR_CURSEG_PERSIST_TYPE + nat_bits_blocks >= blocks_per_seg)) { 4216 f2fs_warn(sbi, "Insane cp_payload: %u, nat_bits_blocks: %u)", 4217 cp_payload, nat_bits_blocks); 4218 return 1; 4219 } 4220 4221 if (unlikely(f2fs_cp_error(sbi))) { 4222 f2fs_err(sbi, "A bug case: need to run fsck"); 4223 return 1; 4224 } 4225 return 0; 4226 } 4227 4228 static void init_sb_info(struct f2fs_sb_info *sbi) 4229 { 4230 struct f2fs_super_block *raw_super = sbi->raw_super; 4231 int i; 4232 4233 sbi->log_sectors_per_block = 4234 le32_to_cpu(raw_super->log_sectors_per_block); 4235 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize); 4236 sbi->blocksize = BIT(sbi->log_blocksize); 4237 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg); 4238 sbi->blocks_per_seg = BIT(sbi->log_blocks_per_seg); 4239 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec); 4240 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone); 4241 sbi->total_sections = le32_to_cpu(raw_super->section_count); 4242 sbi->total_node_count = SEGS_TO_BLKS(sbi, 4243 ((le32_to_cpu(raw_super->segment_count_nat) / 2) * 4244 NAT_ENTRY_PER_BLOCK)); 4245 sbi->allocate_section_hint = le32_to_cpu(raw_super->section_count); 4246 sbi->allocate_section_policy = ALLOCATE_FORWARD_NOHINT; 4247 F2FS_ROOT_INO(sbi) = le32_to_cpu(raw_super->root_ino); 4248 F2FS_NODE_INO(sbi) = le32_to_cpu(raw_super->node_ino); 4249 F2FS_META_INO(sbi) = le32_to_cpu(raw_super->meta_ino); 4250 sbi->cur_victim_sec = NULL_SECNO; 4251 sbi->gc_mode = GC_NORMAL; 4252 sbi->next_victim_seg[BG_GC] = NULL_SEGNO; 4253 sbi->next_victim_seg[FG_GC] = NULL_SEGNO; 4254 sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH; 4255 sbi->migration_granularity = SEGS_PER_SEC(sbi); 4256 sbi->migration_window_granularity = f2fs_sb_has_blkzoned(sbi) ? 4257 DEF_MIGRATION_WINDOW_GRANULARITY_ZONED : SEGS_PER_SEC(sbi); 4258 sbi->seq_file_ra_mul = MIN_RA_MUL; 4259 sbi->max_fragment_chunk = DEF_FRAGMENT_SIZE; 4260 sbi->max_fragment_hole = DEF_FRAGMENT_SIZE; 4261 spin_lock_init(&sbi->gc_remaining_trials_lock); 4262 atomic64_set(&sbi->current_atomic_write, 0); 4263 4264 sbi->dir_level = DEF_DIR_LEVEL; 4265 sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL; 4266 sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL; 4267 sbi->interval_time[DISCARD_TIME] = DEF_IDLE_INTERVAL; 4268 sbi->interval_time[GC_TIME] = DEF_IDLE_INTERVAL; 4269 sbi->interval_time[DISABLE_TIME] = DEF_DISABLE_INTERVAL; 4270 sbi->interval_time[ENABLE_TIME] = DEF_ENABLE_INTERVAL; 4271 sbi->interval_time[UMOUNT_DISCARD_TIMEOUT] = 4272 DEF_UMOUNT_DISCARD_TIMEOUT; 4273 clear_sbi_flag(sbi, SBI_NEED_FSCK); 4274 4275 for (i = 0; i < NR_COUNT_TYPE; i++) 4276 atomic_set(&sbi->nr_pages[i], 0); 4277 4278 for (i = 0; i < META; i++) 4279 atomic_set(&sbi->wb_sync_req[i], 0); 4280 4281 INIT_LIST_HEAD(&sbi->s_list); 4282 mutex_init(&sbi->umount_mutex); 4283 init_f2fs_rwsem(&sbi->io_order_lock); 4284 spin_lock_init(&sbi->cp_lock); 4285 4286 sbi->dirty_device = 0; 4287 spin_lock_init(&sbi->dev_lock); 4288 4289 init_f2fs_rwsem(&sbi->sb_lock); 4290 init_f2fs_rwsem(&sbi->pin_sem); 4291 } 4292 4293 static int init_percpu_info(struct f2fs_sb_info *sbi) 4294 { 4295 int err; 4296 4297 err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL); 4298 if (err) 4299 return err; 4300 4301 err = percpu_counter_init(&sbi->rf_node_block_count, 0, GFP_KERNEL); 4302 if (err) 4303 goto err_valid_block; 4304 4305 err = percpu_counter_init(&sbi->total_valid_inode_count, 0, 4306 GFP_KERNEL); 4307 if (err) 4308 goto err_node_block; 4309 return 0; 4310 4311 err_node_block: 4312 percpu_counter_destroy(&sbi->rf_node_block_count); 4313 err_valid_block: 4314 percpu_counter_destroy(&sbi->alloc_valid_block_count); 4315 return err; 4316 } 4317 4318 #ifdef CONFIG_BLK_DEV_ZONED 4319 4320 struct f2fs_report_zones_args { 4321 struct f2fs_sb_info *sbi; 4322 struct f2fs_dev_info *dev; 4323 }; 4324 4325 static int f2fs_report_zone_cb(struct blk_zone *zone, unsigned int idx, 4326 void *data) 4327 { 4328 struct f2fs_report_zones_args *rz_args = data; 4329 block_t unusable_blocks = (zone->len - zone->capacity) >> 4330 F2FS_LOG_SECTORS_PER_BLOCK; 4331 4332 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL) 4333 return 0; 4334 4335 set_bit(idx, rz_args->dev->blkz_seq); 4336 if (!rz_args->sbi->unusable_blocks_per_sec) { 4337 rz_args->sbi->unusable_blocks_per_sec = unusable_blocks; 4338 return 0; 4339 } 4340 if (rz_args->sbi->unusable_blocks_per_sec != unusable_blocks) { 4341 f2fs_err(rz_args->sbi, "F2FS supports single zone capacity\n"); 4342 return -EINVAL; 4343 } 4344 return 0; 4345 } 4346 4347 static int init_blkz_info(struct f2fs_sb_info *sbi, int devi) 4348 { 4349 struct block_device *bdev = FDEV(devi).bdev; 4350 sector_t nr_sectors = bdev_nr_sectors(bdev); 4351 struct f2fs_report_zones_args rep_zone_arg; 4352 u64 zone_sectors; 4353 unsigned int max_open_zones; 4354 int ret; 4355 4356 if (!f2fs_sb_has_blkzoned(sbi)) 4357 return 0; 4358 4359 if (bdev_is_zoned(FDEV(devi).bdev)) { 4360 max_open_zones = bdev_max_open_zones(bdev); 4361 if (max_open_zones && (max_open_zones < sbi->max_open_zones)) 4362 sbi->max_open_zones = max_open_zones; 4363 if (sbi->max_open_zones < F2FS_OPTION(sbi).active_logs) { 4364 f2fs_err(sbi, 4365 "zoned: max open zones %u is too small, need at least %u open zones", 4366 sbi->max_open_zones, F2FS_OPTION(sbi).active_logs); 4367 return -EINVAL; 4368 } 4369 } 4370 4371 zone_sectors = bdev_zone_sectors(bdev); 4372 if (sbi->blocks_per_blkz && sbi->blocks_per_blkz != 4373 SECTOR_TO_BLOCK(zone_sectors)) 4374 return -EINVAL; 4375 sbi->blocks_per_blkz = SECTOR_TO_BLOCK(zone_sectors); 4376 FDEV(devi).nr_blkz = div_u64(SECTOR_TO_BLOCK(nr_sectors), 4377 sbi->blocks_per_blkz); 4378 if (nr_sectors & (zone_sectors - 1)) 4379 FDEV(devi).nr_blkz++; 4380 4381 FDEV(devi).blkz_seq = f2fs_kvzalloc(sbi, 4382 BITS_TO_LONGS(FDEV(devi).nr_blkz) 4383 * sizeof(unsigned long), 4384 GFP_KERNEL); 4385 if (!FDEV(devi).blkz_seq) 4386 return -ENOMEM; 4387 4388 rep_zone_arg.sbi = sbi; 4389 rep_zone_arg.dev = &FDEV(devi); 4390 4391 ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES, f2fs_report_zone_cb, 4392 &rep_zone_arg); 4393 if (ret < 0) 4394 return ret; 4395 return 0; 4396 } 4397 #endif 4398 4399 /* 4400 * Read f2fs raw super block. 4401 * Because we have two copies of super block, so read both of them 4402 * to get the first valid one. If any one of them is broken, we pass 4403 * them recovery flag back to the caller. 4404 */ 4405 static int read_raw_super_block(struct f2fs_sb_info *sbi, 4406 struct f2fs_super_block **raw_super, 4407 int *valid_super_block, int *recovery) 4408 { 4409 struct super_block *sb = sbi->sb; 4410 int block; 4411 struct folio *folio; 4412 struct f2fs_super_block *super; 4413 int err = 0; 4414 4415 super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL); 4416 if (!super) 4417 return -ENOMEM; 4418 4419 for (block = 0; block < 2; block++) { 4420 folio = read_mapping_folio(sb->s_bdev->bd_mapping, block, NULL); 4421 if (IS_ERR(folio)) { 4422 f2fs_err(sbi, "Unable to read %dth superblock", 4423 block + 1); 4424 err = PTR_ERR(folio); 4425 *recovery = 1; 4426 continue; 4427 } 4428 4429 /* sanity checking of raw super */ 4430 err = sanity_check_raw_super(sbi, folio, block); 4431 if (err) { 4432 f2fs_err(sbi, "Can't find valid F2FS filesystem in %dth superblock", 4433 block + 1); 4434 folio_put(folio); 4435 *recovery = 1; 4436 continue; 4437 } 4438 4439 if (!*raw_super) { 4440 memcpy(super, F2FS_SUPER_BLOCK(folio, block), 4441 sizeof(*super)); 4442 *valid_super_block = block; 4443 *raw_super = super; 4444 } 4445 folio_put(folio); 4446 } 4447 4448 /* No valid superblock */ 4449 if (!*raw_super) 4450 kfree(super); 4451 else 4452 err = 0; 4453 4454 return err; 4455 } 4456 4457 int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover) 4458 { 4459 struct folio *folio; 4460 pgoff_t index; 4461 __u32 crc = 0; 4462 int err; 4463 4464 if ((recover && f2fs_readonly(sbi->sb)) || 4465 f2fs_hw_is_readonly(sbi)) { 4466 set_sbi_flag(sbi, SBI_NEED_SB_WRITE); 4467 return -EROFS; 4468 } 4469 4470 /* we should update superblock crc here */ 4471 if (!recover && f2fs_sb_has_sb_chksum(sbi)) { 4472 crc = f2fs_crc32(F2FS_RAW_SUPER(sbi), 4473 offsetof(struct f2fs_super_block, crc)); 4474 F2FS_RAW_SUPER(sbi)->crc = cpu_to_le32(crc); 4475 } 4476 4477 /* write back-up superblock first */ 4478 index = sbi->valid_super_block ? 0 : 1; 4479 folio = read_mapping_folio(sbi->sb->s_bdev->bd_mapping, index, NULL); 4480 if (IS_ERR(folio)) 4481 return PTR_ERR(folio); 4482 err = __f2fs_commit_super(sbi, folio, index, true); 4483 folio_put(folio); 4484 4485 /* if we are in recovery path, skip writing valid superblock */ 4486 if (recover || err) 4487 return err; 4488 4489 /* write current valid superblock */ 4490 index = sbi->valid_super_block; 4491 folio = read_mapping_folio(sbi->sb->s_bdev->bd_mapping, index, NULL); 4492 if (IS_ERR(folio)) 4493 return PTR_ERR(folio); 4494 err = __f2fs_commit_super(sbi, folio, index, true); 4495 folio_put(folio); 4496 return err; 4497 } 4498 4499 static void save_stop_reason(struct f2fs_sb_info *sbi, unsigned char reason) 4500 { 4501 unsigned long flags; 4502 4503 spin_lock_irqsave(&sbi->error_lock, flags); 4504 if (sbi->stop_reason[reason] < GENMASK(BITS_PER_BYTE - 1, 0)) 4505 sbi->stop_reason[reason]++; 4506 spin_unlock_irqrestore(&sbi->error_lock, flags); 4507 } 4508 4509 static void f2fs_record_stop_reason(struct f2fs_sb_info *sbi) 4510 { 4511 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi); 4512 unsigned long flags; 4513 int err; 4514 4515 f2fs_down_write(&sbi->sb_lock); 4516 4517 spin_lock_irqsave(&sbi->error_lock, flags); 4518 if (sbi->error_dirty) { 4519 memcpy(F2FS_RAW_SUPER(sbi)->s_errors, sbi->errors, 4520 MAX_F2FS_ERRORS); 4521 sbi->error_dirty = false; 4522 } 4523 memcpy(raw_super->s_stop_reason, sbi->stop_reason, MAX_STOP_REASON); 4524 spin_unlock_irqrestore(&sbi->error_lock, flags); 4525 4526 err = f2fs_commit_super(sbi, false); 4527 4528 f2fs_up_write(&sbi->sb_lock); 4529 if (err) 4530 f2fs_err_ratelimited(sbi, 4531 "f2fs_commit_super fails to record stop_reason, err:%d", 4532 err); 4533 } 4534 4535 void f2fs_save_errors(struct f2fs_sb_info *sbi, unsigned char flag) 4536 { 4537 unsigned long flags; 4538 4539 spin_lock_irqsave(&sbi->error_lock, flags); 4540 if (!test_bit(flag, (unsigned long *)sbi->errors)) { 4541 set_bit(flag, (unsigned long *)sbi->errors); 4542 sbi->error_dirty = true; 4543 } 4544 spin_unlock_irqrestore(&sbi->error_lock, flags); 4545 } 4546 4547 static bool f2fs_update_errors(struct f2fs_sb_info *sbi) 4548 { 4549 unsigned long flags; 4550 bool need_update = false; 4551 4552 spin_lock_irqsave(&sbi->error_lock, flags); 4553 if (sbi->error_dirty) { 4554 memcpy(F2FS_RAW_SUPER(sbi)->s_errors, sbi->errors, 4555 MAX_F2FS_ERRORS); 4556 sbi->error_dirty = false; 4557 need_update = true; 4558 } 4559 spin_unlock_irqrestore(&sbi->error_lock, flags); 4560 4561 return need_update; 4562 } 4563 4564 static void f2fs_record_errors(struct f2fs_sb_info *sbi, unsigned char error) 4565 { 4566 int err; 4567 4568 f2fs_down_write(&sbi->sb_lock); 4569 4570 if (!f2fs_update_errors(sbi)) 4571 goto out_unlock; 4572 4573 err = f2fs_commit_super(sbi, false); 4574 if (err) 4575 f2fs_err_ratelimited(sbi, 4576 "f2fs_commit_super fails to record errors:%u, err:%d", 4577 error, err); 4578 out_unlock: 4579 f2fs_up_write(&sbi->sb_lock); 4580 } 4581 4582 void f2fs_handle_error(struct f2fs_sb_info *sbi, unsigned char error) 4583 { 4584 f2fs_save_errors(sbi, error); 4585 f2fs_record_errors(sbi, error); 4586 } 4587 4588 void f2fs_handle_error_async(struct f2fs_sb_info *sbi, unsigned char error) 4589 { 4590 f2fs_save_errors(sbi, error); 4591 4592 if (!sbi->error_dirty) 4593 return; 4594 if (!test_bit(error, (unsigned long *)sbi->errors)) 4595 return; 4596 schedule_work(&sbi->s_error_work); 4597 } 4598 4599 static bool system_going_down(void) 4600 { 4601 return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF 4602 || system_state == SYSTEM_RESTART; 4603 } 4604 4605 void f2fs_handle_critical_error(struct f2fs_sb_info *sbi, unsigned char reason) 4606 { 4607 struct super_block *sb = sbi->sb; 4608 bool shutdown = reason == STOP_CP_REASON_SHUTDOWN; 4609 bool continue_fs = !shutdown && 4610 F2FS_OPTION(sbi).errors == MOUNT_ERRORS_CONTINUE; 4611 4612 set_ckpt_flags(sbi, CP_ERROR_FLAG); 4613 4614 if (!f2fs_hw_is_readonly(sbi)) { 4615 save_stop_reason(sbi, reason); 4616 4617 /* 4618 * always create an asynchronous task to record stop_reason 4619 * in order to avoid potential deadlock when running into 4620 * f2fs_record_stop_reason() synchronously. 4621 */ 4622 schedule_work(&sbi->s_error_work); 4623 } 4624 4625 /* 4626 * We force ERRORS_RO behavior when system is rebooting. Otherwise we 4627 * could panic during 'reboot -f' as the underlying device got already 4628 * disabled. 4629 */ 4630 if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_PANIC && 4631 !shutdown && !system_going_down() && 4632 !is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN)) 4633 panic("F2FS-fs (device %s): panic forced after error\n", 4634 sb->s_id); 4635 4636 if (shutdown) 4637 set_sbi_flag(sbi, SBI_IS_SHUTDOWN); 4638 else 4639 dump_stack(); 4640 4641 /* 4642 * Continue filesystem operators if errors=continue. Should not set 4643 * RO by shutdown, since RO bypasses thaw_super which can hang the 4644 * system. 4645 */ 4646 if (continue_fs || f2fs_readonly(sb) || shutdown) { 4647 f2fs_warn(sbi, "Stopped filesystem due to reason: %d", reason); 4648 return; 4649 } 4650 4651 f2fs_warn(sbi, "Remounting filesystem read-only"); 4652 4653 /* 4654 * We have already set CP_ERROR_FLAG flag to stop all updates 4655 * to filesystem, so it doesn't need to set SB_RDONLY flag here 4656 * because the flag should be set covered w/ sb->s_umount semaphore 4657 * via remount procedure, otherwise, it will confuse code like 4658 * freeze_super() which will lead to deadlocks and other problems. 4659 */ 4660 } 4661 4662 static void f2fs_record_error_work(struct work_struct *work) 4663 { 4664 struct f2fs_sb_info *sbi = container_of(work, 4665 struct f2fs_sb_info, s_error_work); 4666 4667 f2fs_record_stop_reason(sbi); 4668 } 4669 4670 static inline unsigned int get_first_seq_zone_segno(struct f2fs_sb_info *sbi) 4671 { 4672 #ifdef CONFIG_BLK_DEV_ZONED 4673 unsigned int zoneno, total_zones; 4674 int devi; 4675 4676 if (!f2fs_sb_has_blkzoned(sbi)) 4677 return NULL_SEGNO; 4678 4679 for (devi = 0; devi < sbi->s_ndevs; devi++) { 4680 if (!bdev_is_zoned(FDEV(devi).bdev)) 4681 continue; 4682 4683 total_zones = GET_ZONE_FROM_SEG(sbi, FDEV(devi).total_segments); 4684 4685 for (zoneno = 0; zoneno < total_zones; zoneno++) { 4686 unsigned int segs, blks; 4687 4688 if (!f2fs_zone_is_seq(sbi, devi, zoneno)) 4689 continue; 4690 4691 segs = GET_SEG_FROM_SEC(sbi, 4692 zoneno * sbi->secs_per_zone); 4693 blks = SEGS_TO_BLKS(sbi, segs); 4694 return GET_SEGNO(sbi, FDEV(devi).start_blk + blks); 4695 } 4696 } 4697 #endif 4698 return NULL_SEGNO; 4699 } 4700 4701 static int f2fs_scan_devices(struct f2fs_sb_info *sbi) 4702 { 4703 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi); 4704 unsigned int max_devices = MAX_DEVICES; 4705 unsigned int logical_blksize; 4706 blk_mode_t mode = sb_open_mode(sbi->sb->s_flags); 4707 int i; 4708 4709 /* Initialize single device information */ 4710 if (!RDEV(0).path[0]) { 4711 if (!bdev_is_zoned(sbi->sb->s_bdev)) 4712 return 0; 4713 max_devices = 1; 4714 } 4715 4716 /* 4717 * Initialize multiple devices information, or single 4718 * zoned block device information. 4719 */ 4720 sbi->devs = f2fs_kzalloc(sbi, 4721 array_size(max_devices, 4722 sizeof(struct f2fs_dev_info)), 4723 GFP_KERNEL); 4724 if (!sbi->devs) 4725 return -ENOMEM; 4726 4727 logical_blksize = bdev_logical_block_size(sbi->sb->s_bdev); 4728 sbi->aligned_blksize = true; 4729 sbi->bggc_io_aware = AWARE_ALL_IO; 4730 #ifdef CONFIG_BLK_DEV_ZONED 4731 sbi->max_open_zones = UINT_MAX; 4732 sbi->blkzone_alloc_policy = BLKZONE_ALLOC_PRIOR_SEQ; 4733 sbi->bggc_io_aware = AWARE_READ_IO; 4734 #endif 4735 4736 for (i = 0; i < max_devices; i++) { 4737 if (max_devices == 1) { 4738 FDEV(i).total_segments = 4739 le32_to_cpu(raw_super->segment_count_main); 4740 FDEV(i).start_blk = 0; 4741 FDEV(i).end_blk = FDEV(i).total_segments * 4742 BLKS_PER_SEG(sbi); 4743 } 4744 4745 if (i == 0) 4746 FDEV(0).bdev_file = sbi->sb->s_bdev_file; 4747 else if (!RDEV(i).path[0]) 4748 break; 4749 4750 if (max_devices > 1) { 4751 /* Multi-device mount */ 4752 memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN); 4753 FDEV(i).total_segments = 4754 le32_to_cpu(RDEV(i).total_segments); 4755 if (i == 0) { 4756 FDEV(i).start_blk = 0; 4757 FDEV(i).end_blk = FDEV(i).start_blk + 4758 SEGS_TO_BLKS(sbi, 4759 FDEV(i).total_segments) - 1 + 4760 le32_to_cpu(raw_super->segment0_blkaddr); 4761 sbi->allocate_section_hint = FDEV(i).total_segments / 4762 SEGS_PER_SEC(sbi); 4763 } else { 4764 FDEV(i).start_blk = FDEV(i - 1).end_blk + 1; 4765 FDEV(i).end_blk = FDEV(i).start_blk + 4766 SEGS_TO_BLKS(sbi, 4767 FDEV(i).total_segments) - 1; 4768 FDEV(i).bdev_file = bdev_file_open_by_path( 4769 FDEV(i).path, mode, sbi->sb, NULL); 4770 } 4771 } 4772 if (IS_ERR(FDEV(i).bdev_file)) 4773 return PTR_ERR(FDEV(i).bdev_file); 4774 4775 FDEV(i).bdev = file_bdev(FDEV(i).bdev_file); 4776 /* to release errored devices */ 4777 sbi->s_ndevs = i + 1; 4778 4779 if (logical_blksize != bdev_logical_block_size(FDEV(i).bdev)) 4780 sbi->aligned_blksize = false; 4781 4782 #ifdef CONFIG_BLK_DEV_ZONED 4783 if (bdev_is_zoned(FDEV(i).bdev)) { 4784 if (!f2fs_sb_has_blkzoned(sbi)) { 4785 f2fs_err(sbi, "Zoned block device feature not enabled"); 4786 return -EINVAL; 4787 } 4788 if (init_blkz_info(sbi, i)) { 4789 f2fs_err(sbi, "Failed to initialize F2FS blkzone information"); 4790 return -EINVAL; 4791 } 4792 if (max_devices == 1) 4793 break; 4794 f2fs_info(sbi, "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: Host-managed)", 4795 i, FDEV(i).path, 4796 FDEV(i).total_segments, 4797 FDEV(i).start_blk, FDEV(i).end_blk); 4798 continue; 4799 } 4800 #endif 4801 f2fs_info(sbi, "Mount Device [%2d]: %20s, %8u, %8x - %8x", 4802 i, FDEV(i).path, 4803 FDEV(i).total_segments, 4804 FDEV(i).start_blk, FDEV(i).end_blk); 4805 } 4806 return 0; 4807 } 4808 4809 static int f2fs_setup_casefold(struct f2fs_sb_info *sbi) 4810 { 4811 #if IS_ENABLED(CONFIG_UNICODE) 4812 if (f2fs_sb_has_casefold(sbi) && !sbi->sb->s_encoding) { 4813 const struct f2fs_sb_encodings *encoding_info; 4814 struct unicode_map *encoding; 4815 __u16 encoding_flags; 4816 4817 encoding_info = f2fs_sb_read_encoding(sbi->raw_super); 4818 if (!encoding_info) { 4819 f2fs_err(sbi, 4820 "Encoding requested by superblock is unknown"); 4821 return -EINVAL; 4822 } 4823 4824 encoding_flags = le16_to_cpu(sbi->raw_super->s_encoding_flags); 4825 encoding = utf8_load(encoding_info->version); 4826 if (IS_ERR(encoding)) { 4827 f2fs_err(sbi, 4828 "can't mount with superblock charset: %s-%u.%u.%u " 4829 "not supported by the kernel. flags: 0x%x.", 4830 encoding_info->name, 4831 unicode_major(encoding_info->version), 4832 unicode_minor(encoding_info->version), 4833 unicode_rev(encoding_info->version), 4834 encoding_flags); 4835 return PTR_ERR(encoding); 4836 } 4837 f2fs_info(sbi, "Using encoding defined by superblock: " 4838 "%s-%u.%u.%u with flags 0x%hx", encoding_info->name, 4839 unicode_major(encoding_info->version), 4840 unicode_minor(encoding_info->version), 4841 unicode_rev(encoding_info->version), 4842 encoding_flags); 4843 4844 sbi->sb->s_encoding = encoding; 4845 sbi->sb->s_encoding_flags = encoding_flags; 4846 } 4847 #else 4848 if (f2fs_sb_has_casefold(sbi)) { 4849 f2fs_err(sbi, "Filesystem with casefold feature cannot be mounted without CONFIG_UNICODE"); 4850 return -EINVAL; 4851 } 4852 #endif 4853 return 0; 4854 } 4855 4856 static void f2fs_tuning_parameters(struct f2fs_sb_info *sbi) 4857 { 4858 /* adjust parameters according to the volume size */ 4859 if (MAIN_SEGS(sbi) <= SMALL_VOLUME_SEGMENTS) { 4860 if (f2fs_block_unit_discard(sbi)) 4861 SM_I(sbi)->dcc_info->discard_granularity = 4862 MIN_DISCARD_GRANULARITY; 4863 if (!f2fs_lfs_mode(sbi)) 4864 SM_I(sbi)->ipu_policy = BIT(F2FS_IPU_FORCE) | 4865 BIT(F2FS_IPU_HONOR_OPU_WRITE); 4866 } 4867 4868 sbi->readdir_ra = true; 4869 } 4870 4871 static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc) 4872 { 4873 struct f2fs_fs_context *ctx = fc->fs_private; 4874 struct f2fs_sb_info *sbi; 4875 struct f2fs_super_block *raw_super; 4876 struct inode *root; 4877 int err; 4878 bool skip_recovery = false, need_fsck = false; 4879 int recovery, i, valid_super_block; 4880 struct curseg_info *seg_i; 4881 int retry_cnt = 1; 4882 #ifdef CONFIG_QUOTA 4883 bool quota_enabled = false; 4884 #endif 4885 4886 try_onemore: 4887 err = -EINVAL; 4888 raw_super = NULL; 4889 valid_super_block = -1; 4890 recovery = 0; 4891 4892 /* allocate memory for f2fs-specific super block info */ 4893 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL); 4894 if (!sbi) 4895 return -ENOMEM; 4896 4897 sbi->sb = sb; 4898 4899 /* initialize locks within allocated memory */ 4900 init_f2fs_rwsem(&sbi->gc_lock); 4901 mutex_init(&sbi->writepages); 4902 init_f2fs_rwsem(&sbi->cp_global_sem); 4903 init_f2fs_rwsem(&sbi->node_write); 4904 init_f2fs_rwsem(&sbi->node_change); 4905 spin_lock_init(&sbi->stat_lock); 4906 init_f2fs_rwsem(&sbi->cp_rwsem); 4907 init_f2fs_rwsem(&sbi->quota_sem); 4908 init_waitqueue_head(&sbi->cp_wait); 4909 spin_lock_init(&sbi->error_lock); 4910 4911 for (i = 0; i < NR_INODE_TYPE; i++) { 4912 INIT_LIST_HEAD(&sbi->inode_list[i]); 4913 spin_lock_init(&sbi->inode_lock[i]); 4914 } 4915 mutex_init(&sbi->flush_lock); 4916 4917 /* set a block size */ 4918 if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) { 4919 f2fs_err(sbi, "unable to set blocksize"); 4920 goto free_sbi; 4921 } 4922 4923 err = read_raw_super_block(sbi, &raw_super, &valid_super_block, 4924 &recovery); 4925 if (err) 4926 goto free_sbi; 4927 4928 sb->s_fs_info = sbi; 4929 sbi->raw_super = raw_super; 4930 4931 INIT_WORK(&sbi->s_error_work, f2fs_record_error_work); 4932 memcpy(sbi->errors, raw_super->s_errors, MAX_F2FS_ERRORS); 4933 memcpy(sbi->stop_reason, raw_super->s_stop_reason, MAX_STOP_REASON); 4934 4935 /* precompute checksum seed for metadata */ 4936 if (f2fs_sb_has_inode_chksum(sbi)) 4937 sbi->s_chksum_seed = f2fs_chksum(~0, raw_super->uuid, 4938 sizeof(raw_super->uuid)); 4939 4940 default_options(sbi, false); 4941 4942 err = f2fs_check_opt_consistency(fc, sb); 4943 if (err) 4944 goto free_sb_buf; 4945 4946 f2fs_apply_options(fc, sb); 4947 4948 err = f2fs_sanity_check_options(sbi, false); 4949 if (err) 4950 goto free_options; 4951 4952 sb->s_maxbytes = max_file_blocks(NULL) << 4953 le32_to_cpu(raw_super->log_blocksize); 4954 sb->s_max_links = F2FS_LINK_MAX; 4955 4956 err = f2fs_setup_casefold(sbi); 4957 if (err) 4958 goto free_options; 4959 4960 #ifdef CONFIG_QUOTA 4961 sb->dq_op = &f2fs_quota_operations; 4962 sb->s_qcop = &f2fs_quotactl_ops; 4963 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ; 4964 4965 if (f2fs_sb_has_quota_ino(sbi)) { 4966 for (i = 0; i < MAXQUOTAS; i++) { 4967 if (f2fs_qf_ino(sbi->sb, i)) 4968 sbi->nquota_files++; 4969 } 4970 } 4971 #endif 4972 4973 sb->s_op = &f2fs_sops; 4974 #ifdef CONFIG_FS_ENCRYPTION 4975 sb->s_cop = &f2fs_cryptops; 4976 #endif 4977 #ifdef CONFIG_FS_VERITY 4978 sb->s_vop = &f2fs_verityops; 4979 #endif 4980 sb->s_xattr = f2fs_xattr_handlers; 4981 sb->s_export_op = &f2fs_export_ops; 4982 sb->s_magic = F2FS_SUPER_MAGIC; 4983 sb->s_time_gran = 1; 4984 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) | 4985 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0); 4986 if (test_opt(sbi, INLINECRYPT)) 4987 sb->s_flags |= SB_INLINECRYPT; 4988 4989 if (test_opt(sbi, LAZYTIME)) 4990 sb->s_flags |= SB_LAZYTIME; 4991 else 4992 sb->s_flags &= ~SB_LAZYTIME; 4993 4994 super_set_uuid(sb, (void *) raw_super->uuid, sizeof(raw_super->uuid)); 4995 super_set_sysfs_name_bdev(sb); 4996 sb->s_iflags |= SB_I_CGROUPWB; 4997 4998 /* init f2fs-specific super block info */ 4999 sbi->valid_super_block = valid_super_block; 5000 5001 /* disallow all the data/node/meta page writes */ 5002 set_sbi_flag(sbi, SBI_POR_DOING); 5003 5004 err = f2fs_init_write_merge_io(sbi); 5005 if (err) 5006 goto free_bio_info; 5007 5008 init_sb_info(sbi); 5009 5010 err = f2fs_init_iostat(sbi); 5011 if (err) 5012 goto free_bio_info; 5013 5014 err = init_percpu_info(sbi); 5015 if (err) 5016 goto free_iostat; 5017 5018 /* init per sbi slab cache */ 5019 err = f2fs_init_xattr_caches(sbi); 5020 if (err) 5021 goto free_percpu; 5022 err = f2fs_init_page_array_cache(sbi); 5023 if (err) 5024 goto free_xattr_cache; 5025 5026 /* get an inode for meta space */ 5027 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi)); 5028 if (IS_ERR(sbi->meta_inode)) { 5029 f2fs_err(sbi, "Failed to read F2FS meta data inode"); 5030 err = PTR_ERR(sbi->meta_inode); 5031 goto free_page_array_cache; 5032 } 5033 5034 err = f2fs_get_valid_checkpoint(sbi); 5035 if (err) { 5036 f2fs_err(sbi, "Failed to get valid F2FS checkpoint"); 5037 goto free_meta_inode; 5038 } 5039 5040 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_QUOTA_NEED_FSCK_FLAG)) 5041 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR); 5042 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_DISABLED_QUICK_FLAG)) { 5043 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK); 5044 sbi->interval_time[DISABLE_TIME] = DEF_DISABLE_QUICK_INTERVAL; 5045 } 5046 5047 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_FSCK_FLAG)) 5048 set_sbi_flag(sbi, SBI_NEED_FSCK); 5049 5050 /* Initialize device list */ 5051 err = f2fs_scan_devices(sbi); 5052 if (err) { 5053 f2fs_err(sbi, "Failed to find devices"); 5054 goto free_devices; 5055 } 5056 5057 err = f2fs_init_post_read_wq(sbi); 5058 if (err) { 5059 f2fs_err(sbi, "Failed to initialize post read workqueue"); 5060 goto free_devices; 5061 } 5062 5063 sbi->total_valid_node_count = 5064 le32_to_cpu(sbi->ckpt->valid_node_count); 5065 percpu_counter_set(&sbi->total_valid_inode_count, 5066 le32_to_cpu(sbi->ckpt->valid_inode_count)); 5067 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count); 5068 sbi->total_valid_block_count = 5069 le64_to_cpu(sbi->ckpt->valid_block_count); 5070 sbi->last_valid_block_count = sbi->total_valid_block_count; 5071 sbi->reserved_blocks = 0; 5072 sbi->current_reserved_blocks = 0; 5073 limit_reserve_root(sbi); 5074 adjust_unusable_cap_perc(sbi); 5075 5076 f2fs_init_extent_cache_info(sbi); 5077 5078 f2fs_init_ino_entry_info(sbi); 5079 5080 f2fs_init_fsync_node_info(sbi); 5081 5082 /* setup checkpoint request control and start checkpoint issue thread */ 5083 f2fs_init_ckpt_req_control(sbi); 5084 if (!f2fs_readonly(sb) && !test_opt(sbi, DISABLE_CHECKPOINT) && 5085 test_opt(sbi, MERGE_CHECKPOINT)) { 5086 err = f2fs_start_ckpt_thread(sbi); 5087 if (err) { 5088 f2fs_err(sbi, 5089 "Failed to start F2FS issue_checkpoint_thread (%d)", 5090 err); 5091 goto stop_ckpt_thread; 5092 } 5093 } 5094 5095 /* setup f2fs internal modules */ 5096 err = f2fs_build_segment_manager(sbi); 5097 if (err) { 5098 f2fs_err(sbi, "Failed to initialize F2FS segment manager (%d)", 5099 err); 5100 goto free_sm; 5101 } 5102 err = f2fs_build_node_manager(sbi); 5103 if (err) { 5104 f2fs_err(sbi, "Failed to initialize F2FS node manager (%d)", 5105 err); 5106 goto free_nm; 5107 } 5108 5109 /* For write statistics */ 5110 sbi->sectors_written_start = f2fs_get_sectors_written(sbi); 5111 5112 /* get segno of first zoned block device */ 5113 sbi->first_seq_zone_segno = get_first_seq_zone_segno(sbi); 5114 5115 sbi->reserved_pin_section = f2fs_sb_has_blkzoned(sbi) ? 5116 ZONED_PIN_SEC_REQUIRED_COUNT : 5117 GET_SEC_FROM_SEG(sbi, overprovision_segments(sbi)); 5118 5119 /* Read accumulated write IO statistics if exists */ 5120 seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE); 5121 if (__exist_node_summaries(sbi)) 5122 sbi->kbytes_written = 5123 le64_to_cpu(seg_i->journal->info.kbytes_written); 5124 5125 f2fs_build_gc_manager(sbi); 5126 5127 err = f2fs_build_stats(sbi); 5128 if (err) 5129 goto free_nm; 5130 5131 /* get an inode for node space */ 5132 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi)); 5133 if (IS_ERR(sbi->node_inode)) { 5134 f2fs_err(sbi, "Failed to read node inode"); 5135 err = PTR_ERR(sbi->node_inode); 5136 goto free_stats; 5137 } 5138 5139 /* read root inode and dentry */ 5140 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi)); 5141 if (IS_ERR(root)) { 5142 f2fs_err(sbi, "Failed to read root inode"); 5143 err = PTR_ERR(root); 5144 goto free_node_inode; 5145 } 5146 if (!S_ISDIR(root->i_mode) || !root->i_blocks || 5147 !root->i_size || !root->i_nlink) { 5148 iput(root); 5149 err = -EINVAL; 5150 goto free_node_inode; 5151 } 5152 5153 generic_set_sb_d_ops(sb); 5154 sb->s_root = d_make_root(root); /* allocate root dentry */ 5155 if (!sb->s_root) { 5156 err = -ENOMEM; 5157 goto free_node_inode; 5158 } 5159 5160 err = f2fs_init_compress_inode(sbi); 5161 if (err) 5162 goto free_root_inode; 5163 5164 err = f2fs_register_sysfs(sbi); 5165 if (err) 5166 goto free_compress_inode; 5167 5168 sbi->umount_lock_holder = current; 5169 #ifdef CONFIG_QUOTA 5170 /* Enable quota usage during mount */ 5171 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sb)) { 5172 err = f2fs_enable_quotas(sb); 5173 if (err) 5174 f2fs_err(sbi, "Cannot turn on quotas: error %d", err); 5175 } 5176 5177 quota_enabled = f2fs_recover_quota_begin(sbi); 5178 #endif 5179 /* if there are any orphan inodes, free them */ 5180 err = f2fs_recover_orphan_inodes(sbi); 5181 if (err) 5182 goto free_meta; 5183 5184 if (unlikely(is_set_ckpt_flags(sbi, CP_DISABLED_FLAG))) { 5185 skip_recovery = true; 5186 goto reset_checkpoint; 5187 } 5188 5189 /* recover fsynced data */ 5190 if (!test_opt(sbi, DISABLE_ROLL_FORWARD) && 5191 !test_opt(sbi, NORECOVERY)) { 5192 /* 5193 * mount should be failed, when device has readonly mode, and 5194 * previous checkpoint was not done by clean system shutdown. 5195 */ 5196 if (f2fs_hw_is_readonly(sbi)) { 5197 if (!is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) { 5198 err = f2fs_recover_fsync_data(sbi, true); 5199 if (err > 0) { 5200 err = -EROFS; 5201 f2fs_err(sbi, "Need to recover fsync data, but " 5202 "write access unavailable, please try " 5203 "mount w/ disable_roll_forward or norecovery"); 5204 } 5205 if (err < 0) 5206 goto free_meta; 5207 } 5208 f2fs_info(sbi, "write access unavailable, skipping recovery"); 5209 goto reset_checkpoint; 5210 } 5211 5212 if (need_fsck) 5213 set_sbi_flag(sbi, SBI_NEED_FSCK); 5214 5215 if (skip_recovery) 5216 goto reset_checkpoint; 5217 5218 err = f2fs_recover_fsync_data(sbi, false); 5219 if (err < 0) { 5220 if (err != -ENOMEM) 5221 skip_recovery = true; 5222 need_fsck = true; 5223 f2fs_err(sbi, "Cannot recover all fsync data errno=%d", 5224 err); 5225 goto free_meta; 5226 } 5227 } else { 5228 err = f2fs_recover_fsync_data(sbi, true); 5229 5230 if (!f2fs_readonly(sb) && err > 0) { 5231 err = -EINVAL; 5232 f2fs_err(sbi, "Need to recover fsync data"); 5233 goto free_meta; 5234 } 5235 } 5236 5237 reset_checkpoint: 5238 #ifdef CONFIG_QUOTA 5239 f2fs_recover_quota_end(sbi, quota_enabled); 5240 #endif 5241 /* 5242 * If the f2fs is not readonly and fsync data recovery succeeds, 5243 * write pointer consistency of cursegs and other zones are already 5244 * checked and fixed during recovery. However, if recovery fails, 5245 * write pointers are left untouched, and retry-mount should check 5246 * them here. 5247 */ 5248 if (skip_recovery) 5249 err = f2fs_check_and_fix_write_pointer(sbi); 5250 if (err) 5251 goto free_meta; 5252 5253 /* f2fs_recover_fsync_data() cleared this already */ 5254 clear_sbi_flag(sbi, SBI_POR_DOING); 5255 5256 err = f2fs_init_inmem_curseg(sbi); 5257 if (err) 5258 goto sync_free_meta; 5259 5260 if (test_opt(sbi, DISABLE_CHECKPOINT)) { 5261 err = f2fs_disable_checkpoint(sbi); 5262 if (err) 5263 goto sync_free_meta; 5264 } else if (is_set_ckpt_flags(sbi, CP_DISABLED_FLAG)) { 5265 f2fs_enable_checkpoint(sbi); 5266 } 5267 5268 /* 5269 * If filesystem is not mounted as read-only then 5270 * do start the gc_thread. 5271 */ 5272 if ((F2FS_OPTION(sbi).bggc_mode != BGGC_MODE_OFF || 5273 test_opt(sbi, GC_MERGE)) && !f2fs_readonly(sb)) { 5274 /* After POR, we can run background GC thread.*/ 5275 err = f2fs_start_gc_thread(sbi); 5276 if (err) 5277 goto sync_free_meta; 5278 } 5279 5280 /* recover broken superblock */ 5281 if (recovery) { 5282 err = f2fs_commit_super(sbi, true); 5283 f2fs_info(sbi, "Try to recover %dth superblock, ret: %d", 5284 sbi->valid_super_block ? 1 : 2, err); 5285 } 5286 5287 f2fs_join_shrinker(sbi); 5288 5289 f2fs_tuning_parameters(sbi); 5290 5291 f2fs_notice(sbi, "Mounted with checkpoint version = %llx", 5292 cur_cp_version(F2FS_CKPT(sbi))); 5293 f2fs_update_time(sbi, CP_TIME); 5294 f2fs_update_time(sbi, REQ_TIME); 5295 clear_sbi_flag(sbi, SBI_CP_DISABLED_QUICK); 5296 5297 sbi->umount_lock_holder = NULL; 5298 return 0; 5299 5300 sync_free_meta: 5301 /* safe to flush all the data */ 5302 sync_filesystem(sbi->sb); 5303 retry_cnt = 0; 5304 5305 free_meta: 5306 #ifdef CONFIG_QUOTA 5307 f2fs_truncate_quota_inode_pages(sb); 5308 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sb)) 5309 f2fs_quota_off_umount(sbi->sb); 5310 #endif 5311 /* 5312 * Some dirty meta pages can be produced by f2fs_recover_orphan_inodes() 5313 * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg() 5314 * followed by f2fs_write_checkpoint() through f2fs_write_node_pages(), which 5315 * falls into an infinite loop in f2fs_sync_meta_pages(). 5316 */ 5317 truncate_inode_pages_final(META_MAPPING(sbi)); 5318 /* evict some inodes being cached by GC */ 5319 evict_inodes(sb); 5320 f2fs_unregister_sysfs(sbi); 5321 free_compress_inode: 5322 f2fs_destroy_compress_inode(sbi); 5323 free_root_inode: 5324 dput(sb->s_root); 5325 sb->s_root = NULL; 5326 free_node_inode: 5327 f2fs_release_ino_entry(sbi, true); 5328 truncate_inode_pages_final(NODE_MAPPING(sbi)); 5329 iput(sbi->node_inode); 5330 sbi->node_inode = NULL; 5331 free_stats: 5332 f2fs_destroy_stats(sbi); 5333 free_nm: 5334 /* stop discard thread before destroying node manager */ 5335 f2fs_stop_discard_thread(sbi); 5336 f2fs_destroy_node_manager(sbi); 5337 free_sm: 5338 f2fs_destroy_segment_manager(sbi); 5339 stop_ckpt_thread: 5340 f2fs_stop_ckpt_thread(sbi); 5341 /* flush s_error_work before sbi destroy */ 5342 flush_work(&sbi->s_error_work); 5343 f2fs_destroy_post_read_wq(sbi); 5344 free_devices: 5345 destroy_device_list(sbi); 5346 kvfree(sbi->ckpt); 5347 free_meta_inode: 5348 make_bad_inode(sbi->meta_inode); 5349 iput(sbi->meta_inode); 5350 sbi->meta_inode = NULL; 5351 free_page_array_cache: 5352 f2fs_destroy_page_array_cache(sbi); 5353 free_xattr_cache: 5354 f2fs_destroy_xattr_caches(sbi); 5355 free_percpu: 5356 destroy_percpu_info(sbi); 5357 free_iostat: 5358 f2fs_destroy_iostat(sbi); 5359 free_bio_info: 5360 for (i = 0; i < NR_PAGE_TYPE; i++) 5361 kfree(sbi->write_io[i]); 5362 5363 #if IS_ENABLED(CONFIG_UNICODE) 5364 utf8_unload(sb->s_encoding); 5365 sb->s_encoding = NULL; 5366 #endif 5367 free_options: 5368 #ifdef CONFIG_QUOTA 5369 for (i = 0; i < MAXQUOTAS; i++) 5370 kfree(F2FS_OPTION(sbi).s_qf_names[i]); 5371 #endif 5372 /* no need to free dummy_enc_policy, we just keep it in ctx when failed */ 5373 swap(F2FS_CTX_INFO(ctx).dummy_enc_policy, F2FS_OPTION(sbi).dummy_enc_policy); 5374 free_sb_buf: 5375 kfree(raw_super); 5376 free_sbi: 5377 kfree(sbi); 5378 sb->s_fs_info = NULL; 5379 5380 /* give only one another chance */ 5381 if (retry_cnt > 0 && skip_recovery) { 5382 retry_cnt--; 5383 shrink_dcache_sb(sb); 5384 goto try_onemore; 5385 } 5386 return err; 5387 } 5388 5389 static int f2fs_get_tree(struct fs_context *fc) 5390 { 5391 return get_tree_bdev(fc, f2fs_fill_super); 5392 } 5393 5394 static int f2fs_reconfigure(struct fs_context *fc) 5395 { 5396 struct super_block *sb = fc->root->d_sb; 5397 5398 return __f2fs_remount(fc, sb); 5399 } 5400 5401 static void f2fs_fc_free(struct fs_context *fc) 5402 { 5403 struct f2fs_fs_context *ctx = fc->fs_private; 5404 5405 if (!ctx) 5406 return; 5407 5408 #ifdef CONFIG_QUOTA 5409 f2fs_unnote_qf_name_all(fc); 5410 #endif 5411 fscrypt_free_dummy_policy(&F2FS_CTX_INFO(ctx).dummy_enc_policy); 5412 kfree(ctx); 5413 } 5414 5415 static const struct fs_context_operations f2fs_context_ops = { 5416 .parse_param = f2fs_parse_param, 5417 .get_tree = f2fs_get_tree, 5418 .reconfigure = f2fs_reconfigure, 5419 .free = f2fs_fc_free, 5420 }; 5421 5422 static void kill_f2fs_super(struct super_block *sb) 5423 { 5424 struct f2fs_sb_info *sbi = F2FS_SB(sb); 5425 5426 if (sb->s_root) { 5427 sbi->umount_lock_holder = current; 5428 5429 set_sbi_flag(sbi, SBI_IS_CLOSE); 5430 f2fs_stop_gc_thread(sbi); 5431 f2fs_stop_discard_thread(sbi); 5432 5433 #ifdef CONFIG_F2FS_FS_COMPRESSION 5434 /* 5435 * latter evict_inode() can bypass checking and invalidating 5436 * compress inode cache. 5437 */ 5438 if (test_opt(sbi, COMPRESS_CACHE)) 5439 truncate_inode_pages_final(COMPRESS_MAPPING(sbi)); 5440 #endif 5441 5442 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) || 5443 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) { 5444 struct cp_control cpc = { 5445 .reason = CP_UMOUNT, 5446 }; 5447 stat_inc_cp_call_count(sbi, TOTAL_CALL); 5448 f2fs_write_checkpoint(sbi, &cpc); 5449 } 5450 5451 if (is_sbi_flag_set(sbi, SBI_IS_RECOVERED) && f2fs_readonly(sb)) 5452 sb->s_flags &= ~SB_RDONLY; 5453 } 5454 kill_block_super(sb); 5455 /* Release block devices last, after fscrypt_destroy_keyring(). */ 5456 if (sbi) { 5457 destroy_device_list(sbi); 5458 kfree(sbi); 5459 sb->s_fs_info = NULL; 5460 } 5461 } 5462 5463 static int f2fs_init_fs_context(struct fs_context *fc) 5464 { 5465 struct f2fs_fs_context *ctx; 5466 5467 ctx = kzalloc(sizeof(struct f2fs_fs_context), GFP_KERNEL); 5468 if (!ctx) 5469 return -ENOMEM; 5470 5471 fc->fs_private = ctx; 5472 fc->ops = &f2fs_context_ops; 5473 5474 return 0; 5475 } 5476 5477 static struct file_system_type f2fs_fs_type = { 5478 .owner = THIS_MODULE, 5479 .name = "f2fs", 5480 .init_fs_context = f2fs_init_fs_context, 5481 .kill_sb = kill_f2fs_super, 5482 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP, 5483 }; 5484 MODULE_ALIAS_FS("f2fs"); 5485 5486 static int __init init_inodecache(void) 5487 { 5488 f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache", 5489 sizeof(struct f2fs_inode_info), 0, 5490 SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL); 5491 return f2fs_inode_cachep ? 0 : -ENOMEM; 5492 } 5493 5494 static void destroy_inodecache(void) 5495 { 5496 /* 5497 * Make sure all delayed rcu free inodes are flushed before we 5498 * destroy cache. 5499 */ 5500 rcu_barrier(); 5501 kmem_cache_destroy(f2fs_inode_cachep); 5502 } 5503 5504 static int __init init_f2fs_fs(void) 5505 { 5506 int err; 5507 5508 err = init_inodecache(); 5509 if (err) 5510 goto fail; 5511 err = f2fs_create_node_manager_caches(); 5512 if (err) 5513 goto free_inodecache; 5514 err = f2fs_create_segment_manager_caches(); 5515 if (err) 5516 goto free_node_manager_caches; 5517 err = f2fs_create_checkpoint_caches(); 5518 if (err) 5519 goto free_segment_manager_caches; 5520 err = f2fs_create_recovery_cache(); 5521 if (err) 5522 goto free_checkpoint_caches; 5523 err = f2fs_create_extent_cache(); 5524 if (err) 5525 goto free_recovery_cache; 5526 err = f2fs_create_garbage_collection_cache(); 5527 if (err) 5528 goto free_extent_cache; 5529 err = f2fs_init_sysfs(); 5530 if (err) 5531 goto free_garbage_collection_cache; 5532 err = f2fs_init_shrinker(); 5533 if (err) 5534 goto free_sysfs; 5535 f2fs_create_root_stats(); 5536 err = f2fs_init_post_read_processing(); 5537 if (err) 5538 goto free_root_stats; 5539 err = f2fs_init_iostat_processing(); 5540 if (err) 5541 goto free_post_read; 5542 err = f2fs_init_bio_entry_cache(); 5543 if (err) 5544 goto free_iostat; 5545 err = f2fs_init_bioset(); 5546 if (err) 5547 goto free_bio_entry_cache; 5548 err = f2fs_init_compress_mempool(); 5549 if (err) 5550 goto free_bioset; 5551 err = f2fs_init_compress_cache(); 5552 if (err) 5553 goto free_compress_mempool; 5554 err = f2fs_create_casefold_cache(); 5555 if (err) 5556 goto free_compress_cache; 5557 err = register_filesystem(&f2fs_fs_type); 5558 if (err) 5559 goto free_casefold_cache; 5560 return 0; 5561 free_casefold_cache: 5562 f2fs_destroy_casefold_cache(); 5563 free_compress_cache: 5564 f2fs_destroy_compress_cache(); 5565 free_compress_mempool: 5566 f2fs_destroy_compress_mempool(); 5567 free_bioset: 5568 f2fs_destroy_bioset(); 5569 free_bio_entry_cache: 5570 f2fs_destroy_bio_entry_cache(); 5571 free_iostat: 5572 f2fs_destroy_iostat_processing(); 5573 free_post_read: 5574 f2fs_destroy_post_read_processing(); 5575 free_root_stats: 5576 f2fs_destroy_root_stats(); 5577 f2fs_exit_shrinker(); 5578 free_sysfs: 5579 f2fs_exit_sysfs(); 5580 free_garbage_collection_cache: 5581 f2fs_destroy_garbage_collection_cache(); 5582 free_extent_cache: 5583 f2fs_destroy_extent_cache(); 5584 free_recovery_cache: 5585 f2fs_destroy_recovery_cache(); 5586 free_checkpoint_caches: 5587 f2fs_destroy_checkpoint_caches(); 5588 free_segment_manager_caches: 5589 f2fs_destroy_segment_manager_caches(); 5590 free_node_manager_caches: 5591 f2fs_destroy_node_manager_caches(); 5592 free_inodecache: 5593 destroy_inodecache(); 5594 fail: 5595 return err; 5596 } 5597 5598 static void __exit exit_f2fs_fs(void) 5599 { 5600 unregister_filesystem(&f2fs_fs_type); 5601 f2fs_destroy_casefold_cache(); 5602 f2fs_destroy_compress_cache(); 5603 f2fs_destroy_compress_mempool(); 5604 f2fs_destroy_bioset(); 5605 f2fs_destroy_bio_entry_cache(); 5606 f2fs_destroy_iostat_processing(); 5607 f2fs_destroy_post_read_processing(); 5608 f2fs_destroy_root_stats(); 5609 f2fs_exit_shrinker(); 5610 f2fs_exit_sysfs(); 5611 f2fs_destroy_garbage_collection_cache(); 5612 f2fs_destroy_extent_cache(); 5613 f2fs_destroy_recovery_cache(); 5614 f2fs_destroy_checkpoint_caches(); 5615 f2fs_destroy_segment_manager_caches(); 5616 f2fs_destroy_node_manager_caches(); 5617 destroy_inodecache(); 5618 } 5619 5620 module_init(init_f2fs_fs) 5621 module_exit(exit_f2fs_fs) 5622 5623 MODULE_AUTHOR("Samsung Electronics's Praesto Team"); 5624 MODULE_DESCRIPTION("Flash Friendly File System"); 5625 MODULE_LICENSE("GPL"); 5626