blob: 75c7dc363e9267378d5cdf39f811c16a7adc8692 [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>
21#include <linux/random.h>
22#include <linux/exportfs.h>
Namjae Jeond3ee4562013-03-17 17:26:14 +090023#include <linux/blkdev.h>
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090024#include <linux/f2fs_fs.h>
25
26#include "f2fs.h"
27#include "node.h"
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +090028#include "segment.h"
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090029#include "xattr.h"
30
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090031#define CREATE_TRACE_POINTS
32#include <trace/events/f2fs.h>
33
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090034static struct kmem_cache *f2fs_inode_cachep;
35
36enum {
Namjae Jeon696c0182013-06-16 09:48:48 +090037 Opt_gc_background,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090038 Opt_disable_roll_forward,
39 Opt_discard,
40 Opt_noheap,
41 Opt_nouser_xattr,
42 Opt_noacl,
43 Opt_active_logs,
44 Opt_disable_ext_identify,
45 Opt_err,
46};
47
48static match_table_t f2fs_tokens = {
Namjae Jeon696c0182013-06-16 09:48:48 +090049 {Opt_gc_background, "background_gc=%s"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090050 {Opt_disable_roll_forward, "disable_roll_forward"},
51 {Opt_discard, "discard"},
52 {Opt_noheap, "no_heap"},
53 {Opt_nouser_xattr, "nouser_xattr"},
54 {Opt_noacl, "noacl"},
55 {Opt_active_logs, "active_logs=%u"},
56 {Opt_disable_ext_identify, "disable_ext_identify"},
57 {Opt_err, NULL},
58};
59
Namjae Jeona07ef782012-12-30 14:52:05 +090060void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
61{
62 struct va_format vaf;
63 va_list args;
64
65 va_start(args, fmt);
66 vaf.fmt = fmt;
67 vaf.va = &args;
68 printk("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
69 va_end(args);
70}
71
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090072static void init_once(void *foo)
73{
74 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
75
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090076 inode_init_once(&fi->vfs_inode);
77}
78
Namjae Jeon696c0182013-06-16 09:48:48 +090079static int parse_options(struct super_block *sb, char *options)
80{
81 struct f2fs_sb_info *sbi = F2FS_SB(sb);
82 substring_t args[MAX_OPT_ARGS];
83 char *p, *name;
84 int arg = 0;
85
86 if (!options)
87 return 0;
88
89 while ((p = strsep(&options, ",")) != NULL) {
90 int token;
91 if (!*p)
92 continue;
93 /*
94 * Initialize args struct so we know whether arg was
95 * found; some options take optional arguments.
96 */
97 args[0].to = args[0].from = NULL;
98 token = match_token(p, f2fs_tokens, args);
99
100 switch (token) {
101 case Opt_gc_background:
102 name = match_strdup(&args[0]);
103
104 if (!name)
105 return -ENOMEM;
106 if (!strncmp(name, "on", 2))
107 set_opt(sbi, BG_GC);
108 else if (!strncmp(name, "off", 3))
109 clear_opt(sbi, BG_GC);
110 else {
111 kfree(name);
112 return -EINVAL;
113 }
114 kfree(name);
115 break;
116 case Opt_disable_roll_forward:
117 set_opt(sbi, DISABLE_ROLL_FORWARD);
118 break;
119 case Opt_discard:
120 set_opt(sbi, DISCARD);
121 break;
122 case Opt_noheap:
123 set_opt(sbi, NOHEAP);
124 break;
125#ifdef CONFIG_F2FS_FS_XATTR
126 case Opt_nouser_xattr:
127 clear_opt(sbi, XATTR_USER);
128 break;
129#else
130 case Opt_nouser_xattr:
131 f2fs_msg(sb, KERN_INFO,
132 "nouser_xattr options not supported");
133 break;
134#endif
135#ifdef CONFIG_F2FS_FS_POSIX_ACL
136 case Opt_noacl:
137 clear_opt(sbi, POSIX_ACL);
138 break;
139#else
140 case Opt_noacl:
141 f2fs_msg(sb, KERN_INFO, "noacl options not supported");
142 break;
143#endif
144 case Opt_active_logs:
145 if (args->from && match_int(args, &arg))
146 return -EINVAL;
147 if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
148 return -EINVAL;
149 sbi->active_logs = arg;
150 break;
151 case Opt_disable_ext_identify:
152 set_opt(sbi, DISABLE_EXT_IDENTIFY);
153 break;
154 default:
155 f2fs_msg(sb, KERN_ERR,
156 "Unrecognized mount option \"%s\" or missing value",
157 p);
158 return -EINVAL;
159 }
160 }
161 return 0;
162}
163
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900164static struct inode *f2fs_alloc_inode(struct super_block *sb)
165{
166 struct f2fs_inode_info *fi;
167
168 fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_NOFS | __GFP_ZERO);
169 if (!fi)
170 return NULL;
171
172 init_once((void *) fi);
173
Masanari Iida434720f2013-03-19 08:03:35 +0900174 /* Initialize f2fs-specific inode info */
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900175 fi->vfs_inode.i_version = 1;
176 atomic_set(&fi->dirty_dents, 0);
177 fi->i_current_depth = 1;
178 fi->i_advise = 0;
179 rwlock_init(&fi->ext.ext_lock);
180
181 set_inode_flag(fi, FI_NEW_INODE);
182
183 return &fi->vfs_inode;
184}
185
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900186static int f2fs_drop_inode(struct inode *inode)
187{
188 /*
189 * This is to avoid a deadlock condition like below.
190 * writeback_single_inode(inode)
191 * - f2fs_write_data_page
192 * - f2fs_gc -> iput -> evict
193 * - inode_wait_for_writeback(inode)
194 */
195 if (!inode_unhashed(inode) && inode->i_state & I_SYNC)
196 return 0;
197 return generic_drop_inode(inode);
198}
199
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900200/*
201 * f2fs_dirty_inode() is called from __mark_inode_dirty()
202 *
203 * We should call set_dirty_inode to write the dirty inode through write_inode.
204 */
205static void f2fs_dirty_inode(struct inode *inode, int flags)
206{
207 set_inode_flag(F2FS_I(inode), FI_DIRTY_INODE);
208 return;
209}
210
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900211static void f2fs_i_callback(struct rcu_head *head)
212{
213 struct inode *inode = container_of(head, struct inode, i_rcu);
214 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
215}
216
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900217static void f2fs_destroy_inode(struct inode *inode)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900218{
219 call_rcu(&inode->i_rcu, f2fs_i_callback);
220}
221
222static void f2fs_put_super(struct super_block *sb)
223{
224 struct f2fs_sb_info *sbi = F2FS_SB(sb);
225
226 f2fs_destroy_stats(sbi);
227 stop_gc_thread(sbi);
228
Jaegeuk Kim43727522013-02-04 15:11:17 +0900229 write_checkpoint(sbi, true);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900230
231 iput(sbi->node_inode);
232 iput(sbi->meta_inode);
233
234 /* destroy f2fs internal modules */
235 destroy_node_manager(sbi);
236 destroy_segment_manager(sbi);
237
238 kfree(sbi->ckpt);
239
240 sb->s_fs_info = NULL;
241 brelse(sbi->raw_super_buf);
242 kfree(sbi);
243}
244
245int f2fs_sync_fs(struct super_block *sb, int sync)
246{
247 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900248
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900249 trace_f2fs_sync_fs(sb, sync);
250
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900251 if (!sbi->s_dirty && !get_pages(sbi, F2FS_DIRTY_NODES))
252 return 0;
253
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900254 if (sync) {
255 mutex_lock(&sbi->gc_mutex);
Jaegeuk Kim43727522013-02-04 15:11:17 +0900256 write_checkpoint(sbi, false);
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900257 mutex_unlock(&sbi->gc_mutex);
258 } else {
Jaegeuk Kim7d82db82013-01-11 13:10:49 +0900259 f2fs_balance_fs(sbi);
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900260 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900261
Namjae Jeon64c576f2012-12-22 12:10:27 +0900262 return 0;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900263}
264
Changman Leed6212a52013-01-29 18:30:07 +0900265static int f2fs_freeze(struct super_block *sb)
266{
267 int err;
268
Jaegeuk Kim77888c12013-05-20 20:28:47 +0900269 if (f2fs_readonly(sb))
Changman Leed6212a52013-01-29 18:30:07 +0900270 return 0;
271
272 err = f2fs_sync_fs(sb, 1);
273 return err;
274}
275
276static int f2fs_unfreeze(struct super_block *sb)
277{
278 return 0;
279}
280
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900281static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
282{
283 struct super_block *sb = dentry->d_sb;
284 struct f2fs_sb_info *sbi = F2FS_SB(sb);
285 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
286 block_t total_count, user_block_count, start_count, ovp_count;
287
288 total_count = le64_to_cpu(sbi->raw_super->block_count);
289 user_block_count = sbi->user_block_count;
290 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
291 ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg;
292 buf->f_type = F2FS_SUPER_MAGIC;
293 buf->f_bsize = sbi->blocksize;
294
295 buf->f_blocks = total_count - start_count;
296 buf->f_bfree = buf->f_blocks - valid_user_blocks(sbi) - ovp_count;
297 buf->f_bavail = user_block_count - valid_user_blocks(sbi);
298
Jaegeuk Kim1362b5e2012-12-12 19:45:49 +0900299 buf->f_files = sbi->total_node_count;
300 buf->f_ffree = sbi->total_node_count - valid_inode_count(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900301
Jaegeuk Kim5a20d332013-03-03 13:58:05 +0900302 buf->f_namelen = F2FS_NAME_LEN;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900303 buf->f_fsid.val[0] = (u32)id;
304 buf->f_fsid.val[1] = (u32)(id >> 32);
305
306 return 0;
307}
308
309static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
310{
311 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
312
Namjae Jeon696c0182013-06-16 09:48:48 +0900313 if (!(root->d_sb->s_flags & MS_RDONLY) && test_opt(sbi, BG_GC))
314 seq_printf(seq, ",background_gc=%s", "on");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900315 else
Namjae Jeon696c0182013-06-16 09:48:48 +0900316 seq_printf(seq, ",background_gc=%s", "off");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900317 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
318 seq_puts(seq, ",disable_roll_forward");
319 if (test_opt(sbi, DISCARD))
320 seq_puts(seq, ",discard");
321 if (test_opt(sbi, NOHEAP))
322 seq_puts(seq, ",no_heap_alloc");
323#ifdef CONFIG_F2FS_FS_XATTR
324 if (test_opt(sbi, XATTR_USER))
325 seq_puts(seq, ",user_xattr");
326 else
327 seq_puts(seq, ",nouser_xattr");
328#endif
329#ifdef CONFIG_F2FS_FS_POSIX_ACL
330 if (test_opt(sbi, POSIX_ACL))
331 seq_puts(seq, ",acl");
332 else
333 seq_puts(seq, ",noacl");
334#endif
335 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
Alejandro Martinez Ruizaa435072013-01-25 19:08:59 +0100336 seq_puts(seq, ",disable_ext_identify");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900337
338 seq_printf(seq, ",active_logs=%u", sbi->active_logs);
339
340 return 0;
341}
342
Namjae Jeon696c0182013-06-16 09:48:48 +0900343static int f2fs_remount(struct super_block *sb, int *flags, char *data)
344{
345 struct f2fs_sb_info *sbi = F2FS_SB(sb);
346 struct f2fs_mount_info org_mount_opt;
347 int err, active_logs;
348
349 /*
350 * Save the old mount options in case we
351 * need to restore them.
352 */
353 org_mount_opt = sbi->mount_opt;
354 active_logs = sbi->active_logs;
355
356 /* parse mount options */
357 err = parse_options(sb, data);
358 if (err)
359 goto restore_opts;
360
361 /*
362 * Previous and new state of filesystem is RO,
363 * so no point in checking GC conditions.
364 */
365 if ((sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY))
366 goto skip;
367
368 /*
369 * We stop the GC thread if FS is mounted as RO
370 * or if background_gc = off is passed in mount
371 * option. Also sync the filesystem.
372 */
373 if ((*flags & MS_RDONLY) || !test_opt(sbi, BG_GC)) {
374 if (sbi->gc_thread) {
375 stop_gc_thread(sbi);
376 f2fs_sync_fs(sb, 1);
377 }
378 } else if (test_opt(sbi, BG_GC) && !sbi->gc_thread) {
379 err = start_gc_thread(sbi);
380 if (err)
381 goto restore_opts;
382 }
383skip:
384 /* Update the POSIXACL Flag */
385 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
386 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
387 return 0;
388
389restore_opts:
390 sbi->mount_opt = org_mount_opt;
391 sbi->active_logs = active_logs;
392 return err;
393}
394
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900395static struct super_operations f2fs_sops = {
396 .alloc_inode = f2fs_alloc_inode,
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900397 .drop_inode = f2fs_drop_inode,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900398 .destroy_inode = f2fs_destroy_inode,
399 .write_inode = f2fs_write_inode,
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900400 .dirty_inode = f2fs_dirty_inode,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900401 .show_options = f2fs_show_options,
402 .evict_inode = f2fs_evict_inode,
403 .put_super = f2fs_put_super,
404 .sync_fs = f2fs_sync_fs,
Changman Leed6212a52013-01-29 18:30:07 +0900405 .freeze_fs = f2fs_freeze,
406 .unfreeze_fs = f2fs_unfreeze,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900407 .statfs = f2fs_statfs,
Namjae Jeon696c0182013-06-16 09:48:48 +0900408 .remount_fs = f2fs_remount,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900409};
410
411static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
412 u64 ino, u32 generation)
413{
414 struct f2fs_sb_info *sbi = F2FS_SB(sb);
415 struct inode *inode;
416
417 if (ino < F2FS_ROOT_INO(sbi))
418 return ERR_PTR(-ESTALE);
419
420 /*
421 * f2fs_iget isn't quite right if the inode is currently unallocated!
422 * However f2fs_iget currently does appropriate checks to handle stale
423 * inodes so everything is OK.
424 */
425 inode = f2fs_iget(sb, ino);
426 if (IS_ERR(inode))
427 return ERR_CAST(inode);
428 if (generation && inode->i_generation != generation) {
429 /* we didn't find the right inode.. */
430 iput(inode);
431 return ERR_PTR(-ESTALE);
432 }
433 return inode;
434}
435
436static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
437 int fh_len, int fh_type)
438{
439 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
440 f2fs_nfs_get_inode);
441}
442
443static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
444 int fh_len, int fh_type)
445{
446 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
447 f2fs_nfs_get_inode);
448}
449
450static const struct export_operations f2fs_export_ops = {
451 .fh_to_dentry = f2fs_fh_to_dentry,
452 .fh_to_parent = f2fs_fh_to_parent,
453 .get_parent = f2fs_get_parent,
454};
455
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900456static loff_t max_file_size(unsigned bits)
457{
458 loff_t result = ADDRS_PER_INODE;
459 loff_t leaf_count = ADDRS_PER_BLOCK;
460
461 /* two direct node blocks */
462 result += (leaf_count * 2);
463
464 /* two indirect node blocks */
465 leaf_count *= NIDS_PER_BLOCK;
466 result += (leaf_count * 2);
467
468 /* one double indirect node block */
469 leaf_count *= NIDS_PER_BLOCK;
470 result += leaf_count;
471
472 result <<= bits;
473 return result;
474}
475
Namjae Jeona07ef782012-12-30 14:52:05 +0900476static int sanity_check_raw_super(struct super_block *sb,
477 struct f2fs_super_block *raw_super)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900478{
479 unsigned int blocksize;
480
Namjae Jeona07ef782012-12-30 14:52:05 +0900481 if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
482 f2fs_msg(sb, KERN_INFO,
483 "Magic Mismatch, valid(0x%x) - read(0x%x)",
484 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900485 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900486 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900487
majianpeng5c9b4692013-02-01 19:07:57 +0800488 /* Currently, support only 4KB page cache size */
489 if (F2FS_BLKSIZE != PAGE_CACHE_SIZE) {
490 f2fs_msg(sb, KERN_INFO,
majianpeng14d7e9d2013-02-01 19:07:03 +0800491 "Invalid page_cache_size (%lu), supports only 4KB\n",
majianpeng5c9b4692013-02-01 19:07:57 +0800492 PAGE_CACHE_SIZE);
493 return 1;
494 }
495
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900496 /* Currently, support only 4KB block size */
497 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
majianpeng5c9b4692013-02-01 19:07:57 +0800498 if (blocksize != F2FS_BLKSIZE) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900499 f2fs_msg(sb, KERN_INFO,
500 "Invalid blocksize (%u), supports only 4KB\n",
501 blocksize);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900502 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900503 }
majianpeng5c9b4692013-02-01 19:07:57 +0800504
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900505 if (le32_to_cpu(raw_super->log_sectorsize) !=
Namjae Jeona07ef782012-12-30 14:52:05 +0900506 F2FS_LOG_SECTOR_SIZE) {
507 f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900508 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900509 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900510 if (le32_to_cpu(raw_super->log_sectors_per_block) !=
Namjae Jeona07ef782012-12-30 14:52:05 +0900511 F2FS_LOG_SECTORS_PER_BLOCK) {
512 f2fs_msg(sb, KERN_INFO, "Invalid log sectors per block");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900513 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900514 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900515 return 0;
516}
517
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900518static int sanity_check_ckpt(struct f2fs_sb_info *sbi)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900519{
520 unsigned int total, fsmeta;
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900521 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
522 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900523
524 total = le32_to_cpu(raw_super->segment_count);
525 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
526 fsmeta += le32_to_cpu(raw_super->segment_count_sit);
527 fsmeta += le32_to_cpu(raw_super->segment_count_nat);
528 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
529 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
530
531 if (fsmeta >= total)
532 return 1;
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900533
534 if (is_set_ckpt_flags(ckpt, CP_ERROR_FLAG)) {
535 f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
536 return 1;
537 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900538 return 0;
539}
540
541static void init_sb_info(struct f2fs_sb_info *sbi)
542{
543 struct f2fs_super_block *raw_super = sbi->raw_super;
544 int i;
545
546 sbi->log_sectors_per_block =
547 le32_to_cpu(raw_super->log_sectors_per_block);
548 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
549 sbi->blocksize = 1 << sbi->log_blocksize;
550 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
551 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
552 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
553 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
554 sbi->total_sections = le32_to_cpu(raw_super->section_count);
555 sbi->total_node_count =
556 (le32_to_cpu(raw_super->segment_count_nat) / 2)
557 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
558 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
559 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
560 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900561 sbi->cur_victim_sec = NULL_SECNO;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900562
563 for (i = 0; i < NR_COUNT_TYPE; i++)
564 atomic_set(&sbi->nr_pages[i], 0);
565}
566
majianpeng14d7e9d2013-02-01 19:07:03 +0800567static int validate_superblock(struct super_block *sb,
568 struct f2fs_super_block **raw_super,
569 struct buffer_head **raw_super_buf, sector_t block)
570{
571 const char *super = (block == 0 ? "first" : "second");
572
573 /* read f2fs raw super block */
574 *raw_super_buf = sb_bread(sb, block);
575 if (!*raw_super_buf) {
576 f2fs_msg(sb, KERN_ERR, "unable to read %s superblock",
577 super);
Namjae Jeonc0d39e62013-03-17 17:26:53 +0900578 return -EIO;
majianpeng14d7e9d2013-02-01 19:07:03 +0800579 }
580
581 *raw_super = (struct f2fs_super_block *)
582 ((char *)(*raw_super_buf)->b_data + F2FS_SUPER_OFFSET);
583
584 /* sanity checking of raw super */
585 if (!sanity_check_raw_super(sb, *raw_super))
586 return 0;
587
588 f2fs_msg(sb, KERN_ERR, "Can't find a valid F2FS filesystem "
589 "in %s superblock", super);
Namjae Jeonc0d39e62013-03-17 17:26:53 +0900590 return -EINVAL;
majianpeng14d7e9d2013-02-01 19:07:03 +0800591}
592
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900593static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
594{
595 struct f2fs_sb_info *sbi;
596 struct f2fs_super_block *raw_super;
597 struct buffer_head *raw_super_buf;
598 struct inode *root;
599 long err = -EINVAL;
600 int i;
601
602 /* allocate memory for f2fs-specific super block info */
603 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
604 if (!sbi)
605 return -ENOMEM;
606
Namjae Jeonff9234a2013-01-12 14:41:13 +0900607 /* set a block size */
Namjae Jeona07ef782012-12-30 14:52:05 +0900608 if (!sb_set_blocksize(sb, F2FS_BLKSIZE)) {
609 f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900610 goto free_sbi;
Namjae Jeona07ef782012-12-30 14:52:05 +0900611 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900612
Namjae Jeonc0d39e62013-03-17 17:26:53 +0900613 err = validate_superblock(sb, &raw_super, &raw_super_buf, 0);
614 if (err) {
majianpeng14d7e9d2013-02-01 19:07:03 +0800615 brelse(raw_super_buf);
Namjae Jeonc0d39e62013-03-17 17:26:53 +0900616 /* check secondary superblock when primary failed */
617 err = validate_superblock(sb, &raw_super, &raw_super_buf, 1);
618 if (err)
majianpeng14d7e9d2013-02-01 19:07:03 +0800619 goto free_sb_buf;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900620 }
Gu Zheng5fb08372013-06-07 14:16:53 +0800621 sb->s_fs_info = sbi;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900622 /* init some FS parameters */
623 sbi->active_logs = NR_CURSEG_TYPE;
624
625 set_opt(sbi, BG_GC);
626
627#ifdef CONFIG_F2FS_FS_XATTR
628 set_opt(sbi, XATTR_USER);
629#endif
630#ifdef CONFIG_F2FS_FS_POSIX_ACL
631 set_opt(sbi, POSIX_ACL);
632#endif
633 /* parse mount options */
Gu Zheng5fb08372013-06-07 14:16:53 +0800634 err = parse_options(sb, (char *)data);
Wei Yongjun66348b72013-04-12 10:23:18 +0800635 if (err)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900636 goto free_sb_buf;
637
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900638 sb->s_maxbytes = max_file_size(le32_to_cpu(raw_super->log_blocksize));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900639 sb->s_max_links = F2FS_LINK_MAX;
640 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
641
642 sb->s_op = &f2fs_sops;
643 sb->s_xattr = f2fs_xattr_handlers;
644 sb->s_export_op = &f2fs_export_ops;
645 sb->s_magic = F2FS_SUPER_MAGIC;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900646 sb->s_time_gran = 1;
647 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
648 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
649 memcpy(sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
650
651 /* init f2fs-specific super block info */
652 sbi->sb = sb;
653 sbi->raw_super = raw_super;
654 sbi->raw_super_buf = raw_super_buf;
655 mutex_init(&sbi->gc_mutex);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900656 mutex_init(&sbi->writepages);
657 mutex_init(&sbi->cp_mutex);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900658 for (i = 0; i < NR_GLOBAL_LOCKS; i++)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900659 mutex_init(&sbi->fs_lock[i]);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900660 mutex_init(&sbi->node_write);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900661 sbi->por_doing = 0;
662 spin_lock_init(&sbi->stat_lock);
663 init_rwsem(&sbi->bio_sem);
664 init_sb_info(sbi);
665
666 /* get an inode for meta space */
667 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
668 if (IS_ERR(sbi->meta_inode)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900669 f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900670 err = PTR_ERR(sbi->meta_inode);
671 goto free_sb_buf;
672 }
673
674 err = get_valid_checkpoint(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +0900675 if (err) {
676 f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900677 goto free_meta_inode;
Namjae Jeona07ef782012-12-30 14:52:05 +0900678 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900679
680 /* sanity checking of checkpoint */
681 err = -EINVAL;
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900682 if (sanity_check_ckpt(sbi)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900683 f2fs_msg(sb, KERN_ERR, "Invalid F2FS checkpoint");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900684 goto free_cp;
Namjae Jeona07ef782012-12-30 14:52:05 +0900685 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900686
687 sbi->total_valid_node_count =
688 le32_to_cpu(sbi->ckpt->valid_node_count);
689 sbi->total_valid_inode_count =
690 le32_to_cpu(sbi->ckpt->valid_inode_count);
691 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
692 sbi->total_valid_block_count =
693 le64_to_cpu(sbi->ckpt->valid_block_count);
694 sbi->last_valid_block_count = sbi->total_valid_block_count;
695 sbi->alloc_valid_block_count = 0;
696 INIT_LIST_HEAD(&sbi->dir_inode_list);
697 spin_lock_init(&sbi->dir_inode_lock);
698
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900699 init_orphan_info(sbi);
700
701 /* setup f2fs internal modules */
702 err = build_segment_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +0900703 if (err) {
704 f2fs_msg(sb, KERN_ERR,
705 "Failed to initialize F2FS segment manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900706 goto free_sm;
Namjae Jeona07ef782012-12-30 14:52:05 +0900707 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900708 err = build_node_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +0900709 if (err) {
710 f2fs_msg(sb, KERN_ERR,
711 "Failed to initialize F2FS node manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900712 goto free_nm;
Namjae Jeona07ef782012-12-30 14:52:05 +0900713 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900714
715 build_gc_manager(sbi);
716
717 /* get an inode for node space */
718 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
719 if (IS_ERR(sbi->node_inode)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900720 f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900721 err = PTR_ERR(sbi->node_inode);
722 goto free_nm;
723 }
724
725 /* if there are nt orphan nodes free them */
726 err = -EINVAL;
Jaegeuk Kim30f0c752012-12-19 16:09:19 +0900727 if (recover_orphan_inodes(sbi))
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900728 goto free_node_inode;
729
730 /* read root inode and dentry */
731 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
732 if (IS_ERR(root)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900733 f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900734 err = PTR_ERR(root);
735 goto free_node_inode;
736 }
737 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size)
738 goto free_root_inode;
739
740 sb->s_root = d_make_root(root); /* allocate root dentry */
741 if (!sb->s_root) {
742 err = -ENOMEM;
743 goto free_root_inode;
744 }
745
746 /* recover fsynced data */
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900747 if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
748 err = recover_fsync_data(sbi);
Chris Friesbde582b2013-05-02 16:07:34 -0500749 if (err)
750 f2fs_msg(sb, KERN_ERR,
751 "Cannot recover all fsync data errno=%ld", err);
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900752 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900753
Namjae Jeon696c0182013-06-16 09:48:48 +0900754 /*
755 * If filesystem is not mounted as read-only then
756 * do start the gc_thread.
757 */
758 if (!(sb->s_flags & MS_RDONLY)) {
759 /* After POR, we can run background GC thread.*/
760 err = start_gc_thread(sbi);
761 if (err)
762 goto fail;
763 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900764
765 err = f2fs_build_stats(sbi);
766 if (err)
767 goto fail;
768
Namjae Jeond3ee4562013-03-17 17:26:14 +0900769 if (test_opt(sbi, DISCARD)) {
770 struct request_queue *q = bdev_get_queue(sb->s_bdev);
771 if (!blk_queue_discard(q))
772 f2fs_msg(sb, KERN_WARNING,
773 "mounting with \"discard\" option, but "
774 "the device does not support discard");
775 }
776
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900777 return 0;
778fail:
779 stop_gc_thread(sbi);
780free_root_inode:
781 dput(sb->s_root);
782 sb->s_root = NULL;
783free_node_inode:
784 iput(sbi->node_inode);
785free_nm:
786 destroy_node_manager(sbi);
787free_sm:
788 destroy_segment_manager(sbi);
789free_cp:
790 kfree(sbi->ckpt);
791free_meta_inode:
792 make_bad_inode(sbi->meta_inode);
793 iput(sbi->meta_inode);
794free_sb_buf:
795 brelse(raw_super_buf);
796free_sbi:
797 kfree(sbi);
798 return err;
799}
800
801static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
802 const char *dev_name, void *data)
803{
804 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
805}
806
807static struct file_system_type f2fs_fs_type = {
808 .owner = THIS_MODULE,
809 .name = "f2fs",
810 .mount = f2fs_mount,
811 .kill_sb = kill_block_super,
812 .fs_flags = FS_REQUIRES_DEV,
813};
Eric W. Biederman7f78e032013-03-02 19:39:14 -0800814MODULE_ALIAS_FS("f2fs");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900815
Namjae Jeon6e6093a2013-01-17 00:08:30 +0900816static int __init init_inodecache(void)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900817{
818 f2fs_inode_cachep = f2fs_kmem_cache_create("f2fs_inode_cache",
819 sizeof(struct f2fs_inode_info), NULL);
820 if (f2fs_inode_cachep == NULL)
821 return -ENOMEM;
822 return 0;
823}
824
825static void destroy_inodecache(void)
826{
827 /*
828 * Make sure all delayed rcu free inodes are flushed before we
829 * destroy cache.
830 */
831 rcu_barrier();
832 kmem_cache_destroy(f2fs_inode_cachep);
833}
834
835static int __init init_f2fs_fs(void)
836{
837 int err;
838
839 err = init_inodecache();
840 if (err)
841 goto fail;
842 err = create_node_manager_caches();
843 if (err)
844 goto fail;
845 err = create_gc_caches();
846 if (err)
847 goto fail;
848 err = create_checkpoint_caches();
849 if (err)
850 goto fail;
Namjae Jeon4589d252013-01-15 19:58:47 +0900851 err = register_filesystem(&f2fs_fs_type);
852 if (err)
853 goto fail;
854 f2fs_create_root_stats();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900855fail:
856 return err;
857}
858
859static void __exit exit_f2fs_fs(void)
860{
Namjae Jeon4589d252013-01-15 19:58:47 +0900861 f2fs_destroy_root_stats();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900862 unregister_filesystem(&f2fs_fs_type);
863 destroy_checkpoint_caches();
864 destroy_gc_caches();
865 destroy_node_manager_caches();
866 destroy_inodecache();
867}
868
869module_init(init_f2fs_fs)
870module_exit(exit_f2fs_fs)
871
872MODULE_AUTHOR("Samsung Electronics's Praesto Team");
873MODULE_DESCRIPTION("Flash Friendly File System");
874MODULE_LICENSE("GPL");