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