1 /* 2 * fs/f2fs/super.c 3 * 4 * Copyright (c) 2012 Samsung Electronics Co., Ltd. 5 * http://www.samsung.com/ 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License version 2 as 9 * published by the Free Software Foundation. 10 */ 11 #include <linux/module.h> 12 #include <linux/init.h> 13 #include <linux/fs.h> 14 #include <linux/statfs.h> 15 #include <linux/proc_fs.h> 16 #include <linux/buffer_head.h> 17 #include <linux/backing-dev.h> 18 #include <linux/kthread.h> 19 #include <linux/parser.h> 20 #include <linux/mount.h> 21 #include <linux/seq_file.h> 22 #include <linux/random.h> 23 #include <linux/exportfs.h> 24 #include <linux/f2fs_fs.h> 25 26 #include "f2fs.h" 27 #include "node.h" 28 #include "xattr.h" 29 30 static struct kmem_cache *f2fs_inode_cachep; 31 32 enum { 33 Opt_gc_background_off, 34 Opt_disable_roll_forward, 35 Opt_discard, 36 Opt_noheap, 37 Opt_nouser_xattr, 38 Opt_noacl, 39 Opt_active_logs, 40 Opt_disable_ext_identify, 41 Opt_err, 42 }; 43 44 static match_table_t f2fs_tokens = { 45 {Opt_gc_background_off, "background_gc_off"}, 46 {Opt_disable_roll_forward, "disable_roll_forward"}, 47 {Opt_discard, "discard"}, 48 {Opt_noheap, "no_heap"}, 49 {Opt_nouser_xattr, "nouser_xattr"}, 50 {Opt_noacl, "noacl"}, 51 {Opt_active_logs, "active_logs=%u"}, 52 {Opt_disable_ext_identify, "disable_ext_identify"}, 53 {Opt_err, NULL}, 54 }; 55 56 static void init_once(void *foo) 57 { 58 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo; 59 60 inode_init_once(&fi->vfs_inode); 61 } 62 63 static struct inode *f2fs_alloc_inode(struct super_block *sb) 64 { 65 struct f2fs_inode_info *fi; 66 67 fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_NOFS | __GFP_ZERO); 68 if (!fi) 69 return NULL; 70 71 init_once((void *) fi); 72 73 /* Initilize f2fs-specific inode info */ 74 fi->vfs_inode.i_version = 1; 75 atomic_set(&fi->dirty_dents, 0); 76 fi->i_current_depth = 1; 77 fi->i_advise = 0; 78 rwlock_init(&fi->ext.ext_lock); 79 80 set_inode_flag(fi, FI_NEW_INODE); 81 82 return &fi->vfs_inode; 83 } 84 85 static void f2fs_i_callback(struct rcu_head *head) 86 { 87 struct inode *inode = container_of(head, struct inode, i_rcu); 88 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode)); 89 } 90 91 static void f2fs_destroy_inode(struct inode *inode) 92 { 93 call_rcu(&inode->i_rcu, f2fs_i_callback); 94 } 95 96 static void f2fs_put_super(struct super_block *sb) 97 { 98 struct f2fs_sb_info *sbi = F2FS_SB(sb); 99 100 f2fs_destroy_stats(sbi); 101 stop_gc_thread(sbi); 102 103 write_checkpoint(sbi, false, true); 104 105 iput(sbi->node_inode); 106 iput(sbi->meta_inode); 107 108 /* destroy f2fs internal modules */ 109 destroy_node_manager(sbi); 110 destroy_segment_manager(sbi); 111 112 kfree(sbi->ckpt); 113 114 sb->s_fs_info = NULL; 115 brelse(sbi->raw_super_buf); 116 kfree(sbi); 117 } 118 119 int f2fs_sync_fs(struct super_block *sb, int sync) 120 { 121 struct f2fs_sb_info *sbi = F2FS_SB(sb); 122 123 if (!sbi->s_dirty && !get_pages(sbi, F2FS_DIRTY_NODES)) 124 return 0; 125 126 if (sync) 127 write_checkpoint(sbi, false, false); 128 129 return 0; 130 } 131 132 static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf) 133 { 134 struct super_block *sb = dentry->d_sb; 135 struct f2fs_sb_info *sbi = F2FS_SB(sb); 136 u64 id = huge_encode_dev(sb->s_bdev->bd_dev); 137 block_t total_count, user_block_count, start_count, ovp_count; 138 139 total_count = le64_to_cpu(sbi->raw_super->block_count); 140 user_block_count = sbi->user_block_count; 141 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr); 142 ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg; 143 buf->f_type = F2FS_SUPER_MAGIC; 144 buf->f_bsize = sbi->blocksize; 145 146 buf->f_blocks = total_count - start_count; 147 buf->f_bfree = buf->f_blocks - valid_user_blocks(sbi) - ovp_count; 148 buf->f_bavail = user_block_count - valid_user_blocks(sbi); 149 150 buf->f_files = sbi->total_node_count; 151 buf->f_ffree = sbi->total_node_count - valid_inode_count(sbi); 152 153 buf->f_namelen = F2FS_MAX_NAME_LEN; 154 buf->f_fsid.val[0] = (u32)id; 155 buf->f_fsid.val[1] = (u32)(id >> 32); 156 157 return 0; 158 } 159 160 static int f2fs_show_options(struct seq_file *seq, struct dentry *root) 161 { 162 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb); 163 164 if (test_opt(sbi, BG_GC)) 165 seq_puts(seq, ",background_gc_on"); 166 else 167 seq_puts(seq, ",background_gc_off"); 168 if (test_opt(sbi, DISABLE_ROLL_FORWARD)) 169 seq_puts(seq, ",disable_roll_forward"); 170 if (test_opt(sbi, DISCARD)) 171 seq_puts(seq, ",discard"); 172 if (test_opt(sbi, NOHEAP)) 173 seq_puts(seq, ",no_heap_alloc"); 174 #ifdef CONFIG_F2FS_FS_XATTR 175 if (test_opt(sbi, XATTR_USER)) 176 seq_puts(seq, ",user_xattr"); 177 else 178 seq_puts(seq, ",nouser_xattr"); 179 #endif 180 #ifdef CONFIG_F2FS_FS_POSIX_ACL 181 if (test_opt(sbi, POSIX_ACL)) 182 seq_puts(seq, ",acl"); 183 else 184 seq_puts(seq, ",noacl"); 185 #endif 186 if (test_opt(sbi, DISABLE_EXT_IDENTIFY)) 187 seq_puts(seq, ",disable_ext_indentify"); 188 189 seq_printf(seq, ",active_logs=%u", sbi->active_logs); 190 191 return 0; 192 } 193 194 static struct super_operations f2fs_sops = { 195 .alloc_inode = f2fs_alloc_inode, 196 .destroy_inode = f2fs_destroy_inode, 197 .write_inode = f2fs_write_inode, 198 .show_options = f2fs_show_options, 199 .evict_inode = f2fs_evict_inode, 200 .put_super = f2fs_put_super, 201 .sync_fs = f2fs_sync_fs, 202 .statfs = f2fs_statfs, 203 }; 204 205 static struct inode *f2fs_nfs_get_inode(struct super_block *sb, 206 u64 ino, u32 generation) 207 { 208 struct f2fs_sb_info *sbi = F2FS_SB(sb); 209 struct inode *inode; 210 211 if (ino < F2FS_ROOT_INO(sbi)) 212 return ERR_PTR(-ESTALE); 213 214 /* 215 * f2fs_iget isn't quite right if the inode is currently unallocated! 216 * However f2fs_iget currently does appropriate checks to handle stale 217 * inodes so everything is OK. 218 */ 219 inode = f2fs_iget(sb, ino); 220 if (IS_ERR(inode)) 221 return ERR_CAST(inode); 222 if (generation && inode->i_generation != generation) { 223 /* we didn't find the right inode.. */ 224 iput(inode); 225 return ERR_PTR(-ESTALE); 226 } 227 return inode; 228 } 229 230 static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid, 231 int fh_len, int fh_type) 232 { 233 return generic_fh_to_dentry(sb, fid, fh_len, fh_type, 234 f2fs_nfs_get_inode); 235 } 236 237 static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid, 238 int fh_len, int fh_type) 239 { 240 return generic_fh_to_parent(sb, fid, fh_len, fh_type, 241 f2fs_nfs_get_inode); 242 } 243 244 static const struct export_operations f2fs_export_ops = { 245 .fh_to_dentry = f2fs_fh_to_dentry, 246 .fh_to_parent = f2fs_fh_to_parent, 247 .get_parent = f2fs_get_parent, 248 }; 249 250 static int parse_options(struct f2fs_sb_info *sbi, char *options) 251 { 252 substring_t args[MAX_OPT_ARGS]; 253 char *p; 254 int arg = 0; 255 256 if (!options) 257 return 0; 258 259 while ((p = strsep(&options, ",")) != NULL) { 260 int token; 261 if (!*p) 262 continue; 263 /* 264 * Initialize args struct so we know whether arg was 265 * found; some options take optional arguments. 266 */ 267 args[0].to = args[0].from = NULL; 268 token = match_token(p, f2fs_tokens, args); 269 270 switch (token) { 271 case Opt_gc_background_off: 272 clear_opt(sbi, BG_GC); 273 break; 274 case Opt_disable_roll_forward: 275 set_opt(sbi, DISABLE_ROLL_FORWARD); 276 break; 277 case Opt_discard: 278 set_opt(sbi, DISCARD); 279 break; 280 case Opt_noheap: 281 set_opt(sbi, NOHEAP); 282 break; 283 #ifdef CONFIG_F2FS_FS_XATTR 284 case Opt_nouser_xattr: 285 clear_opt(sbi, XATTR_USER); 286 break; 287 #else 288 case Opt_nouser_xattr: 289 pr_info("nouser_xattr options not supported\n"); 290 break; 291 #endif 292 #ifdef CONFIG_F2FS_FS_POSIX_ACL 293 case Opt_noacl: 294 clear_opt(sbi, POSIX_ACL); 295 break; 296 #else 297 case Opt_noacl: 298 pr_info("noacl options not supported\n"); 299 break; 300 #endif 301 case Opt_active_logs: 302 if (args->from && match_int(args, &arg)) 303 return -EINVAL; 304 if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE) 305 return -EINVAL; 306 sbi->active_logs = arg; 307 break; 308 case Opt_disable_ext_identify: 309 set_opt(sbi, DISABLE_EXT_IDENTIFY); 310 break; 311 default: 312 pr_err("Unrecognized mount option \"%s\" or missing value\n", 313 p); 314 return -EINVAL; 315 } 316 } 317 return 0; 318 } 319 320 static loff_t max_file_size(unsigned bits) 321 { 322 loff_t result = ADDRS_PER_INODE; 323 loff_t leaf_count = ADDRS_PER_BLOCK; 324 325 /* two direct node blocks */ 326 result += (leaf_count * 2); 327 328 /* two indirect node blocks */ 329 leaf_count *= NIDS_PER_BLOCK; 330 result += (leaf_count * 2); 331 332 /* one double indirect node block */ 333 leaf_count *= NIDS_PER_BLOCK; 334 result += leaf_count; 335 336 result <<= bits; 337 return result; 338 } 339 340 static int sanity_check_raw_super(struct f2fs_super_block *raw_super) 341 { 342 unsigned int blocksize; 343 344 if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) 345 return 1; 346 347 /* Currently, support only 4KB block size */ 348 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize); 349 if (blocksize != PAGE_CACHE_SIZE) 350 return 1; 351 if (le32_to_cpu(raw_super->log_sectorsize) != 352 F2FS_LOG_SECTOR_SIZE) 353 return 1; 354 if (le32_to_cpu(raw_super->log_sectors_per_block) != 355 F2FS_LOG_SECTORS_PER_BLOCK) 356 return 1; 357 return 0; 358 } 359 360 static int sanity_check_ckpt(struct f2fs_super_block *raw_super, 361 struct f2fs_checkpoint *ckpt) 362 { 363 unsigned int total, fsmeta; 364 365 total = le32_to_cpu(raw_super->segment_count); 366 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt); 367 fsmeta += le32_to_cpu(raw_super->segment_count_sit); 368 fsmeta += le32_to_cpu(raw_super->segment_count_nat); 369 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count); 370 fsmeta += le32_to_cpu(raw_super->segment_count_ssa); 371 372 if (fsmeta >= total) 373 return 1; 374 return 0; 375 } 376 377 static void init_sb_info(struct f2fs_sb_info *sbi) 378 { 379 struct f2fs_super_block *raw_super = sbi->raw_super; 380 int i; 381 382 sbi->log_sectors_per_block = 383 le32_to_cpu(raw_super->log_sectors_per_block); 384 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize); 385 sbi->blocksize = 1 << sbi->log_blocksize; 386 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg); 387 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg; 388 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec); 389 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone); 390 sbi->total_sections = le32_to_cpu(raw_super->section_count); 391 sbi->total_node_count = 392 (le32_to_cpu(raw_super->segment_count_nat) / 2) 393 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK; 394 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino); 395 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino); 396 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino); 397 398 for (i = 0; i < NR_COUNT_TYPE; i++) 399 atomic_set(&sbi->nr_pages[i], 0); 400 } 401 402 static int f2fs_fill_super(struct super_block *sb, void *data, int silent) 403 { 404 struct f2fs_sb_info *sbi; 405 struct f2fs_super_block *raw_super; 406 struct buffer_head *raw_super_buf; 407 struct inode *root; 408 long err = -EINVAL; 409 int i; 410 411 /* allocate memory for f2fs-specific super block info */ 412 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL); 413 if (!sbi) 414 return -ENOMEM; 415 416 /* set a temporary block size */ 417 if (!sb_set_blocksize(sb, F2FS_BLKSIZE)) 418 goto free_sbi; 419 420 /* read f2fs raw super block */ 421 raw_super_buf = sb_bread(sb, 0); 422 if (!raw_super_buf) { 423 err = -EIO; 424 goto free_sbi; 425 } 426 raw_super = (struct f2fs_super_block *) 427 ((char *)raw_super_buf->b_data + F2FS_SUPER_OFFSET); 428 429 /* init some FS parameters */ 430 sbi->active_logs = NR_CURSEG_TYPE; 431 432 set_opt(sbi, BG_GC); 433 434 #ifdef CONFIG_F2FS_FS_XATTR 435 set_opt(sbi, XATTR_USER); 436 #endif 437 #ifdef CONFIG_F2FS_FS_POSIX_ACL 438 set_opt(sbi, POSIX_ACL); 439 #endif 440 /* parse mount options */ 441 if (parse_options(sbi, (char *)data)) 442 goto free_sb_buf; 443 444 /* sanity checking of raw super */ 445 if (sanity_check_raw_super(raw_super)) 446 goto free_sb_buf; 447 448 sb->s_maxbytes = max_file_size(le32_to_cpu(raw_super->log_blocksize)); 449 sb->s_max_links = F2FS_LINK_MAX; 450 get_random_bytes(&sbi->s_next_generation, sizeof(u32)); 451 452 sb->s_op = &f2fs_sops; 453 sb->s_xattr = f2fs_xattr_handlers; 454 sb->s_export_op = &f2fs_export_ops; 455 sb->s_magic = F2FS_SUPER_MAGIC; 456 sb->s_fs_info = sbi; 457 sb->s_time_gran = 1; 458 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) | 459 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0); 460 memcpy(sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid)); 461 462 /* init f2fs-specific super block info */ 463 sbi->sb = sb; 464 sbi->raw_super = raw_super; 465 sbi->raw_super_buf = raw_super_buf; 466 mutex_init(&sbi->gc_mutex); 467 mutex_init(&sbi->write_inode); 468 mutex_init(&sbi->writepages); 469 mutex_init(&sbi->cp_mutex); 470 for (i = 0; i < NR_LOCK_TYPE; i++) 471 mutex_init(&sbi->fs_lock[i]); 472 sbi->por_doing = 0; 473 spin_lock_init(&sbi->stat_lock); 474 init_rwsem(&sbi->bio_sem); 475 init_sb_info(sbi); 476 477 /* get an inode for meta space */ 478 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi)); 479 if (IS_ERR(sbi->meta_inode)) { 480 err = PTR_ERR(sbi->meta_inode); 481 goto free_sb_buf; 482 } 483 484 err = get_valid_checkpoint(sbi); 485 if (err) 486 goto free_meta_inode; 487 488 /* sanity checking of checkpoint */ 489 err = -EINVAL; 490 if (sanity_check_ckpt(raw_super, sbi->ckpt)) 491 goto free_cp; 492 493 sbi->total_valid_node_count = 494 le32_to_cpu(sbi->ckpt->valid_node_count); 495 sbi->total_valid_inode_count = 496 le32_to_cpu(sbi->ckpt->valid_inode_count); 497 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count); 498 sbi->total_valid_block_count = 499 le64_to_cpu(sbi->ckpt->valid_block_count); 500 sbi->last_valid_block_count = sbi->total_valid_block_count; 501 sbi->alloc_valid_block_count = 0; 502 INIT_LIST_HEAD(&sbi->dir_inode_list); 503 spin_lock_init(&sbi->dir_inode_lock); 504 505 /* init super block */ 506 if (!sb_set_blocksize(sb, sbi->blocksize)) 507 goto free_cp; 508 509 init_orphan_info(sbi); 510 511 /* setup f2fs internal modules */ 512 err = build_segment_manager(sbi); 513 if (err) 514 goto free_sm; 515 err = build_node_manager(sbi); 516 if (err) 517 goto free_nm; 518 519 build_gc_manager(sbi); 520 521 /* get an inode for node space */ 522 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi)); 523 if (IS_ERR(sbi->node_inode)) { 524 err = PTR_ERR(sbi->node_inode); 525 goto free_nm; 526 } 527 528 /* if there are nt orphan nodes free them */ 529 err = -EINVAL; 530 if (recover_orphan_inodes(sbi)) 531 goto free_node_inode; 532 533 /* read root inode and dentry */ 534 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi)); 535 if (IS_ERR(root)) { 536 err = PTR_ERR(root); 537 goto free_node_inode; 538 } 539 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) 540 goto free_root_inode; 541 542 sb->s_root = d_make_root(root); /* allocate root dentry */ 543 if (!sb->s_root) { 544 err = -ENOMEM; 545 goto free_root_inode; 546 } 547 548 /* recover fsynced data */ 549 if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) 550 recover_fsync_data(sbi); 551 552 /* After POR, we can run background GC thread */ 553 err = start_gc_thread(sbi); 554 if (err) 555 goto fail; 556 557 err = f2fs_build_stats(sbi); 558 if (err) 559 goto fail; 560 561 return 0; 562 fail: 563 stop_gc_thread(sbi); 564 free_root_inode: 565 dput(sb->s_root); 566 sb->s_root = NULL; 567 free_node_inode: 568 iput(sbi->node_inode); 569 free_nm: 570 destroy_node_manager(sbi); 571 free_sm: 572 destroy_segment_manager(sbi); 573 free_cp: 574 kfree(sbi->ckpt); 575 free_meta_inode: 576 make_bad_inode(sbi->meta_inode); 577 iput(sbi->meta_inode); 578 free_sb_buf: 579 brelse(raw_super_buf); 580 free_sbi: 581 kfree(sbi); 582 return err; 583 } 584 585 static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags, 586 const char *dev_name, void *data) 587 { 588 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super); 589 } 590 591 static struct file_system_type f2fs_fs_type = { 592 .owner = THIS_MODULE, 593 .name = "f2fs", 594 .mount = f2fs_mount, 595 .kill_sb = kill_block_super, 596 .fs_flags = FS_REQUIRES_DEV, 597 }; 598 599 static int init_inodecache(void) 600 { 601 f2fs_inode_cachep = f2fs_kmem_cache_create("f2fs_inode_cache", 602 sizeof(struct f2fs_inode_info), NULL); 603 if (f2fs_inode_cachep == NULL) 604 return -ENOMEM; 605 return 0; 606 } 607 608 static void destroy_inodecache(void) 609 { 610 /* 611 * Make sure all delayed rcu free inodes are flushed before we 612 * destroy cache. 613 */ 614 rcu_barrier(); 615 kmem_cache_destroy(f2fs_inode_cachep); 616 } 617 618 static int __init init_f2fs_fs(void) 619 { 620 int err; 621 622 err = init_inodecache(); 623 if (err) 624 goto fail; 625 err = create_node_manager_caches(); 626 if (err) 627 goto fail; 628 err = create_gc_caches(); 629 if (err) 630 goto fail; 631 err = create_checkpoint_caches(); 632 if (err) 633 goto fail; 634 return register_filesystem(&f2fs_fs_type); 635 fail: 636 return err; 637 } 638 639 static void __exit exit_f2fs_fs(void) 640 { 641 destroy_root_stats(); 642 unregister_filesystem(&f2fs_fs_type); 643 destroy_checkpoint_caches(); 644 destroy_gc_caches(); 645 destroy_node_manager_caches(); 646 destroy_inodecache(); 647 } 648 649 module_init(init_f2fs_fs) 650 module_exit(exit_f2fs_fs) 651 652 MODULE_AUTHOR("Samsung Electronics's Praesto Team"); 653 MODULE_DESCRIPTION("Flash Friendly File System"); 654 MODULE_LICENSE("GPL"); 655