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/f2fs_fs.h>
30 static struct kmem_cache *f2fs_inode_cachep;
33 Opt_gc_background_off,
34 Opt_disable_roll_forward,
40 Opt_disable_ext_identify,
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"},
51 {Opt_active_logs, "active_logs=%u"},
52 {Opt_disable_ext_identify, "disable_ext_identify"},
56 void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
64 printk("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
68 static void init_once(void *foo)
70 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
72 inode_init_once(&fi->vfs_inode);
75 static struct inode *f2fs_alloc_inode(struct super_block *sb)
77 struct f2fs_inode_info *fi;
79 fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_NOFS | __GFP_ZERO);
83 init_once((void *) fi);
85 /* Initilize f2fs-specific inode info */
86 fi->vfs_inode.i_version = 1;
87 atomic_set(&fi->dirty_dents, 0);
88 fi->i_current_depth = 1;
90 rwlock_init(&fi->ext.ext_lock);
92 set_inode_flag(fi, FI_NEW_INODE);
94 return &fi->vfs_inode;
97 static void f2fs_i_callback(struct rcu_head *head)
99 struct inode *inode = container_of(head, struct inode, i_rcu);
100 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
103 static void f2fs_destroy_inode(struct inode *inode)
105 call_rcu(&inode->i_rcu, f2fs_i_callback);
108 static void f2fs_put_super(struct super_block *sb)
110 struct f2fs_sb_info *sbi = F2FS_SB(sb);
112 f2fs_destroy_stats(sbi);
115 write_checkpoint(sbi, false, true);
117 iput(sbi->node_inode);
118 iput(sbi->meta_inode);
120 /* destroy f2fs internal modules */
121 destroy_node_manager(sbi);
122 destroy_segment_manager(sbi);
126 sb->s_fs_info = NULL;
127 brelse(sbi->raw_super_buf);
131 int f2fs_sync_fs(struct super_block *sb, int sync)
133 struct f2fs_sb_info *sbi = F2FS_SB(sb);
135 if (!sbi->s_dirty && !get_pages(sbi, F2FS_DIRTY_NODES))
139 write_checkpoint(sbi, false, false);
141 f2fs_balance_fs(sbi);
146 static int f2fs_freeze(struct super_block *sb)
150 if (sb->s_flags & MS_RDONLY)
153 err = f2fs_sync_fs(sb, 1);
157 static int f2fs_unfreeze(struct super_block *sb)
162 static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
164 struct super_block *sb = dentry->d_sb;
165 struct f2fs_sb_info *sbi = F2FS_SB(sb);
166 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
167 block_t total_count, user_block_count, start_count, ovp_count;
169 total_count = le64_to_cpu(sbi->raw_super->block_count);
170 user_block_count = sbi->user_block_count;
171 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
172 ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg;
173 buf->f_type = F2FS_SUPER_MAGIC;
174 buf->f_bsize = sbi->blocksize;
176 buf->f_blocks = total_count - start_count;
177 buf->f_bfree = buf->f_blocks - valid_user_blocks(sbi) - ovp_count;
178 buf->f_bavail = user_block_count - valid_user_blocks(sbi);
180 buf->f_files = sbi->total_node_count;
181 buf->f_ffree = sbi->total_node_count - valid_inode_count(sbi);
183 buf->f_namelen = F2FS_MAX_NAME_LEN;
184 buf->f_fsid.val[0] = (u32)id;
185 buf->f_fsid.val[1] = (u32)(id >> 32);
190 static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
192 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
194 if (test_opt(sbi, BG_GC))
195 seq_puts(seq, ",background_gc_on");
197 seq_puts(seq, ",background_gc_off");
198 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
199 seq_puts(seq, ",disable_roll_forward");
200 if (test_opt(sbi, DISCARD))
201 seq_puts(seq, ",discard");
202 if (test_opt(sbi, NOHEAP))
203 seq_puts(seq, ",no_heap_alloc");
204 #ifdef CONFIG_F2FS_FS_XATTR
205 if (test_opt(sbi, XATTR_USER))
206 seq_puts(seq, ",user_xattr");
208 seq_puts(seq, ",nouser_xattr");
210 #ifdef CONFIG_F2FS_FS_POSIX_ACL
211 if (test_opt(sbi, POSIX_ACL))
212 seq_puts(seq, ",acl");
214 seq_puts(seq, ",noacl");
216 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
217 seq_puts(seq, ",disable_ext_identify");
219 seq_printf(seq, ",active_logs=%u", sbi->active_logs);
224 static struct super_operations f2fs_sops = {
225 .alloc_inode = f2fs_alloc_inode,
226 .destroy_inode = f2fs_destroy_inode,
227 .write_inode = f2fs_write_inode,
228 .show_options = f2fs_show_options,
229 .evict_inode = f2fs_evict_inode,
230 .put_super = f2fs_put_super,
231 .sync_fs = f2fs_sync_fs,
232 .freeze_fs = f2fs_freeze,
233 .unfreeze_fs = f2fs_unfreeze,
234 .statfs = f2fs_statfs,
237 static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
238 u64 ino, u32 generation)
240 struct f2fs_sb_info *sbi = F2FS_SB(sb);
243 if (ino < F2FS_ROOT_INO(sbi))
244 return ERR_PTR(-ESTALE);
247 * f2fs_iget isn't quite right if the inode is currently unallocated!
248 * However f2fs_iget currently does appropriate checks to handle stale
249 * inodes so everything is OK.
251 inode = f2fs_iget(sb, ino);
253 return ERR_CAST(inode);
254 if (generation && inode->i_generation != generation) {
255 /* we didn't find the right inode.. */
257 return ERR_PTR(-ESTALE);
262 static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
263 int fh_len, int fh_type)
265 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
269 static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
270 int fh_len, int fh_type)
272 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
276 static const struct export_operations f2fs_export_ops = {
277 .fh_to_dentry = f2fs_fh_to_dentry,
278 .fh_to_parent = f2fs_fh_to_parent,
279 .get_parent = f2fs_get_parent,
282 static int parse_options(struct super_block *sb, struct f2fs_sb_info *sbi,
285 substring_t args[MAX_OPT_ARGS];
292 while ((p = strsep(&options, ",")) != NULL) {
297 * Initialize args struct so we know whether arg was
298 * found; some options take optional arguments.
300 args[0].to = args[0].from = NULL;
301 token = match_token(p, f2fs_tokens, args);
304 case Opt_gc_background_off:
305 clear_opt(sbi, BG_GC);
307 case Opt_disable_roll_forward:
308 set_opt(sbi, DISABLE_ROLL_FORWARD);
311 set_opt(sbi, DISCARD);
314 set_opt(sbi, NOHEAP);
316 #ifdef CONFIG_F2FS_FS_XATTR
317 case Opt_nouser_xattr:
318 clear_opt(sbi, XATTR_USER);
321 case Opt_nouser_xattr:
322 f2fs_msg(sb, KERN_INFO,
323 "nouser_xattr options not supported");
326 #ifdef CONFIG_F2FS_FS_POSIX_ACL
328 clear_opt(sbi, POSIX_ACL);
332 f2fs_msg(sb, KERN_INFO, "noacl options not supported");
335 case Opt_active_logs:
336 if (args->from && match_int(args, &arg))
338 if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
340 sbi->active_logs = arg;
342 case Opt_disable_ext_identify:
343 set_opt(sbi, DISABLE_EXT_IDENTIFY);
346 f2fs_msg(sb, KERN_ERR,
347 "Unrecognized mount option \"%s\" or missing value",
355 static loff_t max_file_size(unsigned bits)
357 loff_t result = ADDRS_PER_INODE;
358 loff_t leaf_count = ADDRS_PER_BLOCK;
360 /* two direct node blocks */
361 result += (leaf_count * 2);
363 /* two indirect node blocks */
364 leaf_count *= NIDS_PER_BLOCK;
365 result += (leaf_count * 2);
367 /* one double indirect node block */
368 leaf_count *= NIDS_PER_BLOCK;
369 result += leaf_count;
375 static int sanity_check_raw_super(struct super_block *sb,
376 struct f2fs_super_block *raw_super)
378 unsigned int blocksize;
380 if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
381 f2fs_msg(sb, KERN_INFO,
382 "Magic Mismatch, valid(0x%x) - read(0x%x)",
383 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
387 /* Currently, support only 4KB block size */
388 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
389 if (blocksize != PAGE_CACHE_SIZE) {
390 f2fs_msg(sb, KERN_INFO,
391 "Invalid blocksize (%u), supports only 4KB\n",
395 if (le32_to_cpu(raw_super->log_sectorsize) !=
396 F2FS_LOG_SECTOR_SIZE) {
397 f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize");
400 if (le32_to_cpu(raw_super->log_sectors_per_block) !=
401 F2FS_LOG_SECTORS_PER_BLOCK) {
402 f2fs_msg(sb, KERN_INFO, "Invalid log sectors per block");
408 static int sanity_check_ckpt(struct f2fs_sb_info *sbi)
410 unsigned int total, fsmeta;
411 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
412 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
414 total = le32_to_cpu(raw_super->segment_count);
415 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
416 fsmeta += le32_to_cpu(raw_super->segment_count_sit);
417 fsmeta += le32_to_cpu(raw_super->segment_count_nat);
418 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
419 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
424 if (is_set_ckpt_flags(ckpt, CP_ERROR_FLAG)) {
425 f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
431 static void init_sb_info(struct f2fs_sb_info *sbi)
433 struct f2fs_super_block *raw_super = sbi->raw_super;
436 sbi->log_sectors_per_block =
437 le32_to_cpu(raw_super->log_sectors_per_block);
438 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
439 sbi->blocksize = 1 << sbi->log_blocksize;
440 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
441 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
442 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
443 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
444 sbi->total_sections = le32_to_cpu(raw_super->section_count);
445 sbi->total_node_count =
446 (le32_to_cpu(raw_super->segment_count_nat) / 2)
447 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
448 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
449 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
450 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
452 for (i = 0; i < NR_COUNT_TYPE; i++)
453 atomic_set(&sbi->nr_pages[i], 0);
456 static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
458 struct f2fs_sb_info *sbi;
459 struct f2fs_super_block *raw_super;
460 struct buffer_head *raw_super_buf;
465 /* allocate memory for f2fs-specific super block info */
466 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
470 /* set a block size */
471 if (!sb_set_blocksize(sb, F2FS_BLKSIZE)) {
472 f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
476 /* read f2fs raw super block */
477 raw_super_buf = sb_bread(sb, 0);
478 if (!raw_super_buf) {
480 f2fs_msg(sb, KERN_ERR, "unable to read superblock");
483 raw_super = (struct f2fs_super_block *)
484 ((char *)raw_super_buf->b_data + F2FS_SUPER_OFFSET);
486 /* init some FS parameters */
487 sbi->active_logs = NR_CURSEG_TYPE;
491 #ifdef CONFIG_F2FS_FS_XATTR
492 set_opt(sbi, XATTR_USER);
494 #ifdef CONFIG_F2FS_FS_POSIX_ACL
495 set_opt(sbi, POSIX_ACL);
497 /* parse mount options */
498 if (parse_options(sb, sbi, (char *)data))
501 /* sanity checking of raw super */
502 if (sanity_check_raw_super(sb, raw_super)) {
503 f2fs_msg(sb, KERN_ERR, "Can't find a valid F2FS filesystem");
507 sb->s_maxbytes = max_file_size(le32_to_cpu(raw_super->log_blocksize));
508 sb->s_max_links = F2FS_LINK_MAX;
509 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
511 sb->s_op = &f2fs_sops;
512 sb->s_xattr = f2fs_xattr_handlers;
513 sb->s_export_op = &f2fs_export_ops;
514 sb->s_magic = F2FS_SUPER_MAGIC;
517 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
518 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
519 memcpy(sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
521 /* init f2fs-specific super block info */
523 sbi->raw_super = raw_super;
524 sbi->raw_super_buf = raw_super_buf;
525 mutex_init(&sbi->gc_mutex);
526 mutex_init(&sbi->write_inode);
527 mutex_init(&sbi->writepages);
528 mutex_init(&sbi->cp_mutex);
529 for (i = 0; i < NR_LOCK_TYPE; i++)
530 mutex_init(&sbi->fs_lock[i]);
532 spin_lock_init(&sbi->stat_lock);
533 init_rwsem(&sbi->bio_sem);
536 /* get an inode for meta space */
537 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
538 if (IS_ERR(sbi->meta_inode)) {
539 f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
540 err = PTR_ERR(sbi->meta_inode);
544 err = get_valid_checkpoint(sbi);
546 f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
547 goto free_meta_inode;
550 /* sanity checking of checkpoint */
552 if (sanity_check_ckpt(sbi)) {
553 f2fs_msg(sb, KERN_ERR, "Invalid F2FS checkpoint");
557 sbi->total_valid_node_count =
558 le32_to_cpu(sbi->ckpt->valid_node_count);
559 sbi->total_valid_inode_count =
560 le32_to_cpu(sbi->ckpt->valid_inode_count);
561 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
562 sbi->total_valid_block_count =
563 le64_to_cpu(sbi->ckpt->valid_block_count);
564 sbi->last_valid_block_count = sbi->total_valid_block_count;
565 sbi->alloc_valid_block_count = 0;
566 INIT_LIST_HEAD(&sbi->dir_inode_list);
567 spin_lock_init(&sbi->dir_inode_lock);
569 init_orphan_info(sbi);
571 /* setup f2fs internal modules */
572 err = build_segment_manager(sbi);
574 f2fs_msg(sb, KERN_ERR,
575 "Failed to initialize F2FS segment manager");
578 err = build_node_manager(sbi);
580 f2fs_msg(sb, KERN_ERR,
581 "Failed to initialize F2FS node manager");
585 build_gc_manager(sbi);
587 /* get an inode for node space */
588 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
589 if (IS_ERR(sbi->node_inode)) {
590 f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
591 err = PTR_ERR(sbi->node_inode);
595 /* if there are nt orphan nodes free them */
597 if (recover_orphan_inodes(sbi))
598 goto free_node_inode;
600 /* read root inode and dentry */
601 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
603 f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
605 goto free_node_inode;
607 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size)
608 goto free_root_inode;
610 sb->s_root = d_make_root(root); /* allocate root dentry */
613 goto free_root_inode;
616 /* recover fsynced data */
617 if (!test_opt(sbi, DISABLE_ROLL_FORWARD))
618 recover_fsync_data(sbi);
620 /* After POR, we can run background GC thread */
621 err = start_gc_thread(sbi);
625 err = f2fs_build_stats(sbi);
636 iput(sbi->node_inode);
638 destroy_node_manager(sbi);
640 destroy_segment_manager(sbi);
644 make_bad_inode(sbi->meta_inode);
645 iput(sbi->meta_inode);
647 brelse(raw_super_buf);
653 static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
654 const char *dev_name, void *data)
656 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
659 static struct file_system_type f2fs_fs_type = {
660 .owner = THIS_MODULE,
663 .kill_sb = kill_block_super,
664 .fs_flags = FS_REQUIRES_DEV,
667 static int __init init_inodecache(void)
669 f2fs_inode_cachep = f2fs_kmem_cache_create("f2fs_inode_cache",
670 sizeof(struct f2fs_inode_info), NULL);
671 if (f2fs_inode_cachep == NULL)
676 static void destroy_inodecache(void)
679 * Make sure all delayed rcu free inodes are flushed before we
683 kmem_cache_destroy(f2fs_inode_cachep);
686 static int __init init_f2fs_fs(void)
690 err = init_inodecache();
693 err = create_node_manager_caches();
696 err = create_gc_caches();
699 err = create_checkpoint_caches();
702 err = register_filesystem(&f2fs_fs_type);
705 f2fs_create_root_stats();
710 static void __exit exit_f2fs_fs(void)
712 f2fs_destroy_root_stats();
713 unregister_filesystem(&f2fs_fs_type);
714 destroy_checkpoint_caches();
716 destroy_node_manager_caches();
717 destroy_inodecache();
720 module_init(init_f2fs_fs)
721 module_exit(exit_f2fs_fs)
723 MODULE_AUTHOR("Samsung Electronics's Praesto Team");
724 MODULE_DESCRIPTION("Flash Friendly File System");
725 MODULE_LICENSE("GPL");