blob: 728c20a8e4568bd71e01fbbd42497e4b58a1b4ef [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>
15#include <linux/proc_fs.h>
16#include <linux/buffer_head.h>
17#include <linux/backing-dev.h>
18#include <linux/kthread.h>
19#include <linux/parser.h>
20#include <linux/mount.h>
21#include <linux/seq_file.h>
22#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>
26
27#include "f2fs.h"
28#include "node.h"
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +090029#include "segment.h"
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090030#include "xattr.h"
31
32static struct kmem_cache *f2fs_inode_cachep;
33
34enum {
35 Opt_gc_background_off,
36 Opt_disable_roll_forward,
37 Opt_discard,
38 Opt_noheap,
39 Opt_nouser_xattr,
40 Opt_noacl,
41 Opt_active_logs,
42 Opt_disable_ext_identify,
43 Opt_err,
44};
45
46static match_table_t f2fs_tokens = {
47 {Opt_gc_background_off, "background_gc_off"},
48 {Opt_disable_roll_forward, "disable_roll_forward"},
49 {Opt_discard, "discard"},
50 {Opt_noheap, "no_heap"},
51 {Opt_nouser_xattr, "nouser_xattr"},
52 {Opt_noacl, "noacl"},
53 {Opt_active_logs, "active_logs=%u"},
54 {Opt_disable_ext_identify, "disable_ext_identify"},
55 {Opt_err, NULL},
56};
57
Namjae Jeona07ef782012-12-30 14:52:05 +090058void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
59{
60 struct va_format vaf;
61 va_list args;
62
63 va_start(args, fmt);
64 vaf.fmt = fmt;
65 vaf.va = &args;
66 printk("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
67 va_end(args);
68}
69
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090070static void init_once(void *foo)
71{
72 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
73
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090074 inode_init_once(&fi->vfs_inode);
75}
76
77static struct inode *f2fs_alloc_inode(struct super_block *sb)
78{
79 struct f2fs_inode_info *fi;
80
81 fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_NOFS | __GFP_ZERO);
82 if (!fi)
83 return NULL;
84
85 init_once((void *) fi);
86
Masanari Iida111d2492013-03-19 08:03:35 +090087 /* Initialize f2fs-specific inode info */
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090088 fi->vfs_inode.i_version = 1;
89 atomic_set(&fi->dirty_dents, 0);
90 fi->i_current_depth = 1;
91 fi->i_advise = 0;
92 rwlock_init(&fi->ext.ext_lock);
93
94 set_inode_flag(fi, FI_NEW_INODE);
95
96 return &fi->vfs_inode;
97}
98
99static void f2fs_i_callback(struct rcu_head *head)
100{
101 struct inode *inode = container_of(head, struct inode, i_rcu);
102 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
103}
104
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900105static void f2fs_destroy_inode(struct inode *inode)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900106{
107 call_rcu(&inode->i_rcu, f2fs_i_callback);
108}
109
110static void f2fs_put_super(struct super_block *sb)
111{
112 struct f2fs_sb_info *sbi = F2FS_SB(sb);
113
114 f2fs_destroy_stats(sbi);
115 stop_gc_thread(sbi);
116
Jaegeuk Kim43727522013-02-04 15:11:17 +0900117 write_checkpoint(sbi, true);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900118
119 iput(sbi->node_inode);
120 iput(sbi->meta_inode);
121
122 /* destroy f2fs internal modules */
123 destroy_node_manager(sbi);
124 destroy_segment_manager(sbi);
125
126 kfree(sbi->ckpt);
127
128 sb->s_fs_info = NULL;
129 brelse(sbi->raw_super_buf);
130 kfree(sbi);
131}
132
133int f2fs_sync_fs(struct super_block *sb, int sync)
134{
135 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900136
137 if (!sbi->s_dirty && !get_pages(sbi, F2FS_DIRTY_NODES))
138 return 0;
139
140 if (sync)
Jaegeuk Kim43727522013-02-04 15:11:17 +0900141 write_checkpoint(sbi, false);
Jaegeuk Kim7d82db82013-01-11 13:10:49 +0900142 else
143 f2fs_balance_fs(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900144
Namjae Jeon64c576f2012-12-22 12:10:27 +0900145 return 0;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900146}
147
Changman Leed6212a52013-01-29 18:30:07 +0900148static int f2fs_freeze(struct super_block *sb)
149{
150 int err;
151
152 if (sb->s_flags & MS_RDONLY)
153 return 0;
154
155 err = f2fs_sync_fs(sb, 1);
156 return err;
157}
158
159static int f2fs_unfreeze(struct super_block *sb)
160{
161 return 0;
162}
163
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900164static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
165{
166 struct super_block *sb = dentry->d_sb;
167 struct f2fs_sb_info *sbi = F2FS_SB(sb);
168 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
169 block_t total_count, user_block_count, start_count, ovp_count;
170
171 total_count = le64_to_cpu(sbi->raw_super->block_count);
172 user_block_count = sbi->user_block_count;
173 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
174 ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg;
175 buf->f_type = F2FS_SUPER_MAGIC;
176 buf->f_bsize = sbi->blocksize;
177
178 buf->f_blocks = total_count - start_count;
179 buf->f_bfree = buf->f_blocks - valid_user_blocks(sbi) - ovp_count;
180 buf->f_bavail = user_block_count - valid_user_blocks(sbi);
181
Jaegeuk Kim1362b5e2012-12-12 19:45:49 +0900182 buf->f_files = sbi->total_node_count;
183 buf->f_ffree = sbi->total_node_count - valid_inode_count(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900184
Jaegeuk Kim5a20d332013-03-03 13:58:05 +0900185 buf->f_namelen = F2FS_NAME_LEN;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900186 buf->f_fsid.val[0] = (u32)id;
187 buf->f_fsid.val[1] = (u32)(id >> 32);
188
189 return 0;
190}
191
192static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
193{
194 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
195
196 if (test_opt(sbi, BG_GC))
197 seq_puts(seq, ",background_gc_on");
198 else
199 seq_puts(seq, ",background_gc_off");
200 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
201 seq_puts(seq, ",disable_roll_forward");
202 if (test_opt(sbi, DISCARD))
203 seq_puts(seq, ",discard");
204 if (test_opt(sbi, NOHEAP))
205 seq_puts(seq, ",no_heap_alloc");
206#ifdef CONFIG_F2FS_FS_XATTR
207 if (test_opt(sbi, XATTR_USER))
208 seq_puts(seq, ",user_xattr");
209 else
210 seq_puts(seq, ",nouser_xattr");
211#endif
212#ifdef CONFIG_F2FS_FS_POSIX_ACL
213 if (test_opt(sbi, POSIX_ACL))
214 seq_puts(seq, ",acl");
215 else
216 seq_puts(seq, ",noacl");
217#endif
218 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
Alejandro Martinez Ruizaa435072013-01-25 19:08:59 +0100219 seq_puts(seq, ",disable_ext_identify");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900220
221 seq_printf(seq, ",active_logs=%u", sbi->active_logs);
222
223 return 0;
224}
225
226static struct super_operations f2fs_sops = {
227 .alloc_inode = f2fs_alloc_inode,
228 .destroy_inode = f2fs_destroy_inode,
229 .write_inode = f2fs_write_inode,
230 .show_options = f2fs_show_options,
231 .evict_inode = f2fs_evict_inode,
232 .put_super = f2fs_put_super,
233 .sync_fs = f2fs_sync_fs,
Changman Leed6212a52013-01-29 18:30:07 +0900234 .freeze_fs = f2fs_freeze,
235 .unfreeze_fs = f2fs_unfreeze,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900236 .statfs = f2fs_statfs,
237};
238
239static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
240 u64 ino, u32 generation)
241{
242 struct f2fs_sb_info *sbi = F2FS_SB(sb);
243 struct inode *inode;
244
245 if (ino < F2FS_ROOT_INO(sbi))
246 return ERR_PTR(-ESTALE);
247
248 /*
249 * f2fs_iget isn't quite right if the inode is currently unallocated!
250 * However f2fs_iget currently does appropriate checks to handle stale
251 * inodes so everything is OK.
252 */
253 inode = f2fs_iget(sb, ino);
254 if (IS_ERR(inode))
255 return ERR_CAST(inode);
256 if (generation && inode->i_generation != generation) {
257 /* we didn't find the right inode.. */
258 iput(inode);
259 return ERR_PTR(-ESTALE);
260 }
261 return inode;
262}
263
264static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
265 int fh_len, int fh_type)
266{
267 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
268 f2fs_nfs_get_inode);
269}
270
271static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
272 int fh_len, int fh_type)
273{
274 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
275 f2fs_nfs_get_inode);
276}
277
278static const struct export_operations f2fs_export_ops = {
279 .fh_to_dentry = f2fs_fh_to_dentry,
280 .fh_to_parent = f2fs_fh_to_parent,
281 .get_parent = f2fs_get_parent,
282};
283
Namjae Jeona07ef782012-12-30 14:52:05 +0900284static int parse_options(struct super_block *sb, struct f2fs_sb_info *sbi,
285 char *options)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900286{
287 substring_t args[MAX_OPT_ARGS];
288 char *p;
289 int arg = 0;
290
291 if (!options)
292 return 0;
293
294 while ((p = strsep(&options, ",")) != NULL) {
295 int token;
296 if (!*p)
297 continue;
298 /*
299 * Initialize args struct so we know whether arg was
300 * found; some options take optional arguments.
301 */
302 args[0].to = args[0].from = NULL;
303 token = match_token(p, f2fs_tokens, args);
304
305 switch (token) {
306 case Opt_gc_background_off:
307 clear_opt(sbi, BG_GC);
308 break;
309 case Opt_disable_roll_forward:
310 set_opt(sbi, DISABLE_ROLL_FORWARD);
311 break;
312 case Opt_discard:
313 set_opt(sbi, DISCARD);
314 break;
315 case Opt_noheap:
316 set_opt(sbi, NOHEAP);
317 break;
318#ifdef CONFIG_F2FS_FS_XATTR
319 case Opt_nouser_xattr:
320 clear_opt(sbi, XATTR_USER);
321 break;
322#else
323 case Opt_nouser_xattr:
Namjae Jeona07ef782012-12-30 14:52:05 +0900324 f2fs_msg(sb, KERN_INFO,
325 "nouser_xattr options not supported");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900326 break;
327#endif
328#ifdef CONFIG_F2FS_FS_POSIX_ACL
329 case Opt_noacl:
330 clear_opt(sbi, POSIX_ACL);
331 break;
332#else
333 case Opt_noacl:
Namjae Jeona07ef782012-12-30 14:52:05 +0900334 f2fs_msg(sb, KERN_INFO, "noacl options not supported");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900335 break;
336#endif
337 case Opt_active_logs:
338 if (args->from && match_int(args, &arg))
339 return -EINVAL;
Jaegeuk Kim12a67142012-12-21 11:47:05 +0900340 if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900341 return -EINVAL;
342 sbi->active_logs = arg;
343 break;
344 case Opt_disable_ext_identify:
345 set_opt(sbi, DISABLE_EXT_IDENTIFY);
346 break;
347 default:
Namjae Jeona07ef782012-12-30 14:52:05 +0900348 f2fs_msg(sb, KERN_ERR,
349 "Unrecognized mount option \"%s\" or missing value",
350 p);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900351 return -EINVAL;
352 }
353 }
354 return 0;
355}
356
357static loff_t max_file_size(unsigned bits)
358{
359 loff_t result = ADDRS_PER_INODE;
360 loff_t leaf_count = ADDRS_PER_BLOCK;
361
362 /* two direct node blocks */
363 result += (leaf_count * 2);
364
365 /* two indirect node blocks */
366 leaf_count *= NIDS_PER_BLOCK;
367 result += (leaf_count * 2);
368
369 /* one double indirect node block */
370 leaf_count *= NIDS_PER_BLOCK;
371 result += leaf_count;
372
373 result <<= bits;
374 return result;
375}
376
Namjae Jeona07ef782012-12-30 14:52:05 +0900377static int sanity_check_raw_super(struct super_block *sb,
378 struct f2fs_super_block *raw_super)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900379{
380 unsigned int blocksize;
381
Namjae Jeona07ef782012-12-30 14:52:05 +0900382 if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
383 f2fs_msg(sb, KERN_INFO,
384 "Magic Mismatch, valid(0x%x) - read(0x%x)",
385 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900386 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900387 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900388
majianpeng5c9b4692013-02-01 19:07:57 +0800389 /* Currently, support only 4KB page cache size */
390 if (F2FS_BLKSIZE != PAGE_CACHE_SIZE) {
391 f2fs_msg(sb, KERN_INFO,
majianpeng14d7e9d2013-02-01 19:07:03 +0800392 "Invalid page_cache_size (%lu), supports only 4KB\n",
majianpeng5c9b4692013-02-01 19:07:57 +0800393 PAGE_CACHE_SIZE);
394 return 1;
395 }
396
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900397 /* Currently, support only 4KB block size */
398 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
majianpeng5c9b4692013-02-01 19:07:57 +0800399 if (blocksize != F2FS_BLKSIZE) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900400 f2fs_msg(sb, KERN_INFO,
401 "Invalid blocksize (%u), supports only 4KB\n",
402 blocksize);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900403 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900404 }
majianpeng5c9b4692013-02-01 19:07:57 +0800405
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900406 if (le32_to_cpu(raw_super->log_sectorsize) !=
Namjae Jeona07ef782012-12-30 14:52:05 +0900407 F2FS_LOG_SECTOR_SIZE) {
408 f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900409 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900410 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900411 if (le32_to_cpu(raw_super->log_sectors_per_block) !=
Namjae Jeona07ef782012-12-30 14:52:05 +0900412 F2FS_LOG_SECTORS_PER_BLOCK) {
413 f2fs_msg(sb, KERN_INFO, "Invalid log sectors per block");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900414 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900415 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900416 return 0;
417}
418
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900419static int sanity_check_ckpt(struct f2fs_sb_info *sbi)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900420{
421 unsigned int total, fsmeta;
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900422 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
423 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900424
425 total = le32_to_cpu(raw_super->segment_count);
426 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
427 fsmeta += le32_to_cpu(raw_super->segment_count_sit);
428 fsmeta += le32_to_cpu(raw_super->segment_count_nat);
429 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
430 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
431
432 if (fsmeta >= total)
433 return 1;
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900434
435 if (is_set_ckpt_flags(ckpt, CP_ERROR_FLAG)) {
436 f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
437 return 1;
438 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900439 return 0;
440}
441
442static void init_sb_info(struct f2fs_sb_info *sbi)
443{
444 struct f2fs_super_block *raw_super = sbi->raw_super;
445 int i;
446
447 sbi->log_sectors_per_block =
448 le32_to_cpu(raw_super->log_sectors_per_block);
449 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
450 sbi->blocksize = 1 << sbi->log_blocksize;
451 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
452 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
453 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
454 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
455 sbi->total_sections = le32_to_cpu(raw_super->section_count);
456 sbi->total_node_count =
457 (le32_to_cpu(raw_super->segment_count_nat) / 2)
458 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
459 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
460 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
461 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900462 sbi->cur_victim_sec = NULL_SECNO;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900463
464 for (i = 0; i < NR_COUNT_TYPE; i++)
465 atomic_set(&sbi->nr_pages[i], 0);
466}
467
majianpeng14d7e9d2013-02-01 19:07:03 +0800468static int validate_superblock(struct super_block *sb,
469 struct f2fs_super_block **raw_super,
470 struct buffer_head **raw_super_buf, sector_t block)
471{
472 const char *super = (block == 0 ? "first" : "second");
473
474 /* read f2fs raw super block */
475 *raw_super_buf = sb_bread(sb, block);
476 if (!*raw_super_buf) {
477 f2fs_msg(sb, KERN_ERR, "unable to read %s superblock",
478 super);
Namjae Jeonc0d39e62013-03-17 17:26:53 +0900479 return -EIO;
majianpeng14d7e9d2013-02-01 19:07:03 +0800480 }
481
482 *raw_super = (struct f2fs_super_block *)
483 ((char *)(*raw_super_buf)->b_data + F2FS_SUPER_OFFSET);
484
485 /* sanity checking of raw super */
486 if (!sanity_check_raw_super(sb, *raw_super))
487 return 0;
488
489 f2fs_msg(sb, KERN_ERR, "Can't find a valid F2FS filesystem "
490 "in %s superblock", super);
Namjae Jeonc0d39e62013-03-17 17:26:53 +0900491 return -EINVAL;
majianpeng14d7e9d2013-02-01 19:07:03 +0800492}
493
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900494static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
495{
496 struct f2fs_sb_info *sbi;
497 struct f2fs_super_block *raw_super;
498 struct buffer_head *raw_super_buf;
499 struct inode *root;
500 long err = -EINVAL;
501 int i;
502
503 /* allocate memory for f2fs-specific super block info */
504 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
505 if (!sbi)
506 return -ENOMEM;
507
Namjae Jeonff9234a2013-01-12 14:41:13 +0900508 /* set a block size */
Namjae Jeona07ef782012-12-30 14:52:05 +0900509 if (!sb_set_blocksize(sb, F2FS_BLKSIZE)) {
510 f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900511 goto free_sbi;
Namjae Jeona07ef782012-12-30 14:52:05 +0900512 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900513
Namjae Jeonc0d39e62013-03-17 17:26:53 +0900514 err = validate_superblock(sb, &raw_super, &raw_super_buf, 0);
515 if (err) {
majianpeng14d7e9d2013-02-01 19:07:03 +0800516 brelse(raw_super_buf);
Namjae Jeonc0d39e62013-03-17 17:26:53 +0900517 /* check secondary superblock when primary failed */
518 err = validate_superblock(sb, &raw_super, &raw_super_buf, 1);
519 if (err)
majianpeng14d7e9d2013-02-01 19:07:03 +0800520 goto free_sb_buf;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900521 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900522 /* init some FS parameters */
523 sbi->active_logs = NR_CURSEG_TYPE;
524
525 set_opt(sbi, BG_GC);
526
527#ifdef CONFIG_F2FS_FS_XATTR
528 set_opt(sbi, XATTR_USER);
529#endif
530#ifdef CONFIG_F2FS_FS_POSIX_ACL
531 set_opt(sbi, POSIX_ACL);
532#endif
533 /* parse mount options */
Namjae Jeona07ef782012-12-30 14:52:05 +0900534 if (parse_options(sb, sbi, (char *)data))
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900535 goto free_sb_buf;
536
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900537 sb->s_maxbytes = max_file_size(le32_to_cpu(raw_super->log_blocksize));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900538 sb->s_max_links = F2FS_LINK_MAX;
539 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
540
541 sb->s_op = &f2fs_sops;
542 sb->s_xattr = f2fs_xattr_handlers;
543 sb->s_export_op = &f2fs_export_ops;
544 sb->s_magic = F2FS_SUPER_MAGIC;
545 sb->s_fs_info = sbi;
546 sb->s_time_gran = 1;
547 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
548 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
549 memcpy(sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
550
551 /* init f2fs-specific super block info */
552 sbi->sb = sb;
553 sbi->raw_super = raw_super;
554 sbi->raw_super_buf = raw_super_buf;
555 mutex_init(&sbi->gc_mutex);
556 mutex_init(&sbi->write_inode);
557 mutex_init(&sbi->writepages);
558 mutex_init(&sbi->cp_mutex);
559 for (i = 0; i < NR_LOCK_TYPE; i++)
560 mutex_init(&sbi->fs_lock[i]);
561 sbi->por_doing = 0;
562 spin_lock_init(&sbi->stat_lock);
563 init_rwsem(&sbi->bio_sem);
564 init_sb_info(sbi);
565
566 /* get an inode for meta space */
567 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
568 if (IS_ERR(sbi->meta_inode)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900569 f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900570 err = PTR_ERR(sbi->meta_inode);
571 goto free_sb_buf;
572 }
573
574 err = get_valid_checkpoint(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +0900575 if (err) {
576 f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900577 goto free_meta_inode;
Namjae Jeona07ef782012-12-30 14:52:05 +0900578 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900579
580 /* sanity checking of checkpoint */
581 err = -EINVAL;
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900582 if (sanity_check_ckpt(sbi)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900583 f2fs_msg(sb, KERN_ERR, "Invalid F2FS checkpoint");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900584 goto free_cp;
Namjae Jeona07ef782012-12-30 14:52:05 +0900585 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900586
587 sbi->total_valid_node_count =
588 le32_to_cpu(sbi->ckpt->valid_node_count);
589 sbi->total_valid_inode_count =
590 le32_to_cpu(sbi->ckpt->valid_inode_count);
591 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
592 sbi->total_valid_block_count =
593 le64_to_cpu(sbi->ckpt->valid_block_count);
594 sbi->last_valid_block_count = sbi->total_valid_block_count;
595 sbi->alloc_valid_block_count = 0;
596 INIT_LIST_HEAD(&sbi->dir_inode_list);
597 spin_lock_init(&sbi->dir_inode_lock);
598
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900599 init_orphan_info(sbi);
600
601 /* setup f2fs internal modules */
602 err = build_segment_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +0900603 if (err) {
604 f2fs_msg(sb, KERN_ERR,
605 "Failed to initialize F2FS segment manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900606 goto free_sm;
Namjae Jeona07ef782012-12-30 14:52:05 +0900607 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900608 err = build_node_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +0900609 if (err) {
610 f2fs_msg(sb, KERN_ERR,
611 "Failed to initialize F2FS node manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900612 goto free_nm;
Namjae Jeona07ef782012-12-30 14:52:05 +0900613 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900614
615 build_gc_manager(sbi);
616
617 /* get an inode for node space */
618 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
619 if (IS_ERR(sbi->node_inode)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900620 f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900621 err = PTR_ERR(sbi->node_inode);
622 goto free_nm;
623 }
624
625 /* if there are nt orphan nodes free them */
626 err = -EINVAL;
Jaegeuk Kim30f0c752012-12-19 16:09:19 +0900627 if (recover_orphan_inodes(sbi))
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900628 goto free_node_inode;
629
630 /* read root inode and dentry */
631 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
632 if (IS_ERR(root)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900633 f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900634 err = PTR_ERR(root);
635 goto free_node_inode;
636 }
637 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size)
638 goto free_root_inode;
639
640 sb->s_root = d_make_root(root); /* allocate root dentry */
641 if (!sb->s_root) {
642 err = -ENOMEM;
643 goto free_root_inode;
644 }
645
646 /* recover fsynced data */
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900647 if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
648 err = recover_fsync_data(sbi);
649 if (err) {
650 f2fs_msg(sb, KERN_ERR, "Failed to recover fsync data");
651 goto free_root_inode;
652 }
653 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900654
655 /* After POR, we can run background GC thread */
656 err = start_gc_thread(sbi);
657 if (err)
658 goto fail;
659
660 err = f2fs_build_stats(sbi);
661 if (err)
662 goto fail;
663
Namjae Jeond3ee4562013-03-17 17:26:14 +0900664 if (test_opt(sbi, DISCARD)) {
665 struct request_queue *q = bdev_get_queue(sb->s_bdev);
666 if (!blk_queue_discard(q))
667 f2fs_msg(sb, KERN_WARNING,
668 "mounting with \"discard\" option, but "
669 "the device does not support discard");
670 }
671
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900672 return 0;
673fail:
674 stop_gc_thread(sbi);
675free_root_inode:
676 dput(sb->s_root);
677 sb->s_root = NULL;
678free_node_inode:
679 iput(sbi->node_inode);
680free_nm:
681 destroy_node_manager(sbi);
682free_sm:
683 destroy_segment_manager(sbi);
684free_cp:
685 kfree(sbi->ckpt);
686free_meta_inode:
687 make_bad_inode(sbi->meta_inode);
688 iput(sbi->meta_inode);
689free_sb_buf:
690 brelse(raw_super_buf);
691free_sbi:
692 kfree(sbi);
693 return err;
694}
695
696static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
697 const char *dev_name, void *data)
698{
699 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
700}
701
702static struct file_system_type f2fs_fs_type = {
703 .owner = THIS_MODULE,
704 .name = "f2fs",
705 .mount = f2fs_mount,
706 .kill_sb = kill_block_super,
707 .fs_flags = FS_REQUIRES_DEV,
708};
709
Namjae Jeon6e6093a2013-01-17 00:08:30 +0900710static int __init init_inodecache(void)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900711{
712 f2fs_inode_cachep = f2fs_kmem_cache_create("f2fs_inode_cache",
713 sizeof(struct f2fs_inode_info), NULL);
714 if (f2fs_inode_cachep == NULL)
715 return -ENOMEM;
716 return 0;
717}
718
719static void destroy_inodecache(void)
720{
721 /*
722 * Make sure all delayed rcu free inodes are flushed before we
723 * destroy cache.
724 */
725 rcu_barrier();
726 kmem_cache_destroy(f2fs_inode_cachep);
727}
728
729static int __init init_f2fs_fs(void)
730{
731 int err;
732
733 err = init_inodecache();
734 if (err)
735 goto fail;
736 err = create_node_manager_caches();
737 if (err)
738 goto fail;
739 err = create_gc_caches();
740 if (err)
741 goto fail;
742 err = create_checkpoint_caches();
743 if (err)
744 goto fail;
Namjae Jeon4589d252013-01-15 19:58:47 +0900745 err = register_filesystem(&f2fs_fs_type);
746 if (err)
747 goto fail;
748 f2fs_create_root_stats();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900749fail:
750 return err;
751}
752
753static void __exit exit_f2fs_fs(void)
754{
Namjae Jeon4589d252013-01-15 19:58:47 +0900755 f2fs_destroy_root_stats();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900756 unregister_filesystem(&f2fs_fs_type);
757 destroy_checkpoint_caches();
758 destroy_gc_caches();
759 destroy_node_manager_caches();
760 destroy_inodecache();
761}
762
763module_init(init_f2fs_fs)
764module_exit(exit_f2fs_fs)
765
766MODULE_AUTHOR("Samsung Electronics's Praesto Team");
767MODULE_DESCRIPTION("Flash Friendly File System");
768MODULE_LICENSE("GPL");