blob: 5835aaf2fe2e3d0ababca0ffcf5b5e0545d39456 [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 Iida111d2492013-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
101static void f2fs_i_callback(struct rcu_head *head)
102{
103 struct inode *inode = container_of(head, struct inode, i_rcu);
104 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
105}
106
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900107static void f2fs_destroy_inode(struct inode *inode)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900108{
109 call_rcu(&inode->i_rcu, f2fs_i_callback);
110}
111
112static void f2fs_put_super(struct super_block *sb)
113{
114 struct f2fs_sb_info *sbi = F2FS_SB(sb);
115
116 f2fs_destroy_stats(sbi);
117 stop_gc_thread(sbi);
118
Jaegeuk Kim43727522013-02-04 15:11:17 +0900119 write_checkpoint(sbi, true);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900120
121 iput(sbi->node_inode);
122 iput(sbi->meta_inode);
123
124 /* destroy f2fs internal modules */
125 destroy_node_manager(sbi);
126 destroy_segment_manager(sbi);
127
128 kfree(sbi->ckpt);
129
130 sb->s_fs_info = NULL;
131 brelse(sbi->raw_super_buf);
132 kfree(sbi);
133}
134
135int f2fs_sync_fs(struct super_block *sb, int sync)
136{
137 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900138
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900139 trace_f2fs_sync_fs(sb, sync);
140
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900141 if (!sbi->s_dirty && !get_pages(sbi, F2FS_DIRTY_NODES))
142 return 0;
143
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900144 if (sync) {
145 mutex_lock(&sbi->gc_mutex);
Jaegeuk Kim43727522013-02-04 15:11:17 +0900146 write_checkpoint(sbi, false);
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900147 mutex_unlock(&sbi->gc_mutex);
148 } else {
Jaegeuk Kim7d82db82013-01-11 13:10:49 +0900149 f2fs_balance_fs(sbi);
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900150 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900151
Namjae Jeon64c576f2012-12-22 12:10:27 +0900152 return 0;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900153}
154
Changman Leed6212a52013-01-29 18:30:07 +0900155static int f2fs_freeze(struct super_block *sb)
156{
157 int err;
158
159 if (sb->s_flags & MS_RDONLY)
160 return 0;
161
162 err = f2fs_sync_fs(sb, 1);
163 return err;
164}
165
166static int f2fs_unfreeze(struct super_block *sb)
167{
168 return 0;
169}
170
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900171static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
172{
173 struct super_block *sb = dentry->d_sb;
174 struct f2fs_sb_info *sbi = F2FS_SB(sb);
175 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
176 block_t total_count, user_block_count, start_count, ovp_count;
177
178 total_count = le64_to_cpu(sbi->raw_super->block_count);
179 user_block_count = sbi->user_block_count;
180 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
181 ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg;
182 buf->f_type = F2FS_SUPER_MAGIC;
183 buf->f_bsize = sbi->blocksize;
184
185 buf->f_blocks = total_count - start_count;
186 buf->f_bfree = buf->f_blocks - valid_user_blocks(sbi) - ovp_count;
187 buf->f_bavail = user_block_count - valid_user_blocks(sbi);
188
Jaegeuk Kim1362b5e2012-12-12 19:45:49 +0900189 buf->f_files = sbi->total_node_count;
190 buf->f_ffree = sbi->total_node_count - valid_inode_count(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900191
Jaegeuk Kim5a20d332013-03-03 13:58:05 +0900192 buf->f_namelen = F2FS_NAME_LEN;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900193 buf->f_fsid.val[0] = (u32)id;
194 buf->f_fsid.val[1] = (u32)(id >> 32);
195
196 return 0;
197}
198
199static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
200{
201 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
202
203 if (test_opt(sbi, BG_GC))
204 seq_puts(seq, ",background_gc_on");
205 else
206 seq_puts(seq, ",background_gc_off");
207 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
208 seq_puts(seq, ",disable_roll_forward");
209 if (test_opt(sbi, DISCARD))
210 seq_puts(seq, ",discard");
211 if (test_opt(sbi, NOHEAP))
212 seq_puts(seq, ",no_heap_alloc");
213#ifdef CONFIG_F2FS_FS_XATTR
214 if (test_opt(sbi, XATTR_USER))
215 seq_puts(seq, ",user_xattr");
216 else
217 seq_puts(seq, ",nouser_xattr");
218#endif
219#ifdef CONFIG_F2FS_FS_POSIX_ACL
220 if (test_opt(sbi, POSIX_ACL))
221 seq_puts(seq, ",acl");
222 else
223 seq_puts(seq, ",noacl");
224#endif
225 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
Alejandro Martinez Ruizaa435072013-01-25 19:08:59 +0100226 seq_puts(seq, ",disable_ext_identify");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900227
228 seq_printf(seq, ",active_logs=%u", sbi->active_logs);
229
230 return 0;
231}
232
233static struct super_operations f2fs_sops = {
234 .alloc_inode = f2fs_alloc_inode,
235 .destroy_inode = f2fs_destroy_inode,
236 .write_inode = f2fs_write_inode,
237 .show_options = f2fs_show_options,
238 .evict_inode = f2fs_evict_inode,
239 .put_super = f2fs_put_super,
240 .sync_fs = f2fs_sync_fs,
Changman Leed6212a52013-01-29 18:30:07 +0900241 .freeze_fs = f2fs_freeze,
242 .unfreeze_fs = f2fs_unfreeze,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900243 .statfs = f2fs_statfs,
244};
245
246static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
247 u64 ino, u32 generation)
248{
249 struct f2fs_sb_info *sbi = F2FS_SB(sb);
250 struct inode *inode;
251
252 if (ino < F2FS_ROOT_INO(sbi))
253 return ERR_PTR(-ESTALE);
254
255 /*
256 * f2fs_iget isn't quite right if the inode is currently unallocated!
257 * However f2fs_iget currently does appropriate checks to handle stale
258 * inodes so everything is OK.
259 */
260 inode = f2fs_iget(sb, ino);
261 if (IS_ERR(inode))
262 return ERR_CAST(inode);
263 if (generation && inode->i_generation != generation) {
264 /* we didn't find the right inode.. */
265 iput(inode);
266 return ERR_PTR(-ESTALE);
267 }
268 return inode;
269}
270
271static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
272 int fh_len, int fh_type)
273{
274 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
275 f2fs_nfs_get_inode);
276}
277
278static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
279 int fh_len, int fh_type)
280{
281 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
282 f2fs_nfs_get_inode);
283}
284
285static const struct export_operations f2fs_export_ops = {
286 .fh_to_dentry = f2fs_fh_to_dentry,
287 .fh_to_parent = f2fs_fh_to_parent,
288 .get_parent = f2fs_get_parent,
289};
290
Namjae Jeona07ef782012-12-30 14:52:05 +0900291static int parse_options(struct super_block *sb, struct f2fs_sb_info *sbi,
292 char *options)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900293{
294 substring_t args[MAX_OPT_ARGS];
295 char *p;
296 int arg = 0;
297
298 if (!options)
299 return 0;
300
301 while ((p = strsep(&options, ",")) != NULL) {
302 int token;
303 if (!*p)
304 continue;
305 /*
306 * Initialize args struct so we know whether arg was
307 * found; some options take optional arguments.
308 */
309 args[0].to = args[0].from = NULL;
310 token = match_token(p, f2fs_tokens, args);
311
312 switch (token) {
313 case Opt_gc_background_off:
314 clear_opt(sbi, BG_GC);
315 break;
316 case Opt_disable_roll_forward:
317 set_opt(sbi, DISABLE_ROLL_FORWARD);
318 break;
319 case Opt_discard:
320 set_opt(sbi, DISCARD);
321 break;
322 case Opt_noheap:
323 set_opt(sbi, NOHEAP);
324 break;
325#ifdef CONFIG_F2FS_FS_XATTR
326 case Opt_nouser_xattr:
327 clear_opt(sbi, XATTR_USER);
328 break;
329#else
330 case Opt_nouser_xattr:
Namjae Jeona07ef782012-12-30 14:52:05 +0900331 f2fs_msg(sb, KERN_INFO,
332 "nouser_xattr options not supported");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900333 break;
334#endif
335#ifdef CONFIG_F2FS_FS_POSIX_ACL
336 case Opt_noacl:
337 clear_opt(sbi, POSIX_ACL);
338 break;
339#else
340 case Opt_noacl:
Namjae Jeona07ef782012-12-30 14:52:05 +0900341 f2fs_msg(sb, KERN_INFO, "noacl options not supported");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900342 break;
343#endif
344 case Opt_active_logs:
345 if (args->from && match_int(args, &arg))
346 return -EINVAL;
Jaegeuk Kim12a67142012-12-21 11:47:05 +0900347 if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900348 return -EINVAL;
349 sbi->active_logs = arg;
350 break;
351 case Opt_disable_ext_identify:
352 set_opt(sbi, DISABLE_EXT_IDENTIFY);
353 break;
354 default:
Namjae Jeona07ef782012-12-30 14:52:05 +0900355 f2fs_msg(sb, KERN_ERR,
356 "Unrecognized mount option \"%s\" or missing value",
357 p);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900358 return -EINVAL;
359 }
360 }
361 return 0;
362}
363
364static loff_t max_file_size(unsigned bits)
365{
366 loff_t result = ADDRS_PER_INODE;
367 loff_t leaf_count = ADDRS_PER_BLOCK;
368
369 /* two direct node blocks */
370 result += (leaf_count * 2);
371
372 /* two indirect node blocks */
373 leaf_count *= NIDS_PER_BLOCK;
374 result += (leaf_count * 2);
375
376 /* one double indirect node block */
377 leaf_count *= NIDS_PER_BLOCK;
378 result += leaf_count;
379
380 result <<= bits;
381 return result;
382}
383
Namjae Jeona07ef782012-12-30 14:52:05 +0900384static int sanity_check_raw_super(struct super_block *sb,
385 struct f2fs_super_block *raw_super)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900386{
387 unsigned int blocksize;
388
Namjae Jeona07ef782012-12-30 14:52:05 +0900389 if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
390 f2fs_msg(sb, KERN_INFO,
391 "Magic Mismatch, valid(0x%x) - read(0x%x)",
392 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900393 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900394 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900395
majianpeng5c9b4692013-02-01 19:07:57 +0800396 /* Currently, support only 4KB page cache size */
397 if (F2FS_BLKSIZE != PAGE_CACHE_SIZE) {
398 f2fs_msg(sb, KERN_INFO,
majianpeng14d7e9d2013-02-01 19:07:03 +0800399 "Invalid page_cache_size (%lu), supports only 4KB\n",
majianpeng5c9b4692013-02-01 19:07:57 +0800400 PAGE_CACHE_SIZE);
401 return 1;
402 }
403
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900404 /* Currently, support only 4KB block size */
405 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
majianpeng5c9b4692013-02-01 19:07:57 +0800406 if (blocksize != F2FS_BLKSIZE) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900407 f2fs_msg(sb, KERN_INFO,
408 "Invalid blocksize (%u), supports only 4KB\n",
409 blocksize);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900410 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900411 }
majianpeng5c9b4692013-02-01 19:07:57 +0800412
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900413 if (le32_to_cpu(raw_super->log_sectorsize) !=
Namjae Jeona07ef782012-12-30 14:52:05 +0900414 F2FS_LOG_SECTOR_SIZE) {
415 f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900416 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900417 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900418 if (le32_to_cpu(raw_super->log_sectors_per_block) !=
Namjae Jeona07ef782012-12-30 14:52:05 +0900419 F2FS_LOG_SECTORS_PER_BLOCK) {
420 f2fs_msg(sb, KERN_INFO, "Invalid log sectors per block");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900421 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900422 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900423 return 0;
424}
425
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900426static int sanity_check_ckpt(struct f2fs_sb_info *sbi)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900427{
428 unsigned int total, fsmeta;
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900429 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
430 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900431
432 total = le32_to_cpu(raw_super->segment_count);
433 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
434 fsmeta += le32_to_cpu(raw_super->segment_count_sit);
435 fsmeta += le32_to_cpu(raw_super->segment_count_nat);
436 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
437 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
438
439 if (fsmeta >= total)
440 return 1;
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900441
442 if (is_set_ckpt_flags(ckpt, CP_ERROR_FLAG)) {
443 f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
444 return 1;
445 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900446 return 0;
447}
448
449static void init_sb_info(struct f2fs_sb_info *sbi)
450{
451 struct f2fs_super_block *raw_super = sbi->raw_super;
452 int i;
453
454 sbi->log_sectors_per_block =
455 le32_to_cpu(raw_super->log_sectors_per_block);
456 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
457 sbi->blocksize = 1 << sbi->log_blocksize;
458 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
459 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
460 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
461 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
462 sbi->total_sections = le32_to_cpu(raw_super->section_count);
463 sbi->total_node_count =
464 (le32_to_cpu(raw_super->segment_count_nat) / 2)
465 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
466 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
467 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
468 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900469 sbi->cur_victim_sec = NULL_SECNO;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900470
471 for (i = 0; i < NR_COUNT_TYPE; i++)
472 atomic_set(&sbi->nr_pages[i], 0);
473}
474
majianpeng14d7e9d2013-02-01 19:07:03 +0800475static int validate_superblock(struct super_block *sb,
476 struct f2fs_super_block **raw_super,
477 struct buffer_head **raw_super_buf, sector_t block)
478{
479 const char *super = (block == 0 ? "first" : "second");
480
481 /* read f2fs raw super block */
482 *raw_super_buf = sb_bread(sb, block);
483 if (!*raw_super_buf) {
484 f2fs_msg(sb, KERN_ERR, "unable to read %s superblock",
485 super);
Namjae Jeonc0d39e62013-03-17 17:26:53 +0900486 return -EIO;
majianpeng14d7e9d2013-02-01 19:07:03 +0800487 }
488
489 *raw_super = (struct f2fs_super_block *)
490 ((char *)(*raw_super_buf)->b_data + F2FS_SUPER_OFFSET);
491
492 /* sanity checking of raw super */
493 if (!sanity_check_raw_super(sb, *raw_super))
494 return 0;
495
496 f2fs_msg(sb, KERN_ERR, "Can't find a valid F2FS filesystem "
497 "in %s superblock", super);
Namjae Jeonc0d39e62013-03-17 17:26:53 +0900498 return -EINVAL;
majianpeng14d7e9d2013-02-01 19:07:03 +0800499}
500
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900501static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
502{
503 struct f2fs_sb_info *sbi;
504 struct f2fs_super_block *raw_super;
505 struct buffer_head *raw_super_buf;
506 struct inode *root;
507 long err = -EINVAL;
508 int i;
509
510 /* allocate memory for f2fs-specific super block info */
511 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
512 if (!sbi)
513 return -ENOMEM;
514
Namjae Jeonff9234a2013-01-12 14:41:13 +0900515 /* set a block size */
Namjae Jeona07ef782012-12-30 14:52:05 +0900516 if (!sb_set_blocksize(sb, F2FS_BLKSIZE)) {
517 f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900518 goto free_sbi;
Namjae Jeona07ef782012-12-30 14:52:05 +0900519 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900520
Namjae Jeonc0d39e62013-03-17 17:26:53 +0900521 err = validate_superblock(sb, &raw_super, &raw_super_buf, 0);
522 if (err) {
majianpeng14d7e9d2013-02-01 19:07:03 +0800523 brelse(raw_super_buf);
Namjae Jeonc0d39e62013-03-17 17:26:53 +0900524 /* check secondary superblock when primary failed */
525 err = validate_superblock(sb, &raw_super, &raw_super_buf, 1);
526 if (err)
majianpeng14d7e9d2013-02-01 19:07:03 +0800527 goto free_sb_buf;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900528 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900529 /* init some FS parameters */
530 sbi->active_logs = NR_CURSEG_TYPE;
531
532 set_opt(sbi, BG_GC);
533
534#ifdef CONFIG_F2FS_FS_XATTR
535 set_opt(sbi, XATTR_USER);
536#endif
537#ifdef CONFIG_F2FS_FS_POSIX_ACL
538 set_opt(sbi, POSIX_ACL);
539#endif
540 /* parse mount options */
Wei Yongjun66348b72013-04-12 10:23:18 +0800541 err = parse_options(sb, sbi, (char *)data);
542 if (err)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900543 goto free_sb_buf;
544
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900545 sb->s_maxbytes = max_file_size(le32_to_cpu(raw_super->log_blocksize));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900546 sb->s_max_links = F2FS_LINK_MAX;
547 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
548
549 sb->s_op = &f2fs_sops;
550 sb->s_xattr = f2fs_xattr_handlers;
551 sb->s_export_op = &f2fs_export_ops;
552 sb->s_magic = F2FS_SUPER_MAGIC;
553 sb->s_fs_info = sbi;
554 sb->s_time_gran = 1;
555 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
556 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
557 memcpy(sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
558
559 /* init f2fs-specific super block info */
560 sbi->sb = sb;
561 sbi->raw_super = raw_super;
562 sbi->raw_super_buf = raw_super_buf;
563 mutex_init(&sbi->gc_mutex);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900564 mutex_init(&sbi->writepages);
565 mutex_init(&sbi->cp_mutex);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900566 for (i = 0; i < NR_GLOBAL_LOCKS; i++)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900567 mutex_init(&sbi->fs_lock[i]);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900568 mutex_init(&sbi->node_write);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900569 sbi->por_doing = 0;
570 spin_lock_init(&sbi->stat_lock);
571 init_rwsem(&sbi->bio_sem);
572 init_sb_info(sbi);
573
574 /* get an inode for meta space */
575 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
576 if (IS_ERR(sbi->meta_inode)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900577 f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900578 err = PTR_ERR(sbi->meta_inode);
579 goto free_sb_buf;
580 }
581
582 err = get_valid_checkpoint(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +0900583 if (err) {
584 f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900585 goto free_meta_inode;
Namjae Jeona07ef782012-12-30 14:52:05 +0900586 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900587
588 /* sanity checking of checkpoint */
589 err = -EINVAL;
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900590 if (sanity_check_ckpt(sbi)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900591 f2fs_msg(sb, KERN_ERR, "Invalid F2FS checkpoint");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900592 goto free_cp;
Namjae Jeona07ef782012-12-30 14:52:05 +0900593 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900594
595 sbi->total_valid_node_count =
596 le32_to_cpu(sbi->ckpt->valid_node_count);
597 sbi->total_valid_inode_count =
598 le32_to_cpu(sbi->ckpt->valid_inode_count);
599 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
600 sbi->total_valid_block_count =
601 le64_to_cpu(sbi->ckpt->valid_block_count);
602 sbi->last_valid_block_count = sbi->total_valid_block_count;
603 sbi->alloc_valid_block_count = 0;
604 INIT_LIST_HEAD(&sbi->dir_inode_list);
605 spin_lock_init(&sbi->dir_inode_lock);
606
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900607 init_orphan_info(sbi);
608
609 /* setup f2fs internal modules */
610 err = build_segment_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +0900611 if (err) {
612 f2fs_msg(sb, KERN_ERR,
613 "Failed to initialize F2FS segment manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900614 goto free_sm;
Namjae Jeona07ef782012-12-30 14:52:05 +0900615 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900616 err = build_node_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +0900617 if (err) {
618 f2fs_msg(sb, KERN_ERR,
619 "Failed to initialize F2FS node manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900620 goto free_nm;
Namjae Jeona07ef782012-12-30 14:52:05 +0900621 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900622
623 build_gc_manager(sbi);
624
625 /* get an inode for node space */
626 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
627 if (IS_ERR(sbi->node_inode)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900628 f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900629 err = PTR_ERR(sbi->node_inode);
630 goto free_nm;
631 }
632
633 /* if there are nt orphan nodes free them */
634 err = -EINVAL;
Jaegeuk Kim30f0c752012-12-19 16:09:19 +0900635 if (recover_orphan_inodes(sbi))
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900636 goto free_node_inode;
637
638 /* read root inode and dentry */
639 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
640 if (IS_ERR(root)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900641 f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900642 err = PTR_ERR(root);
643 goto free_node_inode;
644 }
645 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size)
646 goto free_root_inode;
647
648 sb->s_root = d_make_root(root); /* allocate root dentry */
649 if (!sb->s_root) {
650 err = -ENOMEM;
651 goto free_root_inode;
652 }
653
654 /* recover fsynced data */
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900655 if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
656 err = recover_fsync_data(sbi);
657 if (err) {
658 f2fs_msg(sb, KERN_ERR, "Failed to recover fsync data");
659 goto free_root_inode;
660 }
661 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900662
663 /* After POR, we can run background GC thread */
664 err = start_gc_thread(sbi);
665 if (err)
666 goto fail;
667
668 err = f2fs_build_stats(sbi);
669 if (err)
670 goto fail;
671
Namjae Jeond3ee4562013-03-17 17:26:14 +0900672 if (test_opt(sbi, DISCARD)) {
673 struct request_queue *q = bdev_get_queue(sb->s_bdev);
674 if (!blk_queue_discard(q))
675 f2fs_msg(sb, KERN_WARNING,
676 "mounting with \"discard\" option, but "
677 "the device does not support discard");
678 }
679
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900680 return 0;
681fail:
682 stop_gc_thread(sbi);
683free_root_inode:
684 dput(sb->s_root);
685 sb->s_root = NULL;
686free_node_inode:
687 iput(sbi->node_inode);
688free_nm:
689 destroy_node_manager(sbi);
690free_sm:
691 destroy_segment_manager(sbi);
692free_cp:
693 kfree(sbi->ckpt);
694free_meta_inode:
695 make_bad_inode(sbi->meta_inode);
696 iput(sbi->meta_inode);
697free_sb_buf:
698 brelse(raw_super_buf);
699free_sbi:
700 kfree(sbi);
701 return err;
702}
703
704static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
705 const char *dev_name, void *data)
706{
707 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
708}
709
710static struct file_system_type f2fs_fs_type = {
711 .owner = THIS_MODULE,
712 .name = "f2fs",
713 .mount = f2fs_mount,
714 .kill_sb = kill_block_super,
715 .fs_flags = FS_REQUIRES_DEV,
716};
717
Namjae Jeon6e6093a2013-01-17 00:08:30 +0900718static int __init init_inodecache(void)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900719{
720 f2fs_inode_cachep = f2fs_kmem_cache_create("f2fs_inode_cache",
721 sizeof(struct f2fs_inode_info), NULL);
722 if (f2fs_inode_cachep == NULL)
723 return -ENOMEM;
724 return 0;
725}
726
727static void destroy_inodecache(void)
728{
729 /*
730 * Make sure all delayed rcu free inodes are flushed before we
731 * destroy cache.
732 */
733 rcu_barrier();
734 kmem_cache_destroy(f2fs_inode_cachep);
735}
736
737static int __init init_f2fs_fs(void)
738{
739 int err;
740
741 err = init_inodecache();
742 if (err)
743 goto fail;
744 err = create_node_manager_caches();
745 if (err)
746 goto fail;
747 err = create_gc_caches();
748 if (err)
749 goto fail;
750 err = create_checkpoint_caches();
751 if (err)
752 goto fail;
Namjae Jeon4589d252013-01-15 19:58:47 +0900753 err = register_filesystem(&f2fs_fs_type);
754 if (err)
755 goto fail;
756 f2fs_create_root_stats();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900757fail:
758 return err;
759}
760
761static void __exit exit_f2fs_fs(void)
762{
Namjae Jeon4589d252013-01-15 19:58:47 +0900763 f2fs_destroy_root_stats();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900764 unregister_filesystem(&f2fs_fs_type);
765 destroy_checkpoint_caches();
766 destroy_gc_caches();
767 destroy_node_manager_caches();
768 destroy_inodecache();
769}
770
771module_init(init_f2fs_fs)
772module_exit(exit_f2fs_fs)
773
774MODULE_AUTHOR("Samsung Electronics's Praesto Team");
775MODULE_DESCRIPTION("Flash Friendly File System");
776MODULE_LICENSE("GPL");