blob: 13d0a0fe49dd413ed70d3a10dc864d27bea5c79a [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002 * 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>
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090015#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>
Jaegeuk Kim5e176d52013-06-28 12:47:01 +090021#include <linux/proc_fs.h>
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090022#include <linux/random.h>
23#include <linux/exportfs.h>
Namjae Jeond3ee4562013-03-17 17:26:14 +090024#include <linux/blkdev.h>
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090025#include <linux/f2fs_fs.h>
Namjae Jeonb59d0ba2013-08-04 23:09:40 +090026#include <linux/sysfs.h>
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090027
28#include "f2fs.h"
29#include "node.h"
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +090030#include "segment.h"
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090031#include "xattr.h"
Namjae Jeonb59d0ba2013-08-04 23:09:40 +090032#include "gc.h"
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090033
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090034#define CREATE_TRACE_POINTS
35#include <trace/events/f2fs.h>
36
Jaegeuk Kim5e176d52013-06-28 12:47:01 +090037static struct proc_dir_entry *f2fs_proc_root;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090038static struct kmem_cache *f2fs_inode_cachep;
Namjae Jeonb59d0ba2013-08-04 23:09:40 +090039static struct kset *f2fs_kset;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090040
41enum {
Namjae Jeon696c0182013-06-16 09:48:48 +090042 Opt_gc_background,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090043 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,
Jaegeuk Kim444c5802013-08-08 15:16:22 +090050 Opt_inline_xattr,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090051 Opt_err,
52};
53
54static match_table_t f2fs_tokens = {
Namjae Jeon696c0182013-06-16 09:48:48 +090055 {Opt_gc_background, "background_gc=%s"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090056 {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"},
Jaegeuk Kim444c5802013-08-08 15:16:22 +090063 {Opt_inline_xattr, "inline_xattr"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090064 {Opt_err, NULL},
65};
66
Namjae Jeonb59d0ba2013-08-04 23:09:40 +090067/* Sysfs support for f2fs */
68struct 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;
74};
75
76static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
77 struct f2fs_sb_info *sbi, char *buf)
78{
79 struct f2fs_gc_kthread *gc_kth = sbi->gc_thread;
80 unsigned int *ui;
81
82 if (!gc_kth)
83 return -EINVAL;
84
85 ui = (unsigned int *)(((char *)gc_kth) + a->offset);
86
87 return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
88}
89
90static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
91 struct f2fs_sb_info *sbi,
92 const char *buf, size_t count)
93{
94 struct f2fs_gc_kthread *gc_kth = sbi->gc_thread;
95 unsigned long t;
96 unsigned int *ui;
97 ssize_t ret;
98
99 if (!gc_kth)
100 return -EINVAL;
101
102 ui = (unsigned int *)(((char *)gc_kth) + a->offset);
103
104 ret = kstrtoul(skip_spaces(buf), 0, &t);
105 if (ret < 0)
106 return ret;
107 *ui = t;
108 return count;
109}
110
111static ssize_t f2fs_attr_show(struct kobject *kobj,
112 struct attribute *attr, char *buf)
113{
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);
117
118 return a->show ? a->show(a, sbi, buf) : 0;
119}
120
121static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
122 const char *buf, size_t len)
123{
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);
127
128 return a->store ? a->store(a, sbi, buf, len) : 0;
129}
130
131static void f2fs_sb_release(struct kobject *kobj)
132{
133 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
134 s_kobj);
135 complete(&sbi->s_kobj_unregister);
136}
137
138#define F2FS_ATTR_OFFSET(_name, _mode, _show, _store, _elname) \
139static 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), \
144}
145
146#define F2FS_RW_ATTR(name, elname) \
147 F2FS_ATTR_OFFSET(name, 0644, f2fs_sbi_show, f2fs_sbi_store, elname)
148
149F2FS_RW_ATTR(gc_min_sleep_time, min_sleep_time);
150F2FS_RW_ATTR(gc_max_sleep_time, max_sleep_time);
151F2FS_RW_ATTR(gc_no_gc_sleep_time, no_gc_sleep_time);
Namjae Jeond2dc0952013-08-04 23:10:15 +0900152F2FS_RW_ATTR(gc_idle, gc_idle);
Namjae Jeonb59d0ba2013-08-04 23:09:40 +0900153
154#define ATTR_LIST(name) (&f2fs_attr_##name.attr)
155static 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),
Namjae Jeond2dc0952013-08-04 23:10:15 +0900159 ATTR_LIST(gc_idle),
Namjae Jeonb59d0ba2013-08-04 23:09:40 +0900160 NULL,
161};
162
163static const struct sysfs_ops f2fs_attr_ops = {
164 .show = f2fs_attr_show,
165 .store = f2fs_attr_store,
166};
167
168static struct kobj_type f2fs_ktype = {
169 .default_attrs = f2fs_attrs,
170 .sysfs_ops = &f2fs_attr_ops,
171 .release = f2fs_sb_release,
172};
173
Namjae Jeona07ef782012-12-30 14:52:05 +0900174void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
175{
176 struct va_format vaf;
177 va_list args;
178
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);
184}
185
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900186static void init_once(void *foo)
187{
188 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
189
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900190 inode_init_once(&fi->vfs_inode);
191}
192
Namjae Jeon696c0182013-06-16 09:48:48 +0900193static int parse_options(struct super_block *sb, char *options)
194{
195 struct f2fs_sb_info *sbi = F2FS_SB(sb);
196 substring_t args[MAX_OPT_ARGS];
197 char *p, *name;
198 int arg = 0;
199
200 if (!options)
201 return 0;
202
203 while ((p = strsep(&options, ",")) != NULL) {
204 int token;
205 if (!*p)
206 continue;
207 /*
208 * Initialize args struct so we know whether arg was
209 * found; some options take optional arguments.
210 */
211 args[0].to = args[0].from = NULL;
212 token = match_token(p, f2fs_tokens, args);
213
214 switch (token) {
215 case Opt_gc_background:
216 name = match_strdup(&args[0]);
217
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;
227 }
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;
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900243 case Opt_inline_xattr:
244 set_opt(sbi, INLINE_XATTR);
245 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900246#else
247 case Opt_nouser_xattr:
248 f2fs_msg(sb, KERN_INFO,
249 "nouser_xattr options not supported");
250 break;
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900251 case Opt_inline_xattr:
252 f2fs_msg(sb, KERN_INFO,
253 "inline_xattr options not supported");
254 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900255#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",
278 p);
279 return -EINVAL;
280 }
281 }
282 return 0;
283}
284
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900285static struct inode *f2fs_alloc_inode(struct super_block *sb)
286{
287 struct f2fs_inode_info *fi;
288
289 fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_NOFS | __GFP_ZERO);
290 if (!fi)
291 return NULL;
292
293 init_once((void *) fi);
294
Masanari Iida434720f2013-03-19 08:03:35 +0900295 /* Initialize f2fs-specific inode info */
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900296 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);
301
302 set_inode_flag(fi, FI_NEW_INODE);
303
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900304 if (test_opt(F2FS_SB(sb), INLINE_XATTR))
305 set_inode_flag(fi, FI_INLINE_XATTR);
306
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900307 return &fi->vfs_inode;
308}
309
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900310static int f2fs_drop_inode(struct inode *inode)
311{
312 /*
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)
318 */
319 if (!inode_unhashed(inode) && inode->i_state & I_SYNC)
320 return 0;
321 return generic_drop_inode(inode);
322}
323
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900324/*
325 * f2fs_dirty_inode() is called from __mark_inode_dirty()
326 *
327 * We should call set_dirty_inode to write the dirty inode through write_inode.
328 */
329static void f2fs_dirty_inode(struct inode *inode, int flags)
330{
331 set_inode_flag(F2FS_I(inode), FI_DIRTY_INODE);
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900332}
333
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900334static void f2fs_i_callback(struct rcu_head *head)
335{
336 struct inode *inode = container_of(head, struct inode, i_rcu);
337 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
338}
339
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900340static void f2fs_destroy_inode(struct inode *inode)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900341{
342 call_rcu(&inode->i_rcu, f2fs_i_callback);
343}
344
345static void f2fs_put_super(struct super_block *sb)
346{
347 struct f2fs_sb_info *sbi = F2FS_SB(sb);
348
Jaegeuk Kim5e176d52013-06-28 12:47:01 +0900349 if (sbi->s_proc) {
350 remove_proc_entry("segment_info", sbi->s_proc);
351 remove_proc_entry(sb->s_id, f2fs_proc_root);
352 }
Namjae Jeonb59d0ba2013-08-04 23:09:40 +0900353 kobject_del(&sbi->s_kobj);
Jaegeuk Kim5e176d52013-06-28 12:47:01 +0900354
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900355 f2fs_destroy_stats(sbi);
356 stop_gc_thread(sbi);
357
Jaegeuk Kim43727522013-02-04 15:11:17 +0900358 write_checkpoint(sbi, true);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900359
360 iput(sbi->node_inode);
361 iput(sbi->meta_inode);
362
363 /* destroy f2fs internal modules */
364 destroy_node_manager(sbi);
365 destroy_segment_manager(sbi);
366
367 kfree(sbi->ckpt);
Namjae Jeonb59d0ba2013-08-04 23:09:40 +0900368 kobject_put(&sbi->s_kobj);
369 wait_for_completion(&sbi->s_kobj_unregister);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900370
371 sb->s_fs_info = NULL;
372 brelse(sbi->raw_super_buf);
373 kfree(sbi);
374}
375
376int f2fs_sync_fs(struct super_block *sb, int sync)
377{
378 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900379
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900380 trace_f2fs_sync_fs(sb, sync);
381
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900382 if (!sbi->s_dirty && !get_pages(sbi, F2FS_DIRTY_NODES))
383 return 0;
384
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900385 if (sync) {
386 mutex_lock(&sbi->gc_mutex);
Jaegeuk Kim43727522013-02-04 15:11:17 +0900387 write_checkpoint(sbi, false);
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900388 mutex_unlock(&sbi->gc_mutex);
389 } else {
Jaegeuk Kim7d82db82013-01-11 13:10:49 +0900390 f2fs_balance_fs(sbi);
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900391 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900392
Namjae Jeon64c576f2012-12-22 12:10:27 +0900393 return 0;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900394}
395
Changman Leed6212a52013-01-29 18:30:07 +0900396static int f2fs_freeze(struct super_block *sb)
397{
398 int err;
399
Jaegeuk Kim77888c12013-05-20 20:28:47 +0900400 if (f2fs_readonly(sb))
Changman Leed6212a52013-01-29 18:30:07 +0900401 return 0;
402
403 err = f2fs_sync_fs(sb, 1);
404 return err;
405}
406
407static int f2fs_unfreeze(struct super_block *sb)
408{
409 return 0;
410}
411
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900412static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
413{
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;
418
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;
425
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);
429
Jaegeuk Kim1362b5e2012-12-12 19:45:49 +0900430 buf->f_files = sbi->total_node_count;
431 buf->f_ffree = sbi->total_node_count - valid_inode_count(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900432
Jaegeuk Kim5a20d332013-03-03 13:58:05 +0900433 buf->f_namelen = F2FS_NAME_LEN;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900434 buf->f_fsid.val[0] = (u32)id;
435 buf->f_fsid.val[1] = (u32)(id >> 32);
436
437 return 0;
438}
439
440static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
441{
442 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
443
Namjae Jeon696c0182013-06-16 09:48:48 +0900444 if (!(root->d_sb->s_flags & MS_RDONLY) && test_opt(sbi, BG_GC))
445 seq_printf(seq, ",background_gc=%s", "on");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900446 else
Namjae Jeon696c0182013-06-16 09:48:48 +0900447 seq_printf(seq, ",background_gc=%s", "off");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900448 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");
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900459 if (test_opt(sbi, INLINE_XATTR))
460 seq_puts(seq, ",inline_xattr");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900461#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))
Alejandro Martinez Ruizaa435072013-01-25 19:08:59 +0100469 seq_puts(seq, ",disable_ext_identify");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900470
471 seq_printf(seq, ",active_logs=%u", sbi->active_logs);
472
473 return 0;
474}
475
Jaegeuk Kim5e176d52013-06-28 12:47:01 +0900476static int segment_info_seq_show(struct seq_file *seq, void *offset)
477{
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;
482
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, " ");
489 }
490 return 0;
491}
492
493static int segment_info_open_fs(struct inode *inode, struct file *file)
494{
495 return single_open(file, segment_info_seq_show, PDE_DATA(inode));
496}
497
498static 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,
504};
505
Namjae Jeon696c0182013-06-16 09:48:48 +0900506static int f2fs_remount(struct super_block *sb, int *flags, char *data)
507{
508 struct f2fs_sb_info *sbi = F2FS_SB(sb);
509 struct f2fs_mount_info org_mount_opt;
510 int err, active_logs;
511
512 /*
513 * Save the old mount options in case we
514 * need to restore them.
515 */
516 org_mount_opt = sbi->mount_opt;
517 active_logs = sbi->active_logs;
518
519 /* parse mount options */
520 err = parse_options(sb, data);
521 if (err)
522 goto restore_opts;
523
524 /*
525 * Previous and new state of filesystem is RO,
526 * so no point in checking GC conditions.
527 */
528 if ((sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY))
529 goto skip;
530
531 /*
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.
535 */
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);
540 }
541 } else if (test_opt(sbi, BG_GC) && !sbi->gc_thread) {
542 err = start_gc_thread(sbi);
543 if (err)
544 goto restore_opts;
545 }
546skip:
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;
551
552restore_opts:
553 sbi->mount_opt = org_mount_opt;
554 sbi->active_logs = active_logs;
555 return err;
556}
557
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900558static struct super_operations f2fs_sops = {
559 .alloc_inode = f2fs_alloc_inode,
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900560 .drop_inode = f2fs_drop_inode,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900561 .destroy_inode = f2fs_destroy_inode,
562 .write_inode = f2fs_write_inode,
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900563 .dirty_inode = f2fs_dirty_inode,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900564 .show_options = f2fs_show_options,
565 .evict_inode = f2fs_evict_inode,
566 .put_super = f2fs_put_super,
567 .sync_fs = f2fs_sync_fs,
Changman Leed6212a52013-01-29 18:30:07 +0900568 .freeze_fs = f2fs_freeze,
569 .unfreeze_fs = f2fs_unfreeze,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900570 .statfs = f2fs_statfs,
Namjae Jeon696c0182013-06-16 09:48:48 +0900571 .remount_fs = f2fs_remount,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900572};
573
574static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
575 u64 ino, u32 generation)
576{
577 struct f2fs_sb_info *sbi = F2FS_SB(sb);
578 struct inode *inode;
579
580 if (ino < F2FS_ROOT_INO(sbi))
581 return ERR_PTR(-ESTALE);
582
583 /*
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.
587 */
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);
595 }
596 return inode;
597}
598
599static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
600 int fh_len, int fh_type)
601{
602 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
603 f2fs_nfs_get_inode);
604}
605
606static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
607 int fh_len, int fh_type)
608{
609 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
610 f2fs_nfs_get_inode);
611}
612
613static 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,
617};
618
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900619static loff_t max_file_size(unsigned bits)
620{
Jaegeuk Kimde936532013-08-12 21:08:03 +0900621 loff_t result = (DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900622 loff_t leaf_count = ADDRS_PER_BLOCK;
623
624 /* two direct node blocks */
625 result += (leaf_count * 2);
626
627 /* two indirect node blocks */
628 leaf_count *= NIDS_PER_BLOCK;
629 result += (leaf_count * 2);
630
631 /* one double indirect node block */
632 leaf_count *= NIDS_PER_BLOCK;
633 result += leaf_count;
634
635 result <<= bits;
636 return result;
637}
638
Namjae Jeona07ef782012-12-30 14:52:05 +0900639static int sanity_check_raw_super(struct super_block *sb,
640 struct f2fs_super_block *raw_super)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900641{
642 unsigned int blocksize;
643
Namjae Jeona07ef782012-12-30 14:52:05 +0900644 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));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900648 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900649 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900650
majianpeng5c9b4692013-02-01 19:07:57 +0800651 /* Currently, support only 4KB page cache size */
652 if (F2FS_BLKSIZE != PAGE_CACHE_SIZE) {
653 f2fs_msg(sb, KERN_INFO,
majianpeng14d7e9d2013-02-01 19:07:03 +0800654 "Invalid page_cache_size (%lu), supports only 4KB\n",
majianpeng5c9b4692013-02-01 19:07:57 +0800655 PAGE_CACHE_SIZE);
656 return 1;
657 }
658
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900659 /* Currently, support only 4KB block size */
660 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
majianpeng5c9b4692013-02-01 19:07:57 +0800661 if (blocksize != F2FS_BLKSIZE) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900662 f2fs_msg(sb, KERN_INFO,
663 "Invalid blocksize (%u), supports only 4KB\n",
664 blocksize);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900665 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900666 }
majianpeng5c9b4692013-02-01 19:07:57 +0800667
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900668 if (le32_to_cpu(raw_super->log_sectorsize) !=
Namjae Jeona07ef782012-12-30 14:52:05 +0900669 F2FS_LOG_SECTOR_SIZE) {
670 f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900671 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900672 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900673 if (le32_to_cpu(raw_super->log_sectors_per_block) !=
Namjae Jeona07ef782012-12-30 14:52:05 +0900674 F2FS_LOG_SECTORS_PER_BLOCK) {
675 f2fs_msg(sb, KERN_INFO, "Invalid log sectors per block");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900676 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900677 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900678 return 0;
679}
680
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900681static int sanity_check_ckpt(struct f2fs_sb_info *sbi)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900682{
683 unsigned int total, fsmeta;
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900684 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
685 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900686
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);
693
694 if (fsmeta >= total)
695 return 1;
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900696
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;
700 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900701 return 0;
702}
703
704static void init_sb_info(struct f2fs_sb_info *sbi)
705{
706 struct f2fs_super_block *raw_super = sbi->raw_super;
707 int i;
708
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);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900724 sbi->cur_victim_sec = NULL_SECNO;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900725
726 for (i = 0; i < NR_COUNT_TYPE; i++)
727 atomic_set(&sbi->nr_pages[i], 0);
728}
729
majianpeng14d7e9d2013-02-01 19:07:03 +0800730static int validate_superblock(struct super_block *sb,
731 struct f2fs_super_block **raw_super,
732 struct buffer_head **raw_super_buf, sector_t block)
733{
734 const char *super = (block == 0 ? "first" : "second");
735
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);
Namjae Jeonc0d39e62013-03-17 17:26:53 +0900741 return -EIO;
majianpeng14d7e9d2013-02-01 19:07:03 +0800742 }
743
744 *raw_super = (struct f2fs_super_block *)
745 ((char *)(*raw_super_buf)->b_data + F2FS_SUPER_OFFSET);
746
747 /* sanity checking of raw super */
748 if (!sanity_check_raw_super(sb, *raw_super))
749 return 0;
750
751 f2fs_msg(sb, KERN_ERR, "Can't find a valid F2FS filesystem "
752 "in %s superblock", super);
Namjae Jeonc0d39e62013-03-17 17:26:53 +0900753 return -EINVAL;
majianpeng14d7e9d2013-02-01 19:07:03 +0800754}
755
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900756static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
757{
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;
764
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;
769
Namjae Jeonff9234a2013-01-12 14:41:13 +0900770 /* set a block size */
Namjae Jeona07ef782012-12-30 14:52:05 +0900771 if (!sb_set_blocksize(sb, F2FS_BLKSIZE)) {
772 f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900773 goto free_sbi;
Namjae Jeona07ef782012-12-30 14:52:05 +0900774 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900775
Namjae Jeonc0d39e62013-03-17 17:26:53 +0900776 err = validate_superblock(sb, &raw_super, &raw_super_buf, 0);
777 if (err) {
majianpeng14d7e9d2013-02-01 19:07:03 +0800778 brelse(raw_super_buf);
Namjae Jeonc0d39e62013-03-17 17:26:53 +0900779 /* check secondary superblock when primary failed */
780 err = validate_superblock(sb, &raw_super, &raw_super_buf, 1);
781 if (err)
majianpeng14d7e9d2013-02-01 19:07:03 +0800782 goto free_sb_buf;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900783 }
Gu Zheng5fb08372013-06-07 14:16:53 +0800784 sb->s_fs_info = sbi;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900785 /* init some FS parameters */
786 sbi->active_logs = NR_CURSEG_TYPE;
787
788 set_opt(sbi, BG_GC);
789
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 */
Gu Zheng5fb08372013-06-07 14:16:53 +0800797 err = parse_options(sb, (char *)data);
Wei Yongjun66348b72013-04-12 10:23:18 +0800798 if (err)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900799 goto free_sb_buf;
800
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900801 sb->s_maxbytes = max_file_size(le32_to_cpu(raw_super->log_blocksize));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900802 sb->s_max_links = F2FS_LINK_MAX;
803 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
804
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;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900809 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));
813
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);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900819 mutex_init(&sbi->writepages);
820 mutex_init(&sbi->cp_mutex);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900821 for (i = 0; i < NR_GLOBAL_LOCKS; i++)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900822 mutex_init(&sbi->fs_lock[i]);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900823 mutex_init(&sbi->node_write);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900824 sbi->por_doing = 0;
825 spin_lock_init(&sbi->stat_lock);
826 init_rwsem(&sbi->bio_sem);
827 init_sb_info(sbi);
828
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)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900832 f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900833 err = PTR_ERR(sbi->meta_inode);
834 goto free_sb_buf;
835 }
836
837 err = get_valid_checkpoint(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +0900838 if (err) {
839 f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900840 goto free_meta_inode;
Namjae Jeona07ef782012-12-30 14:52:05 +0900841 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900842
843 /* sanity checking of checkpoint */
844 err = -EINVAL;
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900845 if (sanity_check_ckpt(sbi)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900846 f2fs_msg(sb, KERN_ERR, "Invalid F2FS checkpoint");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900847 goto free_cp;
Namjae Jeona07ef782012-12-30 14:52:05 +0900848 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900849
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);
861
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900862 init_orphan_info(sbi);
863
864 /* setup f2fs internal modules */
865 err = build_segment_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +0900866 if (err) {
867 f2fs_msg(sb, KERN_ERR,
868 "Failed to initialize F2FS segment manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900869 goto free_sm;
Namjae Jeona07ef782012-12-30 14:52:05 +0900870 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900871 err = build_node_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +0900872 if (err) {
873 f2fs_msg(sb, KERN_ERR,
874 "Failed to initialize F2FS node manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900875 goto free_nm;
Namjae Jeona07ef782012-12-30 14:52:05 +0900876 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900877
878 build_gc_manager(sbi);
879
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)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900883 f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900884 err = PTR_ERR(sbi->node_inode);
885 goto free_nm;
886 }
887
888 /* if there are nt orphan nodes free them */
889 err = -EINVAL;
Jaegeuk Kim30f0c752012-12-19 16:09:19 +0900890 if (recover_orphan_inodes(sbi))
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900891 goto free_node_inode;
892
893 /* read root inode and dentry */
894 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
895 if (IS_ERR(root)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900896 f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900897 err = PTR_ERR(root);
898 goto free_node_inode;
899 }
900 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size)
901 goto free_root_inode;
902
903 sb->s_root = d_make_root(root); /* allocate root dentry */
904 if (!sb->s_root) {
905 err = -ENOMEM;
906 goto free_root_inode;
907 }
908
909 /* recover fsynced data */
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900910 if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
911 err = recover_fsync_data(sbi);
Chris Friesbde582b2013-05-02 16:07:34 -0500912 if (err)
913 f2fs_msg(sb, KERN_ERR,
914 "Cannot recover all fsync data errno=%ld", err);
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900915 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900916
Namjae Jeon696c0182013-06-16 09:48:48 +0900917 /*
918 * If filesystem is not mounted as read-only then
919 * do start the gc_thread.
920 */
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;
926 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900927
928 err = f2fs_build_stats(sbi);
929 if (err)
930 goto fail;
931
Jaegeuk Kim5e176d52013-06-28 12:47:01 +0900932 if (f2fs_proc_root)
933 sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
934
935 if (sbi->s_proc)
936 proc_create_data("segment_info", S_IRUGO, sbi->s_proc,
937 &f2fs_seq_segment_info_fops, sb);
938
Namjae Jeond3ee4562013-03-17 17:26:14 +0900939 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");
945 }
946
Namjae Jeonb59d0ba2013-08-04 23:09:40 +0900947 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;
953
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900954 return 0;
955fail:
956 stop_gc_thread(sbi);
957free_root_inode:
958 dput(sb->s_root);
959 sb->s_root = NULL;
960free_node_inode:
961 iput(sbi->node_inode);
962free_nm:
963 destroy_node_manager(sbi);
964free_sm:
965 destroy_segment_manager(sbi);
966free_cp:
967 kfree(sbi->ckpt);
968free_meta_inode:
969 make_bad_inode(sbi->meta_inode);
970 iput(sbi->meta_inode);
971free_sb_buf:
972 brelse(raw_super_buf);
973free_sbi:
974 kfree(sbi);
975 return err;
976}
977
978static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
979 const char *dev_name, void *data)
980{
981 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
982}
983
984static 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,
990};
Eric W. Biederman7f78e032013-03-02 19:39:14 -0800991MODULE_ALIAS_FS("f2fs");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900992
Namjae Jeon6e6093a2013-01-17 00:08:30 +0900993static int __init init_inodecache(void)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900994{
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;
1000}
1001
1002static void destroy_inodecache(void)
1003{
1004 /*
1005 * Make sure all delayed rcu free inodes are flushed before we
1006 * destroy cache.
1007 */
1008 rcu_barrier();
1009 kmem_cache_destroy(f2fs_inode_cachep);
1010}
1011
1012static int __init init_f2fs_fs(void)
1013{
1014 int err;
1015
1016 err = init_inodecache();
1017 if (err)
1018 goto fail;
1019 err = create_node_manager_caches();
1020 if (err)
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08001021 goto free_inodecache;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001022 err = create_gc_caches();
1023 if (err)
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08001024 goto free_node_manager_caches;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001025 err = create_checkpoint_caches();
1026 if (err)
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08001027 goto free_gc_caches;
Namjae Jeonb59d0ba2013-08-04 23:09:40 +09001028 f2fs_kset = kset_create_and_add("f2fs", NULL, fs_kobj);
Wei Yongjun6e6b9782013-08-23 08:30:20 +08001029 if (!f2fs_kset) {
1030 err = -ENOMEM;
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08001031 goto free_checkpoint_caches;
Wei Yongjun6e6b9782013-08-23 08:30:20 +08001032 }
Namjae Jeon4589d252013-01-15 19:58:47 +09001033 err = register_filesystem(&f2fs_fs_type);
1034 if (err)
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08001035 goto free_kset;
Namjae Jeon4589d252013-01-15 19:58:47 +09001036 f2fs_create_root_stats();
Jaegeuk Kim5e176d52013-06-28 12:47:01 +09001037 f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08001038 return 0;
1039
1040free_kset:
1041 kset_unregister(f2fs_kset);
1042free_checkpoint_caches:
1043 destroy_checkpoint_caches();
1044free_gc_caches:
1045 destroy_gc_caches();
1046free_node_manager_caches:
1047 destroy_node_manager_caches();
1048free_inodecache:
1049 destroy_inodecache();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001050fail:
1051 return err;
1052}
1053
1054static void __exit exit_f2fs_fs(void)
1055{
Jaegeuk Kim5e176d52013-06-28 12:47:01 +09001056 remove_proc_entry("fs/f2fs", NULL);
Namjae Jeon4589d252013-01-15 19:58:47 +09001057 f2fs_destroy_root_stats();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001058 unregister_filesystem(&f2fs_fs_type);
1059 destroy_checkpoint_caches();
1060 destroy_gc_caches();
1061 destroy_node_manager_caches();
1062 destroy_inodecache();
Namjae Jeonb59d0ba2013-08-04 23:09:40 +09001063 kset_unregister(f2fs_kset);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001064}
1065
1066module_init(init_f2fs_fs)
1067module_exit(exit_f2fs_fs)
1068
1069MODULE_AUTHOR("Samsung Electronics's Praesto Team");
1070MODULE_DESCRIPTION("Flash Friendly File System");
1071MODULE_LICENSE("GPL");