blob: ba56549bb2f35173d2dbc668f4ee1843b14db6ee [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 {
37 Opt_gc_background_off,
38 Opt_disable_roll_forward,
39 Opt_discard,
40 Opt_noheap,
41 Opt_nouser_xattr,
42 Opt_noacl,
43 Opt_active_logs,
44 Opt_disable_ext_identify,
45 Opt_err,
46};
47
48static match_table_t f2fs_tokens = {
49 {Opt_gc_background_off, "background_gc_off"},
50 {Opt_disable_roll_forward, "disable_roll_forward"},
51 {Opt_discard, "discard"},
52 {Opt_noheap, "no_heap"},
53 {Opt_nouser_xattr, "nouser_xattr"},
54 {Opt_noacl, "noacl"},
55 {Opt_active_logs, "active_logs=%u"},
56 {Opt_disable_ext_identify, "disable_ext_identify"},
57 {Opt_err, NULL},
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
79static struct inode *f2fs_alloc_inode(struct super_block *sb)
80{
81 struct f2fs_inode_info *fi;
82
83 fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_NOFS | __GFP_ZERO);
84 if (!fi)
85 return NULL;
86
87 init_once((void *) fi);
88
Masanari Iida434720f2013-03-19 08:03:35 +090089 /* Initialize f2fs-specific inode info */
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090090 fi->vfs_inode.i_version = 1;
91 atomic_set(&fi->dirty_dents, 0);
92 fi->i_current_depth = 1;
93 fi->i_advise = 0;
94 rwlock_init(&fi->ext.ext_lock);
95
96 set_inode_flag(fi, FI_NEW_INODE);
97
98 return &fi->vfs_inode;
99}
100
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900101static int f2fs_drop_inode(struct inode *inode)
102{
103 /*
104 * This is to avoid a deadlock condition like below.
105 * writeback_single_inode(inode)
106 * - f2fs_write_data_page
107 * - f2fs_gc -> iput -> evict
108 * - inode_wait_for_writeback(inode)
109 */
110 if (!inode_unhashed(inode) && inode->i_state & I_SYNC)
111 return 0;
112 return generic_drop_inode(inode);
113}
114
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900115/*
116 * f2fs_dirty_inode() is called from __mark_inode_dirty()
117 *
118 * We should call set_dirty_inode to write the dirty inode through write_inode.
119 */
120static void f2fs_dirty_inode(struct inode *inode, int flags)
121{
122 set_inode_flag(F2FS_I(inode), FI_DIRTY_INODE);
123 return;
124}
125
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900126static void f2fs_i_callback(struct rcu_head *head)
127{
128 struct inode *inode = container_of(head, struct inode, i_rcu);
129 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
130}
131
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900132static void f2fs_destroy_inode(struct inode *inode)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900133{
134 call_rcu(&inode->i_rcu, f2fs_i_callback);
135}
136
137static void f2fs_put_super(struct super_block *sb)
138{
139 struct f2fs_sb_info *sbi = F2FS_SB(sb);
140
141 f2fs_destroy_stats(sbi);
142 stop_gc_thread(sbi);
143
Jaegeuk Kim43727522013-02-04 15:11:17 +0900144 write_checkpoint(sbi, true);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900145
146 iput(sbi->node_inode);
147 iput(sbi->meta_inode);
148
149 /* destroy f2fs internal modules */
150 destroy_node_manager(sbi);
151 destroy_segment_manager(sbi);
152
153 kfree(sbi->ckpt);
154
155 sb->s_fs_info = NULL;
156 brelse(sbi->raw_super_buf);
157 kfree(sbi);
158}
159
160int f2fs_sync_fs(struct super_block *sb, int sync)
161{
162 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900163
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900164 trace_f2fs_sync_fs(sb, sync);
165
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900166 if (!sbi->s_dirty && !get_pages(sbi, F2FS_DIRTY_NODES))
167 return 0;
168
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900169 if (sync) {
170 mutex_lock(&sbi->gc_mutex);
Jaegeuk Kim43727522013-02-04 15:11:17 +0900171 write_checkpoint(sbi, false);
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900172 mutex_unlock(&sbi->gc_mutex);
173 } else {
Jaegeuk Kim7d82db82013-01-11 13:10:49 +0900174 f2fs_balance_fs(sbi);
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900175 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900176
Namjae Jeon64c576f2012-12-22 12:10:27 +0900177 return 0;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900178}
179
Changman Leed6212a52013-01-29 18:30:07 +0900180static int f2fs_freeze(struct super_block *sb)
181{
182 int err;
183
Jaegeuk Kim77888c12013-05-20 20:28:47 +0900184 if (f2fs_readonly(sb))
Changman Leed6212a52013-01-29 18:30:07 +0900185 return 0;
186
187 err = f2fs_sync_fs(sb, 1);
188 return err;
189}
190
191static int f2fs_unfreeze(struct super_block *sb)
192{
193 return 0;
194}
195
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900196static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
197{
198 struct super_block *sb = dentry->d_sb;
199 struct f2fs_sb_info *sbi = F2FS_SB(sb);
200 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
201 block_t total_count, user_block_count, start_count, ovp_count;
202
203 total_count = le64_to_cpu(sbi->raw_super->block_count);
204 user_block_count = sbi->user_block_count;
205 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
206 ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg;
207 buf->f_type = F2FS_SUPER_MAGIC;
208 buf->f_bsize = sbi->blocksize;
209
210 buf->f_blocks = total_count - start_count;
211 buf->f_bfree = buf->f_blocks - valid_user_blocks(sbi) - ovp_count;
212 buf->f_bavail = user_block_count - valid_user_blocks(sbi);
213
Jaegeuk Kim1362b5e2012-12-12 19:45:49 +0900214 buf->f_files = sbi->total_node_count;
215 buf->f_ffree = sbi->total_node_count - valid_inode_count(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900216
Jaegeuk Kim5a20d332013-03-03 13:58:05 +0900217 buf->f_namelen = F2FS_NAME_LEN;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900218 buf->f_fsid.val[0] = (u32)id;
219 buf->f_fsid.val[1] = (u32)(id >> 32);
220
221 return 0;
222}
223
224static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
225{
226 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
227
228 if (test_opt(sbi, BG_GC))
229 seq_puts(seq, ",background_gc_on");
230 else
231 seq_puts(seq, ",background_gc_off");
232 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
233 seq_puts(seq, ",disable_roll_forward");
234 if (test_opt(sbi, DISCARD))
235 seq_puts(seq, ",discard");
236 if (test_opt(sbi, NOHEAP))
237 seq_puts(seq, ",no_heap_alloc");
238#ifdef CONFIG_F2FS_FS_XATTR
239 if (test_opt(sbi, XATTR_USER))
240 seq_puts(seq, ",user_xattr");
241 else
242 seq_puts(seq, ",nouser_xattr");
243#endif
244#ifdef CONFIG_F2FS_FS_POSIX_ACL
245 if (test_opt(sbi, POSIX_ACL))
246 seq_puts(seq, ",acl");
247 else
248 seq_puts(seq, ",noacl");
249#endif
250 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
Alejandro Martinez Ruizaa435072013-01-25 19:08:59 +0100251 seq_puts(seq, ",disable_ext_identify");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900252
253 seq_printf(seq, ",active_logs=%u", sbi->active_logs);
254
255 return 0;
256}
257
258static struct super_operations f2fs_sops = {
259 .alloc_inode = f2fs_alloc_inode,
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900260 .drop_inode = f2fs_drop_inode,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900261 .destroy_inode = f2fs_destroy_inode,
262 .write_inode = f2fs_write_inode,
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900263 .dirty_inode = f2fs_dirty_inode,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900264 .show_options = f2fs_show_options,
265 .evict_inode = f2fs_evict_inode,
266 .put_super = f2fs_put_super,
267 .sync_fs = f2fs_sync_fs,
Changman Leed6212a52013-01-29 18:30:07 +0900268 .freeze_fs = f2fs_freeze,
269 .unfreeze_fs = f2fs_unfreeze,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900270 .statfs = f2fs_statfs,
271};
272
273static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
274 u64 ino, u32 generation)
275{
276 struct f2fs_sb_info *sbi = F2FS_SB(sb);
277 struct inode *inode;
278
279 if (ino < F2FS_ROOT_INO(sbi))
280 return ERR_PTR(-ESTALE);
281
282 /*
283 * f2fs_iget isn't quite right if the inode is currently unallocated!
284 * However f2fs_iget currently does appropriate checks to handle stale
285 * inodes so everything is OK.
286 */
287 inode = f2fs_iget(sb, ino);
288 if (IS_ERR(inode))
289 return ERR_CAST(inode);
290 if (generation && inode->i_generation != generation) {
291 /* we didn't find the right inode.. */
292 iput(inode);
293 return ERR_PTR(-ESTALE);
294 }
295 return inode;
296}
297
298static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
299 int fh_len, int fh_type)
300{
301 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
302 f2fs_nfs_get_inode);
303}
304
305static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
306 int fh_len, int fh_type)
307{
308 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
309 f2fs_nfs_get_inode);
310}
311
312static const struct export_operations f2fs_export_ops = {
313 .fh_to_dentry = f2fs_fh_to_dentry,
314 .fh_to_parent = f2fs_fh_to_parent,
315 .get_parent = f2fs_get_parent,
316};
317
Gu Zheng5fb08372013-06-07 14:16:53 +0800318static int parse_options(struct super_block *sb, char *options)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900319{
Gu Zheng5fb08372013-06-07 14:16:53 +0800320 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900321 substring_t args[MAX_OPT_ARGS];
322 char *p;
323 int arg = 0;
324
325 if (!options)
326 return 0;
327
328 while ((p = strsep(&options, ",")) != NULL) {
329 int token;
330 if (!*p)
331 continue;
332 /*
333 * Initialize args struct so we know whether arg was
334 * found; some options take optional arguments.
335 */
336 args[0].to = args[0].from = NULL;
337 token = match_token(p, f2fs_tokens, args);
338
339 switch (token) {
340 case Opt_gc_background_off:
341 clear_opt(sbi, BG_GC);
342 break;
343 case Opt_disable_roll_forward:
344 set_opt(sbi, DISABLE_ROLL_FORWARD);
345 break;
346 case Opt_discard:
347 set_opt(sbi, DISCARD);
348 break;
349 case Opt_noheap:
350 set_opt(sbi, NOHEAP);
351 break;
352#ifdef CONFIG_F2FS_FS_XATTR
353 case Opt_nouser_xattr:
354 clear_opt(sbi, XATTR_USER);
355 break;
356#else
357 case Opt_nouser_xattr:
Namjae Jeona07ef782012-12-30 14:52:05 +0900358 f2fs_msg(sb, KERN_INFO,
359 "nouser_xattr options not supported");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900360 break;
361#endif
362#ifdef CONFIG_F2FS_FS_POSIX_ACL
363 case Opt_noacl:
364 clear_opt(sbi, POSIX_ACL);
365 break;
366#else
367 case Opt_noacl:
Namjae Jeona07ef782012-12-30 14:52:05 +0900368 f2fs_msg(sb, KERN_INFO, "noacl options not supported");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900369 break;
370#endif
371 case Opt_active_logs:
372 if (args->from && match_int(args, &arg))
373 return -EINVAL;
Jaegeuk Kim12a67142012-12-21 11:47:05 +0900374 if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900375 return -EINVAL;
376 sbi->active_logs = arg;
377 break;
378 case Opt_disable_ext_identify:
379 set_opt(sbi, DISABLE_EXT_IDENTIFY);
380 break;
381 default:
Namjae Jeona07ef782012-12-30 14:52:05 +0900382 f2fs_msg(sb, KERN_ERR,
383 "Unrecognized mount option \"%s\" or missing value",
384 p);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900385 return -EINVAL;
386 }
387 }
388 return 0;
389}
390
391static loff_t max_file_size(unsigned bits)
392{
393 loff_t result = ADDRS_PER_INODE;
394 loff_t leaf_count = ADDRS_PER_BLOCK;
395
396 /* two direct node blocks */
397 result += (leaf_count * 2);
398
399 /* two indirect node blocks */
400 leaf_count *= NIDS_PER_BLOCK;
401 result += (leaf_count * 2);
402
403 /* one double indirect node block */
404 leaf_count *= NIDS_PER_BLOCK;
405 result += leaf_count;
406
407 result <<= bits;
408 return result;
409}
410
Namjae Jeona07ef782012-12-30 14:52:05 +0900411static int sanity_check_raw_super(struct super_block *sb,
412 struct f2fs_super_block *raw_super)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900413{
414 unsigned int blocksize;
415
Namjae Jeona07ef782012-12-30 14:52:05 +0900416 if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
417 f2fs_msg(sb, KERN_INFO,
418 "Magic Mismatch, valid(0x%x) - read(0x%x)",
419 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900420 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900421 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900422
majianpeng5c9b4692013-02-01 19:07:57 +0800423 /* Currently, support only 4KB page cache size */
424 if (F2FS_BLKSIZE != PAGE_CACHE_SIZE) {
425 f2fs_msg(sb, KERN_INFO,
majianpeng14d7e9d2013-02-01 19:07:03 +0800426 "Invalid page_cache_size (%lu), supports only 4KB\n",
majianpeng5c9b4692013-02-01 19:07:57 +0800427 PAGE_CACHE_SIZE);
428 return 1;
429 }
430
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900431 /* Currently, support only 4KB block size */
432 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
majianpeng5c9b4692013-02-01 19:07:57 +0800433 if (blocksize != F2FS_BLKSIZE) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900434 f2fs_msg(sb, KERN_INFO,
435 "Invalid blocksize (%u), supports only 4KB\n",
436 blocksize);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900437 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900438 }
majianpeng5c9b4692013-02-01 19:07:57 +0800439
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900440 if (le32_to_cpu(raw_super->log_sectorsize) !=
Namjae Jeona07ef782012-12-30 14:52:05 +0900441 F2FS_LOG_SECTOR_SIZE) {
442 f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900443 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900444 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900445 if (le32_to_cpu(raw_super->log_sectors_per_block) !=
Namjae Jeona07ef782012-12-30 14:52:05 +0900446 F2FS_LOG_SECTORS_PER_BLOCK) {
447 f2fs_msg(sb, KERN_INFO, "Invalid log sectors per block");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900448 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900449 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900450 return 0;
451}
452
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900453static int sanity_check_ckpt(struct f2fs_sb_info *sbi)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900454{
455 unsigned int total, fsmeta;
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900456 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
457 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900458
459 total = le32_to_cpu(raw_super->segment_count);
460 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
461 fsmeta += le32_to_cpu(raw_super->segment_count_sit);
462 fsmeta += le32_to_cpu(raw_super->segment_count_nat);
463 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
464 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
465
466 if (fsmeta >= total)
467 return 1;
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900468
469 if (is_set_ckpt_flags(ckpt, CP_ERROR_FLAG)) {
470 f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
471 return 1;
472 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900473 return 0;
474}
475
476static void init_sb_info(struct f2fs_sb_info *sbi)
477{
478 struct f2fs_super_block *raw_super = sbi->raw_super;
479 int i;
480
481 sbi->log_sectors_per_block =
482 le32_to_cpu(raw_super->log_sectors_per_block);
483 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
484 sbi->blocksize = 1 << sbi->log_blocksize;
485 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
486 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
487 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
488 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
489 sbi->total_sections = le32_to_cpu(raw_super->section_count);
490 sbi->total_node_count =
491 (le32_to_cpu(raw_super->segment_count_nat) / 2)
492 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
493 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
494 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
495 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900496 sbi->cur_victim_sec = NULL_SECNO;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900497
498 for (i = 0; i < NR_COUNT_TYPE; i++)
499 atomic_set(&sbi->nr_pages[i], 0);
500}
501
majianpeng14d7e9d2013-02-01 19:07:03 +0800502static int validate_superblock(struct super_block *sb,
503 struct f2fs_super_block **raw_super,
504 struct buffer_head **raw_super_buf, sector_t block)
505{
506 const char *super = (block == 0 ? "first" : "second");
507
508 /* read f2fs raw super block */
509 *raw_super_buf = sb_bread(sb, block);
510 if (!*raw_super_buf) {
511 f2fs_msg(sb, KERN_ERR, "unable to read %s superblock",
512 super);
Namjae Jeonc0d39e62013-03-17 17:26:53 +0900513 return -EIO;
majianpeng14d7e9d2013-02-01 19:07:03 +0800514 }
515
516 *raw_super = (struct f2fs_super_block *)
517 ((char *)(*raw_super_buf)->b_data + F2FS_SUPER_OFFSET);
518
519 /* sanity checking of raw super */
520 if (!sanity_check_raw_super(sb, *raw_super))
521 return 0;
522
523 f2fs_msg(sb, KERN_ERR, "Can't find a valid F2FS filesystem "
524 "in %s superblock", super);
Namjae Jeonc0d39e62013-03-17 17:26:53 +0900525 return -EINVAL;
majianpeng14d7e9d2013-02-01 19:07:03 +0800526}
527
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900528static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
529{
530 struct f2fs_sb_info *sbi;
531 struct f2fs_super_block *raw_super;
532 struct buffer_head *raw_super_buf;
533 struct inode *root;
534 long err = -EINVAL;
535 int i;
536
537 /* allocate memory for f2fs-specific super block info */
538 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
539 if (!sbi)
540 return -ENOMEM;
541
Namjae Jeonff9234a2013-01-12 14:41:13 +0900542 /* set a block size */
Namjae Jeona07ef782012-12-30 14:52:05 +0900543 if (!sb_set_blocksize(sb, F2FS_BLKSIZE)) {
544 f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900545 goto free_sbi;
Namjae Jeona07ef782012-12-30 14:52:05 +0900546 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900547
Namjae Jeonc0d39e62013-03-17 17:26:53 +0900548 err = validate_superblock(sb, &raw_super, &raw_super_buf, 0);
549 if (err) {
majianpeng14d7e9d2013-02-01 19:07:03 +0800550 brelse(raw_super_buf);
Namjae Jeonc0d39e62013-03-17 17:26:53 +0900551 /* check secondary superblock when primary failed */
552 err = validate_superblock(sb, &raw_super, &raw_super_buf, 1);
553 if (err)
majianpeng14d7e9d2013-02-01 19:07:03 +0800554 goto free_sb_buf;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900555 }
Gu Zheng5fb08372013-06-07 14:16:53 +0800556 sb->s_fs_info = sbi;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900557 /* init some FS parameters */
558 sbi->active_logs = NR_CURSEG_TYPE;
559
560 set_opt(sbi, BG_GC);
561
562#ifdef CONFIG_F2FS_FS_XATTR
563 set_opt(sbi, XATTR_USER);
564#endif
565#ifdef CONFIG_F2FS_FS_POSIX_ACL
566 set_opt(sbi, POSIX_ACL);
567#endif
568 /* parse mount options */
Gu Zheng5fb08372013-06-07 14:16:53 +0800569 err = parse_options(sb, (char *)data);
Wei Yongjun66348b72013-04-12 10:23:18 +0800570 if (err)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900571 goto free_sb_buf;
572
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900573 sb->s_maxbytes = max_file_size(le32_to_cpu(raw_super->log_blocksize));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900574 sb->s_max_links = F2FS_LINK_MAX;
575 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
576
577 sb->s_op = &f2fs_sops;
578 sb->s_xattr = f2fs_xattr_handlers;
579 sb->s_export_op = &f2fs_export_ops;
580 sb->s_magic = F2FS_SUPER_MAGIC;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900581 sb->s_time_gran = 1;
582 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
583 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
584 memcpy(sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
585
586 /* init f2fs-specific super block info */
587 sbi->sb = sb;
588 sbi->raw_super = raw_super;
589 sbi->raw_super_buf = raw_super_buf;
590 mutex_init(&sbi->gc_mutex);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900591 mutex_init(&sbi->writepages);
592 mutex_init(&sbi->cp_mutex);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900593 for (i = 0; i < NR_GLOBAL_LOCKS; i++)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900594 mutex_init(&sbi->fs_lock[i]);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900595 mutex_init(&sbi->node_write);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900596 sbi->por_doing = 0;
597 spin_lock_init(&sbi->stat_lock);
598 init_rwsem(&sbi->bio_sem);
599 init_sb_info(sbi);
600
601 /* get an inode for meta space */
602 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
603 if (IS_ERR(sbi->meta_inode)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900604 f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900605 err = PTR_ERR(sbi->meta_inode);
606 goto free_sb_buf;
607 }
608
609 err = get_valid_checkpoint(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +0900610 if (err) {
611 f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900612 goto free_meta_inode;
Namjae Jeona07ef782012-12-30 14:52:05 +0900613 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900614
615 /* sanity checking of checkpoint */
616 err = -EINVAL;
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900617 if (sanity_check_ckpt(sbi)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900618 f2fs_msg(sb, KERN_ERR, "Invalid F2FS checkpoint");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900619 goto free_cp;
Namjae Jeona07ef782012-12-30 14:52:05 +0900620 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900621
622 sbi->total_valid_node_count =
623 le32_to_cpu(sbi->ckpt->valid_node_count);
624 sbi->total_valid_inode_count =
625 le32_to_cpu(sbi->ckpt->valid_inode_count);
626 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
627 sbi->total_valid_block_count =
628 le64_to_cpu(sbi->ckpt->valid_block_count);
629 sbi->last_valid_block_count = sbi->total_valid_block_count;
630 sbi->alloc_valid_block_count = 0;
631 INIT_LIST_HEAD(&sbi->dir_inode_list);
632 spin_lock_init(&sbi->dir_inode_lock);
633
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900634 init_orphan_info(sbi);
635
636 /* setup f2fs internal modules */
637 err = build_segment_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +0900638 if (err) {
639 f2fs_msg(sb, KERN_ERR,
640 "Failed to initialize F2FS segment manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900641 goto free_sm;
Namjae Jeona07ef782012-12-30 14:52:05 +0900642 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900643 err = build_node_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +0900644 if (err) {
645 f2fs_msg(sb, KERN_ERR,
646 "Failed to initialize F2FS node manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900647 goto free_nm;
Namjae Jeona07ef782012-12-30 14:52:05 +0900648 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900649
650 build_gc_manager(sbi);
651
652 /* get an inode for node space */
653 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
654 if (IS_ERR(sbi->node_inode)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900655 f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900656 err = PTR_ERR(sbi->node_inode);
657 goto free_nm;
658 }
659
660 /* if there are nt orphan nodes free them */
661 err = -EINVAL;
Jaegeuk Kim30f0c752012-12-19 16:09:19 +0900662 if (recover_orphan_inodes(sbi))
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900663 goto free_node_inode;
664
665 /* read root inode and dentry */
666 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
667 if (IS_ERR(root)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900668 f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900669 err = PTR_ERR(root);
670 goto free_node_inode;
671 }
672 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size)
673 goto free_root_inode;
674
675 sb->s_root = d_make_root(root); /* allocate root dentry */
676 if (!sb->s_root) {
677 err = -ENOMEM;
678 goto free_root_inode;
679 }
680
681 /* recover fsynced data */
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900682 if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
683 err = recover_fsync_data(sbi);
Chris Friesbde582b2013-05-02 16:07:34 -0500684 if (err)
685 f2fs_msg(sb, KERN_ERR,
686 "Cannot recover all fsync data errno=%ld", err);
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900687 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900688
689 /* After POR, we can run background GC thread */
690 err = start_gc_thread(sbi);
691 if (err)
692 goto fail;
693
694 err = f2fs_build_stats(sbi);
695 if (err)
696 goto fail;
697
Namjae Jeond3ee4562013-03-17 17:26:14 +0900698 if (test_opt(sbi, DISCARD)) {
699 struct request_queue *q = bdev_get_queue(sb->s_bdev);
700 if (!blk_queue_discard(q))
701 f2fs_msg(sb, KERN_WARNING,
702 "mounting with \"discard\" option, but "
703 "the device does not support discard");
704 }
705
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900706 return 0;
707fail:
708 stop_gc_thread(sbi);
709free_root_inode:
710 dput(sb->s_root);
711 sb->s_root = NULL;
712free_node_inode:
713 iput(sbi->node_inode);
714free_nm:
715 destroy_node_manager(sbi);
716free_sm:
717 destroy_segment_manager(sbi);
718free_cp:
719 kfree(sbi->ckpt);
720free_meta_inode:
721 make_bad_inode(sbi->meta_inode);
722 iput(sbi->meta_inode);
723free_sb_buf:
724 brelse(raw_super_buf);
725free_sbi:
726 kfree(sbi);
727 return err;
728}
729
730static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
731 const char *dev_name, void *data)
732{
733 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
734}
735
736static struct file_system_type f2fs_fs_type = {
737 .owner = THIS_MODULE,
738 .name = "f2fs",
739 .mount = f2fs_mount,
740 .kill_sb = kill_block_super,
741 .fs_flags = FS_REQUIRES_DEV,
742};
Eric W. Biederman7f78e032013-03-02 19:39:14 -0800743MODULE_ALIAS_FS("f2fs");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900744
Namjae Jeon6e6093a2013-01-17 00:08:30 +0900745static int __init init_inodecache(void)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900746{
747 f2fs_inode_cachep = f2fs_kmem_cache_create("f2fs_inode_cache",
748 sizeof(struct f2fs_inode_info), NULL);
749 if (f2fs_inode_cachep == NULL)
750 return -ENOMEM;
751 return 0;
752}
753
754static void destroy_inodecache(void)
755{
756 /*
757 * Make sure all delayed rcu free inodes are flushed before we
758 * destroy cache.
759 */
760 rcu_barrier();
761 kmem_cache_destroy(f2fs_inode_cachep);
762}
763
764static int __init init_f2fs_fs(void)
765{
766 int err;
767
768 err = init_inodecache();
769 if (err)
770 goto fail;
771 err = create_node_manager_caches();
772 if (err)
773 goto fail;
774 err = create_gc_caches();
775 if (err)
776 goto fail;
777 err = create_checkpoint_caches();
778 if (err)
779 goto fail;
Namjae Jeon4589d252013-01-15 19:58:47 +0900780 err = register_filesystem(&f2fs_fs_type);
781 if (err)
782 goto fail;
783 f2fs_create_root_stats();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900784fail:
785 return err;
786}
787
788static void __exit exit_f2fs_fs(void)
789{
Namjae Jeon4589d252013-01-15 19:58:47 +0900790 f2fs_destroy_root_stats();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900791 unregister_filesystem(&f2fs_fs_type);
792 destroy_checkpoint_caches();
793 destroy_gc_caches();
794 destroy_node_manager_caches();
795 destroy_inodecache();
796}
797
798module_init(init_f2fs_fs)
799module_exit(exit_f2fs_fs)
800
801MODULE_AUTHOR("Samsung Electronics's Praesto Team");
802MODULE_DESCRIPTION("Flash Friendly File System");
803MODULE_LICENSE("GPL");