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