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