4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
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.
11 #include <linux/module.h>
12 #include <linux/init.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/blkdev.h>
25 #include <linux/f2fs_fs.h>
32 #define CREATE_TRACE_POINTS
33 #include <trace/events/f2fs.h>
35 static struct kmem_cache *f2fs_inode_cachep;
38 Opt_gc_background_off,
39 Opt_disable_roll_forward,
45 Opt_disable_ext_identify,
49 static match_table_t f2fs_tokens = {
50 {Opt_gc_background_off, "background_gc_off"},
51 {Opt_disable_roll_forward, "disable_roll_forward"},
52 {Opt_discard, "discard"},
53 {Opt_noheap, "no_heap"},
54 {Opt_nouser_xattr, "nouser_xattr"},
56 {Opt_active_logs, "active_logs=%u"},
57 {Opt_disable_ext_identify, "disable_ext_identify"},
61 void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
69 printk("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
73 static void init_once(void *foo)
75 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
77 inode_init_once(&fi->vfs_inode);
80 static struct inode *f2fs_alloc_inode(struct super_block *sb)
82 struct f2fs_inode_info *fi;
84 fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_NOFS | __GFP_ZERO);
88 init_once((void *) fi);
90 /* Initialize f2fs-specific inode info */
91 fi->vfs_inode.i_version = 1;
92 atomic_set(&fi->dirty_dents, 0);
93 fi->i_current_depth = 1;
95 rwlock_init(&fi->ext.ext_lock);
97 set_inode_flag(fi, FI_NEW_INODE);
99 return &fi->vfs_inode;
102 static void f2fs_i_callback(struct rcu_head *head)
104 struct inode *inode = container_of(head, struct inode, i_rcu);
105 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
108 static void f2fs_destroy_inode(struct inode *inode)
110 call_rcu(&inode->i_rcu, f2fs_i_callback);
113 static void f2fs_put_super(struct super_block *sb)
115 struct f2fs_sb_info *sbi = F2FS_SB(sb);
117 f2fs_destroy_stats(sbi);
120 write_checkpoint(sbi, true);
122 iput(sbi->node_inode);
123 iput(sbi->meta_inode);
125 /* destroy f2fs internal modules */
126 destroy_node_manager(sbi);
127 destroy_segment_manager(sbi);
131 sb->s_fs_info = NULL;
132 brelse(sbi->raw_super_buf);
136 int f2fs_sync_fs(struct super_block *sb, int sync)
138 struct f2fs_sb_info *sbi = F2FS_SB(sb);
140 trace_f2fs_sync_fs(sb, sync);
142 if (!sbi->s_dirty && !get_pages(sbi, F2FS_DIRTY_NODES))
146 mutex_lock(&sbi->gc_mutex);
147 write_checkpoint(sbi, false);
148 mutex_unlock(&sbi->gc_mutex);
150 f2fs_balance_fs(sbi);
156 static int f2fs_freeze(struct super_block *sb)
160 if (sb->s_flags & MS_RDONLY)
163 err = f2fs_sync_fs(sb, 1);
167 static int f2fs_unfreeze(struct super_block *sb)
172 static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
174 struct super_block *sb = dentry->d_sb;
175 struct f2fs_sb_info *sbi = F2FS_SB(sb);
176 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
177 block_t total_count, user_block_count, start_count, ovp_count;
179 total_count = le64_to_cpu(sbi->raw_super->block_count);
180 user_block_count = sbi->user_block_count;
181 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
182 ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg;
183 buf->f_type = F2FS_SUPER_MAGIC;
184 buf->f_bsize = sbi->blocksize;
186 buf->f_blocks = total_count - start_count;
187 buf->f_bfree = buf->f_blocks - valid_user_blocks(sbi) - ovp_count;
188 buf->f_bavail = user_block_count - valid_user_blocks(sbi);
190 buf->f_files = sbi->total_node_count;
191 buf->f_ffree = sbi->total_node_count - valid_inode_count(sbi);
193 buf->f_namelen = F2FS_NAME_LEN;
194 buf->f_fsid.val[0] = (u32)id;
195 buf->f_fsid.val[1] = (u32)(id >> 32);
200 static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
202 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
204 if (test_opt(sbi, BG_GC))
205 seq_puts(seq, ",background_gc_on");
207 seq_puts(seq, ",background_gc_off");
208 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
209 seq_puts(seq, ",disable_roll_forward");
210 if (test_opt(sbi, DISCARD))
211 seq_puts(seq, ",discard");
212 if (test_opt(sbi, NOHEAP))
213 seq_puts(seq, ",no_heap_alloc");
214 #ifdef CONFIG_F2FS_FS_XATTR
215 if (test_opt(sbi, XATTR_USER))
216 seq_puts(seq, ",user_xattr");
218 seq_puts(seq, ",nouser_xattr");
220 #ifdef CONFIG_F2FS_FS_POSIX_ACL
221 if (test_opt(sbi, POSIX_ACL))
222 seq_puts(seq, ",acl");
224 seq_puts(seq, ",noacl");
226 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
227 seq_puts(seq, ",disable_ext_identify");
229 seq_printf(seq, ",active_logs=%u", sbi->active_logs);
234 static struct super_operations f2fs_sops = {
235 .alloc_inode = f2fs_alloc_inode,
236 .destroy_inode = f2fs_destroy_inode,
237 .write_inode = f2fs_write_inode,
238 .show_options = f2fs_show_options,
239 .evict_inode = f2fs_evict_inode,
240 .put_super = f2fs_put_super,
241 .sync_fs = f2fs_sync_fs,
242 .freeze_fs = f2fs_freeze,
243 .unfreeze_fs = f2fs_unfreeze,
244 .statfs = f2fs_statfs,
247 static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
248 u64 ino, u32 generation)
250 struct f2fs_sb_info *sbi = F2FS_SB(sb);
253 if (ino < F2FS_ROOT_INO(sbi))
254 return ERR_PTR(-ESTALE);
257 * f2fs_iget isn't quite right if the inode is currently unallocated!
258 * However f2fs_iget currently does appropriate checks to handle stale
259 * inodes so everything is OK.
261 inode = f2fs_iget(sb, ino);
263 return ERR_CAST(inode);
264 if (generation && inode->i_generation != generation) {
265 /* we didn't find the right inode.. */
267 return ERR_PTR(-ESTALE);
272 static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
273 int fh_len, int fh_type)
275 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
279 static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
280 int fh_len, int fh_type)
282 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
286 static const struct export_operations f2fs_export_ops = {
287 .fh_to_dentry = f2fs_fh_to_dentry,
288 .fh_to_parent = f2fs_fh_to_parent,
289 .get_parent = f2fs_get_parent,
292 static int parse_options(struct super_block *sb, struct f2fs_sb_info *sbi,
295 substring_t args[MAX_OPT_ARGS];
302 while ((p = strsep(&options, ",")) != NULL) {
307 * Initialize args struct so we know whether arg was
308 * found; some options take optional arguments.
310 args[0].to = args[0].from = NULL;
311 token = match_token(p, f2fs_tokens, args);
314 case Opt_gc_background_off:
315 clear_opt(sbi, BG_GC);
317 case Opt_disable_roll_forward:
318 set_opt(sbi, DISABLE_ROLL_FORWARD);
321 set_opt(sbi, DISCARD);
324 set_opt(sbi, NOHEAP);
326 #ifdef CONFIG_F2FS_FS_XATTR
327 case Opt_nouser_xattr:
328 clear_opt(sbi, XATTR_USER);
331 case Opt_nouser_xattr:
332 f2fs_msg(sb, KERN_INFO,
333 "nouser_xattr options not supported");
336 #ifdef CONFIG_F2FS_FS_POSIX_ACL
338 clear_opt(sbi, POSIX_ACL);
342 f2fs_msg(sb, KERN_INFO, "noacl options not supported");
345 case Opt_active_logs:
346 if (args->from && match_int(args, &arg))
348 if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
350 sbi->active_logs = arg;
352 case Opt_disable_ext_identify:
353 set_opt(sbi, DISABLE_EXT_IDENTIFY);
356 f2fs_msg(sb, KERN_ERR,
357 "Unrecognized mount option \"%s\" or missing value",
365 static loff_t max_file_size(unsigned bits)
367 loff_t result = ADDRS_PER_INODE;
368 loff_t leaf_count = ADDRS_PER_BLOCK;
370 /* two direct node blocks */
371 result += (leaf_count * 2);
373 /* two indirect node blocks */
374 leaf_count *= NIDS_PER_BLOCK;
375 result += (leaf_count * 2);
377 /* one double indirect node block */
378 leaf_count *= NIDS_PER_BLOCK;
379 result += leaf_count;
385 static int sanity_check_raw_super(struct super_block *sb,
386 struct f2fs_super_block *raw_super)
388 unsigned int blocksize;
390 if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
391 f2fs_msg(sb, KERN_INFO,
392 "Magic Mismatch, valid(0x%x) - read(0x%x)",
393 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
397 /* Currently, support only 4KB page cache size */
398 if (F2FS_BLKSIZE != PAGE_CACHE_SIZE) {
399 f2fs_msg(sb, KERN_INFO,
400 "Invalid page_cache_size (%lu), supports only 4KB\n",
405 /* Currently, support only 4KB block size */
406 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
407 if (blocksize != F2FS_BLKSIZE) {
408 f2fs_msg(sb, KERN_INFO,
409 "Invalid blocksize (%u), supports only 4KB\n",
414 if (le32_to_cpu(raw_super->log_sectorsize) !=
415 F2FS_LOG_SECTOR_SIZE) {
416 f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize");
419 if (le32_to_cpu(raw_super->log_sectors_per_block) !=
420 F2FS_LOG_SECTORS_PER_BLOCK) {
421 f2fs_msg(sb, KERN_INFO, "Invalid log sectors per block");
427 static int sanity_check_ckpt(struct f2fs_sb_info *sbi)
429 unsigned int total, fsmeta;
430 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
431 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
433 total = le32_to_cpu(raw_super->segment_count);
434 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
435 fsmeta += le32_to_cpu(raw_super->segment_count_sit);
436 fsmeta += le32_to_cpu(raw_super->segment_count_nat);
437 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
438 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
443 if (is_set_ckpt_flags(ckpt, CP_ERROR_FLAG)) {
444 f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
450 static void init_sb_info(struct f2fs_sb_info *sbi)
452 struct f2fs_super_block *raw_super = sbi->raw_super;
455 sbi->log_sectors_per_block =
456 le32_to_cpu(raw_super->log_sectors_per_block);
457 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
458 sbi->blocksize = 1 << sbi->log_blocksize;
459 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
460 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
461 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
462 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
463 sbi->total_sections = le32_to_cpu(raw_super->section_count);
464 sbi->total_node_count =
465 (le32_to_cpu(raw_super->segment_count_nat) / 2)
466 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
467 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
468 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
469 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
470 sbi->cur_victim_sec = NULL_SECNO;
472 for (i = 0; i < NR_COUNT_TYPE; i++)
473 atomic_set(&sbi->nr_pages[i], 0);
476 static int validate_superblock(struct super_block *sb,
477 struct f2fs_super_block **raw_super,
478 struct buffer_head **raw_super_buf, sector_t block)
480 const char *super = (block == 0 ? "first" : "second");
482 /* read f2fs raw super block */
483 *raw_super_buf = sb_bread(sb, block);
484 if (!*raw_super_buf) {
485 f2fs_msg(sb, KERN_ERR, "unable to read %s superblock",
490 *raw_super = (struct f2fs_super_block *)
491 ((char *)(*raw_super_buf)->b_data + F2FS_SUPER_OFFSET);
493 /* sanity checking of raw super */
494 if (!sanity_check_raw_super(sb, *raw_super))
497 f2fs_msg(sb, KERN_ERR, "Can't find a valid F2FS filesystem "
498 "in %s superblock", super);
502 static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
504 struct f2fs_sb_info *sbi;
505 struct f2fs_super_block *raw_super;
506 struct buffer_head *raw_super_buf;
511 /* allocate memory for f2fs-specific super block info */
512 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
516 /* set a block size */
517 if (!sb_set_blocksize(sb, F2FS_BLKSIZE)) {
518 f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
522 err = validate_superblock(sb, &raw_super, &raw_super_buf, 0);
524 brelse(raw_super_buf);
525 /* check secondary superblock when primary failed */
526 err = validate_superblock(sb, &raw_super, &raw_super_buf, 1);
530 /* init some FS parameters */
531 sbi->active_logs = NR_CURSEG_TYPE;
535 #ifdef CONFIG_F2FS_FS_XATTR
536 set_opt(sbi, XATTR_USER);
538 #ifdef CONFIG_F2FS_FS_POSIX_ACL
539 set_opt(sbi, POSIX_ACL);
541 /* parse mount options */
542 err = parse_options(sb, sbi, (char *)data);
546 sb->s_maxbytes = max_file_size(le32_to_cpu(raw_super->log_blocksize));
547 sb->s_max_links = F2FS_LINK_MAX;
548 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
550 sb->s_op = &f2fs_sops;
551 sb->s_xattr = f2fs_xattr_handlers;
552 sb->s_export_op = &f2fs_export_ops;
553 sb->s_magic = F2FS_SUPER_MAGIC;
556 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
557 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
558 memcpy(sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
560 /* init f2fs-specific super block info */
562 sbi->raw_super = raw_super;
563 sbi->raw_super_buf = raw_super_buf;
564 mutex_init(&sbi->gc_mutex);
565 mutex_init(&sbi->writepages);
566 mutex_init(&sbi->cp_mutex);
567 for (i = 0; i < NR_GLOBAL_LOCKS; i++)
568 mutex_init(&sbi->fs_lock[i]);
569 mutex_init(&sbi->node_write);
571 spin_lock_init(&sbi->stat_lock);
572 init_rwsem(&sbi->bio_sem);
575 /* get an inode for meta space */
576 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
577 if (IS_ERR(sbi->meta_inode)) {
578 f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
579 err = PTR_ERR(sbi->meta_inode);
583 err = get_valid_checkpoint(sbi);
585 f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
586 goto free_meta_inode;
589 /* sanity checking of checkpoint */
591 if (sanity_check_ckpt(sbi)) {
592 f2fs_msg(sb, KERN_ERR, "Invalid F2FS checkpoint");
596 sbi->total_valid_node_count =
597 le32_to_cpu(sbi->ckpt->valid_node_count);
598 sbi->total_valid_inode_count =
599 le32_to_cpu(sbi->ckpt->valid_inode_count);
600 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
601 sbi->total_valid_block_count =
602 le64_to_cpu(sbi->ckpt->valid_block_count);
603 sbi->last_valid_block_count = sbi->total_valid_block_count;
604 sbi->alloc_valid_block_count = 0;
605 INIT_LIST_HEAD(&sbi->dir_inode_list);
606 spin_lock_init(&sbi->dir_inode_lock);
608 init_orphan_info(sbi);
610 /* setup f2fs internal modules */
611 err = build_segment_manager(sbi);
613 f2fs_msg(sb, KERN_ERR,
614 "Failed to initialize F2FS segment manager");
617 err = build_node_manager(sbi);
619 f2fs_msg(sb, KERN_ERR,
620 "Failed to initialize F2FS node manager");
624 build_gc_manager(sbi);
626 /* get an inode for node space */
627 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
628 if (IS_ERR(sbi->node_inode)) {
629 f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
630 err = PTR_ERR(sbi->node_inode);
634 /* if there are nt orphan nodes free them */
636 if (recover_orphan_inodes(sbi))
637 goto free_node_inode;
639 /* read root inode and dentry */
640 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
642 f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
644 goto free_node_inode;
646 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size)
647 goto free_root_inode;
649 sb->s_root = d_make_root(root); /* allocate root dentry */
652 goto free_root_inode;
655 /* recover fsynced data */
656 if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
657 err = recover_fsync_data(sbi);
659 f2fs_msg(sb, KERN_ERR, "Failed to recover fsync data");
660 goto free_root_inode;
664 /* After POR, we can run background GC thread */
665 err = start_gc_thread(sbi);
669 err = f2fs_build_stats(sbi);
673 if (test_opt(sbi, DISCARD)) {
674 struct request_queue *q = bdev_get_queue(sb->s_bdev);
675 if (!blk_queue_discard(q))
676 f2fs_msg(sb, KERN_WARNING,
677 "mounting with \"discard\" option, but "
678 "the device does not support discard");
688 iput(sbi->node_inode);
690 destroy_node_manager(sbi);
692 destroy_segment_manager(sbi);
696 make_bad_inode(sbi->meta_inode);
697 iput(sbi->meta_inode);
699 brelse(raw_super_buf);
705 static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
706 const char *dev_name, void *data)
708 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
711 static struct file_system_type f2fs_fs_type = {
712 .owner = THIS_MODULE,
715 .kill_sb = kill_block_super,
716 .fs_flags = FS_REQUIRES_DEV,
719 static int __init init_inodecache(void)
721 f2fs_inode_cachep = f2fs_kmem_cache_create("f2fs_inode_cache",
722 sizeof(struct f2fs_inode_info), NULL);
723 if (f2fs_inode_cachep == NULL)
728 static void destroy_inodecache(void)
731 * Make sure all delayed rcu free inodes are flushed before we
735 kmem_cache_destroy(f2fs_inode_cachep);
738 static int __init init_f2fs_fs(void)
742 err = init_inodecache();
745 err = create_node_manager_caches();
748 err = create_gc_caches();
751 err = create_checkpoint_caches();
754 err = register_filesystem(&f2fs_fs_type);
757 f2fs_create_root_stats();
762 static void __exit exit_f2fs_fs(void)
764 f2fs_destroy_root_stats();
765 unregister_filesystem(&f2fs_fs_type);
766 destroy_checkpoint_caches();
768 destroy_node_manager_caches();
769 destroy_inodecache();
772 module_init(init_f2fs_fs)
773 module_exit(exit_f2fs_fs)
775 MODULE_AUTHOR("Samsung Electronics's Praesto Team");
776 MODULE_DESCRIPTION("Flash Friendly File System");
777 MODULE_LICENSE("GPL");