blob: fde8e6aca9be077d6f8c3f44eb02e4cf09fd013d [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,
Kelly Anderson4058c512013-10-07 11:36:20 +090046 Opt_user_xattr,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090047 Opt_nouser_xattr,
Kelly Anderson4058c512013-10-07 11:36:20 +090048 Opt_acl,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090049 Opt_noacl,
50 Opt_active_logs,
51 Opt_disable_ext_identify,
Jaegeuk Kim444c5802013-08-08 15:16:22 +090052 Opt_inline_xattr,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090053 Opt_err,
54};
55
56static match_table_t f2fs_tokens = {
Namjae Jeon696c0182013-06-16 09:48:48 +090057 {Opt_gc_background, "background_gc=%s"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090058 {Opt_disable_roll_forward, "disable_roll_forward"},
59 {Opt_discard, "discard"},
60 {Opt_noheap, "no_heap"},
Kelly Anderson4058c512013-10-07 11:36:20 +090061 {Opt_user_xattr, "user_xattr"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090062 {Opt_nouser_xattr, "nouser_xattr"},
Kelly Anderson4058c512013-10-07 11:36:20 +090063 {Opt_acl, "acl"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090064 {Opt_noacl, "noacl"},
65 {Opt_active_logs, "active_logs=%u"},
66 {Opt_disable_ext_identify, "disable_ext_identify"},
Jaegeuk Kim444c5802013-08-08 15:16:22 +090067 {Opt_inline_xattr, "inline_xattr"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090068 {Opt_err, NULL},
69};
70
Namjae Jeonb59d0ba2013-08-04 23:09:40 +090071/* Sysfs support for f2fs */
72struct f2fs_attr {
73 struct attribute attr;
74 ssize_t (*show)(struct f2fs_attr *, struct f2fs_sb_info *, char *);
75 ssize_t (*store)(struct f2fs_attr *, struct f2fs_sb_info *,
76 const char *, size_t);
77 int offset;
78};
79
80static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
81 struct f2fs_sb_info *sbi, char *buf)
82{
83 struct f2fs_gc_kthread *gc_kth = sbi->gc_thread;
84 unsigned int *ui;
85
86 if (!gc_kth)
87 return -EINVAL;
88
89 ui = (unsigned int *)(((char *)gc_kth) + a->offset);
90
91 return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
92}
93
94static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
95 struct f2fs_sb_info *sbi,
96 const char *buf, size_t count)
97{
98 struct f2fs_gc_kthread *gc_kth = sbi->gc_thread;
99 unsigned long t;
100 unsigned int *ui;
101 ssize_t ret;
102
103 if (!gc_kth)
104 return -EINVAL;
105
106 ui = (unsigned int *)(((char *)gc_kth) + a->offset);
107
108 ret = kstrtoul(skip_spaces(buf), 0, &t);
109 if (ret < 0)
110 return ret;
111 *ui = t;
112 return count;
113}
114
115static ssize_t f2fs_attr_show(struct kobject *kobj,
116 struct attribute *attr, char *buf)
117{
118 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
119 s_kobj);
120 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
121
122 return a->show ? a->show(a, sbi, buf) : 0;
123}
124
125static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
126 const char *buf, size_t len)
127{
128 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
129 s_kobj);
130 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
131
132 return a->store ? a->store(a, sbi, buf, len) : 0;
133}
134
135static void f2fs_sb_release(struct kobject *kobj)
136{
137 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
138 s_kobj);
139 complete(&sbi->s_kobj_unregister);
140}
141
142#define F2FS_ATTR_OFFSET(_name, _mode, _show, _store, _elname) \
143static struct f2fs_attr f2fs_attr_##_name = { \
144 .attr = {.name = __stringify(_name), .mode = _mode }, \
145 .show = _show, \
146 .store = _store, \
147 .offset = offsetof(struct f2fs_gc_kthread, _elname), \
148}
149
150#define F2FS_RW_ATTR(name, elname) \
151 F2FS_ATTR_OFFSET(name, 0644, f2fs_sbi_show, f2fs_sbi_store, elname)
152
153F2FS_RW_ATTR(gc_min_sleep_time, min_sleep_time);
154F2FS_RW_ATTR(gc_max_sleep_time, max_sleep_time);
155F2FS_RW_ATTR(gc_no_gc_sleep_time, no_gc_sleep_time);
Namjae Jeond2dc0952013-08-04 23:10:15 +0900156F2FS_RW_ATTR(gc_idle, gc_idle);
Namjae Jeonb59d0ba2013-08-04 23:09:40 +0900157
158#define ATTR_LIST(name) (&f2fs_attr_##name.attr)
159static struct attribute *f2fs_attrs[] = {
160 ATTR_LIST(gc_min_sleep_time),
161 ATTR_LIST(gc_max_sleep_time),
162 ATTR_LIST(gc_no_gc_sleep_time),
Namjae Jeond2dc0952013-08-04 23:10:15 +0900163 ATTR_LIST(gc_idle),
Namjae Jeonb59d0ba2013-08-04 23:09:40 +0900164 NULL,
165};
166
167static const struct sysfs_ops f2fs_attr_ops = {
168 .show = f2fs_attr_show,
169 .store = f2fs_attr_store,
170};
171
172static struct kobj_type f2fs_ktype = {
173 .default_attrs = f2fs_attrs,
174 .sysfs_ops = &f2fs_attr_ops,
175 .release = f2fs_sb_release,
176};
177
Namjae Jeona07ef782012-12-30 14:52:05 +0900178void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
179{
180 struct va_format vaf;
181 va_list args;
182
183 va_start(args, fmt);
184 vaf.fmt = fmt;
185 vaf.va = &args;
186 printk("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
187 va_end(args);
188}
189
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900190static void init_once(void *foo)
191{
192 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
193
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900194 inode_init_once(&fi->vfs_inode);
195}
196
Namjae Jeon696c0182013-06-16 09:48:48 +0900197static int parse_options(struct super_block *sb, char *options)
198{
199 struct f2fs_sb_info *sbi = F2FS_SB(sb);
200 substring_t args[MAX_OPT_ARGS];
201 char *p, *name;
202 int arg = 0;
203
204 if (!options)
205 return 0;
206
207 while ((p = strsep(&options, ",")) != NULL) {
208 int token;
209 if (!*p)
210 continue;
211 /*
212 * Initialize args struct so we know whether arg was
213 * found; some options take optional arguments.
214 */
215 args[0].to = args[0].from = NULL;
216 token = match_token(p, f2fs_tokens, args);
217
218 switch (token) {
219 case Opt_gc_background:
220 name = match_strdup(&args[0]);
221
222 if (!name)
223 return -ENOMEM;
224 if (!strncmp(name, "on", 2))
225 set_opt(sbi, BG_GC);
226 else if (!strncmp(name, "off", 3))
227 clear_opt(sbi, BG_GC);
228 else {
229 kfree(name);
230 return -EINVAL;
231 }
232 kfree(name);
233 break;
234 case Opt_disable_roll_forward:
235 set_opt(sbi, DISABLE_ROLL_FORWARD);
236 break;
237 case Opt_discard:
238 set_opt(sbi, DISCARD);
239 break;
240 case Opt_noheap:
241 set_opt(sbi, NOHEAP);
242 break;
243#ifdef CONFIG_F2FS_FS_XATTR
Kelly Anderson4058c512013-10-07 11:36:20 +0900244 case Opt_user_xattr:
245 set_opt(sbi, XATTR_USER);
246 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900247 case Opt_nouser_xattr:
248 clear_opt(sbi, XATTR_USER);
249 break;
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900250 case Opt_inline_xattr:
251 set_opt(sbi, INLINE_XATTR);
252 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900253#else
Kelly Anderson4058c512013-10-07 11:36:20 +0900254 case Opt_user_xattr:
255 f2fs_msg(sb, KERN_INFO,
256 "user_xattr options not supported");
257 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900258 case Opt_nouser_xattr:
259 f2fs_msg(sb, KERN_INFO,
260 "nouser_xattr options not supported");
261 break;
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900262 case Opt_inline_xattr:
263 f2fs_msg(sb, KERN_INFO,
264 "inline_xattr options not supported");
265 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900266#endif
267#ifdef CONFIG_F2FS_FS_POSIX_ACL
Kelly Anderson4058c512013-10-07 11:36:20 +0900268 case Opt_acl:
269 set_opt(sbi, POSIX_ACL);
270 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900271 case Opt_noacl:
272 clear_opt(sbi, POSIX_ACL);
273 break;
274#else
Kelly Anderson4058c512013-10-07 11:36:20 +0900275 case Opt_acl:
276 f2fs_msg(sb, KERN_INFO, "acl options not supported");
277 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900278 case Opt_noacl:
279 f2fs_msg(sb, KERN_INFO, "noacl options not supported");
280 break;
281#endif
282 case Opt_active_logs:
283 if (args->from && match_int(args, &arg))
284 return -EINVAL;
285 if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
286 return -EINVAL;
287 sbi->active_logs = arg;
288 break;
289 case Opt_disable_ext_identify:
290 set_opt(sbi, DISABLE_EXT_IDENTIFY);
291 break;
292 default:
293 f2fs_msg(sb, KERN_ERR,
294 "Unrecognized mount option \"%s\" or missing value",
295 p);
296 return -EINVAL;
297 }
298 }
299 return 0;
300}
301
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900302static struct inode *f2fs_alloc_inode(struct super_block *sb)
303{
304 struct f2fs_inode_info *fi;
305
306 fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_NOFS | __GFP_ZERO);
307 if (!fi)
308 return NULL;
309
310 init_once((void *) fi);
311
Masanari Iida434720f2013-03-19 08:03:35 +0900312 /* Initialize f2fs-specific inode info */
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900313 fi->vfs_inode.i_version = 1;
314 atomic_set(&fi->dirty_dents, 0);
315 fi->i_current_depth = 1;
316 fi->i_advise = 0;
317 rwlock_init(&fi->ext.ext_lock);
318
319 set_inode_flag(fi, FI_NEW_INODE);
320
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900321 if (test_opt(F2FS_SB(sb), INLINE_XATTR))
322 set_inode_flag(fi, FI_INLINE_XATTR);
323
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900324 return &fi->vfs_inode;
325}
326
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900327static int f2fs_drop_inode(struct inode *inode)
328{
329 /*
330 * This is to avoid a deadlock condition like below.
331 * writeback_single_inode(inode)
332 * - f2fs_write_data_page
333 * - f2fs_gc -> iput -> evict
334 * - inode_wait_for_writeback(inode)
335 */
336 if (!inode_unhashed(inode) && inode->i_state & I_SYNC)
337 return 0;
338 return generic_drop_inode(inode);
339}
340
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900341/*
342 * f2fs_dirty_inode() is called from __mark_inode_dirty()
343 *
344 * We should call set_dirty_inode to write the dirty inode through write_inode.
345 */
346static void f2fs_dirty_inode(struct inode *inode, int flags)
347{
348 set_inode_flag(F2FS_I(inode), FI_DIRTY_INODE);
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900349}
350
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900351static void f2fs_i_callback(struct rcu_head *head)
352{
353 struct inode *inode = container_of(head, struct inode, i_rcu);
354 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
355}
356
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900357static void f2fs_destroy_inode(struct inode *inode)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900358{
359 call_rcu(&inode->i_rcu, f2fs_i_callback);
360}
361
362static void f2fs_put_super(struct super_block *sb)
363{
364 struct f2fs_sb_info *sbi = F2FS_SB(sb);
365
Jaegeuk Kim5e176d52013-06-28 12:47:01 +0900366 if (sbi->s_proc) {
367 remove_proc_entry("segment_info", sbi->s_proc);
368 remove_proc_entry(sb->s_id, f2fs_proc_root);
369 }
Namjae Jeonb59d0ba2013-08-04 23:09:40 +0900370 kobject_del(&sbi->s_kobj);
Jaegeuk Kim5e176d52013-06-28 12:47:01 +0900371
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900372 f2fs_destroy_stats(sbi);
373 stop_gc_thread(sbi);
374
Jaegeuk Kim43727522013-02-04 15:11:17 +0900375 write_checkpoint(sbi, true);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900376
377 iput(sbi->node_inode);
378 iput(sbi->meta_inode);
379
380 /* destroy f2fs internal modules */
381 destroy_node_manager(sbi);
382 destroy_segment_manager(sbi);
383
384 kfree(sbi->ckpt);
Namjae Jeonb59d0ba2013-08-04 23:09:40 +0900385 kobject_put(&sbi->s_kobj);
386 wait_for_completion(&sbi->s_kobj_unregister);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900387
388 sb->s_fs_info = NULL;
389 brelse(sbi->raw_super_buf);
390 kfree(sbi);
391}
392
393int f2fs_sync_fs(struct super_block *sb, int sync)
394{
395 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900396
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900397 trace_f2fs_sync_fs(sb, sync);
398
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900399 if (!sbi->s_dirty && !get_pages(sbi, F2FS_DIRTY_NODES))
400 return 0;
401
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900402 if (sync) {
403 mutex_lock(&sbi->gc_mutex);
Jaegeuk Kim43727522013-02-04 15:11:17 +0900404 write_checkpoint(sbi, false);
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900405 mutex_unlock(&sbi->gc_mutex);
406 } else {
Jaegeuk Kim7d82db82013-01-11 13:10:49 +0900407 f2fs_balance_fs(sbi);
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900408 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900409
Namjae Jeon64c576f2012-12-22 12:10:27 +0900410 return 0;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900411}
412
Changman Leed6212a52013-01-29 18:30:07 +0900413static int f2fs_freeze(struct super_block *sb)
414{
415 int err;
416
Jaegeuk Kim77888c12013-05-20 20:28:47 +0900417 if (f2fs_readonly(sb))
Changman Leed6212a52013-01-29 18:30:07 +0900418 return 0;
419
420 err = f2fs_sync_fs(sb, 1);
421 return err;
422}
423
424static int f2fs_unfreeze(struct super_block *sb)
425{
426 return 0;
427}
428
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900429static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
430{
431 struct super_block *sb = dentry->d_sb;
432 struct f2fs_sb_info *sbi = F2FS_SB(sb);
433 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
434 block_t total_count, user_block_count, start_count, ovp_count;
435
436 total_count = le64_to_cpu(sbi->raw_super->block_count);
437 user_block_count = sbi->user_block_count;
438 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
439 ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg;
440 buf->f_type = F2FS_SUPER_MAGIC;
441 buf->f_bsize = sbi->blocksize;
442
443 buf->f_blocks = total_count - start_count;
444 buf->f_bfree = buf->f_blocks - valid_user_blocks(sbi) - ovp_count;
445 buf->f_bavail = user_block_count - valid_user_blocks(sbi);
446
Jaegeuk Kim1362b5e2012-12-12 19:45:49 +0900447 buf->f_files = sbi->total_node_count;
448 buf->f_ffree = sbi->total_node_count - valid_inode_count(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900449
Jaegeuk Kim5a20d332013-03-03 13:58:05 +0900450 buf->f_namelen = F2FS_NAME_LEN;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900451 buf->f_fsid.val[0] = (u32)id;
452 buf->f_fsid.val[1] = (u32)(id >> 32);
453
454 return 0;
455}
456
457static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
458{
459 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
460
Namjae Jeon696c0182013-06-16 09:48:48 +0900461 if (!(root->d_sb->s_flags & MS_RDONLY) && test_opt(sbi, BG_GC))
462 seq_printf(seq, ",background_gc=%s", "on");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900463 else
Namjae Jeon696c0182013-06-16 09:48:48 +0900464 seq_printf(seq, ",background_gc=%s", "off");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900465 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
466 seq_puts(seq, ",disable_roll_forward");
467 if (test_opt(sbi, DISCARD))
468 seq_puts(seq, ",discard");
469 if (test_opt(sbi, NOHEAP))
470 seq_puts(seq, ",no_heap_alloc");
471#ifdef CONFIG_F2FS_FS_XATTR
472 if (test_opt(sbi, XATTR_USER))
473 seq_puts(seq, ",user_xattr");
474 else
475 seq_puts(seq, ",nouser_xattr");
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900476 if (test_opt(sbi, INLINE_XATTR))
477 seq_puts(seq, ",inline_xattr");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900478#endif
479#ifdef CONFIG_F2FS_FS_POSIX_ACL
480 if (test_opt(sbi, POSIX_ACL))
481 seq_puts(seq, ",acl");
482 else
483 seq_puts(seq, ",noacl");
484#endif
485 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
Alejandro Martinez Ruizaa435072013-01-25 19:08:59 +0100486 seq_puts(seq, ",disable_ext_identify");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900487
488 seq_printf(seq, ",active_logs=%u", sbi->active_logs);
489
490 return 0;
491}
492
Jaegeuk Kim5e176d52013-06-28 12:47:01 +0900493static int segment_info_seq_show(struct seq_file *seq, void *offset)
494{
495 struct super_block *sb = seq->private;
496 struct f2fs_sb_info *sbi = F2FS_SB(sb);
497 unsigned int total_segs = le32_to_cpu(sbi->raw_super->segment_count_main);
498 int i;
499
500 for (i = 0; i < total_segs; i++) {
501 seq_printf(seq, "%u", get_valid_blocks(sbi, i, 1));
502 if (i != 0 && (i % 10) == 0)
503 seq_puts(seq, "\n");
504 else
505 seq_puts(seq, " ");
506 }
507 return 0;
508}
509
510static int segment_info_open_fs(struct inode *inode, struct file *file)
511{
512 return single_open(file, segment_info_seq_show, PDE_DATA(inode));
513}
514
515static const struct file_operations f2fs_seq_segment_info_fops = {
516 .owner = THIS_MODULE,
517 .open = segment_info_open_fs,
518 .read = seq_read,
519 .llseek = seq_lseek,
520 .release = single_release,
521};
522
Namjae Jeon696c0182013-06-16 09:48:48 +0900523static int f2fs_remount(struct super_block *sb, int *flags, char *data)
524{
525 struct f2fs_sb_info *sbi = F2FS_SB(sb);
526 struct f2fs_mount_info org_mount_opt;
527 int err, active_logs;
528
529 /*
530 * Save the old mount options in case we
531 * need to restore them.
532 */
533 org_mount_opt = sbi->mount_opt;
534 active_logs = sbi->active_logs;
535
536 /* parse mount options */
537 err = parse_options(sb, data);
538 if (err)
539 goto restore_opts;
540
541 /*
542 * Previous and new state of filesystem is RO,
543 * so no point in checking GC conditions.
544 */
545 if ((sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY))
546 goto skip;
547
548 /*
549 * We stop the GC thread if FS is mounted as RO
550 * or if background_gc = off is passed in mount
551 * option. Also sync the filesystem.
552 */
553 if ((*flags & MS_RDONLY) || !test_opt(sbi, BG_GC)) {
554 if (sbi->gc_thread) {
555 stop_gc_thread(sbi);
556 f2fs_sync_fs(sb, 1);
557 }
558 } else if (test_opt(sbi, BG_GC) && !sbi->gc_thread) {
559 err = start_gc_thread(sbi);
560 if (err)
561 goto restore_opts;
562 }
563skip:
564 /* Update the POSIXACL Flag */
565 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
566 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
567 return 0;
568
569restore_opts:
570 sbi->mount_opt = org_mount_opt;
571 sbi->active_logs = active_logs;
572 return err;
573}
574
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900575static struct super_operations f2fs_sops = {
576 .alloc_inode = f2fs_alloc_inode,
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900577 .drop_inode = f2fs_drop_inode,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900578 .destroy_inode = f2fs_destroy_inode,
579 .write_inode = f2fs_write_inode,
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900580 .dirty_inode = f2fs_dirty_inode,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900581 .show_options = f2fs_show_options,
582 .evict_inode = f2fs_evict_inode,
583 .put_super = f2fs_put_super,
584 .sync_fs = f2fs_sync_fs,
Changman Leed6212a52013-01-29 18:30:07 +0900585 .freeze_fs = f2fs_freeze,
586 .unfreeze_fs = f2fs_unfreeze,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900587 .statfs = f2fs_statfs,
Namjae Jeon696c0182013-06-16 09:48:48 +0900588 .remount_fs = f2fs_remount,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900589};
590
591static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
592 u64 ino, u32 generation)
593{
594 struct f2fs_sb_info *sbi = F2FS_SB(sb);
595 struct inode *inode;
596
597 if (ino < F2FS_ROOT_INO(sbi))
598 return ERR_PTR(-ESTALE);
599
600 /*
601 * f2fs_iget isn't quite right if the inode is currently unallocated!
602 * However f2fs_iget currently does appropriate checks to handle stale
603 * inodes so everything is OK.
604 */
605 inode = f2fs_iget(sb, ino);
606 if (IS_ERR(inode))
607 return ERR_CAST(inode);
608 if (generation && inode->i_generation != generation) {
609 /* we didn't find the right inode.. */
610 iput(inode);
611 return ERR_PTR(-ESTALE);
612 }
613 return inode;
614}
615
616static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
617 int fh_len, int fh_type)
618{
619 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
620 f2fs_nfs_get_inode);
621}
622
623static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
624 int fh_len, int fh_type)
625{
626 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
627 f2fs_nfs_get_inode);
628}
629
630static const struct export_operations f2fs_export_ops = {
631 .fh_to_dentry = f2fs_fh_to_dentry,
632 .fh_to_parent = f2fs_fh_to_parent,
633 .get_parent = f2fs_get_parent,
634};
635
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900636static loff_t max_file_size(unsigned bits)
637{
Jaegeuk Kimde936532013-08-12 21:08:03 +0900638 loff_t result = (DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900639 loff_t leaf_count = ADDRS_PER_BLOCK;
640
641 /* two direct node blocks */
642 result += (leaf_count * 2);
643
644 /* two indirect node blocks */
645 leaf_count *= NIDS_PER_BLOCK;
646 result += (leaf_count * 2);
647
648 /* one double indirect node block */
649 leaf_count *= NIDS_PER_BLOCK;
650 result += leaf_count;
651
652 result <<= bits;
653 return result;
654}
655
Namjae Jeona07ef782012-12-30 14:52:05 +0900656static int sanity_check_raw_super(struct super_block *sb,
657 struct f2fs_super_block *raw_super)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900658{
659 unsigned int blocksize;
660
Namjae Jeona07ef782012-12-30 14:52:05 +0900661 if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
662 f2fs_msg(sb, KERN_INFO,
663 "Magic Mismatch, valid(0x%x) - read(0x%x)",
664 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900665 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900666 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900667
majianpeng5c9b4692013-02-01 19:07:57 +0800668 /* Currently, support only 4KB page cache size */
669 if (F2FS_BLKSIZE != PAGE_CACHE_SIZE) {
670 f2fs_msg(sb, KERN_INFO,
majianpeng14d7e9d2013-02-01 19:07:03 +0800671 "Invalid page_cache_size (%lu), supports only 4KB\n",
majianpeng5c9b4692013-02-01 19:07:57 +0800672 PAGE_CACHE_SIZE);
673 return 1;
674 }
675
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900676 /* Currently, support only 4KB block size */
677 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
majianpeng5c9b4692013-02-01 19:07:57 +0800678 if (blocksize != F2FS_BLKSIZE) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900679 f2fs_msg(sb, KERN_INFO,
680 "Invalid blocksize (%u), supports only 4KB\n",
681 blocksize);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900682 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900683 }
majianpeng5c9b4692013-02-01 19:07:57 +0800684
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900685 if (le32_to_cpu(raw_super->log_sectorsize) !=
Namjae Jeona07ef782012-12-30 14:52:05 +0900686 F2FS_LOG_SECTOR_SIZE) {
687 f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900688 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900689 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900690 if (le32_to_cpu(raw_super->log_sectors_per_block) !=
Namjae Jeona07ef782012-12-30 14:52:05 +0900691 F2FS_LOG_SECTORS_PER_BLOCK) {
692 f2fs_msg(sb, KERN_INFO, "Invalid log sectors per block");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900693 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900694 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900695 return 0;
696}
697
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900698static int sanity_check_ckpt(struct f2fs_sb_info *sbi)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900699{
700 unsigned int total, fsmeta;
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900701 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
702 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900703
704 total = le32_to_cpu(raw_super->segment_count);
705 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
706 fsmeta += le32_to_cpu(raw_super->segment_count_sit);
707 fsmeta += le32_to_cpu(raw_super->segment_count_nat);
708 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
709 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
710
711 if (fsmeta >= total)
712 return 1;
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900713
714 if (is_set_ckpt_flags(ckpt, CP_ERROR_FLAG)) {
715 f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
716 return 1;
717 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900718 return 0;
719}
720
721static void init_sb_info(struct f2fs_sb_info *sbi)
722{
723 struct f2fs_super_block *raw_super = sbi->raw_super;
724 int i;
725
726 sbi->log_sectors_per_block =
727 le32_to_cpu(raw_super->log_sectors_per_block);
728 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
729 sbi->blocksize = 1 << sbi->log_blocksize;
730 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
731 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
732 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
733 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
734 sbi->total_sections = le32_to_cpu(raw_super->section_count);
735 sbi->total_node_count =
736 (le32_to_cpu(raw_super->segment_count_nat) / 2)
737 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
738 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
739 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
740 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900741 sbi->cur_victim_sec = NULL_SECNO;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900742
743 for (i = 0; i < NR_COUNT_TYPE; i++)
744 atomic_set(&sbi->nr_pages[i], 0);
745}
746
majianpeng14d7e9d2013-02-01 19:07:03 +0800747static int validate_superblock(struct super_block *sb,
748 struct f2fs_super_block **raw_super,
749 struct buffer_head **raw_super_buf, sector_t block)
750{
751 const char *super = (block == 0 ? "first" : "second");
752
753 /* read f2fs raw super block */
754 *raw_super_buf = sb_bread(sb, block);
755 if (!*raw_super_buf) {
756 f2fs_msg(sb, KERN_ERR, "unable to read %s superblock",
757 super);
Namjae Jeonc0d39e62013-03-17 17:26:53 +0900758 return -EIO;
majianpeng14d7e9d2013-02-01 19:07:03 +0800759 }
760
761 *raw_super = (struct f2fs_super_block *)
762 ((char *)(*raw_super_buf)->b_data + F2FS_SUPER_OFFSET);
763
764 /* sanity checking of raw super */
765 if (!sanity_check_raw_super(sb, *raw_super))
766 return 0;
767
768 f2fs_msg(sb, KERN_ERR, "Can't find a valid F2FS filesystem "
769 "in %s superblock", super);
Namjae Jeonc0d39e62013-03-17 17:26:53 +0900770 return -EINVAL;
majianpeng14d7e9d2013-02-01 19:07:03 +0800771}
772
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900773static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
774{
775 struct f2fs_sb_info *sbi;
776 struct f2fs_super_block *raw_super;
777 struct buffer_head *raw_super_buf;
778 struct inode *root;
779 long err = -EINVAL;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900780
781 /* allocate memory for f2fs-specific super block info */
782 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
783 if (!sbi)
784 return -ENOMEM;
785
Namjae Jeonff9234a2013-01-12 14:41:13 +0900786 /* set a block size */
Namjae Jeona07ef782012-12-30 14:52:05 +0900787 if (!sb_set_blocksize(sb, F2FS_BLKSIZE)) {
788 f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900789 goto free_sbi;
Namjae Jeona07ef782012-12-30 14:52:05 +0900790 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900791
Namjae Jeonc0d39e62013-03-17 17:26:53 +0900792 err = validate_superblock(sb, &raw_super, &raw_super_buf, 0);
793 if (err) {
majianpeng14d7e9d2013-02-01 19:07:03 +0800794 brelse(raw_super_buf);
Namjae Jeonc0d39e62013-03-17 17:26:53 +0900795 /* check secondary superblock when primary failed */
796 err = validate_superblock(sb, &raw_super, &raw_super_buf, 1);
797 if (err)
majianpeng14d7e9d2013-02-01 19:07:03 +0800798 goto free_sb_buf;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900799 }
Gu Zheng5fb08372013-06-07 14:16:53 +0800800 sb->s_fs_info = sbi;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900801 /* init some FS parameters */
802 sbi->active_logs = NR_CURSEG_TYPE;
803
804 set_opt(sbi, BG_GC);
805
806#ifdef CONFIG_F2FS_FS_XATTR
807 set_opt(sbi, XATTR_USER);
808#endif
809#ifdef CONFIG_F2FS_FS_POSIX_ACL
810 set_opt(sbi, POSIX_ACL);
811#endif
812 /* parse mount options */
Gu Zheng5fb08372013-06-07 14:16:53 +0800813 err = parse_options(sb, (char *)data);
Wei Yongjun66348b72013-04-12 10:23:18 +0800814 if (err)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900815 goto free_sb_buf;
816
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900817 sb->s_maxbytes = max_file_size(le32_to_cpu(raw_super->log_blocksize));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900818 sb->s_max_links = F2FS_LINK_MAX;
819 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
820
821 sb->s_op = &f2fs_sops;
822 sb->s_xattr = f2fs_xattr_handlers;
823 sb->s_export_op = &f2fs_export_ops;
824 sb->s_magic = F2FS_SUPER_MAGIC;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900825 sb->s_time_gran = 1;
826 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
827 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
828 memcpy(sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
829
830 /* init f2fs-specific super block info */
831 sbi->sb = sb;
832 sbi->raw_super = raw_super;
833 sbi->raw_super_buf = raw_super_buf;
834 mutex_init(&sbi->gc_mutex);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900835 mutex_init(&sbi->writepages);
836 mutex_init(&sbi->cp_mutex);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900837 mutex_init(&sbi->node_write);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900838 sbi->por_doing = 0;
839 spin_lock_init(&sbi->stat_lock);
840 init_rwsem(&sbi->bio_sem);
Gu Zhenge4795562013-09-27 18:08:30 +0800841 init_rwsem(&sbi->cp_rwsem);
842 init_waitqueue_head(&sbi->cp_wait);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900843 init_sb_info(sbi);
844
845 /* get an inode for meta space */
846 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
847 if (IS_ERR(sbi->meta_inode)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900848 f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900849 err = PTR_ERR(sbi->meta_inode);
850 goto free_sb_buf;
851 }
852
853 err = get_valid_checkpoint(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +0900854 if (err) {
855 f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900856 goto free_meta_inode;
Namjae Jeona07ef782012-12-30 14:52:05 +0900857 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900858
859 /* sanity checking of checkpoint */
860 err = -EINVAL;
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900861 if (sanity_check_ckpt(sbi)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900862 f2fs_msg(sb, KERN_ERR, "Invalid F2FS checkpoint");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900863 goto free_cp;
Namjae Jeona07ef782012-12-30 14:52:05 +0900864 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900865
866 sbi->total_valid_node_count =
867 le32_to_cpu(sbi->ckpt->valid_node_count);
868 sbi->total_valid_inode_count =
869 le32_to_cpu(sbi->ckpt->valid_inode_count);
870 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
871 sbi->total_valid_block_count =
872 le64_to_cpu(sbi->ckpt->valid_block_count);
873 sbi->last_valid_block_count = sbi->total_valid_block_count;
874 sbi->alloc_valid_block_count = 0;
875 INIT_LIST_HEAD(&sbi->dir_inode_list);
876 spin_lock_init(&sbi->dir_inode_lock);
877
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900878 init_orphan_info(sbi);
879
880 /* setup f2fs internal modules */
881 err = build_segment_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +0900882 if (err) {
883 f2fs_msg(sb, KERN_ERR,
884 "Failed to initialize F2FS segment manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900885 goto free_sm;
Namjae Jeona07ef782012-12-30 14:52:05 +0900886 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900887 err = build_node_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +0900888 if (err) {
889 f2fs_msg(sb, KERN_ERR,
890 "Failed to initialize F2FS node manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900891 goto free_nm;
Namjae Jeona07ef782012-12-30 14:52:05 +0900892 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900893
894 build_gc_manager(sbi);
895
896 /* get an inode for node space */
897 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
898 if (IS_ERR(sbi->node_inode)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900899 f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900900 err = PTR_ERR(sbi->node_inode);
901 goto free_nm;
902 }
903
904 /* if there are nt orphan nodes free them */
905 err = -EINVAL;
Jaegeuk Kim30f0c752012-12-19 16:09:19 +0900906 if (recover_orphan_inodes(sbi))
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900907 goto free_node_inode;
908
909 /* read root inode and dentry */
910 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
911 if (IS_ERR(root)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900912 f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900913 err = PTR_ERR(root);
914 goto free_node_inode;
915 }
916 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size)
917 goto free_root_inode;
918
919 sb->s_root = d_make_root(root); /* allocate root dentry */
920 if (!sb->s_root) {
921 err = -ENOMEM;
922 goto free_root_inode;
923 }
924
925 /* recover fsynced data */
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900926 if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
927 err = recover_fsync_data(sbi);
Chris Friesbde582b2013-05-02 16:07:34 -0500928 if (err)
929 f2fs_msg(sb, KERN_ERR,
930 "Cannot recover all fsync data errno=%ld", err);
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900931 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900932
Namjae Jeon696c0182013-06-16 09:48:48 +0900933 /*
934 * If filesystem is not mounted as read-only then
935 * do start the gc_thread.
936 */
937 if (!(sb->s_flags & MS_RDONLY)) {
938 /* After POR, we can run background GC thread.*/
939 err = start_gc_thread(sbi);
940 if (err)
941 goto fail;
942 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900943
944 err = f2fs_build_stats(sbi);
945 if (err)
946 goto fail;
947
Jaegeuk Kim5e176d52013-06-28 12:47:01 +0900948 if (f2fs_proc_root)
949 sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
950
951 if (sbi->s_proc)
952 proc_create_data("segment_info", S_IRUGO, sbi->s_proc,
953 &f2fs_seq_segment_info_fops, sb);
954
Namjae Jeond3ee4562013-03-17 17:26:14 +0900955 if (test_opt(sbi, DISCARD)) {
956 struct request_queue *q = bdev_get_queue(sb->s_bdev);
957 if (!blk_queue_discard(q))
958 f2fs_msg(sb, KERN_WARNING,
959 "mounting with \"discard\" option, but "
960 "the device does not support discard");
961 }
962
Namjae Jeonb59d0ba2013-08-04 23:09:40 +0900963 sbi->s_kobj.kset = f2fs_kset;
964 init_completion(&sbi->s_kobj_unregister);
965 err = kobject_init_and_add(&sbi->s_kobj, &f2fs_ktype, NULL,
966 "%s", sb->s_id);
967 if (err)
968 goto fail;
969
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900970 return 0;
971fail:
972 stop_gc_thread(sbi);
973free_root_inode:
974 dput(sb->s_root);
975 sb->s_root = NULL;
976free_node_inode:
977 iput(sbi->node_inode);
978free_nm:
979 destroy_node_manager(sbi);
980free_sm:
981 destroy_segment_manager(sbi);
982free_cp:
983 kfree(sbi->ckpt);
984free_meta_inode:
985 make_bad_inode(sbi->meta_inode);
986 iput(sbi->meta_inode);
987free_sb_buf:
988 brelse(raw_super_buf);
989free_sbi:
990 kfree(sbi);
991 return err;
992}
993
994static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
995 const char *dev_name, void *data)
996{
997 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
998}
999
1000static struct file_system_type f2fs_fs_type = {
1001 .owner = THIS_MODULE,
1002 .name = "f2fs",
1003 .mount = f2fs_mount,
1004 .kill_sb = kill_block_super,
1005 .fs_flags = FS_REQUIRES_DEV,
1006};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08001007MODULE_ALIAS_FS("f2fs");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001008
Namjae Jeon6e6093a2013-01-17 00:08:30 +09001009static int __init init_inodecache(void)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001010{
1011 f2fs_inode_cachep = f2fs_kmem_cache_create("f2fs_inode_cache",
1012 sizeof(struct f2fs_inode_info), NULL);
1013 if (f2fs_inode_cachep == NULL)
1014 return -ENOMEM;
1015 return 0;
1016}
1017
1018static void destroy_inodecache(void)
1019{
1020 /*
1021 * Make sure all delayed rcu free inodes are flushed before we
1022 * destroy cache.
1023 */
1024 rcu_barrier();
1025 kmem_cache_destroy(f2fs_inode_cachep);
1026}
1027
1028static int __init init_f2fs_fs(void)
1029{
1030 int err;
1031
1032 err = init_inodecache();
1033 if (err)
1034 goto fail;
1035 err = create_node_manager_caches();
1036 if (err)
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08001037 goto free_inodecache;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001038 err = create_gc_caches();
1039 if (err)
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08001040 goto free_node_manager_caches;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001041 err = create_checkpoint_caches();
1042 if (err)
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08001043 goto free_gc_caches;
Namjae Jeonb59d0ba2013-08-04 23:09:40 +09001044 f2fs_kset = kset_create_and_add("f2fs", NULL, fs_kobj);
Wei Yongjun6e6b9782013-08-23 08:30:20 +08001045 if (!f2fs_kset) {
1046 err = -ENOMEM;
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08001047 goto free_checkpoint_caches;
Wei Yongjun6e6b9782013-08-23 08:30:20 +08001048 }
Namjae Jeon4589d252013-01-15 19:58:47 +09001049 err = register_filesystem(&f2fs_fs_type);
1050 if (err)
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08001051 goto free_kset;
Namjae Jeon4589d252013-01-15 19:58:47 +09001052 f2fs_create_root_stats();
Jaegeuk Kim5e176d52013-06-28 12:47:01 +09001053 f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08001054 return 0;
1055
1056free_kset:
1057 kset_unregister(f2fs_kset);
1058free_checkpoint_caches:
1059 destroy_checkpoint_caches();
1060free_gc_caches:
1061 destroy_gc_caches();
1062free_node_manager_caches:
1063 destroy_node_manager_caches();
1064free_inodecache:
1065 destroy_inodecache();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001066fail:
1067 return err;
1068}
1069
1070static void __exit exit_f2fs_fs(void)
1071{
Jaegeuk Kim5e176d52013-06-28 12:47:01 +09001072 remove_proc_entry("fs/f2fs", NULL);
Namjae Jeon4589d252013-01-15 19:58:47 +09001073 f2fs_destroy_root_stats();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001074 unregister_filesystem(&f2fs_fs_type);
1075 destroy_checkpoint_caches();
1076 destroy_gc_caches();
1077 destroy_node_manager_caches();
1078 destroy_inodecache();
Namjae Jeonb59d0ba2013-08-04 23:09:40 +09001079 kset_unregister(f2fs_kset);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001080}
1081
1082module_init(init_f2fs_fs)
1083module_exit(exit_f2fs_fs)
1084
1085MODULE_AUTHOR("Samsung Electronics's Praesto Team");
1086MODULE_DESCRIPTION("Flash Friendly File System");
1087MODULE_LICENSE("GPL");