Merge remote-tracking branch 'asoc/fix/cs42l52' into asoc-linus
[linux-2.6/btrfs-unstable.git] / fs / f2fs / super.c
blob13d0a0fe49dd413ed70d3a10dc864d27bea5c79a
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/proc_fs.h>
22 #include <linux/random.h>
23 #include <linux/exportfs.h>
24 #include <linux/blkdev.h>
25 #include <linux/f2fs_fs.h>
26 #include <linux/sysfs.h>
28 #include "f2fs.h"
29 #include "node.h"
30 #include "segment.h"
31 #include "xattr.h"
32 #include "gc.h"
34 #define CREATE_TRACE_POINTS
35 #include <trace/events/f2fs.h>
37 static struct proc_dir_entry *f2fs_proc_root;
38 static struct kmem_cache *f2fs_inode_cachep;
39 static struct kset *f2fs_kset;
41 enum {
42 Opt_gc_background,
43 Opt_disable_roll_forward,
44 Opt_discard,
45 Opt_noheap,
46 Opt_nouser_xattr,
47 Opt_noacl,
48 Opt_active_logs,
49 Opt_disable_ext_identify,
50 Opt_inline_xattr,
51 Opt_err,
54 static match_table_t f2fs_tokens = {
55 {Opt_gc_background, "background_gc=%s"},
56 {Opt_disable_roll_forward, "disable_roll_forward"},
57 {Opt_discard, "discard"},
58 {Opt_noheap, "no_heap"},
59 {Opt_nouser_xattr, "nouser_xattr"},
60 {Opt_noacl, "noacl"},
61 {Opt_active_logs, "active_logs=%u"},
62 {Opt_disable_ext_identify, "disable_ext_identify"},
63 {Opt_inline_xattr, "inline_xattr"},
64 {Opt_err, NULL},
67 /* Sysfs support for f2fs */
68 struct f2fs_attr {
69 struct attribute attr;
70 ssize_t (*show)(struct f2fs_attr *, struct f2fs_sb_info *, char *);
71 ssize_t (*store)(struct f2fs_attr *, struct f2fs_sb_info *,
72 const char *, size_t);
73 int offset;
76 static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
77 struct f2fs_sb_info *sbi, char *buf)
79 struct f2fs_gc_kthread *gc_kth = sbi->gc_thread;
80 unsigned int *ui;
82 if (!gc_kth)
83 return -EINVAL;
85 ui = (unsigned int *)(((char *)gc_kth) + a->offset);
87 return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
90 static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
91 struct f2fs_sb_info *sbi,
92 const char *buf, size_t count)
94 struct f2fs_gc_kthread *gc_kth = sbi->gc_thread;
95 unsigned long t;
96 unsigned int *ui;
97 ssize_t ret;
99 if (!gc_kth)
100 return -EINVAL;
102 ui = (unsigned int *)(((char *)gc_kth) + a->offset);
104 ret = kstrtoul(skip_spaces(buf), 0, &t);
105 if (ret < 0)
106 return ret;
107 *ui = t;
108 return count;
111 static ssize_t f2fs_attr_show(struct kobject *kobj,
112 struct attribute *attr, char *buf)
114 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
115 s_kobj);
116 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
118 return a->show ? a->show(a, sbi, buf) : 0;
121 static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
122 const char *buf, size_t len)
124 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
125 s_kobj);
126 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
128 return a->store ? a->store(a, sbi, buf, len) : 0;
131 static void f2fs_sb_release(struct kobject *kobj)
133 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
134 s_kobj);
135 complete(&sbi->s_kobj_unregister);
138 #define F2FS_ATTR_OFFSET(_name, _mode, _show, _store, _elname) \
139 static struct f2fs_attr f2fs_attr_##_name = { \
140 .attr = {.name = __stringify(_name), .mode = _mode }, \
141 .show = _show, \
142 .store = _store, \
143 .offset = offsetof(struct f2fs_gc_kthread, _elname), \
146 #define F2FS_RW_ATTR(name, elname) \
147 F2FS_ATTR_OFFSET(name, 0644, f2fs_sbi_show, f2fs_sbi_store, elname)
149 F2FS_RW_ATTR(gc_min_sleep_time, min_sleep_time);
150 F2FS_RW_ATTR(gc_max_sleep_time, max_sleep_time);
151 F2FS_RW_ATTR(gc_no_gc_sleep_time, no_gc_sleep_time);
152 F2FS_RW_ATTR(gc_idle, gc_idle);
154 #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
155 static struct attribute *f2fs_attrs[] = {
156 ATTR_LIST(gc_min_sleep_time),
157 ATTR_LIST(gc_max_sleep_time),
158 ATTR_LIST(gc_no_gc_sleep_time),
159 ATTR_LIST(gc_idle),
160 NULL,
163 static const struct sysfs_ops f2fs_attr_ops = {
164 .show = f2fs_attr_show,
165 .store = f2fs_attr_store,
168 static struct kobj_type f2fs_ktype = {
169 .default_attrs = f2fs_attrs,
170 .sysfs_ops = &f2fs_attr_ops,
171 .release = f2fs_sb_release,
174 void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
176 struct va_format vaf;
177 va_list args;
179 va_start(args, fmt);
180 vaf.fmt = fmt;
181 vaf.va = &args;
182 printk("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
183 va_end(args);
186 static void init_once(void *foo)
188 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
190 inode_init_once(&fi->vfs_inode);
193 static int parse_options(struct super_block *sb, char *options)
195 struct f2fs_sb_info *sbi = F2FS_SB(sb);
196 substring_t args[MAX_OPT_ARGS];
197 char *p, *name;
198 int arg = 0;
200 if (!options)
201 return 0;
203 while ((p = strsep(&options, ",")) != NULL) {
204 int token;
205 if (!*p)
206 continue;
208 * Initialize args struct so we know whether arg was
209 * found; some options take optional arguments.
211 args[0].to = args[0].from = NULL;
212 token = match_token(p, f2fs_tokens, args);
214 switch (token) {
215 case Opt_gc_background:
216 name = match_strdup(&args[0]);
218 if (!name)
219 return -ENOMEM;
220 if (!strncmp(name, "on", 2))
221 set_opt(sbi, BG_GC);
222 else if (!strncmp(name, "off", 3))
223 clear_opt(sbi, BG_GC);
224 else {
225 kfree(name);
226 return -EINVAL;
228 kfree(name);
229 break;
230 case Opt_disable_roll_forward:
231 set_opt(sbi, DISABLE_ROLL_FORWARD);
232 break;
233 case Opt_discard:
234 set_opt(sbi, DISCARD);
235 break;
236 case Opt_noheap:
237 set_opt(sbi, NOHEAP);
238 break;
239 #ifdef CONFIG_F2FS_FS_XATTR
240 case Opt_nouser_xattr:
241 clear_opt(sbi, XATTR_USER);
242 break;
243 case Opt_inline_xattr:
244 set_opt(sbi, INLINE_XATTR);
245 break;
246 #else
247 case Opt_nouser_xattr:
248 f2fs_msg(sb, KERN_INFO,
249 "nouser_xattr options not supported");
250 break;
251 case Opt_inline_xattr:
252 f2fs_msg(sb, KERN_INFO,
253 "inline_xattr options not supported");
254 break;
255 #endif
256 #ifdef CONFIG_F2FS_FS_POSIX_ACL
257 case Opt_noacl:
258 clear_opt(sbi, POSIX_ACL);
259 break;
260 #else
261 case Opt_noacl:
262 f2fs_msg(sb, KERN_INFO, "noacl options not supported");
263 break;
264 #endif
265 case Opt_active_logs:
266 if (args->from && match_int(args, &arg))
267 return -EINVAL;
268 if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
269 return -EINVAL;
270 sbi->active_logs = arg;
271 break;
272 case Opt_disable_ext_identify:
273 set_opt(sbi, DISABLE_EXT_IDENTIFY);
274 break;
275 default:
276 f2fs_msg(sb, KERN_ERR,
277 "Unrecognized mount option \"%s\" or missing value",
279 return -EINVAL;
282 return 0;
285 static struct inode *f2fs_alloc_inode(struct super_block *sb)
287 struct f2fs_inode_info *fi;
289 fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_NOFS | __GFP_ZERO);
290 if (!fi)
291 return NULL;
293 init_once((void *) fi);
295 /* Initialize f2fs-specific inode info */
296 fi->vfs_inode.i_version = 1;
297 atomic_set(&fi->dirty_dents, 0);
298 fi->i_current_depth = 1;
299 fi->i_advise = 0;
300 rwlock_init(&fi->ext.ext_lock);
302 set_inode_flag(fi, FI_NEW_INODE);
304 if (test_opt(F2FS_SB(sb), INLINE_XATTR))
305 set_inode_flag(fi, FI_INLINE_XATTR);
307 return &fi->vfs_inode;
310 static int f2fs_drop_inode(struct inode *inode)
313 * This is to avoid a deadlock condition like below.
314 * writeback_single_inode(inode)
315 * - f2fs_write_data_page
316 * - f2fs_gc -> iput -> evict
317 * - inode_wait_for_writeback(inode)
319 if (!inode_unhashed(inode) && inode->i_state & I_SYNC)
320 return 0;
321 return generic_drop_inode(inode);
325 * f2fs_dirty_inode() is called from __mark_inode_dirty()
327 * We should call set_dirty_inode to write the dirty inode through write_inode.
329 static void f2fs_dirty_inode(struct inode *inode, int flags)
331 set_inode_flag(F2FS_I(inode), FI_DIRTY_INODE);
334 static void f2fs_i_callback(struct rcu_head *head)
336 struct inode *inode = container_of(head, struct inode, i_rcu);
337 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
340 static void f2fs_destroy_inode(struct inode *inode)
342 call_rcu(&inode->i_rcu, f2fs_i_callback);
345 static void f2fs_put_super(struct super_block *sb)
347 struct f2fs_sb_info *sbi = F2FS_SB(sb);
349 if (sbi->s_proc) {
350 remove_proc_entry("segment_info", sbi->s_proc);
351 remove_proc_entry(sb->s_id, f2fs_proc_root);
353 kobject_del(&sbi->s_kobj);
355 f2fs_destroy_stats(sbi);
356 stop_gc_thread(sbi);
358 write_checkpoint(sbi, true);
360 iput(sbi->node_inode);
361 iput(sbi->meta_inode);
363 /* destroy f2fs internal modules */
364 destroy_node_manager(sbi);
365 destroy_segment_manager(sbi);
367 kfree(sbi->ckpt);
368 kobject_put(&sbi->s_kobj);
369 wait_for_completion(&sbi->s_kobj_unregister);
371 sb->s_fs_info = NULL;
372 brelse(sbi->raw_super_buf);
373 kfree(sbi);
376 int f2fs_sync_fs(struct super_block *sb, int sync)
378 struct f2fs_sb_info *sbi = F2FS_SB(sb);
380 trace_f2fs_sync_fs(sb, sync);
382 if (!sbi->s_dirty && !get_pages(sbi, F2FS_DIRTY_NODES))
383 return 0;
385 if (sync) {
386 mutex_lock(&sbi->gc_mutex);
387 write_checkpoint(sbi, false);
388 mutex_unlock(&sbi->gc_mutex);
389 } else {
390 f2fs_balance_fs(sbi);
393 return 0;
396 static int f2fs_freeze(struct super_block *sb)
398 int err;
400 if (f2fs_readonly(sb))
401 return 0;
403 err = f2fs_sync_fs(sb, 1);
404 return err;
407 static int f2fs_unfreeze(struct super_block *sb)
409 return 0;
412 static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
414 struct super_block *sb = dentry->d_sb;
415 struct f2fs_sb_info *sbi = F2FS_SB(sb);
416 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
417 block_t total_count, user_block_count, start_count, ovp_count;
419 total_count = le64_to_cpu(sbi->raw_super->block_count);
420 user_block_count = sbi->user_block_count;
421 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
422 ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg;
423 buf->f_type = F2FS_SUPER_MAGIC;
424 buf->f_bsize = sbi->blocksize;
426 buf->f_blocks = total_count - start_count;
427 buf->f_bfree = buf->f_blocks - valid_user_blocks(sbi) - ovp_count;
428 buf->f_bavail = user_block_count - valid_user_blocks(sbi);
430 buf->f_files = sbi->total_node_count;
431 buf->f_ffree = sbi->total_node_count - valid_inode_count(sbi);
433 buf->f_namelen = F2FS_NAME_LEN;
434 buf->f_fsid.val[0] = (u32)id;
435 buf->f_fsid.val[1] = (u32)(id >> 32);
437 return 0;
440 static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
442 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
444 if (!(root->d_sb->s_flags & MS_RDONLY) && test_opt(sbi, BG_GC))
445 seq_printf(seq, ",background_gc=%s", "on");
446 else
447 seq_printf(seq, ",background_gc=%s", "off");
448 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
449 seq_puts(seq, ",disable_roll_forward");
450 if (test_opt(sbi, DISCARD))
451 seq_puts(seq, ",discard");
452 if (test_opt(sbi, NOHEAP))
453 seq_puts(seq, ",no_heap_alloc");
454 #ifdef CONFIG_F2FS_FS_XATTR
455 if (test_opt(sbi, XATTR_USER))
456 seq_puts(seq, ",user_xattr");
457 else
458 seq_puts(seq, ",nouser_xattr");
459 if (test_opt(sbi, INLINE_XATTR))
460 seq_puts(seq, ",inline_xattr");
461 #endif
462 #ifdef CONFIG_F2FS_FS_POSIX_ACL
463 if (test_opt(sbi, POSIX_ACL))
464 seq_puts(seq, ",acl");
465 else
466 seq_puts(seq, ",noacl");
467 #endif
468 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
469 seq_puts(seq, ",disable_ext_identify");
471 seq_printf(seq, ",active_logs=%u", sbi->active_logs);
473 return 0;
476 static int segment_info_seq_show(struct seq_file *seq, void *offset)
478 struct super_block *sb = seq->private;
479 struct f2fs_sb_info *sbi = F2FS_SB(sb);
480 unsigned int total_segs = le32_to_cpu(sbi->raw_super->segment_count_main);
481 int i;
483 for (i = 0; i < total_segs; i++) {
484 seq_printf(seq, "%u", get_valid_blocks(sbi, i, 1));
485 if (i != 0 && (i % 10) == 0)
486 seq_puts(seq, "\n");
487 else
488 seq_puts(seq, " ");
490 return 0;
493 static int segment_info_open_fs(struct inode *inode, struct file *file)
495 return single_open(file, segment_info_seq_show, PDE_DATA(inode));
498 static const struct file_operations f2fs_seq_segment_info_fops = {
499 .owner = THIS_MODULE,
500 .open = segment_info_open_fs,
501 .read = seq_read,
502 .llseek = seq_lseek,
503 .release = single_release,
506 static int f2fs_remount(struct super_block *sb, int *flags, char *data)
508 struct f2fs_sb_info *sbi = F2FS_SB(sb);
509 struct f2fs_mount_info org_mount_opt;
510 int err, active_logs;
513 * Save the old mount options in case we
514 * need to restore them.
516 org_mount_opt = sbi->mount_opt;
517 active_logs = sbi->active_logs;
519 /* parse mount options */
520 err = parse_options(sb, data);
521 if (err)
522 goto restore_opts;
525 * Previous and new state of filesystem is RO,
526 * so no point in checking GC conditions.
528 if ((sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY))
529 goto skip;
532 * We stop the GC thread if FS is mounted as RO
533 * or if background_gc = off is passed in mount
534 * option. Also sync the filesystem.
536 if ((*flags & MS_RDONLY) || !test_opt(sbi, BG_GC)) {
537 if (sbi->gc_thread) {
538 stop_gc_thread(sbi);
539 f2fs_sync_fs(sb, 1);
541 } else if (test_opt(sbi, BG_GC) && !sbi->gc_thread) {
542 err = start_gc_thread(sbi);
543 if (err)
544 goto restore_opts;
546 skip:
547 /* Update the POSIXACL Flag */
548 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
549 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
550 return 0;
552 restore_opts:
553 sbi->mount_opt = org_mount_opt;
554 sbi->active_logs = active_logs;
555 return err;
558 static struct super_operations f2fs_sops = {
559 .alloc_inode = f2fs_alloc_inode,
560 .drop_inode = f2fs_drop_inode,
561 .destroy_inode = f2fs_destroy_inode,
562 .write_inode = f2fs_write_inode,
563 .dirty_inode = f2fs_dirty_inode,
564 .show_options = f2fs_show_options,
565 .evict_inode = f2fs_evict_inode,
566 .put_super = f2fs_put_super,
567 .sync_fs = f2fs_sync_fs,
568 .freeze_fs = f2fs_freeze,
569 .unfreeze_fs = f2fs_unfreeze,
570 .statfs = f2fs_statfs,
571 .remount_fs = f2fs_remount,
574 static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
575 u64 ino, u32 generation)
577 struct f2fs_sb_info *sbi = F2FS_SB(sb);
578 struct inode *inode;
580 if (ino < F2FS_ROOT_INO(sbi))
581 return ERR_PTR(-ESTALE);
584 * f2fs_iget isn't quite right if the inode is currently unallocated!
585 * However f2fs_iget currently does appropriate checks to handle stale
586 * inodes so everything is OK.
588 inode = f2fs_iget(sb, ino);
589 if (IS_ERR(inode))
590 return ERR_CAST(inode);
591 if (generation && inode->i_generation != generation) {
592 /* we didn't find the right inode.. */
593 iput(inode);
594 return ERR_PTR(-ESTALE);
596 return inode;
599 static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
600 int fh_len, int fh_type)
602 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
603 f2fs_nfs_get_inode);
606 static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
607 int fh_len, int fh_type)
609 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
610 f2fs_nfs_get_inode);
613 static const struct export_operations f2fs_export_ops = {
614 .fh_to_dentry = f2fs_fh_to_dentry,
615 .fh_to_parent = f2fs_fh_to_parent,
616 .get_parent = f2fs_get_parent,
619 static loff_t max_file_size(unsigned bits)
621 loff_t result = (DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS);
622 loff_t leaf_count = ADDRS_PER_BLOCK;
624 /* two direct node blocks */
625 result += (leaf_count * 2);
627 /* two indirect node blocks */
628 leaf_count *= NIDS_PER_BLOCK;
629 result += (leaf_count * 2);
631 /* one double indirect node block */
632 leaf_count *= NIDS_PER_BLOCK;
633 result += leaf_count;
635 result <<= bits;
636 return result;
639 static int sanity_check_raw_super(struct super_block *sb,
640 struct f2fs_super_block *raw_super)
642 unsigned int blocksize;
644 if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
645 f2fs_msg(sb, KERN_INFO,
646 "Magic Mismatch, valid(0x%x) - read(0x%x)",
647 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
648 return 1;
651 /* Currently, support only 4KB page cache size */
652 if (F2FS_BLKSIZE != PAGE_CACHE_SIZE) {
653 f2fs_msg(sb, KERN_INFO,
654 "Invalid page_cache_size (%lu), supports only 4KB\n",
655 PAGE_CACHE_SIZE);
656 return 1;
659 /* Currently, support only 4KB block size */
660 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
661 if (blocksize != F2FS_BLKSIZE) {
662 f2fs_msg(sb, KERN_INFO,
663 "Invalid blocksize (%u), supports only 4KB\n",
664 blocksize);
665 return 1;
668 if (le32_to_cpu(raw_super->log_sectorsize) !=
669 F2FS_LOG_SECTOR_SIZE) {
670 f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize");
671 return 1;
673 if (le32_to_cpu(raw_super->log_sectors_per_block) !=
674 F2FS_LOG_SECTORS_PER_BLOCK) {
675 f2fs_msg(sb, KERN_INFO, "Invalid log sectors per block");
676 return 1;
678 return 0;
681 static int sanity_check_ckpt(struct f2fs_sb_info *sbi)
683 unsigned int total, fsmeta;
684 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
685 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
687 total = le32_to_cpu(raw_super->segment_count);
688 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
689 fsmeta += le32_to_cpu(raw_super->segment_count_sit);
690 fsmeta += le32_to_cpu(raw_super->segment_count_nat);
691 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
692 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
694 if (fsmeta >= total)
695 return 1;
697 if (is_set_ckpt_flags(ckpt, CP_ERROR_FLAG)) {
698 f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
699 return 1;
701 return 0;
704 static void init_sb_info(struct f2fs_sb_info *sbi)
706 struct f2fs_super_block *raw_super = sbi->raw_super;
707 int i;
709 sbi->log_sectors_per_block =
710 le32_to_cpu(raw_super->log_sectors_per_block);
711 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
712 sbi->blocksize = 1 << sbi->log_blocksize;
713 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
714 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
715 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
716 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
717 sbi->total_sections = le32_to_cpu(raw_super->section_count);
718 sbi->total_node_count =
719 (le32_to_cpu(raw_super->segment_count_nat) / 2)
720 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
721 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
722 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
723 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
724 sbi->cur_victim_sec = NULL_SECNO;
726 for (i = 0; i < NR_COUNT_TYPE; i++)
727 atomic_set(&sbi->nr_pages[i], 0);
730 static int validate_superblock(struct super_block *sb,
731 struct f2fs_super_block **raw_super,
732 struct buffer_head **raw_super_buf, sector_t block)
734 const char *super = (block == 0 ? "first" : "second");
736 /* read f2fs raw super block */
737 *raw_super_buf = sb_bread(sb, block);
738 if (!*raw_super_buf) {
739 f2fs_msg(sb, KERN_ERR, "unable to read %s superblock",
740 super);
741 return -EIO;
744 *raw_super = (struct f2fs_super_block *)
745 ((char *)(*raw_super_buf)->b_data + F2FS_SUPER_OFFSET);
747 /* sanity checking of raw super */
748 if (!sanity_check_raw_super(sb, *raw_super))
749 return 0;
751 f2fs_msg(sb, KERN_ERR, "Can't find a valid F2FS filesystem "
752 "in %s superblock", super);
753 return -EINVAL;
756 static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
758 struct f2fs_sb_info *sbi;
759 struct f2fs_super_block *raw_super;
760 struct buffer_head *raw_super_buf;
761 struct inode *root;
762 long err = -EINVAL;
763 int i;
765 /* allocate memory for f2fs-specific super block info */
766 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
767 if (!sbi)
768 return -ENOMEM;
770 /* set a block size */
771 if (!sb_set_blocksize(sb, F2FS_BLKSIZE)) {
772 f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
773 goto free_sbi;
776 err = validate_superblock(sb, &raw_super, &raw_super_buf, 0);
777 if (err) {
778 brelse(raw_super_buf);
779 /* check secondary superblock when primary failed */
780 err = validate_superblock(sb, &raw_super, &raw_super_buf, 1);
781 if (err)
782 goto free_sb_buf;
784 sb->s_fs_info = sbi;
785 /* init some FS parameters */
786 sbi->active_logs = NR_CURSEG_TYPE;
788 set_opt(sbi, BG_GC);
790 #ifdef CONFIG_F2FS_FS_XATTR
791 set_opt(sbi, XATTR_USER);
792 #endif
793 #ifdef CONFIG_F2FS_FS_POSIX_ACL
794 set_opt(sbi, POSIX_ACL);
795 #endif
796 /* parse mount options */
797 err = parse_options(sb, (char *)data);
798 if (err)
799 goto free_sb_buf;
801 sb->s_maxbytes = max_file_size(le32_to_cpu(raw_super->log_blocksize));
802 sb->s_max_links = F2FS_LINK_MAX;
803 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
805 sb->s_op = &f2fs_sops;
806 sb->s_xattr = f2fs_xattr_handlers;
807 sb->s_export_op = &f2fs_export_ops;
808 sb->s_magic = F2FS_SUPER_MAGIC;
809 sb->s_time_gran = 1;
810 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
811 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
812 memcpy(sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
814 /* init f2fs-specific super block info */
815 sbi->sb = sb;
816 sbi->raw_super = raw_super;
817 sbi->raw_super_buf = raw_super_buf;
818 mutex_init(&sbi->gc_mutex);
819 mutex_init(&sbi->writepages);
820 mutex_init(&sbi->cp_mutex);
821 for (i = 0; i < NR_GLOBAL_LOCKS; i++)
822 mutex_init(&sbi->fs_lock[i]);
823 mutex_init(&sbi->node_write);
824 sbi->por_doing = 0;
825 spin_lock_init(&sbi->stat_lock);
826 init_rwsem(&sbi->bio_sem);
827 init_sb_info(sbi);
829 /* get an inode for meta space */
830 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
831 if (IS_ERR(sbi->meta_inode)) {
832 f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
833 err = PTR_ERR(sbi->meta_inode);
834 goto free_sb_buf;
837 err = get_valid_checkpoint(sbi);
838 if (err) {
839 f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
840 goto free_meta_inode;
843 /* sanity checking of checkpoint */
844 err = -EINVAL;
845 if (sanity_check_ckpt(sbi)) {
846 f2fs_msg(sb, KERN_ERR, "Invalid F2FS checkpoint");
847 goto free_cp;
850 sbi->total_valid_node_count =
851 le32_to_cpu(sbi->ckpt->valid_node_count);
852 sbi->total_valid_inode_count =
853 le32_to_cpu(sbi->ckpt->valid_inode_count);
854 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
855 sbi->total_valid_block_count =
856 le64_to_cpu(sbi->ckpt->valid_block_count);
857 sbi->last_valid_block_count = sbi->total_valid_block_count;
858 sbi->alloc_valid_block_count = 0;
859 INIT_LIST_HEAD(&sbi->dir_inode_list);
860 spin_lock_init(&sbi->dir_inode_lock);
862 init_orphan_info(sbi);
864 /* setup f2fs internal modules */
865 err = build_segment_manager(sbi);
866 if (err) {
867 f2fs_msg(sb, KERN_ERR,
868 "Failed to initialize F2FS segment manager");
869 goto free_sm;
871 err = build_node_manager(sbi);
872 if (err) {
873 f2fs_msg(sb, KERN_ERR,
874 "Failed to initialize F2FS node manager");
875 goto free_nm;
878 build_gc_manager(sbi);
880 /* get an inode for node space */
881 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
882 if (IS_ERR(sbi->node_inode)) {
883 f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
884 err = PTR_ERR(sbi->node_inode);
885 goto free_nm;
888 /* if there are nt orphan nodes free them */
889 err = -EINVAL;
890 if (recover_orphan_inodes(sbi))
891 goto free_node_inode;
893 /* read root inode and dentry */
894 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
895 if (IS_ERR(root)) {
896 f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
897 err = PTR_ERR(root);
898 goto free_node_inode;
900 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size)
901 goto free_root_inode;
903 sb->s_root = d_make_root(root); /* allocate root dentry */
904 if (!sb->s_root) {
905 err = -ENOMEM;
906 goto free_root_inode;
909 /* recover fsynced data */
910 if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
911 err = recover_fsync_data(sbi);
912 if (err)
913 f2fs_msg(sb, KERN_ERR,
914 "Cannot recover all fsync data errno=%ld", err);
918 * If filesystem is not mounted as read-only then
919 * do start the gc_thread.
921 if (!(sb->s_flags & MS_RDONLY)) {
922 /* After POR, we can run background GC thread.*/
923 err = start_gc_thread(sbi);
924 if (err)
925 goto fail;
928 err = f2fs_build_stats(sbi);
929 if (err)
930 goto fail;
932 if (f2fs_proc_root)
933 sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
935 if (sbi->s_proc)
936 proc_create_data("segment_info", S_IRUGO, sbi->s_proc,
937 &f2fs_seq_segment_info_fops, sb);
939 if (test_opt(sbi, DISCARD)) {
940 struct request_queue *q = bdev_get_queue(sb->s_bdev);
941 if (!blk_queue_discard(q))
942 f2fs_msg(sb, KERN_WARNING,
943 "mounting with \"discard\" option, but "
944 "the device does not support discard");
947 sbi->s_kobj.kset = f2fs_kset;
948 init_completion(&sbi->s_kobj_unregister);
949 err = kobject_init_and_add(&sbi->s_kobj, &f2fs_ktype, NULL,
950 "%s", sb->s_id);
951 if (err)
952 goto fail;
954 return 0;
955 fail:
956 stop_gc_thread(sbi);
957 free_root_inode:
958 dput(sb->s_root);
959 sb->s_root = NULL;
960 free_node_inode:
961 iput(sbi->node_inode);
962 free_nm:
963 destroy_node_manager(sbi);
964 free_sm:
965 destroy_segment_manager(sbi);
966 free_cp:
967 kfree(sbi->ckpt);
968 free_meta_inode:
969 make_bad_inode(sbi->meta_inode);
970 iput(sbi->meta_inode);
971 free_sb_buf:
972 brelse(raw_super_buf);
973 free_sbi:
974 kfree(sbi);
975 return err;
978 static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
979 const char *dev_name, void *data)
981 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
984 static struct file_system_type f2fs_fs_type = {
985 .owner = THIS_MODULE,
986 .name = "f2fs",
987 .mount = f2fs_mount,
988 .kill_sb = kill_block_super,
989 .fs_flags = FS_REQUIRES_DEV,
991 MODULE_ALIAS_FS("f2fs");
993 static int __init init_inodecache(void)
995 f2fs_inode_cachep = f2fs_kmem_cache_create("f2fs_inode_cache",
996 sizeof(struct f2fs_inode_info), NULL);
997 if (f2fs_inode_cachep == NULL)
998 return -ENOMEM;
999 return 0;
1002 static void destroy_inodecache(void)
1005 * Make sure all delayed rcu free inodes are flushed before we
1006 * destroy cache.
1008 rcu_barrier();
1009 kmem_cache_destroy(f2fs_inode_cachep);
1012 static int __init init_f2fs_fs(void)
1014 int err;
1016 err = init_inodecache();
1017 if (err)
1018 goto fail;
1019 err = create_node_manager_caches();
1020 if (err)
1021 goto free_inodecache;
1022 err = create_gc_caches();
1023 if (err)
1024 goto free_node_manager_caches;
1025 err = create_checkpoint_caches();
1026 if (err)
1027 goto free_gc_caches;
1028 f2fs_kset = kset_create_and_add("f2fs", NULL, fs_kobj);
1029 if (!f2fs_kset) {
1030 err = -ENOMEM;
1031 goto free_checkpoint_caches;
1033 err = register_filesystem(&f2fs_fs_type);
1034 if (err)
1035 goto free_kset;
1036 f2fs_create_root_stats();
1037 f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
1038 return 0;
1040 free_kset:
1041 kset_unregister(f2fs_kset);
1042 free_checkpoint_caches:
1043 destroy_checkpoint_caches();
1044 free_gc_caches:
1045 destroy_gc_caches();
1046 free_node_manager_caches:
1047 destroy_node_manager_caches();
1048 free_inodecache:
1049 destroy_inodecache();
1050 fail:
1051 return err;
1054 static void __exit exit_f2fs_fs(void)
1056 remove_proc_entry("fs/f2fs", NULL);
1057 f2fs_destroy_root_stats();
1058 unregister_filesystem(&f2fs_fs_type);
1059 destroy_checkpoint_caches();
1060 destroy_gc_caches();
1061 destroy_node_manager_caches();
1062 destroy_inodecache();
1063 kset_unregister(f2fs_kset);
1066 module_init(init_f2fs_fs)
1067 module_exit(exit_f2fs_fs)
1069 MODULE_AUTHOR("Samsung Electronics's Praesto Team");
1070 MODULE_DESCRIPTION("Flash Friendly File System");
1071 MODULE_LICENSE("GPL");