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