Merge tag 'blackfin-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/realm...
[linux-2.6.git] / fs / f2fs / super.c
blob75c7dc363e9267378d5cdf39f811c16a7adc8692
1 /*
2 * fs/f2fs/super.c
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>
13 #include <linux/fs.h>
14 #include <linux/statfs.h>
15 #include <linux/buffer_head.h>
16 #include <linux/backing-dev.h>
17 #include <linux/kthread.h>
18 #include <linux/parser.h>
19 #include <linux/mount.h>
20 #include <linux/seq_file.h>
21 #include <linux/random.h>
22 #include <linux/exportfs.h>
23 #include <linux/blkdev.h>
24 #include <linux/f2fs_fs.h>
26 #include "f2fs.h"
27 #include "node.h"
28 #include "segment.h"
29 #include "xattr.h"
31 #define CREATE_TRACE_POINTS
32 #include <trace/events/f2fs.h>
34 static struct kmem_cache *f2fs_inode_cachep;
36 enum {
37 Opt_gc_background,
38 Opt_disable_roll_forward,
39 Opt_discard,
40 Opt_noheap,
41 Opt_nouser_xattr,
42 Opt_noacl,
43 Opt_active_logs,
44 Opt_disable_ext_identify,
45 Opt_err,
48 static match_table_t f2fs_tokens = {
49 {Opt_gc_background, "background_gc=%s"},
50 {Opt_disable_roll_forward, "disable_roll_forward"},
51 {Opt_discard, "discard"},
52 {Opt_noheap, "no_heap"},
53 {Opt_nouser_xattr, "nouser_xattr"},
54 {Opt_noacl, "noacl"},
55 {Opt_active_logs, "active_logs=%u"},
56 {Opt_disable_ext_identify, "disable_ext_identify"},
57 {Opt_err, NULL},
60 void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
62 struct va_format vaf;
63 va_list args;
65 va_start(args, fmt);
66 vaf.fmt = fmt;
67 vaf.va = &args;
68 printk("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
69 va_end(args);
72 static void init_once(void *foo)
74 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
76 inode_init_once(&fi->vfs_inode);
79 static int parse_options(struct super_block *sb, char *options)
81 struct f2fs_sb_info *sbi = F2FS_SB(sb);
82 substring_t args[MAX_OPT_ARGS];
83 char *p, *name;
84 int arg = 0;
86 if (!options)
87 return 0;
89 while ((p = strsep(&options, ",")) != NULL) {
90 int token;
91 if (!*p)
92 continue;
94 * Initialize args struct so we know whether arg was
95 * found; some options take optional arguments.
97 args[0].to = args[0].from = NULL;
98 token = match_token(p, f2fs_tokens, args);
100 switch (token) {
101 case Opt_gc_background:
102 name = match_strdup(&args[0]);
104 if (!name)
105 return -ENOMEM;
106 if (!strncmp(name, "on", 2))
107 set_opt(sbi, BG_GC);
108 else if (!strncmp(name, "off", 3))
109 clear_opt(sbi, BG_GC);
110 else {
111 kfree(name);
112 return -EINVAL;
114 kfree(name);
115 break;
116 case Opt_disable_roll_forward:
117 set_opt(sbi, DISABLE_ROLL_FORWARD);
118 break;
119 case Opt_discard:
120 set_opt(sbi, DISCARD);
121 break;
122 case Opt_noheap:
123 set_opt(sbi, NOHEAP);
124 break;
125 #ifdef CONFIG_F2FS_FS_XATTR
126 case Opt_nouser_xattr:
127 clear_opt(sbi, XATTR_USER);
128 break;
129 #else
130 case Opt_nouser_xattr:
131 f2fs_msg(sb, KERN_INFO,
132 "nouser_xattr options not supported");
133 break;
134 #endif
135 #ifdef CONFIG_F2FS_FS_POSIX_ACL
136 case Opt_noacl:
137 clear_opt(sbi, POSIX_ACL);
138 break;
139 #else
140 case Opt_noacl:
141 f2fs_msg(sb, KERN_INFO, "noacl options not supported");
142 break;
143 #endif
144 case Opt_active_logs:
145 if (args->from && match_int(args, &arg))
146 return -EINVAL;
147 if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
148 return -EINVAL;
149 sbi->active_logs = arg;
150 break;
151 case Opt_disable_ext_identify:
152 set_opt(sbi, DISABLE_EXT_IDENTIFY);
153 break;
154 default:
155 f2fs_msg(sb, KERN_ERR,
156 "Unrecognized mount option \"%s\" or missing value",
158 return -EINVAL;
161 return 0;
164 static struct inode *f2fs_alloc_inode(struct super_block *sb)
166 struct f2fs_inode_info *fi;
168 fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_NOFS | __GFP_ZERO);
169 if (!fi)
170 return NULL;
172 init_once((void *) fi);
174 /* Initialize f2fs-specific inode info */
175 fi->vfs_inode.i_version = 1;
176 atomic_set(&fi->dirty_dents, 0);
177 fi->i_current_depth = 1;
178 fi->i_advise = 0;
179 rwlock_init(&fi->ext.ext_lock);
181 set_inode_flag(fi, FI_NEW_INODE);
183 return &fi->vfs_inode;
186 static int f2fs_drop_inode(struct inode *inode)
189 * This is to avoid a deadlock condition like below.
190 * writeback_single_inode(inode)
191 * - f2fs_write_data_page
192 * - f2fs_gc -> iput -> evict
193 * - inode_wait_for_writeback(inode)
195 if (!inode_unhashed(inode) && inode->i_state & I_SYNC)
196 return 0;
197 return generic_drop_inode(inode);
201 * f2fs_dirty_inode() is called from __mark_inode_dirty()
203 * We should call set_dirty_inode to write the dirty inode through write_inode.
205 static void f2fs_dirty_inode(struct inode *inode, int flags)
207 set_inode_flag(F2FS_I(inode), FI_DIRTY_INODE);
208 return;
211 static void f2fs_i_callback(struct rcu_head *head)
213 struct inode *inode = container_of(head, struct inode, i_rcu);
214 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
217 static void f2fs_destroy_inode(struct inode *inode)
219 call_rcu(&inode->i_rcu, f2fs_i_callback);
222 static void f2fs_put_super(struct super_block *sb)
224 struct f2fs_sb_info *sbi = F2FS_SB(sb);
226 f2fs_destroy_stats(sbi);
227 stop_gc_thread(sbi);
229 write_checkpoint(sbi, true);
231 iput(sbi->node_inode);
232 iput(sbi->meta_inode);
234 /* destroy f2fs internal modules */
235 destroy_node_manager(sbi);
236 destroy_segment_manager(sbi);
238 kfree(sbi->ckpt);
240 sb->s_fs_info = NULL;
241 brelse(sbi->raw_super_buf);
242 kfree(sbi);
245 int f2fs_sync_fs(struct super_block *sb, int sync)
247 struct f2fs_sb_info *sbi = F2FS_SB(sb);
249 trace_f2fs_sync_fs(sb, sync);
251 if (!sbi->s_dirty && !get_pages(sbi, F2FS_DIRTY_NODES))
252 return 0;
254 if (sync) {
255 mutex_lock(&sbi->gc_mutex);
256 write_checkpoint(sbi, false);
257 mutex_unlock(&sbi->gc_mutex);
258 } else {
259 f2fs_balance_fs(sbi);
262 return 0;
265 static int f2fs_freeze(struct super_block *sb)
267 int err;
269 if (f2fs_readonly(sb))
270 return 0;
272 err = f2fs_sync_fs(sb, 1);
273 return err;
276 static int f2fs_unfreeze(struct super_block *sb)
278 return 0;
281 static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
283 struct super_block *sb = dentry->d_sb;
284 struct f2fs_sb_info *sbi = F2FS_SB(sb);
285 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
286 block_t total_count, user_block_count, start_count, ovp_count;
288 total_count = le64_to_cpu(sbi->raw_super->block_count);
289 user_block_count = sbi->user_block_count;
290 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
291 ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg;
292 buf->f_type = F2FS_SUPER_MAGIC;
293 buf->f_bsize = sbi->blocksize;
295 buf->f_blocks = total_count - start_count;
296 buf->f_bfree = buf->f_blocks - valid_user_blocks(sbi) - ovp_count;
297 buf->f_bavail = user_block_count - valid_user_blocks(sbi);
299 buf->f_files = sbi->total_node_count;
300 buf->f_ffree = sbi->total_node_count - valid_inode_count(sbi);
302 buf->f_namelen = F2FS_NAME_LEN;
303 buf->f_fsid.val[0] = (u32)id;
304 buf->f_fsid.val[1] = (u32)(id >> 32);
306 return 0;
309 static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
311 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
313 if (!(root->d_sb->s_flags & MS_RDONLY) && test_opt(sbi, BG_GC))
314 seq_printf(seq, ",background_gc=%s", "on");
315 else
316 seq_printf(seq, ",background_gc=%s", "off");
317 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
318 seq_puts(seq, ",disable_roll_forward");
319 if (test_opt(sbi, DISCARD))
320 seq_puts(seq, ",discard");
321 if (test_opt(sbi, NOHEAP))
322 seq_puts(seq, ",no_heap_alloc");
323 #ifdef CONFIG_F2FS_FS_XATTR
324 if (test_opt(sbi, XATTR_USER))
325 seq_puts(seq, ",user_xattr");
326 else
327 seq_puts(seq, ",nouser_xattr");
328 #endif
329 #ifdef CONFIG_F2FS_FS_POSIX_ACL
330 if (test_opt(sbi, POSIX_ACL))
331 seq_puts(seq, ",acl");
332 else
333 seq_puts(seq, ",noacl");
334 #endif
335 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
336 seq_puts(seq, ",disable_ext_identify");
338 seq_printf(seq, ",active_logs=%u", sbi->active_logs);
340 return 0;
343 static int f2fs_remount(struct super_block *sb, int *flags, char *data)
345 struct f2fs_sb_info *sbi = F2FS_SB(sb);
346 struct f2fs_mount_info org_mount_opt;
347 int err, active_logs;
350 * Save the old mount options in case we
351 * need to restore them.
353 org_mount_opt = sbi->mount_opt;
354 active_logs = sbi->active_logs;
356 /* parse mount options */
357 err = parse_options(sb, data);
358 if (err)
359 goto restore_opts;
362 * Previous and new state of filesystem is RO,
363 * so no point in checking GC conditions.
365 if ((sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY))
366 goto skip;
369 * We stop the GC thread if FS is mounted as RO
370 * or if background_gc = off is passed in mount
371 * option. Also sync the filesystem.
373 if ((*flags & MS_RDONLY) || !test_opt(sbi, BG_GC)) {
374 if (sbi->gc_thread) {
375 stop_gc_thread(sbi);
376 f2fs_sync_fs(sb, 1);
378 } else if (test_opt(sbi, BG_GC) && !sbi->gc_thread) {
379 err = start_gc_thread(sbi);
380 if (err)
381 goto restore_opts;
383 skip:
384 /* Update the POSIXACL Flag */
385 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
386 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
387 return 0;
389 restore_opts:
390 sbi->mount_opt = org_mount_opt;
391 sbi->active_logs = active_logs;
392 return err;
395 static struct super_operations f2fs_sops = {
396 .alloc_inode = f2fs_alloc_inode,
397 .drop_inode = f2fs_drop_inode,
398 .destroy_inode = f2fs_destroy_inode,
399 .write_inode = f2fs_write_inode,
400 .dirty_inode = f2fs_dirty_inode,
401 .show_options = f2fs_show_options,
402 .evict_inode = f2fs_evict_inode,
403 .put_super = f2fs_put_super,
404 .sync_fs = f2fs_sync_fs,
405 .freeze_fs = f2fs_freeze,
406 .unfreeze_fs = f2fs_unfreeze,
407 .statfs = f2fs_statfs,
408 .remount_fs = f2fs_remount,
411 static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
412 u64 ino, u32 generation)
414 struct f2fs_sb_info *sbi = F2FS_SB(sb);
415 struct inode *inode;
417 if (ino < F2FS_ROOT_INO(sbi))
418 return ERR_PTR(-ESTALE);
421 * f2fs_iget isn't quite right if the inode is currently unallocated!
422 * However f2fs_iget currently does appropriate checks to handle stale
423 * inodes so everything is OK.
425 inode = f2fs_iget(sb, ino);
426 if (IS_ERR(inode))
427 return ERR_CAST(inode);
428 if (generation && inode->i_generation != generation) {
429 /* we didn't find the right inode.. */
430 iput(inode);
431 return ERR_PTR(-ESTALE);
433 return inode;
436 static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
437 int fh_len, int fh_type)
439 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
440 f2fs_nfs_get_inode);
443 static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
444 int fh_len, int fh_type)
446 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
447 f2fs_nfs_get_inode);
450 static const struct export_operations f2fs_export_ops = {
451 .fh_to_dentry = f2fs_fh_to_dentry,
452 .fh_to_parent = f2fs_fh_to_parent,
453 .get_parent = f2fs_get_parent,
456 static loff_t max_file_size(unsigned bits)
458 loff_t result = ADDRS_PER_INODE;
459 loff_t leaf_count = ADDRS_PER_BLOCK;
461 /* two direct node blocks */
462 result += (leaf_count * 2);
464 /* two indirect node blocks */
465 leaf_count *= NIDS_PER_BLOCK;
466 result += (leaf_count * 2);
468 /* one double indirect node block */
469 leaf_count *= NIDS_PER_BLOCK;
470 result += leaf_count;
472 result <<= bits;
473 return result;
476 static int sanity_check_raw_super(struct super_block *sb,
477 struct f2fs_super_block *raw_super)
479 unsigned int blocksize;
481 if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
482 f2fs_msg(sb, KERN_INFO,
483 "Magic Mismatch, valid(0x%x) - read(0x%x)",
484 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
485 return 1;
488 /* Currently, support only 4KB page cache size */
489 if (F2FS_BLKSIZE != PAGE_CACHE_SIZE) {
490 f2fs_msg(sb, KERN_INFO,
491 "Invalid page_cache_size (%lu), supports only 4KB\n",
492 PAGE_CACHE_SIZE);
493 return 1;
496 /* Currently, support only 4KB block size */
497 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
498 if (blocksize != F2FS_BLKSIZE) {
499 f2fs_msg(sb, KERN_INFO,
500 "Invalid blocksize (%u), supports only 4KB\n",
501 blocksize);
502 return 1;
505 if (le32_to_cpu(raw_super->log_sectorsize) !=
506 F2FS_LOG_SECTOR_SIZE) {
507 f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize");
508 return 1;
510 if (le32_to_cpu(raw_super->log_sectors_per_block) !=
511 F2FS_LOG_SECTORS_PER_BLOCK) {
512 f2fs_msg(sb, KERN_INFO, "Invalid log sectors per block");
513 return 1;
515 return 0;
518 static int sanity_check_ckpt(struct f2fs_sb_info *sbi)
520 unsigned int total, fsmeta;
521 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
522 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
524 total = le32_to_cpu(raw_super->segment_count);
525 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
526 fsmeta += le32_to_cpu(raw_super->segment_count_sit);
527 fsmeta += le32_to_cpu(raw_super->segment_count_nat);
528 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
529 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
531 if (fsmeta >= total)
532 return 1;
534 if (is_set_ckpt_flags(ckpt, CP_ERROR_FLAG)) {
535 f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
536 return 1;
538 return 0;
541 static void init_sb_info(struct f2fs_sb_info *sbi)
543 struct f2fs_super_block *raw_super = sbi->raw_super;
544 int i;
546 sbi->log_sectors_per_block =
547 le32_to_cpu(raw_super->log_sectors_per_block);
548 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
549 sbi->blocksize = 1 << sbi->log_blocksize;
550 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
551 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
552 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
553 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
554 sbi->total_sections = le32_to_cpu(raw_super->section_count);
555 sbi->total_node_count =
556 (le32_to_cpu(raw_super->segment_count_nat) / 2)
557 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
558 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
559 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
560 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
561 sbi->cur_victim_sec = NULL_SECNO;
563 for (i = 0; i < NR_COUNT_TYPE; i++)
564 atomic_set(&sbi->nr_pages[i], 0);
567 static int validate_superblock(struct super_block *sb,
568 struct f2fs_super_block **raw_super,
569 struct buffer_head **raw_super_buf, sector_t block)
571 const char *super = (block == 0 ? "first" : "second");
573 /* read f2fs raw super block */
574 *raw_super_buf = sb_bread(sb, block);
575 if (!*raw_super_buf) {
576 f2fs_msg(sb, KERN_ERR, "unable to read %s superblock",
577 super);
578 return -EIO;
581 *raw_super = (struct f2fs_super_block *)
582 ((char *)(*raw_super_buf)->b_data + F2FS_SUPER_OFFSET);
584 /* sanity checking of raw super */
585 if (!sanity_check_raw_super(sb, *raw_super))
586 return 0;
588 f2fs_msg(sb, KERN_ERR, "Can't find a valid F2FS filesystem "
589 "in %s superblock", super);
590 return -EINVAL;
593 static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
595 struct f2fs_sb_info *sbi;
596 struct f2fs_super_block *raw_super;
597 struct buffer_head *raw_super_buf;
598 struct inode *root;
599 long err = -EINVAL;
600 int i;
602 /* allocate memory for f2fs-specific super block info */
603 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
604 if (!sbi)
605 return -ENOMEM;
607 /* set a block size */
608 if (!sb_set_blocksize(sb, F2FS_BLKSIZE)) {
609 f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
610 goto free_sbi;
613 err = validate_superblock(sb, &raw_super, &raw_super_buf, 0);
614 if (err) {
615 brelse(raw_super_buf);
616 /* check secondary superblock when primary failed */
617 err = validate_superblock(sb, &raw_super, &raw_super_buf, 1);
618 if (err)
619 goto free_sb_buf;
621 sb->s_fs_info = sbi;
622 /* init some FS parameters */
623 sbi->active_logs = NR_CURSEG_TYPE;
625 set_opt(sbi, BG_GC);
627 #ifdef CONFIG_F2FS_FS_XATTR
628 set_opt(sbi, XATTR_USER);
629 #endif
630 #ifdef CONFIG_F2FS_FS_POSIX_ACL
631 set_opt(sbi, POSIX_ACL);
632 #endif
633 /* parse mount options */
634 err = parse_options(sb, (char *)data);
635 if (err)
636 goto free_sb_buf;
638 sb->s_maxbytes = max_file_size(le32_to_cpu(raw_super->log_blocksize));
639 sb->s_max_links = F2FS_LINK_MAX;
640 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
642 sb->s_op = &f2fs_sops;
643 sb->s_xattr = f2fs_xattr_handlers;
644 sb->s_export_op = &f2fs_export_ops;
645 sb->s_magic = F2FS_SUPER_MAGIC;
646 sb->s_time_gran = 1;
647 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
648 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
649 memcpy(sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
651 /* init f2fs-specific super block info */
652 sbi->sb = sb;
653 sbi->raw_super = raw_super;
654 sbi->raw_super_buf = raw_super_buf;
655 mutex_init(&sbi->gc_mutex);
656 mutex_init(&sbi->writepages);
657 mutex_init(&sbi->cp_mutex);
658 for (i = 0; i < NR_GLOBAL_LOCKS; i++)
659 mutex_init(&sbi->fs_lock[i]);
660 mutex_init(&sbi->node_write);
661 sbi->por_doing = 0;
662 spin_lock_init(&sbi->stat_lock);
663 init_rwsem(&sbi->bio_sem);
664 init_sb_info(sbi);
666 /* get an inode for meta space */
667 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
668 if (IS_ERR(sbi->meta_inode)) {
669 f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
670 err = PTR_ERR(sbi->meta_inode);
671 goto free_sb_buf;
674 err = get_valid_checkpoint(sbi);
675 if (err) {
676 f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
677 goto free_meta_inode;
680 /* sanity checking of checkpoint */
681 err = -EINVAL;
682 if (sanity_check_ckpt(sbi)) {
683 f2fs_msg(sb, KERN_ERR, "Invalid F2FS checkpoint");
684 goto free_cp;
687 sbi->total_valid_node_count =
688 le32_to_cpu(sbi->ckpt->valid_node_count);
689 sbi->total_valid_inode_count =
690 le32_to_cpu(sbi->ckpt->valid_inode_count);
691 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
692 sbi->total_valid_block_count =
693 le64_to_cpu(sbi->ckpt->valid_block_count);
694 sbi->last_valid_block_count = sbi->total_valid_block_count;
695 sbi->alloc_valid_block_count = 0;
696 INIT_LIST_HEAD(&sbi->dir_inode_list);
697 spin_lock_init(&sbi->dir_inode_lock);
699 init_orphan_info(sbi);
701 /* setup f2fs internal modules */
702 err = build_segment_manager(sbi);
703 if (err) {
704 f2fs_msg(sb, KERN_ERR,
705 "Failed to initialize F2FS segment manager");
706 goto free_sm;
708 err = build_node_manager(sbi);
709 if (err) {
710 f2fs_msg(sb, KERN_ERR,
711 "Failed to initialize F2FS node manager");
712 goto free_nm;
715 build_gc_manager(sbi);
717 /* get an inode for node space */
718 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
719 if (IS_ERR(sbi->node_inode)) {
720 f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
721 err = PTR_ERR(sbi->node_inode);
722 goto free_nm;
725 /* if there are nt orphan nodes free them */
726 err = -EINVAL;
727 if (recover_orphan_inodes(sbi))
728 goto free_node_inode;
730 /* read root inode and dentry */
731 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
732 if (IS_ERR(root)) {
733 f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
734 err = PTR_ERR(root);
735 goto free_node_inode;
737 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size)
738 goto free_root_inode;
740 sb->s_root = d_make_root(root); /* allocate root dentry */
741 if (!sb->s_root) {
742 err = -ENOMEM;
743 goto free_root_inode;
746 /* recover fsynced data */
747 if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
748 err = recover_fsync_data(sbi);
749 if (err)
750 f2fs_msg(sb, KERN_ERR,
751 "Cannot recover all fsync data errno=%ld", err);
755 * If filesystem is not mounted as read-only then
756 * do start the gc_thread.
758 if (!(sb->s_flags & MS_RDONLY)) {
759 /* After POR, we can run background GC thread.*/
760 err = start_gc_thread(sbi);
761 if (err)
762 goto fail;
765 err = f2fs_build_stats(sbi);
766 if (err)
767 goto fail;
769 if (test_opt(sbi, DISCARD)) {
770 struct request_queue *q = bdev_get_queue(sb->s_bdev);
771 if (!blk_queue_discard(q))
772 f2fs_msg(sb, KERN_WARNING,
773 "mounting with \"discard\" option, but "
774 "the device does not support discard");
777 return 0;
778 fail:
779 stop_gc_thread(sbi);
780 free_root_inode:
781 dput(sb->s_root);
782 sb->s_root = NULL;
783 free_node_inode:
784 iput(sbi->node_inode);
785 free_nm:
786 destroy_node_manager(sbi);
787 free_sm:
788 destroy_segment_manager(sbi);
789 free_cp:
790 kfree(sbi->ckpt);
791 free_meta_inode:
792 make_bad_inode(sbi->meta_inode);
793 iput(sbi->meta_inode);
794 free_sb_buf:
795 brelse(raw_super_buf);
796 free_sbi:
797 kfree(sbi);
798 return err;
801 static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
802 const char *dev_name, void *data)
804 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
807 static struct file_system_type f2fs_fs_type = {
808 .owner = THIS_MODULE,
809 .name = "f2fs",
810 .mount = f2fs_mount,
811 .kill_sb = kill_block_super,
812 .fs_flags = FS_REQUIRES_DEV,
814 MODULE_ALIAS_FS("f2fs");
816 static int __init init_inodecache(void)
818 f2fs_inode_cachep = f2fs_kmem_cache_create("f2fs_inode_cache",
819 sizeof(struct f2fs_inode_info), NULL);
820 if (f2fs_inode_cachep == NULL)
821 return -ENOMEM;
822 return 0;
825 static void destroy_inodecache(void)
828 * Make sure all delayed rcu free inodes are flushed before we
829 * destroy cache.
831 rcu_barrier();
832 kmem_cache_destroy(f2fs_inode_cachep);
835 static int __init init_f2fs_fs(void)
837 int err;
839 err = init_inodecache();
840 if (err)
841 goto fail;
842 err = create_node_manager_caches();
843 if (err)
844 goto fail;
845 err = create_gc_caches();
846 if (err)
847 goto fail;
848 err = create_checkpoint_caches();
849 if (err)
850 goto fail;
851 err = register_filesystem(&f2fs_fs_type);
852 if (err)
853 goto fail;
854 f2fs_create_root_stats();
855 fail:
856 return err;
859 static void __exit exit_f2fs_fs(void)
861 f2fs_destroy_root_stats();
862 unregister_filesystem(&f2fs_fs_type);
863 destroy_checkpoint_caches();
864 destroy_gc_caches();
865 destroy_node_manager_caches();
866 destroy_inodecache();
869 module_init(init_f2fs_fs)
870 module_exit(exit_f2fs_fs)
872 MODULE_AUTHOR("Samsung Electronics's Praesto Team");
873 MODULE_DESCRIPTION("Flash Friendly File System");
874 MODULE_LICENSE("GPL");