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