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